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 | |
| 19 | #include <stdint.h> |
| 20 | #include <stdbool.h> |
| 21 | #include "qemu-common.h" |
| 22 | #include "cpu-common.h" |
| 23 | #include "targphys.h" |
| 24 | #include "qemu-queue.h" |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 25 | #include "iorange.h" |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 26 | #include "ioport.h" |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 27 | #include "int128.h" |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 28 | |
| 29 | typedef struct MemoryRegionOps MemoryRegionOps; |
| 30 | typedef struct MemoryRegion MemoryRegion; |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 31 | typedef struct MemoryRegionPortio MemoryRegionPortio; |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 32 | typedef struct MemoryRegionMmio MemoryRegionMmio; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 33 | |
| 34 | /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic |
| 35 | * registration. |
| 36 | */ |
| 37 | #define DIRTY_MEMORY_VGA 0 |
| 38 | #define DIRTY_MEMORY_CODE 1 |
| 39 | #define DIRTY_MEMORY_MIGRATION 3 |
| 40 | |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 41 | struct MemoryRegionMmio { |
| 42 | CPUReadMemoryFunc *read[3]; |
| 43 | CPUWriteMemoryFunc *write[3]; |
| 44 | }; |
| 45 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 46 | /* |
| 47 | * Memory region callbacks |
| 48 | */ |
| 49 | struct MemoryRegionOps { |
| 50 | /* Read from the memory region. @addr is relative to @mr; @size is |
| 51 | * in bytes. */ |
| 52 | uint64_t (*read)(void *opaque, |
| 53 | target_phys_addr_t addr, |
| 54 | unsigned size); |
| 55 | /* Write to the memory region. @addr is relative to @mr; @size is |
| 56 | * in bytes. */ |
| 57 | void (*write)(void *opaque, |
| 58 | target_phys_addr_t addr, |
| 59 | uint64_t data, |
| 60 | unsigned size); |
| 61 | |
| 62 | enum device_endian endianness; |
| 63 | /* Guest-visible constraints: */ |
| 64 | struct { |
| 65 | /* If nonzero, specify bounds on access sizes beyond which a machine |
| 66 | * check is thrown. |
| 67 | */ |
| 68 | unsigned min_access_size; |
| 69 | unsigned max_access_size; |
| 70 | /* If true, unaligned accesses are supported. Otherwise unaligned |
| 71 | * accesses throw machine checks. |
| 72 | */ |
| 73 | bool unaligned; |
Avi Kivity | 897fa7c | 2011-11-13 13:05:27 +0200 | [diff] [blame] | 74 | /* |
| 75 | * If present, and returns #false, the transaction is not accepted |
| 76 | * by the device (and results in machine dependent behaviour such |
| 77 | * as a machine check exception). |
| 78 | */ |
| 79 | bool (*accepts)(void *opaque, target_phys_addr_t addr, |
| 80 | unsigned size, bool is_write); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 81 | } valid; |
| 82 | /* Internal implementation constraints: */ |
| 83 | struct { |
| 84 | /* If nonzero, specifies the minimum size implemented. Smaller sizes |
| 85 | * will be rounded upwards and a partial result will be returned. |
| 86 | */ |
| 87 | unsigned min_access_size; |
| 88 | /* If nonzero, specifies the maximum size implemented. Larger sizes |
| 89 | * will be done as a series of accesses with smaller sizes. |
| 90 | */ |
| 91 | unsigned max_access_size; |
| 92 | /* If true, unaligned accesses are supported. Otherwise all accesses |
| 93 | * are converted to (possibly multiple) naturally aligned accesses. |
| 94 | */ |
| 95 | bool unaligned; |
| 96 | } impl; |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 97 | |
| 98 | /* If .read and .write are not present, old_portio may be used for |
| 99 | * backwards compatibility with old portio registration |
| 100 | */ |
| 101 | const MemoryRegionPortio *old_portio; |
Avi Kivity | 74901c3 | 2011-07-26 14:26:10 +0300 | [diff] [blame] | 102 | /* If .read and .write are not present, old_mmio may be used for |
| 103 | * backwards compatibility with old mmio registration |
| 104 | */ |
| 105 | const MemoryRegionMmio old_mmio; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | typedef struct CoalescedMemoryRange CoalescedMemoryRange; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 109 | typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 110 | |
| 111 | struct MemoryRegion { |
| 112 | /* All fields are private - violators will be prosecuted */ |
| 113 | const MemoryRegionOps *ops; |
| 114 | void *opaque; |
| 115 | MemoryRegion *parent; |
Avi Kivity | 08dafab | 2011-10-16 13:19:17 +0200 | [diff] [blame] | 116 | Int128 size; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 117 | target_phys_addr_t addr; |
Avi Kivity | 545e92e | 2011-08-08 19:58:48 +0300 | [diff] [blame] | 118 | void (*destructor)(MemoryRegion *mr); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 119 | ram_addr_t ram_addr; |
Avi Kivity | 658b222 | 2011-07-26 14:26:08 +0300 | [diff] [blame] | 120 | IORange iorange; |
Avi Kivity | b3b00c7 | 2012-01-02 13:20:11 +0200 | [diff] [blame] | 121 | bool subpage; |
Avi Kivity | 14a3c10 | 2011-07-26 14:26:06 +0300 | [diff] [blame] | 122 | bool terminates; |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 123 | bool readable; |
Avi Kivity | 8ea9252 | 2011-12-08 15:58:43 +0200 | [diff] [blame] | 124 | bool ram; |
Avi Kivity | fb1cd6f | 2011-09-25 14:48:47 +0300 | [diff] [blame] | 125 | bool readonly; /* For RAM regions */ |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 126 | bool enabled; |
Avi Kivity | 75c578d | 2012-01-02 15:40:52 +0200 | [diff] [blame] | 127 | bool rom_device; |
Jan Kiszka | 1660e72 | 2011-10-23 16:01:19 +0200 | [diff] [blame] | 128 | bool warning_printed; /* For reservations */ |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 129 | MemoryRegion *alias; |
| 130 | target_phys_addr_t alias_offset; |
| 131 | unsigned priority; |
| 132 | bool may_overlap; |
| 133 | QTAILQ_HEAD(subregions, MemoryRegion) subregions; |
| 134 | QTAILQ_ENTRY(MemoryRegion) subregions_link; |
| 135 | QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced; |
| 136 | const char *name; |
Avi Kivity | 5a58334 | 2011-07-26 14:26:02 +0300 | [diff] [blame] | 137 | uint8_t dirty_log_mask; |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 138 | unsigned ioeventfd_nb; |
| 139 | MemoryRegionIoeventfd *ioeventfds; |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 140 | }; |
| 141 | |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 142 | struct MemoryRegionPortio { |
| 143 | uint32_t offset; |
| 144 | uint32_t len; |
| 145 | unsigned size; |
| 146 | IOPortReadFunc *read; |
| 147 | IOPortWriteFunc *write; |
| 148 | }; |
| 149 | |
Avi Kivity | 2dd3022 | 2011-08-08 16:08:54 +0300 | [diff] [blame] | 150 | #define PORTIO_END_OF_LIST() { } |
Avi Kivity | 627a0e9 | 2011-07-26 14:26:09 +0300 | [diff] [blame] | 151 | |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 152 | typedef struct MemoryRegionSection MemoryRegionSection; |
| 153 | |
| 154 | /** |
| 155 | * MemoryRegionSection: describes a fragment of a #MemoryRegion |
| 156 | * |
| 157 | * @mr: the region, or %NULL if empty |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 158 | * @address_space: the address space the region is mapped in |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 159 | * @offset_within_region: the beginning of the section, relative to @mr's start |
| 160 | * @size: the size of the section; will not exceed @mr's boundaries |
| 161 | * @offset_within_address_space: the address of the first byte of the section |
| 162 | * relative to the region's address space |
Avi Kivity | 7a8499e | 2012-02-08 17:01:23 +0200 | [diff] [blame] | 163 | * @readonly: writes to this section are ignored |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 164 | */ |
| 165 | struct MemoryRegionSection { |
| 166 | MemoryRegion *mr; |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 167 | MemoryRegion *address_space; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 168 | target_phys_addr_t offset_within_region; |
| 169 | uint64_t size; |
| 170 | target_phys_addr_t offset_within_address_space; |
Avi Kivity | 7a8499e | 2012-02-08 17:01:23 +0200 | [diff] [blame] | 171 | bool readonly; |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 172 | }; |
| 173 | |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 174 | typedef struct MemoryListener MemoryListener; |
| 175 | |
| 176 | /** |
| 177 | * MemoryListener: callbacks structure for updates to the physical memory map |
| 178 | * |
| 179 | * Allows a component to adjust to changes in the guest-visible memory map. |
| 180 | * Use with memory_listener_register() and memory_listener_unregister(). |
| 181 | */ |
| 182 | struct MemoryListener { |
| 183 | void (*region_add)(MemoryListener *listener, MemoryRegionSection *section); |
| 184 | void (*region_del)(MemoryListener *listener, MemoryRegionSection *section); |
| 185 | void (*log_start)(MemoryListener *listener, MemoryRegionSection *section); |
| 186 | void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section); |
| 187 | void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section); |
| 188 | void (*log_global_start)(MemoryListener *listener); |
| 189 | void (*log_global_stop)(MemoryListener *listener); |
Avi Kivity | 80a1ea3 | 2012-02-08 16:39:06 +0200 | [diff] [blame] | 190 | void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section, |
| 191 | bool match_data, uint64_t data, int fd); |
| 192 | void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section, |
| 193 | bool match_data, uint64_t data, int fd); |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 194 | /* Lower = earlier (during add), later (during del) */ |
| 195 | unsigned priority; |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame^] | 196 | MemoryRegion *address_space_filter; |
Avi Kivity | 72e22d2 | 2012-02-08 15:05:50 +0200 | [diff] [blame] | 197 | QTAILQ_ENTRY(MemoryListener) link; |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 200 | /** |
| 201 | * memory_region_init: Initialize a memory region |
| 202 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 203 | * The region typically acts as a container for other memory regions. Use |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 204 | * memory_region_add_subregion() to add subregions. |
| 205 | * |
| 206 | * @mr: the #MemoryRegion to be initialized |
| 207 | * @name: used for debugging; not visible to the user or ABI |
| 208 | * @size: size of the region; any subregions beyond this size will be clipped |
| 209 | */ |
| 210 | void memory_region_init(MemoryRegion *mr, |
| 211 | const char *name, |
| 212 | uint64_t size); |
| 213 | /** |
| 214 | * memory_region_init_io: Initialize an I/O memory region. |
| 215 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 216 | * 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] | 217 | * if @size is nonzero, subregions will be clipped to @size. |
| 218 | * |
| 219 | * @mr: the #MemoryRegion to be initialized. |
| 220 | * @ops: a structure containing read and write callbacks to be used when |
| 221 | * I/O is performed on the region. |
| 222 | * @opaque: passed to to the read and write callbacks of the @ops structure. |
| 223 | * @name: used for debugging; not visible to the user or ABI |
| 224 | * @size: size of the region. |
| 225 | */ |
| 226 | void memory_region_init_io(MemoryRegion *mr, |
| 227 | const MemoryRegionOps *ops, |
| 228 | void *opaque, |
| 229 | const char *name, |
| 230 | uint64_t size); |
| 231 | |
| 232 | /** |
| 233 | * memory_region_init_ram: Initialize RAM memory region. Accesses into the |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 234 | * region will modify memory directly. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 235 | * |
| 236 | * @mr: the #MemoryRegion to be initialized. |
Avi Kivity | c5705a7 | 2011-12-20 15:59:12 +0200 | [diff] [blame] | 237 | * @name: the name of the region. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 238 | * @size: size of the region. |
| 239 | */ |
| 240 | void memory_region_init_ram(MemoryRegion *mr, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 241 | const char *name, |
| 242 | uint64_t size); |
| 243 | |
| 244 | /** |
| 245 | * memory_region_init_ram: Initialize RAM memory region from a user-provided. |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 246 | * pointer. Accesses into the region will modify |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 247 | * memory directly. |
| 248 | * |
| 249 | * @mr: the #MemoryRegion to be initialized. |
Avi Kivity | c5705a7 | 2011-12-20 15:59:12 +0200 | [diff] [blame] | 250 | * @name: the name of the region. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 251 | * @size: size of the region. |
| 252 | * @ptr: memory to be mapped; must contain at least @size bytes. |
| 253 | */ |
| 254 | void memory_region_init_ram_ptr(MemoryRegion *mr, |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 255 | const char *name, |
| 256 | uint64_t size, |
| 257 | void *ptr); |
| 258 | |
| 259 | /** |
| 260 | * memory_region_init_alias: Initialize a memory region that aliases all or a |
| 261 | * part of another memory region. |
| 262 | * |
| 263 | * @mr: the #MemoryRegion to be initialized. |
| 264 | * @name: used for debugging; not visible to the user or ABI |
| 265 | * @orig: the region to be referenced; @mr will be equivalent to |
| 266 | * @orig between @offset and @offset + @size - 1. |
| 267 | * @offset: start of the section in @orig to be referenced. |
| 268 | * @size: size of the region. |
| 269 | */ |
| 270 | void memory_region_init_alias(MemoryRegion *mr, |
| 271 | const char *name, |
| 272 | MemoryRegion *orig, |
| 273 | target_phys_addr_t offset, |
| 274 | uint64_t size); |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * memory_region_init_rom_device: Initialize a ROM memory region. Writes are |
| 278 | * handled via callbacks. |
| 279 | * |
| 280 | * @mr: the #MemoryRegion to be initialized. |
| 281 | * @ops: callbacks for write access handling. |
Avi Kivity | c5705a7 | 2011-12-20 15:59:12 +0200 | [diff] [blame] | 282 | * @name: the name of the region. |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 283 | * @size: size of the region. |
| 284 | */ |
| 285 | void memory_region_init_rom_device(MemoryRegion *mr, |
| 286 | const MemoryRegionOps *ops, |
Avi Kivity | 75f5941 | 2011-08-26 00:35:15 +0300 | [diff] [blame] | 287 | void *opaque, |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 288 | const char *name, |
| 289 | uint64_t size); |
| 290 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 291 | /** |
Jan Kiszka | 1660e72 | 2011-10-23 16:01:19 +0200 | [diff] [blame] | 292 | * memory_region_init_reservation: Initialize a memory region that reserves |
| 293 | * I/O space. |
| 294 | * |
| 295 | * A reservation region primariy serves debugging purposes. It claims I/O |
| 296 | * space that is not supposed to be handled by QEMU itself. Any access via |
| 297 | * the memory API will cause an abort(). |
| 298 | * |
| 299 | * @mr: the #MemoryRegion to be initialized |
| 300 | * @name: used for debugging; not visible to the user or ABI |
| 301 | * @size: size of the region. |
| 302 | */ |
| 303 | void memory_region_init_reservation(MemoryRegion *mr, |
| 304 | const char *name, |
| 305 | uint64_t size); |
| 306 | /** |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 307 | * memory_region_destroy: Destroy a memory region and reclaim all resources. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 308 | * |
| 309 | * @mr: the region to be destroyed. May not currently be a subregion |
| 310 | * (see memory_region_add_subregion()) or referenced in an alias |
| 311 | * (see memory_region_init_alias()). |
| 312 | */ |
| 313 | void memory_region_destroy(MemoryRegion *mr); |
| 314 | |
| 315 | /** |
| 316 | * memory_region_size: get a memory region's size. |
| 317 | * |
| 318 | * @mr: the memory region being queried. |
| 319 | */ |
| 320 | uint64_t memory_region_size(MemoryRegion *mr); |
| 321 | |
| 322 | /** |
Avi Kivity | 8ea9252 | 2011-12-08 15:58:43 +0200 | [diff] [blame] | 323 | * memory_region_is_ram: check whether a memory region is random access |
| 324 | * |
| 325 | * Returns %true is a memory region is random access. |
| 326 | * |
| 327 | * @mr: the memory region being queried |
| 328 | */ |
| 329 | bool memory_region_is_ram(MemoryRegion *mr); |
| 330 | |
| 331 | /** |
Avi Kivity | 8991c79 | 2011-12-20 15:53:11 +0200 | [diff] [blame] | 332 | * memory_region_name: get a memory region's name |
| 333 | * |
| 334 | * Returns the string that was used to initialize the memory region. |
| 335 | * |
| 336 | * @mr: the memory region being queried |
| 337 | */ |
| 338 | const char *memory_region_name(MemoryRegion *mr); |
| 339 | |
| 340 | /** |
Avi Kivity | 55043ba | 2011-12-15 17:20:34 +0200 | [diff] [blame] | 341 | * memory_region_is_logging: return whether a memory region is logging writes |
| 342 | * |
| 343 | * Returns %true if the memory region is logging writes |
| 344 | * |
| 345 | * @mr: the memory region being queried |
| 346 | */ |
| 347 | bool memory_region_is_logging(MemoryRegion *mr); |
| 348 | |
| 349 | /** |
Avi Kivity | ce7923d | 2011-12-08 16:05:11 +0200 | [diff] [blame] | 350 | * memory_region_is_rom: check whether a memory region is ROM |
| 351 | * |
| 352 | * Returns %true is a memory region is read-only memory. |
| 353 | * |
| 354 | * @mr: the memory region being queried |
| 355 | */ |
| 356 | bool memory_region_is_rom(MemoryRegion *mr); |
| 357 | |
| 358 | /** |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 359 | * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. |
| 360 | * |
| 361 | * Returns a host pointer to a RAM memory region (created with |
| 362 | * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with |
| 363 | * care. |
| 364 | * |
| 365 | * @mr: the memory region being queried. |
| 366 | */ |
| 367 | void *memory_region_get_ram_ptr(MemoryRegion *mr); |
| 368 | |
| 369 | /** |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 370 | * memory_region_set_log: Turn dirty logging on or off for a region. |
| 371 | * |
| 372 | * Turns dirty logging on or off for a specified client (display, migration). |
| 373 | * Only meaningful for RAM regions. |
| 374 | * |
| 375 | * @mr: the memory region being updated. |
| 376 | * @log: whether dirty logging is to be enabled or disabled. |
| 377 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or |
| 378 | * %DIRTY_MEMORY_VGA. |
| 379 | */ |
| 380 | void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client); |
| 381 | |
| 382 | /** |
Blue Swirl | cd7a45c | 2012-01-22 16:38:21 +0000 | [diff] [blame] | 383 | * memory_region_get_dirty: Check whether a range of bytes is dirty |
| 384 | * for a specified client. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 385 | * |
Blue Swirl | cd7a45c | 2012-01-22 16:38:21 +0000 | [diff] [blame] | 386 | * 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] | 387 | * call to memory_region_reset_dirty() with the same @client. Dirty logging |
| 388 | * must be enabled. |
| 389 | * |
| 390 | * @mr: the memory region being queried. |
| 391 | * @addr: the address (relative to the start of the region) being queried. |
Blue Swirl | cd7a45c | 2012-01-22 16:38:21 +0000 | [diff] [blame] | 392 | * @size: the size of the range being queried. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 393 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or |
| 394 | * %DIRTY_MEMORY_VGA. |
| 395 | */ |
| 396 | bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr, |
Blue Swirl | cd7a45c | 2012-01-22 16:38:21 +0000 | [diff] [blame] | 397 | target_phys_addr_t size, unsigned client); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 398 | |
| 399 | /** |
Blue Swirl | fd4aa97 | 2011-10-16 16:04:59 +0000 | [diff] [blame] | 400 | * 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] | 401 | * |
Blue Swirl | fd4aa97 | 2011-10-16 16:04:59 +0000 | [diff] [blame] | 402 | * Marks a range of bytes as dirty, after it has been dirtied outside |
| 403 | * guest code. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 404 | * |
Blue Swirl | fd4aa97 | 2011-10-16 16:04:59 +0000 | [diff] [blame] | 405 | * @mr: the memory region being dirtied. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 406 | * @addr: the address (relative to the start of the region) being dirtied. |
Blue Swirl | fd4aa97 | 2011-10-16 16:04:59 +0000 | [diff] [blame] | 407 | * @size: size of the range being dirtied. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 408 | */ |
Blue Swirl | fd4aa97 | 2011-10-16 16:04:59 +0000 | [diff] [blame] | 409 | void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr, |
| 410 | target_phys_addr_t size); |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 411 | |
| 412 | /** |
| 413 | * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with |
| 414 | * any external TLBs (e.g. kvm) |
| 415 | * |
| 416 | * Flushes dirty information from accelerators such as kvm and vhost-net |
| 417 | * and makes it available to users of the memory API. |
| 418 | * |
| 419 | * @mr: the region being flushed. |
| 420 | */ |
| 421 | void memory_region_sync_dirty_bitmap(MemoryRegion *mr); |
| 422 | |
| 423 | /** |
| 424 | * memory_region_reset_dirty: Mark a range of pages as clean, for a specified |
| 425 | * client. |
| 426 | * |
| 427 | * Marks a range of pages as no longer dirty. |
| 428 | * |
| 429 | * @mr: the region being updated. |
| 430 | * @addr: the start of the subrange being cleaned. |
| 431 | * @size: the size of the subrange being cleaned. |
| 432 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or |
| 433 | * %DIRTY_MEMORY_VGA. |
| 434 | */ |
| 435 | void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr, |
| 436 | target_phys_addr_t size, unsigned client); |
| 437 | |
| 438 | /** |
| 439 | * memory_region_set_readonly: Turn a memory region read-only (or read-write) |
| 440 | * |
| 441 | * Allows a memory region to be marked as read-only (turning it into a ROM). |
| 442 | * only useful on RAM regions. |
| 443 | * |
| 444 | * @mr: the region being updated. |
| 445 | * @readonly: whether rhe region is to be ROM or RAM. |
| 446 | */ |
| 447 | void memory_region_set_readonly(MemoryRegion *mr, bool readonly); |
| 448 | |
| 449 | /** |
Avi Kivity | d0a9b5b | 2011-08-08 19:58:49 +0300 | [diff] [blame] | 450 | * memory_region_rom_device_set_readable: enable/disable ROM readability |
| 451 | * |
| 452 | * Allows a ROM device (initialized with memory_region_init_rom_device() to |
| 453 | * to be marked as readable (default) or not readable. When it is readable, |
| 454 | * the device is mapped to guest memory. When not readable, reads are |
| 455 | * forwarded to the #MemoryRegion.read function. |
| 456 | * |
| 457 | * @mr: the memory region to be updated |
| 458 | * @readable: whether reads are satisified directly (%true) or via callbacks |
| 459 | * (%false) |
| 460 | */ |
| 461 | void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable); |
| 462 | |
| 463 | /** |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 464 | * memory_region_set_coalescing: Enable memory coalescing for the region. |
| 465 | * |
| 466 | * Enabled writes to a region to be queued for later processing. MMIO ->write |
| 467 | * callbacks may be delayed until a non-coalesced MMIO is issued. |
| 468 | * Only useful for IO regions. Roughly similar to write-combining hardware. |
| 469 | * |
| 470 | * @mr: the memory region to be write coalesced |
| 471 | */ |
| 472 | void memory_region_set_coalescing(MemoryRegion *mr); |
| 473 | |
| 474 | /** |
| 475 | * memory_region_add_coalescing: Enable memory coalescing for a sub-range of |
| 476 | * a region. |
| 477 | * |
| 478 | * Like memory_region_set_coalescing(), but works on a sub-range of a region. |
| 479 | * Multiple calls can be issued coalesced disjoint ranges. |
| 480 | * |
| 481 | * @mr: the memory region to be updated. |
| 482 | * @offset: the start of the range within the region to be coalesced. |
| 483 | * @size: the size of the subrange to be coalesced. |
| 484 | */ |
| 485 | void memory_region_add_coalescing(MemoryRegion *mr, |
| 486 | target_phys_addr_t offset, |
| 487 | uint64_t size); |
| 488 | |
| 489 | /** |
| 490 | * memory_region_clear_coalescing: Disable MMIO coalescing for the region. |
| 491 | * |
| 492 | * Disables any coalescing caused by memory_region_set_coalescing() or |
| 493 | * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory |
| 494 | * hardware. |
| 495 | * |
| 496 | * @mr: the memory region to be updated. |
| 497 | */ |
| 498 | void memory_region_clear_coalescing(MemoryRegion *mr); |
| 499 | |
| 500 | /** |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 501 | * memory_region_add_eventfd: Request an eventfd to be triggered when a word |
| 502 | * is written to a location. |
| 503 | * |
| 504 | * Marks a word in an IO region (initialized with memory_region_init_io()) |
| 505 | * 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] | 506 | * 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] | 507 | * action if the callback _is_ called). |
| 508 | * |
| 509 | * @mr: the memory region being updated. |
| 510 | * @addr: the address within @mr that is to be monitored |
| 511 | * @size: the size of the access to trigger the eventfd |
| 512 | * @match_data: whether to match against @data, instead of just @addr |
| 513 | * @data: the data to match against the guest write |
| 514 | * @fd: the eventfd to be triggered when @addr, @size, and @data all match. |
| 515 | **/ |
| 516 | void memory_region_add_eventfd(MemoryRegion *mr, |
| 517 | target_phys_addr_t addr, |
| 518 | unsigned size, |
| 519 | bool match_data, |
| 520 | uint64_t data, |
| 521 | int fd); |
| 522 | |
| 523 | /** |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 524 | * memory_region_del_eventfd: Cancel an eventfd. |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 525 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 526 | * Cancels an eventfd trigger requested by a previous |
| 527 | * memory_region_add_eventfd() call. |
Avi Kivity | 3e9d69e | 2011-07-26 14:26:11 +0300 | [diff] [blame] | 528 | * |
| 529 | * @mr: the memory region being updated. |
| 530 | * @addr: the address within @mr that is to be monitored |
| 531 | * @size: the size of the access to trigger the eventfd |
| 532 | * @match_data: whether to match against @data, instead of just @addr |
| 533 | * @data: the data to match against the guest write |
| 534 | * @fd: the eventfd to be triggered when @addr, @size, and @data all match. |
| 535 | */ |
| 536 | void memory_region_del_eventfd(MemoryRegion *mr, |
| 537 | target_phys_addr_t addr, |
| 538 | unsigned size, |
| 539 | bool match_data, |
| 540 | uint64_t data, |
| 541 | int fd); |
| 542 | /** |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 543 | * memory_region_add_subregion: Add a subregion to a container. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 544 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 545 | * Adds a subregion at @offset. The subregion may not overlap with other |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 546 | * subregions (except for those explicitly marked as overlapping). A region |
| 547 | * may only be added once as a subregion (unless removed with |
| 548 | * memory_region_del_subregion()); use memory_region_init_alias() if you |
| 549 | * want a region to be a subregion in multiple locations. |
| 550 | * |
| 551 | * @mr: the region to contain the new subregion; must be a container |
| 552 | * initialized with memory_region_init(). |
| 553 | * @offset: the offset relative to @mr where @subregion is added. |
| 554 | * @subregion: the subregion to be added. |
| 555 | */ |
| 556 | void memory_region_add_subregion(MemoryRegion *mr, |
| 557 | target_phys_addr_t offset, |
| 558 | MemoryRegion *subregion); |
| 559 | /** |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 560 | * memory_region_add_subregion: Add a subregion to a container, with overlap. |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 561 | * |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 562 | * Adds a subregion at @offset. The subregion may overlap with other |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 563 | * subregions. Conflicts are resolved by having a higher @priority hide a |
| 564 | * lower @priority. Subregions without priority are taken as @priority 0. |
| 565 | * A region may only be added once as a subregion (unless removed with |
| 566 | * memory_region_del_subregion()); use memory_region_init_alias() if you |
| 567 | * want a region to be a subregion in multiple locations. |
| 568 | * |
| 569 | * @mr: the region to contain the new subregion; must be a container |
| 570 | * initialized with memory_region_init(). |
| 571 | * @offset: the offset relative to @mr where @subregion is added. |
| 572 | * @subregion: the subregion to be added. |
| 573 | * @priority: used for resolving overlaps; highest priority wins. |
| 574 | */ |
| 575 | void memory_region_add_subregion_overlap(MemoryRegion *mr, |
| 576 | target_phys_addr_t offset, |
| 577 | MemoryRegion *subregion, |
| 578 | unsigned priority); |
Avi Kivity | e34911c | 2011-12-19 12:06:23 +0200 | [diff] [blame] | 579 | |
| 580 | /** |
| 581 | * memory_region_get_ram_addr: Get the ram address associated with a memory |
| 582 | * region |
| 583 | * |
Stefan Weil | dabdf39 | 2012-01-08 19:35:09 +0100 | [diff] [blame] | 584 | * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen |
Avi Kivity | e34911c | 2011-12-19 12:06:23 +0200 | [diff] [blame] | 585 | * code is being reworked. |
| 586 | */ |
| 587 | ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr); |
| 588 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 589 | /** |
| 590 | * memory_region_del_subregion: Remove a subregion. |
| 591 | * |
| 592 | * Removes a subregion from its container. |
| 593 | * |
| 594 | * @mr: the container to be updated. |
| 595 | * @subregion: the region being removed; must be a current subregion of @mr. |
| 596 | */ |
| 597 | void memory_region_del_subregion(MemoryRegion *mr, |
| 598 | MemoryRegion *subregion); |
| 599 | |
Avi Kivity | 6bba19b | 2011-09-14 11:54:58 +0300 | [diff] [blame] | 600 | /* |
| 601 | * memory_region_set_enabled: dynamically enable or disable a region |
| 602 | * |
| 603 | * Enables or disables a memory region. A disabled memory region |
| 604 | * ignores all accesses to itself and its subregions. It does not |
| 605 | * obscure sibling subregions with lower priority - it simply behaves as |
| 606 | * if it was removed from the hierarchy. |
| 607 | * |
| 608 | * Regions default to being enabled. |
| 609 | * |
| 610 | * @mr: the region to be updated |
| 611 | * @enabled: whether to enable or disable the region |
| 612 | */ |
| 613 | void memory_region_set_enabled(MemoryRegion *mr, bool enabled); |
| 614 | |
Avi Kivity | 2282e1a | 2011-09-14 12:10:12 +0300 | [diff] [blame] | 615 | /* |
| 616 | * memory_region_set_address: dynamically update the address of a region |
| 617 | * |
| 618 | * Dynamically updates the address of a region, relative to its parent. |
| 619 | * May be used on regions are currently part of a memory hierarchy. |
| 620 | * |
| 621 | * @mr: the region to be updated |
| 622 | * @addr: new address, relative to parent region |
| 623 | */ |
| 624 | void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr); |
| 625 | |
Avi Kivity | 4703359 | 2011-12-04 19:16:50 +0200 | [diff] [blame] | 626 | /* |
| 627 | * memory_region_set_alias_offset: dynamically update a memory alias's offset |
| 628 | * |
| 629 | * Dynamically updates the offset into the target region that an alias points |
| 630 | * to, as if the fourth argument to memory_region_init_alias() has changed. |
| 631 | * |
| 632 | * @mr: the #MemoryRegion to be updated; should be an alias. |
| 633 | * @offset: the new offset into the target memory region |
| 634 | */ |
| 635 | void memory_region_set_alias_offset(MemoryRegion *mr, |
| 636 | target_phys_addr_t offset); |
| 637 | |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 638 | /** |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 639 | * memory_region_find: locate a MemoryRegion in an address space |
| 640 | * |
| 641 | * Locates the first #MemoryRegion within an address space given by |
| 642 | * @address_space that overlaps the range given by @addr and @size. |
| 643 | * |
| 644 | * Returns a #MemoryRegionSection that describes a contiguous overlap. |
| 645 | * It will have the following characteristics: |
| 646 | * .@offset_within_address_space >= @addr |
| 647 | * .@offset_within_address_space + .@size <= @addr + @size |
| 648 | * .@size = 0 iff no overlap was found |
| 649 | * .@mr is non-%NULL iff an overlap was found |
| 650 | * |
| 651 | * @address_space: a top-level (i.e. parentless) region that contains |
| 652 | * the region to be found |
| 653 | * @addr: start of the area within @address_space to be searched |
| 654 | * @size: size of the area to be searched |
| 655 | */ |
| 656 | MemoryRegionSection memory_region_find(MemoryRegion *address_space, |
| 657 | target_phys_addr_t addr, uint64_t size); |
| 658 | |
Avi Kivity | 86e775c | 2011-12-15 16:24:49 +0200 | [diff] [blame] | 659 | |
| 660 | /** |
| 661 | * memory_global_sync_dirty_bitmap: synchronize the dirty log for all memory |
| 662 | * |
| 663 | * Synchronizes the dirty page log for an entire address space. |
| 664 | * @address_space: a top-level (i.e. parentless) region that contains the |
| 665 | * memory being synchronized |
| 666 | */ |
| 667 | void memory_global_sync_dirty_bitmap(MemoryRegion *address_space); |
| 668 | |
Avi Kivity | e217795 | 2011-12-08 15:00:18 +0200 | [diff] [blame] | 669 | /** |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 670 | * memory_region_transaction_begin: Start a transaction. |
| 671 | * |
| 672 | * During a transaction, changes will be accumulated and made visible |
Stefan Weil | dabdf39 | 2012-01-08 19:35:09 +0100 | [diff] [blame] | 673 | * only when the transaction ends (is committed). |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 674 | */ |
| 675 | void memory_region_transaction_begin(void); |
Ademar de Souza Reis Jr | 69ddaf6 | 2011-12-05 16:54:14 -0300 | [diff] [blame] | 676 | |
| 677 | /** |
| 678 | * memory_region_transaction_commit: Commit a transaction and make changes |
| 679 | * visible to the guest. |
Avi Kivity | 4ef4db8 | 2011-07-26 14:26:13 +0300 | [diff] [blame] | 680 | */ |
| 681 | void memory_region_transaction_commit(void); |
| 682 | |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 683 | /** |
| 684 | * memory_listener_register: register callbacks to be called when memory |
| 685 | * sections are mapped or unmapped into an address |
| 686 | * space |
| 687 | * |
| 688 | * @listener: an object containing the callbacks to be called |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame^] | 689 | * @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] | 690 | */ |
Avi Kivity | 7376e58 | 2012-02-08 21:05:17 +0200 | [diff] [blame^] | 691 | void memory_listener_register(MemoryListener *listener, MemoryRegion *filter); |
Avi Kivity | 7664e80 | 2011-12-11 14:47:25 +0200 | [diff] [blame] | 692 | |
| 693 | /** |
| 694 | * memory_listener_unregister: undo the effect of memory_listener_register() |
| 695 | * |
| 696 | * @listener: an object containing the callbacks to be removed |
| 697 | */ |
| 698 | void memory_listener_unregister(MemoryListener *listener); |
| 699 | |
| 700 | /** |
| 701 | * memory_global_dirty_log_start: begin dirty logging for all regions |
| 702 | */ |
| 703 | void memory_global_dirty_log_start(void); |
| 704 | |
| 705 | /** |
| 706 | * memory_global_dirty_log_stop: begin dirty logging for all regions |
| 707 | */ |
| 708 | void memory_global_dirty_log_stop(void); |
| 709 | |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 710 | void mtree_info(fprintf_function mon_printf, void *f); |
| 711 | |
Avi Kivity | 093bc2c | 2011-07-26 14:26:01 +0300 | [diff] [blame] | 712 | #endif |
| 713 | |
| 714 | #endif |