xref: /qemu/target/ppc/cpu.c (revision c07cd110a1824e2d046581af7375f16dac26e96f)
1 /*
2  *  PowerPC CPU routines for qemu.
3  *
4  * Copyright (c) 2017 Nikunj A Dadhania, IBM Corporation.
5  *
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.1 of the License, or (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "cpu-models.h"
23 #include "cpu-qom.h"
24 #include "exec/log.h"
25 #include "fpu/softfloat-helpers.h"
26 #include "mmu-hash64.h"
27 #include "helper_regs.h"
28 #include "system/tcg.h"
29 
30 target_ulong cpu_read_xer(const CPUPPCState *env)
31 {
32     if (is_isa300(env)) {
33         return env->xer | (env->so << XER_SO) |
34             (env->ov << XER_OV) | (env->ca << XER_CA) |
35             (env->ov32 << XER_OV32) | (env->ca32 << XER_CA32);
36     }
37 
38     return env->xer | (env->so << XER_SO) | (env->ov << XER_OV) |
39         (env->ca << XER_CA);
40 }
41 
42 void cpu_write_xer(CPUPPCState *env, target_ulong xer)
43 {
44     env->so = (xer >> XER_SO) & 1;
45     env->ov = (xer >> XER_OV) & 1;
46     env->ca = (xer >> XER_CA) & 1;
47     /* write all the flags, while reading back check of isa300 */
48     env->ov32 = (xer >> XER_OV32) & 1;
49     env->ca32 = (xer >> XER_CA32) & 1;
50     env->xer = xer & ~((1ul << XER_SO) |
51                        (1ul << XER_OV) | (1ul << XER_CA) |
52                        (1ul << XER_OV32) | (1ul << XER_CA32));
53 }
54 
55 void ppc_store_vscr(CPUPPCState *env, uint32_t vscr)
56 {
57     env->vscr = vscr & ~(1u << VSCR_SAT);
58     /* Which bit we set is completely arbitrary, but clear the rest.  */
59     env->vscr_sat.u64[0] = vscr & (1u << VSCR_SAT);
60     env->vscr_sat.u64[1] = 0;
61     set_flush_to_zero((vscr >> VSCR_NJ) & 1, &env->vec_status);
62     set_flush_inputs_to_zero((vscr >> VSCR_NJ) & 1, &env->vec_status);
63 }
64 
65 uint32_t ppc_get_vscr(CPUPPCState *env)
66 {
67     uint32_t sat = (env->vscr_sat.u64[0] | env->vscr_sat.u64[1]) != 0;
68     return env->vscr | (sat << VSCR_SAT);
69 }
70 
71 void ppc_set_cr(CPUPPCState *env, uint64_t cr)
72 {
73     for (int i = 7; i >= 0; i--) {
74         env->crf[i] = cr & 0xf;
75         cr >>= 4;
76     }
77 }
78 
79 uint64_t ppc_get_cr(const CPUPPCState *env)
80 {
81     uint64_t cr = 0;
82     for (int i = 0; i < 8; i++) {
83         cr |= (env->crf[i] & 0xf) << (4 * (7 - i));
84     }
85     return cr;
86 }
87 
88 /* GDBstub can read and write MSR... */
89 void ppc_store_msr(CPUPPCState *env, target_ulong value)
90 {
91     hreg_store_msr(env, value, 0);
92 }
93 
94 #if !defined(CONFIG_USER_ONLY)
95 void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val)
96 {
97     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
98     CPUPPCState *env = &cpu->env;
99 
100     env->spr[SPR_LPCR] = val & pcc->lpcr_mask;
101     /* The gtse bit affects hflags */
102     hreg_compute_hflags(env);
103 
104     ppc_maybe_interrupt(env);
105 }
106 
107 #if defined(TARGET_PPC64)
108 void ppc_update_ciabr(CPUPPCState *env)
109 {
110     CPUState *cs = env_cpu(env);
111     target_ulong ciabr = env->spr[SPR_CIABR];
112     target_ulong ciea, priv;
113 
114     ciea = ciabr & PPC_BITMASK(0, 61);
115     priv = ciabr & PPC_BITMASK(62, 63);
116 
117     if (env->ciabr_breakpoint) {
118         cpu_breakpoint_remove_by_ref(cs, env->ciabr_breakpoint);
119         env->ciabr_breakpoint = NULL;
120     }
121 
122     if (priv) {
123         cpu_breakpoint_insert(cs, ciea, BP_CPU, &env->ciabr_breakpoint);
124     }
125 }
126 
127 void ppc_store_ciabr(CPUPPCState *env, target_ulong val)
128 {
129     env->spr[SPR_CIABR] = val;
130     ppc_update_ciabr(env);
131 }
132 
133 void ppc_update_daw(CPUPPCState *env, int rid)
134 {
135     CPUState *cs = env_cpu(env);
136     int spr_dawr = rid ? SPR_DAWR1 : SPR_DAWR0;
137     int spr_dawrx = rid ? SPR_DAWRX1 : SPR_DAWRX0;
138     target_ulong deaw = env->spr[spr_dawr] & PPC_BITMASK(0, 60);
139     uint32_t dawrx = env->spr[spr_dawrx];
140     int mrd = extract32(dawrx, PPC_BIT_NR(48), 54 - 48);
141     bool dw = extract32(dawrx, PPC_BIT_NR(57), 1);
142     bool dr = extract32(dawrx, PPC_BIT_NR(58), 1);
143     bool hv = extract32(dawrx, PPC_BIT_NR(61), 1);
144     bool sv = extract32(dawrx, PPC_BIT_NR(62), 1);
145     bool pr = extract32(dawrx, PPC_BIT_NR(62), 1);
146     vaddr len;
147     int flags;
148 
149     if (env->dawr_watchpoint[rid]) {
150         cpu_watchpoint_remove_by_ref(cs, env->dawr_watchpoint[rid]);
151         env->dawr_watchpoint[rid] = NULL;
152     }
153 
154     if (!dr && !dw) {
155         return;
156     }
157 
158     if (!hv && !sv && !pr) {
159         return;
160     }
161 
162     len = (mrd + 1) * 8;
163     flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
164     if (dr) {
165         flags |= BP_MEM_READ;
166     }
167     if (dw) {
168         flags |= BP_MEM_WRITE;
169     }
170 
171     cpu_watchpoint_insert(cs, deaw, len, flags, &env->dawr_watchpoint[rid]);
172 }
173 
174 void ppc_store_dawr0(CPUPPCState *env, target_ulong val)
175 {
176     env->spr[SPR_DAWR0] = val;
177     ppc_update_daw(env, 0);
178 }
179 
180 static void ppc_store_dawrx(CPUPPCState *env, uint32_t val, int rid)
181 {
182     int hrammc = extract32(val, PPC_BIT_NR(56), 1);
183 
184     if (hrammc) {
185         /* This might be done with a second watchpoint at the xor of DEAW[0] */
186         qemu_log_mask(LOG_UNIMP, "%s: DAWRX%d[HRAMMC] is unimplemented\n",
187                       __func__, rid);
188     }
189 
190     env->spr[rid ? SPR_DAWRX1 : SPR_DAWRX0] = val;
191     ppc_update_daw(env, rid);
192 }
193 
194 void ppc_store_dawrx0(CPUPPCState *env, uint32_t val)
195 {
196     ppc_store_dawrx(env, val, 0);
197 }
198 
199 void ppc_store_dawr1(CPUPPCState *env, target_ulong val)
200 {
201     env->spr[SPR_DAWR1] = val;
202     ppc_update_daw(env, 1);
203 }
204 
205 void ppc_store_dawrx1(CPUPPCState *env, uint32_t val)
206 {
207     ppc_store_dawrx(env, val, 1);
208 }
209 
210 #endif
211 #endif
212 
213 static inline void fpscr_set_rounding_mode(CPUPPCState *env)
214 {
215     int rnd_type;
216 
217     /* Set rounding mode */
218     switch (env->fpscr & FP_RN) {
219     case 0:
220         /* Best approximation (round to nearest) */
221         rnd_type = float_round_nearest_even;
222         break;
223     case 1:
224         /* Smaller magnitude (round toward zero) */
225         rnd_type = float_round_to_zero;
226         break;
227     case 2:
228         /* Round toward +infinite */
229         rnd_type = float_round_up;
230         break;
231     default:
232     case 3:
233         /* Round toward -infinite */
234         rnd_type = float_round_down;
235         break;
236     }
237     set_float_rounding_mode(rnd_type, &env->fp_status);
238 }
239 
240 void ppc_store_fpscr(CPUPPCState *env, target_ulong val)
241 {
242     val &= FPSCR_MTFS_MASK;
243     if (val & FPSCR_IX) {
244         val |= FP_VX;
245     }
246     if ((val >> FPSCR_XX) & (val >> FPSCR_XE) & 0x1f) {
247         val |= FP_FEX;
248     }
249     env->fpscr = val;
250     env->fp_status.rebias_overflow  = (FP_OE & env->fpscr) ? true : false;
251     env->fp_status.rebias_underflow = (FP_UE & env->fpscr) ? true : false;
252     if (tcg_enabled()) {
253         fpscr_set_rounding_mode(env);
254     }
255 }
256