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