4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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
20 #define DATA_SIZE (1 << SHIFT)
25 #define DATA_TYPE uint64_t
29 #define DATA_TYPE uint32_t
33 #define DATA_TYPE uint16_t
37 #define DATA_TYPE uint8_t
39 #error unsupported data size
42 static DATA_TYPE
glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(unsigned long addr
,
45 static void glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(unsigned long addr
,
50 static inline DATA_TYPE
glue(io_read
, SUFFIX
)(unsigned long physaddr
,
51 unsigned long tlb_addr
)
56 index
= (tlb_addr
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
58 res
= io_mem_read
[index
][SHIFT
](physaddr
);
60 #ifdef TARGET_WORDS_BIGENDIAN
61 res
= (uint64_t)io_mem_read
[index
][2](physaddr
) << 32;
62 res
|= io_mem_read
[index
][2](physaddr
+ 4);
64 res
= io_mem_read
[index
][2](physaddr
);
65 res
|= (uint64_t)io_mem_read
[index
][2](physaddr
+ 4) << 32;
67 #endif /* SHIFT > 2 */
71 static inline void glue(io_write
, SUFFIX
)(unsigned long physaddr
,
73 unsigned long tlb_addr
)
77 index
= (tlb_addr
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
79 io_mem_write
[index
][SHIFT
](physaddr
, val
);
81 #ifdef TARGET_WORDS_BIGENDIAN
82 io_mem_write
[index
][2](physaddr
, val
>> 32);
83 io_mem_write
[index
][2](physaddr
+ 4, val
);
85 io_mem_write
[index
][2](physaddr
, val
);
86 io_mem_write
[index
][2](physaddr
+ 4, val
>> 32);
88 #endif /* SHIFT > 2 */
91 /* handle all cases except unaligned access which span two pages */
92 DATA_TYPE
REGPARM(1) glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(unsigned long addr
,
97 unsigned long physaddr
, tlb_addr
;
100 /* test if there is match for unaligned or IO access */
101 /* XXX: could done more in memory macro in a non portable way */
102 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
104 tlb_addr
= env
->tlb_read
[is_user
][index
].address
;
105 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
106 physaddr
= addr
+ env
->tlb_read
[is_user
][index
].addend
;
107 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
109 if ((addr
& (DATA_SIZE
- 1)) != 0)
110 goto do_unaligned_access
;
111 res
= glue(io_read
, SUFFIX
)(physaddr
, tlb_addr
);
112 } else if (((addr
& 0xfff) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
113 /* slow unaligned access (it spans two pages or IO) */
116 res
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr
,
119 /* unaligned access in the same page */
120 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)physaddr
);
123 /* the page is not in the TLB : fill it */
125 tlb_fill(addr
, 0, is_user
, retaddr
);
131 /* handle all unaligned cases */
132 static DATA_TYPE
glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(unsigned long addr
,
136 DATA_TYPE res
, res1
, res2
;
138 unsigned long physaddr
, tlb_addr
, addr1
, addr2
;
140 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
142 tlb_addr
= env
->tlb_read
[is_user
][index
].address
;
143 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
144 physaddr
= addr
+ env
->tlb_read
[is_user
][index
].addend
;
145 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
147 if ((addr
& (DATA_SIZE
- 1)) != 0)
148 goto do_unaligned_access
;
149 res
= glue(io_read
, SUFFIX
)(physaddr
, tlb_addr
);
150 } else if (((addr
& 0xfff) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
152 /* slow unaligned access (it spans two pages) */
153 addr1
= addr
& ~(DATA_SIZE
- 1);
154 addr2
= addr1
+ DATA_SIZE
;
155 res1
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr1
,
157 res2
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr2
,
159 shift
= (addr
& (DATA_SIZE
- 1)) * 8;
160 #ifdef TARGET_WORDS_BIGENDIAN
161 res
= (res1
<< shift
) | (res2
>> ((DATA_SIZE
* 8) - shift
));
163 res
= (res1
>> shift
) | (res2
<< ((DATA_SIZE
* 8) - shift
));
166 /* unaligned/aligned access in the same page */
167 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)physaddr
);
170 /* the page is not in the TLB : fill it */
171 tlb_fill(addr
, 0, is_user
, retaddr
);
178 void REGPARM(2) glue(glue(__st
, SUFFIX
), MMUSUFFIX
)(unsigned long addr
,
182 unsigned long physaddr
, tlb_addr
;
186 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
188 tlb_addr
= env
->tlb_write
[is_user
][index
].address
;
189 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
190 physaddr
= addr
+ env
->tlb_read
[is_user
][index
].addend
;
191 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
193 if ((addr
& (DATA_SIZE
- 1)) != 0)
194 goto do_unaligned_access
;
195 glue(io_write
, SUFFIX
)(physaddr
, val
, tlb_addr
);
196 } else if (((addr
& 0xfff) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
199 glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(addr
, val
,
202 /* aligned/unaligned access in the same page */
203 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)physaddr
, val
);
206 /* the page is not in the TLB : fill it */
208 tlb_fill(addr
, 1, is_user
, retaddr
);
213 /* handles all unaligned cases */
214 static void glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(unsigned long addr
,
219 unsigned long physaddr
, tlb_addr
;
222 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
224 tlb_addr
= env
->tlb_write
[is_user
][index
].address
;
225 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
226 physaddr
= addr
+ env
->tlb_read
[is_user
][index
].addend
;
227 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
229 if ((addr
& (DATA_SIZE
- 1)) != 0)
230 goto do_unaligned_access
;
231 glue(io_write
, SUFFIX
)(physaddr
, val
, tlb_addr
);
232 } else if (((addr
& 0xfff) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
234 /* XXX: not efficient, but simple */
235 for(i
= 0;i
< DATA_SIZE
; i
++) {
236 #ifdef TARGET_WORDS_BIGENDIAN
237 glue(slow_stb
, MMUSUFFIX
)(addr
+ i
, val
>> (((DATA_SIZE
- 1) * 8) - (i
* 8)),
240 glue(slow_stb
, MMUSUFFIX
)(addr
+ i
, val
>> (i
* 8),
245 /* aligned/unaligned access in the same page */
246 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)physaddr
, val
);
249 /* the page is not in the TLB : fill it */
250 tlb_fill(addr
, 1, is_user
, retaddr
);