]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/xtensa/kernel/process.c
sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[mirror_ubuntu-artful-kernel.git] / arch / xtensa / kernel / process.c
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/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/stddef.h>
25 #include <linux/unistd.h>
26 #include <linux/ptrace.h>
27 #include <linux/elf.h>
28 #include <linux/hw_breakpoint.h>
29 #include <linux/init.h>
30 #include <linux/prctl.h>
31 #include <linux/init_task.h>
32 #include <linux/module.h>
33 #include <linux/mqueue.h>
34 #include <linux/fs.h>
35 #include <linux/slab.h>
36 #include <linux/rcupdate.h>
37
38 #include <asm/pgtable.h>
39 #include <linux/uaccess.h>
40 #include <asm/io.h>
41 #include <asm/processor.h>
42 #include <asm/platform.h>
43 #include <asm/mmu.h>
44 #include <asm/irq.h>
45 #include <linux/atomic.h>
46 #include <asm/asm-offsets.h>
47 #include <asm/regs.h>
48 #include <asm/hw_breakpoint.h>
49
50 extern void ret_from_fork(void);
51 extern void ret_from_kernel_thread(void);
52
53 struct task_struct *current_set[NR_CPUS] = {&init_task, };
54
55 void (*pm_power_off)(void) = NULL;
56 EXPORT_SYMBOL(pm_power_off);
57
58
59 #if XTENSA_HAVE_COPROCESSORS
60
61 void coprocessor_release_all(struct thread_info *ti)
62 {
63 unsigned long cpenable;
64 int i;
65
66 /* Make sure we don't switch tasks during this operation. */
67
68 preempt_disable();
69
70 /* Walk through all cp owners and release it for the requested one. */
71
72 cpenable = ti->cpenable;
73
74 for (i = 0; i < XCHAL_CP_MAX; i++) {
75 if (coprocessor_owner[i] == ti) {
76 coprocessor_owner[i] = 0;
77 cpenable &= ~(1 << i);
78 }
79 }
80
81 ti->cpenable = cpenable;
82 coprocessor_clear_cpenable();
83
84 preempt_enable();
85 }
86
87 void coprocessor_flush_all(struct thread_info *ti)
88 {
89 unsigned long cpenable;
90 int i;
91
92 preempt_disable();
93
94 cpenable = ti->cpenable;
95
96 for (i = 0; i < XCHAL_CP_MAX; i++) {
97 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
98 coprocessor_flush(ti, i);
99 cpenable >>= 1;
100 }
101
102 preempt_enable();
103 }
104
105 #endif
106
107
108 /*
109 * Powermanagement idle function, if any is provided by the platform.
110 */
111 void arch_cpu_idle(void)
112 {
113 platform_idle();
114 }
115
116 /*
117 * This is called when the thread calls exit().
118 */
119 void exit_thread(struct task_struct *tsk)
120 {
121 #if XTENSA_HAVE_COPROCESSORS
122 coprocessor_release_all(task_thread_info(tsk));
123 #endif
124 }
125
126 /*
127 * Flush thread state. This is called when a thread does an execve()
128 * Note that we flush coprocessor registers for the case execve fails.
129 */
130 void flush_thread(void)
131 {
132 #if XTENSA_HAVE_COPROCESSORS
133 struct thread_info *ti = current_thread_info();
134 coprocessor_flush_all(ti);
135 coprocessor_release_all(ti);
136 #endif
137 flush_ptrace_hw_breakpoint(current);
138 }
139
140 /*
141 * this gets called so that we can store coprocessor state into memory and
142 * copy the current task into the new thread.
143 */
144 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
145 {
146 #if XTENSA_HAVE_COPROCESSORS
147 coprocessor_flush_all(task_thread_info(src));
148 #endif
149 *dst = *src;
150 return 0;
151 }
152
153 /*
154 * Copy thread.
155 *
156 * There are two modes in which this function is called:
157 * 1) Userspace thread creation,
158 * regs != NULL, usp_thread_fn is userspace stack pointer.
159 * It is expected to copy parent regs (in case CLONE_VM is not set
160 * in the clone_flags) and set up passed usp in the childregs.
161 * 2) Kernel thread creation,
162 * regs == NULL, usp_thread_fn is the function to run in the new thread
163 * and thread_fn_arg is its parameter.
164 * childregs are not used for the kernel threads.
165 *
166 * The stack layout for the new thread looks like this:
167 *
168 * +------------------------+
169 * | childregs |
170 * +------------------------+ <- thread.sp = sp in dummy-frame
171 * | dummy-frame | (saved in dummy-frame spill-area)
172 * +------------------------+
173 *
174 * We create a dummy frame to return to either ret_from_fork or
175 * ret_from_kernel_thread:
176 * a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
177 * sp points to itself (thread.sp)
178 * a2, a3 are unused for userspace threads,
179 * a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
180 *
181 * Note: This is a pristine frame, so we don't need any spill region on top of
182 * childregs.
183 *
184 * The fun part: if we're keeping the same VM (i.e. cloning a thread,
185 * not an entire process), we're normally given a new usp, and we CANNOT share
186 * any live address register windows. If we just copy those live frames over,
187 * the two threads (parent and child) will overflow the same frames onto the
188 * parent stack at different times, likely corrupting the parent stack (esp.
189 * if the parent returns from functions that called clone() and calls new
190 * ones, before the child overflows its now old copies of its parent windows).
191 * One solution is to spill windows to the parent stack, but that's fairly
192 * involved. Much simpler to just not copy those live frames across.
193 */
194
195 int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
196 unsigned long thread_fn_arg, struct task_struct *p)
197 {
198 struct pt_regs *childregs = task_pt_regs(p);
199
200 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
201 struct thread_info *ti;
202 #endif
203
204 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
205 *((int*)childregs - 3) = (unsigned long)childregs;
206 *((int*)childregs - 4) = 0;
207
208 p->thread.sp = (unsigned long)childregs;
209
210 if (!(p->flags & PF_KTHREAD)) {
211 struct pt_regs *regs = current_pt_regs();
212 unsigned long usp = usp_thread_fn ?
213 usp_thread_fn : regs->areg[1];
214
215 p->thread.ra = MAKE_RA_FOR_CALL(
216 (unsigned long)ret_from_fork, 0x1);
217
218 /* This does not copy all the regs.
219 * In a bout of brilliance or madness,
220 * ARs beyond a0-a15 exist past the end of the struct.
221 */
222 *childregs = *regs;
223 childregs->areg[1] = usp;
224 childregs->areg[2] = 0;
225
226 /* When sharing memory with the parent thread, the child
227 usually starts on a pristine stack, so we have to reset
228 windowbase, windowstart and wmask.
229 (Note that such a new thread is required to always create
230 an initial call4 frame)
231 The exception is vfork, where the new thread continues to
232 run on the parent's stack until it calls execve. This could
233 be a call8 or call12, which requires a legal stack frame
234 of the previous caller for the overflow handlers to work.
235 (Note that it's always legal to overflow live registers).
236 In this case, ensure to spill at least the stack pointer
237 of that frame. */
238
239 if (clone_flags & CLONE_VM) {
240 /* check that caller window is live and same stack */
241 int len = childregs->wmask & ~0xf;
242 if (regs->areg[1] == usp && len != 0) {
243 int callinc = (regs->areg[0] >> 30) & 3;
244 int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
245 put_user(regs->areg[caller_ars+1],
246 (unsigned __user*)(usp - 12));
247 }
248 childregs->wmask = 1;
249 childregs->windowstart = 1;
250 childregs->windowbase = 0;
251 } else {
252 int len = childregs->wmask & ~0xf;
253 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
254 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
255 }
256
257 /* The thread pointer is passed in the '4th argument' (= a5) */
258 if (clone_flags & CLONE_SETTLS)
259 childregs->threadptr = childregs->areg[5];
260 } else {
261 p->thread.ra = MAKE_RA_FOR_CALL(
262 (unsigned long)ret_from_kernel_thread, 1);
263
264 /* pass parameters to ret_from_kernel_thread:
265 * a2 = thread_fn, a3 = thread_fn arg
266 */
267 *((int *)childregs - 1) = thread_fn_arg;
268 *((int *)childregs - 2) = usp_thread_fn;
269
270 /* Childregs are only used when we're going to userspace
271 * in which case start_thread will set them up.
272 */
273 }
274
275 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
276 ti = task_thread_info(p);
277 ti->cpenable = 0;
278 #endif
279
280 clear_ptrace_hw_breakpoint(p);
281
282 return 0;
283 }
284
285
286 /*
287 * These bracket the sleeping functions..
288 */
289
290 unsigned long get_wchan(struct task_struct *p)
291 {
292 unsigned long sp, pc;
293 unsigned long stack_page = (unsigned long) task_stack_page(p);
294 int count = 0;
295
296 if (!p || p == current || p->state == TASK_RUNNING)
297 return 0;
298
299 sp = p->thread.sp;
300 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
301
302 do {
303 if (sp < stack_page + sizeof(struct task_struct) ||
304 sp >= (stack_page + THREAD_SIZE) ||
305 pc == 0)
306 return 0;
307 if (!in_sched_functions(pc))
308 return pc;
309
310 /* Stack layout: sp-4: ra, sp-3: sp' */
311
312 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
313 sp = *(unsigned long *)sp - 3;
314 } while (count++ < 16);
315 return 0;
316 }
317
318 /*
319 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
320 * of processor registers. Besides different ordering,
321 * xtensa_gregset_t contains non-live register information that
322 * 'struct pt_regs' does not. Exception handling (primarily) uses
323 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
324 *
325 */
326
327 void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
328 {
329 unsigned long wb, ws, wm;
330 int live, last;
331
332 wb = regs->windowbase;
333 ws = regs->windowstart;
334 wm = regs->wmask;
335 ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
336
337 /* Don't leak any random bits. */
338
339 memset(elfregs, 0, sizeof(*elfregs));
340
341 /* Note: PS.EXCM is not set while user task is running; its
342 * being set in regs->ps is for exception handling convenience.
343 */
344
345 elfregs->pc = regs->pc;
346 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
347 elfregs->lbeg = regs->lbeg;
348 elfregs->lend = regs->lend;
349 elfregs->lcount = regs->lcount;
350 elfregs->sar = regs->sar;
351 elfregs->windowstart = ws;
352
353 live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
354 last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
355 memcpy(elfregs->a, regs->areg, live * 4);
356 memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
357 }
358
359 int dump_fpu(void)
360 {
361 return 0;
362 }