]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - arch/xtensa/kernel/process.c
xtensa process.c must #include <linux/fs.h>
[mirror_ubuntu-hirsute-kernel.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/slab.h>
27 #include <linux/elf.h>
28 #include <linux/init.h>
29 #include <linux/prctl.h>
30 #include <linux/init_task.h>
31 #include <linux/module.h>
32 #include <linux/mqueue.h>
33 #include <linux/fs.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>
44 #include <asm/asm-offsets.h>
45 #include <asm/regs.h>
46
47 extern void ret_from_fork(void);
48
49 static struct fs_struct init_fs = INIT_FS;
50 static struct files_struct init_files = INIT_FILES;
51 static struct signal_struct init_signals = INIT_SIGNALS(init_signals);
52 static struct sighand_struct init_sighand = INIT_SIGHAND(init_sighand);
53 struct mm_struct init_mm = INIT_MM(init_mm);
54 EXPORT_SYMBOL(init_mm);
55
56 union thread_union init_thread_union
57 __attribute__((__section__(".data.init_task"))) =
58 { INIT_THREAD_INFO(init_task) };
59
60 struct task_struct init_task = INIT_TASK(init_task);
61 EXPORT_SYMBOL(init_task);
62
63 struct task_struct *current_set[NR_CPUS] = {&init_task, };
64
65 void (*pm_power_off)(void) = NULL;
66 EXPORT_SYMBOL(pm_power_off);
67
68
69 /*
70 * Powermanagement idle function, if any is provided by the platform.
71 */
72
73 void 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();
81 preempt_enable_no_resched();
82 schedule();
83 preempt_disable();
84 }
85 }
86
87 /*
88 * Free current thread data structures etc..
89 */
90
91 void exit_thread(void)
92 {
93 }
94
95 void flush_thread(void)
96 {
97 }
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
119 int 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. */
128 tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
129 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 * These bracket the sleeping functions..
165 */
166
167 unsigned long get_wchan(struct task_struct *p)
168 {
169 unsigned long sp, pc;
170 unsigned long stack_page = (unsigned long) task_stack_page(p);
171 int count = 0;
172
173 if (!p || p == current || p->state == TASK_RUNNING)
174 return 0;
175
176 sp = p->thread.sp;
177 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
178
179 do {
180 if (sp < stack_page + sizeof(struct task_struct) ||
181 sp >= (stack_page + THREAD_SIZE) ||
182 pc == 0)
183 return 0;
184 if (!in_sched_functions(pc))
185 return pc;
186
187 /* Stack layout: sp-4: ra, sp-3: sp' */
188
189 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
190 sp = *(unsigned long *)sp - 3;
191 } while (count++ < 16);
192 return 0;
193 }
194
195 /*
196 * do_copy_regs() gathers information from 'struct pt_regs' and
197 * 'current->thread.areg[]' to fill in the xtensa_gregset_t
198 * structure.
199 *
200 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
201 * of processor registers. Besides different ordering,
202 * xtensa_gregset_t contains non-live register information that
203 * 'struct pt_regs' does not. Exception handling (primarily) uses
204 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
205 *
206 */
207
208 void do_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
209 struct task_struct *tsk)
210 {
211 int i, n, wb_offset;
212
213 elfregs->xchal_config_id0 = XCHAL_HW_CONFIGID0;
214 elfregs->xchal_config_id1 = XCHAL_HW_CONFIGID1;
215
216 __asm__ __volatile__ ("rsr %0, 176\n" : "=a" (i));
217 elfregs->cpux = i;
218 __asm__ __volatile__ ("rsr %0, 208\n" : "=a" (i));
219 elfregs->cpuy = i;
220
221 /* Note: PS.EXCM is not set while user task is running; its
222 * being set in regs->ps is for exception handling convenience.
223 */
224
225 elfregs->pc = regs->pc;
226 elfregs->ps = (regs->ps & ~(1 << PS_EXCM_BIT));
227 elfregs->exccause = regs->exccause;
228 elfregs->excvaddr = regs->excvaddr;
229 elfregs->windowbase = regs->windowbase;
230 elfregs->windowstart = regs->windowstart;
231 elfregs->lbeg = regs->lbeg;
232 elfregs->lend = regs->lend;
233 elfregs->lcount = regs->lcount;
234 elfregs->sar = regs->sar;
235 elfregs->syscall = regs->syscall;
236
237 /* Copy register file.
238 * The layout looks like this:
239 *
240 * | a0 ... a15 | Z ... Z | arX ... arY |
241 * current window unused saved frames
242 */
243
244 memset (elfregs->ar, 0, sizeof(elfregs->ar));
245
246 wb_offset = regs->windowbase * 4;
247 n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
248
249 for (i = 0; i < n; i++)
250 elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
251
252 n = (regs->wmask >> 4) * 4;
253
254 for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
255 elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
256 }
257
258 void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
259 {
260 do_copy_regs ((xtensa_gregset_t *)elfregs, regs, current);
261 }
262
263
264 /* The inverse of do_copy_regs(). No error or sanity checking. */
265
266 void do_restore_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
267 struct task_struct *tsk)
268 {
269 int i, n, wb_offset;
270
271 /* Note: PS.EXCM is not set while user task is running; it
272 * needs to be set in regs->ps is for exception handling convenience.
273 */
274
275 regs->pc = elfregs->pc;
276 regs->ps = (elfregs->ps | (1 << PS_EXCM_BIT));
277 regs->exccause = elfregs->exccause;
278 regs->excvaddr = elfregs->excvaddr;
279 regs->windowbase = elfregs->windowbase;
280 regs->windowstart = elfregs->windowstart;
281 regs->lbeg = elfregs->lbeg;
282 regs->lend = elfregs->lend;
283 regs->lcount = elfregs->lcount;
284 regs->sar = elfregs->sar;
285 regs->syscall = elfregs->syscall;
286
287 /* Clear everything. */
288
289 memset (regs->areg, 0, sizeof(regs->areg));
290
291 /* Copy regs from live window frame. */
292
293 wb_offset = regs->windowbase * 4;
294 n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
295
296 for (i = 0; i < n; i++)
297 regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
298
299 n = (regs->wmask >> 4) * 4;
300
301 for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
302 regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
303 }
304
305 /*
306 * do_save_fpregs() gathers information from 'struct pt_regs' and
307 * 'current->thread' to fill in the elf_fpregset_t structure.
308 *
309 * Core files and ptrace use elf_fpregset_t.
310 */
311
312 void do_save_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
313 struct task_struct *tsk)
314 {
315 #if XCHAL_HAVE_CP
316
317 extern unsigned char _xtensa_reginfo_tables[];
318 extern unsigned _xtensa_reginfo_table_size;
319 int i;
320 unsigned long flags;
321
322 /* Before dumping coprocessor state from memory,
323 * ensure any live coprocessor contents for this
324 * task are first saved to memory:
325 */
326 local_irq_save(flags);
327
328 for (i = 0; i < XCHAL_CP_MAX; i++) {
329 if (tsk == coprocessor_info[i].owner) {
330 enable_coprocessor(i);
331 save_coprocessor_registers(
332 tsk->thread.cp_save+coprocessor_info[i].offset,i);
333 disable_coprocessor(i);
334 }
335 }
336
337 local_irq_restore(flags);
338
339 /* Now dump coprocessor & extra state: */
340 memcpy((unsigned char*)fpregs,
341 _xtensa_reginfo_tables, _xtensa_reginfo_table_size);
342 memcpy((unsigned char*)fpregs + _xtensa_reginfo_table_size,
343 tsk->thread.cp_save, XTENSA_CP_EXTRA_SIZE);
344 #endif
345 }
346
347 /*
348 * The inverse of do_save_fpregs().
349 * Copies coprocessor and extra state from fpregs into regs and tsk->thread.
350 * Returns 0 on success, non-zero if layout doesn't match.
351 */
352
353 int do_restore_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
354 struct task_struct *tsk)
355 {
356 #if XCHAL_HAVE_CP
357
358 extern unsigned char _xtensa_reginfo_tables[];
359 extern unsigned _xtensa_reginfo_table_size;
360 int i;
361 unsigned long flags;
362
363 /* Make sure save area layouts match.
364 * FIXME: in the future we could allow restoring from
365 * a different layout of the same registers, by comparing
366 * fpregs' table with _xtensa_reginfo_tables and matching
367 * entries and copying registers one at a time.
368 * Not too sure yet whether that's very useful.
369 */
370
371 if( memcmp((unsigned char*)fpregs,
372 _xtensa_reginfo_tables, _xtensa_reginfo_table_size) ) {
373 return -1;
374 }
375
376 /* Before restoring coprocessor state from memory,
377 * ensure any live coprocessor contents for this
378 * task are first invalidated.
379 */
380
381 local_irq_save(flags);
382
383 for (i = 0; i < XCHAL_CP_MAX; i++) {
384 if (tsk == coprocessor_info[i].owner) {
385 enable_coprocessor(i);
386 save_coprocessor_registers(
387 tsk->thread.cp_save+coprocessor_info[i].offset,i);
388 coprocessor_info[i].owner = 0;
389 disable_coprocessor(i);
390 }
391 }
392
393 local_irq_restore(flags);
394
395 /* Now restore coprocessor & extra state: */
396
397 memcpy(tsk->thread.cp_save,
398 (unsigned char*)fpregs + _xtensa_reginfo_table_size,
399 XTENSA_CP_EXTRA_SIZE);
400 #endif
401 return 0;
402 }
403 /*
404 * Fill in the CP structure for a core dump for a particular task.
405 */
406
407 int
408 dump_task_fpu(struct pt_regs *regs, struct task_struct *task, elf_fpregset_t *r)
409 {
410 return 0; /* no coprocessors active on this processor */
411 }
412
413 /*
414 * Fill in the CP structure for a core dump.
415 * This includes any FPU coprocessor.
416 * Here, we dump all coprocessors, and other ("extra") custom state.
417 *
418 * This function is called by elf_core_dump() in fs/binfmt_elf.c
419 * (in which case 'regs' comes from calls to do_coredump, see signals.c).
420 */
421 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
422 {
423 return dump_task_fpu(regs, current, r);
424 }
425
426 asmlinkage
427 long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
428 void __user *parent_tid, void *child_tls,
429 void __user *child_tid, long a5,
430 struct pt_regs *regs)
431 {
432 if (!newsp)
433 newsp = regs->areg[1];
434 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
435 }
436
437 /*
438 * * xtensa_execve() executes a new program.
439 * */
440
441 asmlinkage
442 long xtensa_execve(char __user *name, char __user * __user *argv,
443 char __user * __user *envp,
444 long a3, long a4, long a5,
445 struct pt_regs *regs)
446 {
447 long error;
448 char * filename;
449
450 filename = getname(name);
451 error = PTR_ERR(filename);
452 if (IS_ERR(filename))
453 goto out;
454 // FIXME: release coprocessor??
455 error = do_execve(filename, argv, envp, regs);
456 if (error == 0) {
457 task_lock(current);
458 current->ptrace &= ~PT_DTRACE;
459 task_unlock(current);
460 }
461 putname(filename);
462 out:
463 return error;
464 }
465