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