Statistics
| Branch: | Revision:

root / softmmu_template.h @ 97eb5b14

History | View | Annotate | Download (9.5 kB)

1 b92e5a22 bellard
/*
2 b92e5a22 bellard
 *  Software MMU support
3 b92e5a22 bellard
 * 
4 b92e5a22 bellard
 *  Copyright (c) 2003 Fabrice Bellard
5 b92e5a22 bellard
 *
6 b92e5a22 bellard
 * This library is free software; you can redistribute it and/or
7 b92e5a22 bellard
 * modify it under the terms of the GNU Lesser General Public
8 b92e5a22 bellard
 * License as published by the Free Software Foundation; either
9 b92e5a22 bellard
 * version 2 of the License, or (at your option) any later version.
10 b92e5a22 bellard
 *
11 b92e5a22 bellard
 * This library is distributed in the hope that it will be useful,
12 b92e5a22 bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 b92e5a22 bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 b92e5a22 bellard
 * Lesser General Public License for more details.
15 b92e5a22 bellard
 *
16 b92e5a22 bellard
 * You should have received a copy of the GNU Lesser General Public
17 b92e5a22 bellard
 * License along with this library; if not, write to the Free Software
18 b92e5a22 bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 b92e5a22 bellard
 */
20 b92e5a22 bellard
#define DATA_SIZE (1 << SHIFT)
21 b92e5a22 bellard
22 b92e5a22 bellard
#if DATA_SIZE == 8
23 b92e5a22 bellard
#define SUFFIX q
24 61382a50 bellard
#define USUFFIX q
25 b92e5a22 bellard
#define DATA_TYPE uint64_t
26 b92e5a22 bellard
#elif DATA_SIZE == 4
27 b92e5a22 bellard
#define SUFFIX l
28 61382a50 bellard
#define USUFFIX l
29 b92e5a22 bellard
#define DATA_TYPE uint32_t
30 b92e5a22 bellard
#elif DATA_SIZE == 2
31 b92e5a22 bellard
#define SUFFIX w
32 61382a50 bellard
#define USUFFIX uw
33 b92e5a22 bellard
#define DATA_TYPE uint16_t
34 b92e5a22 bellard
#elif DATA_SIZE == 1
35 b92e5a22 bellard
#define SUFFIX b
36 61382a50 bellard
#define USUFFIX ub
37 b92e5a22 bellard
#define DATA_TYPE uint8_t
38 b92e5a22 bellard
#else
39 b92e5a22 bellard
#error unsupported data size
40 b92e5a22 bellard
#endif
41 b92e5a22 bellard
42 61382a50 bellard
static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(unsigned long addr, 
43 61382a50 bellard
                                                        int is_user,
44 61382a50 bellard
                                                        void *retaddr);
45 61382a50 bellard
static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(unsigned long addr, 
46 61382a50 bellard
                                                   DATA_TYPE val, 
47 61382a50 bellard
                                                   int is_user,
48 61382a50 bellard
                                                   void *retaddr);
49 b92e5a22 bellard
50 b92e5a22 bellard
static inline DATA_TYPE glue(io_read, SUFFIX)(unsigned long physaddr, 
51 b92e5a22 bellard
                                              unsigned long tlb_addr)
