exec.c 73.6 KB
Newer Older
B
bellard 已提交
1
/*
2
 *  Virtual page mapping
3
 *
B
bellard 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 *  Copyright (c) 2003 Fabrice Bellard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
B
bellard 已提交
18
 */
B
bellard 已提交
19
#include "config.h"
B
bellard 已提交
20 21 22
#ifdef _WIN32
#include <windows.h>
#else
B
bellard 已提交
23
#include <sys/types.h>
B
bellard 已提交
24 25
#include <sys/mman.h>
#endif
B
bellard 已提交
26

27
#include "qemu-common.h"
B
bellard 已提交
28
#include "cpu.h"
B
bellard 已提交
29
#include "tcg.h"
30
#include "hw/hw.h"
31
#include "hw/qdev.h"
32
#include "qemu/osdep.h"
33
#include "sysemu/kvm.h"
J
Jun Nakajima 已提交
34
#include "hw/xen.h"
35 36
#include "qemu/timer.h"
#include "qemu/config-file.h"
37
#include "exec/memory.h"
38
#include "sysemu/dma.h"
39
#include "exec/address-spaces.h"
40 41
#if defined(CONFIG_USER_ONLY)
#include <qemu.h>
J
Jun Nakajima 已提交
42
#else /* !CONFIG_USER_ONLY */
43
#include "sysemu/xen-mapcache.h"
44
#include "trace.h"
45
#endif
46
#include "exec/cpu-all.h"
B
bellard 已提交
47

48
#include "exec/cputlb.h"
49
#include "translate-all.h"
50

51
#include "exec/memory-internal.h"
52

P
pbrook 已提交
53
//#define DEBUG_UNASSIGNED
54
//#define DEBUG_SUBPAGE
T
ths 已提交
55

56
#if !defined(CONFIG_USER_ONLY)
57
int phys_ram_fd;
A
aliguori 已提交
58
static int in_migration;
P
pbrook 已提交
59

P
Paolo Bonzini 已提交
60
RAMList ram_list = { .blocks = QTAILQ_HEAD_INITIALIZER(ram_list.blocks) };
A
Avi Kivity 已提交
61 62

static MemoryRegion *system_memory;
63
static MemoryRegion *system_io;
A
Avi Kivity 已提交
64

65 66
AddressSpace address_space_io;
AddressSpace address_space_memory;
67
DMAContext dma_context_memory;
68

69
MemoryRegion io_mem_ram, io_mem_rom, io_mem_unassigned, io_mem_notdirty;
70
static MemoryRegion io_mem_subpage_ram;
71

72
#endif
73

74
CPUArchState *first_cpu;
B
bellard 已提交
75 76
/* current CPU in the current thread. It is only valid inside
   cpu_exec() */
77
DEFINE_TLS(CPUArchState *,cpu_single_env);
P
pbrook 已提交
78
/* 0 = Do not count executed instructions.
T
ths 已提交
79
   1 = Precise instruction counting.
P
pbrook 已提交
80
   2 = Adaptive rate instruction counting.  */
81
int use_icount;
B
bellard 已提交
82

83
#if !defined(CONFIG_USER_ONLY)
84

85 86 87
static MemoryRegionSection *phys_sections;
static unsigned phys_sections_nb, phys_sections_nb_alloc;
static uint16_t phys_section_unassigned;
88 89 90
static uint16_t phys_section_notdirty;
static uint16_t phys_section_rom;
static uint16_t phys_section_watch;
91

92 93 94 95
/* Simple allocator for PhysPageEntry nodes */
static PhysPageEntry (*phys_map_nodes)[L2_SIZE];
static unsigned phys_map_nodes_nb, phys_map_nodes_nb_alloc;

96
#define PHYS_MAP_NODE_NIL (((uint16_t)~0) >> 1)
97

98
static void io_mem_init(void);
A
Avi Kivity 已提交
99
static void memory_map_init(void);
B
Blue Swirl 已提交
100
static void *qemu_safe_ram_ptr(ram_addr_t addr);
101

102
static MemoryRegion io_mem_watch;
103
#endif
B
bellard 已提交
104

105
#if !defined(CONFIG_USER_ONLY)
106

107
static void phys_map_node_reserve(unsigned nodes)
108
{
109
    if (phys_map_nodes_nb + nodes > phys_map_nodes_nb_alloc) {
110 111
        typedef PhysPageEntry Node[L2_SIZE];
        phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc * 2, 16);
112 113
        phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc,
                                      phys_map_nodes_nb + nodes);
114 115 116
        phys_map_nodes = g_renew(Node, phys_map_nodes,
                                 phys_map_nodes_nb_alloc);
    }
117 118 119 120 121 122 123 124 125 126
}

static uint16_t phys_map_node_alloc(void)
{
    unsigned i;
    uint16_t ret;

    ret = phys_map_nodes_nb++;
    assert(ret != PHYS_MAP_NODE_NIL);
    assert(ret != phys_map_nodes_nb_alloc);
127
    for (i = 0; i < L2_SIZE; ++i) {
128
        phys_map_nodes[ret][i].is_leaf = 0;
129
        phys_map_nodes[ret][i].ptr = PHYS_MAP_NODE_NIL;
130
    }
131
    return ret;
132 133 134 135 136 137 138
}

static void phys_map_nodes_reset(void)
{
    phys_map_nodes_nb = 0;
}

B
bellard 已提交
139

A
Avi Kivity 已提交
140 141
static void phys_page_set_level(PhysPageEntry *lp, hwaddr *index,
                                hwaddr *nb, uint16_t leaf,
142
                                int level)
143 144 145
{
    PhysPageEntry *p;
    int i;
A
Avi Kivity 已提交
146
    hwaddr step = (hwaddr)1 << (level * L2_BITS);
147

148
    if (!lp->is_leaf && lp->ptr == PHYS_MAP_NODE_NIL) {
149 150
        lp->ptr = phys_map_node_alloc();
        p = phys_map_nodes[lp->ptr];
151 152
        if (level == 0) {
            for (i = 0; i < L2_SIZE; i++) {
153
                p[i].is_leaf = 1;
154
                p[i].ptr = phys_section_unassigned;
155
            }
P
pbrook 已提交
156
        }
157
    } else {
158
        p = phys_map_nodes[lp->ptr];
B
bellard 已提交
159
    }
160
    lp = &p[(*index >> (level * L2_BITS)) & (L2_SIZE - 1)];
161

162
    while (*nb && lp < &p[L2_SIZE]) {
163 164
        if ((*index & (step - 1)) == 0 && *nb >= step) {
            lp->is_leaf = true;
165
            lp->ptr = leaf;
166 167
            *index += step;
            *nb -= step;
168 169 170 171
        } else {
            phys_page_set_level(lp, index, nb, leaf, level - 1);
        }
        ++lp;
172 173 174
    }
}

A
Avi Kivity 已提交
175
static void phys_page_set(AddressSpaceDispatch *d,
A
Avi Kivity 已提交
176
                          hwaddr index, hwaddr nb,
177
                          uint16_t leaf)
178
{
179
    /* Wildly overreserve - it doesn't matter much. */
180
    phys_map_node_reserve(3 * P_L2_LEVELS);
181

A
Avi Kivity 已提交
182
    phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
B
bellard 已提交
183 184
}

A
Avi Kivity 已提交
185
MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
B
bellard 已提交
186
{
A
Avi Kivity 已提交
187
    PhysPageEntry lp = d->phys_map;
188 189 190
    PhysPageEntry *p;
    int i;
    uint16_t s_index = phys_section_unassigned;
191

192
    for (i = P_L2_LEVELS - 1; i >= 0 && !lp.is_leaf; i--) {
193
        if (lp.ptr == PHYS_MAP_NODE_NIL) {
194 195
            goto not_found;
        }
196
        p = phys_map_nodes[lp.ptr];
197
        lp = p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)];
198
    }
199

200
    s_index = lp.ptr;
201
not_found:
202 203 204
    return &phys_sections[s_index];
}

B
Blue Swirl 已提交
205 206 207 208
bool memory_region_is_unassigned(MemoryRegion *mr)
{
    return mr != &io_mem_ram && mr != &io_mem_rom
        && mr != &io_mem_notdirty && !mr->rom_device
209
        && mr != &io_mem_watch;
B
bellard 已提交
210
}
211
#endif
B
bellard 已提交
212

213
void cpu_exec_init_all(void)
214
{
215
#if !defined(CONFIG_USER_ONLY)
216
    qemu_mutex_init(&ram_list.mutex);
217 218
    memory_map_init();
    io_mem_init();
219
#endif
220
}
221

222 223 224
#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)

static int cpu_common_post_load(void *opaque, int version_id)
B
bellard 已提交
225
{
226
    CPUArchState *env = opaque;
B
bellard 已提交
227

228 229 230 231 232 233
    /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
       version_id is increased. */
    env->interrupt_request &= ~0x01;
    tlb_flush(env, 1);

    return 0;
B
bellard 已提交
234
}
B
bellard 已提交
235

236 237 238 239 240 241 242 243 244 245 246 247 248
static const VMStateDescription vmstate_cpu_common = {
    .name = "cpu_common",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .post_load = cpu_common_post_load,
    .fields      = (VMStateField []) {
        VMSTATE_UINT32(halted, CPUArchState),
        VMSTATE_UINT32(interrupt_request, CPUArchState),
        VMSTATE_END_OF_LIST()
    }
};
#endif
B
bellard 已提交
249

250
CPUState *qemu_get_cpu(int index)
B
bellard 已提交
251
{
252
    CPUArchState *env = first_cpu;
253
    CPUState *cpu = NULL;
B
bellard 已提交
254

255
    while (env) {
256 257
        cpu = ENV_GET_CPU(env);
        if (cpu->cpu_index == index) {
258
            break;
259
        }
260
        env = env->next_cpu;
B
bellard 已提交
261
    }
262

263
    return cpu;
B
bellard 已提交
264 265
}

266
void cpu_exec_init(CPUArchState *env)
B
bellard 已提交
267
{
268 269 270 271 272 273 274 275 276 277 278 279 280 281
    CPUState *cpu = ENV_GET_CPU(env);
    CPUArchState **penv;
    int cpu_index;

#if defined(CONFIG_USER_ONLY)
    cpu_list_lock();
#endif
    env->next_cpu = NULL;
    penv = &first_cpu;
    cpu_index = 0;
    while (*penv != NULL) {
        penv = &(*penv)->next_cpu;
        cpu_index++;
    }
282
    cpu->cpu_index = cpu_index;
283
    cpu->numa_node = 0;
284 285 286 287 288 289 290 291 292 293 294 295 296 297
    QTAILQ_INIT(&env->breakpoints);
    QTAILQ_INIT(&env->watchpoints);
#ifndef CONFIG_USER_ONLY
    cpu->thread_id = qemu_get_thread_id();
#endif
    *penv = env;
#if defined(CONFIG_USER_ONLY)
    cpu_list_unlock();
#endif
#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
    vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env);
    register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
                    cpu_save, cpu_load, env);
#endif
B
bellard 已提交
298 299
}

B
bellard 已提交
300
#if defined(TARGET_HAS_ICE)
301
#if defined(CONFIG_USER_ONLY)
302
static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
303 304 305 306
{
    tb_invalidate_phys_page_range(pc, pc + 1, 0);
}
#else
307 308
static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
{
309 310
    tb_invalidate_phys_addr(cpu_get_phys_page_debug(env, pc) |
            (pc & ~TARGET_PAGE_MASK));
311
}
B
bellard 已提交
312
#endif
313
#endif /* TARGET_HAS_ICE */
B
bellard 已提交
314

315
#if defined(CONFIG_USER_ONLY)
316
void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
317 318 319 320

{
}

321
int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
322 323 324 325 326
                          int flags, CPUWatchpoint **watchpoint)
{
    return -ENOSYS;
}
#else
327
/* Add a watchpoint.  */
328
int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
329
                          int flags, CPUWatchpoint **watchpoint)
