]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/sh/kernel/process.c
doc: Add SH to vdso and earlyprintk in kernel-parameters.txt
[mirror_ubuntu-zesty-kernel.git] / arch / sh / kernel / process.c
CommitLineData
aec5e0e1
PM
1/*
2 * arch/sh/kernel/process.c
1da177e4 3 *
aec5e0e1 4 * This file handles the architecture-dependent parts of process handling..
1da177e4
LT
5 *
6 * Copyright (C) 1995 Linus Torvalds
7 *
8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
8ae91b9a 9 * Copyright (C) 2006 Lineo Solutions Inc. support SH4A UBC
aec5e0e1 10 * Copyright (C) 2002 - 2006 Paul Mundt
1da177e4 11 */
1da177e4 12#include <linux/module.h>
1da177e4
LT
13#include <linux/mm.h>
14#include <linux/elfcore.h>
a3310bbd 15#include <linux/pm.h>
1da177e4 16#include <linux/kallsyms.h>
a3310bbd 17#include <linux/kexec.h>
1da177e4
LT
18#include <asm/uaccess.h>
19#include <asm/mmu_context.h>
b5233d07 20#include <asm/ubc.h>
1da177e4 21
aec5e0e1 22static int hlt_counter;
1da177e4
LT
23int ubc_usercnt = 0;
24
25#define HARD_IDLE_TIMEOUT (HZ / 3)
26
a3310bbd 27void (*pm_idle)(void);
a3310bbd
PM
28void (*pm_power_off)(void);
29EXPORT_SYMBOL(pm_power_off);
30
1da177e4
LT
31void disable_hlt(void)
32{
33 hlt_counter++;
34}
1da177e4
LT
35EXPORT_SYMBOL(disable_hlt);
36
37void enable_hlt(void)
38{
39 hlt_counter--;
40}
1da177e4
LT
41EXPORT_SYMBOL(enable_hlt);
42
a3310bbd
PM
43void default_idle(void)
44{
45 if (!hlt_counter)
46 cpu_sleep();
47 else
48 cpu_relax();
49}
50
64c7c8f8 51void cpu_idle(void)
1da177e4
LT
52{
53 /* endless idle loop with no priority at all */
54 while (1) {
a3310bbd
PM
55 void (*idle)(void) = pm_idle;
56
57 if (!idle)
58 idle = default_idle;
59
60 while (!need_resched())
61 idle();
1da177e4 62
5bfb5d69 63 preempt_enable_no_resched();
1da177e4 64 schedule();
5bfb5d69 65 preempt_disable();
1da177e4
LT
66 }
67}
68
1da177e4
LT
69void machine_restart(char * __unused)
70{
71 /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
72 asm volatile("ldc %0, sr\n\t"
73 "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
74}
75
1da177e4
LT
76void machine_halt(void)
77{
a3310bbd 78 local_irq_disable();
1da177e4 79
1da177e4
LT
80 while (1)
81 cpu_sleep();
82}
83
1da177e4
LT
84void machine_power_off(void)
85{
a3310bbd
PM
86 if (pm_power_off)
87 pm_power_off();
1da177e4
LT
88}
89
1da177e4
LT
90void show_regs(struct pt_regs * regs)
91{
92 printk("\n");
93 printk("Pid : %d, Comm: %20s\n", current->pid, current->comm);
6b002230 94 print_symbol("PC is at %s\n", instruction_pointer(regs));
1da177e4
LT
95 printk("PC : %08lx SP : %08lx SR : %08lx ",
96 regs->pc, regs->regs[15], regs->sr);
97#ifdef CONFIG_MMU
98 printk("TEA : %08x ", ctrl_inl(MMU_TEA));
99#else
100 printk(" ");
101#endif
102 printk("%s\n", print_tainted());
103
104 printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
105 regs->regs[0],regs->regs[1],
106 regs->regs[2],regs->regs[3]);
107 printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
108 regs->regs[4],regs->regs[5],
109 regs->regs[6],regs->regs[7]);
110 printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
111 regs->regs[8],regs->regs[9],
112 regs->regs[10],regs->regs[11]);
113 printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
114 regs->regs[12],regs->regs[13],
115 regs->regs[14]);
116 printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
117 regs->mach, regs->macl, regs->gbr, regs->pr);
118
6b002230 119 show_trace(NULL, (unsigned long *)regs->regs[15], regs);
1da177e4
LT
120}
121
122/*
123 * Create a kernel thread
124 */
125
126/*
127 * This is the mechanism for creating a new kernel thread.
128 *
129 */
130extern void kernel_thread_helper(void);
131__asm__(".align 5\n"
132 "kernel_thread_helper:\n\t"
133 "jsr @r5\n\t"
134 " nop\n\t"
135 "mov.l 1f, r1\n\t"
136 "jsr @r1\n\t"
137 " mov r0, r4\n\t"
138 ".align 2\n\t"
139 "1:.long do_exit");
140
aec5e0e1 141/* Don't use this in BL=1(cli). Or else, CPU resets! */
1da177e4 142int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
aec5e0e1 143{
1da177e4
LT
144 struct pt_regs regs;
145
146 memset(&regs, 0, sizeof(regs));
aec5e0e1
PM
147 regs.regs[4] = (unsigned long)arg;
148 regs.regs[5] = (unsigned long)fn;
1da177e4 149
aec5e0e1 150 regs.pc = (unsigned long)kernel_thread_helper;
1da177e4
LT
151 regs.sr = (1 << 30);
152
153 /* Ok, create the new process.. */
aec5e0e1
PM
154 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
155 &regs, 0, NULL, NULL);
1da177e4
LT
156}
157
158/*
159 * Free current thread data structures etc..
160 */
161void exit_thread(void)
162{
163 if (current->thread.ubc_pc) {
164 current->thread.ubc_pc = 0;
165 ubc_usercnt -= 1;
166 }
167}
168
169void flush_thread(void)
170{
171#if defined(CONFIG_SH_FPU)
172 struct task_struct *tsk = current;
1da177e4 173 /* Forget lazy FPU state */
3cf0f4ec 174 clear_fpu(tsk, task_pt_regs(tsk));
1da177e4
LT
175 clear_used_math();
176#endif
177}
178
179void release_thread(struct task_struct *dead_task)
180{
181 /* do nothing */
182}
183
184/* Fill in the fpu structure for a core dump.. */
185int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
186{
187 int fpvalid = 0;
188
189#if defined(CONFIG_SH_FPU)
190 struct task_struct *tsk = current;
191
192 fpvalid = !!tsk_used_math(tsk);
193 if (fpvalid) {
194 unlazy_fpu(tsk, regs);
195 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
196 }
197#endif
198
199 return fpvalid;
200}
201
aec5e0e1 202/*
1da177e4
LT
203 * Capture the user space registers if the task is not running (in user space)
204 */
205int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
206{
207 struct pt_regs ptregs;
aec5e0e1 208
3cf0f4ec 209 ptregs = *task_pt_regs(tsk);
1da177e4
LT
210 elf_core_copy_regs(regs, &ptregs);
211
212 return 1;
213}
214
aec5e0e1 215int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpu)
1da177e4
LT
216{
217 int fpvalid = 0;
218
219#if defined(CONFIG_SH_FPU)
220 fpvalid = !!tsk_used_math(tsk);
221 if (fpvalid) {
3cf0f4ec 222 unlazy_fpu(tsk, task_pt_regs(tsk));
1da177e4
LT
223 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
224 }
225#endif
226
227 return fpvalid;
228}
229
230asmlinkage void ret_from_fork(void);
231
232int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
233 unsigned long unused,
234 struct task_struct *p, struct pt_regs *regs)
235{
2991be72 236 struct thread_info *ti = task_thread_info(p);
1da177e4
LT
237 struct pt_regs *childregs;
238#if defined(CONFIG_SH_FPU)
239 struct task_struct *tsk = current;
240
241 unlazy_fpu(tsk, regs);
242 p->thread.fpu = tsk->thread.fpu;
243 copy_to_stopped_child_used_math(p);
244#endif
245
3cf0f4ec 246 childregs = task_pt_regs(p);
1da177e4
LT
247 *childregs = *regs;
248
249 if (user_mode(regs)) {
250 childregs->regs[15] = usp;
2991be72 251 ti->addr_limit = USER_DS;
1da177e4 252 } else {
aec5e0e1
PM
253 childregs->regs[15] = (unsigned long)task_stack_page(p) +
254 THREAD_SIZE;
2991be72 255 ti->addr_limit = KERNEL_DS;
1da177e4 256 }
aec5e0e1
PM
257
258 if (clone_flags & CLONE_SETTLS)
1da177e4 259 childregs->gbr = childregs->regs[0];
aec5e0e1 260
1da177e4
LT
261 childregs->regs[0] = 0; /* Set return value for child */
262
263 p->thread.sp = (unsigned long) childregs;
264 p->thread.pc = (unsigned long) ret_from_fork;
265
266 p->thread.ubc_pc = 0;
267
268 return 0;
269}
270
1da177e4 271/* Tracing by user break controller. */
aec5e0e1 272static void ubc_set_tracing(int asid, unsigned long pc)
1da177e4 273{
8ae91b9a
RS
274#if defined(CONFIG_CPU_SH4A)
275 unsigned long val;
276
277 val = (UBC_CBR_ID_INST | UBC_CBR_RW_READ | UBC_CBR_CE);
278 val |= (UBC_CBR_AIE | UBC_CBR_AIV_SET(asid));
279
280 ctrl_outl(val, UBC_CBR0);
281 ctrl_outl(pc, UBC_CAR0);
282 ctrl_outl(0x0, UBC_CAMR0);
283 ctrl_outl(0x0, UBC_CBCR);
284
285 val = (UBC_CRR_RES | UBC_CRR_PCB | UBC_CRR_BIE);
286 ctrl_outl(val, UBC_CRR0);
287
aec5e0e1 288 /* Read UBC register that we wrote last, for checking update */
8ae91b9a
RS
289 val = ctrl_inl(UBC_CRR0);
290
291#else /* CONFIG_CPU_SH4A */
1da177e4
LT
292 ctrl_outl(pc, UBC_BARA);
293
a2d1a5fa 294#ifdef CONFIG_MMU
1da177e4 295 /* We don't have any ASID settings for the SH-2! */
11c19656 296 if (current_cpu_data.type != CPU_SH7604)
1da177e4 297 ctrl_outb(asid, UBC_BASRA);
a2d1a5fa 298#endif
1da177e4
LT
299
300 ctrl_outl(0, UBC_BAMRA);
301
11c19656
PM
302 if (current_cpu_data.type == CPU_SH7729 ||
303 current_cpu_data.type == CPU_SH7710) {
1da177e4
LT
304 ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
305 ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
306 } else {
307 ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
308 ctrl_outw(BRCR_PCBA, UBC_BRCR);
309 }
8ae91b9a 310#endif /* CONFIG_CPU_SH4A */
1da177e4
LT
311}
312
313/*
314 * switch_to(x,y) should switch tasks from x to y.
315 *
316 */
aec5e0e1
PM
317struct task_struct *__switch_to(struct task_struct *prev,
318 struct task_struct *next)
1da177e4
LT
319{
320#if defined(CONFIG_SH_FPU)
3cf0f4ec 321 unlazy_fpu(prev, task_pt_regs(prev));
1da177e4
LT
322#endif
323
324#ifdef CONFIG_PREEMPT
325 {
326 unsigned long flags;
327 struct pt_regs *regs;
328
329 local_irq_save(flags);
3cf0f4ec 330 regs = task_pt_regs(prev);
1da177e4
LT
331 if (user_mode(regs) && regs->regs[15] >= 0xc0000000) {
332 int offset = (int)regs->regs[15];
333
334 /* Reset stack pointer: clear critical region mark */
335 regs->regs[15] = regs->regs[1];
336 if (regs->pc < regs->regs[0])
337 /* Go to rewind point */
338 regs->pc = regs->regs[0] + offset;
339 }
340 local_irq_restore(flags);
341 }
342#endif
343
a2d1a5fa 344#ifdef CONFIG_MMU
1da177e4
LT
345 /*
346 * Restore the kernel mode register
aec5e0e1 347 * k7 (r7_bank1)
1da177e4
LT
348 */
349 asm volatile("ldc %0, r7_bank"
350 : /* no output */
cafcfcaa 351 : "r" (task_thread_info(next)));
a2d1a5fa 352#endif
1da177e4 353
1da177e4
LT
354 /* If no tasks are using the UBC, we're done */
355 if (ubc_usercnt == 0)
356 /* If no tasks are using the UBC, we're done */;
357 else if (next->thread.ubc_pc && next->mm) {
a2d1a5fa
YS
358 int asid = 0;
359#ifdef CONFIG_MMU
aec5e0e1 360 asid |= cpu_asid(smp_processor_id(), next->mm);
a2d1a5fa
YS
361#endif
362 ubc_set_tracing(asid, next->thread.ubc_pc);
1da177e4 363 } else {
8ae91b9a
RS
364#if defined(CONFIG_CPU_SH4A)
365 ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
366 ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
367#else
1da177e4
LT
368 ctrl_outw(0, UBC_BBRA);
369 ctrl_outw(0, UBC_BBRB);
8ae91b9a 370#endif
1da177e4 371 }
1da177e4
LT
372
373 return prev;
374}
375
376asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
377 unsigned long r6, unsigned long r7,
f0bc814c 378 struct pt_regs __regs)
1da177e4 379{
f0bc814c 380 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
1da177e4 381#ifdef CONFIG_MMU
f0bc814c 382 return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
1da177e4
LT
383#else
384 /* fork almost works, enough to trick you into looking elsewhere :-( */
385 return -EINVAL;
386#endif
387}
388
389asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
390 unsigned long parent_tidptr,
391 unsigned long child_tidptr,
f0bc814c 392 struct pt_regs __regs)
1da177e4 393{
f0bc814c 394 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
1da177e4 395 if (!newsp)
f0bc814c
SM
396 newsp = regs->regs[15];
397 return do_fork(clone_flags, newsp, regs, 0,
aec5e0e1
PM
398 (int __user *)parent_tidptr,
399 (int __user *)child_tidptr);
1da177e4
LT
400}
401
402/*
403 * This is trivial, and on the face of it looks like it
404 * could equally well be done in user mode.
405 *
406 * Not so, for quite unobvious reasons - register pressure.
407 * In user mode vfork() cannot have a stack frame, and if
408 * done by calling the "clone()" system call directly, you
409 * do not have enough call-clobbered registers to hold all
410 * the information you need.
411 */
412asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
413 unsigned long r6, unsigned long r7,
f0bc814c 414 struct pt_regs __regs)
1da177e4 415{
f0bc814c
SM
416 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
417 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
1da177e4
LT
418 0, NULL, NULL);
419}
420
421/*
422 * sys_execve() executes a new program.
423 */
424asmlinkage int sys_execve(char *ufilename, char **uargv,
425 char **uenvp, unsigned long r7,
f0bc814c 426 struct pt_regs __regs)
1da177e4 427{
f0bc814c 428 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
1da177e4
LT
429 int error;
430 char *filename;
431
432 filename = getname((char __user *)ufilename);
433 error = PTR_ERR(filename);
434 if (IS_ERR(filename))
435 goto out;
436
437 error = do_execve(filename,
438 (char __user * __user *)uargv,
439 (char __user * __user *)uenvp,
f0bc814c 440 regs);
1da177e4
LT
441 if (error == 0) {
442 task_lock(current);
443 current->ptrace &= ~PT_DTRACE;
444 task_unlock(current);
445 }
446 putname(filename);
447out:
448 return error;
449}
450
451unsigned long get_wchan(struct task_struct *p)
452{
453 unsigned long schedule_frame;
454 unsigned long pc;
455
456 if (!p || p == current || p->state == TASK_RUNNING)
457 return 0;
458
459 /*
460 * The same comment as on the Alpha applies here, too ...
461 */
462 pc = thread_saved_pc(p);
463 if (in_sched_functions(pc)) {
b652c23c
PM
464 schedule_frame = (unsigned long)p->thread.sp;
465 return ((unsigned long *)schedule_frame)[21];
1da177e4 466 }
b652c23c 467
1da177e4
LT
468 return pc;
469}
470
f0bc814c 471asmlinkage void break_point_trap(void)
1da177e4
LT
472{
473 /* Clear tracing. */
8ae91b9a
RS
474#if defined(CONFIG_CPU_SH4A)
475 ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
476 ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
477#else
1da177e4
LT
478 ctrl_outw(0, UBC_BBRA);
479 ctrl_outw(0, UBC_BBRB);
8ae91b9a 480#endif
1da177e4
LT
481 current->thread.ubc_pc = 0;
482 ubc_usercnt -= 1;
483
484 force_sig(SIGTRAP, current);
485}
486
f413d0d9
PM
487/*
488 * Generic trap handler.
489 */
490asmlinkage void debug_trap_handler(unsigned long r4, unsigned long r5,
491 unsigned long r6, unsigned long r7,
492 struct pt_regs __regs)
493{
494 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
495
496 /* Rewind */
497 regs->pc -= 2;
498
499 force_sig(SIGTRAP, current);
500}
501
502/*
503 * Special handler for BUG() traps.
504 */
505asmlinkage void bug_trap_handler(unsigned long r4, unsigned long r5,
506 unsigned long r6, unsigned long r7,
507 struct pt_regs __regs)
1da177e4 508{
f0bc814c
SM
509 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
510
dc34d312 511 /* Rewind */
f0bc814c 512 regs->pc -= 2;
dc34d312
PM
513
514#ifdef CONFIG_BUG
515 if (__kernel_text_address(instruction_pointer(regs))) {
516 u16 insn = *(u16 *)instruction_pointer(regs);
517 if (insn == TRAPA_BUG_OPCODE)
518 handle_BUG(regs);
519 }
520#endif
521
1da177e4
LT
522 force_sig(SIGTRAP, current);
523}