exec.c 72.1 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"
P
Paolo Bonzini 已提交
34
#include "hw/xen/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

53
//#define DEBUG_SUBPAGE
T
ths 已提交
54

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

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

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

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

68 69
MemoryRegion io_mem_rom, io_mem_notdirty;
static MemoryRegion io_mem_unassigned, io_mem_subpage_ram;
70

71
#endif
72

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

82
#if !defined(CONFIG_USER_ONLY)
83

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

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

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

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

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

104
#if !defined(CONFIG_USER_ONLY)
105

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

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);
126
    for (i = 0; i < L2_SIZE; ++i) {
127
        phys_map_nodes[ret][i].is_leaf = 0;
128
        phys_map_nodes[ret][i].ptr = PHYS_MAP_NODE_NIL;
129
    }
130
    return ret;
131 132 133 134 135 136 137
}

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

B
bellard 已提交
138

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

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

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

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

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

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

190
    for (i = P_L2_LEVELS - 1; i >= 0 && !lp.is_leaf; i--) {
191
        if (lp.ptr == PHYS_MAP_NODE_NIL) {
P
Paolo Bonzini 已提交
192
            return &phys_sections[phys_section_unassigned];
193
        }
194
        p = phys_map_nodes[lp.ptr];
195
        lp = p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)];
196
    }
P
Paolo Bonzini 已提交
197
    return &phys_sections[lp.ptr];
198 199
}

B
Blue Swirl 已提交
200 201
bool memory_region_is_unassigned(MemoryRegion *mr)
{
P
Paolo Bonzini 已提交
202
    return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device
203
        && mr != &io_mem_watch;
B
bellard 已提交
204
}
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
                                             hwaddr *xlat, hwaddr *plen,
                                             bool is_write)
{
    MemoryRegionSection *section;
    Int128 diff;

    section = phys_page_find(as->dispatch, addr >> TARGET_PAGE_BITS);
    /* Compute offset within MemoryRegionSection */
    addr -= section->offset_within_address_space;

    /* Compute offset within MemoryRegion */
    *xlat = addr + section->offset_within_region;

    diff = int128_sub(section->mr->size, int128_make64(addr));
    *plen = MIN(int128_get64(diff), *plen);
    return section;
}
224
#endif
B
bellard 已提交
225

226
void cpu_exec_init_all(void)
227
{
228
#if !defined(CONFIG_USER_ONLY)
229
    qemu_mutex_init(&ram_list.mutex);
230 231
    memory_map_init();
    io_mem_init();
232
#endif
233
}
234

235
#if !defined(CONFIG_USER_ONLY)
236 237

static int cpu_common_post_load(void *opaque, int version_id)
B
bellard 已提交
238
{
239
    CPUState *cpu = opaque;
B
bellard 已提交
240

241 242
    /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
       version_id is increased. */
243 244
    cpu->interrupt_request &= ~0x01;
    tlb_flush(cpu->env_ptr, 1);
245 246

    return 0;
B
bellard 已提交
247
}
B
bellard 已提交
248

249 250 251 252 253 254 255
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 []) {
256 257
        VMSTATE_UINT32(halted, CPUState),
        VMSTATE_UINT32(interrupt_request, CPUState),
258 259 260
        VMSTATE_END_OF_LIST()
    }
};
261 262
#else
#define vmstate_cpu_common vmstate_dummy
263
#endif
B
bellard 已提交
264

265
CPUState *qemu_get_cpu(int index)
B
bellard 已提交
266
{
267
    CPUArchState *env = first_cpu;
268
    CPUState *cpu = NULL;
B
bellard 已提交
269

270
    while (env) {
271 272
        cpu = ENV_GET_CPU(env);
        if (cpu->cpu_index == index) {
273
            break;
274
        }
275
        env = env->next_cpu;
B
bellard 已提交
276
    }
277

278
    return env ? cpu : NULL;
B
bellard 已提交
279 280
}

281 282 283 284 285 286 287 288 289 290
void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data)
{
    CPUArchState *env = first_cpu;

    while (env) {
        func(ENV_GET_CPU(env), data);
        env = env->next_cpu;
    }
}

291
void cpu_exec_init(CPUArchState *env)
B
bellard 已提交
292
{
293
    CPUState *cpu = ENV_GET_CPU(env);
294
    CPUClass *cc = CPU_GET_CLASS(cpu);
295 296 297 298 299 300 301 302 303 304 305 306 307
    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++;
    }
308
    cpu->cpu_index = cpu_index;
309
    cpu->numa_node = 0;
310 311 312 313 314 315 316 317 318
    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
319
    vmstate_register(NULL, cpu_index, &vmstate_cpu_common, cpu);
320 321 322
#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
    register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
                    cpu_save, cpu_load, env);
323
    assert(cc->vmsd == NULL);
324
#endif
325 326 327
    if (cc->vmsd != NULL) {
        vmstate_register(NULL, cpu_index, cc->vmsd, cpu);
    }
B
bellard 已提交
328 329
}

B
bellard 已提交
330
#if defined(TARGET_HAS_ICE)
331
#if defined(CONFIG_USER_ONLY)
332
static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
333 334 335 336
{
    tb_invalidate_phys_page_range(pc, pc + 1, 0);
}
#else
337 338
static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
{
339 340
    tb_invalidate_phys_addr(cpu_get_phys_page_debug(env, pc) |
            (pc & ~TARGET_PAGE_MASK));
341
}
B
bellard 已提交
342
#endif
343
#endif /* TARGET_HAS_ICE */
B
bellard 已提交
344

345
#if defined(CONFIG_USER_ONLY)
346
void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
347 348 349 350

{
}

351
int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
352 353 354 355 356
                          int flags, CPUWatchpoint **watchpoint)
{
    return -ENOSYS;
}
#else
357
/* Add a watchpoint.  */
358
int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
359
                          int flags, CPUWatchpoint **watchpoint)
360
{
361
    target_ulong len_mask = ~(len - 1);
362
    CPUWatchpoint *wp;
363

364
    /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
365 366
    if ((len & (len - 1)) || (addr & ~len_mask) ||
            len == 0 || len > TARGET_PAGE_SIZE) {
367 368 369 370
        fprintf(stderr, "qemu: tried to set invalid watchpoint at "
                TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
        return -EINVAL;
    }
371
    wp = g_malloc(sizeof(*wp));
372 373

    wp->vaddr = addr;
374
    wp->len_mask = len_mask;
375 376
    wp->flags = flags;

377
    /* keep all GDB-injected watchpoints in front */
378
    if (flags & BP_GDB)
B
Blue Swirl 已提交
379
        QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
380
    else
B
Blue Swirl 已提交
381
        QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
382 383

    tlb_flush_page(env, addr);
384 385 386 387

    if (watchpoint)
        *watchpoint = wp;
    return 0;
388 389
}

390
/* Remove a specific watchpoint.  */
391
int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr, target_ulong len,
392
                          int flags)
393
{
394
    target_ulong len_mask = ~(len - 1);
395
    CPUWatchpoint *wp;
396

B
Blue Swirl 已提交
397
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
398
        if (addr == wp->vaddr && len_mask == wp->len_mask
399
                && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
400
            cpu_watchpoint_remove_by_ref(env, wp);
401 402 403
            return 0;
        }
    }
404
    return -ENOENT;
405 406
}

407
/* Remove a specific watchpoint by reference.  */
408
void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint)
409
{
B
Blue Swirl 已提交
410
    QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
411

412 413
    tlb_flush_page(env, watchpoint->vaddr);

414
    g_free(watchpoint);
415 416 417
}

/* Remove all matching watchpoints.  */
418
void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
419
{
420
    CPUWatchpoint *wp, *next;
421

B
Blue Swirl 已提交
422
    QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
423 424
        if (wp->flags & mask)
            cpu_watchpoint_remove_by_ref(env, wp);
425
    }
426
}
427
#endif
428

429
/* Add a breakpoint.  */
430
int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
431
                          CPUBreakpoint **breakpoint)
B
bellard 已提交
432
{
B
bellard 已提交
433
#if defined(TARGET_HAS_ICE)
434
    CPUBreakpoint *bp;
435

436
    bp = g_malloc(sizeof(*bp));
B
bellard 已提交
437

438 439 440
    bp->pc = pc;
    bp->flags = flags;

441
    /* keep all GDB-injected breakpoints in front */
442
    if (flags & BP_GDB)
B
Blue Swirl 已提交
443
        QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
444
    else
B
Blue Swirl 已提交
445
        QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
446

B
bellard 已提交
447
    breakpoint_invalidate(env, pc);
448 449 450

    if (breakpoint)
        *breakpoint = bp;
B
bellard 已提交
451 452
    return 0;
#else
453
    return -ENOSYS;
B
bellard 已提交
454 455 456
#endif
}

