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