52 b92e5a22 bellard
{
53 b92e5a22 bellard
    DATA_TYPE res;
54 b92e5a22 bellard
    int index;
55 b92e5a22 bellard
56 b92e5a22 bellard
    index = (tlb_addr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
57 b92e5a22 bellard
#if SHIFT <= 2
58 b92e5a22 bellard
    res = io_mem_read[index][SHIFT](physaddr);
59 b92e5a22 bellard
#else
60 b92e5a22 bellard
#ifdef TARGET_WORDS_BIGENDIAN
61 b92e5a22 bellard
    res = (uint64_t)io_mem_read[index][2](physaddr) << 32;
62 b92e5a22 bellard
    res |= io_mem_read[index][2](physaddr + 4);
63 b92e5a22 bellard
#else
64 b92e5a22 bellard
    res = io_mem_read[index][2](physaddr);
65 b92e5a22 bellard
    res |= (uint64_t)io_mem_read[index][2](physaddr + 4) << 32;
66 b92e5a22 bellard
#endif
67 b92e5a22 bellard
#endif /* SHIFT > 2 */
68 b92e5a22 bellard
    return res;
69 b92e5a22 bellard
}
70 b92e5a22 bellard
71 b92e5a22 bellard
static inline void glue(io_write, SUFFIX)(unsigned long physaddr, 
72 b92e5a22 bellard
                                          DATA_TYPE val,
73 b92e5a22 bellard
                                          unsigned long tlb_addr)
74 b92e5a22 bellard
{
75 b92e5a22 bellard
    int index;
76 b92e5a22 bellard
77 b92e5a22 bellard
    index = (tlb_addr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
78 b92e5a22 bellard
#if SHIFT <= 2
79 1ccde1cb bellard
    io_mem_write[index][SHIFT](physaddr, val, tlb_addr);
80 b92e5a22 bellard
#else
81 b92e5a22 bellard
#ifdef TARGET_WORDS_BIGENDIAN
82 1ccde1cb bellard
    io_mem_write[index][2](physaddr, val >> 32, tlb_addr);
83 1ccde1cb bellard
    io_mem_write[index][2](physaddr + 4, val, tlb_addr);
84 b92e5a22 bellard
#else
85 1ccde1cb bellard
    io_mem_write[index][2](physaddr, val, tlb_addr);
86 1ccde1cb bellard
    io_mem_write[index][2](physaddr + 4, val >> 32, tlb_addr);
87 b92e5a22 bellard
#endif
88 b92e5a22 bellard
#endif /* SHIFT > 2 */
89 b92e5a22 bellard
}
90 b92e5a22 bellard
91 b92e5a22 bellard
/* handle all cases except unaligned access which span two pages */
92 61382a50 bellard
DATA_TYPE REGPARM(1) glue(glue(__ld, SUFFIX), MMUSUFFIX)(unsigned long addr,
93 61382a50 bellard
                                                         int is_user)
94 b92e5a22 bellard
{
95 b92e5a22 bellard
    DATA_TYPE res;
96 61382a50 bellard
    int index;
97 b92e5a22 bellard
    unsigned long physaddr, tlb_addr;
98 b92e5a22 bellard
    void *retaddr;
99 b92e5a22 bellard
    
100 b92e5a22 bellard
    /* test if there is match for unaligned or IO access */
101 b92e5a22 bellard
    /* XXX: could done more in memory macro in a non portable way */
102 b92e5a22 bellard
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
103 b92e5a22 bellard
 redo:
104 b92e5a22 bellard
    tlb_addr = env->tlb_read[is_user][index].address;
105 b92e5a22 bellard
    if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
106 b92e5a22 bellard
        physaddr = addr + env->tlb_read[is_user][index].addend;
107 b92e5a22 bellard
        if (tlb_addr & ~TARGET_PAGE_MASK) {
108 b92e5a22 bellard
            /* IO access */
109 b92e5a22 bellard
            if ((addr & (DATA_SIZE - 1)) != 0)
110 b92e5a22 bellard
                goto do_unaligned_access;
111 b92e5a22 bellard
            res = glue(io_read, SUFFIX)(physaddr, tlb_addr);
112 b92e5a22 bellard
        } else if (((addr & 0xfff) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
113 b92e5a22 bellard
            /* slow unaligned access (it spans two pages or IO) */
114 b92e5a22 bellard
        do_unaligned_access:
115 61382a50 bellard
            retaddr = GETPC();
116 61382a50 bellard
            res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr, 
117 61382a50 bellard
                                                         is_user, retaddr);
118 b92e5a22 bellard
        } else {
119 b92e5a22 bellard
            /* unaligned access in the same page */
120 61382a50 bellard
            res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)physaddr);
121 b92e5a22 bellard
        }
122 b92e5a22 bellard
    } else {
123 b92e5a22 bellard
        /* the page is not in the TLB : fill it */
124 61382a50 bellard
        retaddr = GETPC();
125 61382a50 bellard
        tlb_fill(addr, 0, is_user, retaddr);
126 b92e5a22 bellard
        goto redo;
127 b92e5a22 bellard
    }
128 b92e5a22 bellard
    return res;
