blob: 3785f3481d71811c384011444daaabf67fba0653 [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>
23#include <linux/smp_lock.h>
24#include <linux/stddef.h>
25#include <linux/unistd.h>
26#include <linux/ptrace.h>
27#include <linux/slab.h>
28#include <linux/elf.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
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>
43#include <asm/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
49static struct fs_struct init_fs = INIT_FS;
50static struct files_struct init_files = INIT_FILES;
51static struct signal_struct init_signals = INIT_SIGNALS(init_signals);
52static struct sighand_struct init_sighand = INIT_SIGHAND(init_sighand);
53struct mm_struct init_mm = INIT_MM(init_mm);
54EXPORT_SYMBOL(init_mm);
55
56union thread_union init_thread_union
57 __attribute__((__section__(".data.init_task"))) =
58{ INIT_THREAD_INFO(init_task) };
59
60struct task_struct init_task = INIT_TASK(init_task);
61EXPORT_SYMBOL(init_task);
62
63struct task_struct *current_set[NR_CPUS] = {&init_task, };
64
Adrian Bunk47f3fc92006-03-06 15:42:47 -080065void (*pm_power_off)(void) = NULL;
66EXPORT_SYMBOL(pm_power_off);
67
Chris Zankel5a0015d2005-06-23 22:01:16 -070068
Chris Zankel5a0015d2005-06-23 22:01:16 -070069/*
70 * Powermanagement idle function, if any is provided by the platform.
71 */
72
73void cpu_idle(void)
74{
75 local_irq_enable();
76
77 /* endless idle loop with no priority at all */
78 while (1) {
79 while (!need_resched())
80 platform_idle();
Nick Piggin5bfb5d62005-11-08 21:39:01 -080081 preempt_enable_no_resched();
Chris Zankel5a0015d2005-06-23 22:01:16 -070082 schedule();
Nick Piggin5bfb5d62005-11-08 21:39:01 -080083 preempt_disable();
Chris Zankel5a0015d2005-06-23 22:01:16 -070084 }
85}
86
87/*
88 * Free current thread data structures etc..
89 */
90
91void exit_thread(void)
92{
Chris Zankel5a0015d2005-06-23 22:01:16 -070093}
94
95void flush_thread(void)
96{
Chris Zankel5a0015d2005-06-23 22:01:16 -070097}
98
99/*
100 * Copy thread.
101 *
102 * The stack layout for the new thread looks like this:
103 *
104 * +------------------------+ <- sp in childregs (= tos)
105 * | childregs |
106 * +------------------------+ <- thread.sp = sp in dummy-frame
107 * | dummy-frame | (saved in dummy-frame spill-area)
108 * +------------------------+
109 *
110 * We create a dummy frame to return to ret_from_fork:
111 * a0 points to ret_from_fork (simulating a call4)
112 * sp points to itself (thread.sp)
113 * a2, a3 are unused.
114 *
115 * Note: This is a pristine frame, so we don't need any spill region on top of
116 * childregs.
117 */
118
119int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
120 unsigned long unused,
121 struct task_struct * p, struct pt_regs * regs)
122{
123 struct pt_regs *childregs;
124 unsigned long tos;
125 int user_mode = user_mode(regs);
126
127 /* Set up new TSS. */
Al Viro04fe6fa2006-01-12 01:05:50 -0800128 tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700129 if (user_mode)
130 childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
131 else
132 childregs = (struct pt_regs*)tos - 1;
133
134 *childregs = *regs;
135
136 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
137 *((int*)childregs - 3) = (unsigned long)childregs;
138 *((int*)childregs - 4) = 0;
139
140 childregs->areg[1] = tos;
141 childregs->areg[2] = 0;
142 p->set_child_tid = p->clear_child_tid = NULL;
143 p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
144 p->thread.sp = (unsigned long)childregs;
145 if (user_mode(regs)) {
146
147 int len = childregs->wmask & ~0xf;
148 childregs->areg[1] = usp;
149 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
150 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
151
152 if (clone_flags & CLONE_SETTLS)
153 childregs->areg[2] = childregs->areg[6];
154
155 } else {
156 /* In kernel space, we start a new thread with a new stack. */
157 childregs->wmask = 1;
158 }
159 return 0;
160}
161
162
163/*
164 * Create a kernel thread
165 */
166
167int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
168{
169 long retval;
170 __asm__ __volatile__
171 ("mov a5, %4\n\t" /* preserve fn in a5 */
172 "mov a6, %3\n\t" /* preserve and setup arg in a6 */
173 "movi a2, %1\n\t" /* load __NR_clone for syscall*/
174 "mov a3, sp\n\t" /* sp check and sys_clone */
175 "mov a4, %5\n\t" /* load flags for syscall */
176 "syscall\n\t"
177 "beq a3, sp, 1f\n\t" /* branch if parent */
178 "callx4 a5\n\t" /* call fn */
179 "movi a2, %2\n\t" /* load __NR_exit for syscall */
180 "mov a3, a6\n\t" /* load fn return value */
181 "syscall\n"
182 "1:\n\t"
183 "mov %0, a2\n\t" /* parent returns zero */
184 :"=r" (retval)
185 :"i" (__NR_clone), "i" (__NR_exit),
186 "r" (arg), "r" (fn),
187 "r" (flags | CLONE_VM)
188 : "a2", "a3", "a4", "a5", "a6" );
189 return retval;
190}
191
192
193/*
194 * These bracket the sleeping functions..
195 */
196
197unsigned long get_wchan(struct task_struct *p)
198{
199 unsigned long sp, pc;
Al Viro04fe6fa2006-01-12 01:05:50 -0800200 unsigned long stack_page = (unsigned long) task_stack_page(p);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700201 int count = 0;
202
203 if (!p || p == current || p->state == TASK_RUNNING)
204 return 0;
205
206 sp = p->thread.sp;
207 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
208
209 do {
210 if (sp < stack_page + sizeof(struct task_struct) ||
211 sp >= (stack_page + THREAD_SIZE) ||
212 pc == 0)
213 return 0;
214 if (!in_sched_functions(pc))
215 return pc;
216
217 /* Stack layout: sp-4: ra, sp-3: sp' */
218
219 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
220 sp = *(unsigned long *)sp - 3;
221 } while (count++ < 16);
222 return 0;
223}
224
225/*
226 * do_copy_regs() gathers information from 'struct pt_regs' and
227 * 'current->thread.areg[]' to fill in the xtensa_gregset_t
228 * structure.
229 *
230 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
231 * of processor registers. Besides different ordering,
232 * xtensa_gregset_t contains non-live register information that
233 * 'struct pt_regs' does not. Exception handling (primarily) uses
234 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
235 *
236 */
237
238void do_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
239 struct task_struct *tsk)
240{
241 int i, n, wb_offset;
242
243 elfregs->xchal_config_id0 = XCHAL_HW_CONFIGID0;
244 elfregs->xchal_config_id1 = XCHAL_HW_CONFIGID1;
245
246 __asm__ __volatile__ ("rsr %0, 176\n" : "=a" (i));
247 elfregs->cpux = i;
248 __asm__ __volatile__ ("rsr %0, 208\n" : "=a" (i));
249 elfregs->cpuy = i;
250
251 /* Note: PS.EXCM is not set while user task is running; its
252 * being set in regs->ps is for exception handling convenience.
253 */
254
255 elfregs->pc = regs->pc;
Chris Zankel173d6682006-12-10 02:18:48 -0800256 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700257 elfregs->exccause = regs->exccause;
258 elfregs->excvaddr = regs->excvaddr;
259 elfregs->windowbase = regs->windowbase;
260 elfregs->windowstart = regs->windowstart;
261 elfregs->lbeg = regs->lbeg;
262 elfregs->lend = regs->lend;
263 elfregs->lcount = regs->lcount;
264 elfregs->sar = regs->sar;
265 elfregs->syscall = regs->syscall;
266
267 /* Copy register file.
268 * The layout looks like this:
269 *
270 * | a0 ... a15 | Z ... Z | arX ... arY |
271 * current window unused saved frames
272 */
273
274 memset (elfregs->ar, 0, sizeof(elfregs->ar));
275
276 wb_offset = regs->windowbase * 4;
277 n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
278
279 for (i = 0; i < n; i++)
280 elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
281
282 n = (regs->wmask >> 4) * 4;
283
284 for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
285 elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
286}
287
288void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
289{
290 do_copy_regs ((xtensa_gregset_t *)elfregs, regs, current);
291}
292
293
294/* The inverse of do_copy_regs(). No error or sanity checking. */
295
296void do_restore_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
297 struct task_struct *tsk)
298{
299 int i, n, wb_offset;
300
301 /* Note: PS.EXCM is not set while user task is running; it
302 * needs to be set in regs->ps is for exception handling convenience.
303 */
304
305 regs->pc = elfregs->pc;
Chris Zankel173d6682006-12-10 02:18:48 -0800306 regs->ps = (elfregs->ps | (1 << PS_EXCM_BIT));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700307 regs->exccause = elfregs->exccause;
308 regs->excvaddr = elfregs->excvaddr;
309 regs->windowbase = elfregs->windowbase;
310 regs->windowstart = elfregs->windowstart;
311 regs->lbeg = elfregs->lbeg;
312 regs->lend = elfregs->lend;
313 regs->lcount = elfregs->lcount;
314 regs->sar = elfregs->sar;
315 regs->syscall = elfregs->syscall;
316
317 /* Clear everything. */
318
319 memset (regs->areg, 0, sizeof(regs->areg));
320
321 /* Copy regs from live window frame. */
322
323 wb_offset = regs->windowbase * 4;
324 n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
325
326 for (i = 0; i < n; i++)
327 regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
328
329 n = (regs->wmask >> 4) * 4;
330
331 for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
332 regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
333}
334
335/*
336 * do_save_fpregs() gathers information from 'struct pt_regs' and
337 * 'current->thread' to fill in the elf_fpregset_t structure.
338 *
339 * Core files and ptrace use elf_fpregset_t.
340 */
341
342void do_save_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
343 struct task_struct *tsk)
344{
345#if XCHAL_HAVE_CP
346
347 extern unsigned char _xtensa_reginfo_tables[];
348 extern unsigned _xtensa_reginfo_table_size;
349 int i;
350 unsigned long flags;
351
352 /* Before dumping coprocessor state from memory,
353 * ensure any live coprocessor contents for this
354 * task are first saved to memory:
355 */
356 local_irq_save(flags);
357
358 for (i = 0; i < XCHAL_CP_MAX; i++) {
359 if (tsk == coprocessor_info[i].owner) {
360 enable_coprocessor(i);
361 save_coprocessor_registers(
362 tsk->thread.cp_save+coprocessor_info[i].offset,i);
363 disable_coprocessor(i);
364 }
365 }
366
367 local_irq_restore(flags);
368
369 /* Now dump coprocessor & extra state: */
370 memcpy((unsigned char*)fpregs,
371 _xtensa_reginfo_tables, _xtensa_reginfo_table_size);
372 memcpy((unsigned char*)fpregs + _xtensa_reginfo_table_size,
373 tsk->thread.cp_save, XTENSA_CP_EXTRA_SIZE);
374#endif
375}
376
377/*
378 * The inverse of do_save_fpregs().
379 * Copies coprocessor and extra state from fpregs into regs and tsk->thread.
380 * Returns 0 on success, non-zero if layout doesn't match.
381 */
382
383int do_restore_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
384 struct task_struct *tsk)
385{
386#if XCHAL_HAVE_CP
387
388 extern unsigned char _xtensa_reginfo_tables[];
389 extern unsigned _xtensa_reginfo_table_size;
390 int i;
391 unsigned long flags;
392
393 /* Make sure save area layouts match.
394 * FIXME: in the future we could allow restoring from
395 * a different layout of the same registers, by comparing
396 * fpregs' table with _xtensa_reginfo_tables and matching
397 * entries and copying registers one at a time.
398 * Not too sure yet whether that's very useful.
399 */
400
401 if( memcmp((unsigned char*)fpregs,
402 _xtensa_reginfo_tables, _xtensa_reginfo_table_size) ) {
403 return -1;
404 }
405
406 /* Before restoring coprocessor state from memory,
407 * ensure any live coprocessor contents for this
408 * task are first invalidated.
409 */
410
411 local_irq_save(flags);
412
413 for (i = 0; i < XCHAL_CP_MAX; i++) {
414 if (tsk == coprocessor_info[i].owner) {
415 enable_coprocessor(i);
416 save_coprocessor_registers(
417 tsk->thread.cp_save+coprocessor_info[i].offset,i);
418 coprocessor_info[i].owner = 0;
419 disable_coprocessor(i);
420 }
421 }
422
423 local_irq_restore(flags);
424
425 /* Now restore coprocessor & extra state: */
426
427 memcpy(tsk->thread.cp_save,
428 (unsigned char*)fpregs + _xtensa_reginfo_table_size,
429 XTENSA_CP_EXTRA_SIZE);
430#endif
431 return 0;
432}
433/*
434 * Fill in the CP structure for a core dump for a particular task.
435 */
436
437int
438dump_task_fpu(struct pt_regs *regs, struct task_struct *task, elf_fpregset_t *r)
439{
Chris Zankel5a0015d2005-06-23 22:01:16 -0700440 return 0; /* no coprocessors active on this processor */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700441}
442
443/*
444 * Fill in the CP structure for a core dump.
445 * This includes any FPU coprocessor.
446 * Here, we dump all coprocessors, and other ("extra") custom state.
447 *
448 * This function is called by elf_core_dump() in fs/binfmt_elf.c
449 * (in which case 'regs' comes from calls to do_coredump, see signals.c).
450 */
451int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
452{
453 return dump_task_fpu(regs, current, r);
454}