330
{
331
    target_ulong len_mask = ~(len - 1);
332
    CPUWatchpoint *wp;
333

334
    /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
335 336
    if ((len & (len - 1)) || (addr & ~len_mask) ||
            len == 0 || len > TARGET_PAGE_SIZE) {
337 338 339 340
        fprintf(stderr, "qemu: tried to set invalid watchpoint at "
                TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
        return -EINVAL;
    }
341
    wp = g_malloc(sizeof(*wp));
342 343

    wp->vaddr = addr;
344
    wp->len_mask = len_mask;
345 346
    wp->flags = flags;

347
    /* keep all GDB-injected watchpoints in front */
348
    if (flags & BP_GDB)
B
Blue Swirl 已提交
349
        QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
350
    else
B
Blue Swirl 已提交
351
        QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
352 353

    tlb_flush_page(env, addr);
354 355 356 357

    if (watchpoint)
        *watchpoint = wp;
    return 0;
358 359
}

360
/* Remove a specific watchpoint.  */
361
int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr, target_ulong len,
362
                          int flags)
363
{
364
    target_ulong len_mask = ~(len - 1);
365
    CPUWatchpoint *wp;
366

B
Blue Swirl 已提交
367
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
368
        if (addr == wp->vaddr && len_mask == wp->len_mask
369
                && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
370
            cpu_watchpoint_remove_by_ref(env, wp);
371 372 373
            return 0;
        }
    }
374
    return -ENOENT;
375 376
}

377
/* Remove a specific watchpoint by reference.  */
378
void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint)
379
{
B
Blue Swirl 已提交
380
    QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
381

382 383
    tlb_flush_page(env, watchpoint->vaddr);

384
    g_free(watchpoint);
385 386 387
}

/* Remove all matching watchpoints.  */
388
void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
389
{
390
    CPUWatchpoint *wp, *next;
391

B
Blue Swirl 已提交
392
    QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
393 394
        if (wp->flags & mask)
            cpu_watchpoint_remove_by_ref(env, wp);
395
    }
396
}
397
#endif
398

399
/* Add a breakpoint.  */
400
int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
401
                          CPUBreakpoint **breakpoint)
B
bellard 已提交
402
{
B
bellard 已提交
403
#if defined(TARGET_HAS_ICE)
404
    CPUBreakpoint *bp;
405

406
    bp = g_malloc(sizeof(*bp));
B
bellard 已提交
407

408 409 410
    bp->pc = pc;
    bp->flags = flags;

411
    /* keep all GDB-injected breakpoints in front */
412
    if (flags & BP_GDB)
B
Blue Swirl 已提交
413
        QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
414
    else
B
Blue Swirl 已提交
415
        QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
416

B
bellard 已提交
417
    breakpoint_invalidate(env, pc);
418 419 420

    if (breakpoint)
        *breakpoint = bp;
B
bellard 已提交
421 422
    return 0;
#else
423
    return -ENOSYS;
B
bellard 已提交
424 425 426
#endif
}

427
/* Remove a specific breakpoint.  */
428
int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags)
429
{
430
#if defined(TARGET_HAS_ICE)
431 432
    CPUBreakpoint *bp;

B
Blue Swirl 已提交
433
    QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
434 435 436 437
        if (bp->pc == pc && bp->flags == flags) {
            cpu_breakpoint_remove_by_ref(env, bp);
            return 0;
        }
438
    }
439 440 441
    return -ENOENT;
#else
    return -ENOSYS;
442 443 444
#endif
}

445
/* Remove a specific breakpoint by reference.  */
446
void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint)
B
bellard 已提交
447
{
B
bellard 已提交
448
#if defined(TARGET_HAS_ICE)
B
Blue Swirl 已提交
449
    QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
B
bellard 已提交
450

451 452
    breakpoint_invalidate(env, breakpoint->pc);

453
    g_free(breakpoint);
454 455 456 457
#endif
}

/* Remove all matching breakpoints. */
458
void cpu_breakpoint_remove_all(CPUArchState *env, int mask)
459 460
{
#if defined(TARGET_HAS_ICE)
461
    CPUBreakpoint *bp, *next;
462

B
Blue Swirl 已提交
463
    QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
464 465
        if (bp->flags & mask)
            cpu_breakpoint_remove_by_ref(env, bp);
466
    }
B
bellard 已提交
467 468 469
#endif
}

B
bellard 已提交
470 471
/* enable or disable single step mode. EXCP_DEBUG is returned by the
   CPU loop after each instruction */
472
void cpu_single_step(CPUArchState *env, int enabled)
B
bellard 已提交
473
{
B
bellard 已提交
474
#if defined(TARGET_HAS_ICE)
B
bellard 已提交
475 476
    if (env->singlestep_enabled != enabled) {
        env->singlestep_enabled = enabled;
477 478 479
        if (kvm_enabled())
            kvm_update_guest_debug(env, 0);
        else {
S
Stuart Brady 已提交
480
            /* must flush all the translated code to avoid inconsistencies */
481 482 483
            /* XXX: only flush what is necessary */
            tb_flush(env);
        }
B
bellard 已提交
484 485 486 487
    }
#endif
}

488
void cpu_reset_interrupt(CPUArchState *env, int mask)
489 490 491 492
{
    env->interrupt_request &= ~mask;
}

493
void cpu_exit(CPUArchState *env)
494
{
495 496 497
    CPUState *cpu = ENV_GET_CPU(env);

    cpu->exit_request = 1;
498 499 500
    cpu_unlink_tb(env);
}

501
void cpu_abort(CPUArchState *env, const char *fmt, ...)
B
bellard 已提交
502 503
{
    va_list ap;
P
pbrook 已提交
504
    va_list ap2;
B
bellard 已提交
505 506

    va_start(ap, fmt);
P
pbrook 已提交
507
    va_copy(ap2, ap);
B
bellard 已提交
508 509 510
    fprintf(stderr, "qemu: fatal: ");
    vfprintf(stderr, fmt, ap);
    fprintf(stderr, "\n");
511
    cpu_dump_state(env, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
512 513 514 515
    if (qemu_log_enabled()) {
        qemu_log("qemu: fatal: ");
        qemu_log_vprintf(fmt, ap2);
        qemu_log("\n");
516
        log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
517
        qemu_log_flush();
518
        qemu_log_close();
519
    }
P
pbrook 已提交
520
    va_end(ap2);
521
    va_end(ap);
522 523 524 525 526 527 528 529
#if defined(CONFIG_USER_ONLY)
    {
        struct sigaction act;
        sigfillset(&act.sa_mask);
        act.sa_handler = SIG_DFL;
        sigaction(SIGABRT, &act, NULL);
    }
#endif
B
bellard 已提交
530 531 532
    abort();
}

533
CPUArchState *cpu_copy(CPUArchState *env)
534
{
535 536
    CPUArchState *new_env = cpu_init(env->cpu_model_str);
    CPUArchState *next_cpu = new_env->next_cpu;
537 538 539 540 541
#if defined(TARGET_HAS_ICE)
    CPUBreakpoint *bp;
    CPUWatchpoint *wp;
#endif

542
    memcpy(new_env, env, sizeof(CPUArchState));
543

544
    /* Preserve chaining. */
545
    new_env->next_cpu = next_cpu;
546 547 548 549

    /* Clone all break/watchpoints.
       Note: Once we support ptrace with hw-debug register access, make sure
       BP_CPU break/watchpoints are handled correctly on clone. */
B
Blue Swirl 已提交
550 551
    QTAILQ_INIT(&env->breakpoints);
    QTAILQ_INIT(&env->watchpoints);
552
#if defined(TARGET_HAS_ICE)
B
Blue Swirl 已提交
553
    QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
554 555
        cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
    }
B
Blue Swirl 已提交
556
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
557 558 559 560 561
        cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
                              wp->flags, NULL);
    }
#endif

562 563 564
    return new_env;
}

565
#if !defined(CONFIG_USER_ONLY)
J
Juan Quintela 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t end,
                                      uintptr_t length)
{
    uintptr_t start1;

    /* we modify the TLB cache so that the dirty bit will be set again
       when accessing the range */
    start1 = (uintptr_t)qemu_safe_ram_ptr(start);
    /* Check that we don't span multiple blocks - this breaks the
       address comparisons below.  */
    if ((uintptr_t)qemu_safe_ram_ptr(end - 1) - start1
            != (end - 1) - start) {
        abort();
    }
    cpu_tlb_reset_dirty_all(start1, length);

}

P
pbrook 已提交
584
/* Note: start and end must be within the same ram block.  */
A
Anthony Liguori 已提交
585
void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
B
bellard 已提交
586
                                     int dirty_flags)
587
{
J
Juan Quintela 已提交
588
    uintptr_t length;
589 590 591 592 593 594 595

    start &= TARGET_PAGE_MASK;
    end = TARGET_PAGE_ALIGN(end);

    length = end - start;
    if (length == 0)
        return;
596
    cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
B
bellard 已提交
597

J
Juan Quintela 已提交
598 599
    if (tcg_enabled()) {
        tlb_reset_dirty_range_all(start, end, length);
P
pbrook 已提交
600
    }
601 602
}

B
Blue Swirl 已提交
603
static int cpu_physical_memory_set_dirty_tracking(int enable)
A
aliguori 已提交
604
{
M
Michael S. Tsirkin 已提交
605
    int ret = 0;
A
aliguori 已提交
606
    in_migration = enable;
M
Michael S. Tsirkin 已提交
607
    return ret;
A
aliguori 已提交
608 609
}

A
Avi Kivity 已提交
610
hwaddr memory_region_section_get_iotlb(CPUArchState *env,
B
Blue Swirl 已提交
611 612
                                                   MemoryRegionSection *section,
                                                   target_ulong vaddr,
A
Avi Kivity 已提交
613
                                                   hwaddr paddr,
B
Blue Swirl 已提交
614 615 616
                                                   int prot,
                                                   target_ulong *address)
{
A
Avi Kivity 已提交
617
    hwaddr iotlb;
B
Blue Swirl 已提交
618 619
    CPUWatchpoint *wp;

620
    if (memory_region_is_ram(section->mr)) {
B
Blue Swirl 已提交
621 622
        /* Normal RAM.  */
        iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
623
            + memory_region_section_addr(section, paddr);
B
Blue Swirl 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636
        if (!section->readonly) {
            iotlb |= phys_section_notdirty;
        } else {
            iotlb |= phys_section_rom;
        }
    } else {
        /* IO handlers are currently passed a physical address.
           It would be nice to pass an offset from the base address
           of that region.  This would avoid having to special case RAM,
           and avoid full address decoding in every device.
           We can't use the high bits of pd for this because
           IO_MEM_ROMD uses these as a ram address.  */
        iotlb = section - phys_sections;
637
        iotlb += memory_region_section_addr(section, paddr);
B
Blue Swirl 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    }

    /* Make accesses to pages with watchpoints go via the
       watchpoint trap routines.  */
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
        if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
            /* Avoid trapping reads of pages with a write breakpoint. */
            if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
                iotlb = phys_section_watch + paddr;
                *address |= TLB_MMIO;
                break;
            }
        }
    }

    return iotlb;
}
655 656
#endif /* defined(CONFIG_USER_ONLY) */

657
#if !defined(CONFIG_USER_ONLY)
658

P
Paul Brook 已提交
659 660
#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
typedef struct subpage_t {
661
    MemoryRegion iomem;
A
Avi Kivity 已提交
662
    hwaddr base;
663
    uint16_t sub_section[TARGET_PAGE_SIZE];
P
Paul Brook 已提交
664 665
} subpage_t;

A
Anthony Liguori 已提交
666
static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
667
                             uint16_t section);
