softmmu_template.h 11.6 KB
Newer Older
1 2
/*
 *  Software MMU support
3
 *
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *  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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#define DATA_SIZE (1 << SHIFT)

#if DATA_SIZE == 8
#define SUFFIX q
B
bellard 已提交
24
#define USUFFIX q
25 26 27
#define DATA_TYPE uint64_t
#elif DATA_SIZE == 4
#define SUFFIX l
B
bellard 已提交
28
#define USUFFIX l
29 30 31
#define DATA_TYPE uint32_t
#elif DATA_SIZE == 2
#define SUFFIX w
B
bellard 已提交
32
#define USUFFIX uw
33 34 35
#define DATA_TYPE uint16_t
#elif DATA_SIZE == 1
#define SUFFIX b
B
bellard 已提交
36
#define USUFFIX ub
37 38 39 40 41
#define DATA_TYPE uint8_t
#else
#error unsupported data size
#endif

B
bellard 已提交
42 43
#ifdef SOFTMMU_CODE_ACCESS
#define READ_ACCESS_TYPE 2
B
bellard 已提交
44
#define ADDR_READ addr_code
B
bellard 已提交
45 46
#else
#define READ_ACCESS_TYPE 0
B
bellard 已提交
47
#define ADDR_READ addr_read
B
bellard 已提交
48 49
#endif

50
static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
51
                                                        int mmu_idx,
B
bellard 已提交
52
                                                        void *retaddr);
53
static inline DATA_TYPE glue(io_read, SUFFIX)(target_phys_addr_t physaddr,
P
pbrook 已提交
54
                                              target_ulong addr)
55 56 57
{
    DATA_TYPE res;
    int index;
P
pbrook 已提交
58 59
    index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
    physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
60 61

#if SHIFT <= 2
B
bellard 已提交
62
    res = io_mem_read[index][SHIFT](io_mem_opaque[index], physaddr);
63 64
#else
#ifdef TARGET_WORDS_BIGENDIAN
B
bellard 已提交
65 66
    res = (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr) << 32;
    res |= io_mem_read[index][2](io_mem_opaque[index], physaddr + 4);
67
#else
B
bellard 已提交
68 69
    res = io_mem_read[index][2](io_mem_opaque[index], physaddr);
    res |= (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr + 4) << 32;
70 71
#endif
#endif /* SHIFT > 2 */
B
bellard 已提交
72 73 74
#ifdef USE_KQEMU
    env->last_io_time = cpu_get_time_fast();
#endif
75 76 77 78
    return res;
}

/* handle all cases except unaligned access which span two pages */
79 80
DATA_TYPE REGPARM glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
                                                      int mmu_idx)
81 82
{
    DATA_TYPE res;
B
bellard 已提交
83
    int index;
B
bellard 已提交
84
    target_ulong tlb_addr;
P
pbrook 已提交
85
    target_phys_addr_t addend;
86
    void *retaddr;
87

88 89 90 91
    /* 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:
92
    tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
93 94 95 96 97
    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 已提交
98 99
            addend = env->iotlb[mmu_idx][index];
            res = glue(io_read, SUFFIX)(addend, addr);
B
bellard 已提交
100
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
101 102
            /* slow unaligned access (it spans two pages or IO) */
        do_unaligned_access:
B
bellard 已提交
103
            retaddr = GETPC();
104
#ifdef ALIGNED_ONLY
105
            do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
106
#endif
107
            res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr,
108
                                                         mmu_idx, retaddr);
109
        } else {
110 111 112 113
            /* unaligned/aligned access in the same page */
#ifdef ALIGNED_ONLY
            if ((addr & (DATA_SIZE - 1)) != 0) {
                retaddr = GETPC();
114
                do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
115 116
            }
#endif
P
pbrook 已提交
117 118
            addend = env->tlb_table[mmu_idx][index].addend;
            res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
119 120 121
        }
    } else {
        /* the page is not in the TLB : fill it */
B
bellard 已提交
122
        retaddr = GETPC();
123 124
#ifdef ALIGNED_ONLY
        if ((addr & (DATA_SIZE - 1)) != 0)
125
            do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
126
#endif
127
        tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
128 129 130 131 132 133
        goto redo;
    }
    return res;
}

/* handle all unaligned cases */
134
static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
135
                                                        int mmu_idx,
B
bellard 已提交
136
                                                        void *retaddr)
137 138
{
    DATA_TYPE res, res1, res2;
B
bellard 已提交
139
    int index, shift;
P
pbrook 已提交
140
    target_phys_addr_t addend;
B
bellard 已提交
141
    target_ulong tlb_addr, addr1, addr2;
142 143 144

    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
145
    tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
146 147 148 149 150
    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 已提交
151 152
            addend = env->iotlb[mmu_idx][index];
            res = glue(io_read, SUFFIX)(addend, addr);
B
bellard 已提交
153
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
154 155 156 157
        do_unaligned_access:
            /* slow unaligned access (it spans two pages) */
            addr1 = addr & ~(DATA_SIZE - 1);
            addr2 = addr1 + DATA_SIZE;
158
            res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr1,
159
                                                          mmu_idx, retaddr);
160
            res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr2,
161
                                                          mmu_idx, retaddr);
162 163 164 165 166 167
            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 已提交
168
            res = (DATA_TYPE)res;
169 170
        } else {
            /* unaligned/aligned access in the same page */
P
pbrook 已提交
171 172
            addend = env->tlb_table[mmu_idx][index].addend;
            res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
173 174 175
        }
    } else {
        /* the page is not in the TLB : fill it */
176
        tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
177 178 179 180 181
        goto redo;
    }
    return res;
}