457
/* Remove a specific breakpoint.  */
458
int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags)
459
{
460
#if defined(TARGET_HAS_ICE)
461 462
    CPUBreakpoint *bp;

B
Blue Swirl 已提交
463
    QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
464 465 466 467
        if (bp->pc == pc && bp->flags == flags) {
            cpu_breakpoint_remove_by_ref(env, bp);
            return 0;
        }
468
    }
469 470 471
    return -ENOENT;
#else
    return -ENOSYS;
472 473 474
#endif
}

475
/* Remove a specific breakpoint by reference.  */
476
void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint)
B
bellard 已提交
477
{
B
bellard 已提交
478
#if defined(TARGET_HAS_ICE)
B
Blue Swirl 已提交
479
    QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
B
bellard 已提交
480

481 482
    breakpoint_invalidate(env, breakpoint->pc);

483
    g_free(breakpoint);
484 485 486 487
#endif
}

/* Remove all matching breakpoints. */
488
void cpu_breakpoint_remove_all(CPUArchState *env, int mask)
489 490
{
#if defined(TARGET_HAS_ICE)
491
    CPUBreakpoint *bp, *next;
492

B
Blue Swirl 已提交
493
    QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
494 495
        if (bp->flags & mask)
            cpu_breakpoint_remove_by_ref(env, bp);
496
    }
B
bellard 已提交
497 498 499
#endif
}

B
bellard 已提交
500 501
/* enable or disable single step mode. EXCP_DEBUG is returned by the
   CPU loop after each instruction */
502
void cpu_single_step(CPUArchState *env, int enabled)
B
bellard 已提交
503
{
B
bellard 已提交
504
#if defined(TARGET_HAS_ICE)
B
bellard 已提交
505 506
    if (env->singlestep_enabled != enabled) {
        env->singlestep_enabled = enabled;
507 508 509
        if (kvm_enabled())
            kvm_update_guest_debug(env, 0);
        else {
S
Stuart Brady 已提交
510
            /* must flush all the translated code to avoid inconsistencies */
511 512 513
            /* XXX: only flush what is necessary */
            tb_flush(env);
        }
B
bellard 已提交
514 515 516 517
    }
#endif
}

518
void cpu_exit(CPUArchState *env)
519
{
520 521 522
    CPUState *cpu = ENV_GET_CPU(env);

    cpu->exit_request = 1;
523
    cpu->tcg_exit_req = 1;
524 525
}

526
void cpu_abort(CPUArchState *env, const char *fmt, ...)
B
bellard 已提交
527 528
{
    va_list ap;
P
pbrook 已提交
529
    va_list ap2;
B
bellard 已提交
530 531

    va_start(ap, fmt);
P
pbrook 已提交
532
    va_copy(ap2, ap);
B
bellard 已提交
533 534 535
    fprintf(stderr, "qemu: fatal: ");
    vfprintf(stderr, fmt, ap);
    fprintf(stderr, "\n");
536
    cpu_dump_state(env, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
537 538 539 540
    if (qemu_log_enabled()) {
        qemu_log("qemu: fatal: ");
        qemu_log_vprintf(fmt, ap2);
        qemu_log("\n");
541
        log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
542
        qemu_log_flush();
543
        qemu_log_close();
544
    }
P
pbrook 已提交
545
    va_end(ap2);
546
    va_end(ap);
547 548 549 550 551 552 553 554
#if defined(CONFIG_USER_ONLY)
    {
        struct sigaction act;
        sigfillset(&act.sa_mask);
        act.sa_handler = SIG_DFL;
        sigaction(SIGABRT, &act, NULL);
    }
#endif
B
bellard 已提交
555 556 557
    abort();
}

558
CPUArchState *cpu_copy(CPUArchState *env)
559
{
560 561
    CPUArchState *new_env = cpu_init(env->cpu_model_str);
    CPUArchState *next_cpu = new_env->next_cpu;
562 563 564 565 566
#if defined(TARGET_HAS_ICE)
    CPUBreakpoint *bp;
    CPUWatchpoint *wp;
#endif

567
    memcpy(new_env, env, sizeof(CPUArchState));
568

569
    /* Preserve chaining. */
570
    new_env->next_cpu = next_cpu;
571 572 573 574

    /* 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 已提交
575 576
    QTAILQ_INIT(&env->breakpoints);
    QTAILQ_INIT(&env->watchpoints);
577
#if defined(TARGET_HAS_ICE)
B
Blue Swirl 已提交
578
    QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
579 580
        cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
    }
B
Blue Swirl 已提交
581
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
582 583 584 585 586
        cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
                              wp->flags, NULL);
    }
#endif

587 588 589
    return new_env;
}

590
#if !defined(CONFIG_USER_ONLY)
J
Juan Quintela 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
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 已提交
609
/* Note: start and end must be within the same ram block.  */
A
Anthony Liguori 已提交
610
void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
B
bellard 已提交
611
                                     int dirty_flags)
612
{
J
Juan Quintela 已提交
613
    uintptr_t length;
614 615 616 617 618 619 620

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

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

J
Juan Quintela 已提交
623 624
    if (tcg_enabled()) {
        tlb_reset_dirty_range_all(start, end, length);
P
pbrook 已提交
625
    }
626 627
}

B
Blue Swirl 已提交
628
static int cpu_physical_memory_set_dirty_tracking(int enable)
A
aliguori 已提交
629
{
M
Michael S. Tsirkin 已提交
630
    int ret = 0;
A
aliguori 已提交
631
    in_migration = enable;
M
Michael S. Tsirkin 已提交
632
    return ret;
A
aliguori 已提交
633 634
}

A
Avi Kivity 已提交
635
hwaddr memory_region_section_get_iotlb(CPUArchState *env,
636 637 638 639 640
                                       MemoryRegionSection *section,
                                       target_ulong vaddr,
                                       hwaddr paddr, hwaddr xlat,
                                       int prot,
                                       target_ulong *address)
B
Blue Swirl 已提交
641
{
A
Avi Kivity 已提交
642
    hwaddr iotlb;
B
Blue Swirl 已提交
643 644
    CPUWatchpoint *wp;

645
    if (memory_region_is_ram(section->mr)) {
B
Blue Swirl 已提交
646 647
        /* Normal RAM.  */
        iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
648
            + xlat;
B
Blue Swirl 已提交
649 650 651 652 653 654 655
        if (!section->readonly) {
            iotlb |= phys_section_notdirty;
        } else {
            iotlb |= phys_section_rom;
        }
    } else {
        iotlb = section - phys_sections;
656
        iotlb += xlat;
B
Blue Swirl 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    }

    /* 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;
}
674 675
#endif /* defined(CONFIG_USER_ONLY) */

676
#if !defined(CONFIG_USER_ONLY)
677

P
Paul Brook 已提交
678 679
#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
typedef struct subpage_t {
680
    MemoryRegion iomem;
A
Avi Kivity 已提交
681
    hwaddr base;
682
    uint16_t sub_section[TARGET_PAGE_SIZE];
P
Paul Brook 已提交
683 684
} subpage_t;

A
Anthony Liguori 已提交
685
static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
686
                             uint16_t section);
A
Avi Kivity 已提交
687
static subpage_t *subpage_init(hwaddr base);
688
static void destroy_page_desc(uint16_t section_index)
689
{
690 691
    MemoryRegionSection *section = &phys_sections[section_index];
    MemoryRegion *mr = section->mr;
692 693 694 695 696 697 698 699

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

700
static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level)
701 702
{
    unsigned i;
703
    PhysPageEntry *p;
704

705
    if (lp->ptr == PHYS_MAP_NODE_NIL) {
706 707 708
        return;
    }

709
    p = phys_map_nodes[lp->ptr];
710
    for (i = 0; i < L2_SIZE; ++i) {
711
        if (!p[i].is_leaf) {
712
            destroy_l2_mapping(&p[i], level - 1);
713
        } else {
714
            destroy_page_desc(p[i].ptr);
715 716
        }
    }
717
    lp->is_leaf = 0;
718
    lp->ptr = PHYS_MAP_NODE_NIL;
719 720
}

A
Avi Kivity 已提交
721
static void destroy_all_mappings(AddressSpaceDispatch *d)
722
{
A
Avi Kivity 已提交
723
    destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
724
    phys_map_nodes_reset();
725 726
}

727 728
static uint16_t phys_section_add(MemoryRegionSection *section)
{
729 730 731 732 733 734
    /* The physical section number is ORed with a page-aligned
     * pointer to produce the iotlb entries.  Thus it should
     * never overflow into the page-aligned value.
     */
    assert(phys_sections_nb < TARGET_PAGE_SIZE);

735 736 737 738 739 740 741 742 743 744 745 746 747 748
    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 已提交
749
static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
750 751
{
    subpage_t *subpage;
A
Avi Kivity 已提交
752
    hwaddr base = section->offset_within_address_space
753
        & TARGET_PAGE_MASK;
A
Avi Kivity 已提交
754
    MemoryRegionSection *existing = phys_page_find(d, base >> TARGET_PAGE_BITS);
755 756 757 758
    MemoryRegionSection subsection = {
        .offset_within_address_space = base,
        .size = TARGET_PAGE_SIZE,
    };
A
Avi Kivity 已提交
759
    hwaddr start, end;
760

761
    assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
762

763
    if (!(existing->mr->subpage)) {
764 765
        subpage = subpage_init(base);
        subsection.mr = &subpage->iomem;
A
Avi Kivity 已提交
766
        phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
767
                      phys_section_add(&subsection));
768
    } else {
769
        subpage = container_of(existing->mr, subpage_t, iomem);
770 771
    }
    start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
772
    end = start + section->size - 1;
773 774 775 776
    subpage_register(subpage, start, end, phys_section_add(section));
}


A
Avi Kivity 已提交
777
static void register_multipage(AddressSpaceDispatch *d, MemoryRegionSection *section)
778
{
A
Avi Kivity 已提交
779
    hwaddr start_addr = section->offset_within_address_space;
780
    ram_addr_t size = section->size;
A
Avi Kivity 已提交
781
    hwaddr addr;
782
    uint16_t section_index = phys_section_add(section);
783

784
    assert(size);
M
Michael S. Tsirkin 已提交
785

786
    addr = start_addr;
A
Avi Kivity 已提交
787
    phys_page_set(d, addr >> TARGET_PAGE_BITS, size >> TARGET_PAGE_BITS,
788
                  section_index);
789 790
}

791 792 793 794 795 796 797 798 799 800 801
QEMU_BUILD_BUG_ON(TARGET_PHYS_ADDR_SPACE_BITS > MAX_PHYS_ADDR_SPACE_BITS)

static MemoryRegionSection limit(MemoryRegionSection section)
{
    section.size = MIN(section.offset_within_address_space + section.size,
                       MAX_PHYS_ADDR + 1)
                   - section.offset_within_address_space;

    return section;
}

A
Avi Kivity 已提交
802
static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
803
{
A
Avi Kivity 已提交
804
    AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
805
    MemoryRegionSection now = limit(*section), remain = limit(*section);
806 807 808 809 810 811

    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 已提交
812
        register_subpage(d, &now);
813 814 815 816
        remain.size -= now.size;
        remain.offset_within_address_space += now.size;
        remain.offset_within_region += now.size;
    }
817 818 819 820
    while (remain.size >= TARGET_PAGE_SIZE) {
        now = remain;
        if (remain.offset_within_region & ~TARGET_PAGE_MASK) {
            now.size = TARGET_PAGE_SIZE;
A
Avi Kivity 已提交
821
            register_subpage(d, &now);
822 823
        } else {
            now.size &= TARGET_PAGE_MASK;
A
Avi Kivity 已提交
824
            register_multipage(d, &now);
825
        }
826 827 828 829 830 831
        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 已提交
832
        register_subpage(d, &now);
833 834 835
    }
}

