softmmu_template.h 13.0 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 58 59 60 61 62 63 64 65 66 67 68 69 70
#ifndef CONFIG_TCG_PASS_AREG0
#define ENV_PARAM
#define ENV_VAR
#define CPU_PREFIX
#define HELPER_PREFIX __
#else
#define ENV_PARAM CPUArchState *env,
#define ENV_VAR env,
#define CPU_PREFIX cpu_
#define HELPER_PREFIX helper_
#endif

static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(ENV_PARAM
                                                        target_ulong addr,
71
                                                        int mmu_idx,
B
bellard 已提交
72
                                                        void *retaddr);
73 74
static inline DATA_TYPE glue(io_read, SUFFIX)(ENV_PARAM
                                              target_phys_addr_t physaddr,
P
pbrook 已提交
75 76
                                              target_ulong addr,
                                              void *retaddr)
77 78
{
    DATA_TYPE res;
79 80
    MemoryRegion *mr = iotlb_to_region(physaddr);

P
pbrook 已提交
81
    physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
P
pbrook 已提交
82
    env->mem_io_pc = (unsigned long)retaddr;
83 84 85
    if (mr != &io_mem_ram && mr != &io_mem_rom
        && mr != &io_mem_unassigned
        && mr != &io_mem_notdirty
P
pbrook 已提交
86 87 88
            && !can_do_io(env)) {
        cpu_io_recompile(env, retaddr);
    }
89

90
    env->mem_io_vaddr = addr;
91
#if SHIFT <= 2
92
    res = io_mem_read(mr, physaddr, 1 << SHIFT);
93 94
#else
#ifdef TARGET_WORDS_BIGENDIAN
95 96
    res = io_mem_read(mr, physaddr, 4) << 32;
    res |= io_mem_read(mr, physaddr + 4, 4);
97
#else
98 99
    res = io_mem_read(mr, physaddr, 4);
    res |= io_mem_read(mr, physaddr + 4, 4) << 32;
100 101 102 103 104 105
#endif
#endif /* SHIFT > 2 */
    return res;
}

/* handle all cases except unaligned access which span two pages */
106 107 108 109
DATA_TYPE
glue(glue(glue(HELPER_PREFIX, ld), SUFFIX), MMUSUFFIX)(ENV_PARAM
                                                       target_ulong addr,
                                                       int mmu_idx)
