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"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
25 #include "qemu/host-utils.h"
27 /* #define DEBUG_HELPER */
29 #define HELPER_LOG(x...) qemu_log(x)
31 #define HELPER_LOG(x...)
34 static uint32_t cc_calc_ltgt_32(int32_t src, int32_t dst)
38 } else if (src < dst) {
45 static uint32_t cc_calc_ltgt0_32(int32_t dst)
47 return cc_calc_ltgt_32(dst, 0);
50 static uint32_t cc_calc_ltgt_64(int64_t src, int64_t dst)
54 } else if (src < dst) {
61 static uint32_t cc_calc_ltgt0_64(int64_t dst)
63 return cc_calc_ltgt_64(dst, 0);
66 static uint32_t cc_calc_ltugtu_32(uint32_t src, uint32_t dst)
70 } else if (src < dst) {
77 static uint32_t cc_calc_ltugtu_64(uint64_t src, uint64_t dst)
81 } else if (src < dst) {
88 static uint32_t cc_calc_tm_32(uint32_t val, uint32_t mask)
90 uint32_t r = val & mask;
94 } else if (r == mask) {
101 static uint32_t cc_calc_tm_64(uint64_t val, uint64_t mask)
103 uint64_t r = val & mask;
107 } else if (r == mask) {
110 int top = clz64(mask);
111 if ((int64_t)(val << top) < 0) {
119 static uint32_t cc_calc_nz(uint64_t dst)
124 static uint32_t cc_calc_add_64(int64_t a1, int64_t a2, int64_t ar)
126 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
127 return 3; /* overflow */
139 static uint32_t cc_calc_addu_64(uint64_t a1, uint64_t a2, uint64_t ar)
141 return (ar != 0) + 2 * (ar < a1);
144 static uint32_t cc_calc_addc_64(uint64_t a1, uint64_t a2, uint64_t ar)
146 /* Recover a2 + carry_in. */
147 uint64_t a2c = ar - a1;
148 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
149 int carry_out = (a2c < a2) || (ar < a1);
151 return (ar != 0) + 2 * carry_out;
154 static uint32_t cc_calc_sub_64(int64_t a1, int64_t a2, int64_t ar)
156 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
157 return 3; /* overflow */
169 static uint32_t cc_calc_subu_64(uint64_t a1, uint64_t a2, uint64_t ar)
182 static uint32_t cc_calc_subb_64(uint64_t a1, uint64_t a2, uint64_t ar)
186 if (ar != a1 - a2) { /* difference means borrow-in */
187 borrow_out = (a2 >= a1);
189 borrow_out = (a2 > a1);
192 return (ar != 0) + 2 * !borrow_out;
195 static uint32_t cc_calc_abs_64(int64_t dst)
197 if ((uint64_t)dst == 0x8000000000000000ULL) {
206 static uint32_t cc_calc_nabs_64(int64_t dst)
211 static uint32_t cc_calc_comp_64(int64_t dst)
213 if ((uint64_t)dst == 0x8000000000000000ULL) {
215 } else if (dst < 0) {
217 } else if (dst > 0) {
225 static uint32_t cc_calc_add_32(int32_t a1, int32_t a2, int32_t ar)
227 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
228 return 3; /* overflow */
240 static uint32_t cc_calc_addu_32(uint32_t a1, uint32_t a2, uint32_t ar)
242 return (ar != 0) + 2 * (ar < a1);
245 static uint32_t cc_calc_addc_32(uint32_t a1, uint32_t a2, uint32_t ar)
247 /* Recover a2 + carry_in. */
248 uint32_t a2c = ar - a1;
249 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
250 int carry_out = (a2c < a2) || (ar < a1);
252 return (ar != 0) + 2 * carry_out;
255 static uint32_t cc_calc_sub_32(int32_t a1, int32_t a2, int32_t ar)
257 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
258 return 3; /* overflow */
270 static uint32_t cc_calc_subu_32(uint32_t a1, uint32_t a2, uint32_t ar)
283 static uint32_t cc_calc_subb_32(uint32_t a1, uint32_t a2, uint32_t ar)
287 if (ar != a1 - a2) { /* difference means borrow-in */
288 borrow_out = (a2 >= a1);
290 borrow_out = (a2 > a1);
293 return (ar != 0) + 2 * !borrow_out;
296 static uint32_t cc_calc_abs_32(int32_t dst)
298 if ((uint32_t)dst == 0x80000000UL) {
307 static uint32_t cc_calc_nabs_32(int32_t dst)
312 static uint32_t cc_calc_comp_32(int32_t dst)
314 if ((uint32_t)dst == 0x80000000UL) {
316 } else if (dst < 0) {
318 } else if (dst > 0) {
325 /* calculate condition code for insert character under mask insn */
326 static uint32_t cc_calc_icm(uint64_t mask, uint64_t val)
328 if ((val & mask) == 0) {
331 int top = clz64(mask);
332 if ((int64_t)(val << top) < 0) {
340 static uint32_t cc_calc_sla_32(uint32_t src, int shift)
342 uint32_t mask = ((1U << shift) - 1U) << (32 - shift);
343 uint32_t sign = 1U << 31;
347 /* Check if the sign bit stays the same. */
353 if ((src & mask) != match) {
358 r = ((src << shift) & ~sign) | (src & sign);
367 static uint32_t cc_calc_sla_64(uint64_t src, int shift)
369 uint64_t mask = ((1ULL << shift) - 1ULL) << (64 - shift);
370 uint64_t sign = 1ULL << 63;
374 /* Check if the sign bit stays the same. */
380 if ((src & mask) != match) {
385 r = ((src << shift) & ~sign) | (src & sign);
394 static uint32_t cc_calc_flogr(uint64_t dst)
399 static uint32_t do_calc_cc(CPUS390XState *env, uint32_t cc_op,
400 uint64_t src, uint64_t dst, uint64_t vr)
402 S390CPU *cpu = s390_env_get_cpu(env);
410 /* cc_op value _is_ cc */
414 r = cc_calc_ltgt0_32(dst);
417 r = cc_calc_ltgt0_64(dst);
420 r = cc_calc_ltgt_32(src, dst);
423 r = cc_calc_ltgt_64(src, dst);
425 case CC_OP_LTUGTU_32:
426 r = cc_calc_ltugtu_32(src, dst);
428 case CC_OP_LTUGTU_64:
429 r = cc_calc_ltugtu_64(src, dst);
432 r = cc_calc_tm_32(src, dst);
435 r = cc_calc_tm_64(src, dst);
441 r = cc_calc_add_64(src, dst, vr);
444 r = cc_calc_addu_64(src, dst, vr);
447 r = cc_calc_addc_64(src, dst, vr);
450 r = cc_calc_sub_64(src, dst, vr);
453 r = cc_calc_subu_64(src, dst, vr);
456 r = cc_calc_subb_64(src, dst, vr);
459 r = cc_calc_abs_64(dst);
462 r = cc_calc_nabs_64(dst);
465 r = cc_calc_comp_64(dst);
469 r = cc_calc_add_32(src, dst, vr);
472 r = cc_calc_addu_32(src, dst, vr);
475 r = cc_calc_addc_32(src, dst, vr);
478 r = cc_calc_sub_32(src, dst, vr);
481 r = cc_calc_subu_32(src, dst, vr);
484 r = cc_calc_subb_32(src, dst, vr);
487 r = cc_calc_abs_32(dst);
490 r = cc_calc_nabs_32(dst);
493 r = cc_calc_comp_32(dst);
497 r = cc_calc_icm(src, dst);
500 r = cc_calc_sla_32(src, dst);
503 r = cc_calc_sla_64(src, dst);
506 r = cc_calc_flogr(dst);
510 r = set_cc_nz_f32(dst);
513 r = set_cc_nz_f64(dst);
516 r = set_cc_nz_f128(make_float128(src, dst));
520 cpu_abort(CPU(cpu), "Unknown CC operation: %s\n", cc_name(cc_op));
523 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__,
524 cc_name(cc_op), src, dst, vr, r);
528 uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
531 return do_calc_cc(env, cc_op, src, dst, vr);
534 uint32_t HELPER(calc_cc)(CPUS390XState *env, uint32_t cc_op, uint64_t src,
535 uint64_t dst, uint64_t vr)
537 return do_calc_cc(env, cc_op, src, dst, vr);
540 #ifndef CONFIG_USER_ONLY
541 void HELPER(load_psw)(CPUS390XState *env, uint64_t mask, uint64_t addr)
543 load_psw(env, mask, addr);
544 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
547 void HELPER(sacf)(CPUS390XState *env, uint64_t a1)
549 HELPER_LOG("%s: %16" PRIx64 "\n", __func__, a1);
551 switch (a1 & 0xf00) {
553 env->psw.mask &= ~PSW_MASK_ASC;
554 env->psw.mask |= PSW_ASC_PRIMARY;
557 env->psw.mask &= ~PSW_MASK_ASC;
558 env->psw.mask |= PSW_ASC_SECONDARY;
561 env->psw.mask &= ~PSW_MASK_ASC;
562 env->psw.mask |= PSW_ASC_HOME;
565 HELPER_LOG("unknown sacf mode: %" PRIx64 "\n", a1);
566 program_interrupt(env, PGM_SPECIFICATION, 2);