blob: 4a2b6653f64e7e7689368d76624d9f3611de5520 [file] [log] [blame]
bellardb92e5a22003-08-08 23:58:05 +00001/*
2 * Software MMU support
ths5fafdf22007-09-16 21:08:06 +00003 *
Blue Swirlefbf29b2011-09-21 20:00:18 +00004 * Generate helpers used by TCG for qemu_ld/st ops and code load
5 * functions.
6 *
7 * Included from target op helpers and exec.c.
8 *
bellardb92e5a22003-08-08 23:58:05 +00009 * Copyright (c) 2003 Fabrice Bellard
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000022 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellardb92e5a22003-08-08 23:58:05 +000023 */
bellardb92e5a22003-08-08 23:58:05 +000024#if DATA_SIZE == 8
25#define SUFFIX q
Richard Henderson701e3a52013-08-27 11:31:48 -070026#define LSUFFIX q
Richard Hendersonc8f94df2013-08-27 14:09:14 -070027#define SDATA_TYPE int64_t
Martin Husemanndc9a3532014-01-18 14:47:23 +010028#define DATA_TYPE uint64_t
bellardb92e5a22003-08-08 23:58:05 +000029#elif DATA_SIZE == 4
30#define SUFFIX l
Richard Henderson701e3a52013-08-27 11:31:48 -070031#define LSUFFIX l
Richard Hendersonc8f94df2013-08-27 14:09:14 -070032#define SDATA_TYPE int32_t
Martin Husemanndc9a3532014-01-18 14:47:23 +010033#define DATA_TYPE uint32_t
bellardb92e5a22003-08-08 23:58:05 +000034#elif DATA_SIZE == 2
35#define SUFFIX w
Richard Henderson701e3a52013-08-27 11:31:48 -070036#define LSUFFIX uw
Richard Hendersonc8f94df2013-08-27 14:09:14 -070037#define SDATA_TYPE int16_t
Martin Husemanndc9a3532014-01-18 14:47:23 +010038#define DATA_TYPE uint16_t
bellardb92e5a22003-08-08 23:58:05 +000039#elif DATA_SIZE == 1
40#define SUFFIX b
Richard Henderson701e3a52013-08-27 11:31:48 -070041#define LSUFFIX ub
Richard Hendersonc8f94df2013-08-27 14:09:14 -070042#define SDATA_TYPE int8_t
Martin Husemanndc9a3532014-01-18 14:47:23 +010043#define DATA_TYPE uint8_t
bellardb92e5a22003-08-08 23:58:05 +000044#else
45#error unsupported data size
46#endif
47
Richard Hendersonc8f94df2013-08-27 14:09:14 -070048
49/* For the benefit of TCG generated code, we want to avoid the complication
50 of ABI-specific return type promotion and always return a value extended
51 to the register size of the host. This is tcg_target_long, except in the
52 case of a 32-bit host and 64-bit data, and for that we always have
53 uint64_t. Don't bother with this widened value for SOFTMMU_CODE_ACCESS. */
54#if defined(SOFTMMU_CODE_ACCESS) || DATA_SIZE == 8
55# define WORD_TYPE DATA_TYPE
56# define USUFFIX SUFFIX
57#else
58# define WORD_TYPE tcg_target_ulong
59# define USUFFIX glue(u, SUFFIX)
60# define SSUFFIX glue(s, SUFFIX)
61#endif
62
bellardb769d8f2004-10-03 15:07:13 +000063#ifdef SOFTMMU_CODE_ACCESS
Leon Alrae55e94092014-07-07 11:23:56 +010064#define READ_ACCESS_TYPE MMU_INST_FETCH
bellard84b7b8e2005-11-28 21:19:04 +000065#define ADDR_READ addr_code
bellardb769d8f2004-10-03 15:07:13 +000066#else
Leon Alrae55e94092014-07-07 11:23:56 +010067#define READ_ACCESS_TYPE MMU_DATA_LOAD
bellard84b7b8e2005-11-28 21:19:04 +000068#define ADDR_READ addr_read
bellardb769d8f2004-10-03 15:07:13 +000069#endif
70
Richard Henderson867b3202013-09-04 11:45:20 -070071#if DATA_SIZE == 8
72# define BSWAP(X) bswap64(X)
73#elif DATA_SIZE == 4
74# define BSWAP(X) bswap32(X)
75#elif DATA_SIZE == 2
76# define BSWAP(X) bswap16(X)
77#else
78# define BSWAP(X) (X)
79#endif
80
Richard Henderson867b3202013-09-04 11:45:20 -070081#if DATA_SIZE == 1
82# define helper_le_ld_name glue(glue(helper_ret_ld, USUFFIX), MMUSUFFIX)
83# define helper_be_ld_name helper_le_ld_name
84# define helper_le_lds_name glue(glue(helper_ret_ld, SSUFFIX), MMUSUFFIX)
85# define helper_be_lds_name helper_le_lds_name
86# define helper_le_st_name glue(glue(helper_ret_st, SUFFIX), MMUSUFFIX)
87# define helper_be_st_name helper_le_st_name
88#else
89# define helper_le_ld_name glue(glue(helper_le_ld, USUFFIX), MMUSUFFIX)
90# define helper_be_ld_name glue(glue(helper_be_ld, USUFFIX), MMUSUFFIX)
91# define helper_le_lds_name glue(glue(helper_le_ld, SSUFFIX), MMUSUFFIX)
92# define helper_be_lds_name glue(glue(helper_be_ld, SSUFFIX), MMUSUFFIX)
93# define helper_le_st_name glue(glue(helper_le_st, SUFFIX), MMUSUFFIX)
94# define helper_be_st_name glue(glue(helper_be_st, SUFFIX), MMUSUFFIX)
95#endif
96
Paolo Bonzini0f590e742014-03-28 17:55:24 +010097#ifndef SOFTMMU_CODE_ACCESS
Blue Swirl89c33332012-09-02 15:28:56 +000098static inline DATA_TYPE glue(io_read, SUFFIX)(CPUArchState *env,
Richard Henderson82a45b92016-07-08 18:51:28 -070099 size_t mmu_idx, size_t index,
pbrook2e70f6e2008-06-29 01:03:05 +0000100 target_ulong addr,
Blue Swirl20503962012-04-09 14:20:20 +0000101 uintptr_t retaddr)
bellardb92e5a22003-08-08 23:58:05 +0000102{
Richard Henderson82a45b92016-07-08 18:51:28 -0700103 CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
104 return io_readx(env, iotlbentry, addr, retaddr, DATA_SIZE);
bellardb92e5a22003-08-08 23:58:05 +0000105}
Paolo Bonzini0f590e742014-03-28 17:55:24 +0100106#endif
bellardb92e5a22003-08-08 23:58:05 +0000107
Richard Henderson3972ef62015-05-13 09:10:33 -0700108WORD_TYPE helper_le_ld_name(CPUArchState *env, target_ulong addr,
109 TCGMemOpIdx oi, uintptr_t retaddr)
bellardb92e5a22003-08-08 23:58:05 +0000110{
Richard Henderson3972ef62015-05-13 09:10:33 -0700111 unsigned mmu_idx = get_mmuidx(oi);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000112 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
113 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
Richard Henderson85aa8082016-07-14 12:43:06 -0700114 unsigned a_bits = get_alignment_bits(get_memop(oi));
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000115 uintptr_t haddr;
Richard Henderson867b3202013-09-04 11:45:20 -0700116 DATA_TYPE res;
ths3b46e622007-09-17 08:09:54 +0000117
Richard Henderson85aa8082016-07-14 12:43:06 -0700118 if (addr & ((1 << a_bits) - 1)) {
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300119 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
120 mmu_idx, retaddr);
121 }
122
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000123 /* If the TLB entry is for a different page, reload and try again. */
124 if ((addr & TARGET_PAGE_MASK)
125 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Samuel Damasheka3902842016-07-06 14:26:52 -0400126 if (!VICTIM_TLB_HIT(ADDR_READ, addr)) {
Xin Tong88e89a52014-08-04 20:35:23 -0500127 tlb_fill(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
128 mmu_idx, retaddr);
129 }
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000130 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
bellardb92e5a22003-08-08 23:58:05 +0000131 }
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000132
133 /* Handle an IO access. */
134 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000135 if ((addr & (DATA_SIZE - 1)) != 0) {
136 goto do_unaligned_access;
137 }
Richard Henderson867b3202013-09-04 11:45:20 -0700138
139 /* ??? Note that the io helpers always read data in the target
140 byte ordering. We should push the LE/BE request down into io. */
Richard Henderson82a45b92016-07-08 18:51:28 -0700141 res = glue(io_read, SUFFIX)(env, mmu_idx, index, addr, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700142 res = TGT_LE(res);
143 return res;
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000144 }
145
146 /* Handle slow unaligned access (it spans two pages or IO). */
147 if (DATA_SIZE > 1
148 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
149 >= TARGET_PAGE_SIZE)) {
150 target_ulong addr1, addr2;
Richard Henderson867b3202013-09-04 11:45:20 -0700151 DATA_TYPE res1, res2;
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000152 unsigned shift;
153 do_unaligned_access:
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000154 addr1 = addr & ~(DATA_SIZE - 1);
155 addr2 = addr1 + DATA_SIZE;
Richard Henderson01ecaf42016-07-26 06:09:16 +0530156 res1 = helper_le_ld_name(env, addr1, oi, retaddr);
157 res2 = helper_le_ld_name(env, addr2, oi, retaddr);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000158 shift = (addr & (DATA_SIZE - 1)) * 8;
Richard Henderson867b3202013-09-04 11:45:20 -0700159
160 /* Little-endian combine. */
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000161 res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000162 return res;
163 }
164
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000165 haddr = addr + env->tlb_table[mmu_idx][index].addend;
Richard Henderson867b3202013-09-04 11:45:20 -0700166#if DATA_SIZE == 1
167 res = glue(glue(ld, LSUFFIX), _p)((uint8_t *)haddr);
168#else
169 res = glue(glue(ld, LSUFFIX), _le_p)((uint8_t *)haddr);
170#endif
171 return res;
bellardb92e5a22003-08-08 23:58:05 +0000172}
173
Richard Henderson867b3202013-09-04 11:45:20 -0700174#if DATA_SIZE > 1
Richard Henderson3972ef62015-05-13 09:10:33 -0700175WORD_TYPE helper_be_ld_name(CPUArchState *env, target_ulong addr,
176 TCGMemOpIdx oi, uintptr_t retaddr)
Richard Henderson867b3202013-09-04 11:45:20 -0700177{
Richard Henderson3972ef62015-05-13 09:10:33 -0700178 unsigned mmu_idx = get_mmuidx(oi);
Richard Henderson867b3202013-09-04 11:45:20 -0700179 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
180 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
Richard Henderson85aa8082016-07-14 12:43:06 -0700181 unsigned a_bits = get_alignment_bits(get_memop(oi));
Richard Henderson867b3202013-09-04 11:45:20 -0700182 uintptr_t haddr;
183 DATA_TYPE res;
184
Richard Henderson85aa8082016-07-14 12:43:06 -0700185 if (addr & ((1 << a_bits) - 1)) {
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300186 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
187 mmu_idx, retaddr);
188 }
189
Richard Henderson867b3202013-09-04 11:45:20 -0700190 /* If the TLB entry is for a different page, reload and try again. */
191 if ((addr & TARGET_PAGE_MASK)
192 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Samuel Damasheka3902842016-07-06 14:26:52 -0400193 if (!VICTIM_TLB_HIT(ADDR_READ, addr)) {
Xin Tong88e89a52014-08-04 20:35:23 -0500194 tlb_fill(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
195 mmu_idx, retaddr);
196 }
Richard Henderson867b3202013-09-04 11:45:20 -0700197 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
198 }
199
200 /* Handle an IO access. */
201 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
Richard Henderson867b3202013-09-04 11:45:20 -0700202 if ((addr & (DATA_SIZE - 1)) != 0) {
203 goto do_unaligned_access;
204 }
Richard Henderson867b3202013-09-04 11:45:20 -0700205
206 /* ??? Note that the io helpers always read data in the target
207 byte ordering. We should push the LE/BE request down into io. */
Richard Henderson82a45b92016-07-08 18:51:28 -0700208 res = glue(io_read, SUFFIX)(env, mmu_idx, index, addr, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700209 res = TGT_BE(res);
210 return res;
211 }
212
213 /* Handle slow unaligned access (it spans two pages or IO). */
214 if (DATA_SIZE > 1
215 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
216 >= TARGET_PAGE_SIZE)) {
217 target_ulong addr1, addr2;
218 DATA_TYPE res1, res2;
219 unsigned shift;
220 do_unaligned_access:
Richard Henderson867b3202013-09-04 11:45:20 -0700221 addr1 = addr & ~(DATA_SIZE - 1);
222 addr2 = addr1 + DATA_SIZE;
Richard Henderson01ecaf42016-07-26 06:09:16 +0530223 res1 = helper_be_ld_name(env, addr1, oi, retaddr);
224 res2 = helper_be_ld_name(env, addr2, oi, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700225 shift = (addr & (DATA_SIZE - 1)) * 8;
226
227 /* Big-endian combine. */
228 res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
229 return res;
230 }
231
Richard Henderson867b3202013-09-04 11:45:20 -0700232 haddr = addr + env->tlb_table[mmu_idx][index].addend;
233 res = glue(glue(ld, LSUFFIX), _be_p)((uint8_t *)haddr);
234 return res;
235}
236#endif /* DATA_SIZE > 1 */
237
bellardb769d8f2004-10-03 15:07:13 +0000238#ifndef SOFTMMU_CODE_ACCESS
239
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700240/* Provide signed versions of the load routines as well. We can of course
241 avoid this for 64-bit data, or for 32-bit data on 32-bit host. */
242#if DATA_SIZE * 8 < TCG_TARGET_REG_BITS
Richard Henderson867b3202013-09-04 11:45:20 -0700243WORD_TYPE helper_le_lds_name(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -0700244 TCGMemOpIdx oi, uintptr_t retaddr)
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700245{
Richard Henderson3972ef62015-05-13 09:10:33 -0700246 return (SDATA_TYPE)helper_le_ld_name(env, addr, oi, retaddr);
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700247}
Richard Henderson867b3202013-09-04 11:45:20 -0700248
249# if DATA_SIZE > 1
250WORD_TYPE helper_be_lds_name(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -0700251 TCGMemOpIdx oi, uintptr_t retaddr)
Richard Henderson867b3202013-09-04 11:45:20 -0700252{
Richard Henderson3972ef62015-05-13 09:10:33 -0700253 return (SDATA_TYPE)helper_be_ld_name(env, addr, oi, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700254}
255# endif
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700256#endif
257
Blue Swirl89c33332012-09-02 15:28:56 +0000258static inline void glue(io_write, SUFFIX)(CPUArchState *env,
Richard Henderson82a45b92016-07-08 18:51:28 -0700259 size_t mmu_idx, size_t index,
bellardb769d8f2004-10-03 15:07:13 +0000260 DATA_TYPE val,
pbrook0f459d12008-06-09 00:20:13 +0000261 target_ulong addr,
Blue Swirl20503962012-04-09 14:20:20 +0000262 uintptr_t retaddr)
bellardb769d8f2004-10-03 15:07:13 +0000263{
Richard Henderson82a45b92016-07-08 18:51:28 -0700264 CPUIOTLBEntry *iotlbentry = &env->iotlb[mmu_idx][index];
265 return io_writex(env, iotlbentry, val, addr, retaddr, DATA_SIZE);
bellardb769d8f2004-10-03 15:07:13 +0000266}
bellardb92e5a22003-08-08 23:58:05 +0000267
Richard Henderson867b3202013-09-04 11:45:20 -0700268void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
Richard Henderson3972ef62015-05-13 09:10:33 -0700269 TCGMemOpIdx oi, uintptr_t retaddr)
bellardb92e5a22003-08-08 23:58:05 +0000270{
Richard Henderson3972ef62015-05-13 09:10:33 -0700271 unsigned mmu_idx = get_mmuidx(oi);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000272 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
273 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
Richard Henderson85aa8082016-07-14 12:43:06 -0700274 unsigned a_bits = get_alignment_bits(get_memop(oi));
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000275 uintptr_t haddr;
ths3b46e622007-09-17 08:09:54 +0000276
Richard Henderson85aa8082016-07-14 12:43:06 -0700277 if (addr & ((1 << a_bits) - 1)) {
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300278 cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
279 mmu_idx, retaddr);
280 }
281
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000282 /* If the TLB entry is for a different page, reload and try again. */
283 if ((addr & TARGET_PAGE_MASK)
284 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Samuel Damasheka3902842016-07-06 14:26:52 -0400285 if (!VICTIM_TLB_HIT(addr_write, addr)) {
Leon Alrae55e94092014-07-07 11:23:56 +0100286 tlb_fill(ENV_GET_CPU(env), addr, MMU_DATA_STORE, mmu_idx, retaddr);
Xin Tong88e89a52014-08-04 20:35:23 -0500287 }
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000288 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
bellardb92e5a22003-08-08 23:58:05 +0000289 }
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000290
291 /* Handle an IO access. */
292 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000293 if ((addr & (DATA_SIZE - 1)) != 0) {
294 goto do_unaligned_access;
295 }
Richard Henderson867b3202013-09-04 11:45:20 -0700296
297 /* ??? Note that the io helpers always read data in the target
298 byte ordering. We should push the LE/BE request down into io. */
299 val = TGT_LE(val);
Richard Henderson82a45b92016-07-08 18:51:28 -0700300 glue(io_write, SUFFIX)(env, mmu_idx, index, val, addr, retaddr);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000301 return;
302 }
303
304 /* Handle slow unaligned access (it spans two pages or IO). */
305 if (DATA_SIZE > 1
306 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
307 >= TARGET_PAGE_SIZE)) {
Samuel Damashek81daaba2016-07-08 12:54:34 -0700308 int i, index2;
309 target_ulong page2, tlb_addr2;
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000310 do_unaligned_access:
Samuel Damashek81daaba2016-07-08 12:54:34 -0700311 /* Ensure the second page is in the TLB. Note that the first page
312 is already guaranteed to be filled, and that the second page
313 cannot evict the first. */
314 page2 = (addr + DATA_SIZE) & TARGET_PAGE_MASK;
315 index2 = (page2 >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
316 tlb_addr2 = env->tlb_table[mmu_idx][index2].addr_write;
317 if (page2 != (tlb_addr2 & (TARGET_PAGE_MASK | TLB_INVALID_MASK))
318 && !VICTIM_TLB_HIT(addr_write, page2)) {
319 tlb_fill(ENV_GET_CPU(env), page2, MMU_DATA_STORE,
320 mmu_idx, retaddr);
321 }
322
323 /* XXX: not efficient, but simple. */
324 /* This loop must go in the forward direction to avoid issues
325 with self-modifying code in Windows 64-bit. */
326 for (i = 0; i < DATA_SIZE; ++i) {
Richard Henderson867b3202013-09-04 11:45:20 -0700327 /* Little-endian extract. */
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000328 uint8_t val8 = val >> (i * 8);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000329 glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
Richard Henderson01ecaf42016-07-26 06:09:16 +0530330 oi, retaddr);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000331 }
332 return;
333 }
334
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000335 haddr = addr + env->tlb_table[mmu_idx][index].addend;
Richard Henderson867b3202013-09-04 11:45:20 -0700336#if DATA_SIZE == 1
337 glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
338#else
339 glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
340#endif
bellardb92e5a22003-08-08 23:58:05 +0000341}
342
Richard Henderson867b3202013-09-04 11:45:20 -0700343#if DATA_SIZE > 1
344void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
Richard Henderson3972ef62015-05-13 09:10:33 -0700345 TCGMemOpIdx oi, uintptr_t retaddr)
Richard Henderson867b3202013-09-04 11:45:20 -0700346{
Richard Henderson3972ef62015-05-13 09:10:33 -0700347 unsigned mmu_idx = get_mmuidx(oi);
Richard Henderson867b3202013-09-04 11:45:20 -0700348 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
349 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
Richard Henderson85aa8082016-07-14 12:43:06 -0700350 unsigned a_bits = get_alignment_bits(get_memop(oi));
Richard Henderson867b3202013-09-04 11:45:20 -0700351 uintptr_t haddr;
352
Richard Henderson85aa8082016-07-14 12:43:06 -0700353 if (addr & ((1 << a_bits) - 1)) {
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300354 cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
355 mmu_idx, retaddr);
356 }
357
Richard Henderson867b3202013-09-04 11:45:20 -0700358 /* If the TLB entry is for a different page, reload and try again. */
359 if ((addr & TARGET_PAGE_MASK)
360 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Samuel Damasheka3902842016-07-06 14:26:52 -0400361 if (!VICTIM_TLB_HIT(addr_write, addr)) {
Leon Alrae55e94092014-07-07 11:23:56 +0100362 tlb_fill(ENV_GET_CPU(env), addr, MMU_DATA_STORE, mmu_idx, retaddr);
Xin Tong88e89a52014-08-04 20:35:23 -0500363 }
Richard Henderson867b3202013-09-04 11:45:20 -0700364 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
365 }
366
367 /* Handle an IO access. */
368 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
Richard Henderson867b3202013-09-04 11:45:20 -0700369 if ((addr & (DATA_SIZE - 1)) != 0) {
370 goto do_unaligned_access;
371 }
Richard Henderson867b3202013-09-04 11:45:20 -0700372
373 /* ??? Note that the io helpers always read data in the target
374 byte ordering. We should push the LE/BE request down into io. */
375 val = TGT_BE(val);
Richard Henderson82a45b92016-07-08 18:51:28 -0700376 glue(io_write, SUFFIX)(env, mmu_idx, index, val, addr, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700377 return;
378 }
379
380 /* Handle slow unaligned access (it spans two pages or IO). */
381 if (DATA_SIZE > 1
382 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
383 >= TARGET_PAGE_SIZE)) {
Samuel Damashek81daaba2016-07-08 12:54:34 -0700384 int i, index2;
385 target_ulong page2, tlb_addr2;
Richard Henderson867b3202013-09-04 11:45:20 -0700386 do_unaligned_access:
Samuel Damashek81daaba2016-07-08 12:54:34 -0700387 /* Ensure the second page is in the TLB. Note that the first page
388 is already guaranteed to be filled, and that the second page
389 cannot evict the first. */
390 page2 = (addr + DATA_SIZE) & TARGET_PAGE_MASK;
391 index2 = (page2 >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
392 tlb_addr2 = env->tlb_table[mmu_idx][index2].addr_write;
393 if (page2 != (tlb_addr2 & (TARGET_PAGE_MASK | TLB_INVALID_MASK))
394 && !VICTIM_TLB_HIT(addr_write, page2)) {
395 tlb_fill(ENV_GET_CPU(env), page2, MMU_DATA_STORE,
396 mmu_idx, retaddr);
397 }
398
Richard Henderson867b3202013-09-04 11:45:20 -0700399 /* XXX: not efficient, but simple */
Samuel Damashek81daaba2016-07-08 12:54:34 -0700400 /* This loop must go in the forward direction to avoid issues
401 with self-modifying code. */
402 for (i = 0; i < DATA_SIZE; ++i) {
Richard Henderson867b3202013-09-04 11:45:20 -0700403 /* Big-endian extract. */
404 uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
Richard Henderson867b3202013-09-04 11:45:20 -0700405 glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
Richard Henderson01ecaf42016-07-26 06:09:16 +0530406 oi, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700407 }
408 return;
409 }
410
Richard Henderson867b3202013-09-04 11:45:20 -0700411 haddr = addr + env->tlb_table[mmu_idx][index].addend;
412 glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
413}
414#endif /* DATA_SIZE > 1 */
bellardb769d8f2004-10-03 15:07:13 +0000415#endif /* !defined(SOFTMMU_CODE_ACCESS) */
416
417#undef READ_ACCESS_TYPE
bellardb92e5a22003-08-08 23:58:05 +0000418#undef DATA_TYPE
419#undef SUFFIX
Richard Henderson701e3a52013-08-27 11:31:48 -0700420#undef LSUFFIX
bellardb92e5a22003-08-08 23:58:05 +0000421#undef DATA_SIZE
bellard84b7b8e2005-11-28 21:19:04 +0000422#undef ADDR_READ
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700423#undef WORD_TYPE
424#undef SDATA_TYPE
425#undef USUFFIX
426#undef SSUFFIX
Richard Henderson867b3202013-09-04 11:45:20 -0700427#undef BSWAP
Richard Henderson867b3202013-09-04 11:45:20 -0700428#undef helper_le_ld_name
429#undef helper_be_ld_name
430#undef helper_le_lds_name
431#undef helper_be_lds_name
432#undef helper_le_st_name
433#undef helper_be_st_name