softmmu_template.h 12.7 KB
Newer Older
1 2
/*
 *  Software MMU support
3
 *
B
Blue Swirl 已提交
4 5 6 7 8
 * Generate helpers used by TCG for qemu_ld/st ops and code load
 * functions.
 *
 * Included from target op helpers and exec.c.
 *
9 10 11 12 13 14 15 16 17 18 19 20 21
 *  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
22
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23
 */
B
Blue Swirl 已提交
24
#include "qemu-timer.h"
25
#include "memory.h"
B
Blue Swirl 已提交
26

27 28 29 30
#define DATA_SIZE (1 << SHIFT)

#if DATA_SIZE == 8
#define SUFFIX q
B
bellard 已提交
31
#define USUFFIX q
32 33 34
#define DATA_TYPE uint64_t
#elif DATA_SIZE == 4
#define SUFFIX l
B
bellard 已提交
35
#define USUFFIX l
36 37 38
#define DATA_TYPE uint32_t
#elif DATA_SIZE == 2
#define SUFFIX w
B
bellard 已提交
39
#define USUFFIX uw
40 41 42
#define DATA_TYPE uint16_t
#elif DATA_SIZE == 1
#define SUFFIX b
B
bellard 已提交
43
#define USUFFIX ub
44 45 46 47 48
#define DATA_TYPE uint8_t
#else
#error unsupported data size
#endif

B
bellard 已提交
49 50
#ifdef SOFTMMU_CODE_ACCESS
#define READ_ACCESS_TYPE 2
B
bellard 已提交
51
#define ADDR_READ addr_code
B
bellard 已提交
52 53
#else
#define READ_ACCESS_TYPE 0
B
bellard 已提交
54
#define ADDR_READ addr_read
B
bellard 已提交
55 56
#endif

57
static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(CPUArchState *env,
58
                                                        target_ulong addr,
59
                                                        int mmu_idx,
60
                                                        uintptr_t retaddr);
61
static inline DATA_TYPE glue(io_read, SUFFIX)(CPUArchState *env,
62
                                              target_phys_addr_t physaddr,
P
pbrook 已提交
63
                                              target_ulong addr,
64
                                              uintptr_t retaddr)
65 66
{
    DATA_TYPE res;
67 68
    MemoryRegion *mr = iotlb_to_region(physaddr);

P
pbrook 已提交
69
    physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
70
    env->mem_io_pc = retaddr;
71 72 73
    if (mr != &io_mem_ram && mr != &io_mem_rom
        && mr != &io_mem_unassigned
        && mr != &io_mem_notdirty
P
pbrook 已提交
74 75 76
            && !can_do_io(env)) {
        cpu_io_recompile(env, retaddr);
    }
77

78
    env->mem_io_vaddr = addr;
79
#if SHIFT <= 2
80
    res = io_mem_read(mr, physaddr, 1 << SHIFT);
81 82
#else
#ifdef TARGET_WORDS_BIGENDIAN
83 84
    res = io_mem_read(mr, physaddr, 4) << 32;
    res |= io_mem_read(mr, physaddr + 4, 4);
85
#else
86 87
    res = io_mem_read(mr, physaddr, 4);
    res |= io_mem_read(mr, physaddr + 4, 4) << 32;
88 89 90 91 92 93
#endif
#endif /* SHIFT > 2 */
    return res;
}

/* handle all cases except unaligned access which span two pages */
94
DATA_TYPE
95 96
glue(glue(helper_ld, SUFFIX), MMUSUFFIX)(CPUArchState *env, target_ulong addr,
                                         int mmu_idx)