110 111
{
    DATA_TYPE res;
B
bellard 已提交
112
    int index;
B
bellard 已提交
113
    target_ulong tlb_addr;
114 115
    target_phys_addr_t ioaddr;
    unsigned long addend;
116
    void *retaddr;
117

118 119 120 121
    /* 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:
122
    tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
123 124 125 126 127
    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 已提交
128
            retaddr = GETPC();
129
            ioaddr = env->iotlb[mmu_idx][index];
130
            res = glue(io_read, SUFFIX)(ENV_VAR ioaddr, addr, retaddr);
B
bellard 已提交
131
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
132 133
            /* slow unaligned access (it spans two pages or IO) */
        do_unaligned_access:
B
bellard 已提交
134
            retaddr = GETPC();
135
#ifdef ALIGNED_ONLY
136
            do_unaligned_access(ENV_VAR addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
137
#endif
138
            res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(ENV_VAR addr,
139
                                                         mmu_idx, retaddr);
140
        } else {
141 142 143 144
            /* unaligned/aligned access in the same page */
#ifdef ALIGNED_ONLY
            if ((addr & (DATA_SIZE - 1)) != 0) {
                retaddr = GETPC();
145
                do_unaligned_access(ENV_VAR addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
146 147
            }
#endif
P
pbrook 已提交
148 149
            addend = env->tlb_table[mmu_idx][index].addend;
            res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
150 151 152
        }
    } else {
        /* the page is not in the TLB : fill it */
B
bellard 已提交
153
        retaddr = GETPC();
154 155
#ifdef ALIGNED_ONLY
        if ((addr & (DATA_SIZE - 1)) != 0)
156
            do_unaligned_access(ENV_VAR addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
157
#endif
158
        tlb_fill(env, addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
159 160 161 162 163 164
        goto redo;
    }
    return res;
}

/* handle all unaligned cases */
165 166 167 168 169
static DATA_TYPE
glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(ENV_PARAM
                                       target_ulong addr,
                                       int mmu_idx,
                                       void *retaddr)
170 171
{
    DATA_TYPE res, res1, res2;
B
bellard 已提交
172
    int index, shift;
173 174
    target_phys_addr_t ioaddr;
    unsigned long addend;
B
bellard 已提交
175
    target_ulong tlb_addr, addr1, addr2;
176 177 178

    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
179
    tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
180 181 182 183 184
    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;
185
            ioaddr = env->iotlb[mmu_idx][index];
186
            res = glue(io_read, SUFFIX)(ENV_VAR ioaddr, addr, retaddr);
B
bellard 已提交
187
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
188 189 190 191
        do_unaligned_access:
            /* slow unaligned access (it spans two pages) */
            addr1 = addr & ~(DATA_SIZE - 1);
            addr2 = addr1 + DATA_SIZE;
192
            res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(ENV_VAR addr1,
193
                                                          mmu_idx, retaddr);
194
            res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(ENV_VAR addr2,
195
                                                          mmu_idx, retaddr);
196 197 198 199 200 201
            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 已提交
202
            res = (DATA_TYPE)res;
203 204
        } else {
            /* unaligned/aligned access in the same page */
P
pbrook 已提交
205 206
            addend = env->tlb_table[mmu_idx][index].addend;
            res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
207 208 209
        }
    } else {
        /* the page is not in the TLB : fill it */
210
        tlb_fill(env, addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
211 212 213 214 215
        goto redo;
    }
    return res;
}

B
bellard 已提交
216 217
#ifndef SOFTMMU_CODE_ACCESS

218 219
static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(ENV_PARAM
                                                   target_ulong addr,
220
                                                   DATA_TYPE val,
221
                                                   int mmu_idx,
B
bellard 已提交
222 223
                                                   void *retaddr);

224 225
static inline void glue(io_write, SUFFIX)(ENV_PARAM
                                          target_phys_addr_t physaddr,
B
bellard 已提交
226
                                          DATA_TYPE val,
P
pbrook 已提交
227
                                          target_ulong addr,
B
bellard 已提交
228 229
                                          void *retaddr)
{
230 231
    MemoryRegion *mr = iotlb_to_region(physaddr);

P
pbrook 已提交
232
    physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
233 234 235
    if (mr != &io_mem_ram && mr != &io_mem_rom
        && mr != &io_mem_unassigned
        && mr != &io_mem_notdirty
P
pbrook 已提交
236 237 238
            && !can_do_io(env)) {
        cpu_io_recompile(env, retaddr);
    }
B
bellard 已提交
239

P
pbrook 已提交
240 241
    env->mem_io_vaddr = addr;
    env->mem_io_pc = (unsigned long)retaddr;
B
bellard 已提交
242
#if SHIFT <= 2
243
    io_mem_write(mr, physaddr, val, 1 << SHIFT);
B
bellard 已提交
244 245
#else
#ifdef TARGET_WORDS_BIGENDIAN
246 247
    io_mem_write(mr, physaddr, (val >> 32), 4);
    io_mem_write(mr, physaddr + 4, (uint32_t)val, 4);
B
bellard 已提交
248
#else
249 250
    io_mem_write(mr, physaddr, (uint32_t)val, 4);
    io_mem_write(mr, physaddr + 4, val >> 32, 4);
B
bellard 已提交
251 252 253
#endif
#endif /* SHIFT > 2 */
}
254

255 256 257 258
void glue(glue(glue(HELPER_PREFIX, st), SUFFIX), MMUSUFFIX)(ENV_PARAM
                                                            target_ulong addr,
                                                            DATA_TYPE val,
                                                            int mmu_idx)
259
{
260 261
    target_phys_addr_t ioaddr;
    unsigned long addend;
B
bellard 已提交
262
    target_ulong tlb_addr;
263
    void *retaddr;
B
bellard 已提交
264
    int index;
265

266 267
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
268
    tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
269 270 271 272 273
    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 已提交
274
            retaddr = GETPC();
275
            ioaddr = env->iotlb[mmu_idx][index];
276
            glue(io_write, SUFFIX)(ENV_VAR ioaddr, val, addr, retaddr);
B
bellard 已提交
277
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
278
        do_unaligned_access:
B
bellard 已提交
279
            retaddr = GETPC();
280
#ifdef ALIGNED_ONLY
281
            do_unaligned_access(ENV_VAR addr, 1, mmu_idx, retaddr);
282
#endif
283
            glue(glue(slow_st, SUFFIX), MMUSUFFIX)(ENV_VAR addr, val,
284
                                                   mmu_idx, retaddr);
285 286
        } else {
            /* aligned/unaligned access in the same page */
287 288 289
#ifdef ALIGNED_ONLY
            if ((addr & (DATA_SIZE - 1)) != 0) {
                retaddr = GETPC();
290
                do_unaligned_access(ENV_VAR addr, 1, mmu_idx, retaddr);
291 292
            }
#endif
P
pbrook 已提交
293 294
            addend = env->tlb_table[mmu_idx][index].addend;
            glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
295 296 297
        }
    } else {
        /* the page is not in the TLB : fill it */
B
bellard 已提交
298
        retaddr = GETPC();
299 300
#ifdef ALIGNED_ONLY
        if ((addr & (DATA_SIZE - 1)) != 0)
301
            do_unaligned_access(ENV_VAR addr, 1, mmu_idx, retaddr);
302
#endif
303
        tlb_fill(env, addr, 1, mmu_idx, retaddr);
304 305 306 307 308
        goto redo;
    }
}

