blob: 284ab2c7b29f3228b06f9f4343b7f17f04630287 [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 */
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/timer.h"
Edgar E. Iglesias77717092013-11-07 19:55:56 +010025#include "exec/address-spaces.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010026#include "exec/memory.h"
Blue Swirl29e922b2010-03-29 19:24:00 +000027
bellardb92e5a22003-08-08 23:58:05 +000028#define DATA_SIZE (1 << SHIFT)
29
30#if DATA_SIZE == 8
31#define SUFFIX q
Richard Henderson701e3a52013-08-27 11:31:48 -070032#define LSUFFIX q
Richard Hendersonc8f94df2013-08-27 14:09:14 -070033#define SDATA_TYPE int64_t
Martin Husemanndc9a3532014-01-18 14:47:23 +010034#define DATA_TYPE uint64_t
bellardb92e5a22003-08-08 23:58:05 +000035#elif DATA_SIZE == 4
36#define SUFFIX l
Richard Henderson701e3a52013-08-27 11:31:48 -070037#define LSUFFIX l
Richard Hendersonc8f94df2013-08-27 14:09:14 -070038#define SDATA_TYPE int32_t
Martin Husemanndc9a3532014-01-18 14:47:23 +010039#define DATA_TYPE uint32_t
bellardb92e5a22003-08-08 23:58:05 +000040#elif DATA_SIZE == 2
41#define SUFFIX w
Richard Henderson701e3a52013-08-27 11:31:48 -070042#define LSUFFIX uw
Richard Hendersonc8f94df2013-08-27 14:09:14 -070043#define SDATA_TYPE int16_t
Martin Husemanndc9a3532014-01-18 14:47:23 +010044#define DATA_TYPE uint16_t
bellardb92e5a22003-08-08 23:58:05 +000045#elif DATA_SIZE == 1
46#define SUFFIX b
Richard Henderson701e3a52013-08-27 11:31:48 -070047#define LSUFFIX ub
Richard Hendersonc8f94df2013-08-27 14:09:14 -070048#define SDATA_TYPE int8_t
Martin Husemanndc9a3532014-01-18 14:47:23 +010049#define DATA_TYPE uint8_t
bellardb92e5a22003-08-08 23:58:05 +000050#else
51#error unsupported data size
52#endif
53
Richard Hendersonc8f94df2013-08-27 14:09:14 -070054
55/* For the benefit of TCG generated code, we want to avoid the complication
56 of ABI-specific return type promotion and always return a value extended
57 to the register size of the host. This is tcg_target_long, except in the
58 case of a 32-bit host and 64-bit data, and for that we always have
59 uint64_t. Don't bother with this widened value for SOFTMMU_CODE_ACCESS. */
60#if defined(SOFTMMU_CODE_ACCESS) || DATA_SIZE == 8
61# define WORD_TYPE DATA_TYPE
62# define USUFFIX SUFFIX
63#else
64# define WORD_TYPE tcg_target_ulong
65# define USUFFIX glue(u, SUFFIX)
66# define SSUFFIX glue(s, SUFFIX)
67#endif
68
bellardb769d8f2004-10-03 15:07:13 +000069#ifdef SOFTMMU_CODE_ACCESS
Leon Alrae55e94092014-07-07 11:23:56 +010070#define READ_ACCESS_TYPE MMU_INST_FETCH
bellard84b7b8e2005-11-28 21:19:04 +000071#define ADDR_READ addr_code
bellardb769d8f2004-10-03 15:07:13 +000072#else
Leon Alrae55e94092014-07-07 11:23:56 +010073#define READ_ACCESS_TYPE MMU_DATA_LOAD
bellard84b7b8e2005-11-28 21:19:04 +000074#define ADDR_READ addr_read
bellardb769d8f2004-10-03 15:07:13 +000075#endif
76
Richard Henderson867b3202013-09-04 11:45:20 -070077#if DATA_SIZE == 8
78# define BSWAP(X) bswap64(X)
79#elif DATA_SIZE == 4
80# define BSWAP(X) bswap32(X)
81#elif DATA_SIZE == 2
82# define BSWAP(X) bswap16(X)
83#else
84# define BSWAP(X) (X)
85#endif
86
87#ifdef TARGET_WORDS_BIGENDIAN
88# define TGT_BE(X) (X)
89# define TGT_LE(X) BSWAP(X)
90#else
91# define TGT_BE(X) BSWAP(X)
92# define TGT_LE(X) (X)
93#endif
94
95#if DATA_SIZE == 1
96# define helper_le_ld_name glue(glue(helper_ret_ld, USUFFIX), MMUSUFFIX)
97# define helper_be_ld_name helper_le_ld_name
98# define helper_le_lds_name glue(glue(helper_ret_ld, SSUFFIX), MMUSUFFIX)
99# define helper_be_lds_name helper_le_lds_name
100# define helper_le_st_name glue(glue(helper_ret_st, SUFFIX), MMUSUFFIX)
101# define helper_be_st_name helper_le_st_name
102#else
103# define helper_le_ld_name glue(glue(helper_le_ld, USUFFIX), MMUSUFFIX)
104# define helper_be_ld_name glue(glue(helper_be_ld, USUFFIX), MMUSUFFIX)
105# define helper_le_lds_name glue(glue(helper_le_ld, SSUFFIX), MMUSUFFIX)
106# define helper_be_lds_name glue(glue(helper_be_ld, SSUFFIX), MMUSUFFIX)
107# define helper_le_st_name glue(glue(helper_le_st, SUFFIX), MMUSUFFIX)
108# define helper_be_st_name glue(glue(helper_be_st, SUFFIX), MMUSUFFIX)
109#endif
110
111#ifdef TARGET_WORDS_BIGENDIAN
112# define helper_te_ld_name helper_be_ld_name
113# define helper_te_st_name helper_be_st_name
114#else
115# define helper_te_ld_name helper_le_ld_name
116# define helper_te_st_name helper_le_st_name
117#endif
118
Paolo Bonzini0f590e742014-03-28 17:55:24 +0100119#ifndef SOFTMMU_CODE_ACCESS
Blue Swirl89c33332012-09-02 15:28:56 +0000120static inline DATA_TYPE glue(io_read, SUFFIX)(CPUArchState *env,
Peter Maydelle469b222015-04-26 16:49:23 +0100121 CPUIOTLBEntry *iotlbentry,
pbrook2e70f6e2008-06-29 01:03:05 +0000122 target_ulong addr,
Blue Swirl20503962012-04-09 14:20:20 +0000123 uintptr_t retaddr)
bellardb92e5a22003-08-08 23:58:05 +0000124{
Paolo Bonzini791af8c2013-05-24 16:10:39 +0200125 uint64_t val;
Edgar E. Iglesias09daed82013-12-17 13:06:51 +1000126 CPUState *cpu = ENV_GET_CPU(env);
Peter Maydelle469b222015-04-26 16:49:23 +0100127 hwaddr physaddr = iotlbentry->addr;
Peter Maydella54c87b2016-01-21 14:15:05 +0000128 MemoryRegion *mr = iotlb_to_region(cpu, physaddr, iotlbentry->attrs);
Avi Kivity37ec01d2012-03-08 18:08:35 +0200129
pbrook0f459d12008-06-09 00:20:13 +0000130 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
Andreas Färber93afead2013-08-26 03:41:01 +0200131 cpu->mem_io_pc = retaddr;
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200132 if (mr != &io_mem_rom && mr != &io_mem_notdirty && !cpu->can_do_io) {
Andreas Färber90b40a62013-09-01 17:21:47 +0200133 cpu_io_recompile(cpu, retaddr);
pbrook2e70f6e2008-06-29 01:03:05 +0000134 }
bellardb92e5a22003-08-08 23:58:05 +0000135
Andreas Färber93afead2013-08-26 03:41:01 +0200136 cpu->mem_io_vaddr = addr;
Peter Maydell3b643492015-04-26 16:49:23 +0100137 memory_region_dispatch_read(mr, physaddr, &val, 1 << SHIFT,
Peter Maydellfadc1cb2015-04-26 16:49:24 +0100138 iotlbentry->attrs);
Paolo Bonzini791af8c2013-05-24 16:10:39 +0200139 return val;
bellardb92e5a22003-08-08 23:58:05 +0000140}
Paolo Bonzini0f590e742014-03-28 17:55:24 +0100141#endif
bellardb92e5a22003-08-08 23:58:05 +0000142
Richard Henderson3972ef62015-05-13 09:10:33 -0700143WORD_TYPE helper_le_ld_name(CPUArchState *env, target_ulong addr,
144 TCGMemOpIdx oi, uintptr_t retaddr)
bellardb92e5a22003-08-08 23:58:05 +0000145{
Richard Henderson3972ef62015-05-13 09:10:33 -0700146 unsigned mmu_idx = get_mmuidx(oi);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000147 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
148 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300149 int a_bits = get_alignment_bits(get_memop(oi));
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000150 uintptr_t haddr;
Richard Henderson867b3202013-09-04 11:45:20 -0700151 DATA_TYPE res;
ths3b46e622007-09-17 08:09:54 +0000152
Richard Henderson0f842f82013-08-27 10:22:54 -0700153 /* Adjust the given return address. */
154 retaddr -= GETPC_ADJ;
155
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300156 if (a_bits > 0 && (addr & ((1 << a_bits) - 1)) != 0) {
157 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
158 mmu_idx, retaddr);
159 }
160
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000161 /* If the TLB entry is for a different page, reload and try again. */
162 if ((addr & TARGET_PAGE_MASK)
163 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Samuel Damasheka3902842016-07-06 14:26:52 -0400164 if (!VICTIM_TLB_HIT(ADDR_READ, addr)) {
Xin Tong88e89a52014-08-04 20:35:23 -0500165 tlb_fill(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
166 mmu_idx, retaddr);
167 }
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000168 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
bellardb92e5a22003-08-08 23:58:05 +0000169 }
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000170
171 /* Handle an IO access. */
172 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
Peter Maydelle469b222015-04-26 16:49:23 +0100173 CPUIOTLBEntry *iotlbentry;
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000174 if ((addr & (DATA_SIZE - 1)) != 0) {
175 goto do_unaligned_access;
176 }
Peter Maydelle469b222015-04-26 16:49:23 +0100177 iotlbentry = &env->iotlb[mmu_idx][index];
Richard Henderson867b3202013-09-04 11:45:20 -0700178
179 /* ??? Note that the io helpers always read data in the target
180 byte ordering. We should push the LE/BE request down into io. */
Peter Maydelle469b222015-04-26 16:49:23 +0100181 res = glue(io_read, SUFFIX)(env, iotlbentry, addr, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700182 res = TGT_LE(res);
183 return res;
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000184 }
185
186 /* Handle slow unaligned access (it spans two pages or IO). */
187 if (DATA_SIZE > 1
188 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
189 >= TARGET_PAGE_SIZE)) {
190 target_ulong addr1, addr2;
Richard Henderson867b3202013-09-04 11:45:20 -0700191 DATA_TYPE res1, res2;
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000192 unsigned shift;
193 do_unaligned_access:
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000194 addr1 = addr & ~(DATA_SIZE - 1);
195 addr2 = addr1 + DATA_SIZE;
Richard Henderson0f842f82013-08-27 10:22:54 -0700196 /* Note the adjustment at the beginning of the function.
197 Undo that for the recursion. */
Richard Henderson3972ef62015-05-13 09:10:33 -0700198 res1 = helper_le_ld_name(env, addr1, oi, retaddr + GETPC_ADJ);
199 res2 = helper_le_ld_name(env, addr2, oi, retaddr + GETPC_ADJ);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000200 shift = (addr & (DATA_SIZE - 1)) * 8;
Richard Henderson867b3202013-09-04 11:45:20 -0700201
202 /* Little-endian combine. */
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000203 res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000204 return res;
205 }
206
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000207 haddr = addr + env->tlb_table[mmu_idx][index].addend;
Richard Henderson867b3202013-09-04 11:45:20 -0700208#if DATA_SIZE == 1
209 res = glue(glue(ld, LSUFFIX), _p)((uint8_t *)haddr);
210#else
211 res = glue(glue(ld, LSUFFIX), _le_p)((uint8_t *)haddr);
212#endif
213 return res;
bellardb92e5a22003-08-08 23:58:05 +0000214}
215
Richard Henderson867b3202013-09-04 11:45:20 -0700216#if DATA_SIZE > 1
Richard Henderson3972ef62015-05-13 09:10:33 -0700217WORD_TYPE helper_be_ld_name(CPUArchState *env, target_ulong addr,
218 TCGMemOpIdx oi, uintptr_t retaddr)
Richard Henderson867b3202013-09-04 11:45:20 -0700219{
Richard Henderson3972ef62015-05-13 09:10:33 -0700220 unsigned mmu_idx = get_mmuidx(oi);
Richard Henderson867b3202013-09-04 11:45:20 -0700221 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
222 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300223 int a_bits = get_alignment_bits(get_memop(oi));
Richard Henderson867b3202013-09-04 11:45:20 -0700224 uintptr_t haddr;
225 DATA_TYPE res;
226
227 /* Adjust the given return address. */
228 retaddr -= GETPC_ADJ;
229
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300230 if (a_bits > 0 && (addr & ((1 << a_bits) - 1)) != 0) {
231 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
232 mmu_idx, retaddr);
233 }
234
Richard Henderson867b3202013-09-04 11:45:20 -0700235 /* If the TLB entry is for a different page, reload and try again. */
236 if ((addr & TARGET_PAGE_MASK)
237 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Samuel Damasheka3902842016-07-06 14:26:52 -0400238 if (!VICTIM_TLB_HIT(ADDR_READ, addr)) {
Xin Tong88e89a52014-08-04 20:35:23 -0500239 tlb_fill(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
240 mmu_idx, retaddr);
241 }
Richard Henderson867b3202013-09-04 11:45:20 -0700242 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
243 }
244
245 /* Handle an IO access. */
246 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
Peter Maydelle469b222015-04-26 16:49:23 +0100247 CPUIOTLBEntry *iotlbentry;
Richard Henderson867b3202013-09-04 11:45:20 -0700248 if ((addr & (DATA_SIZE - 1)) != 0) {
249 goto do_unaligned_access;
250 }
Peter Maydelle469b222015-04-26 16:49:23 +0100251 iotlbentry = &env->iotlb[mmu_idx][index];
Richard Henderson867b3202013-09-04 11:45:20 -0700252
253 /* ??? Note that the io helpers always read data in the target
254 byte ordering. We should push the LE/BE request down into io. */
Peter Maydelle469b222015-04-26 16:49:23 +0100255 res = glue(io_read, SUFFIX)(env, iotlbentry, addr, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700256 res = TGT_BE(res);
257 return res;
258 }
259
260 /* Handle slow unaligned access (it spans two pages or IO). */
261 if (DATA_SIZE > 1
262 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
263 >= TARGET_PAGE_SIZE)) {
264 target_ulong addr1, addr2;
265 DATA_TYPE res1, res2;
266 unsigned shift;
267 do_unaligned_access:
Richard Henderson867b3202013-09-04 11:45:20 -0700268 addr1 = addr & ~(DATA_SIZE - 1);
269 addr2 = addr1 + DATA_SIZE;
270 /* Note the adjustment at the beginning of the function.
271 Undo that for the recursion. */
Richard Henderson3972ef62015-05-13 09:10:33 -0700272 res1 = helper_be_ld_name(env, addr1, oi, retaddr + GETPC_ADJ);
273 res2 = helper_be_ld_name(env, addr2, oi, retaddr + GETPC_ADJ);
Richard Henderson867b3202013-09-04 11:45:20 -0700274 shift = (addr & (DATA_SIZE - 1)) * 8;
275
276 /* Big-endian combine. */
277 res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
278 return res;
279 }
280
Richard Henderson867b3202013-09-04 11:45:20 -0700281 haddr = addr + env->tlb_table[mmu_idx][index].addend;
282 res = glue(glue(ld, LSUFFIX), _be_p)((uint8_t *)haddr);
283 return res;
284}
285#endif /* DATA_SIZE > 1 */
286
bellardb769d8f2004-10-03 15:07:13 +0000287#ifndef SOFTMMU_CODE_ACCESS
288
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700289/* Provide signed versions of the load routines as well. We can of course
290 avoid this for 64-bit data, or for 32-bit data on 32-bit host. */
291#if DATA_SIZE * 8 < TCG_TARGET_REG_BITS
Richard Henderson867b3202013-09-04 11:45:20 -0700292WORD_TYPE helper_le_lds_name(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -0700293 TCGMemOpIdx oi, uintptr_t retaddr)
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700294{
Richard Henderson3972ef62015-05-13 09:10:33 -0700295 return (SDATA_TYPE)helper_le_ld_name(env, addr, oi, retaddr);
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700296}
Richard Henderson867b3202013-09-04 11:45:20 -0700297
298# if DATA_SIZE > 1
299WORD_TYPE helper_be_lds_name(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -0700300 TCGMemOpIdx oi, uintptr_t retaddr)
Richard Henderson867b3202013-09-04 11:45:20 -0700301{
Richard Henderson3972ef62015-05-13 09:10:33 -0700302 return (SDATA_TYPE)helper_be_ld_name(env, addr, oi, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700303}
304# endif
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700305#endif
306
Blue Swirl89c33332012-09-02 15:28:56 +0000307static inline void glue(io_write, SUFFIX)(CPUArchState *env,
Peter Maydelle469b222015-04-26 16:49:23 +0100308 CPUIOTLBEntry *iotlbentry,
bellardb769d8f2004-10-03 15:07:13 +0000309 DATA_TYPE val,
pbrook0f459d12008-06-09 00:20:13 +0000310 target_ulong addr,
Blue Swirl20503962012-04-09 14:20:20 +0000311 uintptr_t retaddr)
bellardb769d8f2004-10-03 15:07:13 +0000312{
Edgar E. Iglesias09daed82013-12-17 13:06:51 +1000313 CPUState *cpu = ENV_GET_CPU(env);
Peter Maydelle469b222015-04-26 16:49:23 +0100314 hwaddr physaddr = iotlbentry->addr;
Peter Maydella54c87b2016-01-21 14:15:05 +0000315 MemoryRegion *mr = iotlb_to_region(cpu, physaddr, iotlbentry->attrs);
Avi Kivity37ec01d2012-03-08 18:08:35 +0200316
pbrook0f459d12008-06-09 00:20:13 +0000317 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200318 if (mr != &io_mem_rom && mr != &io_mem_notdirty && !cpu->can_do_io) {
Andreas Färber90b40a62013-09-01 17:21:47 +0200319 cpu_io_recompile(cpu, retaddr);
pbrook2e70f6e2008-06-29 01:03:05 +0000320 }
bellardb769d8f2004-10-03 15:07:13 +0000321
Andreas Färber93afead2013-08-26 03:41:01 +0200322 cpu->mem_io_vaddr = addr;
323 cpu->mem_io_pc = retaddr;
Peter Maydell3b643492015-04-26 16:49:23 +0100324 memory_region_dispatch_write(mr, physaddr, val, 1 << SHIFT,
Peter Maydellfadc1cb2015-04-26 16:49:24 +0100325 iotlbentry->attrs);
bellardb769d8f2004-10-03 15:07:13 +0000326}
bellardb92e5a22003-08-08 23:58:05 +0000327
Richard Henderson867b3202013-09-04 11:45:20 -0700328void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
Richard Henderson3972ef62015-05-13 09:10:33 -0700329 TCGMemOpIdx oi, uintptr_t retaddr)
bellardb92e5a22003-08-08 23:58:05 +0000330{
Richard Henderson3972ef62015-05-13 09:10:33 -0700331 unsigned mmu_idx = get_mmuidx(oi);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000332 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
333 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300334 int a_bits = get_alignment_bits(get_memop(oi));
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000335 uintptr_t haddr;
ths3b46e622007-09-17 08:09:54 +0000336
Richard Henderson0f842f82013-08-27 10:22:54 -0700337 /* Adjust the given return address. */
338 retaddr -= GETPC_ADJ;
339
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300340 if (a_bits > 0 && (addr & ((1 << a_bits) - 1)) != 0) {
341 cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
342 mmu_idx, retaddr);
343 }
344
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000345 /* If the TLB entry is for a different page, reload and try again. */
346 if ((addr & TARGET_PAGE_MASK)
347 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Samuel Damasheka3902842016-07-06 14:26:52 -0400348 if (!VICTIM_TLB_HIT(addr_write, addr)) {
Leon Alrae55e94092014-07-07 11:23:56 +0100349 tlb_fill(ENV_GET_CPU(env), addr, MMU_DATA_STORE, mmu_idx, retaddr);
Xin Tong88e89a52014-08-04 20:35:23 -0500350 }
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000351 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
bellardb92e5a22003-08-08 23:58:05 +0000352 }
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000353
354 /* Handle an IO access. */
355 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
Peter Maydelle469b222015-04-26 16:49:23 +0100356 CPUIOTLBEntry *iotlbentry;
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000357 if ((addr & (DATA_SIZE - 1)) != 0) {
358 goto do_unaligned_access;
359 }
Peter Maydelle469b222015-04-26 16:49:23 +0100360 iotlbentry = &env->iotlb[mmu_idx][index];
Richard Henderson867b3202013-09-04 11:45:20 -0700361
362 /* ??? Note that the io helpers always read data in the target
363 byte ordering. We should push the LE/BE request down into io. */
364 val = TGT_LE(val);
Peter Maydelle469b222015-04-26 16:49:23 +0100365 glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000366 return;
367 }
368
369 /* Handle slow unaligned access (it spans two pages or IO). */
370 if (DATA_SIZE > 1
371 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
372 >= TARGET_PAGE_SIZE)) {
Samuel Damashek81daaba2016-07-08 12:54:34 -0700373 int i, index2;
374 target_ulong page2, tlb_addr2;
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000375 do_unaligned_access:
Samuel Damashek81daaba2016-07-08 12:54:34 -0700376 /* Ensure the second page is in the TLB. Note that the first page
377 is already guaranteed to be filled, and that the second page
378 cannot evict the first. */
379 page2 = (addr + DATA_SIZE) & TARGET_PAGE_MASK;
380 index2 = (page2 >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
381 tlb_addr2 = env->tlb_table[mmu_idx][index2].addr_write;
382 if (page2 != (tlb_addr2 & (TARGET_PAGE_MASK | TLB_INVALID_MASK))
383 && !VICTIM_TLB_HIT(addr_write, page2)) {
384 tlb_fill(ENV_GET_CPU(env), page2, MMU_DATA_STORE,
385 mmu_idx, retaddr);
386 }
387
388 /* XXX: not efficient, but simple. */
389 /* This loop must go in the forward direction to avoid issues
390 with self-modifying code in Windows 64-bit. */
391 for (i = 0; i < DATA_SIZE; ++i) {
Richard Henderson867b3202013-09-04 11:45:20 -0700392 /* Little-endian extract. */
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000393 uint8_t val8 = val >> (i * 8);
Richard Henderson0f842f82013-08-27 10:22:54 -0700394 /* Note the adjustment at the beginning of the function.
395 Undo that for the recursion. */
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000396 glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
Richard Henderson3972ef62015-05-13 09:10:33 -0700397 oi, retaddr + GETPC_ADJ);
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000398 }
399 return;
400 }
401
Richard Hendersonaac1fb02013-07-26 08:29:15 -1000402 haddr = addr + env->tlb_table[mmu_idx][index].addend;
Richard Henderson867b3202013-09-04 11:45:20 -0700403#if DATA_SIZE == 1
404 glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
405#else
406 glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
407#endif
bellardb92e5a22003-08-08 23:58:05 +0000408}
409
Richard Henderson867b3202013-09-04 11:45:20 -0700410#if DATA_SIZE > 1
411void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
Richard Henderson3972ef62015-05-13 09:10:33 -0700412 TCGMemOpIdx oi, uintptr_t retaddr)
Richard Henderson867b3202013-09-04 11:45:20 -0700413{
Richard Henderson3972ef62015-05-13 09:10:33 -0700414 unsigned mmu_idx = get_mmuidx(oi);
Richard Henderson867b3202013-09-04 11:45:20 -0700415 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
416 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300417 int a_bits = get_alignment_bits(get_memop(oi));
Richard Henderson867b3202013-09-04 11:45:20 -0700418 uintptr_t haddr;
419
420 /* Adjust the given return address. */
421 retaddr -= GETPC_ADJ;
422
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300423 if (a_bits > 0 && (addr & ((1 << a_bits) - 1)) != 0) {
424 cpu_unaligned_access(ENV_GET_CPU(env), addr, MMU_DATA_STORE,
425 mmu_idx, retaddr);
426 }
427
Richard Henderson867b3202013-09-04 11:45:20 -0700428 /* If the TLB entry is for a different page, reload and try again. */
429 if ((addr & TARGET_PAGE_MASK)
430 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
Samuel Damasheka3902842016-07-06 14:26:52 -0400431 if (!VICTIM_TLB_HIT(addr_write, addr)) {
Leon Alrae55e94092014-07-07 11:23:56 +0100432 tlb_fill(ENV_GET_CPU(env), addr, MMU_DATA_STORE, mmu_idx, retaddr);
Xin Tong88e89a52014-08-04 20:35:23 -0500433 }
Richard Henderson867b3202013-09-04 11:45:20 -0700434 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
435 }
436
437 /* Handle an IO access. */
438 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
Peter Maydelle469b222015-04-26 16:49:23 +0100439 CPUIOTLBEntry *iotlbentry;
Richard Henderson867b3202013-09-04 11:45:20 -0700440 if ((addr & (DATA_SIZE - 1)) != 0) {
441 goto do_unaligned_access;
442 }
Peter Maydelle469b222015-04-26 16:49:23 +0100443 iotlbentry = &env->iotlb[mmu_idx][index];
Richard Henderson867b3202013-09-04 11:45:20 -0700444
445 /* ??? Note that the io helpers always read data in the target
446 byte ordering. We should push the LE/BE request down into io. */
447 val = TGT_BE(val);
Peter Maydelle469b222015-04-26 16:49:23 +0100448 glue(io_write, SUFFIX)(env, iotlbentry, val, addr, retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -0700449 return;
450 }
451
452 /* Handle slow unaligned access (it spans two pages or IO). */
453 if (DATA_SIZE > 1
454 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
455 >= TARGET_PAGE_SIZE)) {
Samuel Damashek81daaba2016-07-08 12:54:34 -0700456 int i, index2;
457 target_ulong page2, tlb_addr2;
Richard Henderson867b3202013-09-04 11:45:20 -0700458 do_unaligned_access:
Samuel Damashek81daaba2016-07-08 12:54:34 -0700459 /* Ensure the second page is in the TLB. Note that the first page
460 is already guaranteed to be filled, and that the second page
461 cannot evict the first. */
462 page2 = (addr + DATA_SIZE) & TARGET_PAGE_MASK;
463 index2 = (page2 >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
464 tlb_addr2 = env->tlb_table[mmu_idx][index2].addr_write;
465 if (page2 != (tlb_addr2 & (TARGET_PAGE_MASK | TLB_INVALID_MASK))
466 && !VICTIM_TLB_HIT(addr_write, page2)) {
467 tlb_fill(ENV_GET_CPU(env), page2, MMU_DATA_STORE,
468 mmu_idx, retaddr);
469 }
470
Richard Henderson867b3202013-09-04 11:45:20 -0700471 /* XXX: not efficient, but simple */
Samuel Damashek81daaba2016-07-08 12:54:34 -0700472 /* This loop must go in the forward direction to avoid issues
473 with self-modifying code. */
474 for (i = 0; i < DATA_SIZE; ++i) {
Richard Henderson867b3202013-09-04 11:45:20 -0700475 /* Big-endian extract. */
476 uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
477 /* Note the adjustment at the beginning of the function.
478 Undo that for the recursion. */
479 glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
Richard Henderson3972ef62015-05-13 09:10:33 -0700480 oi, retaddr + GETPC_ADJ);
Richard Henderson867b3202013-09-04 11:45:20 -0700481 }
482 return;
483 }
484
Richard Henderson867b3202013-09-04 11:45:20 -0700485 haddr = addr + env->tlb_table[mmu_idx][index].addend;
486 glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
487}
488#endif /* DATA_SIZE > 1 */
489
Yongbok Kim3b4afc92015-06-01 12:13:23 +0100490#if DATA_SIZE == 1
491/* Probe for whether the specified guest write access is permitted.
492 * If it is not permitted then an exception will be taken in the same
493 * way as if this were a real write access (and we will not return).
494 * Otherwise the function will return, and there will be a valid
495 * entry in the TLB for this access.
496 */
497void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx,
498 uintptr_t retaddr)
499{
500 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
501 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
502
503 if ((addr & TARGET_PAGE_MASK)
504 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
505 /* TLB entry is for a different page */
Samuel Damasheka3902842016-07-06 14:26:52 -0400506 if (!VICTIM_TLB_HIT(addr_write, addr)) {
Yongbok Kim3b4afc92015-06-01 12:13:23 +0100507 tlb_fill(ENV_GET_CPU(env), addr, MMU_DATA_STORE, mmu_idx, retaddr);
508 }
509 }
510}
511#endif
bellardb769d8f2004-10-03 15:07:13 +0000512#endif /* !defined(SOFTMMU_CODE_ACCESS) */
513
514#undef READ_ACCESS_TYPE
bellardb92e5a22003-08-08 23:58:05 +0000515#undef SHIFT
516#undef DATA_TYPE
517#undef SUFFIX
Richard Henderson701e3a52013-08-27 11:31:48 -0700518#undef LSUFFIX
bellardb92e5a22003-08-08 23:58:05 +0000519#undef DATA_SIZE
bellard84b7b8e2005-11-28 21:19:04 +0000520#undef ADDR_READ
Richard Hendersonc8f94df2013-08-27 14:09:14 -0700521#undef WORD_TYPE
522#undef SDATA_TYPE
523#undef USUFFIX
524#undef SSUFFIX
Richard Henderson867b3202013-09-04 11:45:20 -0700525#undef BSWAP
526#undef TGT_BE
527#undef TGT_LE
528#undef CPU_BE
529#undef CPU_LE
530#undef helper_le_ld_name
531#undef helper_be_ld_name
532#undef helper_le_lds_name
533#undef helper_be_lds_name
534#undef helper_le_st_name
535#undef helper_be_st_name
536#undef helper_te_ld_name
537#undef helper_te_st_name