A
Avi Kivity 已提交
668
static subpage_t *subpage_init(hwaddr base);
669
static void destroy_page_desc(uint16_t section_index)
670
{
671 672
    MemoryRegionSection *section = &phys_sections[section_index];
    MemoryRegion *mr = section->mr;
673 674 675 676 677 678 679 680

    if (mr->subpage) {
        subpage_t *subpage = container_of(mr, subpage_t, iomem);
        memory_region_destroy(&subpage->iomem);
        g_free(subpage);
    }
}

681
static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level)
682 683
{
    unsigned i;
684
    PhysPageEntry *p;
685

686
    if (lp->ptr == PHYS_MAP_NODE_NIL) {
687 688 689
        return;
    }

690
    p = phys_map_nodes[lp->ptr];
691
    for (i = 0; i < L2_SIZE; ++i) {
692
        if (!p[i].is_leaf) {
693
            destroy_l2_mapping(&p[i], level - 1);
694
        } else {
695
            destroy_page_desc(p[i].ptr);
696 697
        }
    }
698
    lp->is_leaf = 0;
699
    lp->ptr = PHYS_MAP_NODE_NIL;
700 701
}

A
Avi Kivity 已提交
702
static void destroy_all_mappings(AddressSpaceDispatch *d)
703
{
A
Avi Kivity 已提交
704
    destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
705
    phys_map_nodes_reset();
706 707
}

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
static uint16_t phys_section_add(MemoryRegionSection *section)
{
    if (phys_sections_nb == phys_sections_nb_alloc) {
        phys_sections_nb_alloc = MAX(phys_sections_nb_alloc * 2, 16);
        phys_sections = g_renew(MemoryRegionSection, phys_sections,
                                phys_sections_nb_alloc);
    }
    phys_sections[phys_sections_nb] = *section;
    return phys_sections_nb++;
}

static void phys_sections_clear(void)
{
    phys_sections_nb = 0;
}

A
Avi Kivity 已提交
724
static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
725 726
{
    subpage_t *subpage;
A
Avi Kivity 已提交
727
    hwaddr base = section->offset_within_address_space
728
        & TARGET_PAGE_MASK;
A
Avi Kivity 已提交
729
    MemoryRegionSection *existing = phys_page_find(d, base >> TARGET_PAGE_BITS);
730 731 732 733
    MemoryRegionSection subsection = {
        .offset_within_address_space = base,
        .size = TARGET_PAGE_SIZE,
    };
A
Avi Kivity 已提交
734
    hwaddr start, end;
735

736
    assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
737

738
    if (!(existing->mr->subpage)) {
739 740
        subpage = subpage_init(base);
        subsection.mr = &subpage->iomem;
A
Avi Kivity 已提交
741
        phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
742
                      phys_section_add(&subsection));
743
    } else {
744
        subpage = container_of(existing->mr, subpage_t, iomem);
745 746
    }
    start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
747
    end = start + section->size - 1;
748 749 750 751
    subpage_register(subpage, start, end, phys_section_add(section));
}


A
Avi Kivity 已提交
752
static void register_multipage(AddressSpaceDispatch *d, MemoryRegionSection *section)
753
{
A
Avi Kivity 已提交
754
    hwaddr start_addr = section->offset_within_address_space;
755
    ram_addr_t size = section->size;
A
Avi Kivity 已提交
756
    hwaddr addr;
757
    uint16_t section_index = phys_section_add(section);
758

759
    assert(size);
M
Michael S. Tsirkin 已提交
760

761
    addr = start_addr;
A
Avi Kivity 已提交
762
    phys_page_set(d, addr >> TARGET_PAGE_BITS, size >> TARGET_PAGE_BITS,
763
                  section_index);
764 765
}

A
Avi Kivity 已提交
766
static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
767
{
A
Avi Kivity 已提交
768
    AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
769 770 771 772 773 774 775
    MemoryRegionSection now = *section, remain = *section;

    if ((now.offset_within_address_space & ~TARGET_PAGE_MASK)
        || (now.size < TARGET_PAGE_SIZE)) {
        now.size = MIN(TARGET_PAGE_ALIGN(now.offset_within_address_space)
                       - now.offset_within_address_space,
                       now.size);
A
Avi Kivity 已提交
776
        register_subpage(d, &now);
777 778 779 780
        remain.size -= now.size;
        remain.offset_within_address_space += now.size;
        remain.offset_within_region += now.size;
    }
781 782 783 784
    while (remain.size >= TARGET_PAGE_SIZE) {
        now = remain;
        if (remain.offset_within_region & ~TARGET_PAGE_MASK) {
            now.size = TARGET_PAGE_SIZE;
A
Avi Kivity 已提交
785
            register_subpage(d, &now);
786 787
        } else {
            now.size &= TARGET_PAGE_MASK;
A
Avi Kivity 已提交
788
            register_multipage(d, &now);
789
        }
790 791 792 793 794 795
        remain.size -= now.size;
        remain.offset_within_address_space += now.size;
        remain.offset_within_region += now.size;
    }
    now = remain;
    if (now.size) {
A
Avi Kivity 已提交
796
        register_subpage(d, &now);
797 798 799
    }
}

800 801 802 803 804 805
void qemu_flush_coalesced_mmio_buffer(void)
{
    if (kvm_enabled())
        kvm_flush_coalesced_mmio_buffer();
}

806 807 808 809 810 811 812 813 814 815
void qemu_mutex_lock_ramlist(void)
{
    qemu_mutex_lock(&ram_list.mutex);
}

void qemu_mutex_unlock_ramlist(void)
{
    qemu_mutex_unlock(&ram_list.mutex);
}

816 817 818 819 820 821 822 823 824 825 826 827
#if defined(__linux__) && !defined(TARGET_S390X)

#include <sys/vfs.h>

#define HUGETLBFS_MAGIC       0x958458f6

static long gethugepagesize(const char *path)
{
    struct statfs fs;
    int ret;

    do {
Y
Yoshiaki Tamura 已提交
828
        ret = statfs(path, &fs);
829 830 831
    } while (ret != 0 && errno == EINTR);

    if (ret != 0) {
Y
Yoshiaki Tamura 已提交
832 833
        perror(path);
        return 0;
834 835 836
    }

    if (fs.f_type != HUGETLBFS_MAGIC)
Y
Yoshiaki Tamura 已提交
837
        fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
838 839 840 841

    return fs.f_bsize;
}

A
Alex Williamson 已提交
842 843 844
static void *file_ram_alloc(RAMBlock *block,
                            ram_addr_t memory,
                            const char *path)
845 846 847 848 849 850 851 852 853 854 855
{
    char *filename;
    void *area;
    int fd;
#ifdef MAP_POPULATE
    int flags;
#endif
    unsigned long hpagesize;

    hpagesize = gethugepagesize(path);
    if (!hpagesize) {
Y
Yoshiaki Tamura 已提交
856
        return NULL;
857 858 859 860 861 862 863 864 865 866 867
    }

    if (memory < hpagesize) {
        return NULL;
    }

    if (kvm_enabled() && !kvm_has_sync_mmu()) {
        fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
        return NULL;
    }

868
    filename = g_strdup_printf("%s/qemu_back_mem.XXXXXX", path);
869 870 871

    fd = mkstemp(filename);
    if (fd < 0) {
Y
Yoshiaki Tamura 已提交
872
        perror("unable to create backing store for hugepages");
873
        g_free(filename);
Y
Yoshiaki Tamura 已提交
874
        return NULL;
875 876
    }
    unlink(filename);
877
    g_free(filename);
878 879 880 881 882 883 884 885 886 887

    memory = (memory+hpagesize-1) & ~(hpagesize-1);

    /*
     * ftruncate is not supported by hugetlbfs in older
     * hosts, so don't bother bailing out on errors.
     * If anything goes wrong with it under other filesystems,
     * mmap will fail.
     */
    if (ftruncate(fd, memory))
Y
Yoshiaki Tamura 已提交
888
        perror("ftruncate");
889 890 891 892 893 894 895 896 897 898 899 900

#ifdef MAP_POPULATE
    /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
     * MAP_PRIVATE is requested.  For mem_prealloc we mmap as MAP_SHARED
     * to sidestep this quirk.
     */
    flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
    area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
#else
    area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
#endif
    if (area == MAP_FAILED) {
Y
Yoshiaki Tamura 已提交
901 902 903
        perror("file_ram_alloc: can't mmap RAM pages");
        close(fd);
        return (NULL);
904
    }
A
Alex Williamson 已提交
905
    block->fd = fd;
906 907 908 909
    return area;
}
#endif

910
static ram_addr_t find_ram_offset(ram_addr_t size)
A
Alex Williamson 已提交
911 912
{
    RAMBlock *block, *next_block;
A
Alex Williamson 已提交
913
    ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
A
Alex Williamson 已提交
914

P
Paolo Bonzini 已提交
915
    if (QTAILQ_EMPTY(&ram_list.blocks))
A
Alex Williamson 已提交
916 917
        return 0;

P
Paolo Bonzini 已提交
918
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
919
        ram_addr_t end, next = RAM_ADDR_MAX;
A
Alex Williamson 已提交
920 921 922

        end = block->offset + block->length;

P
Paolo Bonzini 已提交
923
        QTAILQ_FOREACH(next_block, &ram_list.blocks, next) {
A
Alex Williamson 已提交
924 925 926 927 928
            if (next_block->offset >= end) {
                next = MIN(next, next_block->offset);
            }
        }
        if (next - end >= size && next - end < mingap) {
A
Alex Williamson 已提交
929
            offset = end;
A
Alex Williamson 已提交
930 931 932
            mingap = next - end;
        }
    }
A
Alex Williamson 已提交
933 934 935 936 937 938 939

    if (offset == RAM_ADDR_MAX) {
        fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
                (uint64_t)size);
        abort();
    }

A
Alex Williamson 已提交
940 941 942
    return offset;
}

J
Juan Quintela 已提交
943
ram_addr_t last_ram_offset(void)
944 945 946 947
{
    RAMBlock *block;
    ram_addr_t last = 0;

P
Paolo Bonzini 已提交
948
    QTAILQ_FOREACH(block, &ram_list.blocks, next)
949 950 951 952 953
        last = MAX(last, block->offset + block->length);

    return last;
}

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
{
    int ret;
    QemuOpts *machine_opts;

    /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
    machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
    if (machine_opts &&
        !qemu_opt_get_bool(machine_opts, "dump-guest-core", true)) {
        ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
        if (ret) {
            perror("qemu_madvise");
            fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
                            "but dump_guest_core=off specified\n");
        }
    }
}

972
void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
973 974 975
{
    RAMBlock *new_block, *block;

976
    new_block = NULL;
P
Paolo Bonzini 已提交
977
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
978 979 980 981 982 983 984
        if (block->offset == addr) {
            new_block = block;
            break;
        }
    }
    assert(new_block);
    assert(!new_block->idstr[0]);
985

986 987
    if (dev) {
        char *id = qdev_get_dev_path(dev);
988 989
        if (id) {
            snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
990
            g_free(id);
991 992 993 994
        }
    }
    pstrcat(new_block->idstr, sizeof(new_block->idstr), name);

995 996
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
P
Paolo Bonzini 已提交
997
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
998
        if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
999 1000 1001 1002 1003
            fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
                    new_block->idstr);
            abort();
        }
    }
1004
    qemu_mutex_unlock_ramlist();
1005 1006
}

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
static int memory_try_enable_merging(void *addr, size_t len)
{
    QemuOpts *opts;

    opts = qemu_opts_find(qemu_find_opts("machine"), 0);
    if (opts && !qemu_opt_get_bool(opts, "mem-merge", true)) {
        /* disabled by the user */
        return 0;
    }

    return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
}

