blob: 29c81f82705e139dc53a9af3f72b0db3d9e14695 [file] [log] [blame]
David Howellsa0616cd2012-03-28 18:30:02 +01001/*
2 * Copyright IBM Corp. 1999, 2009
3 *
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
5 */
6
7#ifndef __ASM_SWITCH_TO_H
8#define __ASM_SWITCH_TO_H
9
10#include <linux/thread_info.h>
Heiko Carstensbee5c282013-08-20 10:24:12 +020011#include <asm/ptrace.h>
David Howellsa0616cd2012-03-28 18:30:02 +010012
13extern struct task_struct *__switch_to(void *, void *);
Michael Mueller64597f92013-07-02 22:58:26 +020014extern void update_cr_regs(struct task_struct *task);
David Howellsa0616cd2012-03-28 18:30:02 +010015
Martin Schwidefsky4725c862013-10-15 16:08:34 +020016static inline int test_fp_ctl(u32 fpc)
David Howellsa0616cd2012-03-28 18:30:02 +010017{
Martin Schwidefsky4725c862013-10-15 16:08:34 +020018 u32 orig_fpc;
19 int rc;
20
David Howellsa0616cd2012-03-28 18:30:02 +010021 if (!MACHINE_HAS_IEEE)
Martin Schwidefsky4725c862013-10-15 16:08:34 +020022 return 0;
23
David Howellsa0616cd2012-03-28 18:30:02 +010024 asm volatile(
Martin Schwidefsky4725c862013-10-15 16:08:34 +020025 " efpc %1\n"
26 " sfpc %2\n"
27 "0: sfpc %1\n"
28 " la %0,0\n"
29 "1:\n"
30 EX_TABLE(0b,1b)
31 : "=d" (rc), "=d" (orig_fpc)
32 : "d" (fpc), "0" (-EINVAL));
33 return rc;
David Howellsa0616cd2012-03-28 18:30:02 +010034}
35
Martin Schwidefsky4725c862013-10-15 16:08:34 +020036static inline void save_fp_ctl(u32 *fpc)
David Howellsa0616cd2012-03-28 18:30:02 +010037{
David Howellsa0616cd2012-03-28 18:30:02 +010038 if (!MACHINE_HAS_IEEE)
39 return;
Martin Schwidefsky4725c862013-10-15 16:08:34 +020040
David Howellsa0616cd2012-03-28 18:30:02 +010041 asm volatile(
Martin Schwidefsky4725c862013-10-15 16:08:34 +020042 " stfpc %0\n"
43 : "+Q" (*fpc));
44}
45
46static inline int restore_fp_ctl(u32 *fpc)
47{
48 int rc;
49
50 if (!MACHINE_HAS_IEEE)
51 return 0;
52
53 asm volatile(
54 "0: lfpc %1\n"
55 " la %0,0\n"
56 "1:\n"
57 EX_TABLE(0b,1b)
58 : "=d" (rc) : "Q" (*fpc), "0" (-EINVAL));
59 return rc;
60}
61
62static inline void save_fp_regs(freg_t *fprs)
63{
64 asm volatile("std 0,%0" : "=Q" (fprs[0]));
65 asm volatile("std 2,%0" : "=Q" (fprs[2]));
66 asm volatile("std 4,%0" : "=Q" (fprs[4]));
67 asm volatile("std 6,%0" : "=Q" (fprs[6]));
68 if (!MACHINE_HAS_IEEE)
69 return;
70 asm volatile("std 1,%0" : "=Q" (fprs[1]));
71 asm volatile("std 3,%0" : "=Q" (fprs[3]));
72 asm volatile("std 5,%0" : "=Q" (fprs[5]));
73 asm volatile("std 7,%0" : "=Q" (fprs[7]));
74 asm volatile("std 8,%0" : "=Q" (fprs[8]));
75 asm volatile("std 9,%0" : "=Q" (fprs[9]));
76 asm volatile("std 10,%0" : "=Q" (fprs[10]));
77 asm volatile("std 11,%0" : "=Q" (fprs[11]));
78 asm volatile("std 12,%0" : "=Q" (fprs[12]));
79 asm volatile("std 13,%0" : "=Q" (fprs[13]));
80 asm volatile("std 14,%0" : "=Q" (fprs[14]));
81 asm volatile("std 15,%0" : "=Q" (fprs[15]));
82}
83
84static inline void restore_fp_regs(freg_t *fprs)
85{
86 asm volatile("ld 0,%0" : : "Q" (fprs[0]));
87 asm volatile("ld 2,%0" : : "Q" (fprs[2]));
88 asm volatile("ld 4,%0" : : "Q" (fprs[4]));
89 asm volatile("ld 6,%0" : : "Q" (fprs[6]));
90 if (!MACHINE_HAS_IEEE)
91 return;
92 asm volatile("ld 1,%0" : : "Q" (fprs[1]));
93 asm volatile("ld 3,%0" : : "Q" (fprs[3]));
94 asm volatile("ld 5,%0" : : "Q" (fprs[5]));
95 asm volatile("ld 7,%0" : : "Q" (fprs[7]));
96 asm volatile("ld 8,%0" : : "Q" (fprs[8]));
97 asm volatile("ld 9,%0" : : "Q" (fprs[9]));
98 asm volatile("ld 10,%0" : : "Q" (fprs[10]));
99 asm volatile("ld 11,%0" : : "Q" (fprs[11]));
100 asm volatile("ld 12,%0" : : "Q" (fprs[12]));
101 asm volatile("ld 13,%0" : : "Q" (fprs[13]));
102 asm volatile("ld 14,%0" : : "Q" (fprs[14]));
103 asm volatile("ld 15,%0" : : "Q" (fprs[15]));
David Howellsa0616cd2012-03-28 18:30:02 +0100104}
105
106static inline void save_access_regs(unsigned int *acrs)
107{
Heiko Carstensbee5c282013-08-20 10:24:12 +0200108 typedef struct { int _[NUM_ACRS]; } acrstype;
109
110 asm volatile("stam 0,15,%0" : "=Q" (*(acrstype *)acrs));
David Howellsa0616cd2012-03-28 18:30:02 +0100111}
112
113static inline void restore_access_regs(unsigned int *acrs)
114{
Heiko Carstensbee5c282013-08-20 10:24:12 +0200115 typedef struct { int _[NUM_ACRS]; } acrstype;
116
117 asm volatile("lam 0,15,%0" : : "Q" (*(acrstype *)acrs));
David Howellsa0616cd2012-03-28 18:30:02 +0100118}
119
120#define switch_to(prev,next,last) do { \
121 if (prev->mm) { \
Martin Schwidefsky4725c862013-10-15 16:08:34 +0200122 save_fp_ctl(&prev->thread.fp_regs.fpc); \
123 save_fp_regs(prev->thread.fp_regs.fprs); \
David Howellsa0616cd2012-03-28 18:30:02 +0100124 save_access_regs(&prev->thread.acrs[0]); \
Jan Glaubere4b8b3f2012-07-31 10:52:05 +0200125 save_ri_cb(prev->thread.ri_cb); \
David Howellsa0616cd2012-03-28 18:30:02 +0100126 } \
127 if (next->mm) { \
Martin Schwidefsky4725c862013-10-15 16:08:34 +0200128 restore_fp_ctl(&next->thread.fp_regs.fpc); \
129 restore_fp_regs(next->thread.fp_regs.fprs); \
David Howellsa0616cd2012-03-28 18:30:02 +0100130 restore_access_regs(&next->thread.acrs[0]); \
Jan Glaubere4b8b3f2012-07-31 10:52:05 +0200131 restore_ri_cb(next->thread.ri_cb, prev->thread.ri_cb); \
Michael Mueller64597f92013-07-02 22:58:26 +0200132 update_cr_regs(next); \
David Howellsa0616cd2012-03-28 18:30:02 +0100133 } \
134 prev = __switch_to(prev,next); \
135} while (0)
136
David Howellsa0616cd2012-03-28 18:30:02 +0100137#define finish_arch_switch(prev) do { \
138 set_fs(current->thread.mm_segment); \
David Howellsa0616cd2012-03-28 18:30:02 +0100139} while (0)
140
141#endif /* __ASM_SWITCH_TO_H */