1 /* $Id: process.c,v 1.1.1.1.2.4 2003/05/29 04:21:33 lethal Exp $
2 *
3 * linux/arch/sh/kernel/process.c
4 *
5 * Copyright (C) 1995 Linus Torvalds
6 *
7 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
8 */
9
10 /*
11 * This file handles the architecture-dependent parts of process handling..
12 */
13
14 #include <linux/unistd.h>
15 #include <linux/slab.h>
16
17 #include <asm/io.h>
18 #include <asm/uaccess.h>
19 #include <asm/mmu_context.h>
20 #include <asm/elf.h>
21
22 static int hlt_counter=0;
23
24 int ubc_usercnt = 0;
25
26 #define HARD_IDLE_TIMEOUT (HZ / 3)
27
disable_hlt(void)28 void disable_hlt(void)
29 {
30 hlt_counter++;
31 }
32
enable_hlt(void)33 void enable_hlt(void)
34 {
35 hlt_counter--;
36 }
37
38 /*
39 * The idle loop on a uniprocessor i386..
40 */
cpu_idle(void * unused)41 void cpu_idle(void *unused)
42 {
43 /* endless idle loop with no priority at all */
44 init_idle();
45 current->nice = 20;
46 current->counter = -100;
47
48 while (1) {
49 if (hlt_counter) {
50 while (1)
51 if (current->need_resched)
52 break;
53 } else {
54 __cli();
55 while (!current->need_resched) {
56 __sti();
57 asm volatile("sleep" : : : "memory");
58 __cli();
59 }
60 __sti();
61 }
62 schedule();
63 check_pgt_cache();
64 }
65 }
66
machine_restart(char * __unused)67 void machine_restart(char * __unused)
68 {
69 /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
70 asm volatile("ldc %0, sr\n\t"
71 "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
72 }
73
machine_halt(void)74 void machine_halt(void)
75 {
76 while (1)
77 asm volatile("sleep" : : : "memory");
78 }
79
machine_power_off(void)80 void machine_power_off(void)
81 {
82 }
83
show_regs(struct pt_regs * regs)84 void show_regs(struct pt_regs * regs)
85 {
86 printk("\n");
87 printk("PC : %08lx SP : %08lx SR : %08lx TEA : %08x %s\n",
88 regs->pc, regs->regs[15], regs->sr, ctrl_inl(MMU_TEA), print_tainted());
89 printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
90 regs->regs[0],regs->regs[1],
91 regs->regs[2],regs->regs[3]);
92 printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
93 regs->regs[4],regs->regs[5],
94 regs->regs[6],regs->regs[7]);
95 printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
96 regs->regs[8],regs->regs[9],
97 regs->regs[10],regs->regs[11]);
98 printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
99 regs->regs[12],regs->regs[13],
100 regs->regs[14]);
101 printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
102 regs->mach, regs->macl, regs->gbr, regs->pr);
103 }
104
alloc_task_struct(void)105 struct task_struct * alloc_task_struct(void)
106 {
107 /* Get two pages */
108 return (struct task_struct *) __get_free_pages(GFP_KERNEL,1);
109 }
110
free_task_struct(struct task_struct * p)111 void free_task_struct(struct task_struct *p)
112 {
113 free_pages((unsigned long) p, 1);
114 }
115
116 /*
117 * Create a kernel thread
118 */
119
120 /*
121 * This is the mechanism for creating a new kernel thread.
122 *
123 */
arch_kernel_thread(int (* fn)(void *),void * arg,unsigned long flags)124 int arch_kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
125 { /* Don't use this in BL=1(cli). Or else, CPU resets! */
126 register unsigned long __sc0 __asm__ ("r0");
127 register unsigned long __sc3 __asm__ ("r3") = __NR_clone;
128 register unsigned long __sc4 __asm__ ("r4") = (long) flags | CLONE_VM;
129 register unsigned long __sc5 __asm__ ("r5") = 0;
130 register unsigned long __sc8 __asm__ ("r8") = (long) arg;
131 register unsigned long __sc9 __asm__ ("r9") = (long) fn;
132
133 __asm__("trapa #0x12\n\t" /* Linux/SH system call */
134 "tst r0, r0\n\t" /* child or parent? */
135 "bf 1f\n\t" /* parent - jump */
136 "jsr @r9\n\t" /* call fn */
137 " mov r8, r4\n\t" /* push argument */
138 "mov r0, r4\n\t" /* return value to arg of exit */
139 "mov %1, r3\n\t" /* exit */
140 "trapa #0x11\n"
141 "1:"
142 : "=z" (__sc0)
143 : "i" (__NR_exit), "r" (__sc3), "r" (__sc4), "r" (__sc5),
144 "r" (__sc8), "r" (__sc9)
145 : "memory", "t");
146 return __sc0;
147 }
148
149 /*
150 * Free current thread data structures etc..
151 */
exit_thread(void)152 void exit_thread(void)
153 {
154 if (current->thread.ubc_pc1) {
155 current->thread.ubc_pc1 = 0;
156 ubc_usercnt -= 1;
157 }
158 }
159
flush_thread(void)160 void flush_thread(void)
161 {
162 #if defined(__sh3__)
163 /* do nothing */
164 /* Possibly, set clear debug registers */
165 #elif defined(__SH4__)
166 struct task_struct *tsk = current;
167
168 /* Forget lazy FPU state */
169 clear_fpu(tsk);
170 tsk->used_math = 0;
171 #endif
172 }
173
release_thread(struct task_struct * dead_task)174 void release_thread(struct task_struct *dead_task)
175 {
176 /* do nothing */
177 }
178
179 /* Fill in the fpu structure for a core dump.. */
dump_fpu(struct pt_regs * regs,elf_fpregset_t * fpu)180 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
181 {
182 #if defined(__SH4__)
183 int fpvalid;
184 struct task_struct *tsk = current;
185
186 fpvalid = tsk->used_math;
187 if (fpvalid) {
188 unlazy_fpu(tsk);
189 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
190 }
191
192 return fpvalid;
193 #else
194 return 0; /* Task didn't use the fpu at all. */
195 #endif
196 }
197
198 asmlinkage void ret_from_fork(void);
199
copy_thread(int nr,unsigned long clone_flags,unsigned long usp,unsigned long unused,struct task_struct * p,struct pt_regs * regs)200 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
201 unsigned long unused,
202 struct task_struct *p, struct pt_regs *regs)
203 {
204 struct pt_regs *childregs;
205 #if defined(__SH4__)
206 struct task_struct *tsk = current;
207
208 unlazy_fpu(tsk);
209 p->thread.fpu = current->thread.fpu;
210 p->used_math = tsk->used_math;
211 #endif
212 childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long) p)) - 1;
213 *childregs = *regs;
214
215 if (user_mode(regs)) {
216 childregs->regs[15] = usp;
217 } else {
218 childregs->regs[15] = (unsigned long)p+2*PAGE_SIZE;
219 }
220 childregs->regs[0] = 0; /* Set return value for child */
221 childregs->sr |= SR_FD; /* Invalidate FPU flag */
222
223 p->thread.sp = (unsigned long) childregs;
224 p->thread.pc = (unsigned long) ret_from_fork;
225
226 p->thread.ubc_pc1 = 0;
227 p->thread.ubc_pc2 = 0;
228
229 return 0;
230 }
231
232 /*
233 * fill in the user structure for a core dump..
234 */
dump_thread(struct pt_regs * regs,struct user * dump)235 void dump_thread(struct pt_regs * regs, struct user * dump)
236 {
237 dump->magic = CMAGIC;
238 dump->start_code = current->mm->start_code;
239 dump->start_data = current->mm->start_data;
240 dump->start_stack = regs->regs[15] & ~(PAGE_SIZE - 1);
241 dump->u_tsize = (current->mm->end_code - dump->start_code) >> PAGE_SHIFT;
242 dump->u_dsize = (current->mm->brk + (PAGE_SIZE-1) - dump->start_data) >> PAGE_SHIFT;
243 dump->u_ssize = (current->mm->start_stack - dump->start_stack +
244 PAGE_SIZE - 1) >> PAGE_SHIFT;
245 /* Debug registers will come here. */
246
247 dump->regs = *regs;
248
249 dump->u_fpvalid = dump_fpu(regs, &dump->fpu);
250 }
251
252 /* Tracing by user break controller. */
253 static inline void
ubc_set_tracing(int asid,unsigned long nextpc1,unsigned nextpc2)254 ubc_set_tracing(int asid, unsigned long nextpc1, unsigned nextpc2)
255 {
256 ctrl_outl(nextpc1, UBC_BARA);
257 ctrl_outb(asid, UBC_BASRA);
258 if(UBC_TYPE_SH7729){
259 ctrl_outl(0, UBC_BAMRA);
260 ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
261 }else{
262 ctrl_outb(0, UBC_BAMRA);
263 ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
264 }
265
266 if (nextpc2 != (unsigned long) -1) {
267 ctrl_outl(nextpc2, UBC_BARB);
268 ctrl_outb(asid, UBC_BASRB);
269 if(UBC_TYPE_SH7729){
270 ctrl_outl(0, UBC_BAMRB);
271 ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRB);
272 }else{
273 ctrl_outb(0, UBC_BAMRB);
274 ctrl_outw(BBR_INST | BBR_READ, UBC_BBRB);
275 }
276 }
277 if(UBC_TYPE_SH7729)
278 ctrl_outl(BRCR_PCTE, UBC_BRCR);
279 else
280 ctrl_outw(0, UBC_BRCR);
281 }
282
283 /*
284 * switch_to(x,y) should switch tasks from x to y.
285 *
286 */
__switch_to(struct task_struct * prev,struct task_struct * next)287 void __switch_to(struct task_struct *prev, struct task_struct *next)
288 {
289 #if defined(__SH4__)
290 unlazy_fpu(prev);
291 #endif
292
293 /*
294 * Restore the kernel mode register
295 * k7 (r7_bank1)
296 */
297 asm volatile("ldc %0, r7_bank"
298 : /* no output */
299 :"r" (next));
300
301 /* If no tasks are using the UBC, we're done */
302 if (ubc_usercnt == 0)
303 return;
304
305 /* Otherwise, set or clear UBC as appropriate */
306 if (next->thread.ubc_pc1) {
307 ubc_set_tracing(next->mm->context & MMU_CONTEXT_ASID_MASK,
308 next->thread.ubc_pc1, next->thread.ubc_pc2);
309 } else {
310 ctrl_outw(0, UBC_BBRA);
311 ctrl_outw(0, UBC_BBRB);
312 }
313 }
314
sys_fork(unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7,struct pt_regs regs)315 asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
316 unsigned long r6, unsigned long r7,
317 struct pt_regs regs)
318 {
319 return do_fork(SIGCHLD, regs.regs[15], ®s, 0);
320 }
321
sys_clone(unsigned long clone_flags,unsigned long newsp,unsigned long r6,unsigned long r7,struct pt_regs regs)322 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
323 unsigned long r6, unsigned long r7,
324 struct pt_regs regs)
325 {
326 if (!newsp)
327 newsp = regs.regs[15];
328 return do_fork(clone_flags, newsp, ®s, 0);
329 }
330
331 /*
332 * This is trivial, and on the face of it looks like it
333 * could equally well be done in user mode.
334 *
335 * Not so, for quite unobvious reasons - register pressure.
336 * In user mode vfork() cannot have a stack frame, and if
337 * done by calling the "clone()" system call directly, you
338 * do not have enough call-clobbered registers to hold all
339 * the information you need.
340 */
sys_vfork(unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7,struct pt_regs regs)341 asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
342 unsigned long r6, unsigned long r7,
343 struct pt_regs regs)
344 {
345 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.regs[15], ®s, 0);
346 }
347
348 /*
349 * sys_execve() executes a new program.
350 */
sys_execve(char * ufilename,char ** uargv,char ** uenvp,unsigned long r7,struct pt_regs regs)351 asmlinkage int sys_execve(char *ufilename, char **uargv,
352 char **uenvp, unsigned long r7,
353 struct pt_regs regs)
354 {
355 int error;
356 char *filename;
357
358 filename = getname(ufilename);
359 error = PTR_ERR(filename);
360 if (IS_ERR(filename))
361 goto out;
362
363 error = do_execve(filename, uargv, uenvp, ®s);
364 if (error == 0)
365 current->ptrace &= ~PT_DTRACE;
366 putname(filename);
367 out:
368 return error;
369 }
370
371 /*
372 * These bracket the sleeping functions..
373 */
374 extern void scheduling_functions_start_here(void);
375 extern void scheduling_functions_end_here(void);
376 #define first_sched ((unsigned long) scheduling_functions_start_here)
377 #define last_sched ((unsigned long) scheduling_functions_end_here)
378
get_wchan(struct task_struct * p)379 unsigned long get_wchan(struct task_struct *p)
380 {
381 unsigned long schedule_frame;
382 unsigned long pc;
383
384 if (!p || p == current || p->state == TASK_RUNNING)
385 return 0;
386
387 /*
388 * The same comment as on the Alpha applies here, too ...
389 */
390 pc = thread_saved_pc(&p->thread);
391 if (pc >= (unsigned long) interruptible_sleep_on && pc < (unsigned long) add_timer) {
392 schedule_frame = ((unsigned long *)(long)p->thread.sp)[1];
393 return (unsigned long)((unsigned long *)schedule_frame)[1];
394 }
395 return pc;
396 }
397
print_syscall(int x)398 asmlinkage void print_syscall(int x)
399 {
400 unsigned long flags, sr;
401 asm("stc sr, %0": "=r" (sr));
402 save_and_cli(flags);
403 printk("%c: %c %c, %c: SYSCALL\n", (x&63)+32,
404 (current->flags&PF_USEDFPU)?'C':' ',
405 (init_task.flags&PF_USEDFPU)?'K':' ', (sr&SR_FD)?' ':'F');
406 restore_flags(flags);
407 }
408
break_point_trap(unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7,struct pt_regs regs)409 asmlinkage void break_point_trap(unsigned long r4, unsigned long r5,
410 unsigned long r6, unsigned long r7,
411 struct pt_regs regs)
412 {
413 /* Clear tracing. */
414 ctrl_outw(0, UBC_BBRA);
415 ctrl_outw(0, UBC_BBRB);
416 current->thread.ubc_pc1 = 0;
417 current->thread.ubc_pc2 = (unsigned long) -1;
418 ubc_usercnt -= 1;
419
420 force_sig(SIGTRAP, current);
421 }
422
break_point_trap_software(unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7,struct pt_regs regs)423 asmlinkage void break_point_trap_software(unsigned long r4, unsigned long r5,
424 unsigned long r6, unsigned long r7,
425 struct pt_regs regs)
426 {
427 regs.pc -= 2;
428 force_sig(SIGTRAP, current);
429 }
430