1020 1021 1022
ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                   MemoryRegion *mr)
{
1023
    RAMBlock *block, *new_block;
1024 1025 1026

    size = TARGET_PAGE_ALIGN(size);
    new_block = g_malloc0(sizeof(*new_block));
1027

1028 1029
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
A
Avi Kivity 已提交
1030
    new_block->mr = mr;
J
Jun Nakajima 已提交
1031
    new_block->offset = find_ram_offset(size);
1032 1033
    if (host) {
        new_block->host = host;
H
Huang Ying 已提交
1034
        new_block->flags |= RAM_PREALLOC_MASK;
1035 1036
    } else {
        if (mem_path) {
1037
#if defined (__linux__) && !defined(TARGET_S390X)
1038 1039 1040
            new_block->host = file_ram_alloc(new_block, size, mem_path);
            if (!new_block->host) {
                new_block->host = qemu_vmalloc(size);
1041
                memory_try_enable_merging(new_block->host, size);
1042
            }
1043
#else
1044 1045
            fprintf(stderr, "-mem-path option unsupported\n");
            exit(1);
1046
#endif
1047
        } else {
1048
            if (xen_enabled()) {
1049
                xen_ram_alloc(new_block->offset, size, mr);
1050 1051 1052
            } else if (kvm_enabled()) {
                /* some s390/kvm configurations have special constraints */
                new_block->host = kvm_vmalloc(size);
J
Jun Nakajima 已提交
1053 1054 1055
            } else {
                new_block->host = qemu_vmalloc(size);
            }
1056
            memory_try_enable_merging(new_block->host, size);
1057
        }
1058
    }
P
pbrook 已提交
1059 1060
    new_block->length = size;

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    /* Keep the list sorted from biggest to smallest block.  */
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
        if (block->length < new_block->length) {
            break;
        }
    }
    if (block) {
        QTAILQ_INSERT_BEFORE(block, new_block, next);
    } else {
        QTAILQ_INSERT_TAIL(&ram_list.blocks, new_block, next);
    }
1072
    ram_list.mru_block = NULL;
P
pbrook 已提交
1073

U
Umesh Deshpande 已提交
1074
    ram_list.version++;
1075
    qemu_mutex_unlock_ramlist();
U
Umesh Deshpande 已提交
1076

1077
    ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
A
Alex Williamson 已提交
1078
                                       last_ram_offset() >> TARGET_PAGE_BITS);
1079 1080
    memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
           0, size >> TARGET_PAGE_BITS);
J
Juan Quintela 已提交
1081
    cpu_physical_memory_set_dirty_range(new_block->offset, size, 0xff);
P
pbrook 已提交
1082

1083
    qemu_ram_setup_dump(new_block->host, size);
1084
    qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
1085

1086 1087 1088
    if (kvm_enabled())
        kvm_setup_guest_memory(new_block->host, size);

P
pbrook 已提交
1089 1090
    return new_block->offset;
}
B
bellard 已提交
1091

1092
ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr)
1093
{
1094
    return qemu_ram_alloc_from_ptr(size, NULL, mr);
1095 1096
}

1097 1098 1099 1100
void qemu_ram_free_from_ptr(ram_addr_t addr)
{
    RAMBlock *block;

1101 1102
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
P
Paolo Bonzini 已提交
1103
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1104
        if (addr == block->offset) {
P
Paolo Bonzini 已提交
1105
            QTAILQ_REMOVE(&ram_list.blocks, block, next);
1106
            ram_list.mru_block = NULL;
U
Umesh Deshpande 已提交
1107
            ram_list.version++;
1108
            g_free(block);
1109
            break;
1110 1111
        }
    }
1112
    qemu_mutex_unlock_ramlist();
1113 1114
}

A
Anthony Liguori 已提交
1115
void qemu_ram_free(ram_addr_t addr)
B
bellard 已提交
1116
{
A
Alex Williamson 已提交
1117 1118
    RAMBlock *block;

1119 1120
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
P
Paolo Bonzini 已提交
1121
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
A
Alex Williamson 已提交
1122
        if (addr == block->offset) {
P
Paolo Bonzini 已提交
1123
            QTAILQ_REMOVE(&ram_list.blocks, block, next);
1124
            ram_list.mru_block = NULL;
U
Umesh Deshpande 已提交
1125
            ram_list.version++;
H
Huang Ying 已提交
1126 1127 1128
            if (block->flags & RAM_PREALLOC_MASK) {
                ;
            } else if (mem_path) {
A
Alex Williamson 已提交
1129 1130 1131 1132 1133 1134 1135
#if defined (__linux__) && !defined(TARGET_S390X)
                if (block->fd) {
                    munmap(block->host, block->length);
                    close(block->fd);
                } else {
                    qemu_vfree(block->host);
                }
1136 1137
#else
                abort();
A
Alex Williamson 已提交
1138 1139 1140 1141 1142
#endif
            } else {
#if defined(TARGET_S390X) && defined(CONFIG_KVM)
                munmap(block->host, block->length);
#else
1143
                if (xen_enabled()) {
J
Jan Kiszka 已提交
1144
                    xen_invalidate_map_cache_entry(block->host);
J
Jun Nakajima 已提交
1145 1146 1147
                } else {
                    qemu_vfree(block->host);
                }
A
Alex Williamson 已提交
1148 1149
#endif
            }
1150
            g_free(block);
1151
            break;
A
Alex Williamson 已提交
1152 1153
        }
    }
1154
    qemu_mutex_unlock_ramlist();
A
Alex Williamson 已提交
1155

B
bellard 已提交
1156 1157
}

H
Huang Ying 已提交
1158 1159 1160 1161 1162 1163 1164 1165
#ifndef _WIN32
void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
{
    RAMBlock *block;
    ram_addr_t offset;
    int flags;
    void *area, *vaddr;

P
Paolo Bonzini 已提交
1166
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
H
Huang Ying 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
        offset = addr - block->offset;
        if (offset < block->length) {
            vaddr = block->host + offset;
            if (block->flags & RAM_PREALLOC_MASK) {
                ;
            } else {
                flags = MAP_FIXED;
                munmap(vaddr, length);
                if (mem_path) {
#if defined(__linux__) && !defined(TARGET_S390X)
                    if (block->fd) {
#ifdef MAP_POPULATE
                        flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
                            MAP_PRIVATE;
#else
                        flags |= MAP_PRIVATE;
#endif
                        area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
                                    flags, block->fd, offset);
                    } else {
                        flags |= MAP_PRIVATE | MAP_ANONYMOUS;
                        area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
                                    flags, -1, 0);
                    }
1191 1192
#else
                    abort();
H
Huang Ying 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
#endif
                } else {
#if defined(TARGET_S390X) && defined(CONFIG_KVM)
                    flags |= MAP_SHARED | MAP_ANONYMOUS;
                    area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE,
                                flags, -1, 0);
#else
                    flags |= MAP_PRIVATE | MAP_ANONYMOUS;
                    area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
                                flags, -1, 0);
#endif
                }
                if (area != vaddr) {
1206 1207
                    fprintf(stderr, "Could not remap addr: "
                            RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
H
Huang Ying 已提交
1208 1209 1210
                            length, addr);
                    exit(1);
                }
1211
                memory_try_enable_merging(vaddr, length);
1212
                qemu_ram_setup_dump(vaddr, length);
H
Huang Ying 已提交
1213 1214 1215 1216 1217 1218 1219
            }
            return;
        }
    }
}
#endif /* !_WIN32 */

1220
/* Return a host pointer to ram allocated with qemu_ram_alloc.
P
pbrook 已提交
1221 1222 1223 1224 1225 1226 1227
   With the exception of the softmmu code in this file, this should
   only be used for local memory (e.g. video ram) that the device owns,
   and knows it isn't going to access beyond the end of the block.

   It should not be used for general purpose DMA.
   Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
 */
A
Anthony Liguori 已提交
1228
void *qemu_get_ram_ptr(ram_addr_t addr)
1229
{
P
pbrook 已提交
1230 1231
    RAMBlock *block;

1232
    /* The list is protected by the iothread lock here.  */
1233 1234 1235 1236
    block = ram_list.mru_block;
    if (block && addr - block->offset < block->length) {
        goto found;
    }
P
Paolo Bonzini 已提交
1237
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
A
Alex Williamson 已提交
1238
        if (addr - block->offset < block->length) {
1239
            goto found;
A
Alex Williamson 已提交
1240
        }
P
pbrook 已提交
1241
    }
A
Alex Williamson 已提交
1242 1243 1244 1245

    fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
    abort();

1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
found:
    ram_list.mru_block = block;
    if (xen_enabled()) {
        /* We need to check if the requested address is in the RAM
         * because we don't want to map the entire memory in QEMU.
         * In that case just map until the end of the page.
         */
        if (block->offset == 0) {
            return xen_map_cache(addr, 0, 0);
        } else if (block->host == NULL) {
            block->host =
                xen_map_cache(block->offset, block->length, 1);
        }
    }
    return block->host + (addr - block->offset);
1261 1262
}

1263 1264 1265 1266
/* Return a host pointer to ram allocated with qemu_ram_alloc.  Same as
 * qemu_get_ram_ptr but do not touch ram_list.mru_block.
 *
 * ??? Is this still necessary?
1267
 */
B
Blue Swirl 已提交
1268
static void *qemu_safe_ram_ptr(ram_addr_t addr)
1269 1270 1271
{
    RAMBlock *block;

1272
    /* The list is protected by the iothread lock here.  */
P
Paolo Bonzini 已提交
1273
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1274
        if (addr - block->offset < block->length) {
1275
            if (xen_enabled()) {
J
Jun Nakajima 已提交
1276 1277
                /* We need to check if the requested address is in the RAM
                 * because we don't want to map the entire memory in QEMU.
1278
                 * In that case just map until the end of the page.
J
Jun Nakajima 已提交
1279 1280
                 */
                if (block->offset == 0) {
J
Jan Kiszka 已提交
1281
                    return xen_map_cache(addr, 0, 0);
J
Jun Nakajima 已提交
1282
                } else if (block->host == NULL) {
J
Jan Kiszka 已提交
1283 1284
                    block->host =
                        xen_map_cache(block->offset, block->length, 1);
J
Jun Nakajima 已提交
1285 1286
                }
            }
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
            return block->host + (addr - block->offset);
        }
    }

    fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
    abort();

    return NULL;
}

1297 1298
/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
 * but takes a size argument */
B
Blue Swirl 已提交
1299
static void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
1300
{
1301 1302 1303
    if (*size == 0) {
        return NULL;
    }
1304
    if (xen_enabled()) {
J
Jan Kiszka 已提交
1305
        return xen_map_cache(addr, *size, 1);
1306
    } else {
1307 1308
        RAMBlock *block;

P
Paolo Bonzini 已提交
1309
        QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
            if (addr - block->offset < block->length) {
                if (addr - block->offset + *size > block->length)
                    *size = block->length - addr + block->offset;
                return block->host + (addr - block->offset);
            }
        }

        fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
        abort();
    }
}

A
Anthony PERARD 已提交
1322 1323 1324 1325 1326
void qemu_put_ram_ptr(void *addr)
{
    trace_qemu_put_ram_ptr(addr);
}

M
Marcelo Tosatti 已提交
1327
int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
P
pbrook 已提交
1328
{
P
pbrook 已提交
1329 1330 1331
    RAMBlock *block;
    uint8_t *host = ptr;

1332
    if (xen_enabled()) {
J
Jan Kiszka 已提交
1333
        *ram_addr = xen_ram_addr_from_mapcache(ptr);
1334 1335 1336
        return 0;
    }

P
Paolo Bonzini 已提交
1337
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
J
Jun Nakajima 已提交
1338 1339 1340 1341
        /* This case append when the block is not mapped. */
        if (block->host == NULL) {
            continue;
        }
A
Alex Williamson 已提交
1342
        if (host - block->host < block->length) {
M
Marcelo Tosatti 已提交
1343 1344
            *ram_addr = block->offset + (host - block->host);
            return 0;
A
Alex Williamson 已提交
1345
        }
P
pbrook 已提交
1346
    }
J
Jun Nakajima 已提交
1347

M
Marcelo Tosatti 已提交
1348 1349
    return -1;
}
A
Alex Williamson 已提交
1350