97 98
{
    DATA_TYPE res;
B
bellard 已提交
99
    int index;
B
bellard 已提交
100
    target_ulong tlb_addr;
101
    target_phys_addr_t ioaddr;
102
    uintptr_t retaddr;
103

104 105 106 107
    /* test if there is match for unaligned or IO access */
    /* XXX: could done more in memory macro in a non portable way */
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
108
    tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
109 110 111 112 113
    if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
        if (tlb_addr & ~TARGET_PAGE_MASK) {
            /* IO access */
            if ((addr & (DATA_SIZE - 1)) != 0)
                goto do_unaligned_access;
P
pbrook 已提交
114
            retaddr = GETPC();
115
            ioaddr = env->iotlb[mmu_idx][index];
116
            res = glue(io_read, SUFFIX)(env, ioaddr, addr, retaddr);
B
bellard 已提交
117
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
118 119
            /* slow unaligned access (it spans two pages or IO) */
        do_unaligned_access:
B
bellard 已提交
120
            retaddr = GETPC();
121
#ifdef ALIGNED_ONLY
122
            do_unaligned_access(env, addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
123
#endif
124
            res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(env, addr,
125
                                                         mmu_idx, retaddr);
126
        } else {
127
            /* unaligned/aligned access in the same page */
S
Stefan Weil 已提交
128
            uintptr_t addend;
129 130 131
#ifdef ALIGNED_ONLY
            if ((addr & (DATA_SIZE - 1)) != 0) {
                retaddr = GETPC();
132
                do_unaligned_access(env, addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
133 134
            }
#endif
P
pbrook 已提交
135
            addend = env->tlb_table[mmu_idx][index].addend;
S
Stefan Weil 已提交
136 137
            res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(intptr_t)
                                                (addr + addend));
138 139 140
        }
    } else {
        /* the page is not in the TLB : fill it */
B
bellard 已提交
141
        retaddr = GETPC();
142 143
#ifdef ALIGNED_ONLY
        if ((addr & (DATA_SIZE - 1)) != 0)
144
            do_unaligned_access(env, addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
145
#endif
146
        tlb_fill(env, addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
147 148 149 150 151 152
        goto redo;
    }
    return res;
}

/* handle all unaligned cases */
153
static DATA_TYPE
154
glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(CPUArchState *env,
155 156
                                       target_ulong addr,
                                       int mmu_idx,
157
                                       uintptr_t retaddr)
158 159
{
    DATA_TYPE res, res1, res2;
B
bellard 已提交
160
    int index, shift;
161
    target_phys_addr_t ioaddr;
B
bellard 已提交
162
    target_ulong tlb_addr, addr1, addr2;
163 164 165

    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
166
    tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
167 168 169 170 171
    if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
        if (tlb_addr & ~TARGET_PAGE_MASK) {
            /* IO access */
            if ((addr & (DATA_SIZE - 1)) != 0)
                goto do_unaligned_access;
172
            ioaddr = env->iotlb[mmu_idx][index];
173
            res = glue(io_read, SUFFIX)(env, ioaddr, addr, retaddr);
B
bellard 已提交
174
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
175 176 177 178
        do_unaligned_access:
            /* slow unaligned access (it spans two pages) */
            addr1 = addr & ~(DATA_SIZE - 1);
            addr2 = addr1 + DATA_SIZE;
179
            res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(env, addr1,
180
                                                          mmu_idx, retaddr);
181
            res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(env, addr2,
182
                                                          mmu_idx, retaddr);
183 184 185 186 187 188
            shift = (addr & (DATA_SIZE - 1)) * 8;
#ifdef TARGET_WORDS_BIGENDIAN
            res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
#else
            res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
#endif
B
bellard 已提交
189
            res = (DATA_TYPE)res;
190 191
        } else {
            /* unaligned/aligned access in the same page */
S
Stefan Weil 已提交
192 193 194
            uintptr_t addend = env->tlb_table[mmu_idx][index].addend;
            res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(intptr_t)
                                                (addr + addend));
195 196 197
        }
    } else {
        /* the page is not in the TLB : fill it */
198
        tlb_fill(env, addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
199 200 201 202 203
        goto redo;
    }
    return res;
}

B
bellard 已提交
204 205
#ifndef SOFTMMU_CODE_ACCESS

206
static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(CPUArchState *env,
207
                                                   target_ulong addr,
208
                                                   DATA_TYPE val,
209
                                                   int mmu_idx,
210
                                                   uintptr_t retaddr);
B
bellard 已提交
211

212
static inline void glue(io_write, SUFFIX)(CPUArchState *env,
213
                                          target_phys_addr_t physaddr,
B
bellard 已提交
214
                                          DATA_TYPE val,
P
pbrook 已提交
215
                                          target_ulong addr,
216
                                          uintptr_t retaddr)
B
bellard 已提交
217
{
218 219
    MemoryRegion *mr = iotlb_to_region(physaddr);

P
pbrook 已提交
220
    physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
221 222 223
    if (mr != &io_mem_ram && mr != &io_mem_rom
        && mr != &io_mem_unassigned
        && mr != &io_mem_notdirty
P
pbrook 已提交
224 225 226
            && !can_do_io(env)) {
        cpu_io_recompile(env, retaddr);
    }
B
bellard 已提交
227

P
pbrook 已提交
228
    env->mem_io_vaddr = addr;
229
    env->mem_io_pc = retaddr;
B
bellard 已提交
230
#if SHIFT <= 2
231
    io_mem_write(mr, physaddr, val, 1 << SHIFT);
B
bellard 已提交
232 233
#else
#ifdef TARGET_WORDS_BIGENDIAN
234 235
    io_mem_write(mr, physaddr, (val >> 32), 4);
    io_mem_write(mr, physaddr + 4, (uint32_t)val, 4);
B
bellard 已提交
236
#else
237 238
    io_mem_write(mr, physaddr, (uint32_t)val, 4);
    io_mem_write(mr, physaddr + 4, val >> 32, 4);
B
bellard 已提交
239 240 241
#endif
#endif /* SHIFT > 2 */
}
242

243 244 245
void glue(glue(helper_st, SUFFIX), MMUSUFFIX)(CPUArchState *env,
                                              target_ulong addr, DATA_TYPE val,
                                              int mmu_idx)
246
{
247
    target_phys_addr_t ioaddr;
B
bellard 已提交
248
    target_ulong tlb_addr;
249
    uintptr_t retaddr;
B
bellard 已提交
250
    int index;
251

252 253
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
254
    tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
255 256 257 258 259
    if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
        if (tlb_addr & ~TARGET_PAGE_MASK) {
            /* IO access */
            if ((addr & (DATA_SIZE - 1)) != 0)
                goto do_unaligned_access;
B
bellard 已提交
260
            retaddr = GETPC();
261
            ioaddr = env->iotlb[mmu_idx][index];
262
            glue(io_write, SUFFIX)(env, ioaddr, val, addr, retaddr);
B
bellard 已提交
263
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
264
        do_unaligned_access:
B
bellard 已提交
265
            retaddr = GETPC();
266
#ifdef ALIGNED_ONLY
267
            do_unaligned_access(env, addr, 1, mmu_idx, retaddr);
268
#endif
269
            glue(glue(slow_st, SUFFIX), MMUSUFFIX)(env, addr, val,
270
                                                   mmu_idx, retaddr);
271 272
        } else {
            /* aligned/unaligned access in the same page */
S
Stefan Weil 已提交
273
            uintptr_t addend;
274 275 276
#ifdef ALIGNED_ONLY
            if ((addr & (DATA_SIZE - 1)) != 0) {
                retaddr = GETPC();
277
                do_unaligned_access(env, addr, 1, mmu_idx, retaddr);
278 279
            }
#endif
P
pbrook 已提交
280
            addend = env->tlb_table[mmu_idx][index].addend;
S
Stefan Weil 已提交
281 282
            glue(glue(st, SUFFIX), _raw)((uint8_t *)(intptr_t)
                                         (addr + addend), val);
283 284 285
        }
    } else {
        /* the page is not in the TLB : fill it */
B
bellard 已提交
286
        retaddr = GETPC();
287 288
#ifdef ALIGNED_ONLY
        if ((addr & (DATA_SIZE - 1)) != 0)
289
            do_unaligned_access(env, addr, 1, mmu_idx, retaddr);
290
#endif
291
        tlb_fill(env, addr, 1, mmu_idx, retaddr);
292 293 294 295 296
        goto redo;
    }
}