129 b92e5a22 bellard
}
130 b92e5a22 bellard
131 b92e5a22 bellard
/* handle all unaligned cases */
132 61382a50 bellard
static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(unsigned long addr, 
133 61382a50 bellard
                                                        int is_user,
134 61382a50 bellard
                                                        void *retaddr)
135 b92e5a22 bellard
{
136 b92e5a22 bellard
    DATA_TYPE res, res1, res2;
137 61382a50 bellard
    int index, shift;
138 b92e5a22 bellard
    unsigned long physaddr, tlb_addr, addr1, addr2;
139 b92e5a22 bellard
140 b92e5a22 bellard
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
141 b92e5a22 bellard
 redo:
142 b92e5a22 bellard
    tlb_addr = env->tlb_read[is_user][index].address;
143 b92e5a22 bellard
    if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
144 b92e5a22 bellard
        physaddr = addr + env->tlb_read[is_user][index].addend;
145 b92e5a22 bellard
        if (tlb_addr & ~TARGET_PAGE_MASK) {
146 b92e5a22 bellard
            /* IO access */
147 b92e5a22 bellard
            if ((addr & (DATA_SIZE - 1)) != 0)
148 b92e5a22 bellard
                goto do_unaligned_access;
149 b92e5a22 bellard
            res = glue(io_read, SUFFIX)(physaddr, tlb_addr);
150 b92e5a22 bellard
        } else if (((addr & 0xfff) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
151 b92e5a22 bellard
        do_unaligned_access:
152 b92e5a22 bellard
            /* slow unaligned access (it spans two pages) */
153 b92e5a22 bellard
            addr1 = addr & ~(DATA_SIZE - 1);
154 b92e5a22 bellard
            addr2 = addr1 + DATA_SIZE;
155 61382a50 bellard
            res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr1, 
156 61382a50 bellard
                                                          is_user, retaddr);
157 61382a50 bellard
            res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr2, 
158 61382a50 bellard
                                                          is_user, retaddr);
159 b92e5a22 bellard
            shift = (addr & (DATA_SIZE - 1)) * 8;
160 b92e5a22 bellard
#ifdef TARGET_WORDS_BIGENDIAN
161 b92e5a22 bellard
            res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
162 b92e5a22 bellard
#else
163 b92e5a22 bellard
            res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
164 b92e5a22 bellard
#endif
165 6986f88c bellard
            res = (DATA_TYPE)res;
166 b92e5a22 bellard
        } else {
167 b92e5a22 bellard
            /* unaligned/aligned access in the same page */
168 61382a50 bellard
            res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)physaddr);
169 b92e5a22 bellard
        }
170 b92e5a22 bellard
    } else {
171 b92e5a22 bellard
        /* the page is not in the TLB : fill it */
172 61382a50 bellard
        tlb_fill(addr, 0, is_user, retaddr);
173 b92e5a22 bellard
        goto redo;
174 b92e5a22 bellard
    }
175 b92e5a22 bellard
    return res;
176 b92e5a22 bellard
}
177 b92e5a22 bellard
178 b92e5a22 bellard
179 61382a50 bellard
void REGPARM(2) glue(glue(__st, SUFFIX), MMUSUFFIX)(unsigned long addr, 
180 61382a50 bellard
                                                    DATA_TYPE val,
181 61382a50 bellard
                                                    int is_user)