M
Marcelo Tosatti 已提交
1351 1352 1353 1354 1355
/* Some of the softmmu routines need to translate from a host pointer
   (typically a TLB entry) back to a ram offset.  */
ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
{
    ram_addr_t ram_addr;
A
Alex Williamson 已提交
1356

M
Marcelo Tosatti 已提交
1357 1358 1359 1360 1361
    if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
        fprintf(stderr, "Bad ram pointer %p\n", ptr);
        abort();
    }
    return ram_addr;
P
pbrook 已提交
1362 1363
}

A
Avi Kivity 已提交
1364
static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
1365
                                    unsigned size)
1366 1367 1368 1369
{
#ifdef DEBUG_UNASSIGNED
    printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
#endif
1370
#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
1371
    cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, size);
1372 1373 1374 1375
#endif
    return 0;
}

A
Avi Kivity 已提交
1376
static void unassigned_mem_write(void *opaque, hwaddr addr,
1377
                                 uint64_t val, unsigned size)
1378 1379
{
#ifdef DEBUG_UNASSIGNED
1380
    printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
1381
#endif
1382
#if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
1383
    cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, size);
P
pbrook 已提交
1384
#endif
1385 1386
}

1387 1388 1389 1390 1391
static const MemoryRegionOps unassigned_mem_ops = {
    .read = unassigned_mem_read,
    .write = unassigned_mem_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
};
1392

A
Avi Kivity 已提交
1393
static uint64_t error_mem_read(void *opaque, hwaddr addr,
1394
                               unsigned size)
1395
{
1396
    abort();
1397 1398
}

A
Avi Kivity 已提交
1399
static void error_mem_write(void *opaque, hwaddr addr,
1400
                            uint64_t value, unsigned size)
1401
{
1402
    abort();
1403 1404
}

1405 1406 1407 1408
static const MemoryRegionOps error_mem_ops = {
    .read = error_mem_read,
    .write = error_mem_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1409 1410
};

1411 1412 1413 1414
static const MemoryRegionOps rom_mem_ops = {
    .read = error_mem_read,
    .write = unassigned_mem_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1415 1416
};

A
Avi Kivity 已提交
1417
static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
1418
                               uint64_t val, unsigned size)
1419
{
1420
    int dirty_flags;
1421
    dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
1422
    if (!(dirty_flags & CODE_DIRTY_FLAG)) {
1423
#if !defined(CONFIG_USER_ONLY)
1424
        tb_invalidate_phys_page_fast(ram_addr, size);
1425
        dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
1426
#endif
1427
    }
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
    switch (size) {
    case 1:
        stb_p(qemu_get_ram_ptr(ram_addr), val);
        break;
    case 2:
        stw_p(qemu_get_ram_ptr(ram_addr), val);
        break;
    case 4:
        stl_p(qemu_get_ram_ptr(ram_addr), val);
        break;
    default:
        abort();
1440
    }
B
bellard 已提交
1441
    dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
1442
    cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
B
bellard 已提交
1443 1444 1445
    /* we remove the notdirty callback only if the code has been
       flushed */
    if (dirty_flags == 0xff)
P
pbrook 已提交
1446
        tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
1447 1448
}

1449 1450 1451 1452
static const MemoryRegionOps notdirty_mem_ops = {
    .read = error_mem_read,
    .write = notdirty_mem_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1453 1454
};

P
pbrook 已提交
1455
/* Generate a debug exception if a watchpoint has been hit.  */
1456
static void check_watchpoint(int offset, int len_mask, int flags)
P
pbrook 已提交
1457
{
1458
    CPUArchState *env = cpu_single_env;
1459
    target_ulong pc, cs_base;
P
pbrook 已提交
1460
    target_ulong vaddr;
1461
    CPUWatchpoint *wp;
1462
    int cpu_flags;
P
pbrook 已提交
1463

1464 1465 1466 1467 1468 1469 1470
    if (env->watchpoint_hit) {
        /* We re-entered the check after replacing the TB. Now raise
         * the debug interrupt so that is will trigger after the
         * current instruction. */
        cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
        return;
    }
P
pbrook 已提交
1471
    vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
B
Blue Swirl 已提交
1472
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
1473 1474
        if ((vaddr == (wp->vaddr & len_mask) ||
             (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
1475 1476 1477
            wp->flags |= BP_WATCHPOINT_HIT;
            if (!env->watchpoint_hit) {
                env->watchpoint_hit = wp;
B
Blue Swirl 已提交
1478
                tb_check_watchpoint(env);
1479 1480
                if (wp->flags & BP_STOP_BEFORE_ACCESS) {
                    env->exception_index = EXCP_DEBUG;
1481
                    cpu_loop_exit(env);
1482 1483 1484
                } else {
                    cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
                    tb_gen_code(env, pc, cs_base, cpu_flags, 1);
1485
                    cpu_resume_from_signal(env, NULL);
1486
                }
1487
            }
1488 1489
        } else {
            wp->flags &= ~BP_WATCHPOINT_HIT;
P
pbrook 已提交
1490 1491 1492 1493
        }
    }
}

1494 1495 1496
/* Watchpoint access routines.  Watchpoints are inserted using TLB tricks,
   so these check for a hit then pass through to the normal out-of-line
   phys routines.  */
A
Avi Kivity 已提交
1497
static uint64_t watch_mem_read(void *opaque, hwaddr addr,
1498
                               unsigned size)
1499
{
1500 1501 1502 1503 1504 1505 1506
    check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_READ);
    switch (size) {
    case 1: return ldub_phys(addr);
    case 2: return lduw_phys(addr);
    case 4: return ldl_phys(addr);
    default: abort();
    }
1507 1508
}

A
Avi Kivity 已提交
1509
static void watch_mem_write(void *opaque, hwaddr addr,
1510
                            uint64_t val, unsigned size)
1511
{
1512 1513
    check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE);
    switch (size) {
1514 1515 1516 1517 1518 1519 1520 1521 1522
    case 1:
        stb_phys(addr, val);
        break;
    case 2:
        stw_phys(addr, val);
        break;
    case 4:
        stl_phys(addr, val);
        break;
1523 1524
    default: abort();
    }
1525 1526
}

1527 1528 1529 1530
static const MemoryRegionOps watch_mem_ops = {
    .read = watch_mem_read,
    .write = watch_mem_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1531 1532
};

A
Avi Kivity 已提交
1533
static uint64_t subpage_read(void *opaque, hwaddr addr,
1534
                             unsigned len)
1535
{
1536
    subpage_t *mmio = opaque;
R
Richard Henderson 已提交
1537
    unsigned int idx = SUBPAGE_IDX(addr);
1538
    MemoryRegionSection *section;
1539 1540 1541 1542 1543
#if defined(DEBUG_SUBPAGE)
    printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
           mmio, len, addr, idx);
#endif

1544 1545 1546 1547
    section = &phys_sections[mmio->sub_section[idx]];
    addr += mmio->base;
    addr -= section->offset_within_address_space;
    addr += section->offset_within_region;
1548
    return io_mem_read(section->mr, addr, len);
1549 1550
}

A
Avi Kivity 已提交
1551
static void subpage_write(void *opaque, hwaddr addr,
1552
                          uint64_t value, unsigned len)
1553
{
1554
    subpage_t *mmio = opaque;
R
Richard Henderson 已提交
1555
    unsigned int idx = SUBPAGE_IDX(addr);
1556
    MemoryRegionSection *section;
1557
#if defined(DEBUG_SUBPAGE)
1558 1559
    printf("%s: subpage %p len %d addr " TARGET_FMT_plx
           " idx %d value %"PRIx64"\n",
R
Richard Henderson 已提交
1560
           __func__, mmio, len, addr, idx, value);
1561
#endif
R
Richard Henderson 已提交
1562

1563 1564 1565 1566
    section = &phys_sections[mmio->sub_section[idx]];
    addr += mmio->base;
    addr -= section->offset_within_address_space;
    addr += section->offset_within_region;
1567
    io_mem_write(section->mr, addr, value, len);
1568 1569
}

1570 1571 1572 1573
static const MemoryRegionOps subpage_ops = {
    .read = subpage_read,
    .write = subpage_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1574 1575
};

A
Avi Kivity 已提交
1576
static uint64_t subpage_ram_read(void *opaque, hwaddr addr,
1577
                                 unsigned size)
1578 1579 1580
{
    ram_addr_t raddr = addr;
    void *ptr = qemu_get_ram_ptr(raddr);
1581 1582 1583 1584 1585 1586
    switch (size) {
    case 1: return ldub_p(ptr);
    case 2: return lduw_p(ptr);
    case 4: return ldl_p(ptr);
    default: abort();
    }
1587 1588
}

A
Avi Kivity 已提交
1589
static void subpage_ram_write(void *opaque, hwaddr addr,
1590
                              uint64_t value, unsigned size)
1591 1592 1593
{
    ram_addr_t raddr = addr;
    void *ptr = qemu_get_ram_ptr(raddr);
1594 1595 1596 1597 1598 1599
    switch (size) {
    case 1: return stb_p(ptr, value);
    case 2: return stw_p(ptr, value);
    case 4: return stl_p(ptr, value);
    default: abort();
    }
1600 1601
}

1602 1603 1604 1605
static const MemoryRegionOps subpage_ram_ops = {
    .read = subpage_ram_read,
    .write = subpage_ram_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1606 1607
};

A
Anthony Liguori 已提交
1608
static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
1609
                             uint16_t section)
1610 1611 1612 1613 1614 1615 1616 1617
{
    int idx, eidx;

    if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
        return -1;
    idx = SUBPAGE_IDX(start);
    eidx = SUBPAGE_IDX(end);
#if defined(DEBUG_SUBPAGE)
1618
    printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
1619 1620
           mmio, start, end, idx, eidx, memory);
#endif
1621 1622 1623 1624
    if (memory_region_is_ram(phys_sections[section].mr)) {
        MemoryRegionSection new_section = phys_sections[section];
        new_section.mr = &io_mem_subpage_ram;
        section = phys_section_add(&new_section);
1625
    }
1626
    for (; idx <= eidx; idx++) {
1627
        mmio->sub_section[idx] = section;
1628 1629 1630 1631 1632
    }

    return 0;
}

A
Avi Kivity 已提交
1633
static subpage_t *subpage_init(hwaddr base)
1634
{
A
Anthony Liguori 已提交
1635
    subpage_t *mmio;
1636

1637
    mmio = g_malloc0(sizeof(subpage_t));
1638 1639

    mmio->base = base;
1640 1641
    memory_region_init_io(&mmio->iomem, &subpage_ops, mmio,
                          "subpage", TARGET_PAGE_SIZE);
A
Avi Kivity 已提交
1642
    mmio->iomem.subpage = true;
1643
#if defined(DEBUG_SUBPAGE)
1644 1645
    printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
           mmio, base, TARGET_PAGE_SIZE, subpage_memory);
1646
#endif
1647
    subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, phys_section_unassigned);
1648 1649 1650 1651

    return mmio;
}

1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
static uint16_t dummy_section(MemoryRegion *mr)
{
    MemoryRegionSection section = {
        .mr = mr,
        .offset_within_address_space = 0,
        .offset_within_region = 0,
        .size = UINT64_MAX,
    };

    return phys_section_add(&section);
}

A
Avi Kivity 已提交
1664
MemoryRegion *iotlb_to_region(hwaddr index)
1665
{
1666
    return phys_sections[index & ~TARGET_PAGE_MASK].mr;
1667 1668
}

A
Avi Kivity 已提交
1669 1670
static void io_mem_init(void)
{
1671 1672 1673 1674 1675 1676
    memory_region_init_io(&io_mem_ram, &error_mem_ops, NULL, "ram", UINT64_MAX);
    memory_region_init_io(&io_mem_rom, &rom_mem_ops, NULL, "rom", UINT64_MAX);
    memory_region_init_io(&io_mem_unassigned, &unassigned_mem_ops, NULL,
                          "unassigned", UINT64_MAX);
    memory_region_init_io(&io_mem_notdirty, &notdirty_mem_ops, NULL,
                          "notdirty", UINT64_MAX);
1677 1678
    memory_region_init_io(&io_mem_subpage_ram, &subpage_ram_ops, NULL,
                          "subpage-ram", UINT64_MAX);
1679 1680
    memory_region_init_io(&io_mem_watch, &watch_mem_ops, NULL,
                          "watch", UINT64_MAX);
A
Avi Kivity 已提交
1681 1682
}

