1 /*
2 * arch/xtensa/kernel/process.c
3 *
4 * Xtensa Processor version.
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 *
10 * Copyright (C) 2001 - 2005 Tensilica Inc.
11 *
12 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
13 * Chris Zankel <chris@zankel.net>
14 * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca>
15 * Kevin Chea
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 <linux/uaccess.h>
41 #include <asm/io.h>
42 #include <asm/processor.h>
43 #include <asm/platform.h>
44 #include <asm/mmu.h>
45 #include <asm/irq.h>
46 #include <linux/atomic.h>
47 #include <asm/asm-offsets.h>
48 #include <asm/regs.h>
49 #include <asm/hw_breakpoint.h>
50
51 extern void ret_from_fork(void);
52 extern void ret_from_kernel_thread(void);
53
54 void (*pm_power_off)(void) = NULL;
55 EXPORT_SYMBOL(pm_power_off);
56
57
58 #ifdef CONFIG_STACKPROTECTOR
59 #include <linux/stackprotector.h>
60 unsigned long __stack_chk_guard __read_mostly;
61 EXPORT_SYMBOL(__stack_chk_guard);
62 #endif
63
64 #if XTENSA_HAVE_COPROCESSORS
65
coprocessor_release_all(struct thread_info * ti)66 void coprocessor_release_all(struct thread_info *ti)
67 {
68 unsigned long cpenable;
69 int i;
70
71 /* Make sure we don't switch tasks during this operation. */
72
73 preempt_disable();
74
75 /* Walk through all cp owners and release it for the requested one. */
76
77 cpenable = ti->cpenable;
78
79 for (i = 0; i < XCHAL_CP_MAX; i++) {
80 if (coprocessor_owner[i] == ti) {
81 coprocessor_owner[i] = 0;
82 cpenable &= ~(1 << i);
83 }
84 }
85
86 ti->cpenable = cpenable;
87 if (ti == current_thread_info())
88 xtensa_set_sr(0, cpenable);
89
90 preempt_enable();
91 }
92
coprocessor_flush_all(struct thread_info * ti)93 void coprocessor_flush_all(struct thread_info *ti)
94 {
95 unsigned long cpenable, old_cpenable;
96 int i;
97
98 preempt_disable();
99
100 old_cpenable = xtensa_get_sr(cpenable);
101 cpenable = ti->cpenable;
102 xtensa_set_sr(cpenable, cpenable);
103
104 for (i = 0; i < XCHAL_CP_MAX; i++) {
105 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
106 coprocessor_flush(ti, i);
107 cpenable >>= 1;
108 }
109 xtensa_set_sr(old_cpenable, cpenable);
110
111 preempt_enable();
112 }
113
114 #endif
115
116
117 /*
118 * Powermanagement idle function, if any is provided by the platform.
119 */
arch_cpu_idle(void)120 void arch_cpu_idle(void)
121 {
122 platform_idle();
123 }
124
125 /*
126 * This is called when the thread calls exit().
127 */
exit_thread(struct task_struct * tsk)128 void exit_thread(struct task_struct *tsk)
129 {
130 #if XTENSA_HAVE_COPROCESSORS
131 coprocessor_release_all(task_thread_info(tsk));
132 #endif
133 }
134
135 /*
136 * Flush thread state. This is called when a thread does an execve()
137 * Note that we flush coprocessor registers for the case execve fails.
138 */
flush_thread(void)139 void flush_thread(void)
140 {
141 #if XTENSA_HAVE_COPROCESSORS
142 struct thread_info *ti = current_thread_info();
143 coprocessor_flush_all(ti);
144 coprocessor_release_all(ti);
145 #endif
146 flush_ptrace_hw_breakpoint(current);
147 }
148
149 /*
150 * this gets called so that we can store coprocessor state into memory and
151 * copy the current task into the new thread.
152 */
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)153 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
154 {
155 #if XTENSA_HAVE_COPROCESSORS
156 coprocessor_flush_all(task_thread_info(src));
157 #endif
158 *dst = *src;
159 return 0;
160 }
161
162 /*
163 * Copy thread.
164 *
165 * There are two modes in which this function is called:
166 * 1) Userspace thread creation,
167 * regs != NULL, usp_thread_fn is userspace stack pointer.
168 * It is expected to copy parent regs (in case CLONE_VM is not set
169 * in the clone_flags) and set up passed usp in the childregs.
170 * 2) Kernel thread creation,
171 * regs == NULL, usp_thread_fn is the function to run in the new thread
172 * and thread_fn_arg is its parameter.
173 * childregs are not used for the kernel threads.
174 *
175 * The stack layout for the new thread looks like this:
176 *
177 * +------------------------+
178 * | childregs |
179 * +------------------------+ <- thread.sp = sp in dummy-frame
180 * | dummy-frame | (saved in dummy-frame spill-area)
181 * +------------------------+
182 *
183 * We create a dummy frame to return to either ret_from_fork or
184 * ret_from_kernel_thread:
185 * a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
186 * sp points to itself (thread.sp)
187 * a2, a3 are unused for userspace threads,
188 * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
189 *
190 * Note: This is a pristine frame, so we don't need any spill region on top of
191 * childregs.
192 *
193 * The fun part: if we're keeping the same VM (i.e. cloning a thread,
194 * not an entire process), we're normally given a new usp, and we CANNOT share
195 * any live address register windows. If we just copy those live frames over,
196 * the two threads (parent and child) will overflow the same frames onto the
197 * parent stack at different times, likely corrupting the parent stack (esp.
198 * if the parent returns from functions that called clone() and calls new
199 * ones, before the child overflows its now old copies of its parent windows).
200 * One solution is to spill windows to the parent stack, but that's fairly
201 * involved. Much simpler to just not copy those live frames across.
202 */
203
copy_thread(unsigned long clone_flags,unsigned long usp_thread_fn,unsigned long thread_fn_arg,struct task_struct * p,unsigned long tls)204 int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
205 unsigned long thread_fn_arg, struct task_struct *p,
206 unsigned long tls)
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 #if defined(__XTENSA_WINDOWED_ABI__)
215 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
216 SPILL_SLOT(childregs, 1) = (unsigned long)childregs;
217 SPILL_SLOT(childregs, 0) = 0;
218
219 p->thread.sp = (unsigned long)childregs;
220 #elif defined(__XTENSA_CALL0_ABI__)
221 /* Reserve 16 bytes for the _switch_to stack frame. */
222 p->thread.sp = (unsigned long)childregs - 16;
223 #else
224 #error Unsupported Xtensa ABI
225 #endif
226
227 if (!(p->flags & (PF_KTHREAD | PF_IO_WORKER))) {
228 struct pt_regs *regs = current_pt_regs();
229 unsigned long usp = usp_thread_fn ?
230 usp_thread_fn : regs->areg[1];
231
232 p->thread.ra = MAKE_RA_FOR_CALL(
233 (unsigned long)ret_from_fork, 0x1);
234
235 /* This does not copy all the regs.
236 * In a bout of brilliance or madness,
237 * ARs beyond a0-a15 exist past the end of the struct.
238 */
239 *childregs = *regs;
240 childregs->areg[1] = usp;
241 childregs->areg[2] = 0;
242
243 /* When sharing memory with the parent thread, the child
244 usually starts on a pristine stack, so we have to reset
245 windowbase, windowstart and wmask.
246 (Note that such a new thread is required to always create
247 an initial call4 frame)
248 The exception is vfork, where the new thread continues to
249 run on the parent's stack until it calls execve. This could
250 be a call8 or call12, which requires a legal stack frame
251 of the previous caller for the overflow handlers to work.
252 (Note that it's always legal to overflow live registers).
253 In this case, ensure to spill at least the stack pointer
254 of that frame. */
255
256 if (clone_flags & CLONE_VM) {
257 /* check that caller window is live and same stack */
258 int len = childregs->wmask & ~0xf;
259 if (regs->areg[1] == usp && len != 0) {
260 int callinc = (regs->areg[0] >> 30) & 3;
261 int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
262 put_user(regs->areg[caller_ars+1],
263 (unsigned __user*)(usp - 12));
264 }
265 childregs->wmask = 1;
266 childregs->windowstart = 1;
267 childregs->windowbase = 0;
268 } else {
269 int len = childregs->wmask & ~0xf;
270 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
271 ®s->areg[XCHAL_NUM_AREGS - len/4], len);
272 }
273
274 childregs->syscall = regs->syscall;
275
276 if (clone_flags & CLONE_SETTLS)
277 childregs->threadptr = tls;
278 } else {
279 p->thread.ra = MAKE_RA_FOR_CALL(
280 (unsigned long)ret_from_kernel_thread, 1);
281
282 /* pass parameters to ret_from_kernel_thread: */
283 #if defined(__XTENSA_WINDOWED_ABI__)
284 /*
285 * a2 = thread_fn, a3 = thread_fn arg.
286 * Window underflow will load registers from the
287 * spill slots on the stack on return from _switch_to.
288 */
289 SPILL_SLOT(childregs, 2) = usp_thread_fn;
290 SPILL_SLOT(childregs, 3) = thread_fn_arg;
291 #elif defined(__XTENSA_CALL0_ABI__)
292 /*
293 * a12 = thread_fn, a13 = thread_fn arg.
294 * _switch_to epilogue will load registers from the stack.
295 */
296 ((unsigned long *)p->thread.sp)[0] = usp_thread_fn;
297 ((unsigned long *)p->thread.sp)[1] = thread_fn_arg;
298 #else
299 #error Unsupported Xtensa ABI
300 #endif
301
302 /* Childregs are only used when we're going to userspace
303 * in which case start_thread will set them up.
304 */
305 }
306
307 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
308 ti = task_thread_info(p);
309 ti->cpenable = 0;
310 #endif
311
312 clear_ptrace_hw_breakpoint(p);
313
314 return 0;
315 }
316
317
318 /*
319 * These bracket the sleeping functions..
320 */
321
__get_wchan(struct task_struct * p)322 unsigned long __get_wchan(struct task_struct *p)
323 {
324 unsigned long sp, pc;
325 unsigned long stack_page = (unsigned long) task_stack_page(p);
326 int count = 0;
327
328 sp = p->thread.sp;
329 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
330
331 do {
332 if (sp < stack_page + sizeof(struct task_struct) ||
333 sp >= (stack_page + THREAD_SIZE) ||
334 pc == 0)
335 return 0;
336 if (!in_sched_functions(pc))
337 return pc;
338
339 /* Stack layout: sp-4: ra, sp-3: sp' */
340
341 pc = MAKE_PC_FROM_RA(SPILL_SLOT(sp, 0), sp);
342 sp = SPILL_SLOT(sp, 1);
343 } while (count++ < 16);
344 return 0;
345 }
346