sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[platform/kernel/linux-exynos.git] / arch / sh / kernel / process_64.c
1 /*
2  * arch/sh/kernel/process_64.c
3  *
4  * This file handles the architecture-dependent parts of process handling..
5  *
6  * Copyright (C) 2000, 2001  Paolo Alberelli
7  * Copyright (C) 2003 - 2007  Paul Mundt
8  * Copyright (C) 2003, 2004 Richard Curnow
9  *
10  * Started from SH3/4 version:
11  *   Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
12  *
13  *   In turn started from i386 version:
14  *     Copyright (C) 1995  Linus Torvalds
15  *
16  * This file is subject to the terms and conditions of the GNU General Public
17  * License.  See the file "COPYING" in the main directory of this archive
18  * for more details.
19  */
20 #include <linux/mm.h>
21 #include <linux/fs.h>
22 #include <linux/ptrace.h>
23 #include <linux/reboot.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/io.h>
28 #include <linux/sched/debug.h>
29 #include <linux/sched/task.h>
30 #include <asm/syscalls.h>
31 #include <linux/uaccess.h>
32 #include <asm/pgtable.h>
33 #include <asm/mmu_context.h>
34 #include <asm/fpu.h>
35 #include <asm/switch_to.h>
36
37 struct task_struct *last_task_used_math = NULL;
38 struct pt_regs fake_swapper_regs = { 0, };
39
40 void show_regs(struct pt_regs *regs)
41 {
42         unsigned long long ah, al, bh, bl, ch, cl;
43
44         printk("\n");
45         show_regs_print_info(KERN_DEFAULT);
46
47         ah = (regs->pc) >> 32;
48         al = (regs->pc) & 0xffffffff;
49         bh = (regs->regs[18]) >> 32;
50         bl = (regs->regs[18]) & 0xffffffff;
51         ch = (regs->regs[15]) >> 32;
52         cl = (regs->regs[15]) & 0xffffffff;
53         printk("PC  : %08Lx%08Lx LINK: %08Lx%08Lx SP  : %08Lx%08Lx\n",
54                ah, al, bh, bl, ch, cl);
55
56         ah = (regs->sr) >> 32;
57         al = (regs->sr) & 0xffffffff;
58         asm volatile ("getcon   " __TEA ", %0" : "=r" (bh));
59         asm volatile ("getcon   " __TEA ", %0" : "=r" (bl));
60         bh = (bh) >> 32;
61         bl = (bl) & 0xffffffff;
62         asm volatile ("getcon   " __KCR0 ", %0" : "=r" (ch));
63         asm volatile ("getcon   " __KCR0 ", %0" : "=r" (cl));
64         ch = (ch) >> 32;
65         cl = (cl) & 0xffffffff;
66         printk("SR  : %08Lx%08Lx TEA : %08Lx%08Lx KCR0: %08Lx%08Lx\n",
67                ah, al, bh, bl, ch, cl);
68
69         ah = (regs->regs[0]) >> 32;
70         al = (regs->regs[0]) & 0xffffffff;
71         bh = (regs->regs[1]) >> 32;
72         bl = (regs->regs[1]) & 0xffffffff;
73         ch = (regs->regs[2]) >> 32;
74         cl = (regs->regs[2]) & 0xffffffff;
75         printk("R0  : %08Lx%08Lx R1  : %08Lx%08Lx R2  : %08Lx%08Lx\n",
76                ah, al, bh, bl, ch, cl);
77
78         ah = (regs->regs[3]) >> 32;
79         al = (regs->regs[3]) & 0xffffffff;
80         bh = (regs->regs[4]) >> 32;
81         bl = (regs->regs[4]) & 0xffffffff;
82         ch = (regs->regs[5]) >> 32;
83         cl = (regs->regs[5]) & 0xffffffff;
84         printk("R3  : %08Lx%08Lx R4  : %08Lx%08Lx R5  : %08Lx%08Lx\n",
85                ah, al, bh, bl, ch, cl);
86
87         ah = (regs->regs[6]) >> 32;
88         al = (regs->regs[6]) & 0xffffffff;
89         bh = (regs->regs[7]) >> 32;
90         bl = (regs->regs[7]) & 0xffffffff;
91         ch = (regs->regs[8]) >> 32;
92         cl = (regs->regs[8]) & 0xffffffff;
93         printk("R6  : %08Lx%08Lx R7  : %08Lx%08Lx R8  : %08Lx%08Lx\n",
94                ah, al, bh, bl, ch, cl);
95
96         ah = (regs->regs[9]) >> 32;
97         al = (regs->regs[9]) & 0xffffffff;
98         bh = (regs->regs[10]) >> 32;
99         bl = (regs->regs[10]) & 0xffffffff;
100         ch = (regs->regs[11]) >> 32;
101         cl = (regs->regs[11]) & 0xffffffff;
102         printk("R9  : %08Lx%08Lx R10 : %08Lx%08Lx R11 : %08Lx%08Lx\n",
103                ah, al, bh, bl, ch, cl);
104
105         ah = (regs->regs[12]) >> 32;
106         al = (regs->regs[12]) & 0xffffffff;
107         bh = (regs->regs[13]) >> 32;
108         bl = (regs->regs[13]) & 0xffffffff;
109         ch = (regs->regs[14]) >> 32;
110         cl = (regs->regs[14]) & 0xffffffff;
111         printk("R12 : %08Lx%08Lx R13 : %08Lx%08Lx R14 : %08Lx%08Lx\n",
112                ah, al, bh, bl, ch, cl);
113
114         ah = (regs->regs[16]) >> 32;
115         al = (regs->regs[16]) & 0xffffffff;
116         bh = (regs->regs[17]) >> 32;
117         bl = (regs->regs[17]) & 0xffffffff;
118         ch = (regs->regs[19]) >> 32;
119         cl = (regs->regs[19]) & 0xffffffff;
120         printk("R16 : %08Lx%08Lx R17 : %08Lx%08Lx R19 : %08Lx%08Lx\n",
121                ah, al, bh, bl, ch, cl);
122
123         ah = (regs->regs[20]) >> 32;
124         al = (regs->regs[20]) & 0xffffffff;
125         bh = (regs->regs[21]) >> 32;
126         bl = (regs->regs[21]) & 0xffffffff;
127         ch = (regs->regs[22]) >> 32;
128         cl = (regs->regs[22]) & 0xffffffff;
129         printk("R20 : %08Lx%08Lx R21 : %08Lx%08Lx R22 : %08Lx%08Lx\n",
130                ah, al, bh, bl, ch, cl);
131
132         ah = (regs->regs[23]) >> 32;
133         al = (regs->regs[23]) & 0xffffffff;
134         bh = (regs->regs[24]) >> 32;
135         bl = (regs->regs[24]) & 0xffffffff;
136         ch = (regs->regs[25]) >> 32;
137         cl = (regs->regs[25]) & 0xffffffff;
138         printk("R23 : %08Lx%08Lx R24 : %08Lx%08Lx R25 : %08Lx%08Lx\n",
139                ah, al, bh, bl, ch, cl);
140
141         ah = (regs->regs[26]) >> 32;
142         al = (regs->regs[26]) & 0xffffffff;
143         bh = (regs->regs[27]) >> 32;
144         bl = (regs->regs[27]) & 0xffffffff;
145         ch = (regs->regs[28]) >> 32;
146         cl = (regs->regs[28]) & 0xffffffff;
147         printk("R26 : %08Lx%08Lx R27 : %08Lx%08Lx R28 : %08Lx%08Lx\n",
148                ah, al, bh, bl, ch, cl);
149
150         ah = (regs->regs[29]) >> 32;
151         al = (regs->regs[29]) & 0xffffffff;
152         bh = (regs->regs[30]) >> 32;
153         bl = (regs->regs[30]) & 0xffffffff;
154         ch = (regs->regs[31]) >> 32;
155         cl = (regs->regs[31]) & 0xffffffff;
156         printk("R29 : %08Lx%08Lx R30 : %08Lx%08Lx R31 : %08Lx%08Lx\n",
157                ah, al, bh, bl, ch, cl);
158
159         ah = (regs->regs[32]) >> 32;
160         al = (regs->regs[32]) & 0xffffffff;
161         bh = (regs->regs[33]) >> 32;
162         bl = (regs->regs[33]) & 0xffffffff;
163         ch = (regs->regs[34]) >> 32;
164         cl = (regs->regs[34]) & 0xffffffff;
165         printk("R32 : %08Lx%08Lx R33 : %08Lx%08Lx R34 : %08Lx%08Lx\n",
166                ah, al, bh, bl, ch, cl);
167
168         ah = (regs->regs[35]) >> 32;
169         al = (regs->regs[35]) & 0xffffffff;
170         bh = (regs->regs[36]) >> 32;
171         bl = (regs->regs[36]) & 0xffffffff;
172         ch = (regs->regs[37]) >> 32;
173         cl = (regs->regs[37]) & 0xffffffff;
174         printk("R35 : %08Lx%08Lx R36 : %08Lx%08Lx R37 : %08Lx%08Lx\n",
175                ah, al, bh, bl, ch, cl);
176
177         ah = (regs->regs[38]) >> 32;
178         al = (regs->regs[38]) & 0xffffffff;
179         bh = (regs->regs[39]) >> 32;
180         bl = (regs->regs[39]) & 0xffffffff;
181         ch = (regs->regs[40]) >> 32;
182         cl = (regs->regs[40]) & 0xffffffff;
183         printk("R38 : %08Lx%08Lx R39 : %08Lx%08Lx R40 : %08Lx%08Lx\n",
184                ah, al, bh, bl, ch, cl);
185
186         ah = (regs->regs[41]) >> 32;
187         al = (regs->regs[41]) & 0xffffffff;
188         bh = (regs->regs[42]) >> 32;
189         bl = (regs->regs[42]) & 0xffffffff;
190         ch = (regs->regs[43]) >> 32;
191         cl = (regs->regs[43]) & 0xffffffff;
192         printk("R41 : %08Lx%08Lx R42 : %08Lx%08Lx R43 : %08Lx%08Lx\n",
193                ah, al, bh, bl, ch, cl);
194
195         ah = (regs->regs[44]) >> 32;
196         al = (regs->regs[44]) & 0xffffffff;
197         bh = (regs->regs[45]) >> 32;
198         bl = (regs->regs[45]) & 0xffffffff;
199         ch = (regs->regs[46]) >> 32;
200         cl = (regs->regs[46]) & 0xffffffff;
201         printk("R44 : %08Lx%08Lx R45 : %08Lx%08Lx R46 : %08Lx%08Lx\n",
202                ah, al, bh, bl, ch, cl);
203
204         ah = (regs->regs[47]) >> 32;
205         al = (regs->regs[47]) & 0xffffffff;
206         bh = (regs->regs[48]) >> 32;
207         bl = (regs->regs[48]) & 0xffffffff;
208         ch = (regs->regs[49]) >> 32;
209         cl = (regs->regs[49]) & 0xffffffff;
210         printk("R47 : %08Lx%08Lx R48 : %08Lx%08Lx R49 : %08Lx%08Lx\n",
211                ah, al, bh, bl, ch, cl);
212
213         ah = (regs->regs[50]) >> 32;
214         al = (regs->regs[50]) & 0xffffffff;
215         bh = (regs->regs[51]) >> 32;
216         bl = (regs->regs[51]) & 0xffffffff;
217         ch = (regs->regs[52]) >> 32;
218         cl = (regs->regs[52]) & 0xffffffff;
219         printk("R50 : %08Lx%08Lx R51 : %08Lx%08Lx R52 : %08Lx%08Lx\n",
220                ah, al, bh, bl, ch, cl);
221
222         ah = (regs->regs[53]) >> 32;
223         al = (regs->regs[53]) & 0xffffffff;
224         bh = (regs->regs[54]) >> 32;
225         bl = (regs->regs[54]) & 0xffffffff;
226         ch = (regs->regs[55]) >> 32;
227         cl = (regs->regs[55]) & 0xffffffff;
228         printk("R53 : %08Lx%08Lx R54 : %08Lx%08Lx R55 : %08Lx%08Lx\n",
229                ah, al, bh, bl, ch, cl);
230
231         ah = (regs->regs[56]) >> 32;
232         al = (regs->regs[56]) & 0xffffffff;
233         bh = (regs->regs[57]) >> 32;
234         bl = (regs->regs[57]) & 0xffffffff;
235         ch = (regs->regs[58]) >> 32;
236         cl = (regs->regs[58]) & 0xffffffff;
237         printk("R56 : %08Lx%08Lx R57 : %08Lx%08Lx R58 : %08Lx%08Lx\n",
238                ah, al, bh, bl, ch, cl);
239
240         ah = (regs->regs[59]) >> 32;
241         al = (regs->regs[59]) & 0xffffffff;
242         bh = (regs->regs[60]) >> 32;
243         bl = (regs->regs[60]) & 0xffffffff;
244         ch = (regs->regs[61]) >> 32;
245         cl = (regs->regs[61]) & 0xffffffff;
246         printk("R59 : %08Lx%08Lx R60 : %08Lx%08Lx R61 : %08Lx%08Lx\n",
247                ah, al, bh, bl, ch, cl);
248
249         ah = (regs->regs[62]) >> 32;
250         al = (regs->regs[62]) & 0xffffffff;
251         bh = (regs->tregs[0]) >> 32;
252         bl = (regs->tregs[0]) & 0xffffffff;
253         ch = (regs->tregs[1]) >> 32;
254         cl = (regs->tregs[1]) & 0xffffffff;
255         printk("R62 : %08Lx%08Lx T0  : %08Lx%08Lx T1  : %08Lx%08Lx\n",
256                ah, al, bh, bl, ch, cl);
257
258         ah = (regs->tregs[2]) >> 32;
259         al = (regs->tregs[2]) & 0xffffffff;
260         bh = (regs->tregs[3]) >> 32;
261         bl = (regs->tregs[3]) & 0xffffffff;
262         ch = (regs->tregs[4]) >> 32;
263         cl = (regs->tregs[4]) & 0xffffffff;
264         printk("T2  : %08Lx%08Lx T3  : %08Lx%08Lx T4  : %08Lx%08Lx\n",
265                ah, al, bh, bl, ch, cl);
266
267         ah = (regs->tregs[5]) >> 32;
268         al = (regs->tregs[5]) & 0xffffffff;
269         bh = (regs->tregs[6]) >> 32;
270         bl = (regs->tregs[6]) & 0xffffffff;
271         ch = (regs->tregs[7]) >> 32;
272         cl = (regs->tregs[7]) & 0xffffffff;
273         printk("T5  : %08Lx%08Lx T6  : %08Lx%08Lx T7  : %08Lx%08Lx\n",
274                ah, al, bh, bl, ch, cl);
275
276         /*
277          * If we're in kernel mode, dump the stack too..
278          */
279         if (!user_mode(regs)) {
280                 void show_stack(struct task_struct *tsk, unsigned long *sp);
281                 unsigned long sp = regs->regs[15] & 0xffffffff;
282                 struct task_struct *tsk = get_current();
283
284                 tsk->thread.kregs = regs;
285
286                 show_stack(tsk, (unsigned long *)sp);
287         }
288 }
289
290 /*
291  * Free current thread data structures etc..
292  */
293 void exit_thread(struct task_struct *tsk)
294 {
295         /*
296          * See arch/sparc/kernel/process.c for the precedent for doing
297          * this -- RPC.
298          *
299          * The SH-5 FPU save/restore approach relies on
300          * last_task_used_math pointing to a live task_struct.  When
301          * another task tries to use the FPU for the 1st time, the FPUDIS
302          * trap handling (see arch/sh/kernel/cpu/sh5/fpu.c) will save the
303          * existing FPU state to the FP regs field within
304          * last_task_used_math before re-loading the new task's FPU state
305          * (or initialising it if the FPU has been used before).  So if
306          * last_task_used_math is stale, and its page has already been
307          * re-allocated for another use, the consequences are rather
308          * grim. Unless we null it here, there is no other path through
309          * which it would get safely nulled.
310          */
311 #ifdef CONFIG_SH_FPU
312         if (last_task_used_math == tsk)
313                 last_task_used_math = NULL;
314 #endif
315 }
316
317 void flush_thread(void)
318 {
319
320         /* Called by fs/exec.c (setup_new_exec) to remove traces of a
321          * previously running executable. */
322 #ifdef CONFIG_SH_FPU
323         if (last_task_used_math == current) {
324                 last_task_used_math = NULL;
325         }
326         /* Force FPU state to be reinitialised after exec */
327         clear_used_math();
328 #endif
329
330         /* if we are a kernel thread, about to change to user thread,
331          * update kreg
332          */
333         if(current->thread.kregs==&fake_swapper_regs) {
334           current->thread.kregs =
335              ((struct pt_regs *)(THREAD_SIZE + (unsigned long) current) - 1);
336           current->thread.uregs = current->thread.kregs;
337         }
338 }
339
340 void release_thread(struct task_struct *dead_task)
341 {
342         /* do nothing */
343 }
344
345 /* Fill in the fpu structure for a core dump.. */
346 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
347 {
348 #ifdef CONFIG_SH_FPU
349         int fpvalid;
350         struct task_struct *tsk = current;
351
352         fpvalid = !!tsk_used_math(tsk);
353         if (fpvalid) {
354                 if (current == last_task_used_math) {
355                         enable_fpu();
356                         save_fpu(tsk);
357                         disable_fpu();
358                         last_task_used_math = 0;
359                         regs->sr |= SR_FD;
360                 }
361
362                 memcpy(fpu, &tsk->thread.xstate->hardfpu, sizeof(*fpu));
363         }
364
365         return fpvalid;
366 #else
367         return 0; /* Task didn't use the fpu at all. */
368 #endif
369 }
370 EXPORT_SYMBOL(dump_fpu);
371
372 asmlinkage void ret_from_fork(void);
373 asmlinkage void ret_from_kernel_thread(void);
374
375 int copy_thread(unsigned long clone_flags, unsigned long usp,
376                 unsigned long arg, struct task_struct *p)
377 {
378         struct pt_regs *childregs;
379
380 #ifdef CONFIG_SH_FPU
381         /* can't happen for a kernel thread */
382         if (last_task_used_math == current) {
383                 enable_fpu();
384                 save_fpu(current);
385                 disable_fpu();
386                 last_task_used_math = NULL;
387                 current_pt_regs()->sr |= SR_FD;
388         }
389 #endif
390         /* Copy from sh version */
391         childregs = (struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1;
392         p->thread.sp = (unsigned long) childregs;
393
394         if (unlikely(p->flags & PF_KTHREAD)) {
395                 memset(childregs, 0, sizeof(struct pt_regs));
396                 childregs->regs[2] = (unsigned long)arg;
397                 childregs->regs[3] = (unsigned long)usp;
398                 childregs->sr = (1 << 30); /* not user_mode */
399                 childregs->sr |= SR_FD; /* Invalidate FPU flag */
400                 p->thread.pc = (unsigned long) ret_from_kernel_thread;
401                 return 0;
402         }
403         *childregs = *current_pt_regs();
404
405         /*
406          * Sign extend the edited stack.
407          * Note that thread.pc and thread.pc will stay
408          * 32-bit wide and context switch must take care
409          * of NEFF sign extension.
410          */
411         if (usp)
412                 childregs->regs[15] = neff_sign_extend(usp);
413         p->thread.uregs = childregs;
414
415         childregs->regs[9] = 0; /* Set return value for child */
416         childregs->sr |= SR_FD; /* Invalidate FPU flag */
417
418         p->thread.pc = (unsigned long) ret_from_fork;
419
420         return 0;
421 }
422
423 #ifdef CONFIG_FRAME_POINTER
424 static int in_sh64_switch_to(unsigned long pc)
425 {
426         extern char __sh64_switch_to_end;
427         /* For a sleeping task, the PC is somewhere in the middle of the function,
428            so we don't have to worry about masking the LSB off */
429         return (pc >= (unsigned long) sh64_switch_to) &&
430                (pc < (unsigned long) &__sh64_switch_to_end);
431 }
432 #endif
433
434 unsigned long get_wchan(struct task_struct *p)
435 {
436         unsigned long pc;
437
438         if (!p || p == current || p->state == TASK_RUNNING)
439                 return 0;
440
441         /*
442          * The same comment as on the Alpha applies here, too ...
443          */
444         pc = thread_saved_pc(p);
445
446 #ifdef CONFIG_FRAME_POINTER
447         if (in_sh64_switch_to(pc)) {
448                 unsigned long schedule_fp;
449                 unsigned long sh64_switch_to_fp;
450                 unsigned long schedule_caller_pc;
451
452                 sh64_switch_to_fp = (long) p->thread.sp;
453                 /* r14 is saved at offset 4 in the sh64_switch_to frame */
454                 schedule_fp = *(unsigned long *) (long)(sh64_switch_to_fp + 4);
455
456                 /* and the caller of 'schedule' is (currently!) saved at offset 24
457                    in the frame of schedule (from disasm) */
458                 schedule_caller_pc = *(unsigned long *) (long)(schedule_fp + 24);
459                 return schedule_caller_pc;
460         }
461 #endif
462         return pc;
463 }