A
Avi Kivity 已提交
1683 1684 1685 1686 1687 1688 1689 1690
static void mem_begin(MemoryListener *listener)
{
    AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);

    destroy_all_mappings(d);
    d->phys_map.ptr = PHYS_MAP_NODE_NIL;
}

1691 1692
static void core_begin(MemoryListener *listener)
{
1693 1694
    phys_sections_clear();
    phys_section_unassigned = dummy_section(&io_mem_unassigned);
1695 1696 1697
    phys_section_notdirty = dummy_section(&io_mem_notdirty);
    phys_section_rom = dummy_section(&io_mem_rom);
    phys_section_watch = dummy_section(&io_mem_watch);
1698 1699
}

1700
static void tcg_commit(MemoryListener *listener)
1701
{
1702
    CPUArchState *env;
1703 1704 1705 1706 1707 1708 1709

    /* since each CPU stores ram addresses in its TLB cache, we must
       reset the modified entries */
    /* XXX: slow ! */
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
        tlb_flush(env, 1);
    }
1710 1711
}

1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
static void core_log_global_start(MemoryListener *listener)
{
    cpu_physical_memory_set_dirty_tracking(1);
}

static void core_log_global_stop(MemoryListener *listener)
{
    cpu_physical_memory_set_dirty_tracking(0);
}

1722 1723 1724
static void io_region_add(MemoryListener *listener,
                          MemoryRegionSection *section)
{
A
Avi Kivity 已提交
1725 1726 1727 1728 1729
    MemoryRegionIORange *mrio = g_new(MemoryRegionIORange, 1);

    mrio->mr = section->mr;
    mrio->offset = section->offset_within_region;
    iorange_init(&mrio->iorange, &memory_region_iorange_ops,
1730
                 section->offset_within_address_space, section->size);
A
Avi Kivity 已提交
1731
    ioport_register(&mrio->iorange);
1732 1733 1734 1735 1736 1737 1738 1739
}

static void io_region_del(MemoryListener *listener,
                          MemoryRegionSection *section)
{
    isa_unassign_ioport(section->offset_within_address_space, section->size);
}

1740
static MemoryListener core_memory_listener = {
1741
    .begin = core_begin,
1742 1743
    .log_global_start = core_log_global_start,
    .log_global_stop = core_log_global_stop,
A
Avi Kivity 已提交
1744
    .priority = 1,
1745 1746
};

1747 1748 1749 1750 1751 1752
static MemoryListener io_memory_listener = {
    .region_add = io_region_add,
    .region_del = io_region_del,
    .priority = 0,
};

1753 1754 1755 1756
static MemoryListener tcg_memory_listener = {
    .commit = tcg_commit,
};

A
Avi Kivity 已提交
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
void address_space_init_dispatch(AddressSpace *as)
{
    AddressSpaceDispatch *d = g_new(AddressSpaceDispatch, 1);

    d->phys_map  = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .is_leaf = 0 };
    d->listener = (MemoryListener) {
        .begin = mem_begin,
        .region_add = mem_add,
        .region_nop = mem_add,
        .priority = 0,
    };
    as->dispatch = d;
    memory_listener_register(&d->listener, as);
}

A
Avi Kivity 已提交
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
void address_space_destroy_dispatch(AddressSpace *as)
{
    AddressSpaceDispatch *d = as->dispatch;

    memory_listener_unregister(&d->listener);
    destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
    g_free(d);
    as->dispatch = NULL;
}

A
Avi Kivity 已提交
1782 1783
static void memory_map_init(void)
{
1784
    system_memory = g_malloc(sizeof(*system_memory));
A
Avi Kivity 已提交
1785
    memory_region_init(system_memory, "system", INT64_MAX);
1786 1787
    address_space_init(&address_space_memory, system_memory);
    address_space_memory.name = "memory";
1788

1789
    system_io = g_malloc(sizeof(*system_io));
1790
    memory_region_init(system_io, "io", 65536);
1791 1792
    address_space_init(&address_space_io, system_io);
    address_space_io.name = "I/O";
1793

1794 1795 1796
    memory_listener_register(&core_memory_listener, &address_space_memory);
    memory_listener_register(&io_memory_listener, &address_space_io);
    memory_listener_register(&tcg_memory_listener, &address_space_memory);
1797 1798 1799

    dma_context_init(&dma_context_memory, &address_space_memory,
                     NULL, NULL, NULL);
A
Avi Kivity 已提交
1800 1801 1802 1803 1804 1805 1806
}

MemoryRegion *get_system_memory(void)
{
    return system_memory;
}

1807 1808 1809 1810 1811
MemoryRegion *get_system_io(void)
{
    return system_io;
}

1812 1813
#endif /* !defined(CONFIG_USER_ONLY) */

B
bellard 已提交
1814 1815
/* physical memory access (slow version, mainly for debug) */
#if defined(CONFIG_USER_ONLY)
1816
int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
P
Paul Brook 已提交
1817
                        uint8_t *buf, int len, int is_write)
B
bellard 已提交
1818 1819 1820
{
    int l, flags;
    target_ulong page;
1821
    void * p;
B
bellard 已提交
1822 1823 1824 1825 1826 1827 1828 1829

    while (len > 0) {
        page = addr & TARGET_PAGE_MASK;
        l = (page + TARGET_PAGE_SIZE) - addr;
        if (l > len)
            l = len;
        flags = page_get_flags(page);
        if (!(flags & PAGE_VALID))
P
Paul Brook 已提交
1830
            return -1;
B
bellard 已提交
1831 1832
        if (is_write) {
            if (!(flags & PAGE_WRITE))
P
Paul Brook 已提交
1833
                return -1;
1834
            /* XXX: this code should not depend on lock_user */
A
aurel32 已提交
1835
            if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
P
Paul Brook 已提交
1836
                return -1;
A
aurel32 已提交
1837 1838
            memcpy(p, buf, l);
            unlock_user(p, addr, l);
B
bellard 已提交
1839 1840
        } else {
            if (!(flags & PAGE_READ))
P
Paul Brook 已提交
1841
                return -1;
1842
            /* XXX: this code should not depend on lock_user */
A
aurel32 已提交
1843
            if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
P
Paul Brook 已提交
1844
                return -1;
A
aurel32 已提交
1845
            memcpy(buf, p, l);
A
aurel32 已提交
1846
            unlock_user(p, addr, 0);
B
bellard 已提交
1847 1848 1849 1850 1851
        }
        len -= l;
        buf += l;
        addr += l;
    }
P
Paul Brook 已提交
1852
    return 0;
B
bellard 已提交
1853
}
B
bellard 已提交
1854

B
bellard 已提交
1855
#else
1856

A
Avi Kivity 已提交
1857 1858
static void invalidate_and_set_dirty(hwaddr addr,
                                     hwaddr length)
1859 1860 1861 1862 1863 1864 1865
{
    if (!cpu_physical_memory_is_dirty(addr)) {
        /* invalidate code */
        tb_invalidate_phys_page_range(addr, addr + length, 0);
        /* set dirty bit */
        cpu_physical_memory_set_dirty_flags(addr, (0xff & ~CODE_DIRTY_FLAG));
    }
1866
    xen_modified_memory(addr, length);
1867 1868
}

A
Avi Kivity 已提交
1869
void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
A
Avi Kivity 已提交
1870
                      int len, bool is_write)
B
bellard 已提交
1871
{
A
Avi Kivity 已提交
1872
    AddressSpaceDispatch *d = as->dispatch;
1873
    int l;
B
bellard 已提交
1874 1875
    uint8_t *ptr;
    uint32_t val;
A
Avi Kivity 已提交
1876
    hwaddr page;
1877
    MemoryRegionSection *section;
1878

B
bellard 已提交
1879 1880 1881 1882 1883
    while (len > 0) {
        page = addr & TARGET_PAGE_MASK;
        l = (page + TARGET_PAGE_SIZE) - addr;
        if (l > len)
            l = len;
A
Avi Kivity 已提交
1884
        section = phys_page_find(d, page >> TARGET_PAGE_BITS);
1885

B
bellard 已提交
1886
        if (is_write) {
1887
            if (!memory_region_is_ram(section->mr)) {
A
Avi Kivity 已提交
1888
                hwaddr addr1;
1889
                addr1 = memory_region_section_addr(section, addr);
B
bellard 已提交
1890 1891
                /* XXX: could force cpu_single_env to NULL to avoid
                   potential bugs */
1892
                if (l >= 4 && ((addr1 & 3) == 0)) {
B
bellard 已提交
1893
                    /* 32 bit write access */
B
bellard 已提交
1894
                    val = ldl_p(buf);
1895
                    io_mem_write(section->mr, addr1, val, 4);
B
bellard 已提交
1896
                    l = 4;
1897
                } else if (l >= 2 && ((addr1 & 1) == 0)) {
B
bellard 已提交
1898
                    /* 16 bit write access */
B
bellard 已提交
1899
                    val = lduw_p(buf);
1900
                    io_mem_write(section->mr, addr1, val, 2);
B
bellard 已提交
1901 1902
                    l = 2;
                } else {
B
bellard 已提交
1903
                    /* 8 bit write access */
B
bellard 已提交
1904
                    val = ldub_p(buf);
1905
                    io_mem_write(section->mr, addr1, val, 1);
B
bellard 已提交
1906 1907
                    l = 1;
                }
1908
            } else if (!section->readonly) {
1909
                ram_addr_t addr1;
1910
                addr1 = memory_region_get_ram_addr(section->mr)
1911
                    + memory_region_section_addr(section, addr);
B
bellard 已提交
1912
                /* RAM case */
P
pbrook 已提交
1913
                ptr = qemu_get_ram_ptr(addr1);
B
bellard 已提交
1914
                memcpy(ptr, buf, l);
1915
                invalidate_and_set_dirty(addr1, l);
A
Anthony PERARD 已提交
1916
                qemu_put_ram_ptr(ptr);
B
bellard 已提交
1917 1918
            }
        } else {
1919 1920
            if (!(memory_region_is_ram(section->mr) ||
                  memory_region_is_romd(section->mr))) {
A
Avi Kivity 已提交
1921
                hwaddr addr1;
B
bellard 已提交
1922
                /* I/O case */
1923
                addr1 = memory_region_section_addr(section, addr);
1924
                if (l >= 4 && ((addr1 & 3) == 0)) {
B
bellard 已提交
1925
                    /* 32 bit read access */
1926
                    val = io_mem_read(section->mr, addr1, 4);
B
bellard 已提交
1927
                    stl_p(buf, val);
B
bellard 已提交
1928
                    l = 4;
1929
                } else if (l >= 2 && ((addr1 & 1) == 0)) {
B
bellard 已提交
1930
                    /* 16 bit read access */
1931
                    val = io_mem_read(section->mr, addr1, 2);
B
bellard 已提交
1932
                    stw_p(buf, val);
B
bellard 已提交
1933 1934
                    l = 2;
                } else {
B
bellard 已提交
1935
                    /* 8 bit read access */
1936
                    val = io_mem_read(section->mr, addr1, 1);
B
bellard 已提交
1937
                    stb_p(buf, val);
B
bellard 已提交
1938 1939 1940 1941
                    l = 1;
                }
            } else {
                /* RAM case */
1942
                ptr = qemu_get_ram_ptr(section->mr->ram_addr
1943 1944
                                       + memory_region_section_addr(section,
                                                                    addr));
1945
                memcpy(buf, ptr, l);
A
Anthony PERARD 已提交
1946
                qemu_put_ram_ptr(ptr);
B
bellard 已提交
1947 1948 1949 1950 1951 1952 1953
            }
        }
        len -= l;
        buf += l;
        addr += l;
    }
}
B
bellard 已提交
1954

