blob: 050323f53231c46a276d77fd0bb365594ca9813c [file] [log] [blame]
Avi Kivity093bc2c2011-07-26 14:26:01 +03001/*
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
Paolo Bonzini022c62c2012-12-17 18:19:49 +010019#include "exec/cpu-common.h"
20#include "exec/hwaddr.h"
Peter Maydellcc05c432015-04-26 16:49:23 +010021#include "exec/memattrs.h"
Paolo Bonzini0987d732016-12-21 00:31:36 +080022#include "exec/ramlist.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/queue.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/int128.h"
David Gibson06866572013-05-14 19:13:56 +100025#include "qemu/notify.h"
Peter Crosthwaiteb4fefef2014-06-05 23:15:52 -070026#include "qom/object.h"
Paolo Bonzini374f2982013-05-17 12:37:03 +020027#include "qemu/rcu.h"
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +100028#include "hw/qdev-core.h"
Avi Kivity093bc2c2011-07-26 14:26:01 +030029
Paolo Bonzini07bdaa42016-03-25 12:55:08 +010030#define RAM_ADDR_INVALID (~(ram_addr_t)0)
31
Paolo Bonzini052e87b2013-05-27 10:08:27 +020032#define MAX_PHYS_ADDR_SPACE_BITS 62
33#define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
34
Peter Crosthwaiteb4fefef2014-06-05 23:15:52 -070035#define TYPE_MEMORY_REGION "qemu:memory-region"
36#define MEMORY_REGION(obj) \
37 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
38
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +100039#define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region"
40#define IOMMU_MEMORY_REGION(obj) \
41 OBJECT_CHECK(IOMMUMemoryRegion, (obj), TYPE_IOMMU_MEMORY_REGION)
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +100042#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)
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +100048
Avi Kivity093bc2c2011-07-26 14:26:01 +030049typedef struct MemoryRegionOps MemoryRegionOps;
Avi Kivity74901c32011-07-26 14:26:10 +030050typedef struct MemoryRegionMmio MemoryRegionMmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +030051
Avi Kivity74901c32011-07-26 14:26:10 +030052struct MemoryRegionMmio {
53 CPUReadMemoryFunc *read[3];
54 CPUWriteMemoryFunc *write[3];
55};
56
Avi Kivity30951152012-10-30 13:47:46 +020057typedef struct IOMMUTLBEntry IOMMUTLBEntry;
58
59/* See address_space_translate: bit 0 is read, bit 1 is write. */
60typedef enum {
61 IOMMU_NONE = 0,
62 IOMMU_RO = 1,
63 IOMMU_WO = 2,
64 IOMMU_RW = 3,
65} IOMMUAccessFlags;
66
Peter Xuf06a6962017-04-07 18:59:13 +080067#define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
68
Avi Kivity30951152012-10-30 13:47:46 +020069struct IOMMUTLBEntry {
70 AddressSpace *target_as;
71 hwaddr iova;
72 hwaddr translated_addr;
73 hwaddr addr_mask; /* 0xfff = 4k translation */
74 IOMMUAccessFlags perm;
75};
76
Peter Xucdb30812016-09-23 13:02:26 +080077/*
78 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
79 * register with one or multiple IOMMU Notifier capability bit(s).
80 */
81typedef 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
Peter Xu698feb52017-04-07 18:59:07 +080091struct IOMMUNotifier;
92typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier,
93 IOMMUTLBEntry *data);
94
Peter Xucdb30812016-09-23 13:02:26 +080095struct IOMMUNotifier {
Peter Xu698feb52017-04-07 18:59:07 +080096 IOMMUNotify notify;
Peter Xucdb30812016-09-23 13:02:26 +080097 IOMMUNotifierFlag notifier_flags;
Peter Xu698feb52017-04-07 18:59:07 +080098 /* Notify for address space range start <= addr <= end */
99 hwaddr start;
100 hwaddr end;
Peter Maydellcb1efcf2018-06-15 14:57:16 +0100101 int iommu_idx;
Peter Xucdb30812016-09-23 13:02:26 +0800102 QLIST_ENTRY(IOMMUNotifier) node;
103};
104typedef struct IOMMUNotifier IOMMUNotifier;
105
Peter Xu698feb52017-04-07 18:59:07 +0800106static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
107 IOMMUNotifierFlag flags,
Peter Maydellcb1efcf2018-06-15 14:57:16 +0100108 hwaddr start, hwaddr end,
109 int iommu_idx)
Peter Xu698feb52017-04-07 18:59:07 +0800110{
111 n->notify = fn;
112 n->notifier_flags = flags;
113 n->start = start;
114 n->end = end;
Peter Maydellcb1efcf2018-06-15 14:57:16 +0100115 n->iommu_idx = iommu_idx;
Peter Xu698feb52017-04-07 18:59:07 +0800116}
117
Avi Kivity093bc2c2011-07-26 14:26:01 +0300118/*
119 * Memory region callbacks
120 */
121struct MemoryRegionOps {
122 /* Read from the memory region. @addr is relative to @mr; @size is
123 * in bytes. */
124 uint64_t (*read)(void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200125 hwaddr addr,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300126 unsigned size);
127 /* Write to the memory region. @addr is relative to @mr; @size is
128 * in bytes. */
129 void (*write)(void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200130 hwaddr addr,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300131 uint64_t data,
132 unsigned size);
133
Peter Maydellcc05c432015-04-26 16:49:23 +0100134 MemTxResult (*read_with_attrs)(void *opaque,
135 hwaddr addr,
136 uint64_t *data,
137 unsigned size,
138 MemTxAttrs attrs);
139 MemTxResult (*write_with_attrs)(void *opaque,
140 hwaddr addr,
141 uint64_t data,
142 unsigned size,
143 MemTxAttrs attrs);
KONRAD Fredericc9356742016-10-19 15:06:49 +0200144 /* Instruction execution pre-callback:
145 * @addr is the address of the access relative to the @mr.
146 * @size is the size of the area returned by the callback.
147 * @offset is the location of the pointer inside @mr.
148 *
149 * Returns a pointer to a location which contains guest code.
150 */
151 void *(*request_ptr)(void *opaque, hwaddr addr, unsigned *size,
152 unsigned *offset);
Peter Maydellcc05c432015-04-26 16:49:23 +0100153
Avi Kivity093bc2c2011-07-26 14:26:01 +0300154 enum device_endian endianness;
155 /* Guest-visible constraints: */
156 struct {
157 /* If nonzero, specify bounds on access sizes beyond which a machine
158 * check is thrown.
159 */
160 unsigned min_access_size;
161 unsigned max_access_size;
162 /* If true, unaligned accesses are supported. Otherwise unaligned
163 * accesses throw machine checks.
164 */
165 bool unaligned;
Avi Kivity897fa7c2011-11-13 13:05:27 +0200166 /*
167 * If present, and returns #false, the transaction is not accepted
168 * by the device (and results in machine dependent behaviour such
169 * as a machine check exception).
170 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200171 bool (*accepts)(void *opaque, hwaddr addr,
Peter Maydell8372d382018-05-31 14:50:52 +0100172 unsigned size, bool is_write,
173 MemTxAttrs attrs);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300174 } valid;
175 /* Internal implementation constraints: */
176 struct {
177 /* If nonzero, specifies the minimum size implemented. Smaller sizes
178 * will be rounded upwards and a partial result will be returned.
179 */
180 unsigned min_access_size;
181 /* If nonzero, specifies the maximum size implemented. Larger sizes
182 * will be done as a series of accesses with smaller sizes.
183 */
184 unsigned max_access_size;
185 /* If true, unaligned accesses are supported. Otherwise all accesses
186 * are converted to (possibly multiple) naturally aligned accesses.
187 */
Fam Zhengedc1ba72014-05-05 15:53:41 +0800188 bool unaligned;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300189 } impl;
Avi Kivity627a0e92011-07-26 14:26:09 +0300190
Avi Kivity74901c32011-07-26 14:26:10 +0300191 /* If .read and .write are not present, old_mmio may be used for
192 * backwards compatibility with old mmio registration
193 */
194 const MemoryRegionMmio old_mmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300195};
196
Alexey Kardashevskiyf1334de2018-02-06 11:08:24 -0700197enum IOMMUMemoryRegionAttr {
198 IOMMU_ATTR_SPAPR_TCE_FD
199};
200
Peter Maydell2ce931d2018-05-31 14:50:52 +0100201/**
202 * IOMMUMemoryRegionClass:
203 *
204 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
205 * and provide an implementation of at least the @translate method here
206 * to handle requests to the memory region. Other methods are optional.
207 *
208 * The IOMMU implementation must use the IOMMU notifier infrastructure
209 * to report whenever mappings are changed, by calling
210 * memory_region_notify_iommu() (or, if necessary, by calling
211 * memory_region_notify_one() for each registered notifier).
Peter Maydell21f40202018-06-15 14:57:15 +0100212 *
213 * Conceptually an IOMMU provides a mapping from input address
214 * to an output TLB entry. If the IOMMU is aware of memory transaction
215 * attributes and the output TLB entry depends on the transaction
216 * attributes, we represent this using IOMMU indexes. Each index
217 * selects a particular translation table that the IOMMU has:
218 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
219 * @translate takes an input address and an IOMMU index
220 * and the mapping returned can only depend on the input address and the
221 * IOMMU index.
222 *
223 * Most IOMMUs don't care about the transaction attributes and support
224 * only a single IOMMU index. A more complex IOMMU might have one index
225 * for secure transactions and one for non-secure transactions.
Peter Maydell2ce931d2018-05-31 14:50:52 +0100226 */
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000227typedef struct IOMMUMemoryRegionClass {
228 /* private */
229 struct DeviceClass parent_class;
Avi Kivity30951152012-10-30 13:47:46 +0200230
Peter Xubf55b7a2017-05-19 11:19:40 +0800231 /*
Peter Maydell2ce931d2018-05-31 14:50:52 +0100232 * Return a TLB entry that contains a given address.
233 *
234 * The IOMMUAccessFlags indicated via @flag are optional and may
235 * be specified as IOMMU_NONE to indicate that the caller needs
236 * the full translation information for both reads and writes. If
237 * the access flags are specified then the IOMMU implementation
238 * may use this as an optimization, to stop doing a page table
239 * walk as soon as it knows that the requested permissions are not
240 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
241 * full page table walk and report the permissions in the returned
242 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
243 * return different mappings for reads and writes.)
244 *
245 * The returned information remains valid while the caller is
246 * holding the big QEMU lock or is inside an RCU critical section;
247 * if the caller wishes to cache the mapping beyond that it must
248 * register an IOMMU notifier so it can invalidate its cached
249 * information when the IOMMU mapping changes.
250 *
251 * @iommu: the IOMMUMemoryRegion
252 * @hwaddr: address to be translated within the memory region
253 * @flag: requested access permissions
Peter Maydell2c91bcf2018-06-15 14:57:16 +0100254 * @iommu_idx: IOMMU index for the translation
Peter Xubf55b7a2017-05-19 11:19:40 +0800255 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000256 IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
Peter Maydell2c91bcf2018-06-15 14:57:16 +0100257 IOMMUAccessFlags flag, int iommu_idx);
Peter Maydell2ce931d2018-05-31 14:50:52 +0100258 /* Returns minimum supported page size in bytes.
259 * If this method is not provided then the minimum is assumed to
260 * be TARGET_PAGE_SIZE.
261 *
262 * @iommu: the IOMMUMemoryRegion
263 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000264 uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
Peter Maydell2ce931d2018-05-31 14:50:52 +0100265 /* Called when IOMMU Notifier flag changes (ie when the set of
266 * events which IOMMU users are requesting notification for changes).
267 * Optional method -- need not be provided if the IOMMU does not
268 * need to know exactly which events must be notified.
269 *
270 * @iommu: the IOMMUMemoryRegion
271 * @old_flags: events which previously needed to be notified
272 * @new_flags: events which now need to be notified
273 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000274 void (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
Peter Xu5bf3d312016-09-23 13:02:27 +0800275 IOMMUNotifierFlag old_flags,
276 IOMMUNotifierFlag new_flags);
Peter Maydell2ce931d2018-05-31 14:50:52 +0100277 /* Called to handle memory_region_iommu_replay().
278 *
279 * The default implementation of memory_region_iommu_replay() is to
280 * call the IOMMU translate method for every page in the address space
281 * with flag == IOMMU_NONE and then call the notifier if translate
282 * returns a valid mapping. If this method is implemented then it
283 * overrides the default behaviour, and must provide the full semantics
284 * of memory_region_iommu_replay(), by calling @notifier for every
285 * translation present in the IOMMU.
286 *
287 * Optional method -- an IOMMU only needs to provide this method
288 * if the default is inefficient or produces undesirable side effects.
289 *
290 * Note: this is not related to record-and-replay functionality.
291 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000292 void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
Alexey Kardashevskiyf1334de2018-02-06 11:08:24 -0700293
Peter Maydell2ce931d2018-05-31 14:50:52 +0100294 /* Get IOMMU misc attributes. This is an optional method that
295 * can be used to allow users of the IOMMU to get implementation-specific
296 * information. The IOMMU implements this method to handle calls
297 * by IOMMU users to memory_region_iommu_get_attr() by filling in
298 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
299 * the IOMMU supports. If the method is unimplemented then
300 * memory_region_iommu_get_attr() will always return -EINVAL.
301 *
302 * @iommu: the IOMMUMemoryRegion
303 * @attr: attribute being queried
304 * @data: memory to fill in with the attribute data
305 *
306 * Returns 0 on success, or a negative errno; in particular
307 * returns -EINVAL for unrecognized or unimplemented attribute types.
308 */
309 int (*get_attr)(IOMMUMemoryRegion *iommu, enum IOMMUMemoryRegionAttr attr,
Alexey Kardashevskiyf1334de2018-02-06 11:08:24 -0700310 void *data);
Peter Maydell21f40202018-06-15 14:57:15 +0100311
312 /* Return the IOMMU index to use for a given set of transaction attributes.
313 *
314 * Optional method: if an IOMMU only supports a single IOMMU index then
315 * the default implementation of memory_region_iommu_attrs_to_index()
316 * will return 0.
317 *
318 * The indexes supported by an IOMMU must be contiguous, starting at 0.
319 *
320 * @iommu: the IOMMUMemoryRegion
321 * @attrs: memory transaction attributes
322 */
323 int (*attrs_to_index)(IOMMUMemoryRegion *iommu, MemTxAttrs attrs);
324
325 /* Return the number of IOMMU indexes this IOMMU supports.
326 *
327 * Optional method: if this method is not provided, then
328 * memory_region_iommu_num_indexes() will return 1, indicating that
329 * only a single IOMMU index is supported.
330 *
331 * @iommu: the IOMMUMemoryRegion
332 */
333 int (*num_indexes)(IOMMUMemoryRegion *iommu);
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000334} IOMMUMemoryRegionClass;
Avi Kivity30951152012-10-30 13:47:46 +0200335
Avi Kivity093bc2c2011-07-26 14:26:01 +0300336typedef struct CoalescedMemoryRange CoalescedMemoryRange;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300337typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300338
339struct MemoryRegion {
Peter Crosthwaiteb4fefef2014-06-05 23:15:52 -0700340 Object parent_obj;
Paolo Bonzinia6768542015-12-09 11:40:14 +0100341
Avi Kivity093bc2c2011-07-26 14:26:01 +0300342 /* All fields are private - violators will be prosecuted */
Paolo Bonzinia6768542015-12-09 11:40:14 +0100343
344 /* The following fields should fit in a cache line */
345 bool romd_mode;
346 bool ram;
347 bool subpage;
348 bool readonly; /* For RAM regions */
349 bool rom_device;
350 bool flush_coalesced_mmio;
351 bool global_locking;
352 uint8_t dirty_log_mask;
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000353 bool is_iommu;
Gonglei58eaa212016-02-22 16:34:55 +0800354 RAMBlock *ram_block;
Paolo Bonzini612263c2015-12-09 11:44:25 +0100355 Object *owner;
Paolo Bonzinia6768542015-12-09 11:40:14 +0100356
357 const MemoryRegionOps *ops;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300358 void *opaque;
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +0200359 MemoryRegion *container;
Avi Kivity08dafab2011-10-16 13:19:17 +0200360 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200361 hwaddr addr;
Avi Kivity545e92e2011-08-08 19:58:48 +0300362 void (*destructor)(MemoryRegion *mr);
Igor Mammedova2b257d2014-10-31 16:38:37 +0000363 uint64_t align;
Avi Kivity14a3c102011-07-26 14:26:06 +0300364 bool terminates;
Alex Williamson21e00fa2016-10-31 09:53:03 -0600365 bool ram_device;
Avi Kivity6bba19b2011-09-14 11:54:58 +0300366 bool enabled;
Jan Kiszka1660e722011-10-23 16:01:19 +0200367 bool warning_printed; /* For reservations */
Paolo Bonzinideb809e2015-07-14 13:56:53 +0200368 uint8_t vga_logging_count;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300369 MemoryRegion *alias;
Avi Kivitya8170e52012-10-23 12:30:10 +0200370 hwaddr alias_offset;
Peter Crosthwaited33382d2014-06-05 23:17:01 -0700371 int32_t priority;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300372 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
373 QTAILQ_ENTRY(MemoryRegion) subregions_link;
374 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
Peter Maydell302fa282014-08-19 20:05:46 +0100375 const char *name;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300376 unsigned ioeventfd_nb;
377 MemoryRegionIoeventfd *ioeventfds;
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000378};
379
380struct IOMMUMemoryRegion {
381 MemoryRegion parent_obj;
382
Peter Xucdb30812016-09-23 13:02:26 +0800383 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
Peter Xu5bf3d312016-09-23 13:02:27 +0800384 IOMMUNotifierFlag iommu_notify_flags;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300385};
386
Peter Xu512fa402017-04-07 18:59:08 +0800387#define IOMMU_NOTIFIER_FOREACH(n, mr) \
388 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
389
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200390/**
391 * MemoryListener: callbacks structure for updates to the physical memory map
392 *
393 * Allows a component to adjust to changes in the guest-visible memory map.
394 * Use with memory_listener_register() and memory_listener_unregister().
395 */
396struct MemoryListener {
397 void (*begin)(MemoryListener *listener);
398 void (*commit)(MemoryListener *listener);
399 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
400 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
401 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
Paolo Bonzinib2dfd712015-04-25 14:38:30 +0200402 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
403 int old, int new);
404 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
405 int old, int new);
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200406 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
407 void (*log_global_start)(MemoryListener *listener);
408 void (*log_global_stop)(MemoryListener *listener);
409 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
410 bool match_data, uint64_t data, EventNotifier *e);
411 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
412 bool match_data, uint64_t data, EventNotifier *e);
413 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
414 hwaddr addr, hwaddr len);
415 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
416 hwaddr addr, hwaddr len);
417 /* Lower = earlier (during add), later (during del) */
418 unsigned priority;
Paolo Bonzinid45fa782016-09-22 16:11:54 +0200419 AddressSpace *address_space;
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200420 QTAILQ_ENTRY(MemoryListener) link;
Paolo Bonzini9a546352016-09-22 16:23:06 +0200421 QTAILQ_ENTRY(MemoryListener) link_as;
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200422};
423
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200424/**
425 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
426 */
427struct AddressSpace {
428 /* All fields are private. */
Paolo Bonzini374f2982013-05-17 12:37:03 +0200429 struct rcu_head rcu;
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +0000430 char *name;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200431 MemoryRegion *root;
Paolo Bonzini374f2982013-05-17 12:37:03 +0200432
433 /* Accessed via RCU. */
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200434 struct FlatView *current_map;
Paolo Bonzini374f2982013-05-17 12:37:03 +0200435
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200436 int ioeventfd_nb;
437 struct MemoryRegionIoeventfd *ioeventfds;
Paolo Bonzini9a546352016-09-22 16:23:06 +0200438 QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners;
Avi Kivity0d673e32012-10-02 15:28:50 +0200439 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200440};
441
Paolo Bonzini785a5072018-03-05 00:31:20 +0100442typedef struct AddressSpaceDispatch AddressSpaceDispatch;
443typedef struct FlatRange FlatRange;
444
445/* Flattened global view of current active memory hierarchy. Kept in sorted
446 * order.
447 */
448struct FlatView {
449 struct rcu_head rcu;
450 unsigned ref;
451 FlatRange *ranges;
452 unsigned nr;
453 unsigned nr_allocated;
454 struct AddressSpaceDispatch *dispatch;
455 MemoryRegion *root;
456};
457
458static inline FlatView *address_space_to_flatview(AddressSpace *as)
459{
460 return atomic_rcu_read(&as->current_map);
461}
462
Alexey Kardashevskiy16620682017-09-21 18:50:58 +1000463
Avi Kivitye2177952011-12-08 15:00:18 +0200464/**
465 * MemoryRegionSection: describes a fragment of a #MemoryRegion
466 *
467 * @mr: the region, or %NULL if empty
Jay Zhou57914ec2018-01-04 13:29:48 +0800468 * @fv: the flat view of the address space the region is mapped in
Avi Kivitye2177952011-12-08 15:00:18 +0200469 * @offset_within_region: the beginning of the section, relative to @mr's start
470 * @size: the size of the section; will not exceed @mr's boundaries
471 * @offset_within_address_space: the address of the first byte of the section
472 * relative to the region's address space
Avi Kivity7a8499e2012-02-08 17:01:23 +0200473 * @readonly: writes to this section are ignored
Avi Kivitye2177952011-12-08 15:00:18 +0200474 */
475struct MemoryRegionSection {
476 MemoryRegion *mr;
Alexey Kardashevskiy16620682017-09-21 18:50:58 +1000477 FlatView *fv;
Avi Kivitya8170e52012-10-23 12:30:10 +0200478 hwaddr offset_within_region;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200479 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200480 hwaddr offset_within_address_space;
Avi Kivity7a8499e2012-02-08 17:01:23 +0200481 bool readonly;
Avi Kivitye2177952011-12-08 15:00:18 +0200482};
483
Avi Kivity093bc2c2011-07-26 14:26:01 +0300484/**
485 * memory_region_init: Initialize a memory region
486 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300487 * The region typically acts as a container for other memory regions. Use
Avi Kivity093bc2c2011-07-26 14:26:01 +0300488 * memory_region_add_subregion() to add subregions.
489 *
490 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400491 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300492 * @name: used for debugging; not visible to the user or ABI
493 * @size: size of the region; any subregions beyond this size will be clipped
494 */
495void memory_region_init(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400496 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300497 const char *name,
498 uint64_t size);
Paolo Bonzini46637be2013-05-07 09:06:00 +0200499
500/**
501 * memory_region_ref: Add 1 to a memory region's reference count
502 *
503 * Whenever memory regions are accessed outside the BQL, they need to be
504 * preserved against hot-unplug. MemoryRegions actually do not have their
505 * own reference count; they piggyback on a QOM object, their "owner".
506 * This function adds a reference to the owner.
507 *
508 * All MemoryRegions must have an owner if they can disappear, even if the
509 * device they belong to operates exclusively under the BQL. This is because
510 * the region could be returned at any time by memory_region_find, and this
511 * is usually under guest control.
512 *
513 * @mr: the #MemoryRegion
514 */
515void memory_region_ref(MemoryRegion *mr);
516
517/**
518 * memory_region_unref: Remove 1 to a memory region's reference count
519 *
520 * Whenever memory regions are accessed outside the BQL, they need to be
521 * preserved against hot-unplug. MemoryRegions actually do not have their
522 * own reference count; they piggyback on a QOM object, their "owner".
523 * This function removes a reference to the owner and possibly destroys it.
524 *
525 * @mr: the #MemoryRegion
526 */
527void memory_region_unref(MemoryRegion *mr);
528
Avi Kivity093bc2c2011-07-26 14:26:01 +0300529/**
530 * memory_region_init_io: Initialize an I/O memory region.
531 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300532 * Accesses into the region will cause the callbacks in @ops to be called.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300533 * if @size is nonzero, subregions will be clipped to @size.
534 *
535 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400536 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300537 * @ops: a structure containing read and write callbacks to be used when
538 * I/O is performed on the region.
Daniel P. Berrangeb6af0972015-08-26 12:17:13 +0100539 * @opaque: passed to the read and write callbacks of the @ops structure.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300540 * @name: used for debugging; not visible to the user or ABI
541 * @size: size of the region.
542 */
543void memory_region_init_io(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400544 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300545 const MemoryRegionOps *ops,
546 void *opaque,
547 const char *name,
548 uint64_t size);
549
550/**
Peter Maydell1cfe48c2017-07-07 15:42:49 +0100551 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
552 * into the region will modify memory
553 * directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300554 *
555 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400556 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000557 * @name: Region name, becomes part of RAMBlock name used in migration stream
558 * must be unique within any device
Avi Kivity093bc2c2011-07-26 14:26:01 +0300559 * @size: size of the region.
Hu Tao49946532014-09-09 13:27:55 +0800560 * @errp: pointer to Error*, to store an error if it happens.
Peter Maydella5c02342017-07-07 15:42:48 +0100561 *
562 * Note that this function does not do anything to cause the data in the
563 * RAM memory region to be migrated; that is the responsibility of the caller.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300564 */
Peter Maydell1cfe48c2017-07-07 15:42:49 +0100565void memory_region_init_ram_nomigrate(MemoryRegion *mr,
566 struct Object *owner,
567 const char *name,
568 uint64_t size,
569 Error **errp);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300570
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200571/**
Marcel Apfelbaum06329cc2017-12-13 16:37:37 +0200572 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
573 * Accesses into the region will
574 * modify memory directly.
575 *
576 * @mr: the #MemoryRegion to be initialized.
577 * @owner: the object that tracks the region's reference count
578 * @name: Region name, becomes part of RAMBlock name used in migration stream
579 * must be unique within any device
580 * @size: size of the region.
581 * @share: allow remapping RAM to different addresses
582 * @errp: pointer to Error*, to store an error if it happens.
583 *
584 * Note that this function is similar to memory_region_init_ram_nomigrate.
585 * The only difference is part of the RAM region can be remapped.
586 */
587void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
588 struct Object *owner,
589 const char *name,
590 uint64_t size,
591 bool share,
592 Error **errp);
593
594/**
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200595 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
596 * RAM. Accesses into the region will
597 * modify memory directly. Only an initial
598 * portion of this RAM is actually used.
599 * The used size can change across reboots.
600 *
601 * @mr: the #MemoryRegion to be initialized.
602 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000603 * @name: Region name, becomes part of RAMBlock name used in migration stream
604 * must be unique within any device
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200605 * @size: used size of the region.
606 * @max_size: max size of the region.
607 * @resized: callback to notify owner about used size change.
608 * @errp: pointer to Error*, to store an error if it happens.
Peter Maydella5c02342017-07-07 15:42:48 +0100609 *
610 * Note that this function does not do anything to cause the data in the
611 * RAM memory region to be migrated; that is the responsibility of the caller.
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200612 */
613void memory_region_init_resizeable_ram(MemoryRegion *mr,
614 struct Object *owner,
615 const char *name,
616 uint64_t size,
617 uint64_t max_size,
618 void (*resized)(const char*,
619 uint64_t length,
620 void *host),
621 Error **errp);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800622#ifdef __linux__
623/**
624 * memory_region_init_ram_from_file: Initialize RAM memory region with a
625 * mmap-ed backend.
626 *
627 * @mr: the #MemoryRegion to be initialized.
628 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000629 * @name: Region name, becomes part of RAMBlock name used in migration stream
630 * must be unique within any device
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800631 * @size: size of the region.
Haozhong Zhang98376842017-12-11 15:28:04 +0800632 * @align: alignment of the region base address; if 0, the default alignment
633 * (getpagesize()) will be used.
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800634 * @share: %true if memory must be mmaped with the MAP_SHARED flag
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800635 * @path: the path in which to allocate the RAM.
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800636 * @errp: pointer to Error*, to store an error if it happens.
Peter Maydella5c02342017-07-07 15:42:48 +0100637 *
638 * Note that this function does not do anything to cause the data in the
639 * RAM memory region to be migrated; that is the responsibility of the caller.
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800640 */
641void memory_region_init_ram_from_file(MemoryRegion *mr,
642 struct Object *owner,
643 const char *name,
644 uint64_t size,
Haozhong Zhang98376842017-12-11 15:28:04 +0800645 uint64_t align,
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800646 bool share,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800647 const char *path,
648 Error **errp);
Marc-André Lureaufea617c2017-06-02 18:12:24 +0400649
650/**
651 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
652 * mmap-ed backend.
653 *
654 * @mr: the #MemoryRegion to be initialized.
655 * @owner: the object that tracks the region's reference count
656 * @name: the name of the region.
657 * @size: size of the region.
658 * @share: %true if memory must be mmaped with the MAP_SHARED flag
659 * @fd: the fd to mmap.
660 * @errp: pointer to Error*, to store an error if it happens.
Peter Maydella5c02342017-07-07 15:42:48 +0100661 *
662 * Note that this function does not do anything to cause the data in the
663 * RAM memory region to be migrated; that is the responsibility of the caller.
Marc-André Lureaufea617c2017-06-02 18:12:24 +0400664 */
665void memory_region_init_ram_from_fd(MemoryRegion *mr,
666 struct Object *owner,
667 const char *name,
668 uint64_t size,
669 bool share,
670 int fd,
671 Error **errp);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800672#endif
673
Avi Kivity093bc2c2011-07-26 14:26:01 +0300674/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +0200675 * memory_region_init_ram_ptr: Initialize RAM memory region from a
676 * user-provided pointer. Accesses into the
677 * region will modify memory directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300678 *
679 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400680 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000681 * @name: Region name, becomes part of RAMBlock name used in migration stream
682 * must be unique within any device
Avi Kivity093bc2c2011-07-26 14:26:01 +0300683 * @size: size of the region.
684 * @ptr: memory to be mapped; must contain at least @size bytes.
Peter Maydella5c02342017-07-07 15:42:48 +0100685 *
686 * Note that this function does not do anything to cause the data in the
687 * RAM memory region to be migrated; that is the responsibility of the caller.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300688 */
689void memory_region_init_ram_ptr(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400690 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300691 const char *name,
692 uint64_t size,
693 void *ptr);
694
695/**
Alex Williamson21e00fa2016-10-31 09:53:03 -0600696 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
697 * a user-provided pointer.
698 *
699 * A RAM device represents a mapping to a physical device, such as to a PCI
700 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
701 * into the VM address space and access to the region will modify memory
702 * directly. However, the memory region should not be included in a memory
703 * dump (device may not be enabled/mapped at the time of the dump), and
704 * operations incompatible with manipulating MMIO should be avoided. Replaces
705 * skip_dump flag.
706 *
707 * @mr: the #MemoryRegion to be initialized.
708 * @owner: the object that tracks the region's reference count
709 * @name: the name of the region.
710 * @size: size of the region.
711 * @ptr: memory to be mapped; must contain at least @size bytes.
Peter Maydella5c02342017-07-07 15:42:48 +0100712 *
713 * Note that this function does not do anything to cause the data in the
714 * RAM memory region to be migrated; that is the responsibility of the caller.
715 * (For RAM device memory regions, migrating the contents rarely makes sense.)
Alex Williamson21e00fa2016-10-31 09:53:03 -0600716 */
717void memory_region_init_ram_device_ptr(MemoryRegion *mr,
718 struct Object *owner,
719 const char *name,
720 uint64_t size,
721 void *ptr);
722
723/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300724 * memory_region_init_alias: Initialize a memory region that aliases all or a
725 * part of another memory region.
726 *
727 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400728 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300729 * @name: used for debugging; not visible to the user or ABI
730 * @orig: the region to be referenced; @mr will be equivalent to
731 * @orig between @offset and @offset + @size - 1.
732 * @offset: start of the section in @orig to be referenced.
733 * @size: size of the region.
734 */
735void memory_region_init_alias(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400736 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300737 const char *name,
738 MemoryRegion *orig,
Avi Kivitya8170e52012-10-23 12:30:10 +0200739 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300740 uint64_t size);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300741
742/**
Peter Maydellb59821a2017-07-07 15:42:50 +0100743 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
Peter Maydella1777f72016-07-04 13:06:35 +0100744 *
Peter Maydellb59821a2017-07-07 15:42:50 +0100745 * This has the same effect as calling memory_region_init_ram_nomigrate()
Peter Maydella1777f72016-07-04 13:06:35 +0100746 * and then marking the resulting region read-only with
747 * memory_region_set_readonly().
748 *
Peter Maydellb59821a2017-07-07 15:42:50 +0100749 * Note that this function does not do anything to cause the data in the
750 * RAM side of the memory region to be migrated; that is the responsibility
751 * of the caller.
752 *
Peter Maydella1777f72016-07-04 13:06:35 +0100753 * @mr: the #MemoryRegion to be initialized.
754 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000755 * @name: Region name, becomes part of RAMBlock name used in migration stream
756 * must be unique within any device
Peter Maydella1777f72016-07-04 13:06:35 +0100757 * @size: size of the region.
758 * @errp: pointer to Error*, to store an error if it happens.
759 */
Peter Maydellb59821a2017-07-07 15:42:50 +0100760void memory_region_init_rom_nomigrate(MemoryRegion *mr,
761 struct Object *owner,
762 const char *name,
763 uint64_t size,
764 Error **errp);
Peter Maydella1777f72016-07-04 13:06:35 +0100765
766/**
Peter Maydellb59821a2017-07-07 15:42:50 +0100767 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
768 * Writes are handled via callbacks.
769 *
770 * Note that this function does not do anything to cause the data in the
771 * RAM side of the memory region to be migrated; that is the responsibility
772 * of the caller.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300773 *
774 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400775 * @owner: the object that tracks the region's reference count
Peter Maydell39e0b032016-07-04 13:06:35 +0100776 * @ops: callbacks for write access handling (must not be NULL).
Jay Zhou57914ec2018-01-04 13:29:48 +0800777 * @opaque: passed to the read and write callbacks of the @ops structure.
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000778 * @name: Region name, becomes part of RAMBlock name used in migration stream
779 * must be unique within any device
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300780 * @size: size of the region.
Hu Tao33e0eb52014-09-09 13:27:57 +0800781 * @errp: pointer to Error*, to store an error if it happens.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300782 */
Peter Maydellb59821a2017-07-07 15:42:50 +0100783void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
784 struct Object *owner,
785 const MemoryRegionOps *ops,
786 void *opaque,
787 const char *name,
788 uint64_t size,
789 Error **errp);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300790
Avi Kivity093bc2c2011-07-26 14:26:01 +0300791/**
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000792 * memory_region_init_iommu: Initialize a memory region of a custom type
793 * that translates addresses
Avi Kivity30951152012-10-30 13:47:46 +0200794 *
795 * An IOMMU region translates addresses and forwards accesses to a target
796 * memory region.
797 *
Peter Maydell2ce931d2018-05-31 14:50:52 +0100798 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
799 * @_iommu_mr should be a pointer to enough memory for an instance of
800 * that subclass, @instance_size is the size of that subclass, and
801 * @mrtypename is its name. This function will initialize @_iommu_mr as an
802 * instance of the subclass, and its methods will then be called to handle
803 * accesses to the memory region. See the documentation of
804 * #IOMMUMemoryRegionClass for further details.
805 *
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000806 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
807 * @instance_size: the IOMMUMemoryRegion subclass instance size
Jay Zhou57914ec2018-01-04 13:29:48 +0800808 * @mrtypename: the type name of the #IOMMUMemoryRegion
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400809 * @owner: the object that tracks the region's reference count
Avi Kivity30951152012-10-30 13:47:46 +0200810 * @name: used for debugging; not visible to the user or ABI
811 * @size: size of the region.
812 */
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000813void memory_region_init_iommu(void *_iommu_mr,
814 size_t instance_size,
815 const char *mrtypename,
816 Object *owner,
Avi Kivity30951152012-10-30 13:47:46 +0200817 const char *name,
818 uint64_t size);
819
Jan Kiszka1660e722011-10-23 16:01:19 +0200820/**
Peter Maydellb08199c2017-07-07 15:42:51 +0100821 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
822 * region will modify memory directly.
823 *
824 * @mr: the #MemoryRegion to be initialized
825 * @owner: the object that tracks the region's reference count (must be
826 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
827 * @name: name of the memory region
828 * @size: size of the region in bytes
829 * @errp: pointer to Error*, to store an error if it happens.
830 *
831 * This function allocates RAM for a board model or device, and
832 * arranges for it to be migrated (by calling vmstate_register_ram()
833 * if @owner is a DeviceState, or vmstate_register_ram_global() if
834 * @owner is NULL).
835 *
836 * TODO: Currently we restrict @owner to being either NULL (for
837 * global RAM regions with no owner) or devices, so that we can
838 * give the RAM block a unique name for migration purposes.
839 * We should lift this restriction and allow arbitrary Objects.
840 * If you pass a non-NULL non-device @owner then we will assert.
841 */
842void memory_region_init_ram(MemoryRegion *mr,
843 struct Object *owner,
844 const char *name,
845 uint64_t size,
846 Error **errp);
847
848/**
849 * memory_region_init_rom: Initialize a ROM memory region.
850 *
851 * This has the same effect as calling memory_region_init_ram()
852 * and then marking the resulting region read-only with
853 * memory_region_set_readonly(). This includes arranging for the
854 * contents to be migrated.
855 *
856 * TODO: Currently we restrict @owner to being either NULL (for
857 * global RAM regions with no owner) or devices, so that we can
858 * give the RAM block a unique name for migration purposes.
859 * We should lift this restriction and allow arbitrary Objects.
860 * If you pass a non-NULL non-device @owner then we will assert.
861 *
862 * @mr: the #MemoryRegion to be initialized.
863 * @owner: the object that tracks the region's reference count
864 * @name: Region name, becomes part of RAMBlock name used in migration stream
865 * must be unique within any device
866 * @size: size of the region.
867 * @errp: pointer to Error*, to store an error if it happens.
868 */
869void memory_region_init_rom(MemoryRegion *mr,
870 struct Object *owner,
871 const char *name,
872 uint64_t size,
873 Error **errp);
874
875/**
876 * memory_region_init_rom_device: Initialize a ROM memory region.
877 * Writes are handled via callbacks.
878 *
879 * This function initializes a memory region backed by RAM for reads
880 * and callbacks for writes, and arranges for the RAM backing to
881 * be migrated (by calling vmstate_register_ram()
882 * if @owner is a DeviceState, or vmstate_register_ram_global() if
883 * @owner is NULL).
884 *
885 * TODO: Currently we restrict @owner to being either NULL (for
886 * global RAM regions with no owner) or devices, so that we can
887 * give the RAM block a unique name for migration purposes.
888 * We should lift this restriction and allow arbitrary Objects.
889 * If you pass a non-NULL non-device @owner then we will assert.
890 *
891 * @mr: the #MemoryRegion to be initialized.
892 * @owner: the object that tracks the region's reference count
893 * @ops: callbacks for write access handling (must not be NULL).
894 * @name: Region name, becomes part of RAMBlock name used in migration stream
895 * must be unique within any device
896 * @size: size of the region.
897 * @errp: pointer to Error*, to store an error if it happens.
898 */
899void memory_region_init_rom_device(MemoryRegion *mr,
900 struct Object *owner,
901 const MemoryRegionOps *ops,
902 void *opaque,
903 const char *name,
904 uint64_t size,
905 Error **errp);
906
907
908/**
Paolo Bonzini803c0812013-05-07 06:59:09 +0200909 * memory_region_owner: get a memory region's owner.
910 *
911 * @mr: the memory region being queried.
912 */
913struct Object *memory_region_owner(MemoryRegion *mr);
914
915/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300916 * memory_region_size: get a memory region's size.
917 *
918 * @mr: the memory region being queried.
919 */
920uint64_t memory_region_size(MemoryRegion *mr);
921
922/**
Avi Kivity8ea92522011-12-08 15:58:43 +0200923 * memory_region_is_ram: check whether a memory region is random access
924 *
925 * Returns %true is a memory region is random access.
926 *
927 * @mr: the memory region being queried
928 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100929static inline bool memory_region_is_ram(MemoryRegion *mr)
930{
931 return mr->ram;
932}
Avi Kivity8ea92522011-12-08 15:58:43 +0200933
934/**
Alex Williamson21e00fa2016-10-31 09:53:03 -0600935 * memory_region_is_ram_device: check whether a memory region is a ram device
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530936 *
Alex Williamson21e00fa2016-10-31 09:53:03 -0600937 * Returns %true is a memory region is a device backed ram region
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530938 *
939 * @mr: the memory region being queried
940 */
Alex Williamson21e00fa2016-10-31 09:53:03 -0600941bool memory_region_is_ram_device(MemoryRegion *mr);
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530942
943/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200944 * memory_region_is_romd: check whether a memory region is in ROMD mode
Blue Swirlfd062572012-04-09 17:38:52 +0000945 *
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200946 * Returns %true if a memory region is a ROM device and currently set to allow
Blue Swirlfd062572012-04-09 17:38:52 +0000947 * direct reads.
948 *
949 * @mr: the memory region being queried
950 */
951static inline bool memory_region_is_romd(MemoryRegion *mr)
952{
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200953 return mr->rom_device && mr->romd_mode;
Blue Swirlfd062572012-04-09 17:38:52 +0000954}
955
956/**
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000957 * memory_region_get_iommu: check whether a memory region is an iommu
Avi Kivity30951152012-10-30 13:47:46 +0200958 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000959 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
960 * otherwise NULL.
Avi Kivity30951152012-10-30 13:47:46 +0200961 *
962 * @mr: the memory region being queried
963 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000964static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100965{
Jason Wang12d37882016-12-30 18:09:18 +0800966 if (mr->alias) {
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000967 return memory_region_get_iommu(mr->alias);
Jason Wang12d37882016-12-30 18:09:18 +0800968 }
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000969 if (mr->is_iommu) {
970 return (IOMMUMemoryRegion *) mr;
971 }
972 return NULL;
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100973}
974
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000975/**
976 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
977 * if an iommu or NULL if not
978 *
Jay Zhou57914ec2018-01-04 13:29:48 +0800979 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
980 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000981 *
982 * @mr: the memory region being queried
983 */
984static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
985 IOMMUMemoryRegion *iommu_mr)
986{
987 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
988}
989
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000990#define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
Avi Kivity30951152012-10-30 13:47:46 +0200991
992/**
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +1000993 * memory_region_iommu_get_min_page_size: get minimum supported page size
994 * for an iommu
995 *
996 * Returns minimum supported page size for an iommu.
997 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000998 * @iommu_mr: the memory region being queried
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +1000999 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001000uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +10001001
1002/**
David Gibson06866572013-05-14 19:13:56 +10001003 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1004 *
Peter Xucdb30812016-09-23 13:02:26 +08001005 * The notification type will be decided by entry.perm bits:
1006 *
1007 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
1008 * - For MAP (newly added entry) notifies: set entry.perm to the
1009 * permission of the page (which is definitely !IOMMU_NONE).
1010 *
1011 * Note: for any IOMMU implementation, an in-place mapping change
1012 * should be notified with an UNMAP followed by a MAP.
1013 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001014 * @iommu_mr: the memory region that was changed
Peter Maydellcb1efcf2018-06-15 14:57:16 +01001015 * @iommu_idx: the IOMMU index for the translation table which has changed
David Gibson06866572013-05-14 19:13:56 +10001016 * @entry: the new entry in the IOMMU translation table. The entry
1017 * replaces all old entries for the same virtual I/O address range.
1018 * Deleted entries have .@perm == 0.
1019 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001020void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
Peter Maydellcb1efcf2018-06-15 14:57:16 +01001021 int iommu_idx,
David Gibson06866572013-05-14 19:13:56 +10001022 IOMMUTLBEntry entry);
1023
1024/**
Peter Xubd2bfa42017-04-07 18:59:10 +08001025 * memory_region_notify_one: notify a change in an IOMMU translation
1026 * entry to a single notifier
1027 *
1028 * This works just like memory_region_notify_iommu(), but it only
1029 * notifies a specific notifier, not all of them.
1030 *
1031 * @notifier: the notifier to be notified
1032 * @entry: the new entry in the IOMMU translation table. The entry
1033 * replaces all old entries for the same virtual I/O address range.
1034 * Deleted entries have .@perm == 0.
1035 */
1036void memory_region_notify_one(IOMMUNotifier *notifier,
1037 IOMMUTLBEntry *entry);
1038
1039/**
David Gibson06866572013-05-14 19:13:56 +10001040 * memory_region_register_iommu_notifier: register a notifier for changes to
1041 * IOMMU translation entries.
1042 *
1043 * @mr: the memory region to observe
Peter Xucdb30812016-09-23 13:02:26 +08001044 * @n: the IOMMUNotifier to be added; the notify callback receives a
1045 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1046 * ceases to be valid on exit from the notifier.
David Gibson06866572013-05-14 19:13:56 +10001047 */
Peter Xucdb30812016-09-23 13:02:26 +08001048void memory_region_register_iommu_notifier(MemoryRegion *mr,
1049 IOMMUNotifier *n);
David Gibson06866572013-05-14 19:13:56 +10001050
1051/**
David Gibsona788f222015-09-30 12:13:55 +10001052 * memory_region_iommu_replay: replay existing IOMMU translations to
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +10001053 * a notifier with the minimum page granularity returned by
1054 * mr->iommu_ops->get_page_size().
David Gibsona788f222015-09-30 12:13:55 +10001055 *
Peter Maydell2ce931d2018-05-31 14:50:52 +01001056 * Note: this is not related to record-and-replay functionality.
1057 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001058 * @iommu_mr: the memory region to observe
David Gibsona788f222015-09-30 12:13:55 +10001059 * @n: the notifier to which to replay iommu mappings
David Gibsona788f222015-09-30 12:13:55 +10001060 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001061void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
David Gibsona788f222015-09-30 12:13:55 +10001062
1063/**
Peter Xude472e42017-04-07 18:59:09 +08001064 * memory_region_iommu_replay_all: replay existing IOMMU translations
1065 * to all the notifiers registered.
1066 *
Peter Maydell2ce931d2018-05-31 14:50:52 +01001067 * Note: this is not related to record-and-replay functionality.
1068 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001069 * @iommu_mr: the memory region to observe
Peter Xude472e42017-04-07 18:59:09 +08001070 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001071void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr);
Peter Xude472e42017-04-07 18:59:09 +08001072
1073/**
David Gibson06866572013-05-14 19:13:56 +10001074 * memory_region_unregister_iommu_notifier: unregister a notifier for
1075 * changes to IOMMU translation entries.
1076 *
Alexey Kardashevskiyd22d8952016-06-30 13:00:23 -06001077 * @mr: the memory region which was observed and for which notity_stopped()
1078 * needs to be called
David Gibson06866572013-05-14 19:13:56 +10001079 * @n: the notifier to be removed.
1080 */
Peter Xucdb30812016-09-23 13:02:26 +08001081void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1082 IOMMUNotifier *n);
David Gibson06866572013-05-14 19:13:56 +10001083
1084/**
Alexey Kardashevskiyf1334de2018-02-06 11:08:24 -07001085 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1086 * defined on the IOMMU.
1087 *
Peter Maydell2ce931d2018-05-31 14:50:52 +01001088 * Returns 0 on success, or a negative errno otherwise. In particular,
1089 * -EINVAL indicates that the IOMMU does not support the requested
1090 * attribute.
Alexey Kardashevskiyf1334de2018-02-06 11:08:24 -07001091 *
1092 * @iommu_mr: the memory region
1093 * @attr: the requested attribute
1094 * @data: a pointer to the requested attribute data
1095 */
1096int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1097 enum IOMMUMemoryRegionAttr attr,
1098 void *data);
1099
1100/**
Peter Maydell21f40202018-06-15 14:57:15 +01001101 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1102 * use for translations with the given memory transaction attributes.
1103 *
1104 * @iommu_mr: the memory region
1105 * @attrs: the memory transaction attributes
1106 */
1107int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
1108 MemTxAttrs attrs);
1109
1110/**
1111 * memory_region_iommu_num_indexes: return the total number of IOMMU
1112 * indexes that this IOMMU supports.
1113 *
1114 * @iommu_mr: the memory region
1115 */
1116int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr);
1117
1118/**
Avi Kivity8991c792011-12-20 15:53:11 +02001119 * memory_region_name: get a memory region's name
1120 *
1121 * Returns the string that was used to initialize the memory region.
1122 *
1123 * @mr: the memory region being queried
1124 */
Peter Crosthwaite5d546d42014-08-14 23:55:03 -07001125const char *memory_region_name(const MemoryRegion *mr);
Avi Kivity8991c792011-12-20 15:53:11 +02001126
1127/**
Avi Kivity55043ba2011-12-15 17:20:34 +02001128 * memory_region_is_logging: return whether a memory region is logging writes
1129 *
Paolo Bonzini2d1a35b2015-03-23 10:50:57 +01001130 * Returns %true if the memory region is logging writes for the given client
1131 *
1132 * @mr: the memory region being queried
1133 * @client: the client being queried
1134 */
1135bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
1136
1137/**
1138 * memory_region_get_dirty_log_mask: return the clients for which a
1139 * memory region is logging writes.
1140 *
Paolo Bonzini677e7802015-03-23 10:53:21 +01001141 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1142 * are the bit indices.
Avi Kivity55043ba2011-12-15 17:20:34 +02001143 *
1144 * @mr: the memory region being queried
1145 */
Paolo Bonzini2d1a35b2015-03-23 10:50:57 +01001146uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
Avi Kivity55043ba2011-12-15 17:20:34 +02001147
1148/**
Avi Kivityce7923d2011-12-08 16:05:11 +02001149 * memory_region_is_rom: check whether a memory region is ROM
1150 *
1151 * Returns %true is a memory region is read-only memory.
1152 *
1153 * @mr: the memory region being queried
1154 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +01001155static inline bool memory_region_is_rom(MemoryRegion *mr)
1156{
1157 return mr->ram && mr->readonly;
1158}
1159
Avi Kivityce7923d2011-12-08 16:05:11 +02001160
1161/**
Paolo Bonzinia35ba7b2014-06-10 19:15:23 +08001162 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1163 *
1164 * Returns a file descriptor backing a file-based RAM memory region,
1165 * or -1 if the region is not a file-based RAM memory region.
1166 *
1167 * @mr: the RAM or alias memory region being queried.
1168 */
1169int memory_region_get_fd(MemoryRegion *mr);
1170
1171/**
Paolo Bonzini07bdaa42016-03-25 12:55:08 +01001172 * memory_region_from_host: Convert a pointer into a RAM memory region
1173 * and an offset within it.
1174 *
1175 * Given a host pointer inside a RAM memory region (created with
1176 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1177 * the MemoryRegion and the offset within it.
1178 *
1179 * Use with care; by the time this function returns, the returned pointer is
1180 * not protected by RCU anymore. If the caller is not within an RCU critical
1181 * section and does not hold the iothread lock, it must have other means of
1182 * protecting the pointer, such as a reference to the region that includes
1183 * the incoming ram_addr_t.
1184 *
Jay Zhou57914ec2018-01-04 13:29:48 +08001185 * @ptr: the host pointer to be converted
1186 * @offset: the offset within memory region
Paolo Bonzini07bdaa42016-03-25 12:55:08 +01001187 */
1188MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
1189
1190/**
Avi Kivity093bc2c2011-07-26 14:26:01 +03001191 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1192 *
1193 * Returns a host pointer to a RAM memory region (created with
Paolo Bonzini49b24af2015-12-16 10:30:47 +01001194 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1195 *
1196 * Use with care; by the time this function returns, the returned pointer is
1197 * not protected by RCU anymore. If the caller is not within an RCU critical
1198 * section and does not hold the iothread lock, it must have other means of
1199 * protecting the pointer, such as a reference to the region that includes
1200 * the incoming ram_addr_t.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001201 *
1202 * @mr: the memory region being queried.
1203 */
1204void *memory_region_get_ram_ptr(MemoryRegion *mr);
1205
Paolo Bonzini37d7c082015-03-23 10:21:46 +01001206/* memory_region_ram_resize: Resize a RAM region.
1207 *
1208 * Only legal before guest might have detected the memory size: e.g. on
1209 * incoming migration, or right after reset.
1210 *
1211 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1212 * @newsize: the new size the region
1213 * @errp: pointer to Error*, to store an error if it happens.
1214 */
1215void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
1216 Error **errp);
1217
Avi Kivity093bc2c2011-07-26 14:26:01 +03001218/**
Avi Kivity093bc2c2011-07-26 14:26:01 +03001219 * memory_region_set_log: Turn dirty logging on or off for a region.
1220 *
1221 * Turns dirty logging on or off for a specified client (display, migration).
1222 * Only meaningful for RAM regions.
1223 *
1224 * @mr: the memory region being updated.
1225 * @log: whether dirty logging is to be enabled or disabled.
Paolo Bonzinidbddac62015-03-23 10:31:53 +01001226 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001227 */
1228void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
1229
1230/**
Blue Swirlcd7a45c2012-01-22 16:38:21 +00001231 * memory_region_get_dirty: Check whether a range of bytes is dirty
1232 * for a specified client.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001233 *
Blue Swirlcd7a45c2012-01-22 16:38:21 +00001234 * Checks whether a range of bytes has been written to since the last
Avi Kivity093bc2c2011-07-26 14:26:01 +03001235 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1236 * must be enabled.
1237 *
1238 * @mr: the memory region being queried.
1239 * @addr: the address (relative to the start of the region) being queried.
Blue Swirlcd7a45c2012-01-22 16:38:21 +00001240 * @size: the size of the range being queried.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001241 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1242 * %DIRTY_MEMORY_VGA.
1243 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001244bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1245 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001246
1247/**
Blue Swirlfd4aa972011-10-16 16:04:59 +00001248 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001249 *
Blue Swirlfd4aa972011-10-16 16:04:59 +00001250 * Marks a range of bytes as dirty, after it has been dirtied outside
1251 * guest code.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001252 *
Blue Swirlfd4aa972011-10-16 16:04:59 +00001253 * @mr: the memory region being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001254 * @addr: the address (relative to the start of the region) being dirtied.
Blue Swirlfd4aa972011-10-16 16:04:59 +00001255 * @size: size of the range being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001256 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001257void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1258 hwaddr size);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001259
1260/**
Gerd Hoffmann8deaf122017-04-21 11:16:25 +02001261 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1262 * bitmap and clear it.
1263 *
1264 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1265 * returns the snapshot. The snapshot can then be used to query dirty
Paolo Bonzini77302fb2018-02-06 18:30:04 +01001266 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1267 * querying the same page multiple times, which is especially useful for
1268 * display updates where the scanlines often are not page aligned.
Gerd Hoffmann8deaf122017-04-21 11:16:25 +02001269 *
1270 * The dirty bitmap region which gets copyed into the snapshot (and
1271 * cleared afterwards) can be larger than requested. The boundaries
1272 * are rounded up/down so complete bitmap longs (covering 64 pages on
1273 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1274 * isn't a problem for display updates as the extra pages are outside
1275 * the visible area, and in case the visible area changes a full
1276 * display redraw is due anyway. Should other use cases for this
1277 * function emerge we might have to revisit this implementation
1278 * detail.
1279 *
1280 * Use g_free to release DirtyBitmapSnapshot.
1281 *
1282 * @mr: the memory region being queried.
1283 * @addr: the address (relative to the start of the region) being queried.
1284 * @size: the size of the range being queried.
1285 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1286 */
1287DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
1288 hwaddr addr,
1289 hwaddr size,
1290 unsigned client);
1291
1292/**
1293 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1294 * in the specified dirty bitmap snapshot.
1295 *
1296 * @mr: the memory region being queried.
1297 * @snap: the dirty bitmap snapshot
1298 * @addr: the address (relative to the start of the region) being queried.
1299 * @size: the size of the range being queried.
1300 */
1301bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
1302 DirtyBitmapSnapshot *snap,
1303 hwaddr addr, hwaddr size);
1304
Juan Quintela6c279db2012-10-17 20:24:28 +02001305/**
Avi Kivity093bc2c2011-07-26 14:26:01 +03001306 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1307 * client.
1308 *
1309 * Marks a range of pages as no longer dirty.
1310 *
1311 * @mr: the region being updated.
1312 * @addr: the start of the subrange being cleaned.
1313 * @size: the size of the subrange being cleaned.
1314 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1315 * %DIRTY_MEMORY_VGA.
1316 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001317void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1318 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001319
1320/**
1321 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1322 *
1323 * Allows a memory region to be marked as read-only (turning it into a ROM).
1324 * only useful on RAM regions.
1325 *
1326 * @mr: the region being updated.
1327 * @readonly: whether rhe region is to be ROM or RAM.
1328 */
1329void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
1330
1331/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +02001332 * memory_region_rom_device_set_romd: enable/disable ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001333 *
1334 * Allows a ROM device (initialized with memory_region_init_rom_device() to
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +02001335 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1336 * device is mapped to guest memory and satisfies read access directly.
1337 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1338 * Writes are always handled by the #MemoryRegion.write function.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001339 *
1340 * @mr: the memory region to be updated
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +02001341 * @romd_mode: %true to put the region into ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001342 */
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +02001343void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001344
1345/**
Avi Kivity093bc2c2011-07-26 14:26:01 +03001346 * memory_region_set_coalescing: Enable memory coalescing for the region.
1347 *
1348 * Enabled writes to a region to be queued for later processing. MMIO ->write
1349 * callbacks may be delayed until a non-coalesced MMIO is issued.
1350 * Only useful for IO regions. Roughly similar to write-combining hardware.
1351 *
1352 * @mr: the memory region to be write coalesced
1353 */
1354void memory_region_set_coalescing(MemoryRegion *mr);
1355
1356/**
1357 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1358 * a region.
1359 *
1360 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1361 * Multiple calls can be issued coalesced disjoint ranges.
1362 *
1363 * @mr: the memory region to be updated.
1364 * @offset: the start of the range within the region to be coalesced.
1365 * @size: the size of the subrange to be coalesced.
1366 */
1367void memory_region_add_coalescing(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001368 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +03001369 uint64_t size);
1370
1371/**
1372 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1373 *
1374 * Disables any coalescing caused by memory_region_set_coalescing() or
1375 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1376 * hardware.
1377 *
1378 * @mr: the memory region to be updated.
1379 */
1380void memory_region_clear_coalescing(MemoryRegion *mr);
1381
1382/**
Jan Kiszkad4105152012-08-23 13:02:29 +02001383 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1384 * accesses.
1385 *
1386 * Ensure that pending coalesced MMIO request are flushed before the memory
1387 * region is accessed. This property is automatically enabled for all regions
1388 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1389 *
1390 * @mr: the memory region to be updated.
1391 */
1392void memory_region_set_flush_coalesced(MemoryRegion *mr);
1393
1394/**
1395 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1396 * accesses.
1397 *
1398 * Clear the automatic coalesced MMIO flushing enabled via
1399 * memory_region_set_flush_coalesced. Note that this service has no effect on
1400 * memory regions that have MMIO coalescing enabled for themselves. For them,
1401 * automatic flushing will stop once coalescing is disabled.
1402 *
1403 * @mr: the memory region to be updated.
1404 */
1405void memory_region_clear_flush_coalesced(MemoryRegion *mr);
1406
1407/**
Jan Kiszka196ea132015-06-18 18:47:20 +02001408 * memory_region_clear_global_locking: Declares that access processing does
1409 * not depend on the QEMU global lock.
1410 *
1411 * By clearing this property, accesses to the memory region will be processed
1412 * outside of QEMU's global lock (unless the lock is held on when issuing the
1413 * access request). In this case, the device model implementing the access
1414 * handlers is responsible for synchronization of concurrency.
1415 *
1416 * @mr: the memory region to be updated.
1417 */
1418void memory_region_clear_global_locking(MemoryRegion *mr);
1419
1420/**
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001421 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1422 * is written to a location.
1423 *
1424 * Marks a word in an IO region (initialized with memory_region_init_io())
1425 * as a trigger for an eventfd event. The I/O callback will not be called.
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001426 * The caller must be prepared to handle failure (that is, take the required
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001427 * action if the callback _is_ called).
1428 *
1429 * @mr: the memory region being updated.
1430 * @addr: the address within @mr that is to be monitored
1431 * @size: the size of the access to trigger the eventfd
1432 * @match_data: whether to match against @data, instead of just @addr
1433 * @data: the data to match against the guest write
Jay Zhou57914ec2018-01-04 13:29:48 +08001434 * @e: event notifier to be triggered when @addr, @size, and @data all match.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001435 **/
1436void memory_region_add_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001437 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001438 unsigned size,
1439 bool match_data,
1440 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +02001441 EventNotifier *e);
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001442
1443/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001444 * memory_region_del_eventfd: Cancel an eventfd.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001445 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001446 * Cancels an eventfd trigger requested by a previous
1447 * memory_region_add_eventfd() call.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001448 *
1449 * @mr: the memory region being updated.
1450 * @addr: the address within @mr that is to be monitored
1451 * @size: the size of the access to trigger the eventfd
1452 * @match_data: whether to match against @data, instead of just @addr
1453 * @data: the data to match against the guest write
Jay Zhou57914ec2018-01-04 13:29:48 +08001454 * @e: event notifier to be triggered when @addr, @size, and @data all match.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001455 */
1456void memory_region_del_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001457 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001458 unsigned size,
1459 bool match_data,
1460 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +02001461 EventNotifier *e);
1462
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001463/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001464 * memory_region_add_subregion: Add a subregion to a container.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001465 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001466 * Adds a subregion at @offset. The subregion may not overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +03001467 * subregions (except for those explicitly marked as overlapping). A region
1468 * may only be added once as a subregion (unless removed with
1469 * memory_region_del_subregion()); use memory_region_init_alias() if you
1470 * want a region to be a subregion in multiple locations.
1471 *
1472 * @mr: the region to contain the new subregion; must be a container
1473 * initialized with memory_region_init().
1474 * @offset: the offset relative to @mr where @subregion is added.
1475 * @subregion: the subregion to be added.
1476 */
1477void memory_region_add_subregion(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001478 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +03001479 MemoryRegion *subregion);
1480/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +02001481 * memory_region_add_subregion_overlap: Add a subregion to a container
1482 * with overlap.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001483 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001484 * Adds a subregion at @offset. The subregion may overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +03001485 * subregions. Conflicts are resolved by having a higher @priority hide a
1486 * lower @priority. Subregions without priority are taken as @priority 0.
1487 * A region may only be added once as a subregion (unless removed with
1488 * memory_region_del_subregion()); use memory_region_init_alias() if you
1489 * want a region to be a subregion in multiple locations.
1490 *
1491 * @mr: the region to contain the new subregion; must be a container
1492 * initialized with memory_region_init().
1493 * @offset: the offset relative to @mr where @subregion is added.
1494 * @subregion: the subregion to be added.
1495 * @priority: used for resolving overlaps; highest priority wins.
1496 */
1497void memory_region_add_subregion_overlap(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001498 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +03001499 MemoryRegion *subregion,
Marcel Apfelbauma1ff8ae2013-09-16 11:21:14 +03001500 int priority);
Avi Kivitye34911c2011-12-19 12:06:23 +02001501
1502/**
1503 * memory_region_get_ram_addr: Get the ram address associated with a memory
1504 * region
Avi Kivitye34911c2011-12-19 12:06:23 +02001505 */
Fam Zheng7ebb2742016-03-01 14:18:20 +08001506ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
Avi Kivitye34911c2011-12-19 12:06:23 +02001507
Igor Mammedova2b257d2014-10-31 16:38:37 +00001508uint64_t memory_region_get_alignment(const MemoryRegion *mr);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001509/**
1510 * memory_region_del_subregion: Remove a subregion.
1511 *
1512 * Removes a subregion from its container.
1513 *
1514 * @mr: the container to be updated.
1515 * @subregion: the region being removed; must be a current subregion of @mr.
1516 */
1517void memory_region_del_subregion(MemoryRegion *mr,
1518 MemoryRegion *subregion);
1519
Avi Kivity6bba19b2011-09-14 11:54:58 +03001520/*
1521 * memory_region_set_enabled: dynamically enable or disable a region
1522 *
1523 * Enables or disables a memory region. A disabled memory region
1524 * ignores all accesses to itself and its subregions. It does not
1525 * obscure sibling subregions with lower priority - it simply behaves as
1526 * if it was removed from the hierarchy.
1527 *
1528 * Regions default to being enabled.
1529 *
1530 * @mr: the region to be updated
1531 * @enabled: whether to enable or disable the region
1532 */
1533void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1534
Avi Kivity2282e1a2011-09-14 12:10:12 +03001535/*
1536 * memory_region_set_address: dynamically update the address of a region
1537 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001538 * Dynamically updates the address of a region, relative to its container.
Avi Kivity2282e1a2011-09-14 12:10:12 +03001539 * May be used on regions are currently part of a memory hierarchy.
1540 *
1541 * @mr: the region to be updated
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001542 * @addr: new address, relative to container region
Avi Kivity2282e1a2011-09-14 12:10:12 +03001543 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001544void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
Avi Kivity2282e1a2011-09-14 12:10:12 +03001545
Avi Kivity47033592011-12-04 19:16:50 +02001546/*
Michael S. Tsirkine7af4c62014-12-16 11:21:23 +02001547 * memory_region_set_size: dynamically update the size of a region.
1548 *
1549 * Dynamically updates the size of a region.
1550 *
1551 * @mr: the region to be updated
1552 * @size: used size of the region.
1553 */
1554void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1555
1556/*
Avi Kivity47033592011-12-04 19:16:50 +02001557 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1558 *
1559 * Dynamically updates the offset into the target region that an alias points
1560 * to, as if the fourth argument to memory_region_init_alias() has changed.
1561 *
1562 * @mr: the #MemoryRegion to be updated; should be an alias.
1563 * @offset: the new offset into the target memory region
1564 */
1565void memory_region_set_alias_offset(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001566 hwaddr offset);
Avi Kivity47033592011-12-04 19:16:50 +02001567
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001568/**
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001569 * memory_region_present: checks if an address relative to a @container
1570 * translates into #MemoryRegion within @container
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001571 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001572 * Answer whether a #MemoryRegion within @container covers the address
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001573 * @addr.
1574 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001575 * @container: a #MemoryRegion within which @addr is a relative address
1576 * @addr: the area within @container to be searched
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001577 */
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001578bool memory_region_present(MemoryRegion *container, hwaddr addr);
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001579
1580/**
Igor Mammedoveed2bac2014-06-02 15:25:06 +02001581 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1582 * into any address space.
1583 *
1584 * @mr: a #MemoryRegion which should be checked if it's mapped
1585 */
1586bool memory_region_is_mapped(MemoryRegion *mr);
1587
1588/**
Paolo Bonzini73034e92013-05-07 15:48:28 +02001589 * memory_region_find: translate an address/size relative to a
1590 * MemoryRegion into a #MemoryRegionSection.
Avi Kivitye2177952011-12-08 15:00:18 +02001591 *
Paolo Bonzini73034e92013-05-07 15:48:28 +02001592 * Locates the first #MemoryRegion within @mr that overlaps the range
1593 * given by @addr and @size.
Avi Kivitye2177952011-12-08 15:00:18 +02001594 *
1595 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1596 * It will have the following characteristics:
Avi Kivitye2177952011-12-08 15:00:18 +02001597 * .@size = 0 iff no overlap was found
1598 * .@mr is non-%NULL iff an overlap was found
1599 *
Paolo Bonzini73034e92013-05-07 15:48:28 +02001600 * Remember that in the return value the @offset_within_region is
1601 * relative to the returned region (in the .@mr field), not to the
1602 * @mr argument.
1603 *
1604 * Similarly, the .@offset_within_address_space is relative to the
1605 * address space that contains both regions, the passed and the
1606 * returned one. However, in the special case where the @mr argument
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001607 * has no container (and thus is the root of the address space), the
Paolo Bonzini73034e92013-05-07 15:48:28 +02001608 * following will hold:
1609 * .@offset_within_address_space >= @addr
1610 * .@offset_within_address_space + .@size <= @addr + @size
1611 *
1612 * @mr: a MemoryRegion within which @addr is a relative address
1613 * @addr: start of the area within @as to be searched
Avi Kivitye2177952011-12-08 15:00:18 +02001614 * @size: size of the area to be searched
1615 */
Paolo Bonzini73034e92013-05-07 15:48:28 +02001616MemoryRegionSection memory_region_find(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001617 hwaddr addr, uint64_t size);
Avi Kivitye2177952011-12-08 15:00:18 +02001618
Blue Swirlfd062572012-04-09 17:38:52 +00001619/**
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +02001620 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
Avi Kivity86e775c2011-12-15 16:24:49 +02001621 *
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +02001622 * Synchronizes the dirty page log for all address spaces.
Avi Kivity86e775c2011-12-15 16:24:49 +02001623 */
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +02001624void memory_global_dirty_log_sync(void);
Avi Kivity86e775c2011-12-15 16:24:49 +02001625
Avi Kivitye2177952011-12-08 15:00:18 +02001626/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001627 * memory_region_transaction_begin: Start a transaction.
1628 *
1629 * During a transaction, changes will be accumulated and made visible
Stefan Weildabdf392012-01-08 19:35:09 +01001630 * only when the transaction ends (is committed).
Avi Kivity4ef4db82011-07-26 14:26:13 +03001631 */
1632void memory_region_transaction_begin(void);
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001633
1634/**
1635 * memory_region_transaction_commit: Commit a transaction and make changes
1636 * visible to the guest.
Avi Kivity4ef4db82011-07-26 14:26:13 +03001637 */
1638void memory_region_transaction_commit(void);
1639
Avi Kivity7664e802011-12-11 14:47:25 +02001640/**
1641 * memory_listener_register: register callbacks to be called when memory
1642 * sections are mapped or unmapped into an address
1643 * space
1644 *
1645 * @listener: an object containing the callbacks to be called
Avi Kivity7376e582012-02-08 21:05:17 +02001646 * @filter: if non-%NULL, only regions in this address space will be observed
Avi Kivity7664e802011-12-11 14:47:25 +02001647 */
Avi Kivityf6790af2012-10-02 20:13:51 +02001648void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
Avi Kivity7664e802011-12-11 14:47:25 +02001649
1650/**
1651 * memory_listener_unregister: undo the effect of memory_listener_register()
1652 *
1653 * @listener: an object containing the callbacks to be removed
1654 */
1655void memory_listener_unregister(MemoryListener *listener);
1656
1657/**
1658 * memory_global_dirty_log_start: begin dirty logging for all regions
1659 */
1660void memory_global_dirty_log_start(void);
1661
1662/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +02001663 * memory_global_dirty_log_stop: end dirty logging for all regions
Avi Kivity7664e802011-12-11 14:47:25 +02001664 */
1665void memory_global_dirty_log_stop(void);
1666
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001667void mtree_info(fprintf_function mon_printf, void *f, bool flatview,
1668 bool dispatch_tree);
Blue Swirl314e2982011-09-11 20:22:05 +00001669
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001670/**
KONRAD Fredericc9356742016-10-19 15:06:49 +02001671 * memory_region_request_mmio_ptr: request a pointer to an mmio
1672 * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer.
1673 * When the device wants to invalidate the pointer it will call
1674 * memory_region_invalidate_mmio_ptr.
1675 *
1676 * @mr: #MemoryRegion to check
1677 * @addr: address within that region
1678 *
1679 * Returns true on success, false otherwise.
1680 */
1681bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr);
1682
1683/**
1684 * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio
1685 * previously requested.
1686 * In the end that means that if something wants to execute from this area it
1687 * will need to request the pointer again.
1688 *
1689 * @mr: #MemoryRegion associated to the pointer.
Jay Zhou57914ec2018-01-04 13:29:48 +08001690 * @offset: offset within the memory region
KONRAD Fredericc9356742016-10-19 15:06:49 +02001691 * @size: size of that area.
1692 */
1693void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset,
1694 unsigned size);
1695
1696/**
Peter Maydell3b643492015-04-26 16:49:23 +01001697 * memory_region_dispatch_read: perform a read directly to the specified
1698 * MemoryRegion.
1699 *
1700 * @mr: #MemoryRegion to access
1701 * @addr: address within that region
1702 * @pval: pointer to uint64_t which the data is written to
1703 * @size: size of the access in bytes
1704 * @attrs: memory transaction attributes to use for the access
1705 */
1706MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1707 hwaddr addr,
1708 uint64_t *pval,
1709 unsigned size,
1710 MemTxAttrs attrs);
1711/**
1712 * memory_region_dispatch_write: perform a write directly to the specified
1713 * MemoryRegion.
1714 *
1715 * @mr: #MemoryRegion to access
1716 * @addr: address within that region
1717 * @data: data to write
1718 * @size: size of the access in bytes
1719 * @attrs: memory transaction attributes to use for the access
1720 */
1721MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1722 hwaddr addr,
1723 uint64_t data,
1724 unsigned size,
1725 MemTxAttrs attrs);
1726
1727/**
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001728 * address_space_init: initializes an address space
1729 *
1730 * @as: an uninitialized #AddressSpace
Veres Lajos67cc32e2015-09-08 22:45:14 +01001731 * @root: a #MemoryRegion that routes addresses for the address space
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001732 * @name: an address space name. The name is only used for debugging
1733 * output.
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001734 */
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001735void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001736
Peter Crosthwaitef0c02d12016-01-21 14:15:06 +00001737/**
Avi Kivity83f3c252012-10-07 12:59:55 +02001738 * address_space_destroy: destroy an address space
1739 *
1740 * Releases all resources associated with an address space. After an address space
1741 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1742 * as well.
1743 *
1744 * @as: address space to be destroyed
1745 */
1746void address_space_destroy(AddressSpace *as);
1747
Avi Kivityac1970f2012-10-03 16:22:53 +02001748/**
1749 * address_space_rw: read from or write to an address space.
1750 *
Peter Maydell5c9eb022015-04-26 16:49:24 +01001751 * Return a MemTxResult indicating whether the operation succeeded
1752 * or failed (eg unassigned memory, device rejected the transaction,
1753 * IOMMU fault).
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001754 *
Avi Kivityac1970f2012-10-03 16:22:53 +02001755 * @as: #AddressSpace to be accessed
1756 * @addr: address within that address space
Peter Maydell5c9eb022015-04-26 16:49:24 +01001757 * @attrs: memory transaction attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001758 * @buf: buffer with the data transferred
Jay Zhou57914ec2018-01-04 13:29:48 +08001759 * @len: the number of bytes to read or write
Avi Kivityac1970f2012-10-03 16:22:53 +02001760 * @is_write: indicates the transfer direction
1761 */
Peter Maydell5c9eb022015-04-26 16:49:24 +01001762MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1763 MemTxAttrs attrs, uint8_t *buf,
1764 int len, bool is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02001765
1766/**
1767 * address_space_write: write to address space.
1768 *
Peter Maydell5c9eb022015-04-26 16:49:24 +01001769 * Return a MemTxResult indicating whether the operation succeeded
1770 * or failed (eg unassigned memory, device rejected the transaction,
1771 * IOMMU fault).
Avi Kivityac1970f2012-10-03 16:22:53 +02001772 *
1773 * @as: #AddressSpace to be accessed
1774 * @addr: address within that address space
Peter Maydell5c9eb022015-04-26 16:49:24 +01001775 * @attrs: memory transaction attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001776 * @buf: buffer with the data transferred
Jay Zhou57914ec2018-01-04 13:29:48 +08001777 * @len: the number of bytes to write
Avi Kivityac1970f2012-10-03 16:22:53 +02001778 */
Peter Maydell5c9eb022015-04-26 16:49:24 +01001779MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1780 MemTxAttrs attrs,
1781 const uint8_t *buf, int len);
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001782
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001783/* address_space_ld*: load from an address space
Peter Maydell50013112015-04-26 16:49:24 +01001784 * address_space_st*: store to an address space
1785 *
1786 * These functions perform a load or store of the byte, word,
1787 * longword or quad to the specified address within the AddressSpace.
1788 * The _le suffixed functions treat the data as little endian;
1789 * _be indicates big endian; no suffix indicates "same endianness
1790 * as guest CPU".
1791 *
1792 * The "guest CPU endianness" accessors are deprecated for use outside
1793 * target-* code; devices should be CPU-agnostic and use either the LE
1794 * or the BE accessors.
1795 *
1796 * @as #AddressSpace to be accessed
1797 * @addr: address within that address space
1798 * @val: data value, for stores
1799 * @attrs: memory transaction attributes
1800 * @result: location to write the success/failure of the transaction;
1801 * if NULL, this information is discarded
1802 */
Peter Maydell50013112015-04-26 16:49:24 +01001803
Paolo Bonzini4269c822018-03-04 23:31:47 +01001804#define SUFFIX
1805#define ARG1 as
1806#define ARG1_DECL AddressSpace *as
1807#include "exec/memory_ldst.inc.h"
1808
1809#define SUFFIX
1810#define ARG1 as
1811#define ARG1_DECL AddressSpace *as
1812#include "exec/memory_ldst_phys.inc.h"
Paolo Bonzini0ce265f2016-11-22 11:34:02 +01001813
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001814struct MemoryRegionCache {
Paolo Bonzini48564042018-03-18 18:26:36 +01001815 void *ptr;
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001816 hwaddr xlat;
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001817 hwaddr len;
Paolo Bonzini48564042018-03-18 18:26:36 +01001818 FlatView *fv;
1819 MemoryRegionSection mrs;
1820 bool is_write;
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001821};
1822
Paolo Bonzini48564042018-03-18 18:26:36 +01001823#define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
1824
Paolo Bonzini5eba0402017-01-27 16:40:16 +01001825
Paolo Bonzini4269c822018-03-04 23:31:47 +01001826/* address_space_ld*_cached: load from a cached #MemoryRegion
1827 * address_space_st*_cached: store into a cached #MemoryRegion
1828 *
1829 * These functions perform a load or store of the byte, word,
1830 * longword or quad to the specified address. The address is
1831 * a physical address in the AddressSpace, but it must lie within
1832 * a #MemoryRegion that was mapped with address_space_cache_init.
1833 *
1834 * The _le suffixed functions treat the data as little endian;
1835 * _be indicates big endian; no suffix indicates "same endianness
1836 * as guest CPU".
1837 *
1838 * The "guest CPU endianness" accessors are deprecated for use outside
1839 * target-* code; devices should be CPU-agnostic and use either the LE
1840 * or the BE accessors.
1841 *
1842 * @cache: previously initialized #MemoryRegionCache to be accessed
1843 * @addr: address within the address space
1844 * @val: data value, for stores
1845 * @attrs: memory transaction attributes
1846 * @result: location to write the success/failure of the transaction;
1847 * if NULL, this information is discarded
1848 */
1849
Paolo Bonzini48564042018-03-18 18:26:36 +01001850#define SUFFIX _cached_slow
Paolo Bonzini4269c822018-03-04 23:31:47 +01001851#define ARG1 cache
1852#define ARG1_DECL MemoryRegionCache *cache
1853#include "exec/memory_ldst.inc.h"
1854
Paolo Bonzini48564042018-03-18 18:26:36 +01001855/* Inline fast path for direct RAM access. */
1856static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache,
1857 hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
1858{
1859 assert(addr < cache->len);
1860 if (likely(cache->ptr)) {
1861 return ldub_p(cache->ptr + addr);
1862 } else {
1863 return address_space_ldub_cached_slow(cache, addr, attrs, result);
1864 }
1865}
1866
1867static inline void address_space_stb_cached(MemoryRegionCache *cache,
1868 hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result)
1869{
1870 assert(addr < cache->len);
1871 if (likely(cache->ptr)) {
1872 stb_p(cache->ptr + addr, val);
1873 } else {
1874 address_space_stb_cached_slow(cache, addr, val, attrs, result);
1875 }
1876}
1877
1878#define ENDIANNESS _le
1879#include "exec/memory_ldst_cached.inc.h"
1880
1881#define ENDIANNESS _be
1882#include "exec/memory_ldst_cached.inc.h"
1883
Paolo Bonzini4269c822018-03-04 23:31:47 +01001884#define SUFFIX _cached
1885#define ARG1 cache
1886#define ARG1_DECL MemoryRegionCache *cache
1887#include "exec/memory_ldst_phys.inc.h"
1888
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001889/* address_space_cache_init: prepare for repeated access to a physical
1890 * memory region
1891 *
1892 * @cache: #MemoryRegionCache to be filled
1893 * @as: #AddressSpace to be accessed
1894 * @addr: address within that address space
1895 * @len: length of buffer
1896 * @is_write: indicates the transfer direction
1897 *
1898 * Will only work with RAM, and may map a subset of the requested range by
1899 * returning a value that is less than @len. On failure, return a negative
1900 * errno value.
1901 *
1902 * Because it only works with RAM, this function can be used for
1903 * read-modify-write operations. In this case, is_write should be %true.
1904 *
1905 * Note that addresses passed to the address_space_*_cached functions
1906 * are relative to @addr.
1907 */
1908int64_t address_space_cache_init(MemoryRegionCache *cache,
1909 AddressSpace *as,
1910 hwaddr addr,
1911 hwaddr len,
1912 bool is_write);
1913
1914/**
1915 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1916 *
1917 * @cache: The #MemoryRegionCache to operate on.
1918 * @addr: The first physical address that was written, relative to the
1919 * address that was passed to @address_space_cache_init.
1920 * @access_len: The number of bytes that were written starting at @addr.
1921 */
1922void address_space_cache_invalidate(MemoryRegionCache *cache,
1923 hwaddr addr,
1924 hwaddr access_len);
1925
1926/**
1927 * address_space_cache_destroy: free a #MemoryRegionCache
1928 *
1929 * @cache: The #MemoryRegionCache whose memory should be released.
1930 */
1931void address_space_cache_destroy(MemoryRegionCache *cache);
1932
Jason Wang052c8fa2016-12-30 18:09:13 +08001933/* address_space_get_iotlb_entry: translate an address into an IOTLB
1934 * entry. Should be called from an RCU critical section.
1935 */
1936IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
Peter Maydell7446eb02018-05-31 14:50:53 +01001937 bool is_write, MemTxAttrs attrs);
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001938
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001939/* address_space_translate: translate an address range into an address space
Paolo Bonzini41063e12015-03-18 14:21:43 +01001940 * into a MemoryRegion and an address range into that section. Should be
1941 * called from an RCU critical section, to avoid that the last reference
1942 * to the returned region disappears after address_space_translate returns.
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001943 *
Jay Zhou57914ec2018-01-04 13:29:48 +08001944 * @fv: #FlatView to be accessed
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001945 * @addr: address within that address space
1946 * @xlat: pointer to address within the returned memory region section's
1947 * #MemoryRegion.
1948 * @len: pointer to length
1949 * @is_write: indicates the transfer direction
Peter Maydellbc6b1ce2018-05-31 14:50:52 +01001950 * @attrs: memory attributes
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001951 */
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10001952MemoryRegion *flatview_translate(FlatView *fv,
1953 hwaddr addr, hwaddr *xlat,
Peter Maydellefa99a22018-05-31 14:50:52 +01001954 hwaddr *len, bool is_write,
1955 MemTxAttrs attrs);
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10001956
1957static inline MemoryRegion *address_space_translate(AddressSpace *as,
1958 hwaddr addr, hwaddr *xlat,
Peter Maydellbc6b1ce2018-05-31 14:50:52 +01001959 hwaddr *len, bool is_write,
1960 MemTxAttrs attrs)
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10001961{
1962 return flatview_translate(address_space_to_flatview(as),
Peter Maydellefa99a22018-05-31 14:50:52 +01001963 addr, xlat, len, is_write, attrs);
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10001964}
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001965
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001966/* address_space_access_valid: check for validity of accessing an address
1967 * space range
1968 *
Avi Kivity30951152012-10-30 13:47:46 +02001969 * Check whether memory is assigned to the given address space range, and
1970 * access is permitted by any IOMMU regions that are active for the address
1971 * space.
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001972 *
1973 * For now, addr and len should be aligned to a page size. This limitation
1974 * will be lifted in the future.
1975 *
1976 * @as: #AddressSpace to be accessed
1977 * @addr: address within that address space
1978 * @len: length of the area to be checked
1979 * @is_write: indicates the transfer direction
Peter Maydellfddffa42018-05-31 14:50:52 +01001980 * @attrs: memory attributes
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001981 */
Peter Maydellfddffa42018-05-31 14:50:52 +01001982bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len,
1983 bool is_write, MemTxAttrs attrs);
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001984
Avi Kivityac1970f2012-10-03 16:22:53 +02001985/* address_space_map: map a physical memory region into a host virtual address
1986 *
1987 * May map a subset of the requested range, given by and returned in @plen.
1988 * May return %NULL if resources needed to perform the mapping are exhausted.
1989 * Use only for reads OR writes - not for read-modify-write operations.
1990 * Use cpu_register_map_client() to know when retrying the map operation is
1991 * likely to succeed.
1992 *
1993 * @as: #AddressSpace to be accessed
1994 * @addr: address within that address space
1995 * @plen: pointer to length of buffer; updated on return
1996 * @is_write: indicates the transfer direction
Peter Maydellf26404f2018-05-31 14:50:52 +01001997 * @attrs: memory attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001998 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001999void *address_space_map(AddressSpace *as, hwaddr addr,
Peter Maydellf26404f2018-05-31 14:50:52 +01002000 hwaddr *plen, bool is_write, MemTxAttrs attrs);
Avi Kivityac1970f2012-10-03 16:22:53 +02002001
2002/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2003 *
2004 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2005 * the amount of memory that was actually read or written by the caller.
2006 *
2007 * @as: #AddressSpace used
Jay Zhou57914ec2018-01-04 13:29:48 +08002008 * @buffer: host pointer as returned by address_space_map()
Avi Kivityac1970f2012-10-03 16:22:53 +02002009 * @len: buffer length as returned by address_space_map()
2010 * @access_len: amount of data actually transferred
2011 * @is_write: indicates the transfer direction
2012 */
Avi Kivitya8170e52012-10-23 12:30:10 +02002013void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2014 int is_write, hwaddr access_len);
Avi Kivityac1970f2012-10-03 16:22:53 +02002015
2016
Paolo Bonzinia203ac72015-12-09 10:18:57 +01002017/* Internal functions, part of the implementation of address_space_read. */
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002018MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
2019 MemTxAttrs attrs, uint8_t *buf, int len);
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10002020MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
2021 MemTxAttrs attrs, uint8_t *buf,
2022 int len, hwaddr addr1, hwaddr l,
2023 MemoryRegion *mr);
Paolo Bonzini0878d0e2016-02-22 11:02:12 +01002024void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002025
Paolo Bonzini48564042018-03-18 18:26:36 +01002026/* Internal functions, part of the implementation of address_space_read_cached
2027 * and address_space_write_cached. */
2028void address_space_read_cached_slow(MemoryRegionCache *cache,
2029 hwaddr addr, void *buf, int len);
2030void address_space_write_cached_slow(MemoryRegionCache *cache,
2031 hwaddr addr, const void *buf, int len);
2032
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002033static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
2034{
2035 if (is_write) {
Alex Williamson4a2e2422016-10-31 09:53:03 -06002036 return memory_region_is_ram(mr) &&
2037 !mr->readonly && !memory_region_is_ram_device(mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002038 } else {
Alex Williamson4a2e2422016-10-31 09:53:03 -06002039 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
2040 memory_region_is_romd(mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002041 }
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002042}
2043
2044/**
2045 * address_space_read: read from an address space.
2046 *
2047 * Return a MemTxResult indicating whether the operation succeeded
2048 * or failed (eg unassigned memory, device rejected the transaction,
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002049 * IOMMU fault). Called within RCU critical section.
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002050 *
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002051 * @as: #AddressSpace to be accessed
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002052 * @addr: address within that address space
2053 * @attrs: memory transaction attributes
2054 * @buf: buffer with the data transferred
2055 */
2056static inline __attribute__((__always_inline__))
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002057MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
2058 MemTxAttrs attrs, uint8_t *buf,
2059 int len)
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002060{
2061 MemTxResult result = MEMTX_OK;
2062 hwaddr l, addr1;
2063 void *ptr;
2064 MemoryRegion *mr;
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002065 FlatView *fv;
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002066
2067 if (__builtin_constant_p(len)) {
2068 if (len) {
2069 rcu_read_lock();
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002070 fv = address_space_to_flatview(as);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002071 l = len;
Peter Maydellefa99a22018-05-31 14:50:52 +01002072 mr = flatview_translate(fv, addr, &addr1, &l, false, attrs);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002073 if (len == l && memory_access_is_direct(mr, false)) {
Paolo Bonzini0878d0e2016-02-22 11:02:12 +01002074 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002075 memcpy(buf, ptr, len);
2076 } else {
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10002077 result = flatview_read_continue(fv, addr, attrs, buf, len,
2078 addr1, l, mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002079 }
2080 rcu_read_unlock();
2081 }
2082 } else {
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002083 result = address_space_read_full(as, addr, attrs, buf, len);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002084 }
2085 return result;
2086}
Paolo Bonzinia203ac72015-12-09 10:18:57 +01002087
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01002088/**
2089 * address_space_read_cached: read from a cached RAM region
2090 *
2091 * @cache: Cached region to be addressed
2092 * @addr: address relative to the base of the RAM region
2093 * @buf: buffer with the data transferred
2094 * @len: length of the data transferred
2095 */
2096static inline void
2097address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
2098 void *buf, int len)
2099{
2100 assert(addr < cache->len && len <= cache->len - addr);
Paolo Bonzini48564042018-03-18 18:26:36 +01002101 if (likely(cache->ptr)) {
2102 memcpy(buf, cache->ptr + addr, len);
2103 } else {
2104 address_space_read_cached_slow(cache, addr, buf, len);
2105 }
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01002106}
2107
2108/**
2109 * address_space_write_cached: write to a cached RAM region
2110 *
2111 * @cache: Cached region to be addressed
2112 * @addr: address relative to the base of the RAM region
2113 * @buf: buffer with the data transferred
2114 * @len: length of the data transferred
2115 */
2116static inline void
2117address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
2118 void *buf, int len)
2119{
2120 assert(addr < cache->len && len <= cache->len - addr);
Paolo Bonzini48564042018-03-18 18:26:36 +01002121 if (likely(cache->ptr)) {
2122 memcpy(cache->ptr + addr, buf, len);
2123 } else {
2124 address_space_write_cached_slow(cache, addr, buf, len);
2125 }
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01002126}
2127
Avi Kivity093bc2c2011-07-26 14:26:01 +03002128#endif
2129
2130#endif