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