A
Avi Kivity 已提交
1955
void address_space_write(AddressSpace *as, hwaddr addr,
A
Avi Kivity 已提交
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
                         const uint8_t *buf, int len)
{
    address_space_rw(as, addr, (uint8_t *)buf, len, true);
}

/**
 * address_space_read: read from an address space.
 *
 * @as: #AddressSpace to be accessed
 * @addr: address within that address space
 * @buf: buffer with the data transferred
 */
A
Avi Kivity 已提交
1968
void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
A
Avi Kivity 已提交
1969 1970 1971 1972 1973
{
    address_space_rw(as, addr, buf, len, false);
}


A
Avi Kivity 已提交
1974
void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
A
Avi Kivity 已提交
1975 1976 1977 1978 1979
                            int len, int is_write)
{
    return address_space_rw(&address_space_memory, addr, buf, len, is_write);
}

B
bellard 已提交
1980
/* used for ROM loading : can write in RAM and ROM */
A
Avi Kivity 已提交
1981
void cpu_physical_memory_write_rom(hwaddr addr,
B
bellard 已提交
1982 1983
                                   const uint8_t *buf, int len)
{
A
Avi Kivity 已提交
1984
    AddressSpaceDispatch *d = address_space_memory.dispatch;
B
bellard 已提交
1985 1986
    int l;
    uint8_t *ptr;
A
Avi Kivity 已提交
1987
    hwaddr page;
1988
    MemoryRegionSection *section;
1989

B
bellard 已提交
1990 1991 1992 1993 1994
    while (len > 0) {
        page = addr & TARGET_PAGE_MASK;
        l = (page + TARGET_PAGE_SIZE) - addr;
        if (l > len)
            l = len;
A
Avi Kivity 已提交
1995
        section = phys_page_find(d, page >> TARGET_PAGE_BITS);
1996

1997 1998
        if (!(memory_region_is_ram(section->mr) ||
              memory_region_is_romd(section->mr))) {
B
bellard 已提交
1999 2000 2001
            /* do nothing */
        } else {
            unsigned long addr1;
2002
            addr1 = memory_region_get_ram_addr(section->mr)
2003
                + memory_region_section_addr(section, addr);
B
bellard 已提交
2004
            /* ROM/RAM case */
P
pbrook 已提交
2005
            ptr = qemu_get_ram_ptr(addr1);
B
bellard 已提交
2006
            memcpy(ptr, buf, l);
2007
            invalidate_and_set_dirty(addr1, l);
A
Anthony PERARD 已提交
2008
            qemu_put_ram_ptr(ptr);
B
bellard 已提交
2009 2010 2011 2012 2013 2014 2015
        }
        len -= l;
        buf += l;
        addr += l;
    }
}

2016 2017
typedef struct {
    void *buffer;
A
Avi Kivity 已提交
2018 2019
    hwaddr addr;
    hwaddr len;
2020 2021 2022 2023
} BounceBuffer;

static BounceBuffer bounce;

2024 2025 2026
typedef struct MapClient {
    void *opaque;
    void (*callback)(void *opaque);
B
Blue Swirl 已提交
2027
    QLIST_ENTRY(MapClient) link;
2028 2029
} MapClient;

B
Blue Swirl 已提交
2030 2031
static QLIST_HEAD(map_client_list, MapClient) map_client_list
    = QLIST_HEAD_INITIALIZER(map_client_list);
2032 2033 2034

void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
{
2035
    MapClient *client = g_malloc(sizeof(*client));
2036 2037 2038

    client->opaque = opaque;
    client->callback = callback;
B
Blue Swirl 已提交
2039
    QLIST_INSERT_HEAD(&map_client_list, client, link);
2040 2041 2042
    return client;
}

B
Blue Swirl 已提交
2043
static void cpu_unregister_map_client(void *_client)
2044 2045 2046
{
    MapClient *client = (MapClient *)_client;

B
Blue Swirl 已提交
2047
    QLIST_REMOVE(client, link);
2048
    g_free(client);
2049 2050 2051 2052 2053 2054
}

static void cpu_notify_map_clients(void)
{
    MapClient *client;

B
Blue Swirl 已提交
2055 2056
    while (!QLIST_EMPTY(&map_client_list)) {
        client = QLIST_FIRST(&map_client_list);
2057
        client->callback(client->opaque);
2058
        cpu_unregister_map_client(client);
2059 2060 2061
    }
}

2062 2063 2064 2065
/* Map a physical memory region into a host virtual address.
 * May map a subset of the requested range, given by and returned in *plen.
 * May return NULL if resources needed to perform the mapping are exhausted.
 * Use only for reads OR writes - not for read-modify-write operations.
2066 2067
 * Use cpu_register_map_client() to know when retrying the map operation is
 * likely to succeed.
2068
 */
A
Avi Kivity 已提交
2069
void *address_space_map(AddressSpace *as,
A
Avi Kivity 已提交
2070 2071
                        hwaddr addr,
                        hwaddr *plen,
A
Avi Kivity 已提交
2072
                        bool is_write)
2073
{
A
Avi Kivity 已提交
2074
    AddressSpaceDispatch *d = as->dispatch;
A
Avi Kivity 已提交
2075 2076
    hwaddr len = *plen;
    hwaddr todo = 0;
2077
    int l;
A
Avi Kivity 已提交
2078
    hwaddr page;
2079
    MemoryRegionSection *section;
2080
    ram_addr_t raddr = RAM_ADDR_MAX;
2081 2082
    ram_addr_t rlen;
    void *ret;
2083 2084 2085 2086 2087 2088

    while (len > 0) {
        page = addr & TARGET_PAGE_MASK;
        l = (page + TARGET_PAGE_SIZE) - addr;
        if (l > len)
            l = len;
A
Avi Kivity 已提交
2089
        section = phys_page_find(d, page >> TARGET_PAGE_BITS);
2090

2091
        if (!(memory_region_is_ram(section->mr) && !section->readonly)) {
2092
            if (todo || bounce.buffer) {
2093 2094 2095 2096 2097 2098
                break;
            }
            bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
            bounce.addr = addr;
            bounce.len = l;
            if (!is_write) {
A
Avi Kivity 已提交
2099
                address_space_read(as, addr, bounce.buffer, l);
2100
            }
2101 2102 2103

            *plen = l;
            return bounce.buffer;
2104
        }
2105
        if (!todo) {
2106
            raddr = memory_region_get_ram_addr(section->mr)
2107
                + memory_region_section_addr(section, addr);
2108
        }
2109 2110 2111

        len -= l;
        addr += l;
2112
        todo += l;
2113
    }
2114 2115 2116 2117
    rlen = todo;
    ret = qemu_ram_ptr_length(raddr, &rlen);
    *plen = rlen;
    return ret;
2118 2119
}

A
Avi Kivity 已提交
2120
/* Unmaps a memory region previously mapped by address_space_map().
2121 2122 2123
 * Will also mark the memory as dirty if is_write == 1.  access_len gives
 * the amount of memory that was actually read or written by the caller.
 */
A
Avi Kivity 已提交
2124 2125
void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
                         int is_write, hwaddr access_len)
2126 2127 2128
{
    if (buffer != bounce.buffer) {
        if (is_write) {
M
Marcelo Tosatti 已提交
2129
            ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
2130 2131 2132 2133 2134
            while (access_len) {
                unsigned l;
                l = TARGET_PAGE_SIZE;
                if (l > access_len)
                    l = access_len;
2135
                invalidate_and_set_dirty(addr1, l);
2136 2137 2138 2139
                addr1 += l;
                access_len -= l;
            }
        }
2140
        if (xen_enabled()) {
J
Jan Kiszka 已提交
2141
            xen_invalidate_map_cache_entry(buffer);
A
Anthony PERARD 已提交
2142
        }
2143 2144 2145
        return;
    }
    if (is_write) {
A
Avi Kivity 已提交
2146
        address_space_write(as, bounce.addr, bounce.buffer, access_len);
2147
    }
2148
    qemu_vfree(bounce.buffer);
2149
    bounce.buffer = NULL;
2150
    cpu_notify_map_clients();
2151
}
B
bellard 已提交
2152

A
Avi Kivity 已提交
2153 2154
void *cpu_physical_memory_map(hwaddr addr,
                              hwaddr *plen,
A
Avi Kivity 已提交
2155 2156 2157 2158 2159
                              int is_write)
{
    return address_space_map(&address_space_memory, addr, plen, is_write);
}

A
Avi Kivity 已提交
2160 2161
void cpu_physical_memory_unmap(void *buffer, hwaddr len,
                               int is_write, hwaddr access_len)
A
Avi Kivity 已提交
2162 2163 2164 2165
{
    return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
}

B
bellard 已提交
2166
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2167
static inline uint32_t ldl_phys_internal(hwaddr addr,
2168
                                         enum device_endian endian)
B
bellard 已提交
2169 2170 2171
{
    uint8_t *ptr;
    uint32_t val;
2172
    MemoryRegionSection *section;
B
bellard 已提交
2173

A
Avi Kivity 已提交
2174
    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
2175

2176 2177
    if (!(memory_region_is_ram(section->mr) ||
          memory_region_is_romd(section->mr))) {
B
bellard 已提交
2178
        /* I/O case */
2179
        addr = memory_region_section_addr(section, addr);
2180
        val = io_mem_read(section->mr, addr, 4);
2181 2182 2183 2184 2185 2186 2187 2188 2189
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap32(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap32(val);
        }
#endif
B
bellard 已提交
2190 2191
    } else {
        /* RAM case */
2192
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2193
                                & TARGET_PAGE_MASK)
2194
                               + memory_region_section_addr(section, addr));
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            val = ldl_le_p(ptr);
            break;
        case DEVICE_BIG_ENDIAN:
            val = ldl_be_p(ptr);
            break;
        default:
            val = ldl_p(ptr);
            break;
        }
B
bellard 已提交
2206 2207 2208 2209
    }
    return val;
}

A
Avi Kivity 已提交
2210
uint32_t ldl_phys(hwaddr addr)
2211 2212 2213 2214
{
    return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2215
uint32_t ldl_le_phys(hwaddr addr)
2216 2217 2218 2219
{
    return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2220
uint32_t ldl_be_phys(hwaddr addr)
2221 2222 2223 2224
{
    return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2225
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2226
static inline uint64_t ldq_phys_internal(hwaddr addr,
2227
                                         enum device_endian endian)
B
bellard 已提交
2228 2229 2230
{
    uint8_t *ptr;
    uint64_t val;
2231
    MemoryRegionSection *section;
B
bellard 已提交
2232

A
Avi Kivity 已提交
2233
    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
2234

2235 2236
    if (!(memory_region_is_ram(section->mr) ||
          memory_region_is_romd(section->mr))) {
B
bellard 已提交
2237
        /* I/O case */
2238
        addr = memory_region_section_addr(section, addr);
2239 2240 2241

        /* XXX This is broken when device endian != cpu endian.
               Fix and add "endian" variable check */
B
bellard 已提交
2242
#ifdef TARGET_WORDS_BIGENDIAN
2243 2244
        val = io_mem_read(section->mr, addr, 4) << 32;
        val |= io_mem_read(section->mr, addr + 4, 4);
B
bellard 已提交
2245
#else
2246 2247
        val = io_mem_read(section->mr, addr, 4);
        val |= io_mem_read(section->mr, addr + 4, 4) << 32;
B
bellard 已提交
2248 2249 2250
#endif
    } else {
        /* RAM case */
2251
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2252
                                & TARGET_PAGE_MASK)
2253
                               + memory_region_section_addr(section, addr));
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            val = ldq_le_p(ptr);
            break;
        case DEVICE_BIG_ENDIAN:
            val = ldq_be_p(ptr);
            break;
        default:
            val = ldq_p(ptr);
            break;
        }
B
bellard 已提交
2265 2266 2267 2268
    }
    return val;
}

