xref: /openbmc/linux/arch/xtensa/kernel/process.c (revision cbdf59ad)
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 <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 
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 coprocessor_release_all(struct thread_info *ti)
68 {
69 	unsigned long cpenable;
70 	int i;
71 
72 	/* Make sure we don't switch tasks during this operation. */
73 
74 	preempt_disable();
75 
76 	/* Walk through all cp owners and release it for the requested one. */
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 
94 void 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  * Powermanagement idle function, if any is provided by the platform.
120  */
121 void arch_cpu_idle(void)
122 {
123 	platform_idle();
124 }
125 
126 /*
127  * This is called when the thread calls exit().
128  */
129 void 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  * Flush thread state. This is called when a thread does an execve()
138  * Note that we flush coprocessor registers for the case execve fails.
139  */
140 void 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  * this gets called so that we can store coprocessor state into memory and
152  * copy the current task into the new thread.
153  */
154 int 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  * Copy thread.
165  *
166  * There are two modes in which this function is called:
167  * 1) Userspace thread creation,
168  *    regs != NULL, usp_thread_fn is userspace stack pointer.
169  *    It is expected to copy parent regs (in case CLONE_VM is not set
170  *    in the clone_flags) and set up passed usp in the childregs.
171  * 2) Kernel thread creation,
172  *    regs == NULL, usp_thread_fn is the function to run in the new thread
173  *    and thread_fn_arg is its parameter.
174  *    childregs are not used for the kernel threads.
175  *
176  * The stack layout for the new thread looks like this:
177  *
178  *	+------------------------+
179  *	|       childregs        |
180  *	+------------------------+ <- thread.sp = sp in dummy-frame
181  *	|      dummy-frame       |    (saved in dummy-frame spill-area)
182  *	+------------------------+
183  *
184  * We create a dummy frame to return to either ret_from_fork or
185  *   ret_from_kernel_thread:
186  *   a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
187  *   sp points to itself (thread.sp)
188  *   a2, a3 are unused for userspace threads,
189  *   a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
190  *
191  * Note: This is a pristine frame, so we don't need any spill region on top of
192  *       childregs.
193  *
194  * The fun part:  if we're keeping the same VM (i.e. cloning a thread,
195  * not an entire process), we're normally given a new usp, and we CANNOT share
196  * any live address register windows.  If we just copy those live frames over,
197  * the two threads (parent and child) will overflow the same frames onto the
198  * parent stack at different times, likely corrupting the parent stack (esp.
199  * if the parent returns from functions that called clone() and calls new
200  * ones, before the child overflows its now old copies of its parent windows).
201  * One solution is to spill windows to the parent stack, but that's fairly
202  * involved.  Much simpler to just not copy those live frames across.
203  */
204 
205 int 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 	/* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
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 		/* This does not copy all the regs.
229 		 * In a bout of brilliance or madness,
230 		 * ARs beyond a0-a15 exist past the end of the struct.
231 		 */
232 		*childregs = *regs;
233 		childregs->areg[1] = usp;
234 		childregs->areg[2] = 0;
235 
236 		/* When sharing memory with the parent thread, the child
237 		   usually starts on a pristine stack, so we have to reset
238 		   windowbase, windowstart and wmask.
239 		   (Note that such a new thread is required to always create
240 		   an initial call4 frame)
241 		   The exception is vfork, where the new thread continues to
242 		   run on the parent's stack until it calls execve. This could
243 		   be a call8 or call12, which requires a legal stack frame
244 		   of the previous caller for the overflow handlers to work.
245 		   (Note that it's always legal to overflow live registers).
246 		   In this case, ensure to spill at least the stack pointer
247 		   of that frame. */
248 
249 		if (clone_flags & CLONE_VM) {
250 			/* check that caller window is live and same stack */
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 			       &regs->areg[XCHAL_NUM_AREGS - len/4], len);
265 		}
266 
267 		/* The thread pointer is passed in the '4th argument' (= a5) */
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 		/* pass parameters to ret_from_kernel_thread:
275 		 * a2 = thread_fn, a3 = thread_fn arg
276 		 */
277 		SPILL_SLOT(childregs, 3) = thread_fn_arg;
278 		SPILL_SLOT(childregs, 2) = usp_thread_fn;
279 
280 		/* Childregs are only used when we're going to userspace
281 		 * in which case start_thread will set them up.
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  * These bracket the sleeping functions..
298  */
299 
300 unsigned 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 		/* Stack layout: sp-4: ra, sp-3: sp' */
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