blob: 2e6fc0087f114e06c99cbf0e27244e1aa0ceaa27 [file] [log] [blame]
bellard54936002003-05-13 00:25:15 +00001/*
Blue Swirl5b6dd862012-12-02 16:04:43 +00002 * Virtual page mapping
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard54936002003-05-13 00:25:15 +00004 * 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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellard54936002003-05-13 00:25:15 +000018 */
bellard67b915a2004-03-31 23:37:16 +000019#include "config.h"
bellardd5a8f072004-09-29 21:15:28 +000020#ifdef _WIN32
21#include <windows.h>
22#else
bellarda98d49b2004-11-14 16:22:05 +000023#include <sys/types.h>
bellardd5a8f072004-09-29 21:15:28 +000024#include <sys/mman.h>
25#endif
bellard54936002003-05-13 00:25:15 +000026
Stefan Weil055403b2010-10-22 23:03:32 +020027#include "qemu-common.h"
bellard6180a182003-09-30 21:04:53 +000028#include "cpu.h"
bellardb67d9a52008-05-23 09:57:34 +000029#include "tcg.h"
pbrookb3c77242008-06-30 16:31:04 +000030#include "hw/hw.h"
Alex Williamsoncc9e98c2010-06-25 11:09:43 -060031#include "hw/qdev.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/osdep.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/kvm.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010034#include "hw/xen/xen.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/timer.h"
36#include "qemu/config-file.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010037#include "exec/memory.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/dma.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010039#include "exec/address-spaces.h"
pbrook53a59602006-03-25 19:31:22 +000040#if defined(CONFIG_USER_ONLY)
41#include <qemu.h>
Jun Nakajima432d2682010-08-31 16:41:25 +010042#else /* !CONFIG_USER_ONLY */
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/xen-mapcache.h"
Stefano Stabellini6506e4f2011-05-19 18:35:44 +010044#include "trace.h"
pbrook53a59602006-03-25 19:31:22 +000045#endif
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +010046#include "exec/cpu-all.h"
bellard54936002003-05-13 00:25:15 +000047
Paolo Bonzini022c62c2012-12-17 18:19:49 +010048#include "exec/cputlb.h"
Blue Swirl5b6dd862012-12-02 16:04:43 +000049#include "translate-all.h"
Blue Swirl0cac1b62012-04-09 16:50:52 +000050
Paolo Bonzini022c62c2012-12-17 18:19:49 +010051#include "exec/memory-internal.h"
Avi Kivity67d95c12011-12-15 15:25:22 +020052
blueswir1db7b5422007-05-26 17:36:03 +000053//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000054
pbrook99773bd2006-04-16 15:14:59 +000055#if !defined(CONFIG_USER_ONLY)
bellard9fa3e852004-01-04 18:06:42 +000056int phys_ram_fd;
aliguori74576192008-10-06 14:02:03 +000057static int in_migration;
pbrook94a6b542009-04-11 17:15:54 +000058
Paolo Bonzinia3161032012-11-14 15:54:48 +010059RAMList ram_list = { .blocks = QTAILQ_HEAD_INITIALIZER(ram_list.blocks) };
Avi Kivity62152b82011-07-26 14:26:14 +030060
61static MemoryRegion *system_memory;
Avi Kivity309cb472011-08-08 16:09:03 +030062static MemoryRegion *system_io;
Avi Kivity62152b82011-07-26 14:26:14 +030063
Avi Kivityf6790af2012-10-02 20:13:51 +020064AddressSpace address_space_io;
65AddressSpace address_space_memory;
Avi Kivity2673a5d2012-10-02 18:49:28 +020066
Paolo Bonzini0844e002013-05-24 14:37:28 +020067MemoryRegion io_mem_rom, io_mem_notdirty;
Jan Kiszkaacc9d802013-05-26 21:55:37 +020068static MemoryRegion io_mem_unassigned;
Avi Kivity0e0df1e2012-01-02 00:32:15 +020069
pbrooke2eef172008-06-08 01:09:01 +000070#endif
bellard9fa3e852004-01-04 18:06:42 +000071
Andreas Färber9349b4f2012-03-14 01:38:32 +010072CPUArchState *first_cpu;
bellard6a00d602005-11-21 23:25:50 +000073/* current CPU in the current thread. It is only valid inside
74 cpu_exec() */
Andreas Färber9349b4f2012-03-14 01:38:32 +010075DEFINE_TLS(CPUArchState *,cpu_single_env);
pbrook2e70f6e2008-06-29 01:03:05 +000076/* 0 = Do not count executed instructions.
thsbf20dc02008-06-30 17:22:19 +000077 1 = Precise instruction counting.
pbrook2e70f6e2008-06-29 01:03:05 +000078 2 = Adaptive rate instruction counting. */
Paolo Bonzini5708fc62012-11-26 15:36:40 +010079int use_icount;
bellard6a00d602005-11-21 23:25:50 +000080
pbrooke2eef172008-06-08 01:09:01 +000081#if !defined(CONFIG_USER_ONLY)
Avi Kivity4346ae32012-02-10 17:00:01 +020082
Paolo Bonzini1db8abb2013-05-21 12:07:21 +020083typedef struct PhysPageEntry PhysPageEntry;
84
85struct PhysPageEntry {
86 uint16_t is_leaf : 1;
87 /* index into phys_sections (is_leaf) or phys_map_nodes (!is_leaf) */
88 uint16_t ptr : 15;
89};
90
91struct AddressSpaceDispatch {
92 /* This is a multi-level map on the physical address space.
93 * The bottom level has pointers to MemoryRegionSections.
94 */
95 PhysPageEntry phys_map;
96 MemoryListener listener;
Jan Kiszkaacc9d802013-05-26 21:55:37 +020097 AddressSpace *as;
Paolo Bonzini1db8abb2013-05-21 12:07:21 +020098};
99
Jan Kiszka90260c62013-05-26 21:46:51 +0200100#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
101typedef struct subpage_t {
102 MemoryRegion iomem;
Jan Kiszkaacc9d802013-05-26 21:55:37 +0200103 AddressSpace *as;
Jan Kiszka90260c62013-05-26 21:46:51 +0200104 hwaddr base;
105 uint16_t sub_section[TARGET_PAGE_SIZE];
106} subpage_t;
107
Avi Kivity5312bd82012-02-12 18:32:55 +0200108static MemoryRegionSection *phys_sections;
109static unsigned phys_sections_nb, phys_sections_nb_alloc;
110static uint16_t phys_section_unassigned;
Avi Kivityaa102232012-03-08 17:06:55 +0200111static uint16_t phys_section_notdirty;
112static uint16_t phys_section_rom;
113static uint16_t phys_section_watch;
Avi Kivity5312bd82012-02-12 18:32:55 +0200114
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200115/* Simple allocator for PhysPageEntry nodes */
116static PhysPageEntry (*phys_map_nodes)[L2_SIZE];
117static unsigned phys_map_nodes_nb, phys_map_nodes_nb_alloc;
118
Avi Kivity07f07b32012-02-13 20:45:32 +0200119#define PHYS_MAP_NODE_NIL (((uint16_t)~0) >> 1)
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200120
pbrooke2eef172008-06-08 01:09:01 +0000121static void io_mem_init(void);
Avi Kivity62152b82011-07-26 14:26:14 +0300122static void memory_map_init(void);
Blue Swirl8b9c99d2012-10-28 11:04:51 +0000123static void *qemu_safe_ram_ptr(ram_addr_t addr);
pbrooke2eef172008-06-08 01:09:01 +0000124
Avi Kivity1ec9b902012-01-02 12:47:48 +0200125static MemoryRegion io_mem_watch;
pbrook6658ffb2007-03-16 23:58:11 +0000126#endif
bellard54936002003-05-13 00:25:15 +0000127
Paul Brook6d9a1302010-02-28 23:55:53 +0000128#if !defined(CONFIG_USER_ONLY)
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200129
Avi Kivityf7bf5462012-02-13 20:12:05 +0200130static void phys_map_node_reserve(unsigned nodes)
131{
132 if (phys_map_nodes_nb + nodes > phys_map_nodes_nb_alloc) {
133 typedef PhysPageEntry Node[L2_SIZE];
134 phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc * 2, 16);
135 phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc,
136 phys_map_nodes_nb + nodes);
137 phys_map_nodes = g_renew(Node, phys_map_nodes,
138 phys_map_nodes_nb_alloc);
139 }
140}
141
142static uint16_t phys_map_node_alloc(void)
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200143{
144 unsigned i;
145 uint16_t ret;
146
Avi Kivityf7bf5462012-02-13 20:12:05 +0200147 ret = phys_map_nodes_nb++;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200148 assert(ret != PHYS_MAP_NODE_NIL);
Avi Kivityf7bf5462012-02-13 20:12:05 +0200149 assert(ret != phys_map_nodes_nb_alloc);
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200150 for (i = 0; i < L2_SIZE; ++i) {
Avi Kivity07f07b32012-02-13 20:45:32 +0200151 phys_map_nodes[ret][i].is_leaf = 0;
Avi Kivityc19e8802012-02-13 20:25:31 +0200152 phys_map_nodes[ret][i].ptr = PHYS_MAP_NODE_NIL;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200153 }
Avi Kivityf7bf5462012-02-13 20:12:05 +0200154 return ret;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200155}
156
157static void phys_map_nodes_reset(void)
158{
159 phys_map_nodes_nb = 0;
160}
161
Avi Kivityf7bf5462012-02-13 20:12:05 +0200162
Avi Kivitya8170e52012-10-23 12:30:10 +0200163static void phys_page_set_level(PhysPageEntry *lp, hwaddr *index,
164 hwaddr *nb, uint16_t leaf,
Avi Kivity29990972012-02-13 20:21:20 +0200165 int level)
Avi Kivityf7bf5462012-02-13 20:12:05 +0200166{
167 PhysPageEntry *p;
168 int i;
Avi Kivitya8170e52012-10-23 12:30:10 +0200169 hwaddr step = (hwaddr)1 << (level * L2_BITS);
Avi Kivityf7bf5462012-02-13 20:12:05 +0200170
Avi Kivity07f07b32012-02-13 20:45:32 +0200171 if (!lp->is_leaf && lp->ptr == PHYS_MAP_NODE_NIL) {
Avi Kivityc19e8802012-02-13 20:25:31 +0200172 lp->ptr = phys_map_node_alloc();
173 p = phys_map_nodes[lp->ptr];
Avi Kivityf7bf5462012-02-13 20:12:05 +0200174 if (level == 0) {
175 for (i = 0; i < L2_SIZE; i++) {
Avi Kivity07f07b32012-02-13 20:45:32 +0200176 p[i].is_leaf = 1;
Avi Kivityc19e8802012-02-13 20:25:31 +0200177 p[i].ptr = phys_section_unassigned;
Avi Kivityf7bf5462012-02-13 20:12:05 +0200178 }
179 }
180 } else {
Avi Kivityc19e8802012-02-13 20:25:31 +0200181 p = phys_map_nodes[lp->ptr];
Avi Kivityf7bf5462012-02-13 20:12:05 +0200182 }
Avi Kivity29990972012-02-13 20:21:20 +0200183 lp = &p[(*index >> (level * L2_BITS)) & (L2_SIZE - 1)];
Avi Kivityf7bf5462012-02-13 20:12:05 +0200184
Avi Kivity29990972012-02-13 20:21:20 +0200185 while (*nb && lp < &p[L2_SIZE]) {
Avi Kivity07f07b32012-02-13 20:45:32 +0200186 if ((*index & (step - 1)) == 0 && *nb >= step) {
187 lp->is_leaf = true;
Avi Kivityc19e8802012-02-13 20:25:31 +0200188 lp->ptr = leaf;
Avi Kivity07f07b32012-02-13 20:45:32 +0200189 *index += step;
190 *nb -= step;
Avi Kivity29990972012-02-13 20:21:20 +0200191 } else {
192 phys_page_set_level(lp, index, nb, leaf, level - 1);
193 }
194 ++lp;
Avi Kivityf7bf5462012-02-13 20:12:05 +0200195 }
196}
197
Avi Kivityac1970f2012-10-03 16:22:53 +0200198static void phys_page_set(AddressSpaceDispatch *d,
Avi Kivitya8170e52012-10-23 12:30:10 +0200199 hwaddr index, hwaddr nb,
Avi Kivity29990972012-02-13 20:21:20 +0200200 uint16_t leaf)
bellard92e873b2004-05-21 14:52:29 +0000201{
Avi Kivity29990972012-02-13 20:21:20 +0200202 /* Wildly overreserve - it doesn't matter much. */
Avi Kivity07f07b32012-02-13 20:45:32 +0200203 phys_map_node_reserve(3 * P_L2_LEVELS);
bellard92e873b2004-05-21 14:52:29 +0000204
Avi Kivityac1970f2012-10-03 16:22:53 +0200205 phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
bellard92e873b2004-05-21 14:52:29 +0000206}
207
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200208static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
bellard92e873b2004-05-21 14:52:29 +0000209{
Avi Kivityac1970f2012-10-03 16:22:53 +0200210 PhysPageEntry lp = d->phys_map;
Avi Kivity31ab2b42012-02-13 16:44:19 +0200211 PhysPageEntry *p;
212 int i;
Avi Kivityf1f6e3b2011-11-20 17:52:22 +0200213
Avi Kivity07f07b32012-02-13 20:45:32 +0200214 for (i = P_L2_LEVELS - 1; i >= 0 && !lp.is_leaf; i--) {
Avi Kivityc19e8802012-02-13 20:25:31 +0200215 if (lp.ptr == PHYS_MAP_NODE_NIL) {
Paolo Bonzinifd298932013-05-20 12:21:07 +0200216 return &phys_sections[phys_section_unassigned];
Avi Kivity31ab2b42012-02-13 16:44:19 +0200217 }
Avi Kivityc19e8802012-02-13 20:25:31 +0200218 p = phys_map_nodes[lp.ptr];
Avi Kivity31ab2b42012-02-13 16:44:19 +0200219 lp = p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)];
Avi Kivityf1f6e3b2011-11-20 17:52:22 +0200220 }
Paolo Bonzinifd298932013-05-20 12:21:07 +0200221 return &phys_sections[lp.ptr];
Avi Kivityf3705d52012-03-08 16:16:34 +0200222}
223
Blue Swirle5548612012-04-21 13:08:33 +0000224bool memory_region_is_unassigned(MemoryRegion *mr)
225{
Paolo Bonzini2a8e7492013-05-24 14:34:08 +0200226 return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device
Blue Swirle5548612012-04-21 13:08:33 +0000227 && mr != &io_mem_watch;
228}
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200229
Jan Kiszka9f029602013-05-06 16:48:02 +0200230static MemoryRegionSection *address_space_lookup_region(AddressSpace *as,
Jan Kiszka90260c62013-05-26 21:46:51 +0200231 hwaddr addr,
232 bool resolve_subpage)
Jan Kiszka9f029602013-05-06 16:48:02 +0200233{
Jan Kiszka90260c62013-05-26 21:46:51 +0200234 MemoryRegionSection *section;
235 subpage_t *subpage;
236
237 section = phys_page_find(as->dispatch, addr >> TARGET_PAGE_BITS);
238 if (resolve_subpage && section->mr->subpage) {
239 subpage = container_of(section->mr, subpage_t, iomem);
240 section = &phys_sections[subpage->sub_section[SUBPAGE_IDX(addr)]];
241 }
242 return section;
Jan Kiszka9f029602013-05-06 16:48:02 +0200243}
244
Jan Kiszka90260c62013-05-26 21:46:51 +0200245static MemoryRegionSection *
246address_space_translate_internal(AddressSpace *as, hwaddr addr, hwaddr *xlat,
247 hwaddr *plen, bool resolve_subpage)
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200248{
249 MemoryRegionSection *section;
250 Int128 diff;
251
Jan Kiszka90260c62013-05-26 21:46:51 +0200252 section = address_space_lookup_region(as, addr, resolve_subpage);
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200253 /* Compute offset within MemoryRegionSection */
254 addr -= section->offset_within_address_space;
255
256 /* Compute offset within MemoryRegion */
257 *xlat = addr + section->offset_within_region;
258
259 diff = int128_sub(section->mr->size, int128_make64(addr));
Peter Maydell3752a032013-06-20 15:18:04 +0100260 *plen = int128_get64(int128_min(diff, int128_make64(*plen)));
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200261 return section;
262}
Jan Kiszka90260c62013-05-26 21:46:51 +0200263
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +0200264MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
265 hwaddr *xlat, hwaddr *plen,
266 bool is_write)
Jan Kiszka90260c62013-05-26 21:46:51 +0200267{
Avi Kivity30951152012-10-30 13:47:46 +0200268 IOMMUTLBEntry iotlb;
269 MemoryRegionSection *section;
270 MemoryRegion *mr;
271 hwaddr len = *plen;
272
273 for (;;) {
274 section = address_space_translate_internal(as, addr, &addr, plen, true);
275 mr = section->mr;
276
277 if (!mr->iommu_ops) {
278 break;
279 }
280
281 iotlb = mr->iommu_ops->translate(mr, addr);
282 addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
283 | (addr & iotlb.addr_mask));
284 len = MIN(len, (addr | iotlb.addr_mask) - addr + 1);
285 if (!(iotlb.perm & (1 << is_write))) {
286 mr = &io_mem_unassigned;
287 break;
288 }
289
290 as = iotlb.target_as;
291 }
292
293 *plen = len;
294 *xlat = addr;
295 return mr;
Jan Kiszka90260c62013-05-26 21:46:51 +0200296}
297
298MemoryRegionSection *
299address_space_translate_for_iotlb(AddressSpace *as, hwaddr addr, hwaddr *xlat,
300 hwaddr *plen)
301{
Avi Kivity30951152012-10-30 13:47:46 +0200302 MemoryRegionSection *section;
303 section = address_space_translate_internal(as, addr, xlat, plen, false);
304
305 assert(!section->mr->iommu_ops);
306 return section;
Jan Kiszka90260c62013-05-26 21:46:51 +0200307}
bellard9fa3e852004-01-04 18:06:42 +0000308#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000309
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200310void cpu_exec_init_all(void)
311{
312#if !defined(CONFIG_USER_ONLY)
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700313 qemu_mutex_init(&ram_list.mutex);
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200314 memory_map_init();
315 io_mem_init();
316#endif
317}
318
Andreas Färberb170fce2013-01-20 20:23:22 +0100319#if !defined(CONFIG_USER_ONLY)
pbrook9656f322008-07-01 20:01:19 +0000320
Juan Quintelae59fb372009-09-29 22:48:21 +0200321static int cpu_common_post_load(void *opaque, int version_id)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200322{
Andreas Färber259186a2013-01-17 18:51:17 +0100323 CPUState *cpu = opaque;
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200324
aurel323098dba2009-03-07 21:28:24 +0000325 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
326 version_id is increased. */
Andreas Färber259186a2013-01-17 18:51:17 +0100327 cpu->interrupt_request &= ~0x01;
328 tlb_flush(cpu->env_ptr, 1);
pbrook9656f322008-07-01 20:01:19 +0000329
330 return 0;
331}
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200332
333static const VMStateDescription vmstate_cpu_common = {
334 .name = "cpu_common",
335 .version_id = 1,
336 .minimum_version_id = 1,
337 .minimum_version_id_old = 1,
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200338 .post_load = cpu_common_post_load,
339 .fields = (VMStateField []) {
Andreas Färber259186a2013-01-17 18:51:17 +0100340 VMSTATE_UINT32(halted, CPUState),
341 VMSTATE_UINT32(interrupt_request, CPUState),
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200342 VMSTATE_END_OF_LIST()
343 }
344};
Andreas Färberb170fce2013-01-20 20:23:22 +0100345#else
346#define vmstate_cpu_common vmstate_dummy
pbrook9656f322008-07-01 20:01:19 +0000347#endif
348
Andreas Färber38d8f5c2012-12-17 19:47:15 +0100349CPUState *qemu_get_cpu(int index)
Glauber Costa950f1472009-06-09 12:15:18 -0400350{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100351 CPUArchState *env = first_cpu;
Andreas Färber38d8f5c2012-12-17 19:47:15 +0100352 CPUState *cpu = NULL;
Glauber Costa950f1472009-06-09 12:15:18 -0400353
354 while (env) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100355 cpu = ENV_GET_CPU(env);
356 if (cpu->cpu_index == index) {
Glauber Costa950f1472009-06-09 12:15:18 -0400357 break;
Andreas Färber55e5c282012-12-17 06:18:02 +0100358 }
Glauber Costa950f1472009-06-09 12:15:18 -0400359 env = env->next_cpu;
360 }
361
Igor Mammedovd76fdda2013-03-07 19:12:43 +0100362 return env ? cpu : NULL;
Glauber Costa950f1472009-06-09 12:15:18 -0400363}
364
Michael S. Tsirkind6b9e0d2013-04-24 22:58:04 +0200365void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data)
366{
367 CPUArchState *env = first_cpu;
368
369 while (env) {
370 func(ENV_GET_CPU(env), data);
371 env = env->next_cpu;
372 }
373}
374
Andreas Färber9349b4f2012-03-14 01:38:32 +0100375void cpu_exec_init(CPUArchState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000376{
Andreas Färber9f09e182012-05-03 06:59:07 +0200377 CPUState *cpu = ENV_GET_CPU(env);
Andreas Färberb170fce2013-01-20 20:23:22 +0100378 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber9349b4f2012-03-14 01:38:32 +0100379 CPUArchState **penv;
bellard6a00d602005-11-21 23:25:50 +0000380 int cpu_index;
381
pbrookc2764712009-03-07 15:24:59 +0000382#if defined(CONFIG_USER_ONLY)
383 cpu_list_lock();
384#endif
bellard6a00d602005-11-21 23:25:50 +0000385 env->next_cpu = NULL;
386 penv = &first_cpu;
387 cpu_index = 0;
388 while (*penv != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700389 penv = &(*penv)->next_cpu;
bellard6a00d602005-11-21 23:25:50 +0000390 cpu_index++;
391 }
Andreas Färber55e5c282012-12-17 06:18:02 +0100392 cpu->cpu_index = cpu_index;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +0100393 cpu->numa_node = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000394 QTAILQ_INIT(&env->breakpoints);
395 QTAILQ_INIT(&env->watchpoints);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100396#ifndef CONFIG_USER_ONLY
Andreas Färber9f09e182012-05-03 06:59:07 +0200397 cpu->thread_id = qemu_get_thread_id();
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100398#endif
bellard6a00d602005-11-21 23:25:50 +0000399 *penv = env;
pbrookc2764712009-03-07 15:24:59 +0000400#if defined(CONFIG_USER_ONLY)
401 cpu_list_unlock();
402#endif
Andreas Färber259186a2013-01-17 18:51:17 +0100403 vmstate_register(NULL, cpu_index, &vmstate_cpu_common, cpu);
pbrookb3c77242008-06-30 16:31:04 +0000404#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
Alex Williamson0be71e32010-06-25 11:09:07 -0600405 register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
pbrookb3c77242008-06-30 16:31:04 +0000406 cpu_save, cpu_load, env);
Andreas Färberb170fce2013-01-20 20:23:22 +0100407 assert(cc->vmsd == NULL);
pbrookb3c77242008-06-30 16:31:04 +0000408#endif
Andreas Färberb170fce2013-01-20 20:23:22 +0100409 if (cc->vmsd != NULL) {
410 vmstate_register(NULL, cpu_index, cc->vmsd, cpu);
411 }
bellardfd6ce8f2003-05-14 19:00:11 +0000412}
413
bellard1fddef42005-04-17 19:16:13 +0000414#if defined(TARGET_HAS_ICE)
Paul Brook94df27f2010-02-28 23:47:45 +0000415#if defined(CONFIG_USER_ONLY)
Andreas Färber9349b4f2012-03-14 01:38:32 +0100416static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
Paul Brook94df27f2010-02-28 23:47:45 +0000417{
418 tb_invalidate_phys_page_range(pc, pc + 1, 0);
419}
420#else
Max Filippov1e7855a2012-04-10 02:48:17 +0400421static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
422{
Max Filippov9d70c4b2012-05-27 20:21:08 +0400423 tb_invalidate_phys_addr(cpu_get_phys_page_debug(env, pc) |
424 (pc & ~TARGET_PAGE_MASK));
Max Filippov1e7855a2012-04-10 02:48:17 +0400425}
bellardc27004e2005-01-03 23:35:10 +0000426#endif
Paul Brook94df27f2010-02-28 23:47:45 +0000427#endif /* TARGET_HAS_ICE */
bellardd720b932004-04-25 17:57:43 +0000428
Paul Brookc527ee82010-03-01 03:31:14 +0000429#if defined(CONFIG_USER_ONLY)
Andreas Färber9349b4f2012-03-14 01:38:32 +0100430void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
Paul Brookc527ee82010-03-01 03:31:14 +0000431
432{
433}
434
Andreas Färber9349b4f2012-03-14 01:38:32 +0100435int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
Paul Brookc527ee82010-03-01 03:31:14 +0000436 int flags, CPUWatchpoint **watchpoint)
437{
438 return -ENOSYS;
439}
440#else
pbrook6658ffb2007-03-16 23:58:11 +0000441/* Add a watchpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100442int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
aliguoria1d1bb32008-11-18 20:07:32 +0000443 int flags, CPUWatchpoint **watchpoint)
pbrook6658ffb2007-03-16 23:58:11 +0000444{
aliguorib4051332008-11-18 20:14:20 +0000445 target_ulong len_mask = ~(len - 1);
aliguoric0ce9982008-11-25 22:13:57 +0000446 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +0000447
aliguorib4051332008-11-18 20:14:20 +0000448 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
Max Filippov0dc23822012-01-29 03:15:23 +0400449 if ((len & (len - 1)) || (addr & ~len_mask) ||
450 len == 0 || len > TARGET_PAGE_SIZE) {
aliguorib4051332008-11-18 20:14:20 +0000451 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
452 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
453 return -EINVAL;
454 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500455 wp = g_malloc(sizeof(*wp));
pbrook6658ffb2007-03-16 23:58:11 +0000456
aliguoria1d1bb32008-11-18 20:07:32 +0000457 wp->vaddr = addr;
aliguorib4051332008-11-18 20:14:20 +0000458 wp->len_mask = len_mask;
aliguoria1d1bb32008-11-18 20:07:32 +0000459 wp->flags = flags;
460
aliguori2dc9f412008-11-18 20:56:59 +0000461 /* keep all GDB-injected watchpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +0000462 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000463 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
aliguoric0ce9982008-11-25 22:13:57 +0000464 else
Blue Swirl72cf2d42009-09-12 07:36:22 +0000465 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +0000466
pbrook6658ffb2007-03-16 23:58:11 +0000467 tlb_flush_page(env, addr);
aliguoria1d1bb32008-11-18 20:07:32 +0000468
469 if (watchpoint)
470 *watchpoint = wp;
471 return 0;
pbrook6658ffb2007-03-16 23:58:11 +0000472}
473
aliguoria1d1bb32008-11-18 20:07:32 +0000474/* Remove a specific watchpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100475int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr, target_ulong len,
aliguoria1d1bb32008-11-18 20:07:32 +0000476 int flags)
pbrook6658ffb2007-03-16 23:58:11 +0000477{
aliguorib4051332008-11-18 20:14:20 +0000478 target_ulong len_mask = ~(len - 1);
aliguoria1d1bb32008-11-18 20:07:32 +0000479 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +0000480
Blue Swirl72cf2d42009-09-12 07:36:22 +0000481 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +0000482 if (addr == wp->vaddr && len_mask == wp->len_mask
aliguori6e140f22008-11-18 20:37:55 +0000483 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
aliguoria1d1bb32008-11-18 20:07:32 +0000484 cpu_watchpoint_remove_by_ref(env, wp);
pbrook6658ffb2007-03-16 23:58:11 +0000485 return 0;
486 }
487 }
aliguoria1d1bb32008-11-18 20:07:32 +0000488 return -ENOENT;
pbrook6658ffb2007-03-16 23:58:11 +0000489}
490
aliguoria1d1bb32008-11-18 20:07:32 +0000491/* Remove a specific watchpoint by reference. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100492void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint)
aliguoria1d1bb32008-11-18 20:07:32 +0000493{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000494 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
edgar_igl7d03f822008-05-17 18:58:29 +0000495
aliguoria1d1bb32008-11-18 20:07:32 +0000496 tlb_flush_page(env, watchpoint->vaddr);
497
Anthony Liguori7267c092011-08-20 22:09:37 -0500498 g_free(watchpoint);
edgar_igl7d03f822008-05-17 18:58:29 +0000499}
500
aliguoria1d1bb32008-11-18 20:07:32 +0000501/* Remove all matching watchpoints. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100502void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
aliguoria1d1bb32008-11-18 20:07:32 +0000503{
aliguoric0ce9982008-11-25 22:13:57 +0000504 CPUWatchpoint *wp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +0000505
Blue Swirl72cf2d42009-09-12 07:36:22 +0000506 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +0000507 if (wp->flags & mask)
508 cpu_watchpoint_remove_by_ref(env, wp);
aliguoric0ce9982008-11-25 22:13:57 +0000509 }
aliguoria1d1bb32008-11-18 20:07:32 +0000510}
Paul Brookc527ee82010-03-01 03:31:14 +0000511#endif
aliguoria1d1bb32008-11-18 20:07:32 +0000512
513/* Add a breakpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100514int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
aliguoria1d1bb32008-11-18 20:07:32 +0000515 CPUBreakpoint **breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +0000516{
bellard1fddef42005-04-17 19:16:13 +0000517#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +0000518 CPUBreakpoint *bp;
ths3b46e622007-09-17 08:09:54 +0000519
Anthony Liguori7267c092011-08-20 22:09:37 -0500520 bp = g_malloc(sizeof(*bp));
aliguoria1d1bb32008-11-18 20:07:32 +0000521
522 bp->pc = pc;
523 bp->flags = flags;
524
aliguori2dc9f412008-11-18 20:56:59 +0000525 /* keep all GDB-injected breakpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +0000526 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000527 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
aliguoric0ce9982008-11-25 22:13:57 +0000528 else
Blue Swirl72cf2d42009-09-12 07:36:22 +0000529 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +0000530
531 breakpoint_invalidate(env, pc);
532
533 if (breakpoint)
534 *breakpoint = bp;
535 return 0;
536#else
537 return -ENOSYS;
538#endif
539}
540
541/* Remove a specific breakpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100542int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags)
aliguoria1d1bb32008-11-18 20:07:32 +0000543{
544#if defined(TARGET_HAS_ICE)
545 CPUBreakpoint *bp;
546
Blue Swirl72cf2d42009-09-12 07:36:22 +0000547 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +0000548 if (bp->pc == pc && bp->flags == flags) {
549 cpu_breakpoint_remove_by_ref(env, bp);
bellard4c3a88a2003-07-26 12:06:08 +0000550 return 0;
aliguoria1d1bb32008-11-18 20:07:32 +0000551 }
bellard4c3a88a2003-07-26 12:06:08 +0000552 }
aliguoria1d1bb32008-11-18 20:07:32 +0000553 return -ENOENT;
bellard4c3a88a2003-07-26 12:06:08 +0000554#else
aliguoria1d1bb32008-11-18 20:07:32 +0000555 return -ENOSYS;
bellard4c3a88a2003-07-26 12:06:08 +0000556#endif
557}
558
aliguoria1d1bb32008-11-18 20:07:32 +0000559/* Remove a specific breakpoint by reference. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100560void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +0000561{
bellard1fddef42005-04-17 19:16:13 +0000562#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000563 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
bellardd720b932004-04-25 17:57:43 +0000564
aliguoria1d1bb32008-11-18 20:07:32 +0000565 breakpoint_invalidate(env, breakpoint->pc);
566
Anthony Liguori7267c092011-08-20 22:09:37 -0500567 g_free(breakpoint);
aliguoria1d1bb32008-11-18 20:07:32 +0000568#endif
569}
570
571/* Remove all matching breakpoints. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100572void cpu_breakpoint_remove_all(CPUArchState *env, int mask)
aliguoria1d1bb32008-11-18 20:07:32 +0000573{
574#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +0000575 CPUBreakpoint *bp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +0000576
Blue Swirl72cf2d42009-09-12 07:36:22 +0000577 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +0000578 if (bp->flags & mask)
579 cpu_breakpoint_remove_by_ref(env, bp);
aliguoric0ce9982008-11-25 22:13:57 +0000580 }
bellard4c3a88a2003-07-26 12:06:08 +0000581#endif
582}
583
bellardc33a3462003-07-29 20:50:33 +0000584/* enable or disable single step mode. EXCP_DEBUG is returned by the
585 CPU loop after each instruction */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100586void cpu_single_step(CPUArchState *env, int enabled)
bellardc33a3462003-07-29 20:50:33 +0000587{
bellard1fddef42005-04-17 19:16:13 +0000588#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +0000589 if (env->singlestep_enabled != enabled) {
590 env->singlestep_enabled = enabled;
aliguorie22a25c2009-03-12 20:12:48 +0000591 if (kvm_enabled())
592 kvm_update_guest_debug(env, 0);
593 else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100594 /* must flush all the translated code to avoid inconsistencies */
aliguorie22a25c2009-03-12 20:12:48 +0000595 /* XXX: only flush what is necessary */
596 tb_flush(env);
597 }
bellardc33a3462003-07-29 20:50:33 +0000598 }
599#endif
600}
601
Andreas Färber9349b4f2012-03-14 01:38:32 +0100602void cpu_exit(CPUArchState *env)
aurel323098dba2009-03-07 21:28:24 +0000603{
Andreas Färberfcd7d002012-12-17 08:02:44 +0100604 CPUState *cpu = ENV_GET_CPU(env);
605
606 cpu->exit_request = 1;
Peter Maydell378df4b2013-02-22 18:10:03 +0000607 cpu->tcg_exit_req = 1;
aurel323098dba2009-03-07 21:28:24 +0000608}
609
Andreas Färber9349b4f2012-03-14 01:38:32 +0100610void cpu_abort(CPUArchState *env, const char *fmt, ...)
bellard75012672003-06-21 13:11:07 +0000611{
612 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +0000613 va_list ap2;
bellard75012672003-06-21 13:11:07 +0000614
615 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +0000616 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +0000617 fprintf(stderr, "qemu: fatal: ");
618 vfprintf(stderr, fmt, ap);
619 fprintf(stderr, "\n");
Peter Maydell6fd2a022012-10-05 15:04:43 +0100620 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
aliguori93fcfe32009-01-15 22:34:14 +0000621 if (qemu_log_enabled()) {
622 qemu_log("qemu: fatal: ");
623 qemu_log_vprintf(fmt, ap2);
624 qemu_log("\n");
Peter Maydell6fd2a022012-10-05 15:04:43 +0100625 log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
aliguori31b1a7b2009-01-15 22:35:09 +0000626 qemu_log_flush();
aliguori93fcfe32009-01-15 22:34:14 +0000627 qemu_log_close();
balrog924edca2007-06-10 14:07:13 +0000628 }
pbrook493ae1f2007-11-23 16:53:59 +0000629 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +0000630 va_end(ap);
Riku Voipiofd052bf2010-01-25 14:30:49 +0200631#if defined(CONFIG_USER_ONLY)
632 {
633 struct sigaction act;
634 sigfillset(&act.sa_mask);
635 act.sa_handler = SIG_DFL;
636 sigaction(SIGABRT, &act, NULL);
637 }
638#endif
bellard75012672003-06-21 13:11:07 +0000639 abort();
640}
641
Andreas Färber9349b4f2012-03-14 01:38:32 +0100642CPUArchState *cpu_copy(CPUArchState *env)
thsc5be9f02007-02-28 20:20:53 +0000643{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100644 CPUArchState *new_env = cpu_init(env->cpu_model_str);
645 CPUArchState *next_cpu = new_env->next_cpu;
aliguori5a38f082009-01-15 20:16:51 +0000646#if defined(TARGET_HAS_ICE)
647 CPUBreakpoint *bp;
648 CPUWatchpoint *wp;
649#endif
650
Andreas Färber9349b4f2012-03-14 01:38:32 +0100651 memcpy(new_env, env, sizeof(CPUArchState));
aliguori5a38f082009-01-15 20:16:51 +0000652
Andreas Färber55e5c282012-12-17 06:18:02 +0100653 /* Preserve chaining. */
thsc5be9f02007-02-28 20:20:53 +0000654 new_env->next_cpu = next_cpu;
aliguori5a38f082009-01-15 20:16:51 +0000655
656 /* Clone all break/watchpoints.
657 Note: Once we support ptrace with hw-debug register access, make sure
658 BP_CPU break/watchpoints are handled correctly on clone. */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000659 QTAILQ_INIT(&env->breakpoints);
660 QTAILQ_INIT(&env->watchpoints);
aliguori5a38f082009-01-15 20:16:51 +0000661#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000662 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +0000663 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
664 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000665 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +0000666 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
667 wp->flags, NULL);
668 }
669#endif
670
thsc5be9f02007-02-28 20:20:53 +0000671 return new_env;
672}
673
bellard01243112004-01-04 15:48:17 +0000674#if !defined(CONFIG_USER_ONLY)
Juan Quintelad24981d2012-05-22 00:42:40 +0200675static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t end,
676 uintptr_t length)
bellard1ccde1c2004-02-06 19:46:14 +0000677{
Juan Quintelad24981d2012-05-22 00:42:40 +0200678 uintptr_t start1;
bellardf23db162005-08-21 19:12:28 +0000679
bellard1ccde1c2004-02-06 19:46:14 +0000680 /* we modify the TLB cache so that the dirty bit will be set again
681 when accessing the range */
Stefan Weil8efe0ca2012-04-12 15:42:19 +0200682 start1 = (uintptr_t)qemu_safe_ram_ptr(start);
Stefan Weila57d23e2011-04-30 22:49:26 +0200683 /* Check that we don't span multiple blocks - this breaks the
pbrook5579c7f2009-04-11 14:47:08 +0000684 address comparisons below. */
Stefan Weil8efe0ca2012-04-12 15:42:19 +0200685 if ((uintptr_t)qemu_safe_ram_ptr(end - 1) - start1
pbrook5579c7f2009-04-11 14:47:08 +0000686 != (end - 1) - start) {
687 abort();
688 }
Blue Swirle5548612012-04-21 13:08:33 +0000689 cpu_tlb_reset_dirty_all(start1, length);
Juan Quintelad24981d2012-05-22 00:42:40 +0200690
691}
692
693/* Note: start and end must be within the same ram block. */
694void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
695 int dirty_flags)
696{
697 uintptr_t length;
698
699 start &= TARGET_PAGE_MASK;
700 end = TARGET_PAGE_ALIGN(end);
701
702 length = end - start;
703 if (length == 0)
704 return;
705 cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
706
707 if (tcg_enabled()) {
708 tlb_reset_dirty_range_all(start, end, length);
709 }
bellard1ccde1c2004-02-06 19:46:14 +0000710}
711
Blue Swirl8b9c99d2012-10-28 11:04:51 +0000712static int cpu_physical_memory_set_dirty_tracking(int enable)
aliguori74576192008-10-06 14:02:03 +0000713{
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +0200714 int ret = 0;
aliguori74576192008-10-06 14:02:03 +0000715 in_migration = enable;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +0200716 return ret;
aliguori74576192008-10-06 14:02:03 +0000717}
718
Avi Kivitya8170e52012-10-23 12:30:10 +0200719hwaddr memory_region_section_get_iotlb(CPUArchState *env,
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200720 MemoryRegionSection *section,
721 target_ulong vaddr,
722 hwaddr paddr, hwaddr xlat,
723 int prot,
724 target_ulong *address)
Blue Swirle5548612012-04-21 13:08:33 +0000725{
Avi Kivitya8170e52012-10-23 12:30:10 +0200726 hwaddr iotlb;
Blue Swirle5548612012-04-21 13:08:33 +0000727 CPUWatchpoint *wp;
728
Blue Swirlcc5bea62012-04-14 14:56:48 +0000729 if (memory_region_is_ram(section->mr)) {
Blue Swirle5548612012-04-21 13:08:33 +0000730 /* Normal RAM. */
731 iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200732 + xlat;
Blue Swirle5548612012-04-21 13:08:33 +0000733 if (!section->readonly) {
734 iotlb |= phys_section_notdirty;
735 } else {
736 iotlb |= phys_section_rom;
737 }
738 } else {
Blue Swirle5548612012-04-21 13:08:33 +0000739 iotlb = section - phys_sections;
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200740 iotlb += xlat;
Blue Swirle5548612012-04-21 13:08:33 +0000741 }
742
743 /* Make accesses to pages with watchpoints go via the
744 watchpoint trap routines. */
745 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
746 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
747 /* Avoid trapping reads of pages with a write breakpoint. */
748 if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
749 iotlb = phys_section_watch + paddr;
750 *address |= TLB_MMIO;
751 break;
752 }
753 }
754 }
755
756 return iotlb;
757}
bellard9fa3e852004-01-04 18:06:42 +0000758#endif /* defined(CONFIG_USER_ONLY) */
759
pbrooke2eef172008-06-08 01:09:01 +0000760#if !defined(CONFIG_USER_ONLY)
pbrook8da3ff12008-12-01 18:59:50 +0000761
Anthony Liguoric227f092009-10-01 16:12:16 -0500762static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
Avi Kivity5312bd82012-02-12 18:32:55 +0200763 uint16_t section);
Jan Kiszkaacc9d802013-05-26 21:55:37 +0200764static subpage_t *subpage_init(AddressSpace *as, hwaddr base);
Avi Kivity5312bd82012-02-12 18:32:55 +0200765static void destroy_page_desc(uint16_t section_index)
Avi Kivity54688b12012-02-09 17:34:32 +0200766{
Avi Kivity5312bd82012-02-12 18:32:55 +0200767 MemoryRegionSection *section = &phys_sections[section_index];
768 MemoryRegion *mr = section->mr;
Avi Kivity54688b12012-02-09 17:34:32 +0200769
770 if (mr->subpage) {
771 subpage_t *subpage = container_of(mr, subpage_t, iomem);
772 memory_region_destroy(&subpage->iomem);
773 g_free(subpage);
774 }
775}
776
Avi Kivity4346ae32012-02-10 17:00:01 +0200777static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level)
Avi Kivity54688b12012-02-09 17:34:32 +0200778{
779 unsigned i;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200780 PhysPageEntry *p;
Avi Kivity54688b12012-02-09 17:34:32 +0200781
Avi Kivityc19e8802012-02-13 20:25:31 +0200782 if (lp->ptr == PHYS_MAP_NODE_NIL) {
Avi Kivity54688b12012-02-09 17:34:32 +0200783 return;
784 }
785
Avi Kivityc19e8802012-02-13 20:25:31 +0200786 p = phys_map_nodes[lp->ptr];
Avi Kivity4346ae32012-02-10 17:00:01 +0200787 for (i = 0; i < L2_SIZE; ++i) {
Avi Kivity07f07b32012-02-13 20:45:32 +0200788 if (!p[i].is_leaf) {
Avi Kivity54688b12012-02-09 17:34:32 +0200789 destroy_l2_mapping(&p[i], level - 1);
Avi Kivity4346ae32012-02-10 17:00:01 +0200790 } else {
Avi Kivityc19e8802012-02-13 20:25:31 +0200791 destroy_page_desc(p[i].ptr);
Avi Kivity54688b12012-02-09 17:34:32 +0200792 }
Avi Kivity54688b12012-02-09 17:34:32 +0200793 }
Avi Kivity07f07b32012-02-13 20:45:32 +0200794 lp->is_leaf = 0;
Avi Kivityc19e8802012-02-13 20:25:31 +0200795 lp->ptr = PHYS_MAP_NODE_NIL;
Avi Kivity54688b12012-02-09 17:34:32 +0200796}
797
Avi Kivityac1970f2012-10-03 16:22:53 +0200798static void destroy_all_mappings(AddressSpaceDispatch *d)
Avi Kivity54688b12012-02-09 17:34:32 +0200799{
Avi Kivityac1970f2012-10-03 16:22:53 +0200800 destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200801 phys_map_nodes_reset();
Avi Kivity54688b12012-02-09 17:34:32 +0200802}
803
Avi Kivity5312bd82012-02-12 18:32:55 +0200804static uint16_t phys_section_add(MemoryRegionSection *section)
805{
Paolo Bonzini68f3f652013-05-07 11:30:23 +0200806 /* The physical section number is ORed with a page-aligned
807 * pointer to produce the iotlb entries. Thus it should
808 * never overflow into the page-aligned value.
809 */
810 assert(phys_sections_nb < TARGET_PAGE_SIZE);
811
Avi Kivity5312bd82012-02-12 18:32:55 +0200812 if (phys_sections_nb == phys_sections_nb_alloc) {
813 phys_sections_nb_alloc = MAX(phys_sections_nb_alloc * 2, 16);
814 phys_sections = g_renew(MemoryRegionSection, phys_sections,
815 phys_sections_nb_alloc);
816 }
817 phys_sections[phys_sections_nb] = *section;
818 return phys_sections_nb++;
819}
820
821static void phys_sections_clear(void)
822{
823 phys_sections_nb = 0;
824}
825
Avi Kivityac1970f2012-10-03 16:22:53 +0200826static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
Avi Kivity0f0cb162012-02-13 17:14:32 +0200827{
828 subpage_t *subpage;
Avi Kivitya8170e52012-10-23 12:30:10 +0200829 hwaddr base = section->offset_within_address_space
Avi Kivity0f0cb162012-02-13 17:14:32 +0200830 & TARGET_PAGE_MASK;
Avi Kivityac1970f2012-10-03 16:22:53 +0200831 MemoryRegionSection *existing = phys_page_find(d, base >> TARGET_PAGE_BITS);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200832 MemoryRegionSection subsection = {
833 .offset_within_address_space = base,
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200834 .size = int128_make64(TARGET_PAGE_SIZE),
Avi Kivity0f0cb162012-02-13 17:14:32 +0200835 };
Avi Kivitya8170e52012-10-23 12:30:10 +0200836 hwaddr start, end;
Avi Kivity0f0cb162012-02-13 17:14:32 +0200837
Avi Kivityf3705d52012-03-08 16:16:34 +0200838 assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200839
Avi Kivityf3705d52012-03-08 16:16:34 +0200840 if (!(existing->mr->subpage)) {
Jan Kiszkaacc9d802013-05-26 21:55:37 +0200841 subpage = subpage_init(d->as, base);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200842 subsection.mr = &subpage->iomem;
Avi Kivityac1970f2012-10-03 16:22:53 +0200843 phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
Avi Kivity29990972012-02-13 20:21:20 +0200844 phys_section_add(&subsection));
Avi Kivity0f0cb162012-02-13 17:14:32 +0200845 } else {
Avi Kivityf3705d52012-03-08 16:16:34 +0200846 subpage = container_of(existing->mr, subpage_t, iomem);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200847 }
848 start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200849 end = start + int128_get64(section->size) - 1;
Avi Kivity0f0cb162012-02-13 17:14:32 +0200850 subpage_register(subpage, start, end, phys_section_add(section));
851}
852
853
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200854static void register_multipage(AddressSpaceDispatch *d,
855 MemoryRegionSection *section)
bellard33417e72003-08-10 21:47:01 +0000856{
Avi Kivitya8170e52012-10-23 12:30:10 +0200857 hwaddr start_addr = section->offset_within_address_space;
Avi Kivity5312bd82012-02-12 18:32:55 +0200858 uint16_t section_index = phys_section_add(section);
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200859 uint64_t num_pages = int128_get64(int128_rshift(section->size,
860 TARGET_PAGE_BITS));
Avi Kivitydd811242012-01-02 12:17:03 +0200861
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200862 assert(num_pages);
863 phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);
bellard33417e72003-08-10 21:47:01 +0000864}
865
Avi Kivityac1970f2012-10-03 16:22:53 +0200866static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
Avi Kivity0f0cb162012-02-13 17:14:32 +0200867{
Avi Kivityac1970f2012-10-03 16:22:53 +0200868 AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
Paolo Bonzini99b9cc02013-05-27 13:18:01 +0200869 MemoryRegionSection now = *section, remain = *section;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200870 Int128 page_size = int128_make64(TARGET_PAGE_SIZE);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200871
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200872 if (now.offset_within_address_space & ~TARGET_PAGE_MASK) {
873 uint64_t left = TARGET_PAGE_ALIGN(now.offset_within_address_space)
874 - now.offset_within_address_space;
875
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200876 now.size = int128_min(int128_make64(left), now.size);
Avi Kivityac1970f2012-10-03 16:22:53 +0200877 register_subpage(d, &now);
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200878 } else {
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200879 now.size = int128_zero();
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200880 }
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200881 while (int128_ne(remain.size, now.size)) {
882 remain.size = int128_sub(remain.size, now.size);
883 remain.offset_within_address_space += int128_get64(now.size);
884 remain.offset_within_region += int128_get64(now.size);
Tyler Hall69b67642012-07-25 18:45:04 -0400885 now = remain;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200886 if (int128_lt(remain.size, page_size)) {
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200887 register_subpage(d, &now);
888 } else if (remain.offset_within_region & ~TARGET_PAGE_MASK) {
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200889 now.size = page_size;
Avi Kivityac1970f2012-10-03 16:22:53 +0200890 register_subpage(d, &now);
Tyler Hall69b67642012-07-25 18:45:04 -0400891 } else {
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200892 now.size = int128_and(now.size, int128_neg(page_size));
Avi Kivityac1970f2012-10-03 16:22:53 +0200893 register_multipage(d, &now);
Tyler Hall69b67642012-07-25 18:45:04 -0400894 }
Avi Kivity0f0cb162012-02-13 17:14:32 +0200895 }
896}
897
Sheng Yang62a27442010-01-26 19:21:16 +0800898void qemu_flush_coalesced_mmio_buffer(void)
899{
900 if (kvm_enabled())
901 kvm_flush_coalesced_mmio_buffer();
902}
903
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700904void qemu_mutex_lock_ramlist(void)
905{
906 qemu_mutex_lock(&ram_list.mutex);
907}
908
909void qemu_mutex_unlock_ramlist(void)
910{
911 qemu_mutex_unlock(&ram_list.mutex);
912}
913
Marcelo Tosattic9027602010-03-01 20:25:08 -0300914#if defined(__linux__) && !defined(TARGET_S390X)
915
916#include <sys/vfs.h>
917
918#define HUGETLBFS_MAGIC 0x958458f6
919
920static long gethugepagesize(const char *path)
921{
922 struct statfs fs;
923 int ret;
924
925 do {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900926 ret = statfs(path, &fs);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300927 } while (ret != 0 && errno == EINTR);
928
929 if (ret != 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900930 perror(path);
931 return 0;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300932 }
933
934 if (fs.f_type != HUGETLBFS_MAGIC)
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900935 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300936
937 return fs.f_bsize;
938}
939
Alex Williamson04b16652010-07-02 11:13:17 -0600940static void *file_ram_alloc(RAMBlock *block,
941 ram_addr_t memory,
942 const char *path)
Marcelo Tosattic9027602010-03-01 20:25:08 -0300943{
944 char *filename;
Peter Feiner8ca761f2013-03-04 13:54:25 -0500945 char *sanitized_name;
946 char *c;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300947 void *area;
948 int fd;
949#ifdef MAP_POPULATE
950 int flags;
951#endif
952 unsigned long hpagesize;
953
954 hpagesize = gethugepagesize(path);
955 if (!hpagesize) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900956 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300957 }
958
959 if (memory < hpagesize) {
960 return NULL;
961 }
962
963 if (kvm_enabled() && !kvm_has_sync_mmu()) {
964 fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
965 return NULL;
966 }
967
Peter Feiner8ca761f2013-03-04 13:54:25 -0500968 /* Make name safe to use with mkstemp by replacing '/' with '_'. */
969 sanitized_name = g_strdup(block->mr->name);
970 for (c = sanitized_name; *c != '\0'; c++) {
971 if (*c == '/')
972 *c = '_';
973 }
974
975 filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
976 sanitized_name);
977 g_free(sanitized_name);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300978
979 fd = mkstemp(filename);
980 if (fd < 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900981 perror("unable to create backing store for hugepages");
Stefan Weile4ada482013-01-16 18:37:23 +0100982 g_free(filename);
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900983 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300984 }
985 unlink(filename);
Stefan Weile4ada482013-01-16 18:37:23 +0100986 g_free(filename);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300987
988 memory = (memory+hpagesize-1) & ~(hpagesize-1);
989
990 /*
991 * ftruncate is not supported by hugetlbfs in older
992 * hosts, so don't bother bailing out on errors.
993 * If anything goes wrong with it under other filesystems,
994 * mmap will fail.
995 */
996 if (ftruncate(fd, memory))
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900997 perror("ftruncate");
Marcelo Tosattic9027602010-03-01 20:25:08 -0300998
999#ifdef MAP_POPULATE
1000 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
1001 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
1002 * to sidestep this quirk.
1003 */
1004 flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
1005 area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
1006#else
1007 area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
1008#endif
1009 if (area == MAP_FAILED) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001010 perror("file_ram_alloc: can't mmap RAM pages");
1011 close(fd);
1012 return (NULL);
Marcelo Tosattic9027602010-03-01 20:25:08 -03001013 }
Alex Williamson04b16652010-07-02 11:13:17 -06001014 block->fd = fd;
Marcelo Tosattic9027602010-03-01 20:25:08 -03001015 return area;
1016}
1017#endif
1018
Alex Williamsond17b5282010-06-25 11:08:38 -06001019static ram_addr_t find_ram_offset(ram_addr_t size)
1020{
Alex Williamson04b16652010-07-02 11:13:17 -06001021 RAMBlock *block, *next_block;
Alex Williamson3e837b22011-10-31 08:54:09 -06001022 ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06001023
Stefan Hajnoczi49cd9ac2013-03-11 10:20:21 +01001024 assert(size != 0); /* it would hand out same offset multiple times */
1025
Paolo Bonzinia3161032012-11-14 15:54:48 +01001026 if (QTAILQ_EMPTY(&ram_list.blocks))
Alex Williamson04b16652010-07-02 11:13:17 -06001027 return 0;
1028
Paolo Bonzinia3161032012-11-14 15:54:48 +01001029 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00001030 ram_addr_t end, next = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06001031
1032 end = block->offset + block->length;
1033
Paolo Bonzinia3161032012-11-14 15:54:48 +01001034 QTAILQ_FOREACH(next_block, &ram_list.blocks, next) {
Alex Williamson04b16652010-07-02 11:13:17 -06001035 if (next_block->offset >= end) {
1036 next = MIN(next, next_block->offset);
1037 }
1038 }
1039 if (next - end >= size && next - end < mingap) {
Alex Williamson3e837b22011-10-31 08:54:09 -06001040 offset = end;
Alex Williamson04b16652010-07-02 11:13:17 -06001041 mingap = next - end;
1042 }
1043 }
Alex Williamson3e837b22011-10-31 08:54:09 -06001044
1045 if (offset == RAM_ADDR_MAX) {
1046 fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
1047 (uint64_t)size);
1048 abort();
1049 }
1050
Alex Williamson04b16652010-07-02 11:13:17 -06001051 return offset;
1052}
1053
Juan Quintela652d7ec2012-07-20 10:37:54 +02001054ram_addr_t last_ram_offset(void)
Alex Williamson04b16652010-07-02 11:13:17 -06001055{
Alex Williamsond17b5282010-06-25 11:08:38 -06001056 RAMBlock *block;
1057 ram_addr_t last = 0;
1058
Paolo Bonzinia3161032012-11-14 15:54:48 +01001059 QTAILQ_FOREACH(block, &ram_list.blocks, next)
Alex Williamsond17b5282010-06-25 11:08:38 -06001060 last = MAX(last, block->offset + block->length);
1061
1062 return last;
1063}
1064
Jason Baronddb97f12012-08-02 15:44:16 -04001065static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
1066{
1067 int ret;
1068 QemuOpts *machine_opts;
1069
1070 /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
1071 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
1072 if (machine_opts &&
1073 !qemu_opt_get_bool(machine_opts, "dump-guest-core", true)) {
1074 ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
1075 if (ret) {
1076 perror("qemu_madvise");
1077 fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
1078 "but dump_guest_core=off specified\n");
1079 }
1080 }
1081}
1082
Avi Kivityc5705a72011-12-20 15:59:12 +02001083void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
Cam Macdonell84b89d72010-07-26 18:10:57 -06001084{
1085 RAMBlock *new_block, *block;
1086
Avi Kivityc5705a72011-12-20 15:59:12 +02001087 new_block = NULL;
Paolo Bonzinia3161032012-11-14 15:54:48 +01001088 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Avi Kivityc5705a72011-12-20 15:59:12 +02001089 if (block->offset == addr) {
1090 new_block = block;
1091 break;
1092 }
1093 }
1094 assert(new_block);
1095 assert(!new_block->idstr[0]);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001096
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001097 if (dev) {
1098 char *id = qdev_get_dev_path(dev);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001099 if (id) {
1100 snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
Anthony Liguori7267c092011-08-20 22:09:37 -05001101 g_free(id);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001102 }
1103 }
1104 pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
1105
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001106 /* This assumes the iothread lock is taken here too. */
1107 qemu_mutex_lock_ramlist();
Paolo Bonzinia3161032012-11-14 15:54:48 +01001108 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Avi Kivityc5705a72011-12-20 15:59:12 +02001109 if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
Cam Macdonell84b89d72010-07-26 18:10:57 -06001110 fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
1111 new_block->idstr);
1112 abort();
1113 }
1114 }
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001115 qemu_mutex_unlock_ramlist();
Avi Kivityc5705a72011-12-20 15:59:12 +02001116}
1117
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001118static int memory_try_enable_merging(void *addr, size_t len)
1119{
1120 QemuOpts *opts;
1121
1122 opts = qemu_opts_find(qemu_find_opts("machine"), 0);
1123 if (opts && !qemu_opt_get_bool(opts, "mem-merge", true)) {
1124 /* disabled by the user */
1125 return 0;
1126 }
1127
1128 return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
1129}
1130
Avi Kivityc5705a72011-12-20 15:59:12 +02001131ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
1132 MemoryRegion *mr)
1133{
Paolo Bonziniabb26d62012-11-14 16:00:51 +01001134 RAMBlock *block, *new_block;
Avi Kivityc5705a72011-12-20 15:59:12 +02001135
1136 size = TARGET_PAGE_ALIGN(size);
1137 new_block = g_malloc0(sizeof(*new_block));
Cam Macdonell84b89d72010-07-26 18:10:57 -06001138
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001139 /* This assumes the iothread lock is taken here too. */
1140 qemu_mutex_lock_ramlist();
Avi Kivity7c637362011-12-21 13:09:49 +02001141 new_block->mr = mr;
Jun Nakajima432d2682010-08-31 16:41:25 +01001142 new_block->offset = find_ram_offset(size);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001143 if (host) {
1144 new_block->host = host;
Huang Yingcd19cfa2011-03-02 08:56:19 +01001145 new_block->flags |= RAM_PREALLOC_MASK;
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001146 } else {
1147 if (mem_path) {
1148#if defined (__linux__) && !defined(TARGET_S390X)
1149 new_block->host = file_ram_alloc(new_block, size, mem_path);
1150 if (!new_block->host) {
Paolo Bonzini6eebf952013-05-13 16:19:55 +02001151 new_block->host = qemu_anon_ram_alloc(size);
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001152 memory_try_enable_merging(new_block->host, size);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001153 }
1154#else
1155 fprintf(stderr, "-mem-path option unsupported\n");
1156 exit(1);
1157#endif
1158 } else {
Jan Kiszka868bb332011-06-21 22:59:09 +02001159 if (xen_enabled()) {
Avi Kivityfce537d2011-12-18 15:48:55 +02001160 xen_ram_alloc(new_block->offset, size, mr);
Christian Borntraegerfdec9912012-06-15 05:10:30 +00001161 } else if (kvm_enabled()) {
1162 /* some s390/kvm configurations have special constraints */
Paolo Bonzini6eebf952013-05-13 16:19:55 +02001163 new_block->host = kvm_ram_alloc(size);
Jun Nakajima432d2682010-08-31 16:41:25 +01001164 } else {
Paolo Bonzini6eebf952013-05-13 16:19:55 +02001165 new_block->host = qemu_anon_ram_alloc(size);
Jun Nakajima432d2682010-08-31 16:41:25 +01001166 }
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001167 memory_try_enable_merging(new_block->host, size);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001168 }
1169 }
Cam Macdonell84b89d72010-07-26 18:10:57 -06001170 new_block->length = size;
1171
Paolo Bonziniabb26d62012-11-14 16:00:51 +01001172 /* Keep the list sorted from biggest to smallest block. */
1173 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1174 if (block->length < new_block->length) {
1175 break;
1176 }
1177 }
1178 if (block) {
1179 QTAILQ_INSERT_BEFORE(block, new_block, next);
1180 } else {
1181 QTAILQ_INSERT_TAIL(&ram_list.blocks, new_block, next);
1182 }
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001183 ram_list.mru_block = NULL;
Cam Macdonell84b89d72010-07-26 18:10:57 -06001184
Umesh Deshpandef798b072011-08-18 11:41:17 -07001185 ram_list.version++;
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001186 qemu_mutex_unlock_ramlist();
Umesh Deshpandef798b072011-08-18 11:41:17 -07001187
Anthony Liguori7267c092011-08-20 22:09:37 -05001188 ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
Cam Macdonell84b89d72010-07-26 18:10:57 -06001189 last_ram_offset() >> TARGET_PAGE_BITS);
Igor Mitsyanko5fda0432012-08-10 18:45:11 +04001190 memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
1191 0, size >> TARGET_PAGE_BITS);
Juan Quintela1720aee2012-06-22 13:14:17 +02001192 cpu_physical_memory_set_dirty_range(new_block->offset, size, 0xff);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001193
Jason Baronddb97f12012-08-02 15:44:16 -04001194 qemu_ram_setup_dump(new_block->host, size);
Luiz Capitulinoad0b5322012-10-05 16:47:57 -03001195 qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
Jason Baronddb97f12012-08-02 15:44:16 -04001196
Cam Macdonell84b89d72010-07-26 18:10:57 -06001197 if (kvm_enabled())
1198 kvm_setup_guest_memory(new_block->host, size);
1199
1200 return new_block->offset;
1201}
1202
Avi Kivityc5705a72011-12-20 15:59:12 +02001203ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr)
pbrook94a6b542009-04-11 17:15:54 +00001204{
Avi Kivityc5705a72011-12-20 15:59:12 +02001205 return qemu_ram_alloc_from_ptr(size, NULL, mr);
pbrook94a6b542009-04-11 17:15:54 +00001206}
bellarde9a1ab12007-02-08 23:08:38 +00001207
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001208void qemu_ram_free_from_ptr(ram_addr_t addr)
1209{
1210 RAMBlock *block;
1211
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001212 /* This assumes the iothread lock is taken here too. */
1213 qemu_mutex_lock_ramlist();
Paolo Bonzinia3161032012-11-14 15:54:48 +01001214 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001215 if (addr == block->offset) {
Paolo Bonzinia3161032012-11-14 15:54:48 +01001216 QTAILQ_REMOVE(&ram_list.blocks, block, next);
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001217 ram_list.mru_block = NULL;
Umesh Deshpandef798b072011-08-18 11:41:17 -07001218 ram_list.version++;
Anthony Liguori7267c092011-08-20 22:09:37 -05001219 g_free(block);
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001220 break;
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001221 }
1222 }
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001223 qemu_mutex_unlock_ramlist();
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001224}
1225
Anthony Liguoric227f092009-10-01 16:12:16 -05001226void qemu_ram_free(ram_addr_t addr)
bellarde9a1ab12007-02-08 23:08:38 +00001227{
Alex Williamson04b16652010-07-02 11:13:17 -06001228 RAMBlock *block;
1229
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001230 /* This assumes the iothread lock is taken here too. */
1231 qemu_mutex_lock_ramlist();
Paolo Bonzinia3161032012-11-14 15:54:48 +01001232 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamson04b16652010-07-02 11:13:17 -06001233 if (addr == block->offset) {
Paolo Bonzinia3161032012-11-14 15:54:48 +01001234 QTAILQ_REMOVE(&ram_list.blocks, block, next);
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001235 ram_list.mru_block = NULL;
Umesh Deshpandef798b072011-08-18 11:41:17 -07001236 ram_list.version++;
Huang Yingcd19cfa2011-03-02 08:56:19 +01001237 if (block->flags & RAM_PREALLOC_MASK) {
1238 ;
1239 } else if (mem_path) {
Alex Williamson04b16652010-07-02 11:13:17 -06001240#if defined (__linux__) && !defined(TARGET_S390X)
1241 if (block->fd) {
1242 munmap(block->host, block->length);
1243 close(block->fd);
1244 } else {
Paolo Bonzinie7a09b92013-05-13 16:19:56 +02001245 qemu_anon_ram_free(block->host, block->length);
Alex Williamson04b16652010-07-02 11:13:17 -06001246 }
Jan Kiszkafd28aa12011-03-15 12:26:14 +01001247#else
1248 abort();
Alex Williamson04b16652010-07-02 11:13:17 -06001249#endif
1250 } else {
Jan Kiszka868bb332011-06-21 22:59:09 +02001251 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001252 xen_invalidate_map_cache_entry(block->host);
Jun Nakajima432d2682010-08-31 16:41:25 +01001253 } else {
Paolo Bonzinie7a09b92013-05-13 16:19:56 +02001254 qemu_anon_ram_free(block->host, block->length);
Jun Nakajima432d2682010-08-31 16:41:25 +01001255 }
Alex Williamson04b16652010-07-02 11:13:17 -06001256 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001257 g_free(block);
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001258 break;
Alex Williamson04b16652010-07-02 11:13:17 -06001259 }
1260 }
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001261 qemu_mutex_unlock_ramlist();
Alex Williamson04b16652010-07-02 11:13:17 -06001262
bellarde9a1ab12007-02-08 23:08:38 +00001263}
1264
Huang Yingcd19cfa2011-03-02 08:56:19 +01001265#ifndef _WIN32
1266void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
1267{
1268 RAMBlock *block;
1269 ram_addr_t offset;
1270 int flags;
1271 void *area, *vaddr;
1272
Paolo Bonzinia3161032012-11-14 15:54:48 +01001273 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Huang Yingcd19cfa2011-03-02 08:56:19 +01001274 offset = addr - block->offset;
1275 if (offset < block->length) {
1276 vaddr = block->host + offset;
1277 if (block->flags & RAM_PREALLOC_MASK) {
1278 ;
1279 } else {
1280 flags = MAP_FIXED;
1281 munmap(vaddr, length);
1282 if (mem_path) {
1283#if defined(__linux__) && !defined(TARGET_S390X)
1284 if (block->fd) {
1285#ifdef MAP_POPULATE
1286 flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
1287 MAP_PRIVATE;
1288#else
1289 flags |= MAP_PRIVATE;
1290#endif
1291 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1292 flags, block->fd, offset);
1293 } else {
1294 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
1295 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1296 flags, -1, 0);
1297 }
Jan Kiszkafd28aa12011-03-15 12:26:14 +01001298#else
1299 abort();
Huang Yingcd19cfa2011-03-02 08:56:19 +01001300#endif
1301 } else {
1302#if defined(TARGET_S390X) && defined(CONFIG_KVM)
1303 flags |= MAP_SHARED | MAP_ANONYMOUS;
1304 area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE,
1305 flags, -1, 0);
1306#else
1307 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
1308 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1309 flags, -1, 0);
1310#endif
1311 }
1312 if (area != vaddr) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00001313 fprintf(stderr, "Could not remap addr: "
1314 RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
Huang Yingcd19cfa2011-03-02 08:56:19 +01001315 length, addr);
1316 exit(1);
1317 }
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001318 memory_try_enable_merging(vaddr, length);
Jason Baronddb97f12012-08-02 15:44:16 -04001319 qemu_ram_setup_dump(vaddr, length);
Huang Yingcd19cfa2011-03-02 08:56:19 +01001320 }
1321 return;
1322 }
1323 }
1324}
1325#endif /* !_WIN32 */
1326
pbrookdc828ca2009-04-09 22:21:07 +00001327/* Return a host pointer to ram allocated with qemu_ram_alloc.
pbrook5579c7f2009-04-11 14:47:08 +00001328 With the exception of the softmmu code in this file, this should
1329 only be used for local memory (e.g. video ram) that the device owns,
1330 and knows it isn't going to access beyond the end of the block.
1331
1332 It should not be used for general purpose DMA.
1333 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
1334 */
Anthony Liguoric227f092009-10-01 16:12:16 -05001335void *qemu_get_ram_ptr(ram_addr_t addr)
pbrookdc828ca2009-04-09 22:21:07 +00001336{
pbrook94a6b542009-04-11 17:15:54 +00001337 RAMBlock *block;
1338
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001339 /* The list is protected by the iothread lock here. */
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001340 block = ram_list.mru_block;
1341 if (block && addr - block->offset < block->length) {
1342 goto found;
1343 }
Paolo Bonzinia3161032012-11-14 15:54:48 +01001344 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamsonf471a172010-06-11 11:11:42 -06001345 if (addr - block->offset < block->length) {
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001346 goto found;
Alex Williamsonf471a172010-06-11 11:11:42 -06001347 }
pbrook94a6b542009-04-11 17:15:54 +00001348 }
Alex Williamsonf471a172010-06-11 11:11:42 -06001349
1350 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1351 abort();
1352
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001353found:
1354 ram_list.mru_block = block;
1355 if (xen_enabled()) {
1356 /* We need to check if the requested address is in the RAM
1357 * because we don't want to map the entire memory in QEMU.
1358 * In that case just map until the end of the page.
1359 */
1360 if (block->offset == 0) {
1361 return xen_map_cache(addr, 0, 0);
1362 } else if (block->host == NULL) {
1363 block->host =
1364 xen_map_cache(block->offset, block->length, 1);
1365 }
1366 }
1367 return block->host + (addr - block->offset);
pbrookdc828ca2009-04-09 22:21:07 +00001368}
1369
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001370/* Return a host pointer to ram allocated with qemu_ram_alloc. Same as
1371 * qemu_get_ram_ptr but do not touch ram_list.mru_block.
1372 *
1373 * ??? Is this still necessary?
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02001374 */
Blue Swirl8b9c99d2012-10-28 11:04:51 +00001375static void *qemu_safe_ram_ptr(ram_addr_t addr)
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02001376{
1377 RAMBlock *block;
1378
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001379 /* The list is protected by the iothread lock here. */
Paolo Bonzinia3161032012-11-14 15:54:48 +01001380 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02001381 if (addr - block->offset < block->length) {
Jan Kiszka868bb332011-06-21 22:59:09 +02001382 if (xen_enabled()) {
Jun Nakajima432d2682010-08-31 16:41:25 +01001383 /* We need to check if the requested address is in the RAM
1384 * because we don't want to map the entire memory in QEMU.
Stefano Stabellini712c2b42011-05-19 18:35:46 +01001385 * In that case just map until the end of the page.
Jun Nakajima432d2682010-08-31 16:41:25 +01001386 */
1387 if (block->offset == 0) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001388 return xen_map_cache(addr, 0, 0);
Jun Nakajima432d2682010-08-31 16:41:25 +01001389 } else if (block->host == NULL) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001390 block->host =
1391 xen_map_cache(block->offset, block->length, 1);
Jun Nakajima432d2682010-08-31 16:41:25 +01001392 }
1393 }
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02001394 return block->host + (addr - block->offset);
1395 }
1396 }
1397
1398 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1399 abort();
1400
1401 return NULL;
1402}
1403
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001404/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
1405 * but takes a size argument */
Blue Swirl8b9c99d2012-10-28 11:04:51 +00001406static void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001407{
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01001408 if (*size == 0) {
1409 return NULL;
1410 }
Jan Kiszka868bb332011-06-21 22:59:09 +02001411 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001412 return xen_map_cache(addr, *size, 1);
Jan Kiszka868bb332011-06-21 22:59:09 +02001413 } else {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001414 RAMBlock *block;
1415
Paolo Bonzinia3161032012-11-14 15:54:48 +01001416 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001417 if (addr - block->offset < block->length) {
1418 if (addr - block->offset + *size > block->length)
1419 *size = block->length - addr + block->offset;
1420 return block->host + (addr - block->offset);
1421 }
1422 }
1423
1424 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1425 abort();
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001426 }
1427}
1428
Marcelo Tosattie8902612010-10-11 15:31:19 -03001429int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
pbrook5579c7f2009-04-11 14:47:08 +00001430{
pbrook94a6b542009-04-11 17:15:54 +00001431 RAMBlock *block;
1432 uint8_t *host = ptr;
1433
Jan Kiszka868bb332011-06-21 22:59:09 +02001434 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001435 *ram_addr = xen_ram_addr_from_mapcache(ptr);
Stefano Stabellini712c2b42011-05-19 18:35:46 +01001436 return 0;
1437 }
1438
Paolo Bonzinia3161032012-11-14 15:54:48 +01001439 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Jun Nakajima432d2682010-08-31 16:41:25 +01001440 /* This case append when the block is not mapped. */
1441 if (block->host == NULL) {
1442 continue;
1443 }
Alex Williamsonf471a172010-06-11 11:11:42 -06001444 if (host - block->host < block->length) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03001445 *ram_addr = block->offset + (host - block->host);
1446 return 0;
Alex Williamsonf471a172010-06-11 11:11:42 -06001447 }
pbrook94a6b542009-04-11 17:15:54 +00001448 }
Jun Nakajima432d2682010-08-31 16:41:25 +01001449
Marcelo Tosattie8902612010-10-11 15:31:19 -03001450 return -1;
1451}
Alex Williamsonf471a172010-06-11 11:11:42 -06001452
Marcelo Tosattie8902612010-10-11 15:31:19 -03001453/* Some of the softmmu routines need to translate from a host pointer
1454 (typically a TLB entry) back to a ram offset. */
1455ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
1456{
1457 ram_addr_t ram_addr;
Alex Williamsonf471a172010-06-11 11:11:42 -06001458
Marcelo Tosattie8902612010-10-11 15:31:19 -03001459 if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
1460 fprintf(stderr, "Bad ram pointer %p\n", ptr);
1461 abort();
1462 }
1463 return ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00001464}
1465
Avi Kivitya8170e52012-10-23 12:30:10 +02001466static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001467 uint64_t val, unsigned size)
bellard1ccde1c2004-02-06 19:46:14 +00001468{
bellard3a7d9292005-08-21 09:26:42 +00001469 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09001470 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00001471 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001472 tb_invalidate_phys_page_fast(ram_addr, size);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09001473 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00001474 }
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001475 switch (size) {
1476 case 1:
1477 stb_p(qemu_get_ram_ptr(ram_addr), val);
1478 break;
1479 case 2:
1480 stw_p(qemu_get_ram_ptr(ram_addr), val);
1481 break;
1482 case 4:
1483 stl_p(qemu_get_ram_ptr(ram_addr), val);
1484 break;
1485 default:
1486 abort();
1487 }
bellardf23db162005-08-21 19:12:28 +00001488 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09001489 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00001490 /* we remove the notdirty callback only if the code has been
1491 flushed */
1492 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00001493 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00001494}
1495
Paolo Bonzinib018ddf2013-05-24 14:48:38 +02001496static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
1497 unsigned size, bool is_write)
1498{
1499 return is_write;
1500}
1501
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001502static const MemoryRegionOps notdirty_mem_ops = {
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001503 .write = notdirty_mem_write,
Paolo Bonzinib018ddf2013-05-24 14:48:38 +02001504 .valid.accepts = notdirty_mem_accepts,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001505 .endianness = DEVICE_NATIVE_ENDIAN,
bellard1ccde1c2004-02-06 19:46:14 +00001506};
1507
pbrook0f459d12008-06-09 00:20:13 +00001508/* Generate a debug exception if a watchpoint has been hit. */
aliguorib4051332008-11-18 20:14:20 +00001509static void check_watchpoint(int offset, int len_mask, int flags)
pbrook0f459d12008-06-09 00:20:13 +00001510{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001511 CPUArchState *env = cpu_single_env;
aliguori06d55cc2008-11-18 20:24:06 +00001512 target_ulong pc, cs_base;
pbrook0f459d12008-06-09 00:20:13 +00001513 target_ulong vaddr;
aliguoria1d1bb32008-11-18 20:07:32 +00001514 CPUWatchpoint *wp;
aliguori06d55cc2008-11-18 20:24:06 +00001515 int cpu_flags;
pbrook0f459d12008-06-09 00:20:13 +00001516
aliguori06d55cc2008-11-18 20:24:06 +00001517 if (env->watchpoint_hit) {
1518 /* We re-entered the check after replacing the TB. Now raise
1519 * the debug interrupt so that is will trigger after the
1520 * current instruction. */
Andreas Färberc3affe52013-01-18 15:03:43 +01001521 cpu_interrupt(ENV_GET_CPU(env), CPU_INTERRUPT_DEBUG);
aliguori06d55cc2008-11-18 20:24:06 +00001522 return;
1523 }
pbrook2e70f6e2008-06-29 01:03:05 +00001524 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001525 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00001526 if ((vaddr == (wp->vaddr & len_mask) ||
1527 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
aliguori6e140f22008-11-18 20:37:55 +00001528 wp->flags |= BP_WATCHPOINT_HIT;
1529 if (!env->watchpoint_hit) {
1530 env->watchpoint_hit = wp;
Blue Swirl5a316522012-12-02 21:28:09 +00001531 tb_check_watchpoint(env);
aliguori6e140f22008-11-18 20:37:55 +00001532 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
1533 env->exception_index = EXCP_DEBUG;
Max Filippov488d6572012-01-29 02:24:39 +04001534 cpu_loop_exit(env);
aliguori6e140f22008-11-18 20:37:55 +00001535 } else {
1536 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
1537 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
Max Filippov488d6572012-01-29 02:24:39 +04001538 cpu_resume_from_signal(env, NULL);
aliguori6e140f22008-11-18 20:37:55 +00001539 }
aliguori06d55cc2008-11-18 20:24:06 +00001540 }
aliguori6e140f22008-11-18 20:37:55 +00001541 } else {
1542 wp->flags &= ~BP_WATCHPOINT_HIT;
pbrook0f459d12008-06-09 00:20:13 +00001543 }
1544 }
1545}
1546
pbrook6658ffb2007-03-16 23:58:11 +00001547/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
1548 so these check for a hit then pass through to the normal out-of-line
1549 phys routines. */
Avi Kivitya8170e52012-10-23 12:30:10 +02001550static uint64_t watch_mem_read(void *opaque, hwaddr addr,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001551 unsigned size)
pbrook6658ffb2007-03-16 23:58:11 +00001552{
Avi Kivity1ec9b902012-01-02 12:47:48 +02001553 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_READ);
1554 switch (size) {
1555 case 1: return ldub_phys(addr);
1556 case 2: return lduw_phys(addr);
1557 case 4: return ldl_phys(addr);
1558 default: abort();
1559 }
pbrook6658ffb2007-03-16 23:58:11 +00001560}
1561
Avi Kivitya8170e52012-10-23 12:30:10 +02001562static void watch_mem_write(void *opaque, hwaddr addr,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001563 uint64_t val, unsigned size)
pbrook6658ffb2007-03-16 23:58:11 +00001564{
Avi Kivity1ec9b902012-01-02 12:47:48 +02001565 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE);
1566 switch (size) {
Max Filippov67364152012-01-29 00:01:40 +04001567 case 1:
1568 stb_phys(addr, val);
1569 break;
1570 case 2:
1571 stw_phys(addr, val);
1572 break;
1573 case 4:
1574 stl_phys(addr, val);
1575 break;
Avi Kivity1ec9b902012-01-02 12:47:48 +02001576 default: abort();
1577 }
pbrook6658ffb2007-03-16 23:58:11 +00001578}
1579
Avi Kivity1ec9b902012-01-02 12:47:48 +02001580static const MemoryRegionOps watch_mem_ops = {
1581 .read = watch_mem_read,
1582 .write = watch_mem_write,
1583 .endianness = DEVICE_NATIVE_ENDIAN,
pbrook6658ffb2007-03-16 23:58:11 +00001584};
pbrook6658ffb2007-03-16 23:58:11 +00001585
Avi Kivitya8170e52012-10-23 12:30:10 +02001586static uint64_t subpage_read(void *opaque, hwaddr addr,
Avi Kivity70c68e42012-01-02 12:32:48 +02001587 unsigned len)
blueswir1db7b5422007-05-26 17:36:03 +00001588{
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001589 subpage_t *subpage = opaque;
1590 uint8_t buf[4];
Paolo Bonzini791af8c2013-05-24 16:10:39 +02001591
blueswir1db7b5422007-05-26 17:36:03 +00001592#if defined(DEBUG_SUBPAGE)
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001593 printf("%s: subpage %p len %d addr " TARGET_FMT_plx "\n", __func__,
1594 subpage, len, addr);
blueswir1db7b5422007-05-26 17:36:03 +00001595#endif
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001596 address_space_read(subpage->as, addr + subpage->base, buf, len);
1597 switch (len) {
1598 case 1:
1599 return ldub_p(buf);
1600 case 2:
1601 return lduw_p(buf);
1602 case 4:
1603 return ldl_p(buf);
1604 default:
1605 abort();
1606 }
blueswir1db7b5422007-05-26 17:36:03 +00001607}
1608
Avi Kivitya8170e52012-10-23 12:30:10 +02001609static void subpage_write(void *opaque, hwaddr addr,
Avi Kivity70c68e42012-01-02 12:32:48 +02001610 uint64_t value, unsigned len)
blueswir1db7b5422007-05-26 17:36:03 +00001611{
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001612 subpage_t *subpage = opaque;
1613 uint8_t buf[4];
1614
blueswir1db7b5422007-05-26 17:36:03 +00001615#if defined(DEBUG_SUBPAGE)
Avi Kivity70c68e42012-01-02 12:32:48 +02001616 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001617 " value %"PRIx64"\n",
1618 __func__, subpage, len, addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00001619#endif
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001620 switch (len) {
1621 case 1:
1622 stb_p(buf, value);
1623 break;
1624 case 2:
1625 stw_p(buf, value);
1626 break;
1627 case 4:
1628 stl_p(buf, value);
1629 break;
1630 default:
1631 abort();
1632 }
1633 address_space_write(subpage->as, addr + subpage->base, buf, len);
blueswir1db7b5422007-05-26 17:36:03 +00001634}
1635
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001636static bool subpage_accepts(void *opaque, hwaddr addr,
1637 unsigned size, bool is_write)
1638{
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001639 subpage_t *subpage = opaque;
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001640#if defined(DEBUG_SUBPAGE)
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001641 printf("%s: subpage %p %c len %d addr " TARGET_FMT_plx "\n",
1642 __func__, subpage, is_write ? 'w' : 'r', len, addr);
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001643#endif
1644
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001645 return address_space_access_valid(subpage->as, addr + subpage->base,
1646 size, is_write);
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001647}
1648
Avi Kivity70c68e42012-01-02 12:32:48 +02001649static const MemoryRegionOps subpage_ops = {
1650 .read = subpage_read,
1651 .write = subpage_write,
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001652 .valid.accepts = subpage_accepts,
Avi Kivity70c68e42012-01-02 12:32:48 +02001653 .endianness = DEVICE_NATIVE_ENDIAN,
blueswir1db7b5422007-05-26 17:36:03 +00001654};
1655
Anthony Liguoric227f092009-10-01 16:12:16 -05001656static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
Avi Kivity5312bd82012-02-12 18:32:55 +02001657 uint16_t section)
blueswir1db7b5422007-05-26 17:36:03 +00001658{
1659 int idx, eidx;
1660
1661 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
1662 return -1;
1663 idx = SUBPAGE_IDX(start);
1664 eidx = SUBPAGE_IDX(end);
1665#if defined(DEBUG_SUBPAGE)
Blue Swirl0bf9e312009-07-20 17:19:25 +00001666 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
blueswir1db7b5422007-05-26 17:36:03 +00001667 mmio, start, end, idx, eidx, memory);
1668#endif
blueswir1db7b5422007-05-26 17:36:03 +00001669 for (; idx <= eidx; idx++) {
Avi Kivity5312bd82012-02-12 18:32:55 +02001670 mmio->sub_section[idx] = section;
blueswir1db7b5422007-05-26 17:36:03 +00001671 }
1672
1673 return 0;
1674}
1675
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001676static subpage_t *subpage_init(AddressSpace *as, hwaddr base)
blueswir1db7b5422007-05-26 17:36:03 +00001677{
Anthony Liguoric227f092009-10-01 16:12:16 -05001678 subpage_t *mmio;
blueswir1db7b5422007-05-26 17:36:03 +00001679
Anthony Liguori7267c092011-08-20 22:09:37 -05001680 mmio = g_malloc0(sizeof(subpage_t));
aliguori1eec6142009-02-05 22:06:18 +00001681
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001682 mmio->as = as;
aliguori1eec6142009-02-05 22:06:18 +00001683 mmio->base = base;
Avi Kivity70c68e42012-01-02 12:32:48 +02001684 memory_region_init_io(&mmio->iomem, &subpage_ops, mmio,
1685 "subpage", TARGET_PAGE_SIZE);
Avi Kivityb3b00c72012-01-02 13:20:11 +02001686 mmio->iomem.subpage = true;
blueswir1db7b5422007-05-26 17:36:03 +00001687#if defined(DEBUG_SUBPAGE)
aliguori1eec6142009-02-05 22:06:18 +00001688 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
1689 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
blueswir1db7b5422007-05-26 17:36:03 +00001690#endif
Avi Kivity0f0cb162012-02-13 17:14:32 +02001691 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, phys_section_unassigned);
blueswir1db7b5422007-05-26 17:36:03 +00001692
1693 return mmio;
1694}
1695
Avi Kivity5312bd82012-02-12 18:32:55 +02001696static uint16_t dummy_section(MemoryRegion *mr)
1697{
1698 MemoryRegionSection section = {
1699 .mr = mr,
1700 .offset_within_address_space = 0,
1701 .offset_within_region = 0,
Paolo Bonzini052e87b2013-05-27 10:08:27 +02001702 .size = int128_2_64(),
Avi Kivity5312bd82012-02-12 18:32:55 +02001703 };
1704
1705 return phys_section_add(&section);
1706}
1707
Avi Kivitya8170e52012-10-23 12:30:10 +02001708MemoryRegion *iotlb_to_region(hwaddr index)
Avi Kivityaa102232012-03-08 17:06:55 +02001709{
Avi Kivity37ec01d2012-03-08 18:08:35 +02001710 return phys_sections[index & ~TARGET_PAGE_MASK].mr;
Avi Kivityaa102232012-03-08 17:06:55 +02001711}
1712
Avi Kivitye9179ce2009-06-14 11:38:52 +03001713static void io_mem_init(void)
1714{
Paolo Bonzinibf8d5162013-05-24 14:39:13 +02001715 memory_region_init_io(&io_mem_rom, &unassigned_mem_ops, NULL, "rom", UINT64_MAX);
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001716 memory_region_init_io(&io_mem_unassigned, &unassigned_mem_ops, NULL,
1717 "unassigned", UINT64_MAX);
1718 memory_region_init_io(&io_mem_notdirty, &notdirty_mem_ops, NULL,
1719 "notdirty", UINT64_MAX);
Avi Kivity1ec9b902012-01-02 12:47:48 +02001720 memory_region_init_io(&io_mem_watch, &watch_mem_ops, NULL,
1721 "watch", UINT64_MAX);
Avi Kivitye9179ce2009-06-14 11:38:52 +03001722}
1723
Avi Kivityac1970f2012-10-03 16:22:53 +02001724static void mem_begin(MemoryListener *listener)
1725{
1726 AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
1727
1728 destroy_all_mappings(d);
1729 d->phys_map.ptr = PHYS_MAP_NODE_NIL;
1730}
1731
Avi Kivity50c1e142012-02-08 21:36:02 +02001732static void core_begin(MemoryListener *listener)
1733{
Avi Kivity5312bd82012-02-12 18:32:55 +02001734 phys_sections_clear();
1735 phys_section_unassigned = dummy_section(&io_mem_unassigned);
Avi Kivityaa102232012-03-08 17:06:55 +02001736 phys_section_notdirty = dummy_section(&io_mem_notdirty);
1737 phys_section_rom = dummy_section(&io_mem_rom);
1738 phys_section_watch = dummy_section(&io_mem_watch);
Avi Kivity50c1e142012-02-08 21:36:02 +02001739}
1740
Avi Kivity1d711482012-10-02 18:54:45 +02001741static void tcg_commit(MemoryListener *listener)
Avi Kivity50c1e142012-02-08 21:36:02 +02001742{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001743 CPUArchState *env;
Avi Kivity117712c2012-02-12 21:23:17 +02001744
1745 /* since each CPU stores ram addresses in its TLB cache, we must
1746 reset the modified entries */
1747 /* XXX: slow ! */
1748 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1749 tlb_flush(env, 1);
1750 }
Avi Kivity50c1e142012-02-08 21:36:02 +02001751}
1752
Avi Kivity93632742012-02-08 16:54:16 +02001753static void core_log_global_start(MemoryListener *listener)
1754{
1755 cpu_physical_memory_set_dirty_tracking(1);
1756}
1757
1758static void core_log_global_stop(MemoryListener *listener)
1759{
1760 cpu_physical_memory_set_dirty_tracking(0);
1761}
1762
Avi Kivity4855d412012-02-08 21:16:05 +02001763static void io_region_add(MemoryListener *listener,
1764 MemoryRegionSection *section)
1765{
Avi Kivitya2d33522012-03-05 17:40:12 +02001766 MemoryRegionIORange *mrio = g_new(MemoryRegionIORange, 1);
1767
1768 mrio->mr = section->mr;
1769 mrio->offset = section->offset_within_region;
1770 iorange_init(&mrio->iorange, &memory_region_iorange_ops,
Paolo Bonzini052e87b2013-05-27 10:08:27 +02001771 section->offset_within_address_space,
1772 int128_get64(section->size));
Avi Kivitya2d33522012-03-05 17:40:12 +02001773 ioport_register(&mrio->iorange);
Avi Kivity4855d412012-02-08 21:16:05 +02001774}
1775
1776static void io_region_del(MemoryListener *listener,
1777 MemoryRegionSection *section)
1778{
Paolo Bonzini052e87b2013-05-27 10:08:27 +02001779 isa_unassign_ioport(section->offset_within_address_space,
1780 int128_get64(section->size));
Avi Kivity4855d412012-02-08 21:16:05 +02001781}
1782
Avi Kivity93632742012-02-08 16:54:16 +02001783static MemoryListener core_memory_listener = {
Avi Kivity50c1e142012-02-08 21:36:02 +02001784 .begin = core_begin,
Avi Kivity93632742012-02-08 16:54:16 +02001785 .log_global_start = core_log_global_start,
1786 .log_global_stop = core_log_global_stop,
Avi Kivityac1970f2012-10-03 16:22:53 +02001787 .priority = 1,
Avi Kivity93632742012-02-08 16:54:16 +02001788};
1789
Avi Kivity4855d412012-02-08 21:16:05 +02001790static MemoryListener io_memory_listener = {
1791 .region_add = io_region_add,
1792 .region_del = io_region_del,
Avi Kivity4855d412012-02-08 21:16:05 +02001793 .priority = 0,
1794};
1795
Avi Kivity1d711482012-10-02 18:54:45 +02001796static MemoryListener tcg_memory_listener = {
1797 .commit = tcg_commit,
1798};
1799
Avi Kivityac1970f2012-10-03 16:22:53 +02001800void address_space_init_dispatch(AddressSpace *as)
1801{
1802 AddressSpaceDispatch *d = g_new(AddressSpaceDispatch, 1);
1803
1804 d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .is_leaf = 0 };
1805 d->listener = (MemoryListener) {
1806 .begin = mem_begin,
1807 .region_add = mem_add,
1808 .region_nop = mem_add,
1809 .priority = 0,
1810 };
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001811 d->as = as;
Avi Kivityac1970f2012-10-03 16:22:53 +02001812 as->dispatch = d;
1813 memory_listener_register(&d->listener, as);
1814}
1815
Avi Kivity83f3c252012-10-07 12:59:55 +02001816void address_space_destroy_dispatch(AddressSpace *as)
1817{
1818 AddressSpaceDispatch *d = as->dispatch;
1819
1820 memory_listener_unregister(&d->listener);
1821 destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
1822 g_free(d);
1823 as->dispatch = NULL;
1824}
1825
Avi Kivity62152b82011-07-26 14:26:14 +03001826static void memory_map_init(void)
1827{
Anthony Liguori7267c092011-08-20 22:09:37 -05001828 system_memory = g_malloc(sizeof(*system_memory));
Avi Kivity8417ceb2011-08-03 11:56:14 +03001829 memory_region_init(system_memory, "system", INT64_MAX);
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001830 address_space_init(&address_space_memory, system_memory, "memory");
Avi Kivity309cb472011-08-08 16:09:03 +03001831
Anthony Liguori7267c092011-08-20 22:09:37 -05001832 system_io = g_malloc(sizeof(*system_io));
Avi Kivity309cb472011-08-08 16:09:03 +03001833 memory_region_init(system_io, "io", 65536);
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001834 address_space_init(&address_space_io, system_io, "I/O");
Avi Kivity93632742012-02-08 16:54:16 +02001835
Avi Kivityf6790af2012-10-02 20:13:51 +02001836 memory_listener_register(&core_memory_listener, &address_space_memory);
1837 memory_listener_register(&io_memory_listener, &address_space_io);
1838 memory_listener_register(&tcg_memory_listener, &address_space_memory);
Avi Kivity62152b82011-07-26 14:26:14 +03001839}
1840
1841MemoryRegion *get_system_memory(void)
1842{
1843 return system_memory;
1844}
1845
Avi Kivity309cb472011-08-08 16:09:03 +03001846MemoryRegion *get_system_io(void)
1847{
1848 return system_io;
1849}
1850
pbrooke2eef172008-06-08 01:09:01 +00001851#endif /* !defined(CONFIG_USER_ONLY) */
1852
bellard13eb76e2004-01-24 15:23:36 +00001853/* physical memory access (slow version, mainly for debug) */
1854#if defined(CONFIG_USER_ONLY)
Andreas Färber9349b4f2012-03-14 01:38:32 +01001855int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
Paul Brooka68fe892010-03-01 00:08:59 +00001856 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00001857{
1858 int l, flags;
1859 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00001860 void * p;
bellard13eb76e2004-01-24 15:23:36 +00001861
1862 while (len > 0) {
1863 page = addr & TARGET_PAGE_MASK;
1864 l = (page + TARGET_PAGE_SIZE) - addr;
1865 if (l > len)
1866 l = len;
1867 flags = page_get_flags(page);
1868 if (!(flags & PAGE_VALID))
Paul Brooka68fe892010-03-01 00:08:59 +00001869 return -1;
bellard13eb76e2004-01-24 15:23:36 +00001870 if (is_write) {
1871 if (!(flags & PAGE_WRITE))
Paul Brooka68fe892010-03-01 00:08:59 +00001872 return -1;
bellard579a97f2007-11-11 14:26:47 +00001873 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00001874 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
Paul Brooka68fe892010-03-01 00:08:59 +00001875 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00001876 memcpy(p, buf, l);
1877 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00001878 } else {
1879 if (!(flags & PAGE_READ))
Paul Brooka68fe892010-03-01 00:08:59 +00001880 return -1;
bellard579a97f2007-11-11 14:26:47 +00001881 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00001882 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
Paul Brooka68fe892010-03-01 00:08:59 +00001883 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00001884 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00001885 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00001886 }
1887 len -= l;
1888 buf += l;
1889 addr += l;
1890 }
Paul Brooka68fe892010-03-01 00:08:59 +00001891 return 0;
bellard13eb76e2004-01-24 15:23:36 +00001892}
bellard8df1cd02005-01-28 22:37:22 +00001893
bellard13eb76e2004-01-24 15:23:36 +00001894#else
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001895
Avi Kivitya8170e52012-10-23 12:30:10 +02001896static void invalidate_and_set_dirty(hwaddr addr,
1897 hwaddr length)
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001898{
1899 if (!cpu_physical_memory_is_dirty(addr)) {
1900 /* invalidate code */
1901 tb_invalidate_phys_page_range(addr, addr + length, 0);
1902 /* set dirty bit */
1903 cpu_physical_memory_set_dirty_flags(addr, (0xff & ~CODE_DIRTY_FLAG));
1904 }
Anthony PERARDe2269392012-10-03 13:49:22 +00001905 xen_modified_memory(addr, length);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001906}
1907
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02001908static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1909{
1910 if (memory_region_is_ram(mr)) {
1911 return !(is_write && mr->readonly);
1912 }
1913 if (memory_region_is_romd(mr)) {
1914 return !is_write;
1915 }
1916
1917 return false;
1918}
1919
Jan Kiszkaf52cc462013-05-26 21:42:40 +02001920static inline int memory_access_size(MemoryRegion *mr, int l, hwaddr addr)
Paolo Bonzini82f25632013-05-24 11:59:43 +02001921{
Jan Kiszkaf52cc462013-05-26 21:42:40 +02001922 if (l >= 4 && (((addr & 3) == 0 || mr->ops->impl.unaligned))) {
Paolo Bonzini82f25632013-05-24 11:59:43 +02001923 return 4;
1924 }
Jan Kiszkaf52cc462013-05-26 21:42:40 +02001925 if (l >= 2 && (((addr & 1) == 0) || mr->ops->impl.unaligned)) {
Paolo Bonzini82f25632013-05-24 11:59:43 +02001926 return 2;
1927 }
1928 return 1;
1929}
1930
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001931bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
Avi Kivityac1970f2012-10-03 16:22:53 +02001932 int len, bool is_write)
bellard13eb76e2004-01-24 15:23:36 +00001933{
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001934 hwaddr l;
bellard13eb76e2004-01-24 15:23:36 +00001935 uint8_t *ptr;
Paolo Bonzini791af8c2013-05-24 16:10:39 +02001936 uint64_t val;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001937 hwaddr addr1;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001938 MemoryRegion *mr;
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001939 bool error = false;
ths3b46e622007-09-17 08:09:54 +00001940
bellard13eb76e2004-01-24 15:23:36 +00001941 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001942 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001943 mr = address_space_translate(as, addr, &addr1, &l, is_write);
ths3b46e622007-09-17 08:09:54 +00001944
bellard13eb76e2004-01-24 15:23:36 +00001945 if (is_write) {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001946 if (!memory_access_is_direct(mr, is_write)) {
1947 l = memory_access_size(mr, l, addr1);
bellard6a00d602005-11-21 23:25:50 +00001948 /* XXX: could force cpu_single_env to NULL to avoid
1949 potential bugs */
Paolo Bonzini82f25632013-05-24 11:59:43 +02001950 if (l == 4) {
bellard1c213d12005-09-03 10:49:04 +00001951 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001952 val = ldl_p(buf);
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001953 error |= io_mem_write(mr, addr1, val, 4);
Paolo Bonzini82f25632013-05-24 11:59:43 +02001954 } else if (l == 2) {
bellard1c213d12005-09-03 10:49:04 +00001955 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001956 val = lduw_p(buf);
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001957 error |= io_mem_write(mr, addr1, val, 2);
bellard13eb76e2004-01-24 15:23:36 +00001958 } else {
bellard1c213d12005-09-03 10:49:04 +00001959 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001960 val = ldub_p(buf);
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001961 error |= io_mem_write(mr, addr1, val, 1);
bellard13eb76e2004-01-24 15:23:36 +00001962 }
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02001963 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001964 addr1 += memory_region_get_ram_addr(mr);
bellard13eb76e2004-01-24 15:23:36 +00001965 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00001966 ptr = qemu_get_ram_ptr(addr1);
bellard13eb76e2004-01-24 15:23:36 +00001967 memcpy(ptr, buf, l);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001968 invalidate_and_set_dirty(addr1, l);
bellard13eb76e2004-01-24 15:23:36 +00001969 }
1970 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001971 if (!memory_access_is_direct(mr, is_write)) {
bellard13eb76e2004-01-24 15:23:36 +00001972 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001973 l = memory_access_size(mr, l, addr1);
Paolo Bonzini82f25632013-05-24 11:59:43 +02001974 if (l == 4) {
bellard13eb76e2004-01-24 15:23:36 +00001975 /* 32 bit read access */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001976 error |= io_mem_read(mr, addr1, &val, 4);
bellardc27004e2005-01-03 23:35:10 +00001977 stl_p(buf, val);
Paolo Bonzini82f25632013-05-24 11:59:43 +02001978 } else if (l == 2) {
bellard13eb76e2004-01-24 15:23:36 +00001979 /* 16 bit read access */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001980 error |= io_mem_read(mr, addr1, &val, 2);
bellardc27004e2005-01-03 23:35:10 +00001981 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00001982 } else {
bellard1c213d12005-09-03 10:49:04 +00001983 /* 8 bit read access */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001984 error |= io_mem_read(mr, addr1, &val, 1);
bellardc27004e2005-01-03 23:35:10 +00001985 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00001986 }
1987 } else {
1988 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001989 ptr = qemu_get_ram_ptr(mr->ram_addr + addr1);
Avi Kivityf3705d52012-03-08 16:16:34 +02001990 memcpy(buf, ptr, l);
bellard13eb76e2004-01-24 15:23:36 +00001991 }
1992 }
1993 len -= l;
1994 buf += l;
1995 addr += l;
1996 }
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001997
1998 return error;
bellard13eb76e2004-01-24 15:23:36 +00001999}
bellard8df1cd02005-01-28 22:37:22 +00002000
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002001bool address_space_write(AddressSpace *as, hwaddr addr,
Avi Kivityac1970f2012-10-03 16:22:53 +02002002 const uint8_t *buf, int len)
2003{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002004 return address_space_rw(as, addr, (uint8_t *)buf, len, true);
Avi Kivityac1970f2012-10-03 16:22:53 +02002005}
2006
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002007bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
Avi Kivityac1970f2012-10-03 16:22:53 +02002008{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002009 return address_space_rw(as, addr, buf, len, false);
Avi Kivityac1970f2012-10-03 16:22:53 +02002010}
2011
2012
Avi Kivitya8170e52012-10-23 12:30:10 +02002013void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
Avi Kivityac1970f2012-10-03 16:22:53 +02002014 int len, int is_write)
2015{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002016 address_space_rw(&address_space_memory, addr, buf, len, is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02002017}
2018
bellardd0ecd2a2006-04-23 17:14:48 +00002019/* used for ROM loading : can write in RAM and ROM */
Avi Kivitya8170e52012-10-23 12:30:10 +02002020void cpu_physical_memory_write_rom(hwaddr addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002021 const uint8_t *buf, int len)
2022{
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002023 hwaddr l;
bellardd0ecd2a2006-04-23 17:14:48 +00002024 uint8_t *ptr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002025 hwaddr addr1;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002026 MemoryRegion *mr;
ths3b46e622007-09-17 08:09:54 +00002027
bellardd0ecd2a2006-04-23 17:14:48 +00002028 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002029 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002030 mr = address_space_translate(&address_space_memory,
2031 addr, &addr1, &l, true);
ths3b46e622007-09-17 08:09:54 +00002032
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002033 if (!(memory_region_is_ram(mr) ||
2034 memory_region_is_romd(mr))) {
bellardd0ecd2a2006-04-23 17:14:48 +00002035 /* do nothing */
2036 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002037 addr1 += memory_region_get_ram_addr(mr);
bellardd0ecd2a2006-04-23 17:14:48 +00002038 /* ROM/RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00002039 ptr = qemu_get_ram_ptr(addr1);
bellardd0ecd2a2006-04-23 17:14:48 +00002040 memcpy(ptr, buf, l);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002041 invalidate_and_set_dirty(addr1, l);
bellardd0ecd2a2006-04-23 17:14:48 +00002042 }
2043 len -= l;
2044 buf += l;
2045 addr += l;
2046 }
2047}
2048
aliguori6d16c2f2009-01-22 16:59:11 +00002049typedef struct {
2050 void *buffer;
Avi Kivitya8170e52012-10-23 12:30:10 +02002051 hwaddr addr;
2052 hwaddr len;
aliguori6d16c2f2009-01-22 16:59:11 +00002053} BounceBuffer;
2054
2055static BounceBuffer bounce;
2056
aliguoriba223c22009-01-22 16:59:16 +00002057typedef struct MapClient {
2058 void *opaque;
2059 void (*callback)(void *opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002060 QLIST_ENTRY(MapClient) link;
aliguoriba223c22009-01-22 16:59:16 +00002061} MapClient;
2062
Blue Swirl72cf2d42009-09-12 07:36:22 +00002063static QLIST_HEAD(map_client_list, MapClient) map_client_list
2064 = QLIST_HEAD_INITIALIZER(map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00002065
2066void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
2067{
Anthony Liguori7267c092011-08-20 22:09:37 -05002068 MapClient *client = g_malloc(sizeof(*client));
aliguoriba223c22009-01-22 16:59:16 +00002069
2070 client->opaque = opaque;
2071 client->callback = callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002072 QLIST_INSERT_HEAD(&map_client_list, client, link);
aliguoriba223c22009-01-22 16:59:16 +00002073 return client;
2074}
2075
Blue Swirl8b9c99d2012-10-28 11:04:51 +00002076static void cpu_unregister_map_client(void *_client)
aliguoriba223c22009-01-22 16:59:16 +00002077{
2078 MapClient *client = (MapClient *)_client;
2079
Blue Swirl72cf2d42009-09-12 07:36:22 +00002080 QLIST_REMOVE(client, link);
Anthony Liguori7267c092011-08-20 22:09:37 -05002081 g_free(client);
aliguoriba223c22009-01-22 16:59:16 +00002082}
2083
2084static void cpu_notify_map_clients(void)
2085{
2086 MapClient *client;
2087
Blue Swirl72cf2d42009-09-12 07:36:22 +00002088 while (!QLIST_EMPTY(&map_client_list)) {
2089 client = QLIST_FIRST(&map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00002090 client->callback(client->opaque);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09002091 cpu_unregister_map_client(client);
aliguoriba223c22009-01-22 16:59:16 +00002092 }
2093}
2094
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002095bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
2096{
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002097 MemoryRegion *mr;
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002098 hwaddr l, xlat;
2099
2100 while (len > 0) {
2101 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002102 mr = address_space_translate(as, addr, &xlat, &l, is_write);
2103 if (!memory_access_is_direct(mr, is_write)) {
2104 l = memory_access_size(mr, l, addr);
2105 if (!memory_region_access_valid(mr, xlat, l, is_write)) {
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002106 return false;
2107 }
2108 }
2109
2110 len -= l;
2111 addr += l;
2112 }
2113 return true;
2114}
2115
aliguori6d16c2f2009-01-22 16:59:11 +00002116/* Map a physical memory region into a host virtual address.
2117 * May map a subset of the requested range, given by and returned in *plen.
2118 * May return NULL if resources needed to perform the mapping are exhausted.
2119 * Use only for reads OR writes - not for read-modify-write operations.
aliguoriba223c22009-01-22 16:59:16 +00002120 * Use cpu_register_map_client() to know when retrying the map operation is
2121 * likely to succeed.
aliguori6d16c2f2009-01-22 16:59:11 +00002122 */
Avi Kivityac1970f2012-10-03 16:22:53 +02002123void *address_space_map(AddressSpace *as,
Avi Kivitya8170e52012-10-23 12:30:10 +02002124 hwaddr addr,
2125 hwaddr *plen,
Avi Kivityac1970f2012-10-03 16:22:53 +02002126 bool is_write)
aliguori6d16c2f2009-01-22 16:59:11 +00002127{
Avi Kivitya8170e52012-10-23 12:30:10 +02002128 hwaddr len = *plen;
2129 hwaddr todo = 0;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002130 hwaddr l, xlat;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002131 MemoryRegion *mr;
Anthony PERARDf15fbc42011-07-20 08:17:42 +00002132 ram_addr_t raddr = RAM_ADDR_MAX;
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002133 ram_addr_t rlen;
2134 void *ret;
aliguori6d16c2f2009-01-22 16:59:11 +00002135
2136 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002137 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002138 mr = address_space_translate(as, addr, &xlat, &l, is_write);
aliguori6d16c2f2009-01-22 16:59:11 +00002139
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002140 if (!memory_access_is_direct(mr, is_write)) {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01002141 if (todo || bounce.buffer) {
aliguori6d16c2f2009-01-22 16:59:11 +00002142 break;
2143 }
2144 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
2145 bounce.addr = addr;
2146 bounce.len = l;
2147 if (!is_write) {
Avi Kivityac1970f2012-10-03 16:22:53 +02002148 address_space_read(as, addr, bounce.buffer, l);
aliguori6d16c2f2009-01-22 16:59:11 +00002149 }
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01002150
2151 *plen = l;
2152 return bounce.buffer;
aliguori6d16c2f2009-01-22 16:59:11 +00002153 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002154 if (!todo) {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002155 raddr = memory_region_get_ram_addr(mr) + xlat;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002156 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002157 if (memory_region_get_ram_addr(mr) + xlat != raddr + todo) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002158 break;
2159 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002160 }
aliguori6d16c2f2009-01-22 16:59:11 +00002161
2162 len -= l;
2163 addr += l;
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01002164 todo += l;
aliguori6d16c2f2009-01-22 16:59:11 +00002165 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002166 rlen = todo;
2167 ret = qemu_ram_ptr_length(raddr, &rlen);
2168 *plen = rlen;
2169 return ret;
aliguori6d16c2f2009-01-22 16:59:11 +00002170}
2171
Avi Kivityac1970f2012-10-03 16:22:53 +02002172/* Unmaps a memory region previously mapped by address_space_map().
aliguori6d16c2f2009-01-22 16:59:11 +00002173 * Will also mark the memory as dirty if is_write == 1. access_len gives
2174 * the amount of memory that was actually read or written by the caller.
2175 */
Avi Kivitya8170e52012-10-23 12:30:10 +02002176void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2177 int is_write, hwaddr access_len)
aliguori6d16c2f2009-01-22 16:59:11 +00002178{
2179 if (buffer != bounce.buffer) {
2180 if (is_write) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03002181 ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00002182 while (access_len) {
2183 unsigned l;
2184 l = TARGET_PAGE_SIZE;
2185 if (l > access_len)
2186 l = access_len;
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002187 invalidate_and_set_dirty(addr1, l);
aliguori6d16c2f2009-01-22 16:59:11 +00002188 addr1 += l;
2189 access_len -= l;
2190 }
2191 }
Jan Kiszka868bb332011-06-21 22:59:09 +02002192 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02002193 xen_invalidate_map_cache_entry(buffer);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01002194 }
aliguori6d16c2f2009-01-22 16:59:11 +00002195 return;
2196 }
2197 if (is_write) {
Avi Kivityac1970f2012-10-03 16:22:53 +02002198 address_space_write(as, bounce.addr, bounce.buffer, access_len);
aliguori6d16c2f2009-01-22 16:59:11 +00002199 }
Herve Poussineauf8a83242010-01-24 21:23:56 +00002200 qemu_vfree(bounce.buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00002201 bounce.buffer = NULL;
aliguoriba223c22009-01-22 16:59:16 +00002202 cpu_notify_map_clients();
aliguori6d16c2f2009-01-22 16:59:11 +00002203}
bellardd0ecd2a2006-04-23 17:14:48 +00002204
Avi Kivitya8170e52012-10-23 12:30:10 +02002205void *cpu_physical_memory_map(hwaddr addr,
2206 hwaddr *plen,
Avi Kivityac1970f2012-10-03 16:22:53 +02002207 int is_write)
2208{
2209 return address_space_map(&address_space_memory, addr, plen, is_write);
2210}
2211
Avi Kivitya8170e52012-10-23 12:30:10 +02002212void cpu_physical_memory_unmap(void *buffer, hwaddr len,
2213 int is_write, hwaddr access_len)
Avi Kivityac1970f2012-10-03 16:22:53 +02002214{
2215 return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
2216}
2217
bellard8df1cd02005-01-28 22:37:22 +00002218/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002219static inline uint32_t ldl_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002220 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00002221{
bellard8df1cd02005-01-28 22:37:22 +00002222 uint8_t *ptr;
Paolo Bonzini791af8c2013-05-24 16:10:39 +02002223 uint64_t val;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002224 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002225 hwaddr l = 4;
2226 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002227
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002228 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2229 false);
2230 if (l < 4 || !memory_access_is_direct(mr, false)) {
bellard8df1cd02005-01-28 22:37:22 +00002231 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002232 io_mem_read(mr, addr1, &val, 4);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002233#if defined(TARGET_WORDS_BIGENDIAN)
2234 if (endian == DEVICE_LITTLE_ENDIAN) {
2235 val = bswap32(val);
2236 }
2237#else
2238 if (endian == DEVICE_BIG_ENDIAN) {
2239 val = bswap32(val);
2240 }
2241#endif
bellard8df1cd02005-01-28 22:37:22 +00002242 } else {
2243 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002244 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002245 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002246 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002247 switch (endian) {
2248 case DEVICE_LITTLE_ENDIAN:
2249 val = ldl_le_p(ptr);
2250 break;
2251 case DEVICE_BIG_ENDIAN:
2252 val = ldl_be_p(ptr);
2253 break;
2254 default:
2255 val = ldl_p(ptr);
2256 break;
2257 }
bellard8df1cd02005-01-28 22:37:22 +00002258 }
2259 return val;
2260}
2261
Avi Kivitya8170e52012-10-23 12:30:10 +02002262uint32_t ldl_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002263{
2264 return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2265}
2266
Avi Kivitya8170e52012-10-23 12:30:10 +02002267uint32_t ldl_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002268{
2269 return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2270}
2271
Avi Kivitya8170e52012-10-23 12:30:10 +02002272uint32_t ldl_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002273{
2274 return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
2275}
2276
bellard84b7b8e2005-11-28 21:19:04 +00002277/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002278static inline uint64_t ldq_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002279 enum device_endian endian)
bellard84b7b8e2005-11-28 21:19:04 +00002280{
bellard84b7b8e2005-11-28 21:19:04 +00002281 uint8_t *ptr;
2282 uint64_t val;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002283 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002284 hwaddr l = 8;
2285 hwaddr addr1;
bellard84b7b8e2005-11-28 21:19:04 +00002286
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002287 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2288 false);
2289 if (l < 8 || !memory_access_is_direct(mr, false)) {
bellard84b7b8e2005-11-28 21:19:04 +00002290 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002291 io_mem_read(mr, addr1, &val, 8);
Paolo Bonzini968a5622013-05-24 17:58:37 +02002292#if defined(TARGET_WORDS_BIGENDIAN)
2293 if (endian == DEVICE_LITTLE_ENDIAN) {
2294 val = bswap64(val);
2295 }
2296#else
2297 if (endian == DEVICE_BIG_ENDIAN) {
2298 val = bswap64(val);
2299 }
2300#endif
bellard84b7b8e2005-11-28 21:19:04 +00002301 } else {
2302 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002303 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002304 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002305 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002306 switch (endian) {
2307 case DEVICE_LITTLE_ENDIAN:
2308 val = ldq_le_p(ptr);
2309 break;
2310 case DEVICE_BIG_ENDIAN:
2311 val = ldq_be_p(ptr);
2312 break;
2313 default:
2314 val = ldq_p(ptr);
2315 break;
2316 }
bellard84b7b8e2005-11-28 21:19:04 +00002317 }
2318 return val;
2319}
2320
Avi Kivitya8170e52012-10-23 12:30:10 +02002321uint64_t ldq_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002322{
2323 return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2324}
2325
Avi Kivitya8170e52012-10-23 12:30:10 +02002326uint64_t ldq_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002327{
2328 return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2329}
2330
Avi Kivitya8170e52012-10-23 12:30:10 +02002331uint64_t ldq_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002332{
2333 return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
2334}
2335
bellardaab33092005-10-30 20:48:42 +00002336/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002337uint32_t ldub_phys(hwaddr addr)
bellardaab33092005-10-30 20:48:42 +00002338{
2339 uint8_t val;
2340 cpu_physical_memory_read(addr, &val, 1);
2341 return val;
2342}
2343
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002344/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002345static inline uint32_t lduw_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002346 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00002347{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002348 uint8_t *ptr;
2349 uint64_t val;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002350 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002351 hwaddr l = 2;
2352 hwaddr addr1;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002353
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002354 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2355 false);
2356 if (l < 2 || !memory_access_is_direct(mr, false)) {
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002357 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002358 io_mem_read(mr, addr1, &val, 2);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002359#if defined(TARGET_WORDS_BIGENDIAN)
2360 if (endian == DEVICE_LITTLE_ENDIAN) {
2361 val = bswap16(val);
2362 }
2363#else
2364 if (endian == DEVICE_BIG_ENDIAN) {
2365 val = bswap16(val);
2366 }
2367#endif
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002368 } else {
2369 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002370 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002371 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002372 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002373 switch (endian) {
2374 case DEVICE_LITTLE_ENDIAN:
2375 val = lduw_le_p(ptr);
2376 break;
2377 case DEVICE_BIG_ENDIAN:
2378 val = lduw_be_p(ptr);
2379 break;
2380 default:
2381 val = lduw_p(ptr);
2382 break;
2383 }
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002384 }
2385 return val;
bellardaab33092005-10-30 20:48:42 +00002386}
2387
Avi Kivitya8170e52012-10-23 12:30:10 +02002388uint32_t lduw_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002389{
2390 return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2391}
2392
Avi Kivitya8170e52012-10-23 12:30:10 +02002393uint32_t lduw_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002394{
2395 return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2396}
2397
Avi Kivitya8170e52012-10-23 12:30:10 +02002398uint32_t lduw_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002399{
2400 return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
2401}
2402
bellard8df1cd02005-01-28 22:37:22 +00002403/* warning: addr must be aligned. The ram page is not masked as dirty
2404 and the code inside is not invalidated. It is useful if the dirty
2405 bits are used to track modified PTEs */
Avi Kivitya8170e52012-10-23 12:30:10 +02002406void stl_phys_notdirty(hwaddr addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00002407{
bellard8df1cd02005-01-28 22:37:22 +00002408 uint8_t *ptr;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002409 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002410 hwaddr l = 4;
2411 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002412
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002413 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2414 true);
2415 if (l < 4 || !memory_access_is_direct(mr, true)) {
2416 io_mem_write(mr, addr1, val, 4);
bellard8df1cd02005-01-28 22:37:22 +00002417 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002418 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
pbrook5579c7f2009-04-11 14:47:08 +00002419 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00002420 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00002421
2422 if (unlikely(in_migration)) {
2423 if (!cpu_physical_memory_is_dirty(addr1)) {
2424 /* invalidate code */
2425 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2426 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002427 cpu_physical_memory_set_dirty_flags(
2428 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori74576192008-10-06 14:02:03 +00002429 }
2430 }
bellard8df1cd02005-01-28 22:37:22 +00002431 }
2432}
2433
2434/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002435static inline void stl_phys_internal(hwaddr addr, uint32_t val,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002436 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00002437{
bellard8df1cd02005-01-28 22:37:22 +00002438 uint8_t *ptr;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002439 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002440 hwaddr l = 4;
2441 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002442
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002443 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2444 true);
2445 if (l < 4 || !memory_access_is_direct(mr, true)) {
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002446#if defined(TARGET_WORDS_BIGENDIAN)
2447 if (endian == DEVICE_LITTLE_ENDIAN) {
2448 val = bswap32(val);
2449 }
2450#else
2451 if (endian == DEVICE_BIG_ENDIAN) {
2452 val = bswap32(val);
2453 }
2454#endif
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002455 io_mem_write(mr, addr1, val, 4);
bellard8df1cd02005-01-28 22:37:22 +00002456 } else {
bellard8df1cd02005-01-28 22:37:22 +00002457 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002458 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
pbrook5579c7f2009-04-11 14:47:08 +00002459 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002460 switch (endian) {
2461 case DEVICE_LITTLE_ENDIAN:
2462 stl_le_p(ptr, val);
2463 break;
2464 case DEVICE_BIG_ENDIAN:
2465 stl_be_p(ptr, val);
2466 break;
2467 default:
2468 stl_p(ptr, val);
2469 break;
2470 }
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002471 invalidate_and_set_dirty(addr1, 4);
bellard8df1cd02005-01-28 22:37:22 +00002472 }
2473}
2474
Avi Kivitya8170e52012-10-23 12:30:10 +02002475void stl_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002476{
2477 stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
2478}
2479
Avi Kivitya8170e52012-10-23 12:30:10 +02002480void stl_le_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002481{
2482 stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
2483}
2484
Avi Kivitya8170e52012-10-23 12:30:10 +02002485void stl_be_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002486{
2487 stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
2488}
2489
bellardaab33092005-10-30 20:48:42 +00002490/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002491void stb_phys(hwaddr addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00002492{
2493 uint8_t v = val;
2494 cpu_physical_memory_write(addr, &v, 1);
2495}
2496
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002497/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002498static inline void stw_phys_internal(hwaddr addr, uint32_t val,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002499 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00002500{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002501 uint8_t *ptr;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002502 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002503 hwaddr l = 2;
2504 hwaddr addr1;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002505
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002506 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2507 true);
2508 if (l < 2 || !memory_access_is_direct(mr, true)) {
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002509#if defined(TARGET_WORDS_BIGENDIAN)
2510 if (endian == DEVICE_LITTLE_ENDIAN) {
2511 val = bswap16(val);
2512 }
2513#else
2514 if (endian == DEVICE_BIG_ENDIAN) {
2515 val = bswap16(val);
2516 }
2517#endif
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002518 io_mem_write(mr, addr1, val, 2);
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002519 } else {
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002520 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002521 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002522 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002523 switch (endian) {
2524 case DEVICE_LITTLE_ENDIAN:
2525 stw_le_p(ptr, val);
2526 break;
2527 case DEVICE_BIG_ENDIAN:
2528 stw_be_p(ptr, val);
2529 break;
2530 default:
2531 stw_p(ptr, val);
2532 break;
2533 }
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002534 invalidate_and_set_dirty(addr1, 2);
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002535 }
bellardaab33092005-10-30 20:48:42 +00002536}
2537
Avi Kivitya8170e52012-10-23 12:30:10 +02002538void stw_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002539{
2540 stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
2541}
2542
Avi Kivitya8170e52012-10-23 12:30:10 +02002543void stw_le_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002544{
2545 stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
2546}
2547
Avi Kivitya8170e52012-10-23 12:30:10 +02002548void stw_be_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002549{
2550 stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
2551}
2552
bellardaab33092005-10-30 20:48:42 +00002553/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002554void stq_phys(hwaddr addr, uint64_t val)
bellardaab33092005-10-30 20:48:42 +00002555{
2556 val = tswap64(val);
Stefan Weil71d2b722011-03-26 21:06:56 +01002557 cpu_physical_memory_write(addr, &val, 8);
bellardaab33092005-10-30 20:48:42 +00002558}
2559
Avi Kivitya8170e52012-10-23 12:30:10 +02002560void stq_le_phys(hwaddr addr, uint64_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002561{
2562 val = cpu_to_le64(val);
2563 cpu_physical_memory_write(addr, &val, 8);
2564}
2565
Avi Kivitya8170e52012-10-23 12:30:10 +02002566void stq_be_phys(hwaddr addr, uint64_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002567{
2568 val = cpu_to_be64(val);
2569 cpu_physical_memory_write(addr, &val, 8);
2570}
2571
aliguori5e2972f2009-03-28 17:51:36 +00002572/* virtual memory access for debug (includes writing to ROM) */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002573int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002574 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002575{
2576 int l;
Avi Kivitya8170e52012-10-23 12:30:10 +02002577 hwaddr phys_addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00002578 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002579
2580 while (len > 0) {
2581 page = addr & TARGET_PAGE_MASK;
2582 phys_addr = cpu_get_phys_page_debug(env, page);
2583 /* if no physical page mapped, return an error */
2584 if (phys_addr == -1)
2585 return -1;
2586 l = (page + TARGET_PAGE_SIZE) - addr;
2587 if (l > len)
2588 l = len;
aliguori5e2972f2009-03-28 17:51:36 +00002589 phys_addr += (addr & ~TARGET_PAGE_MASK);
aliguori5e2972f2009-03-28 17:51:36 +00002590 if (is_write)
2591 cpu_physical_memory_write_rom(phys_addr, buf, l);
2592 else
aliguori5e2972f2009-03-28 17:51:36 +00002593 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002594 len -= l;
2595 buf += l;
2596 addr += l;
2597 }
2598 return 0;
2599}
Paul Brooka68fe892010-03-01 00:08:59 +00002600#endif
bellard13eb76e2004-01-24 15:23:36 +00002601
Blue Swirl8e4a4242013-01-06 18:30:17 +00002602#if !defined(CONFIG_USER_ONLY)
2603
2604/*
2605 * A helper function for the _utterly broken_ virtio device model to find out if
2606 * it's running on a big endian machine. Don't do this at home kids!
2607 */
2608bool virtio_is_big_endian(void);
2609bool virtio_is_big_endian(void)
2610{
2611#if defined(TARGET_WORDS_BIGENDIAN)
2612 return true;
2613#else
2614 return false;
2615#endif
2616}
2617
2618#endif
2619
Wen Congyang76f35532012-05-07 12:04:18 +08002620#ifndef CONFIG_USER_ONLY
Avi Kivitya8170e52012-10-23 12:30:10 +02002621bool cpu_physical_memory_is_io(hwaddr phys_addr)
Wen Congyang76f35532012-05-07 12:04:18 +08002622{
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002623 MemoryRegion*mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002624 hwaddr l = 1;
Wen Congyang76f35532012-05-07 12:04:18 +08002625
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002626 mr = address_space_translate(&address_space_memory,
2627 phys_addr, &phys_addr, &l, false);
Wen Congyang76f35532012-05-07 12:04:18 +08002628
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002629 return !(memory_region_is_ram(mr) ||
2630 memory_region_is_romd(mr));
Wen Congyang76f35532012-05-07 12:04:18 +08002631}
2632#endif
Michael R. Hinesbd2fa512013-06-25 21:35:34 -04002633
2634void qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
2635{
2636 RAMBlock *block;
2637
2638 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
2639 func(block->host, block->offset, block->length, opaque);
2640 }
2641}