GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / powerpc / kernel / tm.S
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Transactional memory support routines to reclaim and recheckpoint
4  * transactional process state.
5  *
6  * Copyright 2012 Matt Evans & Michael Neuling, IBM Corporation.
7  */
8
9 #include <asm/asm-offsets.h>
10 #include <asm/ppc_asm.h>
11 #include <asm/ppc-opcode.h>
12 #include <asm/ptrace.h>
13 #include <asm/reg.h>
14 #include <asm/bug.h>
15
16 #ifdef CONFIG_VSX
17 /* See fpu.S, this is borrowed from there */
18 #define __SAVE_32FPRS_VSRS(n,c,base)            \
19 BEGIN_FTR_SECTION                               \
20         b       2f;                             \
21 END_FTR_SECTION_IFSET(CPU_FTR_VSX);             \
22         SAVE_32FPRS(n,base);                    \
23         b       3f;                             \
24 2:      SAVE_32VSRS(n,c,base);                  \
25 3:
26 #define __REST_32FPRS_VSRS(n,c,base)            \
27 BEGIN_FTR_SECTION                               \
28         b       2f;                             \
29 END_FTR_SECTION_IFSET(CPU_FTR_VSX);             \
30         REST_32FPRS(n,base);                    \
31         b       3f;                             \
32 2:      REST_32VSRS(n,c,base);                  \
33 3:
34 #else
35 #define __SAVE_32FPRS_VSRS(n,c,base)    SAVE_32FPRS(n, base)
36 #define __REST_32FPRS_VSRS(n,c,base)    REST_32FPRS(n, base)
37 #endif
38 #define SAVE_32FPRS_VSRS(n,c,base) \
39         __SAVE_32FPRS_VSRS(n,__REG_##c,__REG_##base)
40 #define REST_32FPRS_VSRS(n,c,base) \
41         __REST_32FPRS_VSRS(n,__REG_##c,__REG_##base)
42
43 /* Stack frame offsets for local variables. */
44 #define TM_FRAME_L0     TM_FRAME_SIZE-16
45 #define TM_FRAME_L1     TM_FRAME_SIZE-8
46
47
48 /* In order to access the TM SPRs, TM must be enabled.  So, do so: */
49 _GLOBAL(tm_enable)
50         mfmsr   r4
51         li      r3, MSR_TM >> 32
52         sldi    r3, r3, 32
53         and.    r0, r4, r3
54         bne     1f
55         or      r4, r4, r3
56         mtmsrd  r4
57 1:      blr
58
59 _GLOBAL(tm_save_sprs)
60         mfspr   r0, SPRN_TFHAR
61         std     r0, THREAD_TM_TFHAR(r3)
62         mfspr   r0, SPRN_TEXASR
63         std     r0, THREAD_TM_TEXASR(r3)
64         mfspr   r0, SPRN_TFIAR
65         std     r0, THREAD_TM_TFIAR(r3)
66         blr
67
68 _GLOBAL(tm_restore_sprs)
69         ld      r0, THREAD_TM_TFHAR(r3)
70         mtspr   SPRN_TFHAR, r0
71         ld      r0, THREAD_TM_TEXASR(r3)
72         mtspr   SPRN_TEXASR, r0
73         ld      r0, THREAD_TM_TFIAR(r3)
74         mtspr   SPRN_TFIAR, r0
75         blr
76
77         /* Passed an 8-bit failure cause as first argument. */
78 _GLOBAL(tm_abort)
79         TABORT(R3)
80         blr
81
82 /* void tm_reclaim(struct thread_struct *thread,
83  *                 unsigned long orig_msr,
84  *                 uint8_t cause)
85  *
86  *      - Performs a full reclaim.  This destroys outstanding
87  *        transactions and updates thread->regs.tm_ckpt_* with the
88  *        original checkpointed state.  Note that thread->regs is
89  *        unchanged.
90  *      - FP regs are written back to thread->transact_fpr before
91  *        reclaiming.  These are the transactional (current) versions.
92  *
93  * Purpose is to both abort transactions of, and preserve the state of,
94  * a transactions at a context switch. We preserve/restore both sets of process
95  * state to restore them when the thread's scheduled again.  We continue in
96  * userland as though nothing happened, but when the transaction is resumed
97  * they will abort back to the checkpointed state we save out here.
98  *
99  * Call with IRQs off, stacks get all out of sync for some periods in here!
100  */
101 _GLOBAL(tm_reclaim)
102         mfcr    r6
103         mflr    r0
104         stw     r6, 8(r1)
105         std     r0, 16(r1)
106         std     r2, STK_GOT(r1)
107         stdu    r1, -TM_FRAME_SIZE(r1)
108
109         /* We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD]. */
110
111         std     r3, STK_PARAM(R3)(r1)
112         std     r4, STK_PARAM(R4)(r1)
113         SAVE_NVGPRS(r1)
114
115         /* We need to setup MSR for VSX register save instructions. */
116         mfmsr   r14
117         mr      r15, r14
118         ori     r15, r15, MSR_FP
119         li      r16, 0
120         ori     r16, r16, MSR_EE /* IRQs hard off */
121         andc    r15, r15, r16
122         oris    r15, r15, MSR_VEC@h
123 #ifdef CONFIG_VSX
124         BEGIN_FTR_SECTION
125         oris    r15,r15, MSR_VSX@h
126         END_FTR_SECTION_IFSET(CPU_FTR_VSX)
127 #endif
128         mtmsrd  r15
129         std     r14, TM_FRAME_L0(r1)
130
131         /* Do sanity check on MSR to make sure we are suspended */
132         li      r7, (MSR_TS_S)@higher
133         srdi    r6, r14, 32
134         and     r6, r6, r7
135 1:      tdeqi   r6, 0
136         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
137
138         /* Stash the stack pointer away for use after reclaim */
139         std     r1, PACAR1(r13)
140
141         /* Clear MSR RI since we are about to change r1, EE is already off. */
142         li      r4, 0
143         mtmsrd  r4, 1
144
145         /*
146          * BE CAREFUL HERE:
147          * At this point we can't take an SLB miss since we have MSR_RI
148          * off. Load only to/from the stack/paca which are in SLB bolted regions
149          * until we turn MSR RI back on.
150          *
151          * The moment we treclaim, ALL of our GPRs will switch
152          * to user register state.  (FPRs, CCR etc. also!)
153          * Use an sprg and a tm_scratch in the PACA to shuffle.
154          */
155         TRECLAIM(R5)                            /* Cause in r5 */
156
157         /* ******************** GPRs ******************** */
158         /* Stash the checkpointed r13 away in the scratch SPR and get the real
159          *  paca
160          */
161         SET_SCRATCH0(r13)
162         GET_PACA(r13)
163
164         /* Stash the checkpointed r1 away in paca tm_scratch and get the real
165          * stack pointer back
166          */
167         std     r1, PACATMSCRATCH(r13)
168         ld      r1, PACAR1(r13)
169
170         std     r11, GPR11(r1)                  /* Temporary stash */
171
172         /*
173          * Move the saved user r1 to the kernel stack in case PACATMSCRATCH is
174          * clobbered by an exception once we turn on MSR_RI below.
175          */
176         ld      r11, PACATMSCRATCH(r13)
177         std     r11, GPR1(r1)
178
179         /*
180          * Store r13 away so we can free up the scratch SPR for the SLB fault
181          * handler (needed once we start accessing the thread_struct).
182          */
183         GET_SCRATCH0(r11)
184         std     r11, GPR13(r1)
185
186         /* Reset MSR RI so we can take SLB faults again */
187         li      r11, MSR_RI
188         mtmsrd  r11, 1
189
190         /* Store the PPR in r11 and reset to decent value */
191         mfspr   r11, SPRN_PPR
192         HMT_MEDIUM
193
194         /* Now get some more GPRS free */
195         std     r7, GPR7(r1)                    /* Temporary stash */
196         std     r12, GPR12(r1)                  /* ''   ''    ''   */
197         ld      r12, STK_PARAM(R3)(r1)          /* Param 0, thread_struct * */
198
199         std     r11, THREAD_TM_PPR(r12)         /* Store PPR and free r11 */
200
201         addi    r7, r12, PT_CKPT_REGS           /* Thread's ckpt_regs */
202
203         /* Make r7 look like an exception frame so that we
204          * can use the neat GPRx(n) macros.  r7 is NOT a pt_regs ptr!
205          */
206         subi    r7, r7, STACK_FRAME_OVERHEAD
207
208         /* Sync the userland GPRs 2-12, 14-31 to thread->regs: */
209         SAVE_GPR(0, r7)                         /* user r0 */
210         SAVE_GPR(2, r7)                 /* user r2 */
211         SAVE_4GPRS(3, r7)                       /* user r3-r6 */
212         SAVE_GPR(8, r7)                         /* user r8 */
213         SAVE_GPR(9, r7)                         /* user r9 */
214         SAVE_GPR(10, r7)                        /* user r10 */
215         ld      r3, GPR1(r1)                    /* user r1 */
216         ld      r4, GPR7(r1)                    /* user r7 */
217         ld      r5, GPR11(r1)                   /* user r11 */
218         ld      r6, GPR12(r1)                   /* user r12 */
219         ld      r8, GPR13(r1)                   /* user r13 */
220         std     r3, GPR1(r7)
221         std     r4, GPR7(r7)
222         std     r5, GPR11(r7)
223         std     r6, GPR12(r7)
224         std     r8, GPR13(r7)
225
226         SAVE_NVGPRS(r7)                         /* user r14-r31 */
227
228         /* ******************** NIP ******************** */
229         mfspr   r3, SPRN_TFHAR
230         std     r3, _NIP(r7)                    /* Returns to failhandler */
231         /* The checkpointed NIP is ignored when rescheduling/rechkpting,
232          * but is used in signal return to 'wind back' to the abort handler.
233          */
234
235         /* ******************** CR,LR,CCR,MSR ********** */
236         mfctr   r3
237         mflr    r4
238         mfcr    r5
239         mfxer   r6
240
241         std     r3, _CTR(r7)
242         std     r4, _LINK(r7)
243         std     r5, _CCR(r7)
244         std     r6, _XER(r7)
245
246
247         /* ******************** TAR, DSCR ********** */
248         mfspr   r3, SPRN_TAR
249         mfspr   r4, SPRN_DSCR
250
251         std     r3, THREAD_TM_TAR(r12)
252         std     r4, THREAD_TM_DSCR(r12)
253
254         /* MSR and flags:  We don't change CRs, and we don't need to alter
255          * MSR.
256          */
257
258
259         /* ******************** FPR/VR/VSRs ************
260          * After reclaiming, capture the checkpointed FPRs/VRs /if used/.
261          *
262          * (If VSX used, FP and VMX are implied.  Or, we don't need to look
263          * at MSR.VSX as copying FP regs if .FP, vector regs if .VMX covers it.)
264          *
265          * We're passed the thread's MSR as the second parameter
266          *
267          * We enabled VEC/FP/VSX in the msr above, so we can execute these
268          * instructions!
269          */
270         ld      r4, STK_PARAM(R4)(r1)           /* Second parameter, MSR * */
271         mr      r3, r12
272         andis.          r0, r4, MSR_VEC@h
273         beq     dont_backup_vec
274
275         addi    r7, r3, THREAD_CKVRSTATE
276         SAVE_32VRS(0, r6, r7)   /* r6 scratch, r7 transact vr state */
277         mfvscr  v0
278         li      r6, VRSTATE_VSCR
279         stvx    v0, r7, r6
280 dont_backup_vec:
281         mfspr   r0, SPRN_VRSAVE
282         std     r0, THREAD_CKVRSAVE(r3)
283
284         andi.   r0, r4, MSR_FP
285         beq     dont_backup_fp
286
287         addi    r7, r3, THREAD_CKFPSTATE
288         SAVE_32FPRS_VSRS(0, R6, R7)     /* r6 scratch, r7 transact fp state */
289
290         mffs    fr0
291         stfd    fr0,FPSTATE_FPSCR(r7)
292
293 dont_backup_fp:
294
295         /* TM regs, incl TEXASR -- these live in thread_struct.  Note they've
296          * been updated by the treclaim, to explain to userland the failure
297          * cause (aborted).
298          */
299         mfspr   r0, SPRN_TEXASR
300         mfspr   r3, SPRN_TFHAR
301         mfspr   r4, SPRN_TFIAR
302         std     r0, THREAD_TM_TEXASR(r12)
303         std     r3, THREAD_TM_TFHAR(r12)
304         std     r4, THREAD_TM_TFIAR(r12)
305
306         /* AMR is checkpointed too, but is unsupported by Linux. */
307
308         /* Restore original MSR/IRQ state & clear TM mode */
309         ld      r14, TM_FRAME_L0(r1)            /* Orig MSR */
310
311         li      r15, 0
312         rldimi  r14, r15, MSR_TS_LG, (63-MSR_TS_LG)-1
313         mtmsrd  r14
314
315         REST_NVGPRS(r1)
316
317         addi    r1, r1, TM_FRAME_SIZE
318         lwz     r4, 8(r1)
319         ld      r0, 16(r1)
320         mtcr    r4
321         mtlr    r0
322         ld      r2, STK_GOT(r1)
323
324         /* Load CPU's default DSCR */
325         ld      r0, PACA_DSCR_DEFAULT(r13)
326         mtspr   SPRN_DSCR, r0
327
328         blr
329
330
331         /* void __tm_recheckpoint(struct thread_struct *thread,
332          *                        unsigned long orig_msr)
333          *      - Restore the checkpointed register state saved by tm_reclaim
334          *        when we switch_to a process.
335          *
336          *      Call with IRQs off, stacks get all out of sync for
337          *      some periods in here!
338          */
339 _GLOBAL(__tm_recheckpoint)
340         mfcr    r5
341         mflr    r0
342         stw     r5, 8(r1)
343         std     r0, 16(r1)
344         std     r2, STK_GOT(r1)
345         stdu    r1, -TM_FRAME_SIZE(r1)
346
347         /* We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD].
348          * This is used for backing up the NVGPRs:
349          */
350         SAVE_NVGPRS(r1)
351
352         /* Load complete register state from ts_ckpt* registers */
353
354         addi    r7, r3, PT_CKPT_REGS            /* Thread's ckpt_regs */
355
356         /* Make r7 look like an exception frame so that we
357          * can use the neat GPRx(n) macros.  r7 is now NOT a pt_regs ptr!
358          */
359         subi    r7, r7, STACK_FRAME_OVERHEAD
360
361         mfmsr   r6
362         /* R4 = original MSR to indicate whether thread used FP/Vector etc. */
363
364         /* Enable FP/vec in MSR if necessary! */
365         lis     r5, MSR_VEC@h
366         ori     r5, r5, MSR_FP
367         and.    r5, r4, r5
368         beq     restore_gprs                    /* if neither, skip both */
369
370 #ifdef CONFIG_VSX
371         BEGIN_FTR_SECTION
372         oris    r5, r5, MSR_VSX@h
373         END_FTR_SECTION_IFSET(CPU_FTR_VSX)
374 #endif
375         or      r5, r6, r5                      /* Set MSR.FP+.VSX/.VEC */
376         mtmsr   r5
377
378 #ifdef CONFIG_ALTIVEC
379         /*
380          * FP and VEC registers: These are recheckpointed from
381          * thread.ckfp_state and thread.ckvr_state respectively. The
382          * thread.fp_state[] version holds the 'live' (transactional)
383          * and will be loaded subsequently by any FPUnavailable trap.
384          */
385         andis.  r0, r4, MSR_VEC@h
386         beq     dont_restore_vec
387
388         addi    r8, r3, THREAD_CKVRSTATE
389         li      r5, VRSTATE_VSCR
390         lvx     v0, r8, r5
391         mtvscr  v0
392         REST_32VRS(0, r5, r8)                   /* r5 scratch, r8 ptr */
393 dont_restore_vec:
394         ld      r5, THREAD_CKVRSAVE(r3)
395         mtspr   SPRN_VRSAVE, r5
396 #endif
397
398         andi.   r0, r4, MSR_FP
399         beq     dont_restore_fp
400
401         addi    r8, r3, THREAD_CKFPSTATE
402         lfd     fr0, FPSTATE_FPSCR(r8)
403         MTFSF_L(fr0)
404         REST_32FPRS_VSRS(0, R4, R8)
405
406 dont_restore_fp:
407         mtmsr   r6                              /* FP/Vec off again! */
408
409 restore_gprs:
410
411         /* ******************** CR,LR,CCR,MSR ********** */
412         ld      r4, _CTR(r7)
413         ld      r5, _LINK(r7)
414         ld      r8, _XER(r7)
415
416         mtctr   r4
417         mtlr    r5
418         mtxer   r8
419
420         /* ******************** TAR ******************** */
421         ld      r4, THREAD_TM_TAR(r3)
422         mtspr   SPRN_TAR,       r4
423
424         /* Load up the PPR and DSCR in GPRs only at this stage */
425         ld      r5, THREAD_TM_DSCR(r3)
426         ld      r6, THREAD_TM_PPR(r3)
427
428         REST_GPR(0, r7)                         /* GPR0 */
429         REST_2GPRS(2, r7)                       /* GPR2-3 */
430         REST_GPR(4, r7)                         /* GPR4 */
431         REST_4GPRS(8, r7)                       /* GPR8-11 */
432         REST_2GPRS(12, r7)                      /* GPR12-13 */
433
434         REST_NVGPRS(r7)                         /* GPR14-31 */
435
436         /* Load up PPR and DSCR here so we don't run with user values for long
437          */
438         mtspr   SPRN_DSCR, r5
439         mtspr   SPRN_PPR, r6
440
441         /* Do final sanity check on TEXASR to make sure FS is set.  Do this
442          * here before we load up the userspace r1 so any bugs we hit will get
443          * a call chain */
444         mfspr   r5, SPRN_TEXASR
445         srdi    r5, r5, 16
446         li      r6, (TEXASR_FS)@h
447         and     r6, r6, r5
448 1:      tdeqi   r6, 0
449         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
450
451         /* Do final sanity check on MSR to make sure we are not transactional
452          * or suspended
453          */
454         mfmsr   r6
455         li      r5, (MSR_TS_MASK)@higher
456         srdi    r6, r6, 32
457         and     r6, r6, r5
458 1:      tdnei   r6, 0
459         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
460
461         /* Restore CR */
462         ld      r6, _CCR(r7)
463         mtcr    r6
464
465         REST_GPR(6, r7)
466
467         /*
468          * Store r1 and r5 on the stack so that we can access them
469          * after we clear MSR RI.
470          */
471
472         REST_GPR(5, r7)
473         std     r5, -8(r1)
474         ld      r5, GPR1(r7)
475         std     r5, -16(r1)
476
477         REST_GPR(7, r7)
478
479         /* Clear MSR RI since we are about to change r1. EE is already off */
480         li      r5, 0
481         mtmsrd  r5, 1
482
483         /*
484          * BE CAREFUL HERE:
485          * At this point we can't take an SLB miss since we have MSR_RI
486          * off. Load only to/from the stack/paca which are in SLB bolted regions
487          * until we turn MSR RI back on.
488          */
489
490         SET_SCRATCH0(r1)
491         ld      r5, -8(r1)
492         ld      r1, -16(r1)
493
494         /* Commit register state as checkpointed state: */
495         TRECHKPT
496
497         HMT_MEDIUM
498
499         /* Our transactional state has now changed.
500          *
501          * Now just get out of here.  Transactional (current) state will be
502          * updated once restore is called on the return path in the _switch-ed
503          * -to process.
504          */
505
506         GET_PACA(r13)
507         GET_SCRATCH0(r1)
508
509         /* R1 is restored, so we are recoverable again.  EE is still off */
510         li      r4, MSR_RI
511         mtmsrd  r4, 1
512
513         REST_NVGPRS(r1)
514
515         addi    r1, r1, TM_FRAME_SIZE
516         lwz     r4, 8(r1)
517         ld      r0, 16(r1)
518         mtcr    r4
519         mtlr    r0
520         ld      r2, STK_GOT(r1)
521
522         /* Load CPU's default DSCR */
523         ld      r0, PACA_DSCR_DEFAULT(r13)
524         mtspr   SPRN_DSCR, r0
525
526         blr
527
528         /* ****************************************************************** */