blob: ef3dafcbb3fd302a307adaca8ba27e8db629b11f [file] [log] [blame]
bellard6af0bf92005-07-02 14:58:51 +00001/*
2 * MIPS emulation helpers for qemu.
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard6af0bf92005-07-02 14:58:51 +00004 * Copyright (c) 2004-2005 Jocelyn Mayer
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
Chetan Pant89975212020-10-16 14:35:09 +00009 * version 2.1 of the License, or (at your option) any later version.
bellard6af0bf92005-07-02 14:58:51 +000010 *
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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Aleksandar Markovic256eb7e2020-02-03 16:57:22 +010018 *
bellard6af0bf92005-07-02 14:58:51 +000019 */
Aleksandar Markovic256eb7e2020-02-03 16:57:22 +010020
Peter Maydellc6848222016-01-18 17:35:00 +000021#include "qemu/osdep.h"
Blue Swirl3e457172011-07-13 12:44:15 +000022#include "cpu.h"
Philippe Mathieu-Daudé26aa3d92017-09-20 16:49:30 -030023#include "internal.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070024#include "exec/helper-proto.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010025#include "exec/exec-all.h"
Tony Nguyene5018242019-08-24 04:36:41 +100026#include "exec/memop.h"
Philippe Mathieu-Daudé81ddae72020-11-14 19:03:11 +010027#include "fpu_helper.h"
Aleksandar Markovic256eb7e2020-02-03 16:57:22 +010028
Yongbok Kim15eacb92014-06-27 08:49:05 +010029static inline target_ulong bitswap(target_ulong v)
30{
Leon Alrae74dda9872014-10-22 14:00:29 +010031 v = ((v >> 1) & (target_ulong)0x5555555555555555ULL) |
32 ((v & (target_ulong)0x5555555555555555ULL) << 1);
33 v = ((v >> 2) & (target_ulong)0x3333333333333333ULL) |
34 ((v & (target_ulong)0x3333333333333333ULL) << 2);
35 v = ((v >> 4) & (target_ulong)0x0F0F0F0F0F0F0F0FULL) |
36 ((v & (target_ulong)0x0F0F0F0F0F0F0F0FULL) << 4);
Yongbok Kim15eacb92014-06-27 08:49:05 +010037 return v;
38}
39
40#ifdef TARGET_MIPS64
41target_ulong helper_dbitswap(target_ulong rt)
42{
43 return bitswap(rt);
44}
45#endif
46
47target_ulong helper_bitswap(target_ulong rt)
48{
49 return (int32_t)bitswap(rt);
50}
51
Matthew Fortunee222f502018-08-02 16:16:20 +020052target_ulong helper_rotx(target_ulong rs, uint32_t shift, uint32_t shiftx,
53 uint32_t stripe)
54{
55 int i;
56 uint64_t tmp0 = ((uint64_t)rs) << 32 | ((uint64_t)rs & 0xffffffff);
57 uint64_t tmp1 = tmp0;
58 for (i = 0; i <= 46; i++) {
59 int s;
60 if (i & 0x8) {
61 s = shift;
62 } else {
63 s = shiftx;
64 }
65
66 if (stripe != 0 && !(i & 0x4)) {
67 s = ~s;
68 }
69 if (s & 0x10) {
70 if (tmp0 & (1LL << (i + 16))) {
71 tmp1 |= 1LL << i;
72 } else {
73 tmp1 &= ~(1LL << i);
74 }
75 }
76 }
77
78 uint64_t tmp2 = tmp1;
79 for (i = 0; i <= 38; i++) {
80 int s;
81 if (i & 0x4) {
82 s = shift;
83 } else {
84 s = shiftx;
85 }
86
87 if (s & 0x8) {
88 if (tmp1 & (1LL << (i + 8))) {
89 tmp2 |= 1LL << i;
90 } else {
91 tmp2 &= ~(1LL << i);
92 }
93 }
94 }
95
96 uint64_t tmp3 = tmp2;
97 for (i = 0; i <= 34; i++) {
98 int s;
99 if (i & 0x2) {
100 s = shift;
101 } else {
102 s = shiftx;
103 }
104 if (s & 0x4) {
105 if (tmp2 & (1LL << (i + 4))) {
106 tmp3 |= 1LL << i;
107 } else {
108 tmp3 &= ~(1LL << i);
109 }
110 }
111 }
112
113 uint64_t tmp4 = tmp3;
114 for (i = 0; i <= 32; i++) {
115 int s;
116 if (i & 0x1) {
117 s = shift;
118 } else {
119 s = shiftx;
120 }
121 if (s & 0x2) {
122 if (tmp3 & (1LL << (i + 2))) {
123 tmp4 |= 1LL << i;
124 } else {
125 tmp4 &= ~(1LL << i);
126 }
127 }
128 }
129
130 uint64_t tmp5 = tmp4;
131 for (i = 0; i <= 31; i++) {
132 int s;
133 s = shift;
134 if (s & 0x1) {
135 if (tmp4 & (1LL << (i + 1))) {
136 tmp5 |= 1LL << i;
137 } else {
138 tmp5 &= ~(1LL << i);
139 }
140 }
141 }
142
143 return (int64_t)(int32_t)(uint32_t)tmp5;
144}
145
aurel32d9bea112009-04-15 14:41:44 +0000146void helper_fork(target_ulong arg1, target_ulong arg2)
thsf1aa6322008-06-09 07:13:38 +0000147{
Aleksandar Markovic14521a22019-10-23 12:23:35 +0200148 /*
149 * arg1 = rt, arg2 = rs
150 * TODO: store to TC register
151 */
thsf1aa6322008-06-09 07:13:38 +0000152}
153
Blue Swirl895c2d02012-09-02 14:52:59 +0000154target_ulong helper_yield(CPUMIPSState *env, target_ulong arg)
thsf1aa6322008-06-09 07:13:38 +0000155{
Blue Swirl1c7242d2010-09-18 05:53:15 +0000156 target_long arg1 = arg;
157
aurel32d9bea112009-04-15 14:41:44 +0000158 if (arg1 < 0) {
thsf1aa6322008-06-09 07:13:38 +0000159 /* No scheduling policy implemented. */
aurel32d9bea112009-04-15 14:41:44 +0000160 if (arg1 != -2) {
thsf1aa6322008-06-09 07:13:38 +0000161 if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
thsb5dc7732008-06-27 10:02:35 +0000162 env->active_tc.CP0_TCStatus & (1 << CP0TCSt_DT)) {
thsf1aa6322008-06-09 07:13:38 +0000163 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
164 env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
Pavel Dovgaluk9c708c72015-07-10 12:57:08 +0300165 do_raise_exception(env, EXCP_THREAD, GETPC());
thsf1aa6322008-06-09 07:13:38 +0000166 }
167 }
aurel32d9bea112009-04-15 14:41:44 +0000168 } else if (arg1 == 0) {
Aleksandar Markovic14521a22019-10-23 12:23:35 +0200169 if (0) {
170 /* TODO: TC underflow */
thsf1aa6322008-06-09 07:13:38 +0000171 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
Pavel Dovgaluk9c708c72015-07-10 12:57:08 +0300172 do_raise_exception(env, EXCP_THREAD, GETPC());
thsf1aa6322008-06-09 07:13:38 +0000173 } else {
Aleksandar Markovic14521a22019-10-23 12:23:35 +0200174 /* TODO: Deallocate TC */
thsf1aa6322008-06-09 07:13:38 +0000175 }
aurel32d9bea112009-04-15 14:41:44 +0000176 } else if (arg1 > 0) {
thsf1aa6322008-06-09 07:13:38 +0000177 /* Yield qualifier inputs not implemented. */
178 env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
179 env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
Pavel Dovgaluk9c708c72015-07-10 12:57:08 +0300180 do_raise_exception(env, EXCP_THREAD, GETPC());
thsf1aa6322008-06-09 07:13:38 +0000181 }
thsbe24bb42008-06-23 12:57:09 +0000182 return env->CP0_YQMask;
thsf1aa6322008-06-09 07:13:38 +0000183}
184
James Hogand96391c2016-04-27 23:21:06 +0100185static inline void check_hwrena(CPUMIPSState *env, int reg, uintptr_t pc)
Yongbok Kimb00c7212015-10-29 15:18:39 +0000186{
187 if ((env->hflags & MIPS_HFLAG_CP0) || (env->CP0_HWREna & (1 << reg))) {
188 return;
189 }
James Hogand96391c2016-04-27 23:21:06 +0100190 do_raise_exception(env, EXCP_RI, pc);
Yongbok Kimb00c7212015-10-29 15:18:39 +0000191}
192
Blue Swirl895c2d02012-09-02 14:52:59 +0000193target_ulong helper_rdhwr_cpunum(CPUMIPSState *env)
ths2b0233a2008-06-12 12:42:35 +0000194{
James Hogand96391c2016-04-27 23:21:06 +0100195 check_hwrena(env, 0, GETPC());
Yongbok Kimb00c7212015-10-29 15:18:39 +0000196 return env->CP0_EBase & 0x3ff;
ths2b0233a2008-06-12 12:42:35 +0000197}
198
Blue Swirl895c2d02012-09-02 14:52:59 +0000199target_ulong helper_rdhwr_synci_step(CPUMIPSState *env)
ths2b0233a2008-06-12 12:42:35 +0000200{
James Hogand96391c2016-04-27 23:21:06 +0100201 check_hwrena(env, 1, GETPC());
Yongbok Kimb00c7212015-10-29 15:18:39 +0000202 return env->SYNCI_Step;
ths2b0233a2008-06-12 12:42:35 +0000203}
204
Blue Swirl895c2d02012-09-02 14:52:59 +0000205target_ulong helper_rdhwr_cc(CPUMIPSState *env)
ths2b0233a2008-06-12 12:42:35 +0000206{
James Hogand96391c2016-04-27 23:21:06 +0100207 check_hwrena(env, 2, GETPC());
Alex Smithcdfcad72015-09-08 11:34:11 +0100208#ifdef CONFIG_USER_ONLY
Aleksandar Markovic215581b2019-02-11 16:28:16 +0100209 return env->CP0_Count;
Alex Smithcdfcad72015-09-08 11:34:11 +0100210#else
Aleksandar Markovic215581b2019-02-11 16:28:16 +0100211 return (int32_t)cpu_mips_get_count(env);
Alex Smithcdfcad72015-09-08 11:34:11 +0100212#endif
ths2b0233a2008-06-12 12:42:35 +0000213}
214
Blue Swirl895c2d02012-09-02 14:52:59 +0000215target_ulong helper_rdhwr_ccres(CPUMIPSState *env)
ths2b0233a2008-06-12 12:42:35 +0000216{
James Hogand96391c2016-04-27 23:21:06 +0100217 check_hwrena(env, 3, GETPC());
Yongbok Kimb00c7212015-10-29 15:18:39 +0000218 return env->CCRes;
219}
thsbe24bb42008-06-23 12:57:09 +0000220
Yongbok Kimb00c7212015-10-29 15:18:39 +0000221target_ulong helper_rdhwr_performance(CPUMIPSState *env)
222{
James Hogand96391c2016-04-27 23:21:06 +0100223 check_hwrena(env, 4, GETPC());
Yongbok Kimb00c7212015-10-29 15:18:39 +0000224 return env->CP0_Performance0;
225}
226
227target_ulong helper_rdhwr_xnp(CPUMIPSState *env)
228{
James Hogand96391c2016-04-27 23:21:06 +0100229 check_hwrena(env, 5, GETPC());
Yongbok Kimb00c7212015-10-29 15:18:39 +0000230 return (env->CP0_Config5 >> CP0C5_XNP) & 1;
ths2b0233a2008-06-12 12:42:35 +0000231}
232
Blue Swirl895c2d02012-09-02 14:52:59 +0000233void helper_pmon(CPUMIPSState *env, int function)
bellard6af0bf92005-07-02 14:58:51 +0000234{
235 function /= 2;
236 switch (function) {
237 case 2: /* TODO: char inbyte(int waitflag); */
Aleksandar Markovic14521a22019-10-23 12:23:35 +0200238 if (env->active_tc.gpr[4] == 0) {
thsb5dc7732008-06-27 10:02:35 +0000239 env->active_tc.gpr[2] = -1;
Aleksandar Markovic14521a22019-10-23 12:23:35 +0200240 }
bellard6af0bf92005-07-02 14:58:51 +0000241 /* Fall through */
242 case 11: /* TODO: char inbyte (void); */
thsb5dc7732008-06-27 10:02:35 +0000243 env->active_tc.gpr[2] = -1;
bellard6af0bf92005-07-02 14:58:51 +0000244 break;
245 case 3:
246 case 12:
thsb5dc7732008-06-27 10:02:35 +0000247 printf("%c", (char)(env->active_tc.gpr[4] & 0xFF));
bellard6af0bf92005-07-02 14:58:51 +0000248 break;
249 case 17:
250 break;
251 case 158:
252 {
Stefan Weilb69e48a2012-04-12 15:43:09 +0200253 unsigned char *fmt = (void *)(uintptr_t)env->active_tc.gpr[4];
bellard6af0bf92005-07-02 14:58:51 +0000254 printf("%s", fmt);
255 }
256 break;
257 }
258}
bellarde37e8632005-07-04 22:17:33 +0000259
ths5fafdf22007-09-16 21:08:06 +0000260#if !defined(CONFIG_USER_ONLY)
bellarde37e8632005-07-04 22:17:33 +0000261
Paolo Bonzini93e22322014-03-28 18:14:58 +0100262void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
Sergey Sorokinb35399b2016-06-14 15:26:17 +0300263 MMUAccessType access_type,
264 int mmu_idx, uintptr_t retaddr)
bellard4ad40f32005-12-05 19:59:36 +0000265{
Paolo Bonzini93e22322014-03-28 18:14:58 +0100266 MIPSCPU *cpu = MIPS_CPU(cs);
267 CPUMIPSState *env = &cpu->env;
Leon Alraeaea14092014-07-07 11:24:01 +0100268 int error_code = 0;
269 int excp;
Paolo Bonzini93e22322014-03-28 18:14:58 +0100270
Yongbok Kime807bcc2018-08-02 16:15:55 +0200271 if (!(env->hflags & MIPS_HFLAG_DM)) {
272 env->CP0_BadVAddr = addr;
273 }
Leon Alraeaea14092014-07-07 11:24:01 +0100274
275 if (access_type == MMU_DATA_STORE) {
276 excp = EXCP_AdES;
277 } else {
278 excp = EXCP_AdEL;
279 if (access_type == MMU_INST_FETCH) {
280 error_code |= EXCP_INST_NOTAVAIL;
281 }
282 }
283
284 do_raise_exception_err(env, excp, error_code, retaddr);
bellard4ad40f32005-12-05 19:59:36 +0000285}
286
Peter Maydell4f02a062019-08-02 17:04:57 +0100287void mips_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
288 vaddr addr, unsigned size,
289 MMUAccessType access_type,
290 int mmu_idx, MemTxAttrs attrs,
291 MemTxResult response, uintptr_t retaddr)
ths647de6c2007-10-20 19:45:44 +0000292{
Andreas Färberc658b942013-05-27 06:49:53 +0200293 MIPSCPU *cpu = MIPS_CPU(cs);
Richard Henderson3803b6b2021-02-27 12:44:00 -0800294 MIPSCPUClass *mcc = MIPS_CPU_GET_CLASS(cpu);
Andreas Färberc658b942013-05-27 06:49:53 +0200295 CPUMIPSState *env = &cpu->env;
296
Peter Maydell4f02a062019-08-02 17:04:57 +0100297 if (access_type == MMU_INST_FETCH) {
298 do_raise_exception(env, EXCP_IBE, retaddr);
Richard Henderson3803b6b2021-02-27 12:44:00 -0800299 } else if (!mcc->no_data_aborts) {
Peter Maydell4f02a062019-08-02 17:04:57 +0100300 do_raise_exception(env, EXCP_DBE, retaddr);
Andreas Färberc658b942013-05-27 06:49:53 +0200301 }
ths647de6c2007-10-20 19:45:44 +0000302}
thsf1aa6322008-06-09 07:13:38 +0000303#endif /* !CONFIG_USER_ONLY */