tests: smbios: fetch whole table in one step instead of reading it step by step
[qemu/ar7.git] / target / s390x / cc_helper.c
blob5d91e458a8a492b5c9d373e0ad5212f6bf2b119e
1 /*
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"
22 #include "cpu.h"
23 #include "internal.h"
24 #include "exec/exec-all.h"
25 #include "exec/helper-proto.h"
26 #include "qemu/host-utils.h"
28 /* #define DEBUG_HELPER */
29 #ifdef DEBUG_HELPER
30 #define HELPER_LOG(x...) qemu_log(x)
31 #else
32 #define HELPER_LOG(x...)
33 #endif
35 static uint32_t cc_calc_ltgt_32(int32_t src, int32_t dst)
37 if (src == dst) {
38 return 0;
39 } else if (src < dst) {
40 return 1;
41 } else {
42 return 2;
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)
53 if (src == dst) {
54 return 0;
55 } else if (src < dst) {
56 return 1;
57 } else {
58 return 2;
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)
69 if (src == dst) {
70 return 0;
71 } else if (src < dst) {
72 return 1;
73 } else {
74 return 2;
78 static uint32_t cc_calc_ltugtu_64(uint64_t src, uint64_t dst)
80 if (src == dst) {
81 return 0;
82 } else if (src < dst) {
83 return 1;
84 } else {
85 return 2;
89 static uint32_t cc_calc_tm_32(uint32_t val, uint32_t mask)
91 uint32_t r = val & mask;
93 if (r == 0) {
94 return 0;
95 } else if (r == mask) {
96 return 3;
97 } else {
98 return 1;
102 static uint32_t cc_calc_tm_64(uint64_t val, uint64_t mask)
104 uint64_t r = val & mask;
106 if (r == 0) {
107 return 0;
108 } else if (r == mask) {
109 return 3;
110 } else {
111 int top = clz64(mask);
112 if ((int64_t)(val << top) < 0) {
113 return 2;
114 } else {
115 return 1;
120 static uint32_t cc_calc_nz(uint64_t dst)
122 return !!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 */
129 } else {
130 if (ar < 0) {
131 return 1;
132 } else if (ar > 0) {
133 return 2;
134 } else {
135 return 0;
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 */
159 } else {
160 if (ar < 0) {
161 return 1;
162 } else if (ar > 0) {
163 return 2;
164 } else {
165 return 0;
170 static uint32_t cc_calc_subu_64(uint64_t a1, uint64_t a2, uint64_t ar)
172 if (ar == 0) {
173 return 2;
174 } else {
175 if (a2 > a1) {
176 return 1;
177 } else {
178 return 3;
183 static uint32_t cc_calc_subb_64(uint64_t a1, uint64_t a2, uint64_t ar)
185 int borrow_out;
187 if (ar != a1 - a2) { /* difference means borrow-in */
188 borrow_out = (a2 >= a1);
189 } else {
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) {
199 return 3;
200 } else if (dst) {
201 return 2;
202 } else {
203 return 0;
207 static uint32_t cc_calc_nabs_64(int64_t dst)
209 return !!dst;
212 static uint32_t cc_calc_comp_64(int64_t dst)
214 if ((uint64_t)dst == 0x8000000000000000ULL) {
215 return 3;
216 } else if (dst < 0) {
217 return 1;
218 } else if (dst > 0) {
219 return 2;
220 } else {
221 return 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 */
230 } else {
231 if (ar < 0) {
232 return 1;
233 } else if (ar > 0) {
234 return 2;
235 } else {
236 return 0;
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 */
260 } else {
261 if (ar < 0) {
262 return 1;
263 } else if (ar > 0) {
264 return 2;
265 } else {
266 return 0;
271 static uint32_t cc_calc_subu_32(uint32_t a1, uint32_t a2, uint32_t ar)
273 if (ar == 0) {
274 return 2;
275 } else {
276 if (a2 > a1) {
277 return 1;
278 } else {
279 return 3;
284 static uint32_t cc_calc_subb_32(uint32_t a1, uint32_t a2, uint32_t ar)
286 int borrow_out;
288 if (ar != a1 - a2) { /* difference means borrow-in */
289 borrow_out = (a2 >= a1);
290 } else {
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) {
300 return 3;
301 } else if (dst) {
302 return 2;
303 } else {
304 return 0;
308 static uint32_t cc_calc_nabs_32(int32_t dst)
310 return !!dst;
313 static uint32_t cc_calc_comp_32(int32_t dst)
315 if ((uint32_t)dst == 0x80000000UL) {
316 return 3;
317 } else if (dst < 0) {
318 return 1;
319 } else if (dst > 0) {
320 return 2;
321 } else {
322 return 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) {
330 return 0;
331 } else {
332 int top = clz64(mask);
333 if ((int64_t)(val << top) < 0) {
334 return 1;
335 } else {
336 return 2;
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;
345 uint32_t match;
346 int32_t r;
348 /* Check if the sign bit stays the same. */
349 if (src & sign) {
350 match = mask;
351 } else {
352 match = 0;
354 if ((src & mask) != match) {
355 /* Overflow. */
356 return 3;
359 r = ((src << shift) & ~sign) | (src & sign);
360 if (r == 0) {
361 return 0;
362 } else if (r < 0) {
363 return 1;
365 return 2;
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;
372 uint64_t match;
373 int64_t r;
375 /* Check if the sign bit stays the same. */
376 if (src & sign) {
377 match = mask;
378 } else {
379 match = 0;
381 if ((src & mask) != match) {
382 /* Overflow. */
383 return 3;
386 r = ((src << shift) & ~sign) | (src & sign);
387 if (r == 0) {
388 return 0;
389 } else if (r < 0) {
390 return 1;
392 return 2;
395 static uint32_t cc_calc_flogr(uint64_t dst)
397 return dst ? 2 : 0;
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);
404 uint32_t r = 0;
406 switch (cc_op) {
407 case CC_OP_CONST0:
408 case CC_OP_CONST1:
409 case CC_OP_CONST2:
410 case CC_OP_CONST3:
411 /* cc_op value _is_ cc */
412 r = cc_op;
413 break;
414 case CC_OP_LTGT0_32:
415 r = cc_calc_ltgt0_32(dst);
416 break;
417 case CC_OP_LTGT0_64:
418 r = cc_calc_ltgt0_64(dst);
419 break;
420 case CC_OP_LTGT_32:
421 r = cc_calc_ltgt_32(src, dst);
422 break;
423 case CC_OP_LTGT_64:
424 r = cc_calc_ltgt_64(src, dst);
425 break;
426 case CC_OP_LTUGTU_32:
427 r = cc_calc_ltugtu_32(src, dst);
428 break;
429 case CC_OP_LTUGTU_64:
430 r = cc_calc_ltugtu_64(src, dst);
431 break;
432 case CC_OP_TM_32:
433 r = cc_calc_tm_32(src, dst);
434 break;
435 case CC_OP_TM_64:
436 r = cc_calc_tm_64(src, dst);
437 break;
438 case CC_OP_NZ:
439 r = cc_calc_nz(dst);
440 break;
441 case CC_OP_ADD_64:
442 r = cc_calc_add_64(src, dst, vr);
443 break;
444 case CC_OP_ADDU_64:
445 r = cc_calc_addu_64(src, dst, vr);
446 break;
447 case CC_OP_ADDC_64:
448 r = cc_calc_addc_64(src, dst, vr);
449 break;
450 case CC_OP_SUB_64:
451 r = cc_calc_sub_64(src, dst, vr);
452 break;
453 case CC_OP_SUBU_64:
454 r = cc_calc_subu_64(src, dst, vr);
455 break;
456 case CC_OP_SUBB_64:
457 r = cc_calc_subb_64(src, dst, vr);
458 break;
459 case CC_OP_ABS_64:
460 r = cc_calc_abs_64(dst);
461 break;
462 case CC_OP_NABS_64:
463 r = cc_calc_nabs_64(dst);
464 break;
465 case CC_OP_COMP_64:
466 r = cc_calc_comp_64(dst);
467 break;
469 case CC_OP_ADD_32:
470 r = cc_calc_add_32(src, dst, vr);
471 break;
472 case CC_OP_ADDU_32:
473 r = cc_calc_addu_32(src, dst, vr);
474 break;
475 case CC_OP_ADDC_32:
476 r = cc_calc_addc_32(src, dst, vr);
477 break;
478 case CC_OP_SUB_32:
479 r = cc_calc_sub_32(src, dst, vr);
480 break;
481 case CC_OP_SUBU_32:
482 r = cc_calc_subu_32(src, dst, vr);
483 break;
484 case CC_OP_SUBB_32:
485 r = cc_calc_subb_32(src, dst, vr);
486 break;
487 case CC_OP_ABS_32:
488 r = cc_calc_abs_32(dst);
489 break;
490 case CC_OP_NABS_32:
491 r = cc_calc_nabs_32(dst);
492 break;
493 case CC_OP_COMP_32:
494 r = cc_calc_comp_32(dst);
495 break;
497 case CC_OP_ICM:
498 r = cc_calc_icm(src, dst);
499 break;
500 case CC_OP_SLA_32:
501 r = cc_calc_sla_32(src, dst);
502 break;
503 case CC_OP_SLA_64:
504 r = cc_calc_sla_64(src, dst);
505 break;
506 case CC_OP_FLOGR:
507 r = cc_calc_flogr(dst);
508 break;
510 case CC_OP_NZ_F32:
511 r = set_cc_nz_f32(dst);
512 break;
513 case CC_OP_NZ_F64:
514 r = set_cc_nz_f64(dst);
515 break;
516 case CC_OP_NZ_F128:
517 r = set_cc_nz_f128(make_float128(src, dst));
518 break;
520 default:
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);
526 return r;
529 uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
530 uint64_t vr)
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) {
553 case 0x000:
554 env->psw.mask &= ~PSW_MASK_ASC;
555 env->psw.mask |= PSW_ASC_PRIMARY;
556 break;
557 case 0x100:
558 env->psw.mask &= ~PSW_MASK_ASC;
559 env->psw.mask |= PSW_ASC_SECONDARY;
560 break;
561 case 0x300:
562 env->psw.mask &= ~PSW_MASK_ASC;
563 env->psw.mask |= PSW_ASC_HOME;
564 break;
565 default:
566 HELPER_LOG("unknown sacf mode: %" PRIx64 "\n", a1);
567 s390_program_interrupt(env, PGM_SPECIFICATION, 2, GETPC());
568 break;
571 #endif