13e00884fSGautham R. Shenoy /*
2136fbf65Szhaolichang * PowerPC internal definitions for qemu.
33e00884fSGautham R. Shenoy *
43e00884fSGautham R. Shenoy * This library is free software; you can redistribute it and/or
53e00884fSGautham R. Shenoy * modify it under the terms of the GNU Lesser General Public
63e00884fSGautham R. Shenoy * License as published by the Free Software Foundation; either
76bd039cdSChetan Pant * version 2.1 of the License, or (at your option) any later version.
83e00884fSGautham R. Shenoy *
93e00884fSGautham R. Shenoy * This library is distributed in the hope that it will be useful,
103e00884fSGautham R. Shenoy * but WITHOUT ANY WARRANTY; without even the implied warranty of
113e00884fSGautham R. Shenoy * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
123e00884fSGautham R. Shenoy * Lesser General Public License for more details.
133e00884fSGautham R. Shenoy *
143e00884fSGautham R. Shenoy * You should have received a copy of the GNU Lesser General Public
153e00884fSGautham R. Shenoy * License along with this library; if not, see <http://www.gnu.org/licenses/>.
163e00884fSGautham R. Shenoy */
173e00884fSGautham R. Shenoy
183e00884fSGautham R. Shenoy #ifndef PPC_INTERNAL_H
193e00884fSGautham R. Shenoy #define PPC_INTERNAL_H
203e00884fSGautham R. Shenoy
216ce1c9d0SPhilippe Mathieu-Daudé #include "exec/breakpoint.h"
2234553153SLucas Mateus Castro (alqotel) #include "hw/registerfields.h"
2374781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
24*c37f8978SRichard Henderson #include "accel/tcg/tb-cpu-state.h"
2534553153SLucas Mateus Castro (alqotel)
261978a41bSPhilippe Mathieu-Daudé /* PM instructions */
271978a41bSPhilippe Mathieu-Daudé typedef enum {
281978a41bSPhilippe Mathieu-Daudé PPC_PM_DOZE,
291978a41bSPhilippe Mathieu-Daudé PPC_PM_NAP,
301978a41bSPhilippe Mathieu-Daudé PPC_PM_SLEEP,
311978a41bSPhilippe Mathieu-Daudé PPC_PM_RVWINKLE,
321978a41bSPhilippe Mathieu-Daudé PPC_PM_STOP,
331978a41bSPhilippe Mathieu-Daudé } powerpc_pm_insn_t;
341978a41bSPhilippe Mathieu-Daudé
353e00884fSGautham R. Shenoy #define FUNC_MASK(name, ret_type, size, max_val) \
363e00884fSGautham R. Shenoy static inline ret_type name(uint##size##_t start, \
373e00884fSGautham R. Shenoy uint##size##_t end) \
383e00884fSGautham R. Shenoy { \
393e00884fSGautham R. Shenoy ret_type ret, max_bit = size - 1; \
403e00884fSGautham R. Shenoy \
413e00884fSGautham R. Shenoy if (likely(start == 0)) { \
423e00884fSGautham R. Shenoy ret = max_val << (max_bit - end); \
433e00884fSGautham R. Shenoy } else if (likely(end == max_bit)) { \
443e00884fSGautham R. Shenoy ret = max_val >> start; \
453e00884fSGautham R. Shenoy } else { \
463e00884fSGautham R. Shenoy ret = (((uint##size##_t)(-1ULL)) >> (start)) ^ \
473e00884fSGautham R. Shenoy (((uint##size##_t)(-1ULL) >> (end)) >> 1); \
483e00884fSGautham R. Shenoy if (unlikely(start > end)) { \
493e00884fSGautham R. Shenoy return ~ret; \
503e00884fSGautham R. Shenoy } \
513e00884fSGautham R. Shenoy } \
523e00884fSGautham R. Shenoy \
533e00884fSGautham R. Shenoy return ret; \
543e00884fSGautham R. Shenoy }
553e00884fSGautham R. Shenoy
563e00884fSGautham R. Shenoy #if defined(TARGET_PPC64)
573e00884fSGautham R. Shenoy FUNC_MASK(MASK, target_ulong, 64, UINT64_MAX);
583e00884fSGautham R. Shenoy #else
593e00884fSGautham R. Shenoy FUNC_MASK(MASK, target_ulong, 32, UINT32_MAX);
603e00884fSGautham R. Shenoy #endif
613e00884fSGautham R. Shenoy FUNC_MASK(mask_u32, uint32_t, 32, UINT32_MAX);
623e00884fSGautham R. Shenoy FUNC_MASK(mask_u64, uint64_t, 64, UINT64_MAX);
633e00884fSGautham R. Shenoy
64985e3023SBharata B Rao /*****************************************************************************/
65985e3023SBharata B Rao /*** Instruction decoding ***/
66985e3023SBharata B Rao #define EXTRACT_HELPER(name, shift, nb) \
67985e3023SBharata B Rao static inline uint32_t name(uint32_t opcode) \
68985e3023SBharata B Rao { \
694c23c2a5SMark Cave-Ayland return extract32(opcode, shift, nb); \
70985e3023SBharata B Rao }
71985e3023SBharata B Rao
72985e3023SBharata B Rao #define EXTRACT_SHELPER(name, shift, nb) \
73985e3023SBharata B Rao static inline int32_t name(uint32_t opcode) \
74985e3023SBharata B Rao { \
754c23c2a5SMark Cave-Ayland return sextract32(opcode, shift, nb); \
76985e3023SBharata B Rao }
77985e3023SBharata B Rao
78985e3023SBharata B Rao #define EXTRACT_HELPER_SPLIT(name, shift1, nb1, shift2, nb2) \
79985e3023SBharata B Rao static inline uint32_t name(uint32_t opcode) \
80985e3023SBharata B Rao { \
814c23c2a5SMark Cave-Ayland return extract32(opcode, shift1, nb1) << nb2 | \
824c23c2a5SMark Cave-Ayland extract32(opcode, shift2, nb2); \
83985e3023SBharata B Rao }
84985e3023SBharata B Rao
85403a884aSNikunj A Dadhania #define EXTRACT_HELPER_SPLIT_3(name, \
86985e3023SBharata B Rao d0_bits, shift_op_d0, shift_d0, \
87985e3023SBharata B Rao d1_bits, shift_op_d1, shift_d1, \
88985e3023SBharata B Rao d2_bits, shift_op_d2, shift_d2) \
89985e3023SBharata B Rao static inline int16_t name(uint32_t opcode) \
90985e3023SBharata B Rao { \
91985e3023SBharata B Rao return \
92985e3023SBharata B Rao (((opcode >> (shift_op_d0)) & ((1 << (d0_bits)) - 1)) << (shift_d0)) | \
93985e3023SBharata B Rao (((opcode >> (shift_op_d1)) & ((1 << (d1_bits)) - 1)) << (shift_d1)) | \
94985e3023SBharata B Rao (((opcode >> (shift_op_d2)) & ((1 << (d2_bits)) - 1)) << (shift_d2)); \
95985e3023SBharata B Rao }
96985e3023SBharata B Rao
97985e3023SBharata B Rao
98985e3023SBharata B Rao /* Opcode part 1 */
99985e3023SBharata B Rao EXTRACT_HELPER(opc1, 26, 6);
100985e3023SBharata B Rao /* Opcode part 2 */
101985e3023SBharata B Rao EXTRACT_HELPER(opc2, 1, 5);
102985e3023SBharata B Rao /* Opcode part 3 */
103985e3023SBharata B Rao EXTRACT_HELPER(opc3, 6, 5);
104985e3023SBharata B Rao /* Opcode part 4 */
105985e3023SBharata B Rao EXTRACT_HELPER(opc4, 16, 5);
106985e3023SBharata B Rao /* Update Cr0 flags */
107985e3023SBharata B Rao EXTRACT_HELPER(Rc, 0, 1);
108985e3023SBharata B Rao /* Update Cr6 flags (Altivec) */
109985e3023SBharata B Rao EXTRACT_HELPER(Rc21, 10, 1);
110985e3023SBharata B Rao /* Destination */
111985e3023SBharata B Rao EXTRACT_HELPER(rD, 21, 5);
112985e3023SBharata B Rao /* Source */
113985e3023SBharata B Rao EXTRACT_HELPER(rS, 21, 5);
114985e3023SBharata B Rao /* First operand */
115985e3023SBharata B Rao EXTRACT_HELPER(rA, 16, 5);
116985e3023SBharata B Rao /* Second operand */
117985e3023SBharata B Rao EXTRACT_HELPER(rB, 11, 5);
118985e3023SBharata B Rao /* Third operand */
119985e3023SBharata B Rao EXTRACT_HELPER(rC, 6, 5);
120985e3023SBharata B Rao /*** Get CRn ***/
121985e3023SBharata B Rao EXTRACT_HELPER(crfD, 23, 3);
122985e3023SBharata B Rao EXTRACT_HELPER(BF, 23, 3);
123985e3023SBharata B Rao EXTRACT_HELPER(crfS, 18, 3);
124985e3023SBharata B Rao EXTRACT_HELPER(crbD, 21, 5);
125985e3023SBharata B Rao EXTRACT_HELPER(crbA, 16, 5);
126985e3023SBharata B Rao EXTRACT_HELPER(crbB, 11, 5);
127985e3023SBharata B Rao /* SPR / TBL */
128985e3023SBharata B Rao EXTRACT_HELPER(_SPR, 11, 10);
SPR(uint32_t opcode)129985e3023SBharata B Rao static inline uint32_t SPR(uint32_t opcode)
130985e3023SBharata B Rao {
131985e3023SBharata B Rao uint32_t sprn = _SPR(opcode);
132985e3023SBharata B Rao
133985e3023SBharata B Rao return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
134985e3023SBharata B Rao }
135985e3023SBharata B Rao /*** Get constants ***/
136985e3023SBharata B Rao /* 16 bits signed immediate value */
137985e3023SBharata B Rao EXTRACT_SHELPER(SIMM, 0, 16);
138985e3023SBharata B Rao /* 16 bits unsigned immediate value */
139985e3023SBharata B Rao EXTRACT_HELPER(UIMM, 0, 16);
140985e3023SBharata B Rao /* 5 bits signed immediate value */
141ffcd21acSMark Cave-Ayland EXTRACT_SHELPER(SIMM5, 16, 5);
142985e3023SBharata B Rao /* 5 bits signed immediate value */
143985e3023SBharata B Rao EXTRACT_HELPER(UIMM5, 16, 5);
144985e3023SBharata B Rao /* 4 bits unsigned immediate value */
145985e3023SBharata B Rao EXTRACT_HELPER(UIMM4, 16, 4);
146985e3023SBharata B Rao /* Bit count */
147985e3023SBharata B Rao EXTRACT_HELPER(NB, 11, 5);
148985e3023SBharata B Rao /* Shift count */
149985e3023SBharata B Rao EXTRACT_HELPER(SH, 11, 5);
150a68a6146SBalamuruhan S /* lwat/stwat/ldat/lwat */
151a68a6146SBalamuruhan S EXTRACT_HELPER(FC, 11, 5);
152985e3023SBharata B Rao /* Vector shift count */
153985e3023SBharata B Rao EXTRACT_HELPER(VSH, 6, 4);
154985e3023SBharata B Rao /* Mask start */
155985e3023SBharata B Rao EXTRACT_HELPER(MB, 6, 5);
156985e3023SBharata B Rao /* Mask end */
157985e3023SBharata B Rao EXTRACT_HELPER(ME, 1, 5);
158985e3023SBharata B Rao /* Trap operand */
159985e3023SBharata B Rao EXTRACT_HELPER(TO, 21, 5);
160985e3023SBharata B Rao
161985e3023SBharata B Rao EXTRACT_HELPER(CRM, 12, 8);
162985e3023SBharata B Rao
163985e3023SBharata B Rao #ifndef CONFIG_USER_ONLY
164985e3023SBharata B Rao EXTRACT_HELPER(SR, 16, 4);
165985e3023SBharata B Rao #endif
166985e3023SBharata B Rao
167985e3023SBharata B Rao /* mtfsf/mtfsfi */
168985e3023SBharata B Rao EXTRACT_HELPER(FPBF, 23, 3);
169985e3023SBharata B Rao EXTRACT_HELPER(FPIMM, 12, 4);
170985e3023SBharata B Rao EXTRACT_HELPER(FPL, 25, 1);
171985e3023SBharata B Rao EXTRACT_HELPER(FPFLM, 17, 8);
172985e3023SBharata B Rao EXTRACT_HELPER(FPW, 16, 1);
173985e3023SBharata B Rao
174985e3023SBharata B Rao /* addpcis */
175403a884aSNikunj A Dadhania EXTRACT_HELPER_SPLIT_3(DX, 10, 6, 6, 5, 16, 1, 1, 0, 0)
176985e3023SBharata B Rao #if defined(TARGET_PPC64)
177985e3023SBharata B Rao /* darn */
178985e3023SBharata B Rao EXTRACT_HELPER(L, 16, 2);
179985e3023SBharata B Rao #endif
1800c9717ffSNicholas Piggin /* wait */
1810c9717ffSNicholas Piggin EXTRACT_HELPER(WC, 21, 2);
1820c9717ffSNicholas Piggin EXTRACT_HELPER(PL, 16, 2);
183985e3023SBharata B Rao
184985e3023SBharata B Rao /*** Jump target decoding ***/
185985e3023SBharata B Rao /* Immediate address */
LI(uint32_t opcode)186985e3023SBharata B Rao static inline target_ulong LI(uint32_t opcode)
187985e3023SBharata B Rao {
188985e3023SBharata B Rao return (opcode >> 0) & 0x03FFFFFC;
189985e3023SBharata B Rao }
190985e3023SBharata B Rao
BD(uint32_t opcode)191985e3023SBharata B Rao static inline uint32_t BD(uint32_t opcode)
192985e3023SBharata B Rao {
193985e3023SBharata B Rao return (opcode >> 0) & 0xFFFC;
194985e3023SBharata B Rao }
195985e3023SBharata B Rao
196985e3023SBharata B Rao EXTRACT_HELPER(BO, 21, 5);
197985e3023SBharata B Rao EXTRACT_HELPER(BI, 16, 5);
198985e3023SBharata B Rao /* Absolute/relative address */
199985e3023SBharata B Rao EXTRACT_HELPER(AA, 1, 1);
200985e3023SBharata B Rao /* Link */
201985e3023SBharata B Rao EXTRACT_HELPER(LK, 0, 1);
202985e3023SBharata B Rao
203985e3023SBharata B Rao /* DFP Z22-form */
204985e3023SBharata B Rao EXTRACT_HELPER(DCM, 10, 6)
205985e3023SBharata B Rao
206985e3023SBharata B Rao /* DFP Z23-form */
207985e3023SBharata B Rao EXTRACT_HELPER(RMC, 9, 2)
208be07ad58SJose Ricardo Ziviani EXTRACT_HELPER(Rrm, 16, 1)
209985e3023SBharata B Rao
210d59ba583SNikunj A Dadhania EXTRACT_HELPER_SPLIT(DQxT, 3, 1, 21, 5);
211985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
212985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
213985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
214985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
215985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xC, 3, 1, 6, 5);
216985e3023SBharata B Rao EXTRACT_HELPER(DM, 8, 2);
217985e3023SBharata B Rao EXTRACT_HELPER(UIM, 16, 2);
218985e3023SBharata B Rao EXTRACT_HELPER(SHW, 8, 2);
219985e3023SBharata B Rao EXTRACT_HELPER(SP, 19, 2);
220985e3023SBharata B Rao EXTRACT_HELPER(IMM8, 11, 8);
22178241762SNikunj A Dadhania EXTRACT_HELPER(DCMX, 16, 7);
222403a884aSNikunj A Dadhania EXTRACT_HELPER_SPLIT_3(DCMX_XV, 5, 16, 0, 1, 2, 5, 1, 6, 6);
223985e3023SBharata B Rao
224f566c047SBharata B Rao void helper_compute_fprf_float16(CPUPPCState *env, float16 arg);
2259aeae8e1SBharata B Rao void helper_compute_fprf_float32(CPUPPCState *env, float32 arg);
22607bdd247SBharata B Rao void helper_compute_fprf_float128(CPUPPCState *env, float128 arg);
2270f3110faSRichard Henderson
2287468e2c8SBruno Larsen (billionai) /* translate.c */
2297468e2c8SBruno Larsen (billionai)
2307468e2c8SBruno Larsen (billionai) int ppc_fixup_cpu(PowerPCCPU *cpu);
2317468e2c8SBruno Larsen (billionai) void create_ppc_opcodes(PowerPCCPU *cpu, Error **errp);
2327468e2c8SBruno Larsen (billionai) void destroy_ppc_opcodes(PowerPCCPU *cpu);
2337468e2c8SBruno Larsen (billionai)
23435a5d74eSBruno Larsen (billionai) /* gdbstub.c */
23535a5d74eSBruno Larsen (billionai) void ppc_gdb_init(CPUState *cs, PowerPCCPUClass *ppc);
236a6506838SAkihiko Odaki const gchar *ppc_gdb_arch_name(CPUState *cs);
23735a5d74eSBruno Larsen (billionai)
238414fa2aaSPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
239414fa2aaSPhilippe Mathieu-Daudé
240cd1038ecSBALATON Zoltan /* Check if permission bit required for the access_type is set in prot */
check_prot_access_type(int prot,MMUAccessType access_type)241cd1038ecSBALATON Zoltan static inline int check_prot_access_type(int prot, MMUAccessType access_type)
242cd1038ecSBALATON Zoltan {
243cd1038ecSBALATON Zoltan return prot & (1 << access_type);
244cd1038ecSBALATON Zoltan }
245cd1038ecSBALATON Zoltan
2465118ebe8SLucas Mateus Castro (alqotel) /* PowerPC MMU emulation */
2475118ebe8SLucas Mateus Castro (alqotel)
2485118ebe8SLucas Mateus Castro (alqotel) bool ppc_xlate(PowerPCCPU *cpu, vaddr eaddr, MMUAccessType access_type,
2495118ebe8SLucas Mateus Castro (alqotel) hwaddr *raddrp, int *psizep, int *protp,
2505118ebe8SLucas Mateus Castro (alqotel) int mmu_idx, bool guest_visible);
2516b9ea7f3SBALATON Zoltan
2525118ebe8SLucas Mateus Castro (alqotel) /* Software driven TLB helpers */
2535118ebe8SLucas Mateus Castro (alqotel) int ppc6xx_tlb_getnum(CPUPPCState *env, target_ulong eaddr,
2545118ebe8SLucas Mateus Castro (alqotel) int way, int is_code);
2555118ebe8SLucas Mateus Castro (alqotel)
256414fa2aaSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
257414fa2aaSPhilippe Mathieu-Daudé
2585118ebe8SLucas Mateus Castro (alqotel) /* Common routines used by software and hardware TLBs emulation */
pte_is_valid(target_ulong pte0)2595118ebe8SLucas Mateus Castro (alqotel) static inline int pte_is_valid(target_ulong pte0)
2605118ebe8SLucas Mateus Castro (alqotel) {
2615118ebe8SLucas Mateus Castro (alqotel) return pte0 & 0x80000000 ? 1 : 0;
2625118ebe8SLucas Mateus Castro (alqotel) }
2635118ebe8SLucas Mateus Castro (alqotel)
pte_invalidate(target_ulong * pte0)2645118ebe8SLucas Mateus Castro (alqotel) static inline void pte_invalidate(target_ulong *pte0)
2655118ebe8SLucas Mateus Castro (alqotel) {
2665118ebe8SLucas Mateus Castro (alqotel) *pte0 &= ~0x80000000;
2675118ebe8SLucas Mateus Castro (alqotel) }
2685118ebe8SLucas Mateus Castro (alqotel)
2695118ebe8SLucas Mateus Castro (alqotel) #define PTE_PTEM_MASK 0x7FFFFFBF
2705118ebe8SLucas Mateus Castro (alqotel) #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
2715118ebe8SLucas Mateus Castro (alqotel)
272215b2ee8SPhilippe Mathieu-Daudé uint32_t ppc_ldl_code(CPUArchState *env, target_ulong addr);
273215b2ee8SPhilippe Mathieu-Daudé
2741db8af5cSRichard Henderson #ifdef CONFIG_USER_ONLY
2751db8af5cSRichard Henderson void ppc_cpu_record_sigsegv(CPUState *cs, vaddr addr,
2761db8af5cSRichard Henderson MMUAccessType access_type,
2771db8af5cSRichard Henderson bool maperr, uintptr_t ra);
2781db8af5cSRichard Henderson #else
2791db8af5cSRichard Henderson bool ppc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
2801db8af5cSRichard Henderson MMUAccessType access_type, int mmu_idx,
2811db8af5cSRichard Henderson bool probe, uintptr_t retaddr);
2828905770bSMarc-André Lureau G_NORETURN void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
283996473e4SRichard Henderson MMUAccessType access_type, int mmu_idx,
2848905770bSMarc-André Lureau uintptr_t retaddr);
28555a7fa34SNicholas Piggin void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
28655a7fa34SNicholas Piggin vaddr addr, unsigned size,
28755a7fa34SNicholas Piggin MMUAccessType access_type,
28855a7fa34SNicholas Piggin int mmu_idx, MemTxAttrs attrs,
28955a7fa34SNicholas Piggin MemTxResult response, uintptr_t retaddr);
29014192307SNicholas Piggin void ppc_cpu_debug_excp_handler(CPUState *cs);
29114192307SNicholas Piggin bool ppc_cpu_debug_check_breakpoint(CPUState *cs);
292d5ee641cSNicholas Piggin bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
2932f96c00bSPhilippe Mathieu-Daudé
2942f96c00bSPhilippe Mathieu-Daudé G_NORETURN void powerpc_checkstop(CPUPPCState *env, const char *reason);
29592c787deSPhilippe Mathieu-Daudé void powerpc_excp(PowerPCCPU *cpu, int excp);
29692c787deSPhilippe Mathieu-Daudé
2972f96c00bSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2985118ebe8SLucas Mateus Castro (alqotel)
29934553153SLucas Mateus Castro (alqotel) FIELD(GER_MSK, XMSK, 0, 4)
30034553153SLucas Mateus Castro (alqotel) FIELD(GER_MSK, YMSK, 4, 4)
30134553153SLucas Mateus Castro (alqotel) FIELD(GER_MSK, PMSK, 8, 8)
30234553153SLucas Mateus Castro (alqotel)
ger_pack_masks(int pmsk,int ymsk,int xmsk)30334553153SLucas Mateus Castro (alqotel) static inline int ger_pack_masks(int pmsk, int ymsk, int xmsk)
30434553153SLucas Mateus Castro (alqotel) {
30534553153SLucas Mateus Castro (alqotel) int msk = 0;
30634553153SLucas Mateus Castro (alqotel) msk = FIELD_DP32(msk, GER_MSK, XMSK, xmsk);
30734553153SLucas Mateus Castro (alqotel) msk = FIELD_DP32(msk, GER_MSK, YMSK, ymsk);
30834553153SLucas Mateus Castro (alqotel) msk = FIELD_DP32(msk, GER_MSK, PMSK, pmsk);
30934553153SLucas Mateus Castro (alqotel) return msk;
31034553153SLucas Mateus Castro (alqotel) }
31134553153SLucas Mateus Castro (alqotel)
312*c37f8978SRichard Henderson TCGTBCPUState ppc_get_tb_cpu_state(CPUState *cs);
313*c37f8978SRichard Henderson
3143e00884fSGautham R. Shenoy #endif /* PPC_INTERNAL_H */
315