blob: ec75c45296e70d0bf874a491e55678a4c8b8f54b [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 Xubf55b7a2017-05-19 11:19:40 +0800254 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000255 IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
Peter Xubf55b7a2017-05-19 11:19:40 +0800256 IOMMUAccessFlags flag);
Peter Maydell2ce931d2018-05-31 14:50:52 +0100257 /* Returns minimum supported page size in bytes.
258 * If this method is not provided then the minimum is assumed to
259 * be TARGET_PAGE_SIZE.
260 *
261 * @iommu: the IOMMUMemoryRegion
262 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000263 uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
Peter Maydell2ce931d2018-05-31 14:50:52 +0100264 /* Called when IOMMU Notifier flag changes (ie when the set of
265 * events which IOMMU users are requesting notification for changes).
266 * Optional method -- need not be provided if the IOMMU does not
267 * need to know exactly which events must be notified.
268 *
269 * @iommu: the IOMMUMemoryRegion
270 * @old_flags: events which previously needed to be notified
271 * @new_flags: events which now need to be notified
272 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000273 void (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
Peter Xu5bf3d312016-09-23 13:02:27 +0800274 IOMMUNotifierFlag old_flags,
275 IOMMUNotifierFlag new_flags);
Peter Maydell2ce931d2018-05-31 14:50:52 +0100276 /* Called to handle memory_region_iommu_replay().
277 *
278 * The default implementation of memory_region_iommu_replay() is to
279 * call the IOMMU translate method for every page in the address space
280 * with flag == IOMMU_NONE and then call the notifier if translate
281 * returns a valid mapping. If this method is implemented then it
282 * overrides the default behaviour, and must provide the full semantics
283 * of memory_region_iommu_replay(), by calling @notifier for every
284 * translation present in the IOMMU.
285 *
286 * Optional method -- an IOMMU only needs to provide this method
287 * if the default is inefficient or produces undesirable side effects.
288 *
289 * Note: this is not related to record-and-replay functionality.
290 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000291 void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
Alexey Kardashevskiyf1334de2018-02-06 11:08:24 -0700292
Peter Maydell2ce931d2018-05-31 14:50:52 +0100293 /* Get IOMMU misc attributes. This is an optional method that
294 * can be used to allow users of the IOMMU to get implementation-specific
295 * information. The IOMMU implements this method to handle calls
296 * by IOMMU users to memory_region_iommu_get_attr() by filling in
297 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
298 * the IOMMU supports. If the method is unimplemented then
299 * memory_region_iommu_get_attr() will always return -EINVAL.
300 *
301 * @iommu: the IOMMUMemoryRegion
302 * @attr: attribute being queried
303 * @data: memory to fill in with the attribute data
304 *
305 * Returns 0 on success, or a negative errno; in particular
306 * returns -EINVAL for unrecognized or unimplemented attribute types.
307 */
308 int (*get_attr)(IOMMUMemoryRegion *iommu, enum IOMMUMemoryRegionAttr attr,
Alexey Kardashevskiyf1334de2018-02-06 11:08:24 -0700309 void *data);
Peter Maydell21f40202018-06-15 14:57:15 +0100310
311 /* Return the IOMMU index to use for a given set of transaction attributes.
312 *
313 * Optional method: if an IOMMU only supports a single IOMMU index then
314 * the default implementation of memory_region_iommu_attrs_to_index()
315 * will return 0.
316 *
317 * The indexes supported by an IOMMU must be contiguous, starting at 0.
318 *
319 * @iommu: the IOMMUMemoryRegion
320 * @attrs: memory transaction attributes
321 */
322 int (*attrs_to_index)(IOMMUMemoryRegion *iommu, MemTxAttrs attrs);
323
324 /* Return the number of IOMMU indexes this IOMMU supports.
325 *
326 * Optional method: if this method is not provided, then
327 * memory_region_iommu_num_indexes() will return 1, indicating that
328 * only a single IOMMU index is supported.
329 *
330 * @iommu: the IOMMUMemoryRegion
331 */
332 int (*num_indexes)(IOMMUMemoryRegion *iommu);
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000333} IOMMUMemoryRegionClass;
Avi Kivity30951152012-10-30 13:47:46 +0200334
Avi Kivity093bc2c2011-07-26 14:26:01 +0300335typedef struct CoalescedMemoryRange CoalescedMemoryRange;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300336typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300337
338struct MemoryRegion {
Peter Crosthwaiteb4fefef2014-06-05 23:15:52 -0700339 Object parent_obj;
Paolo Bonzinia6768542015-12-09 11:40:14 +0100340
Avi Kivity093bc2c2011-07-26 14:26:01 +0300341 /* All fields are private - violators will be prosecuted */
Paolo Bonzinia6768542015-12-09 11:40:14 +0100342
343 /* The following fields should fit in a cache line */
344 bool romd_mode;
345 bool ram;
346 bool subpage;
347 bool readonly; /* For RAM regions */
348 bool rom_device;
349 bool flush_coalesced_mmio;
350 bool global_locking;
351 uint8_t dirty_log_mask;
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000352 bool is_iommu;
Gonglei58eaa212016-02-22 16:34:55 +0800353 RAMBlock *ram_block;
Paolo Bonzini612263c2015-12-09 11:44:25 +0100354 Object *owner;
Paolo Bonzinia6768542015-12-09 11:40:14 +0100355
356 const MemoryRegionOps *ops;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300357 void *opaque;
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +0200358 MemoryRegion *container;
Avi Kivity08dafab2011-10-16 13:19:17 +0200359 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200360 hwaddr addr;
Avi Kivity545e92e2011-08-08 19:58:48 +0300361 void (*destructor)(MemoryRegion *mr);
Igor Mammedova2b257d2014-10-31 16:38:37 +0000362 uint64_t align;
Avi Kivity14a3c102011-07-26 14:26:06 +0300363 bool terminates;
Alex Williamson21e00fa2016-10-31 09:53:03 -0600364 bool ram_device;
Avi Kivity6bba19b2011-09-14 11:54:58 +0300365 bool enabled;
Jan Kiszka1660e722011-10-23 16:01:19 +0200366 bool warning_printed; /* For reservations */
Paolo Bonzinideb809e2015-07-14 13:56:53 +0200367 uint8_t vga_logging_count;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300368 MemoryRegion *alias;
Avi Kivitya8170e52012-10-23 12:30:10 +0200369 hwaddr alias_offset;
Peter Crosthwaited33382d2014-06-05 23:17:01 -0700370 int32_t priority;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300371 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
372 QTAILQ_ENTRY(MemoryRegion) subregions_link;
373 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
Peter Maydell302fa282014-08-19 20:05:46 +0100374 const char *name;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300375 unsigned ioeventfd_nb;
376 MemoryRegionIoeventfd *ioeventfds;
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000377};
378
379struct IOMMUMemoryRegion {
380 MemoryRegion parent_obj;
381
Peter Xucdb30812016-09-23 13:02:26 +0800382 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
Peter Xu5bf3d312016-09-23 13:02:27 +0800383 IOMMUNotifierFlag iommu_notify_flags;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300384};
385
Peter Xu512fa402017-04-07 18:59:08 +0800386#define IOMMU_NOTIFIER_FOREACH(n, mr) \
387 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
388
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200389/**
390 * MemoryListener: callbacks structure for updates to the physical memory map
391 *
392 * Allows a component to adjust to changes in the guest-visible memory map.
393 * Use with memory_listener_register() and memory_listener_unregister().
394 */
395struct MemoryListener {
396 void (*begin)(MemoryListener *listener);
397 void (*commit)(MemoryListener *listener);
398 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
399 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
400 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
Paolo Bonzinib2dfd712015-04-25 14:38:30 +0200401 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
402 int old, int new);
403 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
404 int old, int new);
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200405 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
406 void (*log_global_start)(MemoryListener *listener);
407 void (*log_global_stop)(MemoryListener *listener);
408 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
409 bool match_data, uint64_t data, EventNotifier *e);
410 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
411 bool match_data, uint64_t data, EventNotifier *e);
412 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
413 hwaddr addr, hwaddr len);
414 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
415 hwaddr addr, hwaddr len);
416 /* Lower = earlier (during add), later (during del) */
417 unsigned priority;
Paolo Bonzinid45fa782016-09-22 16:11:54 +0200418 AddressSpace *address_space;
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200419 QTAILQ_ENTRY(MemoryListener) link;
Paolo Bonzini9a546352016-09-22 16:23:06 +0200420 QTAILQ_ENTRY(MemoryListener) link_as;
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200421};
422
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200423/**
424 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
425 */
426struct AddressSpace {
427 /* All fields are private. */
Paolo Bonzini374f2982013-05-17 12:37:03 +0200428 struct rcu_head rcu;
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +0000429 char *name;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200430 MemoryRegion *root;
Paolo Bonzini374f2982013-05-17 12:37:03 +0200431
432 /* Accessed via RCU. */
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200433 struct FlatView *current_map;
Paolo Bonzini374f2982013-05-17 12:37:03 +0200434
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200435 int ioeventfd_nb;
436 struct MemoryRegionIoeventfd *ioeventfds;
Paolo Bonzini9a546352016-09-22 16:23:06 +0200437 QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners;
Avi Kivity0d673e32012-10-02 15:28:50 +0200438 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200439};
440
Paolo Bonzini785a5072018-03-05 00:31:20 +0100441typedef struct AddressSpaceDispatch AddressSpaceDispatch;
442typedef struct FlatRange FlatRange;
443
444/* Flattened global view of current active memory hierarchy. Kept in sorted
445 * order.
446 */
447struct FlatView {
448 struct rcu_head rcu;
449 unsigned ref;
450 FlatRange *ranges;
451 unsigned nr;
452 unsigned nr_allocated;
453 struct AddressSpaceDispatch *dispatch;
454 MemoryRegion *root;
455};
456
457static inline FlatView *address_space_to_flatview(AddressSpace *as)
458{
459 return atomic_rcu_read(&as->current_map);
460}
461
Alexey Kardashevskiy16620682017-09-21 18:50:58 +1000462
Avi Kivitye2177952011-12-08 15:00:18 +0200463/**
464 * MemoryRegionSection: describes a fragment of a #MemoryRegion
465 *
466 * @mr: the region, or %NULL if empty
Jay Zhou57914ec2018-01-04 13:29:48 +0800467 * @fv: the flat view of the address space the region is mapped in
Avi Kivitye2177952011-12-08 15:00:18 +0200468 * @offset_within_region: the beginning of the section, relative to @mr's start
469 * @size: the size of the section; will not exceed @mr's boundaries
470 * @offset_within_address_space: the address of the first byte of the section
471 * relative to the region's address space
Avi Kivity7a8499e2012-02-08 17:01:23 +0200472 * @readonly: writes to this section are ignored
Avi Kivitye2177952011-12-08 15:00:18 +0200473 */
474struct MemoryRegionSection {
475 MemoryRegion *mr;
Alexey Kardashevskiy16620682017-09-21 18:50:58 +1000476 FlatView *fv;
Avi Kivitya8170e52012-10-23 12:30:10 +0200477 hwaddr offset_within_region;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200478 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200479 hwaddr offset_within_address_space;
Avi Kivity7a8499e2012-02-08 17:01:23 +0200480 bool readonly;
Avi Kivitye2177952011-12-08 15:00:18 +0200481};
482
Avi Kivity093bc2c2011-07-26 14:26:01 +0300483/**
484 * memory_region_init: Initialize a memory region
485 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300486 * The region typically acts as a container for other memory regions. Use
Avi Kivity093bc2c2011-07-26 14:26:01 +0300487 * memory_region_add_subregion() to add subregions.
488 *
489 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400490 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300491 * @name: used for debugging; not visible to the user or ABI
492 * @size: size of the region; any subregions beyond this size will be clipped
493 */
494void memory_region_init(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400495 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300496 const char *name,
497 uint64_t size);
Paolo Bonzini46637be2013-05-07 09:06:00 +0200498
499/**
500 * memory_region_ref: Add 1 to a memory region's reference count
501 *
502 * Whenever memory regions are accessed outside the BQL, they need to be
503 * preserved against hot-unplug. MemoryRegions actually do not have their
504 * own reference count; they piggyback on a QOM object, their "owner".
505 * This function adds a reference to the owner.
506 *
507 * All MemoryRegions must have an owner if they can disappear, even if the
508 * device they belong to operates exclusively under the BQL. This is because
509 * the region could be returned at any time by memory_region_find, and this
510 * is usually under guest control.
511 *
512 * @mr: the #MemoryRegion
513 */
514void memory_region_ref(MemoryRegion *mr);
515
516/**
517 * memory_region_unref: Remove 1 to a memory region's reference count
518 *
519 * Whenever memory regions are accessed outside the BQL, they need to be
520 * preserved against hot-unplug. MemoryRegions actually do not have their
521 * own reference count; they piggyback on a QOM object, their "owner".
522 * This function removes a reference to the owner and possibly destroys it.
523 *
524 * @mr: the #MemoryRegion
525 */
526void memory_region_unref(MemoryRegion *mr);
527
Avi Kivity093bc2c2011-07-26 14:26:01 +0300528/**
529 * memory_region_init_io: Initialize an I/O memory region.
530 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300531 * Accesses into the region will cause the callbacks in @ops to be called.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300532 * if @size is nonzero, subregions will be clipped to @size.
533 *
534 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400535 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300536 * @ops: a structure containing read and write callbacks to be used when
537 * I/O is performed on the region.
Daniel P. Berrangeb6af0972015-08-26 12:17:13 +0100538 * @opaque: passed to the read and write callbacks of the @ops structure.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300539 * @name: used for debugging; not visible to the user or ABI
540 * @size: size of the region.
541 */
542void memory_region_init_io(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400543 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300544 const MemoryRegionOps *ops,
545 void *opaque,
546 const char *name,
547 uint64_t size);
548
549/**
Peter Maydell1cfe48c2017-07-07 15:42:49 +0100550 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
551 * into the region will modify memory
552 * directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300553 *
554 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400555 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000556 * @name: Region name, becomes part of RAMBlock name used in migration stream
557 * must be unique within any device
Avi Kivity093bc2c2011-07-26 14:26:01 +0300558 * @size: size of the region.
Hu Tao49946532014-09-09 13:27:55 +0800559 * @errp: pointer to Error*, to store an error if it happens.
Peter Maydella5c02342017-07-07 15:42:48 +0100560 *
561 * Note that this function does not do anything to cause the data in the
562 * RAM memory region to be migrated; that is the responsibility of the caller.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300563 */
Peter Maydell1cfe48c2017-07-07 15:42:49 +0100564void memory_region_init_ram_nomigrate(MemoryRegion *mr,
565 struct Object *owner,
566 const char *name,
567 uint64_t size,
568 Error **errp);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300569
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200570/**
Marcel Apfelbaum06329cc2017-12-13 16:37:37 +0200571 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
572 * Accesses into the region will
573 * modify memory directly.
574 *
575 * @mr: the #MemoryRegion to be initialized.
576 * @owner: the object that tracks the region's reference count
577 * @name: Region name, becomes part of RAMBlock name used in migration stream
578 * must be unique within any device
579 * @size: size of the region.
580 * @share: allow remapping RAM to different addresses
581 * @errp: pointer to Error*, to store an error if it happens.
582 *
583 * Note that this function is similar to memory_region_init_ram_nomigrate.
584 * The only difference is part of the RAM region can be remapped.
585 */
586void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
587 struct Object *owner,
588 const char *name,
589 uint64_t size,
590 bool share,
591 Error **errp);
592
593/**
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200594 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
595 * RAM. Accesses into the region will
596 * modify memory directly. Only an initial
597 * portion of this RAM is actually used.
598 * The used size can change across reboots.
599 *
600 * @mr: the #MemoryRegion to be initialized.
601 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000602 * @name: Region name, becomes part of RAMBlock name used in migration stream
603 * must be unique within any device
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200604 * @size: used size of the region.
605 * @max_size: max size of the region.
606 * @resized: callback to notify owner about used size change.
607 * @errp: pointer to Error*, to store an error if it happens.
Peter Maydella5c02342017-07-07 15:42:48 +0100608 *
609 * Note that this function does not do anything to cause the data in the
610 * RAM memory region to be migrated; that is the responsibility of the caller.
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200611 */
612void memory_region_init_resizeable_ram(MemoryRegion *mr,
613 struct Object *owner,
614 const char *name,
615 uint64_t size,
616 uint64_t max_size,
617 void (*resized)(const char*,
618 uint64_t length,
619 void *host),
620 Error **errp);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800621#ifdef __linux__
622/**
623 * memory_region_init_ram_from_file: Initialize RAM memory region with a
624 * mmap-ed backend.
625 *
626 * @mr: the #MemoryRegion to be initialized.
627 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000628 * @name: Region name, becomes part of RAMBlock name used in migration stream
629 * must be unique within any device
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800630 * @size: size of the region.
Haozhong Zhang98376842017-12-11 15:28:04 +0800631 * @align: alignment of the region base address; if 0, the default alignment
632 * (getpagesize()) will be used.
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800633 * @share: %true if memory must be mmaped with the MAP_SHARED flag
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800634 * @path: the path in which to allocate the RAM.
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800635 * @errp: pointer to Error*, to store an error if it happens.
Peter Maydella5c02342017-07-07 15:42:48 +0100636 *
637 * Note that this function does not do anything to cause the data in the
638 * RAM memory region to be migrated; that is the responsibility of the caller.
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800639 */
640void memory_region_init_ram_from_file(MemoryRegion *mr,
641 struct Object *owner,
642 const char *name,
643 uint64_t size,
Haozhong Zhang98376842017-12-11 15:28:04 +0800644 uint64_t align,
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800645 bool share,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800646 const char *path,
647 Error **errp);
Marc-André Lureaufea617c2017-06-02 18:12:24 +0400648
649/**
650 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
651 * mmap-ed backend.
652 *
653 * @mr: the #MemoryRegion to be initialized.
654 * @owner: the object that tracks the region's reference count
655 * @name: the name of the region.
656 * @size: size of the region.
657 * @share: %true if memory must be mmaped with the MAP_SHARED flag
658 * @fd: the fd to mmap.
659 * @errp: pointer to Error*, to store an error if it happens.
Peter Maydella5c02342017-07-07 15:42:48 +0100660 *
661 * Note that this function does not do anything to cause the data in the
662 * RAM memory region to be migrated; that is the responsibility of the caller.
Marc-André Lureaufea617c2017-06-02 18:12:24 +0400663 */
664void memory_region_init_ram_from_fd(MemoryRegion *mr,
665 struct Object *owner,
666 const char *name,
667 uint64_t size,
668 bool share,
669 int fd,
670 Error **errp);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800671#endif
672
Avi Kivity093bc2c2011-07-26 14:26:01 +0300673/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +0200674 * memory_region_init_ram_ptr: Initialize RAM memory region from a
675 * user-provided pointer. Accesses into the
676 * region will modify memory directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300677 *
678 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400679 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000680 * @name: Region name, becomes part of RAMBlock name used in migration stream
681 * must be unique within any device
Avi Kivity093bc2c2011-07-26 14:26:01 +0300682 * @size: size of the region.
683 * @ptr: memory to be mapped; must contain at least @size bytes.
Peter Maydella5c02342017-07-07 15:42:48 +0100684 *
685 * Note that this function does not do anything to cause the data in the
686 * RAM memory region to be migrated; that is the responsibility of the caller.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300687 */
688void memory_region_init_ram_ptr(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400689 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300690 const char *name,
691 uint64_t size,
692 void *ptr);
693
694/**
Alex Williamson21e00fa2016-10-31 09:53:03 -0600695 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
696 * a user-provided pointer.
697 *
698 * A RAM device represents a mapping to a physical device, such as to a PCI
699 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
700 * into the VM address space and access to the region will modify memory
701 * directly. However, the memory region should not be included in a memory
702 * dump (device may not be enabled/mapped at the time of the dump), and
703 * operations incompatible with manipulating MMIO should be avoided. Replaces
704 * skip_dump flag.
705 *
706 * @mr: the #MemoryRegion to be initialized.
707 * @owner: the object that tracks the region's reference count
708 * @name: the name of the region.
709 * @size: size of the region.
710 * @ptr: memory to be mapped; must contain at least @size bytes.
Peter Maydella5c02342017-07-07 15:42:48 +0100711 *
712 * Note that this function does not do anything to cause the data in the
713 * RAM memory region to be migrated; that is the responsibility of the caller.
714 * (For RAM device memory regions, migrating the contents rarely makes sense.)
Alex Williamson21e00fa2016-10-31 09:53:03 -0600715 */
716void memory_region_init_ram_device_ptr(MemoryRegion *mr,
717 struct Object *owner,
718 const char *name,
719 uint64_t size,
720 void *ptr);
721
722/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300723 * memory_region_init_alias: Initialize a memory region that aliases all or a
724 * part of another memory region.
725 *
726 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400727 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300728 * @name: used for debugging; not visible to the user or ABI
729 * @orig: the region to be referenced; @mr will be equivalent to
730 * @orig between @offset and @offset + @size - 1.
731 * @offset: start of the section in @orig to be referenced.
732 * @size: size of the region.
733 */
734void memory_region_init_alias(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400735 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300736 const char *name,
737 MemoryRegion *orig,
Avi Kivitya8170e52012-10-23 12:30:10 +0200738 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300739 uint64_t size);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300740
741/**
Peter Maydellb59821a2017-07-07 15:42:50 +0100742 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
Peter Maydella1777f72016-07-04 13:06:35 +0100743 *
Peter Maydellb59821a2017-07-07 15:42:50 +0100744 * This has the same effect as calling memory_region_init_ram_nomigrate()
Peter Maydella1777f72016-07-04 13:06:35 +0100745 * and then marking the resulting region read-only with
746 * memory_region_set_readonly().
747 *
Peter Maydellb59821a2017-07-07 15:42:50 +0100748 * Note that this function does not do anything to cause the data in the
749 * RAM side of the memory region to be migrated; that is the responsibility
750 * of the caller.
751 *
Peter Maydella1777f72016-07-04 13:06:35 +0100752 * @mr: the #MemoryRegion to be initialized.
753 * @owner: the object that tracks the region's reference count
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000754 * @name: Region name, becomes part of RAMBlock name used in migration stream
755 * must be unique within any device
Peter Maydella1777f72016-07-04 13:06:35 +0100756 * @size: size of the region.
757 * @errp: pointer to Error*, to store an error if it happens.
758 */
Peter Maydellb59821a2017-07-07 15:42:50 +0100759void memory_region_init_rom_nomigrate(MemoryRegion *mr,
760 struct Object *owner,
761 const char *name,
762 uint64_t size,
763 Error **errp);
Peter Maydella1777f72016-07-04 13:06:35 +0100764
765/**
Peter Maydellb59821a2017-07-07 15:42:50 +0100766 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
767 * Writes are handled via callbacks.
768 *
769 * Note that this function does not do anything to cause the data in the
770 * RAM side of the memory region to be migrated; that is the responsibility
771 * of the caller.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300772 *
773 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400774 * @owner: the object that tracks the region's reference count
Peter Maydell39e0b032016-07-04 13:06:35 +0100775 * @ops: callbacks for write access handling (must not be NULL).
Jay Zhou57914ec2018-01-04 13:29:48 +0800776 * @opaque: passed to the read and write callbacks of the @ops structure.
Dr. David Alan Gilberte8f5fe22017-03-09 15:27:08 +0000777 * @name: Region name, becomes part of RAMBlock name used in migration stream
778 * must be unique within any device
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300779 * @size: size of the region.
Hu Tao33e0eb52014-09-09 13:27:57 +0800780 * @errp: pointer to Error*, to store an error if it happens.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300781 */
Peter Maydellb59821a2017-07-07 15:42:50 +0100782void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
783 struct Object *owner,
784 const MemoryRegionOps *ops,
785 void *opaque,
786 const char *name,
787 uint64_t size,
788 Error **errp);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300789
Avi Kivity093bc2c2011-07-26 14:26:01 +0300790/**
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000791 * memory_region_init_iommu: Initialize a memory region of a custom type
792 * that translates addresses
Avi Kivity30951152012-10-30 13:47:46 +0200793 *
794 * An IOMMU region translates addresses and forwards accesses to a target
795 * memory region.
796 *
Peter Maydell2ce931d2018-05-31 14:50:52 +0100797 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
798 * @_iommu_mr should be a pointer to enough memory for an instance of
799 * that subclass, @instance_size is the size of that subclass, and
800 * @mrtypename is its name. This function will initialize @_iommu_mr as an
801 * instance of the subclass, and its methods will then be called to handle
802 * accesses to the memory region. See the documentation of
803 * #IOMMUMemoryRegionClass for further details.
804 *
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000805 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
806 * @instance_size: the IOMMUMemoryRegion subclass instance size
Jay Zhou57914ec2018-01-04 13:29:48 +0800807 * @mrtypename: the type name of the #IOMMUMemoryRegion
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400808 * @owner: the object that tracks the region's reference count
Avi Kivity30951152012-10-30 13:47:46 +0200809 * @name: used for debugging; not visible to the user or ABI
810 * @size: size of the region.
811 */
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000812void memory_region_init_iommu(void *_iommu_mr,
813 size_t instance_size,
814 const char *mrtypename,
815 Object *owner,
Avi Kivity30951152012-10-30 13:47:46 +0200816 const char *name,
817 uint64_t size);
818
Jan Kiszka1660e722011-10-23 16:01:19 +0200819/**
Peter Maydellb08199c2017-07-07 15:42:51 +0100820 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
821 * region will modify memory directly.
822 *
823 * @mr: the #MemoryRegion to be initialized
824 * @owner: the object that tracks the region's reference count (must be
825 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
826 * @name: name of the memory region
827 * @size: size of the region in bytes
828 * @errp: pointer to Error*, to store an error if it happens.
829 *
830 * This function allocates RAM for a board model or device, and
831 * arranges for it to be migrated (by calling vmstate_register_ram()
832 * if @owner is a DeviceState, or vmstate_register_ram_global() if
833 * @owner is NULL).
834 *
835 * TODO: Currently we restrict @owner to being either NULL (for
836 * global RAM regions with no owner) or devices, so that we can
837 * give the RAM block a unique name for migration purposes.
838 * We should lift this restriction and allow arbitrary Objects.
839 * If you pass a non-NULL non-device @owner then we will assert.
840 */
841void memory_region_init_ram(MemoryRegion *mr,
842 struct Object *owner,
843 const char *name,
844 uint64_t size,
845 Error **errp);
846
847/**
848 * memory_region_init_rom: Initialize a ROM memory region.
849 *
850 * This has the same effect as calling memory_region_init_ram()
851 * and then marking the resulting region read-only with
852 * memory_region_set_readonly(). This includes arranging for the
853 * contents to be migrated.
854 *
855 * TODO: Currently we restrict @owner to being either NULL (for
856 * global RAM regions with no owner) or devices, so that we can
857 * give the RAM block a unique name for migration purposes.
858 * We should lift this restriction and allow arbitrary Objects.
859 * If you pass a non-NULL non-device @owner then we will assert.
860 *
861 * @mr: the #MemoryRegion to be initialized.
862 * @owner: the object that tracks the region's reference count
863 * @name: Region name, becomes part of RAMBlock name used in migration stream
864 * must be unique within any device
865 * @size: size of the region.
866 * @errp: pointer to Error*, to store an error if it happens.
867 */
868void memory_region_init_rom(MemoryRegion *mr,
869 struct Object *owner,
870 const char *name,
871 uint64_t size,
872 Error **errp);
873
874/**
875 * memory_region_init_rom_device: Initialize a ROM memory region.
876 * Writes are handled via callbacks.
877 *
878 * This function initializes a memory region backed by RAM for reads
879 * and callbacks for writes, and arranges for the RAM backing to
880 * be migrated (by calling vmstate_register_ram()
881 * if @owner is a DeviceState, or vmstate_register_ram_global() if
882 * @owner is NULL).
883 *
884 * TODO: Currently we restrict @owner to being either NULL (for
885 * global RAM regions with no owner) or devices, so that we can
886 * give the RAM block a unique name for migration purposes.
887 * We should lift this restriction and allow arbitrary Objects.
888 * If you pass a non-NULL non-device @owner then we will assert.
889 *
890 * @mr: the #MemoryRegion to be initialized.
891 * @owner: the object that tracks the region's reference count
892 * @ops: callbacks for write access handling (must not be NULL).
893 * @name: Region name, becomes part of RAMBlock name used in migration stream
894 * must be unique within any device
895 * @size: size of the region.
896 * @errp: pointer to Error*, to store an error if it happens.
897 */
898void memory_region_init_rom_device(MemoryRegion *mr,
899 struct Object *owner,
900 const MemoryRegionOps *ops,
901 void *opaque,
902 const char *name,
903 uint64_t size,
904 Error **errp);
905
906
907/**
Paolo Bonzini803c0812013-05-07 06:59:09 +0200908 * memory_region_owner: get a memory region's owner.
909 *
910 * @mr: the memory region being queried.
911 */
912struct Object *memory_region_owner(MemoryRegion *mr);
913
914/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300915 * memory_region_size: get a memory region's size.
916 *
917 * @mr: the memory region being queried.
918 */
919uint64_t memory_region_size(MemoryRegion *mr);
920
921/**
Avi Kivity8ea92522011-12-08 15:58:43 +0200922 * memory_region_is_ram: check whether a memory region is random access
923 *
924 * Returns %true is a memory region is random access.
925 *
926 * @mr: the memory region being queried
927 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100928static inline bool memory_region_is_ram(MemoryRegion *mr)
929{
930 return mr->ram;
931}
Avi Kivity8ea92522011-12-08 15:58:43 +0200932
933/**
Alex Williamson21e00fa2016-10-31 09:53:03 -0600934 * memory_region_is_ram_device: check whether a memory region is a ram device
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530935 *
Alex Williamson21e00fa2016-10-31 09:53:03 -0600936 * Returns %true is a memory region is a device backed ram region
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530937 *
938 * @mr: the memory region being queried
939 */
Alex Williamson21e00fa2016-10-31 09:53:03 -0600940bool memory_region_is_ram_device(MemoryRegion *mr);
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530941
942/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200943 * memory_region_is_romd: check whether a memory region is in ROMD mode
Blue Swirlfd062572012-04-09 17:38:52 +0000944 *
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200945 * Returns %true if a memory region is a ROM device and currently set to allow
Blue Swirlfd062572012-04-09 17:38:52 +0000946 * direct reads.
947 *
948 * @mr: the memory region being queried
949 */
950static inline bool memory_region_is_romd(MemoryRegion *mr)
951{
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200952 return mr->rom_device && mr->romd_mode;
Blue Swirlfd062572012-04-09 17:38:52 +0000953}
954
955/**
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000956 * memory_region_get_iommu: check whether a memory region is an iommu
Avi Kivity30951152012-10-30 13:47:46 +0200957 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000958 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
959 * otherwise NULL.
Avi Kivity30951152012-10-30 13:47:46 +0200960 *
961 * @mr: the memory region being queried
962 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000963static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100964{
Jason Wang12d37882016-12-30 18:09:18 +0800965 if (mr->alias) {
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000966 return memory_region_get_iommu(mr->alias);
Jason Wang12d37882016-12-30 18:09:18 +0800967 }
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000968 if (mr->is_iommu) {
969 return (IOMMUMemoryRegion *) mr;
970 }
971 return NULL;
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100972}
973
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000974/**
975 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
976 * if an iommu or NULL if not
977 *
Jay Zhou57914ec2018-01-04 13:29:48 +0800978 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
979 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +1000980 *
981 * @mr: the memory region being queried
982 */
983static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
984 IOMMUMemoryRegion *iommu_mr)
985{
986 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
987}
988
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000989#define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
Avi Kivity30951152012-10-30 13:47:46 +0200990
991/**
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +1000992 * memory_region_iommu_get_min_page_size: get minimum supported page size
993 * for an iommu
994 *
995 * Returns minimum supported page size for an iommu.
996 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000997 * @iommu_mr: the memory region being queried
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +1000998 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +1000999uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +10001000
1001/**
David Gibson06866572013-05-14 19:13:56 +10001002 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1003 *
Peter Xucdb30812016-09-23 13:02:26 +08001004 * The notification type will be decided by entry.perm bits:
1005 *
1006 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
1007 * - For MAP (newly added entry) notifies: set entry.perm to the
1008 * permission of the page (which is definitely !IOMMU_NONE).
1009 *
1010 * Note: for any IOMMU implementation, an in-place mapping change
1011 * should be notified with an UNMAP followed by a MAP.
1012 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001013 * @iommu_mr: the memory region that was changed
Peter Maydellcb1efcf2018-06-15 14:57:16 +01001014 * @iommu_idx: the IOMMU index for the translation table which has changed
David Gibson06866572013-05-14 19:13:56 +10001015 * @entry: the new entry in the IOMMU translation table. The entry
1016 * replaces all old entries for the same virtual I/O address range.
1017 * Deleted entries have .@perm == 0.
1018 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001019void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
Peter Maydellcb1efcf2018-06-15 14:57:16 +01001020 int iommu_idx,
David Gibson06866572013-05-14 19:13:56 +10001021 IOMMUTLBEntry entry);
1022
1023/**
Peter Xubd2bfa42017-04-07 18:59:10 +08001024 * memory_region_notify_one: notify a change in an IOMMU translation
1025 * entry to a single notifier
1026 *
1027 * This works just like memory_region_notify_iommu(), but it only
1028 * notifies a specific notifier, not all of them.
1029 *
1030 * @notifier: the notifier to be notified
1031 * @entry: the new entry in the IOMMU translation table. The entry
1032 * replaces all old entries for the same virtual I/O address range.
1033 * Deleted entries have .@perm == 0.
1034 */
1035void memory_region_notify_one(IOMMUNotifier *notifier,
1036 IOMMUTLBEntry *entry);
1037
1038/**
David Gibson06866572013-05-14 19:13:56 +10001039 * memory_region_register_iommu_notifier: register a notifier for changes to
1040 * IOMMU translation entries.
1041 *
1042 * @mr: the memory region to observe
Peter Xucdb30812016-09-23 13:02:26 +08001043 * @n: the IOMMUNotifier to be added; the notify callback receives a
1044 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1045 * ceases to be valid on exit from the notifier.
David Gibson06866572013-05-14 19:13:56 +10001046 */
Peter Xucdb30812016-09-23 13:02:26 +08001047void memory_region_register_iommu_notifier(MemoryRegion *mr,
1048 IOMMUNotifier *n);
David Gibson06866572013-05-14 19:13:56 +10001049
1050/**
David Gibsona788f222015-09-30 12:13:55 +10001051 * memory_region_iommu_replay: replay existing IOMMU translations to
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +10001052 * a notifier with the minimum page granularity returned by
1053 * mr->iommu_ops->get_page_size().
David Gibsona788f222015-09-30 12:13:55 +10001054 *
Peter Maydell2ce931d2018-05-31 14:50:52 +01001055 * Note: this is not related to record-and-replay functionality.
1056 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001057 * @iommu_mr: the memory region to observe
David Gibsona788f222015-09-30 12:13:55 +10001058 * @n: the notifier to which to replay iommu mappings
David Gibsona788f222015-09-30 12:13:55 +10001059 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001060void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
David Gibsona788f222015-09-30 12:13:55 +10001061
1062/**
Peter Xude472e42017-04-07 18:59:09 +08001063 * memory_region_iommu_replay_all: replay existing IOMMU translations
1064 * to all the notifiers registered.
1065 *
Peter Maydell2ce931d2018-05-31 14:50:52 +01001066 * Note: this is not related to record-and-replay functionality.
1067 *
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001068 * @iommu_mr: the memory region to observe
Peter Xude472e42017-04-07 18:59:09 +08001069 */
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001070void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr);
Peter Xude472e42017-04-07 18:59:09 +08001071
1072/**
David Gibson06866572013-05-14 19:13:56 +10001073 * memory_region_unregister_iommu_notifier: unregister a notifier for
1074 * changes to IOMMU translation entries.
1075 *
Alexey Kardashevskiyd22d8952016-06-30 13:00:23 -06001076 * @mr: the memory region which was observed and for which notity_stopped()
1077 * needs to be called
David Gibson06866572013-05-14 19:13:56 +10001078 * @n: the notifier to be removed.
1079 */
Peter Xucdb30812016-09-23 13:02:26 +08001080void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1081 IOMMUNotifier *n);
David Gibson06866572013-05-14 19:13:56 +10001082
1083/**
Alexey Kardashevskiyf1334de2018-02-06 11:08:24 -07001084 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1085 * defined on the IOMMU.
1086 *
Peter Maydell2ce931d2018-05-31 14:50:52 +01001087 * Returns 0 on success, or a negative errno otherwise. In particular,
1088 * -EINVAL indicates that the IOMMU does not support the requested
1089 * attribute.
Alexey Kardashevskiyf1334de2018-02-06 11:08:24 -07001090 *
1091 * @iommu_mr: the memory region
1092 * @attr: the requested attribute
1093 * @data: a pointer to the requested attribute data
1094 */
1095int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1096 enum IOMMUMemoryRegionAttr attr,
1097 void *data);
1098
1099/**
Peter Maydell21f40202018-06-15 14:57:15 +01001100 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1101 * use for translations with the given memory transaction attributes.
1102 *
1103 * @iommu_mr: the memory region
1104 * @attrs: the memory transaction attributes
1105 */
1106int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
1107 MemTxAttrs attrs);
1108
1109/**
1110 * memory_region_iommu_num_indexes: return the total number of IOMMU
1111 * indexes that this IOMMU supports.
1112 *
1113 * @iommu_mr: the memory region
1114 */
1115int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr);
1116
1117/**
Avi Kivity8991c792011-12-20 15:53:11 +02001118 * memory_region_name: get a memory region's name
1119 *
1120 * Returns the string that was used to initialize the memory region.
1121 *
1122 * @mr: the memory region being queried
1123 */
Peter Crosthwaite5d546d42014-08-14 23:55:03 -07001124const char *memory_region_name(const MemoryRegion *mr);
Avi Kivity8991c792011-12-20 15:53:11 +02001125
1126/**
Avi Kivity55043ba2011-12-15 17:20:34 +02001127 * memory_region_is_logging: return whether a memory region is logging writes
1128 *
Paolo Bonzini2d1a35b2015-03-23 10:50:57 +01001129 * Returns %true if the memory region is logging writes for the given client
1130 *
1131 * @mr: the memory region being queried
1132 * @client: the client being queried
1133 */
1134bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
1135
1136/**
1137 * memory_region_get_dirty_log_mask: return the clients for which a
1138 * memory region is logging writes.
1139 *
Paolo Bonzini677e7802015-03-23 10:53:21 +01001140 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1141 * are the bit indices.
Avi Kivity55043ba2011-12-15 17:20:34 +02001142 *
1143 * @mr: the memory region being queried
1144 */
Paolo Bonzini2d1a35b2015-03-23 10:50:57 +01001145uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
Avi Kivity55043ba2011-12-15 17:20:34 +02001146
1147/**
Avi Kivityce7923d2011-12-08 16:05:11 +02001148 * memory_region_is_rom: check whether a memory region is ROM
1149 *
1150 * Returns %true is a memory region is read-only memory.
1151 *
1152 * @mr: the memory region being queried
1153 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +01001154static inline bool memory_region_is_rom(MemoryRegion *mr)
1155{
1156 return mr->ram && mr->readonly;
1157}
1158
Avi Kivityce7923d2011-12-08 16:05:11 +02001159
1160/**
Paolo Bonzinia35ba7b2014-06-10 19:15:23 +08001161 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1162 *
1163 * Returns a file descriptor backing a file-based RAM memory region,
1164 * or -1 if the region is not a file-based RAM memory region.
1165 *
1166 * @mr: the RAM or alias memory region being queried.
1167 */
1168int memory_region_get_fd(MemoryRegion *mr);
1169
1170/**
Paolo Bonzini07bdaa42016-03-25 12:55:08 +01001171 * memory_region_from_host: Convert a pointer into a RAM memory region
1172 * and an offset within it.
1173 *
1174 * Given a host pointer inside a RAM memory region (created with
1175 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1176 * the MemoryRegion and the offset within it.
1177 *
1178 * Use with care; by the time this function returns, the returned pointer is
1179 * not protected by RCU anymore. If the caller is not within an RCU critical
1180 * section and does not hold the iothread lock, it must have other means of
1181 * protecting the pointer, such as a reference to the region that includes
1182 * the incoming ram_addr_t.
1183 *
Jay Zhou57914ec2018-01-04 13:29:48 +08001184 * @ptr: the host pointer to be converted
1185 * @offset: the offset within memory region
Paolo Bonzini07bdaa42016-03-25 12:55:08 +01001186 */
1187MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
1188
1189/**
Avi Kivity093bc2c2011-07-26 14:26:01 +03001190 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1191 *
1192 * Returns a host pointer to a RAM memory region (created with
Paolo Bonzini49b24af2015-12-16 10:30:47 +01001193 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1194 *
1195 * Use with care; by the time this function returns, the returned pointer is
1196 * not protected by RCU anymore. If the caller is not within an RCU critical
1197 * section and does not hold the iothread lock, it must have other means of
1198 * protecting the pointer, such as a reference to the region that includes
1199 * the incoming ram_addr_t.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001200 *
1201 * @mr: the memory region being queried.
1202 */
1203void *memory_region_get_ram_ptr(MemoryRegion *mr);
1204
Paolo Bonzini37d7c082015-03-23 10:21:46 +01001205/* memory_region_ram_resize: Resize a RAM region.
1206 *
1207 * Only legal before guest might have detected the memory size: e.g. on
1208 * incoming migration, or right after reset.
1209 *
1210 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1211 * @newsize: the new size the region
1212 * @errp: pointer to Error*, to store an error if it happens.
1213 */
1214void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
1215 Error **errp);
1216
Avi Kivity093bc2c2011-07-26 14:26:01 +03001217/**
Avi Kivity093bc2c2011-07-26 14:26:01 +03001218 * memory_region_set_log: Turn dirty logging on or off for a region.
1219 *
1220 * Turns dirty logging on or off for a specified client (display, migration).
1221 * Only meaningful for RAM regions.
1222 *
1223 * @mr: the memory region being updated.
1224 * @log: whether dirty logging is to be enabled or disabled.
Paolo Bonzinidbddac62015-03-23 10:31:53 +01001225 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001226 */
1227void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
1228
1229/**
Blue Swirlcd7a45c2012-01-22 16:38:21 +00001230 * memory_region_get_dirty: Check whether a range of bytes is dirty
1231 * for a specified client.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001232 *
Blue Swirlcd7a45c2012-01-22 16:38:21 +00001233 * Checks whether a range of bytes has been written to since the last
Avi Kivity093bc2c2011-07-26 14:26:01 +03001234 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1235 * must be enabled.
1236 *
1237 * @mr: the memory region being queried.
1238 * @addr: the address (relative to the start of the region) being queried.
Blue Swirlcd7a45c2012-01-22 16:38:21 +00001239 * @size: the size of the range being queried.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001240 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1241 * %DIRTY_MEMORY_VGA.
1242 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001243bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1244 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001245
1246/**
Blue Swirlfd4aa972011-10-16 16:04:59 +00001247 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001248 *
Blue Swirlfd4aa972011-10-16 16:04:59 +00001249 * Marks a range of bytes as dirty, after it has been dirtied outside
1250 * guest code.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001251 *
Blue Swirlfd4aa972011-10-16 16:04:59 +00001252 * @mr: the memory region being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001253 * @addr: the address (relative to the start of the region) being dirtied.
Blue Swirlfd4aa972011-10-16 16:04:59 +00001254 * @size: size of the range being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001255 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001256void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1257 hwaddr size);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001258
1259/**
Gerd Hoffmann8deaf122017-04-21 11:16:25 +02001260 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1261 * bitmap and clear it.
1262 *
1263 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1264 * returns the snapshot. The snapshot can then be used to query dirty
Paolo Bonzini77302fb2018-02-06 18:30:04 +01001265 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1266 * querying the same page multiple times, which is especially useful for
1267 * display updates where the scanlines often are not page aligned.
Gerd Hoffmann8deaf122017-04-21 11:16:25 +02001268 *
1269 * The dirty bitmap region which gets copyed into the snapshot (and
1270 * cleared afterwards) can be larger than requested. The boundaries
1271 * are rounded up/down so complete bitmap longs (covering 64 pages on
1272 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1273 * isn't a problem for display updates as the extra pages are outside
1274 * the visible area, and in case the visible area changes a full
1275 * display redraw is due anyway. Should other use cases for this
1276 * function emerge we might have to revisit this implementation
1277 * detail.
1278 *
1279 * Use g_free to release DirtyBitmapSnapshot.
1280 *
1281 * @mr: the memory region being queried.
1282 * @addr: the address (relative to the start of the region) being queried.
1283 * @size: the size of the range being queried.
1284 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1285 */
1286DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
1287 hwaddr addr,
1288 hwaddr size,
1289 unsigned client);
1290
1291/**
1292 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1293 * in the specified dirty bitmap snapshot.
1294 *
1295 * @mr: the memory region being queried.
1296 * @snap: the dirty bitmap snapshot
1297 * @addr: the address (relative to the start of the region) being queried.
1298 * @size: the size of the range being queried.
1299 */
1300bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
1301 DirtyBitmapSnapshot *snap,
1302 hwaddr addr, hwaddr size);
1303
Juan Quintela6c279db2012-10-17 20:24:28 +02001304/**
Avi Kivity093bc2c2011-07-26 14:26:01 +03001305 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1306 * client.
1307 *
1308 * Marks a range of pages as no longer dirty.
1309 *
1310 * @mr: the region being updated.
1311 * @addr: the start of the subrange being cleaned.
1312 * @size: the size of the subrange being cleaned.
1313 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1314 * %DIRTY_MEMORY_VGA.
1315 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001316void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1317 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001318
1319/**
1320 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1321 *
1322 * Allows a memory region to be marked as read-only (turning it into a ROM).
1323 * only useful on RAM regions.
1324 *
1325 * @mr: the region being updated.
1326 * @readonly: whether rhe region is to be ROM or RAM.
1327 */
1328void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
1329
1330/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +02001331 * memory_region_rom_device_set_romd: enable/disable ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001332 *
1333 * Allows a ROM device (initialized with memory_region_init_rom_device() to
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +02001334 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1335 * device is mapped to guest memory and satisfies read access directly.
1336 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1337 * Writes are always handled by the #MemoryRegion.write function.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001338 *
1339 * @mr: the memory region to be updated
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +02001340 * @romd_mode: %true to put the region into ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001341 */
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +02001342void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +03001343
1344/**
Avi Kivity093bc2c2011-07-26 14:26:01 +03001345 * memory_region_set_coalescing: Enable memory coalescing for the region.
1346 *
1347 * Enabled writes to a region to be queued for later processing. MMIO ->write
1348 * callbacks may be delayed until a non-coalesced MMIO is issued.
1349 * Only useful for IO regions. Roughly similar to write-combining hardware.
1350 *
1351 * @mr: the memory region to be write coalesced
1352 */
1353void memory_region_set_coalescing(MemoryRegion *mr);
1354
1355/**
1356 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1357 * a region.
1358 *
1359 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1360 * Multiple calls can be issued coalesced disjoint ranges.
1361 *
1362 * @mr: the memory region to be updated.
1363 * @offset: the start of the range within the region to be coalesced.
1364 * @size: the size of the subrange to be coalesced.
1365 */
1366void memory_region_add_coalescing(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001367 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +03001368 uint64_t size);
1369
1370/**
1371 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1372 *
1373 * Disables any coalescing caused by memory_region_set_coalescing() or
1374 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1375 * hardware.
1376 *
1377 * @mr: the memory region to be updated.
1378 */
1379void memory_region_clear_coalescing(MemoryRegion *mr);
1380
1381/**
Jan Kiszkad4105152012-08-23 13:02:29 +02001382 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1383 * accesses.
1384 *
1385 * Ensure that pending coalesced MMIO request are flushed before the memory
1386 * region is accessed. This property is automatically enabled for all regions
1387 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1388 *
1389 * @mr: the memory region to be updated.
1390 */
1391void memory_region_set_flush_coalesced(MemoryRegion *mr);
1392
1393/**
1394 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1395 * accesses.
1396 *
1397 * Clear the automatic coalesced MMIO flushing enabled via
1398 * memory_region_set_flush_coalesced. Note that this service has no effect on
1399 * memory regions that have MMIO coalescing enabled for themselves. For them,
1400 * automatic flushing will stop once coalescing is disabled.
1401 *
1402 * @mr: the memory region to be updated.
1403 */
1404void memory_region_clear_flush_coalesced(MemoryRegion *mr);
1405
1406/**
Jan Kiszka196ea132015-06-18 18:47:20 +02001407 * memory_region_clear_global_locking: Declares that access processing does
1408 * not depend on the QEMU global lock.
1409 *
1410 * By clearing this property, accesses to the memory region will be processed
1411 * outside of QEMU's global lock (unless the lock is held on when issuing the
1412 * access request). In this case, the device model implementing the access
1413 * handlers is responsible for synchronization of concurrency.
1414 *
1415 * @mr: the memory region to be updated.
1416 */
1417void memory_region_clear_global_locking(MemoryRegion *mr);
1418
1419/**
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001420 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1421 * is written to a location.
1422 *
1423 * Marks a word in an IO region (initialized with memory_region_init_io())
1424 * 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 -03001425 * The caller must be prepared to handle failure (that is, take the required
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001426 * action if the callback _is_ called).
1427 *
1428 * @mr: the memory region being updated.
1429 * @addr: the address within @mr that is to be monitored
1430 * @size: the size of the access to trigger the eventfd
1431 * @match_data: whether to match against @data, instead of just @addr
1432 * @data: the data to match against the guest write
Jay Zhou57914ec2018-01-04 13:29:48 +08001433 * @e: event notifier to be triggered when @addr, @size, and @data all match.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001434 **/
1435void memory_region_add_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001436 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001437 unsigned size,
1438 bool match_data,
1439 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +02001440 EventNotifier *e);
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001441
1442/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001443 * memory_region_del_eventfd: Cancel an eventfd.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001444 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001445 * Cancels an eventfd trigger requested by a previous
1446 * memory_region_add_eventfd() call.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001447 *
1448 * @mr: the memory region being updated.
1449 * @addr: the address within @mr that is to be monitored
1450 * @size: the size of the access to trigger the eventfd
1451 * @match_data: whether to match against @data, instead of just @addr
1452 * @data: the data to match against the guest write
Jay Zhou57914ec2018-01-04 13:29:48 +08001453 * @e: event notifier to be triggered when @addr, @size, and @data all match.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001454 */
1455void memory_region_del_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001456 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001457 unsigned size,
1458 bool match_data,
1459 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +02001460 EventNotifier *e);
1461
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001462/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001463 * memory_region_add_subregion: Add a subregion to a container.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001464 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001465 * Adds a subregion at @offset. The subregion may not overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +03001466 * subregions (except for those explicitly marked as overlapping). A region
1467 * may only be added once as a subregion (unless removed with
1468 * memory_region_del_subregion()); use memory_region_init_alias() if you
1469 * want a region to be a subregion in multiple locations.
1470 *
1471 * @mr: the region to contain the new subregion; must be a container
1472 * initialized with memory_region_init().
1473 * @offset: the offset relative to @mr where @subregion is added.
1474 * @subregion: the subregion to be added.
1475 */
1476void memory_region_add_subregion(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001477 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +03001478 MemoryRegion *subregion);
1479/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +02001480 * memory_region_add_subregion_overlap: Add a subregion to a container
1481 * with overlap.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001482 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001483 * Adds a subregion at @offset. The subregion may overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +03001484 * subregions. Conflicts are resolved by having a higher @priority hide a
1485 * lower @priority. Subregions without priority are taken as @priority 0.
1486 * A region may only be added once as a subregion (unless removed with
1487 * memory_region_del_subregion()); use memory_region_init_alias() if you
1488 * want a region to be a subregion in multiple locations.
1489 *
1490 * @mr: the region to contain the new subregion; must be a container
1491 * initialized with memory_region_init().
1492 * @offset: the offset relative to @mr where @subregion is added.
1493 * @subregion: the subregion to be added.
1494 * @priority: used for resolving overlaps; highest priority wins.
1495 */
1496void memory_region_add_subregion_overlap(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001497 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +03001498 MemoryRegion *subregion,
Marcel Apfelbauma1ff8ae2013-09-16 11:21:14 +03001499 int priority);
Avi Kivitye34911c2011-12-19 12:06:23 +02001500
1501/**
1502 * memory_region_get_ram_addr: Get the ram address associated with a memory
1503 * region
Avi Kivitye34911c2011-12-19 12:06:23 +02001504 */
Fam Zheng7ebb2742016-03-01 14:18:20 +08001505ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
Avi Kivitye34911c2011-12-19 12:06:23 +02001506
Igor Mammedova2b257d2014-10-31 16:38:37 +00001507uint64_t memory_region_get_alignment(const MemoryRegion *mr);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001508/**
1509 * memory_region_del_subregion: Remove a subregion.
1510 *
1511 * Removes a subregion from its container.
1512 *
1513 * @mr: the container to be updated.
1514 * @subregion: the region being removed; must be a current subregion of @mr.
1515 */
1516void memory_region_del_subregion(MemoryRegion *mr,
1517 MemoryRegion *subregion);
1518
Avi Kivity6bba19b2011-09-14 11:54:58 +03001519/*
1520 * memory_region_set_enabled: dynamically enable or disable a region
1521 *
1522 * Enables or disables a memory region. A disabled memory region
1523 * ignores all accesses to itself and its subregions. It does not
1524 * obscure sibling subregions with lower priority - it simply behaves as
1525 * if it was removed from the hierarchy.
1526 *
1527 * Regions default to being enabled.
1528 *
1529 * @mr: the region to be updated
1530 * @enabled: whether to enable or disable the region
1531 */
1532void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1533
Avi Kivity2282e1a2011-09-14 12:10:12 +03001534/*
1535 * memory_region_set_address: dynamically update the address of a region
1536 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001537 * Dynamically updates the address of a region, relative to its container.
Avi Kivity2282e1a2011-09-14 12:10:12 +03001538 * May be used on regions are currently part of a memory hierarchy.
1539 *
1540 * @mr: the region to be updated
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001541 * @addr: new address, relative to container region
Avi Kivity2282e1a2011-09-14 12:10:12 +03001542 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001543void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
Avi Kivity2282e1a2011-09-14 12:10:12 +03001544
Avi Kivity47033592011-12-04 19:16:50 +02001545/*
Michael S. Tsirkine7af4c62014-12-16 11:21:23 +02001546 * memory_region_set_size: dynamically update the size of a region.
1547 *
1548 * Dynamically updates the size of a region.
1549 *
1550 * @mr: the region to be updated
1551 * @size: used size of the region.
1552 */
1553void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1554
1555/*
Avi Kivity47033592011-12-04 19:16:50 +02001556 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1557 *
1558 * Dynamically updates the offset into the target region that an alias points
1559 * to, as if the fourth argument to memory_region_init_alias() has changed.
1560 *
1561 * @mr: the #MemoryRegion to be updated; should be an alias.
1562 * @offset: the new offset into the target memory region
1563 */
1564void memory_region_set_alias_offset(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001565 hwaddr offset);
Avi Kivity47033592011-12-04 19:16:50 +02001566
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001567/**
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001568 * memory_region_present: checks if an address relative to a @container
1569 * translates into #MemoryRegion within @container
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001570 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001571 * Answer whether a #MemoryRegion within @container covers the address
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001572 * @addr.
1573 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001574 * @container: a #MemoryRegion within which @addr is a relative address
1575 * @addr: the area within @container to be searched
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001576 */
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001577bool memory_region_present(MemoryRegion *container, hwaddr addr);
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001578
1579/**
Igor Mammedoveed2bac2014-06-02 15:25:06 +02001580 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1581 * into any address space.
1582 *
1583 * @mr: a #MemoryRegion which should be checked if it's mapped
1584 */
1585bool memory_region_is_mapped(MemoryRegion *mr);
1586
1587/**
Paolo Bonzini73034e92013-05-07 15:48:28 +02001588 * memory_region_find: translate an address/size relative to a
1589 * MemoryRegion into a #MemoryRegionSection.
Avi Kivitye2177952011-12-08 15:00:18 +02001590 *
Paolo Bonzini73034e92013-05-07 15:48:28 +02001591 * Locates the first #MemoryRegion within @mr that overlaps the range
1592 * given by @addr and @size.
Avi Kivitye2177952011-12-08 15:00:18 +02001593 *
1594 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1595 * It will have the following characteristics:
Avi Kivitye2177952011-12-08 15:00:18 +02001596 * .@size = 0 iff no overlap was found
1597 * .@mr is non-%NULL iff an overlap was found
1598 *
Paolo Bonzini73034e92013-05-07 15:48:28 +02001599 * Remember that in the return value the @offset_within_region is
1600 * relative to the returned region (in the .@mr field), not to the
1601 * @mr argument.
1602 *
1603 * Similarly, the .@offset_within_address_space is relative to the
1604 * address space that contains both regions, the passed and the
1605 * returned one. However, in the special case where the @mr argument
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001606 * has no container (and thus is the root of the address space), the
Paolo Bonzini73034e92013-05-07 15:48:28 +02001607 * following will hold:
1608 * .@offset_within_address_space >= @addr
1609 * .@offset_within_address_space + .@size <= @addr + @size
1610 *
1611 * @mr: a MemoryRegion within which @addr is a relative address
1612 * @addr: start of the area within @as to be searched
Avi Kivitye2177952011-12-08 15:00:18 +02001613 * @size: size of the area to be searched
1614 */
Paolo Bonzini73034e92013-05-07 15:48:28 +02001615MemoryRegionSection memory_region_find(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001616 hwaddr addr, uint64_t size);
Avi Kivitye2177952011-12-08 15:00:18 +02001617
Blue Swirlfd062572012-04-09 17:38:52 +00001618/**
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +02001619 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
Avi Kivity86e775c2011-12-15 16:24:49 +02001620 *
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +02001621 * Synchronizes the dirty page log for all address spaces.
Avi Kivity86e775c2011-12-15 16:24:49 +02001622 */
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +02001623void memory_global_dirty_log_sync(void);
Avi Kivity86e775c2011-12-15 16:24:49 +02001624
Avi Kivitye2177952011-12-08 15:00:18 +02001625/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001626 * memory_region_transaction_begin: Start a transaction.
1627 *
1628 * During a transaction, changes will be accumulated and made visible
Stefan Weildabdf392012-01-08 19:35:09 +01001629 * only when the transaction ends (is committed).
Avi Kivity4ef4db82011-07-26 14:26:13 +03001630 */
1631void memory_region_transaction_begin(void);
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001632
1633/**
1634 * memory_region_transaction_commit: Commit a transaction and make changes
1635 * visible to the guest.
Avi Kivity4ef4db82011-07-26 14:26:13 +03001636 */
1637void memory_region_transaction_commit(void);
1638
Avi Kivity7664e802011-12-11 14:47:25 +02001639/**
1640 * memory_listener_register: register callbacks to be called when memory
1641 * sections are mapped or unmapped into an address
1642 * space
1643 *
1644 * @listener: an object containing the callbacks to be called
Avi Kivity7376e582012-02-08 21:05:17 +02001645 * @filter: if non-%NULL, only regions in this address space will be observed
Avi Kivity7664e802011-12-11 14:47:25 +02001646 */
Avi Kivityf6790af2012-10-02 20:13:51 +02001647void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
Avi Kivity7664e802011-12-11 14:47:25 +02001648
1649/**
1650 * memory_listener_unregister: undo the effect of memory_listener_register()
1651 *
1652 * @listener: an object containing the callbacks to be removed
1653 */
1654void memory_listener_unregister(MemoryListener *listener);
1655
1656/**
1657 * memory_global_dirty_log_start: begin dirty logging for all regions
1658 */
1659void memory_global_dirty_log_start(void);
1660
1661/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +02001662 * memory_global_dirty_log_stop: end dirty logging for all regions
Avi Kivity7664e802011-12-11 14:47:25 +02001663 */
1664void memory_global_dirty_log_stop(void);
1665
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001666void mtree_info(fprintf_function mon_printf, void *f, bool flatview,
1667 bool dispatch_tree);
Blue Swirl314e2982011-09-11 20:22:05 +00001668
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001669/**
KONRAD Fredericc9356742016-10-19 15:06:49 +02001670 * memory_region_request_mmio_ptr: request a pointer to an mmio
1671 * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer.
1672 * When the device wants to invalidate the pointer it will call
1673 * memory_region_invalidate_mmio_ptr.
1674 *
1675 * @mr: #MemoryRegion to check
1676 * @addr: address within that region
1677 *
1678 * Returns true on success, false otherwise.
1679 */
1680bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr);
1681
1682/**
1683 * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio
1684 * previously requested.
1685 * In the end that means that if something wants to execute from this area it
1686 * will need to request the pointer again.
1687 *
1688 * @mr: #MemoryRegion associated to the pointer.
Jay Zhou57914ec2018-01-04 13:29:48 +08001689 * @offset: offset within the memory region
KONRAD Fredericc9356742016-10-19 15:06:49 +02001690 * @size: size of that area.
1691 */
1692void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset,
1693 unsigned size);
1694
1695/**
Peter Maydell3b643492015-04-26 16:49:23 +01001696 * memory_region_dispatch_read: perform a read directly to the specified
1697 * MemoryRegion.
1698 *
1699 * @mr: #MemoryRegion to access
1700 * @addr: address within that region
1701 * @pval: pointer to uint64_t which the data is written to
1702 * @size: size of the access in bytes
1703 * @attrs: memory transaction attributes to use for the access
1704 */
1705MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1706 hwaddr addr,
1707 uint64_t *pval,
1708 unsigned size,
1709 MemTxAttrs attrs);
1710/**
1711 * memory_region_dispatch_write: perform a write directly to the specified
1712 * MemoryRegion.
1713 *
1714 * @mr: #MemoryRegion to access
1715 * @addr: address within that region
1716 * @data: data to write
1717 * @size: size of the access in bytes
1718 * @attrs: memory transaction attributes to use for the access
1719 */
1720MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1721 hwaddr addr,
1722 uint64_t data,
1723 unsigned size,
1724 MemTxAttrs attrs);
1725
1726/**
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001727 * address_space_init: initializes an address space
1728 *
1729 * @as: an uninitialized #AddressSpace
Veres Lajos67cc32e2015-09-08 22:45:14 +01001730 * @root: a #MemoryRegion that routes addresses for the address space
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001731 * @name: an address space name. The name is only used for debugging
1732 * output.
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001733 */
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001734void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001735
Peter Crosthwaitef0c02d12016-01-21 14:15:06 +00001736/**
Avi Kivity83f3c252012-10-07 12:59:55 +02001737 * address_space_destroy: destroy an address space
1738 *
1739 * Releases all resources associated with an address space. After an address space
1740 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1741 * as well.
1742 *
1743 * @as: address space to be destroyed
1744 */
1745void address_space_destroy(AddressSpace *as);
1746
Avi Kivityac1970f2012-10-03 16:22:53 +02001747/**
1748 * address_space_rw: read from or write to an address space.
1749 *
Peter Maydell5c9eb022015-04-26 16:49:24 +01001750 * Return a MemTxResult indicating whether the operation succeeded
1751 * or failed (eg unassigned memory, device rejected the transaction,
1752 * IOMMU fault).
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001753 *
Avi Kivityac1970f2012-10-03 16:22:53 +02001754 * @as: #AddressSpace to be accessed
1755 * @addr: address within that address space
Peter Maydell5c9eb022015-04-26 16:49:24 +01001756 * @attrs: memory transaction attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001757 * @buf: buffer with the data transferred
Jay Zhou57914ec2018-01-04 13:29:48 +08001758 * @len: the number of bytes to read or write
Avi Kivityac1970f2012-10-03 16:22:53 +02001759 * @is_write: indicates the transfer direction
1760 */
Peter Maydell5c9eb022015-04-26 16:49:24 +01001761MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1762 MemTxAttrs attrs, uint8_t *buf,
1763 int len, bool is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02001764
1765/**
1766 * address_space_write: write to address space.
1767 *
Peter Maydell5c9eb022015-04-26 16:49:24 +01001768 * Return a MemTxResult indicating whether the operation succeeded
1769 * or failed (eg unassigned memory, device rejected the transaction,
1770 * IOMMU fault).
Avi Kivityac1970f2012-10-03 16:22:53 +02001771 *
1772 * @as: #AddressSpace to be accessed
1773 * @addr: address within that address space
Peter Maydell5c9eb022015-04-26 16:49:24 +01001774 * @attrs: memory transaction attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001775 * @buf: buffer with the data transferred
Jay Zhou57914ec2018-01-04 13:29:48 +08001776 * @len: the number of bytes to write
Avi Kivityac1970f2012-10-03 16:22:53 +02001777 */
Peter Maydell5c9eb022015-04-26 16:49:24 +01001778MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1779 MemTxAttrs attrs,
1780 const uint8_t *buf, int len);
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001781
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001782/* address_space_ld*: load from an address space
Peter Maydell50013112015-04-26 16:49:24 +01001783 * address_space_st*: store to an address space
1784 *
1785 * These functions perform a load or store of the byte, word,
1786 * longword or quad to the specified address within the AddressSpace.
1787 * The _le suffixed functions treat the data as little endian;
1788 * _be indicates big endian; no suffix indicates "same endianness
1789 * as guest CPU".
1790 *
1791 * The "guest CPU endianness" accessors are deprecated for use outside
1792 * target-* code; devices should be CPU-agnostic and use either the LE
1793 * or the BE accessors.
1794 *
1795 * @as #AddressSpace to be accessed
1796 * @addr: address within that address space
1797 * @val: data value, for stores
1798 * @attrs: memory transaction attributes
1799 * @result: location to write the success/failure of the transaction;
1800 * if NULL, this information is discarded
1801 */
Peter Maydell50013112015-04-26 16:49:24 +01001802
Paolo Bonzini4269c822018-03-04 23:31:47 +01001803#define SUFFIX
1804#define ARG1 as
1805#define ARG1_DECL AddressSpace *as
1806#include "exec/memory_ldst.inc.h"
1807
1808#define SUFFIX
1809#define ARG1 as
1810#define ARG1_DECL AddressSpace *as
1811#include "exec/memory_ldst_phys.inc.h"
Paolo Bonzini0ce265f2016-11-22 11:34:02 +01001812
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001813struct MemoryRegionCache {
Paolo Bonzini48564042018-03-18 18:26:36 +01001814 void *ptr;
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001815 hwaddr xlat;
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001816 hwaddr len;
Paolo Bonzini48564042018-03-18 18:26:36 +01001817 FlatView *fv;
1818 MemoryRegionSection mrs;
1819 bool is_write;
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001820};
1821
Paolo Bonzini48564042018-03-18 18:26:36 +01001822#define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
1823
Paolo Bonzini5eba0402017-01-27 16:40:16 +01001824
Paolo Bonzini4269c822018-03-04 23:31:47 +01001825/* address_space_ld*_cached: load from a cached #MemoryRegion
1826 * address_space_st*_cached: store into a cached #MemoryRegion
1827 *
1828 * These functions perform a load or store of the byte, word,
1829 * longword or quad to the specified address. The address is
1830 * a physical address in the AddressSpace, but it must lie within
1831 * a #MemoryRegion that was mapped with address_space_cache_init.
1832 *
1833 * The _le suffixed functions treat the data as little endian;
1834 * _be indicates big endian; no suffix indicates "same endianness
1835 * as guest CPU".
1836 *
1837 * The "guest CPU endianness" accessors are deprecated for use outside
1838 * target-* code; devices should be CPU-agnostic and use either the LE
1839 * or the BE accessors.
1840 *
1841 * @cache: previously initialized #MemoryRegionCache to be accessed
1842 * @addr: address within the address space
1843 * @val: data value, for stores
1844 * @attrs: memory transaction attributes
1845 * @result: location to write the success/failure of the transaction;
1846 * if NULL, this information is discarded
1847 */
1848
Paolo Bonzini48564042018-03-18 18:26:36 +01001849#define SUFFIX _cached_slow
Paolo Bonzini4269c822018-03-04 23:31:47 +01001850#define ARG1 cache
1851#define ARG1_DECL MemoryRegionCache *cache
1852#include "exec/memory_ldst.inc.h"
1853
Paolo Bonzini48564042018-03-18 18:26:36 +01001854/* Inline fast path for direct RAM access. */
1855static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache,
1856 hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
1857{
1858 assert(addr < cache->len);
1859 if (likely(cache->ptr)) {
1860 return ldub_p(cache->ptr + addr);
1861 } else {
1862 return address_space_ldub_cached_slow(cache, addr, attrs, result);
1863 }
1864}
1865
1866static inline void address_space_stb_cached(MemoryRegionCache *cache,
1867 hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result)
1868{
1869 assert(addr < cache->len);
1870 if (likely(cache->ptr)) {
1871 stb_p(cache->ptr + addr, val);
1872 } else {
1873 address_space_stb_cached_slow(cache, addr, val, attrs, result);
1874 }
1875}
1876
1877#define ENDIANNESS _le
1878#include "exec/memory_ldst_cached.inc.h"
1879
1880#define ENDIANNESS _be
1881#include "exec/memory_ldst_cached.inc.h"
1882
Paolo Bonzini4269c822018-03-04 23:31:47 +01001883#define SUFFIX _cached
1884#define ARG1 cache
1885#define ARG1_DECL MemoryRegionCache *cache
1886#include "exec/memory_ldst_phys.inc.h"
1887
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001888/* address_space_cache_init: prepare for repeated access to a physical
1889 * memory region
1890 *
1891 * @cache: #MemoryRegionCache to be filled
1892 * @as: #AddressSpace to be accessed
1893 * @addr: address within that address space
1894 * @len: length of buffer
1895 * @is_write: indicates the transfer direction
1896 *
1897 * Will only work with RAM, and may map a subset of the requested range by
1898 * returning a value that is less than @len. On failure, return a negative
1899 * errno value.
1900 *
1901 * Because it only works with RAM, this function can be used for
1902 * read-modify-write operations. In this case, is_write should be %true.
1903 *
1904 * Note that addresses passed to the address_space_*_cached functions
1905 * are relative to @addr.
1906 */
1907int64_t address_space_cache_init(MemoryRegionCache *cache,
1908 AddressSpace *as,
1909 hwaddr addr,
1910 hwaddr len,
1911 bool is_write);
1912
1913/**
1914 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1915 *
1916 * @cache: The #MemoryRegionCache to operate on.
1917 * @addr: The first physical address that was written, relative to the
1918 * address that was passed to @address_space_cache_init.
1919 * @access_len: The number of bytes that were written starting at @addr.
1920 */
1921void address_space_cache_invalidate(MemoryRegionCache *cache,
1922 hwaddr addr,
1923 hwaddr access_len);
1924
1925/**
1926 * address_space_cache_destroy: free a #MemoryRegionCache
1927 *
1928 * @cache: The #MemoryRegionCache whose memory should be released.
1929 */
1930void address_space_cache_destroy(MemoryRegionCache *cache);
1931
Jason Wang052c8fa2016-12-30 18:09:13 +08001932/* address_space_get_iotlb_entry: translate an address into an IOTLB
1933 * entry. Should be called from an RCU critical section.
1934 */
1935IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
Peter Maydell7446eb02018-05-31 14:50:53 +01001936 bool is_write, MemTxAttrs attrs);
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01001937
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001938/* address_space_translate: translate an address range into an address space
Paolo Bonzini41063e12015-03-18 14:21:43 +01001939 * into a MemoryRegion and an address range into that section. Should be
1940 * called from an RCU critical section, to avoid that the last reference
1941 * to the returned region disappears after address_space_translate returns.
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001942 *
Jay Zhou57914ec2018-01-04 13:29:48 +08001943 * @fv: #FlatView to be accessed
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001944 * @addr: address within that address space
1945 * @xlat: pointer to address within the returned memory region section's
1946 * #MemoryRegion.
1947 * @len: pointer to length
1948 * @is_write: indicates the transfer direction
Peter Maydellbc6b1ce2018-05-31 14:50:52 +01001949 * @attrs: memory attributes
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001950 */
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10001951MemoryRegion *flatview_translate(FlatView *fv,
1952 hwaddr addr, hwaddr *xlat,
Peter Maydellefa99a22018-05-31 14:50:52 +01001953 hwaddr *len, bool is_write,
1954 MemTxAttrs attrs);
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10001955
1956static inline MemoryRegion *address_space_translate(AddressSpace *as,
1957 hwaddr addr, hwaddr *xlat,
Peter Maydellbc6b1ce2018-05-31 14:50:52 +01001958 hwaddr *len, bool is_write,
1959 MemTxAttrs attrs)
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10001960{
1961 return flatview_translate(address_space_to_flatview(as),
Peter Maydellefa99a22018-05-31 14:50:52 +01001962 addr, xlat, len, is_write, attrs);
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10001963}
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001964
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001965/* address_space_access_valid: check for validity of accessing an address
1966 * space range
1967 *
Avi Kivity30951152012-10-30 13:47:46 +02001968 * Check whether memory is assigned to the given address space range, and
1969 * access is permitted by any IOMMU regions that are active for the address
1970 * space.
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001971 *
1972 * For now, addr and len should be aligned to a page size. This limitation
1973 * will be lifted in the future.
1974 *
1975 * @as: #AddressSpace to be accessed
1976 * @addr: address within that address space
1977 * @len: length of the area to be checked
1978 * @is_write: indicates the transfer direction
Peter Maydellfddffa42018-05-31 14:50:52 +01001979 * @attrs: memory attributes
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001980 */
Peter Maydellfddffa42018-05-31 14:50:52 +01001981bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len,
1982 bool is_write, MemTxAttrs attrs);
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001983
Avi Kivityac1970f2012-10-03 16:22:53 +02001984/* address_space_map: map a physical memory region into a host virtual address
1985 *
1986 * May map a subset of the requested range, given by and returned in @plen.
1987 * May return %NULL if resources needed to perform the mapping are exhausted.
1988 * Use only for reads OR writes - not for read-modify-write operations.
1989 * Use cpu_register_map_client() to know when retrying the map operation is
1990 * likely to succeed.
1991 *
1992 * @as: #AddressSpace to be accessed
1993 * @addr: address within that address space
1994 * @plen: pointer to length of buffer; updated on return
1995 * @is_write: indicates the transfer direction
Peter Maydellf26404f2018-05-31 14:50:52 +01001996 * @attrs: memory attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001997 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001998void *address_space_map(AddressSpace *as, hwaddr addr,
Peter Maydellf26404f2018-05-31 14:50:52 +01001999 hwaddr *plen, bool is_write, MemTxAttrs attrs);
Avi Kivityac1970f2012-10-03 16:22:53 +02002000
2001/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2002 *
2003 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2004 * the amount of memory that was actually read or written by the caller.
2005 *
2006 * @as: #AddressSpace used
Jay Zhou57914ec2018-01-04 13:29:48 +08002007 * @buffer: host pointer as returned by address_space_map()
Avi Kivityac1970f2012-10-03 16:22:53 +02002008 * @len: buffer length as returned by address_space_map()
2009 * @access_len: amount of data actually transferred
2010 * @is_write: indicates the transfer direction
2011 */
Avi Kivitya8170e52012-10-23 12:30:10 +02002012void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2013 int is_write, hwaddr access_len);
Avi Kivityac1970f2012-10-03 16:22:53 +02002014
2015
Paolo Bonzinia203ac72015-12-09 10:18:57 +01002016/* Internal functions, part of the implementation of address_space_read. */
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002017MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
2018 MemTxAttrs attrs, uint8_t *buf, int len);
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10002019MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
2020 MemTxAttrs attrs, uint8_t *buf,
2021 int len, hwaddr addr1, hwaddr l,
2022 MemoryRegion *mr);
Paolo Bonzini0878d0e2016-02-22 11:02:12 +01002023void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002024
Paolo Bonzini48564042018-03-18 18:26:36 +01002025/* Internal functions, part of the implementation of address_space_read_cached
2026 * and address_space_write_cached. */
2027void address_space_read_cached_slow(MemoryRegionCache *cache,
2028 hwaddr addr, void *buf, int len);
2029void address_space_write_cached_slow(MemoryRegionCache *cache,
2030 hwaddr addr, const void *buf, int len);
2031
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002032static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
2033{
2034 if (is_write) {
Alex Williamson4a2e2422016-10-31 09:53:03 -06002035 return memory_region_is_ram(mr) &&
2036 !mr->readonly && !memory_region_is_ram_device(mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002037 } else {
Alex Williamson4a2e2422016-10-31 09:53:03 -06002038 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
2039 memory_region_is_romd(mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002040 }
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002041}
2042
2043/**
2044 * address_space_read: read from an address space.
2045 *
2046 * Return a MemTxResult indicating whether the operation succeeded
2047 * or failed (eg unassigned memory, device rejected the transaction,
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002048 * IOMMU fault). Called within RCU critical section.
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002049 *
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002050 * @as: #AddressSpace to be accessed
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002051 * @addr: address within that address space
2052 * @attrs: memory transaction attributes
2053 * @buf: buffer with the data transferred
2054 */
2055static inline __attribute__((__always_inline__))
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002056MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
2057 MemTxAttrs attrs, uint8_t *buf,
2058 int len)
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002059{
2060 MemTxResult result = MEMTX_OK;
2061 hwaddr l, addr1;
2062 void *ptr;
2063 MemoryRegion *mr;
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002064 FlatView *fv;
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002065
2066 if (__builtin_constant_p(len)) {
2067 if (len) {
2068 rcu_read_lock();
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002069 fv = address_space_to_flatview(as);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002070 l = len;
Peter Maydellefa99a22018-05-31 14:50:52 +01002071 mr = flatview_translate(fv, addr, &addr1, &l, false, attrs);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002072 if (len == l && memory_access_is_direct(mr, false)) {
Paolo Bonzini0878d0e2016-02-22 11:02:12 +01002073 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002074 memcpy(buf, ptr, len);
2075 } else {
Alexey Kardashevskiy16620682017-09-21 18:50:58 +10002076 result = flatview_read_continue(fv, addr, attrs, buf, len,
2077 addr1, l, mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002078 }
2079 rcu_read_unlock();
2080 }
2081 } else {
Paolo Bonzinib2a44fc2018-03-05 00:19:49 +01002082 result = address_space_read_full(as, addr, attrs, buf, len);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01002083 }
2084 return result;
2085}
Paolo Bonzinia203ac72015-12-09 10:18:57 +01002086
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01002087/**
2088 * address_space_read_cached: read from a cached RAM region
2089 *
2090 * @cache: Cached region to be addressed
2091 * @addr: address relative to the base of the RAM region
2092 * @buf: buffer with the data transferred
2093 * @len: length of the data transferred
2094 */
2095static inline void
2096address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
2097 void *buf, int len)
2098{
2099 assert(addr < cache->len && len <= cache->len - addr);
Paolo Bonzini48564042018-03-18 18:26:36 +01002100 if (likely(cache->ptr)) {
2101 memcpy(buf, cache->ptr + addr, len);
2102 } else {
2103 address_space_read_cached_slow(cache, addr, buf, len);
2104 }
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01002105}
2106
2107/**
2108 * address_space_write_cached: write to a cached RAM region
2109 *
2110 * @cache: Cached region to be addressed
2111 * @addr: address relative to the base of the RAM region
2112 * @buf: buffer with the data transferred
2113 * @len: length of the data transferred
2114 */
2115static inline void
2116address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
2117 void *buf, int len)
2118{
2119 assert(addr < cache->len && len <= cache->len - addr);
Paolo Bonzini48564042018-03-18 18:26:36 +01002120 if (likely(cache->ptr)) {
2121 memcpy(cache->ptr + addr, buf, len);
2122 } else {
2123 address_space_write_cached_slow(cache, addr, buf, len);
2124 }
Paolo Bonzini1f4e4962016-11-22 12:04:52 +01002125}
2126
Avi Kivity093bc2c2011-07-26 14:26:01 +03002127#endif
2128
2129#endif