]> git.proxmox.com Git - mirror_qemu.git/blame - memory.h
memory: add memory_region_is_rom()
[mirror_qemu.git] / memory.h
CommitLineData
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
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"
658b2224 25#include "iorange.h"
627a0e90 26#include "ioport.h"
08dafab4 27#include "int128.h"
093bc2cd
AK
28
29typedef struct MemoryRegionOps MemoryRegionOps;
30typedef struct MemoryRegion MemoryRegion;
627a0e90 31typedef struct MemoryRegionPortio MemoryRegionPortio;
74901c3b 32typedef struct MemoryRegionMmio MemoryRegionMmio;
093bc2cd
AK
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
74901c3b
AK
41struct MemoryRegionMmio {
42 CPUReadMemoryFunc *read[3];
43 CPUWriteMemoryFunc *write[3];
44};
45
093bc2cd
AK
46/*
47 * Memory region callbacks
48 */
49struct 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;
897fa7cf
AK
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);
093bc2cd
AK
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;
627a0e90
AK
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;
74901c3b
AK
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;
093bc2cd
AK
106};
107
108typedef struct CoalescedMemoryRange CoalescedMemoryRange;
3e9d69e7 109typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
093bc2cd
AK
110
111struct MemoryRegion {
112 /* All fields are private - violators will be prosecuted */
113 const MemoryRegionOps *ops;
114 void *opaque;
115 MemoryRegion *parent;
08dafab4 116 Int128 size;
093bc2cd
AK
117 target_phys_addr_t addr;
118 target_phys_addr_t offset;
16ef61c9 119 bool backend_registered;
545e92e0 120 void (*destructor)(MemoryRegion *mr);
093bc2cd 121 ram_addr_t ram_addr;
658b2224 122 IORange iorange;
14a3c10a 123 bool terminates;
d0a9b5bc 124 bool readable;
8ea9252a 125 bool ram;
fb1cd6f9 126 bool readonly; /* For RAM regions */
6bba19ba 127 bool enabled;
093bc2cd
AK
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;
5a583347 136 uint8_t dirty_log_mask;
3e9d69e7
AK
137 unsigned ioeventfd_nb;
138 MemoryRegionIoeventfd *ioeventfds;
093bc2cd
AK
139};
140
627a0e90
AK
141struct MemoryRegionPortio {
142 uint32_t offset;
143 uint32_t len;
144 unsigned size;
145 IOPortReadFunc *read;
146 IOPortWriteFunc *write;
147};
148
2dd30228 149#define PORTIO_END_OF_LIST() { }
627a0e90 150
093bc2cd
AK
151/**
152 * memory_region_init: Initialize a memory region
153 *
69ddaf66 154 * The region typically acts as a container for other memory regions. Use
093bc2cd
AK
155 * memory_region_add_subregion() to add subregions.
156 *
157 * @mr: the #MemoryRegion to be initialized
158 * @name: used for debugging; not visible to the user or ABI
159 * @size: size of the region; any subregions beyond this size will be clipped
160 */
161void memory_region_init(MemoryRegion *mr,
162 const char *name,
163 uint64_t size);
164/**
165 * memory_region_init_io: Initialize an I/O memory region.
166 *
69ddaf66 167 * Accesses into the region will cause the callbacks in @ops to be called.
093bc2cd
AK
168 * if @size is nonzero, subregions will be clipped to @size.
169 *
170 * @mr: the #MemoryRegion to be initialized.
171 * @ops: a structure containing read and write callbacks to be used when
172 * I/O is performed on the region.
173 * @opaque: passed to to the read and write callbacks of the @ops structure.
174 * @name: used for debugging; not visible to the user or ABI
175 * @size: size of the region.
176 */
177void memory_region_init_io(MemoryRegion *mr,
178 const MemoryRegionOps *ops,
179 void *opaque,
180 const char *name,
181 uint64_t size);
182
183/**
184 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
69ddaf66 185 * region will modify memory directly.
093bc2cd
AK
186 *
187 * @mr: the #MemoryRegion to be initialized.
188 * @dev: a device associated with the region; may be %NULL.
189 * @name: the name of the region; the pair (@dev, @name) must be globally
190 * unique. The name is part of the save/restore ABI and so cannot be
191 * changed.
192 * @size: size of the region.
193 */
194void memory_region_init_ram(MemoryRegion *mr,
195 DeviceState *dev, /* FIXME: layering violation */
196 const char *name,
197 uint64_t size);
198
199/**
200 * memory_region_init_ram: Initialize RAM memory region from a user-provided.
69ddaf66 201 * pointer. Accesses into the region will modify
093bc2cd
AK
202 * memory directly.
203 *
204 * @mr: the #MemoryRegion to be initialized.
205 * @dev: a device associated with the region; may be %NULL.
206 * @name: the name of the region; the pair (@dev, @name) must be globally
207 * unique. The name is part of the save/restore ABI and so cannot be
208 * changed.
209 * @size: size of the region.
210 * @ptr: memory to be mapped; must contain at least @size bytes.
211 */
212void memory_region_init_ram_ptr(MemoryRegion *mr,
213 DeviceState *dev, /* FIXME: layering violation */
214 const char *name,
215 uint64_t size,
216 void *ptr);
217
218/**
219 * memory_region_init_alias: Initialize a memory region that aliases all or a
220 * part of another memory region.
221 *
222 * @mr: the #MemoryRegion to be initialized.
223 * @name: used for debugging; not visible to the user or ABI
224 * @orig: the region to be referenced; @mr will be equivalent to
225 * @orig between @offset and @offset + @size - 1.
226 * @offset: start of the section in @orig to be referenced.
227 * @size: size of the region.
228 */
229void memory_region_init_alias(MemoryRegion *mr,
230 const char *name,
231 MemoryRegion *orig,
232 target_phys_addr_t offset,
233 uint64_t size);
d0a9b5bc
AK
234
235/**
236 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
237 * handled via callbacks.
238 *
239 * @mr: the #MemoryRegion to be initialized.
240 * @ops: callbacks for write access handling.
241 * @dev: a device associated with the region; may be %NULL.
242 * @name: the name of the region; the pair (@dev, @name) must be globally
243 * unique. The name is part of the save/restore ABI and so cannot be
244 * changed.
245 * @size: size of the region.
246 */
247void memory_region_init_rom_device(MemoryRegion *mr,
248 const MemoryRegionOps *ops,
75f5941c 249 void *opaque,
d0a9b5bc
AK
250 DeviceState *dev, /* FIXME: layering violation */
251 const char *name,
252 uint64_t size);
253
093bc2cd 254/**
69ddaf66 255 * memory_region_destroy: Destroy a memory region and reclaim all resources.
093bc2cd
AK
256 *
257 * @mr: the region to be destroyed. May not currently be a subregion
258 * (see memory_region_add_subregion()) or referenced in an alias
259 * (see memory_region_init_alias()).
260 */
261void memory_region_destroy(MemoryRegion *mr);
262
263/**
264 * memory_region_size: get a memory region's size.
265 *
266 * @mr: the memory region being queried.
267 */
268uint64_t memory_region_size(MemoryRegion *mr);
269
8ea9252a
AK
270/**
271 * memory_region_is_ram: check whether a memory region is random access
272 *
273 * Returns %true is a memory region is random access.
274 *
275 * @mr: the memory region being queried
276 */
277bool memory_region_is_ram(MemoryRegion *mr);
278
ce7923da
AK
279/**
280 * memory_region_is_rom: check whether a memory region is ROM
281 *
282 * Returns %true is a memory region is read-only memory.
283 *
284 * @mr: the memory region being queried
285 */
286bool memory_region_is_rom(MemoryRegion *mr);
287
093bc2cd
AK
288/**
289 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
290 *
291 * Returns a host pointer to a RAM memory region (created with
292 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
293 * care.
294 *
295 * @mr: the memory region being queried.
296 */
297void *memory_region_get_ram_ptr(MemoryRegion *mr);
298
299/**
300 * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
301 * callbacks.
302 *
303 * This function is deprecated and should not be used in new code.
304 */
305void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset);
306
307/**
308 * memory_region_set_log: Turn dirty logging on or off for a region.
309 *
310 * Turns dirty logging on or off for a specified client (display, migration).
311 * Only meaningful for RAM regions.
312 *
313 * @mr: the memory region being updated.
314 * @log: whether dirty logging is to be enabled or disabled.
315 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
316 * %DIRTY_MEMORY_VGA.
317 */
318void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
319
320/**
321 * memory_region_get_dirty: Check whether a page is dirty for a specified
322 * client.
323 *
324 * Checks whether a page has been written to since the last
325 * call to memory_region_reset_dirty() with the same @client. Dirty logging
326 * must be enabled.
327 *
328 * @mr: the memory region being queried.
329 * @addr: the address (relative to the start of the region) being queried.
330 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
331 * %DIRTY_MEMORY_VGA.
332 */
333bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
334 unsigned client);
335
336/**
337 * memory_region_set_dirty: Mark a page as dirty in a memory region.
338 *
339 * Marks a page as dirty, after it has been dirtied outside guest code.
340 *
341 * @mr: the memory region being queried.
342 * @addr: the address (relative to the start of the region) being dirtied.
343 */
344void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr);
345
346/**
347 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
348 * any external TLBs (e.g. kvm)
349 *
350 * Flushes dirty information from accelerators such as kvm and vhost-net
351 * and makes it available to users of the memory API.
352 *
353 * @mr: the region being flushed.
354 */
355void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
356
357/**
358 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
359 * client.
360 *
361 * Marks a range of pages as no longer dirty.
362 *
363 * @mr: the region being updated.
364 * @addr: the start of the subrange being cleaned.
365 * @size: the size of the subrange being cleaned.
366 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
367 * %DIRTY_MEMORY_VGA.
368 */
369void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
370 target_phys_addr_t size, unsigned client);
371
372/**
373 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
374 *
375 * Allows a memory region to be marked as read-only (turning it into a ROM).
376 * only useful on RAM regions.
377 *
378 * @mr: the region being updated.
379 * @readonly: whether rhe region is to be ROM or RAM.
380 */
381void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
382
d0a9b5bc
AK
383/**
384 * memory_region_rom_device_set_readable: enable/disable ROM readability
385 *
386 * Allows a ROM device (initialized with memory_region_init_rom_device() to
387 * to be marked as readable (default) or not readable. When it is readable,
388 * the device is mapped to guest memory. When not readable, reads are
389 * forwarded to the #MemoryRegion.read function.
390 *
391 * @mr: the memory region to be updated
392 * @readable: whether reads are satisified directly (%true) or via callbacks
393 * (%false)
394 */
395void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable);
396
093bc2cd
AK
397/**
398 * memory_region_set_coalescing: Enable memory coalescing for the region.
399 *
400 * Enabled writes to a region to be queued for later processing. MMIO ->write
401 * callbacks may be delayed until a non-coalesced MMIO is issued.
402 * Only useful for IO regions. Roughly similar to write-combining hardware.
403 *
404 * @mr: the memory region to be write coalesced
405 */
406void memory_region_set_coalescing(MemoryRegion *mr);
407
408/**
409 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
410 * a region.
411 *
412 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
413 * Multiple calls can be issued coalesced disjoint ranges.
414 *
415 * @mr: the memory region to be updated.
416 * @offset: the start of the range within the region to be coalesced.
417 * @size: the size of the subrange to be coalesced.
418 */
419void memory_region_add_coalescing(MemoryRegion *mr,
420 target_phys_addr_t offset,
421 uint64_t size);
422
423/**
424 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
425 *
426 * Disables any coalescing caused by memory_region_set_coalescing() or
427 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
428 * hardware.
429 *
430 * @mr: the memory region to be updated.
431 */
432void memory_region_clear_coalescing(MemoryRegion *mr);
433
3e9d69e7
AK
434/**
435 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
436 * is written to a location.
437 *
438 * Marks a word in an IO region (initialized with memory_region_init_io())
439 * as a trigger for an eventfd event. The I/O callback will not be called.
69ddaf66 440 * The caller must be prepared to handle failure (that is, take the required
3e9d69e7
AK
441 * action if the callback _is_ called).
442 *
443 * @mr: the memory region being updated.
444 * @addr: the address within @mr that is to be monitored
445 * @size: the size of the access to trigger the eventfd
446 * @match_data: whether to match against @data, instead of just @addr
447 * @data: the data to match against the guest write
448 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
449 **/
450void memory_region_add_eventfd(MemoryRegion *mr,
451 target_phys_addr_t addr,
452 unsigned size,
453 bool match_data,
454 uint64_t data,
455 int fd);
456
457/**
69ddaf66 458 * memory_region_del_eventfd: Cancel an eventfd.
3e9d69e7 459 *
69ddaf66
ASRJ
460 * Cancels an eventfd trigger requested by a previous
461 * memory_region_add_eventfd() call.
3e9d69e7
AK
462 *
463 * @mr: the memory region being updated.
464 * @addr: the address within @mr that is to be monitored
465 * @size: the size of the access to trigger the eventfd
466 * @match_data: whether to match against @data, instead of just @addr
467 * @data: the data to match against the guest write
468 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
469 */
470void memory_region_del_eventfd(MemoryRegion *mr,
471 target_phys_addr_t addr,
472 unsigned size,
473 bool match_data,
474 uint64_t data,
475 int fd);
093bc2cd 476/**
69ddaf66 477 * memory_region_add_subregion: Add a subregion to a container.
093bc2cd 478 *
69ddaf66 479 * Adds a subregion at @offset. The subregion may not overlap with other
093bc2cd
AK
480 * subregions (except for those explicitly marked as overlapping). A region
481 * may only be added once as a subregion (unless removed with
482 * memory_region_del_subregion()); use memory_region_init_alias() if you
483 * want a region to be a subregion in multiple locations.
484 *
485 * @mr: the region to contain the new subregion; must be a container
486 * initialized with memory_region_init().
487 * @offset: the offset relative to @mr where @subregion is added.
488 * @subregion: the subregion to be added.
489 */
490void memory_region_add_subregion(MemoryRegion *mr,
491 target_phys_addr_t offset,
492 MemoryRegion *subregion);
493/**
69ddaf66 494 * memory_region_add_subregion: Add a subregion to a container, with overlap.
093bc2cd 495 *
69ddaf66 496 * Adds a subregion at @offset. The subregion may overlap with other
093bc2cd
AK
497 * subregions. Conflicts are resolved by having a higher @priority hide a
498 * lower @priority. Subregions without priority are taken as @priority 0.
499 * A region may only be added once as a subregion (unless removed with
500 * memory_region_del_subregion()); use memory_region_init_alias() if you
501 * want a region to be a subregion in multiple locations.
502 *
503 * @mr: the region to contain the new subregion; must be a container
504 * initialized with memory_region_init().
505 * @offset: the offset relative to @mr where @subregion is added.
506 * @subregion: the subregion to be added.
507 * @priority: used for resolving overlaps; highest priority wins.
508 */
509void memory_region_add_subregion_overlap(MemoryRegion *mr,
510 target_phys_addr_t offset,
511 MemoryRegion *subregion,
512 unsigned priority);
513/**
514 * memory_region_del_subregion: Remove a subregion.
515 *
516 * Removes a subregion from its container.
517 *
518 * @mr: the container to be updated.
519 * @subregion: the region being removed; must be a current subregion of @mr.
520 */
521void memory_region_del_subregion(MemoryRegion *mr,
522 MemoryRegion *subregion);
523
6bba19ba
AK
524/*
525 * memory_region_set_enabled: dynamically enable or disable a region
526 *
527 * Enables or disables a memory region. A disabled memory region
528 * ignores all accesses to itself and its subregions. It does not
529 * obscure sibling subregions with lower priority - it simply behaves as
530 * if it was removed from the hierarchy.
531 *
532 * Regions default to being enabled.
533 *
534 * @mr: the region to be updated
535 * @enabled: whether to enable or disable the region
536 */
537void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
538
2282e1af
AK
539/*
540 * memory_region_set_address: dynamically update the address of a region
541 *
542 * Dynamically updates the address of a region, relative to its parent.
543 * May be used on regions are currently part of a memory hierarchy.
544 *
545 * @mr: the region to be updated
546 * @addr: new address, relative to parent region
547 */
548void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr);
549
4703359e
AK
550/*
551 * memory_region_set_alias_offset: dynamically update a memory alias's offset
552 *
553 * Dynamically updates the offset into the target region that an alias points
554 * to, as if the fourth argument to memory_region_init_alias() has changed.
555 *
556 * @mr: the #MemoryRegion to be updated; should be an alias.
557 * @offset: the new offset into the target memory region
558 */
559void memory_region_set_alias_offset(MemoryRegion *mr,
560 target_phys_addr_t offset);
561
69ddaf66
ASRJ
562/**
563 * memory_region_transaction_begin: Start a transaction.
564 *
565 * During a transaction, changes will be accumulated and made visible
566 * only when the transaction ends (is commited).
4ef4db86
AK
567 */
568void memory_region_transaction_begin(void);
69ddaf66
ASRJ
569
570/**
571 * memory_region_transaction_commit: Commit a transaction and make changes
572 * visible to the guest.
4ef4db86
AK
573 */
574void memory_region_transaction_commit(void);
575
314e2987
BS
576void mtree_info(fprintf_function mon_printf, void *f);
577
093bc2cd
AK
578#endif
579
580#endif