xref: /openbmc/linux/arch/xtensa/kernel/process.c (revision 89cc9abe)
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 #include <asm/traps.h>
51 
52 extern void ret_from_fork(void);
53 extern void ret_from_kernel_thread(void);
54 
55 void (*pm_power_off)(void) = NULL;
56 EXPORT_SYMBOL(pm_power_off);
57 
58 
59 #ifdef CONFIG_STACKPROTECTOR
60 #include <linux/stackprotector.h>
61 unsigned long __stack_chk_guard __read_mostly;
62 EXPORT_SYMBOL(__stack_chk_guard);
63 #endif
64 
65 #if XTENSA_HAVE_COPROCESSORS
66 
67 void local_coprocessors_flush_release_all(void)
68 {
69 	struct thread_info **coprocessor_owner;
70 	struct thread_info *unique_owner[XCHAL_CP_MAX];
71 	int n = 0;
72 	int i, j;
73 
74 	coprocessor_owner = this_cpu_ptr(&exc_table)->coprocessor_owner;
75 	xtensa_set_sr(XCHAL_CP_MASK, cpenable);
76 
77 	for (i = 0; i < XCHAL_CP_MAX; i++) {
78 		struct thread_info *ti = coprocessor_owner[i];
79 
80 		if (ti) {
81 			coprocessor_flush(ti, i);
82 
83 			for (j = 0; j < n; j++)
84 				if (unique_owner[j] == ti)
85 					break;
86 			if (j == n)
87 				unique_owner[n++] = ti;
88 
89 			coprocessor_owner[i] = NULL;
90 		}
91 	}
92 	for (i = 0; i < n; i++) {
93 		/* pairs with memw (1) in fast_coprocessor and memw in switch_to */
94 		smp_wmb();
95 		unique_owner[i]->cpenable = 0;
96 	}
97 	xtensa_set_sr(0, cpenable);
98 }
99 
100 static void local_coprocessor_release_all(void *info)
101 {
102 	struct thread_info *ti = info;
103 	struct thread_info **coprocessor_owner;
104 	int i;
105 
106 	coprocessor_owner = this_cpu_ptr(&exc_table)->coprocessor_owner;
107 
108 	/* Walk through all cp owners and release it for the requested one. */
109 
110 	for (i = 0; i < XCHAL_CP_MAX; i++) {
111 		if (coprocessor_owner[i] == ti)
112 			coprocessor_owner[i] = NULL;
113 	}
114 	/* pairs with memw (1) in fast_coprocessor and memw in switch_to */
115 	smp_wmb();
116 	ti->cpenable = 0;
117 	if (ti == current_thread_info())
118 		xtensa_set_sr(0, cpenable);
119 }
120 
121 void coprocessor_release_all(struct thread_info *ti)
122 {
123 	if (ti->cpenable) {
124 		/* pairs with memw (2) in fast_coprocessor */
125 		smp_rmb();
126 		smp_call_function_single(ti->cp_owner_cpu,
127 					 local_coprocessor_release_all,
128 					 ti, true);
129 	}
130 }
131 
132 static void local_coprocessor_flush_all(void *info)
133 {
134 	struct thread_info *ti = info;
135 	struct thread_info **coprocessor_owner;
136 	unsigned long old_cpenable;
137 	int i;
138 
139 	coprocessor_owner = this_cpu_ptr(&exc_table)->coprocessor_owner;
140 	old_cpenable = xtensa_xsr(ti->cpenable, cpenable);
141 
142 	for (i = 0; i < XCHAL_CP_MAX; i++) {
143 		if (coprocessor_owner[i] == ti)
144 			coprocessor_flush(ti, i);
145 	}
146 	xtensa_set_sr(old_cpenable, cpenable);
147 }
148 
149 void coprocessor_flush_all(struct thread_info *ti)
150 {
151 	if (ti->cpenable) {
152 		/* pairs with memw (2) in fast_coprocessor */
153 		smp_rmb();
154 		smp_call_function_single(ti->cp_owner_cpu,
155 					 local_coprocessor_flush_all,
156 					 ti, true);
157 	}
158 }
159 
160 static void local_coprocessor_flush_release_all(void *info)
161 {
162 	local_coprocessor_flush_all(info);
163 	local_coprocessor_release_all(info);
164 }
165 
166 void coprocessor_flush_release_all(struct thread_info *ti)
167 {
168 	if (ti->cpenable) {
169 		/* pairs with memw (2) in fast_coprocessor */
170 		smp_rmb();
171 		smp_call_function_single(ti->cp_owner_cpu,
172 					 local_coprocessor_flush_release_all,
173 					 ti, true);
174 	}
175 }
176 
177 #endif
178 
179 
180 /*
181  * Powermanagement idle function, if any is provided by the platform.
182  */
183 void arch_cpu_idle(void)
184 {
185 	platform_idle();
186 	raw_local_irq_disable();
187 }
188 
189 /*
190  * This is called when the thread calls exit().
191  */
192 void exit_thread(struct task_struct *tsk)
193 {
194 #if XTENSA_HAVE_COPROCESSORS
195 	coprocessor_release_all(task_thread_info(tsk));
196 #endif
197 }
198 
199 /*
200  * Flush thread state. This is called when a thread does an execve()
201  * Note that we flush coprocessor registers for the case execve fails.
202  */
203 void flush_thread(void)
204 {
205 #if XTENSA_HAVE_COPROCESSORS
206 	struct thread_info *ti = current_thread_info();
207 	coprocessor_flush_release_all(ti);
208 #endif
209 	flush_ptrace_hw_breakpoint(current);
210 }
211 
212 /*
213  * this gets called so that we can store coprocessor state into memory and
214  * copy the current task into the new thread.
215  */
216 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
217 {
218 #if XTENSA_HAVE_COPROCESSORS
219 	coprocessor_flush_all(task_thread_info(src));
220 #endif
221 	*dst = *src;
222 	return 0;
223 }
224 
225 /*
226  * Copy thread.
227  *
228  * There are two modes in which this function is called:
229  * 1) Userspace thread creation,
230  *    regs != NULL, usp_thread_fn is userspace stack pointer.
231  *    It is expected to copy parent regs (in case CLONE_VM is not set
232  *    in the clone_flags) and set up passed usp in the childregs.
233  * 2) Kernel thread creation,
234  *    regs == NULL, usp_thread_fn is the function to run in the new thread
235  *    and thread_fn_arg is its parameter.
236  *    childregs are not used for the kernel threads.
237  *
238  * The stack layout for the new thread looks like this:
239  *
240  *	+------------------------+
241  *	|       childregs        |
242  *	+------------------------+ <- thread.sp = sp in dummy-frame
243  *	|      dummy-frame       |    (saved in dummy-frame spill-area)
244  *	+------------------------+
245  *
246  * We create a dummy frame to return to either ret_from_fork or
247  *   ret_from_kernel_thread:
248  *   a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
249  *   sp points to itself (thread.sp)
250  *   a2, a3 are unused for userspace threads,
251  *   a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
252  *
253  * Note: This is a pristine frame, so we don't need any spill region on top of
254  *       childregs.
255  *
256  * The fun part:  if we're keeping the same VM (i.e. cloning a thread,
257  * not an entire process), we're normally given a new usp, and we CANNOT share
258  * any live address register windows.  If we just copy those live frames over,
259  * the two threads (parent and child) will overflow the same frames onto the
260  * parent stack at different times, likely corrupting the parent stack (esp.
261  * if the parent returns from functions that called clone() and calls new
262  * ones, before the child overflows its now old copies of its parent windows).
263  * One solution is to spill windows to the parent stack, but that's fairly
264  * involved.  Much simpler to just not copy those live frames across.
265  */
266 
267 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
268 {
269 	unsigned long clone_flags = args->flags;
270 	unsigned long usp_thread_fn = args->stack;
271 	unsigned long tls = args->tls;
272 	struct pt_regs *childregs = task_pt_regs(p);
273 
274 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
275 	struct thread_info *ti;
276 #endif
277 
278 #if defined(__XTENSA_WINDOWED_ABI__)
279 	/* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
280 	SPILL_SLOT(childregs, 1) = (unsigned long)childregs;
281 	SPILL_SLOT(childregs, 0) = 0;
282 
283 	p->thread.sp = (unsigned long)childregs;
284 #elif defined(__XTENSA_CALL0_ABI__)
285 	/* Reserve 16 bytes for the _switch_to stack frame. */
286 	p->thread.sp = (unsigned long)childregs - 16;
287 #else
288 #error Unsupported Xtensa ABI
289 #endif
290 
291 	if (!args->fn) {
292 		struct pt_regs *regs = current_pt_regs();
293 		unsigned long usp = usp_thread_fn ?
294 			usp_thread_fn : regs->areg[1];
295 
296 		p->thread.ra = MAKE_RA_FOR_CALL(
297 				(unsigned long)ret_from_fork, 0x1);
298 
299 		*childregs = *regs;
300 		childregs->areg[1] = usp;
301 		childregs->areg[2] = 0;
302 
303 		/* When sharing memory with the parent thread, the child
304 		   usually starts on a pristine stack, so we have to reset
305 		   windowbase, windowstart and wmask.
306 		   (Note that such a new thread is required to always create
307 		   an initial call4 frame)
308 		   The exception is vfork, where the new thread continues to
309 		   run on the parent's stack until it calls execve. This could
310 		   be a call8 or call12, which requires a legal stack frame
311 		   of the previous caller for the overflow handlers to work.
312 		   (Note that it's always legal to overflow live registers).
313 		   In this case, ensure to spill at least the stack pointer
314 		   of that frame. */
315 
316 		if (clone_flags & CLONE_VM) {
317 			/* check that caller window is live and same stack */
318 			int len = childregs->wmask & ~0xf;
319 			if (regs->areg[1] == usp && len != 0) {
320 				int callinc = (regs->areg[0] >> 30) & 3;
321 				int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
322 				put_user(regs->areg[caller_ars+1],
323 					 (unsigned __user*)(usp - 12));
324 			}
325 			childregs->wmask = 1;
326 			childregs->windowstart = 1;
327 			childregs->windowbase = 0;
328 		}
329 
330 		if (clone_flags & CLONE_SETTLS)
331 			childregs->threadptr = tls;
332 	} else {
333 		p->thread.ra = MAKE_RA_FOR_CALL(
334 				(unsigned long)ret_from_kernel_thread, 1);
335 
336 		/* pass parameters to ret_from_kernel_thread: */
337 #if defined(__XTENSA_WINDOWED_ABI__)
338 		/*
339 		 * a2 = thread_fn, a3 = thread_fn arg.
340 		 * Window underflow will load registers from the
341 		 * spill slots on the stack on return from _switch_to.
342 		 */
343 		SPILL_SLOT(childregs, 2) = (unsigned long)args->fn;
344 		SPILL_SLOT(childregs, 3) = (unsigned long)args->fn_arg;
345 #elif defined(__XTENSA_CALL0_ABI__)
346 		/*
347 		 * a12 = thread_fn, a13 = thread_fn arg.
348 		 * _switch_to epilogue will load registers from the stack.
349 		 */
350 		((unsigned long *)p->thread.sp)[0] = (unsigned long)args->fn;
351 		((unsigned long *)p->thread.sp)[1] = (unsigned long)args->fn_arg;
352 #else
353 #error Unsupported Xtensa ABI
354 #endif
355 
356 		/* Childregs are only used when we're going to userspace
357 		 * in which case start_thread will set them up.
358 		 */
359 	}
360 
361 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
362 	ti = task_thread_info(p);
363 	ti->cpenable = 0;
364 #endif
365 
366 	clear_ptrace_hw_breakpoint(p);
367 
368 	return 0;
369 }
370 
371 
372 /*
373  * These bracket the sleeping functions..
374  */
375 
376 unsigned long __get_wchan(struct task_struct *p)
377 {
378 	unsigned long sp, pc;
379 	unsigned long stack_page = (unsigned long) task_stack_page(p);
380 	int count = 0;
381 
382 	sp = p->thread.sp;
383 	pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
384 
385 	do {
386 		if (sp < stack_page + sizeof(struct task_struct) ||
387 		    sp >= (stack_page + THREAD_SIZE) ||
388 		    pc == 0)
389 			return 0;
390 		if (!in_sched_functions(pc))
391 			return pc;
392 
393 		/* Stack layout: sp-4: ra, sp-3: sp' */
394 
395 		pc = MAKE_PC_FROM_RA(SPILL_SLOT(sp, 0), sp);
396 		sp = SPILL_SLOT(sp, 1);
397 	} while (count++ < 16);
398 	return 0;
399 }
400