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 | |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 19 | #include "exec/cpu-common.h" |
| 20 | #include "exec/hwaddr.h" |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 21 | #include "exec/memattrs.h" |
Paolo Bonzini | 0987d73 | 2016-12-21 00:31:36 +0800 | [diff] [blame] | 22 | #include "exec/ramlist.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 23 | #include "qemu/queue.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 24 | #include "qemu/int128.h" |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 25 | #include "qemu/notify.h" |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 26 | #include "qom/object.h" |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 27 | #include "qemu/rcu.h" |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 28 | #include "hw/qdev-core.h" |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 29 | |
Paolo Bonzini | 07bdaa4 | 2016-03-25 12:55:08 +0100 | [diff] [blame] | 30 | #define RAM_ADDR_INVALID (~(ram_addr_t)0) |
| 31 | |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 32 | #define MAX_PHYS_ADDR_SPACE_BITS 62 |
| 33 | #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1) |
| 34 | |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 35 | #define TYPE_MEMORY_REGION "qemu:memory-region" |
| 36 | #define MEMORY_REGION(obj) \ |
| 37 | OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION) |
| 38 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 39 | #define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region" |
| 40 | #define IOMMU_MEMORY_REGION(obj) \ |
| 41 | OBJECT_CHECK(IOMMUMemoryRegion, (obj), TYPE_IOMMU_MEMORY_REGION) |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 42 | #define IOMMU_MEMORY_REGION_CLASS(klass) \ |
| 43 | OBJECT_CLASS_CHECK(IOMMUMemoryRegionClass, (klass), \ |
| 44 | TYPE_IOMMU_MEMORY_REGION) |
| 45 | #define IOMMU_MEMORY_REGION_GET_CLASS(obj) \ |
| 46 | OBJECT_GET_CLASS(IOMMUMemoryRegionClass, (obj), \ |
| 47 | TYPE_IOMMU_MEMORY_REGION) |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 48 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 49 | typedef struct MemoryRegionOps MemoryRegionOps; |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 50 | typedef struct MemoryRegionMmio MemoryRegionMmio; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 51 | |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 52 | struct MemoryRegionMmio { |
| 53 | CPUReadMemoryFunc *read[3]; |
| 54 | CPUWriteMemoryFunc *write[3]; |
| 55 | }; |
| 56 | |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 57 | typedef struct IOMMUTLBEntry IOMMUTLBEntry; |
| 58 | |
| 59 | /* See address_space_translate: bit 0 is read, bit 1 is write. */ |
| 60 | typedef enum { |
| 61 | IOMMU_NONE = 0, |
| 62 | IOMMU_RO = 1, |
| 63 | IOMMU_WO = 2, |
| 64 | IOMMU_RW = 3, |
| 65 | } IOMMUAccessFlags; |
| 66 | |
Peter Xu | f06a696 | 2017-04-07 18:59:13 +0800 | [diff] [blame] | 67 | #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0)) |
| 68 | |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 69 | struct IOMMUTLBEntry { |
| 70 | AddressSpace *target_as; |
| 71 | hwaddr iova; |
| 72 | hwaddr translated_addr; |
| 73 | hwaddr addr_mask; /* 0xfff = 4k translation */ |
| 74 | IOMMUAccessFlags perm; |
| 75 | }; |
| 76 | |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 77 | /* |
| 78 | * Bitmap for different IOMMUNotifier capabilities. Each notifier can |
| 79 | * register with one or multiple IOMMU Notifier capability bit(s). |
| 80 | */ |
| 81 | typedef enum { |
| 82 | IOMMU_NOTIFIER_NONE = 0, |
| 83 | /* Notify cache invalidations */ |
| 84 | IOMMU_NOTIFIER_UNMAP = 0x1, |
| 85 | /* Notify entry changes (newly created entries) */ |
| 86 | IOMMU_NOTIFIER_MAP = 0x2, |
| 87 | } IOMMUNotifierFlag; |
| 88 | |
| 89 | #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP) |
| 90 | |
Peter Xu | 698feb5 | 2017-04-07 18:59:07 +0800 | [diff] [blame] | 91 | struct IOMMUNotifier; |
| 92 | typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier, |
| 93 | IOMMUTLBEntry *data); |
| 94 | |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 95 | struct IOMMUNotifier { |
Peter Xu | 698feb5 | 2017-04-07 18:59:07 +0800 | [diff] [blame] | 96 | IOMMUNotify notify; |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 97 | IOMMUNotifierFlag notifier_flags; |
Peter Xu | 698feb5 | 2017-04-07 18:59:07 +0800 | [diff] [blame] | 98 | /* Notify for address space range start <= addr <= end */ |
| 99 | hwaddr start; |
| 100 | hwaddr end; |
Peter Maydell | cb1efcf | 2018-06-15 14:57:16 +0100 | [diff] [blame^] | 101 | int iommu_idx; |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 102 | QLIST_ENTRY(IOMMUNotifier) node; |
| 103 | }; |
| 104 | typedef struct IOMMUNotifier IOMMUNotifier; |
| 105 | |
Peter Xu | 698feb5 | 2017-04-07 18:59:07 +0800 | [diff] [blame] | 106 | static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, |
| 107 | IOMMUNotifierFlag flags, |
Peter Maydell | cb1efcf | 2018-06-15 14:57:16 +0100 | [diff] [blame^] | 108 | hwaddr start, hwaddr end, |
| 109 | int iommu_idx) |
Peter Xu | 698feb5 | 2017-04-07 18:59:07 +0800 | [diff] [blame] | 110 | { |
| 111 | n->notify = fn; |
| 112 | n->notifier_flags = flags; |
| 113 | n->start = start; |
| 114 | n->end = end; |
Peter Maydell | cb1efcf | 2018-06-15 14:57:16 +0100 | [diff] [blame^] | 115 | n->iommu_idx = iommu_idx; |
Peter Xu | 698feb5 | 2017-04-07 18:59:07 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 118 | /* |
| 119 | * Memory region callbacks |
| 120 | */ |
| 121 | struct MemoryRegionOps { |
| 122 | /* Read from the memory region. @addr is relative to @mr; @size is |
| 123 | * in bytes. */ |
| 124 | uint64_t (*read)(void *opaque, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 125 | hwaddr addr, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 126 | unsigned size); |
| 127 | /* Write to the memory region. @addr is relative to @mr; @size is |
| 128 | * in bytes. */ |
| 129 | void (*write)(void *opaque, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 130 | hwaddr addr, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 131 | uint64_t data, |
| 132 | unsigned size); |
| 133 | |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 134 | MemTxResult (*read_with_attrs)(void *opaque, |
| 135 | hwaddr addr, |
| 136 | uint64_t *data, |
| 137 | unsigned size, |
| 138 | MemTxAttrs attrs); |
| 139 | MemTxResult (*write_with_attrs)(void *opaque, |
| 140 | hwaddr addr, |
| 141 | uint64_t data, |
| 142 | unsigned size, |
| 143 | MemTxAttrs attrs); |
KONRAD Frederic | c935674 | 2016-10-19 15:06:49 +0200 | [diff] [blame] | 144 | /* Instruction execution pre-callback: |
| 145 | * @addr is the address of the access relative to the @mr. |
| 146 | * @size is the size of the area returned by the callback. |
| 147 | * @offset is the location of the pointer inside @mr. |
| 148 | * |
| 149 | * Returns a pointer to a location which contains guest code. |
| 150 | */ |
| 151 | void *(*request_ptr)(void *opaque, hwaddr addr, unsigned *size, |
| 152 | unsigned *offset); |
Peter Maydell | cc05c43 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 153 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 154 | enum device_endian endianness; |
| 155 | /* Guest-visible constraints: */ |
| 156 | struct { |
| 157 | /* If nonzero, specify bounds on access sizes beyond which a machine |
| 158 | * check is thrown. |
| 159 | */ |
| 160 | unsigned min_access_size; |
| 161 | unsigned max_access_size; |
| 162 | /* If true, unaligned accesses are supported. Otherwise unaligned |
| 163 | * accesses throw machine checks. |
| 164 | */ |
| 165 | bool unaligned; |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 166 | /* |
| 167 | * If present, and returns #false, the transaction is not accepted |
| 168 | * by the device (and results in machine dependent behaviour such |
| 169 | * as a machine check exception). |
| 170 | */ |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 171 | bool (*accepts)(void *opaque, hwaddr addr, |
Peter Maydell | 8372d38 | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 172 | unsigned size, bool is_write, |
| 173 | MemTxAttrs attrs); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 174 | } valid; |
| 175 | /* Internal implementation constraints: */ |
| 176 | struct { |
| 177 | /* If nonzero, specifies the minimum size implemented. Smaller sizes |
| 178 | * will be rounded upwards and a partial result will be returned. |
| 179 | */ |
| 180 | unsigned min_access_size; |
| 181 | /* If nonzero, specifies the maximum size implemented. Larger sizes |
| 182 | * will be done as a series of accesses with smaller sizes. |
| 183 | */ |
| 184 | unsigned max_access_size; |
| 185 | /* If true, unaligned accesses are supported. Otherwise all accesses |
| 186 | * are converted to (possibly multiple) naturally aligned accesses. |
| 187 | */ |
Fam Zheng | edc1ba7 | 2014-05-05 15:53:41 +0800 | [diff] [blame] | 188 | bool unaligned; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 189 | } impl; |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 190 | |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 191 | /* If .read and .write are not present, old_mmio may be used for |
| 192 | * backwards compatibility with old mmio registration |
| 193 | */ |
| 194 | const MemoryRegionMmio old_mmio; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 195 | }; |
| 196 | |
Alexey Kardashevskiy | f1334de | 2018-02-06 11:08:24 -0700 | [diff] [blame] | 197 | enum IOMMUMemoryRegionAttr { |
| 198 | IOMMU_ATTR_SPAPR_TCE_FD |
| 199 | }; |
| 200 | |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 201 | /** |
| 202 | * IOMMUMemoryRegionClass: |
| 203 | * |
| 204 | * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION |
| 205 | * and provide an implementation of at least the @translate method here |
| 206 | * to handle requests to the memory region. Other methods are optional. |
| 207 | * |
| 208 | * The IOMMU implementation must use the IOMMU notifier infrastructure |
| 209 | * to report whenever mappings are changed, by calling |
| 210 | * memory_region_notify_iommu() (or, if necessary, by calling |
| 211 | * memory_region_notify_one() for each registered notifier). |
Peter Maydell | 21f4020 | 2018-06-15 14:57:15 +0100 | [diff] [blame] | 212 | * |
| 213 | * Conceptually an IOMMU provides a mapping from input address |
| 214 | * to an output TLB entry. If the IOMMU is aware of memory transaction |
| 215 | * attributes and the output TLB entry depends on the transaction |
| 216 | * attributes, we represent this using IOMMU indexes. Each index |
| 217 | * selects a particular translation table that the IOMMU has: |
| 218 | * @attrs_to_index returns the IOMMU index for a set of transaction attributes |
| 219 | * @translate takes an input address and an IOMMU index |
| 220 | * and the mapping returned can only depend on the input address and the |
| 221 | * IOMMU index. |
| 222 | * |
| 223 | * Most IOMMUs don't care about the transaction attributes and support |
| 224 | * only a single IOMMU index. A more complex IOMMU might have one index |
| 225 | * for secure transactions and one for non-secure transactions. |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 226 | */ |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 227 | typedef struct IOMMUMemoryRegionClass { |
| 228 | /* private */ |
| 229 | struct DeviceClass parent_class; |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 230 | |
Peter Xu | bf55b7a | 2017-05-19 11:19:40 +0800 | [diff] [blame] | 231 | /* |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 232 | * Return a TLB entry that contains a given address. |
| 233 | * |
| 234 | * The IOMMUAccessFlags indicated via @flag are optional and may |
| 235 | * be specified as IOMMU_NONE to indicate that the caller needs |
| 236 | * the full translation information for both reads and writes. If |
| 237 | * the access flags are specified then the IOMMU implementation |
| 238 | * may use this as an optimization, to stop doing a page table |
| 239 | * walk as soon as it knows that the requested permissions are not |
| 240 | * allowed. If IOMMU_NONE is passed then the IOMMU must do the |
| 241 | * full page table walk and report the permissions in the returned |
| 242 | * IOMMUTLBEntry. (Note that this implies that an IOMMU may not |
| 243 | * return different mappings for reads and writes.) |
| 244 | * |
| 245 | * The returned information remains valid while the caller is |
| 246 | * holding the big QEMU lock or is inside an RCU critical section; |
| 247 | * if the caller wishes to cache the mapping beyond that it must |
| 248 | * register an IOMMU notifier so it can invalidate its cached |
| 249 | * information when the IOMMU mapping changes. |
| 250 | * |
| 251 | * @iommu: the IOMMUMemoryRegion |
| 252 | * @hwaddr: address to be translated within the memory region |
| 253 | * @flag: requested access permissions |
Peter Xu | bf55b7a | 2017-05-19 11:19:40 +0800 | [diff] [blame] | 254 | */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 255 | IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr, |
Peter Xu | bf55b7a | 2017-05-19 11:19:40 +0800 | [diff] [blame] | 256 | IOMMUAccessFlags flag); |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 257 | /* Returns minimum supported page size in bytes. |
| 258 | * If this method is not provided then the minimum is assumed to |
| 259 | * be TARGET_PAGE_SIZE. |
| 260 | * |
| 261 | * @iommu: the IOMMUMemoryRegion |
| 262 | */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 263 | uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu); |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 264 | /* Called when IOMMU Notifier flag changes (ie when the set of |
| 265 | * events which IOMMU users are requesting notification for changes). |
| 266 | * Optional method -- need not be provided if the IOMMU does not |
| 267 | * need to know exactly which events must be notified. |
| 268 | * |
| 269 | * @iommu: the IOMMUMemoryRegion |
| 270 | * @old_flags: events which previously needed to be notified |
| 271 | * @new_flags: events which now need to be notified |
| 272 | */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 273 | void (*notify_flag_changed)(IOMMUMemoryRegion *iommu, |
Peter Xu | 5bf3d31 | 2016-09-23 13:02:27 +0800 | [diff] [blame] | 274 | IOMMUNotifierFlag old_flags, |
| 275 | IOMMUNotifierFlag new_flags); |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 276 | /* Called to handle memory_region_iommu_replay(). |
| 277 | * |
| 278 | * The default implementation of memory_region_iommu_replay() is to |
| 279 | * call the IOMMU translate method for every page in the address space |
| 280 | * with flag == IOMMU_NONE and then call the notifier if translate |
| 281 | * returns a valid mapping. If this method is implemented then it |
| 282 | * overrides the default behaviour, and must provide the full semantics |
| 283 | * of memory_region_iommu_replay(), by calling @notifier for every |
| 284 | * translation present in the IOMMU. |
| 285 | * |
| 286 | * Optional method -- an IOMMU only needs to provide this method |
| 287 | * if the default is inefficient or produces undesirable side effects. |
| 288 | * |
| 289 | * Note: this is not related to record-and-replay functionality. |
| 290 | */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 291 | void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier); |
Alexey Kardashevskiy | f1334de | 2018-02-06 11:08:24 -0700 | [diff] [blame] | 292 | |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 293 | /* Get IOMMU misc attributes. This is an optional method that |
| 294 | * can be used to allow users of the IOMMU to get implementation-specific |
| 295 | * information. The IOMMU implements this method to handle calls |
| 296 | * by IOMMU users to memory_region_iommu_get_attr() by filling in |
| 297 | * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that |
| 298 | * the IOMMU supports. If the method is unimplemented then |
| 299 | * memory_region_iommu_get_attr() will always return -EINVAL. |
| 300 | * |
| 301 | * @iommu: the IOMMUMemoryRegion |
| 302 | * @attr: attribute being queried |
| 303 | * @data: memory to fill in with the attribute data |
| 304 | * |
| 305 | * Returns 0 on success, or a negative errno; in particular |
| 306 | * returns -EINVAL for unrecognized or unimplemented attribute types. |
| 307 | */ |
| 308 | int (*get_attr)(IOMMUMemoryRegion *iommu, enum IOMMUMemoryRegionAttr attr, |
Alexey Kardashevskiy | f1334de | 2018-02-06 11:08:24 -0700 | [diff] [blame] | 309 | void *data); |
Peter Maydell | 21f4020 | 2018-06-15 14:57:15 +0100 | [diff] [blame] | 310 | |
| 311 | /* Return the IOMMU index to use for a given set of transaction attributes. |
| 312 | * |
| 313 | * Optional method: if an IOMMU only supports a single IOMMU index then |
| 314 | * the default implementation of memory_region_iommu_attrs_to_index() |
| 315 | * will return 0. |
| 316 | * |
| 317 | * The indexes supported by an IOMMU must be contiguous, starting at 0. |
| 318 | * |
| 319 | * @iommu: the IOMMUMemoryRegion |
| 320 | * @attrs: memory transaction attributes |
| 321 | */ |
| 322 | int (*attrs_to_index)(IOMMUMemoryRegion *iommu, MemTxAttrs attrs); |
| 323 | |
| 324 | /* Return the number of IOMMU indexes this IOMMU supports. |
| 325 | * |
| 326 | * Optional method: if this method is not provided, then |
| 327 | * memory_region_iommu_num_indexes() will return 1, indicating that |
| 328 | * only a single IOMMU index is supported. |
| 329 | * |
| 330 | * @iommu: the IOMMUMemoryRegion |
| 331 | */ |
| 332 | int (*num_indexes)(IOMMUMemoryRegion *iommu); |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 333 | } IOMMUMemoryRegionClass; |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 334 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 335 | typedef struct CoalescedMemoryRange CoalescedMemoryRange; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 336 | typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 337 | |
| 338 | struct MemoryRegion { |
Peter Crosthwaite | b4fefef | 2014-06-05 23:15:52 -0700 | [diff] [blame] | 339 | Object parent_obj; |
Paolo Bonzini | a676854 | 2015-12-09 11:40:14 +0100 | [diff] [blame] | 340 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 341 | /* All fields are private - violators will be prosecuted */ |
Paolo Bonzini | a676854 | 2015-12-09 11:40:14 +0100 | [diff] [blame] | 342 | |
| 343 | /* The following fields should fit in a cache line */ |
| 344 | bool romd_mode; |
| 345 | bool ram; |
| 346 | bool subpage; |
| 347 | bool readonly; /* For RAM regions */ |
| 348 | bool rom_device; |
| 349 | bool flush_coalesced_mmio; |
| 350 | bool global_locking; |
| 351 | uint8_t dirty_log_mask; |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 352 | bool is_iommu; |
Gonglei | 58eaa21 | 2016-02-22 16:34:55 +0800 | [diff] [blame] | 353 | RAMBlock *ram_block; |
Paolo Bonzini | 612263c | 2015-12-09 11:44:25 +0100 | [diff] [blame] | 354 | Object *owner; |
Paolo Bonzini | a676854 | 2015-12-09 11:40:14 +0100 | [diff] [blame] | 355 | |
| 356 | const MemoryRegionOps *ops; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 357 | void *opaque; |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 358 | MemoryRegion *container; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 359 | Int128 size; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 360 | hwaddr addr; |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 361 | void (*destructor)(MemoryRegion *mr); |
Igor Mammedov | a2b257d | 2014-10-31 16:38:37 +0000 | [diff] [blame] | 362 | uint64_t align; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 363 | bool terminates; |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 364 | bool ram_device; |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 365 | bool enabled; |
Jan Kiszka | 1660e72 | 2011-10-23 16:01:19 +0200 | [diff] [blame] | 366 | bool warning_printed; /* For reservations */ |
Paolo Bonzini | deb809e | 2015-07-14 13:56:53 +0200 | [diff] [blame] | 367 | uint8_t vga_logging_count; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 368 | MemoryRegion *alias; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 369 | hwaddr alias_offset; |
Peter Crosthwaite | d33382d | 2014-06-05 23:17:01 -0700 | [diff] [blame] | 370 | int32_t priority; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 371 | QTAILQ_HEAD(subregions, MemoryRegion) subregions; |
| 372 | QTAILQ_ENTRY(MemoryRegion) subregions_link; |
| 373 | QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced; |
Peter Maydell | 302fa28 | 2014-08-19 20:05:46 +0100 | [diff] [blame] | 374 | const char *name; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 375 | unsigned ioeventfd_nb; |
| 376 | MemoryRegionIoeventfd *ioeventfds; |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | struct IOMMUMemoryRegion { |
| 380 | MemoryRegion parent_obj; |
| 381 | |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 382 | QLIST_HEAD(, IOMMUNotifier) iommu_notify; |
Peter Xu | 5bf3d31 | 2016-09-23 13:02:27 +0800 | [diff] [blame] | 383 | IOMMUNotifierFlag iommu_notify_flags; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 384 | }; |
| 385 | |
Peter Xu | 512fa40 | 2017-04-07 18:59:08 +0800 | [diff] [blame] | 386 | #define IOMMU_NOTIFIER_FOREACH(n, mr) \ |
| 387 | QLIST_FOREACH((n), &(mr)->iommu_notify, node) |
| 388 | |
Paolo Bonzini | c2fc83e | 2013-06-02 15:20:47 +0200 | [diff] [blame] | 389 | /** |
| 390 | * MemoryListener: callbacks structure for updates to the physical memory map |
| 391 | * |
| 392 | * Allows a component to adjust to changes in the guest-visible memory map. |
| 393 | * Use with memory_listener_register() and memory_listener_unregister(). |
| 394 | */ |
| 395 | struct MemoryListener { |
| 396 | void (*begin)(MemoryListener *listener); |
| 397 | void (*commit)(MemoryListener *listener); |
| 398 | void (*region_add)(MemoryListener *listener, MemoryRegionSection *section); |
| 399 | void (*region_del)(MemoryListener *listener, MemoryRegionSection *section); |
| 400 | void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section); |
Paolo Bonzini | b2dfd71 | 2015-04-25 14:38:30 +0200 | [diff] [blame] | 401 | void (*log_start)(MemoryListener *listener, MemoryRegionSection *section, |
| 402 | int old, int new); |
| 403 | void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section, |
| 404 | int old, int new); |
Paolo Bonzini | c2fc83e | 2013-06-02 15:20:47 +0200 | [diff] [blame] | 405 | void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section); |
| 406 | void (*log_global_start)(MemoryListener *listener); |
| 407 | void (*log_global_stop)(MemoryListener *listener); |
| 408 | void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section, |
| 409 | bool match_data, uint64_t data, EventNotifier *e); |
| 410 | void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section, |
| 411 | bool match_data, uint64_t data, EventNotifier *e); |
| 412 | void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section, |
| 413 | hwaddr addr, hwaddr len); |
| 414 | void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section, |
| 415 | hwaddr addr, hwaddr len); |
| 416 | /* Lower = earlier (during add), later (during del) */ |
| 417 | unsigned priority; |
Paolo Bonzini | d45fa78 | 2016-09-22 16:11:54 +0200 | [diff] [blame] | 418 | AddressSpace *address_space; |
Paolo Bonzini | c2fc83e | 2013-06-02 15:20:47 +0200 | [diff] [blame] | 419 | QTAILQ_ENTRY(MemoryListener) link; |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 420 | QTAILQ_ENTRY(MemoryListener) link_as; |
Paolo Bonzini | c2fc83e | 2013-06-02 15:20:47 +0200 | [diff] [blame] | 421 | }; |
| 422 | |
Avi Kivity | 9ad2bbc | 2012-10-02 14:59:23 +0200 | [diff] [blame] | 423 | /** |
| 424 | * AddressSpace: describes a mapping of addresses to #MemoryRegion objects |
| 425 | */ |
| 426 | struct AddressSpace { |
| 427 | /* All fields are private. */ |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 428 | struct rcu_head rcu; |
Alexey Kardashevskiy | 7dca804 | 2013-04-29 16:25:51 +0000 | [diff] [blame] | 429 | char *name; |
Avi Kivity | 9ad2bbc | 2012-10-02 14:59:23 +0200 | [diff] [blame] | 430 | MemoryRegion *root; |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 431 | |
| 432 | /* Accessed via RCU. */ |
Avi Kivity | 9ad2bbc | 2012-10-02 14:59:23 +0200 | [diff] [blame] | 433 | struct FlatView *current_map; |
Paolo Bonzini | 374f298 | 2013-05-17 12:37:03 +0200 | [diff] [blame] | 434 | |
Avi Kivity | 9ad2bbc | 2012-10-02 14:59:23 +0200 | [diff] [blame] | 435 | int ioeventfd_nb; |
| 436 | struct MemoryRegionIoeventfd *ioeventfds; |
Paolo Bonzini | 9a54635 | 2016-09-22 16:23:06 +0200 | [diff] [blame] | 437 | QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners; |
Avi Kivity | 0d673e3 | 2012-10-02 15:28:50 +0200 | [diff] [blame] | 438 | QTAILQ_ENTRY(AddressSpace) address_spaces_link; |
Avi Kivity | 9ad2bbc | 2012-10-02 14:59:23 +0200 | [diff] [blame] | 439 | }; |
| 440 | |
Paolo Bonzini | 785a507 | 2018-03-05 00:31:20 +0100 | [diff] [blame] | 441 | typedef struct AddressSpaceDispatch AddressSpaceDispatch; |
| 442 | typedef struct FlatRange FlatRange; |
| 443 | |
| 444 | /* Flattened global view of current active memory hierarchy. Kept in sorted |
| 445 | * order. |
| 446 | */ |
| 447 | struct FlatView { |
| 448 | struct rcu_head rcu; |
| 449 | unsigned ref; |
| 450 | FlatRange *ranges; |
| 451 | unsigned nr; |
| 452 | unsigned nr_allocated; |
| 453 | struct AddressSpaceDispatch *dispatch; |
| 454 | MemoryRegion *root; |
| 455 | }; |
| 456 | |
| 457 | static inline FlatView *address_space_to_flatview(AddressSpace *as) |
| 458 | { |
| 459 | return atomic_rcu_read(&as->current_map); |
| 460 | } |
| 461 | |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 462 | |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 463 | /** |
| 464 | * MemoryRegionSection: describes a fragment of a #MemoryRegion |
| 465 | * |
| 466 | * @mr: the region, or %NULL if empty |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 467 | * @fv: the flat view of the address space the region is mapped in |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 468 | * @offset_within_region: the beginning of the section, relative to @mr's start |
| 469 | * @size: the size of the section; will not exceed @mr's boundaries |
| 470 | * @offset_within_address_space: the address of the first byte of the section |
| 471 | * relative to the region's address space |
Avi Kivity | 7a8499e | 2012-02-08 17:01:23 +0200 | [diff] [blame] | 472 | * @readonly: writes to this section are ignored |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 473 | */ |
| 474 | struct MemoryRegionSection { |
| 475 | MemoryRegion *mr; |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 476 | FlatView *fv; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 477 | hwaddr offset_within_region; |
Paolo Bonzini | 052e87b | 2013-05-27 10:08:27 +0200 | [diff] [blame] | 478 | Int128 size; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 479 | hwaddr offset_within_address_space; |
Avi Kivity | 7a8499e | 2012-02-08 17:01:23 +0200 | [diff] [blame] | 480 | bool readonly; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 481 | }; |
| 482 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 483 | /** |
| 484 | * memory_region_init: Initialize a memory region |
| 485 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 486 | * The region typically acts as a container for other memory regions. Use |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 487 | * memory_region_add_subregion() to add subregions. |
| 488 | * |
| 489 | * @mr: the #MemoryRegion to be initialized |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 490 | * @owner: the object that tracks the region's reference count |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 491 | * @name: used for debugging; not visible to the user or ABI |
| 492 | * @size: size of the region; any subregions beyond this size will be clipped |
| 493 | */ |
| 494 | void memory_region_init(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 495 | struct Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 496 | const char *name, |
| 497 | uint64_t size); |
Paolo Bonzini | 46637be | 2013-05-07 09:06:00 +0200 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * memory_region_ref: Add 1 to a memory region's reference count |
| 501 | * |
| 502 | * Whenever memory regions are accessed outside the BQL, they need to be |
| 503 | * preserved against hot-unplug. MemoryRegions actually do not have their |
| 504 | * own reference count; they piggyback on a QOM object, their "owner". |
| 505 | * This function adds a reference to the owner. |
| 506 | * |
| 507 | * All MemoryRegions must have an owner if they can disappear, even if the |
| 508 | * device they belong to operates exclusively under the BQL. This is because |
| 509 | * the region could be returned at any time by memory_region_find, and this |
| 510 | * is usually under guest control. |
| 511 | * |
| 512 | * @mr: the #MemoryRegion |
| 513 | */ |
| 514 | void memory_region_ref(MemoryRegion *mr); |
| 515 | |
| 516 | /** |
| 517 | * memory_region_unref: Remove 1 to a memory region's reference count |
| 518 | * |
| 519 | * Whenever memory regions are accessed outside the BQL, they need to be |
| 520 | * preserved against hot-unplug. MemoryRegions actually do not have their |
| 521 | * own reference count; they piggyback on a QOM object, their "owner". |
| 522 | * This function removes a reference to the owner and possibly destroys it. |
| 523 | * |
| 524 | * @mr: the #MemoryRegion |
| 525 | */ |
| 526 | void memory_region_unref(MemoryRegion *mr); |
| 527 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 528 | /** |
| 529 | * memory_region_init_io: Initialize an I/O memory region. |
| 530 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 531 | * 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] | 532 | * if @size is nonzero, subregions will be clipped to @size. |
| 533 | * |
| 534 | * @mr: the #MemoryRegion to be initialized. |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 535 | * @owner: the object that tracks the region's reference count |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 536 | * @ops: a structure containing read and write callbacks to be used when |
| 537 | * I/O is performed on the region. |
Daniel P. Berrange | b6af097 | 2015-08-26 12:17:13 +0100 | [diff] [blame] | 538 | * @opaque: passed to the read and write callbacks of the @ops structure. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 539 | * @name: used for debugging; not visible to the user or ABI |
| 540 | * @size: size of the region. |
| 541 | */ |
| 542 | void memory_region_init_io(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 543 | struct Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 544 | const MemoryRegionOps *ops, |
| 545 | void *opaque, |
| 546 | const char *name, |
| 547 | uint64_t size); |
| 548 | |
| 549 | /** |
Peter Maydell | 1cfe48c | 2017-07-07 15:42:49 +0100 | [diff] [blame] | 550 | * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses |
| 551 | * into the region will modify memory |
| 552 | * directly. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 553 | * |
| 554 | * @mr: the #MemoryRegion to be initialized. |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 555 | * @owner: the object that tracks the region's reference count |
Dr. David Alan Gilbert | e8f5fe2 | 2017-03-09 15:27:08 +0000 | [diff] [blame] | 556 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
| 557 | * must be unique within any device |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 558 | * @size: size of the region. |
Hu Tao | 4994653 | 2014-09-09 13:27:55 +0800 | [diff] [blame] | 559 | * @errp: pointer to Error*, to store an error if it happens. |
Peter Maydell | a5c0234 | 2017-07-07 15:42:48 +0100 | [diff] [blame] | 560 | * |
| 561 | * Note that this function does not do anything to cause the data in the |
| 562 | * RAM memory region to be migrated; that is the responsibility of the caller. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 563 | */ |
Peter Maydell | 1cfe48c | 2017-07-07 15:42:49 +0100 | [diff] [blame] | 564 | void memory_region_init_ram_nomigrate(MemoryRegion *mr, |
| 565 | struct Object *owner, |
| 566 | const char *name, |
| 567 | uint64_t size, |
| 568 | Error **errp); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 569 | |
Michael S. Tsirkin | 60786ef | 2014-11-17 00:24:36 +0200 | [diff] [blame] | 570 | /** |
Marcel Apfelbaum | 06329cc | 2017-12-13 16:37:37 +0200 | [diff] [blame] | 571 | * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region. |
| 572 | * Accesses into the region will |
| 573 | * modify memory directly. |
| 574 | * |
| 575 | * @mr: the #MemoryRegion to be initialized. |
| 576 | * @owner: the object that tracks the region's reference count |
| 577 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
| 578 | * must be unique within any device |
| 579 | * @size: size of the region. |
| 580 | * @share: allow remapping RAM to different addresses |
| 581 | * @errp: pointer to Error*, to store an error if it happens. |
| 582 | * |
| 583 | * Note that this function is similar to memory_region_init_ram_nomigrate. |
| 584 | * The only difference is part of the RAM region can be remapped. |
| 585 | */ |
| 586 | void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr, |
| 587 | struct Object *owner, |
| 588 | const char *name, |
| 589 | uint64_t size, |
| 590 | bool share, |
| 591 | Error **errp); |
| 592 | |
| 593 | /** |
Michael S. Tsirkin | 60786ef | 2014-11-17 00:24:36 +0200 | [diff] [blame] | 594 | * memory_region_init_resizeable_ram: Initialize memory region with resizeable |
| 595 | * RAM. Accesses into the region will |
| 596 | * modify memory directly. Only an initial |
| 597 | * portion of this RAM is actually used. |
| 598 | * The used size can change across reboots. |
| 599 | * |
| 600 | * @mr: the #MemoryRegion to be initialized. |
| 601 | * @owner: the object that tracks the region's reference count |
Dr. David Alan Gilbert | e8f5fe2 | 2017-03-09 15:27:08 +0000 | [diff] [blame] | 602 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
| 603 | * must be unique within any device |
Michael S. Tsirkin | 60786ef | 2014-11-17 00:24:36 +0200 | [diff] [blame] | 604 | * @size: used size of the region. |
| 605 | * @max_size: max size of the region. |
| 606 | * @resized: callback to notify owner about used size change. |
| 607 | * @errp: pointer to Error*, to store an error if it happens. |
Peter Maydell | a5c0234 | 2017-07-07 15:42:48 +0100 | [diff] [blame] | 608 | * |
| 609 | * Note that this function does not do anything to cause the data in the |
| 610 | * RAM memory region to be migrated; that is the responsibility of the caller. |
Michael S. Tsirkin | 60786ef | 2014-11-17 00:24:36 +0200 | [diff] [blame] | 611 | */ |
| 612 | void memory_region_init_resizeable_ram(MemoryRegion *mr, |
| 613 | struct Object *owner, |
| 614 | const char *name, |
| 615 | uint64_t size, |
| 616 | uint64_t max_size, |
| 617 | void (*resized)(const char*, |
| 618 | uint64_t length, |
| 619 | void *host), |
| 620 | Error **errp); |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 621 | #ifdef __linux__ |
| 622 | /** |
| 623 | * memory_region_init_ram_from_file: Initialize RAM memory region with a |
| 624 | * mmap-ed backend. |
| 625 | * |
| 626 | * @mr: the #MemoryRegion to be initialized. |
| 627 | * @owner: the object that tracks the region's reference count |
Dr. David Alan Gilbert | e8f5fe2 | 2017-03-09 15:27:08 +0000 | [diff] [blame] | 628 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
| 629 | * must be unique within any device |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 630 | * @size: size of the region. |
Haozhong Zhang | 9837684 | 2017-12-11 15:28:04 +0800 | [diff] [blame] | 631 | * @align: alignment of the region base address; if 0, the default alignment |
| 632 | * (getpagesize()) will be used. |
Paolo Bonzini | dbcb898 | 2014-06-10 19:15:24 +0800 | [diff] [blame] | 633 | * @share: %true if memory must be mmaped with the MAP_SHARED flag |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 634 | * @path: the path in which to allocate the RAM. |
Paolo Bonzini | 7f56e74 | 2014-05-14 17:43:20 +0800 | [diff] [blame] | 635 | * @errp: pointer to Error*, to store an error if it happens. |
Peter Maydell | a5c0234 | 2017-07-07 15:42:48 +0100 | [diff] [blame] | 636 | * |
| 637 | * Note that this function does not do anything to cause the data in the |
| 638 | * RAM memory region to be migrated; that is the responsibility of the caller. |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 639 | */ |
| 640 | void memory_region_init_ram_from_file(MemoryRegion *mr, |
| 641 | struct Object *owner, |
| 642 | const char *name, |
| 643 | uint64_t size, |
Haozhong Zhang | 9837684 | 2017-12-11 15:28:04 +0800 | [diff] [blame] | 644 | uint64_t align, |
Paolo Bonzini | dbcb898 | 2014-06-10 19:15:24 +0800 | [diff] [blame] | 645 | bool share, |
Paolo Bonzini | 7f56e74 | 2014-05-14 17:43:20 +0800 | [diff] [blame] | 646 | const char *path, |
| 647 | Error **errp); |
Marc-André Lureau | fea617c | 2017-06-02 18:12:24 +0400 | [diff] [blame] | 648 | |
| 649 | /** |
| 650 | * memory_region_init_ram_from_fd: Initialize RAM memory region with a |
| 651 | * mmap-ed backend. |
| 652 | * |
| 653 | * @mr: the #MemoryRegion to be initialized. |
| 654 | * @owner: the object that tracks the region's reference count |
| 655 | * @name: the name of the region. |
| 656 | * @size: size of the region. |
| 657 | * @share: %true if memory must be mmaped with the MAP_SHARED flag |
| 658 | * @fd: the fd to mmap. |
| 659 | * @errp: pointer to Error*, to store an error if it happens. |
Peter Maydell | a5c0234 | 2017-07-07 15:42:48 +0100 | [diff] [blame] | 660 | * |
| 661 | * Note that this function does not do anything to cause the data in the |
| 662 | * RAM memory region to be migrated; that is the responsibility of the caller. |
Marc-André Lureau | fea617c | 2017-06-02 18:12:24 +0400 | [diff] [blame] | 663 | */ |
| 664 | void memory_region_init_ram_from_fd(MemoryRegion *mr, |
| 665 | struct Object *owner, |
| 666 | const char *name, |
| 667 | uint64_t size, |
| 668 | bool share, |
| 669 | int fd, |
| 670 | Error **errp); |
Paolo Bonzini | 0b183fc | 2014-05-14 17:43:19 +0800 | [diff] [blame] | 671 | #endif |
| 672 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 673 | /** |
BALATON Zoltan | 1a7e8ca | 2012-08-22 17:18:38 +0200 | [diff] [blame] | 674 | * memory_region_init_ram_ptr: Initialize RAM memory region from a |
| 675 | * user-provided pointer. Accesses into the |
| 676 | * region will modify memory directly. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 677 | * |
| 678 | * @mr: the #MemoryRegion to be initialized. |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 679 | * @owner: the object that tracks the region's reference count |
Dr. David Alan Gilbert | e8f5fe2 | 2017-03-09 15:27:08 +0000 | [diff] [blame] | 680 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
| 681 | * must be unique within any device |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 682 | * @size: size of the region. |
| 683 | * @ptr: memory to be mapped; must contain at least @size bytes. |
Peter Maydell | a5c0234 | 2017-07-07 15:42:48 +0100 | [diff] [blame] | 684 | * |
| 685 | * Note that this function does not do anything to cause the data in the |
| 686 | * RAM memory region to be migrated; that is the responsibility of the caller. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 687 | */ |
| 688 | void memory_region_init_ram_ptr(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 689 | struct Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 690 | const char *name, |
| 691 | uint64_t size, |
| 692 | void *ptr); |
| 693 | |
| 694 | /** |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 695 | * memory_region_init_ram_device_ptr: Initialize RAM device memory region from |
| 696 | * a user-provided pointer. |
| 697 | * |
| 698 | * A RAM device represents a mapping to a physical device, such as to a PCI |
| 699 | * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped |
| 700 | * into the VM address space and access to the region will modify memory |
| 701 | * directly. However, the memory region should not be included in a memory |
| 702 | * dump (device may not be enabled/mapped at the time of the dump), and |
| 703 | * operations incompatible with manipulating MMIO should be avoided. Replaces |
| 704 | * skip_dump flag. |
| 705 | * |
| 706 | * @mr: the #MemoryRegion to be initialized. |
| 707 | * @owner: the object that tracks the region's reference count |
| 708 | * @name: the name of the region. |
| 709 | * @size: size of the region. |
| 710 | * @ptr: memory to be mapped; must contain at least @size bytes. |
Peter Maydell | a5c0234 | 2017-07-07 15:42:48 +0100 | [diff] [blame] | 711 | * |
| 712 | * Note that this function does not do anything to cause the data in the |
| 713 | * RAM memory region to be migrated; that is the responsibility of the caller. |
| 714 | * (For RAM device memory regions, migrating the contents rarely makes sense.) |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 715 | */ |
| 716 | void memory_region_init_ram_device_ptr(MemoryRegion *mr, |
| 717 | struct Object *owner, |
| 718 | const char *name, |
| 719 | uint64_t size, |
| 720 | void *ptr); |
| 721 | |
| 722 | /** |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 723 | * memory_region_init_alias: Initialize a memory region that aliases all or a |
| 724 | * part of another memory region. |
| 725 | * |
| 726 | * @mr: the #MemoryRegion to be initialized. |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 727 | * @owner: the object that tracks the region's reference count |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 728 | * @name: used for debugging; not visible to the user or ABI |
| 729 | * @orig: the region to be referenced; @mr will be equivalent to |
| 730 | * @orig between @offset and @offset + @size - 1. |
| 731 | * @offset: start of the section in @orig to be referenced. |
| 732 | * @size: size of the region. |
| 733 | */ |
| 734 | void memory_region_init_alias(MemoryRegion *mr, |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 735 | struct Object *owner, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 736 | const char *name, |
| 737 | MemoryRegion *orig, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 738 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 739 | uint64_t size); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 740 | |
| 741 | /** |
Peter Maydell | b59821a | 2017-07-07 15:42:50 +0100 | [diff] [blame] | 742 | * memory_region_init_rom_nomigrate: Initialize a ROM memory region. |
Peter Maydell | a1777f7 | 2016-07-04 13:06:35 +0100 | [diff] [blame] | 743 | * |
Peter Maydell | b59821a | 2017-07-07 15:42:50 +0100 | [diff] [blame] | 744 | * This has the same effect as calling memory_region_init_ram_nomigrate() |
Peter Maydell | a1777f7 | 2016-07-04 13:06:35 +0100 | [diff] [blame] | 745 | * and then marking the resulting region read-only with |
| 746 | * memory_region_set_readonly(). |
| 747 | * |
Peter Maydell | b59821a | 2017-07-07 15:42:50 +0100 | [diff] [blame] | 748 | * Note that this function does not do anything to cause the data in the |
| 749 | * RAM side of the memory region to be migrated; that is the responsibility |
| 750 | * of the caller. |
| 751 | * |
Peter Maydell | a1777f7 | 2016-07-04 13:06:35 +0100 | [diff] [blame] | 752 | * @mr: the #MemoryRegion to be initialized. |
| 753 | * @owner: the object that tracks the region's reference count |
Dr. David Alan Gilbert | e8f5fe2 | 2017-03-09 15:27:08 +0000 | [diff] [blame] | 754 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
| 755 | * must be unique within any device |
Peter Maydell | a1777f7 | 2016-07-04 13:06:35 +0100 | [diff] [blame] | 756 | * @size: size of the region. |
| 757 | * @errp: pointer to Error*, to store an error if it happens. |
| 758 | */ |
Peter Maydell | b59821a | 2017-07-07 15:42:50 +0100 | [diff] [blame] | 759 | void memory_region_init_rom_nomigrate(MemoryRegion *mr, |
| 760 | struct Object *owner, |
| 761 | const char *name, |
| 762 | uint64_t size, |
| 763 | Error **errp); |
Peter Maydell | a1777f7 | 2016-07-04 13:06:35 +0100 | [diff] [blame] | 764 | |
| 765 | /** |
Peter Maydell | b59821a | 2017-07-07 15:42:50 +0100 | [diff] [blame] | 766 | * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region. |
| 767 | * Writes are handled via callbacks. |
| 768 | * |
| 769 | * Note that this function does not do anything to cause the data in the |
| 770 | * RAM side of the memory region to be migrated; that is the responsibility |
| 771 | * of the caller. |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 772 | * |
| 773 | * @mr: the #MemoryRegion to be initialized. |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 774 | * @owner: the object that tracks the region's reference count |
Peter Maydell | 39e0b03 | 2016-07-04 13:06:35 +0100 | [diff] [blame] | 775 | * @ops: callbacks for write access handling (must not be NULL). |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 776 | * @opaque: passed to the read and write callbacks of the @ops structure. |
Dr. David Alan Gilbert | e8f5fe2 | 2017-03-09 15:27:08 +0000 | [diff] [blame] | 777 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
| 778 | * must be unique within any device |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 779 | * @size: size of the region. |
Hu Tao | 33e0eb5 | 2014-09-09 13:27:57 +0800 | [diff] [blame] | 780 | * @errp: pointer to Error*, to store an error if it happens. |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 781 | */ |
Peter Maydell | b59821a | 2017-07-07 15:42:50 +0100 | [diff] [blame] | 782 | void memory_region_init_rom_device_nomigrate(MemoryRegion *mr, |
| 783 | struct Object *owner, |
| 784 | const MemoryRegionOps *ops, |
| 785 | void *opaque, |
| 786 | const char *name, |
| 787 | uint64_t size, |
| 788 | Error **errp); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 789 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 790 | /** |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 791 | * memory_region_init_iommu: Initialize a memory region of a custom type |
| 792 | * that translates addresses |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 793 | * |
| 794 | * An IOMMU region translates addresses and forwards accesses to a target |
| 795 | * memory region. |
| 796 | * |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 797 | * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION. |
| 798 | * @_iommu_mr should be a pointer to enough memory for an instance of |
| 799 | * that subclass, @instance_size is the size of that subclass, and |
| 800 | * @mrtypename is its name. This function will initialize @_iommu_mr as an |
| 801 | * instance of the subclass, and its methods will then be called to handle |
| 802 | * accesses to the memory region. See the documentation of |
| 803 | * #IOMMUMemoryRegionClass for further details. |
| 804 | * |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 805 | * @_iommu_mr: the #IOMMUMemoryRegion to be initialized |
| 806 | * @instance_size: the IOMMUMemoryRegion subclass instance size |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 807 | * @mrtypename: the type name of the #IOMMUMemoryRegion |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 808 | * @owner: the object that tracks the region's reference count |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 809 | * @name: used for debugging; not visible to the user or ABI |
| 810 | * @size: size of the region. |
| 811 | */ |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 812 | void memory_region_init_iommu(void *_iommu_mr, |
| 813 | size_t instance_size, |
| 814 | const char *mrtypename, |
| 815 | Object *owner, |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 816 | const char *name, |
| 817 | uint64_t size); |
| 818 | |
Jan Kiszka | 1660e72 | 2011-10-23 16:01:19 +0200 | [diff] [blame] | 819 | /** |
Peter Maydell | b08199c | 2017-07-07 15:42:51 +0100 | [diff] [blame] | 820 | * memory_region_init_ram - Initialize RAM memory region. Accesses into the |
| 821 | * region will modify memory directly. |
| 822 | * |
| 823 | * @mr: the #MemoryRegion to be initialized |
| 824 | * @owner: the object that tracks the region's reference count (must be |
| 825 | * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL) |
| 826 | * @name: name of the memory region |
| 827 | * @size: size of the region in bytes |
| 828 | * @errp: pointer to Error*, to store an error if it happens. |
| 829 | * |
| 830 | * This function allocates RAM for a board model or device, and |
| 831 | * arranges for it to be migrated (by calling vmstate_register_ram() |
| 832 | * if @owner is a DeviceState, or vmstate_register_ram_global() if |
| 833 | * @owner is NULL). |
| 834 | * |
| 835 | * TODO: Currently we restrict @owner to being either NULL (for |
| 836 | * global RAM regions with no owner) or devices, so that we can |
| 837 | * give the RAM block a unique name for migration purposes. |
| 838 | * We should lift this restriction and allow arbitrary Objects. |
| 839 | * If you pass a non-NULL non-device @owner then we will assert. |
| 840 | */ |
| 841 | void memory_region_init_ram(MemoryRegion *mr, |
| 842 | struct Object *owner, |
| 843 | const char *name, |
| 844 | uint64_t size, |
| 845 | Error **errp); |
| 846 | |
| 847 | /** |
| 848 | * memory_region_init_rom: Initialize a ROM memory region. |
| 849 | * |
| 850 | * This has the same effect as calling memory_region_init_ram() |
| 851 | * and then marking the resulting region read-only with |
| 852 | * memory_region_set_readonly(). This includes arranging for the |
| 853 | * contents to be migrated. |
| 854 | * |
| 855 | * TODO: Currently we restrict @owner to being either NULL (for |
| 856 | * global RAM regions with no owner) or devices, so that we can |
| 857 | * give the RAM block a unique name for migration purposes. |
| 858 | * We should lift this restriction and allow arbitrary Objects. |
| 859 | * If you pass a non-NULL non-device @owner then we will assert. |
| 860 | * |
| 861 | * @mr: the #MemoryRegion to be initialized. |
| 862 | * @owner: the object that tracks the region's reference count |
| 863 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
| 864 | * must be unique within any device |
| 865 | * @size: size of the region. |
| 866 | * @errp: pointer to Error*, to store an error if it happens. |
| 867 | */ |
| 868 | void memory_region_init_rom(MemoryRegion *mr, |
| 869 | struct Object *owner, |
| 870 | const char *name, |
| 871 | uint64_t size, |
| 872 | Error **errp); |
| 873 | |
| 874 | /** |
| 875 | * memory_region_init_rom_device: Initialize a ROM memory region. |
| 876 | * Writes are handled via callbacks. |
| 877 | * |
| 878 | * This function initializes a memory region backed by RAM for reads |
| 879 | * and callbacks for writes, and arranges for the RAM backing to |
| 880 | * be migrated (by calling vmstate_register_ram() |
| 881 | * if @owner is a DeviceState, or vmstate_register_ram_global() if |
| 882 | * @owner is NULL). |
| 883 | * |
| 884 | * TODO: Currently we restrict @owner to being either NULL (for |
| 885 | * global RAM regions with no owner) or devices, so that we can |
| 886 | * give the RAM block a unique name for migration purposes. |
| 887 | * We should lift this restriction and allow arbitrary Objects. |
| 888 | * If you pass a non-NULL non-device @owner then we will assert. |
| 889 | * |
| 890 | * @mr: the #MemoryRegion to be initialized. |
| 891 | * @owner: the object that tracks the region's reference count |
| 892 | * @ops: callbacks for write access handling (must not be NULL). |
| 893 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
| 894 | * must be unique within any device |
| 895 | * @size: size of the region. |
| 896 | * @errp: pointer to Error*, to store an error if it happens. |
| 897 | */ |
| 898 | void memory_region_init_rom_device(MemoryRegion *mr, |
| 899 | struct Object *owner, |
| 900 | const MemoryRegionOps *ops, |
| 901 | void *opaque, |
| 902 | const char *name, |
| 903 | uint64_t size, |
| 904 | Error **errp); |
| 905 | |
| 906 | |
| 907 | /** |
Paolo Bonzini | 803c081 | 2013-05-07 06:59:09 +0200 | [diff] [blame] | 908 | * memory_region_owner: get a memory region's owner. |
| 909 | * |
| 910 | * @mr: the memory region being queried. |
| 911 | */ |
| 912 | struct Object *memory_region_owner(MemoryRegion *mr); |
| 913 | |
| 914 | /** |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 915 | * memory_region_size: get a memory region's size. |
| 916 | * |
| 917 | * @mr: the memory region being queried. |
| 918 | */ |
| 919 | uint64_t memory_region_size(MemoryRegion *mr); |
| 920 | |
| 921 | /** |
Avi Kivity | 8ea9252 | 2011-12-08 15:58:43 +0200 | [diff] [blame] | 922 | * memory_region_is_ram: check whether a memory region is random access |
| 923 | * |
| 924 | * Returns %true is a memory region is random access. |
| 925 | * |
| 926 | * @mr: the memory region being queried |
| 927 | */ |
Paolo Bonzini | 1619d1f | 2015-12-09 17:47:39 +0100 | [diff] [blame] | 928 | static inline bool memory_region_is_ram(MemoryRegion *mr) |
| 929 | { |
| 930 | return mr->ram; |
| 931 | } |
Avi Kivity | 8ea9252 | 2011-12-08 15:58:43 +0200 | [diff] [blame] | 932 | |
| 933 | /** |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 934 | * memory_region_is_ram_device: check whether a memory region is a ram device |
Nikunj A Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 935 | * |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 936 | * Returns %true is a memory region is a device backed ram region |
Nikunj A Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 937 | * |
| 938 | * @mr: the memory region being queried |
| 939 | */ |
Alex Williamson | 21e00fa | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 940 | bool memory_region_is_ram_device(MemoryRegion *mr); |
Nikunj A Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 941 | |
| 942 | /** |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 943 | * 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] | 944 | * |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 945 | * 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] | 946 | * direct reads. |
| 947 | * |
| 948 | * @mr: the memory region being queried |
| 949 | */ |
| 950 | static inline bool memory_region_is_romd(MemoryRegion *mr) |
| 951 | { |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 952 | return mr->rom_device && mr->romd_mode; |
Blue Swirl | fd06257 | 2012-04-09 17:38:52 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /** |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 956 | * memory_region_get_iommu: check whether a memory region is an iommu |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 957 | * |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 958 | * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu, |
| 959 | * otherwise NULL. |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 960 | * |
| 961 | * @mr: the memory region being queried |
| 962 | */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 963 | static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr) |
Paolo Bonzini | 1619d1f | 2015-12-09 17:47:39 +0100 | [diff] [blame] | 964 | { |
Jason Wang | 12d3788 | 2016-12-30 18:09:18 +0800 | [diff] [blame] | 965 | if (mr->alias) { |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 966 | return memory_region_get_iommu(mr->alias); |
Jason Wang | 12d3788 | 2016-12-30 18:09:18 +0800 | [diff] [blame] | 967 | } |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 968 | if (mr->is_iommu) { |
| 969 | return (IOMMUMemoryRegion *) mr; |
| 970 | } |
| 971 | return NULL; |
Paolo Bonzini | 1619d1f | 2015-12-09 17:47:39 +0100 | [diff] [blame] | 972 | } |
| 973 | |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 974 | /** |
| 975 | * memory_region_get_iommu_class_nocheck: returns iommu memory region class |
| 976 | * if an iommu or NULL if not |
| 977 | * |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 978 | * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu, |
| 979 | * otherwise NULL. This is fast path avoiding QOM checking, use with caution. |
Alexey Kardashevskiy | 1221a47 | 2017-07-11 13:56:20 +1000 | [diff] [blame] | 980 | * |
| 981 | * @mr: the memory region being queried |
| 982 | */ |
| 983 | static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck( |
| 984 | IOMMUMemoryRegion *iommu_mr) |
| 985 | { |
| 986 | return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class); |
| 987 | } |
| 988 | |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 989 | #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL) |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 990 | |
| 991 | /** |
Alexey Kardashevskiy | f682e9c | 2016-06-21 11:14:01 +1000 | [diff] [blame] | 992 | * memory_region_iommu_get_min_page_size: get minimum supported page size |
| 993 | * for an iommu |
| 994 | * |
| 995 | * Returns minimum supported page size for an iommu. |
| 996 | * |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 997 | * @iommu_mr: the memory region being queried |
Alexey Kardashevskiy | f682e9c | 2016-06-21 11:14:01 +1000 | [diff] [blame] | 998 | */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 999 | uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr); |
Alexey Kardashevskiy | f682e9c | 2016-06-21 11:14:01 +1000 | [diff] [blame] | 1000 | |
| 1001 | /** |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1002 | * memory_region_notify_iommu: notify a change in an IOMMU translation entry. |
| 1003 | * |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1004 | * The notification type will be decided by entry.perm bits: |
| 1005 | * |
| 1006 | * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE. |
| 1007 | * - For MAP (newly added entry) notifies: set entry.perm to the |
| 1008 | * permission of the page (which is definitely !IOMMU_NONE). |
| 1009 | * |
| 1010 | * Note: for any IOMMU implementation, an in-place mapping change |
| 1011 | * should be notified with an UNMAP followed by a MAP. |
| 1012 | * |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1013 | * @iommu_mr: the memory region that was changed |
Peter Maydell | cb1efcf | 2018-06-15 14:57:16 +0100 | [diff] [blame^] | 1014 | * @iommu_idx: the IOMMU index for the translation table which has changed |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1015 | * @entry: the new entry in the IOMMU translation table. The entry |
| 1016 | * replaces all old entries for the same virtual I/O address range. |
| 1017 | * Deleted entries have .@perm == 0. |
| 1018 | */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1019 | void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr, |
Peter Maydell | cb1efcf | 2018-06-15 14:57:16 +0100 | [diff] [blame^] | 1020 | int iommu_idx, |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1021 | IOMMUTLBEntry entry); |
| 1022 | |
| 1023 | /** |
Peter Xu | bd2bfa4 | 2017-04-07 18:59:10 +0800 | [diff] [blame] | 1024 | * memory_region_notify_one: notify a change in an IOMMU translation |
| 1025 | * entry to a single notifier |
| 1026 | * |
| 1027 | * This works just like memory_region_notify_iommu(), but it only |
| 1028 | * notifies a specific notifier, not all of them. |
| 1029 | * |
| 1030 | * @notifier: the notifier to be notified |
| 1031 | * @entry: the new entry in the IOMMU translation table. The entry |
| 1032 | * replaces all old entries for the same virtual I/O address range. |
| 1033 | * Deleted entries have .@perm == 0. |
| 1034 | */ |
| 1035 | void memory_region_notify_one(IOMMUNotifier *notifier, |
| 1036 | IOMMUTLBEntry *entry); |
| 1037 | |
| 1038 | /** |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1039 | * memory_region_register_iommu_notifier: register a notifier for changes to |
| 1040 | * IOMMU translation entries. |
| 1041 | * |
| 1042 | * @mr: the memory region to observe |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1043 | * @n: the IOMMUNotifier to be added; the notify callback receives a |
| 1044 | * pointer to an #IOMMUTLBEntry as the opaque value; the pointer |
| 1045 | * ceases to be valid on exit from the notifier. |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1046 | */ |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1047 | void memory_region_register_iommu_notifier(MemoryRegion *mr, |
| 1048 | IOMMUNotifier *n); |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1049 | |
| 1050 | /** |
David Gibson | a788f22 | 2015-09-30 12:13:55 +1000 | [diff] [blame] | 1051 | * memory_region_iommu_replay: replay existing IOMMU translations to |
Alexey Kardashevskiy | f682e9c | 2016-06-21 11:14:01 +1000 | [diff] [blame] | 1052 | * a notifier with the minimum page granularity returned by |
| 1053 | * mr->iommu_ops->get_page_size(). |
David Gibson | a788f22 | 2015-09-30 12:13:55 +1000 | [diff] [blame] | 1054 | * |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1055 | * Note: this is not related to record-and-replay functionality. |
| 1056 | * |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1057 | * @iommu_mr: the memory region to observe |
David Gibson | a788f22 | 2015-09-30 12:13:55 +1000 | [diff] [blame] | 1058 | * @n: the notifier to which to replay iommu mappings |
David Gibson | a788f22 | 2015-09-30 12:13:55 +1000 | [diff] [blame] | 1059 | */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1060 | void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n); |
David Gibson | a788f22 | 2015-09-30 12:13:55 +1000 | [diff] [blame] | 1061 | |
| 1062 | /** |
Peter Xu | de472e4 | 2017-04-07 18:59:09 +0800 | [diff] [blame] | 1063 | * memory_region_iommu_replay_all: replay existing IOMMU translations |
| 1064 | * to all the notifiers registered. |
| 1065 | * |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1066 | * Note: this is not related to record-and-replay functionality. |
| 1067 | * |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1068 | * @iommu_mr: the memory region to observe |
Peter Xu | de472e4 | 2017-04-07 18:59:09 +0800 | [diff] [blame] | 1069 | */ |
Alexey Kardashevskiy | 3df9d74 | 2017-07-11 13:56:19 +1000 | [diff] [blame] | 1070 | void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr); |
Peter Xu | de472e4 | 2017-04-07 18:59:09 +0800 | [diff] [blame] | 1071 | |
| 1072 | /** |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1073 | * memory_region_unregister_iommu_notifier: unregister a notifier for |
| 1074 | * changes to IOMMU translation entries. |
| 1075 | * |
Alexey Kardashevskiy | d22d895 | 2016-06-30 13:00:23 -0600 | [diff] [blame] | 1076 | * @mr: the memory region which was observed and for which notity_stopped() |
| 1077 | * needs to be called |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1078 | * @n: the notifier to be removed. |
| 1079 | */ |
Peter Xu | cdb3081 | 2016-09-23 13:02:26 +0800 | [diff] [blame] | 1080 | void memory_region_unregister_iommu_notifier(MemoryRegion *mr, |
| 1081 | IOMMUNotifier *n); |
David Gibson | 0686657 | 2013-05-14 19:13:56 +1000 | [diff] [blame] | 1082 | |
| 1083 | /** |
Alexey Kardashevskiy | f1334de | 2018-02-06 11:08:24 -0700 | [diff] [blame] | 1084 | * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is |
| 1085 | * defined on the IOMMU. |
| 1086 | * |
Peter Maydell | 2ce931d | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1087 | * Returns 0 on success, or a negative errno otherwise. In particular, |
| 1088 | * -EINVAL indicates that the IOMMU does not support the requested |
| 1089 | * attribute. |
Alexey Kardashevskiy | f1334de | 2018-02-06 11:08:24 -0700 | [diff] [blame] | 1090 | * |
| 1091 | * @iommu_mr: the memory region |
| 1092 | * @attr: the requested attribute |
| 1093 | * @data: a pointer to the requested attribute data |
| 1094 | */ |
| 1095 | int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr, |
| 1096 | enum IOMMUMemoryRegionAttr attr, |
| 1097 | void *data); |
| 1098 | |
| 1099 | /** |
Peter Maydell | 21f4020 | 2018-06-15 14:57:15 +0100 | [diff] [blame] | 1100 | * memory_region_iommu_attrs_to_index: return the IOMMU index to |
| 1101 | * use for translations with the given memory transaction attributes. |
| 1102 | * |
| 1103 | * @iommu_mr: the memory region |
| 1104 | * @attrs: the memory transaction attributes |
| 1105 | */ |
| 1106 | int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr, |
| 1107 | MemTxAttrs attrs); |
| 1108 | |
| 1109 | /** |
| 1110 | * memory_region_iommu_num_indexes: return the total number of IOMMU |
| 1111 | * indexes that this IOMMU supports. |
| 1112 | * |
| 1113 | * @iommu_mr: the memory region |
| 1114 | */ |
| 1115 | int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr); |
| 1116 | |
| 1117 | /** |
Avi Kivity | 8991c79 | 2011-12-20 15:53:11 +0200 | [diff] [blame] | 1118 | * memory_region_name: get a memory region's name |
| 1119 | * |
| 1120 | * Returns the string that was used to initialize the memory region. |
| 1121 | * |
| 1122 | * @mr: the memory region being queried |
| 1123 | */ |
Peter Crosthwaite | 5d546d4 | 2014-08-14 23:55:03 -0700 | [diff] [blame] | 1124 | const char *memory_region_name(const MemoryRegion *mr); |
Avi Kivity | 8991c79 | 2011-12-20 15:53:11 +0200 | [diff] [blame] | 1125 | |
| 1126 | /** |
Avi Kivity | 55043ba | 2011-12-15 17:20:34 +0200 | [diff] [blame] | 1127 | * memory_region_is_logging: return whether a memory region is logging writes |
| 1128 | * |
Paolo Bonzini | 2d1a35b | 2015-03-23 10:50:57 +0100 | [diff] [blame] | 1129 | * Returns %true if the memory region is logging writes for the given client |
| 1130 | * |
| 1131 | * @mr: the memory region being queried |
| 1132 | * @client: the client being queried |
| 1133 | */ |
| 1134 | bool memory_region_is_logging(MemoryRegion *mr, uint8_t client); |
| 1135 | |
| 1136 | /** |
| 1137 | * memory_region_get_dirty_log_mask: return the clients for which a |
| 1138 | * memory region is logging writes. |
| 1139 | * |
Paolo Bonzini | 677e780 | 2015-03-23 10:53:21 +0100 | [diff] [blame] | 1140 | * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants |
| 1141 | * are the bit indices. |
Avi Kivity | 55043ba | 2011-12-15 17:20:34 +0200 | [diff] [blame] | 1142 | * |
| 1143 | * @mr: the memory region being queried |
| 1144 | */ |
Paolo Bonzini | 2d1a35b | 2015-03-23 10:50:57 +0100 | [diff] [blame] | 1145 | uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr); |
Avi Kivity | 55043ba | 2011-12-15 17:20:34 +0200 | [diff] [blame] | 1146 | |
| 1147 | /** |
Avi Kivity | ce7923d | 2011-12-08 16:05:11 +0200 | [diff] [blame] | 1148 | * memory_region_is_rom: check whether a memory region is ROM |
| 1149 | * |
| 1150 | * Returns %true is a memory region is read-only memory. |
| 1151 | * |
| 1152 | * @mr: the memory region being queried |
| 1153 | */ |
Paolo Bonzini | 1619d1f | 2015-12-09 17:47:39 +0100 | [diff] [blame] | 1154 | static inline bool memory_region_is_rom(MemoryRegion *mr) |
| 1155 | { |
| 1156 | return mr->ram && mr->readonly; |
| 1157 | } |
| 1158 | |
Avi Kivity | ce7923d | 2011-12-08 16:05:11 +0200 | [diff] [blame] | 1159 | |
| 1160 | /** |
Paolo Bonzini | a35ba7b | 2014-06-10 19:15:23 +0800 | [diff] [blame] | 1161 | * memory_region_get_fd: Get a file descriptor backing a RAM memory region. |
| 1162 | * |
| 1163 | * Returns a file descriptor backing a file-based RAM memory region, |
| 1164 | * or -1 if the region is not a file-based RAM memory region. |
| 1165 | * |
| 1166 | * @mr: the RAM or alias memory region being queried. |
| 1167 | */ |
| 1168 | int memory_region_get_fd(MemoryRegion *mr); |
| 1169 | |
| 1170 | /** |
Paolo Bonzini | 07bdaa4 | 2016-03-25 12:55:08 +0100 | [diff] [blame] | 1171 | * memory_region_from_host: Convert a pointer into a RAM memory region |
| 1172 | * and an offset within it. |
| 1173 | * |
| 1174 | * Given a host pointer inside a RAM memory region (created with |
| 1175 | * memory_region_init_ram() or memory_region_init_ram_ptr()), return |
| 1176 | * the MemoryRegion and the offset within it. |
| 1177 | * |
| 1178 | * Use with care; by the time this function returns, the returned pointer is |
| 1179 | * not protected by RCU anymore. If the caller is not within an RCU critical |
| 1180 | * section and does not hold the iothread lock, it must have other means of |
| 1181 | * protecting the pointer, such as a reference to the region that includes |
| 1182 | * the incoming ram_addr_t. |
| 1183 | * |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 1184 | * @ptr: the host pointer to be converted |
| 1185 | * @offset: the offset within memory region |
Paolo Bonzini | 07bdaa4 | 2016-03-25 12:55:08 +0100 | [diff] [blame] | 1186 | */ |
| 1187 | MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset); |
| 1188 | |
| 1189 | /** |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1190 | * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. |
| 1191 | * |
| 1192 | * Returns a host pointer to a RAM memory region (created with |
Paolo Bonzini | 49b24af | 2015-12-16 10:30:47 +0100 | [diff] [blame] | 1193 | * memory_region_init_ram() or memory_region_init_ram_ptr()). |
| 1194 | * |
| 1195 | * Use with care; by the time this function returns, the returned pointer is |
| 1196 | * not protected by RCU anymore. If the caller is not within an RCU critical |
| 1197 | * section and does not hold the iothread lock, it must have other means of |
| 1198 | * protecting the pointer, such as a reference to the region that includes |
| 1199 | * the incoming ram_addr_t. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1200 | * |
| 1201 | * @mr: the memory region being queried. |
| 1202 | */ |
| 1203 | void *memory_region_get_ram_ptr(MemoryRegion *mr); |
| 1204 | |
Paolo Bonzini | 37d7c08 | 2015-03-23 10:21:46 +0100 | [diff] [blame] | 1205 | /* memory_region_ram_resize: Resize a RAM region. |
| 1206 | * |
| 1207 | * Only legal before guest might have detected the memory size: e.g. on |
| 1208 | * incoming migration, or right after reset. |
| 1209 | * |
| 1210 | * @mr: a memory region created with @memory_region_init_resizeable_ram. |
| 1211 | * @newsize: the new size the region |
| 1212 | * @errp: pointer to Error*, to store an error if it happens. |
| 1213 | */ |
| 1214 | void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, |
| 1215 | Error **errp); |
| 1216 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1217 | /** |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1218 | * memory_region_set_log: Turn dirty logging on or off for a region. |
| 1219 | * |
| 1220 | * Turns dirty logging on or off for a specified client (display, migration). |
| 1221 | * Only meaningful for RAM regions. |
| 1222 | * |
| 1223 | * @mr: the memory region being updated. |
| 1224 | * @log: whether dirty logging is to be enabled or disabled. |
Paolo Bonzini | dbddac6 | 2015-03-23 10:31:53 +0100 | [diff] [blame] | 1225 | * @client: the user of the logging information; %DIRTY_MEMORY_VGA only. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1226 | */ |
| 1227 | void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client); |
| 1228 | |
| 1229 | /** |
Blue Swirl | cd7a45c | 2012-01-22 16:38:21 +0000 | [diff] [blame] | 1230 | * memory_region_get_dirty: Check whether a range of bytes is dirty |
| 1231 | * for a specified client. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1232 | * |
Blue Swirl | cd7a45c | 2012-01-22 16:38:21 +0000 | [diff] [blame] | 1233 | * 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] | 1234 | * call to memory_region_reset_dirty() with the same @client. Dirty logging |
| 1235 | * must be enabled. |
| 1236 | * |
| 1237 | * @mr: the memory region being queried. |
| 1238 | * @addr: the address (relative to the start of the region) being queried. |
Blue Swirl | cd7a45c | 2012-01-22 16:38:21 +0000 | [diff] [blame] | 1239 | * @size: the size of the range being queried. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1240 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or |
| 1241 | * %DIRTY_MEMORY_VGA. |
| 1242 | */ |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1243 | bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr, |
| 1244 | hwaddr size, unsigned client); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1245 | |
| 1246 | /** |
Blue Swirl | fd4aa97 | 2011-10-16 16:04:59 +0000 | [diff] [blame] | 1247 | * 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] | 1248 | * |
Blue Swirl | fd4aa97 | 2011-10-16 16:04:59 +0000 | [diff] [blame] | 1249 | * Marks a range of bytes as dirty, after it has been dirtied outside |
| 1250 | * guest code. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1251 | * |
Blue Swirl | fd4aa97 | 2011-10-16 16:04:59 +0000 | [diff] [blame] | 1252 | * @mr: the memory region being dirtied. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1253 | * @addr: the address (relative to the start of the region) being dirtied. |
Blue Swirl | fd4aa97 | 2011-10-16 16:04:59 +0000 | [diff] [blame] | 1254 | * @size: size of the range being dirtied. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1255 | */ |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1256 | void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, |
| 1257 | hwaddr size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1258 | |
| 1259 | /** |
Gerd Hoffmann | 8deaf12 | 2017-04-21 11:16:25 +0200 | [diff] [blame] | 1260 | * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty |
| 1261 | * bitmap and clear it. |
| 1262 | * |
| 1263 | * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and |
| 1264 | * returns the snapshot. The snapshot can then be used to query dirty |
Paolo Bonzini | 77302fb | 2018-02-06 18:30:04 +0100 | [diff] [blame] | 1265 | * status, using memory_region_snapshot_get_dirty. Snapshotting allows |
| 1266 | * querying the same page multiple times, which is especially useful for |
| 1267 | * display updates where the scanlines often are not page aligned. |
Gerd Hoffmann | 8deaf12 | 2017-04-21 11:16:25 +0200 | [diff] [blame] | 1268 | * |
| 1269 | * The dirty bitmap region which gets copyed into the snapshot (and |
| 1270 | * cleared afterwards) can be larger than requested. The boundaries |
| 1271 | * are rounded up/down so complete bitmap longs (covering 64 pages on |
| 1272 | * 64bit hosts) can be copied over into the bitmap snapshot. Which |
| 1273 | * isn't a problem for display updates as the extra pages are outside |
| 1274 | * the visible area, and in case the visible area changes a full |
| 1275 | * display redraw is due anyway. Should other use cases for this |
| 1276 | * function emerge we might have to revisit this implementation |
| 1277 | * detail. |
| 1278 | * |
| 1279 | * Use g_free to release DirtyBitmapSnapshot. |
| 1280 | * |
| 1281 | * @mr: the memory region being queried. |
| 1282 | * @addr: the address (relative to the start of the region) being queried. |
| 1283 | * @size: the size of the range being queried. |
| 1284 | * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA. |
| 1285 | */ |
| 1286 | DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr, |
| 1287 | hwaddr addr, |
| 1288 | hwaddr size, |
| 1289 | unsigned client); |
| 1290 | |
| 1291 | /** |
| 1292 | * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty |
| 1293 | * in the specified dirty bitmap snapshot. |
| 1294 | * |
| 1295 | * @mr: the memory region being queried. |
| 1296 | * @snap: the dirty bitmap snapshot |
| 1297 | * @addr: the address (relative to the start of the region) being queried. |
| 1298 | * @size: the size of the range being queried. |
| 1299 | */ |
| 1300 | bool memory_region_snapshot_get_dirty(MemoryRegion *mr, |
| 1301 | DirtyBitmapSnapshot *snap, |
| 1302 | hwaddr addr, hwaddr size); |
| 1303 | |
Juan Quintela | 6c279db | 2012-10-17 20:24:28 +0200 | [diff] [blame] | 1304 | /** |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1305 | * memory_region_reset_dirty: Mark a range of pages as clean, for a specified |
| 1306 | * client. |
| 1307 | * |
| 1308 | * Marks a range of pages as no longer dirty. |
| 1309 | * |
| 1310 | * @mr: the region being updated. |
| 1311 | * @addr: the start of the subrange being cleaned. |
| 1312 | * @size: the size of the subrange being cleaned. |
| 1313 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or |
| 1314 | * %DIRTY_MEMORY_VGA. |
| 1315 | */ |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1316 | void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, |
| 1317 | hwaddr size, unsigned client); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1318 | |
| 1319 | /** |
| 1320 | * memory_region_set_readonly: Turn a memory region read-only (or read-write) |
| 1321 | * |
| 1322 | * Allows a memory region to be marked as read-only (turning it into a ROM). |
| 1323 | * only useful on RAM regions. |
| 1324 | * |
| 1325 | * @mr: the region being updated. |
| 1326 | * @readonly: whether rhe region is to be ROM or RAM. |
| 1327 | */ |
| 1328 | void memory_region_set_readonly(MemoryRegion *mr, bool readonly); |
| 1329 | |
| 1330 | /** |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 1331 | * memory_region_rom_device_set_romd: enable/disable ROMD mode |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1332 | * |
| 1333 | * Allows a ROM device (initialized with memory_region_init_rom_device() to |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 1334 | * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the |
| 1335 | * device is mapped to guest memory and satisfies read access directly. |
| 1336 | * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function. |
| 1337 | * Writes are always handled by the #MemoryRegion.write function. |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1338 | * |
| 1339 | * @mr: the memory region to be updated |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 1340 | * @romd_mode: %true to put the region into ROMD mode |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1341 | */ |
Jan Kiszka | 5f9a5ea | 2013-05-07 19:04:25 +0200 | [diff] [blame] | 1342 | void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 1343 | |
| 1344 | /** |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1345 | * memory_region_set_coalescing: Enable memory coalescing for the region. |
| 1346 | * |
| 1347 | * Enabled writes to a region to be queued for later processing. MMIO ->write |
| 1348 | * callbacks may be delayed until a non-coalesced MMIO is issued. |
| 1349 | * Only useful for IO regions. Roughly similar to write-combining hardware. |
| 1350 | * |
| 1351 | * @mr: the memory region to be write coalesced |
| 1352 | */ |
| 1353 | void memory_region_set_coalescing(MemoryRegion *mr); |
| 1354 | |
| 1355 | /** |
| 1356 | * memory_region_add_coalescing: Enable memory coalescing for a sub-range of |
| 1357 | * a region. |
| 1358 | * |
| 1359 | * Like memory_region_set_coalescing(), but works on a sub-range of a region. |
| 1360 | * Multiple calls can be issued coalesced disjoint ranges. |
| 1361 | * |
| 1362 | * @mr: the memory region to be updated. |
| 1363 | * @offset: the start of the range within the region to be coalesced. |
| 1364 | * @size: the size of the subrange to be coalesced. |
| 1365 | */ |
| 1366 | void memory_region_add_coalescing(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1367 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1368 | uint64_t size); |
| 1369 | |
| 1370 | /** |
| 1371 | * memory_region_clear_coalescing: Disable MMIO coalescing for the region. |
| 1372 | * |
| 1373 | * Disables any coalescing caused by memory_region_set_coalescing() or |
| 1374 | * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory |
| 1375 | * hardware. |
| 1376 | * |
| 1377 | * @mr: the memory region to be updated. |
| 1378 | */ |
| 1379 | void memory_region_clear_coalescing(MemoryRegion *mr); |
| 1380 | |
| 1381 | /** |
Jan Kiszka | d410515 | 2012-08-23 13:02:29 +0200 | [diff] [blame] | 1382 | * memory_region_set_flush_coalesced: Enforce memory coalescing flush before |
| 1383 | * accesses. |
| 1384 | * |
| 1385 | * Ensure that pending coalesced MMIO request are flushed before the memory |
| 1386 | * region is accessed. This property is automatically enabled for all regions |
| 1387 | * passed to memory_region_set_coalescing() and memory_region_add_coalescing(). |
| 1388 | * |
| 1389 | * @mr: the memory region to be updated. |
| 1390 | */ |
| 1391 | void memory_region_set_flush_coalesced(MemoryRegion *mr); |
| 1392 | |
| 1393 | /** |
| 1394 | * memory_region_clear_flush_coalesced: Disable memory coalescing flush before |
| 1395 | * accesses. |
| 1396 | * |
| 1397 | * Clear the automatic coalesced MMIO flushing enabled via |
| 1398 | * memory_region_set_flush_coalesced. Note that this service has no effect on |
| 1399 | * memory regions that have MMIO coalescing enabled for themselves. For them, |
| 1400 | * automatic flushing will stop once coalescing is disabled. |
| 1401 | * |
| 1402 | * @mr: the memory region to be updated. |
| 1403 | */ |
| 1404 | void memory_region_clear_flush_coalesced(MemoryRegion *mr); |
| 1405 | |
| 1406 | /** |
Jan Kiszka | 196ea13 | 2015-06-18 18:47:20 +0200 | [diff] [blame] | 1407 | * memory_region_clear_global_locking: Declares that access processing does |
| 1408 | * not depend on the QEMU global lock. |
| 1409 | * |
| 1410 | * By clearing this property, accesses to the memory region will be processed |
| 1411 | * outside of QEMU's global lock (unless the lock is held on when issuing the |
| 1412 | * access request). In this case, the device model implementing the access |
| 1413 | * handlers is responsible for synchronization of concurrency. |
| 1414 | * |
| 1415 | * @mr: the memory region to be updated. |
| 1416 | */ |
| 1417 | void memory_region_clear_global_locking(MemoryRegion *mr); |
| 1418 | |
| 1419 | /** |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1420 | * memory_region_add_eventfd: Request an eventfd to be triggered when a word |
| 1421 | * is written to a location. |
| 1422 | * |
| 1423 | * Marks a word in an IO region (initialized with memory_region_init_io()) |
| 1424 | * 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] | 1425 | * 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] | 1426 | * action if the callback _is_ called). |
| 1427 | * |
| 1428 | * @mr: the memory region being updated. |
| 1429 | * @addr: the address within @mr that is to be monitored |
| 1430 | * @size: the size of the access to trigger the eventfd |
| 1431 | * @match_data: whether to match against @data, instead of just @addr |
| 1432 | * @data: the data to match against the guest write |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 1433 | * @e: event notifier to be triggered when @addr, @size, and @data all match. |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1434 | **/ |
| 1435 | void memory_region_add_eventfd(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1436 | hwaddr addr, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1437 | unsigned size, |
| 1438 | bool match_data, |
| 1439 | uint64_t data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 1440 | EventNotifier *e); |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1441 | |
| 1442 | /** |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 1443 | * memory_region_del_eventfd: Cancel an eventfd. |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1444 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 1445 | * Cancels an eventfd trigger requested by a previous |
| 1446 | * memory_region_add_eventfd() call. |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1447 | * |
| 1448 | * @mr: the memory region being updated. |
| 1449 | * @addr: the address within @mr that is to be monitored |
| 1450 | * @size: the size of the access to trigger the eventfd |
| 1451 | * @match_data: whether to match against @data, instead of just @addr |
| 1452 | * @data: the data to match against the guest write |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 1453 | * @e: event notifier to be triggered when @addr, @size, and @data all match. |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1454 | */ |
| 1455 | void memory_region_del_eventfd(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1456 | hwaddr addr, |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1457 | unsigned size, |
| 1458 | bool match_data, |
| 1459 | uint64_t data, |
Paolo Bonzini | 753d5e1 | 2012-07-05 17:16:27 +0200 | [diff] [blame] | 1460 | EventNotifier *e); |
| 1461 | |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 1462 | /** |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 1463 | * memory_region_add_subregion: Add a subregion to a container. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1464 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 1465 | * Adds a subregion at @offset. The subregion may not overlap with other |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1466 | * subregions (except for those explicitly marked as overlapping). A region |
| 1467 | * may only be added once as a subregion (unless removed with |
| 1468 | * memory_region_del_subregion()); use memory_region_init_alias() if you |
| 1469 | * want a region to be a subregion in multiple locations. |
| 1470 | * |
| 1471 | * @mr: the region to contain the new subregion; must be a container |
| 1472 | * initialized with memory_region_init(). |
| 1473 | * @offset: the offset relative to @mr where @subregion is added. |
| 1474 | * @subregion: the subregion to be added. |
| 1475 | */ |
| 1476 | void memory_region_add_subregion(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1477 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1478 | MemoryRegion *subregion); |
| 1479 | /** |
BALATON Zoltan | 1a7e8ca | 2012-08-22 17:18:38 +0200 | [diff] [blame] | 1480 | * memory_region_add_subregion_overlap: Add a subregion to a container |
| 1481 | * with overlap. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1482 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 1483 | * Adds a subregion at @offset. The subregion may overlap with other |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1484 | * subregions. Conflicts are resolved by having a higher @priority hide a |
| 1485 | * lower @priority. Subregions without priority are taken as @priority 0. |
| 1486 | * A region may only be added once as a subregion (unless removed with |
| 1487 | * memory_region_del_subregion()); use memory_region_init_alias() if you |
| 1488 | * want a region to be a subregion in multiple locations. |
| 1489 | * |
| 1490 | * @mr: the region to contain the new subregion; must be a container |
| 1491 | * initialized with memory_region_init(). |
| 1492 | * @offset: the offset relative to @mr where @subregion is added. |
| 1493 | * @subregion: the subregion to be added. |
| 1494 | * @priority: used for resolving overlaps; highest priority wins. |
| 1495 | */ |
| 1496 | void memory_region_add_subregion_overlap(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1497 | hwaddr offset, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1498 | MemoryRegion *subregion, |
Marcel Apfelbaum | a1ff8ae | 2013-09-16 11:21:14 +0300 | [diff] [blame] | 1499 | int priority); |
Avi Kivity | e34911c | 2011-12-19 12:06:23 +0200 | [diff] [blame] | 1500 | |
| 1501 | /** |
| 1502 | * memory_region_get_ram_addr: Get the ram address associated with a memory |
| 1503 | * region |
Avi Kivity | e34911c | 2011-12-19 12:06:23 +0200 | [diff] [blame] | 1504 | */ |
Fam Zheng | 7ebb274 | 2016-03-01 14:18:20 +0800 | [diff] [blame] | 1505 | ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr); |
Avi Kivity | e34911c | 2011-12-19 12:06:23 +0200 | [diff] [blame] | 1506 | |
Igor Mammedov | a2b257d | 2014-10-31 16:38:37 +0000 | [diff] [blame] | 1507 | uint64_t memory_region_get_alignment(const MemoryRegion *mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 1508 | /** |
| 1509 | * memory_region_del_subregion: Remove a subregion. |
| 1510 | * |
| 1511 | * Removes a subregion from its container. |
| 1512 | * |
| 1513 | * @mr: the container to be updated. |
| 1514 | * @subregion: the region being removed; must be a current subregion of @mr. |
| 1515 | */ |
| 1516 | void memory_region_del_subregion(MemoryRegion *mr, |
| 1517 | MemoryRegion *subregion); |
| 1518 | |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 1519 | /* |
| 1520 | * memory_region_set_enabled: dynamically enable or disable a region |
| 1521 | * |
| 1522 | * Enables or disables a memory region. A disabled memory region |
| 1523 | * ignores all accesses to itself and its subregions. It does not |
| 1524 | * obscure sibling subregions with lower priority - it simply behaves as |
| 1525 | * if it was removed from the hierarchy. |
| 1526 | * |
| 1527 | * Regions default to being enabled. |
| 1528 | * |
| 1529 | * @mr: the region to be updated |
| 1530 | * @enabled: whether to enable or disable the region |
| 1531 | */ |
| 1532 | void memory_region_set_enabled(MemoryRegion *mr, bool enabled); |
| 1533 | |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1534 | /* |
| 1535 | * memory_region_set_address: dynamically update the address of a region |
| 1536 | * |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1537 | * Dynamically updates the address of a region, relative to its container. |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1538 | * May be used on regions are currently part of a memory hierarchy. |
| 1539 | * |
| 1540 | * @mr: the region to be updated |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1541 | * @addr: new address, relative to container region |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1542 | */ |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1543 | void memory_region_set_address(MemoryRegion *mr, hwaddr addr); |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 1544 | |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 1545 | /* |
Michael S. Tsirkin | e7af4c6 | 2014-12-16 11:21:23 +0200 | [diff] [blame] | 1546 | * memory_region_set_size: dynamically update the size of a region. |
| 1547 | * |
| 1548 | * Dynamically updates the size of a region. |
| 1549 | * |
| 1550 | * @mr: the region to be updated |
| 1551 | * @size: used size of the region. |
| 1552 | */ |
| 1553 | void memory_region_set_size(MemoryRegion *mr, uint64_t size); |
| 1554 | |
| 1555 | /* |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 1556 | * memory_region_set_alias_offset: dynamically update a memory alias's offset |
| 1557 | * |
| 1558 | * Dynamically updates the offset into the target region that an alias points |
| 1559 | * to, as if the fourth argument to memory_region_init_alias() has changed. |
| 1560 | * |
| 1561 | * @mr: the #MemoryRegion to be updated; should be an alias. |
| 1562 | * @offset: the new offset into the target memory region |
| 1563 | */ |
| 1564 | void memory_region_set_alias_offset(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1565 | hwaddr offset); |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 1566 | |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 1567 | /** |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1568 | * memory_region_present: checks if an address relative to a @container |
| 1569 | * translates into #MemoryRegion within @container |
Paolo Bonzini | 3ce1090 | 2013-07-02 13:40:48 +0200 | [diff] [blame] | 1570 | * |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1571 | * Answer whether a #MemoryRegion within @container covers the address |
Paolo Bonzini | 3ce1090 | 2013-07-02 13:40:48 +0200 | [diff] [blame] | 1572 | * @addr. |
| 1573 | * |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1574 | * @container: a #MemoryRegion within which @addr is a relative address |
| 1575 | * @addr: the area within @container to be searched |
Paolo Bonzini | 3ce1090 | 2013-07-02 13:40:48 +0200 | [diff] [blame] | 1576 | */ |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1577 | bool memory_region_present(MemoryRegion *container, hwaddr addr); |
Paolo Bonzini | 3ce1090 | 2013-07-02 13:40:48 +0200 | [diff] [blame] | 1578 | |
| 1579 | /** |
Igor Mammedov | eed2bac | 2014-06-02 15:25:06 +0200 | [diff] [blame] | 1580 | * memory_region_is_mapped: returns true if #MemoryRegion is mapped |
| 1581 | * into any address space. |
| 1582 | * |
| 1583 | * @mr: a #MemoryRegion which should be checked if it's mapped |
| 1584 | */ |
| 1585 | bool memory_region_is_mapped(MemoryRegion *mr); |
| 1586 | |
| 1587 | /** |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1588 | * memory_region_find: translate an address/size relative to a |
| 1589 | * MemoryRegion into a #MemoryRegionSection. |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1590 | * |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1591 | * Locates the first #MemoryRegion within @mr that overlaps the range |
| 1592 | * given by @addr and @size. |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1593 | * |
| 1594 | * Returns a #MemoryRegionSection that describes a contiguous overlap. |
| 1595 | * It will have the following characteristics: |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1596 | * .@size = 0 iff no overlap was found |
| 1597 | * .@mr is non-%NULL iff an overlap was found |
| 1598 | * |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1599 | * Remember that in the return value the @offset_within_region is |
| 1600 | * relative to the returned region (in the .@mr field), not to the |
| 1601 | * @mr argument. |
| 1602 | * |
| 1603 | * Similarly, the .@offset_within_address_space is relative to the |
| 1604 | * address space that contains both regions, the passed and the |
| 1605 | * returned one. However, in the special case where the @mr argument |
Paolo Bonzini | feca4ac | 2014-06-11 11:18:09 +0200 | [diff] [blame] | 1606 | * 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] | 1607 | * following will hold: |
| 1608 | * .@offset_within_address_space >= @addr |
| 1609 | * .@offset_within_address_space + .@size <= @addr + @size |
| 1610 | * |
| 1611 | * @mr: a MemoryRegion within which @addr is a relative address |
| 1612 | * @addr: start of the area within @as to be searched |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1613 | * @size: size of the area to be searched |
| 1614 | */ |
Paolo Bonzini | 73034e9 | 2013-05-07 15:48:28 +0200 | [diff] [blame] | 1615 | MemoryRegionSection memory_region_find(MemoryRegion *mr, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1616 | hwaddr addr, uint64_t size); |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1617 | |
Blue Swirl | fd06257 | 2012-04-09 17:38:52 +0000 | [diff] [blame] | 1618 | /** |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 1619 | * memory_global_dirty_log_sync: synchronize the dirty log for all memory |
Avi Kivity | 86e775c | 2011-12-15 16:24:49 +0200 | [diff] [blame] | 1620 | * |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 1621 | * Synchronizes the dirty page log for all address spaces. |
Avi Kivity | 86e775c | 2011-12-15 16:24:49 +0200 | [diff] [blame] | 1622 | */ |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 1623 | void memory_global_dirty_log_sync(void); |
Avi Kivity | 86e775c | 2011-12-15 16:24:49 +0200 | [diff] [blame] | 1624 | |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 1625 | /** |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 1626 | * memory_region_transaction_begin: Start a transaction. |
| 1627 | * |
| 1628 | * During a transaction, changes will be accumulated and made visible |
Stefan Weil | dabdf39 | 2012-01-08 19:35:09 +0100 | [diff] [blame] | 1629 | * only when the transaction ends (is committed). |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 1630 | */ |
| 1631 | void memory_region_transaction_begin(void); |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 1632 | |
| 1633 | /** |
| 1634 | * memory_region_transaction_commit: Commit a transaction and make changes |
| 1635 | * visible to the guest. |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 1636 | */ |
| 1637 | void memory_region_transaction_commit(void); |
| 1638 | |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1639 | /** |
| 1640 | * memory_listener_register: register callbacks to be called when memory |
| 1641 | * sections are mapped or unmapped into an address |
| 1642 | * space |
| 1643 | * |
| 1644 | * @listener: an object containing the callbacks to be called |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame] | 1645 | * @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] | 1646 | */ |
Avi Kivity | f6790af | 2012-10-02 20:13:51 +0200 | [diff] [blame] | 1647 | void memory_listener_register(MemoryListener *listener, AddressSpace *filter); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1648 | |
| 1649 | /** |
| 1650 | * memory_listener_unregister: undo the effect of memory_listener_register() |
| 1651 | * |
| 1652 | * @listener: an object containing the callbacks to be removed |
| 1653 | */ |
| 1654 | void memory_listener_unregister(MemoryListener *listener); |
| 1655 | |
| 1656 | /** |
| 1657 | * memory_global_dirty_log_start: begin dirty logging for all regions |
| 1658 | */ |
| 1659 | void memory_global_dirty_log_start(void); |
| 1660 | |
| 1661 | /** |
BALATON Zoltan | 1a7e8ca | 2012-08-22 17:18:38 +0200 | [diff] [blame] | 1662 | * memory_global_dirty_log_stop: end dirty logging for all regions |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 1663 | */ |
| 1664 | void memory_global_dirty_log_stop(void); |
| 1665 | |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 1666 | void mtree_info(fprintf_function mon_printf, void *f, bool flatview, |
| 1667 | bool dispatch_tree); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1668 | |
Avi Kivity | 9ad2bbc | 2012-10-02 14:59:23 +0200 | [diff] [blame] | 1669 | /** |
KONRAD Frederic | c935674 | 2016-10-19 15:06:49 +0200 | [diff] [blame] | 1670 | * memory_region_request_mmio_ptr: request a pointer to an mmio |
| 1671 | * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer. |
| 1672 | * When the device wants to invalidate the pointer it will call |
| 1673 | * memory_region_invalidate_mmio_ptr. |
| 1674 | * |
| 1675 | * @mr: #MemoryRegion to check |
| 1676 | * @addr: address within that region |
| 1677 | * |
| 1678 | * Returns true on success, false otherwise. |
| 1679 | */ |
| 1680 | bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr); |
| 1681 | |
| 1682 | /** |
| 1683 | * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio |
| 1684 | * previously requested. |
| 1685 | * In the end that means that if something wants to execute from this area it |
| 1686 | * will need to request the pointer again. |
| 1687 | * |
| 1688 | * @mr: #MemoryRegion associated to the pointer. |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 1689 | * @offset: offset within the memory region |
KONRAD Frederic | c935674 | 2016-10-19 15:06:49 +0200 | [diff] [blame] | 1690 | * @size: size of that area. |
| 1691 | */ |
| 1692 | void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset, |
| 1693 | unsigned size); |
| 1694 | |
| 1695 | /** |
Peter Maydell | 3b64349 | 2015-04-26 16:49:23 +0100 | [diff] [blame] | 1696 | * memory_region_dispatch_read: perform a read directly to the specified |
| 1697 | * MemoryRegion. |
| 1698 | * |
| 1699 | * @mr: #MemoryRegion to access |
| 1700 | * @addr: address within that region |
| 1701 | * @pval: pointer to uint64_t which the data is written to |
| 1702 | * @size: size of the access in bytes |
| 1703 | * @attrs: memory transaction attributes to use for the access |
| 1704 | */ |
| 1705 | MemTxResult memory_region_dispatch_read(MemoryRegion *mr, |
| 1706 | hwaddr addr, |
| 1707 | uint64_t *pval, |
| 1708 | unsigned size, |
| 1709 | MemTxAttrs attrs); |
| 1710 | /** |
| 1711 | * memory_region_dispatch_write: perform a write directly to the specified |
| 1712 | * MemoryRegion. |
| 1713 | * |
| 1714 | * @mr: #MemoryRegion to access |
| 1715 | * @addr: address within that region |
| 1716 | * @data: data to write |
| 1717 | * @size: size of the access in bytes |
| 1718 | * @attrs: memory transaction attributes to use for the access |
| 1719 | */ |
| 1720 | MemTxResult memory_region_dispatch_write(MemoryRegion *mr, |
| 1721 | hwaddr addr, |
| 1722 | uint64_t data, |
| 1723 | unsigned size, |
| 1724 | MemTxAttrs attrs); |
| 1725 | |
| 1726 | /** |
Avi Kivity | 9ad2bbc | 2012-10-02 14:59:23 +0200 | [diff] [blame] | 1727 | * address_space_init: initializes an address space |
| 1728 | * |
| 1729 | * @as: an uninitialized #AddressSpace |
Veres Lajos | 67cc32e | 2015-09-08 22:45:14 +0100 | [diff] [blame] | 1730 | * @root: a #MemoryRegion that routes addresses for the address space |
Alexey Kardashevskiy | 7dca804 | 2013-04-29 16:25:51 +0000 | [diff] [blame] | 1731 | * @name: an address space name. The name is only used for debugging |
| 1732 | * output. |
Avi Kivity | 9ad2bbc | 2012-10-02 14:59:23 +0200 | [diff] [blame] | 1733 | */ |
Alexey Kardashevskiy | 7dca804 | 2013-04-29 16:25:51 +0000 | [diff] [blame] | 1734 | void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name); |
Avi Kivity | 9ad2bbc | 2012-10-02 14:59:23 +0200 | [diff] [blame] | 1735 | |
Peter Crosthwaite | f0c02d1 | 2016-01-21 14:15:06 +0000 | [diff] [blame] | 1736 | /** |
Avi Kivity | 83f3c25 | 2012-10-07 12:59:55 +0200 | [diff] [blame] | 1737 | * address_space_destroy: destroy an address space |
| 1738 | * |
| 1739 | * Releases all resources associated with an address space. After an address space |
| 1740 | * is destroyed, its root memory region (given by address_space_init()) may be destroyed |
| 1741 | * as well. |
| 1742 | * |
| 1743 | * @as: address space to be destroyed |
| 1744 | */ |
| 1745 | void address_space_destroy(AddressSpace *as); |
| 1746 | |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1747 | /** |
| 1748 | * address_space_rw: read from or write to an address space. |
| 1749 | * |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1750 | * Return a MemTxResult indicating whether the operation succeeded |
| 1751 | * or failed (eg unassigned memory, device rejected the transaction, |
| 1752 | * IOMMU fault). |
Paolo Bonzini | fd8aaa7 | 2013-05-21 09:56:55 +0200 | [diff] [blame] | 1753 | * |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1754 | * @as: #AddressSpace to be accessed |
| 1755 | * @addr: address within that address space |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1756 | * @attrs: memory transaction attributes |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1757 | * @buf: buffer with the data transferred |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 1758 | * @len: the number of bytes to read or write |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1759 | * @is_write: indicates the transfer direction |
| 1760 | */ |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1761 | MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, |
| 1762 | MemTxAttrs attrs, uint8_t *buf, |
| 1763 | int len, bool is_write); |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1764 | |
| 1765 | /** |
| 1766 | * address_space_write: write to address space. |
| 1767 | * |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1768 | * Return a MemTxResult indicating whether the operation succeeded |
| 1769 | * or failed (eg unassigned memory, device rejected the transaction, |
| 1770 | * IOMMU fault). |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1771 | * |
| 1772 | * @as: #AddressSpace to be accessed |
| 1773 | * @addr: address within that address space |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1774 | * @attrs: memory transaction attributes |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1775 | * @buf: buffer with the data transferred |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 1776 | * @len: the number of bytes to write |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1777 | */ |
Peter Maydell | 5c9eb02 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1778 | MemTxResult address_space_write(AddressSpace *as, hwaddr addr, |
| 1779 | MemTxAttrs attrs, |
| 1780 | const uint8_t *buf, int len); |
Paolo Bonzini | fd8aaa7 | 2013-05-21 09:56:55 +0200 | [diff] [blame] | 1781 | |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 1782 | /* address_space_ld*: load from an address space |
Peter Maydell | 5001311 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1783 | * address_space_st*: store to an address space |
| 1784 | * |
| 1785 | * These functions perform a load or store of the byte, word, |
| 1786 | * longword or quad to the specified address within the AddressSpace. |
| 1787 | * The _le suffixed functions treat the data as little endian; |
| 1788 | * _be indicates big endian; no suffix indicates "same endianness |
| 1789 | * as guest CPU". |
| 1790 | * |
| 1791 | * The "guest CPU endianness" accessors are deprecated for use outside |
| 1792 | * target-* code; devices should be CPU-agnostic and use either the LE |
| 1793 | * or the BE accessors. |
| 1794 | * |
| 1795 | * @as #AddressSpace to be accessed |
| 1796 | * @addr: address within that address space |
| 1797 | * @val: data value, for stores |
| 1798 | * @attrs: memory transaction attributes |
| 1799 | * @result: location to write the success/failure of the transaction; |
| 1800 | * if NULL, this information is discarded |
| 1801 | */ |
Peter Maydell | 5001311 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1802 | |
Paolo Bonzini | 4269c82 | 2018-03-04 23:31:47 +0100 | [diff] [blame] | 1803 | #define SUFFIX |
| 1804 | #define ARG1 as |
| 1805 | #define ARG1_DECL AddressSpace *as |
| 1806 | #include "exec/memory_ldst.inc.h" |
| 1807 | |
| 1808 | #define SUFFIX |
| 1809 | #define ARG1 as |
| 1810 | #define ARG1_DECL AddressSpace *as |
| 1811 | #include "exec/memory_ldst_phys.inc.h" |
Paolo Bonzini | 0ce265f | 2016-11-22 11:34:02 +0100 | [diff] [blame] | 1812 | |
Paolo Bonzini | 1f4e496 | 2016-11-22 12:04:52 +0100 | [diff] [blame] | 1813 | struct MemoryRegionCache { |
Paolo Bonzini | 4856404 | 2018-03-18 18:26:36 +0100 | [diff] [blame] | 1814 | void *ptr; |
Paolo Bonzini | 1f4e496 | 2016-11-22 12:04:52 +0100 | [diff] [blame] | 1815 | hwaddr xlat; |
Paolo Bonzini | 1f4e496 | 2016-11-22 12:04:52 +0100 | [diff] [blame] | 1816 | hwaddr len; |
Paolo Bonzini | 4856404 | 2018-03-18 18:26:36 +0100 | [diff] [blame] | 1817 | FlatView *fv; |
| 1818 | MemoryRegionSection mrs; |
| 1819 | bool is_write; |
Paolo Bonzini | 1f4e496 | 2016-11-22 12:04:52 +0100 | [diff] [blame] | 1820 | }; |
| 1821 | |
Paolo Bonzini | 4856404 | 2018-03-18 18:26:36 +0100 | [diff] [blame] | 1822 | #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL }) |
| 1823 | |
Paolo Bonzini | 5eba040 | 2017-01-27 16:40:16 +0100 | [diff] [blame] | 1824 | |
Paolo Bonzini | 4269c82 | 2018-03-04 23:31:47 +0100 | [diff] [blame] | 1825 | /* address_space_ld*_cached: load from a cached #MemoryRegion |
| 1826 | * address_space_st*_cached: store into a cached #MemoryRegion |
| 1827 | * |
| 1828 | * These functions perform a load or store of the byte, word, |
| 1829 | * longword or quad to the specified address. The address is |
| 1830 | * a physical address in the AddressSpace, but it must lie within |
| 1831 | * a #MemoryRegion that was mapped with address_space_cache_init. |
| 1832 | * |
| 1833 | * The _le suffixed functions treat the data as little endian; |
| 1834 | * _be indicates big endian; no suffix indicates "same endianness |
| 1835 | * as guest CPU". |
| 1836 | * |
| 1837 | * The "guest CPU endianness" accessors are deprecated for use outside |
| 1838 | * target-* code; devices should be CPU-agnostic and use either the LE |
| 1839 | * or the BE accessors. |
| 1840 | * |
| 1841 | * @cache: previously initialized #MemoryRegionCache to be accessed |
| 1842 | * @addr: address within the address space |
| 1843 | * @val: data value, for stores |
| 1844 | * @attrs: memory transaction attributes |
| 1845 | * @result: location to write the success/failure of the transaction; |
| 1846 | * if NULL, this information is discarded |
| 1847 | */ |
| 1848 | |
Paolo Bonzini | 4856404 | 2018-03-18 18:26:36 +0100 | [diff] [blame] | 1849 | #define SUFFIX _cached_slow |
Paolo Bonzini | 4269c82 | 2018-03-04 23:31:47 +0100 | [diff] [blame] | 1850 | #define ARG1 cache |
| 1851 | #define ARG1_DECL MemoryRegionCache *cache |
| 1852 | #include "exec/memory_ldst.inc.h" |
| 1853 | |
Paolo Bonzini | 4856404 | 2018-03-18 18:26:36 +0100 | [diff] [blame] | 1854 | /* Inline fast path for direct RAM access. */ |
| 1855 | static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, |
| 1856 | hwaddr addr, MemTxAttrs attrs, MemTxResult *result) |
| 1857 | { |
| 1858 | assert(addr < cache->len); |
| 1859 | if (likely(cache->ptr)) { |
| 1860 | return ldub_p(cache->ptr + addr); |
| 1861 | } else { |
| 1862 | return address_space_ldub_cached_slow(cache, addr, attrs, result); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | static inline void address_space_stb_cached(MemoryRegionCache *cache, |
| 1867 | hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result) |
| 1868 | { |
| 1869 | assert(addr < cache->len); |
| 1870 | if (likely(cache->ptr)) { |
| 1871 | stb_p(cache->ptr + addr, val); |
| 1872 | } else { |
| 1873 | address_space_stb_cached_slow(cache, addr, val, attrs, result); |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | #define ENDIANNESS _le |
| 1878 | #include "exec/memory_ldst_cached.inc.h" |
| 1879 | |
| 1880 | #define ENDIANNESS _be |
| 1881 | #include "exec/memory_ldst_cached.inc.h" |
| 1882 | |
Paolo Bonzini | 4269c82 | 2018-03-04 23:31:47 +0100 | [diff] [blame] | 1883 | #define SUFFIX _cached |
| 1884 | #define ARG1 cache |
| 1885 | #define ARG1_DECL MemoryRegionCache *cache |
| 1886 | #include "exec/memory_ldst_phys.inc.h" |
| 1887 | |
Paolo Bonzini | 1f4e496 | 2016-11-22 12:04:52 +0100 | [diff] [blame] | 1888 | /* address_space_cache_init: prepare for repeated access to a physical |
| 1889 | * memory region |
| 1890 | * |
| 1891 | * @cache: #MemoryRegionCache to be filled |
| 1892 | * @as: #AddressSpace to be accessed |
| 1893 | * @addr: address within that address space |
| 1894 | * @len: length of buffer |
| 1895 | * @is_write: indicates the transfer direction |
| 1896 | * |
| 1897 | * Will only work with RAM, and may map a subset of the requested range by |
| 1898 | * returning a value that is less than @len. On failure, return a negative |
| 1899 | * errno value. |
| 1900 | * |
| 1901 | * Because it only works with RAM, this function can be used for |
| 1902 | * read-modify-write operations. In this case, is_write should be %true. |
| 1903 | * |
| 1904 | * Note that addresses passed to the address_space_*_cached functions |
| 1905 | * are relative to @addr. |
| 1906 | */ |
| 1907 | int64_t address_space_cache_init(MemoryRegionCache *cache, |
| 1908 | AddressSpace *as, |
| 1909 | hwaddr addr, |
| 1910 | hwaddr len, |
| 1911 | bool is_write); |
| 1912 | |
| 1913 | /** |
| 1914 | * address_space_cache_invalidate: complete a write to a #MemoryRegionCache |
| 1915 | * |
| 1916 | * @cache: The #MemoryRegionCache to operate on. |
| 1917 | * @addr: The first physical address that was written, relative to the |
| 1918 | * address that was passed to @address_space_cache_init. |
| 1919 | * @access_len: The number of bytes that were written starting at @addr. |
| 1920 | */ |
| 1921 | void address_space_cache_invalidate(MemoryRegionCache *cache, |
| 1922 | hwaddr addr, |
| 1923 | hwaddr access_len); |
| 1924 | |
| 1925 | /** |
| 1926 | * address_space_cache_destroy: free a #MemoryRegionCache |
| 1927 | * |
| 1928 | * @cache: The #MemoryRegionCache whose memory should be released. |
| 1929 | */ |
| 1930 | void address_space_cache_destroy(MemoryRegionCache *cache); |
| 1931 | |
Jason Wang | 052c8fa | 2016-12-30 18:09:13 +0800 | [diff] [blame] | 1932 | /* address_space_get_iotlb_entry: translate an address into an IOTLB |
| 1933 | * entry. Should be called from an RCU critical section. |
| 1934 | */ |
| 1935 | IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr, |
Peter Maydell | 7446eb0 | 2018-05-31 14:50:53 +0100 | [diff] [blame] | 1936 | bool is_write, MemTxAttrs attrs); |
Paolo Bonzini | 1f4e496 | 2016-11-22 12:04:52 +0100 | [diff] [blame] | 1937 | |
Paolo Bonzini | 149f54b | 2013-05-24 12:59:37 +0200 | [diff] [blame] | 1938 | /* address_space_translate: translate an address range into an address space |
Paolo Bonzini | 41063e1 | 2015-03-18 14:21:43 +0100 | [diff] [blame] | 1939 | * into a MemoryRegion and an address range into that section. Should be |
| 1940 | * called from an RCU critical section, to avoid that the last reference |
| 1941 | * to the returned region disappears after address_space_translate returns. |
Paolo Bonzini | 149f54b | 2013-05-24 12:59:37 +0200 | [diff] [blame] | 1942 | * |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 1943 | * @fv: #FlatView to be accessed |
Paolo Bonzini | 149f54b | 2013-05-24 12:59:37 +0200 | [diff] [blame] | 1944 | * @addr: address within that address space |
| 1945 | * @xlat: pointer to address within the returned memory region section's |
| 1946 | * #MemoryRegion. |
| 1947 | * @len: pointer to length |
| 1948 | * @is_write: indicates the transfer direction |
Peter Maydell | bc6b1ce | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1949 | * @attrs: memory attributes |
Paolo Bonzini | 149f54b | 2013-05-24 12:59:37 +0200 | [diff] [blame] | 1950 | */ |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 1951 | MemoryRegion *flatview_translate(FlatView *fv, |
| 1952 | hwaddr addr, hwaddr *xlat, |
Peter Maydell | efa99a2 | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1953 | hwaddr *len, bool is_write, |
| 1954 | MemTxAttrs attrs); |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 1955 | |
| 1956 | static inline MemoryRegion *address_space_translate(AddressSpace *as, |
| 1957 | hwaddr addr, hwaddr *xlat, |
Peter Maydell | bc6b1ce | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1958 | hwaddr *len, bool is_write, |
| 1959 | MemTxAttrs attrs) |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 1960 | { |
| 1961 | return flatview_translate(address_space_to_flatview(as), |
Peter Maydell | efa99a2 | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1962 | addr, xlat, len, is_write, attrs); |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 1963 | } |
Paolo Bonzini | 149f54b | 2013-05-24 12:59:37 +0200 | [diff] [blame] | 1964 | |
Paolo Bonzini | 51644ab | 2013-04-11 15:40:59 +0200 | [diff] [blame] | 1965 | /* address_space_access_valid: check for validity of accessing an address |
| 1966 | * space range |
| 1967 | * |
Avi Kivity | 3095115 | 2012-10-30 13:47:46 +0200 | [diff] [blame] | 1968 | * Check whether memory is assigned to the given address space range, and |
| 1969 | * access is permitted by any IOMMU regions that are active for the address |
| 1970 | * space. |
Paolo Bonzini | 51644ab | 2013-04-11 15:40:59 +0200 | [diff] [blame] | 1971 | * |
| 1972 | * For now, addr and len should be aligned to a page size. This limitation |
| 1973 | * will be lifted in the future. |
| 1974 | * |
| 1975 | * @as: #AddressSpace to be accessed |
| 1976 | * @addr: address within that address space |
| 1977 | * @len: length of the area to be checked |
| 1978 | * @is_write: indicates the transfer direction |
Peter Maydell | fddffa4 | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1979 | * @attrs: memory attributes |
Paolo Bonzini | 51644ab | 2013-04-11 15:40:59 +0200 | [diff] [blame] | 1980 | */ |
Peter Maydell | fddffa4 | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1981 | bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, |
| 1982 | bool is_write, MemTxAttrs attrs); |
Paolo Bonzini | 51644ab | 2013-04-11 15:40:59 +0200 | [diff] [blame] | 1983 | |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1984 | /* address_space_map: map a physical memory region into a host virtual address |
| 1985 | * |
| 1986 | * May map a subset of the requested range, given by and returned in @plen. |
| 1987 | * May return %NULL if resources needed to perform the mapping are exhausted. |
| 1988 | * Use only for reads OR writes - not for read-modify-write operations. |
| 1989 | * Use cpu_register_map_client() to know when retrying the map operation is |
| 1990 | * likely to succeed. |
| 1991 | * |
| 1992 | * @as: #AddressSpace to be accessed |
| 1993 | * @addr: address within that address space |
| 1994 | * @plen: pointer to length of buffer; updated on return |
| 1995 | * @is_write: indicates the transfer direction |
Peter Maydell | f26404f | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1996 | * @attrs: memory attributes |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 1997 | */ |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1998 | void *address_space_map(AddressSpace *as, hwaddr addr, |
Peter Maydell | f26404f | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 1999 | hwaddr *plen, bool is_write, MemTxAttrs attrs); |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 2000 | |
| 2001 | /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map() |
| 2002 | * |
| 2003 | * Will also mark the memory as dirty if @is_write == %true. @access_len gives |
| 2004 | * the amount of memory that was actually read or written by the caller. |
| 2005 | * |
| 2006 | * @as: #AddressSpace used |
Jay Zhou | 57914ec | 2018-01-04 13:29:48 +0800 | [diff] [blame] | 2007 | * @buffer: host pointer as returned by address_space_map() |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 2008 | * @len: buffer length as returned by address_space_map() |
| 2009 | * @access_len: amount of data actually transferred |
| 2010 | * @is_write: indicates the transfer direction |
| 2011 | */ |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2012 | void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, |
| 2013 | int is_write, hwaddr access_len); |
Avi Kivity | ac1970f | 2012-10-03 16:22:53 +0200 | [diff] [blame] | 2014 | |
| 2015 | |
Paolo Bonzini | a203ac7 | 2015-12-09 10:18:57 +0100 | [diff] [blame] | 2016 | /* Internal functions, part of the implementation of address_space_read. */ |
Paolo Bonzini | b2a44fc | 2018-03-05 00:19:49 +0100 | [diff] [blame] | 2017 | MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, |
| 2018 | MemTxAttrs attrs, uint8_t *buf, int len); |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 2019 | MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr, |
| 2020 | MemTxAttrs attrs, uint8_t *buf, |
| 2021 | int len, hwaddr addr1, hwaddr l, |
| 2022 | MemoryRegion *mr); |
Paolo Bonzini | 0878d0e | 2016-02-22 11:02:12 +0100 | [diff] [blame] | 2023 | void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr); |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2024 | |
Paolo Bonzini | 4856404 | 2018-03-18 18:26:36 +0100 | [diff] [blame] | 2025 | /* Internal functions, part of the implementation of address_space_read_cached |
| 2026 | * and address_space_write_cached. */ |
| 2027 | void address_space_read_cached_slow(MemoryRegionCache *cache, |
| 2028 | hwaddr addr, void *buf, int len); |
| 2029 | void address_space_write_cached_slow(MemoryRegionCache *cache, |
| 2030 | hwaddr addr, const void *buf, int len); |
| 2031 | |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2032 | static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) |
| 2033 | { |
| 2034 | if (is_write) { |
Alex Williamson | 4a2e242 | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 2035 | return memory_region_is_ram(mr) && |
| 2036 | !mr->readonly && !memory_region_is_ram_device(mr); |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2037 | } else { |
Alex Williamson | 4a2e242 | 2016-10-31 09:53:03 -0600 | [diff] [blame] | 2038 | return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) || |
| 2039 | memory_region_is_romd(mr); |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2040 | } |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | /** |
| 2044 | * address_space_read: read from an address space. |
| 2045 | * |
| 2046 | * Return a MemTxResult indicating whether the operation succeeded |
| 2047 | * or failed (eg unassigned memory, device rejected the transaction, |
Paolo Bonzini | b2a44fc | 2018-03-05 00:19:49 +0100 | [diff] [blame] | 2048 | * IOMMU fault). Called within RCU critical section. |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2049 | * |
Paolo Bonzini | b2a44fc | 2018-03-05 00:19:49 +0100 | [diff] [blame] | 2050 | * @as: #AddressSpace to be accessed |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2051 | * @addr: address within that address space |
| 2052 | * @attrs: memory transaction attributes |
| 2053 | * @buf: buffer with the data transferred |
| 2054 | */ |
| 2055 | static inline __attribute__((__always_inline__)) |
Paolo Bonzini | b2a44fc | 2018-03-05 00:19:49 +0100 | [diff] [blame] | 2056 | MemTxResult address_space_read(AddressSpace *as, hwaddr addr, |
| 2057 | MemTxAttrs attrs, uint8_t *buf, |
| 2058 | int len) |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2059 | { |
| 2060 | MemTxResult result = MEMTX_OK; |
| 2061 | hwaddr l, addr1; |
| 2062 | void *ptr; |
| 2063 | MemoryRegion *mr; |
Paolo Bonzini | b2a44fc | 2018-03-05 00:19:49 +0100 | [diff] [blame] | 2064 | FlatView *fv; |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2065 | |
| 2066 | if (__builtin_constant_p(len)) { |
| 2067 | if (len) { |
| 2068 | rcu_read_lock(); |
Paolo Bonzini | b2a44fc | 2018-03-05 00:19:49 +0100 | [diff] [blame] | 2069 | fv = address_space_to_flatview(as); |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2070 | l = len; |
Peter Maydell | efa99a2 | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 2071 | mr = flatview_translate(fv, addr, &addr1, &l, false, attrs); |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2072 | if (len == l && memory_access_is_direct(mr, false)) { |
Paolo Bonzini | 0878d0e | 2016-02-22 11:02:12 +0100 | [diff] [blame] | 2073 | ptr = qemu_map_ram_ptr(mr->ram_block, addr1); |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2074 | memcpy(buf, ptr, len); |
| 2075 | } else { |
Alexey Kardashevskiy | 1662068 | 2017-09-21 18:50:58 +1000 | [diff] [blame] | 2076 | result = flatview_read_continue(fv, addr, attrs, buf, len, |
| 2077 | addr1, l, mr); |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2078 | } |
| 2079 | rcu_read_unlock(); |
| 2080 | } |
| 2081 | } else { |
Paolo Bonzini | b2a44fc | 2018-03-05 00:19:49 +0100 | [diff] [blame] | 2082 | result = address_space_read_full(as, addr, attrs, buf, len); |
Paolo Bonzini | 3cc8f88 | 2015-12-09 10:34:13 +0100 | [diff] [blame] | 2083 | } |
| 2084 | return result; |
| 2085 | } |
Paolo Bonzini | a203ac7 | 2015-12-09 10:18:57 +0100 | [diff] [blame] | 2086 | |
Paolo Bonzini | 1f4e496 | 2016-11-22 12:04:52 +0100 | [diff] [blame] | 2087 | /** |
| 2088 | * address_space_read_cached: read from a cached RAM region |
| 2089 | * |
| 2090 | * @cache: Cached region to be addressed |
| 2091 | * @addr: address relative to the base of the RAM region |
| 2092 | * @buf: buffer with the data transferred |
| 2093 | * @len: length of the data transferred |
| 2094 | */ |
| 2095 | static inline void |
| 2096 | address_space_read_cached(MemoryRegionCache *cache, hwaddr addr, |
| 2097 | void *buf, int len) |
| 2098 | { |
| 2099 | assert(addr < cache->len && len <= cache->len - addr); |
Paolo Bonzini | 4856404 | 2018-03-18 18:26:36 +0100 | [diff] [blame] | 2100 | if (likely(cache->ptr)) { |
| 2101 | memcpy(buf, cache->ptr + addr, len); |
| 2102 | } else { |
| 2103 | address_space_read_cached_slow(cache, addr, buf, len); |
| 2104 | } |
Paolo Bonzini | 1f4e496 | 2016-11-22 12:04:52 +0100 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | /** |
| 2108 | * address_space_write_cached: write to a cached RAM region |
| 2109 | * |
| 2110 | * @cache: Cached region to be addressed |
| 2111 | * @addr: address relative to the base of the RAM region |
| 2112 | * @buf: buffer with the data transferred |
| 2113 | * @len: length of the data transferred |
| 2114 | */ |
| 2115 | static inline void |
| 2116 | address_space_write_cached(MemoryRegionCache *cache, hwaddr addr, |
| 2117 | void *buf, int len) |
| 2118 | { |
| 2119 | assert(addr < cache->len && len <= cache->len - addr); |
Paolo Bonzini | 4856404 | 2018-03-18 18:26:36 +0100 | [diff] [blame] | 2120 | if (likely(cache->ptr)) { |
| 2121 | memcpy(cache->ptr + addr, buf, len); |
| 2122 | } else { |
| 2123 | address_space_write_cached_slow(cache, addr, buf, len); |
| 2124 | } |
Paolo Bonzini | 1f4e496 | 2016-11-22 12:04:52 +0100 | [diff] [blame] | 2125 | } |
| 2126 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 2127 | #endif |
| 2128 | |
| 2129 | #endif |