182 b92e5a22 bellard
{
183 b92e5a22 bellard
    unsigned long physaddr, tlb_addr;
184 b92e5a22 bellard
    void *retaddr;
185 61382a50 bellard
    int index;
186 b92e5a22 bellard
    
187 b92e5a22 bellard
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
188 b92e5a22 bellard
 redo:
189 b92e5a22 bellard
    tlb_addr = env->tlb_write[is_user][index].address;
190 b92e5a22 bellard
    if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
191 4ad06a29 bellard
        physaddr = addr + env->tlb_write[is_user][index].addend;
192 b92e5a22 bellard
        if (tlb_addr & ~TARGET_PAGE_MASK) {
193 b92e5a22 bellard
            /* IO access */
194 b92e5a22 bellard
            if ((addr & (DATA_SIZE - 1)) != 0)
195 b92e5a22 bellard
                goto do_unaligned_access;
196 b92e5a22 bellard
            glue(io_write, SUFFIX)(physaddr, val, tlb_addr);
197 b92e5a22 bellard
        } else if (((addr & 0xfff) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
198 b92e5a22 bellard
        do_unaligned_access:
199 61382a50 bellard
            retaddr = GETPC();
200 61382a50 bellard
            glue(glue(slow_st, SUFFIX), MMUSUFFIX)(addr, val, 
201 61382a50 bellard
                                                   is_user, retaddr);
202 b92e5a22 bellard
        } else {
203 b92e5a22 bellard
            /* aligned/unaligned access in the same page */
204 b92e5a22 bellard
            glue(glue(st, SUFFIX), _raw)((uint8_t *)physaddr, val);
205 b92e5a22 bellard
        }
206 b92e5a22 bellard
    } else {
207 b92e5a22 bellard
        /* the page is not in the TLB : fill it */
208 61382a50 bellard
        retaddr = GETPC();
209 61382a50 bellard
        tlb_fill(addr, 1, is_user, retaddr);
210 b92e5a22 bellard
        goto redo;
211 b92e5a22 bellard
    }
212 b92e5a22 bellard
}
213 b92e5a22 bellard
214 b92e5a22 bellard
/* handles all unaligned cases */
215 61382a50 bellard
static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(unsigned long addr, 
216 61382a50 bellard
                                                   DATA_TYPE val,
217 61382a50 bellard
                                                   int is_user,
218 61382a50 bellard
                                                   void *retaddr)
219 b92e5a22 bellard
{
220 b92e5a22 bellard
    unsigned long physaddr, tlb_addr;
221 61382a50 bellard
    int index, i;
222 b92e5a22 bellard
223 b92e5a22 bellard
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
224 b92e5a22 bellard
 redo:
225 b92e5a22 bellard
    tlb_addr = env->tlb_write[is_user][index].address;
226 b92e5a22 bellard
    if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
227 4ad06a29 bellard
        physaddr = addr + env->tlb_write[is_user][index].addend;
228 b92e5a22 bellard
        if (tlb_addr & ~TARGET_PAGE_MASK) {
229 b92e5a22 bellard
            /* IO access */
230 b92e5a22 bellard
            if ((addr & (DATA_SIZE - 1)) != 0)
231 b92e5a22 bellard
                goto do_unaligned_access;
232 b92e5a22 bellard
            glue(io_write, SUFFIX)(physaddr, val, tlb_addr);
233 b92e5a22 bellard
        } else if (((addr & 0xfff) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
234 b92e5a22 bellard
        do_unaligned_access:
235 b92e5a22 bellard
            /* XXX: not efficient, but simple */
236 b92e5a22 bellard
            for(i = 0;i < DATA_SIZE; i++) {
237 b92e5a22 bellard
#ifdef TARGET_WORDS_BIGENDIAN
238 61382a50 bellard
                glue(slow_stb, MMUSUFFIX)(addr + i, val >> (((DATA_SIZE - 1) * 8) - (i * 8)), 
239 61382a50 bellard
                                          is_user, retaddr);
240 b92e5a22 bellard
#else
241 61382a50 bellard
                glue(slow_stb, MMUSUFFIX)(addr + i, val >> (i * 8), 
242 61382a50 bellard
                                          is_user, retaddr);
243 b92e5a22 bellard
#endif
244 b92e5a22 bellard
            }
245 b92e5a22 bellard
        } else {
246 b92e5a22 bellard
            /* aligned/unaligned access in the same page */
247 b92e5a22 bellard
            glue(glue(st, SUFFIX), _raw)((uint8_t *)physaddr, val);
248 b92e5a22 bellard
        }
249 b92e5a22 bellard
    } else {
250 b92e5a22 bellard
        /* the page is not in the TLB : fill it */
251 61382a50 bellard
        tlb_fill(addr, 1, is_user, retaddr);
252 b92e5a22 bellard
        goto redo;
253 b92e5a22 bellard
    }
254 b92e5a22 bellard
}
255 b92e5a22 bellard
256 b92e5a22 bellard
#undef SHIFT
257 b92e5a22 bellard
#undef DATA_TYPE
258 b92e5a22 bellard
#undef SUFFIX
259 61382a50 bellard
#undef USUFFIX
260 b92e5a22 bellard
#undef DATA_SIZE