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