2 * Miscellaneous PowerPC emulation helpers for QEMU.
4 * Copyright (c) 2003-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.1 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, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
25 #include "qemu/error-report.h"
26 #include "qemu/main-loop.h"
27 #include "mmu-book3s-v3.h"
29 #include "helper_regs.h"
31 /*****************************************************************************/
33 void helper_load_dump_spr(CPUPPCState
*env
, uint32_t sprn
)
35 qemu_log("Read SPR %d %03x => " TARGET_FMT_lx
"\n", sprn
, sprn
,
39 void helper_store_dump_spr(CPUPPCState
*env
, uint32_t sprn
)
41 qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx
"\n", sprn
, sprn
,
46 static void raise_hv_fu_exception(CPUPPCState
*env
, uint32_t bit
,
47 const char *caller
, uint32_t cause
,
50 qemu_log_mask(CPU_LOG_INT
, "HV Facility %d is unavailable (%s)\n",
53 env
->spr
[SPR_HFSCR
] &= ~((target_ulong
)FSCR_IC_MASK
<< FSCR_IC_POS
);
55 raise_exception_err_ra(env
, POWERPC_EXCP_HV_FU
, cause
, raddr
);
58 static void raise_fu_exception(CPUPPCState
*env
, uint32_t bit
,
59 uint32_t sprn
, uint32_t cause
,
62 qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn
, bit
);
64 env
->spr
[SPR_FSCR
] &= ~((target_ulong
)FSCR_IC_MASK
<< FSCR_IC_POS
);
65 cause
&= FSCR_IC_MASK
;
66 env
->spr
[SPR_FSCR
] |= (target_ulong
)cause
<< FSCR_IC_POS
;
68 raise_exception_err_ra(env
, POWERPC_EXCP_FU
, 0, raddr
);
72 void helper_hfscr_facility_check(CPUPPCState
*env
, uint32_t bit
,
73 const char *caller
, uint32_t cause
)
76 if ((env
->msr_mask
& MSR_HVB
) && !FIELD_EX64(env
->msr
, MSR
, HV
) &&
77 !(env
->spr
[SPR_HFSCR
] & (1UL << bit
))) {
78 raise_hv_fu_exception(env
, bit
, caller
, cause
, GETPC());
83 void helper_fscr_facility_check(CPUPPCState
*env
, uint32_t bit
,
84 uint32_t sprn
, uint32_t cause
)
87 if (env
->spr
[SPR_FSCR
] & (1ULL << bit
)) {
88 /* Facility is enabled, continue */
91 raise_fu_exception(env
, bit
, sprn
, cause
, GETPC());
95 void helper_msr_facility_check(CPUPPCState
*env
, uint32_t bit
,
96 uint32_t sprn
, uint32_t cause
)
99 if (env
->msr
& (1ULL << bit
)) {
100 /* Facility is enabled, continue */
103 raise_fu_exception(env
, bit
, sprn
, cause
, GETPC());
107 #if !defined(CONFIG_USER_ONLY)
109 void helper_store_sdr1(CPUPPCState
*env
, target_ulong val
)
111 if (env
->spr
[SPR_SDR1
] != val
) {
112 ppc_store_sdr1(env
, val
);
113 tlb_flush(env_cpu(env
));
117 #if defined(TARGET_PPC64)
118 void helper_store_ptcr(CPUPPCState
*env
, target_ulong val
)
120 if (env
->spr
[SPR_PTCR
] != val
) {
121 PowerPCCPU
*cpu
= env_archcpu(env
);
122 target_ulong ptcr_mask
= PTCR_PATB
| PTCR_PATS
;
123 target_ulong patbsize
= val
& PTCR_PATS
;
125 qemu_log_mask(CPU_LOG_MMU
, "%s: " TARGET_FMT_lx
"\n", __func__
, val
);
128 assert(env
->mmu_model
& POWERPC_MMU_3_00
);
130 if (val
& ~ptcr_mask
) {
131 error_report("Invalid bits 0x"TARGET_FMT_lx
" set in PTCR",
137 error_report("Invalid Partition Table size 0x" TARGET_FMT_lx
138 " stored in PTCR", patbsize
);
142 env
->spr
[SPR_PTCR
] = val
;
143 tlb_flush(env_cpu(env
));
147 void helper_store_pcr(CPUPPCState
*env
, target_ulong value
)
149 PowerPCCPU
*cpu
= env_archcpu(env
);
150 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
152 env
->spr
[SPR_PCR
] = value
& pcc
->pcr_mask
;
156 * DPDES register is shared. Each bit reflects the state of the
157 * doorbell interrupt of a thread of the same core.
159 target_ulong
helper_load_dpdes(CPUPPCState
*env
)
161 target_ulong dpdes
= 0;
163 helper_hfscr_facility_check(env
, HFSCR_MSGP
, "load DPDES", HFSCR_IC_MSGP
);
165 /* TODO: TCG supports only one thread */
166 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DOORBELL
)) {
173 void helper_store_dpdes(CPUPPCState
*env
, target_ulong val
)
175 PowerPCCPU
*cpu
= env_archcpu(env
);
176 CPUState
*cs
= CPU(cpu
);
178 helper_hfscr_facility_check(env
, HFSCR_MSGP
, "store DPDES", HFSCR_IC_MSGP
);
180 /* TODO: TCG supports only one thread */
182 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid DPDES register value "
183 TARGET_FMT_lx
"\n", val
);
188 env
->pending_interrupts
|= 1 << PPC_INTERRUPT_DOORBELL
;
189 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
191 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DOORBELL
);
194 #endif /* defined(TARGET_PPC64) */
196 void helper_store_pidr(CPUPPCState
*env
, target_ulong val
)
198 env
->spr
[SPR_BOOKS_PID
] = val
;
199 tlb_flush(env_cpu(env
));
202 void helper_store_lpidr(CPUPPCState
*env
, target_ulong val
)
204 env
->spr
[SPR_LPIDR
] = val
;
207 * We need to flush the TLB on LPID changes as we only tag HV vs
208 * guest in TCG TLB. Also the quadrants means the HV will
209 * potentially access and cache entries for the current LPID as
212 tlb_flush(env_cpu(env
));
215 void helper_store_40x_dbcr0(CPUPPCState
*env
, target_ulong val
)
217 /* Bits 26 & 27 affect single-stepping. */
218 hreg_compute_hflags(env
);
219 /* Bits 28 & 29 affect reset or shutdown. */
220 store_40x_dbcr0(env
, val
);
223 void helper_store_40x_sler(CPUPPCState
*env
, target_ulong val
)
225 store_40x_sler(env
, val
);
229 /*****************************************************************************/
230 /* Special registers manipulation */
233 * This code is lifted from MacOnLinux. It is called whenever THRM1,2
234 * or 3 is read an fixes up the values in such a way that will make
235 * MacOS not hang. These registers exist on some 75x and 74xx
238 void helper_fixup_thrm(CPUPPCState
*env
)
243 #define THRM1_TIN (1 << 31)
244 #define THRM1_TIV (1 << 30)
245 #define THRM1_THRES(x) (((x) & 0x7f) << 23)
246 #define THRM1_TID (1 << 2)
247 #define THRM1_TIE (1 << 1)
248 #define THRM1_V (1 << 0)
249 #define THRM3_E (1 << 0)
251 if (!(env
->spr
[SPR_THRM3
] & THRM3_E
)) {
255 /* Note: Thermal interrupts are unimplemented */
256 for (i
= SPR_THRM1
; i
<= SPR_THRM2
; i
++) {
258 if (!(v
& THRM1_V
)) {
263 t
= v
& THRM1_THRES(127);
264 if ((v
& THRM1_TID
) && t
< THRM1_THRES(24)) {
267 if (!(v
& THRM1_TID
) && t
> THRM1_THRES(24)) {