blob: 2c8d6a3d250afd2d18fa8b41b03dc26b0ad97ca2 [file] [log] [blame]
Chris Zankel5a0015d2005-06-23 22:01:16 -07001/*
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
Chris Zankel5a0015d2005-06-23 22:01:16 -070018#include <linux/errno.h>
19#include <linux/sched.h>
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/smp.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070023#include <linux/stddef.h>
24#include <linux/unistd.h>
25#include <linux/ptrace.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070026#include <linux/elf.h>
27#include <linux/init.h>
28#include <linux/prctl.h>
29#include <linux/init_task.h>
30#include <linux/module.h>
31#include <linux/mqueue.h>
Chris Zankel73089cb2007-08-04 09:27:30 -070032#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070034
35#include <asm/pgtable.h>
36#include <asm/uaccess.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070037#include <asm/io.h>
38#include <asm/processor.h>
39#include <asm/platform.h>
40#include <asm/mmu.h>
41#include <asm/irq.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Sam Ravnborg0013a852005-09-09 20:57:26 +020043#include <asm/asm-offsets.h>
Chris Zankel173d6682006-12-10 02:18:48 -080044#include <asm/regs.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070045
46extern void ret_from_fork(void);
47
Chris Zankel5a0015d2005-06-23 22:01:16 -070048struct task_struct *current_set[NR_CPUS] = {&init_task, };
49
Adrian Bunk47f3fc92006-03-06 15:42:47 -080050void (*pm_power_off)(void) = NULL;
51EXPORT_SYMBOL(pm_power_off);
52
Chris Zankel5a0015d2005-06-23 22:01:16 -070053
Chris Zankelc658eac2008-02-12 13:17:07 -080054#if XTENSA_HAVE_COPROCESSORS
55
56void coprocessor_release_all(struct thread_info *ti)
57{
58 unsigned long cpenable;
59 int i;
60
61 /* Make sure we don't switch tasks during this operation. */
62
63 preempt_disable();
64
65 /* Walk through all cp owners and release it for the requested one. */
66
67 cpenable = ti->cpenable;
68
69 for (i = 0; i < XCHAL_CP_MAX; i++) {
70 if (coprocessor_owner[i] == ti) {
71 coprocessor_owner[i] = 0;
72 cpenable &= ~(1 << i);
73 }
74 }
75
76 ti->cpenable = cpenable;
77 coprocessor_clear_cpenable();
78
79 preempt_enable();
80}
81
82void coprocessor_flush_all(struct thread_info *ti)
83{
84 unsigned long cpenable;
85 int i;
86
87 preempt_disable();
88
89 cpenable = ti->cpenable;
90
91 for (i = 0; i < XCHAL_CP_MAX; i++) {
92 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
93 coprocessor_flush(ti, i);
94 cpenable >>= 1;
95 }
96
97 preempt_enable();
98}
99
100#endif
101
102
Chris Zankel5a0015d2005-06-23 22:01:16 -0700103/*
104 * Powermanagement idle function, if any is provided by the platform.
105 */
106
107void cpu_idle(void)
108{
109 local_irq_enable();
110
111 /* endless idle loop with no priority at all */
112 while (1) {
113 while (!need_resched())
114 platform_idle();
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +0100115 schedule_preempt_disabled();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700116 }
117}
118
119/*
Chris Zankelc658eac2008-02-12 13:17:07 -0800120 * This is called when the thread calls exit().
Chris Zankel5a0015d2005-06-23 22:01:16 -0700121 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700122void exit_thread(void)
123{
Chris Zankelc658eac2008-02-12 13:17:07 -0800124#if XTENSA_HAVE_COPROCESSORS
125 coprocessor_release_all(current_thread_info());
126#endif
Chris Zankel5a0015d2005-06-23 22:01:16 -0700127}
128
Chris Zankelc658eac2008-02-12 13:17:07 -0800129/*
130 * Flush thread state. This is called when a thread does an execve()
131 * Note that we flush coprocessor registers for the case execve fails.
132 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700133void flush_thread(void)
134{
Chris Zankelc658eac2008-02-12 13:17:07 -0800135#if XTENSA_HAVE_COPROCESSORS
136 struct thread_info *ti = current_thread_info();
137 coprocessor_flush_all(ti);
138 coprocessor_release_all(ti);
139#endif
140}
141
142/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700143 * this gets called so that we can store coprocessor state into memory and
144 * copy the current task into the new thread.
Chris Zankelc658eac2008-02-12 13:17:07 -0800145 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700146int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Chris Zankelc658eac2008-02-12 13:17:07 -0800147{
148#if XTENSA_HAVE_COPROCESSORS
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700149 coprocessor_flush_all(task_thread_info(src));
Chris Zankelc658eac2008-02-12 13:17:07 -0800150#endif
Suresh Siddha55ccf3f2012-05-16 15:03:51 -0700151 *dst = *src;
152 return 0;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700153}
154
155/*
156 * Copy thread.
157 *
158 * The stack layout for the new thread looks like this:
159 *
160 * +------------------------+ <- sp in childregs (= tos)
161 * | childregs |
162 * +------------------------+ <- thread.sp = sp in dummy-frame
163 * | dummy-frame | (saved in dummy-frame spill-area)
164 * +------------------------+
165 *
166 * We create a dummy frame to return to ret_from_fork:
167 * a0 points to ret_from_fork (simulating a call4)
168 * sp points to itself (thread.sp)
169 * a2, a3 are unused.
170 *
171 * Note: This is a pristine frame, so we don't need any spill region on top of
172 * childregs.
173 */
174
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700175int copy_thread(unsigned long clone_flags, unsigned long usp,
Chris Zankel5a0015d2005-06-23 22:01:16 -0700176 unsigned long unused,
177 struct task_struct * p, struct pt_regs * regs)
178{
179 struct pt_regs *childregs;
Chris Zankelc658eac2008-02-12 13:17:07 -0800180 struct thread_info *ti;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700181 unsigned long tos;
182 int user_mode = user_mode(regs);
183
184 /* Set up new TSS. */
Al Viro04fe6fa2006-01-12 01:05:50 -0800185 tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700186 if (user_mode)
187 childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
188 else
189 childregs = (struct pt_regs*)tos - 1;
190
191 *childregs = *regs;
192
193 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
194 *((int*)childregs - 3) = (unsigned long)childregs;
195 *((int*)childregs - 4) = 0;
196
197 childregs->areg[1] = tos;
198 childregs->areg[2] = 0;
199 p->set_child_tid = p->clear_child_tid = NULL;
200 p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
201 p->thread.sp = (unsigned long)childregs;
Chris Zankelc658eac2008-02-12 13:17:07 -0800202
Chris Zankel5a0015d2005-06-23 22:01:16 -0700203 if (user_mode(regs)) {
204
205 int len = childregs->wmask & ~0xf;
206 childregs->areg[1] = usp;
207 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
208 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
Chris Zankelc658eac2008-02-12 13:17:07 -0800209// FIXME: we need to set THREADPTR in thread_info...
Chris Zankel5a0015d2005-06-23 22:01:16 -0700210 if (clone_flags & CLONE_SETTLS)
211 childregs->areg[2] = childregs->areg[6];
212
213 } else {
214 /* In kernel space, we start a new thread with a new stack. */
215 childregs->wmask = 1;
216 }
Chris Zankelc658eac2008-02-12 13:17:07 -0800217
218#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
219 ti = task_thread_info(p);
220 ti->cpenable = 0;
221#endif
222
Chris Zankel5a0015d2005-06-23 22:01:16 -0700223 return 0;
224}
225
226
227/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700228 * These bracket the sleeping functions..
229 */
230
231unsigned long get_wchan(struct task_struct *p)
232{
233 unsigned long sp, pc;
Al Viro04fe6fa2006-01-12 01:05:50 -0800234 unsigned long stack_page = (unsigned long) task_stack_page(p);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700235 int count = 0;
236
237 if (!p || p == current || p->state == TASK_RUNNING)
238 return 0;
239
240 sp = p->thread.sp;
241 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
242
243 do {
244 if (sp < stack_page + sizeof(struct task_struct) ||
245 sp >= (stack_page + THREAD_SIZE) ||
246 pc == 0)
247 return 0;
248 if (!in_sched_functions(pc))
249 return pc;
250
251 /* Stack layout: sp-4: ra, sp-3: sp' */
252
253 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
254 sp = *(unsigned long *)sp - 3;
255 } while (count++ < 16);
256 return 0;
257}
258
259/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700260 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
261 * of processor registers. Besides different ordering,
262 * xtensa_gregset_t contains non-live register information that
263 * 'struct pt_regs' does not. Exception handling (primarily) uses
264 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
265 *
266 */
267
Chris Zankelc658eac2008-02-12 13:17:07 -0800268void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700269{
Chris Zankelc658eac2008-02-12 13:17:07 -0800270 unsigned long wb, ws, wm;
271 int live, last;
272
273 wb = regs->windowbase;
274 ws = regs->windowstart;
275 wm = regs->wmask;
276 ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
277
278 /* Don't leak any random bits. */
279
Alan Cox688bb412012-07-11 14:02:50 -0700280 memset(elfregs, 0, sizeof(*elfregs));
Chris Zankelc658eac2008-02-12 13:17:07 -0800281
Chris Zankel5a0015d2005-06-23 22:01:16 -0700282 /* Note: PS.EXCM is not set while user task is running; its
283 * being set in regs->ps is for exception handling convenience.
284 */
285
286 elfregs->pc = regs->pc;
Chris Zankel173d6682006-12-10 02:18:48 -0800287 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700288 elfregs->lbeg = regs->lbeg;
289 elfregs->lend = regs->lend;
290 elfregs->lcount = regs->lcount;
291 elfregs->sar = regs->sar;
Chris Zankelc658eac2008-02-12 13:17:07 -0800292 elfregs->windowstart = ws;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700293
Chris Zankelc658eac2008-02-12 13:17:07 -0800294 live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
295 last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
296 memcpy(elfregs->a, regs->areg, live * 4);
297 memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700298}
299
Chris Zankelc658eac2008-02-12 13:17:07 -0800300int dump_fpu(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700301{
Chris Zankel5a0015d2005-06-23 22:01:16 -0700302 return 0;
303}
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800304
305asmlinkage
306long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
307 void __user *parent_tid, void *child_tls,
308 void __user *child_tid, long a5,
309 struct pt_regs *regs)
310{
311 if (!newsp)
312 newsp = regs->areg[1];
313 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
314}
315
316/*
Chris Zankelc658eac2008-02-12 13:17:07 -0800317 * xtensa_execve() executes a new program.
318 */
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800319
320asmlinkage
David Howellsd7627462010-08-17 23:52:56 +0100321long xtensa_execve(const char __user *name,
322 const char __user *const __user *argv,
323 const char __user *const __user *envp,
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800324 long a3, long a4, long a5,
325 struct pt_regs *regs)
326{
327 long error;
328 char * filename;
329
330 filename = getname(name);
331 error = PTR_ERR(filename);
332 if (IS_ERR(filename))
333 goto out;
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800334 error = do_execve(filename, argv, envp, regs);
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800335 putname(filename);
336out:
337 return error;
338}
339