A
Avi Kivity 已提交
2269
uint64_t ldq_phys(hwaddr addr)
2270 2271 2272 2273
{
    return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2274
uint64_t ldq_le_phys(hwaddr addr)
2275 2276 2277 2278
{
    return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2279
uint64_t ldq_be_phys(hwaddr addr)
2280 2281 2282 2283
{
    return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2284
/* XXX: optimize */
A
Avi Kivity 已提交
2285
uint32_t ldub_phys(hwaddr addr)
B
bellard 已提交
2286 2287 2288 2289 2290 2291
{
    uint8_t val;
    cpu_physical_memory_read(addr, &val, 1);
    return val;
}

2292
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2293
static inline uint32_t lduw_phys_internal(hwaddr addr,
2294
                                          enum device_endian endian)
B
bellard 已提交
2295
{
2296 2297
    uint8_t *ptr;
    uint64_t val;
2298
    MemoryRegionSection *section;
2299

A
Avi Kivity 已提交
2300
    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
2301

2302 2303
    if (!(memory_region_is_ram(section->mr) ||
          memory_region_is_romd(section->mr))) {
2304
        /* I/O case */
2305
        addr = memory_region_section_addr(section, addr);
2306
        val = io_mem_read(section->mr, addr, 2);
2307 2308 2309 2310 2311 2312 2313 2314 2315
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap16(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap16(val);
        }
#endif
2316 2317
    } else {
        /* RAM case */
2318
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2319
                                & TARGET_PAGE_MASK)
2320
                               + memory_region_section_addr(section, addr));
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            val = lduw_le_p(ptr);
            break;
        case DEVICE_BIG_ENDIAN:
            val = lduw_be_p(ptr);
            break;
        default:
            val = lduw_p(ptr);
            break;
        }
2332 2333
    }
    return val;
B
bellard 已提交
2334 2335
}

A
Avi Kivity 已提交
2336
uint32_t lduw_phys(hwaddr addr)
2337 2338 2339 2340
{
    return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2341
uint32_t lduw_le_phys(hwaddr addr)
2342 2343 2344 2345
{
    return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2346
uint32_t lduw_be_phys(hwaddr addr)
2347 2348 2349 2350
{
    return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2351 2352 2353
/* warning: addr must be aligned. The ram page is not masked as dirty
   and the code inside is not invalidated. It is useful if the dirty
   bits are used to track modified PTEs */
A
Avi Kivity 已提交
2354
void stl_phys_notdirty(hwaddr addr, uint32_t val)
B
bellard 已提交
2355 2356
{
    uint8_t *ptr;
2357
    MemoryRegionSection *section;
B
bellard 已提交
2358

A
Avi Kivity 已提交
2359
    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
2360

2361
    if (!memory_region_is_ram(section->mr) || section->readonly) {
2362
        addr = memory_region_section_addr(section, addr);
2363
        if (memory_region_is_ram(section->mr)) {
2364
            section = &phys_sections[phys_section_rom];
2365
        }
2366
        io_mem_write(section->mr, addr, val, 4);
B
bellard 已提交
2367
    } else {
2368
        unsigned long addr1 = (memory_region_get_ram_addr(section->mr)
2369
                               & TARGET_PAGE_MASK)
2370
            + memory_region_section_addr(section, addr);
P
pbrook 已提交
2371
        ptr = qemu_get_ram_ptr(addr1);
B
bellard 已提交
2372
        stl_p(ptr, val);
A
aliguori 已提交
2373 2374 2375 2376 2377 2378

        if (unlikely(in_migration)) {
            if (!cpu_physical_memory_is_dirty(addr1)) {
                /* invalidate code */
                tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
                /* set dirty bit */
2379 2380
                cpu_physical_memory_set_dirty_flags(
                    addr1, (0xff & ~CODE_DIRTY_FLAG));
A
aliguori 已提交
2381 2382
            }
        }
B
bellard 已提交
2383 2384 2385
    }
}

A
Avi Kivity 已提交
2386
void stq_phys_notdirty(hwaddr addr, uint64_t val)
J
j_mayer 已提交
2387 2388
{
    uint8_t *ptr;
2389
    MemoryRegionSection *section;
J
j_mayer 已提交
2390

A
Avi Kivity 已提交
2391
    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
2392

2393
    if (!memory_region_is_ram(section->mr) || section->readonly) {
2394
        addr = memory_region_section_addr(section, addr);
2395
        if (memory_region_is_ram(section->mr)) {
2396
            section = &phys_sections[phys_section_rom];
2397
        }
J
j_mayer 已提交
2398
#ifdef TARGET_WORDS_BIGENDIAN
2399 2400
        io_mem_write(section->mr, addr, val >> 32, 4);
        io_mem_write(section->mr, addr + 4, (uint32_t)val, 4);
J
j_mayer 已提交
2401
#else
2402 2403
        io_mem_write(section->mr, addr, (uint32_t)val, 4);
        io_mem_write(section->mr, addr + 4, val >> 32, 4);
J
j_mayer 已提交
2404 2405
#endif
    } else {
2406
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2407
                                & TARGET_PAGE_MASK)
2408
                               + memory_region_section_addr(section, addr));
J
j_mayer 已提交
2409 2410 2411 2412
        stq_p(ptr, val);
    }
}

B
bellard 已提交
2413
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2414
static inline void stl_phys_internal(hwaddr addr, uint32_t val,
2415
                                     enum device_endian endian)
B
bellard 已提交
2416 2417
{
    uint8_t *ptr;
2418
    MemoryRegionSection *section;
B
bellard 已提交
2419

A
Avi Kivity 已提交
2420
    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
2421

2422
    if (!memory_region_is_ram(section->mr) || section->readonly) {
2423
        addr = memory_region_section_addr(section, addr);
2424
        if (memory_region_is_ram(section->mr)) {
2425
            section = &phys_sections[phys_section_rom];
2426
        }
2427 2428 2429 2430 2431 2432 2433 2434 2435
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap32(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap32(val);
        }
#endif
2436
        io_mem_write(section->mr, addr, val, 4);
B
bellard 已提交
2437 2438
    } else {
        unsigned long addr1;
2439
        addr1 = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
2440
            + memory_region_section_addr(section, addr);
B
bellard 已提交
2441
        /* RAM case */
P
pbrook 已提交
2442
        ptr = qemu_get_ram_ptr(addr1);
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            stl_le_p(ptr, val);
            break;
        case DEVICE_BIG_ENDIAN:
            stl_be_p(ptr, val);
            break;
        default:
            stl_p(ptr, val);
            break;
        }
2454
        invalidate_and_set_dirty(addr1, 4);
B
bellard 已提交
2455 2456 2457
    }
}

A
Avi Kivity 已提交
2458
void stl_phys(hwaddr addr, uint32_t val)
2459 2460 2461 2462
{
    stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2463
void stl_le_phys(hwaddr addr, uint32_t val)
2464 2465 2466 2467
{
    stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2468
void stl_be_phys(hwaddr addr, uint32_t val)
2469 2470 2471 2472
{
    stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2473
/* XXX: optimize */
A
Avi Kivity 已提交
2474
void stb_phys(hwaddr addr, uint32_t val)
B
bellard 已提交
2475 2476 2477 2478 2479
{
    uint8_t v = val;
    cpu_physical_memory_write(addr, &v, 1);
}

2480
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2481
static inline void stw_phys_internal(hwaddr addr, uint32_t val,
2482
                                     enum device_endian endian)
B
bellard 已提交
2483
{
2484
    uint8_t *ptr;
2485
    MemoryRegionSection *section;
2486

A
Avi Kivity 已提交
2487
    section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
2488

2489
    if (!memory_region_is_ram(section->mr) || section->readonly) {
2490
        addr = memory_region_section_addr(section, addr);
2491
        if (memory_region_is_ram(section->mr)) {
2492
            section = &phys_sections[phys_section_rom];
2493
        }
2494 2495 2496 2497 2498 2499 2500 2501 2502
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap16(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap16(val);
        }
#endif
2503
        io_mem_write(section->mr, addr, val, 2);
2504 2505
    } else {
        unsigned long addr1;
2506
        addr1 = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
2507
            + memory_region_section_addr(section, addr);
2508 2509
        /* RAM case */
        ptr = qemu_get_ram_ptr(addr1);
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            stw_le_p(ptr, val);
            break;
        case DEVICE_BIG_ENDIAN:
            stw_be_p(ptr, val);
            break;
        default:
            stw_p(ptr, val);
            break;
        }
2521
        invalidate_and_set_dirty(addr1, 2);
2522
    }
B
bellard 已提交
2523 2524
}

A
Avi Kivity 已提交
2525
void stw_phys(hwaddr addr, uint32_t val)
2526 2527 2528 2529
{
    stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2530
void stw_le_phys(hwaddr addr, uint32_t val)
2531 2532 2533 2534
{
    stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2535
void stw_be_phys(hwaddr addr, uint32_t val)
2536 2537 2538 2539
{
    stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2540
/* XXX: optimize */
A
Avi Kivity 已提交
2541
void stq_phys(hwaddr addr, uint64_t val)
B
bellard 已提交
2542 2543
{
    val = tswap64(val);
2544
    cpu_physical_memory_write(addr, &val, 8);
B
bellard 已提交
2545 2546
}

A
Avi Kivity 已提交
2547
void stq_le_phys(hwaddr addr, uint64_t val)
2548 2549 2550 2551 2552
{
    val = cpu_to_le64(val);
    cpu_physical_memory_write(addr, &val, 8);
}

A
Avi Kivity 已提交
2553
void stq_be_phys(hwaddr addr, uint64_t val)
2554 2555 2556 2557 2558
{
    val = cpu_to_be64(val);
    cpu_physical_memory_write(addr, &val, 8);
}

2559
/* virtual memory access for debug (includes writing to ROM) */
2560
int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
2561
                        uint8_t *buf, int len, int is_write)
B
bellard 已提交
2562 2563
{
    int l;
A
Avi Kivity 已提交
2564
    hwaddr phys_addr;
2565
    target_ulong page;
B
bellard 已提交
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575

    while (len > 0) {
        page = addr & TARGET_PAGE_MASK;
        phys_addr = cpu_get_phys_page_debug(env, page);
        /* if no physical page mapped, return an error */
        if (phys_addr == -1)
            return -1;
        l = (page + TARGET_PAGE_SIZE) - addr;
        if (l > len)
            l = len;
2576 2577 2578 2579 2580
        phys_addr += (addr & ~TARGET_PAGE_MASK);
        if (is_write)
            cpu_physical_memory_write_rom(phys_addr, buf, l);
        else
            cpu_physical_memory_rw(phys_addr, buf, l, is_write);
B
bellard 已提交
2581 2582 2583 2584 2585 2586
        len -= l;
        buf += l;
        addr += l;
    }
    return 0;
}
P
Paul Brook 已提交
2587
#endif
B
bellard 已提交
2588

2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
#if !defined(CONFIG_USER_ONLY)

/*
 * A helper function for the _utterly broken_ virtio device model to find out if
 * it's running on a big endian machine. Don't do this at home kids!
 */
bool virtio_is_big_endian(void);
bool virtio_is_big_endian(void)
{
#if defined(TARGET_WORDS_BIGENDIAN)
    return true;
#else
    return false;
#endif
}

#endif

2607
#ifndef CONFIG_USER_ONLY
A
Avi Kivity 已提交
2608
bool cpu_physical_memory_is_io(hwaddr phys_addr)
2609 2610 2611
{
    MemoryRegionSection *section;

A
Avi Kivity 已提交
2612 2613
    section = phys_page_find(address_space_memory.dispatch,
                             phys_addr >> TARGET_PAGE_BITS);
2614 2615 2616 2617 2618

    return !(memory_region_is_ram(section->mr) ||
             memory_region_is_romd(section->mr));
}
#endif