]>
Commit | Line | Data |
---|---|---|
54936004 | 1 | /* |
5b6dd868 | 2 | * Virtual page mapping |
5fafdf24 | 3 | * |
54936004 FB |
4 | * Copyright (c) 2003 Fabrice Bellard |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
54936004 | 18 | */ |
67b915a5 | 19 | #include "config.h" |
777872e5 | 20 | #ifndef _WIN32 |
a98d49b1 | 21 | #include <sys/types.h> |
d5a8f07c FB |
22 | #include <sys/mman.h> |
23 | #endif | |
54936004 | 24 | |
055403b2 | 25 | #include "qemu-common.h" |
6180a181 | 26 | #include "cpu.h" |
b67d9a52 | 27 | #include "tcg.h" |
b3c7724c | 28 | #include "hw/hw.h" |
4485bd26 | 29 | #if !defined(CONFIG_USER_ONLY) |
47c8ca53 | 30 | #include "hw/boards.h" |
4485bd26 | 31 | #endif |
cc9e98cb | 32 | #include "hw/qdev.h" |
1de7afc9 | 33 | #include "qemu/osdep.h" |
9c17d615 | 34 | #include "sysemu/kvm.h" |
2ff3de68 | 35 | #include "sysemu/sysemu.h" |
0d09e41a | 36 | #include "hw/xen/xen.h" |
1de7afc9 PB |
37 | #include "qemu/timer.h" |
38 | #include "qemu/config-file.h" | |
75a34036 | 39 | #include "qemu/error-report.h" |
022c62cb | 40 | #include "exec/memory.h" |
9c17d615 | 41 | #include "sysemu/dma.h" |
022c62cb | 42 | #include "exec/address-spaces.h" |
53a5960a PB |
43 | #if defined(CONFIG_USER_ONLY) |
44 | #include <qemu.h> | |
432d268c | 45 | #else /* !CONFIG_USER_ONLY */ |
9c17d615 | 46 | #include "sysemu/xen-mapcache.h" |
6506e4f9 | 47 | #include "trace.h" |
53a5960a | 48 | #endif |
0d6d3c87 | 49 | #include "exec/cpu-all.h" |
0dc3f44a | 50 | #include "qemu/rcu_queue.h" |
4840f10e | 51 | #include "qemu/main-loop.h" |
5b6dd868 | 52 | #include "translate-all.h" |
7615936e | 53 | #include "sysemu/replay.h" |
0cac1b66 | 54 | |
022c62cb | 55 | #include "exec/memory-internal.h" |
220c3ebd | 56 | #include "exec/ram_addr.h" |
67d95c15 | 57 | |
b35ba30f | 58 | #include "qemu/range.h" |
794e8f30 MT |
59 | #ifndef _WIN32 |
60 | #include "qemu/mmap-alloc.h" | |
61 | #endif | |
b35ba30f | 62 | |
db7b5426 | 63 | //#define DEBUG_SUBPAGE |
1196be37 | 64 | |
e2eef170 | 65 | #if !defined(CONFIG_USER_ONLY) |
0dc3f44a MD |
66 | /* ram_list is read under rcu_read_lock()/rcu_read_unlock(). Writes |
67 | * are protected by the ramlist lock. | |
68 | */ | |
0d53d9fe | 69 | RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list.blocks) }; |
62152b8a AK |
70 | |
71 | static MemoryRegion *system_memory; | |
309cb471 | 72 | static MemoryRegion *system_io; |
62152b8a | 73 | |
f6790af6 AK |
74 | AddressSpace address_space_io; |
75 | AddressSpace address_space_memory; | |
2673a5da | 76 | |
0844e007 | 77 | MemoryRegion io_mem_rom, io_mem_notdirty; |
acc9d80b | 78 | static MemoryRegion io_mem_unassigned; |
0e0df1e2 | 79 | |
7bd4f430 PB |
80 | /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */ |
81 | #define RAM_PREALLOC (1 << 0) | |
82 | ||
dbcb8981 PB |
83 | /* RAM is mmap-ed with MAP_SHARED */ |
84 | #define RAM_SHARED (1 << 1) | |
85 | ||
62be4e3a MT |
86 | /* Only a portion of RAM (used_length) is actually used, and migrated. |
87 | * This used_length size can change across reboots. | |
88 | */ | |
89 | #define RAM_RESIZEABLE (1 << 2) | |
90 | ||
794e8f30 | 91 | /* RAM is backed by an mmapped file. |
8561c924 | 92 | */ |
794e8f30 | 93 | #define RAM_FILE (1 << 3) |
e2eef170 | 94 | #endif |
9fa3e853 | 95 | |
bdc44640 | 96 | struct CPUTailQ cpus = QTAILQ_HEAD_INITIALIZER(cpus); |
6a00d601 FB |
97 | /* current CPU in the current thread. It is only valid inside |
98 | cpu_exec() */ | |
f240eb6f | 99 | __thread CPUState *current_cpu; |
2e70f6ef | 100 | /* 0 = Do not count executed instructions. |
bf20dc07 | 101 | 1 = Precise instruction counting. |
2e70f6ef | 102 | 2 = Adaptive rate instruction counting. */ |
5708fc66 | 103 | int use_icount; |
6a00d601 | 104 | |
e2eef170 | 105 | #if !defined(CONFIG_USER_ONLY) |
4346ae3e | 106 | |
1db8abb1 PB |
107 | typedef struct PhysPageEntry PhysPageEntry; |
108 | ||
109 | struct PhysPageEntry { | |
9736e55b | 110 | /* How many bits skip to next level (in units of L2_SIZE). 0 for a leaf. */ |
8b795765 | 111 | uint32_t skip : 6; |
9736e55b | 112 | /* index into phys_sections (!skip) or phys_map_nodes (skip) */ |
8b795765 | 113 | uint32_t ptr : 26; |
1db8abb1 PB |
114 | }; |
115 | ||
8b795765 MT |
116 | #define PHYS_MAP_NODE_NIL (((uint32_t)~0) >> 6) |
117 | ||
03f49957 | 118 | /* Size of the L2 (and L3, etc) page tables. */ |
57271d63 | 119 | #define ADDR_SPACE_BITS 64 |
03f49957 | 120 | |
026736ce | 121 | #define P_L2_BITS 9 |
03f49957 PB |
122 | #define P_L2_SIZE (1 << P_L2_BITS) |
123 | ||
124 | #define P_L2_LEVELS (((ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / P_L2_BITS) + 1) | |
125 | ||
126 | typedef PhysPageEntry Node[P_L2_SIZE]; | |
0475d94f | 127 | |
53cb28cb | 128 | typedef struct PhysPageMap { |
79e2b9ae PB |
129 | struct rcu_head rcu; |
130 | ||
53cb28cb MA |
131 | unsigned sections_nb; |
132 | unsigned sections_nb_alloc; | |
133 | unsigned nodes_nb; | |
134 | unsigned nodes_nb_alloc; | |
135 | Node *nodes; | |
136 | MemoryRegionSection *sections; | |
137 | } PhysPageMap; | |
138 | ||
1db8abb1 | 139 | struct AddressSpaceDispatch { |
79e2b9ae PB |
140 | struct rcu_head rcu; |
141 | ||
1db8abb1 PB |
142 | /* This is a multi-level map on the physical address space. |
143 | * The bottom level has pointers to MemoryRegionSections. | |
144 | */ | |
145 | PhysPageEntry phys_map; | |
53cb28cb | 146 | PhysPageMap map; |
acc9d80b | 147 | AddressSpace *as; |
1db8abb1 PB |
148 | }; |
149 | ||
90260c6c JK |
150 | #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK) |
151 | typedef struct subpage_t { | |
152 | MemoryRegion iomem; | |
acc9d80b | 153 | AddressSpace *as; |
90260c6c JK |
154 | hwaddr base; |
155 | uint16_t sub_section[TARGET_PAGE_SIZE]; | |
156 | } subpage_t; | |
157 | ||
b41aac4f LPF |
158 | #define PHYS_SECTION_UNASSIGNED 0 |
159 | #define PHYS_SECTION_NOTDIRTY 1 | |
160 | #define PHYS_SECTION_ROM 2 | |
161 | #define PHYS_SECTION_WATCH 3 | |
5312bd8b | 162 | |
e2eef170 | 163 | static void io_mem_init(void); |
62152b8a | 164 | static void memory_map_init(void); |
09daed84 | 165 | static void tcg_commit(MemoryListener *listener); |
e2eef170 | 166 | |
1ec9b909 | 167 | static MemoryRegion io_mem_watch; |
32857f4d PM |
168 | |
169 | /** | |
170 | * CPUAddressSpace: all the information a CPU needs about an AddressSpace | |
171 | * @cpu: the CPU whose AddressSpace this is | |
172 | * @as: the AddressSpace itself | |
173 | * @memory_dispatch: its dispatch pointer (cached, RCU protected) | |
174 | * @tcg_as_listener: listener for tracking changes to the AddressSpace | |
175 | */ | |
176 | struct CPUAddressSpace { | |
177 | CPUState *cpu; | |
178 | AddressSpace *as; | |
179 | struct AddressSpaceDispatch *memory_dispatch; | |
180 | MemoryListener tcg_as_listener; | |
181 | }; | |
182 | ||
6658ffb8 | 183 | #endif |
fd6ce8f6 | 184 | |
6d9a1304 | 185 | #if !defined(CONFIG_USER_ONLY) |
d6f2ea22 | 186 | |
53cb28cb | 187 | static void phys_map_node_reserve(PhysPageMap *map, unsigned nodes) |
d6f2ea22 | 188 | { |
53cb28cb MA |
189 | if (map->nodes_nb + nodes > map->nodes_nb_alloc) { |
190 | map->nodes_nb_alloc = MAX(map->nodes_nb_alloc * 2, 16); | |
191 | map->nodes_nb_alloc = MAX(map->nodes_nb_alloc, map->nodes_nb + nodes); | |
192 | map->nodes = g_renew(Node, map->nodes, map->nodes_nb_alloc); | |
d6f2ea22 | 193 | } |
f7bf5461 AK |
194 | } |
195 | ||
db94604b | 196 | static uint32_t phys_map_node_alloc(PhysPageMap *map, bool leaf) |
f7bf5461 AK |
197 | { |
198 | unsigned i; | |
8b795765 | 199 | uint32_t ret; |
db94604b PB |
200 | PhysPageEntry e; |
201 | PhysPageEntry *p; | |
f7bf5461 | 202 | |
53cb28cb | 203 | ret = map->nodes_nb++; |
db94604b | 204 | p = map->nodes[ret]; |
f7bf5461 | 205 | assert(ret != PHYS_MAP_NODE_NIL); |
53cb28cb | 206 | assert(ret != map->nodes_nb_alloc); |
db94604b PB |
207 | |
208 | e.skip = leaf ? 0 : 1; | |
209 | e.ptr = leaf ? PHYS_SECTION_UNASSIGNED : PHYS_MAP_NODE_NIL; | |
03f49957 | 210 | for (i = 0; i < P_L2_SIZE; ++i) { |
db94604b | 211 | memcpy(&p[i], &e, sizeof(e)); |
d6f2ea22 | 212 | } |
f7bf5461 | 213 | return ret; |
d6f2ea22 AK |
214 | } |
215 | ||
53cb28cb MA |
216 | static void phys_page_set_level(PhysPageMap *map, PhysPageEntry *lp, |
217 | hwaddr *index, hwaddr *nb, uint16_t leaf, | |
2999097b | 218 | int level) |
f7bf5461 AK |
219 | { |
220 | PhysPageEntry *p; | |
03f49957 | 221 | hwaddr step = (hwaddr)1 << (level * P_L2_BITS); |
108c49b8 | 222 | |
9736e55b | 223 | if (lp->skip && lp->ptr == PHYS_MAP_NODE_NIL) { |
db94604b | 224 | lp->ptr = phys_map_node_alloc(map, level == 0); |
92e873b9 | 225 | } |
db94604b | 226 | p = map->nodes[lp->ptr]; |
03f49957 | 227 | lp = &p[(*index >> (level * P_L2_BITS)) & (P_L2_SIZE - 1)]; |
f7bf5461 | 228 | |
03f49957 | 229 | while (*nb && lp < &p[P_L2_SIZE]) { |
07f07b31 | 230 | if ((*index & (step - 1)) == 0 && *nb >= step) { |
9736e55b | 231 | lp->skip = 0; |
c19e8800 | 232 | lp->ptr = leaf; |
07f07b31 AK |
233 | *index += step; |
234 | *nb -= step; | |
2999097b | 235 | } else { |
53cb28cb | 236 | phys_page_set_level(map, lp, index, nb, leaf, level - 1); |
2999097b AK |
237 | } |
238 | ++lp; | |
f7bf5461 AK |
239 | } |
240 | } | |
241 | ||
ac1970fb | 242 | static void phys_page_set(AddressSpaceDispatch *d, |
a8170e5e | 243 | hwaddr index, hwaddr nb, |
2999097b | 244 | uint16_t leaf) |
f7bf5461 | 245 | { |
2999097b | 246 | /* Wildly overreserve - it doesn't matter much. */ |
53cb28cb | 247 | phys_map_node_reserve(&d->map, 3 * P_L2_LEVELS); |
5cd2c5b6 | 248 | |
53cb28cb | 249 | phys_page_set_level(&d->map, &d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1); |
92e873b9 FB |
250 | } |
251 | ||
b35ba30f MT |
252 | /* Compact a non leaf page entry. Simply detect that the entry has a single child, |
253 | * and update our entry so we can skip it and go directly to the destination. | |
254 | */ | |
255 | static void phys_page_compact(PhysPageEntry *lp, Node *nodes, unsigned long *compacted) | |
256 | { | |
257 | unsigned valid_ptr = P_L2_SIZE; | |
258 | int valid = 0; | |
259 | PhysPageEntry *p; | |
260 | int i; | |
261 | ||
262 | if (lp->ptr == PHYS_MAP_NODE_NIL) { | |
263 | return; | |
264 | } | |
265 | ||
266 | p = nodes[lp->ptr]; | |
267 | for (i = 0; i < P_L2_SIZE; i++) { | |
268 | if (p[i].ptr == PHYS_MAP_NODE_NIL) { | |
269 | continue; | |
270 | } | |
271 | ||
272 | valid_ptr = i; | |
273 | valid++; | |
274 | if (p[i].skip) { | |
275 | phys_page_compact(&p[i], nodes, compacted); | |
276 | } | |
277 | } | |
278 | ||
279 | /* We can only compress if there's only one child. */ | |
280 | if (valid != 1) { | |
281 | return; | |
282 | } | |
283 | ||
284 | assert(valid_ptr < P_L2_SIZE); | |
285 | ||
286 | /* Don't compress if it won't fit in the # of bits we have. */ | |
287 | if (lp->skip + p[valid_ptr].skip >= (1 << 3)) { | |
288 | return; | |
289 | } | |
290 | ||
291 | lp->ptr = p[valid_ptr].ptr; | |
292 | if (!p[valid_ptr].skip) { | |
293 | /* If our only child is a leaf, make this a leaf. */ | |
294 | /* By design, we should have made this node a leaf to begin with so we | |
295 | * should never reach here. | |
296 | * But since it's so simple to handle this, let's do it just in case we | |
297 | * change this rule. | |
298 | */ | |
299 | lp->skip = 0; | |
300 | } else { | |
301 | lp->skip += p[valid_ptr].skip; | |
302 | } | |
303 | } | |
304 | ||
305 | static void phys_page_compact_all(AddressSpaceDispatch *d, int nodes_nb) | |
306 | { | |
307 | DECLARE_BITMAP(compacted, nodes_nb); | |
308 | ||
309 | if (d->phys_map.skip) { | |
53cb28cb | 310 | phys_page_compact(&d->phys_map, d->map.nodes, compacted); |
b35ba30f MT |
311 | } |
312 | } | |
313 | ||
97115a8d | 314 | static MemoryRegionSection *phys_page_find(PhysPageEntry lp, hwaddr addr, |
9affd6fc | 315 | Node *nodes, MemoryRegionSection *sections) |
92e873b9 | 316 | { |
31ab2b4a | 317 | PhysPageEntry *p; |
97115a8d | 318 | hwaddr index = addr >> TARGET_PAGE_BITS; |
31ab2b4a | 319 | int i; |
f1f6e3b8 | 320 | |
9736e55b | 321 | for (i = P_L2_LEVELS; lp.skip && (i -= lp.skip) >= 0;) { |
c19e8800 | 322 | if (lp.ptr == PHYS_MAP_NODE_NIL) { |
9affd6fc | 323 | return §ions[PHYS_SECTION_UNASSIGNED]; |
31ab2b4a | 324 | } |
9affd6fc | 325 | p = nodes[lp.ptr]; |
03f49957 | 326 | lp = p[(index >> (i * P_L2_BITS)) & (P_L2_SIZE - 1)]; |
5312bd8b | 327 | } |
b35ba30f MT |
328 | |
329 | if (sections[lp.ptr].size.hi || | |
330 | range_covers_byte(sections[lp.ptr].offset_within_address_space, | |
331 | sections[lp.ptr].size.lo, addr)) { | |
332 | return §ions[lp.ptr]; | |
333 | } else { | |
334 | return §ions[PHYS_SECTION_UNASSIGNED]; | |
335 | } | |
f3705d53 AK |
336 | } |
337 | ||
e5548617 BS |
338 | bool memory_region_is_unassigned(MemoryRegion *mr) |
339 | { | |
2a8e7499 | 340 | return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device |
5b6dd868 | 341 | && mr != &io_mem_watch; |
fd6ce8f6 | 342 | } |
149f54b5 | 343 | |
79e2b9ae | 344 | /* Called from RCU critical section */ |
c7086b4a | 345 | static MemoryRegionSection *address_space_lookup_region(AddressSpaceDispatch *d, |
90260c6c JK |
346 | hwaddr addr, |
347 | bool resolve_subpage) | |
9f029603 | 348 | { |
90260c6c JK |
349 | MemoryRegionSection *section; |
350 | subpage_t *subpage; | |
351 | ||
53cb28cb | 352 | section = phys_page_find(d->phys_map, addr, d->map.nodes, d->map.sections); |
90260c6c JK |
353 | if (resolve_subpage && section->mr->subpage) { |
354 | subpage = container_of(section->mr, subpage_t, iomem); | |
53cb28cb | 355 | section = &d->map.sections[subpage->sub_section[SUBPAGE_IDX(addr)]]; |
90260c6c JK |
356 | } |
357 | return section; | |
9f029603 JK |
358 | } |
359 | ||
79e2b9ae | 360 | /* Called from RCU critical section */ |
90260c6c | 361 | static MemoryRegionSection * |
c7086b4a | 362 | address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *xlat, |
90260c6c | 363 | hwaddr *plen, bool resolve_subpage) |
149f54b5 PB |
364 | { |
365 | MemoryRegionSection *section; | |
965eb2fc | 366 | MemoryRegion *mr; |
a87f3954 | 367 | Int128 diff; |
149f54b5 | 368 | |
c7086b4a | 369 | section = address_space_lookup_region(d, addr, resolve_subpage); |
149f54b5 PB |
370 | /* Compute offset within MemoryRegionSection */ |
371 | addr -= section->offset_within_address_space; | |
372 | ||
373 | /* Compute offset within MemoryRegion */ | |
374 | *xlat = addr + section->offset_within_region; | |
375 | ||
965eb2fc | 376 | mr = section->mr; |
b242e0e0 PB |
377 | |
378 | /* MMIO registers can be expected to perform full-width accesses based only | |
379 | * on their address, without considering adjacent registers that could | |
380 | * decode to completely different MemoryRegions. When such registers | |
381 | * exist (e.g. I/O ports 0xcf8 and 0xcf9 on most PC chipsets), MMIO | |
382 | * regions overlap wildly. For this reason we cannot clamp the accesses | |
383 | * here. | |
384 | * | |
385 | * If the length is small (as is the case for address_space_ldl/stl), | |
386 | * everything works fine. If the incoming length is large, however, | |
387 | * the caller really has to do the clamping through memory_access_size. | |
388 | */ | |
965eb2fc | 389 | if (memory_region_is_ram(mr)) { |
e4a511f8 | 390 | diff = int128_sub(section->size, int128_make64(addr)); |
965eb2fc PB |
391 | *plen = int128_get64(int128_min(diff, int128_make64(*plen))); |
392 | } | |
149f54b5 PB |
393 | return section; |
394 | } | |
90260c6c | 395 | |
a87f3954 PB |
396 | static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) |
397 | { | |
398 | if (memory_region_is_ram(mr)) { | |
399 | return !(is_write && mr->readonly); | |
400 | } | |
401 | if (memory_region_is_romd(mr)) { | |
402 | return !is_write; | |
403 | } | |
404 | ||
405 | return false; | |
406 | } | |
407 | ||
41063e1e | 408 | /* Called from RCU critical section */ |
5c8a00ce PB |
409 | MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr, |
410 | hwaddr *xlat, hwaddr *plen, | |
411 | bool is_write) | |
90260c6c | 412 | { |
30951157 AK |
413 | IOMMUTLBEntry iotlb; |
414 | MemoryRegionSection *section; | |
415 | MemoryRegion *mr; | |
30951157 AK |
416 | |
417 | for (;;) { | |
79e2b9ae PB |
418 | AddressSpaceDispatch *d = atomic_rcu_read(&as->dispatch); |
419 | section = address_space_translate_internal(d, addr, &addr, plen, true); | |
30951157 AK |
420 | mr = section->mr; |
421 | ||
422 | if (!mr->iommu_ops) { | |
423 | break; | |
424 | } | |
425 | ||
8d7b8cb9 | 426 | iotlb = mr->iommu_ops->translate(mr, addr, is_write); |
30951157 AK |
427 | addr = ((iotlb.translated_addr & ~iotlb.addr_mask) |
428 | | (addr & iotlb.addr_mask)); | |
23820dbf | 429 | *plen = MIN(*plen, (addr | iotlb.addr_mask) - addr + 1); |
30951157 AK |
430 | if (!(iotlb.perm & (1 << is_write))) { |
431 | mr = &io_mem_unassigned; | |
432 | break; | |
433 | } | |
434 | ||
435 | as = iotlb.target_as; | |
436 | } | |
437 | ||
fe680d0d | 438 | if (xen_enabled() && memory_access_is_direct(mr, is_write)) { |
a87f3954 | 439 | hwaddr page = ((addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE) - addr; |
23820dbf | 440 | *plen = MIN(page, *plen); |
a87f3954 PB |
441 | } |
442 | ||
30951157 AK |
443 | *xlat = addr; |
444 | return mr; | |
90260c6c JK |
445 | } |
446 | ||
79e2b9ae | 447 | /* Called from RCU critical section */ |
90260c6c | 448 | MemoryRegionSection * |
9d82b5a7 PB |
449 | address_space_translate_for_iotlb(CPUState *cpu, hwaddr addr, |
450 | hwaddr *xlat, hwaddr *plen) | |
90260c6c | 451 | { |
30951157 | 452 | MemoryRegionSection *section; |
32857f4d | 453 | section = address_space_translate_internal(cpu->cpu_ases[0].memory_dispatch, |
9d82b5a7 | 454 | addr, xlat, plen, false); |
30951157 AK |
455 | |
456 | assert(!section->mr->iommu_ops); | |
457 | return section; | |
90260c6c | 458 | } |
5b6dd868 | 459 | #endif |
fd6ce8f6 | 460 | |
b170fce3 | 461 | #if !defined(CONFIG_USER_ONLY) |
5b6dd868 BS |
462 | |
463 | static int cpu_common_post_load(void *opaque, int version_id) | |
fd6ce8f6 | 464 | { |
259186a7 | 465 | CPUState *cpu = opaque; |
a513fe19 | 466 | |
5b6dd868 BS |
467 | /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the |
468 | version_id is increased. */ | |
259186a7 | 469 | cpu->interrupt_request &= ~0x01; |
c01a71c1 | 470 | tlb_flush(cpu, 1); |
5b6dd868 BS |
471 | |
472 | return 0; | |
a513fe19 | 473 | } |
7501267e | 474 | |
6c3bff0e PD |
475 | static int cpu_common_pre_load(void *opaque) |
476 | { | |
477 | CPUState *cpu = opaque; | |
478 | ||
adee6424 | 479 | cpu->exception_index = -1; |
6c3bff0e PD |
480 | |
481 | return 0; | |
482 | } | |
483 | ||
484 | static bool cpu_common_exception_index_needed(void *opaque) | |
485 | { | |
486 | CPUState *cpu = opaque; | |
487 | ||
adee6424 | 488 | return tcg_enabled() && cpu->exception_index != -1; |
6c3bff0e PD |
489 | } |
490 | ||
491 | static const VMStateDescription vmstate_cpu_common_exception_index = { | |
492 | .name = "cpu_common/exception_index", | |
493 | .version_id = 1, | |
494 | .minimum_version_id = 1, | |
5cd8cada | 495 | .needed = cpu_common_exception_index_needed, |
6c3bff0e PD |
496 | .fields = (VMStateField[]) { |
497 | VMSTATE_INT32(exception_index, CPUState), | |
498 | VMSTATE_END_OF_LIST() | |
499 | } | |
500 | }; | |
501 | ||
bac05aa9 AS |
502 | static bool cpu_common_crash_occurred_needed(void *opaque) |
503 | { | |
504 | CPUState *cpu = opaque; | |
505 | ||
506 | return cpu->crash_occurred; | |
507 | } | |
508 | ||
509 | static const VMStateDescription vmstate_cpu_common_crash_occurred = { | |
510 | .name = "cpu_common/crash_occurred", | |
511 | .version_id = 1, | |
512 | .minimum_version_id = 1, | |
513 | .needed = cpu_common_crash_occurred_needed, | |
514 | .fields = (VMStateField[]) { | |
515 | VMSTATE_BOOL(crash_occurred, CPUState), | |
516 | VMSTATE_END_OF_LIST() | |
517 | } | |
518 | }; | |
519 | ||
1a1562f5 | 520 | const VMStateDescription vmstate_cpu_common = { |
5b6dd868 BS |
521 | .name = "cpu_common", |
522 | .version_id = 1, | |
523 | .minimum_version_id = 1, | |
6c3bff0e | 524 | .pre_load = cpu_common_pre_load, |
5b6dd868 | 525 | .post_load = cpu_common_post_load, |
35d08458 | 526 | .fields = (VMStateField[]) { |
259186a7 AF |
527 | VMSTATE_UINT32(halted, CPUState), |
528 | VMSTATE_UINT32(interrupt_request, CPUState), | |
5b6dd868 | 529 | VMSTATE_END_OF_LIST() |
6c3bff0e | 530 | }, |
5cd8cada JQ |
531 | .subsections = (const VMStateDescription*[]) { |
532 | &vmstate_cpu_common_exception_index, | |
bac05aa9 | 533 | &vmstate_cpu_common_crash_occurred, |
5cd8cada | 534 | NULL |
5b6dd868 BS |
535 | } |
536 | }; | |
1a1562f5 | 537 | |
5b6dd868 | 538 | #endif |
ea041c0e | 539 | |
38d8f5c8 | 540 | CPUState *qemu_get_cpu(int index) |
ea041c0e | 541 | { |
bdc44640 | 542 | CPUState *cpu; |
ea041c0e | 543 | |
bdc44640 | 544 | CPU_FOREACH(cpu) { |
55e5c285 | 545 | if (cpu->cpu_index == index) { |
bdc44640 | 546 | return cpu; |
55e5c285 | 547 | } |
ea041c0e | 548 | } |
5b6dd868 | 549 | |
bdc44640 | 550 | return NULL; |
ea041c0e FB |
551 | } |
552 | ||
09daed84 EI |
553 | #if !defined(CONFIG_USER_ONLY) |
554 | void tcg_cpu_address_space_init(CPUState *cpu, AddressSpace *as) | |
555 | { | |
556 | /* We only support one address space per cpu at the moment. */ | |
557 | assert(cpu->as == as); | |
558 | ||
32857f4d PM |
559 | if (cpu->cpu_ases) { |
560 | /* We've already registered the listener for our only AS */ | |
561 | return; | |
09daed84 | 562 | } |
32857f4d PM |
563 | |
564 | cpu->cpu_ases = g_new0(CPUAddressSpace, 1); | |
565 | cpu->cpu_ases[0].cpu = cpu; | |
566 | cpu->cpu_ases[0].as = as; | |
567 | cpu->cpu_ases[0].tcg_as_listener.commit = tcg_commit; | |
568 | memory_listener_register(&cpu->cpu_ases[0].tcg_as_listener, as); | |
09daed84 EI |
569 | } |
570 | #endif | |
571 | ||
b7bca733 BR |
572 | #ifndef CONFIG_USER_ONLY |
573 | static DECLARE_BITMAP(cpu_index_map, MAX_CPUMASK_BITS); | |
574 | ||
575 | static int cpu_get_free_index(Error **errp) | |
576 | { | |
577 | int cpu = find_first_zero_bit(cpu_index_map, MAX_CPUMASK_BITS); | |
578 | ||
579 | if (cpu >= MAX_CPUMASK_BITS) { | |
580 | error_setg(errp, "Trying to use more CPUs than max of %d", | |
581 | MAX_CPUMASK_BITS); | |
582 | return -1; | |
583 | } | |
584 | ||
585 | bitmap_set(cpu_index_map, cpu, 1); | |
586 | return cpu; | |
587 | } | |
588 | ||
589 | void cpu_exec_exit(CPUState *cpu) | |
590 | { | |
591 | if (cpu->cpu_index == -1) { | |
592 | /* cpu_index was never allocated by this @cpu or was already freed. */ | |
593 | return; | |
594 | } | |
595 | ||
596 | bitmap_clear(cpu_index_map, cpu->cpu_index, 1); | |
597 | cpu->cpu_index = -1; | |
598 | } | |
599 | #else | |
600 | ||
601 | static int cpu_get_free_index(Error **errp) | |
602 | { | |
603 | CPUState *some_cpu; | |
604 | int cpu_index = 0; | |
605 | ||
606 | CPU_FOREACH(some_cpu) { | |
607 | cpu_index++; | |
608 | } | |
609 | return cpu_index; | |
610 | } | |
611 | ||
612 | void cpu_exec_exit(CPUState *cpu) | |
613 | { | |
614 | } | |
615 | #endif | |
616 | ||
4bad9e39 | 617 | void cpu_exec_init(CPUState *cpu, Error **errp) |
ea041c0e | 618 | { |
b170fce3 | 619 | CPUClass *cc = CPU_GET_CLASS(cpu); |
5b6dd868 | 620 | int cpu_index; |
b7bca733 | 621 | Error *local_err = NULL; |
5b6dd868 | 622 | |
291135b5 EH |
623 | #ifndef CONFIG_USER_ONLY |
624 | cpu->as = &address_space_memory; | |
625 | cpu->thread_id = qemu_get_thread_id(); | |
291135b5 EH |
626 | #endif |
627 | ||
5b6dd868 BS |
628 | #if defined(CONFIG_USER_ONLY) |
629 | cpu_list_lock(); | |
630 | #endif | |
b7bca733 BR |
631 | cpu_index = cpu->cpu_index = cpu_get_free_index(&local_err); |
632 | if (local_err) { | |
633 | error_propagate(errp, local_err); | |
634 | #if defined(CONFIG_USER_ONLY) | |
635 | cpu_list_unlock(); | |
636 | #endif | |
637 | return; | |
5b6dd868 | 638 | } |
bdc44640 | 639 | QTAILQ_INSERT_TAIL(&cpus, cpu, node); |
5b6dd868 BS |
640 | #if defined(CONFIG_USER_ONLY) |
641 | cpu_list_unlock(); | |
642 | #endif | |
e0d47944 AF |
643 | if (qdev_get_vmsd(DEVICE(cpu)) == NULL) { |
644 | vmstate_register(NULL, cpu_index, &vmstate_cpu_common, cpu); | |
645 | } | |
5b6dd868 | 646 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
5b6dd868 | 647 | register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION, |
4bad9e39 | 648 | cpu_save, cpu_load, cpu->env_ptr); |
b170fce3 | 649 | assert(cc->vmsd == NULL); |
e0d47944 | 650 | assert(qdev_get_vmsd(DEVICE(cpu)) == NULL); |
5b6dd868 | 651 | #endif |
b170fce3 AF |
652 | if (cc->vmsd != NULL) { |
653 | vmstate_register(NULL, cpu_index, cc->vmsd, cpu); | |
654 | } | |
ea041c0e FB |
655 | } |
656 | ||
94df27fd | 657 | #if defined(CONFIG_USER_ONLY) |
00b941e5 | 658 | static void breakpoint_invalidate(CPUState *cpu, target_ulong pc) |
94df27fd PB |
659 | { |
660 | tb_invalidate_phys_page_range(pc, pc + 1, 0); | |
661 | } | |
662 | #else | |
00b941e5 | 663 | static void breakpoint_invalidate(CPUState *cpu, target_ulong pc) |
1e7855a5 | 664 | { |
e8262a1b MF |
665 | hwaddr phys = cpu_get_phys_page_debug(cpu, pc); |
666 | if (phys != -1) { | |
09daed84 | 667 | tb_invalidate_phys_addr(cpu->as, |
29d8ec7b | 668 | phys | (pc & ~TARGET_PAGE_MASK)); |
e8262a1b | 669 | } |
1e7855a5 | 670 | } |
c27004ec | 671 | #endif |
d720b93d | 672 | |
c527ee8f | 673 | #if defined(CONFIG_USER_ONLY) |
75a34036 | 674 | void cpu_watchpoint_remove_all(CPUState *cpu, int mask) |
c527ee8f PB |
675 | |
676 | { | |
677 | } | |
678 | ||
3ee887e8 PM |
679 | int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len, |
680 | int flags) | |
681 | { | |
682 | return -ENOSYS; | |
683 | } | |
684 | ||
685 | void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint) | |
686 | { | |
687 | } | |
688 | ||
75a34036 | 689 | int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len, |
c527ee8f PB |
690 | int flags, CPUWatchpoint **watchpoint) |
691 | { | |
692 | return -ENOSYS; | |
693 | } | |
694 | #else | |
6658ffb8 | 695 | /* Add a watchpoint. */ |
75a34036 | 696 | int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len, |
a1d1bb31 | 697 | int flags, CPUWatchpoint **watchpoint) |
6658ffb8 | 698 | { |
c0ce998e | 699 | CPUWatchpoint *wp; |
6658ffb8 | 700 | |
05068c0d | 701 | /* forbid ranges which are empty or run off the end of the address space */ |
07e2863d | 702 | if (len == 0 || (addr + len - 1) < addr) { |
75a34036 AF |
703 | error_report("tried to set invalid watchpoint at %" |
704 | VADDR_PRIx ", len=%" VADDR_PRIu, addr, len); | |
b4051334 AL |
705 | return -EINVAL; |
706 | } | |
7267c094 | 707 | wp = g_malloc(sizeof(*wp)); |
a1d1bb31 AL |
708 | |
709 | wp->vaddr = addr; | |
05068c0d | 710 | wp->len = len; |
a1d1bb31 AL |
711 | wp->flags = flags; |
712 | ||
2dc9f411 | 713 | /* keep all GDB-injected watchpoints in front */ |
ff4700b0 AF |
714 | if (flags & BP_GDB) { |
715 | QTAILQ_INSERT_HEAD(&cpu->watchpoints, wp, entry); | |
716 | } else { | |
717 | QTAILQ_INSERT_TAIL(&cpu->watchpoints, wp, entry); | |
718 | } | |
6658ffb8 | 719 | |
31b030d4 | 720 | tlb_flush_page(cpu, addr); |
a1d1bb31 AL |
721 | |
722 | if (watchpoint) | |
723 | *watchpoint = wp; | |
724 | return 0; | |
6658ffb8 PB |
725 | } |
726 | ||
a1d1bb31 | 727 | /* Remove a specific watchpoint. */ |
75a34036 | 728 | int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len, |
a1d1bb31 | 729 | int flags) |
6658ffb8 | 730 | { |
a1d1bb31 | 731 | CPUWatchpoint *wp; |
6658ffb8 | 732 | |
ff4700b0 | 733 | QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) { |
05068c0d | 734 | if (addr == wp->vaddr && len == wp->len |
6e140f28 | 735 | && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) { |
75a34036 | 736 | cpu_watchpoint_remove_by_ref(cpu, wp); |
6658ffb8 PB |
737 | return 0; |
738 | } | |
739 | } | |
a1d1bb31 | 740 | return -ENOENT; |
6658ffb8 PB |
741 | } |
742 | ||
a1d1bb31 | 743 | /* Remove a specific watchpoint by reference. */ |
75a34036 | 744 | void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint) |
a1d1bb31 | 745 | { |
ff4700b0 | 746 | QTAILQ_REMOVE(&cpu->watchpoints, watchpoint, entry); |
7d03f82f | 747 | |
31b030d4 | 748 | tlb_flush_page(cpu, watchpoint->vaddr); |
a1d1bb31 | 749 | |
7267c094 | 750 | g_free(watchpoint); |
a1d1bb31 AL |
751 | } |
752 | ||
753 | /* Remove all matching watchpoints. */ | |
75a34036 | 754 | void cpu_watchpoint_remove_all(CPUState *cpu, int mask) |
a1d1bb31 | 755 | { |
c0ce998e | 756 | CPUWatchpoint *wp, *next; |
a1d1bb31 | 757 | |
ff4700b0 | 758 | QTAILQ_FOREACH_SAFE(wp, &cpu->watchpoints, entry, next) { |
75a34036 AF |
759 | if (wp->flags & mask) { |
760 | cpu_watchpoint_remove_by_ref(cpu, wp); | |
761 | } | |
c0ce998e | 762 | } |
7d03f82f | 763 | } |
05068c0d PM |
764 | |
765 | /* Return true if this watchpoint address matches the specified | |
766 | * access (ie the address range covered by the watchpoint overlaps | |
767 | * partially or completely with the address range covered by the | |
768 | * access). | |
769 | */ | |
770 | static inline bool cpu_watchpoint_address_matches(CPUWatchpoint *wp, | |
771 | vaddr addr, | |
772 | vaddr len) | |
773 | { | |
774 | /* We know the lengths are non-zero, but a little caution is | |
775 | * required to avoid errors in the case where the range ends | |
776 | * exactly at the top of the address space and so addr + len | |
777 | * wraps round to zero. | |
778 | */ | |
779 | vaddr wpend = wp->vaddr + wp->len - 1; | |
780 | vaddr addrend = addr + len - 1; | |
781 | ||
782 | return !(addr > wpend || wp->vaddr > addrend); | |
783 | } | |
784 | ||
c527ee8f | 785 | #endif |
7d03f82f | 786 | |
a1d1bb31 | 787 | /* Add a breakpoint. */ |
b3310ab3 | 788 | int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags, |
a1d1bb31 | 789 | CPUBreakpoint **breakpoint) |
4c3a88a2 | 790 | { |
c0ce998e | 791 | CPUBreakpoint *bp; |
3b46e624 | 792 | |
7267c094 | 793 | bp = g_malloc(sizeof(*bp)); |
4c3a88a2 | 794 | |
a1d1bb31 AL |
795 | bp->pc = pc; |
796 | bp->flags = flags; | |
797 | ||
2dc9f411 | 798 | /* keep all GDB-injected breakpoints in front */ |
00b941e5 | 799 | if (flags & BP_GDB) { |
f0c3c505 | 800 | QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry); |
00b941e5 | 801 | } else { |
f0c3c505 | 802 | QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry); |
00b941e5 | 803 | } |
3b46e624 | 804 | |
f0c3c505 | 805 | breakpoint_invalidate(cpu, pc); |
a1d1bb31 | 806 | |
00b941e5 | 807 | if (breakpoint) { |
a1d1bb31 | 808 | *breakpoint = bp; |
00b941e5 | 809 | } |
4c3a88a2 | 810 | return 0; |
4c3a88a2 FB |
811 | } |
812 | ||
a1d1bb31 | 813 | /* Remove a specific breakpoint. */ |
b3310ab3 | 814 | int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags) |
a1d1bb31 | 815 | { |
a1d1bb31 AL |
816 | CPUBreakpoint *bp; |
817 | ||
f0c3c505 | 818 | QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) { |
a1d1bb31 | 819 | if (bp->pc == pc && bp->flags == flags) { |
b3310ab3 | 820 | cpu_breakpoint_remove_by_ref(cpu, bp); |
a1d1bb31 AL |
821 | return 0; |
822 | } | |
7d03f82f | 823 | } |
a1d1bb31 | 824 | return -ENOENT; |
7d03f82f EI |
825 | } |
826 | ||
a1d1bb31 | 827 | /* Remove a specific breakpoint by reference. */ |
b3310ab3 | 828 | void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint) |
4c3a88a2 | 829 | { |
f0c3c505 AF |
830 | QTAILQ_REMOVE(&cpu->breakpoints, breakpoint, entry); |
831 | ||
832 | breakpoint_invalidate(cpu, breakpoint->pc); | |
a1d1bb31 | 833 | |
7267c094 | 834 | g_free(breakpoint); |
a1d1bb31 AL |
835 | } |
836 | ||
837 | /* Remove all matching breakpoints. */ | |
b3310ab3 | 838 | void cpu_breakpoint_remove_all(CPUState *cpu, int mask) |
a1d1bb31 | 839 | { |
c0ce998e | 840 | CPUBreakpoint *bp, *next; |
a1d1bb31 | 841 | |
f0c3c505 | 842 | QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) { |
b3310ab3 AF |
843 | if (bp->flags & mask) { |
844 | cpu_breakpoint_remove_by_ref(cpu, bp); | |
845 | } | |
c0ce998e | 846 | } |
4c3a88a2 FB |
847 | } |
848 | ||
c33a346e FB |
849 | /* enable or disable single step mode. EXCP_DEBUG is returned by the |
850 | CPU loop after each instruction */ | |
3825b28f | 851 | void cpu_single_step(CPUState *cpu, int enabled) |
c33a346e | 852 | { |
ed2803da AF |
853 | if (cpu->singlestep_enabled != enabled) { |
854 | cpu->singlestep_enabled = enabled; | |
855 | if (kvm_enabled()) { | |
38e478ec | 856 | kvm_update_guest_debug(cpu, 0); |
ed2803da | 857 | } else { |
ccbb4d44 | 858 | /* must flush all the translated code to avoid inconsistencies */ |
e22a25c9 | 859 | /* XXX: only flush what is necessary */ |
bbd77c18 | 860 | tb_flush(cpu); |
e22a25c9 | 861 | } |
c33a346e | 862 | } |
c33a346e FB |
863 | } |
864 | ||
a47dddd7 | 865 | void cpu_abort(CPUState *cpu, const char *fmt, ...) |
7501267e FB |
866 | { |
867 | va_list ap; | |
493ae1f0 | 868 | va_list ap2; |
7501267e FB |
869 | |
870 | va_start(ap, fmt); | |
493ae1f0 | 871 | va_copy(ap2, ap); |
7501267e FB |
872 | fprintf(stderr, "qemu: fatal: "); |
873 | vfprintf(stderr, fmt, ap); | |
874 | fprintf(stderr, "\n"); | |
878096ee | 875 | cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP); |
93fcfe39 AL |
876 | if (qemu_log_enabled()) { |
877 | qemu_log("qemu: fatal: "); | |
878 | qemu_log_vprintf(fmt, ap2); | |
879 | qemu_log("\n"); | |
a0762859 | 880 | log_cpu_state(cpu, CPU_DUMP_FPU | CPU_DUMP_CCOP); |
31b1a7b4 | 881 | qemu_log_flush(); |
93fcfe39 | 882 | qemu_log_close(); |
924edcae | 883 | } |
493ae1f0 | 884 | va_end(ap2); |
f9373291 | 885 | va_end(ap); |
7615936e | 886 | replay_finish(); |
fd052bf6 RV |
887 | #if defined(CONFIG_USER_ONLY) |
888 | { | |
889 | struct sigaction act; | |
890 | sigfillset(&act.sa_mask); | |
891 | act.sa_handler = SIG_DFL; | |
892 | sigaction(SIGABRT, &act, NULL); | |
893 | } | |
894 | #endif | |
7501267e FB |
895 | abort(); |
896 | } | |
897 | ||
0124311e | 898 | #if !defined(CONFIG_USER_ONLY) |
0dc3f44a | 899 | /* Called from RCU critical section */ |
041603fe PB |
900 | static RAMBlock *qemu_get_ram_block(ram_addr_t addr) |
901 | { | |
902 | RAMBlock *block; | |
903 | ||
43771539 | 904 | block = atomic_rcu_read(&ram_list.mru_block); |
9b8424d5 | 905 | if (block && addr - block->offset < block->max_length) { |
68851b98 | 906 | return block; |
041603fe | 907 | } |
0dc3f44a | 908 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
9b8424d5 | 909 | if (addr - block->offset < block->max_length) { |
041603fe PB |
910 | goto found; |
911 | } | |
912 | } | |
913 | ||
914 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); | |
915 | abort(); | |
916 | ||
917 | found: | |
43771539 PB |
918 | /* It is safe to write mru_block outside the iothread lock. This |
919 | * is what happens: | |
920 | * | |
921 | * mru_block = xxx | |
922 | * rcu_read_unlock() | |
923 | * xxx removed from list | |
924 | * rcu_read_lock() | |
925 | * read mru_block | |
926 | * mru_block = NULL; | |
927 | * call_rcu(reclaim_ramblock, xxx); | |
928 | * rcu_read_unlock() | |
929 | * | |
930 | * atomic_rcu_set is not needed here. The block was already published | |
931 | * when it was placed into the list. Here we're just making an extra | |
932 | * copy of the pointer. | |
933 | */ | |
041603fe PB |
934 | ram_list.mru_block = block; |
935 | return block; | |
936 | } | |
937 | ||
a2f4d5be | 938 | static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length) |
d24981d3 | 939 | { |
9a13565d | 940 | CPUState *cpu; |
041603fe | 941 | ram_addr_t start1; |
a2f4d5be JQ |
942 | RAMBlock *block; |
943 | ram_addr_t end; | |
944 | ||
945 | end = TARGET_PAGE_ALIGN(start + length); | |
946 | start &= TARGET_PAGE_MASK; | |
d24981d3 | 947 | |
0dc3f44a | 948 | rcu_read_lock(); |
041603fe PB |
949 | block = qemu_get_ram_block(start); |
950 | assert(block == qemu_get_ram_block(end - 1)); | |
1240be24 | 951 | start1 = (uintptr_t)ramblock_ptr(block, start - block->offset); |
9a13565d PC |
952 | CPU_FOREACH(cpu) { |
953 | tlb_reset_dirty(cpu, start1, length); | |
954 | } | |
0dc3f44a | 955 | rcu_read_unlock(); |
d24981d3 JQ |
956 | } |
957 | ||
5579c7f3 | 958 | /* Note: start and end must be within the same ram block. */ |
03eebc9e SH |
959 | bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start, |
960 | ram_addr_t length, | |
961 | unsigned client) | |
1ccde1cb | 962 | { |
03eebc9e SH |
963 | unsigned long end, page; |
964 | bool dirty; | |
965 | ||
966 | if (length == 0) { | |
967 | return false; | |
968 | } | |
f23db169 | 969 | |
03eebc9e SH |
970 | end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS; |
971 | page = start >> TARGET_PAGE_BITS; | |
972 | dirty = bitmap_test_and_clear_atomic(ram_list.dirty_memory[client], | |
973 | page, end - page); | |
974 | ||
975 | if (dirty && tcg_enabled()) { | |
a2f4d5be | 976 | tlb_reset_dirty_range_all(start, length); |
5579c7f3 | 977 | } |
03eebc9e SH |
978 | |
979 | return dirty; | |
1ccde1cb FB |
980 | } |
981 | ||
79e2b9ae | 982 | /* Called from RCU critical section */ |
bb0e627a | 983 | hwaddr memory_region_section_get_iotlb(CPUState *cpu, |
149f54b5 PB |
984 | MemoryRegionSection *section, |
985 | target_ulong vaddr, | |
986 | hwaddr paddr, hwaddr xlat, | |
987 | int prot, | |
988 | target_ulong *address) | |
e5548617 | 989 | { |
a8170e5e | 990 | hwaddr iotlb; |
e5548617 BS |
991 | CPUWatchpoint *wp; |
992 | ||
cc5bea60 | 993 | if (memory_region_is_ram(section->mr)) { |
e5548617 BS |
994 | /* Normal RAM. */ |
995 | iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK) | |
149f54b5 | 996 | + xlat; |
e5548617 | 997 | if (!section->readonly) { |
b41aac4f | 998 | iotlb |= PHYS_SECTION_NOTDIRTY; |
e5548617 | 999 | } else { |
b41aac4f | 1000 | iotlb |= PHYS_SECTION_ROM; |
e5548617 BS |
1001 | } |
1002 | } else { | |
0b8e2c10 PM |
1003 | AddressSpaceDispatch *d; |
1004 | ||
1005 | d = atomic_rcu_read(§ion->address_space->dispatch); | |
1006 | iotlb = section - d->map.sections; | |
149f54b5 | 1007 | iotlb += xlat; |
e5548617 BS |
1008 | } |
1009 | ||
1010 | /* Make accesses to pages with watchpoints go via the | |
1011 | watchpoint trap routines. */ | |
ff4700b0 | 1012 | QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) { |
05068c0d | 1013 | if (cpu_watchpoint_address_matches(wp, vaddr, TARGET_PAGE_SIZE)) { |
e5548617 BS |
1014 | /* Avoid trapping reads of pages with a write breakpoint. */ |
1015 | if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) { | |
b41aac4f | 1016 | iotlb = PHYS_SECTION_WATCH + paddr; |
e5548617 BS |
1017 | *address |= TLB_MMIO; |
1018 | break; | |
1019 | } | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | return iotlb; | |
1024 | } | |
9fa3e853 FB |
1025 | #endif /* defined(CONFIG_USER_ONLY) */ |
1026 | ||
e2eef170 | 1027 | #if !defined(CONFIG_USER_ONLY) |
8da3ff18 | 1028 | |
c227f099 | 1029 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
5312bd8b | 1030 | uint16_t section); |
acc9d80b | 1031 | static subpage_t *subpage_init(AddressSpace *as, hwaddr base); |
54688b1e | 1032 | |
a2b257d6 IM |
1033 | static void *(*phys_mem_alloc)(size_t size, uint64_t *align) = |
1034 | qemu_anon_ram_alloc; | |
91138037 MA |
1035 | |
1036 | /* | |
1037 | * Set a custom physical guest memory alloator. | |
1038 | * Accelerators with unusual needs may need this. Hopefully, we can | |
1039 | * get rid of it eventually. | |
1040 | */ | |
a2b257d6 | 1041 | void phys_mem_set_alloc(void *(*alloc)(size_t, uint64_t *align)) |
91138037 MA |
1042 | { |
1043 | phys_mem_alloc = alloc; | |
1044 | } | |
1045 | ||
53cb28cb MA |
1046 | static uint16_t phys_section_add(PhysPageMap *map, |
1047 | MemoryRegionSection *section) | |
5312bd8b | 1048 | { |
68f3f65b PB |
1049 | /* The physical section number is ORed with a page-aligned |
1050 | * pointer to produce the iotlb entries. Thus it should | |
1051 | * never overflow into the page-aligned value. | |
1052 | */ | |
53cb28cb | 1053 | assert(map->sections_nb < TARGET_PAGE_SIZE); |
68f3f65b | 1054 | |
53cb28cb MA |
1055 | if (map->sections_nb == map->sections_nb_alloc) { |
1056 | map->sections_nb_alloc = MAX(map->sections_nb_alloc * 2, 16); | |
1057 | map->sections = g_renew(MemoryRegionSection, map->sections, | |
1058 | map->sections_nb_alloc); | |
5312bd8b | 1059 | } |
53cb28cb | 1060 | map->sections[map->sections_nb] = *section; |
dfde4e6e | 1061 | memory_region_ref(section->mr); |
53cb28cb | 1062 | return map->sections_nb++; |
5312bd8b AK |
1063 | } |
1064 | ||
058bc4b5 PB |
1065 | static void phys_section_destroy(MemoryRegion *mr) |
1066 | { | |
dfde4e6e PB |
1067 | memory_region_unref(mr); |
1068 | ||
058bc4b5 PB |
1069 | if (mr->subpage) { |
1070 | subpage_t *subpage = container_of(mr, subpage_t, iomem); | |
b4fefef9 | 1071 | object_unref(OBJECT(&subpage->iomem)); |
058bc4b5 PB |
1072 | g_free(subpage); |
1073 | } | |
1074 | } | |
1075 | ||
6092666e | 1076 | static void phys_sections_free(PhysPageMap *map) |
5312bd8b | 1077 | { |
9affd6fc PB |
1078 | while (map->sections_nb > 0) { |
1079 | MemoryRegionSection *section = &map->sections[--map->sections_nb]; | |
058bc4b5 PB |
1080 | phys_section_destroy(section->mr); |
1081 | } | |
9affd6fc PB |
1082 | g_free(map->sections); |
1083 | g_free(map->nodes); | |
5312bd8b AK |
1084 | } |
1085 | ||
ac1970fb | 1086 | static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section) |
0f0cb164 AK |
1087 | { |
1088 | subpage_t *subpage; | |
a8170e5e | 1089 | hwaddr base = section->offset_within_address_space |
0f0cb164 | 1090 | & TARGET_PAGE_MASK; |
97115a8d | 1091 | MemoryRegionSection *existing = phys_page_find(d->phys_map, base, |
53cb28cb | 1092 | d->map.nodes, d->map.sections); |
0f0cb164 AK |
1093 | MemoryRegionSection subsection = { |
1094 | .offset_within_address_space = base, | |
052e87b0 | 1095 | .size = int128_make64(TARGET_PAGE_SIZE), |
0f0cb164 | 1096 | }; |
a8170e5e | 1097 | hwaddr start, end; |
0f0cb164 | 1098 | |
f3705d53 | 1099 | assert(existing->mr->subpage || existing->mr == &io_mem_unassigned); |
0f0cb164 | 1100 | |
f3705d53 | 1101 | if (!(existing->mr->subpage)) { |
acc9d80b | 1102 | subpage = subpage_init(d->as, base); |
3be91e86 | 1103 | subsection.address_space = d->as; |
0f0cb164 | 1104 | subsection.mr = &subpage->iomem; |
ac1970fb | 1105 | phys_page_set(d, base >> TARGET_PAGE_BITS, 1, |
53cb28cb | 1106 | phys_section_add(&d->map, &subsection)); |
0f0cb164 | 1107 | } else { |
f3705d53 | 1108 | subpage = container_of(existing->mr, subpage_t, iomem); |
0f0cb164 AK |
1109 | } |
1110 | start = section->offset_within_address_space & ~TARGET_PAGE_MASK; | |
052e87b0 | 1111 | end = start + int128_get64(section->size) - 1; |
53cb28cb MA |
1112 | subpage_register(subpage, start, end, |
1113 | phys_section_add(&d->map, section)); | |
0f0cb164 AK |
1114 | } |
1115 | ||
1116 | ||
052e87b0 PB |
1117 | static void register_multipage(AddressSpaceDispatch *d, |
1118 | MemoryRegionSection *section) | |
33417e70 | 1119 | { |
a8170e5e | 1120 | hwaddr start_addr = section->offset_within_address_space; |
53cb28cb | 1121 | uint16_t section_index = phys_section_add(&d->map, section); |
052e87b0 PB |
1122 | uint64_t num_pages = int128_get64(int128_rshift(section->size, |
1123 | TARGET_PAGE_BITS)); | |
dd81124b | 1124 | |
733d5ef5 PB |
1125 | assert(num_pages); |
1126 | phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index); | |
33417e70 FB |
1127 | } |
1128 | ||
ac1970fb | 1129 | static void mem_add(MemoryListener *listener, MemoryRegionSection *section) |
0f0cb164 | 1130 | { |
89ae337a | 1131 | AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener); |
00752703 | 1132 | AddressSpaceDispatch *d = as->next_dispatch; |
99b9cc06 | 1133 | MemoryRegionSection now = *section, remain = *section; |
052e87b0 | 1134 | Int128 page_size = int128_make64(TARGET_PAGE_SIZE); |
0f0cb164 | 1135 | |
733d5ef5 PB |
1136 | if (now.offset_within_address_space & ~TARGET_PAGE_MASK) { |
1137 | uint64_t left = TARGET_PAGE_ALIGN(now.offset_within_address_space) | |
1138 | - now.offset_within_address_space; | |
1139 | ||
052e87b0 | 1140 | now.size = int128_min(int128_make64(left), now.size); |
ac1970fb | 1141 | register_subpage(d, &now); |
733d5ef5 | 1142 | } else { |
052e87b0 | 1143 | now.size = int128_zero(); |
733d5ef5 | 1144 | } |
052e87b0 PB |
1145 | while (int128_ne(remain.size, now.size)) { |
1146 | remain.size = int128_sub(remain.size, now.size); | |
1147 | remain.offset_within_address_space += int128_get64(now.size); | |
1148 | remain.offset_within_region += int128_get64(now.size); | |
69b67646 | 1149 | now = remain; |
052e87b0 | 1150 | if (int128_lt(remain.size, page_size)) { |
733d5ef5 | 1151 | register_subpage(d, &now); |
88266249 | 1152 | } else if (remain.offset_within_address_space & ~TARGET_PAGE_MASK) { |
052e87b0 | 1153 | now.size = page_size; |
ac1970fb | 1154 | register_subpage(d, &now); |
69b67646 | 1155 | } else { |
052e87b0 | 1156 | now.size = int128_and(now.size, int128_neg(page_size)); |
ac1970fb | 1157 | register_multipage(d, &now); |
69b67646 | 1158 | } |
0f0cb164 AK |
1159 | } |
1160 | } | |
1161 | ||
62a2744c SY |
1162 | void qemu_flush_coalesced_mmio_buffer(void) |
1163 | { | |
1164 | if (kvm_enabled()) | |
1165 | kvm_flush_coalesced_mmio_buffer(); | |
1166 | } | |
1167 | ||
b2a8658e UD |
1168 | void qemu_mutex_lock_ramlist(void) |
1169 | { | |
1170 | qemu_mutex_lock(&ram_list.mutex); | |
1171 | } | |
1172 | ||
1173 | void qemu_mutex_unlock_ramlist(void) | |
1174 | { | |
1175 | qemu_mutex_unlock(&ram_list.mutex); | |
1176 | } | |
1177 | ||
e1e84ba0 | 1178 | #ifdef __linux__ |
c902760f MT |
1179 | |
1180 | #include <sys/vfs.h> | |
1181 | ||
1182 | #define HUGETLBFS_MAGIC 0x958458f6 | |
1183 | ||
fc7a5800 | 1184 | static long gethugepagesize(const char *path, Error **errp) |
c902760f MT |
1185 | { |
1186 | struct statfs fs; | |
1187 | int ret; | |
1188 | ||
1189 | do { | |
9742bf26 | 1190 | ret = statfs(path, &fs); |
c902760f MT |
1191 | } while (ret != 0 && errno == EINTR); |
1192 | ||
1193 | if (ret != 0) { | |
fc7a5800 HT |
1194 | error_setg_errno(errp, errno, "failed to get page size of file %s", |
1195 | path); | |
9742bf26 | 1196 | return 0; |
c902760f MT |
1197 | } |
1198 | ||
1199 | if (fs.f_type != HUGETLBFS_MAGIC) | |
9742bf26 | 1200 | fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path); |
c902760f MT |
1201 | |
1202 | return fs.f_bsize; | |
1203 | } | |
1204 | ||
04b16653 AW |
1205 | static void *file_ram_alloc(RAMBlock *block, |
1206 | ram_addr_t memory, | |
7f56e740 PB |
1207 | const char *path, |
1208 | Error **errp) | |
c902760f | 1209 | { |
8d31d6b6 | 1210 | struct stat st; |
c902760f | 1211 | char *filename; |
8ca761f6 PF |
1212 | char *sanitized_name; |
1213 | char *c; | |
794e8f30 | 1214 | void *area; |
c902760f | 1215 | int fd; |
557529dd | 1216 | uint64_t hpagesize; |
fc7a5800 | 1217 | Error *local_err = NULL; |
c902760f | 1218 | |
fc7a5800 HT |
1219 | hpagesize = gethugepagesize(path, &local_err); |
1220 | if (local_err) { | |
1221 | error_propagate(errp, local_err); | |
f9a49dfa | 1222 | goto error; |
c902760f | 1223 | } |
a2b257d6 | 1224 | block->mr->align = hpagesize; |
c902760f MT |
1225 | |
1226 | if (memory < hpagesize) { | |
557529dd HT |
1227 | error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to " |
1228 | "or larger than huge page size 0x%" PRIx64, | |
1229 | memory, hpagesize); | |
1230 | goto error; | |
c902760f MT |
1231 | } |
1232 | ||
1233 | if (kvm_enabled() && !kvm_has_sync_mmu()) { | |
7f56e740 PB |
1234 | error_setg(errp, |
1235 | "host lacks kvm mmu notifiers, -mem-path unsupported"); | |
f9a49dfa | 1236 | goto error; |
c902760f MT |
1237 | } |
1238 | ||
8d31d6b6 PF |
1239 | if (!stat(path, &st) && S_ISDIR(st.st_mode)) { |
1240 | /* Make name safe to use with mkstemp by replacing '/' with '_'. */ | |
1241 | sanitized_name = g_strdup(memory_region_name(block->mr)); | |
1242 | for (c = sanitized_name; *c != '\0'; c++) { | |
1243 | if (*c == '/') { | |
1244 | *c = '_'; | |
1245 | } | |
1246 | } | |
8ca761f6 | 1247 | |
8d31d6b6 PF |
1248 | filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path, |
1249 | sanitized_name); | |
1250 | g_free(sanitized_name); | |
1251 | ||
1252 | fd = mkstemp(filename); | |
1253 | if (fd >= 0) { | |
1254 | unlink(filename); | |
1255 | } | |
1256 | g_free(filename); | |
1257 | } else { | |
1258 | fd = open(path, O_RDWR | O_CREAT, 0644); | |
1259 | } | |
c902760f | 1260 | |
c902760f | 1261 | if (fd < 0) { |
7f56e740 PB |
1262 | error_setg_errno(errp, errno, |
1263 | "unable to create backing store for hugepages"); | |
f9a49dfa | 1264 | goto error; |
c902760f | 1265 | } |
c902760f | 1266 | |
9284f319 | 1267 | memory = ROUND_UP(memory, hpagesize); |
c902760f MT |
1268 | |
1269 | /* | |
1270 | * ftruncate is not supported by hugetlbfs in older | |
1271 | * hosts, so don't bother bailing out on errors. | |
1272 | * If anything goes wrong with it under other filesystems, | |
1273 | * mmap will fail. | |
1274 | */ | |
7f56e740 | 1275 | if (ftruncate(fd, memory)) { |
9742bf26 | 1276 | perror("ftruncate"); |
7f56e740 | 1277 | } |
c902760f | 1278 | |
794e8f30 | 1279 | area = qemu_ram_mmap(fd, memory, hpagesize, block->flags & RAM_SHARED); |
c902760f | 1280 | if (area == MAP_FAILED) { |
7f56e740 PB |
1281 | error_setg_errno(errp, errno, |
1282 | "unable to map backing store for hugepages"); | |
9742bf26 | 1283 | close(fd); |
f9a49dfa | 1284 | goto error; |
c902760f | 1285 | } |
ef36fa14 MT |
1286 | |
1287 | if (mem_prealloc) { | |
38183310 | 1288 | os_mem_prealloc(fd, area, memory); |
ef36fa14 MT |
1289 | } |
1290 | ||
04b16653 | 1291 | block->fd = fd; |
c902760f | 1292 | return area; |
f9a49dfa MT |
1293 | |
1294 | error: | |
f9a49dfa | 1295 | return NULL; |
c902760f MT |
1296 | } |
1297 | #endif | |
1298 | ||
0dc3f44a | 1299 | /* Called with the ramlist lock held. */ |
d17b5288 | 1300 | static ram_addr_t find_ram_offset(ram_addr_t size) |
04b16653 AW |
1301 | { |
1302 | RAMBlock *block, *next_block; | |
3e837b2c | 1303 | ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX; |
04b16653 | 1304 | |
49cd9ac6 SH |
1305 | assert(size != 0); /* it would hand out same offset multiple times */ |
1306 | ||
0dc3f44a | 1307 | if (QLIST_EMPTY_RCU(&ram_list.blocks)) { |
04b16653 | 1308 | return 0; |
0d53d9fe | 1309 | } |
04b16653 | 1310 | |
0dc3f44a | 1311 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
f15fbc4b | 1312 | ram_addr_t end, next = RAM_ADDR_MAX; |
04b16653 | 1313 | |
62be4e3a | 1314 | end = block->offset + block->max_length; |
04b16653 | 1315 | |
0dc3f44a | 1316 | QLIST_FOREACH_RCU(next_block, &ram_list.blocks, next) { |
04b16653 AW |
1317 | if (next_block->offset >= end) { |
1318 | next = MIN(next, next_block->offset); | |
1319 | } | |
1320 | } | |
1321 | if (next - end >= size && next - end < mingap) { | |
3e837b2c | 1322 | offset = end; |
04b16653 AW |
1323 | mingap = next - end; |
1324 | } | |
1325 | } | |
3e837b2c AW |
1326 | |
1327 | if (offset == RAM_ADDR_MAX) { | |
1328 | fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n", | |
1329 | (uint64_t)size); | |
1330 | abort(); | |
1331 | } | |
1332 | ||
04b16653 AW |
1333 | return offset; |
1334 | } | |
1335 | ||
652d7ec2 | 1336 | ram_addr_t last_ram_offset(void) |
d17b5288 AW |
1337 | { |
1338 | RAMBlock *block; | |
1339 | ram_addr_t last = 0; | |
1340 | ||
0dc3f44a MD |
1341 | rcu_read_lock(); |
1342 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { | |
62be4e3a | 1343 | last = MAX(last, block->offset + block->max_length); |
0d53d9fe | 1344 | } |
0dc3f44a | 1345 | rcu_read_unlock(); |
d17b5288 AW |
1346 | return last; |
1347 | } | |
1348 | ||
ddb97f1d JB |
1349 | static void qemu_ram_setup_dump(void *addr, ram_addr_t size) |
1350 | { | |
1351 | int ret; | |
ddb97f1d JB |
1352 | |
1353 | /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */ | |
47c8ca53 | 1354 | if (!machine_dump_guest_core(current_machine)) { |
ddb97f1d JB |
1355 | ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP); |
1356 | if (ret) { | |
1357 | perror("qemu_madvise"); | |
1358 | fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, " | |
1359 | "but dump_guest_core=off specified\n"); | |
1360 | } | |
1361 | } | |
1362 | } | |
1363 | ||
0dc3f44a MD |
1364 | /* Called within an RCU critical section, or while the ramlist lock |
1365 | * is held. | |
1366 | */ | |
20cfe881 | 1367 | static RAMBlock *find_ram_block(ram_addr_t addr) |
84b89d78 | 1368 | { |
20cfe881 | 1369 | RAMBlock *block; |
84b89d78 | 1370 | |
0dc3f44a | 1371 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
c5705a77 | 1372 | if (block->offset == addr) { |
20cfe881 | 1373 | return block; |
c5705a77 AK |
1374 | } |
1375 | } | |
20cfe881 HT |
1376 | |
1377 | return NULL; | |
1378 | } | |
1379 | ||
ae3a7047 | 1380 | /* Called with iothread lock held. */ |
20cfe881 HT |
1381 | void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev) |
1382 | { | |
ae3a7047 | 1383 | RAMBlock *new_block, *block; |
20cfe881 | 1384 | |
0dc3f44a | 1385 | rcu_read_lock(); |
ae3a7047 | 1386 | new_block = find_ram_block(addr); |
c5705a77 AK |
1387 | assert(new_block); |
1388 | assert(!new_block->idstr[0]); | |
84b89d78 | 1389 | |
09e5ab63 AL |
1390 | if (dev) { |
1391 | char *id = qdev_get_dev_path(dev); | |
84b89d78 CM |
1392 | if (id) { |
1393 | snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id); | |
7267c094 | 1394 | g_free(id); |
84b89d78 CM |
1395 | } |
1396 | } | |
1397 | pstrcat(new_block->idstr, sizeof(new_block->idstr), name); | |
1398 | ||
0dc3f44a | 1399 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
c5705a77 | 1400 | if (block != new_block && !strcmp(block->idstr, new_block->idstr)) { |
84b89d78 CM |
1401 | fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n", |
1402 | new_block->idstr); | |
1403 | abort(); | |
1404 | } | |
1405 | } | |
0dc3f44a | 1406 | rcu_read_unlock(); |
c5705a77 AK |
1407 | } |
1408 | ||
ae3a7047 | 1409 | /* Called with iothread lock held. */ |
20cfe881 HT |
1410 | void qemu_ram_unset_idstr(ram_addr_t addr) |
1411 | { | |
ae3a7047 | 1412 | RAMBlock *block; |
20cfe881 | 1413 | |
ae3a7047 MD |
1414 | /* FIXME: arch_init.c assumes that this is not called throughout |
1415 | * migration. Ignore the problem since hot-unplug during migration | |
1416 | * does not work anyway. | |
1417 | */ | |
1418 | ||
0dc3f44a | 1419 | rcu_read_lock(); |
ae3a7047 | 1420 | block = find_ram_block(addr); |
20cfe881 HT |
1421 | if (block) { |
1422 | memset(block->idstr, 0, sizeof(block->idstr)); | |
1423 | } | |
0dc3f44a | 1424 | rcu_read_unlock(); |
20cfe881 HT |
1425 | } |
1426 | ||
8490fc78 LC |
1427 | static int memory_try_enable_merging(void *addr, size_t len) |
1428 | { | |
75cc7f01 | 1429 | if (!machine_mem_merge(current_machine)) { |
8490fc78 LC |
1430 | /* disabled by the user */ |
1431 | return 0; | |
1432 | } | |
1433 | ||
1434 | return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE); | |
1435 | } | |
1436 | ||
62be4e3a MT |
1437 | /* Only legal before guest might have detected the memory size: e.g. on |
1438 | * incoming migration, or right after reset. | |
1439 | * | |
1440 | * As memory core doesn't know how is memory accessed, it is up to | |
1441 | * resize callback to update device state and/or add assertions to detect | |
1442 | * misuse, if necessary. | |
1443 | */ | |
1444 | int qemu_ram_resize(ram_addr_t base, ram_addr_t newsize, Error **errp) | |
1445 | { | |
1446 | RAMBlock *block = find_ram_block(base); | |
1447 | ||
1448 | assert(block); | |
1449 | ||
129ddaf3 MT |
1450 | newsize = TARGET_PAGE_ALIGN(newsize); |
1451 | ||
62be4e3a MT |
1452 | if (block->used_length == newsize) { |
1453 | return 0; | |
1454 | } | |
1455 | ||
1456 | if (!(block->flags & RAM_RESIZEABLE)) { | |
1457 | error_setg_errno(errp, EINVAL, | |
1458 | "Length mismatch: %s: 0x" RAM_ADDR_FMT | |
1459 | " in != 0x" RAM_ADDR_FMT, block->idstr, | |
1460 | newsize, block->used_length); | |
1461 | return -EINVAL; | |
1462 | } | |
1463 | ||
1464 | if (block->max_length < newsize) { | |
1465 | error_setg_errno(errp, EINVAL, | |
1466 | "Length too large: %s: 0x" RAM_ADDR_FMT | |
1467 | " > 0x" RAM_ADDR_FMT, block->idstr, | |
1468 | newsize, block->max_length); | |
1469 | return -EINVAL; | |
1470 | } | |
1471 | ||
1472 | cpu_physical_memory_clear_dirty_range(block->offset, block->used_length); | |
1473 | block->used_length = newsize; | |
58d2707e PB |
1474 | cpu_physical_memory_set_dirty_range(block->offset, block->used_length, |
1475 | DIRTY_CLIENTS_ALL); | |
62be4e3a MT |
1476 | memory_region_set_size(block->mr, newsize); |
1477 | if (block->resized) { | |
1478 | block->resized(block->idstr, newsize, block->host); | |
1479 | } | |
1480 | return 0; | |
1481 | } | |
1482 | ||
ef701d7b | 1483 | static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp) |
c5705a77 | 1484 | { |
e1c57ab8 | 1485 | RAMBlock *block; |
0d53d9fe | 1486 | RAMBlock *last_block = NULL; |
2152f5ca JQ |
1487 | ram_addr_t old_ram_size, new_ram_size; |
1488 | ||
1489 | old_ram_size = last_ram_offset() >> TARGET_PAGE_BITS; | |
c5705a77 | 1490 | |
b2a8658e | 1491 | qemu_mutex_lock_ramlist(); |
9b8424d5 | 1492 | new_block->offset = find_ram_offset(new_block->max_length); |
e1c57ab8 PB |
1493 | |
1494 | if (!new_block->host) { | |
1495 | if (xen_enabled()) { | |
9b8424d5 MT |
1496 | xen_ram_alloc(new_block->offset, new_block->max_length, |
1497 | new_block->mr); | |
e1c57ab8 | 1498 | } else { |
9b8424d5 | 1499 | new_block->host = phys_mem_alloc(new_block->max_length, |
a2b257d6 | 1500 | &new_block->mr->align); |
39228250 | 1501 | if (!new_block->host) { |
ef701d7b HT |
1502 | error_setg_errno(errp, errno, |
1503 | "cannot set up guest memory '%s'", | |
1504 | memory_region_name(new_block->mr)); | |
1505 | qemu_mutex_unlock_ramlist(); | |
1506 | return -1; | |
39228250 | 1507 | } |
9b8424d5 | 1508 | memory_try_enable_merging(new_block->host, new_block->max_length); |
6977dfe6 | 1509 | } |
c902760f | 1510 | } |
94a6b54f | 1511 | |
dd631697 LZ |
1512 | new_ram_size = MAX(old_ram_size, |
1513 | (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS); | |
1514 | if (new_ram_size > old_ram_size) { | |
1515 | migration_bitmap_extend(old_ram_size, new_ram_size); | |
1516 | } | |
0d53d9fe MD |
1517 | /* Keep the list sorted from biggest to smallest block. Unlike QTAILQ, |
1518 | * QLIST (which has an RCU-friendly variant) does not have insertion at | |
1519 | * tail, so save the last element in last_block. | |
1520 | */ | |
0dc3f44a | 1521 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
0d53d9fe | 1522 | last_block = block; |
9b8424d5 | 1523 | if (block->max_length < new_block->max_length) { |
abb26d63 PB |
1524 | break; |
1525 | } | |
1526 | } | |
1527 | if (block) { | |
0dc3f44a | 1528 | QLIST_INSERT_BEFORE_RCU(block, new_block, next); |
0d53d9fe | 1529 | } else if (last_block) { |
0dc3f44a | 1530 | QLIST_INSERT_AFTER_RCU(last_block, new_block, next); |
0d53d9fe | 1531 | } else { /* list is empty */ |
0dc3f44a | 1532 | QLIST_INSERT_HEAD_RCU(&ram_list.blocks, new_block, next); |
abb26d63 | 1533 | } |
0d6d3c87 | 1534 | ram_list.mru_block = NULL; |
94a6b54f | 1535 | |
0dc3f44a MD |
1536 | /* Write list before version */ |
1537 | smp_wmb(); | |
f798b07f | 1538 | ram_list.version++; |
b2a8658e | 1539 | qemu_mutex_unlock_ramlist(); |
f798b07f | 1540 | |
2152f5ca JQ |
1541 | new_ram_size = last_ram_offset() >> TARGET_PAGE_BITS; |
1542 | ||
1543 | if (new_ram_size > old_ram_size) { | |
1ab4c8ce | 1544 | int i; |
ae3a7047 MD |
1545 | |
1546 | /* ram_list.dirty_memory[] is protected by the iothread lock. */ | |
1ab4c8ce JQ |
1547 | for (i = 0; i < DIRTY_MEMORY_NUM; i++) { |
1548 | ram_list.dirty_memory[i] = | |
1549 | bitmap_zero_extend(ram_list.dirty_memory[i], | |
1550 | old_ram_size, new_ram_size); | |
1551 | } | |
2152f5ca | 1552 | } |
9b8424d5 | 1553 | cpu_physical_memory_set_dirty_range(new_block->offset, |
58d2707e PB |
1554 | new_block->used_length, |
1555 | DIRTY_CLIENTS_ALL); | |
94a6b54f | 1556 | |
a904c911 PB |
1557 | if (new_block->host) { |
1558 | qemu_ram_setup_dump(new_block->host, new_block->max_length); | |
1559 | qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_HUGEPAGE); | |
1560 | qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_DONTFORK); | |
1561 | if (kvm_enabled()) { | |
1562 | kvm_setup_guest_memory(new_block->host, new_block->max_length); | |
1563 | } | |
e1c57ab8 | 1564 | } |
6f0437e8 | 1565 | |
94a6b54f PB |
1566 | return new_block->offset; |
1567 | } | |
e9a1ab19 | 1568 | |
0b183fc8 | 1569 | #ifdef __linux__ |
e1c57ab8 | 1570 | ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, |
dbcb8981 | 1571 | bool share, const char *mem_path, |
7f56e740 | 1572 | Error **errp) |
e1c57ab8 PB |
1573 | { |
1574 | RAMBlock *new_block; | |
ef701d7b HT |
1575 | ram_addr_t addr; |
1576 | Error *local_err = NULL; | |
e1c57ab8 PB |
1577 | |
1578 | if (xen_enabled()) { | |
7f56e740 PB |
1579 | error_setg(errp, "-mem-path not supported with Xen"); |
1580 | return -1; | |
e1c57ab8 PB |
1581 | } |
1582 | ||
1583 | if (phys_mem_alloc != qemu_anon_ram_alloc) { | |
1584 | /* | |
1585 | * file_ram_alloc() needs to allocate just like | |
1586 | * phys_mem_alloc, but we haven't bothered to provide | |
1587 | * a hook there. | |
1588 | */ | |
7f56e740 PB |
1589 | error_setg(errp, |
1590 | "-mem-path not supported with this accelerator"); | |
1591 | return -1; | |
e1c57ab8 PB |
1592 | } |
1593 | ||
1594 | size = TARGET_PAGE_ALIGN(size); | |
1595 | new_block = g_malloc0(sizeof(*new_block)); | |
1596 | new_block->mr = mr; | |
9b8424d5 MT |
1597 | new_block->used_length = size; |
1598 | new_block->max_length = size; | |
dbcb8981 | 1599 | new_block->flags = share ? RAM_SHARED : 0; |
794e8f30 | 1600 | new_block->flags |= RAM_FILE; |
7f56e740 PB |
1601 | new_block->host = file_ram_alloc(new_block, size, |
1602 | mem_path, errp); | |
1603 | if (!new_block->host) { | |
1604 | g_free(new_block); | |
1605 | return -1; | |
1606 | } | |
1607 | ||
ef701d7b HT |
1608 | addr = ram_block_add(new_block, &local_err); |
1609 | if (local_err) { | |
1610 | g_free(new_block); | |
1611 | error_propagate(errp, local_err); | |
1612 | return -1; | |
1613 | } | |
1614 | return addr; | |
e1c57ab8 | 1615 | } |
0b183fc8 | 1616 | #endif |
e1c57ab8 | 1617 | |
62be4e3a MT |
1618 | static |
1619 | ram_addr_t qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size, | |
1620 | void (*resized)(const char*, | |
1621 | uint64_t length, | |
1622 | void *host), | |
1623 | void *host, bool resizeable, | |
ef701d7b | 1624 | MemoryRegion *mr, Error **errp) |
e1c57ab8 PB |
1625 | { |
1626 | RAMBlock *new_block; | |
ef701d7b HT |
1627 | ram_addr_t addr; |
1628 | Error *local_err = NULL; | |
e1c57ab8 PB |
1629 | |
1630 | size = TARGET_PAGE_ALIGN(size); | |
62be4e3a | 1631 | max_size = TARGET_PAGE_ALIGN(max_size); |
e1c57ab8 PB |
1632 | new_block = g_malloc0(sizeof(*new_block)); |
1633 | new_block->mr = mr; | |
62be4e3a | 1634 | new_block->resized = resized; |
9b8424d5 MT |
1635 | new_block->used_length = size; |
1636 | new_block->max_length = max_size; | |
62be4e3a | 1637 | assert(max_size >= size); |
e1c57ab8 PB |
1638 | new_block->fd = -1; |
1639 | new_block->host = host; | |
1640 | if (host) { | |
7bd4f430 | 1641 | new_block->flags |= RAM_PREALLOC; |
e1c57ab8 | 1642 | } |
62be4e3a MT |
1643 | if (resizeable) { |
1644 | new_block->flags |= RAM_RESIZEABLE; | |
1645 | } | |
ef701d7b HT |
1646 | addr = ram_block_add(new_block, &local_err); |
1647 | if (local_err) { | |
1648 | g_free(new_block); | |
1649 | error_propagate(errp, local_err); | |
1650 | return -1; | |
1651 | } | |
1652 | return addr; | |
e1c57ab8 PB |
1653 | } |
1654 | ||
62be4e3a MT |
1655 | ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, |
1656 | MemoryRegion *mr, Error **errp) | |
1657 | { | |
1658 | return qemu_ram_alloc_internal(size, size, NULL, host, false, mr, errp); | |
1659 | } | |
1660 | ||
ef701d7b | 1661 | ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error **errp) |
6977dfe6 | 1662 | { |
62be4e3a MT |
1663 | return qemu_ram_alloc_internal(size, size, NULL, NULL, false, mr, errp); |
1664 | } | |
1665 | ||
1666 | ram_addr_t qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t maxsz, | |
1667 | void (*resized)(const char*, | |
1668 | uint64_t length, | |
1669 | void *host), | |
1670 | MemoryRegion *mr, Error **errp) | |
1671 | { | |
1672 | return qemu_ram_alloc_internal(size, maxsz, resized, NULL, true, mr, errp); | |
6977dfe6 YT |
1673 | } |
1674 | ||
1f2e98b6 AW |
1675 | void qemu_ram_free_from_ptr(ram_addr_t addr) |
1676 | { | |
1677 | RAMBlock *block; | |
1678 | ||
b2a8658e | 1679 | qemu_mutex_lock_ramlist(); |
0dc3f44a | 1680 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
1f2e98b6 | 1681 | if (addr == block->offset) { |
0dc3f44a | 1682 | QLIST_REMOVE_RCU(block, next); |
0d6d3c87 | 1683 | ram_list.mru_block = NULL; |
0dc3f44a MD |
1684 | /* Write list before version */ |
1685 | smp_wmb(); | |
f798b07f | 1686 | ram_list.version++; |
43771539 | 1687 | g_free_rcu(block, rcu); |
b2a8658e | 1688 | break; |
1f2e98b6 AW |
1689 | } |
1690 | } | |
b2a8658e | 1691 | qemu_mutex_unlock_ramlist(); |
1f2e98b6 AW |
1692 | } |
1693 | ||
43771539 PB |
1694 | static void reclaim_ramblock(RAMBlock *block) |
1695 | { | |
1696 | if (block->flags & RAM_PREALLOC) { | |
1697 | ; | |
1698 | } else if (xen_enabled()) { | |
1699 | xen_invalidate_map_cache_entry(block->host); | |
1700 | #ifndef _WIN32 | |
1701 | } else if (block->fd >= 0) { | |
794e8f30 MT |
1702 | if (block->flags & RAM_FILE) { |
1703 | qemu_ram_munmap(block->host, block->max_length); | |
8561c924 MT |
1704 | } else { |
1705 | munmap(block->host, block->max_length); | |
1706 | } | |
43771539 PB |
1707 | close(block->fd); |
1708 | #endif | |
1709 | } else { | |
1710 | qemu_anon_ram_free(block->host, block->max_length); | |
1711 | } | |
1712 | g_free(block); | |
1713 | } | |
1714 | ||
c227f099 | 1715 | void qemu_ram_free(ram_addr_t addr) |
e9a1ab19 | 1716 | { |
04b16653 AW |
1717 | RAMBlock *block; |
1718 | ||
b2a8658e | 1719 | qemu_mutex_lock_ramlist(); |
0dc3f44a | 1720 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
04b16653 | 1721 | if (addr == block->offset) { |
0dc3f44a | 1722 | QLIST_REMOVE_RCU(block, next); |
0d6d3c87 | 1723 | ram_list.mru_block = NULL; |
0dc3f44a MD |
1724 | /* Write list before version */ |
1725 | smp_wmb(); | |
f798b07f | 1726 | ram_list.version++; |
43771539 | 1727 | call_rcu(block, reclaim_ramblock, rcu); |
b2a8658e | 1728 | break; |
04b16653 AW |
1729 | } |
1730 | } | |
b2a8658e | 1731 | qemu_mutex_unlock_ramlist(); |
e9a1ab19 FB |
1732 | } |
1733 | ||
cd19cfa2 HY |
1734 | #ifndef _WIN32 |
1735 | void qemu_ram_remap(ram_addr_t addr, ram_addr_t length) | |
1736 | { | |
1737 | RAMBlock *block; | |
1738 | ram_addr_t offset; | |
1739 | int flags; | |
1740 | void *area, *vaddr; | |
1741 | ||
0dc3f44a | 1742 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
cd19cfa2 | 1743 | offset = addr - block->offset; |
9b8424d5 | 1744 | if (offset < block->max_length) { |
1240be24 | 1745 | vaddr = ramblock_ptr(block, offset); |
7bd4f430 | 1746 | if (block->flags & RAM_PREALLOC) { |
cd19cfa2 | 1747 | ; |
dfeaf2ab MA |
1748 | } else if (xen_enabled()) { |
1749 | abort(); | |
cd19cfa2 HY |
1750 | } else { |
1751 | flags = MAP_FIXED; | |
3435f395 | 1752 | if (block->fd >= 0) { |
dbcb8981 PB |
1753 | flags |= (block->flags & RAM_SHARED ? |
1754 | MAP_SHARED : MAP_PRIVATE); | |
3435f395 MA |
1755 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, |
1756 | flags, block->fd, offset); | |
cd19cfa2 | 1757 | } else { |
2eb9fbaa MA |
1758 | /* |
1759 | * Remap needs to match alloc. Accelerators that | |
1760 | * set phys_mem_alloc never remap. If they did, | |
1761 | * we'd need a remap hook here. | |
1762 | */ | |
1763 | assert(phys_mem_alloc == qemu_anon_ram_alloc); | |
1764 | ||
cd19cfa2 HY |
1765 | flags |= MAP_PRIVATE | MAP_ANONYMOUS; |
1766 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, | |
1767 | flags, -1, 0); | |
cd19cfa2 HY |
1768 | } |
1769 | if (area != vaddr) { | |
f15fbc4b AP |
1770 | fprintf(stderr, "Could not remap addr: " |
1771 | RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n", | |
cd19cfa2 HY |
1772 | length, addr); |
1773 | exit(1); | |
1774 | } | |
8490fc78 | 1775 | memory_try_enable_merging(vaddr, length); |
ddb97f1d | 1776 | qemu_ram_setup_dump(vaddr, length); |
cd19cfa2 | 1777 | } |
cd19cfa2 HY |
1778 | } |
1779 | } | |
1780 | } | |
1781 | #endif /* !_WIN32 */ | |
1782 | ||
a35ba7be PB |
1783 | int qemu_get_ram_fd(ram_addr_t addr) |
1784 | { | |
ae3a7047 MD |
1785 | RAMBlock *block; |
1786 | int fd; | |
a35ba7be | 1787 | |
0dc3f44a | 1788 | rcu_read_lock(); |
ae3a7047 MD |
1789 | block = qemu_get_ram_block(addr); |
1790 | fd = block->fd; | |
0dc3f44a | 1791 | rcu_read_unlock(); |
ae3a7047 | 1792 | return fd; |
a35ba7be PB |
1793 | } |
1794 | ||
3fd74b84 DM |
1795 | void *qemu_get_ram_block_host_ptr(ram_addr_t addr) |
1796 | { | |
ae3a7047 MD |
1797 | RAMBlock *block; |
1798 | void *ptr; | |
3fd74b84 | 1799 | |
0dc3f44a | 1800 | rcu_read_lock(); |
ae3a7047 MD |
1801 | block = qemu_get_ram_block(addr); |
1802 | ptr = ramblock_ptr(block, 0); | |
0dc3f44a | 1803 | rcu_read_unlock(); |
ae3a7047 | 1804 | return ptr; |
3fd74b84 DM |
1805 | } |
1806 | ||
1b5ec234 | 1807 | /* Return a host pointer to ram allocated with qemu_ram_alloc. |
ae3a7047 MD |
1808 | * This should not be used for general purpose DMA. Use address_space_map |
1809 | * or address_space_rw instead. For local memory (e.g. video ram) that the | |
1810 | * device owns, use memory_region_get_ram_ptr. | |
0dc3f44a MD |
1811 | * |
1812 | * By the time this function returns, the returned pointer is not protected | |
1813 | * by RCU anymore. If the caller is not within an RCU critical section and | |
1814 | * does not hold the iothread lock, it must have other means of protecting the | |
1815 | * pointer, such as a reference to the region that includes the incoming | |
1816 | * ram_addr_t. | |
1b5ec234 PB |
1817 | */ |
1818 | void *qemu_get_ram_ptr(ram_addr_t addr) | |
1819 | { | |
ae3a7047 MD |
1820 | RAMBlock *block; |
1821 | void *ptr; | |
1b5ec234 | 1822 | |
0dc3f44a | 1823 | rcu_read_lock(); |
ae3a7047 MD |
1824 | block = qemu_get_ram_block(addr); |
1825 | ||
1826 | if (xen_enabled() && block->host == NULL) { | |
0d6d3c87 PB |
1827 | /* We need to check if the requested address is in the RAM |
1828 | * because we don't want to map the entire memory in QEMU. | |
1829 | * In that case just map until the end of the page. | |
1830 | */ | |
1831 | if (block->offset == 0) { | |
ae3a7047 | 1832 | ptr = xen_map_cache(addr, 0, 0); |
0dc3f44a | 1833 | goto unlock; |
0d6d3c87 | 1834 | } |
ae3a7047 MD |
1835 | |
1836 | block->host = xen_map_cache(block->offset, block->max_length, 1); | |
0d6d3c87 | 1837 | } |
ae3a7047 MD |
1838 | ptr = ramblock_ptr(block, addr - block->offset); |
1839 | ||
0dc3f44a MD |
1840 | unlock: |
1841 | rcu_read_unlock(); | |
ae3a7047 | 1842 | return ptr; |
dc828ca1 PB |
1843 | } |
1844 | ||
38bee5dc | 1845 | /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr |
ae3a7047 | 1846 | * but takes a size argument. |
0dc3f44a MD |
1847 | * |
1848 | * By the time this function returns, the returned pointer is not protected | |
1849 | * by RCU anymore. If the caller is not within an RCU critical section and | |
1850 | * does not hold the iothread lock, it must have other means of protecting the | |
1851 | * pointer, such as a reference to the region that includes the incoming | |
1852 | * ram_addr_t. | |
ae3a7047 | 1853 | */ |
cb85f7ab | 1854 | static void *qemu_ram_ptr_length(ram_addr_t addr, hwaddr *size) |
38bee5dc | 1855 | { |
ae3a7047 | 1856 | void *ptr; |
8ab934f9 SS |
1857 | if (*size == 0) { |
1858 | return NULL; | |
1859 | } | |
868bb33f | 1860 | if (xen_enabled()) { |
e41d7c69 | 1861 | return xen_map_cache(addr, *size, 1); |
868bb33f | 1862 | } else { |
38bee5dc | 1863 | RAMBlock *block; |
0dc3f44a MD |
1864 | rcu_read_lock(); |
1865 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { | |
9b8424d5 MT |
1866 | if (addr - block->offset < block->max_length) { |
1867 | if (addr - block->offset + *size > block->max_length) | |
1868 | *size = block->max_length - addr + block->offset; | |
ae3a7047 | 1869 | ptr = ramblock_ptr(block, addr - block->offset); |
0dc3f44a | 1870 | rcu_read_unlock(); |
ae3a7047 | 1871 | return ptr; |
38bee5dc SS |
1872 | } |
1873 | } | |
1874 | ||
1875 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); | |
1876 | abort(); | |
38bee5dc SS |
1877 | } |
1878 | } | |
1879 | ||
7443b437 | 1880 | /* Some of the softmmu routines need to translate from a host pointer |
ae3a7047 MD |
1881 | * (typically a TLB entry) back to a ram offset. |
1882 | * | |
1883 | * By the time this function returns, the returned pointer is not protected | |
1884 | * by RCU anymore. If the caller is not within an RCU critical section and | |
1885 | * does not hold the iothread lock, it must have other means of protecting the | |
1886 | * pointer, such as a reference to the region that includes the incoming | |
1887 | * ram_addr_t. | |
1888 | */ | |
1b5ec234 | 1889 | MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr) |
5579c7f3 | 1890 | { |
94a6b54f PB |
1891 | RAMBlock *block; |
1892 | uint8_t *host = ptr; | |
ae3a7047 | 1893 | MemoryRegion *mr; |
94a6b54f | 1894 | |
868bb33f | 1895 | if (xen_enabled()) { |
0dc3f44a | 1896 | rcu_read_lock(); |
e41d7c69 | 1897 | *ram_addr = xen_ram_addr_from_mapcache(ptr); |
ae3a7047 | 1898 | mr = qemu_get_ram_block(*ram_addr)->mr; |
0dc3f44a | 1899 | rcu_read_unlock(); |
ae3a7047 | 1900 | return mr; |
712c2b41 SS |
1901 | } |
1902 | ||
0dc3f44a MD |
1903 | rcu_read_lock(); |
1904 | block = atomic_rcu_read(&ram_list.mru_block); | |
9b8424d5 | 1905 | if (block && block->host && host - block->host < block->max_length) { |
23887b79 PB |
1906 | goto found; |
1907 | } | |
1908 | ||
0dc3f44a | 1909 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
432d268c JN |
1910 | /* This case append when the block is not mapped. */ |
1911 | if (block->host == NULL) { | |
1912 | continue; | |
1913 | } | |
9b8424d5 | 1914 | if (host - block->host < block->max_length) { |
23887b79 | 1915 | goto found; |
f471a17e | 1916 | } |
94a6b54f | 1917 | } |
432d268c | 1918 | |
0dc3f44a | 1919 | rcu_read_unlock(); |
1b5ec234 | 1920 | return NULL; |
23887b79 PB |
1921 | |
1922 | found: | |
1923 | *ram_addr = block->offset + (host - block->host); | |
ae3a7047 | 1924 | mr = block->mr; |
0dc3f44a | 1925 | rcu_read_unlock(); |
ae3a7047 | 1926 | return mr; |
e890261f | 1927 | } |
f471a17e | 1928 | |
a8170e5e | 1929 | static void notdirty_mem_write(void *opaque, hwaddr ram_addr, |
0e0df1e2 | 1930 | uint64_t val, unsigned size) |
9fa3e853 | 1931 | { |
52159192 | 1932 | if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) { |
0e0df1e2 | 1933 | tb_invalidate_phys_page_fast(ram_addr, size); |
3a7d929e | 1934 | } |
0e0df1e2 AK |
1935 | switch (size) { |
1936 | case 1: | |
1937 | stb_p(qemu_get_ram_ptr(ram_addr), val); | |
1938 | break; | |
1939 | case 2: | |
1940 | stw_p(qemu_get_ram_ptr(ram_addr), val); | |
1941 | break; | |
1942 | case 4: | |
1943 | stl_p(qemu_get_ram_ptr(ram_addr), val); | |
1944 | break; | |
1945 | default: | |
1946 | abort(); | |
3a7d929e | 1947 | } |
58d2707e PB |
1948 | /* Set both VGA and migration bits for simplicity and to remove |
1949 | * the notdirty callback faster. | |
1950 | */ | |
1951 | cpu_physical_memory_set_dirty_range(ram_addr, size, | |
1952 | DIRTY_CLIENTS_NOCODE); | |
f23db169 FB |
1953 | /* we remove the notdirty callback only if the code has been |
1954 | flushed */ | |
a2cd8c85 | 1955 | if (!cpu_physical_memory_is_clean(ram_addr)) { |
bcae01e4 | 1956 | tlb_set_dirty(current_cpu, current_cpu->mem_io_vaddr); |
4917cf44 | 1957 | } |
9fa3e853 FB |
1958 | } |
1959 | ||
b018ddf6 PB |
1960 | static bool notdirty_mem_accepts(void *opaque, hwaddr addr, |
1961 | unsigned size, bool is_write) | |
1962 | { | |
1963 | return is_write; | |
1964 | } | |
1965 | ||
0e0df1e2 | 1966 | static const MemoryRegionOps notdirty_mem_ops = { |
0e0df1e2 | 1967 | .write = notdirty_mem_write, |
b018ddf6 | 1968 | .valid.accepts = notdirty_mem_accepts, |
0e0df1e2 | 1969 | .endianness = DEVICE_NATIVE_ENDIAN, |
1ccde1cb FB |
1970 | }; |
1971 | ||
0f459d16 | 1972 | /* Generate a debug exception if a watchpoint has been hit. */ |
66b9b43c | 1973 | static void check_watchpoint(int offset, int len, MemTxAttrs attrs, int flags) |
0f459d16 | 1974 | { |
93afeade AF |
1975 | CPUState *cpu = current_cpu; |
1976 | CPUArchState *env = cpu->env_ptr; | |
06d55cc1 | 1977 | target_ulong pc, cs_base; |
0f459d16 | 1978 | target_ulong vaddr; |
a1d1bb31 | 1979 | CPUWatchpoint *wp; |
06d55cc1 | 1980 | int cpu_flags; |
0f459d16 | 1981 | |
ff4700b0 | 1982 | if (cpu->watchpoint_hit) { |
06d55cc1 AL |
1983 | /* We re-entered the check after replacing the TB. Now raise |
1984 | * the debug interrupt so that is will trigger after the | |
1985 | * current instruction. */ | |
93afeade | 1986 | cpu_interrupt(cpu, CPU_INTERRUPT_DEBUG); |
06d55cc1 AL |
1987 | return; |
1988 | } | |
93afeade | 1989 | vaddr = (cpu->mem_io_vaddr & TARGET_PAGE_MASK) + offset; |
ff4700b0 | 1990 | QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) { |
05068c0d PM |
1991 | if (cpu_watchpoint_address_matches(wp, vaddr, len) |
1992 | && (wp->flags & flags)) { | |
08225676 PM |
1993 | if (flags == BP_MEM_READ) { |
1994 | wp->flags |= BP_WATCHPOINT_HIT_READ; | |
1995 | } else { | |
1996 | wp->flags |= BP_WATCHPOINT_HIT_WRITE; | |
1997 | } | |
1998 | wp->hitaddr = vaddr; | |
66b9b43c | 1999 | wp->hitattrs = attrs; |
ff4700b0 AF |
2000 | if (!cpu->watchpoint_hit) { |
2001 | cpu->watchpoint_hit = wp; | |
239c51a5 | 2002 | tb_check_watchpoint(cpu); |
6e140f28 | 2003 | if (wp->flags & BP_STOP_BEFORE_ACCESS) { |
27103424 | 2004 | cpu->exception_index = EXCP_DEBUG; |
5638d180 | 2005 | cpu_loop_exit(cpu); |
6e140f28 AL |
2006 | } else { |
2007 | cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags); | |
648f034c | 2008 | tb_gen_code(cpu, pc, cs_base, cpu_flags, 1); |
0ea8cb88 | 2009 | cpu_resume_from_signal(cpu, NULL); |
6e140f28 | 2010 | } |
06d55cc1 | 2011 | } |
6e140f28 AL |
2012 | } else { |
2013 | wp->flags &= ~BP_WATCHPOINT_HIT; | |
0f459d16 PB |
2014 | } |
2015 | } | |
2016 | } | |
2017 | ||
6658ffb8 PB |
2018 | /* Watchpoint access routines. Watchpoints are inserted using TLB tricks, |
2019 | so these check for a hit then pass through to the normal out-of-line | |
2020 | phys routines. */ | |
66b9b43c PM |
2021 | static MemTxResult watch_mem_read(void *opaque, hwaddr addr, uint64_t *pdata, |
2022 | unsigned size, MemTxAttrs attrs) | |
6658ffb8 | 2023 | { |
66b9b43c PM |
2024 | MemTxResult res; |
2025 | uint64_t data; | |
2026 | ||
2027 | check_watchpoint(addr & ~TARGET_PAGE_MASK, size, attrs, BP_MEM_READ); | |
1ec9b909 | 2028 | switch (size) { |
66b9b43c PM |
2029 | case 1: |
2030 | data = address_space_ldub(&address_space_memory, addr, attrs, &res); | |
2031 | break; | |
2032 | case 2: | |
2033 | data = address_space_lduw(&address_space_memory, addr, attrs, &res); | |
2034 | break; | |
2035 | case 4: | |
2036 | data = address_space_ldl(&address_space_memory, addr, attrs, &res); | |
2037 | break; | |
1ec9b909 AK |
2038 | default: abort(); |
2039 | } | |
66b9b43c PM |
2040 | *pdata = data; |
2041 | return res; | |
6658ffb8 PB |
2042 | } |
2043 | ||
66b9b43c PM |
2044 | static MemTxResult watch_mem_write(void *opaque, hwaddr addr, |
2045 | uint64_t val, unsigned size, | |
2046 | MemTxAttrs attrs) | |
6658ffb8 | 2047 | { |
66b9b43c PM |
2048 | MemTxResult res; |
2049 | ||
2050 | check_watchpoint(addr & ~TARGET_PAGE_MASK, size, attrs, BP_MEM_WRITE); | |
1ec9b909 | 2051 | switch (size) { |
67364150 | 2052 | case 1: |
66b9b43c | 2053 | address_space_stb(&address_space_memory, addr, val, attrs, &res); |
67364150 MF |
2054 | break; |
2055 | case 2: | |
66b9b43c | 2056 | address_space_stw(&address_space_memory, addr, val, attrs, &res); |
67364150 MF |
2057 | break; |
2058 | case 4: | |
66b9b43c | 2059 | address_space_stl(&address_space_memory, addr, val, attrs, &res); |
67364150 | 2060 | break; |
1ec9b909 AK |
2061 | default: abort(); |
2062 | } | |
66b9b43c | 2063 | return res; |
6658ffb8 PB |
2064 | } |
2065 | ||
1ec9b909 | 2066 | static const MemoryRegionOps watch_mem_ops = { |
66b9b43c PM |
2067 | .read_with_attrs = watch_mem_read, |
2068 | .write_with_attrs = watch_mem_write, | |
1ec9b909 | 2069 | .endianness = DEVICE_NATIVE_ENDIAN, |
6658ffb8 | 2070 | }; |
6658ffb8 | 2071 | |
f25a49e0 PM |
2072 | static MemTxResult subpage_read(void *opaque, hwaddr addr, uint64_t *data, |
2073 | unsigned len, MemTxAttrs attrs) | |
db7b5426 | 2074 | { |
acc9d80b | 2075 | subpage_t *subpage = opaque; |
ff6cff75 | 2076 | uint8_t buf[8]; |
5c9eb028 | 2077 | MemTxResult res; |
791af8c8 | 2078 | |
db7b5426 | 2079 | #if defined(DEBUG_SUBPAGE) |
016e9d62 | 2080 | printf("%s: subpage %p len %u addr " TARGET_FMT_plx "\n", __func__, |
acc9d80b | 2081 | subpage, len, addr); |
db7b5426 | 2082 | #endif |
5c9eb028 PM |
2083 | res = address_space_read(subpage->as, addr + subpage->base, |
2084 | attrs, buf, len); | |
2085 | if (res) { | |
2086 | return res; | |
f25a49e0 | 2087 | } |
acc9d80b JK |
2088 | switch (len) { |
2089 | case 1: | |
f25a49e0 PM |
2090 | *data = ldub_p(buf); |
2091 | return MEMTX_OK; | |
acc9d80b | 2092 | case 2: |
f25a49e0 PM |
2093 | *data = lduw_p(buf); |
2094 | return MEMTX_OK; | |
acc9d80b | 2095 | case 4: |
f25a49e0 PM |
2096 | *data = ldl_p(buf); |
2097 | return MEMTX_OK; | |
ff6cff75 | 2098 | case 8: |
f25a49e0 PM |
2099 | *data = ldq_p(buf); |
2100 | return MEMTX_OK; | |
acc9d80b JK |
2101 | default: |
2102 | abort(); | |
2103 | } | |
db7b5426 BS |
2104 | } |
2105 | ||
f25a49e0 PM |
2106 | static MemTxResult subpage_write(void *opaque, hwaddr addr, |
2107 | uint64_t value, unsigned len, MemTxAttrs attrs) | |
db7b5426 | 2108 | { |
acc9d80b | 2109 | subpage_t *subpage = opaque; |
ff6cff75 | 2110 | uint8_t buf[8]; |
acc9d80b | 2111 | |
db7b5426 | 2112 | #if defined(DEBUG_SUBPAGE) |
016e9d62 | 2113 | printf("%s: subpage %p len %u addr " TARGET_FMT_plx |
acc9d80b JK |
2114 | " value %"PRIx64"\n", |
2115 | __func__, subpage, len, addr, value); | |
db7b5426 | 2116 | #endif |
acc9d80b JK |
2117 | switch (len) { |
2118 | case 1: | |
2119 | stb_p(buf, value); | |
2120 | break; | |
2121 | case 2: | |
2122 | stw_p(buf, value); | |
2123 | break; | |
2124 | case 4: | |
2125 | stl_p(buf, value); | |
2126 | break; | |
ff6cff75 PB |
2127 | case 8: |
2128 | stq_p(buf, value); | |
2129 | break; | |
acc9d80b JK |
2130 | default: |
2131 | abort(); | |
2132 | } | |
5c9eb028 PM |
2133 | return address_space_write(subpage->as, addr + subpage->base, |
2134 | attrs, buf, len); | |
db7b5426 BS |
2135 | } |
2136 | ||
c353e4cc | 2137 | static bool subpage_accepts(void *opaque, hwaddr addr, |
016e9d62 | 2138 | unsigned len, bool is_write) |
c353e4cc | 2139 | { |
acc9d80b | 2140 | subpage_t *subpage = opaque; |
c353e4cc | 2141 | #if defined(DEBUG_SUBPAGE) |
016e9d62 | 2142 | printf("%s: subpage %p %c len %u addr " TARGET_FMT_plx "\n", |
acc9d80b | 2143 | __func__, subpage, is_write ? 'w' : 'r', len, addr); |
c353e4cc PB |
2144 | #endif |
2145 | ||
acc9d80b | 2146 | return address_space_access_valid(subpage->as, addr + subpage->base, |
016e9d62 | 2147 | len, is_write); |
c353e4cc PB |
2148 | } |
2149 | ||
70c68e44 | 2150 | static const MemoryRegionOps subpage_ops = { |
f25a49e0 PM |
2151 | .read_with_attrs = subpage_read, |
2152 | .write_with_attrs = subpage_write, | |
ff6cff75 PB |
2153 | .impl.min_access_size = 1, |
2154 | .impl.max_access_size = 8, | |
2155 | .valid.min_access_size = 1, | |
2156 | .valid.max_access_size = 8, | |
c353e4cc | 2157 | .valid.accepts = subpage_accepts, |
70c68e44 | 2158 | .endianness = DEVICE_NATIVE_ENDIAN, |
db7b5426 BS |
2159 | }; |
2160 | ||
c227f099 | 2161 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
5312bd8b | 2162 | uint16_t section) |
db7b5426 BS |
2163 | { |
2164 | int idx, eidx; | |
2165 | ||
2166 | if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE) | |
2167 | return -1; | |
2168 | idx = SUBPAGE_IDX(start); | |
2169 | eidx = SUBPAGE_IDX(end); | |
2170 | #if defined(DEBUG_SUBPAGE) | |
016e9d62 AK |
2171 | printf("%s: %p start %08x end %08x idx %08x eidx %08x section %d\n", |
2172 | __func__, mmio, start, end, idx, eidx, section); | |
db7b5426 | 2173 | #endif |
db7b5426 | 2174 | for (; idx <= eidx; idx++) { |
5312bd8b | 2175 | mmio->sub_section[idx] = section; |
db7b5426 BS |
2176 | } |
2177 | ||
2178 | return 0; | |
2179 | } | |
2180 | ||
acc9d80b | 2181 | static subpage_t *subpage_init(AddressSpace *as, hwaddr base) |
db7b5426 | 2182 | { |
c227f099 | 2183 | subpage_t *mmio; |
db7b5426 | 2184 | |
7267c094 | 2185 | mmio = g_malloc0(sizeof(subpage_t)); |
1eec614b | 2186 | |
acc9d80b | 2187 | mmio->as = as; |
1eec614b | 2188 | mmio->base = base; |
2c9b15ca | 2189 | memory_region_init_io(&mmio->iomem, NULL, &subpage_ops, mmio, |
b4fefef9 | 2190 | NULL, TARGET_PAGE_SIZE); |
b3b00c78 | 2191 | mmio->iomem.subpage = true; |
db7b5426 | 2192 | #if defined(DEBUG_SUBPAGE) |
016e9d62 AK |
2193 | printf("%s: %p base " TARGET_FMT_plx " len %08x\n", __func__, |
2194 | mmio, base, TARGET_PAGE_SIZE); | |
db7b5426 | 2195 | #endif |
b41aac4f | 2196 | subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, PHYS_SECTION_UNASSIGNED); |
db7b5426 BS |
2197 | |
2198 | return mmio; | |
2199 | } | |
2200 | ||
a656e22f PC |
2201 | static uint16_t dummy_section(PhysPageMap *map, AddressSpace *as, |
2202 | MemoryRegion *mr) | |
5312bd8b | 2203 | { |
a656e22f | 2204 | assert(as); |
5312bd8b | 2205 | MemoryRegionSection section = { |
a656e22f | 2206 | .address_space = as, |
5312bd8b AK |
2207 | .mr = mr, |
2208 | .offset_within_address_space = 0, | |
2209 | .offset_within_region = 0, | |
052e87b0 | 2210 | .size = int128_2_64(), |
5312bd8b AK |
2211 | }; |
2212 | ||
53cb28cb | 2213 | return phys_section_add(map, §ion); |
5312bd8b AK |
2214 | } |
2215 | ||
9d82b5a7 | 2216 | MemoryRegion *iotlb_to_region(CPUState *cpu, hwaddr index) |
aa102231 | 2217 | { |
32857f4d PM |
2218 | CPUAddressSpace *cpuas = &cpu->cpu_ases[0]; |
2219 | AddressSpaceDispatch *d = atomic_rcu_read(&cpuas->memory_dispatch); | |
79e2b9ae | 2220 | MemoryRegionSection *sections = d->map.sections; |
9d82b5a7 PB |
2221 | |
2222 | return sections[index & ~TARGET_PAGE_MASK].mr; | |
aa102231 AK |
2223 | } |
2224 | ||
e9179ce1 AK |
2225 | static void io_mem_init(void) |
2226 | { | |
1f6245e5 | 2227 | memory_region_init_io(&io_mem_rom, NULL, &unassigned_mem_ops, NULL, NULL, UINT64_MAX); |
2c9b15ca | 2228 | memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL, |
1f6245e5 | 2229 | NULL, UINT64_MAX); |
2c9b15ca | 2230 | memory_region_init_io(&io_mem_notdirty, NULL, ¬dirty_mem_ops, NULL, |
1f6245e5 | 2231 | NULL, UINT64_MAX); |
2c9b15ca | 2232 | memory_region_init_io(&io_mem_watch, NULL, &watch_mem_ops, NULL, |
1f6245e5 | 2233 | NULL, UINT64_MAX); |
e9179ce1 AK |
2234 | } |
2235 | ||
ac1970fb | 2236 | static void mem_begin(MemoryListener *listener) |
00752703 PB |
2237 | { |
2238 | AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener); | |
53cb28cb MA |
2239 | AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1); |
2240 | uint16_t n; | |
2241 | ||
a656e22f | 2242 | n = dummy_section(&d->map, as, &io_mem_unassigned); |
53cb28cb | 2243 | assert(n == PHYS_SECTION_UNASSIGNED); |
a656e22f | 2244 | n = dummy_section(&d->map, as, &io_mem_notdirty); |
53cb28cb | 2245 | assert(n == PHYS_SECTION_NOTDIRTY); |
a656e22f | 2246 | n = dummy_section(&d->map, as, &io_mem_rom); |
53cb28cb | 2247 | assert(n == PHYS_SECTION_ROM); |
a656e22f | 2248 | n = dummy_section(&d->map, as, &io_mem_watch); |
53cb28cb | 2249 | assert(n == PHYS_SECTION_WATCH); |
00752703 | 2250 | |
9736e55b | 2251 | d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 }; |
00752703 PB |
2252 | d->as = as; |
2253 | as->next_dispatch = d; | |
2254 | } | |
2255 | ||
79e2b9ae PB |
2256 | static void address_space_dispatch_free(AddressSpaceDispatch *d) |
2257 | { | |
2258 | phys_sections_free(&d->map); | |
2259 | g_free(d); | |
2260 | } | |
2261 | ||
00752703 | 2262 | static void mem_commit(MemoryListener *listener) |
ac1970fb | 2263 | { |
89ae337a | 2264 | AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener); |
0475d94f PB |
2265 | AddressSpaceDispatch *cur = as->dispatch; |
2266 | AddressSpaceDispatch *next = as->next_dispatch; | |
2267 | ||
53cb28cb | 2268 | phys_page_compact_all(next, next->map.nodes_nb); |
b35ba30f | 2269 | |
79e2b9ae | 2270 | atomic_rcu_set(&as->dispatch, next); |
53cb28cb | 2271 | if (cur) { |
79e2b9ae | 2272 | call_rcu(cur, address_space_dispatch_free, rcu); |
53cb28cb | 2273 | } |
9affd6fc PB |
2274 | } |
2275 | ||
1d71148e | 2276 | static void tcg_commit(MemoryListener *listener) |
50c1e149 | 2277 | { |
32857f4d PM |
2278 | CPUAddressSpace *cpuas; |
2279 | AddressSpaceDispatch *d; | |
117712c3 AK |
2280 | |
2281 | /* since each CPU stores ram addresses in its TLB cache, we must | |
2282 | reset the modified entries */ | |
32857f4d PM |
2283 | cpuas = container_of(listener, CPUAddressSpace, tcg_as_listener); |
2284 | cpu_reloading_memory_map(); | |
2285 | /* The CPU and TLB are protected by the iothread lock. | |
2286 | * We reload the dispatch pointer now because cpu_reloading_memory_map() | |
2287 | * may have split the RCU critical section. | |
2288 | */ | |
2289 | d = atomic_rcu_read(&cpuas->as->dispatch); | |
2290 | cpuas->memory_dispatch = d; | |
2291 | tlb_flush(cpuas->cpu, 1); | |
50c1e149 AK |
2292 | } |
2293 | ||
ac1970fb AK |
2294 | void address_space_init_dispatch(AddressSpace *as) |
2295 | { | |
00752703 | 2296 | as->dispatch = NULL; |
89ae337a | 2297 | as->dispatch_listener = (MemoryListener) { |
ac1970fb | 2298 | .begin = mem_begin, |
00752703 | 2299 | .commit = mem_commit, |
ac1970fb AK |
2300 | .region_add = mem_add, |
2301 | .region_nop = mem_add, | |
2302 | .priority = 0, | |
2303 | }; | |
89ae337a | 2304 | memory_listener_register(&as->dispatch_listener, as); |
ac1970fb AK |
2305 | } |
2306 | ||
6e48e8f9 PB |
2307 | void address_space_unregister(AddressSpace *as) |
2308 | { | |
2309 | memory_listener_unregister(&as->dispatch_listener); | |
2310 | } | |
2311 | ||
83f3c251 AK |
2312 | void address_space_destroy_dispatch(AddressSpace *as) |
2313 | { | |
2314 | AddressSpaceDispatch *d = as->dispatch; | |
2315 | ||
79e2b9ae PB |
2316 | atomic_rcu_set(&as->dispatch, NULL); |
2317 | if (d) { | |
2318 | call_rcu(d, address_space_dispatch_free, rcu); | |
2319 | } | |
83f3c251 AK |
2320 | } |
2321 | ||
62152b8a AK |
2322 | static void memory_map_init(void) |
2323 | { | |
7267c094 | 2324 | system_memory = g_malloc(sizeof(*system_memory)); |
03f49957 | 2325 | |
57271d63 | 2326 | memory_region_init(system_memory, NULL, "system", UINT64_MAX); |
7dca8043 | 2327 | address_space_init(&address_space_memory, system_memory, "memory"); |
309cb471 | 2328 | |
7267c094 | 2329 | system_io = g_malloc(sizeof(*system_io)); |
3bb28b72 JK |
2330 | memory_region_init_io(system_io, NULL, &unassigned_io_ops, NULL, "io", |
2331 | 65536); | |
7dca8043 | 2332 | address_space_init(&address_space_io, system_io, "I/O"); |
62152b8a AK |
2333 | } |
2334 | ||
2335 | MemoryRegion *get_system_memory(void) | |
2336 | { | |
2337 | return system_memory; | |
2338 | } | |
2339 | ||
309cb471 AK |
2340 | MemoryRegion *get_system_io(void) |
2341 | { | |
2342 | return system_io; | |
2343 | } | |
2344 | ||
e2eef170 PB |
2345 | #endif /* !defined(CONFIG_USER_ONLY) */ |
2346 | ||
13eb76e0 FB |
2347 | /* physical memory access (slow version, mainly for debug) */ |
2348 | #if defined(CONFIG_USER_ONLY) | |
f17ec444 | 2349 | int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr, |
a68fe89c | 2350 | uint8_t *buf, int len, int is_write) |
13eb76e0 FB |
2351 | { |
2352 | int l, flags; | |
2353 | target_ulong page; | |
53a5960a | 2354 | void * p; |
13eb76e0 FB |
2355 | |
2356 | while (len > 0) { | |
2357 | page = addr & TARGET_PAGE_MASK; | |
2358 | l = (page + TARGET_PAGE_SIZE) - addr; | |
2359 | if (l > len) | |
2360 | l = len; | |
2361 | flags = page_get_flags(page); | |
2362 | if (!(flags & PAGE_VALID)) | |
a68fe89c | 2363 | return -1; |
13eb76e0 FB |
2364 | if (is_write) { |
2365 | if (!(flags & PAGE_WRITE)) | |
a68fe89c | 2366 | return -1; |
579a97f7 | 2367 | /* XXX: this code should not depend on lock_user */ |
72fb7daa | 2368 | if (!(p = lock_user(VERIFY_WRITE, addr, l, 0))) |
a68fe89c | 2369 | return -1; |
72fb7daa AJ |
2370 | memcpy(p, buf, l); |
2371 | unlock_user(p, addr, l); | |
13eb76e0 FB |
2372 | } else { |
2373 | if (!(flags & PAGE_READ)) | |
a68fe89c | 2374 | return -1; |
579a97f7 | 2375 | /* XXX: this code should not depend on lock_user */ |
72fb7daa | 2376 | if (!(p = lock_user(VERIFY_READ, addr, l, 1))) |
a68fe89c | 2377 | return -1; |
72fb7daa | 2378 | memcpy(buf, p, l); |
5b257578 | 2379 | unlock_user(p, addr, 0); |
13eb76e0 FB |
2380 | } |
2381 | len -= l; | |
2382 | buf += l; | |
2383 | addr += l; | |
2384 | } | |
a68fe89c | 2385 | return 0; |
13eb76e0 | 2386 | } |
8df1cd07 | 2387 | |
13eb76e0 | 2388 | #else |
51d7a9eb | 2389 | |
845b6214 | 2390 | static void invalidate_and_set_dirty(MemoryRegion *mr, hwaddr addr, |
a8170e5e | 2391 | hwaddr length) |
51d7a9eb | 2392 | { |
e87f7778 PB |
2393 | uint8_t dirty_log_mask = memory_region_get_dirty_log_mask(mr); |
2394 | /* No early return if dirty_log_mask is or becomes 0, because | |
2395 | * cpu_physical_memory_set_dirty_range will still call | |
2396 | * xen_modified_memory. | |
2397 | */ | |
2398 | if (dirty_log_mask) { | |
2399 | dirty_log_mask = | |
2400 | cpu_physical_memory_range_includes_clean(addr, length, dirty_log_mask); | |
2401 | } | |
2402 | if (dirty_log_mask & (1 << DIRTY_MEMORY_CODE)) { | |
2403 | tb_invalidate_phys_range(addr, addr + length); | |
2404 | dirty_log_mask &= ~(1 << DIRTY_MEMORY_CODE); | |
51d7a9eb | 2405 | } |
e87f7778 | 2406 | cpu_physical_memory_set_dirty_range(addr, length, dirty_log_mask); |
51d7a9eb AP |
2407 | } |
2408 | ||
23326164 | 2409 | static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr) |
82f2563f | 2410 | { |
e1622f4b | 2411 | unsigned access_size_max = mr->ops->valid.max_access_size; |
23326164 RH |
2412 | |
2413 | /* Regions are assumed to support 1-4 byte accesses unless | |
2414 | otherwise specified. */ | |
23326164 RH |
2415 | if (access_size_max == 0) { |
2416 | access_size_max = 4; | |
2417 | } | |
2418 | ||
2419 | /* Bound the maximum access by the alignment of the address. */ | |
2420 | if (!mr->ops->impl.unaligned) { | |
2421 | unsigned align_size_max = addr & -addr; | |
2422 | if (align_size_max != 0 && align_size_max < access_size_max) { | |
2423 | access_size_max = align_size_max; | |
2424 | } | |
82f2563f | 2425 | } |
23326164 RH |
2426 | |
2427 | /* Don't attempt accesses larger than the maximum. */ | |
2428 | if (l > access_size_max) { | |
2429 | l = access_size_max; | |
82f2563f | 2430 | } |
6554f5c0 | 2431 | l = pow2floor(l); |
23326164 RH |
2432 | |
2433 | return l; | |
82f2563f PB |
2434 | } |
2435 | ||
4840f10e | 2436 | static bool prepare_mmio_access(MemoryRegion *mr) |
125b3806 | 2437 | { |
4840f10e JK |
2438 | bool unlocked = !qemu_mutex_iothread_locked(); |
2439 | bool release_lock = false; | |
2440 | ||
2441 | if (unlocked && mr->global_locking) { | |
2442 | qemu_mutex_lock_iothread(); | |
2443 | unlocked = false; | |
2444 | release_lock = true; | |
2445 | } | |
125b3806 | 2446 | if (mr->flush_coalesced_mmio) { |
4840f10e JK |
2447 | if (unlocked) { |
2448 | qemu_mutex_lock_iothread(); | |
2449 | } | |
125b3806 | 2450 | qemu_flush_coalesced_mmio_buffer(); |
4840f10e JK |
2451 | if (unlocked) { |
2452 | qemu_mutex_unlock_iothread(); | |
2453 | } | |
125b3806 | 2454 | } |
4840f10e JK |
2455 | |
2456 | return release_lock; | |
125b3806 PB |
2457 | } |
2458 | ||
5c9eb028 PM |
2459 | MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, |
2460 | uint8_t *buf, int len, bool is_write) | |
13eb76e0 | 2461 | { |
149f54b5 | 2462 | hwaddr l; |
13eb76e0 | 2463 | uint8_t *ptr; |
791af8c8 | 2464 | uint64_t val; |
149f54b5 | 2465 | hwaddr addr1; |
5c8a00ce | 2466 | MemoryRegion *mr; |
3b643495 | 2467 | MemTxResult result = MEMTX_OK; |
4840f10e | 2468 | bool release_lock = false; |
3b46e624 | 2469 | |
41063e1e | 2470 | rcu_read_lock(); |
13eb76e0 | 2471 | while (len > 0) { |
149f54b5 | 2472 | l = len; |
5c8a00ce | 2473 | mr = address_space_translate(as, addr, &addr1, &l, is_write); |
3b46e624 | 2474 | |
13eb76e0 | 2475 | if (is_write) { |
5c8a00ce | 2476 | if (!memory_access_is_direct(mr, is_write)) { |
4840f10e | 2477 | release_lock |= prepare_mmio_access(mr); |
5c8a00ce | 2478 | l = memory_access_size(mr, l, addr1); |
4917cf44 | 2479 | /* XXX: could force current_cpu to NULL to avoid |
6a00d601 | 2480 | potential bugs */ |
23326164 RH |
2481 | switch (l) { |
2482 | case 8: | |
2483 | /* 64 bit write access */ | |
2484 | val = ldq_p(buf); | |
3b643495 PM |
2485 | result |= memory_region_dispatch_write(mr, addr1, val, 8, |
2486 | attrs); | |
23326164 RH |
2487 | break; |
2488 | case 4: | |
1c213d19 | 2489 | /* 32 bit write access */ |
c27004ec | 2490 | val = ldl_p(buf); |
3b643495 PM |
2491 | result |= memory_region_dispatch_write(mr, addr1, val, 4, |
2492 | attrs); | |
23326164 RH |
2493 | break; |
2494 | case 2: | |
1c213d19 | 2495 | /* 16 bit write access */ |
c27004ec | 2496 | val = lduw_p(buf); |
3b643495 PM |
2497 | result |= memory_region_dispatch_write(mr, addr1, val, 2, |
2498 | attrs); | |
23326164 RH |
2499 | break; |
2500 | case 1: | |
1c213d19 | 2501 | /* 8 bit write access */ |
c27004ec | 2502 | val = ldub_p(buf); |
3b643495 PM |
2503 | result |= memory_region_dispatch_write(mr, addr1, val, 1, |
2504 | attrs); | |
23326164 RH |
2505 | break; |
2506 | default: | |
2507 | abort(); | |
13eb76e0 | 2508 | } |
2bbfa05d | 2509 | } else { |
5c8a00ce | 2510 | addr1 += memory_region_get_ram_addr(mr); |
13eb76e0 | 2511 | /* RAM case */ |
5579c7f3 | 2512 | ptr = qemu_get_ram_ptr(addr1); |
13eb76e0 | 2513 | memcpy(ptr, buf, l); |
845b6214 | 2514 | invalidate_and_set_dirty(mr, addr1, l); |
13eb76e0 FB |
2515 | } |
2516 | } else { | |
5c8a00ce | 2517 | if (!memory_access_is_direct(mr, is_write)) { |
13eb76e0 | 2518 | /* I/O case */ |
4840f10e | 2519 | release_lock |= prepare_mmio_access(mr); |
5c8a00ce | 2520 | l = memory_access_size(mr, l, addr1); |
23326164 RH |
2521 | switch (l) { |
2522 | case 8: | |
2523 | /* 64 bit read access */ | |
3b643495 PM |
2524 | result |= memory_region_dispatch_read(mr, addr1, &val, 8, |
2525 | attrs); | |
23326164 RH |
2526 | stq_p(buf, val); |
2527 | break; | |
2528 | case 4: | |
13eb76e0 | 2529 | /* 32 bit read access */ |
3b643495 PM |
2530 | result |= memory_region_dispatch_read(mr, addr1, &val, 4, |
2531 | attrs); | |
c27004ec | 2532 | stl_p(buf, val); |
23326164 RH |
2533 | break; |
2534 | case 2: | |
13eb76e0 | 2535 | /* 16 bit read access */ |
3b643495 PM |
2536 | result |= memory_region_dispatch_read(mr, addr1, &val, 2, |
2537 | attrs); | |
c27004ec | 2538 | stw_p(buf, val); |
23326164 RH |
2539 | break; |
2540 | case 1: | |
1c213d19 | 2541 | /* 8 bit read access */ |
3b643495 PM |
2542 | result |= memory_region_dispatch_read(mr, addr1, &val, 1, |
2543 | attrs); | |
c27004ec | 2544 | stb_p(buf, val); |
23326164 RH |
2545 | break; |
2546 | default: | |
2547 | abort(); | |
13eb76e0 FB |
2548 | } |
2549 | } else { | |
2550 | /* RAM case */ | |
5c8a00ce | 2551 | ptr = qemu_get_ram_ptr(mr->ram_addr + addr1); |
f3705d53 | 2552 | memcpy(buf, ptr, l); |
13eb76e0 FB |
2553 | } |
2554 | } | |
4840f10e JK |
2555 | |
2556 | if (release_lock) { | |
2557 | qemu_mutex_unlock_iothread(); | |
2558 | release_lock = false; | |
2559 | } | |
2560 | ||
13eb76e0 FB |
2561 | len -= l; |
2562 | buf += l; | |
2563 | addr += l; | |
2564 | } | |
41063e1e | 2565 | rcu_read_unlock(); |
fd8aaa76 | 2566 | |
3b643495 | 2567 | return result; |
13eb76e0 | 2568 | } |
8df1cd07 | 2569 | |
5c9eb028 PM |
2570 | MemTxResult address_space_write(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, |
2571 | const uint8_t *buf, int len) | |
ac1970fb | 2572 | { |
5c9eb028 | 2573 | return address_space_rw(as, addr, attrs, (uint8_t *)buf, len, true); |
ac1970fb AK |
2574 | } |
2575 | ||
5c9eb028 PM |
2576 | MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, |
2577 | uint8_t *buf, int len) | |
ac1970fb | 2578 | { |
5c9eb028 | 2579 | return address_space_rw(as, addr, attrs, buf, len, false); |
ac1970fb AK |
2580 | } |
2581 | ||
2582 | ||
a8170e5e | 2583 | void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf, |
ac1970fb AK |
2584 | int len, int is_write) |
2585 | { | |
5c9eb028 PM |
2586 | address_space_rw(&address_space_memory, addr, MEMTXATTRS_UNSPECIFIED, |
2587 | buf, len, is_write); | |
ac1970fb AK |
2588 | } |
2589 | ||
582b55a9 AG |
2590 | enum write_rom_type { |
2591 | WRITE_DATA, | |
2592 | FLUSH_CACHE, | |
2593 | }; | |
2594 | ||
2a221651 | 2595 | static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as, |
582b55a9 | 2596 | hwaddr addr, const uint8_t *buf, int len, enum write_rom_type type) |
d0ecd2aa | 2597 | { |
149f54b5 | 2598 | hwaddr l; |
d0ecd2aa | 2599 | uint8_t *ptr; |
149f54b5 | 2600 | hwaddr addr1; |
5c8a00ce | 2601 | MemoryRegion *mr; |
3b46e624 | 2602 | |
41063e1e | 2603 | rcu_read_lock(); |
d0ecd2aa | 2604 | while (len > 0) { |
149f54b5 | 2605 | l = len; |
2a221651 | 2606 | mr = address_space_translate(as, addr, &addr1, &l, true); |
3b46e624 | 2607 | |
5c8a00ce PB |
2608 | if (!(memory_region_is_ram(mr) || |
2609 | memory_region_is_romd(mr))) { | |
b242e0e0 | 2610 | l = memory_access_size(mr, l, addr1); |
d0ecd2aa | 2611 | } else { |
5c8a00ce | 2612 | addr1 += memory_region_get_ram_addr(mr); |
d0ecd2aa | 2613 | /* ROM/RAM case */ |
5579c7f3 | 2614 | ptr = qemu_get_ram_ptr(addr1); |
582b55a9 AG |
2615 | switch (type) { |
2616 | case WRITE_DATA: | |
2617 | memcpy(ptr, buf, l); | |
845b6214 | 2618 | invalidate_and_set_dirty(mr, addr1, l); |
582b55a9 AG |
2619 | break; |
2620 | case FLUSH_CACHE: | |
2621 | flush_icache_range((uintptr_t)ptr, (uintptr_t)ptr + l); | |
2622 | break; | |
2623 | } | |
d0ecd2aa FB |
2624 | } |
2625 | len -= l; | |
2626 | buf += l; | |
2627 | addr += l; | |
2628 | } | |
41063e1e | 2629 | rcu_read_unlock(); |
d0ecd2aa FB |
2630 | } |
2631 | ||
582b55a9 | 2632 | /* used for ROM loading : can write in RAM and ROM */ |
2a221651 | 2633 | void cpu_physical_memory_write_rom(AddressSpace *as, hwaddr addr, |
582b55a9 AG |
2634 | const uint8_t *buf, int len) |
2635 | { | |
2a221651 | 2636 | cpu_physical_memory_write_rom_internal(as, addr, buf, len, WRITE_DATA); |
582b55a9 AG |
2637 | } |
2638 | ||
2639 | void cpu_flush_icache_range(hwaddr start, int len) | |
2640 | { | |
2641 | /* | |
2642 | * This function should do the same thing as an icache flush that was | |
2643 | * triggered from within the guest. For TCG we are always cache coherent, | |
2644 | * so there is no need to flush anything. For KVM / Xen we need to flush | |
2645 | * the host's instruction cache at least. | |
2646 | */ | |
2647 | if (tcg_enabled()) { | |
2648 | return; | |
2649 | } | |
2650 | ||
2a221651 EI |
2651 | cpu_physical_memory_write_rom_internal(&address_space_memory, |
2652 | start, NULL, len, FLUSH_CACHE); | |
582b55a9 AG |
2653 | } |
2654 | ||
6d16c2f8 | 2655 | typedef struct { |
d3e71559 | 2656 | MemoryRegion *mr; |
6d16c2f8 | 2657 | void *buffer; |
a8170e5e AK |
2658 | hwaddr addr; |
2659 | hwaddr len; | |
c2cba0ff | 2660 | bool in_use; |
6d16c2f8 AL |
2661 | } BounceBuffer; |
2662 | ||
2663 | static BounceBuffer bounce; | |
2664 | ||
ba223c29 | 2665 | typedef struct MapClient { |
e95205e1 | 2666 | QEMUBH *bh; |
72cf2d4f | 2667 | QLIST_ENTRY(MapClient) link; |
ba223c29 AL |
2668 | } MapClient; |
2669 | ||
38e047b5 | 2670 | QemuMutex map_client_list_lock; |
72cf2d4f BS |
2671 | static QLIST_HEAD(map_client_list, MapClient) map_client_list |
2672 | = QLIST_HEAD_INITIALIZER(map_client_list); | |
ba223c29 | 2673 | |
e95205e1 FZ |
2674 | static void cpu_unregister_map_client_do(MapClient *client) |
2675 | { | |
2676 | QLIST_REMOVE(client, link); | |
2677 | g_free(client); | |
2678 | } | |
2679 | ||
33b6c2ed FZ |
2680 | static void cpu_notify_map_clients_locked(void) |
2681 | { | |
2682 | MapClient *client; | |
2683 | ||
2684 | while (!QLIST_EMPTY(&map_client_list)) { | |
2685 | client = QLIST_FIRST(&map_client_list); | |
e95205e1 FZ |
2686 | qemu_bh_schedule(client->bh); |
2687 | cpu_unregister_map_client_do(client); | |
33b6c2ed FZ |
2688 | } |
2689 | } | |
2690 | ||
e95205e1 | 2691 | void cpu_register_map_client(QEMUBH *bh) |
ba223c29 | 2692 | { |
7267c094 | 2693 | MapClient *client = g_malloc(sizeof(*client)); |
ba223c29 | 2694 | |
38e047b5 | 2695 | qemu_mutex_lock(&map_client_list_lock); |
e95205e1 | 2696 | client->bh = bh; |
72cf2d4f | 2697 | QLIST_INSERT_HEAD(&map_client_list, client, link); |
33b6c2ed FZ |
2698 | if (!atomic_read(&bounce.in_use)) { |
2699 | cpu_notify_map_clients_locked(); | |
2700 | } | |
38e047b5 | 2701 | qemu_mutex_unlock(&map_client_list_lock); |
ba223c29 AL |
2702 | } |
2703 | ||
38e047b5 | 2704 | void cpu_exec_init_all(void) |
ba223c29 | 2705 | { |
38e047b5 | 2706 | qemu_mutex_init(&ram_list.mutex); |
38e047b5 | 2707 | io_mem_init(); |
680a4783 | 2708 | memory_map_init(); |
38e047b5 | 2709 | qemu_mutex_init(&map_client_list_lock); |
ba223c29 AL |
2710 | } |
2711 | ||
e95205e1 | 2712 | void cpu_unregister_map_client(QEMUBH *bh) |
ba223c29 AL |
2713 | { |
2714 | MapClient *client; | |
2715 | ||
e95205e1 FZ |
2716 | qemu_mutex_lock(&map_client_list_lock); |
2717 | QLIST_FOREACH(client, &map_client_list, link) { | |
2718 | if (client->bh == bh) { | |
2719 | cpu_unregister_map_client_do(client); | |
2720 | break; | |
2721 | } | |
ba223c29 | 2722 | } |
e95205e1 | 2723 | qemu_mutex_unlock(&map_client_list_lock); |
ba223c29 AL |
2724 | } |
2725 | ||
2726 | static void cpu_notify_map_clients(void) | |
2727 | { | |
38e047b5 | 2728 | qemu_mutex_lock(&map_client_list_lock); |
33b6c2ed | 2729 | cpu_notify_map_clients_locked(); |
38e047b5 | 2730 | qemu_mutex_unlock(&map_client_list_lock); |
ba223c29 AL |
2731 | } |
2732 | ||
51644ab7 PB |
2733 | bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write) |
2734 | { | |
5c8a00ce | 2735 | MemoryRegion *mr; |
51644ab7 PB |
2736 | hwaddr l, xlat; |
2737 | ||
41063e1e | 2738 | rcu_read_lock(); |
51644ab7 PB |
2739 | while (len > 0) { |
2740 | l = len; | |
5c8a00ce PB |
2741 | mr = address_space_translate(as, addr, &xlat, &l, is_write); |
2742 | if (!memory_access_is_direct(mr, is_write)) { | |
2743 | l = memory_access_size(mr, l, addr); | |
2744 | if (!memory_region_access_valid(mr, xlat, l, is_write)) { | |
51644ab7 PB |
2745 | return false; |
2746 | } | |
2747 | } | |
2748 | ||
2749 | len -= l; | |
2750 | addr += l; | |
2751 | } | |
41063e1e | 2752 | rcu_read_unlock(); |
51644ab7 PB |
2753 | return true; |
2754 | } | |
2755 | ||
6d16c2f8 AL |
2756 | /* Map a physical memory region into a host virtual address. |
2757 | * May map a subset of the requested range, given by and returned in *plen. | |
2758 | * May return NULL if resources needed to perform the mapping are exhausted. | |
2759 | * Use only for reads OR writes - not for read-modify-write operations. | |
ba223c29 AL |
2760 | * Use cpu_register_map_client() to know when retrying the map operation is |
2761 | * likely to succeed. | |
6d16c2f8 | 2762 | */ |
ac1970fb | 2763 | void *address_space_map(AddressSpace *as, |
a8170e5e AK |
2764 | hwaddr addr, |
2765 | hwaddr *plen, | |
ac1970fb | 2766 | bool is_write) |
6d16c2f8 | 2767 | { |
a8170e5e | 2768 | hwaddr len = *plen; |
e3127ae0 PB |
2769 | hwaddr done = 0; |
2770 | hwaddr l, xlat, base; | |
2771 | MemoryRegion *mr, *this_mr; | |
2772 | ram_addr_t raddr; | |
6d16c2f8 | 2773 | |
e3127ae0 PB |
2774 | if (len == 0) { |
2775 | return NULL; | |
2776 | } | |
38bee5dc | 2777 | |
e3127ae0 | 2778 | l = len; |
41063e1e | 2779 | rcu_read_lock(); |
e3127ae0 | 2780 | mr = address_space_translate(as, addr, &xlat, &l, is_write); |
41063e1e | 2781 | |
e3127ae0 | 2782 | if (!memory_access_is_direct(mr, is_write)) { |
c2cba0ff | 2783 | if (atomic_xchg(&bounce.in_use, true)) { |
41063e1e | 2784 | rcu_read_unlock(); |
e3127ae0 | 2785 | return NULL; |
6d16c2f8 | 2786 | } |
e85d9db5 KW |
2787 | /* Avoid unbounded allocations */ |
2788 | l = MIN(l, TARGET_PAGE_SIZE); | |
2789 | bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, l); | |
e3127ae0 PB |
2790 | bounce.addr = addr; |
2791 | bounce.len = l; | |
d3e71559 PB |
2792 | |
2793 | memory_region_ref(mr); | |
2794 | bounce.mr = mr; | |
e3127ae0 | 2795 | if (!is_write) { |
5c9eb028 PM |
2796 | address_space_read(as, addr, MEMTXATTRS_UNSPECIFIED, |
2797 | bounce.buffer, l); | |
8ab934f9 | 2798 | } |
6d16c2f8 | 2799 | |
41063e1e | 2800 | rcu_read_unlock(); |
e3127ae0 PB |
2801 | *plen = l; |
2802 | return bounce.buffer; | |
2803 | } | |
2804 | ||
2805 | base = xlat; | |
2806 | raddr = memory_region_get_ram_addr(mr); | |
2807 | ||
2808 | for (;;) { | |
6d16c2f8 AL |
2809 | len -= l; |
2810 | addr += l; | |
e3127ae0 PB |
2811 | done += l; |
2812 | if (len == 0) { | |
2813 | break; | |
2814 | } | |
2815 | ||
2816 | l = len; | |
2817 | this_mr = address_space_translate(as, addr, &xlat, &l, is_write); | |
2818 | if (this_mr != mr || xlat != base + done) { | |
2819 | break; | |
2820 | } | |
6d16c2f8 | 2821 | } |
e3127ae0 | 2822 | |
d3e71559 | 2823 | memory_region_ref(mr); |
41063e1e | 2824 | rcu_read_unlock(); |
e3127ae0 PB |
2825 | *plen = done; |
2826 | return qemu_ram_ptr_length(raddr + base, plen); | |
6d16c2f8 AL |
2827 | } |
2828 | ||
ac1970fb | 2829 | /* Unmaps a memory region previously mapped by address_space_map(). |
6d16c2f8 AL |
2830 | * Will also mark the memory as dirty if is_write == 1. access_len gives |
2831 | * the amount of memory that was actually read or written by the caller. | |
2832 | */ | |
a8170e5e AK |
2833 | void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, |
2834 | int is_write, hwaddr access_len) | |
6d16c2f8 AL |
2835 | { |
2836 | if (buffer != bounce.buffer) { | |
d3e71559 PB |
2837 | MemoryRegion *mr; |
2838 | ram_addr_t addr1; | |
2839 | ||
2840 | mr = qemu_ram_addr_from_host(buffer, &addr1); | |
2841 | assert(mr != NULL); | |
6d16c2f8 | 2842 | if (is_write) { |
845b6214 | 2843 | invalidate_and_set_dirty(mr, addr1, access_len); |
6d16c2f8 | 2844 | } |
868bb33f | 2845 | if (xen_enabled()) { |
e41d7c69 | 2846 | xen_invalidate_map_cache_entry(buffer); |
050a0ddf | 2847 | } |
d3e71559 | 2848 | memory_region_unref(mr); |
6d16c2f8 AL |
2849 | return; |
2850 | } | |
2851 | if (is_write) { | |
5c9eb028 PM |
2852 | address_space_write(as, bounce.addr, MEMTXATTRS_UNSPECIFIED, |
2853 | bounce.buffer, access_len); | |
6d16c2f8 | 2854 | } |
f8a83245 | 2855 | qemu_vfree(bounce.buffer); |
6d16c2f8 | 2856 | bounce.buffer = NULL; |
d3e71559 | 2857 | memory_region_unref(bounce.mr); |
c2cba0ff | 2858 | atomic_mb_set(&bounce.in_use, false); |
ba223c29 | 2859 | cpu_notify_map_clients(); |
6d16c2f8 | 2860 | } |
d0ecd2aa | 2861 | |
a8170e5e AK |
2862 | void *cpu_physical_memory_map(hwaddr addr, |
2863 | hwaddr *plen, | |
ac1970fb AK |
2864 | int is_write) |
2865 | { | |
2866 | return address_space_map(&address_space_memory, addr, plen, is_write); | |
2867 | } | |
2868 | ||
a8170e5e AK |
2869 | void cpu_physical_memory_unmap(void *buffer, hwaddr len, |
2870 | int is_write, hwaddr access_len) | |
ac1970fb AK |
2871 | { |
2872 | return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len); | |
2873 | } | |
2874 | ||
8df1cd07 | 2875 | /* warning: addr must be aligned */ |
50013115 PM |
2876 | static inline uint32_t address_space_ldl_internal(AddressSpace *as, hwaddr addr, |
2877 | MemTxAttrs attrs, | |
2878 | MemTxResult *result, | |
2879 | enum device_endian endian) | |
8df1cd07 | 2880 | { |
8df1cd07 | 2881 | uint8_t *ptr; |
791af8c8 | 2882 | uint64_t val; |
5c8a00ce | 2883 | MemoryRegion *mr; |
149f54b5 PB |
2884 | hwaddr l = 4; |
2885 | hwaddr addr1; | |
50013115 | 2886 | MemTxResult r; |
4840f10e | 2887 | bool release_lock = false; |
8df1cd07 | 2888 | |
41063e1e | 2889 | rcu_read_lock(); |
fdfba1a2 | 2890 | mr = address_space_translate(as, addr, &addr1, &l, false); |
5c8a00ce | 2891 | if (l < 4 || !memory_access_is_direct(mr, false)) { |
4840f10e | 2892 | release_lock |= prepare_mmio_access(mr); |
125b3806 | 2893 | |
8df1cd07 | 2894 | /* I/O case */ |
50013115 | 2895 | r = memory_region_dispatch_read(mr, addr1, &val, 4, attrs); |
1e78bcc1 AG |
2896 | #if defined(TARGET_WORDS_BIGENDIAN) |
2897 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
2898 | val = bswap32(val); | |
2899 | } | |
2900 | #else | |
2901 | if (endian == DEVICE_BIG_ENDIAN) { | |
2902 | val = bswap32(val); | |
2903 | } | |
2904 | #endif | |
8df1cd07 FB |
2905 | } else { |
2906 | /* RAM case */ | |
5c8a00ce | 2907 | ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr) |
06ef3525 | 2908 | & TARGET_PAGE_MASK) |
149f54b5 | 2909 | + addr1); |
1e78bcc1 AG |
2910 | switch (endian) { |
2911 | case DEVICE_LITTLE_ENDIAN: | |
2912 | val = ldl_le_p(ptr); | |
2913 | break; | |
2914 | case DEVICE_BIG_ENDIAN: | |
2915 | val = ldl_be_p(ptr); | |
2916 | break; | |
2917 | default: | |
2918 | val = ldl_p(ptr); | |
2919 | break; | |
2920 | } | |
50013115 PM |
2921 | r = MEMTX_OK; |
2922 | } | |
2923 | if (result) { | |
2924 | *result = r; | |
8df1cd07 | 2925 | } |
4840f10e JK |
2926 | if (release_lock) { |
2927 | qemu_mutex_unlock_iothread(); | |
2928 | } | |
41063e1e | 2929 | rcu_read_unlock(); |
8df1cd07 FB |
2930 | return val; |
2931 | } | |
2932 | ||
50013115 PM |
2933 | uint32_t address_space_ldl(AddressSpace *as, hwaddr addr, |
2934 | MemTxAttrs attrs, MemTxResult *result) | |
2935 | { | |
2936 | return address_space_ldl_internal(as, addr, attrs, result, | |
2937 | DEVICE_NATIVE_ENDIAN); | |
2938 | } | |
2939 | ||
2940 | uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr, | |
2941 | MemTxAttrs attrs, MemTxResult *result) | |
2942 | { | |
2943 | return address_space_ldl_internal(as, addr, attrs, result, | |
2944 | DEVICE_LITTLE_ENDIAN); | |
2945 | } | |
2946 | ||
2947 | uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr, | |
2948 | MemTxAttrs attrs, MemTxResult *result) | |
2949 | { | |
2950 | return address_space_ldl_internal(as, addr, attrs, result, | |
2951 | DEVICE_BIG_ENDIAN); | |
2952 | } | |
2953 | ||
fdfba1a2 | 2954 | uint32_t ldl_phys(AddressSpace *as, hwaddr addr) |
1e78bcc1 | 2955 | { |
50013115 | 2956 | return address_space_ldl(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
2957 | } |
2958 | ||
fdfba1a2 | 2959 | uint32_t ldl_le_phys(AddressSpace *as, hwaddr addr) |
1e78bcc1 | 2960 | { |
50013115 | 2961 | return address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
2962 | } |
2963 | ||
fdfba1a2 | 2964 | uint32_t ldl_be_phys(AddressSpace *as, hwaddr addr) |
1e78bcc1 | 2965 | { |
50013115 | 2966 | return address_space_ldl_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
2967 | } |
2968 | ||
84b7b8e7 | 2969 | /* warning: addr must be aligned */ |
50013115 PM |
2970 | static inline uint64_t address_space_ldq_internal(AddressSpace *as, hwaddr addr, |
2971 | MemTxAttrs attrs, | |
2972 | MemTxResult *result, | |
2973 | enum device_endian endian) | |
84b7b8e7 | 2974 | { |
84b7b8e7 FB |
2975 | uint8_t *ptr; |
2976 | uint64_t val; | |
5c8a00ce | 2977 | MemoryRegion *mr; |
149f54b5 PB |
2978 | hwaddr l = 8; |
2979 | hwaddr addr1; | |
50013115 | 2980 | MemTxResult r; |
4840f10e | 2981 | bool release_lock = false; |
84b7b8e7 | 2982 | |
41063e1e | 2983 | rcu_read_lock(); |
2c17449b | 2984 | mr = address_space_translate(as, addr, &addr1, &l, |
5c8a00ce PB |
2985 | false); |
2986 | if (l < 8 || !memory_access_is_direct(mr, false)) { | |
4840f10e | 2987 | release_lock |= prepare_mmio_access(mr); |
125b3806 | 2988 | |
84b7b8e7 | 2989 | /* I/O case */ |
50013115 | 2990 | r = memory_region_dispatch_read(mr, addr1, &val, 8, attrs); |
968a5627 PB |
2991 | #if defined(TARGET_WORDS_BIGENDIAN) |
2992 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
2993 | val = bswap64(val); | |
2994 | } | |
2995 | #else | |
2996 | if (endian == DEVICE_BIG_ENDIAN) { | |
2997 | val = bswap64(val); | |
2998 | } | |
84b7b8e7 FB |
2999 | #endif |
3000 | } else { | |
3001 | /* RAM case */ | |
5c8a00ce | 3002 | ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr) |
06ef3525 | 3003 | & TARGET_PAGE_MASK) |
149f54b5 | 3004 | + addr1); |
1e78bcc1 AG |
3005 | switch (endian) { |
3006 | case DEVICE_LITTLE_ENDIAN: | |
3007 | val = ldq_le_p(ptr); | |
3008 | break; | |
3009 | case DEVICE_BIG_ENDIAN: | |
3010 | val = ldq_be_p(ptr); | |
3011 | break; | |
3012 | default: | |
3013 | val = ldq_p(ptr); | |
3014 | break; | |
3015 | } | |
50013115 PM |
3016 | r = MEMTX_OK; |
3017 | } | |
3018 | if (result) { | |
3019 | *result = r; | |
84b7b8e7 | 3020 | } |
4840f10e JK |
3021 | if (release_lock) { |
3022 | qemu_mutex_unlock_iothread(); | |
3023 | } | |
41063e1e | 3024 | rcu_read_unlock(); |
84b7b8e7 FB |
3025 | return val; |
3026 | } | |
3027 | ||
50013115 PM |
3028 | uint64_t address_space_ldq(AddressSpace *as, hwaddr addr, |
3029 | MemTxAttrs attrs, MemTxResult *result) | |
3030 | { | |
3031 | return address_space_ldq_internal(as, addr, attrs, result, | |
3032 | DEVICE_NATIVE_ENDIAN); | |
3033 | } | |
3034 | ||
3035 | uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr, | |
3036 | MemTxAttrs attrs, MemTxResult *result) | |
3037 | { | |
3038 | return address_space_ldq_internal(as, addr, attrs, result, | |
3039 | DEVICE_LITTLE_ENDIAN); | |
3040 | } | |
3041 | ||
3042 | uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr, | |
3043 | MemTxAttrs attrs, MemTxResult *result) | |
3044 | { | |
3045 | return address_space_ldq_internal(as, addr, attrs, result, | |
3046 | DEVICE_BIG_ENDIAN); | |
3047 | } | |
3048 | ||
2c17449b | 3049 | uint64_t ldq_phys(AddressSpace *as, hwaddr addr) |
1e78bcc1 | 3050 | { |
50013115 | 3051 | return address_space_ldq(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3052 | } |
3053 | ||
2c17449b | 3054 | uint64_t ldq_le_phys(AddressSpace *as, hwaddr addr) |
1e78bcc1 | 3055 | { |
50013115 | 3056 | return address_space_ldq_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3057 | } |
3058 | ||
2c17449b | 3059 | uint64_t ldq_be_phys(AddressSpace *as, hwaddr addr) |
1e78bcc1 | 3060 | { |
50013115 | 3061 | return address_space_ldq_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3062 | } |
3063 | ||
aab33094 | 3064 | /* XXX: optimize */ |
50013115 PM |
3065 | uint32_t address_space_ldub(AddressSpace *as, hwaddr addr, |
3066 | MemTxAttrs attrs, MemTxResult *result) | |
aab33094 FB |
3067 | { |
3068 | uint8_t val; | |
50013115 PM |
3069 | MemTxResult r; |
3070 | ||
3071 | r = address_space_rw(as, addr, attrs, &val, 1, 0); | |
3072 | if (result) { | |
3073 | *result = r; | |
3074 | } | |
aab33094 FB |
3075 | return val; |
3076 | } | |
3077 | ||
50013115 PM |
3078 | uint32_t ldub_phys(AddressSpace *as, hwaddr addr) |
3079 | { | |
3080 | return address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); | |
3081 | } | |
3082 | ||
733f0b02 | 3083 | /* warning: addr must be aligned */ |
50013115 PM |
3084 | static inline uint32_t address_space_lduw_internal(AddressSpace *as, |
3085 | hwaddr addr, | |
3086 | MemTxAttrs attrs, | |
3087 | MemTxResult *result, | |
3088 | enum device_endian endian) | |
aab33094 | 3089 | { |
733f0b02 MT |
3090 | uint8_t *ptr; |
3091 | uint64_t val; | |
5c8a00ce | 3092 | MemoryRegion *mr; |
149f54b5 PB |
3093 | hwaddr l = 2; |
3094 | hwaddr addr1; | |
50013115 | 3095 | MemTxResult r; |
4840f10e | 3096 | bool release_lock = false; |
733f0b02 | 3097 | |
41063e1e | 3098 | rcu_read_lock(); |
41701aa4 | 3099 | mr = address_space_translate(as, addr, &addr1, &l, |
5c8a00ce PB |
3100 | false); |
3101 | if (l < 2 || !memory_access_is_direct(mr, false)) { | |
4840f10e | 3102 | release_lock |= prepare_mmio_access(mr); |
125b3806 | 3103 | |
733f0b02 | 3104 | /* I/O case */ |
50013115 | 3105 | r = memory_region_dispatch_read(mr, addr1, &val, 2, attrs); |
1e78bcc1 AG |
3106 | #if defined(TARGET_WORDS_BIGENDIAN) |
3107 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
3108 | val = bswap16(val); | |
3109 | } | |
3110 | #else | |
3111 | if (endian == DEVICE_BIG_ENDIAN) { | |
3112 | val = bswap16(val); | |
3113 | } | |
3114 | #endif | |
733f0b02 MT |
3115 | } else { |
3116 | /* RAM case */ | |
5c8a00ce | 3117 | ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr) |
06ef3525 | 3118 | & TARGET_PAGE_MASK) |
149f54b5 | 3119 | + addr1); |
1e78bcc1 AG |
3120 | switch (endian) { |
3121 | case DEVICE_LITTLE_ENDIAN: | |
3122 | val = lduw_le_p(ptr); | |
3123 | break; | |
3124 | case DEVICE_BIG_ENDIAN: | |
3125 | val = lduw_be_p(ptr); | |
3126 | break; | |
3127 | default: | |
3128 | val = lduw_p(ptr); | |
3129 | break; | |
3130 | } | |
50013115 PM |
3131 | r = MEMTX_OK; |
3132 | } | |
3133 | if (result) { | |
3134 | *result = r; | |
733f0b02 | 3135 | } |
4840f10e JK |
3136 | if (release_lock) { |
3137 | qemu_mutex_unlock_iothread(); | |
3138 | } | |
41063e1e | 3139 | rcu_read_unlock(); |
733f0b02 | 3140 | return val; |
aab33094 FB |
3141 | } |
3142 | ||
50013115 PM |
3143 | uint32_t address_space_lduw(AddressSpace *as, hwaddr addr, |
3144 | MemTxAttrs attrs, MemTxResult *result) | |
3145 | { | |
3146 | return address_space_lduw_internal(as, addr, attrs, result, | |
3147 | DEVICE_NATIVE_ENDIAN); | |
3148 | } | |
3149 | ||
3150 | uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr, | |
3151 | MemTxAttrs attrs, MemTxResult *result) | |
3152 | { | |
3153 | return address_space_lduw_internal(as, addr, attrs, result, | |
3154 | DEVICE_LITTLE_ENDIAN); | |
3155 | } | |
3156 | ||
3157 | uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr, | |
3158 | MemTxAttrs attrs, MemTxResult *result) | |
3159 | { | |
3160 | return address_space_lduw_internal(as, addr, attrs, result, | |
3161 | DEVICE_BIG_ENDIAN); | |
3162 | } | |
3163 | ||
41701aa4 | 3164 | uint32_t lduw_phys(AddressSpace *as, hwaddr addr) |
1e78bcc1 | 3165 | { |
50013115 | 3166 | return address_space_lduw(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3167 | } |
3168 | ||
41701aa4 | 3169 | uint32_t lduw_le_phys(AddressSpace *as, hwaddr addr) |
1e78bcc1 | 3170 | { |
50013115 | 3171 | return address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3172 | } |
3173 | ||
41701aa4 | 3174 | uint32_t lduw_be_phys(AddressSpace *as, hwaddr addr) |
1e78bcc1 | 3175 | { |
50013115 | 3176 | return address_space_lduw_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3177 | } |
3178 | ||
8df1cd07 FB |
3179 | /* warning: addr must be aligned. The ram page is not masked as dirty |
3180 | and the code inside is not invalidated. It is useful if the dirty | |
3181 | bits are used to track modified PTEs */ | |
50013115 PM |
3182 | void address_space_stl_notdirty(AddressSpace *as, hwaddr addr, uint32_t val, |
3183 | MemTxAttrs attrs, MemTxResult *result) | |
8df1cd07 | 3184 | { |
8df1cd07 | 3185 | uint8_t *ptr; |
5c8a00ce | 3186 | MemoryRegion *mr; |
149f54b5 PB |
3187 | hwaddr l = 4; |
3188 | hwaddr addr1; | |
50013115 | 3189 | MemTxResult r; |
845b6214 | 3190 | uint8_t dirty_log_mask; |
4840f10e | 3191 | bool release_lock = false; |
8df1cd07 | 3192 | |
41063e1e | 3193 | rcu_read_lock(); |
2198a121 | 3194 | mr = address_space_translate(as, addr, &addr1, &l, |
5c8a00ce PB |
3195 | true); |
3196 | if (l < 4 || !memory_access_is_direct(mr, true)) { | |
4840f10e | 3197 | release_lock |= prepare_mmio_access(mr); |
125b3806 | 3198 | |
50013115 | 3199 | r = memory_region_dispatch_write(mr, addr1, val, 4, attrs); |
8df1cd07 | 3200 | } else { |
5c8a00ce | 3201 | addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK; |
5579c7f3 | 3202 | ptr = qemu_get_ram_ptr(addr1); |
8df1cd07 | 3203 | stl_p(ptr, val); |
74576198 | 3204 | |
845b6214 PB |
3205 | dirty_log_mask = memory_region_get_dirty_log_mask(mr); |
3206 | dirty_log_mask &= ~(1 << DIRTY_MEMORY_CODE); | |
58d2707e | 3207 | cpu_physical_memory_set_dirty_range(addr1, 4, dirty_log_mask); |
50013115 PM |
3208 | r = MEMTX_OK; |
3209 | } | |
3210 | if (result) { | |
3211 | *result = r; | |
8df1cd07 | 3212 | } |
4840f10e JK |
3213 | if (release_lock) { |
3214 | qemu_mutex_unlock_iothread(); | |
3215 | } | |
41063e1e | 3216 | rcu_read_unlock(); |
8df1cd07 FB |
3217 | } |
3218 | ||
50013115 PM |
3219 | void stl_phys_notdirty(AddressSpace *as, hwaddr addr, uint32_t val) |
3220 | { | |
3221 | address_space_stl_notdirty(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); | |
3222 | } | |
3223 | ||
8df1cd07 | 3224 | /* warning: addr must be aligned */ |
50013115 PM |
3225 | static inline void address_space_stl_internal(AddressSpace *as, |
3226 | hwaddr addr, uint32_t val, | |
3227 | MemTxAttrs attrs, | |
3228 | MemTxResult *result, | |
3229 | enum device_endian endian) | |
8df1cd07 | 3230 | { |
8df1cd07 | 3231 | uint8_t *ptr; |
5c8a00ce | 3232 | MemoryRegion *mr; |
149f54b5 PB |
3233 | hwaddr l = 4; |
3234 | hwaddr addr1; | |
50013115 | 3235 | MemTxResult r; |
4840f10e | 3236 | bool release_lock = false; |
8df1cd07 | 3237 | |
41063e1e | 3238 | rcu_read_lock(); |
ab1da857 | 3239 | mr = address_space_translate(as, addr, &addr1, &l, |
5c8a00ce PB |
3240 | true); |
3241 | if (l < 4 || !memory_access_is_direct(mr, true)) { | |
4840f10e | 3242 | release_lock |= prepare_mmio_access(mr); |
125b3806 | 3243 | |
1e78bcc1 AG |
3244 | #if defined(TARGET_WORDS_BIGENDIAN) |
3245 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
3246 | val = bswap32(val); | |
3247 | } | |
3248 | #else | |
3249 | if (endian == DEVICE_BIG_ENDIAN) { | |
3250 | val = bswap32(val); | |
3251 | } | |
3252 | #endif | |
50013115 | 3253 | r = memory_region_dispatch_write(mr, addr1, val, 4, attrs); |
8df1cd07 | 3254 | } else { |
8df1cd07 | 3255 | /* RAM case */ |
5c8a00ce | 3256 | addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK; |
5579c7f3 | 3257 | ptr = qemu_get_ram_ptr(addr1); |
1e78bcc1 AG |
3258 | switch (endian) { |
3259 | case DEVICE_LITTLE_ENDIAN: | |
3260 | stl_le_p(ptr, val); | |
3261 | break; | |
3262 | case DEVICE_BIG_ENDIAN: | |
3263 | stl_be_p(ptr, val); | |
3264 | break; | |
3265 | default: | |
3266 | stl_p(ptr, val); | |
3267 | break; | |
3268 | } | |
845b6214 | 3269 | invalidate_and_set_dirty(mr, addr1, 4); |
50013115 PM |
3270 | r = MEMTX_OK; |
3271 | } | |
3272 | if (result) { | |
3273 | *result = r; | |
8df1cd07 | 3274 | } |
4840f10e JK |
3275 | if (release_lock) { |
3276 | qemu_mutex_unlock_iothread(); | |
3277 | } | |
41063e1e | 3278 | rcu_read_unlock(); |
8df1cd07 FB |
3279 | } |
3280 | ||
50013115 PM |
3281 | void address_space_stl(AddressSpace *as, hwaddr addr, uint32_t val, |
3282 | MemTxAttrs attrs, MemTxResult *result) | |
3283 | { | |
3284 | address_space_stl_internal(as, addr, val, attrs, result, | |
3285 | DEVICE_NATIVE_ENDIAN); | |
3286 | } | |
3287 | ||
3288 | void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val, | |
3289 | MemTxAttrs attrs, MemTxResult *result) | |
3290 | { | |
3291 | address_space_stl_internal(as, addr, val, attrs, result, | |
3292 | DEVICE_LITTLE_ENDIAN); | |
3293 | } | |
3294 | ||
3295 | void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val, | |
3296 | MemTxAttrs attrs, MemTxResult *result) | |
3297 | { | |
3298 | address_space_stl_internal(as, addr, val, attrs, result, | |
3299 | DEVICE_BIG_ENDIAN); | |
3300 | } | |
3301 | ||
ab1da857 | 3302 | void stl_phys(AddressSpace *as, hwaddr addr, uint32_t val) |
1e78bcc1 | 3303 | { |
50013115 | 3304 | address_space_stl(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3305 | } |
3306 | ||
ab1da857 | 3307 | void stl_le_phys(AddressSpace *as, hwaddr addr, uint32_t val) |
1e78bcc1 | 3308 | { |
50013115 | 3309 | address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3310 | } |
3311 | ||
ab1da857 | 3312 | void stl_be_phys(AddressSpace *as, hwaddr addr, uint32_t val) |
1e78bcc1 | 3313 | { |
50013115 | 3314 | address_space_stl_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3315 | } |
3316 | ||
aab33094 | 3317 | /* XXX: optimize */ |
50013115 PM |
3318 | void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val, |
3319 | MemTxAttrs attrs, MemTxResult *result) | |
aab33094 FB |
3320 | { |
3321 | uint8_t v = val; | |
50013115 PM |
3322 | MemTxResult r; |
3323 | ||
3324 | r = address_space_rw(as, addr, attrs, &v, 1, 1); | |
3325 | if (result) { | |
3326 | *result = r; | |
3327 | } | |
3328 | } | |
3329 | ||
3330 | void stb_phys(AddressSpace *as, hwaddr addr, uint32_t val) | |
3331 | { | |
3332 | address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); | |
aab33094 FB |
3333 | } |
3334 | ||
733f0b02 | 3335 | /* warning: addr must be aligned */ |
50013115 PM |
3336 | static inline void address_space_stw_internal(AddressSpace *as, |
3337 | hwaddr addr, uint32_t val, | |
3338 | MemTxAttrs attrs, | |
3339 | MemTxResult *result, | |
3340 | enum device_endian endian) | |
aab33094 | 3341 | { |
733f0b02 | 3342 | uint8_t *ptr; |
5c8a00ce | 3343 | MemoryRegion *mr; |
149f54b5 PB |
3344 | hwaddr l = 2; |
3345 | hwaddr addr1; | |
50013115 | 3346 | MemTxResult r; |
4840f10e | 3347 | bool release_lock = false; |
733f0b02 | 3348 | |
41063e1e | 3349 | rcu_read_lock(); |
5ce5944d | 3350 | mr = address_space_translate(as, addr, &addr1, &l, true); |
5c8a00ce | 3351 | if (l < 2 || !memory_access_is_direct(mr, true)) { |
4840f10e | 3352 | release_lock |= prepare_mmio_access(mr); |
125b3806 | 3353 | |
1e78bcc1 AG |
3354 | #if defined(TARGET_WORDS_BIGENDIAN) |
3355 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
3356 | val = bswap16(val); | |
3357 | } | |
3358 | #else | |
3359 | if (endian == DEVICE_BIG_ENDIAN) { | |
3360 | val = bswap16(val); | |
3361 | } | |
3362 | #endif | |
50013115 | 3363 | r = memory_region_dispatch_write(mr, addr1, val, 2, attrs); |
733f0b02 | 3364 | } else { |
733f0b02 | 3365 | /* RAM case */ |
5c8a00ce | 3366 | addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK; |
733f0b02 | 3367 | ptr = qemu_get_ram_ptr(addr1); |
1e78bcc1 AG |
3368 | switch (endian) { |
3369 | case DEVICE_LITTLE_ENDIAN: | |
3370 | stw_le_p(ptr, val); | |
3371 | break; | |
3372 | case DEVICE_BIG_ENDIAN: | |
3373 | stw_be_p(ptr, val); | |
3374 | break; | |
3375 | default: | |
3376 | stw_p(ptr, val); | |
3377 | break; | |
3378 | } | |
845b6214 | 3379 | invalidate_and_set_dirty(mr, addr1, 2); |
50013115 PM |
3380 | r = MEMTX_OK; |
3381 | } | |
3382 | if (result) { | |
3383 | *result = r; | |
733f0b02 | 3384 | } |
4840f10e JK |
3385 | if (release_lock) { |
3386 | qemu_mutex_unlock_iothread(); | |
3387 | } | |
41063e1e | 3388 | rcu_read_unlock(); |
aab33094 FB |
3389 | } |
3390 | ||
50013115 PM |
3391 | void address_space_stw(AddressSpace *as, hwaddr addr, uint32_t val, |
3392 | MemTxAttrs attrs, MemTxResult *result) | |
3393 | { | |
3394 | address_space_stw_internal(as, addr, val, attrs, result, | |
3395 | DEVICE_NATIVE_ENDIAN); | |
3396 | } | |
3397 | ||
3398 | void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val, | |
3399 | MemTxAttrs attrs, MemTxResult *result) | |
3400 | { | |
3401 | address_space_stw_internal(as, addr, val, attrs, result, | |
3402 | DEVICE_LITTLE_ENDIAN); | |
3403 | } | |
3404 | ||
3405 | void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val, | |
3406 | MemTxAttrs attrs, MemTxResult *result) | |
3407 | { | |
3408 | address_space_stw_internal(as, addr, val, attrs, result, | |
3409 | DEVICE_BIG_ENDIAN); | |
3410 | } | |
3411 | ||
5ce5944d | 3412 | void stw_phys(AddressSpace *as, hwaddr addr, uint32_t val) |
1e78bcc1 | 3413 | { |
50013115 | 3414 | address_space_stw(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3415 | } |
3416 | ||
5ce5944d | 3417 | void stw_le_phys(AddressSpace *as, hwaddr addr, uint32_t val) |
1e78bcc1 | 3418 | { |
50013115 | 3419 | address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3420 | } |
3421 | ||
5ce5944d | 3422 | void stw_be_phys(AddressSpace *as, hwaddr addr, uint32_t val) |
1e78bcc1 | 3423 | { |
50013115 | 3424 | address_space_stw_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3425 | } |
3426 | ||
aab33094 | 3427 | /* XXX: optimize */ |
50013115 PM |
3428 | void address_space_stq(AddressSpace *as, hwaddr addr, uint64_t val, |
3429 | MemTxAttrs attrs, MemTxResult *result) | |
aab33094 | 3430 | { |
50013115 | 3431 | MemTxResult r; |
aab33094 | 3432 | val = tswap64(val); |
50013115 PM |
3433 | r = address_space_rw(as, addr, attrs, (void *) &val, 8, 1); |
3434 | if (result) { | |
3435 | *result = r; | |
3436 | } | |
aab33094 FB |
3437 | } |
3438 | ||
50013115 PM |
3439 | void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val, |
3440 | MemTxAttrs attrs, MemTxResult *result) | |
1e78bcc1 | 3441 | { |
50013115 | 3442 | MemTxResult r; |
1e78bcc1 | 3443 | val = cpu_to_le64(val); |
50013115 PM |
3444 | r = address_space_rw(as, addr, attrs, (void *) &val, 8, 1); |
3445 | if (result) { | |
3446 | *result = r; | |
3447 | } | |
3448 | } | |
3449 | void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val, | |
3450 | MemTxAttrs attrs, MemTxResult *result) | |
3451 | { | |
3452 | MemTxResult r; | |
3453 | val = cpu_to_be64(val); | |
3454 | r = address_space_rw(as, addr, attrs, (void *) &val, 8, 1); | |
3455 | if (result) { | |
3456 | *result = r; | |
3457 | } | |
3458 | } | |
3459 | ||
3460 | void stq_phys(AddressSpace *as, hwaddr addr, uint64_t val) | |
3461 | { | |
3462 | address_space_stq(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); | |
3463 | } | |
3464 | ||
3465 | void stq_le_phys(AddressSpace *as, hwaddr addr, uint64_t val) | |
3466 | { | |
3467 | address_space_stq_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); | |
1e78bcc1 AG |
3468 | } |
3469 | ||
f606604f | 3470 | void stq_be_phys(AddressSpace *as, hwaddr addr, uint64_t val) |
1e78bcc1 | 3471 | { |
50013115 | 3472 | address_space_stq_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL); |
1e78bcc1 AG |
3473 | } |
3474 | ||
5e2972fd | 3475 | /* virtual memory access for debug (includes writing to ROM) */ |
f17ec444 | 3476 | int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr, |
b448f2f3 | 3477 | uint8_t *buf, int len, int is_write) |
13eb76e0 FB |
3478 | { |
3479 | int l; | |
a8170e5e | 3480 | hwaddr phys_addr; |
9b3c35e0 | 3481 | target_ulong page; |
13eb76e0 FB |
3482 | |
3483 | while (len > 0) { | |
3484 | page = addr & TARGET_PAGE_MASK; | |
f17ec444 | 3485 | phys_addr = cpu_get_phys_page_debug(cpu, page); |
13eb76e0 FB |
3486 | /* if no physical page mapped, return an error */ |
3487 | if (phys_addr == -1) | |
3488 | return -1; | |
3489 | l = (page + TARGET_PAGE_SIZE) - addr; | |
3490 | if (l > len) | |
3491 | l = len; | |
5e2972fd | 3492 | phys_addr += (addr & ~TARGET_PAGE_MASK); |
2e38847b EI |
3493 | if (is_write) { |
3494 | cpu_physical_memory_write_rom(cpu->as, phys_addr, buf, l); | |
3495 | } else { | |
5c9eb028 PM |
3496 | address_space_rw(cpu->as, phys_addr, MEMTXATTRS_UNSPECIFIED, |
3497 | buf, l, 0); | |
2e38847b | 3498 | } |
13eb76e0 FB |
3499 | len -= l; |
3500 | buf += l; | |
3501 | addr += l; | |
3502 | } | |
3503 | return 0; | |
3504 | } | |
a68fe89c | 3505 | #endif |
13eb76e0 | 3506 | |
8e4a424b BS |
3507 | /* |
3508 | * A helper function for the _utterly broken_ virtio device model to find out if | |
3509 | * it's running on a big endian machine. Don't do this at home kids! | |
3510 | */ | |
98ed8ecf GK |
3511 | bool target_words_bigendian(void); |
3512 | bool target_words_bigendian(void) | |
8e4a424b BS |
3513 | { |
3514 | #if defined(TARGET_WORDS_BIGENDIAN) | |
3515 | return true; | |
3516 | #else | |
3517 | return false; | |
3518 | #endif | |
3519 | } | |
3520 | ||
76f35538 | 3521 | #ifndef CONFIG_USER_ONLY |
a8170e5e | 3522 | bool cpu_physical_memory_is_io(hwaddr phys_addr) |
76f35538 | 3523 | { |
5c8a00ce | 3524 | MemoryRegion*mr; |
149f54b5 | 3525 | hwaddr l = 1; |
41063e1e | 3526 | bool res; |
76f35538 | 3527 | |
41063e1e | 3528 | rcu_read_lock(); |
5c8a00ce PB |
3529 | mr = address_space_translate(&address_space_memory, |
3530 | phys_addr, &phys_addr, &l, false); | |
76f35538 | 3531 | |
41063e1e PB |
3532 | res = !(memory_region_is_ram(mr) || memory_region_is_romd(mr)); |
3533 | rcu_read_unlock(); | |
3534 | return res; | |
76f35538 | 3535 | } |
bd2fa51f | 3536 | |
e3807054 | 3537 | int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque) |
bd2fa51f MH |
3538 | { |
3539 | RAMBlock *block; | |
e3807054 | 3540 | int ret = 0; |
bd2fa51f | 3541 | |
0dc3f44a MD |
3542 | rcu_read_lock(); |
3543 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { | |
e3807054 DDAG |
3544 | ret = func(block->idstr, block->host, block->offset, |
3545 | block->used_length, opaque); | |
3546 | if (ret) { | |
3547 | break; | |
3548 | } | |
bd2fa51f | 3549 | } |
0dc3f44a | 3550 | rcu_read_unlock(); |
e3807054 | 3551 | return ret; |
bd2fa51f | 3552 | } |
ec3f8c99 | 3553 | #endif |