836 837 838 839 840 841
void qemu_flush_coalesced_mmio_buffer(void)
{
    if (kvm_enabled())
        kvm_flush_coalesced_mmio_buffer();
}

842 843 844 845 846 847 848 849 850 851
void qemu_mutex_lock_ramlist(void)
{
    qemu_mutex_lock(&ram_list.mutex);
}

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

852 853 854 855 856 857 858 859 860 861 862 863
#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 已提交
864
        ret = statfs(path, &fs);
865 866 867
    } while (ret != 0 && errno == EINTR);

    if (ret != 0) {
Y
Yoshiaki Tamura 已提交
868 869
        perror(path);
        return 0;
870 871 872
    }

    if (fs.f_type != HUGETLBFS_MAGIC)
Y
Yoshiaki Tamura 已提交
873
        fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
874 875 876 877

    return fs.f_bsize;
}

A
Alex Williamson 已提交
878 879 880
static void *file_ram_alloc(RAMBlock *block,
                            ram_addr_t memory,
                            const char *path)
881 882
{
    char *filename;
883 884
    char *sanitized_name;
    char *c;
885 886 887 888 889 890 891 892 893
    void *area;
    int fd;
#ifdef MAP_POPULATE
    int flags;
#endif
    unsigned long hpagesize;

    hpagesize = gethugepagesize(path);
    if (!hpagesize) {
Y
Yoshiaki Tamura 已提交
894
        return NULL;
895 896 897 898 899 900 901 902 903 904 905
    }

    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;
    }

906 907 908 909 910 911 912 913 914 915
    /* Make name safe to use with mkstemp by replacing '/' with '_'. */
    sanitized_name = g_strdup(block->mr->name);
    for (c = sanitized_name; *c != '\0'; c++) {
        if (*c == '/')
            *c = '_';
    }

    filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
                               sanitized_name);
    g_free(sanitized_name);
916 917 918

    fd = mkstemp(filename);
    if (fd < 0) {
Y
Yoshiaki Tamura 已提交
919
        perror("unable to create backing store for hugepages");
920
        g_free(filename);
Y
Yoshiaki Tamura 已提交
921
        return NULL;
922 923
    }
    unlink(filename);
924
    g_free(filename);
925 926 927 928 929 930 931 932 933 934

    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 已提交
935
        perror("ftruncate");
936 937 938 939 940 941 942 943 944 945 946 947

#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 已提交
948 949 950
        perror("file_ram_alloc: can't mmap RAM pages");
        close(fd);
        return (NULL);
951
    }
A
Alex Williamson 已提交
952
    block->fd = fd;
953 954 955 956
    return area;
}
#endif

957
static ram_addr_t find_ram_offset(ram_addr_t size)
A
Alex Williamson 已提交
958 959
{
    RAMBlock *block, *next_block;
A
Alex Williamson 已提交
960
    ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
A
Alex Williamson 已提交
961

962 963
    assert(size != 0); /* it would hand out same offset multiple times */

P
Paolo Bonzini 已提交
964
    if (QTAILQ_EMPTY(&ram_list.blocks))
A
Alex Williamson 已提交
965 966
        return 0;

P
Paolo Bonzini 已提交
967
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
968
        ram_addr_t end, next = RAM_ADDR_MAX;
A
Alex Williamson 已提交
969 970 971

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

P
Paolo Bonzini 已提交
972
        QTAILQ_FOREACH(next_block, &ram_list.blocks, next) {
A
Alex Williamson 已提交
973 974 975 976 977
            if (next_block->offset >= end) {
                next = MIN(next, next_block->offset);
            }
        }
        if (next - end >= size && next - end < mingap) {
A
Alex Williamson 已提交
978
            offset = end;
A
Alex Williamson 已提交
979 980 981
            mingap = next - end;
        }
    }
A
Alex Williamson 已提交
982 983 984 985 986 987 988

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

A
Alex Williamson 已提交
989 990 991
    return offset;
}

J
Juan Quintela 已提交
992
ram_addr_t last_ram_offset(void)
993 994 995 996
{
    RAMBlock *block;
    ram_addr_t last = 0;

P
Paolo Bonzini 已提交
997
    QTAILQ_FOREACH(block, &ram_list.blocks, next)
998 999 1000 1001 1002
        last = MAX(last, block->offset + block->length);

    return last;
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
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");
        }
    }
}

1021
void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
1022 1023 1024
{
    RAMBlock *new_block, *block;

1025
    new_block = NULL;
P
Paolo Bonzini 已提交
1026
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1027 1028 1029 1030 1031 1032 1033
        if (block->offset == addr) {
            new_block = block;
            break;
        }
    }
    assert(new_block);
    assert(!new_block->idstr[0]);
1034

1035 1036
    if (dev) {
        char *id = qdev_get_dev_path(dev);
1037 1038
        if (id) {
            snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
1039
            g_free(id);
1040 1041 1042 1043
        }
    }
    pstrcat(new_block->idstr, sizeof(new_block->idstr), name);

1044 1045
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
P
Paolo Bonzini 已提交
1046
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1047
        if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
1048 1049 1050 1051 1052
            fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
                    new_block->idstr);
            abort();
        }
    }
1053
    qemu_mutex_unlock_ramlist();
1054 1055
}

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
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);
}

