vfs: define struct filename and have getname() return it
[cascardo/linux.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/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/stddef.h>
24 #include <linux/unistd.h>
25 #include <linux/ptrace.h>
26 #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>
32 #include <linux/fs.h>
33 #include <linux/slab.h>
34 #include <linux/rcupdate.h>
35
36 #include <asm/pgtable.h>
37 #include <asm/uaccess.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>
43 #include <linux/atomic.h>
44 #include <asm/asm-offsets.h>
45 #include <asm/regs.h>
46
47 extern void ret_from_fork(void);
48
49 struct task_struct *current_set[NR_CPUS] = {&init_task, };
50
51 void (*pm_power_off)(void) = NULL;
52 EXPORT_SYMBOL(pm_power_off);
53
54
55 #if XTENSA_HAVE_COPROCESSORS
56
57 void 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
83 void 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
104 /*
105  * Powermanagement idle function, if any is provided by the platform.
106  */
107
108 void cpu_idle(void)
109 {
110         local_irq_enable();
111
112         /* endless idle loop with no priority at all */
113         while (1) {
114                 rcu_idle_enter();
115                 while (!need_resched())
116                         platform_idle();
117                 rcu_idle_exit();
118                 schedule_preempt_disabled();
119         }
120 }
121
122 /*
123  * This is called when the thread calls exit().
124  */
125 void exit_thread(void)
126 {
127 #if XTENSA_HAVE_COPROCESSORS
128         coprocessor_release_all(current_thread_info());
129 #endif
130 }
131
132 /*
133  * Flush thread state. This is called when a thread does an execve()
134  * Note that we flush coprocessor registers for the case execve fails.
135  */
136 void flush_thread(void)
137 {
138 #if XTENSA_HAVE_COPROCESSORS
139         struct thread_info *ti = current_thread_info();
140         coprocessor_flush_all(ti);
141         coprocessor_release_all(ti);
142 #endif
143 }
144
145 /*
146  * this gets called so that we can store coprocessor state into memory and
147  * copy the current task into the new thread.
148  */
149 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
150 {
151 #if XTENSA_HAVE_COPROCESSORS
152         coprocessor_flush_all(task_thread_info(src));
153 #endif
154         *dst = *src;
155         return 0;
156 }
157
158 /*
159  * Copy thread.
160  *
161  * The stack layout for the new thread looks like this:
162  *
163  *      +------------------------+ <- sp in childregs (= tos)
164  *      |       childregs        |
165  *      +------------------------+ <- thread.sp = sp in dummy-frame
166  *      |      dummy-frame       |    (saved in dummy-frame spill-area)
167  *      +------------------------+
168  *
169  * We create a dummy frame to return to ret_from_fork:
170  *   a0 points to ret_from_fork (simulating a call4)
171  *   sp points to itself (thread.sp)
172  *   a2, a3 are unused.
173  *
174  * Note: This is a pristine frame, so we don't need any spill region on top of
175  *       childregs.
176  */
177
178 int copy_thread(unsigned long clone_flags, unsigned long usp,
179                 unsigned long unused,
180                 struct task_struct * p, struct pt_regs * regs)
181 {
182         struct pt_regs *childregs;
183         struct thread_info *ti;
184         unsigned long tos;
185         int user_mode = user_mode(regs);
186
187         /* Set up new TSS. */
188         tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
189         if (user_mode)
190                 childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
191         else
192                 childregs = (struct pt_regs*)tos - 1;
193
194         *childregs = *regs;
195
196         /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
197         *((int*)childregs - 3) = (unsigned long)childregs;
198         *((int*)childregs - 4) = 0;
199
200         childregs->areg[1] = tos;
201         childregs->areg[2] = 0;
202         p->set_child_tid = p->clear_child_tid = NULL;
203         p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
204         p->thread.sp = (unsigned long)childregs;
205
206         if (user_mode(regs)) {
207
208                 int len = childregs->wmask & ~0xf;
209                 childregs->areg[1] = usp;
210                 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
211                        &regs->areg[XCHAL_NUM_AREGS - len/4], len);
212 // FIXME: we need to set THREADPTR in thread_info...
213                 if (clone_flags & CLONE_SETTLS)
214                         childregs->areg[2] = childregs->areg[6];
215
216         } else {
217                 /* In kernel space, we start a new thread with a new stack. */
218                 childregs->wmask = 1;
219         }
220
221 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
222         ti = task_thread_info(p);
223         ti->cpenable = 0;
224 #endif
225
226         return 0;
227 }
228
229
230 /*
231  * These bracket the sleeping functions..
232  */
233
234 unsigned long get_wchan(struct task_struct *p)
235 {
236         unsigned long sp, pc;
237         unsigned long stack_page = (unsigned long) task_stack_page(p);
238         int count = 0;
239
240         if (!p || p == current || p->state == TASK_RUNNING)
241                 return 0;
242
243         sp = p->thread.sp;
244         pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
245
246         do {
247                 if (sp < stack_page + sizeof(struct task_struct) ||
248                     sp >= (stack_page + THREAD_SIZE) ||
249                     pc == 0)
250                         return 0;
251                 if (!in_sched_functions(pc))
252                         return pc;
253
254                 /* Stack layout: sp-4: ra, sp-3: sp' */
255
256                 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
257                 sp = *(unsigned long *)sp - 3;
258         } while (count++ < 16);
259         return 0;
260 }
261
262 /*
263  * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
264  * of processor registers.  Besides different ordering,
265  * xtensa_gregset_t contains non-live register information that
266  * 'struct pt_regs' does not.  Exception handling (primarily) uses
267  * 'struct pt_regs'.  Core files and ptrace use xtensa_gregset_t.
268  *
269  */
270
271 void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
272 {
273         unsigned long wb, ws, wm;
274         int live, last;
275
276         wb = regs->windowbase;
277         ws = regs->windowstart;
278         wm = regs->wmask;
279         ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);
280
281         /* Don't leak any random bits. */
282
283         memset(elfregs, 0, sizeof(*elfregs));
284
285         /* Note:  PS.EXCM is not set while user task is running; its
286          * being set in regs->ps is for exception handling convenience.
287          */
288
289         elfregs->pc             = regs->pc;
290         elfregs->ps             = (regs->ps & ~(1 << PS_EXCM_BIT));
291         elfregs->lbeg           = regs->lbeg;
292         elfregs->lend           = regs->lend;
293         elfregs->lcount         = regs->lcount;
294         elfregs->sar            = regs->sar;
295         elfregs->windowstart    = ws;
296
297         live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
298         last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
299         memcpy(elfregs->a, regs->areg, live * 4);
300         memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
301 }
302
303 int dump_fpu(void)
304 {
305         return 0;
306 }
307
308 asmlinkage
309 long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
310                   void __user *parent_tid, void *child_tls,
311                   void __user *child_tid, long a5,
312                   struct pt_regs *regs)
313 {
314         if (!newsp)
315                 newsp = regs->areg[1];
316         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
317 }
318
319 /*
320  * xtensa_execve() executes a new program.
321  */
322
323 asmlinkage
324 long xtensa_execve(const char __user *name,
325                    const char __user *const __user *argv,
326                    const char __user *const __user *envp,
327                    long a3, long a4, long a5,
328                    struct pt_regs *regs)
329 {
330         long error;
331         struct filename *filename;
332
333         filename = getname(name);
334         error = PTR_ERR(filename);
335         if (IS_ERR(filename))
336                 goto out;
337         error = do_execve(filename->name, argv, envp, regs);
338         putname(filename);
339 out:
340         return error;
341 }
342