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