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_add_64(int64_t a1
, int64_t a2
, int64_t ar
)
128 if ((a1
> 0 && a2
> 0 && ar
< 0) || (a1
< 0 && a2
< 0 && ar
> 0)) {
129 return 3; /* overflow */
141 static uint32_t cc_calc_addu_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
143 return (ar
!= 0) + 2 * (ar
< a1
);
146 static uint32_t cc_calc_addc_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
148 /* Recover a2 + carry_in. */
149 uint64_t a2c
= ar
- a1
;
150 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
151 int carry_out
= (a2c
< a2
) || (ar
< a1
);
153 return (ar
!= 0) + 2 * carry_out
;
156 static uint32_t cc_calc_sub_64(int64_t a1
, int64_t a2
, int64_t ar
)
158 if ((a1
> 0 && a2
< 0 && ar
< 0) || (a1
< 0 && a2
> 0 && ar
> 0)) {
159 return 3; /* overflow */
171 static uint32_t cc_calc_subu_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
184 static uint32_t cc_calc_subb_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
188 if (ar
!= a1
- a2
) { /* difference means borrow-in */
189 borrow_out
= (a2
>= a1
);
191 borrow_out
= (a2
> a1
);
194 return (ar
!= 0) + 2 * !borrow_out
;
197 static uint32_t cc_calc_abs_64(int64_t dst
)
199 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
208 static uint32_t cc_calc_nabs_64(int64_t dst
)
213 static uint32_t cc_calc_comp_64(int64_t dst
)
215 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
217 } else if (dst
< 0) {
219 } else if (dst
> 0) {
227 static uint32_t cc_calc_add_32(int32_t a1
, int32_t a2
, int32_t ar
)
229 if ((a1
> 0 && a2
> 0 && ar
< 0) || (a1
< 0 && a2
< 0 && ar
> 0)) {
230 return 3; /* overflow */
242 static uint32_t cc_calc_addu_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
244 return (ar
!= 0) + 2 * (ar
< a1
);
247 static uint32_t cc_calc_addc_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
249 /* Recover a2 + carry_in. */
250 uint32_t a2c
= ar
- a1
;
251 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
252 int carry_out
= (a2c
< a2
) || (ar
< a1
);
254 return (ar
!= 0) + 2 * carry_out
;
257 static uint32_t cc_calc_sub_32(int32_t a1
, int32_t a2
, int32_t ar
)
259 if ((a1
> 0 && a2
< 0 && ar
< 0) || (a1
< 0 && a2
> 0 && ar
> 0)) {
260 return 3; /* overflow */
272 static uint32_t cc_calc_subu_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
285 static uint32_t cc_calc_subb_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
289 if (ar
!= a1
- a2
) { /* difference means borrow-in */
290 borrow_out
= (a2
>= a1
);
292 borrow_out
= (a2
> a1
);
295 return (ar
!= 0) + 2 * !borrow_out
;
298 static uint32_t cc_calc_abs_32(int32_t dst
)
300 if ((uint32_t)dst
== 0x80000000UL
) {
309 static uint32_t cc_calc_nabs_32(int32_t dst
)
314 static uint32_t cc_calc_comp_32(int32_t dst
)
316 if ((uint32_t)dst
== 0x80000000UL
) {
318 } else if (dst
< 0) {
320 } else if (dst
> 0) {
327 /* calculate condition code for insert character under mask insn */
328 static uint32_t cc_calc_icm(uint64_t mask
, uint64_t val
)
330 if ((val
& mask
) == 0) {
333 int top
= clz64(mask
);
334 if ((int64_t)(val
<< top
) < 0) {
342 static uint32_t cc_calc_sla_32(uint32_t src
, int shift
)
344 uint32_t mask
= ((1U << shift
) - 1U) << (32 - shift
);
345 uint32_t sign
= 1U << 31;
349 /* Check if the sign bit stays the same. */
355 if ((src
& mask
) != match
) {
360 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
369 static uint32_t cc_calc_sla_64(uint64_t src
, int shift
)
371 uint64_t mask
= ((1ULL << shift
) - 1ULL) << (64 - shift
);
372 uint64_t sign
= 1ULL << 63;
376 /* Check if the sign bit stays the same. */
382 if ((src
& mask
) != match
) {
387 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
396 static uint32_t cc_calc_flogr(uint64_t dst
)
401 static uint32_t cc_calc_lcbb(uint64_t dst
)
403 return dst
== 16 ? 0 : 3;
406 static uint32_t cc_calc_vc(uint64_t low
, uint64_t high
)
408 if (high
== -1ull && low
== -1ull) {
409 /* all elements match */
411 } else if (high
== 0 && low
== 0) {
412 /* no elements match */
415 /* some elements but not all match */
420 static uint32_t cc_calc_muls_32(int64_t res
)
422 const int64_t tmp
= res
>> 31;
426 } else if (tmp
&& tmp
!= -1) {
428 } else if (res
< 0) {
434 static uint64_t cc_calc_muls_64(int64_t res_high
, uint64_t res_low
)
436 if (!res_high
&& !res_low
) {
438 } else if (res_high
+ (res_low
>> 63) != 0) {
440 } else if (res_high
< 0) {
446 static uint32_t do_calc_cc(CPUS390XState
*env
, uint32_t cc_op
,
447 uint64_t src
, uint64_t dst
, uint64_t vr
)
456 /* cc_op value _is_ cc */
460 r
= cc_calc_ltgt0_32(dst
);
463 r
= cc_calc_ltgt0_64(dst
);
466 r
= cc_calc_ltgt_32(src
, dst
);
469 r
= cc_calc_ltgt_64(src
, dst
);
471 case CC_OP_LTUGTU_32
:
472 r
= cc_calc_ltugtu_32(src
, dst
);
474 case CC_OP_LTUGTU_64
:
475 r
= cc_calc_ltugtu_64(src
, dst
);
478 r
= cc_calc_tm_32(src
, dst
);
481 r
= cc_calc_tm_64(src
, dst
);
487 r
= cc_calc_add_64(src
, dst
, vr
);
490 r
= cc_calc_addu_64(src
, dst
, vr
);
493 r
= cc_calc_addc_64(src
, dst
, vr
);
496 r
= cc_calc_sub_64(src
, dst
, vr
);
499 r
= cc_calc_subu_64(src
, dst
, vr
);
502 r
= cc_calc_subb_64(src
, dst
, vr
);
505 r
= cc_calc_abs_64(dst
);
508 r
= cc_calc_nabs_64(dst
);
511 r
= cc_calc_comp_64(dst
);
514 r
= cc_calc_muls_64(src
, dst
);
518 r
= cc_calc_add_32(src
, dst
, vr
);
521 r
= cc_calc_addu_32(src
, dst
, vr
);
524 r
= cc_calc_addc_32(src
, dst
, vr
);
527 r
= cc_calc_sub_32(src
, dst
, vr
);
530 r
= cc_calc_subu_32(src
, dst
, vr
);
533 r
= cc_calc_subb_32(src
, dst
, vr
);
536 r
= cc_calc_abs_32(dst
);
539 r
= cc_calc_nabs_32(dst
);
542 r
= cc_calc_comp_32(dst
);
545 r
= cc_calc_muls_32(dst
);
549 r
= cc_calc_icm(src
, dst
);
552 r
= cc_calc_sla_32(src
, dst
);
555 r
= cc_calc_sla_64(src
, dst
);
558 r
= cc_calc_flogr(dst
);
561 r
= cc_calc_lcbb(dst
);
564 r
= cc_calc_vc(src
, dst
);
568 r
= set_cc_nz_f32(dst
);
571 r
= set_cc_nz_f64(dst
);
574 r
= set_cc_nz_f128(make_float128(src
, dst
));
578 cpu_abort(env_cpu(env
), "Unknown CC operation: %s\n", cc_name(cc_op
));
581 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__
,
582 cc_name(cc_op
), src
, dst
, vr
, r
);
586 uint32_t calc_cc(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
, uint64_t dst
,
589 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
592 uint32_t HELPER(calc_cc
)(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
,
593 uint64_t dst
, uint64_t vr
)
595 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
598 #ifndef CONFIG_USER_ONLY
599 void HELPER(load_psw
)(CPUS390XState
*env
, uint64_t mask
, uint64_t addr
)
601 load_psw(env
, mask
, addr
);
602 cpu_loop_exit(env_cpu(env
));
605 void HELPER(sacf
)(CPUS390XState
*env
, uint64_t a1
)
607 HELPER_LOG("%s: %16" PRIx64
"\n", __func__
, a1
);
609 switch (a1
& 0xf00) {
611 env
->psw
.mask
&= ~PSW_MASK_ASC
;
612 env
->psw
.mask
|= PSW_ASC_PRIMARY
;
615 env
->psw
.mask
&= ~PSW_MASK_ASC
;
616 env
->psw
.mask
|= PSW_ASC_SECONDARY
;
619 env
->psw
.mask
&= ~PSW_MASK_ASC
;
620 env
->psw
.mask
|= PSW_ASC_HOME
;
623 HELPER_LOG("unknown sacf mode: %" PRIx64
"\n", a1
);
624 tcg_s390_program_interrupt(env
, PGM_SPECIFICATION
, GETPC());