2 * S/390 condition code helper routines
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2009 Alexander Graf
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
24 #include "tcg_s390x.h"
25 #include "exec/exec-all.h"
26 #include "exec/helper-proto.h"
27 #include "qemu/host-utils.h"
29 /* #define DEBUG_HELPER */
31 #define HELPER_LOG(x...) qemu_log(x)
33 #define HELPER_LOG(x...)
36 static uint32_t cc_calc_ltgt_32(int32_t src
, int32_t dst
)
40 } else if (src
< dst
) {
47 static uint32_t cc_calc_ltgt0_32(int32_t dst
)
49 return cc_calc_ltgt_32(dst
, 0);
52 static uint32_t cc_calc_ltgt_64(int64_t src
, int64_t dst
)
56 } else if (src
< dst
) {
63 static uint32_t cc_calc_ltgt0_64(int64_t dst
)
65 return cc_calc_ltgt_64(dst
, 0);
68 static uint32_t cc_calc_ltugtu_32(uint32_t src
, uint32_t dst
)
72 } else if (src
< dst
) {
79 static uint32_t cc_calc_ltugtu_64(uint64_t src
, uint64_t dst
)
83 } else if (src
< dst
) {
90 static uint32_t cc_calc_tm_32(uint32_t val
, uint32_t mask
)
92 uint32_t r
= val
& mask
;
96 } else if (r
== mask
) {
103 static uint32_t cc_calc_tm_64(uint64_t val
, uint64_t mask
)
105 uint64_t r
= val
& mask
;
109 } else if (r
== mask
) {
112 int top
= clz64(mask
);
113 if ((int64_t)(val
<< top
) < 0) {
121 static uint32_t cc_calc_nz(uint64_t dst
)
126 static uint32_t cc_calc_addu(uint64_t carry_out
, uint64_t result
)
128 g_assert(carry_out
<= 1);
129 return (result
!= 0) + 2 * carry_out
;
132 static uint32_t cc_calc_subu(uint64_t borrow_out
, uint64_t result
)
134 return cc_calc_addu(borrow_out
+ 1, result
);
137 static uint32_t cc_calc_add_64(int64_t a1
, int64_t a2
, int64_t ar
)
139 if ((a1
> 0 && a2
> 0 && ar
< 0) || (a1
< 0 && a2
< 0 && ar
> 0)) {
140 return 3; /* overflow */
152 static uint32_t cc_calc_sub_64(int64_t a1
, int64_t a2
, int64_t ar
)
154 if ((a1
> 0 && a2
< 0 && ar
< 0) || (a1
< 0 && a2
> 0 && ar
> 0)) {
155 return 3; /* overflow */
167 static uint32_t cc_calc_abs_64(int64_t dst
)
169 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
178 static uint32_t cc_calc_nabs_64(int64_t dst
)
183 static uint32_t cc_calc_comp_64(int64_t dst
)
185 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
187 } else if (dst
< 0) {
189 } else if (dst
> 0) {
197 static uint32_t cc_calc_add_32(int32_t a1
, int32_t a2
, int32_t ar
)
199 if ((a1
> 0 && a2
> 0 && ar
< 0) || (a1
< 0 && a2
< 0 && ar
> 0)) {
200 return 3; /* overflow */
212 static uint32_t cc_calc_sub_32(int32_t a1
, int32_t a2
, int32_t ar
)
214 if ((a1
> 0 && a2
< 0 && ar
< 0) || (a1
< 0 && a2
> 0 && ar
> 0)) {
215 return 3; /* overflow */
227 static uint32_t cc_calc_abs_32(int32_t dst
)
229 if ((uint32_t)dst
== 0x80000000UL
) {
238 static uint32_t cc_calc_nabs_32(int32_t dst
)
243 static uint32_t cc_calc_comp_32(int32_t dst
)
245 if ((uint32_t)dst
== 0x80000000UL
) {
247 } else if (dst
< 0) {
249 } else if (dst
> 0) {
256 /* calculate condition code for insert character under mask insn */
257 static uint32_t cc_calc_icm(uint64_t mask
, uint64_t val
)
259 if ((val
& mask
) == 0) {
262 int top
= clz64(mask
);
263 if ((int64_t)(val
<< top
) < 0) {
271 static uint32_t cc_calc_sla_32(uint32_t src
, int shift
)
273 uint32_t mask
= ((1U << shift
) - 1U) << (32 - shift
);
274 uint32_t sign
= 1U << 31;
278 /* Check if the sign bit stays the same. */
284 if ((src
& mask
) != match
) {
289 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
298 static uint32_t cc_calc_sla_64(uint64_t src
, int shift
)
300 uint64_t mask
= ((1ULL << shift
) - 1ULL) << (64 - shift
);
301 uint64_t sign
= 1ULL << 63;
305 /* Check if the sign bit stays the same. */
311 if ((src
& mask
) != match
) {
316 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
325 static uint32_t cc_calc_flogr(uint64_t dst
)
330 static uint32_t cc_calc_lcbb(uint64_t dst
)
332 return dst
== 16 ? 0 : 3;
335 static uint32_t cc_calc_vc(uint64_t low
, uint64_t high
)
337 if (high
== -1ull && low
== -1ull) {
338 /* all elements match */
340 } else if (high
== 0 && low
== 0) {
341 /* no elements match */
344 /* some elements but not all match */
349 static uint32_t cc_calc_muls_32(int64_t res
)
351 const int64_t tmp
= res
>> 31;
355 } else if (tmp
&& tmp
!= -1) {
357 } else if (res
< 0) {
363 static uint64_t cc_calc_muls_64(int64_t res_high
, uint64_t res_low
)
365 if (!res_high
&& !res_low
) {
367 } else if (res_high
+ (res_low
>> 63) != 0) {
369 } else if (res_high
< 0) {
375 static uint32_t do_calc_cc(CPUS390XState
*env
, uint32_t cc_op
,
376 uint64_t src
, uint64_t dst
, uint64_t vr
)
385 /* cc_op value _is_ cc */
389 r
= cc_calc_ltgt0_32(dst
);
392 r
= cc_calc_ltgt0_64(dst
);
395 r
= cc_calc_ltgt_32(src
, dst
);
398 r
= cc_calc_ltgt_64(src
, dst
);
400 case CC_OP_LTUGTU_32
:
401 r
= cc_calc_ltugtu_32(src
, dst
);
403 case CC_OP_LTUGTU_64
:
404 r
= cc_calc_ltugtu_64(src
, dst
);
407 r
= cc_calc_tm_32(src
, dst
);
410 r
= cc_calc_tm_64(src
, dst
);
416 r
= cc_calc_addu(src
, dst
);
419 r
= cc_calc_subu(src
, dst
);
422 r
= cc_calc_add_64(src
, dst
, vr
);
425 r
= cc_calc_sub_64(src
, dst
, vr
);
428 r
= cc_calc_abs_64(dst
);
431 r
= cc_calc_nabs_64(dst
);
434 r
= cc_calc_comp_64(dst
);
437 r
= cc_calc_muls_64(src
, dst
);
441 r
= cc_calc_add_32(src
, dst
, vr
);
444 r
= cc_calc_sub_32(src
, dst
, vr
);
447 r
= cc_calc_abs_32(dst
);
450 r
= cc_calc_nabs_32(dst
);
453 r
= cc_calc_comp_32(dst
);
456 r
= cc_calc_muls_32(dst
);
460 r
= cc_calc_icm(src
, dst
);
463 r
= cc_calc_sla_32(src
, dst
);
466 r
= cc_calc_sla_64(src
, dst
);
469 r
= cc_calc_flogr(dst
);
472 r
= cc_calc_lcbb(dst
);
475 r
= cc_calc_vc(src
, dst
);
479 r
= set_cc_nz_f32(dst
);
482 r
= set_cc_nz_f64(dst
);
485 r
= set_cc_nz_f128(make_float128(src
, dst
));
489 cpu_abort(env_cpu(env
), "Unknown CC operation: %s\n", cc_name(cc_op
));
492 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__
,
493 cc_name(cc_op
), src
, dst
, vr
, r
);
497 uint32_t calc_cc(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
, uint64_t dst
,
500 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
503 uint32_t HELPER(calc_cc
)(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
,
504 uint64_t dst
, uint64_t vr
)
506 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
509 #ifndef CONFIG_USER_ONLY
510 void HELPER(load_psw
)(CPUS390XState
*env
, uint64_t mask
, uint64_t addr
)
512 load_psw(env
, mask
, addr
);
513 cpu_loop_exit(env_cpu(env
));
516 void HELPER(sacf
)(CPUS390XState
*env
, uint64_t a1
)
518 HELPER_LOG("%s: %16" PRIx64
"\n", __func__
, a1
);
520 switch (a1
& 0xf00) {
522 env
->psw
.mask
&= ~PSW_MASK_ASC
;
523 env
->psw
.mask
|= PSW_ASC_PRIMARY
;
526 env
->psw
.mask
&= ~PSW_MASK_ASC
;
527 env
->psw
.mask
|= PSW_ASC_SECONDARY
;
530 env
->psw
.mask
&= ~PSW_MASK_ASC
;
531 env
->psw
.mask
|= PSW_ASC_HOME
;
534 HELPER_LOG("unknown sacf mode: %" PRIx64
"\n", a1
);
535 tcg_s390_program_interrupt(env
, PGM_SPECIFICATION
, GETPC());