1069 1070 1071
ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                   MemoryRegion *mr)
{
1072
    RAMBlock *block, *new_block;
1073 1074 1075

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

1077 1078
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
A
Avi Kivity 已提交
1079
    new_block->mr = mr;
J
Jun Nakajima 已提交
1080
    new_block->offset = find_ram_offset(size);
1081 1082
    if (host) {
        new_block->host = host;
H
Huang Ying 已提交
1083
        new_block->flags |= RAM_PREALLOC_MASK;
1084 1085
    } else {
        if (mem_path) {
1086
#if defined (__linux__) && !defined(TARGET_S390X)
1087 1088
            new_block->host = file_ram_alloc(new_block, size, mem_path);
            if (!new_block->host) {
1089
                new_block->host = qemu_anon_ram_alloc(size);
1090
                memory_try_enable_merging(new_block->host, size);
1091
            }
1092
#else
1093 1094
            fprintf(stderr, "-mem-path option unsupported\n");
            exit(1);
1095
#endif
1096
        } else {
1097
            if (xen_enabled()) {
1098
                xen_ram_alloc(new_block->offset, size, mr);
1099 1100
            } else if (kvm_enabled()) {
                /* some s390/kvm configurations have special constraints */
1101
                new_block->host = kvm_ram_alloc(size);
J
Jun Nakajima 已提交
1102
            } else {
1103
                new_block->host = qemu_anon_ram_alloc(size);
J
Jun Nakajima 已提交
1104
            }
1105
            memory_try_enable_merging(new_block->host, size);
1106
        }
1107
    }
P
pbrook 已提交
1108 1109
    new_block->length = size;

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
    /* 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);
    }
1121
    ram_list.mru_block = NULL;
P
pbrook 已提交
1122

U
Umesh Deshpande 已提交
1123
    ram_list.version++;
1124
    qemu_mutex_unlock_ramlist();
U
Umesh Deshpande 已提交
1125

1126
    ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
A
Alex Williamson 已提交
1127
                                       last_ram_offset() >> TARGET_PAGE_BITS);
1128 1129
    memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
           0, size >> TARGET_PAGE_BITS);
J
Juan Quintela 已提交
1130
    cpu_physical_memory_set_dirty_range(new_block->offset, size, 0xff);
P
pbrook 已提交
1131

1132
    qemu_ram_setup_dump(new_block->host, size);
1133
    qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
1134

1135 1136 1137
    if (kvm_enabled())
        kvm_setup_guest_memory(new_block->host, size);

P
pbrook 已提交
1138 1139
    return new_block->offset;
}
B
bellard 已提交
1140

1141
ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr)
1142
{
1143
    return qemu_ram_alloc_from_ptr(size, NULL, mr);
1144 1145
}

1146 1147 1148 1149
void qemu_ram_free_from_ptr(ram_addr_t addr)
{
    RAMBlock *block;

1150 1151
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
P
Paolo Bonzini 已提交
1152
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1153
        if (addr == block->offset) {
P
Paolo Bonzini 已提交
1154
            QTAILQ_REMOVE(&ram_list.blocks, block, next);
1155
            ram_list.mru_block = NULL;
U
Umesh Deshpande 已提交
1156
            ram_list.version++;
1157
            g_free(block);
1158
            break;
1159 1160
        }
    }
1161
    qemu_mutex_unlock_ramlist();
1162 1163
}

A
Anthony Liguori 已提交
1164
void qemu_ram_free(ram_addr_t addr)
B
bellard 已提交
1165
{
A
Alex Williamson 已提交
1166 1167
    RAMBlock *block;

1168 1169
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
P
Paolo Bonzini 已提交
1170
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
A
Alex Williamson 已提交
1171
        if (addr == block->offset) {
P
Paolo Bonzini 已提交
1172
            QTAILQ_REMOVE(&ram_list.blocks, block, next);
1173
            ram_list.mru_block = NULL;
U
Umesh Deshpande 已提交
1174
            ram_list.version++;
H
Huang Ying 已提交
1175 1176 1177
            if (block->flags & RAM_PREALLOC_MASK) {
                ;
            } else if (mem_path) {
A
Alex Williamson 已提交
1178 1179 1180 1181 1182
#if defined (__linux__) && !defined(TARGET_S390X)
                if (block->fd) {
                    munmap(block->host, block->length);
                    close(block->fd);
                } else {
1183
                    qemu_anon_ram_free(block->host, block->length);
A
Alex Williamson 已提交
1184
                }
1185 1186
#else
                abort();
A
Alex Williamson 已提交
1187 1188
#endif
            } else {
1189
                if (xen_enabled()) {
J
Jan Kiszka 已提交
1190
                    xen_invalidate_map_cache_entry(block->host);
J
Jun Nakajima 已提交
1191
                } else {
1192
                    qemu_anon_ram_free(block->host, block->length);
J
Jun Nakajima 已提交
1193
                }
A
Alex Williamson 已提交
1194
            }
1195
            g_free(block);
1196
            break;
A
Alex Williamson 已提交
1197 1198
        }
    }
1199
    qemu_mutex_unlock_ramlist();
A
Alex Williamson 已提交
1200

B
bellard 已提交
1201 1202
}

H
Huang Ying 已提交
1203 1204 1205 1206 1207 1208 1209 1210
#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 已提交
1211
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
H
Huang Ying 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
        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);
                    }
1236 1237
#else
                    abort();
H
Huang Ying 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
#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) {
1251 1252
                    fprintf(stderr, "Could not remap addr: "
                            RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
H
Huang Ying 已提交
1253 1254 1255
                            length, addr);
                    exit(1);
                }
1256
                memory_try_enable_merging(vaddr, length);
1257
                qemu_ram_setup_dump(vaddr, length);
H
Huang Ying 已提交
1258 1259 1260 1261 1262 1263 1264
            }
            return;
        }
    }
}
#endif /* !_WIN32 */

1265
/* Return a host pointer to ram allocated with qemu_ram_alloc.
P
pbrook 已提交
1266 1267 1268 1269 1270 1271 1272
   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 已提交
1273
void *qemu_get_ram_ptr(ram_addr_t addr)
1274
{
P
pbrook 已提交
1275 1276
    RAMBlock *block;

1277
    /* The list is protected by the iothread lock here.  */
1278 1279 1280 1281
    block = ram_list.mru_block;
    if (block && addr - block->offset < block->length) {
        goto found;
    }
P
Paolo Bonzini 已提交
1282
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
A
Alex Williamson 已提交
1283
        if (addr - block->offset < block->length) {
1284
            goto found;
A
Alex Williamson 已提交
1285
        }
P
pbrook 已提交
1286
    }
A
Alex Williamson 已提交
1287 1288 1289 1290

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

1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
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);
1306 1307
}

1308 1309 1310 1311
/* 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?
1312
 */
B
Blue Swirl 已提交
1313
static void *qemu_safe_ram_ptr(ram_addr_t addr)
1314 1315 1316
{
    RAMBlock *block;

1317
    /* The list is protected by the iothread lock here.  */
P
Paolo Bonzini 已提交
1318
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1319
        if (addr - block->offset < block->length) {
1320
            if (xen_enabled()) {
J
Jun Nakajima 已提交
1321 1322
                /* We need to check if the requested address is in the RAM
                 * because we don't want to map the entire memory in QEMU.
1323
                 * In that case just map until the end of the page.
J
Jun Nakajima 已提交
1324 1325
                 */
                if (block->offset == 0) {
J
Jan Kiszka 已提交
1326
                    return xen_map_cache(addr, 0, 0);
J
Jun Nakajima 已提交
1327
                } else if (block->host == NULL) {
J
Jan Kiszka 已提交
1328 1329
                    block->host =
                        xen_map_cache(block->offset, block->length, 1);
J
Jun Nakajima 已提交
1330 1331
                }
            }
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
            return block->host + (addr - block->offset);
        }
    }

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

    return NULL;
}

1342 1343
/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
 * but takes a size argument */
B
Blue Swirl 已提交
1344
static void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
1345
{
1346 1347 1348
    if (*size == 0) {
        return NULL;
    }
1349
    if (xen_enabled()) {
J
Jan Kiszka 已提交
1350
        return xen_map_cache(addr, *size, 1);
1351
    } else {
1352 1353
        RAMBlock *block;

P
Paolo Bonzini 已提交
1354
        QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
            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();
    }
}

M
Marcelo Tosatti 已提交
1367
int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
P
pbrook 已提交
1368
{
P
pbrook 已提交
1369 1370 1371
    RAMBlock *block;
    uint8_t *host = ptr;

1372
    if (xen_enabled()) {
J
Jan Kiszka 已提交
1373
        *ram_addr = xen_ram_addr_from_mapcache(ptr);
1374 1375 1376
        return 0;
    }

P
Paolo Bonzini 已提交
1377
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
J
Jun Nakajima 已提交
1378 1379 1380 1381
        /* This case append when the block is not mapped. */
        if (block->host == NULL) {
            continue;
        }
A
Alex Williamson 已提交
1382
        if (host - block->host < block->length) {
M
Marcelo Tosatti 已提交
1383 1384
            *ram_addr = block->offset + (host - block->host);
            return 0;
A
Alex Williamson 已提交
1385
        }
P
pbrook 已提交
1386
    }
J
Jun Nakajima 已提交
1387

M
Marcelo Tosatti 已提交
1388 1389
    return -1;
}
A
Alex Williamson 已提交
1390

