]> git.proxmox.com Git - qemu.git/blob - memory.h
memory: introduce memory_region_find()
[qemu.git] / memory.h
1 /*
2 * Physical memory management API
3 *
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
5 *
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14 #ifndef MEMORY_H
15 #define MEMORY_H
16
17 #ifndef CONFIG_USER_ONLY
18
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include "qemu-common.h"
22 #include "cpu-common.h"
23 #include "targphys.h"
24 #include "qemu-queue.h"
25 #include "iorange.h"
26 #include "ioport.h"
27 #include "int128.h"
28
29 typedef struct MemoryRegionOps MemoryRegionOps;
30 typedef struct MemoryRegion MemoryRegion;
31 typedef struct MemoryRegionPortio MemoryRegionPortio;
32 typedef struct MemoryRegionMmio MemoryRegionMmio;
33
34 /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
35 * registration.
36 */
37 #define DIRTY_MEMORY_VGA 0
38 #define DIRTY_MEMORY_CODE 1
39 #define DIRTY_MEMORY_MIGRATION 3
40
41 struct MemoryRegionMmio {
42 CPUReadMemoryFunc *read[3];
43 CPUWriteMemoryFunc *write[3];
44 };
45
46 /*
47 * Memory region callbacks
48 */
49 struct MemoryRegionOps {
50 /* Read from the memory region. @addr is relative to @mr; @size is
51 * in bytes. */
52 uint64_t (*read)(void *opaque,
53 target_phys_addr_t addr,
54 unsigned size);
55 /* Write to the memory region. @addr is relative to @mr; @size is
56 * in bytes. */
57 void (*write)(void *opaque,
58 target_phys_addr_t addr,
59 uint64_t data,
60 unsigned size);
61
62 enum device_endian endianness;
63 /* Guest-visible constraints: */
64 struct {
65 /* If nonzero, specify bounds on access sizes beyond which a machine
66 * check is thrown.
67 */
68 unsigned min_access_size;
69 unsigned max_access_size;
70 /* If true, unaligned accesses are supported. Otherwise unaligned
71 * accesses throw machine checks.
72 */
73 bool unaligned;
74 /*
75 * If present, and returns #false, the transaction is not accepted
76 * by the device (and results in machine dependent behaviour such
77 * as a machine check exception).
78 */
79 bool (*accepts)(void *opaque, target_phys_addr_t addr,
80 unsigned size, bool is_write);
81 } valid;
82 /* Internal implementation constraints: */
83 struct {
84 /* If nonzero, specifies the minimum size implemented. Smaller sizes
85 * will be rounded upwards and a partial result will be returned.
86 */
87 unsigned min_access_size;
88 /* If nonzero, specifies the maximum size implemented. Larger sizes
89 * will be done as a series of accesses with smaller sizes.
90 */
91 unsigned max_access_size;
92 /* If true, unaligned accesses are supported. Otherwise all accesses
93 * are converted to (possibly multiple) naturally aligned accesses.
94 */
95 bool unaligned;
96 } impl;
97
98 /* If .read and .write are not present, old_portio may be used for
99 * backwards compatibility with old portio registration
100 */
101 const MemoryRegionPortio *old_portio;
102 /* If .read and .write are not present, old_mmio may be used for
103 * backwards compatibility with old mmio registration
104 */
105 const MemoryRegionMmio old_mmio;
106 };
107
108 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
109 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
110
111 struct MemoryRegion {
112 /* All fields are private - violators will be prosecuted */
113 const MemoryRegionOps *ops;
114 void *opaque;
115 MemoryRegion *parent;
116 Int128 size;
117 target_phys_addr_t addr;
118 target_phys_addr_t offset;
119 bool backend_registered;
120 void (*destructor)(MemoryRegion *mr);
121 ram_addr_t ram_addr;
122 IORange iorange;
123 bool terminates;
124 bool readable;
125 bool ram;
126 bool readonly; /* For RAM regions */
127 bool enabled;
128 MemoryRegion *alias;
129 target_phys_addr_t alias_offset;
130 unsigned priority;
131 bool may_overlap;
132 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
133 QTAILQ_ENTRY(MemoryRegion) subregions_link;
134 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
135 const char *name;
136 uint8_t dirty_log_mask;
137 unsigned ioeventfd_nb;
138 MemoryRegionIoeventfd *ioeventfds;
139 };
140
141 struct MemoryRegionPortio {
142 uint32_t offset;
143 uint32_t len;
144 unsigned size;
145 IOPortReadFunc *read;
146 IOPortWriteFunc *write;
147 };
148
149 #define PORTIO_END_OF_LIST() { }
150
151 typedef struct MemoryRegionSection MemoryRegionSection;
152
153 /**
154 * MemoryRegionSection: describes a fragment of a #MemoryRegion
155 *
156 * @mr: the region, or %NULL if empty
157 * @offset_within_region: the beginning of the section, relative to @mr's start
158 * @size: the size of the section; will not exceed @mr's boundaries
159 * @offset_within_address_space: the address of the first byte of the section
160 * relative to the region's address space
161 */
162 struct MemoryRegionSection {
163 MemoryRegion *mr;
164 target_phys_addr_t offset_within_region;
165 uint64_t size;
166 target_phys_addr_t offset_within_address_space;
167 };
168
169 /**
170 * memory_region_init: Initialize a memory region
171 *
172 * The region typically acts as a container for other memory regions. Use
173 * memory_region_add_subregion() to add subregions.
174 *
175 * @mr: the #MemoryRegion to be initialized
176 * @name: used for debugging; not visible to the user or ABI
177 * @size: size of the region; any subregions beyond this size will be clipped
178 */
179 void memory_region_init(MemoryRegion *mr,
180 const char *name,
181 uint64_t size);
182 /**
183 * memory_region_init_io: Initialize an I/O memory region.
184 *
185 * Accesses into the region will cause the callbacks in @ops to be called.
186 * if @size is nonzero, subregions will be clipped to @size.
187 *
188 * @mr: the #MemoryRegion to be initialized.
189 * @ops: a structure containing read and write callbacks to be used when
190 * I/O is performed on the region.
191 * @opaque: passed to to the read and write callbacks of the @ops structure.
192 * @name: used for debugging; not visible to the user or ABI
193 * @size: size of the region.
194 */
195 void memory_region_init_io(MemoryRegion *mr,
196 const MemoryRegionOps *ops,
197 void *opaque,
198 const char *name,
199 uint64_t size);
200
201 /**
202 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
203 * region will modify memory directly.
204 *
205 * @mr: the #MemoryRegion to be initialized.
206 * @dev: a device associated with the region; may be %NULL.
207 * @name: the name of the region; the pair (@dev, @name) must be globally
208 * unique. The name is part of the save/restore ABI and so cannot be
209 * changed.
210 * @size: size of the region.
211 */
212 void memory_region_init_ram(MemoryRegion *mr,
213 DeviceState *dev, /* FIXME: layering violation */
214 const char *name,
215 uint64_t size);
216
217 /**
218 * memory_region_init_ram: Initialize RAM memory region from a user-provided.
219 * pointer. Accesses into the region will modify
220 * memory directly.
221 *
222 * @mr: the #MemoryRegion to be initialized.
223 * @dev: a device associated with the region; may be %NULL.
224 * @name: the name of the region; the pair (@dev, @name) must be globally
225 * unique. The name is part of the save/restore ABI and so cannot be
226 * changed.
227 * @size: size of the region.
228 * @ptr: memory to be mapped; must contain at least @size bytes.
229 */
230 void memory_region_init_ram_ptr(MemoryRegion *mr,
231 DeviceState *dev, /* FIXME: layering violation */
232 const char *name,
233 uint64_t size,
234 void *ptr);
235
236 /**
237 * memory_region_init_alias: Initialize a memory region that aliases all or a
238 * part of another memory region.
239 *
240 * @mr: the #MemoryRegion to be initialized.
241 * @name: used for debugging; not visible to the user or ABI
242 * @orig: the region to be referenced; @mr will be equivalent to
243 * @orig between @offset and @offset + @size - 1.
244 * @offset: start of the section in @orig to be referenced.
245 * @size: size of the region.
246 */
247 void memory_region_init_alias(MemoryRegion *mr,
248 const char *name,
249 MemoryRegion *orig,
250 target_phys_addr_t offset,
251 uint64_t size);
252
253 /**
254 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
255 * handled via callbacks.
256 *
257 * @mr: the #MemoryRegion to be initialized.
258 * @ops: callbacks for write access handling.
259 * @dev: a device associated with the region; may be %NULL.
260 * @name: the name of the region; the pair (@dev, @name) must be globally
261 * unique. The name is part of the save/restore ABI and so cannot be
262 * changed.
263 * @size: size of the region.
264 */
265 void memory_region_init_rom_device(MemoryRegion *mr,
266 const MemoryRegionOps *ops,
267 void *opaque,
268 DeviceState *dev, /* FIXME: layering violation */
269 const char *name,
270 uint64_t size);
271
272 /**
273 * memory_region_destroy: Destroy a memory region and reclaim all resources.
274 *
275 * @mr: the region to be destroyed. May not currently be a subregion
276 * (see memory_region_add_subregion()) or referenced in an alias
277 * (see memory_region_init_alias()).
278 */
279 void memory_region_destroy(MemoryRegion *mr);
280
281 /**
282 * memory_region_size: get a memory region's size.
283 *
284 * @mr: the memory region being queried.
285 */
286 uint64_t memory_region_size(MemoryRegion *mr);
287
288 /**
289 * memory_region_is_ram: check whether a memory region is random access
290 *
291 * Returns %true is a memory region is random access.
292 *
293 * @mr: the memory region being queried
294 */
295 bool memory_region_is_ram(MemoryRegion *mr);
296
297 /**
298 * memory_region_is_logging: return whether a memory region is logging writes
299 *
300 * Returns %true if the memory region is logging writes
301 *
302 * @mr: the memory region being queried
303 */
304 bool memory_region_is_logging(MemoryRegion *mr);
305
306 /**
307 * memory_region_is_rom: check whether a memory region is ROM
308 *
309 * Returns %true is a memory region is read-only memory.
310 *
311 * @mr: the memory region being queried
312 */
313 bool memory_region_is_rom(MemoryRegion *mr);
314
315 /**
316 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
317 *
318 * Returns a host pointer to a RAM memory region (created with
319 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
320 * care.
321 *
322 * @mr: the memory region being queried.
323 */
324 void *memory_region_get_ram_ptr(MemoryRegion *mr);
325
326 /**
327 * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
328 * callbacks.
329 *
330 * This function is deprecated and should not be used in new code.
331 */
332 void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset);
333
334 /**
335 * memory_region_set_log: Turn dirty logging on or off for a region.
336 *
337 * Turns dirty logging on or off for a specified client (display, migration).
338 * Only meaningful for RAM regions.
339 *
340 * @mr: the memory region being updated.
341 * @log: whether dirty logging is to be enabled or disabled.
342 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
343 * %DIRTY_MEMORY_VGA.
344 */
345 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
346
347 /**
348 * memory_region_get_dirty: Check whether a page is dirty for a specified
349 * client.
350 *
351 * Checks whether a page has been written to since the last
352 * call to memory_region_reset_dirty() with the same @client. Dirty logging
353 * must be enabled.
354 *
355 * @mr: the memory region being queried.
356 * @addr: the address (relative to the start of the region) being queried.
357 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
358 * %DIRTY_MEMORY_VGA.
359 */
360 bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
361 unsigned client);
362
363 /**
364 * memory_region_set_dirty: Mark a page as dirty in a memory region.
365 *
366 * Marks a page as dirty, after it has been dirtied outside guest code.
367 *
368 * @mr: the memory region being queried.
369 * @addr: the address (relative to the start of the region) being dirtied.
370 */
371 void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr);
372
373 /**
374 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
375 * any external TLBs (e.g. kvm)
376 *
377 * Flushes dirty information from accelerators such as kvm and vhost-net
378 * and makes it available to users of the memory API.
379 *
380 * @mr: the region being flushed.
381 */
382 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
383
384 /**
385 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
386 * client.
387 *
388 * Marks a range of pages as no longer dirty.
389 *
390 * @mr: the region being updated.
391 * @addr: the start of the subrange being cleaned.
392 * @size: the size of the subrange being cleaned.
393 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
394 * %DIRTY_MEMORY_VGA.
395 */
396 void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
397 target_phys_addr_t size, unsigned client);
398
399 /**
400 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
401 *
402 * Allows a memory region to be marked as read-only (turning it into a ROM).
403 * only useful on RAM regions.
404 *
405 * @mr: the region being updated.
406 * @readonly: whether rhe region is to be ROM or RAM.
407 */
408 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
409
410 /**
411 * memory_region_rom_device_set_readable: enable/disable ROM readability
412 *
413 * Allows a ROM device (initialized with memory_region_init_rom_device() to
414 * to be marked as readable (default) or not readable. When it is readable,
415 * the device is mapped to guest memory. When not readable, reads are
416 * forwarded to the #MemoryRegion.read function.
417 *
418 * @mr: the memory region to be updated
419 * @readable: whether reads are satisified directly (%true) or via callbacks
420 * (%false)
421 */
422 void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable);
423
424 /**
425 * memory_region_set_coalescing: Enable memory coalescing for the region.
426 *
427 * Enabled writes to a region to be queued for later processing. MMIO ->write
428 * callbacks may be delayed until a non-coalesced MMIO is issued.
429 * Only useful for IO regions. Roughly similar to write-combining hardware.
430 *
431 * @mr: the memory region to be write coalesced
432 */
433 void memory_region_set_coalescing(MemoryRegion *mr);
434
435 /**
436 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
437 * a region.
438 *
439 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
440 * Multiple calls can be issued coalesced disjoint ranges.
441 *
442 * @mr: the memory region to be updated.
443 * @offset: the start of the range within the region to be coalesced.
444 * @size: the size of the subrange to be coalesced.
445 */
446 void memory_region_add_coalescing(MemoryRegion *mr,
447 target_phys_addr_t offset,
448 uint64_t size);
449
450 /**
451 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
452 *
453 * Disables any coalescing caused by memory_region_set_coalescing() or
454 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
455 * hardware.
456 *
457 * @mr: the memory region to be updated.
458 */
459 void memory_region_clear_coalescing(MemoryRegion *mr);
460
461 /**
462 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
463 * is written to a location.
464 *
465 * Marks a word in an IO region (initialized with memory_region_init_io())
466 * as a trigger for an eventfd event. The I/O callback will not be called.
467 * The caller must be prepared to handle failure (that is, take the required
468 * action if the callback _is_ called).
469 *
470 * @mr: the memory region being updated.
471 * @addr: the address within @mr that is to be monitored
472 * @size: the size of the access to trigger the eventfd
473 * @match_data: whether to match against @data, instead of just @addr
474 * @data: the data to match against the guest write
475 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
476 **/
477 void memory_region_add_eventfd(MemoryRegion *mr,
478 target_phys_addr_t addr,
479 unsigned size,
480 bool match_data,
481 uint64_t data,
482 int fd);
483
484 /**
485 * memory_region_del_eventfd: Cancel an eventfd.
486 *
487 * Cancels an eventfd trigger requested by a previous
488 * memory_region_add_eventfd() call.
489 *
490 * @mr: the memory region being updated.
491 * @addr: the address within @mr that is to be monitored
492 * @size: the size of the access to trigger the eventfd
493 * @match_data: whether to match against @data, instead of just @addr
494 * @data: the data to match against the guest write
495 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
496 */
497 void memory_region_del_eventfd(MemoryRegion *mr,
498 target_phys_addr_t addr,
499 unsigned size,
500 bool match_data,
501 uint64_t data,
502 int fd);
503 /**
504 * memory_region_add_subregion: Add a subregion to a container.
505 *
506 * Adds a subregion at @offset. The subregion may not overlap with other
507 * subregions (except for those explicitly marked as overlapping). A region
508 * may only be added once as a subregion (unless removed with
509 * memory_region_del_subregion()); use memory_region_init_alias() if you
510 * want a region to be a subregion in multiple locations.
511 *
512 * @mr: the region to contain the new subregion; must be a container
513 * initialized with memory_region_init().
514 * @offset: the offset relative to @mr where @subregion is added.
515 * @subregion: the subregion to be added.
516 */
517 void memory_region_add_subregion(MemoryRegion *mr,
518 target_phys_addr_t offset,
519 MemoryRegion *subregion);
520 /**
521 * memory_region_add_subregion: Add a subregion to a container, with overlap.
522 *
523 * Adds a subregion at @offset. The subregion may overlap with other
524 * subregions. Conflicts are resolved by having a higher @priority hide a
525 * lower @priority. Subregions without priority are taken as @priority 0.
526 * A region may only be added once as a subregion (unless removed with
527 * memory_region_del_subregion()); use memory_region_init_alias() if you
528 * want a region to be a subregion in multiple locations.
529 *
530 * @mr: the region to contain the new subregion; must be a container
531 * initialized with memory_region_init().
532 * @offset: the offset relative to @mr where @subregion is added.
533 * @subregion: the subregion to be added.
534 * @priority: used for resolving overlaps; highest priority wins.
535 */
536 void memory_region_add_subregion_overlap(MemoryRegion *mr,
537 target_phys_addr_t offset,
538 MemoryRegion *subregion,
539 unsigned priority);
540 /**
541 * memory_region_del_subregion: Remove a subregion.
542 *
543 * Removes a subregion from its container.
544 *
545 * @mr: the container to be updated.
546 * @subregion: the region being removed; must be a current subregion of @mr.
547 */
548 void memory_region_del_subregion(MemoryRegion *mr,
549 MemoryRegion *subregion);
550
551 /*
552 * memory_region_set_enabled: dynamically enable or disable a region
553 *
554 * Enables or disables a memory region. A disabled memory region
555 * ignores all accesses to itself and its subregions. It does not
556 * obscure sibling subregions with lower priority - it simply behaves as
557 * if it was removed from the hierarchy.
558 *
559 * Regions default to being enabled.
560 *
561 * @mr: the region to be updated
562 * @enabled: whether to enable or disable the region
563 */
564 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
565
566 /*
567 * memory_region_set_address: dynamically update the address of a region
568 *
569 * Dynamically updates the address of a region, relative to its parent.
570 * May be used on regions are currently part of a memory hierarchy.
571 *
572 * @mr: the region to be updated
573 * @addr: new address, relative to parent region
574 */
575 void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr);
576
577 /*
578 * memory_region_set_alias_offset: dynamically update a memory alias's offset
579 *
580 * Dynamically updates the offset into the target region that an alias points
581 * to, as if the fourth argument to memory_region_init_alias() has changed.
582 *
583 * @mr: the #MemoryRegion to be updated; should be an alias.
584 * @offset: the new offset into the target memory region
585 */
586 void memory_region_set_alias_offset(MemoryRegion *mr,
587 target_phys_addr_t offset);
588
589 /**
590 * memory_region_find: locate a MemoryRegion in an address space
591 *
592 * Locates the first #MemoryRegion within an address space given by
593 * @address_space that overlaps the range given by @addr and @size.
594 *
595 * Returns a #MemoryRegionSection that describes a contiguous overlap.
596 * It will have the following characteristics:
597 * .@offset_within_address_space >= @addr
598 * .@offset_within_address_space + .@size <= @addr + @size
599 * .@size = 0 iff no overlap was found
600 * .@mr is non-%NULL iff an overlap was found
601 *
602 * @address_space: a top-level (i.e. parentless) region that contains
603 * the region to be found
604 * @addr: start of the area within @address_space to be searched
605 * @size: size of the area to be searched
606 */
607 MemoryRegionSection memory_region_find(MemoryRegion *address_space,
608 target_phys_addr_t addr, uint64_t size);
609
610 /**
611 * memory_region_transaction_begin: Start a transaction.
612 *
613 * During a transaction, changes will be accumulated and made visible
614 * only when the transaction ends (is commited).
615 */
616 void memory_region_transaction_begin(void);
617
618 /**
619 * memory_region_transaction_commit: Commit a transaction and make changes
620 * visible to the guest.
621 */
622 void memory_region_transaction_commit(void);
623
624 void mtree_info(fprintf_function mon_printf, void *f);
625
626 #endif
627
628 #endif