1fcf5ef2aSThomas Huth /*
2136fbf65Szhaolichang * PowerPC internal definitions for qemu.
3fcf5ef2aSThomas Huth *
4fcf5ef2aSThomas Huth * This library is free software; you can redistribute it and/or
5fcf5ef2aSThomas Huth * modify it under the terms of the GNU Lesser General Public
6fcf5ef2aSThomas Huth * License as published by the Free Software Foundation; either
76bd039cdSChetan Pant * version 2.1 of the License, or (at your option) any later version.
8fcf5ef2aSThomas Huth *
9fcf5ef2aSThomas Huth * This library is distributed in the hope that it will be useful,
10fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of
11fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12fcf5ef2aSThomas Huth * Lesser General Public License for more details.
13fcf5ef2aSThomas Huth *
14fcf5ef2aSThomas Huth * You should have received a copy of the GNU Lesser General Public
15fcf5ef2aSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16fcf5ef2aSThomas Huth */
17fcf5ef2aSThomas Huth
18fcf5ef2aSThomas Huth #ifndef PPC_INTERNAL_H
19fcf5ef2aSThomas Huth #define PPC_INTERNAL_H
20fcf5ef2aSThomas Huth
216ce1c9d0SPhilippe Mathieu-Daudé #include "exec/breakpoint.h"
2234553153SLucas Mateus Castro (alqotel) #include "hw/registerfields.h"
2374781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
2434553153SLucas Mateus Castro (alqotel)
251978a41bSPhilippe Mathieu-Daudé /* PM instructions */
261978a41bSPhilippe Mathieu-Daudé typedef enum {
271978a41bSPhilippe Mathieu-Daudé PPC_PM_DOZE,
281978a41bSPhilippe Mathieu-Daudé PPC_PM_NAP,
291978a41bSPhilippe Mathieu-Daudé PPC_PM_SLEEP,
301978a41bSPhilippe Mathieu-Daudé PPC_PM_RVWINKLE,
311978a41bSPhilippe Mathieu-Daudé PPC_PM_STOP,
321978a41bSPhilippe Mathieu-Daudé } powerpc_pm_insn_t;
331978a41bSPhilippe Mathieu-Daudé
34fcf5ef2aSThomas Huth #define FUNC_MASK(name, ret_type, size, max_val) \
35fcf5ef2aSThomas Huth static inline ret_type name(uint##size##_t start, \
36fcf5ef2aSThomas Huth uint##size##_t end) \
37fcf5ef2aSThomas Huth { \
38fcf5ef2aSThomas Huth ret_type ret, max_bit = size - 1; \
39fcf5ef2aSThomas Huth \
40fcf5ef2aSThomas Huth if (likely(start == 0)) { \
41fcf5ef2aSThomas Huth ret = max_val << (max_bit - end); \
42fcf5ef2aSThomas Huth } else if (likely(end == max_bit)) { \
43fcf5ef2aSThomas Huth ret = max_val >> start; \
44fcf5ef2aSThomas Huth } else { \
45fcf5ef2aSThomas Huth ret = (((uint##size##_t)(-1ULL)) >> (start)) ^ \
46fcf5ef2aSThomas Huth (((uint##size##_t)(-1ULL) >> (end)) >> 1); \
47fcf5ef2aSThomas Huth if (unlikely(start > end)) { \
48fcf5ef2aSThomas Huth return ~ret; \
49fcf5ef2aSThomas Huth } \
50fcf5ef2aSThomas Huth } \
51fcf5ef2aSThomas Huth \
52fcf5ef2aSThomas Huth return ret; \
53fcf5ef2aSThomas Huth }
54fcf5ef2aSThomas Huth
55fcf5ef2aSThomas Huth #if defined(TARGET_PPC64)
56fcf5ef2aSThomas Huth FUNC_MASK(MASK, target_ulong, 64, UINT64_MAX);
57fcf5ef2aSThomas Huth #else
58fcf5ef2aSThomas Huth FUNC_MASK(MASK, target_ulong, 32, UINT32_MAX);
59fcf5ef2aSThomas Huth #endif
60fcf5ef2aSThomas Huth FUNC_MASK(mask_u32, uint32_t, 32, UINT32_MAX);
61fcf5ef2aSThomas Huth FUNC_MASK(mask_u64, uint64_t, 64, UINT64_MAX);
62fcf5ef2aSThomas Huth
63985e3023SBharata B Rao /*****************************************************************************/
64985e3023SBharata B Rao /*** Instruction decoding ***/
65985e3023SBharata B Rao #define EXTRACT_HELPER(name, shift, nb) \
66985e3023SBharata B Rao static inline uint32_t name(uint32_t opcode) \
67985e3023SBharata B Rao { \
684c23c2a5SMark Cave-Ayland return extract32(opcode, shift, nb); \
69985e3023SBharata B Rao }
70985e3023SBharata B Rao
71985e3023SBharata B Rao #define EXTRACT_SHELPER(name, shift, nb) \
72985e3023SBharata B Rao static inline int32_t name(uint32_t opcode) \
73985e3023SBharata B Rao { \
744c23c2a5SMark Cave-Ayland return sextract32(opcode, shift, nb); \
75985e3023SBharata B Rao }
76985e3023SBharata B Rao
77985e3023SBharata B Rao #define EXTRACT_HELPER_SPLIT(name, shift1, nb1, shift2, nb2) \
78985e3023SBharata B Rao static inline uint32_t name(uint32_t opcode) \
79985e3023SBharata B Rao { \
804c23c2a5SMark Cave-Ayland return extract32(opcode, shift1, nb1) << nb2 | \
814c23c2a5SMark Cave-Ayland extract32(opcode, shift2, nb2); \
82985e3023SBharata B Rao }
83985e3023SBharata B Rao
84403a884aSNikunj A Dadhania #define EXTRACT_HELPER_SPLIT_3(name, \
85985e3023SBharata B Rao d0_bits, shift_op_d0, shift_d0, \
86985e3023SBharata B Rao d1_bits, shift_op_d1, shift_d1, \
87985e3023SBharata B Rao d2_bits, shift_op_d2, shift_d2) \
88985e3023SBharata B Rao static inline int16_t name(uint32_t opcode) \
89985e3023SBharata B Rao { \
90985e3023SBharata B Rao return \
91985e3023SBharata B Rao (((opcode >> (shift_op_d0)) & ((1 << (d0_bits)) - 1)) << (shift_d0)) | \
92985e3023SBharata B Rao (((opcode >> (shift_op_d1)) & ((1 << (d1_bits)) - 1)) << (shift_d1)) | \
93985e3023SBharata B Rao (((opcode >> (shift_op_d2)) & ((1 << (d2_bits)) - 1)) << (shift_d2)); \
94985e3023SBharata B Rao }
95985e3023SBharata B Rao
96985e3023SBharata B Rao
97985e3023SBharata B Rao /* Opcode part 1 */
98985e3023SBharata B Rao EXTRACT_HELPER(opc1, 26, 6);
99985e3023SBharata B Rao /* Opcode part 2 */
100985e3023SBharata B Rao EXTRACT_HELPER(opc2, 1, 5);
101985e3023SBharata B Rao /* Opcode part 3 */
102985e3023SBharata B Rao EXTRACT_HELPER(opc3, 6, 5);
103985e3023SBharata B Rao /* Opcode part 4 */
104985e3023SBharata B Rao EXTRACT_HELPER(opc4, 16, 5);
105985e3023SBharata B Rao /* Update Cr0 flags */
106985e3023SBharata B Rao EXTRACT_HELPER(Rc, 0, 1);
107985e3023SBharata B Rao /* Update Cr6 flags (Altivec) */
108985e3023SBharata B Rao EXTRACT_HELPER(Rc21, 10, 1);
109985e3023SBharata B Rao /* Destination */
110985e3023SBharata B Rao EXTRACT_HELPER(rD, 21, 5);
111985e3023SBharata B Rao /* Source */
112985e3023SBharata B Rao EXTRACT_HELPER(rS, 21, 5);
113985e3023SBharata B Rao /* First operand */
114985e3023SBharata B Rao EXTRACT_HELPER(rA, 16, 5);
115985e3023SBharata B Rao /* Second operand */
116985e3023SBharata B Rao EXTRACT_HELPER(rB, 11, 5);
117985e3023SBharata B Rao /* Third operand */
118985e3023SBharata B Rao EXTRACT_HELPER(rC, 6, 5);
119985e3023SBharata B Rao /*** Get CRn ***/
120985e3023SBharata B Rao EXTRACT_HELPER(crfD, 23, 3);
121985e3023SBharata B Rao EXTRACT_HELPER(BF, 23, 3);
122985e3023SBharata B Rao EXTRACT_HELPER(crfS, 18, 3);
123985e3023SBharata B Rao EXTRACT_HELPER(crbD, 21, 5);
124985e3023SBharata B Rao EXTRACT_HELPER(crbA, 16, 5);
125985e3023SBharata B Rao EXTRACT_HELPER(crbB, 11, 5);
126985e3023SBharata B Rao /* SPR / TBL */
127985e3023SBharata B Rao EXTRACT_HELPER(_SPR, 11, 10);
SPR(uint32_t opcode)128985e3023SBharata B Rao static inline uint32_t SPR(uint32_t opcode)
129985e3023SBharata B Rao {
130985e3023SBharata B Rao uint32_t sprn = _SPR(opcode);
131985e3023SBharata B Rao
132985e3023SBharata B Rao return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
133985e3023SBharata B Rao }
134985e3023SBharata B Rao /*** Get constants ***/
135985e3023SBharata B Rao /* 16 bits signed immediate value */
136985e3023SBharata B Rao EXTRACT_SHELPER(SIMM, 0, 16);
137985e3023SBharata B Rao /* 16 bits unsigned immediate value */
138985e3023SBharata B Rao EXTRACT_HELPER(UIMM, 0, 16);
139985e3023SBharata B Rao /* 5 bits signed immediate value */
140ffcd21acSMark Cave-Ayland EXTRACT_SHELPER(SIMM5, 16, 5);
141985e3023SBharata B Rao /* 5 bits signed immediate value */
142985e3023SBharata B Rao EXTRACT_HELPER(UIMM5, 16, 5);
143985e3023SBharata B Rao /* 4 bits unsigned immediate value */
144985e3023SBharata B Rao EXTRACT_HELPER(UIMM4, 16, 4);
145985e3023SBharata B Rao /* Bit count */
146985e3023SBharata B Rao EXTRACT_HELPER(NB, 11, 5);
147985e3023SBharata B Rao /* Shift count */
148985e3023SBharata B Rao EXTRACT_HELPER(SH, 11, 5);
149a68a6146SBalamuruhan S /* lwat/stwat/ldat/lwat */
150a68a6146SBalamuruhan S EXTRACT_HELPER(FC, 11, 5);
151985e3023SBharata B Rao /* Vector shift count */
152985e3023SBharata B Rao EXTRACT_HELPER(VSH, 6, 4);
153985e3023SBharata B Rao /* Mask start */
154985e3023SBharata B Rao EXTRACT_HELPER(MB, 6, 5);
155985e3023SBharata B Rao /* Mask end */
156985e3023SBharata B Rao EXTRACT_HELPER(ME, 1, 5);
157985e3023SBharata B Rao /* Trap operand */
158985e3023SBharata B Rao EXTRACT_HELPER(TO, 21, 5);
159985e3023SBharata B Rao
160985e3023SBharata B Rao EXTRACT_HELPER(CRM, 12, 8);
161985e3023SBharata B Rao
162985e3023SBharata B Rao #ifndef CONFIG_USER_ONLY
163985e3023SBharata B Rao EXTRACT_HELPER(SR, 16, 4);
164985e3023SBharata B Rao #endif
165985e3023SBharata B Rao
166985e3023SBharata B Rao /* mtfsf/mtfsfi */
167985e3023SBharata B Rao EXTRACT_HELPER(FPBF, 23, 3);
168985e3023SBharata B Rao EXTRACT_HELPER(FPIMM, 12, 4);
169985e3023SBharata B Rao EXTRACT_HELPER(FPL, 25, 1);
170985e3023SBharata B Rao EXTRACT_HELPER(FPFLM, 17, 8);
171985e3023SBharata B Rao EXTRACT_HELPER(FPW, 16, 1);
172985e3023SBharata B Rao
173985e3023SBharata B Rao /* addpcis */
174403a884aSNikunj A Dadhania EXTRACT_HELPER_SPLIT_3(DX, 10, 6, 6, 5, 16, 1, 1, 0, 0)
175985e3023SBharata B Rao #if defined(TARGET_PPC64)
176985e3023SBharata B Rao /* darn */
177985e3023SBharata B Rao EXTRACT_HELPER(L, 16, 2);
178985e3023SBharata B Rao #endif
1790c9717ffSNicholas Piggin /* wait */
1800c9717ffSNicholas Piggin EXTRACT_HELPER(WC, 21, 2);
1810c9717ffSNicholas Piggin EXTRACT_HELPER(PL, 16, 2);
182985e3023SBharata B Rao
183985e3023SBharata B Rao /*** Jump target decoding ***/
184985e3023SBharata B Rao /* Immediate address */
LI(uint32_t opcode)185985e3023SBharata B Rao static inline target_ulong LI(uint32_t opcode)
186985e3023SBharata B Rao {
187985e3023SBharata B Rao return (opcode >> 0) & 0x03FFFFFC;
188985e3023SBharata B Rao }
189985e3023SBharata B Rao
BD(uint32_t opcode)190985e3023SBharata B Rao static inline uint32_t BD(uint32_t opcode)
191985e3023SBharata B Rao {
192985e3023SBharata B Rao return (opcode >> 0) & 0xFFFC;
193985e3023SBharata B Rao }
194985e3023SBharata B Rao
195985e3023SBharata B Rao EXTRACT_HELPER(BO, 21, 5);
196985e3023SBharata B Rao EXTRACT_HELPER(BI, 16, 5);
197985e3023SBharata B Rao /* Absolute/relative address */
198985e3023SBharata B Rao EXTRACT_HELPER(AA, 1, 1);
199985e3023SBharata B Rao /* Link */
200985e3023SBharata B Rao EXTRACT_HELPER(LK, 0, 1);
201985e3023SBharata B Rao
202985e3023SBharata B Rao /* DFP Z22-form */
203985e3023SBharata B Rao EXTRACT_HELPER(DCM, 10, 6)
204985e3023SBharata B Rao
205985e3023SBharata B Rao /* DFP Z23-form */
206985e3023SBharata B Rao EXTRACT_HELPER(RMC, 9, 2)
207be07ad58SJose Ricardo Ziviani EXTRACT_HELPER(Rrm, 16, 1)
208985e3023SBharata B Rao
209d59ba583SNikunj A Dadhania EXTRACT_HELPER_SPLIT(DQxT, 3, 1, 21, 5);
210985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
211985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
212985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
213985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
214985e3023SBharata B Rao EXTRACT_HELPER_SPLIT(xC, 3, 1, 6, 5);
215985e3023SBharata B Rao EXTRACT_HELPER(DM, 8, 2);
216985e3023SBharata B Rao EXTRACT_HELPER(UIM, 16, 2);
217985e3023SBharata B Rao EXTRACT_HELPER(SHW, 8, 2);
218985e3023SBharata B Rao EXTRACT_HELPER(SP, 19, 2);
219985e3023SBharata B Rao EXTRACT_HELPER(IMM8, 11, 8);
22078241762SNikunj A Dadhania EXTRACT_HELPER(DCMX, 16, 7);
221403a884aSNikunj A Dadhania EXTRACT_HELPER_SPLIT_3(DCMX_XV, 5, 16, 0, 1, 2, 5, 1, 6, 6);
222985e3023SBharata B Rao
223f566c047SBharata B Rao void helper_compute_fprf_float16(CPUPPCState *env, float16 arg);
2249aeae8e1SBharata B Rao void helper_compute_fprf_float32(CPUPPCState *env, float32 arg);
22507bdd247SBharata B Rao void helper_compute_fprf_float128(CPUPPCState *env, float128 arg);
2260f3110faSRichard Henderson
2277468e2c8SBruno Larsen (billionai) /* translate.c */
2287468e2c8SBruno Larsen (billionai)
2297468e2c8SBruno Larsen (billionai) int ppc_fixup_cpu(PowerPCCPU *cpu);
2307468e2c8SBruno Larsen (billionai) void create_ppc_opcodes(PowerPCCPU *cpu, Error **errp);
2317468e2c8SBruno Larsen (billionai) void destroy_ppc_opcodes(PowerPCCPU *cpu);
2327468e2c8SBruno Larsen (billionai)
23335a5d74eSBruno Larsen (billionai) /* gdbstub.c */
23435a5d74eSBruno Larsen (billionai) void ppc_gdb_init(CPUState *cs, PowerPCCPUClass *ppc);
235a6506838SAkihiko Odaki const gchar *ppc_gdb_arch_name(CPUState *cs);
23635a5d74eSBruno Larsen (billionai)
237414fa2aaSPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
238414fa2aaSPhilippe Mathieu-Daudé
239*cd1038ecSBALATON Zoltan /* Check if permission bit required for the access_type is set in prot */
check_prot_access_type(int prot,MMUAccessType access_type)240*cd1038ecSBALATON Zoltan static inline int check_prot_access_type(int prot, MMUAccessType access_type)
241*cd1038ecSBALATON Zoltan {
242*cd1038ecSBALATON Zoltan return prot & (1 << access_type);
243*cd1038ecSBALATON Zoltan }
244*cd1038ecSBALATON Zoltan
2455118ebe8SLucas Mateus Castro (alqotel) /* PowerPC MMU emulation */
2465118ebe8SLucas Mateus Castro (alqotel)
2475118ebe8SLucas Mateus Castro (alqotel) bool ppc_xlate(PowerPCCPU *cpu, vaddr eaddr, MMUAccessType access_type,
2485118ebe8SLucas Mateus Castro (alqotel) hwaddr *raddrp, int *psizep, int *protp,
2495118ebe8SLucas Mateus Castro (alqotel) int mmu_idx, bool guest_visible);
2506b9ea7f3SBALATON Zoltan
2515118ebe8SLucas Mateus Castro (alqotel) /* Software driven TLB helpers */
2525118ebe8SLucas Mateus Castro (alqotel) int ppc6xx_tlb_getnum(CPUPPCState *env, target_ulong eaddr,
2535118ebe8SLucas Mateus Castro (alqotel) int way, int is_code);
2545118ebe8SLucas Mateus Castro (alqotel)
255414fa2aaSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
256414fa2aaSPhilippe Mathieu-Daudé
2575118ebe8SLucas Mateus Castro (alqotel) /* Common routines used by software and hardware TLBs emulation */
pte_is_valid(target_ulong pte0)2585118ebe8SLucas Mateus Castro (alqotel) static inline int pte_is_valid(target_ulong pte0)
2595118ebe8SLucas Mateus Castro (alqotel) {
2605118ebe8SLucas Mateus Castro (alqotel) return pte0 & 0x80000000 ? 1 : 0;
2615118ebe8SLucas Mateus Castro (alqotel) }
2625118ebe8SLucas Mateus Castro (alqotel)
pte_invalidate(target_ulong * pte0)2635118ebe8SLucas Mateus Castro (alqotel) static inline void pte_invalidate(target_ulong *pte0)
2645118ebe8SLucas Mateus Castro (alqotel) {
2655118ebe8SLucas Mateus Castro (alqotel) *pte0 &= ~0x80000000;
2665118ebe8SLucas Mateus Castro (alqotel) }
2675118ebe8SLucas Mateus Castro (alqotel)
2685118ebe8SLucas Mateus Castro (alqotel) #define PTE_PTEM_MASK 0x7FFFFFBF
2695118ebe8SLucas Mateus Castro (alqotel) #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
2705118ebe8SLucas Mateus Castro (alqotel)
2711db8af5cSRichard Henderson #ifdef CONFIG_USER_ONLY
2721db8af5cSRichard Henderson void ppc_cpu_record_sigsegv(CPUState *cs, vaddr addr,
2731db8af5cSRichard Henderson MMUAccessType access_type,
2741db8af5cSRichard Henderson bool maperr, uintptr_t ra);
2751db8af5cSRichard Henderson #else
2761db8af5cSRichard Henderson bool ppc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
2771db8af5cSRichard Henderson MMUAccessType access_type, int mmu_idx,
2781db8af5cSRichard Henderson bool probe, uintptr_t retaddr);
2798905770bSMarc-André Lureau G_NORETURN void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
280996473e4SRichard Henderson MMUAccessType access_type, int mmu_idx,
2818905770bSMarc-André Lureau uintptr_t retaddr);
28255a7fa34SNicholas Piggin void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
28355a7fa34SNicholas Piggin vaddr addr, unsigned size,
28455a7fa34SNicholas Piggin MMUAccessType access_type,
28555a7fa34SNicholas Piggin int mmu_idx, MemTxAttrs attrs,
28655a7fa34SNicholas Piggin MemTxResult response, uintptr_t retaddr);
28714192307SNicholas Piggin void ppc_cpu_debug_excp_handler(CPUState *cs);
28814192307SNicholas Piggin bool ppc_cpu_debug_check_breakpoint(CPUState *cs);
289d5ee641cSNicholas Piggin bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
2901db8af5cSRichard Henderson #endif
2915118ebe8SLucas Mateus Castro (alqotel)
29234553153SLucas Mateus Castro (alqotel) FIELD(GER_MSK, XMSK, 0, 4)
29334553153SLucas Mateus Castro (alqotel) FIELD(GER_MSK, YMSK, 4, 4)
29434553153SLucas Mateus Castro (alqotel) FIELD(GER_MSK, PMSK, 8, 8)
29534553153SLucas Mateus Castro (alqotel)
ger_pack_masks(int pmsk,int ymsk,int xmsk)29634553153SLucas Mateus Castro (alqotel) static inline int ger_pack_masks(int pmsk, int ymsk, int xmsk)
29734553153SLucas Mateus Castro (alqotel) {
29834553153SLucas Mateus Castro (alqotel) int msk = 0;
29934553153SLucas Mateus Castro (alqotel) msk = FIELD_DP32(msk, GER_MSK, XMSK, xmsk);
30034553153SLucas Mateus Castro (alqotel) msk = FIELD_DP32(msk, GER_MSK, YMSK, ymsk);
30134553153SLucas Mateus Castro (alqotel) msk = FIELD_DP32(msk, GER_MSK, PMSK, pmsk);
30234553153SLucas Mateus Castro (alqotel) return msk;
30334553153SLucas Mateus Castro (alqotel) }
30434553153SLucas Mateus Castro (alqotel)
305fcf5ef2aSThomas Huth #endif /* PPC_INTERNAL_H */
306