2 * Alpha emulation cpu micro-operations helpers for qemu.
4 * Copyright (c) 2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "host-utils.h"
23 #include "softfloat.h"
25 #include "op_helper.h"
27 #define MEMSUFFIX _raw
28 #include "op_helper_mem.h"
30 #if !defined(CONFIG_USER_ONLY)
31 #define MEMSUFFIX _kernel
32 #include "op_helper_mem.h"
34 #define MEMSUFFIX _executive
35 #include "op_helper_mem.h"
37 #define MEMSUFFIX _supervisor
38 #include "op_helper_mem.h"
40 #define MEMSUFFIX _user
41 #include "op_helper_mem.h"
43 /* This is used for pal modes */
44 #define MEMSUFFIX _data
45 #include "op_helper_mem.h"
48 void helper_tb_flush (void)
53 void cpu_dump_EA (target_ulong EA
);
54 void helper_print_mem_EA (target_ulong EA
)
59 /*****************************************************************************/
60 /* Exceptions processing helpers */
61 void helper_excp (uint32_t excp
, uint32_t error
)
63 env
->exception_index
= excp
;
64 env
->error_code
= error
;
68 void helper_amask (void)
70 switch (env
->implver
) {
72 /* EV4, EV45, LCA, LCA45 & EV5 */
82 void helper_load_pcc (void)
88 void helper_load_implver (void)
93 void helper_load_fpcr (void)
96 #ifdef CONFIG_SOFTFLOAT
97 T0
|= env
->fp_status
.float_exception_flags
<< 52;
98 if (env
->fp_status
.float_exception_flags
)
100 env
->ipr
[IPR_EXC_SUM
] &= ~0x3E:
101 env
->ipr
[IPR_EXC_SUM
] |= env
->fp_status
.float_exception_flags
<< 1;
103 switch (env
->fp_status
.float_rounding_mode
) {
104 case float_round_nearest_even
:
107 case float_round_down
:
113 case float_round_to_zero
:
118 void helper_store_fpcr (void)
120 #ifdef CONFIG_SOFTFLOAT
121 set_float_exception_flags((T0
>> 52) & 0x3F, &FP_STATUS
);
123 switch ((T0
>> 58) & 3) {
125 set_float_rounding_mode(float_round_to_zero
, &FP_STATUS
);
128 set_float_rounding_mode(float_round_down
, &FP_STATUS
);
131 set_float_rounding_mode(float_round_nearest_even
, &FP_STATUS
);
134 set_float_rounding_mode(float_round_up
, &FP_STATUS
);
139 void helper_load_irf (void)
145 void helper_set_irf (void)
150 void helper_clear_irf (void)
155 void helper_addqv (void)
159 if (unlikely((T2
^ T1
^ (-1ULL)) & (T2
^ T0
) & (1ULL << 63))) {
160 helper_excp(EXCP_ARITH
, EXCP_ARITH_OVERFLOW
);
164 void helper_addlv (void)
167 T0
= (uint32_t)(T0
+ T1
);
168 if (unlikely((T2
^ T1
^ (-1UL)) & (T2
^ T0
) & (1UL << 31))) {
169 helper_excp(EXCP_ARITH
, EXCP_ARITH_OVERFLOW
);
173 void helper_subqv (void)
177 if (unlikely(((~T2
) ^ T0
^ (-1ULL)) & ((~T2
) ^ T1
) & (1ULL << 63))) {
178 helper_excp(EXCP_ARITH
, EXCP_ARITH_OVERFLOW
);
182 void helper_sublv (void)
185 T0
= (uint32_t)(T0
- T1
);
186 if (unlikely(((~T2
) ^ T0
^ (-1UL)) & ((~T2
) ^ T1
) & (1UL << 31))) {
187 helper_excp(EXCP_ARITH
, EXCP_ARITH_OVERFLOW
);
191 void helper_mullv (void)
193 int64_t res
= (int64_t)T0
* (int64_t)T1
;
195 if (unlikely((int32_t)res
!= res
)) {
196 helper_excp(EXCP_ARITH
, EXCP_ARITH_OVERFLOW
);
198 T0
= (int64_t)((int32_t)res
);
205 muls64(&tl
, &th
, T0
, T1
);
206 /* If th != 0 && th != -1, then we had an overflow */
207 if (unlikely((th
+ 1) > 1)) {
208 helper_excp(EXCP_ARITH
, EXCP_ARITH_OVERFLOW
);
213 void helper_ctpop (void)
218 void helper_ctlz (void)
223 void helper_cttz (void)
228 static always_inline
uint64_t byte_zap (uint64_t op
, uint8_t mskb
)
233 mask
|= ((mskb
>> 0) & 1) * 0x00000000000000FFULL
;
234 mask
|= ((mskb
>> 1) & 1) * 0x000000000000FF00ULL
;
235 mask
|= ((mskb
>> 2) & 1) * 0x0000000000FF0000ULL
;
236 mask
|= ((mskb
>> 3) & 1) * 0x00000000FF000000ULL
;
237 mask
|= ((mskb
>> 4) & 1) * 0x000000FF00000000ULL
;
238 mask
|= ((mskb
>> 5) & 1) * 0x0000FF0000000000ULL
;
239 mask
|= ((mskb
>> 6) & 1) * 0x00FF000000000000ULL
;
240 mask
|= ((mskb
>> 7) & 1) * 0xFF00000000000000ULL
;
245 void helper_mskbl (void)
247 T0
= byte_zap(T0
, 0x01 << (T1
& 7));
250 void helper_extbl (void)
253 T0
= byte_zap(T0
, 0xFE);
256 void helper_insbl (void)
259 T0
= byte_zap(T0
, ~(0x01 << (T1
& 7)));
262 void helper_mskwl (void)
264 T0
= byte_zap(T0
, 0x03 << (T1
& 7));
267 void helper_extwl (void)
270 T0
= byte_zap(T0
, 0xFC);
273 void helper_inswl (void)
276 T0
= byte_zap(T0
, ~(0x03 << (T1
& 7)));
279 void helper_mskll (void)
281 T0
= byte_zap(T0
, 0x0F << (T1
& 7));
284 void helper_extll (void)
287 T0
= byte_zap(T0
, 0xF0);
290 void helper_insll (void)
293 T0
= byte_zap(T0
, ~(0x0F << (T1
& 7)));
296 void helper_zap (void)
298 T0
= byte_zap(T0
, T1
);
301 void helper_zapnot (void)
303 T0
= byte_zap(T0
, ~T1
);
306 void helper_mskql (void)
308 T0
= byte_zap(T0
, 0xFF << (T1
& 7));
311 void helper_extql (void)
314 T0
= byte_zap(T0
, 0x00);
317 void helper_insql (void)
320 T0
= byte_zap(T0
, ~(0xFF << (T1
& 7)));
323 void helper_mskwh (void)
325 T0
= byte_zap(T0
, (0x03 << (T1
& 7)) >> 8);
328 void helper_inswh (void)
330 T0
>>= 64 - ((T1
& 7) * 8);
331 T0
= byte_zap(T0
, ~((0x03 << (T1
& 7)) >> 8));
334 void helper_extwh (void)
336 T0
<<= 64 - ((T1
& 7) * 8);
337 T0
= byte_zap(T0
, ~0x07);
340 void helper_msklh (void)
342 T0
= byte_zap(T0
, (0x0F << (T1
& 7)) >> 8);
345 void helper_inslh (void)
347 T0
>>= 64 - ((T1
& 7) * 8);
348 T0
= byte_zap(T0
, ~((0x0F << (T1
& 7)) >> 8));
351 void helper_extlh (void)
353 T0
<<= 64 - ((T1
& 7) * 8);
354 T0
= byte_zap(T0
, ~0x0F);
357 void helper_mskqh (void)
359 T0
= byte_zap(T0
, (0xFF << (T1
& 7)) >> 8);
362 void helper_insqh (void)
364 T0
>>= 64 - ((T1
& 7) * 8);
365 T0
= byte_zap(T0
, ~((0xFF << (T1
& 7)) >> 8));
368 void helper_extqh (void)
370 T0
<<= 64 - ((T1
& 7) * 8);
371 T0
= byte_zap(T0
, 0x00);
374 void helper_cmpbge (void)
376 uint8_t opa
, opb
, res
;
380 for (i
= 0; i
< 7; i
++) {
389 void helper_cmov_fir (int freg
)
392 env
->fir
[freg
] = FT1
;
395 void helper_sqrts (void)
397 FT0
= float32_sqrt(FT0
, &FP_STATUS
);
400 void helper_cpys (void)
409 r
.i
= p
.i
& 0x8000000000000000ULL
;
410 r
.i
|= q
.i
& ~0x8000000000000000ULL
;
414 void helper_cpysn (void)
423 r
.i
= (~p
.i
) & 0x8000000000000000ULL
;
424 r
.i
|= q
.i
& ~0x8000000000000000ULL
;
428 void helper_cpyse (void)
437 r
.i
= p
.i
& 0xFFF0000000000000ULL
;
438 r
.i
|= q
.i
& ~0xFFF0000000000000ULL
;
442 void helper_itofs (void)
450 FT0
= int64_to_float32(p
.i
, &FP_STATUS
);
453 void helper_ftois (void)
460 p
.i
= float32_to_int64(FT0
, &FP_STATUS
);
464 void helper_sqrtt (void)
466 FT0
= float64_sqrt(FT0
, &FP_STATUS
);
469 void helper_cmptun (void)
477 if (float64_is_nan(FT0
) || float64_is_nan(FT1
))
478 p
.i
= 0x4000000000000000ULL
;
482 void helper_cmpteq (void)
490 if (float64_eq(FT0
, FT1
, &FP_STATUS
))
491 p
.i
= 0x4000000000000000ULL
;
495 void helper_cmptle (void)
503 if (float64_le(FT0
, FT1
, &FP_STATUS
))
504 p
.i
= 0x4000000000000000ULL
;
508 void helper_cmptlt (void)
516 if (float64_lt(FT0
, FT1
, &FP_STATUS
))
517 p
.i
= 0x4000000000000000ULL
;
521 void helper_itoft (void)
529 FT0
= int64_to_float64(p
.i
, &FP_STATUS
);
532 void helper_ftoit (void)
539 p
.i
= float64_to_int64(FT0
, &FP_STATUS
);
543 static always_inline
int vaxf_is_valid (float ff
)
552 exp
= (p
.i
>> 23) & 0xFF;
553 mant
= p
.i
& 0x007FFFFF;
554 if (exp
== 0 && ((p
.i
& 0x80000000) || mant
!= 0)) {
555 /* Reserved operands / Dirty zero */
562 static always_inline
float vaxf_to_ieee32 (float ff
)
571 exp
= (p
.i
>> 23) & 0xFF;
582 static always_inline
float ieee32_to_vaxf (float fi
)
591 exp
= (p
.i
>> 23) & 0xFF;
592 mant
= p
.i
& 0x007FFFFF;
594 /* NaN or infinity */
596 } else if (exp
== 0) {
616 void helper_addf (void)
620 if (!vaxf_is_valid(FT0
) || !vaxf_is_valid(FT1
)) {
623 ft0
= vaxf_to_ieee32(FT0
);
624 ft1
= vaxf_to_ieee32(FT1
);
625 ft2
= float32_add(ft0
, ft1
, &FP_STATUS
);
626 FT0
= ieee32_to_vaxf(ft2
);
629 void helper_subf (void)
633 if (!vaxf_is_valid(FT0
) || !vaxf_is_valid(FT1
)) {
636 ft0
= vaxf_to_ieee32(FT0
);
637 ft1
= vaxf_to_ieee32(FT1
);
638 ft2
= float32_sub(ft0
, ft1
, &FP_STATUS
);
639 FT0
= ieee32_to_vaxf(ft2
);
642 void helper_mulf (void)
646 if (!vaxf_is_valid(FT0
) || !vaxf_is_valid(FT1
)) {
649 ft0
= vaxf_to_ieee32(FT0
);
650 ft1
= vaxf_to_ieee32(FT1
);
651 ft2
= float32_mul(ft0
, ft1
, &FP_STATUS
);
652 FT0
= ieee32_to_vaxf(ft2
);
655 void helper_divf (void)
659 if (!vaxf_is_valid(FT0
) || !vaxf_is_valid(FT1
)) {
662 ft0
= vaxf_to_ieee32(FT0
);
663 ft1
= vaxf_to_ieee32(FT1
);
664 ft2
= float32_div(ft0
, ft1
, &FP_STATUS
);
665 FT0
= ieee32_to_vaxf(ft2
);
668 void helper_sqrtf (void)
672 if (!vaxf_is_valid(FT0
) || !vaxf_is_valid(FT1
)) {
675 ft0
= vaxf_to_ieee32(FT0
);
676 ft1
= float32_sqrt(ft0
, &FP_STATUS
);
677 FT0
= ieee32_to_vaxf(ft1
);
680 void helper_itoff (void)
685 static always_inline
int vaxg_is_valid (double ff
)
694 exp
= (p
.i
>> 52) & 0x7FF;
695 mant
= p
.i
& 0x000FFFFFFFFFFFFFULL
;
696 if (exp
== 0 && ((p
.i
& 0x8000000000000000ULL
) || mant
!= 0)) {
697 /* Reserved operands / Dirty zero */
704 static always_inline
double vaxg_to_ieee64 (double fg
)
713 exp
= (p
.i
>> 52) & 0x7FF;
724 static always_inline
double ieee64_to_vaxg (double fi
)
734 exp
= (p
.i
>> 52) & 0x7FF;
735 mant
= p
.i
& 0x000FFFFFFFFFFFFFULL
;
737 /* NaN or infinity */
738 p
.i
= 1; /* VAX dirty zero */
739 } else if (exp
== 0) {
750 p
.i
= 1; /* VAX dirty zero */
759 void helper_addg (void)
761 double ft0
, ft1
, ft2
;
763 if (!vaxg_is_valid(FT0
) || !vaxg_is_valid(FT1
)) {
766 ft0
= vaxg_to_ieee64(FT0
);
767 ft1
= vaxg_to_ieee64(FT1
);
768 ft2
= float64_add(ft0
, ft1
, &FP_STATUS
);
769 FT0
= ieee64_to_vaxg(ft2
);
772 void helper_subg (void)
774 double ft0
, ft1
, ft2
;
776 if (!vaxg_is_valid(FT0
) || !vaxg_is_valid(FT1
)) {
779 ft0
= vaxg_to_ieee64(FT0
);
780 ft1
= vaxg_to_ieee64(FT1
);
781 ft2
= float64_sub(ft0
, ft1
, &FP_STATUS
);
782 FT0
= ieee64_to_vaxg(ft2
);
785 void helper_mulg (void)
787 double ft0
, ft1
, ft2
;
789 if (!vaxg_is_valid(FT0
) || !vaxg_is_valid(FT1
)) {
792 ft0
= vaxg_to_ieee64(FT0
);
793 ft1
= vaxg_to_ieee64(FT1
);
794 ft2
= float64_mul(ft0
, ft1
, &FP_STATUS
);
795 FT0
= ieee64_to_vaxg(ft2
);
798 void helper_divg (void)
800 double ft0
, ft1
, ft2
;
802 if (!vaxg_is_valid(FT0
) || !vaxg_is_valid(FT1
)) {
805 ft0
= vaxg_to_ieee64(FT0
);
806 ft1
= vaxg_to_ieee64(FT1
);
807 ft2
= float64_div(ft0
, ft1
, &FP_STATUS
);
808 FT0
= ieee64_to_vaxg(ft2
);
811 void helper_sqrtg (void)
815 if (!vaxg_is_valid(FT0
) || !vaxg_is_valid(FT1
)) {
818 ft0
= vaxg_to_ieee64(FT0
);
819 ft1
= float64_sqrt(ft0
, &FP_STATUS
);
820 FT0
= ieee64_to_vaxg(ft1
);
823 void helper_cmpgeq (void)
831 if (!vaxg_is_valid(FT0
) || !vaxg_is_valid(FT1
)) {
834 ft0
= vaxg_to_ieee64(FT0
);
835 ft1
= vaxg_to_ieee64(FT1
);
837 if (float64_eq(ft0
, ft1
, &FP_STATUS
))
838 p
.u
= 0x4000000000000000ULL
;
842 void helper_cmpglt (void)
850 if (!vaxg_is_valid(FT0
) || !vaxg_is_valid(FT1
)) {
853 ft0
= vaxg_to_ieee64(FT0
);
854 ft1
= vaxg_to_ieee64(FT1
);
856 if (float64_lt(ft0
, ft1
, &FP_STATUS
))
857 p
.u
= 0x4000000000000000ULL
;
861 void helper_cmpgle (void)
869 if (!vaxg_is_valid(FT0
) || !vaxg_is_valid(FT1
)) {
872 ft0
= vaxg_to_ieee64(FT0
);
873 ft1
= vaxg_to_ieee64(FT1
);
875 if (float64_le(ft0
, ft1
, &FP_STATUS
))
876 p
.u
= 0x4000000000000000ULL
;
880 void helper_cvtqs (void)
891 void helper_cvttq (void)
902 void helper_cvtqt (void)
913 void helper_cvtqf (void)
921 FT0
= ieee32_to_vaxf(p
.u
);
924 void helper_cvtgf (void)
928 ft0
= vaxg_to_ieee64(FT0
);
929 FT0
= ieee32_to_vaxf(ft0
);
932 void helper_cvtgd (void)
937 void helper_cvtgq (void)
944 p
.u
= vaxg_to_ieee64(FT0
);
948 void helper_cvtqg (void)
956 FT0
= ieee64_to_vaxg(p
.u
);
959 void helper_cvtdg (void)
964 void helper_cvtlq (void)
972 q
.u
= (p
.u
>> 29) & 0x3FFFFFFF;
974 q
.u
= (int64_t)((int32_t)q
.u
);
978 static always_inline
void __helper_cvtql (int s
, int v
)
986 q
.u
= ((uint64_t)(p
.u
& 0xC0000000)) << 32;
987 q
.u
|= ((uint64_t)(p
.u
& 0x7FFFFFFF)) << 29;
989 if (v
&& (int64_t)((int32_t)p
.u
) != (int64_t)p
.u
) {
990 helper_excp(EXCP_ARITH
, EXCP_ARITH_OVERFLOW
);
997 void helper_cvtql (void)
999 __helper_cvtql(0, 0);
1002 void helper_cvtqlv (void)
1004 __helper_cvtql(0, 1);
1007 void helper_cvtqlsv (void)
1009 __helper_cvtql(1, 1);
1012 void helper_cmpfeq (void)
1014 if (float64_eq(FT0
, FT1
, &FP_STATUS
))
1020 void helper_cmpfne (void)
1022 if (float64_eq(FT0
, FT1
, &FP_STATUS
))
1028 void helper_cmpflt (void)
1030 if (float64_lt(FT0
, FT1
, &FP_STATUS
))
1036 void helper_cmpfle (void)
1038 if (float64_lt(FT0
, FT1
, &FP_STATUS
))
1044 void helper_cmpfgt (void)
1046 if (float64_le(FT0
, FT1
, &FP_STATUS
))
1052 void helper_cmpfge (void)
1054 if (float64_lt(FT0
, FT1
, &FP_STATUS
))
1060 #if !defined (CONFIG_USER_ONLY)
1061 void helper_mfpr (int iprn
)
1065 if (cpu_alpha_mfpr(env
, iprn
, &val
) == 0)
1069 void helper_mtpr (int iprn
)
1071 cpu_alpha_mtpr(env
, iprn
, T0
, NULL
);
1075 #if defined(HOST_SPARC) || defined(HOST_SPARC64)
1076 void helper_reset_FT0 (void)
1081 void helper_reset_FT1 (void)
1086 void helper_reset_FT2 (void)
1092 /*****************************************************************************/
1093 /* Softmmu support */
1094 #if !defined (CONFIG_USER_ONLY)
1097 # define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
1099 # define GETPC() (__builtin_return_address(0))
1102 /* XXX: the two following helpers are pure hacks.
1103 * Hopefully, we emulate the PALcode, then we should never see
1104 * HW_LD / HW_ST instructions.
1106 void helper_ld_phys_to_virt (void)
1108 uint64_t tlb_addr
, physaddr
;
1112 mmu_idx
= cpu_mmu_index(env
);
1113 index
= (T0
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
1115 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_read
;
1116 if ((T0
& TARGET_PAGE_MASK
) ==
1117 (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
1118 physaddr
= T0
+ env
->tlb_table
[mmu_idx
][index
].addend
;
1120 /* the page is not in the TLB : fill it */
1122 tlb_fill(T0
, 0, mmu_idx
, retaddr
);
1128 void helper_st_phys_to_virt (void)
1130 uint64_t tlb_addr
, physaddr
;
1134 mmu_idx
= cpu_mmu_index(env
);
1135 index
= (T0
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
1137 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
1138 if ((T0
& TARGET_PAGE_MASK
) ==
1139 (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
1140 physaddr
= T0
+ env
->tlb_table
[mmu_idx
][index
].addend
;
1142 /* the page is not in the TLB : fill it */
1144 tlb_fill(T0
, 1, mmu_idx
, retaddr
);
1150 #define MMUSUFFIX _mmu
1153 #include "softmmu_template.h"
1156 #include "softmmu_template.h"
1159 #include "softmmu_template.h"
1162 #include "softmmu_template.h"
1164 /* try to fill the TLB and return an exception if error. If retaddr is
1165 NULL, it means that the function was called in C code (i.e. not
1166 from generated code or from helper.c) */
1167 /* XXX: fix it to restore all registers */
1168 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
1170 TranslationBlock
*tb
;
1171 CPUState
*saved_env
;
1175 /* XXX: hack to restore env in all cases, even if not called from
1178 env
= cpu_single_env
;
1179 ret
= cpu_alpha_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
1180 if (!likely(ret
== 0)) {
1181 if (likely(retaddr
)) {
1182 /* now we have a real cpu fault */
1183 pc
= (unsigned long)retaddr
;
1184 tb
= tb_find_pc(pc
);
1186 /* the PC is inside the translated code. It means that we have
1187 a virtual CPU fault */
1188 cpu_restore_state(tb
, env
, pc
, NULL
);
1191 /* Exception index and error code are already set */