M
Marcelo Tosatti 已提交
1391 1392 1393 1394 1395
/* 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 已提交
1396

M
Marcelo Tosatti 已提交
1397 1398 1399 1400 1401
    if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
        fprintf(stderr, "Bad ram pointer %p\n", ptr);
        abort();
    }
    return ram_addr;
P
pbrook 已提交
1402 1403
}

A
Avi Kivity 已提交
1404
static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
1405
                               uint64_t val, unsigned size)
1406
{
1407
    int dirty_flags;
1408
    dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
1409
    if (!(dirty_flags & CODE_DIRTY_FLAG)) {
1410
        tb_invalidate_phys_page_fast(ram_addr, size);
1411
        dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
1412
    }
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
    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();
1425
    }
B
bellard 已提交
1426
    dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
1427
    cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
B
bellard 已提交
1428 1429 1430
    /* we remove the notdirty callback only if the code has been
       flushed */
    if (dirty_flags == 0xff)
P
pbrook 已提交
1431
        tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
1432 1433
}

1434 1435 1436 1437 1438 1439
static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
                                 unsigned size, bool is_write)
{
    return is_write;
}

1440 1441
static const MemoryRegionOps notdirty_mem_ops = {
    .write = notdirty_mem_write,
1442
    .valid.accepts = notdirty_mem_accepts,
1443
    .endianness = DEVICE_NATIVE_ENDIAN,
1444 1445
};

P
pbrook 已提交
1446
/* Generate a debug exception if a watchpoint has been hit.  */
1447
static void check_watchpoint(int offset, int len_mask, int flags)
P
pbrook 已提交
1448
{
1449
    CPUArchState *env = cpu_single_env;
1450
    target_ulong pc, cs_base;
P
pbrook 已提交
1451
    target_ulong vaddr;
1452
    CPUWatchpoint *wp;
1453
    int cpu_flags;
P
pbrook 已提交
1454

1455 1456 1457 1458
    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. */
1459
        cpu_interrupt(ENV_GET_CPU(env), CPU_INTERRUPT_DEBUG);
1460 1461
        return;
    }
P
pbrook 已提交
1462
    vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
B
Blue Swirl 已提交
1463
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
1464 1465
        if ((vaddr == (wp->vaddr & len_mask) ||
             (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
1466 1467 1468
            wp->flags |= BP_WATCHPOINT_HIT;
            if (!env->watchpoint_hit) {
                env->watchpoint_hit = wp;
B
Blue Swirl 已提交
1469
                tb_check_watchpoint(env);
1470 1471
                if (wp->flags & BP_STOP_BEFORE_ACCESS) {
                    env->exception_index = EXCP_DEBUG;
1472
                    cpu_loop_exit(env);
1473 1474 1475
                } else {
                    cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
                    tb_gen_code(env, pc, cs_base, cpu_flags, 1);
1476
                    cpu_resume_from_signal(env, NULL);
1477
                }
1478
            }
1479 1480
        } else {
            wp->flags &= ~BP_WATCHPOINT_HIT;
P
pbrook 已提交
1481 1482 1483 1484
        }
    }
}

1485 1486 1487
/* 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 已提交
1488
static uint64_t watch_mem_read(void *opaque, hwaddr addr,
1489
                               unsigned size)
1490
{
1491 1492 1493 1494 1495 1496 1497
    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();
    }
1498 1499
}

A
Avi Kivity 已提交
1500
static void watch_mem_write(void *opaque, hwaddr addr,
1501
                            uint64_t val, unsigned size)
1502
{
1503 1504
    check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE);
    switch (size) {
1505 1506 1507 1508 1509 1510 1511 1512 1513
    case 1:
        stb_phys(addr, val);
        break;
    case 2:
        stw_phys(addr, val);
        break;
    case 4:
        stl_phys(addr, val);
        break;
1514 1515
    default: abort();
    }
1516 1517
}

1518 1519 1520 1521
static const MemoryRegionOps watch_mem_ops = {
    .read = watch_mem_read,
    .write = watch_mem_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1522 1523
};

A
Avi Kivity 已提交
1524
static uint64_t subpage_read(void *opaque, hwaddr addr,
1525
                             unsigned len)
1526
{
1527
    subpage_t *mmio = opaque;
R
Richard Henderson 已提交
1528
    unsigned int idx = SUBPAGE_IDX(addr);
1529 1530
    uint64_t val;

1531
    MemoryRegionSection *section;
1532 1533 1534 1535 1536
#if defined(DEBUG_SUBPAGE)
    printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
           mmio, len, addr, idx);
#endif

1537 1538 1539 1540
    section = &phys_sections[mmio->sub_section[idx]];
    addr += mmio->base;
    addr -= section->offset_within_address_space;
    addr += section->offset_within_region;
1541 1542
    io_mem_read(section->mr, addr, &val, len);
    return val;
1543 1544
}

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

1557 1558 1559 1560
    section = &phys_sections[mmio->sub_section[idx]];
    addr += mmio->base;
    addr -= section->offset_within_address_space;
    addr += section->offset_within_region;
1561
    io_mem_write(section->mr, addr, value, len);
1562 1563
}

1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
static bool subpage_accepts(void *opaque, hwaddr addr,
                            unsigned size, bool is_write)
{
    subpage_t *mmio = opaque;
    unsigned int idx = SUBPAGE_IDX(addr);
    MemoryRegionSection *section;
#if defined(DEBUG_SUBPAGE)
    printf("%s: subpage %p %c len %d addr " TARGET_FMT_plx
           " idx %d\n", __func__, mmio,
           is_write ? 'w' : 'r', len, addr, idx);
#endif

    section = &phys_sections[mmio->sub_section[idx]];
    addr += mmio->base;
    addr -= section->offset_within_address_space;
    addr += section->offset_within_region;
    return memory_region_access_valid(section->mr, addr, size, is_write);
}

1583 1584 1585
static const MemoryRegionOps subpage_ops = {
    .read = subpage_read,
    .write = subpage_write,
1586
    .valid.accepts = subpage_accepts,
1587
    .endianness = DEVICE_NATIVE_ENDIAN,
1588 1589
};

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

A
Avi Kivity 已提交
1603
static void subpage_ram_write(void *opaque, hwaddr addr,
1604
                              uint64_t value, unsigned size)
1605 1606 1607
{
    ram_addr_t raddr = addr;
    void *ptr = qemu_get_ram_ptr(raddr);
1608 1609 1610 1611 1612 1613
    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();
    }
1614 1615
}

1616 1617 1618 1619
static const MemoryRegionOps subpage_ram_ops = {
    .read = subpage_ram_read,
    .write = subpage_ram_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1620 1621
};

A
Anthony Liguori 已提交
1622
static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
1623
                             uint16_t section)
1624 1625 1626 1627 1628 1629 1630 1631
{
    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)
1632
    printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
1633 1634
           mmio, start, end, idx, eidx, memory);
#endif
1635 1636 1637 1638
    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);
1639
    }
1640
    for (; idx <= eidx; idx++) {
1641
        mmio->sub_section[idx] = section;
1642 1643 1644 1645 1646
    }

    return 0;
}

A
Avi Kivity 已提交
1647
static subpage_t *subpage_init(hwaddr base)
1648
{
A
Anthony Liguori 已提交
1649
    subpage_t *mmio;
1650

1651
    mmio = g_malloc0(sizeof(subpage_t));
1652 1653

    mmio->base = base;
1654 1655
    memory_region_init_io(&mmio->iomem, &subpage_ops, mmio,
                          "subpage", TARGET_PAGE_SIZE);
A
Avi Kivity 已提交
1656
    mmio->iomem.subpage = true;
1657
#if defined(DEBUG_SUBPAGE)
1658 1659
    printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
           mmio, base, TARGET_PAGE_SIZE, subpage_memory);
1660
#endif
1661
    subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, phys_section_unassigned);
1662 1663 1664 1665

    return mmio;
}

1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
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 已提交
1678
MemoryRegion *iotlb_to_region(hwaddr index)
1679
{
1680
    return phys_sections[index & ~TARGET_PAGE_MASK].mr;
1681 1682
}

A
Avi Kivity 已提交
1683 1684
static void io_mem_init(void)
{
P
Paolo Bonzini 已提交
1685
    memory_region_init_io(&io_mem_rom, &unassigned_mem_ops, NULL, "rom", UINT64_MAX);
1686 1687 1688 1689
    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);
1690 1691
    memory_region_init_io(&io_mem_subpage_ram, &subpage_ram_ops, NULL,
                          "subpage-ram", UINT64_MAX);
1692 1693
    memory_region_init_io(&io_mem_watch, &watch_mem_ops, NULL,
                          "watch", UINT64_MAX);
A
Avi Kivity 已提交
1694 1695
}

A
Avi Kivity 已提交
1696 1697 1698 1699 1700 1701 1702 1703
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;
}

1704 1705
static void core_begin(MemoryListener *listener)
{
1706 1707
    phys_sections_clear();
    phys_section_unassigned = dummy_section(&io_mem_unassigned);
1708 1709 1710
    phys_section_notdirty = dummy_section(&io_mem_notdirty);
    phys_section_rom = dummy_section(&io_mem_rom);
    phys_section_watch = dummy_section(&io_mem_watch);
1711 1712
}

1713
static void tcg_commit(MemoryListener *listener)
1714
{
1715
    CPUArchState *env;
1716 1717 1718 1719 1720 1721 1722

    /* 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);
    }
1723 1724
}

1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
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);
}

1735 1736 1737
static void io_region_add(MemoryListener *listener,
                          MemoryRegionSection *section)
{
A
Avi Kivity 已提交
1738 1739 1740 1741 1742
    MemoryRegionIORange *mrio = g_new(MemoryRegionIORange, 1);

    mrio->mr = section->mr;
    mrio->offset = section->offset_within_region;
    iorange_init(&mrio->iorange, &memory_region_iorange_ops,
1743
                 section->offset_within_address_space, section->size);
A
Avi Kivity 已提交
1744
    ioport_register(&mrio->iorange);
1745 1746 1747 1748 1749 1750 1751 1752
}

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

1753
static MemoryListener core_memory_listener = {
1754
    .begin = core_begin,
1755 1756
    .log_global_start = core_log_global_start,
    .log_global_stop = core_log_global_stop,
A
Avi Kivity 已提交
1757
    .priority = 1,
1758 1759
};

1760 1761 1762 1763 1764 1765
static MemoryListener io_memory_listener = {
    .region_add = io_region_add,
    .region_del = io_region_del,
    .priority = 0,
};

1766 1767 1768 1769
static MemoryListener tcg_memory_listener = {
    .commit = tcg_commit,
};

A
Avi Kivity 已提交
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
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 已提交
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
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 已提交
1795 1796
static void memory_map_init(void)
{
1797
    system_memory = g_malloc(sizeof(*system_memory));
A
Avi Kivity 已提交
1798
    memory_region_init(system_memory, "system", INT64_MAX);
1799 1800
    address_space_init(&address_space_memory, system_memory);
    address_space_memory.name = "memory";
1801

1802
    system_io = g_malloc(sizeof(*system_io));
1803
    memory_region_init(system_io, "io", 65536);
1804 1805
    address_space_init(&address_space_io, system_io);
    address_space_io.name = "I/O";
1806

1807 1808 1809
    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);
1810 1811 1812

    dma_context_init(&dma_context_memory, &address_space_memory,
                     NULL, NULL, NULL);
A
Avi Kivity 已提交
1813 1814 1815 1816 1817 1818 1819
}

MemoryRegion *get_system_memory(void)
{
    return system_memory;
}

1820 1821 1822 1823 1824
MemoryRegion *get_system_io(void)
{
    return system_io;
}

1825 1826
#endif /* !defined(CONFIG_USER_ONLY) */