/* handles all unaligned cases */
297
static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(CPUArchState *env,
298
                                                   target_ulong addr,
B
bellard 已提交
299
                                                   DATA_TYPE val,
300
                                                   int mmu_idx,
301
                                                   uintptr_t retaddr)
302
{
303
    target_phys_addr_t ioaddr;
B
bellard 已提交
304
    target_ulong tlb_addr;
B
bellard 已提交
305
    int index, i;
306 307 308

    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
309
    tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
310 311 312 313 314
    if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
        if (tlb_addr & ~TARGET_PAGE_MASK) {
            /* IO access */
            if ((addr & (DATA_SIZE - 1)) != 0)
                goto do_unaligned_access;
315
            ioaddr = env->iotlb[mmu_idx][index];
316
            glue(io_write, SUFFIX)(env, ioaddr, val, addr, retaddr);
B
bellard 已提交
317
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
318 319
        do_unaligned_access:
            /* XXX: not efficient, but simple */
B
balrog 已提交
320 321
            /* Note: relies on the fact that tlb_fill() does not remove the
             * previous page from the TLB cache.  */
322
            for(i = DATA_SIZE - 1; i >= 0; i--) {
323
#ifdef TARGET_WORDS_BIGENDIAN
324
                glue(slow_stb, MMUSUFFIX)(env, addr + i,
325
                                          val >> (((DATA_SIZE - 1) * 8) - (i * 8)),
326
                                          mmu_idx, retaddr);
327
#else
328
                glue(slow_stb, MMUSUFFIX)(env, addr + i,
329
                                          val >> (i * 8),
330
                                          mmu_idx, retaddr);
331 332 333 334
#endif
            }
        } else {
            /* aligned/unaligned access in the same page */
S
Stefan Weil 已提交
335 336 337
            uintptr_t addend = env->tlb_table[mmu_idx][index].addend;
            glue(glue(st, SUFFIX), _raw)((uint8_t *)(intptr_t)
                                         (addr + addend), val);
338 339 340
        }
    } else {
        /* the page is not in the TLB : fill it */
341
        tlb_fill(env, addr, 1, mmu_idx, retaddr);
342 343 344 345
        goto redo;
    }
}

B
bellard 已提交
346 347 348
#endif /* !defined(SOFTMMU_CODE_ACCESS) */

#undef READ_ACCESS_TYPE
349 350 351
#undef SHIFT
#undef DATA_TYPE
#undef SUFFIX
B
bellard 已提交
352
#undef USUFFIX
353
#undef DATA_SIZE
B
bellard 已提交
354
#undef ADDR_READ