memory: move mem_path handling to memory_region_allocate_system_memory
[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 #define DIRTY_MEMORY_VGA 0
20 #define DIRTY_MEMORY_CODE 1
21 #define DIRTY_MEMORY_MIGRATION 2
22 #define DIRTY_MEMORY_NUM 3 /* num of dirty bits */
23
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include "qemu-common.h"
27 #include "exec/cpu-common.h"
28 #ifndef CONFIG_USER_ONLY
29 #include "exec/hwaddr.h"
30 #endif
31 #include "qemu/queue.h"
32 #include "qemu/int128.h"
33 #include "qemu/notify.h"
34
35 #define MAX_PHYS_ADDR_SPACE_BITS 62
36 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
37
38 typedef struct MemoryRegionOps MemoryRegionOps;
39 typedef struct MemoryRegionMmio MemoryRegionMmio;
40
41 struct MemoryRegionMmio {
42 CPUReadMemoryFunc *read[3];
43 CPUWriteMemoryFunc *write[3];
44 };
45
46 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
47
48 /* See address_space_translate: bit 0 is read, bit 1 is write. */
49 typedef enum {
50 IOMMU_NONE = 0,
51 IOMMU_RO = 1,
52 IOMMU_WO = 2,
53 IOMMU_RW = 3,
54 } IOMMUAccessFlags;
55
56 struct IOMMUTLBEntry {
57 AddressSpace *target_as;
58 hwaddr iova;
59 hwaddr translated_addr;
60 hwaddr addr_mask; /* 0xfff = 4k translation */
61 IOMMUAccessFlags perm;
62 };
63
64 /*
65 * Memory region callbacks
66 */
67 struct MemoryRegionOps {
68 /* Read from the memory region. @addr is relative to @mr; @size is
69 * in bytes. */
70 uint64_t (*read)(void *opaque,
71 hwaddr addr,
72 unsigned size);
73 /* Write to the memory region. @addr is relative to @mr; @size is
74 * in bytes. */
75 void (*write)(void *opaque,
76 hwaddr addr,
77 uint64_t data,
78 unsigned size);
79
80 enum device_endian endianness;
81 /* Guest-visible constraints: */
82 struct {
83 /* If nonzero, specify bounds on access sizes beyond which a machine
84 * check is thrown.
85 */
86 unsigned min_access_size;
87 unsigned max_access_size;
88 /* If true, unaligned accesses are supported. Otherwise unaligned
89 * accesses throw machine checks.
90 */
91 bool unaligned;
92 /*
93 * If present, and returns #false, the transaction is not accepted
94 * by the device (and results in machine dependent behaviour such
95 * as a machine check exception).
96 */
97 bool (*accepts)(void *opaque, hwaddr addr,
98 unsigned size, bool is_write);
99 } valid;
100 /* Internal implementation constraints: */
101 struct {
102 /* If nonzero, specifies the minimum size implemented. Smaller sizes
103 * will be rounded upwards and a partial result will be returned.
104 */
105 unsigned min_access_size;
106 /* If nonzero, specifies the maximum size implemented. Larger sizes
107 * will be done as a series of accesses with smaller sizes.
108 */
109 unsigned max_access_size;
110 /* If true, unaligned accesses are supported. Otherwise all accesses
111 * are converted to (possibly multiple) naturally aligned accesses.
112 */
113 bool unaligned;
114 } impl;
115
116 /* If .read and .write are not present, old_mmio may be used for
117 * backwards compatibility with old mmio registration
118 */
119 const MemoryRegionMmio old_mmio;
120 };
121
122 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
123
124 struct MemoryRegionIOMMUOps {
125 /* Return a TLB entry that contains a given address. */
126 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr);
127 };
128
129 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
130 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
131
132 struct MemoryRegion {
133 /* All fields are private - violators will be prosecuted */
134 const MemoryRegionOps *ops;
135 const MemoryRegionIOMMUOps *iommu_ops;
136 void *opaque;
137 struct Object *owner;
138 MemoryRegion *container;
139 Int128 size;
140 hwaddr addr;
141 void (*destructor)(MemoryRegion *mr);
142 ram_addr_t ram_addr;
143 bool subpage;
144 bool terminates;
145 bool romd_mode;
146 bool ram;
147 bool readonly; /* For RAM regions */
148 bool enabled;
149 bool rom_device;
150 bool warning_printed; /* For reservations */
151 bool flush_coalesced_mmio;
152 MemoryRegion *alias;
153 hwaddr alias_offset;
154 int priority;
155 bool may_overlap;
156 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
157 QTAILQ_ENTRY(MemoryRegion) subregions_link;
158 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
159 const char *name;
160 uint8_t dirty_log_mask;
161 unsigned ioeventfd_nb;
162 MemoryRegionIoeventfd *ioeventfds;
163 NotifierList iommu_notify;
164 };
165
166 /**
167 * MemoryListener: callbacks structure for updates to the physical memory map
168 *
169 * Allows a component to adjust to changes in the guest-visible memory map.
170 * Use with memory_listener_register() and memory_listener_unregister().
171 */
172 struct MemoryListener {
173 void (*begin)(MemoryListener *listener);
174 void (*commit)(MemoryListener *listener);
175 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
176 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
177 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
178 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section);
179 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section);
180 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
181 void (*log_global_start)(MemoryListener *listener);
182 void (*log_global_stop)(MemoryListener *listener);
183 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
184 bool match_data, uint64_t data, EventNotifier *e);
185 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
186 bool match_data, uint64_t data, EventNotifier *e);
187 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
188 hwaddr addr, hwaddr len);
189 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
190 hwaddr addr, hwaddr len);
191 /* Lower = earlier (during add), later (during del) */
192 unsigned priority;
193 AddressSpace *address_space_filter;
194 QTAILQ_ENTRY(MemoryListener) link;
195 };
196
197 /**
198 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
199 */
200 struct AddressSpace {
201 /* All fields are private. */
202 char *name;
203 MemoryRegion *root;
204 struct FlatView *current_map;
205 int ioeventfd_nb;
206 struct MemoryRegionIoeventfd *ioeventfds;
207 struct AddressSpaceDispatch *dispatch;
208 struct AddressSpaceDispatch *next_dispatch;
209 MemoryListener dispatch_listener;
210
211 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
212 };
213
214 /**
215 * MemoryRegionSection: describes a fragment of a #MemoryRegion
216 *
217 * @mr: the region, or %NULL if empty
218 * @address_space: the address space the region is mapped in
219 * @offset_within_region: the beginning of the section, relative to @mr's start
220 * @size: the size of the section; will not exceed @mr's boundaries
221 * @offset_within_address_space: the address of the first byte of the section
222 * relative to the region's address space
223 * @readonly: writes to this section are ignored
224 */
225 struct MemoryRegionSection {
226 MemoryRegion *mr;
227 AddressSpace *address_space;
228 hwaddr offset_within_region;
229 Int128 size;
230 hwaddr offset_within_address_space;
231 bool readonly;
232 };
233
234 /**
235 * memory_region_init: Initialize a memory region
236 *
237 * The region typically acts as a container for other memory regions. Use
238 * memory_region_add_subregion() to add subregions.
239 *
240 * @mr: the #MemoryRegion to be initialized
241 * @owner: the object that tracks the region's reference count
242 * @name: used for debugging; not visible to the user or ABI
243 * @size: size of the region; any subregions beyond this size will be clipped
244 */
245 void memory_region_init(MemoryRegion *mr,
246 struct Object *owner,
247 const char *name,
248 uint64_t size);
249
250 /**
251 * memory_region_ref: Add 1 to a memory region's reference count
252 *
253 * Whenever memory regions are accessed outside the BQL, they need to be
254 * preserved against hot-unplug. MemoryRegions actually do not have their
255 * own reference count; they piggyback on a QOM object, their "owner".
256 * This function adds a reference to the owner.
257 *
258 * All MemoryRegions must have an owner if they can disappear, even if the
259 * device they belong to operates exclusively under the BQL. This is because
260 * the region could be returned at any time by memory_region_find, and this
261 * is usually under guest control.
262 *
263 * @mr: the #MemoryRegion
264 */
265 void memory_region_ref(MemoryRegion *mr);
266
267 /**
268 * memory_region_unref: Remove 1 to a memory region's reference count
269 *
270 * Whenever memory regions are accessed outside the BQL, they need to be
271 * preserved against hot-unplug. MemoryRegions actually do not have their
272 * own reference count; they piggyback on a QOM object, their "owner".
273 * This function removes a reference to the owner and possibly destroys it.
274 *
275 * @mr: the #MemoryRegion
276 */
277 void memory_region_unref(MemoryRegion *mr);
278
279 /**
280 * memory_region_init_io: Initialize an I/O memory region.
281 *
282 * Accesses into the region will cause the callbacks in @ops to be called.
283 * if @size is nonzero, subregions will be clipped to @size.
284 *
285 * @mr: the #MemoryRegion to be initialized.
286 * @owner: the object that tracks the region's reference count
287 * @ops: a structure containing read and write callbacks to be used when
288 * I/O is performed on the region.
289 * @opaque: passed to to the read and write callbacks of the @ops structure.
290 * @name: used for debugging; not visible to the user or ABI
291 * @size: size of the region.
292 */
293 void memory_region_init_io(MemoryRegion *mr,
294 struct Object *owner,
295 const MemoryRegionOps *ops,
296 void *opaque,
297 const char *name,
298 uint64_t size);
299
300 /**
301 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
302 * region will modify memory directly.
303 *
304 * @mr: the #MemoryRegion to be initialized.
305 * @owner: the object that tracks the region's reference count
306 * @name: the name of the region.
307 * @size: size of the region.
308 */
309 void memory_region_init_ram(MemoryRegion *mr,
310 struct Object *owner,
311 const char *name,
312 uint64_t size);
313
314 #ifdef __linux__
315 /**
316 * memory_region_init_ram_from_file: Initialize RAM memory region with a
317 * mmap-ed backend.
318 *
319 * @mr: the #MemoryRegion to be initialized.
320 * @owner: the object that tracks the region's reference count
321 * @name: the name of the region.
322 * @size: size of the region.
323 * @path: the path in which to allocate the RAM.
324 */
325 void memory_region_init_ram_from_file(MemoryRegion *mr,
326 struct Object *owner,
327 const char *name,
328 uint64_t size,
329 const char *path);
330 #endif
331
332 /**
333 * memory_region_init_ram_ptr: Initialize RAM memory region from a
334 * user-provided pointer. Accesses into the
335 * region will modify memory directly.
336 *
337 * @mr: the #MemoryRegion to be initialized.
338 * @owner: the object that tracks the region's reference count
339 * @name: the name of the region.
340 * @size: size of the region.
341 * @ptr: memory to be mapped; must contain at least @size bytes.
342 */
343 void memory_region_init_ram_ptr(MemoryRegion *mr,
344 struct Object *owner,
345 const char *name,
346 uint64_t size,
347 void *ptr);
348
349 /**
350 * memory_region_init_alias: Initialize a memory region that aliases all or a
351 * part of another memory region.
352 *
353 * @mr: the #MemoryRegion to be initialized.
354 * @owner: the object that tracks the region's reference count
355 * @name: used for debugging; not visible to the user or ABI
356 * @orig: the region to be referenced; @mr will be equivalent to
357 * @orig between @offset and @offset + @size - 1.
358 * @offset: start of the section in @orig to be referenced.
359 * @size: size of the region.
360 */
361 void memory_region_init_alias(MemoryRegion *mr,
362 struct Object *owner,
363 const char *name,
364 MemoryRegion *orig,
365 hwaddr offset,
366 uint64_t size);
367
368 /**
369 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
370 * handled via callbacks.
371 *
372 * @mr: the #MemoryRegion to be initialized.
373 * @owner: the object that tracks the region's reference count
374 * @ops: callbacks for write access handling.
375 * @name: the name of the region.
376 * @size: size of the region.
377 */
378 void memory_region_init_rom_device(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_reservation: Initialize a memory region that reserves
387 * I/O space.
388 *
389 * A reservation region primariy serves debugging purposes. It claims I/O
390 * space that is not supposed to be handled by QEMU itself. Any access via
391 * the memory API will cause an abort().
392 *
393 * @mr: the #MemoryRegion to be initialized
394 * @owner: the object that tracks the region's reference count
395 * @name: used for debugging; not visible to the user or ABI
396 * @size: size of the region.
397 */
398 void memory_region_init_reservation(MemoryRegion *mr,
399 struct Object *owner,
400 const char *name,
401 uint64_t size);
402
403 /**
404 * memory_region_init_iommu: Initialize a memory region that translates
405 * addresses
406 *
407 * An IOMMU region translates addresses and forwards accesses to a target
408 * memory region.
409 *
410 * @mr: the #MemoryRegion to be initialized
411 * @owner: the object that tracks the region's reference count
412 * @ops: a function that translates addresses into the @target region
413 * @name: used for debugging; not visible to the user or ABI
414 * @size: size of the region.
415 */
416 void memory_region_init_iommu(MemoryRegion *mr,
417 struct Object *owner,
418 const MemoryRegionIOMMUOps *ops,
419 const char *name,
420 uint64_t size);
421
422 /**
423 * memory_region_destroy: Destroy a memory region and reclaim all resources.
424 *
425 * @mr: the region to be destroyed. May not currently be a subregion
426 * (see memory_region_add_subregion()) or referenced in an alias
427 * (see memory_region_init_alias()).
428 */
429 void memory_region_destroy(MemoryRegion *mr);
430
431 /**
432 * memory_region_owner: get a memory region's owner.
433 *
434 * @mr: the memory region being queried.
435 */
436 struct Object *memory_region_owner(MemoryRegion *mr);
437
438 /**
439 * memory_region_size: get a memory region's size.
440 *
441 * @mr: the memory region being queried.
442 */
443 uint64_t memory_region_size(MemoryRegion *mr);
444
445 /**
446 * memory_region_is_ram: check whether a memory region is random access
447 *
448 * Returns %true is a memory region is random access.
449 *
450 * @mr: the memory region being queried
451 */
452 bool memory_region_is_ram(MemoryRegion *mr);
453
454 /**
455 * memory_region_is_romd: check whether a memory region is in ROMD mode
456 *
457 * Returns %true if a memory region is a ROM device and currently set to allow
458 * direct reads.
459 *
460 * @mr: the memory region being queried
461 */
462 static inline bool memory_region_is_romd(MemoryRegion *mr)
463 {
464 return mr->rom_device && mr->romd_mode;
465 }
466
467 /**
468 * memory_region_is_iommu: check whether a memory region is an iommu
469 *
470 * Returns %true is a memory region is an iommu.
471 *
472 * @mr: the memory region being queried
473 */
474 bool memory_region_is_iommu(MemoryRegion *mr);
475
476 /**
477 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
478 *
479 * @mr: the memory region that was changed
480 * @entry: the new entry in the IOMMU translation table. The entry
481 * replaces all old entries for the same virtual I/O address range.
482 * Deleted entries have .@perm == 0.
483 */
484 void memory_region_notify_iommu(MemoryRegion *mr,
485 IOMMUTLBEntry entry);
486
487 /**
488 * memory_region_register_iommu_notifier: register a notifier for changes to
489 * IOMMU translation entries.
490 *
491 * @mr: the memory region to observe
492 * @n: the notifier to be added; the notifier receives a pointer to an
493 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
494 * valid on exit from the notifier.
495 */
496 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
497
498 /**
499 * memory_region_unregister_iommu_notifier: unregister a notifier for
500 * changes to IOMMU translation entries.
501 *
502 * @n: the notifier to be removed.
503 */
504 void memory_region_unregister_iommu_notifier(Notifier *n);
505
506 /**
507 * memory_region_name: get a memory region's name
508 *
509 * Returns the string that was used to initialize the memory region.
510 *
511 * @mr: the memory region being queried
512 */
513 const char *memory_region_name(MemoryRegion *mr);
514
515 /**
516 * memory_region_is_logging: return whether a memory region is logging writes
517 *
518 * Returns %true if the memory region is logging writes
519 *
520 * @mr: the memory region being queried
521 */
522 bool memory_region_is_logging(MemoryRegion *mr);
523
524 /**
525 * memory_region_is_rom: check whether a memory region is ROM
526 *
527 * Returns %true is a memory region is read-only memory.
528 *
529 * @mr: the memory region being queried
530 */
531 bool memory_region_is_rom(MemoryRegion *mr);
532
533 /**
534 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
535 *
536 * Returns a host pointer to a RAM memory region (created with
537 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
538 * care.
539 *
540 * @mr: the memory region being queried.
541 */
542 void *memory_region_get_ram_ptr(MemoryRegion *mr);
543
544 /**
545 * memory_region_set_log: Turn dirty logging on or off for a region.
546 *
547 * Turns dirty logging on or off for a specified client (display, migration).
548 * Only meaningful for RAM regions.
549 *
550 * @mr: the memory region being updated.
551 * @log: whether dirty logging is to be enabled or disabled.
552 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
553 * %DIRTY_MEMORY_VGA.
554 */
555 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
556
557 /**
558 * memory_region_get_dirty: Check whether a range of bytes is dirty
559 * for a specified client.
560 *
561 * Checks whether a range of bytes has been written to since the last
562 * call to memory_region_reset_dirty() with the same @client. Dirty logging
563 * must be enabled.
564 *
565 * @mr: the memory region being queried.
566 * @addr: the address (relative to the start of the region) being queried.
567 * @size: the size of the range being queried.
568 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
569 * %DIRTY_MEMORY_VGA.
570 */
571 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
572 hwaddr size, unsigned client);
573
574 /**
575 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
576 *
577 * Marks a range of bytes as dirty, after it has been dirtied outside
578 * guest code.
579 *
580 * @mr: the memory region being dirtied.
581 * @addr: the address (relative to the start of the region) being dirtied.
582 * @size: size of the range being dirtied.
583 */
584 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
585 hwaddr size);
586
587 /**
588 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
589 * for a specified client. It clears them.
590 *
591 * Checks whether a range of bytes has been written to since the last
592 * call to memory_region_reset_dirty() with the same @client. Dirty logging
593 * must be enabled.
594 *
595 * @mr: the memory region being queried.
596 * @addr: the address (relative to the start of the region) being queried.
597 * @size: the size of the range being queried.
598 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
599 * %DIRTY_MEMORY_VGA.
600 */
601 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
602 hwaddr size, unsigned client);
603 /**
604 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
605 * any external TLBs (e.g. kvm)
606 *
607 * Flushes dirty information from accelerators such as kvm and vhost-net
608 * and makes it available to users of the memory API.
609 *
610 * @mr: the region being flushed.
611 */
612 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
613
614 /**
615 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
616 * client.
617 *
618 * Marks a range of pages as no longer dirty.
619 *
620 * @mr: the region being updated.
621 * @addr: the start of the subrange being cleaned.
622 * @size: the size of the subrange being cleaned.
623 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
624 * %DIRTY_MEMORY_VGA.
625 */
626 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
627 hwaddr size, unsigned client);
628
629 /**
630 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
631 *
632 * Allows a memory region to be marked as read-only (turning it into a ROM).
633 * only useful on RAM regions.
634 *
635 * @mr: the region being updated.
636 * @readonly: whether rhe region is to be ROM or RAM.
637 */
638 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
639
640 /**
641 * memory_region_rom_device_set_romd: enable/disable ROMD mode
642 *
643 * Allows a ROM device (initialized with memory_region_init_rom_device() to
644 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
645 * device is mapped to guest memory and satisfies read access directly.
646 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
647 * Writes are always handled by the #MemoryRegion.write function.
648 *
649 * @mr: the memory region to be updated
650 * @romd_mode: %true to put the region into ROMD mode
651 */
652 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
653
654 /**
655 * memory_region_set_coalescing: Enable memory coalescing for the region.
656 *
657 * Enabled writes to a region to be queued for later processing. MMIO ->write
658 * callbacks may be delayed until a non-coalesced MMIO is issued.
659 * Only useful for IO regions. Roughly similar to write-combining hardware.
660 *
661 * @mr: the memory region to be write coalesced
662 */
663 void memory_region_set_coalescing(MemoryRegion *mr);
664
665 /**
666 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
667 * a region.
668 *
669 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
670 * Multiple calls can be issued coalesced disjoint ranges.
671 *
672 * @mr: the memory region to be updated.
673 * @offset: the start of the range within the region to be coalesced.
674 * @size: the size of the subrange to be coalesced.
675 */
676 void memory_region_add_coalescing(MemoryRegion *mr,
677 hwaddr offset,
678 uint64_t size);
679
680 /**
681 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
682 *
683 * Disables any coalescing caused by memory_region_set_coalescing() or
684 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
685 * hardware.
686 *
687 * @mr: the memory region to be updated.
688 */
689 void memory_region_clear_coalescing(MemoryRegion *mr);
690
691 /**
692 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
693 * accesses.
694 *
695 * Ensure that pending coalesced MMIO request are flushed before the memory
696 * region is accessed. This property is automatically enabled for all regions
697 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
698 *
699 * @mr: the memory region to be updated.
700 */
701 void memory_region_set_flush_coalesced(MemoryRegion *mr);
702
703 /**
704 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
705 * accesses.
706 *
707 * Clear the automatic coalesced MMIO flushing enabled via
708 * memory_region_set_flush_coalesced. Note that this service has no effect on
709 * memory regions that have MMIO coalescing enabled for themselves. For them,
710 * automatic flushing will stop once coalescing is disabled.
711 *
712 * @mr: the memory region to be updated.
713 */
714 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
715
716 /**
717 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
718 * is written to a location.
719 *
720 * Marks a word in an IO region (initialized with memory_region_init_io())
721 * as a trigger for an eventfd event. The I/O callback will not be called.
722 * The caller must be prepared to handle failure (that is, take the required
723 * action if the callback _is_ called).
724 *
725 * @mr: the memory region being updated.
726 * @addr: the address within @mr that is to be monitored
727 * @size: the size of the access to trigger the eventfd
728 * @match_data: whether to match against @data, instead of just @addr
729 * @data: the data to match against the guest write
730 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
731 **/
732 void memory_region_add_eventfd(MemoryRegion *mr,
733 hwaddr addr,
734 unsigned size,
735 bool match_data,
736 uint64_t data,
737 EventNotifier *e);
738
739 /**
740 * memory_region_del_eventfd: Cancel an eventfd.
741 *
742 * Cancels an eventfd trigger requested by a previous
743 * memory_region_add_eventfd() call.
744 *
745 * @mr: the memory region being updated.
746 * @addr: the address within @mr that is to be monitored
747 * @size: the size of the access to trigger the eventfd
748 * @match_data: whether to match against @data, instead of just @addr
749 * @data: the data to match against the guest write
750 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
751 */
752 void memory_region_del_eventfd(MemoryRegion *mr,
753 hwaddr addr,
754 unsigned size,
755 bool match_data,
756 uint64_t data,
757 EventNotifier *e);
758
759 /**
760 * memory_region_add_subregion: Add a subregion to a container.
761 *
762 * Adds a subregion at @offset. The subregion may not overlap with other
763 * subregions (except for those explicitly marked as overlapping). A region
764 * may only be added once as a subregion (unless removed with
765 * memory_region_del_subregion()); use memory_region_init_alias() if you
766 * want a region to be a subregion in multiple locations.
767 *
768 * @mr: the region to contain the new subregion; must be a container
769 * initialized with memory_region_init().
770 * @offset: the offset relative to @mr where @subregion is added.
771 * @subregion: the subregion to be added.
772 */
773 void memory_region_add_subregion(MemoryRegion *mr,
774 hwaddr offset,
775 MemoryRegion *subregion);
776 /**
777 * memory_region_add_subregion_overlap: Add a subregion to a container
778 * with overlap.
779 *
780 * Adds a subregion at @offset. The subregion may overlap with other
781 * subregions. Conflicts are resolved by having a higher @priority hide a
782 * lower @priority. Subregions without priority are taken as @priority 0.
783 * A region may only be added once as a subregion (unless removed with
784 * memory_region_del_subregion()); use memory_region_init_alias() if you
785 * want a region to be a subregion in multiple locations.
786 *
787 * @mr: the region to contain the new subregion; must be a container
788 * initialized with memory_region_init().
789 * @offset: the offset relative to @mr where @subregion is added.
790 * @subregion: the subregion to be added.
791 * @priority: used for resolving overlaps; highest priority wins.
792 */
793 void memory_region_add_subregion_overlap(MemoryRegion *mr,
794 hwaddr offset,
795 MemoryRegion *subregion,
796 int priority);
797
798 /**
799 * memory_region_get_ram_addr: Get the ram address associated with a memory
800 * region
801 *
802 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
803 * code is being reworked.
804 */
805 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
806
807 /**
808 * memory_region_del_subregion: Remove a subregion.
809 *
810 * Removes a subregion from its container.
811 *
812 * @mr: the container to be updated.
813 * @subregion: the region being removed; must be a current subregion of @mr.
814 */
815 void memory_region_del_subregion(MemoryRegion *mr,
816 MemoryRegion *subregion);
817
818 /*
819 * memory_region_set_enabled: dynamically enable or disable a region
820 *
821 * Enables or disables a memory region. A disabled memory region
822 * ignores all accesses to itself and its subregions. It does not
823 * obscure sibling subregions with lower priority - it simply behaves as
824 * if it was removed from the hierarchy.
825 *
826 * Regions default to being enabled.
827 *
828 * @mr: the region to be updated
829 * @enabled: whether to enable or disable the region
830 */
831 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
832
833 /*
834 * memory_region_set_address: dynamically update the address of a region
835 *
836 * Dynamically updates the address of a region, relative to its container.
837 * May be used on regions are currently part of a memory hierarchy.
838 *
839 * @mr: the region to be updated
840 * @addr: new address, relative to container region
841 */
842 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
843
844 /*
845 * memory_region_set_alias_offset: dynamically update a memory alias's offset
846 *
847 * Dynamically updates the offset into the target region that an alias points
848 * to, as if the fourth argument to memory_region_init_alias() has changed.
849 *
850 * @mr: the #MemoryRegion to be updated; should be an alias.
851 * @offset: the new offset into the target memory region
852 */
853 void memory_region_set_alias_offset(MemoryRegion *mr,
854 hwaddr offset);
855
856 /**
857 * memory_region_present: checks if an address relative to a @container
858 * translates into #MemoryRegion within @container
859 *
860 * Answer whether a #MemoryRegion within @container covers the address
861 * @addr.
862 *
863 * @container: a #MemoryRegion within which @addr is a relative address
864 * @addr: the area within @container to be searched
865 */
866 bool memory_region_present(MemoryRegion *container, hwaddr addr);
867
868 /**
869 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
870 * into any address space.
871 *
872 * @mr: a #MemoryRegion which should be checked if it's mapped
873 */
874 bool memory_region_is_mapped(MemoryRegion *mr);
875
876 /**
877 * memory_region_find: translate an address/size relative to a
878 * MemoryRegion into a #MemoryRegionSection.
879 *
880 * Locates the first #MemoryRegion within @mr that overlaps the range
881 * given by @addr and @size.
882 *
883 * Returns a #MemoryRegionSection that describes a contiguous overlap.
884 * It will have the following characteristics:
885 * .@size = 0 iff no overlap was found
886 * .@mr is non-%NULL iff an overlap was found
887 *
888 * Remember that in the return value the @offset_within_region is
889 * relative to the returned region (in the .@mr field), not to the
890 * @mr argument.
891 *
892 * Similarly, the .@offset_within_address_space is relative to the
893 * address space that contains both regions, the passed and the
894 * returned one. However, in the special case where the @mr argument
895 * has no container (and thus is the root of the address space), the
896 * following will hold:
897 * .@offset_within_address_space >= @addr
898 * .@offset_within_address_space + .@size <= @addr + @size
899 *
900 * @mr: a MemoryRegion within which @addr is a relative address
901 * @addr: start of the area within @as to be searched
902 * @size: size of the area to be searched
903 */
904 MemoryRegionSection memory_region_find(MemoryRegion *mr,
905 hwaddr addr, uint64_t size);
906
907 /**
908 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
909 *
910 * Synchronizes the dirty page log for an entire address space.
911 * @as: the address space that contains the memory being synchronized
912 */
913 void address_space_sync_dirty_bitmap(AddressSpace *as);
914
915 /**
916 * memory_region_transaction_begin: Start a transaction.
917 *
918 * During a transaction, changes will be accumulated and made visible
919 * only when the transaction ends (is committed).
920 */
921 void memory_region_transaction_begin(void);
922
923 /**
924 * memory_region_transaction_commit: Commit a transaction and make changes
925 * visible to the guest.
926 */
927 void memory_region_transaction_commit(void);
928
929 /**
930 * memory_listener_register: register callbacks to be called when memory
931 * sections are mapped or unmapped into an address
932 * space
933 *
934 * @listener: an object containing the callbacks to be called
935 * @filter: if non-%NULL, only regions in this address space will be observed
936 */
937 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
938
939 /**
940 * memory_listener_unregister: undo the effect of memory_listener_register()
941 *
942 * @listener: an object containing the callbacks to be removed
943 */
944 void memory_listener_unregister(MemoryListener *listener);
945
946 /**
947 * memory_global_dirty_log_start: begin dirty logging for all regions
948 */
949 void memory_global_dirty_log_start(void);
950
951 /**
952 * memory_global_dirty_log_stop: end dirty logging for all regions
953 */
954 void memory_global_dirty_log_stop(void);
955
956 void mtree_info(fprintf_function mon_printf, void *f);
957
958 /**
959 * address_space_init: initializes an address space
960 *
961 * @as: an uninitialized #AddressSpace
962 * @root: a #MemoryRegion that routes addesses for the address space
963 * @name: an address space name. The name is only used for debugging
964 * output.
965 */
966 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
967
968
969 /**
970 * address_space_destroy: destroy an address space
971 *
972 * Releases all resources associated with an address space. After an address space
973 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
974 * as well.
975 *
976 * @as: address space to be destroyed
977 */
978 void address_space_destroy(AddressSpace *as);
979
980 /**
981 * address_space_rw: read from or write to an address space.
982 *
983 * Return true if the operation hit any unassigned memory or encountered an
984 * IOMMU fault.
985 *
986 * @as: #AddressSpace to be accessed
987 * @addr: address within that address space
988 * @buf: buffer with the data transferred
989 * @is_write: indicates the transfer direction
990 */
991 bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
992 int len, bool is_write);
993
994 /**
995 * address_space_write: write to address space.
996 *
997 * Return true if the operation hit any unassigned memory or encountered an
998 * IOMMU fault.
999 *
1000 * @as: #AddressSpace to be accessed
1001 * @addr: address within that address space
1002 * @buf: buffer with the data transferred
1003 */
1004 bool address_space_write(AddressSpace *as, hwaddr addr,
1005 const uint8_t *buf, int len);
1006
1007 /**
1008 * address_space_read: read from an address space.
1009 *
1010 * Return true if the operation hit any unassigned memory or encountered an
1011 * IOMMU fault.
1012 *
1013 * @as: #AddressSpace to be accessed
1014 * @addr: address within that address space
1015 * @buf: buffer with the data transferred
1016 */
1017 bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
1018
1019 /* address_space_translate: translate an address range into an address space
1020 * into a MemoryRegion and an address range into that section
1021 *
1022 * @as: #AddressSpace to be accessed
1023 * @addr: address within that address space
1024 * @xlat: pointer to address within the returned memory region section's
1025 * #MemoryRegion.
1026 * @len: pointer to length
1027 * @is_write: indicates the transfer direction
1028 */
1029 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1030 hwaddr *xlat, hwaddr *len,
1031 bool is_write);
1032
1033 /* address_space_access_valid: check for validity of accessing an address
1034 * space range
1035 *
1036 * Check whether memory is assigned to the given address space range, and
1037 * access is permitted by any IOMMU regions that are active for the address
1038 * space.
1039 *
1040 * For now, addr and len should be aligned to a page size. This limitation
1041 * will be lifted in the future.
1042 *
1043 * @as: #AddressSpace to be accessed
1044 * @addr: address within that address space
1045 * @len: length of the area to be checked
1046 * @is_write: indicates the transfer direction
1047 */
1048 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1049
1050 /* address_space_map: map a physical memory region into a host virtual address
1051 *
1052 * May map a subset of the requested range, given by and returned in @plen.
1053 * May return %NULL if resources needed to perform the mapping are exhausted.
1054 * Use only for reads OR writes - not for read-modify-write operations.
1055 * Use cpu_register_map_client() to know when retrying the map operation is
1056 * likely to succeed.
1057 *
1058 * @as: #AddressSpace to be accessed
1059 * @addr: address within that address space
1060 * @plen: pointer to length of buffer; updated on return
1061 * @is_write: indicates the transfer direction
1062 */
1063 void *address_space_map(AddressSpace *as, hwaddr addr,
1064 hwaddr *plen, bool is_write);
1065
1066 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1067 *
1068 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1069 * the amount of memory that was actually read or written by the caller.
1070 *
1071 * @as: #AddressSpace used
1072 * @addr: address within that address space
1073 * @len: buffer length as returned by address_space_map()
1074 * @access_len: amount of data actually transferred
1075 * @is_write: indicates the transfer direction
1076 */
1077 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1078 int is_write, hwaddr access_len);
1079
1080
1081 #endif
1082
1083 #endif