root / softmmu_template.h @ 046d6672
History | View | Annotate | Download (9.6 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 | d720b93d | bellard | unsigned long tlb_addr, |
74 | d720b93d | bellard | void *retaddr)
|
75 | b92e5a22 | bellard | { |
76 | b92e5a22 | bellard | int index;
|
77 | b92e5a22 | bellard | |
78 | b92e5a22 | bellard | index = (tlb_addr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
|
79 | d720b93d | bellard | env->mem_write_vaddr = tlb_addr; |
80 | d720b93d | bellard | env->mem_write_pc = (unsigned long)retaddr; |
81 | b92e5a22 | bellard | #if SHIFT <= 2 |
82 | d720b93d | bellard | io_mem_write[index][SHIFT](physaddr, val); |
83 | b92e5a22 | bellard | #else
|
84 | b92e5a22 | bellard | #ifdef TARGET_WORDS_BIGENDIAN
|
85 | d720b93d | bellard | io_mem_write[index][2](physaddr, val >> 32); |
86 | d720b93d | bellard | io_mem_write[index][2](physaddr + 4, val); |
87 | b92e5a22 | bellard | #else
|
88 | d720b93d | bellard | io_mem_write[index][2](physaddr, val);
|
89 | d720b93d | bellard | io_mem_write[index][2](physaddr + 4, val >> 32); |
90 | b92e5a22 | bellard | #endif
|
91 | b92e5a22 | bellard | #endif /* SHIFT > 2 */ |
92 | b92e5a22 | bellard | } |
93 | b92e5a22 | bellard | |
94 | b92e5a22 | bellard | /* handle all cases except unaligned access which span two pages */
|
95 | 61382a50 | bellard | DATA_TYPE REGPARM(1) glue(glue(__ld, SUFFIX), MMUSUFFIX)(unsigned long addr, |
96 | 61382a50 | bellard | int is_user)
|
97 | b92e5a22 | bellard | { |
98 | b92e5a22 | bellard | DATA_TYPE res; |
99 | 61382a50 | bellard | int index;
|
100 | b92e5a22 | bellard | unsigned long physaddr, tlb_addr; |
101 | b92e5a22 | bellard | void *retaddr;
|
102 | b92e5a22 | bellard | |
103 | b92e5a22 | bellard | /* test if there is match for unaligned or IO access */
|
104 | b92e5a22 | bellard | /* XXX: could done more in memory macro in a non portable way */
|
105 | b92e5a22 | bellard | index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
|
106 | b92e5a22 | bellard | redo:
|
107 | b92e5a22 | bellard | tlb_addr = env->tlb_read[is_user][index].address; |
108 | b92e5a22 | bellard | if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
|
109 | b92e5a22 | bellard | physaddr = addr + env->tlb_read[is_user][index].addend; |
110 | b92e5a22 | bellard | if (tlb_addr & ~TARGET_PAGE_MASK) {
|
111 | b92e5a22 | bellard | /* IO access */
|
112 | b92e5a22 | bellard | if ((addr & (DATA_SIZE - 1)) != 0) |
113 | b92e5a22 | bellard | goto do_unaligned_access;
|
114 | b92e5a22 | bellard | res = glue(io_read, SUFFIX)(physaddr, tlb_addr); |
115 | b92e5a22 | bellard | } else if (((addr & 0xfff) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) { |
116 | b92e5a22 | bellard | /* slow unaligned access (it spans two pages or IO) */
|
117 | b92e5a22 | bellard | do_unaligned_access:
|
118 | 61382a50 | bellard | retaddr = GETPC(); |
119 | 61382a50 | bellard | res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr, |
120 | 61382a50 | bellard | is_user, retaddr); |
121 | b92e5a22 | bellard | } else {
|
122 | b92e5a22 | bellard | /* unaligned access in the same page */
|
123 | 61382a50 | bellard | res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)physaddr); |
124 | b92e5a22 | bellard | } |
125 | b92e5a22 | bellard | } else {
|
126 | b92e5a22 | bellard | /* the page is not in the TLB : fill it */
|
127 | 61382a50 | bellard | retaddr = GETPC(); |
128 | 61382a50 | bellard | tlb_fill(addr, 0, is_user, retaddr);
|
129 | b92e5a22 | bellard | goto redo;
|
130 | b92e5a22 | bellard | } |
131 | b92e5a22 | bellard | return res;
|
132 | b92e5a22 | bellard | } |
133 | b92e5a22 | bellard | |
134 | b92e5a22 | bellard | /* handle all unaligned cases */
|
135 | 61382a50 | bellard | static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(unsigned long addr, |
136 | 61382a50 | bellard | int is_user,
|
137 | 61382a50 | bellard | void *retaddr)
|
138 | b92e5a22 | bellard | { |
139 | b92e5a22 | bellard | DATA_TYPE res, res1, res2; |
140 | 61382a50 | bellard | int index, shift;
|
141 | b92e5a22 | bellard | unsigned long physaddr, tlb_addr, addr1, addr2; |
142 | b92e5a22 | bellard | |
143 | b92e5a22 | bellard | index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
|
144 | b92e5a22 | bellard | redo:
|
145 | b92e5a22 | bellard | tlb_addr = env->tlb_read[is_user][index].address; |
146 | b92e5a22 | bellard | if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
|
147 | b92e5a22 | bellard | physaddr = addr + env->tlb_read[is_user][index].addend; |
148 | b92e5a22 | bellard | if (tlb_addr & ~TARGET_PAGE_MASK) {
|
149 | b92e5a22 | bellard | /* IO access */
|
150 | b92e5a22 | bellard | if ((addr & (DATA_SIZE - 1)) != 0) |
151 | b92e5a22 | bellard | goto do_unaligned_access;
|
152 | b92e5a22 | bellard | res = glue(io_read, SUFFIX)(physaddr, tlb_addr); |
153 | b92e5a22 | bellard | } else if (((addr & 0xfff) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) { |
154 | b92e5a22 | bellard | do_unaligned_access:
|
155 | b92e5a22 | bellard | /* slow unaligned access (it spans two pages) */
|
156 | b92e5a22 | bellard | addr1 = addr & ~(DATA_SIZE - 1);
|
157 | b92e5a22 | bellard | addr2 = addr1 + DATA_SIZE; |
158 | 61382a50 | bellard | res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr1, |
159 | 61382a50 | bellard | is_user, retaddr); |
160 | 61382a50 | bellard | res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr2, |
161 | 61382a50 | bellard | is_user, retaddr); |
162 | b92e5a22 | bellard | shift = (addr & (DATA_SIZE - 1)) * 8; |
163 | b92e5a22 | bellard | #ifdef TARGET_WORDS_BIGENDIAN
|
164 | b92e5a22 | bellard | res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
|
165 | b92e5a22 | bellard | #else
|
166 | b92e5a22 | bellard | res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
|
167 | b92e5a22 | bellard | #endif
|
168 | 6986f88c | bellard | res = (DATA_TYPE)res; |
169 | b92e5a22 | bellard | } else {
|
170 | b92e5a22 | bellard | /* unaligned/aligned access in the same page */
|
171 | 61382a50 | bellard | res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)physaddr); |
172 | b92e5a22 | bellard | } |
173 | b92e5a22 | bellard | } else {
|
174 | b92e5a22 | bellard | /* the page is not in the TLB : fill it */
|
175 | 61382a50 | bellard | tlb_fill(addr, 0, is_user, retaddr);
|
176 | b92e5a22 | bellard | goto redo;
|
177 | b92e5a22 | bellard | } |
178 | b92e5a22 | bellard | return res;
|
179 | b92e5a22 | bellard | } |
180 | b92e5a22 | bellard | |
181 | b92e5a22 | bellard | |
182 | 61382a50 | bellard | void REGPARM(2) glue(glue(__st, SUFFIX), MMUSUFFIX)(unsigned long addr, |
183 | 61382a50 | bellard | DATA_TYPE val, |
184 | 61382a50 | bellard | int is_user)
|
185 | b92e5a22 | bellard | { |
186 | b92e5a22 | bellard | unsigned long physaddr, tlb_addr; |
187 | b92e5a22 | bellard | void *retaddr;
|
188 | 61382a50 | bellard | int index;
|
189 | b92e5a22 | bellard | |
190 | b92e5a22 | bellard | index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
|
191 | b92e5a22 | bellard | redo:
|
192 | b92e5a22 | bellard | tlb_addr = env->tlb_write[is_user][index].address; |
193 | b92e5a22 | bellard | if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
|
194 | 4ad06a29 | bellard | physaddr = addr + env->tlb_write[is_user][index].addend; |
195 | b92e5a22 | bellard | if (tlb_addr & ~TARGET_PAGE_MASK) {
|
196 | b92e5a22 | bellard | /* IO access */
|
197 | b92e5a22 | bellard | if ((addr & (DATA_SIZE - 1)) != 0) |
198 | b92e5a22 | bellard | goto do_unaligned_access;
|
199 | d720b93d | bellard | retaddr = GETPC(); |
200 | d720b93d | bellard | glue(io_write, SUFFIX)(physaddr, val, tlb_addr, retaddr); |
201 | b92e5a22 | bellard | } else if (((addr & 0xfff) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) { |
202 | b92e5a22 | bellard | do_unaligned_access:
|
203 | 61382a50 | bellard | retaddr = GETPC(); |
204 | 61382a50 | bellard | glue(glue(slow_st, SUFFIX), MMUSUFFIX)(addr, val, |
205 | 61382a50 | bellard | is_user, retaddr); |
206 | b92e5a22 | bellard | } else {
|
207 | b92e5a22 | bellard | /* aligned/unaligned access in the same page */
|
208 | b92e5a22 | bellard | glue(glue(st, SUFFIX), _raw)((uint8_t *)physaddr, val); |
209 | b92e5a22 | bellard | } |
210 | b92e5a22 | bellard | } else {
|
211 | b92e5a22 | bellard | /* the page is not in the TLB : fill it */
|
212 | 61382a50 | bellard | retaddr = GETPC(); |
213 | 61382a50 | bellard | tlb_fill(addr, 1, is_user, retaddr);
|
214 | b92e5a22 | bellard | goto redo;
|
215 | b92e5a22 | bellard | } |
216 | b92e5a22 | bellard | } |
217 | b92e5a22 | bellard | |
218 | b92e5a22 | bellard | /* handles all unaligned cases */
|
219 | 61382a50 | bellard | static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(unsigned long addr, |
220 | 61382a50 | bellard | DATA_TYPE val, |
221 | 61382a50 | bellard | int is_user,
|
222 | 61382a50 | bellard | void *retaddr)
|
223 | b92e5a22 | bellard | { |
224 | b92e5a22 | bellard | unsigned long physaddr, tlb_addr; |
225 | 61382a50 | bellard | int index, i;
|
226 | b92e5a22 | bellard | |
227 | b92e5a22 | bellard | index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
|
228 | b92e5a22 | bellard | redo:
|
229 | b92e5a22 | bellard | tlb_addr = env->tlb_write[is_user][index].address; |
230 | b92e5a22 | bellard | if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
|
231 | 4ad06a29 | bellard | physaddr = addr + env->tlb_write[is_user][index].addend; |
232 | b92e5a22 | bellard | if (tlb_addr & ~TARGET_PAGE_MASK) {
|
233 | b92e5a22 | bellard | /* IO access */
|
234 | b92e5a22 | bellard | if ((addr & (DATA_SIZE - 1)) != 0) |
235 | b92e5a22 | bellard | goto do_unaligned_access;
|
236 | d720b93d | bellard | glue(io_write, SUFFIX)(physaddr, val, tlb_addr, retaddr); |
237 | b92e5a22 | bellard | } else if (((addr & 0xfff) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) { |
238 | b92e5a22 | bellard | do_unaligned_access:
|
239 | b92e5a22 | bellard | /* XXX: not efficient, but simple */
|
240 | b92e5a22 | bellard | for(i = 0;i < DATA_SIZE; i++) { |
241 | b92e5a22 | bellard | #ifdef TARGET_WORDS_BIGENDIAN
|
242 | 61382a50 | bellard | glue(slow_stb, MMUSUFFIX)(addr + i, val >> (((DATA_SIZE - 1) * 8) - (i * 8)), |
243 | 61382a50 | bellard | is_user, retaddr); |
244 | b92e5a22 | bellard | #else
|
245 | 61382a50 | bellard | glue(slow_stb, MMUSUFFIX)(addr + i, val >> (i * 8),
|
246 | 61382a50 | bellard | is_user, retaddr); |
247 | b92e5a22 | bellard | #endif
|
248 | b92e5a22 | bellard | } |
249 | b92e5a22 | bellard | } else {
|
250 | b92e5a22 | bellard | /* aligned/unaligned access in the same page */
|
251 | b92e5a22 | bellard | glue(glue(st, SUFFIX), _raw)((uint8_t *)physaddr, val); |
252 | b92e5a22 | bellard | } |
253 | b92e5a22 | bellard | } else {
|
254 | b92e5a22 | bellard | /* the page is not in the TLB : fill it */
|
255 | 61382a50 | bellard | tlb_fill(addr, 1, is_user, retaddr);
|
256 | b92e5a22 | bellard | goto redo;
|
257 | b92e5a22 | bellard | } |
258 | b92e5a22 | bellard | } |
259 | b92e5a22 | bellard | |
260 | b92e5a22 | bellard | #undef SHIFT
|
261 | b92e5a22 | bellard | #undef DATA_TYPE
|
262 | b92e5a22 | bellard | #undef SUFFIX
|
263 | 61382a50 | bellard | #undef USUFFIX
|
264 | b92e5a22 | bellard | #undef DATA_SIZE |