powerpc/32: get rid of COPY_EE in exception entry
[linux-2.6-block.git] / arch / powerpc / kernel / head_booke.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __HEAD_BOOKE_H__
3 #define __HEAD_BOOKE_H__
4
5 #include <asm/ptrace.h> /* for STACK_FRAME_REGS_MARKER */
6 #include <asm/kvm_asm.h>
7 #include <asm/kvm_booke_hv_asm.h>
8
9 /*
10  * Macros used for common Book-e exception handling
11  */
12
13 #define SET_IVOR(vector_number, vector_label)           \
14                 li      r26,vector_label@l;             \
15                 mtspr   SPRN_IVOR##vector_number,r26;   \
16                 sync
17
18 #if (THREAD_SHIFT < 15)
19 #define ALLOC_STACK_FRAME(reg, val)                     \
20         addi reg,reg,val
21 #else
22 #define ALLOC_STACK_FRAME(reg, val)                     \
23         addis   reg,reg,val@ha;                         \
24         addi    reg,reg,val@l
25 #endif
26
27 /*
28  * Macro used to get to thread save registers.
29  * Note that entries 0-3 are used for the prolog code, and the remaining
30  * entries are available for specific exception use in the event a handler
31  * requires more than 4 scratch registers.
32  */
33 #define THREAD_NORMSAVE(offset) (THREAD_NORMSAVES + (offset * 4))
34
35 #ifdef CONFIG_PPC_FSL_BOOK3E
36 #define BOOKE_CLEAR_BTB(reg)                                                                    \
37 START_BTB_FLUSH_SECTION                                                         \
38         BTB_FLUSH(reg)                                                                  \
39 END_BTB_FLUSH_SECTION
40 #else
41 #define BOOKE_CLEAR_BTB(reg)
42 #endif
43
44
45 #define NORMAL_EXCEPTION_PROLOG(intno)                                               \
46         mtspr   SPRN_SPRG_WSCRATCH0, r10;       /* save one register */      \
47         mfspr   r10, SPRN_SPRG_THREAD;                                       \
48         stw     r11, THREAD_NORMSAVE(0)(r10);                                \
49         stw     r13, THREAD_NORMSAVE(2)(r10);                                \
50         mfcr    r13;                    /* save CR in r13 for now          */\
51         mfspr   r11, SPRN_SRR1;                                              \
52         DO_KVM  BOOKE_INTERRUPT_##intno SPRN_SRR1;                           \
53         andi.   r11, r11, MSR_PR;       /* check whether user or kernel    */\
54         mr      r11, r1;                                                     \
55         beq     1f;                                                          \
56         BOOKE_CLEAR_BTB(r11)                                            \
57         /* if from user, start at top of this thread's kernel stack */       \
58         lwz     r11, TASK_STACK - THREAD(r10);                               \
59         ALLOC_STACK_FRAME(r11, THREAD_SIZE);                                 \
60 1 :     subi    r11, r11, INT_FRAME_SIZE; /* Allocate exception frame */     \
61         stw     r13, _CCR(r11);         /* save various registers */         \
62         stw     r12,GPR12(r11);                                              \
63         stw     r9,GPR9(r11);                                                \
64         mfspr   r13, SPRN_SPRG_RSCRATCH0;                                    \
65         stw     r13, GPR10(r11);                                             \
66         lwz     r12, THREAD_NORMSAVE(0)(r10);                                \
67         stw     r12,GPR11(r11);                                              \
68         lwz     r13, THREAD_NORMSAVE(2)(r10); /* restore r13 */              \
69         mflr    r10;                                                         \
70         stw     r10,_LINK(r11);                                              \
71         mfspr   r12,SPRN_SRR0;                                               \
72         stw     r1, GPR1(r11);                                               \
73         mfspr   r9,SPRN_SRR1;                                                \
74         stw     r1, 0(r11);                                                  \
75         mr      r1, r11;                                                     \
76         rlwinm  r9,r9,0,14,12;          /* clear MSR_WE (necessary?)       */\
77         stw     r0,GPR0(r11);                                                \
78         lis     r10, STACK_FRAME_REGS_MARKER@ha;/* exception frame marker */ \
79         addi    r10, r10, STACK_FRAME_REGS_MARKER@l;                         \
80         stw     r10, 8(r11);                                                 \
81         SAVE_4GPRS(3, r11);                                                  \
82         SAVE_2GPRS(7, r11)
83
84 /* To handle the additional exception priority levels on 40x and Book-E
85  * processors we allocate a stack per additional priority level.
86  *
87  * On 40x critical is the only additional level
88  * On 44x/e500 we have critical and machine check
89  * On e200 we have critical and debug (machine check occurs via critical)
90  *
91  * Additionally we reserve a SPRG for each priority level so we can free up a
92  * GPR to use as the base for indirect access to the exception stacks.  This
93  * is necessary since the MMU is always on, for Book-E parts, and the stacks
94  * are offset from KERNELBASE.
95  *
96  * There is some space optimization to be had here if desired.  However
97  * to allow for a common kernel with support for debug exceptions either
98  * going to critical or their own debug level we aren't currently
99  * providing configurations that micro-optimize space usage.
100  */
101
102 #define MC_STACK_BASE           mcheckirq_ctx
103 #define CRIT_STACK_BASE         critirq_ctx
104
105 /* only on e500mc/e200 */
106 #define DBG_STACK_BASE          dbgirq_ctx
107
108 #define EXC_LVL_FRAME_OVERHEAD  (THREAD_SIZE - INT_FRAME_SIZE - EXC_LVL_SIZE)
109
110 #ifdef CONFIG_SMP
111 #define BOOKE_LOAD_EXC_LEVEL_STACK(level)               \
112         mfspr   r8,SPRN_PIR;                            \
113         slwi    r8,r8,2;                                \
114         addis   r8,r8,level##_STACK_BASE@ha;            \
115         lwz     r8,level##_STACK_BASE@l(r8);            \
116         addi    r8,r8,EXC_LVL_FRAME_OVERHEAD;
117 #else
118 #define BOOKE_LOAD_EXC_LEVEL_STACK(level)               \
119         lis     r8,level##_STACK_BASE@ha;               \
120         lwz     r8,level##_STACK_BASE@l(r8);            \
121         addi    r8,r8,EXC_LVL_FRAME_OVERHEAD;
122 #endif
123
124 /*
125  * Exception prolog for critical/machine check exceptions.  This is a
126  * little different from the normal exception prolog above since a
127  * critical/machine check exception can potentially occur at any point
128  * during normal exception processing. Thus we cannot use the same SPRG
129  * registers as the normal prolog above. Instead we use a portion of the
130  * critical/machine check exception stack at low physical addresses.
131  */
132 #define EXC_LEVEL_EXCEPTION_PROLOG(exc_level, intno, exc_level_srr0, exc_level_srr1) \
133         mtspr   SPRN_SPRG_WSCRATCH_##exc_level,r8;                           \
134         BOOKE_LOAD_EXC_LEVEL_STACK(exc_level);/* r8 points to the exc_level stack*/ \
135         stw     r9,GPR9(r8);            /* save various registers          */\
136         mfcr    r9;                     /* save CR in r9 for now           */\
137         stw     r10,GPR10(r8);                                               \
138         stw     r11,GPR11(r8);                                               \
139         stw     r9,_CCR(r8);            /* save CR on stack                */\
140         mfspr   r11,exc_level_srr1;     /* check whether user or kernel    */\
141         DO_KVM  BOOKE_INTERRUPT_##intno exc_level_srr1;                      \
142         BOOKE_CLEAR_BTB(r10)                                            \
143         andi.   r11,r11,MSR_PR;                                              \
144         mfspr   r11,SPRN_SPRG_THREAD;   /* if from user, start at top of   */\
145         lwz     r11, TASK_STACK - THREAD(r11); /* this thread's kernel stack */\
146         addi    r11,r11,EXC_LVL_FRAME_OVERHEAD; /* allocate stack frame    */\
147         beq     1f;                                                          \
148         /* COMING FROM USER MODE */                                          \
149         stw     r9,_CCR(r11);           /* save CR                         */\
150         lwz     r10,GPR10(r8);          /* copy regs from exception stack  */\
151         lwz     r9,GPR9(r8);                                                 \
152         stw     r10,GPR10(r11);                                              \
153         lwz     r10,GPR11(r8);                                               \
154         stw     r9,GPR9(r11);                                                \
155         stw     r10,GPR11(r11);                                              \
156         b       2f;                                                          \
157         /* COMING FROM PRIV MODE */                                          \
158 1:      mr      r11, r8;                                                             \
159 2:      mfspr   r8,SPRN_SPRG_RSCRATCH_##exc_level;                           \
160         stw     r12,GPR12(r11);         /* save various registers          */\
161         mflr    r10;                                                         \
162         stw     r10,_LINK(r11);                                              \
163         mfspr   r12,SPRN_DEAR;          /* save DEAR and ESR in the frame  */\
164         stw     r12,_DEAR(r11);         /* since they may have had stuff   */\
165         mfspr   r9,SPRN_ESR;            /* in them at the point where the  */\
166         stw     r9,_ESR(r11);           /* exception was taken             */\
167         mfspr   r12,exc_level_srr0;                                          \
168         stw     r1,GPR1(r11);                                                \
169         mfspr   r9,exc_level_srr1;                                           \
170         stw     r1,0(r11);                                                   \
171         mr      r1,r11;                                                      \
172         rlwinm  r9,r9,0,14,12;          /* clear MSR_WE (necessary?)       */\
173         stw     r0,GPR0(r11);                                                \
174         SAVE_4GPRS(3, r11);                                                  \
175         SAVE_2GPRS(7, r11)
176
177 #define CRITICAL_EXCEPTION_PROLOG(intno) \
178                 EXC_LEVEL_EXCEPTION_PROLOG(CRIT, intno, SPRN_CSRR0, SPRN_CSRR1)
179 #define DEBUG_EXCEPTION_PROLOG \
180                 EXC_LEVEL_EXCEPTION_PROLOG(DBG, DEBUG, SPRN_DSRR0, SPRN_DSRR1)
181 #define MCHECK_EXCEPTION_PROLOG \
182                 EXC_LEVEL_EXCEPTION_PROLOG(MC, MACHINE_CHECK, \
183                         SPRN_MCSRR0, SPRN_MCSRR1)
184
185 /*
186  * Guest Doorbell -- this is a bit odd in that uses GSRR0/1 despite
187  * being delivered to the host.  This exception can only happen
188  * inside a KVM guest -- so we just handle up to the DO_KVM rather
189  * than try to fit this into one of the existing prolog macros.
190  */
191 #define GUEST_DOORBELL_EXCEPTION \
192         START_EXCEPTION(GuestDoorbell);                                      \
193         mtspr   SPRN_SPRG_WSCRATCH0, r10;       /* save one register */      \
194         mfspr   r10, SPRN_SPRG_THREAD;                                       \
195         stw     r11, THREAD_NORMSAVE(0)(r10);                                \
196         mfspr   r11, SPRN_SRR1;                                              \
197         stw     r13, THREAD_NORMSAVE(2)(r10);                                \
198         mfcr    r13;                    /* save CR in r13 for now          */\
199         DO_KVM  BOOKE_INTERRUPT_GUEST_DBELL SPRN_GSRR1;                      \
200         trap
201
202 /*
203  * Exception vectors.
204  */
205 #define START_EXCEPTION(label)                                               \
206         .align 5;                                                            \
207 label:
208
209 #define EXCEPTION(n, intno, label, hdlr, xfer)                  \
210         START_EXCEPTION(label);                                 \
211         NORMAL_EXCEPTION_PROLOG(intno);                         \
212         addi    r3,r1,STACK_FRAME_OVERHEAD;                     \
213         xfer(n, hdlr)
214
215 #define CRITICAL_EXCEPTION(n, intno, label, hdlr)                       \
216         START_EXCEPTION(label);                                         \
217         CRITICAL_EXCEPTION_PROLOG(intno);                               \
218         addi    r3,r1,STACK_FRAME_OVERHEAD;                             \
219         EXC_XFER_TEMPLATE(hdlr, n+2, (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), \
220                           crit_transfer_to_handler, ret_from_crit_exc)
221
222 #define MCHECK_EXCEPTION(n, label, hdlr)                        \
223         START_EXCEPTION(label);                                 \
224         MCHECK_EXCEPTION_PROLOG;                                \
225         mfspr   r5,SPRN_ESR;                                    \
226         stw     r5,_ESR(r11);                                   \
227         addi    r3,r1,STACK_FRAME_OVERHEAD;                     \
228         EXC_XFER_TEMPLATE(hdlr, n+4, (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), \
229                           mcheck_transfer_to_handler, ret_from_mcheck_exc)
230
231 #define EXC_XFER_TEMPLATE(hdlr, trap, msr, tfer, ret)   \
232         li      r10,trap;                                       \
233         stw     r10,_TRAP(r11);                                 \
234         lis     r10,msr@h;                                      \
235         ori     r10,r10,msr@l;                                  \
236         bl      tfer;                                           \
237         .long   hdlr;                                           \
238         .long   ret
239
240 #define EXC_XFER_STD(n, hdlr)           \
241         EXC_XFER_TEMPLATE(hdlr, n, MSR_KERNEL, transfer_to_handler_full, \
242                           ret_from_except_full)
243
244 #define EXC_XFER_LITE(n, hdlr)          \
245         EXC_XFER_TEMPLATE(hdlr, n+1, MSR_KERNEL, transfer_to_handler, \
246                           ret_from_except)
247
248 #define EXC_XFER_SYS(n, hdlr)                                           \
249         EXC_XFER_TEMPLATE(hdlr, n+1, MSR_KERNEL | MSR_EE, transfer_to_handler, \
250                           ret_from_except)
251
252 /* Check for a single step debug exception while in an exception
253  * handler before state has been saved.  This is to catch the case
254  * where an instruction that we are trying to single step causes
255  * an exception (eg ITLB/DTLB miss) and thus the first instruction of
256  * the exception handler generates a single step debug exception.
257  *
258  * If we get a debug trap on the first instruction of an exception handler,
259  * we reset the MSR_DE in the _exception handler's_ MSR (the debug trap is
260  * a critical exception, so we are using SPRN_CSRR1 to manipulate the MSR).
261  * The exception handler was handling a non-critical interrupt, so it will
262  * save (and later restore) the MSR via SPRN_CSRR1, which will still have
263  * the MSR_DE bit set.
264  */
265 #define DEBUG_DEBUG_EXCEPTION                                                 \
266         START_EXCEPTION(DebugDebug);                                          \
267         DEBUG_EXCEPTION_PROLOG;                                               \
268                                                                               \
269         /*                                                                    \
270          * If there is a single step or branch-taken exception in an          \
271          * exception entry sequence, it was probably meant to apply to        \
272          * the code where the exception occurred (since exception entry       \
273          * doesn't turn off DE automatically).  We simulate the effect        \
274          * of turning off DE on entry to an exception handler by turning      \
275          * off DE in the DSRR1 value and clearing the debug status.           \
276          */                                                                   \
277         mfspr   r10,SPRN_DBSR;          /* check single-step/branch taken */  \
278         andis.  r10,r10,(DBSR_IC|DBSR_BT)@h;                                  \
279         beq+    2f;                                                           \
280                                                                               \
281         lis     r10,interrupt_base@h;   /* check if exception in vectors */   \
282         ori     r10,r10,interrupt_base@l;                                     \
283         cmplw   r12,r10;                                                      \
284         blt+    2f;                     /* addr below exception vectors */    \
285                                                                               \
286         lis     r10,interrupt_end@h;                                          \
287         ori     r10,r10,interrupt_end@l;                                      \
288         cmplw   r12,r10;                                                      \
289         bgt+    2f;                     /* addr above exception vectors */    \
290                                                                               \
291         /* here it looks like we got an inappropriate debug exception. */     \
292 1:      rlwinm  r9,r9,0,~MSR_DE;        /* clear DE in the CDRR1 value */     \
293         lis     r10,(DBSR_IC|DBSR_BT)@h;        /* clear the IC event */      \
294         mtspr   SPRN_DBSR,r10;                                                \
295         /* restore state and get out */                                       \
296         lwz     r10,_CCR(r11);                                                \
297         lwz     r0,GPR0(r11);                                                 \
298         lwz     r1,GPR1(r11);                                                 \
299         mtcrf   0x80,r10;                                                     \
300         mtspr   SPRN_DSRR0,r12;                                               \
301         mtspr   SPRN_DSRR1,r9;                                                \
302         lwz     r9,GPR9(r11);                                                 \
303         lwz     r12,GPR12(r11);                                               \
304         mtspr   SPRN_SPRG_WSCRATCH_DBG,r8;                                    \
305         BOOKE_LOAD_EXC_LEVEL_STACK(DBG); /* r8 points to the debug stack */ \
306         lwz     r10,GPR10(r8);                                                \
307         lwz     r11,GPR11(r8);                                                \
308         mfspr   r8,SPRN_SPRG_RSCRATCH_DBG;                                    \
309                                                                               \
310         PPC_RFDI;                                                             \
311         b       .;                                                            \
312                                                                               \
313         /* continue normal handling for a debug exception... */               \
314 2:      mfspr   r4,SPRN_DBSR;                                                 \
315         addi    r3,r1,STACK_FRAME_OVERHEAD;                                   \
316         EXC_XFER_TEMPLATE(DebugException, 0x2008, (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), debug_transfer_to_handler, ret_from_debug_exc)
317
318 #define DEBUG_CRIT_EXCEPTION                                                  \
319         START_EXCEPTION(DebugCrit);                                           \
320         CRITICAL_EXCEPTION_PROLOG(DEBUG);                                     \
321                                                                               \
322         /*                                                                    \
323          * If there is a single step or branch-taken exception in an          \
324          * exception entry sequence, it was probably meant to apply to        \
325          * the code where the exception occurred (since exception entry       \
326          * doesn't turn off DE automatically).  We simulate the effect        \
327          * of turning off DE on entry to an exception handler by turning      \
328          * off DE in the CSRR1 value and clearing the debug status.           \
329          */                                                                   \
330         mfspr   r10,SPRN_DBSR;          /* check single-step/branch taken */  \
331         andis.  r10,r10,(DBSR_IC|DBSR_BT)@h;                                  \
332         beq+    2f;                                                           \
333                                                                               \
334         lis     r10,interrupt_base@h;   /* check if exception in vectors */   \
335         ori     r10,r10,interrupt_base@l;                                     \
336         cmplw   r12,r10;                                                      \
337         blt+    2f;                     /* addr below exception vectors */    \
338                                                                               \
339         lis     r10,interrupt_end@h;                                          \
340         ori     r10,r10,interrupt_end@l;                                      \
341         cmplw   r12,r10;                                                      \
342         bgt+    2f;                     /* addr above exception vectors */    \
343                                                                               \
344         /* here it looks like we got an inappropriate debug exception. */     \
345 1:      rlwinm  r9,r9,0,~MSR_DE;        /* clear DE in the CSRR1 value */     \
346         lis     r10,(DBSR_IC|DBSR_BT)@h;        /* clear the IC event */      \
347         mtspr   SPRN_DBSR,r10;                                                \
348         /* restore state and get out */                                       \
349         lwz     r10,_CCR(r11);                                                \
350         lwz     r0,GPR0(r11);                                                 \
351         lwz     r1,GPR1(r11);                                                 \
352         mtcrf   0x80,r10;                                                     \
353         mtspr   SPRN_CSRR0,r12;                                               \
354         mtspr   SPRN_CSRR1,r9;                                                \
355         lwz     r9,GPR9(r11);                                                 \
356         lwz     r12,GPR12(r11);                                               \
357         mtspr   SPRN_SPRG_WSCRATCH_CRIT,r8;                                   \
358         BOOKE_LOAD_EXC_LEVEL_STACK(CRIT); /* r8 points to the debug stack */  \
359         lwz     r10,GPR10(r8);                                                \
360         lwz     r11,GPR11(r8);                                                \
361         mfspr   r8,SPRN_SPRG_RSCRATCH_CRIT;                                   \
362                                                                               \
363         rfci;                                                                 \
364         b       .;                                                            \
365                                                                               \
366         /* continue normal handling for a critical exception... */            \
367 2:      mfspr   r4,SPRN_DBSR;                                                 \
368         addi    r3,r1,STACK_FRAME_OVERHEAD;                                   \
369         EXC_XFER_TEMPLATE(DebugException, 0x2002, (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), crit_transfer_to_handler, ret_from_crit_exc)
370
371 #define DATA_STORAGE_EXCEPTION                                                \
372         START_EXCEPTION(DataStorage)                                          \
373         NORMAL_EXCEPTION_PROLOG(DATA_STORAGE);                \
374         mfspr   r5,SPRN_ESR;            /* Grab the ESR and save it */        \
375         stw     r5,_ESR(r11);                                                 \
376         mfspr   r4,SPRN_DEAR;           /* Grab the DEAR */                   \
377         EXC_XFER_LITE(0x0300, handle_page_fault)
378
379 #define INSTRUCTION_STORAGE_EXCEPTION                                         \
380         START_EXCEPTION(InstructionStorage)                                   \
381         NORMAL_EXCEPTION_PROLOG(INST_STORAGE);                \
382         mfspr   r5,SPRN_ESR;            /* Grab the ESR and save it */        \
383         stw     r5,_ESR(r11);                                                 \
384         mr      r4,r12;                 /* Pass SRR0 as arg2 */               \
385         li      r5,0;                   /* Pass zero as arg3 */               \
386         EXC_XFER_LITE(0x0400, handle_page_fault)
387
388 #define ALIGNMENT_EXCEPTION                                                   \
389         START_EXCEPTION(Alignment)                                            \
390         NORMAL_EXCEPTION_PROLOG(ALIGNMENT);                   \
391         mfspr   r4,SPRN_DEAR;           /* Grab the DEAR and save it */       \
392         stw     r4,_DEAR(r11);                                                \
393         addi    r3,r1,STACK_FRAME_OVERHEAD;                                   \
394         EXC_XFER_STD(0x0600, alignment_exception)
395
396 #define PROGRAM_EXCEPTION                                                     \
397         START_EXCEPTION(Program)                                              \
398         NORMAL_EXCEPTION_PROLOG(PROGRAM);                     \
399         mfspr   r4,SPRN_ESR;            /* Grab the ESR and save it */        \
400         stw     r4,_ESR(r11);                                                 \
401         addi    r3,r1,STACK_FRAME_OVERHEAD;                                   \
402         EXC_XFER_STD(0x0700, program_check_exception)
403
404 #define DECREMENTER_EXCEPTION                                                 \
405         START_EXCEPTION(Decrementer)                                          \
406         NORMAL_EXCEPTION_PROLOG(DECREMENTER);                 \
407         lis     r0,TSR_DIS@h;           /* Setup the DEC interrupt mask */    \
408         mtspr   SPRN_TSR,r0;            /* Clear the DEC interrupt */         \
409         addi    r3,r1,STACK_FRAME_OVERHEAD;                                   \
410         EXC_XFER_LITE(0x0900, timer_interrupt)
411
412 #define FP_UNAVAILABLE_EXCEPTION                                              \
413         START_EXCEPTION(FloatingPointUnavailable)                             \
414         NORMAL_EXCEPTION_PROLOG(FP_UNAVAIL);                  \
415         beq     1f;                                                           \
416         bl      load_up_fpu;            /* if from user, just load it up */   \
417         b       fast_exception_return;                                        \
418 1:      addi    r3,r1,STACK_FRAME_OVERHEAD;                                   \
419         EXC_XFER_STD(0x800, kernel_fp_unavailable_exception)
420
421 #ifndef __ASSEMBLY__
422 struct exception_regs {
423         unsigned long mas0;
424         unsigned long mas1;
425         unsigned long mas2;
426         unsigned long mas3;
427         unsigned long mas6;
428         unsigned long mas7;
429         unsigned long srr0;
430         unsigned long srr1;
431         unsigned long csrr0;
432         unsigned long csrr1;
433         unsigned long dsrr0;
434         unsigned long dsrr1;
435         unsigned long saved_ksp_limit;
436 };
437
438 /* ensure this structure is always sized to a multiple of the stack alignment */
439 #define STACK_EXC_LVL_FRAME_SIZE        _ALIGN_UP(sizeof (struct exception_regs), 16)
440
441 #endif /* __ASSEMBLY__ */
442 #endif /* __HEAD_BOOKE_H__ */