/* handles all unaligned cases */
309 310
static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(ENV_PARAM
                                                   target_ulong addr,
B
bellard 已提交
311
                                                   DATA_TYPE val,
312
                                                   int mmu_idx,
B
bellard 已提交
313
                                                   void *retaddr)
314
{
315 316
    target_phys_addr_t ioaddr;
    unsigned long addend;
B
bellard 已提交
317
    target_ulong tlb_addr;
B
bellard 已提交
318
    int index, i;
319 320 321

    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
322
    tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
323 324 325 326 327
    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;
328
            ioaddr = env->iotlb[mmu_idx][index];
329
            glue(io_write, SUFFIX)(ENV_VAR ioaddr, val, addr, retaddr);
B
bellard 已提交
330
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
331 332
        do_unaligned_access:
            /* XXX: not efficient, but simple */
B
balrog 已提交
333 334
            /* Note: relies on the fact that tlb_fill() does not remove the
             * previous page from the TLB cache.  */
335
            for(i = DATA_SIZE - 1; i >= 0; i--) {
336
#ifdef TARGET_WORDS_BIGENDIAN
337 338
                glue(slow_stb, MMUSUFFIX)(ENV_VAR addr + i,
                                          val >> (((DATA_SIZE - 1) * 8) - (i * 8)),
339
                                          mmu_idx, retaddr);
340
#else
341 342
                glue(slow_stb, MMUSUFFIX)(ENV_VAR addr + i,
                                          val >> (i * 8),
343
                                          mmu_idx, retaddr);
344 345 346 347
#endif
            }
        } else {
            /* aligned/unaligned access in the same page */
P
pbrook 已提交
348 349
            addend = env->tlb_table[mmu_idx][index].addend;
            glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
350 351 352
        }
    } else {
        /* the page is not in the TLB : fill it */
353
        tlb_fill(env, addr, 1, mmu_idx, retaddr);
354 355 356 357
        goto redo;
    }
}

B
bellard 已提交
358 359 360
#endif /* !defined(SOFTMMU_CODE_ACCESS) */

#undef READ_ACCESS_TYPE
361 362 363
#undef SHIFT
#undef DATA_TYPE
#undef SUFFIX
B
bellard 已提交
364
#undef USUFFIX
365
#undef DATA_SIZE
B
bellard 已提交
366
#undef ADDR_READ
367 368 369 370
#undef ENV_PARAM
#undef ENV_VAR
#undef CPU_PREFIX
#undef HELPER_PREFIX