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