B
bellard 已提交
1827 1828
/* physical memory access (slow version, mainly for debug) */
#if defined(CONFIG_USER_ONLY)
1829
int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
P
Paul Brook 已提交
1830
                        uint8_t *buf, int len, int is_write)
B
bellard 已提交
1831 1832 1833
{
    int l, flags;
    target_ulong page;
1834
    void * p;
B
bellard 已提交
1835 1836 1837 1838 1839 1840 1841 1842

    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 已提交
1843
            return -1;
B
bellard 已提交
1844 1845
        if (is_write) {
            if (!(flags & PAGE_WRITE))
P
Paul Brook 已提交
1846
                return -1;
1847
            /* XXX: this code should not depend on lock_user */
A
aurel32 已提交
1848
            if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
P
Paul Brook 已提交
1849
                return -1;
A
aurel32 已提交
1850 1851
            memcpy(p, buf, l);
            unlock_user(p, addr, l);
B
bellard 已提交
1852 1853
        } else {
            if (!(flags & PAGE_READ))
P
Paul Brook 已提交
1854
                return -1;
1855
            /* XXX: this code should not depend on lock_user */
A
aurel32 已提交
1856
            if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
P
Paul Brook 已提交
1857
                return -1;
A
aurel32 已提交
1858
            memcpy(buf, p, l);
A
aurel32 已提交
1859
            unlock_user(p, addr, 0);
B
bellard 已提交
1860 1861 1862 1863 1864
        }
        len -= l;
        buf += l;
        addr += l;
    }
P
Paul Brook 已提交
1865
    return 0;
B
bellard 已提交
1866
}
B
bellard 已提交
1867

B
bellard 已提交
1868
#else
1869

A
Avi Kivity 已提交
1870 1871
static void invalidate_and_set_dirty(hwaddr addr,
                                     hwaddr length)
1872 1873 1874 1875 1876 1877 1878
{
    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));
    }
1879
    xen_modified_memory(addr, length);
1880 1881
}

1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
{
    if (memory_region_is_ram(mr)) {
        return !(is_write && mr->readonly);
    }
    if (memory_region_is_romd(mr)) {
        return !is_write;
    }

    return false;
}

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
static inline int memory_access_size(int l, hwaddr addr)
{
    if (l >= 4 && ((addr & 3) == 0)) {
        return 4;
    }
    if (l >= 2 && ((addr & 1) == 0)) {
        return 2;
    }
    return 1;
}

1905
bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
A
Avi Kivity 已提交
1906
                      int len, bool is_write)
B
bellard 已提交
1907
{
1908
    hwaddr l;
B
bellard 已提交
1909
    uint8_t *ptr;
1910
    uint64_t val;
1911
    hwaddr addr1;
1912
    MemoryRegionSection *section;
1913
    bool error = false;
1914

B
bellard 已提交
1915
    while (len > 0) {
1916 1917
        l = len;
        section = address_space_translate(as, addr, &addr1, &l, is_write);
1918

B
bellard 已提交
1919
        if (is_write) {
1920
            if (!memory_access_is_direct(section->mr, is_write)) {
1921
                l = memory_access_size(l, addr1);
B
bellard 已提交
1922 1923
                /* XXX: could force cpu_single_env to NULL to avoid
                   potential bugs */
1924
                if (l == 4) {
B
bellard 已提交
1925
                    /* 32 bit write access */
B
bellard 已提交
1926
                    val = ldl_p(buf);
1927
                    error |= io_mem_write(section->mr, addr1, val, 4);
1928
                } else if (l == 2) {
B
bellard 已提交
1929
                    /* 16 bit write access */
B
bellard 已提交
1930
                    val = lduw_p(buf);
1931
                    error |= io_mem_write(section->mr, addr1, val, 2);
B
bellard 已提交
1932
                } else {
B
bellard 已提交
1933
                    /* 8 bit write access */
B
bellard 已提交
1934
                    val = ldub_p(buf);
1935
                    error |= io_mem_write(section->mr, addr1, val, 1);
B
bellard 已提交
1936
                }
1937
            } else {
1938
                addr1 += memory_region_get_ram_addr(section->mr);
B
bellard 已提交
1939
                /* RAM case */
P
pbrook 已提交
1940
                ptr = qemu_get_ram_ptr(addr1);
B
bellard 已提交
1941
                memcpy(ptr, buf, l);
1942
                invalidate_and_set_dirty(addr1, l);
B
bellard 已提交
1943 1944
            }
        } else {
1945
            if (!memory_access_is_direct(section->mr, is_write)) {
B
bellard 已提交
1946
                /* I/O case */
1947 1948
                l = memory_access_size(l, addr1);
                if (l == 4) {
B
bellard 已提交
1949
                    /* 32 bit read access */
1950
                    error |= io_mem_read(section->mr, addr1, &val, 4);
B
bellard 已提交
1951
                    stl_p(buf, val);
1952
                } else if (l == 2) {
B
bellard 已提交
1953
                    /* 16 bit read access */
1954
                    error |= io_mem_read(section->mr, addr1, &val, 2);
B
bellard 已提交
1955
                    stw_p(buf, val);
B
bellard 已提交
1956
                } else {
B
bellard 已提交
1957
                    /* 8 bit read access */
1958
                    error |= io_mem_read(section->mr, addr1, &val, 1);
B
bellard 已提交
1959
                    stb_p(buf, val);
B
bellard 已提交
1960 1961 1962
                }
            } else {
                /* RAM case */
1963
                ptr = qemu_get_ram_ptr(section->mr->ram_addr + addr1);
1964
                memcpy(buf, ptr, l);
B
bellard 已提交
1965 1966 1967 1968 1969 1970
            }
        }
        len -= l;
        buf += l;
        addr += l;
    }
1971 1972

    return error;
B
bellard 已提交
1973
}
B
bellard 已提交
1974

1975
bool address_space_write(AddressSpace *as, hwaddr addr,
A
Avi Kivity 已提交
1976 1977
                         const uint8_t *buf, int len)
{
1978
    return address_space_rw(as, addr, (uint8_t *)buf, len, true);
A
Avi Kivity 已提交
1979 1980
}

