1 /* $Id: process.c,v 1.28 2004/05/05 16:54:23 lethal Exp $
3 * linux/arch/sh/kernel/process.c
5 * Copyright (C) 1995 Linus Torvalds
7 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
11 * This file handles the architecture-dependent parts of process handling..
14 #include <linux/module.h>
15 #include <linux/unistd.h>
17 #include <linux/elfcore.h>
18 #include <linux/slab.h>
19 #include <linux/a.out.h>
20 #include <linux/ptrace.h>
21 #include <linux/platform.h>
22 #include <linux/kallsyms.h>
25 #include <asm/uaccess.h>
26 #include <asm/mmu_context.h>
28 #if defined(CONFIG_SH_HS7751RVOIP)
29 #include <asm/hs7751rvoip/hs7751rvoip.h>
30 #elif defined(CONFIG_SH_RTS7751R2D)
31 #include <asm/rts7751r2d/rts7751r2d.h>
34 static int hlt_counter
=0;
38 #define HARD_IDLE_TIMEOUT (HZ / 3)
40 void disable_hlt(void)
45 EXPORT_SYMBOL(disable_hlt
);
52 EXPORT_SYMBOL(enable_hlt
);
56 /* endless idle loop with no priority at all */
59 while (!need_resched())
62 while (!need_resched())
66 preempt_enable_no_resched();
72 void machine_restart(char * __unused
)
74 /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
75 asm volatile("ldc %0, sr\n\t"
76 "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
79 void machine_halt(void)
81 #if defined(CONFIG_SH_HS7751RVOIP)
84 value
= ctrl_inw(PA_OUTPORTR
);
85 ctrl_outw((value
& 0xffdf), PA_OUTPORTR
);
86 #elif defined(CONFIG_SH_RTS7751R2D)
87 ctrl_outw(0x0001, PA_POWOFF
);
93 void machine_power_off(void)
95 #if defined(CONFIG_SH_HS7751RVOIP)
98 value
= ctrl_inw(PA_OUTPORTR
);
99 ctrl_outw((value
& 0xffdf), PA_OUTPORTR
);
100 #elif defined(CONFIG_SH_RTS7751R2D)
101 ctrl_outw(0x0001, PA_POWOFF
);
105 void show_regs(struct pt_regs
* regs
)
108 printk("Pid : %d, Comm: %20s\n", current
->pid
, current
->comm
);
109 print_symbol("PC is at %s\n", regs
->pc
);
110 printk("PC : %08lx SP : %08lx SR : %08lx ",
111 regs
->pc
, regs
->regs
[15], regs
->sr
);
113 printk("TEA : %08x ", ctrl_inl(MMU_TEA
));
117 printk("%s\n", print_tainted());
119 printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
120 regs
->regs
[0],regs
->regs
[1],
121 regs
->regs
[2],regs
->regs
[3]);
122 printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
123 regs
->regs
[4],regs
->regs
[5],
124 regs
->regs
[6],regs
->regs
[7]);
125 printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
126 regs
->regs
[8],regs
->regs
[9],
127 regs
->regs
[10],regs
->regs
[11]);
128 printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
129 regs
->regs
[12],regs
->regs
[13],
131 printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
132 regs
->mach
, regs
->macl
, regs
->gbr
, regs
->pr
);
135 * If we're in kernel mode, dump the stack too..
137 if (!user_mode(regs
)) {
138 extern void show_task(unsigned long *sp
);
139 unsigned long sp
= regs
->regs
[15];
141 show_task((unsigned long *)sp
);
146 * Create a kernel thread
150 * This is the mechanism for creating a new kernel thread.
153 extern void kernel_thread_helper(void);
155 "kernel_thread_helper:\n\t"
164 int kernel_thread(int (*fn
)(void *), void * arg
, unsigned long flags
)
165 { /* Don't use this in BL=1(cli). Or else, CPU resets! */
168 memset(®s
, 0, sizeof(regs
));
169 regs
.regs
[4] = (unsigned long) arg
;
170 regs
.regs
[5] = (unsigned long) fn
;
172 regs
.pc
= (unsigned long) kernel_thread_helper
;
175 /* Ok, create the new process.. */
176 return do_fork(flags
| CLONE_VM
| CLONE_UNTRACED
, 0, ®s
, 0, NULL
, NULL
);
180 * Free current thread data structures etc..
182 void exit_thread(void)
184 if (current
->thread
.ubc_pc
) {
185 current
->thread
.ubc_pc
= 0;
190 void flush_thread(void)
192 #if defined(CONFIG_SH_FPU)
193 struct task_struct
*tsk
= current
;
194 struct pt_regs
*regs
= (struct pt_regs
*)
195 ((unsigned long)tsk
->thread_info
196 + THREAD_SIZE
- sizeof(struct pt_regs
)
197 - sizeof(unsigned long));
199 /* Forget lazy FPU state */
200 clear_fpu(tsk
, regs
);
205 void release_thread(struct task_struct
*dead_task
)
210 /* Fill in the fpu structure for a core dump.. */
211 int dump_fpu(struct pt_regs
*regs
, elf_fpregset_t
*fpu
)
215 #if defined(CONFIG_SH_FPU)
216 struct task_struct
*tsk
= current
;
218 fpvalid
= !!tsk_used_math(tsk
);
220 unlazy_fpu(tsk
, regs
);
221 memcpy(fpu
, &tsk
->thread
.fpu
.hard
, sizeof(*fpu
));
229 * Capture the user space registers if the task is not running (in user space)
231 int dump_task_regs(struct task_struct
*tsk
, elf_gregset_t
*regs
)
233 struct pt_regs ptregs
;
235 ptregs
= *(struct pt_regs
*)
236 ((unsigned long)tsk
->thread_info
+ THREAD_SIZE
237 - sizeof(struct pt_regs
)
239 - sizeof(struct pt_dspregs
)
241 - sizeof(unsigned long));
242 elf_core_copy_regs(regs
, &ptregs
);
248 dump_task_fpu (struct task_struct
*tsk
, elf_fpregset_t
*fpu
)
252 #if defined(CONFIG_SH_FPU)
253 fpvalid
= !!tsk_used_math(tsk
);
255 struct pt_regs
*regs
= (struct pt_regs
*)
256 ((unsigned long)tsk
->thread_info
257 + THREAD_SIZE
- sizeof(struct pt_regs
)
258 - sizeof(unsigned long));
259 unlazy_fpu(tsk
, regs
);
260 memcpy(fpu
, &tsk
->thread
.fpu
.hard
, sizeof(*fpu
));
267 asmlinkage
void ret_from_fork(void);
269 int copy_thread(int nr
, unsigned long clone_flags
, unsigned long usp
,
270 unsigned long unused
,
271 struct task_struct
*p
, struct pt_regs
*regs
)
273 struct pt_regs
*childregs
;
274 #if defined(CONFIG_SH_FPU)
275 struct task_struct
*tsk
= current
;
277 unlazy_fpu(tsk
, regs
);
278 p
->thread
.fpu
= tsk
->thread
.fpu
;
279 copy_to_stopped_child_used_math(p
);
282 childregs
= ((struct pt_regs
*)
283 (THREAD_SIZE
+ (unsigned long) p
->thread_info
)
285 - sizeof(struct pt_dspregs
)
287 - sizeof(unsigned long)) - 1;
290 if (user_mode(regs
)) {
291 childregs
->regs
[15] = usp
;
293 childregs
->regs
[15] = (unsigned long)p
->thread_info
+ THREAD_SIZE
;
295 if (clone_flags
& CLONE_SETTLS
) {
296 childregs
->gbr
= childregs
->regs
[0];
298 childregs
->regs
[0] = 0; /* Set return value for child */
300 p
->thread
.sp
= (unsigned long) childregs
;
301 p
->thread
.pc
= (unsigned long) ret_from_fork
;
303 p
->thread
.ubc_pc
= 0;
309 * fill in the user structure for a core dump..
311 void dump_thread(struct pt_regs
* regs
, struct user
* dump
)
313 dump
->magic
= CMAGIC
;
314 dump
->start_code
= current
->mm
->start_code
;
315 dump
->start_data
= current
->mm
->start_data
;
316 dump
->start_stack
= regs
->regs
[15] & ~(PAGE_SIZE
- 1);
317 dump
->u_tsize
= (current
->mm
->end_code
- dump
->start_code
) >> PAGE_SHIFT
;
318 dump
->u_dsize
= (current
->mm
->brk
+ (PAGE_SIZE
-1) - dump
->start_data
) >> PAGE_SHIFT
;
319 dump
->u_ssize
= (current
->mm
->start_stack
- dump
->start_stack
+
320 PAGE_SIZE
- 1) >> PAGE_SHIFT
;
321 /* Debug registers will come here. */
325 dump
->u_fpvalid
= dump_fpu(regs
, &dump
->fpu
);
328 /* Tracing by user break controller. */
330 ubc_set_tracing(int asid
, unsigned long pc
)
332 ctrl_outl(pc
, UBC_BARA
);
334 /* We don't have any ASID settings for the SH-2! */
335 if (cpu_data
->type
!= CPU_SH7604
)
336 ctrl_outb(asid
, UBC_BASRA
);
338 ctrl_outl(0, UBC_BAMRA
);
340 if (cpu_data
->type
== CPU_SH7729
) {
341 ctrl_outw(BBR_INST
| BBR_READ
| BBR_CPU
, UBC_BBRA
);
342 ctrl_outl(BRCR_PCBA
| BRCR_PCTE
, UBC_BRCR
);
344 ctrl_outw(BBR_INST
| BBR_READ
, UBC_BBRA
);
345 ctrl_outw(BRCR_PCBA
, UBC_BRCR
);
350 * switch_to(x,y) should switch tasks from x to y.
353 struct task_struct
*__switch_to(struct task_struct
*prev
, struct task_struct
*next
)
355 #if defined(CONFIG_SH_FPU)
356 struct pt_regs
*regs
= (struct pt_regs
*)
357 ((unsigned long)prev
->thread_info
358 + THREAD_SIZE
- sizeof(struct pt_regs
)
359 - sizeof(unsigned long));
360 unlazy_fpu(prev
, regs
);
363 #ifdef CONFIG_PREEMPT
366 struct pt_regs
*regs
;
368 local_irq_save(flags
);
369 regs
= (struct pt_regs
*)
370 ((unsigned long)prev
->thread_info
371 + THREAD_SIZE
- sizeof(struct pt_regs
)
373 - sizeof(struct pt_dspregs
)
375 - sizeof(unsigned long));
376 if (user_mode(regs
) && regs
->regs
[15] >= 0xc0000000) {
377 int offset
= (int)regs
->regs
[15];
379 /* Reset stack pointer: clear critical region mark */
380 regs
->regs
[15] = regs
->regs
[1];
381 if (regs
->pc
< regs
->regs
[0])
382 /* Go to rewind point */
383 regs
->pc
= regs
->regs
[0] + offset
;
385 local_irq_restore(flags
);
390 * Restore the kernel mode register
393 asm volatile("ldc %0, r7_bank"
395 : "r" (next
->thread_info
));
398 /* If no tasks are using the UBC, we're done */
399 if (ubc_usercnt
== 0)
400 /* If no tasks are using the UBC, we're done */;
401 else if (next
->thread
.ubc_pc
&& next
->mm
) {
402 ubc_set_tracing(next
->mm
->context
& MMU_CONTEXT_ASID_MASK
,
403 next
->thread
.ubc_pc
);
405 ctrl_outw(0, UBC_BBRA
);
406 ctrl_outw(0, UBC_BBRB
);
413 asmlinkage
int sys_fork(unsigned long r4
, unsigned long r5
,
414 unsigned long r6
, unsigned long r7
,
418 return do_fork(SIGCHLD
, regs
.regs
[15], ®s
, 0, NULL
, NULL
);
420 /* fork almost works, enough to trick you into looking elsewhere :-( */
425 asmlinkage
int sys_clone(unsigned long clone_flags
, unsigned long newsp
,
426 unsigned long parent_tidptr
,
427 unsigned long child_tidptr
,
431 newsp
= regs
.regs
[15];
432 return do_fork(clone_flags
, newsp
, ®s
, 0,
433 (int __user
*)parent_tidptr
, (int __user
*)child_tidptr
);
437 * This is trivial, and on the face of it looks like it
438 * could equally well be done in user mode.
440 * Not so, for quite unobvious reasons - register pressure.
441 * In user mode vfork() cannot have a stack frame, and if
442 * done by calling the "clone()" system call directly, you
443 * do not have enough call-clobbered registers to hold all
444 * the information you need.
446 asmlinkage
int sys_vfork(unsigned long r4
, unsigned long r5
,
447 unsigned long r6
, unsigned long r7
,
450 return do_fork(CLONE_VFORK
| CLONE_VM
| SIGCHLD
, regs
.regs
[15], ®s
,
455 * sys_execve() executes a new program.
457 asmlinkage
int sys_execve(char *ufilename
, char **uargv
,
458 char **uenvp
, unsigned long r7
,
464 filename
= getname((char __user
*)ufilename
);
465 error
= PTR_ERR(filename
);
466 if (IS_ERR(filename
))
469 error
= do_execve(filename
,
470 (char __user
* __user
*)uargv
,
471 (char __user
* __user
*)uenvp
,
475 current
->ptrace
&= ~PT_DTRACE
;
476 task_unlock(current
);
483 unsigned long get_wchan(struct task_struct
*p
)
485 unsigned long schedule_frame
;
488 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
492 * The same comment as on the Alpha applies here, too ...
494 pc
= thread_saved_pc(p
);
495 if (in_sched_functions(pc
)) {
496 schedule_frame
= ((unsigned long *)(long)p
->thread
.sp
)[1];
497 return (unsigned long)((unsigned long *)schedule_frame
)[1];
502 asmlinkage
void break_point_trap(unsigned long r4
, unsigned long r5
,
503 unsigned long r6
, unsigned long r7
,
507 ctrl_outw(0, UBC_BBRA
);
508 ctrl_outw(0, UBC_BBRB
);
509 current
->thread
.ubc_pc
= 0;
512 force_sig(SIGTRAP
, current
);
515 asmlinkage
void break_point_trap_software(unsigned long r4
, unsigned long r5
,
516 unsigned long r6
, unsigned long r7
,
520 force_sig(SIGTRAP
, current
);