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/>.
21 #include "qemu/osdep.h"
24 #include "exec/exec-all.h"
25 #include "exec/helper-proto.h"
26 #include "qemu/host-utils.h"
28 /* #define DEBUG_HELPER */
30 #define HELPER_LOG(x...) qemu_log(x)
32 #define HELPER_LOG(x...)
35 static uint32_t cc_calc_ltgt_32(int32_t src, int32_t dst)
39 } else if (src < dst) {
46 static uint32_t cc_calc_ltgt0_32(int32_t dst)
48 return cc_calc_ltgt_32(dst, 0);
51 static uint32_t cc_calc_ltgt_64(int64_t src, int64_t dst)
55 } else if (src < dst) {
62 static uint32_t cc_calc_ltgt0_64(int64_t dst)
64 return cc_calc_ltgt_64(dst, 0);
67 static uint32_t cc_calc_ltugtu_32(uint32_t src, uint32_t dst)
71 } else if (src < dst) {
78 static uint32_t cc_calc_ltugtu_64(uint64_t src, uint64_t dst)
82 } else if (src < dst) {
89 static uint32_t cc_calc_tm_32(uint32_t val, uint32_t mask)
91 uint32_t r = val & mask;
95 } else if (r == mask) {
102 static uint32_t cc_calc_tm_64(uint64_t val, uint64_t mask)
104 uint64_t r = val & mask;
108 } else if (r == mask) {
111 int top = clz64(mask);
112 if ((int64_t)(val << top) < 0) {
120 static uint32_t cc_calc_nz(uint64_t dst)
125 static uint32_t cc_calc_add_64(int64_t a1, int64_t a2, int64_t ar)
127 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
128 return 3; /* overflow */
140 static uint32_t cc_calc_addu_64(uint64_t a1, uint64_t a2, uint64_t ar)
142 return (ar != 0) + 2 * (ar < a1);
145 static uint32_t cc_calc_addc_64(uint64_t a1, uint64_t a2, uint64_t ar)
147 /* Recover a2 + carry_in. */
148 uint64_t a2c = ar - a1;
149 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
150 int carry_out = (a2c < a2) || (ar < a1);
152 return (ar != 0) + 2 * carry_out;
155 static uint32_t cc_calc_sub_64(int64_t a1, int64_t a2, int64_t ar)
157 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
158 return 3; /* overflow */
170 static uint32_t cc_calc_subu_64(uint64_t a1, uint64_t a2, uint64_t ar)
183 static uint32_t cc_calc_subb_64(uint64_t a1, uint64_t a2, uint64_t ar)
187 if (ar != a1 - a2) { /* difference means borrow-in */
188 borrow_out = (a2 >= a1);
190 borrow_out = (a2 > a1);
193 return (ar != 0) + 2 * !borrow_out;
196 static uint32_t cc_calc_abs_64(int64_t dst)
198 if ((uint64_t)dst == 0x8000000000000000ULL) {
207 static uint32_t cc_calc_nabs_64(int64_t dst)
212 static uint32_t cc_calc_comp_64(int64_t dst)
214 if ((uint64_t)dst == 0x8000000000000000ULL) {
216 } else if (dst < 0) {
218 } else if (dst > 0) {
226 static uint32_t cc_calc_add_32(int32_t a1, int32_t a2, int32_t ar)
228 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
229 return 3; /* overflow */
241 static uint32_t cc_calc_addu_32(uint32_t a1, uint32_t a2, uint32_t ar)
243 return (ar != 0) + 2 * (ar < a1);
246 static uint32_t cc_calc_addc_32(uint32_t a1, uint32_t a2, uint32_t ar)
248 /* Recover a2 + carry_in. */
249 uint32_t a2c = ar - a1;
250 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
251 int carry_out = (a2c < a2) || (ar < a1);
253 return (ar != 0) + 2 * carry_out;
256 static uint32_t cc_calc_sub_32(int32_t a1, int32_t a2, int32_t ar)
258 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
259 return 3; /* overflow */
271 static uint32_t cc_calc_subu_32(uint32_t a1, uint32_t a2, uint32_t ar)
284 static uint32_t cc_calc_subb_32(uint32_t a1, uint32_t a2, uint32_t ar)
288 if (ar != a1 - a2) { /* difference means borrow-in */
289 borrow_out = (a2 >= a1);
291 borrow_out = (a2 > a1);
294 return (ar != 0) + 2 * !borrow_out;
297 static uint32_t cc_calc_abs_32(int32_t dst)
299 if ((uint32_t)dst == 0x80000000UL) {
308 static uint32_t cc_calc_nabs_32(int32_t dst)
313 static uint32_t cc_calc_comp_32(int32_t dst)
315 if ((uint32_t)dst == 0x80000000UL) {
317 } else if (dst < 0) {
319 } else if (dst > 0) {
326 /* calculate condition code for insert character under mask insn */
327 static uint32_t cc_calc_icm(uint64_t mask, uint64_t val)
329 if ((val & mask) == 0) {
332 int top = clz64(mask);
333 if ((int64_t)(val << top) < 0) {
341 static uint32_t cc_calc_sla_32(uint32_t src, int shift)
343 uint32_t mask = ((1U << shift) - 1U) << (32 - shift);
344 uint32_t sign = 1U << 31;
348 /* Check if the sign bit stays the same. */
354 if ((src & mask) != match) {
359 r = ((src << shift) & ~sign) | (src & sign);
368 static uint32_t cc_calc_sla_64(uint64_t src, int shift)
370 uint64_t mask = ((1ULL << shift) - 1ULL) << (64 - shift);
371 uint64_t sign = 1ULL << 63;
375 /* Check if the sign bit stays the same. */
381 if ((src & mask) != match) {
386 r = ((src << shift) & ~sign) | (src & sign);
395 static uint32_t cc_calc_flogr(uint64_t dst)
400 static uint32_t do_calc_cc(CPUS390XState *env, uint32_t cc_op,
401 uint64_t src, uint64_t dst, uint64_t vr)
403 S390CPU *cpu = s390_env_get_cpu(env);
411 /* cc_op value _is_ cc */
415 r = cc_calc_ltgt0_32(dst);
418 r = cc_calc_ltgt0_64(dst);
421 r = cc_calc_ltgt_32(src, dst);
424 r = cc_calc_ltgt_64(src, dst);
426 case CC_OP_LTUGTU_32:
427 r = cc_calc_ltugtu_32(src, dst);
429 case CC_OP_LTUGTU_64:
430 r = cc_calc_ltugtu_64(src, dst);
433 r = cc_calc_tm_32(src, dst);
436 r = cc_calc_tm_64(src, dst);
442 r = cc_calc_add_64(src, dst, vr);
445 r = cc_calc_addu_64(src, dst, vr);
448 r = cc_calc_addc_64(src, dst, vr);
451 r = cc_calc_sub_64(src, dst, vr);
454 r = cc_calc_subu_64(src, dst, vr);
457 r = cc_calc_subb_64(src, dst, vr);
460 r = cc_calc_abs_64(dst);
463 r = cc_calc_nabs_64(dst);
466 r = cc_calc_comp_64(dst);
470 r = cc_calc_add_32(src, dst, vr);
473 r = cc_calc_addu_32(src, dst, vr);
476 r = cc_calc_addc_32(src, dst, vr);
479 r = cc_calc_sub_32(src, dst, vr);
482 r = cc_calc_subu_32(src, dst, vr);
485 r = cc_calc_subb_32(src, dst, vr);
488 r = cc_calc_abs_32(dst);
491 r = cc_calc_nabs_32(dst);
494 r = cc_calc_comp_32(dst);
498 r = cc_calc_icm(src, dst);
501 r = cc_calc_sla_32(src, dst);
504 r = cc_calc_sla_64(src, dst);
507 r = cc_calc_flogr(dst);
511 r = set_cc_nz_f32(dst);
514 r = set_cc_nz_f64(dst);
517 r = set_cc_nz_f128(make_float128(src, dst));
521 cpu_abort(CPU(cpu), "Unknown CC operation: %s\n", cc_name(cc_op));
524 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__,
525 cc_name(cc_op), src, dst, vr, r);
529 uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
532 return do_calc_cc(env, cc_op, src, dst, vr);
535 uint32_t HELPER(calc_cc)(CPUS390XState *env, uint32_t cc_op, uint64_t src,
536 uint64_t dst, uint64_t vr)
538 return do_calc_cc(env, cc_op, src, dst, vr);
541 #ifndef CONFIG_USER_ONLY
542 void HELPER(load_psw)(CPUS390XState *env, uint64_t mask, uint64_t addr)
544 load_psw(env, mask, addr);
545 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
548 void HELPER(sacf)(CPUS390XState *env, uint64_t a1)
550 HELPER_LOG("%s: %16" PRIx64 "\n", __func__, a1);
552 switch (a1 & 0xf00) {
554 env->psw.mask &= ~PSW_MASK_ASC;
555 env->psw.mask |= PSW_ASC_PRIMARY;
558 env->psw.mask &= ~PSW_MASK_ASC;
559 env->psw.mask |= PSW_ASC_SECONDARY;
562 env->psw.mask &= ~PSW_MASK_ASC;
563 env->psw.mask |= PSW_ASC_HOME;
566 HELPER_LOG("unknown sacf mode: %" PRIx64 "\n", a1);
567 s390_program_interrupt(env, PGM_SPECIFICATION, 2, GETPC());