1981
bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
A
Avi Kivity 已提交
1982
{
1983
    return address_space_rw(as, addr, buf, len, false);
A
Avi Kivity 已提交
1984 1985 1986
}


A
Avi Kivity 已提交
1987
void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
A
Avi Kivity 已提交
1988 1989
                            int len, int is_write)
{
1990
    address_space_rw(&address_space_memory, addr, buf, len, is_write);
A
Avi Kivity 已提交
1991 1992
}

B
bellard 已提交
1993
/* used for ROM loading : can write in RAM and ROM */
A
Avi Kivity 已提交
1994
void cpu_physical_memory_write_rom(hwaddr addr,
B
bellard 已提交
1995 1996
                                   const uint8_t *buf, int len)
{
1997
    hwaddr l;
B
bellard 已提交
1998
    uint8_t *ptr;
1999
    hwaddr addr1;
2000
    MemoryRegionSection *section;
2001

B
bellard 已提交
2002
    while (len > 0) {
2003 2004 2005
        l = len;
        section = address_space_translate(&address_space_memory,
                                          addr, &addr1, &l, true);
2006

2007 2008
        if (!(memory_region_is_ram(section->mr) ||
              memory_region_is_romd(section->mr))) {
B
bellard 已提交
2009 2010
            /* do nothing */
        } else {
2011
            addr1 += memory_region_get_ram_addr(section->mr);
B
bellard 已提交
2012
            /* ROM/RAM case */
P
pbrook 已提交
2013
            ptr = qemu_get_ram_ptr(addr1);
B
bellard 已提交
2014
            memcpy(ptr, buf, l);
2015
            invalidate_and_set_dirty(addr1, l);
B
bellard 已提交
2016 2017 2018 2019 2020 2021 2022
        }
        len -= l;
        buf += l;
        addr += l;
    }
}

2023 2024
typedef struct {
    void *buffer;
A
Avi Kivity 已提交
2025 2026
    hwaddr addr;
    hwaddr len;
2027 2028 2029 2030
} BounceBuffer;

static BounceBuffer bounce;

2031 2032 2033
typedef struct MapClient {
    void *opaque;
    void (*callback)(void *opaque);
B
Blue Swirl 已提交
2034
    QLIST_ENTRY(MapClient) link;
2035 2036
} MapClient;

B
Blue Swirl 已提交
2037 2038
static QLIST_HEAD(map_client_list, MapClient) map_client_list
    = QLIST_HEAD_INITIALIZER(map_client_list);
2039 2040 2041

void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
{
2042
    MapClient *client = g_malloc(sizeof(*client));
2043 2044 2045

    client->opaque = opaque;
    client->callback = callback;
B
Blue Swirl 已提交
2046
    QLIST_INSERT_HEAD(&map_client_list, client, link);
2047 2048 2049
    return client;
}

B
Blue Swirl 已提交
2050
static void cpu_unregister_map_client(void *_client)
2051 2052 2053
{
    MapClient *client = (MapClient *)_client;

B
Blue Swirl 已提交
2054
    QLIST_REMOVE(client, link);
2055
    g_free(client);
2056 2057 2058 2059 2060 2061
}

static void cpu_notify_map_clients(void)
{
    MapClient *client;

B
Blue Swirl 已提交
2062 2063
    while (!QLIST_EMPTY(&map_client_list)) {
        client = QLIST_FIRST(&map_client_list);
2064
        client->callback(client->opaque);
2065
        cpu_unregister_map_client(client);
2066 2067 2068
    }
}

2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
{
    MemoryRegionSection *section;
    hwaddr l, xlat;

    while (len > 0) {
        l = len;
        section = address_space_translate(as, addr, &xlat, &l, is_write);
        if (!memory_access_is_direct(section->mr, is_write)) {
            l = memory_access_size(l, addr);
            if (!memory_region_access_valid(section->mr, xlat, l, is_write)) {
                return false;
            }
        }

        len -= l;
        addr += l;
    }
    return true;
}

2090 2091 2092 2093
/* 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.
2094 2095
 * Use cpu_register_map_client() to know when retrying the map operation is
 * likely to succeed.
2096
 */
A
Avi Kivity 已提交
2097
void *address_space_map(AddressSpace *as,
A
Avi Kivity 已提交
2098 2099
                        hwaddr addr,
                        hwaddr *plen,
A
Avi Kivity 已提交
2100
                        bool is_write)
2101
{
A
Avi Kivity 已提交
2102 2103
    hwaddr len = *plen;
    hwaddr todo = 0;
2104
    hwaddr l, xlat;
2105
    MemoryRegionSection *section;
2106
    ram_addr_t raddr = RAM_ADDR_MAX;
2107 2108
    ram_addr_t rlen;
    void *ret;
2109 2110

    while (len > 0) {
2111 2112
        l = len;
        section = address_space_translate(as, addr, &xlat, &l, is_write);
2113

2114
        if (!memory_access_is_direct(section->mr, is_write)) {
2115
            if (todo || bounce.buffer) {
2116 2117 2118 2119 2120 2121
                break;
            }
            bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
            bounce.addr = addr;
            bounce.len = l;
            if (!is_write) {
A
Avi Kivity 已提交
2122
                address_space_read(as, addr, bounce.buffer, l);
2123
            }
2124 2125 2126

            *plen = l;
            return bounce.buffer;
2127
        }
2128
        if (!todo) {
2129 2130 2131 2132 2133
            raddr = memory_region_get_ram_addr(section->mr) + xlat;
        } else {
            if (memory_region_get_ram_addr(section->mr) + xlat != raddr + todo) {
                break;
            }
2134
        }
2135 2136 2137

        len -= l;
        addr += l;
2138
        todo += l;
2139
    }
2140 2141 2142 2143
    rlen = todo;
    ret = qemu_ram_ptr_length(raddr, &rlen);
    *plen = rlen;
    return ret;
2144 2145
}

A
Avi Kivity 已提交
2146
/* Unmaps a memory region previously mapped by address_space_map().
2147 2148 2149
 * 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 已提交
2150 2151
void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
                         int is_write, hwaddr access_len)
2152 2153 2154
{
    if (buffer != bounce.buffer) {
        if (is_write) {
M
Marcelo Tosatti 已提交
2155
            ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
2156 2157 2158 2159 2160
            while (access_len) {
                unsigned l;
                l = TARGET_PAGE_SIZE;
                if (l > access_len)
                    l = access_len;
2161
                invalidate_and_set_dirty(addr1, l);
2162 2163 2164 2165
                addr1 += l;
                access_len -= l;
            }
        }
2166
        if (xen_enabled()) {
J
Jan Kiszka 已提交
2167
            xen_invalidate_map_cache_entry(buffer);
A
Anthony PERARD 已提交
2168
        }
2169 2170 2171
        return;
    }
    if (is_write) {
A
Avi Kivity 已提交
2172
        address_space_write(as, bounce.addr, bounce.buffer, access_len);
2173
    }
2174
    qemu_vfree(bounce.buffer);
2175
    bounce.buffer = NULL;
2176
    cpu_notify_map_clients();
2177
}
B
bellard 已提交
2178

A
Avi Kivity 已提交
2179 2180
void *cpu_physical_memory_map(hwaddr addr,
                              hwaddr *plen,
A
Avi Kivity 已提交
2181 2182 2183 2184 2185
                              int is_write)
{
    return address_space_map(&address_space_memory, addr, plen, is_write);
}

A
Avi Kivity 已提交
2186 2187
void cpu_physical_memory_unmap(void *buffer, hwaddr len,
                               int is_write, hwaddr access_len)
A
Avi Kivity 已提交
2188 2189 2190 2191
{
    return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
}

B
bellard 已提交
2192
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2193
static inline uint32_t ldl_phys_internal(hwaddr addr,
2194
                                         enum device_endian endian)
B
bellard 已提交
2195 2196
{
    uint8_t *ptr;
2197
    uint64_t val;
2198
    MemoryRegionSection *section;
2199 2200
    hwaddr l = 4;
    hwaddr addr1;
B
bellard 已提交
2201

2202 2203
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      false);
2204
    if (l < 4 || !memory_access_is_direct(section->mr, false)) {
B
bellard 已提交
2205
        /* I/O case */
2206
        io_mem_read(section->mr, addr1, &val, 4);
2207 2208 2209 2210 2211 2212 2213 2214 2215
#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 已提交
2216 2217
    } else {
        /* RAM case */
2218
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2219
                                & TARGET_PAGE_MASK)
2220
                               + addr1);
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
        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 已提交
2232 2233 2234 2235
    }
    return val;
}

