4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "cpu_loop-common.h"
24 static void gen_sigill_reg(CPUTLGState
*env
)
26 target_siginfo_t info
;
28 info
.si_signo
= TARGET_SIGILL
;
30 info
.si_code
= TARGET_ILL_PRVREG
;
31 info
._sifields
._sigfault
._addr
= env
->pc
;
32 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
35 static void do_signal(CPUTLGState
*env
, int signo
, int sigcode
)
37 target_siginfo_t info
;
39 info
.si_signo
= signo
;
41 info
._sifields
._sigfault
._addr
= env
->pc
;
43 if (signo
== TARGET_SIGSEGV
) {
44 /* The passed in sigcode is a dummy; check for a page mapping
45 and pass either MAPERR or ACCERR. */
46 target_ulong addr
= env
->excaddr
;
47 info
._sifields
._sigfault
._addr
= addr
;
48 if (page_check_range(addr
, 1, PAGE_VALID
) < 0) {
49 sigcode
= TARGET_SEGV_MAPERR
;
51 sigcode
= TARGET_SEGV_ACCERR
;
54 info
.si_code
= sigcode
;
56 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
59 static void gen_sigsegv_maperr(CPUTLGState
*env
, target_ulong addr
)
62 do_signal(env
, TARGET_SIGSEGV
, 0);
65 static void set_regval(CPUTLGState
*env
, uint8_t reg
, uint64_t val
)
67 if (unlikely(reg
>= TILEGX_R_COUNT
)) {
81 g_assert_not_reached();
88 * Compare the 8-byte contents of the CmpValue SPR with the 8-byte value in
89 * memory at the address held in the first source register. If the values are
90 * not equal, then no memory operation is performed. If the values are equal,
91 * the 8-byte quantity from the second source register is written into memory
92 * at the address held in the first source register. In either case, the result
93 * of the instruction is the value read from memory. The compare and write to
94 * memory are atomic and thus can be used for synchronization purposes. This
95 * instruction only operates for addresses aligned to a 8-byte boundary.
96 * Unaligned memory access causes an Unaligned Data Reference interrupt.
98 * Functional Description (64-bit)
99 * uint64_t memVal = memoryReadDoubleWord (rf[SrcA]);
101 * if (memVal == SPR[CmpValueSPR])
102 * memoryWriteDoubleWord (rf[SrcA], rf[SrcB]);
104 * Functional Description (32-bit)
105 * uint64_t memVal = signExtend32 (memoryReadWord (rf[SrcA]));
107 * if (memVal == signExtend32 (SPR[CmpValueSPR]))
108 * memoryWriteWord (rf[SrcA], rf[SrcB]);
111 * This function also processes exch and exch4 which need not process SPR.
113 static void do_exch(CPUTLGState
*env
, bool quad
, bool cmp
)
116 target_long val
, sprval
;
120 addr
= env
->atomic_srca
;
121 if (quad
? get_user_s64(val
, addr
) : get_user_s32(val
, addr
)) {
127 sprval
= env
->spregs
[TILEGX_SPR_CMPEXCH
];
129 sprval
= sextract64(env
->spregs
[TILEGX_SPR_CMPEXCH
], 0, 32);
133 if (!cmp
|| val
== sprval
) {
134 target_long valb
= env
->atomic_srcb
;
135 if (quad
? put_user_u64(valb
, addr
) : put_user_u32(valb
, addr
)) {
140 set_regval(env
, env
->atomic_dstr
, val
);
146 gen_sigsegv_maperr(env
, addr
);
149 static void do_fetch(CPUTLGState
*env
, int trapnr
, bool quad
)
153 target_long val
, valb
;
157 addr
= env
->atomic_srca
;
158 valb
= env
->atomic_srcb
;
159 if (quad
? get_user_s64(val
, addr
) : get_user_s32(val
, addr
)) {
164 case TILEGX_EXCP_OPCODE_FETCHADD
:
165 case TILEGX_EXCP_OPCODE_FETCHADD4
:
168 case TILEGX_EXCP_OPCODE_FETCHADDGEZ
:
174 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4
:
176 if ((int32_t)valb
< 0) {
180 case TILEGX_EXCP_OPCODE_FETCHAND
:
181 case TILEGX_EXCP_OPCODE_FETCHAND4
:
184 case TILEGX_EXCP_OPCODE_FETCHOR
:
185 case TILEGX_EXCP_OPCODE_FETCHOR4
:
189 g_assert_not_reached();
193 if (quad
? put_user_u64(valb
, addr
) : put_user_u32(valb
, addr
)) {
198 set_regval(env
, env
->atomic_dstr
, val
);
204 gen_sigsegv_maperr(env
, addr
);
207 void cpu_loop(CPUTLGState
*env
)
209 CPUState
*cs
= CPU(tilegx_env_get_cpu(env
));
214 trapnr
= cpu_exec(cs
);
216 process_queued_cpu_work(cs
);
219 case TILEGX_EXCP_SYSCALL
:
221 abi_ulong ret
= do_syscall(env
, env
->regs
[TILEGX_R_NR
],
222 env
->regs
[0], env
->regs
[1],
223 env
->regs
[2], env
->regs
[3],
224 env
->regs
[4], env
->regs
[5],
225 env
->regs
[6], env
->regs
[7]);
226 if (ret
== -TARGET_ERESTARTSYS
) {
228 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
229 env
->regs
[TILEGX_R_RE
] = ret
;
230 env
->regs
[TILEGX_R_ERR
] = TILEGX_IS_ERRNO(ret
) ? -ret
: 0;
234 case TILEGX_EXCP_OPCODE_EXCH
:
235 do_exch(env
, true, false);
237 case TILEGX_EXCP_OPCODE_EXCH4
:
238 do_exch(env
, false, false);
240 case TILEGX_EXCP_OPCODE_CMPEXCH
:
241 do_exch(env
, true, true);
243 case TILEGX_EXCP_OPCODE_CMPEXCH4
:
244 do_exch(env
, false, true);
246 case TILEGX_EXCP_OPCODE_FETCHADD
:
247 case TILEGX_EXCP_OPCODE_FETCHADDGEZ
:
248 case TILEGX_EXCP_OPCODE_FETCHAND
:
249 case TILEGX_EXCP_OPCODE_FETCHOR
:
250 do_fetch(env
, trapnr
, true);
252 case TILEGX_EXCP_OPCODE_FETCHADD4
:
253 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4
:
254 case TILEGX_EXCP_OPCODE_FETCHAND4
:
255 case TILEGX_EXCP_OPCODE_FETCHOR4
:
256 do_fetch(env
, trapnr
, false);
258 case TILEGX_EXCP_SIGNAL
:
259 do_signal(env
, env
->signo
, env
->sigcode
);
261 case TILEGX_EXCP_REG_IDN_ACCESS
:
262 case TILEGX_EXCP_REG_UDN_ACCESS
:
266 cpu_exec_step_atomic(cs
);
269 fprintf(stderr
, "trapnr is %d[0x%x].\n", trapnr
, trapnr
);
270 g_assert_not_reached();
272 process_pending_signals(env
);
276 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
279 for (i
= 0; i
< TILEGX_R_COUNT
; i
++) {
280 env
->regs
[i
] = regs
->regs
[i
];
282 for (i
= 0; i
< TILEGX_SPR_COUNT
; i
++) {