B
bellard 已提交
182 183
#ifndef SOFTMMU_CODE_ACCESS

184 185
static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
                                                   DATA_TYPE val,
186
                                                   int mmu_idx,
B
bellard 已提交
187 188
                                                   void *retaddr);

189
static inline void glue(io_write, SUFFIX)(target_phys_addr_t physaddr,
B
bellard 已提交
190
                                          DATA_TYPE val,
P
pbrook 已提交
191
                                          target_ulong addr,
B
bellard 已提交
192 193 194
                                          void *retaddr)
{
    int index;
P
pbrook 已提交
195 196
    index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
    physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
B
bellard 已提交
197

P
pbrook 已提交
198
    env->mem_write_vaddr = addr;
B
bellard 已提交
199 200 201 202 203 204 205 206 207 208 209 210
    env->mem_write_pc = (unsigned long)retaddr;
#if SHIFT <= 2
    io_mem_write[index][SHIFT](io_mem_opaque[index], physaddr, val);
#else
#ifdef TARGET_WORDS_BIGENDIAN
    io_mem_write[index][2](io_mem_opaque[index], physaddr, val >> 32);
    io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val);
#else
    io_mem_write[index][2](io_mem_opaque[index], physaddr, val);
    io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val >> 32);
#endif
#endif /* SHIFT > 2 */
B
bellard 已提交
211 212 213
#ifdef USE_KQEMU
    env->last_io_time = cpu_get_time_fast();
#endif
B
bellard 已提交
214
}
215

216 217 218
void REGPARM glue(glue(__st, SUFFIX), MMUSUFFIX)(target_ulong addr,
                                                 DATA_TYPE val,
                                                 int mmu_idx)
219
{
P
pbrook 已提交
220
    target_phys_addr_t addend;
B
bellard 已提交
221
    target_ulong tlb_addr;
222
    void *retaddr;
B
bellard 已提交
223
    int index;
224

225 226
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
227
    tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
228 229 230 231 232
    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 已提交
233
            retaddr = GETPC();
P
pbrook 已提交
234 235
            addend = env->iotlb[mmu_idx][index];
            glue(io_write, SUFFIX)(addend, val, addr, retaddr);
B
bellard 已提交
236
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
237
        do_unaligned_access:
B
bellard 已提交
238
            retaddr = GETPC();
239
#ifdef ALIGNED_ONLY
240
            do_unaligned_access(addr, 1, mmu_idx, retaddr);
241
#endif
242
            glue(glue(slow_st, SUFFIX), MMUSUFFIX)(addr, val,
243
                                                   mmu_idx, retaddr);
244 245
        } else {
            /* aligned/unaligned access in the same page */
246 247 248
#ifdef ALIGNED_ONLY
            if ((addr & (DATA_SIZE - 1)) != 0) {
                retaddr = GETPC();
249
                do_unaligned_access(addr, 1, mmu_idx, retaddr);
250 251
            }
#endif
P
pbrook 已提交
252 253
            addend = env->tlb_table[mmu_idx][index].addend;
            glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
254 255 256
        }
    } else {
        /* the page is not in the TLB : fill it */
B
bellard 已提交
257
        retaddr = GETPC();
258 259
#ifdef ALIGNED_ONLY
        if ((addr & (DATA_SIZE - 1)) != 0)
260
            do_unaligned_access(addr, 1, mmu_idx, retaddr);
261
#endif
262
        tlb_fill(addr, 1, mmu_idx, retaddr);
263 264 265 266 267
        goto redo;
    }
}

/* handles all unaligned cases */
268
static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
B
bellard 已提交
269
                                                   DATA_TYPE val,
270
                                                   int mmu_idx,
B
bellard 已提交
271
                                                   void *retaddr)
272
{
P
pbrook 已提交
273
    target_phys_addr_t addend;
B
bellard 已提交
274
    target_ulong tlb_addr;
B
bellard 已提交
275
    int index, i;
276 277 278

    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 redo:
279
    tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
280 281 282 283 284
    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 已提交
285 286
            addend = env->iotlb[mmu_idx][index];
            glue(io_write, SUFFIX)(addend, val, addr, retaddr);
B
bellard 已提交
287
        } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
288 289
        do_unaligned_access:
            /* XXX: not efficient, but simple */
B
balrog 已提交
290 291
            /* Note: relies on the fact that tlb_fill() does not remove the
             * previous page from the TLB cache.  */
292
            for(i = DATA_SIZE - 1; i >= 0; i--) {
293
#ifdef TARGET_WORDS_BIGENDIAN
294
                glue(slow_stb, MMUSUFFIX)(addr + i, val >> (((DATA_SIZE - 1) * 8) - (i * 8)),
295
                                          mmu_idx, retaddr);
296
#else
297
                glue(slow_stb, MMUSUFFIX)(addr + i, val >> (i * 8),
298
                                          mmu_idx, retaddr);
299 300 301 302
#endif
            }
        } else {
            /* aligned/unaligned access in the same page */
P
pbrook 已提交
303 304
            addend = env->tlb_table[mmu_idx][index].addend;
            glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
305 306 307
        }
    } else {
        /* the page is not in the TLB : fill it */
308
        tlb_fill(addr, 1, mmu_idx, retaddr);
309 310 311 312
        goto redo;
    }
}

B
bellard 已提交
313 314 315
#endif /* !defined(SOFTMMU_CODE_ACCESS) */

#undef READ_ACCESS_TYPE
316 317 318
#undef SHIFT
#undef DATA_TYPE
#undef SUFFIX
B
bellard 已提交
319
#undef USUFFIX
320
#undef DATA_SIZE
B
bellard 已提交
321
#undef ADDR_READ