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