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"
21 #include "qemu-common.h"
23 #include "cpu_loop-common.h"
25 static void gen_sigill_reg(CPUTLGState
*env
)
27 target_siginfo_t info
;
29 info
.si_signo
= TARGET_SIGILL
;
31 info
.si_code
= TARGET_ILL_PRVREG
;
32 info
._sifields
._sigfault
._addr
= env
->pc
;
33 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
36 static void do_signal(CPUTLGState
*env
, int signo
, int sigcode
)
38 target_siginfo_t info
;
40 info
.si_signo
= signo
;
42 info
._sifields
._sigfault
._addr
= env
->pc
;
44 if (signo
== TARGET_SIGSEGV
) {
45 /* The passed in sigcode is a dummy; check for a page mapping
46 and pass either MAPERR or ACCERR. */
47 target_ulong addr
= env
->excaddr
;
48 info
._sifields
._sigfault
._addr
= addr
;
49 if (page_check_range(addr
, 1, PAGE_VALID
) < 0) {
50 sigcode
= TARGET_SEGV_MAPERR
;
52 sigcode
= TARGET_SEGV_ACCERR
;
55 info
.si_code
= sigcode
;
57 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
60 static void gen_sigsegv_maperr(CPUTLGState
*env
, target_ulong addr
)
63 do_signal(env
, TARGET_SIGSEGV
, 0);
66 static void set_regval(CPUTLGState
*env
, uint8_t reg
, uint64_t val
)
68 if (unlikely(reg
>= TILEGX_R_COUNT
)) {
82 g_assert_not_reached();
89 * Compare the 8-byte contents of the CmpValue SPR with the 8-byte value in
90 * memory at the address held in the first source register. If the values are
91 * not equal, then no memory operation is performed. If the values are equal,
92 * the 8-byte quantity from the second source register is written into memory
93 * at the address held in the first source register. In either case, the result
94 * of the instruction is the value read from memory. The compare and write to
95 * memory are atomic and thus can be used for synchronization purposes. This
96 * instruction only operates for addresses aligned to a 8-byte boundary.
97 * Unaligned memory access causes an Unaligned Data Reference interrupt.
99 * Functional Description (64-bit)
100 * uint64_t memVal = memoryReadDoubleWord (rf[SrcA]);
102 * if (memVal == SPR[CmpValueSPR])
103 * memoryWriteDoubleWord (rf[SrcA], rf[SrcB]);
105 * Functional Description (32-bit)
106 * uint64_t memVal = signExtend32 (memoryReadWord (rf[SrcA]));
108 * if (memVal == signExtend32 (SPR[CmpValueSPR]))
109 * memoryWriteWord (rf[SrcA], rf[SrcB]);
112 * This function also processes exch and exch4 which need not process SPR.
114 static void do_exch(CPUTLGState
*env
, bool quad
, bool cmp
)
117 target_long val
, sprval
;
121 addr
= env
->atomic_srca
;
122 if (quad
? get_user_s64(val
, addr
) : get_user_s32(val
, addr
)) {
128 sprval
= env
->spregs
[TILEGX_SPR_CMPEXCH
];
130 sprval
= sextract64(env
->spregs
[TILEGX_SPR_CMPEXCH
], 0, 32);
134 if (!cmp
|| val
== sprval
) {
135 target_long valb
= env
->atomic_srcb
;
136 if (quad
? put_user_u64(valb
, addr
) : put_user_u32(valb
, addr
)) {
141 set_regval(env
, env
->atomic_dstr
, val
);
147 gen_sigsegv_maperr(env
, addr
);
150 static void do_fetch(CPUTLGState
*env
, int trapnr
, bool quad
)
154 target_long val
, valb
;
158 addr
= env
->atomic_srca
;
159 valb
= env
->atomic_srcb
;
160 if (quad
? get_user_s64(val
, addr
) : get_user_s32(val
, addr
)) {
165 case TILEGX_EXCP_OPCODE_FETCHADD
:
166 case TILEGX_EXCP_OPCODE_FETCHADD4
:
169 case TILEGX_EXCP_OPCODE_FETCHADDGEZ
:
175 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4
:
177 if ((int32_t)valb
< 0) {
181 case TILEGX_EXCP_OPCODE_FETCHAND
:
182 case TILEGX_EXCP_OPCODE_FETCHAND4
:
185 case TILEGX_EXCP_OPCODE_FETCHOR
:
186 case TILEGX_EXCP_OPCODE_FETCHOR4
:
190 g_assert_not_reached();
194 if (quad
? put_user_u64(valb
, addr
) : put_user_u32(valb
, addr
)) {
199 set_regval(env
, env
->atomic_dstr
, val
);
205 gen_sigsegv_maperr(env
, addr
);
208 void cpu_loop(CPUTLGState
*env
)
210 CPUState
*cs
= env_cpu(env
);
215 trapnr
= cpu_exec(cs
);
217 process_queued_cpu_work(cs
);
220 case TILEGX_EXCP_SYSCALL
:
222 abi_ulong ret
= do_syscall(env
, env
->regs
[TILEGX_R_NR
],
223 env
->regs
[0], env
->regs
[1],
224 env
->regs
[2], env
->regs
[3],
225 env
->regs
[4], env
->regs
[5],
226 env
->regs
[6], env
->regs
[7]);
227 if (ret
== -TARGET_ERESTARTSYS
) {
229 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
230 env
->regs
[TILEGX_R_RE
] = ret
;
231 env
->regs
[TILEGX_R_ERR
] = TILEGX_IS_ERRNO(ret
) ? -ret
: 0;
235 case TILEGX_EXCP_OPCODE_EXCH
:
236 do_exch(env
, true, false);
238 case TILEGX_EXCP_OPCODE_EXCH4
:
239 do_exch(env
, false, false);
241 case TILEGX_EXCP_OPCODE_CMPEXCH
:
242 do_exch(env
, true, true);
244 case TILEGX_EXCP_OPCODE_CMPEXCH4
:
245 do_exch(env
, false, true);
247 case TILEGX_EXCP_OPCODE_FETCHADD
:
248 case TILEGX_EXCP_OPCODE_FETCHADDGEZ
:
249 case TILEGX_EXCP_OPCODE_FETCHAND
:
250 case TILEGX_EXCP_OPCODE_FETCHOR
:
251 do_fetch(env
, trapnr
, true);
253 case TILEGX_EXCP_OPCODE_FETCHADD4
:
254 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4
:
255 case TILEGX_EXCP_OPCODE_FETCHAND4
:
256 case TILEGX_EXCP_OPCODE_FETCHOR4
:
257 do_fetch(env
, trapnr
, false);
259 case TILEGX_EXCP_SIGNAL
:
260 do_signal(env
, env
->signo
, env
->sigcode
);
262 case TILEGX_EXCP_REG_IDN_ACCESS
:
263 case TILEGX_EXCP_REG_UDN_ACCESS
:
267 cpu_exec_step_atomic(cs
);
270 fprintf(stderr
, "trapnr is %d[0x%x].\n", trapnr
, trapnr
);
271 g_assert_not_reached();
273 process_pending_signals(env
);
277 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
280 for (i
= 0; i
< TILEGX_R_COUNT
; i
++) {
281 env
->regs
[i
] = regs
->regs
[i
];
283 for (i
= 0; i
< TILEGX_SPR_COUNT
; i
++) {