blob: 2c9004770c4e1d0ca3aa63a10128510bbccab659 [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>
37#include <asm/system.h>
38#include <asm/io.h>
39#include <asm/processor.h>
40#include <asm/platform.h>
41#include <asm/mmu.h>
42#include <asm/irq.h>
Arun Sharma600634972011-07-26 16:09:06 -070043#include <linux/atomic.h>
Sam Ravnborg0013a852005-09-09 20:57:26 +020044#include <asm/asm-offsets.h>
Chris Zankel173d6682006-12-10 02:18:48 -080045#include <asm/regs.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070046
47extern void ret_from_fork(void);
48
Chris Zankel5a0015d2005-06-23 22:01:16 -070049struct task_struct *current_set[NR_CPUS] = {&init_task, };
50
Adrian Bunk47f3fc92006-03-06 15:42:47 -080051void (*pm_power_off)(void) = NULL;
52EXPORT_SYMBOL(pm_power_off);
53
Chris Zankel5a0015d2005-06-23 22:01:16 -070054
Chris Zankelc658eac2008-02-12 13:17:07 -080055#if XTENSA_HAVE_COPROCESSORS
56
57void coprocessor_release_all(struct thread_info *ti)
58{
59 unsigned long cpenable;
60 int i;
61
62 /* Make sure we don't switch tasks during this operation. */
63
64 preempt_disable();
65
66 /* Walk through all cp owners and release it for the requested one. */
67
68 cpenable = ti->cpenable;
69
70 for (i = 0; i < XCHAL_CP_MAX; i++) {
71 if (coprocessor_owner[i] == ti) {
72 coprocessor_owner[i] = 0;
73 cpenable &= ~(1 << i);
74 }
75 }
76
77 ti->cpenable = cpenable;
78 coprocessor_clear_cpenable();
79
80 preempt_enable();
81}
82
83void coprocessor_flush_all(struct thread_info *ti)
84{
85 unsigned long cpenable;
86 int i;
87
88 preempt_disable();
89
90 cpenable = ti->cpenable;
91
92 for (i = 0; i < XCHAL_CP_MAX; i++) {
93 if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
94 coprocessor_flush(ti, i);
95 cpenable >>= 1;
96 }
97
98 preempt_enable();
99}
100
101#endif
102
103
Chris Zankel5a0015d2005-06-23 22:01:16 -0700104/*
105 * Powermanagement idle function, if any is provided by the platform.
106 */
107
108void cpu_idle(void)
109{
110 local_irq_enable();
111
112 /* endless idle loop with no priority at all */
113 while (1) {
114 while (!need_resched())
115 platform_idle();
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +0100116 schedule_preempt_disabled();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700117 }
118}
119
120/*
Chris Zankelc658eac2008-02-12 13:17:07 -0800121 * This is called when the thread calls exit().
Chris Zankel5a0015d2005-06-23 22:01:16 -0700122 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700123void exit_thread(void)
124{
Chris Zankelc658eac2008-02-12 13:17:07 -0800125#if XTENSA_HAVE_COPROCESSORS
126 coprocessor_release_all(current_thread_info());
127#endif
Chris Zankel5a0015d2005-06-23 22:01:16 -0700128}
129
Chris Zankelc658eac2008-02-12 13:17:07 -0800130/*
131 * Flush thread state. This is called when a thread does an execve()
132 * Note that we flush coprocessor registers for the case execve fails.
133 */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700134void flush_thread(void)
135{
Chris Zankelc658eac2008-02-12 13:17:07 -0800136#if XTENSA_HAVE_COPROCESSORS
137 struct thread_info *ti = current_thread_info();
138 coprocessor_flush_all(ti);
139 coprocessor_release_all(ti);
140#endif
141}
142
143/*
144 * This is called before the thread is copied.
145 */
146void prepare_to_copy(struct task_struct *tsk)
147{
148#if XTENSA_HAVE_COPROCESSORS
149 coprocessor_flush_all(task_thread_info(tsk));
150#endif
Chris Zankel5a0015d2005-06-23 22:01:16 -0700151}
152
153/*
154 * Copy thread.
155 *
156 * The stack layout for the new thread looks like this:
157 *
158 * +------------------------+ <- sp in childregs (= tos)
159 * | childregs |
160 * +------------------------+ <- thread.sp = sp in dummy-frame
161 * | dummy-frame | (saved in dummy-frame spill-area)
162 * +------------------------+
163 *
164 * We create a dummy frame to return to ret_from_fork:
165 * a0 points to ret_from_fork (simulating a call4)
166 * sp points to itself (thread.sp)
167 * a2, a3 are unused.
168 *
169 * Note: This is a pristine frame, so we don't need any spill region on top of
170 * childregs.
171 */
172
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700173int copy_thread(unsigned long clone_flags, unsigned long usp,
Chris Zankel5a0015d2005-06-23 22:01:16 -0700174 unsigned long unused,
175 struct task_struct * p, struct pt_regs * regs)
176{
177 struct pt_regs *childregs;
Chris Zankelc658eac2008-02-12 13:17:07 -0800178 struct thread_info *ti;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700179 unsigned long tos;
180 int user_mode = user_mode(regs);
181
182 /* Set up new TSS. */
Al Viro04fe6fa2006-01-12 01:05:50 -0800183 tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700184 if (user_mode)
185 childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
186 else
187 childregs = (struct pt_regs*)tos - 1;
188
189 *childregs = *regs;
190
191 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
192 *((int*)childregs - 3) = (unsigned long)childregs;
193 *((int*)childregs - 4) = 0;
194
195 childregs->areg[1] = tos;
196 childregs->areg[2] = 0;
197 p->set_child_tid = p->clear_child_tid = NULL;
198 p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
199 p->thread.sp = (unsigned long)childregs;
Chris Zankelc658eac2008-02-12 13:17:07 -0800200
Chris Zankel5a0015d2005-06-23 22:01:16 -0700201 if (user_mode(regs)) {
202
203 int len = childregs->wmask & ~0xf;
204 childregs->areg[1] = usp;
205 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
206 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
Chris Zankelc658eac2008-02-12 13:17:07 -0800207// FIXME: we need to set THREADPTR in thread_info...
Chris Zankel5a0015d2005-06-23 22:01:16 -0700208 if (clone_flags & CLONE_SETTLS)
209 childregs->areg[2] = childregs->areg[6];
210
211 } else {
212 /* In kernel space, we start a new thread with a new stack. */
213 childregs->wmask = 1;
214 }
Chris Zankelc658eac2008-02-12 13:17:07 -0800215
216#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
217 ti = task_thread_info(p);
218 ti->cpenable = 0;
219#endif
220
Chris Zankel5a0015d2005-06-23 22:01:16 -0700221 return 0;
222}
223
224
225/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700226 * These bracket the sleeping functions..
227 */
228
229unsigned long get_wchan(struct task_struct *p)
230{
231 unsigned long sp, pc;
Al Viro04fe6fa2006-01-12 01:05:50 -0800232 unsigned long stack_page = (unsigned long) task_stack_page(p);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700233 int count = 0;
234
235 if (!p || p == current || p->state == TASK_RUNNING)
236 return 0;
237
238 sp = p->thread.sp;
239 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
240
241 do {
242 if (sp < stack_page + sizeof(struct task_struct) ||
243 sp >= (stack_page + THREAD_SIZE) ||
244 pc == 0)
245 return 0;
246 if (!in_sched_functions(pc))
247 return pc;
248
249 /* Stack layout: sp-4: ra, sp-3: sp' */
250
251 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
252 sp = *(unsigned long *)sp - 3;
253 } while (count++ < 16);
254 return 0;
255}
256
257/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700258 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
259 * of processor registers. Besides different ordering,
260 * xtensa_gregset_t contains non-live register information that
261 * 'struct pt_regs' does not. Exception handling (primarily) uses
262 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
263 *
264 */
265
Chris Zankelc658eac2008-02-12 13:17:07 -0800266void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700267{
Chris Zankelc658eac2008-02-12 13:17:07 -0800268 unsigned long wb, ws, wm;
269 int live, last;
270
271 wb = regs->windowbase;
272 ws = regs->windowstart;
273 wm = regs->wmask;
274 ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
275
276 /* Don't leak any random bits. */
277
278 memset(elfregs, 0, sizeof (elfregs));
279
Chris Zankel5a0015d2005-06-23 22:01:16 -0700280 /* Note: PS.EXCM is not set while user task is running; its
281 * being set in regs->ps is for exception handling convenience.
282 */
283
284 elfregs->pc = regs->pc;
Chris Zankel173d6682006-12-10 02:18:48 -0800285 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700286 elfregs->lbeg = regs->lbeg;
287 elfregs->lend = regs->lend;
288 elfregs->lcount = regs->lcount;
289 elfregs->sar = regs->sar;
Chris Zankelc658eac2008-02-12 13:17:07 -0800290 elfregs->windowstart = ws;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700291
Chris Zankelc658eac2008-02-12 13:17:07 -0800292 live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
293 last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
294 memcpy(elfregs->a, regs->areg, live * 4);
295 memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700296}
297
Chris Zankelc658eac2008-02-12 13:17:07 -0800298int dump_fpu(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700299{
Chris Zankel5a0015d2005-06-23 22:01:16 -0700300 return 0;
301}
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800302
303asmlinkage
304long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
305 void __user *parent_tid, void *child_tls,
306 void __user *child_tid, long a5,
307 struct pt_regs *regs)
308{
309 if (!newsp)
310 newsp = regs->areg[1];
311 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
312}
313
314/*
Chris Zankelc658eac2008-02-12 13:17:07 -0800315 * xtensa_execve() executes a new program.
316 */
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800317
318asmlinkage
David Howellsd7627462010-08-17 23:52:56 +0100319long xtensa_execve(const char __user *name,
320 const char __user *const __user *argv,
321 const char __user *const __user *envp,
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800322 long a3, long a4, long a5,
323 struct pt_regs *regs)
324{
325 long error;
326 char * filename;
327
328 filename = getname(name);
329 error = PTR_ERR(filename);
330 if (IS_ERR(filename))
331 goto out;
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800332 error = do_execve(filename, argv, envp, regs);
Chris Zankelfc4fb2a2006-12-10 02:18:52 -0800333 putname(filename);
334out:
335 return error;
336}
337