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 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/>.
22 #include "exec/helper-proto.h"
23 #include "qemu/host-utils.h"
25 /* #define DEBUG_HELPER */
27 #define HELPER_LOG(x...) qemu_log(x)
29 #define HELPER_LOG(x...)
32 static uint32_t cc_calc_ltgt_32(int32_t src
, int32_t dst
)
36 } else if (src
< dst
) {
43 static uint32_t cc_calc_ltgt0_32(int32_t dst
)
45 return cc_calc_ltgt_32(dst
, 0);
48 static uint32_t cc_calc_ltgt_64(int64_t src
, int64_t dst
)
52 } else if (src
< dst
) {
59 static uint32_t cc_calc_ltgt0_64(int64_t dst
)
61 return cc_calc_ltgt_64(dst
, 0);
64 static uint32_t cc_calc_ltugtu_32(uint32_t src
, uint32_t dst
)
68 } else if (src
< dst
) {
75 static uint32_t cc_calc_ltugtu_64(uint64_t src
, uint64_t dst
)
79 } else if (src
< dst
) {
86 static uint32_t cc_calc_tm_32(uint32_t val
, uint32_t mask
)
88 uint32_t r
= val
& mask
;
92 } else if (r
== mask
) {
99 static uint32_t cc_calc_tm_64(uint64_t val
, uint64_t mask
)
101 uint64_t r
= val
& mask
;
105 } else if (r
== mask
) {
108 int top
= clz64(mask
);
109 if ((int64_t)(val
<< top
) < 0) {
117 static uint32_t cc_calc_nz(uint64_t dst
)
122 static uint32_t cc_calc_add_64(int64_t a1
, int64_t a2
, int64_t ar
)
124 if ((a1
> 0 && a2
> 0 && ar
< 0) || (a1
< 0 && a2
< 0 && ar
> 0)) {
125 return 3; /* overflow */
137 static uint32_t cc_calc_addu_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
139 return (ar
!= 0) + 2 * (ar
< a1
);
142 static uint32_t cc_calc_addc_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
144 /* Recover a2 + carry_in. */
145 uint64_t a2c
= ar
- a1
;
146 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
147 int carry_out
= (a2c
< a2
) || (ar
< a1
);
149 return (ar
!= 0) + 2 * carry_out
;
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_subu_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
180 static uint32_t cc_calc_subb_64(uint64_t a1
, uint64_t a2
, uint64_t ar
)
184 if (ar
!= a1
- a2
) { /* difference means borrow-in */
185 borrow_out
= (a2
>= a1
);
187 borrow_out
= (a2
> a1
);
190 return (ar
!= 0) + 2 * !borrow_out
;
193 static uint32_t cc_calc_abs_64(int64_t dst
)
195 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
204 static uint32_t cc_calc_nabs_64(int64_t dst
)
209 static uint32_t cc_calc_comp_64(int64_t dst
)
211 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
213 } else if (dst
< 0) {
215 } else if (dst
> 0) {
223 static uint32_t cc_calc_add_32(int32_t a1
, int32_t a2
, int32_t ar
)
225 if ((a1
> 0 && a2
> 0 && ar
< 0) || (a1
< 0 && a2
< 0 && ar
> 0)) {
226 return 3; /* overflow */
238 static uint32_t cc_calc_addu_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
240 return (ar
!= 0) + 2 * (ar
< a1
);
243 static uint32_t cc_calc_addc_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
245 /* Recover a2 + carry_in. */
246 uint32_t a2c
= ar
- a1
;
247 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
248 int carry_out
= (a2c
< a2
) || (ar
< a1
);
250 return (ar
!= 0) + 2 * carry_out
;
253 static uint32_t cc_calc_sub_32(int32_t a1
, int32_t a2
, int32_t ar
)
255 if ((a1
> 0 && a2
< 0 && ar
< 0) || (a1
< 0 && a2
> 0 && ar
> 0)) {
256 return 3; /* overflow */
268 static uint32_t cc_calc_subu_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
281 static uint32_t cc_calc_subb_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
285 if (ar
!= a1
- a2
) { /* difference means borrow-in */
286 borrow_out
= (a2
>= a1
);
288 borrow_out
= (a2
> a1
);
291 return (ar
!= 0) + 2 * !borrow_out
;
294 static uint32_t cc_calc_abs_32(int32_t dst
)
296 if ((uint32_t)dst
== 0x80000000UL
) {
305 static uint32_t cc_calc_nabs_32(int32_t dst
)
310 static uint32_t cc_calc_comp_32(int32_t dst
)
312 if ((uint32_t)dst
== 0x80000000UL
) {
314 } else if (dst
< 0) {
316 } else if (dst
> 0) {
323 /* calculate condition code for insert character under mask insn */
324 static uint32_t cc_calc_icm(uint64_t mask
, uint64_t val
)
326 if ((val
& mask
) == 0) {
329 int top
= clz64(mask
);
330 if ((int64_t)(val
<< top
) < 0) {
338 static uint32_t cc_calc_sla_32(uint32_t src
, int shift
)
340 uint32_t mask
= ((1U << shift
) - 1U) << (32 - shift
);
341 uint32_t sign
= 1U << 31;
345 /* Check if the sign bit stays the same. */
351 if ((src
& mask
) != match
) {
356 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
365 static uint32_t cc_calc_sla_64(uint64_t src
, int shift
)
367 uint64_t mask
= ((1ULL << shift
) - 1ULL) << (64 - shift
);
368 uint64_t sign
= 1ULL << 63;
372 /* Check if the sign bit stays the same. */
378 if ((src
& mask
) != match
) {
383 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
392 static uint32_t cc_calc_flogr(uint64_t dst
)
397 static uint32_t do_calc_cc(CPUS390XState
*env
, uint32_t cc_op
,
398 uint64_t src
, uint64_t dst
, uint64_t vr
)
400 S390CPU
*cpu
= s390_env_get_cpu(env
);
408 /* cc_op value _is_ cc */
412 r
= cc_calc_ltgt0_32(dst
);
415 r
= cc_calc_ltgt0_64(dst
);
418 r
= cc_calc_ltgt_32(src
, dst
);
421 r
= cc_calc_ltgt_64(src
, dst
);
423 case CC_OP_LTUGTU_32
:
424 r
= cc_calc_ltugtu_32(src
, dst
);
426 case CC_OP_LTUGTU_64
:
427 r
= cc_calc_ltugtu_64(src
, dst
);
430 r
= cc_calc_tm_32(src
, dst
);
433 r
= cc_calc_tm_64(src
, dst
);
439 r
= cc_calc_add_64(src
, dst
, vr
);
442 r
= cc_calc_addu_64(src
, dst
, vr
);
445 r
= cc_calc_addc_64(src
, dst
, vr
);
448 r
= cc_calc_sub_64(src
, dst
, vr
);
451 r
= cc_calc_subu_64(src
, dst
, vr
);
454 r
= cc_calc_subb_64(src
, dst
, vr
);
457 r
= cc_calc_abs_64(dst
);
460 r
= cc_calc_nabs_64(dst
);
463 r
= cc_calc_comp_64(dst
);
467 r
= cc_calc_add_32(src
, dst
, vr
);
470 r
= cc_calc_addu_32(src
, dst
, vr
);
473 r
= cc_calc_addc_32(src
, dst
, vr
);
476 r
= cc_calc_sub_32(src
, dst
, vr
);
479 r
= cc_calc_subu_32(src
, dst
, vr
);
482 r
= cc_calc_subb_32(src
, dst
, vr
);
485 r
= cc_calc_abs_32(dst
);
488 r
= cc_calc_nabs_32(dst
);
491 r
= cc_calc_comp_32(dst
);
495 r
= cc_calc_icm(src
, dst
);
498 r
= cc_calc_sla_32(src
, dst
);
501 r
= cc_calc_sla_64(src
, dst
);
504 r
= cc_calc_flogr(dst
);
508 r
= set_cc_nz_f32(dst
);
511 r
= set_cc_nz_f64(dst
);
514 r
= set_cc_nz_f128(make_float128(src
, dst
));
518 cpu_abort(CPU(cpu
), "Unknown CC operation: %s\n", cc_name(cc_op
));
521 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__
,
522 cc_name(cc_op
), src
, dst
, vr
, r
);
526 uint32_t calc_cc(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
, uint64_t dst
,
529 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
532 uint32_t HELPER(calc_cc
)(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
,
533 uint64_t dst
, uint64_t vr
)
535 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
538 #ifndef CONFIG_USER_ONLY
539 void HELPER(load_psw
)(CPUS390XState
*env
, uint64_t mask
, uint64_t addr
)
541 load_psw(env
, mask
, addr
);
542 cpu_loop_exit(CPU(s390_env_get_cpu(env
)));
545 void HELPER(sacf
)(CPUS390XState
*env
, uint64_t a1
)
547 HELPER_LOG("%s: %16" PRIx64
"\n", __func__
, a1
);
549 switch (a1
& 0xf00) {
551 env
->psw
.mask
&= ~PSW_MASK_ASC
;
552 env
->psw
.mask
|= PSW_ASC_PRIMARY
;
555 env
->psw
.mask
&= ~PSW_MASK_ASC
;
556 env
->psw
.mask
|= PSW_ASC_SECONDARY
;
559 env
->psw
.mask
&= ~PSW_MASK_ASC
;
560 env
->psw
.mask
|= PSW_ASC_HOME
;
563 HELPER_LOG("unknown sacf mode: %" PRIx64
"\n", a1
);
564 program_interrupt(env
, PGM_SPECIFICATION
, 2);