A
Avi Kivity 已提交
2236
uint32_t ldl_phys(hwaddr addr)
2237 2238 2239 2240
{
    return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2241
uint32_t ldl_le_phys(hwaddr addr)
2242 2243 2244 2245
{
    return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2246
uint32_t ldl_be_phys(hwaddr addr)
2247 2248 2249 2250
{
    return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2251
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2252
static inline uint64_t ldq_phys_internal(hwaddr addr,
2253
                                         enum device_endian endian)
B
bellard 已提交
2254 2255 2256
{
    uint8_t *ptr;
    uint64_t val;
2257
    MemoryRegionSection *section;
2258 2259
    hwaddr l = 8;
    hwaddr addr1;
B
bellard 已提交
2260

2261 2262
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      false);
2263
    if (l < 8 || !memory_access_is_direct(section->mr, false)) {
B
bellard 已提交
2264
        /* I/O case */
2265
        io_mem_read(section->mr, addr1, &val, 8);
2266 2267 2268 2269 2270 2271 2272 2273
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap64(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap64(val);
        }
B
bellard 已提交
2274 2275 2276
#endif
    } else {
        /* RAM case */
2277
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2278
                                & TARGET_PAGE_MASK)
2279
                               + addr1);
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
        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 已提交
2291 2292 2293 2294
    }
    return val;
}

A
Avi Kivity 已提交
2295
uint64_t ldq_phys(hwaddr addr)
2296 2297 2298 2299
{
    return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2300
uint64_t ldq_le_phys(hwaddr addr)
2301 2302 2303 2304
{
    return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2305
uint64_t ldq_be_phys(hwaddr addr)
2306 2307 2308 2309
{
    return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2310
/* XXX: optimize */
A
Avi Kivity 已提交
2311
uint32_t ldub_phys(hwaddr addr)
B
bellard 已提交
2312 2313 2314 2315 2316 2317
{
    uint8_t val;
    cpu_physical_memory_read(addr, &val, 1);
    return val;
}

2318
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2319
static inline uint32_t lduw_phys_internal(hwaddr addr,
2320
                                          enum device_endian endian)
B
bellard 已提交
2321
{
2322 2323
    uint8_t *ptr;
    uint64_t val;
2324
    MemoryRegionSection *section;
2325 2326
    hwaddr l = 2;
    hwaddr addr1;
2327

2328 2329
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      false);
2330
    if (l < 2 || !memory_access_is_direct(section->mr, false)) {
2331
        /* I/O case */
2332
        io_mem_read(section->mr, addr1, &val, 2);
2333 2334 2335 2336 2337 2338 2339 2340 2341
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap16(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap16(val);
        }
#endif
2342 2343
    } else {
        /* RAM case */
2344
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2345
                                & TARGET_PAGE_MASK)
2346
                               + addr1);
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
        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;
        }
2358 2359
    }
    return val;
B
bellard 已提交
2360 2361
}

A
Avi Kivity 已提交
2362
uint32_t lduw_phys(hwaddr addr)
2363 2364 2365 2366
{
    return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2367
uint32_t lduw_le_phys(hwaddr addr)
2368 2369 2370 2371
{
    return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2372
uint32_t lduw_be_phys(hwaddr addr)
2373 2374 2375 2376
{
    return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2377 2378 2379
/* 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 已提交
2380
void stl_phys_notdirty(hwaddr addr, uint32_t val)
B
bellard 已提交
2381 2382
{
    uint8_t *ptr;
2383
    MemoryRegionSection *section;
2384 2385
    hwaddr l = 4;
    hwaddr addr1;
B
bellard 已提交
2386

2387 2388
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      true);
2389
    if (l < 4 || !memory_access_is_direct(section->mr, true)) {
2390
        io_mem_write(section->mr, addr1, val, 4);
B
bellard 已提交
2391
    } else {
2392
        addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
P
pbrook 已提交
2393
        ptr = qemu_get_ram_ptr(addr1);
B
bellard 已提交
2394
        stl_p(ptr, val);
A
aliguori 已提交
2395 2396 2397 2398 2399 2400

        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 */
2401 2402
                cpu_physical_memory_set_dirty_flags(
                    addr1, (0xff & ~CODE_DIRTY_FLAG));
A
aliguori 已提交
2403 2404
            }
        }
B
bellard 已提交
2405 2406 2407 2408
    }
}

/* warning: addr must be aligned */
A
Avi Kivity 已提交
2409
static inline void stl_phys_internal(hwaddr addr, uint32_t val,
2410
                                     enum device_endian endian)
B
bellard 已提交
2411 2412
{
    uint8_t *ptr;
2413
    MemoryRegionSection *section;
2414 2415
    hwaddr l = 4;
    hwaddr addr1;
B
bellard 已提交
2416

2417 2418
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      true);
2419
    if (l < 4 || !memory_access_is_direct(section->mr, true)) {
2420 2421 2422 2423 2424 2425 2426 2427 2428
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap32(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap32(val);
        }
#endif
2429
        io_mem_write(section->mr, addr1, val, 4);
B
bellard 已提交
2430 2431
    } else {
        /* RAM case */
2432
        addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
P
pbrook 已提交
2433
        ptr = qemu_get_ram_ptr(addr1);
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
        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;
        }
2445
        invalidate_and_set_dirty(addr1, 4);
B
bellard 已提交
2446 2447 2448
    }
}

A
Avi Kivity 已提交
2449
void stl_phys(hwaddr addr, uint32_t val)
2450 2451 2452 2453
{
    stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2454
void stl_le_phys(hwaddr addr, uint32_t val)
2455 2456 2457 2458
{
    stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2459
void stl_be_phys(hwaddr addr, uint32_t val)
2460 2461 2462 2463
{
    stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2464
/* XXX: optimize */
A
Avi Kivity 已提交
2465
void stb_phys(hwaddr addr, uint32_t val)
B
bellard 已提交
2466 2467 2468 2469 2470
{
    uint8_t v = val;
    cpu_physical_memory_write(addr, &v, 1);
}

2471
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2472
static inline void stw_phys_internal(hwaddr addr, uint32_t val,
2473
                                     enum device_endian endian)
B
bellard 已提交
2474
{
2475
    uint8_t *ptr;
2476
    MemoryRegionSection *section;
2477 2478
    hwaddr l = 2;
    hwaddr addr1;
2479

2480 2481
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      true);
2482
    if (l < 2 || !memory_access_is_direct(section->mr, true)) {
2483 2484 2485 2486 2487 2488 2489 2490 2491
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap16(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap16(val);
        }
#endif
2492
        io_mem_write(section->mr, addr1, val, 2);
2493 2494
    } else {
        /* RAM case */
2495
        addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
2496
        ptr = qemu_get_ram_ptr(addr1);
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
        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;
        }
2508
        invalidate_and_set_dirty(addr1, 2);
2509
    }
B
bellard 已提交
2510 2511
}

A
Avi Kivity 已提交
2512
void stw_phys(hwaddr addr, uint32_t val)
2513 2514 2515 2516
{
    stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2517
void stw_le_phys(hwaddr addr, uint32_t val)
2518 2519 2520 2521
{
    stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2522
void stw_be_phys(hwaddr addr, uint32_t val)
2523 2524 2525 2526
{
    stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2527
/* XXX: optimize */
A
Avi Kivity 已提交
2528
void stq_phys(hwaddr addr, uint64_t val)
B
bellard 已提交
2529 2530
{
    val = tswap64(val);
2531
    cpu_physical_memory_write(addr, &val, 8);
B
bellard 已提交
2532 2533
}

A
Avi Kivity 已提交
2534
void stq_le_phys(hwaddr addr, uint64_t val)
2535 2536 2537 2538 2539
{
    val = cpu_to_le64(val);
    cpu_physical_memory_write(addr, &val, 8);
}

A
Avi Kivity 已提交
2540
void stq_be_phys(hwaddr addr, uint64_t val)
2541 2542 2543 2544 2545
{
    val = cpu_to_be64(val);
    cpu_physical_memory_write(addr, &val, 8);
}

2546
/* virtual memory access for debug (includes writing to ROM) */
2547
int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
2548
                        uint8_t *buf, int len, int is_write)
B
bellard 已提交
2549 2550
{
    int l;
A
Avi Kivity 已提交
2551
    hwaddr phys_addr;
2552
    target_ulong page;
B
bellard 已提交
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562

    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;
2563 2564 2565 2566 2567
        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 已提交
2568 2569 2570 2571 2572 2573
        len -= l;
        buf += l;
        addr += l;
    }
    return 0;
}
P
Paul Brook 已提交
2574
#endif
B
bellard 已提交
2575

2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
#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

2594
#ifndef CONFIG_USER_ONLY
A
Avi Kivity 已提交
2595
bool cpu_physical_memory_is_io(hwaddr phys_addr)
2596 2597
{
    MemoryRegionSection *section;
2598
    hwaddr l = 1;
2599

2600 2601
    section = address_space_translate(&address_space_memory,
                                      phys_addr, &phys_addr, &l, false);
2602 2603 2604 2605 2606

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