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_add_64(int64_t a1
, int64_t a2
, int64_t ar
)
134 if ((a1
> 0 && a2
> 0 && ar
< 0) || (a1
< 0 && a2
< 0 && ar
> 0)) {
135 return 3; /* overflow */
147 static uint32_t cc_calc_sub_64(int64_t a1
, int64_t a2
, int64_t ar
)
149 if ((a1
> 0 && a2
< 0 && ar
< 0) || (a1
< 0 && a2
> 0 && ar
> 0)) {
150 return 3; /* overflow */
162 static uint32_t cc_calc_subu_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
175 static uint32_t cc_calc_subb_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
179 if (ar
!= a1
- a2
) { /* difference means borrow-in */
180 borrow_out
= (a2
>= a1
);
182 borrow_out
= (a2
> a1
);
185 return (ar
!= 0) + 2 * !borrow_out
;
188 static uint32_t cc_calc_abs_64(int64_t dst
)
190 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
199 static uint32_t cc_calc_nabs_64(int64_t dst
)
204 static uint32_t cc_calc_comp_64(int64_t dst
)
206 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
208 } else if (dst
< 0) {
210 } else if (dst
> 0) {
218 static uint32_t cc_calc_add_32(int32_t a1
, int32_t a2
, int32_t ar
)
220 if ((a1
> 0 && a2
> 0 && ar
< 0) || (a1
< 0 && a2
< 0 && ar
> 0)) {
221 return 3; /* overflow */
233 static uint32_t cc_calc_sub_32(int32_t a1
, int32_t a2
, int32_t ar
)
235 if ((a1
> 0 && a2
< 0 && ar
< 0) || (a1
< 0 && a2
> 0 && ar
> 0)) {
236 return 3; /* overflow */
248 static uint32_t cc_calc_subu_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
261 static uint32_t cc_calc_subb_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
265 if (ar
!= a1
- a2
) { /* difference means borrow-in */
266 borrow_out
= (a2
>= a1
);
268 borrow_out
= (a2
> a1
);
271 return (ar
!= 0) + 2 * !borrow_out
;
274 static uint32_t cc_calc_abs_32(int32_t dst
)
276 if ((uint32_t)dst
== 0x80000000UL
) {
285 static uint32_t cc_calc_nabs_32(int32_t dst
)
290 static uint32_t cc_calc_comp_32(int32_t dst
)
292 if ((uint32_t)dst
== 0x80000000UL
) {
294 } else if (dst
< 0) {
296 } else if (dst
> 0) {
303 /* calculate condition code for insert character under mask insn */
304 static uint32_t cc_calc_icm(uint64_t mask
, uint64_t val
)
306 if ((val
& mask
) == 0) {
309 int top
= clz64(mask
);
310 if ((int64_t)(val
<< top
) < 0) {
318 static uint32_t cc_calc_sla_32(uint32_t src
, int shift
)
320 uint32_t mask
= ((1U << shift
) - 1U) << (32 - shift
);
321 uint32_t sign
= 1U << 31;
325 /* Check if the sign bit stays the same. */
331 if ((src
& mask
) != match
) {
336 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
345 static uint32_t cc_calc_sla_64(uint64_t src
, int shift
)
347 uint64_t mask
= ((1ULL << shift
) - 1ULL) << (64 - shift
);
348 uint64_t sign
= 1ULL << 63;
352 /* Check if the sign bit stays the same. */
358 if ((src
& mask
) != match
) {
363 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
372 static uint32_t cc_calc_flogr(uint64_t dst
)
377 static uint32_t cc_calc_lcbb(uint64_t dst
)
379 return dst
== 16 ? 0 : 3;
382 static uint32_t cc_calc_vc(uint64_t low
, uint64_t high
)
384 if (high
== -1ull && low
== -1ull) {
385 /* all elements match */
387 } else if (high
== 0 && low
== 0) {
388 /* no elements match */
391 /* some elements but not all match */
396 static uint32_t cc_calc_muls_32(int64_t res
)
398 const int64_t tmp
= res
>> 31;
402 } else if (tmp
&& tmp
!= -1) {
404 } else if (res
< 0) {
410 static uint64_t cc_calc_muls_64(int64_t res_high
, uint64_t res_low
)
412 if (!res_high
&& !res_low
) {
414 } else if (res_high
+ (res_low
>> 63) != 0) {
416 } else if (res_high
< 0) {
422 static uint32_t do_calc_cc(CPUS390XState
*env
, uint32_t cc_op
,
423 uint64_t src
, uint64_t dst
, uint64_t vr
)
432 /* cc_op value _is_ cc */
436 r
= cc_calc_ltgt0_32(dst
);
439 r
= cc_calc_ltgt0_64(dst
);
442 r
= cc_calc_ltgt_32(src
, dst
);
445 r
= cc_calc_ltgt_64(src
, dst
);
447 case CC_OP_LTUGTU_32
:
448 r
= cc_calc_ltugtu_32(src
, dst
);
450 case CC_OP_LTUGTU_64
:
451 r
= cc_calc_ltugtu_64(src
, dst
);
454 r
= cc_calc_tm_32(src
, dst
);
457 r
= cc_calc_tm_64(src
, dst
);
463 r
= cc_calc_addu(src
, dst
);
466 r
= cc_calc_add_64(src
, dst
, vr
);
469 r
= cc_calc_sub_64(src
, dst
, vr
);
472 r
= cc_calc_subu_64(src
, dst
, vr
);
475 r
= cc_calc_subb_64(src
, dst
, vr
);
478 r
= cc_calc_abs_64(dst
);
481 r
= cc_calc_nabs_64(dst
);
484 r
= cc_calc_comp_64(dst
);
487 r
= cc_calc_muls_64(src
, dst
);
491 r
= cc_calc_add_32(src
, dst
, vr
);
494 r
= cc_calc_sub_32(src
, dst
, vr
);
497 r
= cc_calc_subu_32(src
, dst
, vr
);
500 r
= cc_calc_subb_32(src
, dst
, vr
);
503 r
= cc_calc_abs_32(dst
);
506 r
= cc_calc_nabs_32(dst
);
509 r
= cc_calc_comp_32(dst
);
512 r
= cc_calc_muls_32(dst
);
516 r
= cc_calc_icm(src
, dst
);
519 r
= cc_calc_sla_32(src
, dst
);
522 r
= cc_calc_sla_64(src
, dst
);
525 r
= cc_calc_flogr(dst
);
528 r
= cc_calc_lcbb(dst
);
531 r
= cc_calc_vc(src
, dst
);
535 r
= set_cc_nz_f32(dst
);
538 r
= set_cc_nz_f64(dst
);
541 r
= set_cc_nz_f128(make_float128(src
, dst
));
545 cpu_abort(env_cpu(env
), "Unknown CC operation: %s\n", cc_name(cc_op
));
548 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__
,
549 cc_name(cc_op
), src
, dst
, vr
, r
);
553 uint32_t calc_cc(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
, uint64_t dst
,
556 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
559 uint32_t HELPER(calc_cc
)(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
,
560 uint64_t dst
, uint64_t vr
)
562 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
565 #ifndef CONFIG_USER_ONLY
566 void HELPER(load_psw
)(CPUS390XState
*env
, uint64_t mask
, uint64_t addr
)
568 load_psw(env
, mask
, addr
);
569 cpu_loop_exit(env_cpu(env
));
572 void HELPER(sacf
)(CPUS390XState
*env
, uint64_t a1
)
574 HELPER_LOG("%s: %16" PRIx64
"\n", __func__
, a1
);
576 switch (a1
& 0xf00) {
578 env
->psw
.mask
&= ~PSW_MASK_ASC
;
579 env
->psw
.mask
|= PSW_ASC_PRIMARY
;
582 env
->psw
.mask
&= ~PSW_MASK_ASC
;
583 env
->psw
.mask
|= PSW_ASC_SECONDARY
;
586 env
->psw
.mask
&= ~PSW_MASK_ASC
;
587 env
->psw
.mask
|= PSW_ASC_HOME
;
590 HELPER_LOG("unknown sacf mode: %" PRIx64
"\n", a1
);
591 tcg_s390_program_interrupt(env
, PGM_SPECIFICATION
, GETPC());