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
)
182 /* We had borrow-in if normal subtraction isn't equal. */
183 int borrow_in
= ar
- (a1
- a2
);
186 /* If a2 was ULONG_MAX, and borrow_in, then a2 is logically 65 bits,
187 and we must have had borrow out. */
188 if (borrow_in
&& a2
== (uint64_t)-1) {
192 borrow_out
= (a2
> a1
);
195 return (ar
!= 0) + 2 * !borrow_out
;
198 static uint32_t cc_calc_abs_64(int64_t dst
)
200 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
209 static uint32_t cc_calc_nabs_64(int64_t dst
)
214 static uint32_t cc_calc_comp_64(int64_t dst
)
216 if ((uint64_t)dst
== 0x8000000000000000ULL
) {
218 } else if (dst
< 0) {
220 } else if (dst
> 0) {
228 static uint32_t cc_calc_add_32(int32_t a1
, int32_t a2
, int32_t ar
)
230 if ((a1
> 0 && a2
> 0 && ar
< 0) || (a1
< 0 && a2
< 0 && ar
> 0)) {
231 return 3; /* overflow */
243 static uint32_t cc_calc_addu_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
245 return (ar
!= 0) + 2 * (ar
< a1
);
248 static uint32_t cc_calc_addc_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
250 /* Recover a2 + carry_in. */
251 uint32_t a2c
= ar
- a1
;
252 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
253 int carry_out
= (a2c
< a2
) || (ar
< a1
);
255 return (ar
!= 0) + 2 * carry_out
;
258 static uint32_t cc_calc_sub_32(int32_t a1
, int32_t a2
, int32_t ar
)
260 if ((a1
> 0 && a2
< 0 && ar
< 0) || (a1
< 0 && a2
> 0 && ar
> 0)) {
261 return 3; /* overflow */
273 static uint32_t cc_calc_subu_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
286 static uint32_t cc_calc_subb_32(uint32_t a1
, uint32_t a2
, uint32_t ar
)
288 /* We had borrow-in if normal subtraction isn't equal. */
289 int borrow_in
= ar
- (a1
- a2
);
292 /* If a2 was UINT_MAX, and borrow_in, then a2 is logically 65 bits,
293 and we must have had borrow out. */
294 if (borrow_in
&& a2
== (uint32_t)-1) {
298 borrow_out
= (a2
> a1
);
301 return (ar
!= 0) + 2 * !borrow_out
;
304 static uint32_t cc_calc_abs_32(int32_t dst
)
306 if ((uint32_t)dst
== 0x80000000UL
) {
315 static uint32_t cc_calc_nabs_32(int32_t dst
)
320 static uint32_t cc_calc_comp_32(int32_t dst
)
322 if ((uint32_t)dst
== 0x80000000UL
) {
324 } else if (dst
< 0) {
326 } else if (dst
> 0) {
333 /* calculate condition code for insert character under mask insn */
334 static uint32_t cc_calc_icm(uint64_t mask
, uint64_t val
)
336 if ((val
& mask
) == 0) {
339 int top
= clz64(mask
);
340 if ((int64_t)(val
<< top
) < 0) {
348 static uint32_t cc_calc_sla_32(uint32_t src
, int shift
)
350 uint32_t mask
= ((1U << shift
) - 1U) << (32 - shift
);
351 uint32_t sign
= 1U << 31;
355 /* Check if the sign bit stays the same. */
361 if ((src
& mask
) != match
) {
366 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
375 static uint32_t cc_calc_sla_64(uint64_t src
, int shift
)
377 uint64_t mask
= ((1ULL << shift
) - 1ULL) << (64 - shift
);
378 uint64_t sign
= 1ULL << 63;
382 /* Check if the sign bit stays the same. */
388 if ((src
& mask
) != match
) {
393 r
= ((src
<< shift
) & ~sign
) | (src
& sign
);
402 static uint32_t cc_calc_flogr(uint64_t dst
)
407 static uint32_t do_calc_cc(CPUS390XState
*env
, uint32_t cc_op
,
408 uint64_t src
, uint64_t dst
, uint64_t vr
)
410 S390CPU
*cpu
= s390_env_get_cpu(env
);
418 /* cc_op value _is_ cc */
422 r
= cc_calc_ltgt0_32(dst
);
425 r
= cc_calc_ltgt0_64(dst
);
428 r
= cc_calc_ltgt_32(src
, dst
);
431 r
= cc_calc_ltgt_64(src
, dst
);
433 case CC_OP_LTUGTU_32
:
434 r
= cc_calc_ltugtu_32(src
, dst
);
436 case CC_OP_LTUGTU_64
:
437 r
= cc_calc_ltugtu_64(src
, dst
);
440 r
= cc_calc_tm_32(src
, dst
);
443 r
= cc_calc_tm_64(src
, dst
);
449 r
= cc_calc_add_64(src
, dst
, vr
);
452 r
= cc_calc_addu_64(src
, dst
, vr
);
455 r
= cc_calc_addc_64(src
, dst
, vr
);
458 r
= cc_calc_sub_64(src
, dst
, vr
);
461 r
= cc_calc_subu_64(src
, dst
, vr
);
464 r
= cc_calc_subb_64(src
, dst
, vr
);
467 r
= cc_calc_abs_64(dst
);
470 r
= cc_calc_nabs_64(dst
);
473 r
= cc_calc_comp_64(dst
);
477 r
= cc_calc_add_32(src
, dst
, vr
);
480 r
= cc_calc_addu_32(src
, dst
, vr
);
483 r
= cc_calc_addc_32(src
, dst
, vr
);
486 r
= cc_calc_sub_32(src
, dst
, vr
);
489 r
= cc_calc_subu_32(src
, dst
, vr
);
492 r
= cc_calc_subb_32(src
, dst
, vr
);
495 r
= cc_calc_abs_32(dst
);
498 r
= cc_calc_nabs_32(dst
);
501 r
= cc_calc_comp_32(dst
);
505 r
= cc_calc_icm(src
, dst
);
508 r
= cc_calc_sla_32(src
, dst
);
511 r
= cc_calc_sla_64(src
, dst
);
514 r
= cc_calc_flogr(dst
);
518 r
= set_cc_nz_f32(dst
);
521 r
= set_cc_nz_f64(dst
);
524 r
= set_cc_nz_f128(make_float128(src
, dst
));
528 cpu_abort(CPU(cpu
), "Unknown CC operation: %s\n", cc_name(cc_op
));
531 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__
,
532 cc_name(cc_op
), src
, dst
, vr
, r
);
536 uint32_t calc_cc(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
, uint64_t dst
,
539 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
542 uint32_t HELPER(calc_cc
)(CPUS390XState
*env
, uint32_t cc_op
, uint64_t src
,
543 uint64_t dst
, uint64_t vr
)
545 return do_calc_cc(env
, cc_op
, src
, dst
, vr
);
548 #ifndef CONFIG_USER_ONLY
549 void HELPER(load_psw
)(CPUS390XState
*env
, uint64_t mask
, uint64_t addr
)
551 load_psw(env
, mask
, addr
);
552 cpu_loop_exit(CPU(s390_env_get_cpu(env
)));
555 void HELPER(sacf
)(CPUS390XState
*env
, uint64_t a1
)
557 HELPER_LOG("%s: %16" PRIx64
"\n", __func__
, a1
);
559 switch (a1
& 0xf00) {
561 env
->psw
.mask
&= ~PSW_MASK_ASC
;
562 env
->psw
.mask
|= PSW_ASC_PRIMARY
;
565 env
->psw
.mask
&= ~PSW_MASK_ASC
;
566 env
->psw
.mask
|= PSW_ASC_SECONDARY
;
569 env
->psw
.mask
&= ~PSW_MASK_ASC
;
570 env
->psw
.mask
|= PSW_ASC_HOME
;
573 qemu_log("unknown sacf mode: %" PRIx64
"\n", a1
);
574 program_interrupt(env
, PGM_SPECIFICATION
, 2);