1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/errno.h>
19#include <linux/sched.h>
20#include <linux/sched/debug.h>
21#include <linux/sched/task.h>
22#include <linux/sched/task_stack.h>
23#include <linux/kernel.h>
24#include <linux/mm.h>
25#include <linux/smp.h>
26#include <linux/stddef.h>
27#include <linux/unistd.h>
28#include <linux/ptrace.h>
29#include <linux/elf.h>
30#include <linux/hw_breakpoint.h>
31#include <linux/init.h>
32#include <linux/prctl.h>
33#include <linux/init_task.h>
34#include <linux/module.h>
35#include <linux/mqueue.h>
36#include <linux/fs.h>
37#include <linux/slab.h>
38#include <linux/rcupdate.h>
39
40#include <asm/pgtable.h>
41#include <linux/uaccess.h>
42#include <asm/io.h>
43#include <asm/processor.h>
44#include <asm/platform.h>
45#include <asm/mmu.h>
46#include <asm/irq.h>
47#include <linux/atomic.h>
48#include <asm/asm-offsets.h>
49#include <asm/regs.h>
50#include <asm/hw_breakpoint.h>
51
52extern void ret_from_fork(void);
53extern void ret_from_kernel_thread(void);
54
55void (*pm_power_off)(void) = NULL;
56EXPORT_SYMBOL(pm_power_off);
57
58
59#ifdef CONFIG_STACKPROTECTOR
60#include <linux/stackprotector.h>
61unsigned long __stack_chk_guard __read_mostly;
62EXPORT_SYMBOL(__stack_chk_guard);
63#endif
64
65#if XTENSA_HAVE_COPROCESSORS
66
67void coprocessor_release_all(struct thread_info *ti)
68{
69 unsigned long cpenable;
70 int i;
71
72
73
74 preempt_disable();
75
76
77
78 cpenable = ti->cpenable;
79
80 for (i = 0; i < XCHAL_CP_MAX; i++) {
81 if (coprocessor_owner[i] == ti) {
82 coprocessor_owner[i] = 0;
83 cpenable &= ~(1 << i);
84 }
85 }
86
87 ti->cpenable = cpenable;
88 if (ti == current_thread_info())
89 xtensa_set_sr(0, cpenable);
90
91 preempt_enable();
92}
93
94void coprocessor_flush_all(struct thread_info *ti)
95{
96 unsigned long cpenable, old_cpenable;
97 int i;
98
99 preempt_disable();
100
101 old_cpenable = xtensa_get_sr(cpenable);
102 cpenable = ti->cpenable;
103 xtensa_set_sr(cpenable, cpenable);
104
105 for (i = 0; i < XCHAL_CP_MAX; i++) {
106 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
107 coprocessor_flush(ti, i);
108 cpenable >>= 1;
109 }
110 xtensa_set_sr(old_cpenable, cpenable);
111
112 preempt_enable();
113}
114
115#endif
116
117
118
119
120
121void arch_cpu_idle(void)
122{
123 platform_idle();
124}
125
126
127
128
129void exit_thread(struct task_struct *tsk)
130{
131#if XTENSA_HAVE_COPROCESSORS
132 coprocessor_release_all(task_thread_info(tsk));
133#endif
134}
135
136
137
138
139
140void flush_thread(void)
141{
142#if XTENSA_HAVE_COPROCESSORS
143 struct thread_info *ti = current_thread_info();
144 coprocessor_flush_all(ti);
145 coprocessor_release_all(ti);
146#endif
147 flush_ptrace_hw_breakpoint(current);
148}
149
150
151
152
153
154int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
155{
156#if XTENSA_HAVE_COPROCESSORS
157 coprocessor_flush_all(task_thread_info(src));
158#endif
159 *dst = *src;
160 return 0;
161}
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
206 unsigned long thread_fn_arg, struct task_struct *p)
207{
208 struct pt_regs *childregs = task_pt_regs(p);
209
210#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
211 struct thread_info *ti;
212#endif
213
214
215 SPILL_SLOT(childregs, 1) = (unsigned long)childregs;
216 SPILL_SLOT(childregs, 0) = 0;
217
218 p->thread.sp = (unsigned long)childregs;
219
220 if (!(p->flags & PF_KTHREAD)) {
221 struct pt_regs *regs = current_pt_regs();
222 unsigned long usp = usp_thread_fn ?
223 usp_thread_fn : regs->areg[1];
224
225 p->thread.ra = MAKE_RA_FOR_CALL(
226 (unsigned long)ret_from_fork, 0x1);
227
228
229
230
231
232 *childregs = *regs;
233 childregs->areg[1] = usp;
234 childregs->areg[2] = 0;
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 if (clone_flags & CLONE_VM) {
250
251 int len = childregs->wmask & ~0xf;
252 if (regs->areg[1] == usp && len != 0) {
253 int callinc = (regs->areg[0] >> 30) & 3;
254 int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
255 put_user(regs->areg[caller_ars+1],
256 (unsigned __user*)(usp - 12));
257 }
258 childregs->wmask = 1;
259 childregs->windowstart = 1;
260 childregs->windowbase = 0;
261 } else {
262 int len = childregs->wmask & ~0xf;
263 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
264 ®s->areg[XCHAL_NUM_AREGS - len/4], len);
265 }
266
267
268 if (clone_flags & CLONE_SETTLS)
269 childregs->threadptr = childregs->areg[5];
270 } else {
271 p->thread.ra = MAKE_RA_FOR_CALL(
272 (unsigned long)ret_from_kernel_thread, 1);
273
274
275
276
277 SPILL_SLOT(childregs, 3) = thread_fn_arg;
278 SPILL_SLOT(childregs, 2) = usp_thread_fn;
279
280
281
282
283 }
284
285#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
286 ti = task_thread_info(p);
287 ti->cpenable = 0;
288#endif
289
290 clear_ptrace_hw_breakpoint(p);
291
292 return 0;
293}
294
295
296
297
298
299
300unsigned long get_wchan(struct task_struct *p)
301{
302 unsigned long sp, pc;
303 unsigned long stack_page = (unsigned long) task_stack_page(p);
304 int count = 0;
305
306 if (!p || p == current || p->state == TASK_RUNNING)
307 return 0;
308
309 sp = p->thread.sp;
310 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
311
312 do {
313 if (sp < stack_page + sizeof(struct task_struct) ||
314 sp >= (stack_page + THREAD_SIZE) ||
315 pc == 0)
316 return 0;
317 if (!in_sched_functions(pc))
318 return pc;
319
320
321
322 pc = MAKE_PC_FROM_RA(SPILL_SLOT(sp, 0), sp);
323 sp = SPILL_SLOT(sp, 1);
324 } while (count++ < 16);
325 return 0;
326}
327