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