7564c371c66043204d62235a4e8183cabdc978de
[linux-drm-fsl-dcu.git] / arch / mips / kernel / process.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1994 - 1999, 2000 by Ralf Baechle and others.
7  * Copyright (C) 2005, 2006 by Ralf Baechle (ralf@linux-mips.org)
8  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9  * Copyright (C) 2004 Thiemo Seufer
10  * Copyright (C) 2013  Imagination Technologies Ltd.
11  */
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/tick.h>
15 #include <linux/kernel.h>
16 #include <linux/mm.h>
17 #include <linux/stddef.h>
18 #include <linux/unistd.h>
19 #include <linux/export.h>
20 #include <linux/ptrace.h>
21 #include <linux/mman.h>
22 #include <linux/personality.h>
23 #include <linux/sys.h>
24 #include <linux/user.h>
25 #include <linux/init.h>
26 #include <linux/completion.h>
27 #include <linux/kallsyms.h>
28 #include <linux/random.h>
29
30 #include <asm/asm.h>
31 #include <asm/bootinfo.h>
32 #include <asm/cpu.h>
33 #include <asm/dsp.h>
34 #include <asm/fpu.h>
35 #include <asm/msa.h>
36 #include <asm/pgtable.h>
37 #include <asm/mipsregs.h>
38 #include <asm/processor.h>
39 #include <asm/uaccess.h>
40 #include <asm/io.h>
41 #include <asm/elf.h>
42 #include <asm/isadep.h>
43 #include <asm/inst.h>
44 #include <asm/stacktrace.h>
45
46 #ifdef CONFIG_HOTPLUG_CPU
47 void arch_cpu_idle_dead(void)
48 {
49         /* What the heck is this check doing ? */
50         if (!cpu_isset(smp_processor_id(), cpu_callin_map))
51                 play_dead();
52 }
53 #endif
54
55 asmlinkage void ret_from_fork(void);
56 asmlinkage void ret_from_kernel_thread(void);
57
58 void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
59 {
60         unsigned long status;
61
62         /* New thread loses kernel privileges. */
63         status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|ST0_FR|KU_MASK);
64         status |= KU_USER;
65         regs->cp0_status = status;
66         clear_used_math();
67         clear_fpu_owner();
68         init_dsp();
69         clear_thread_flag(TIF_MSA_CTX_LIVE);
70         disable_msa();
71         regs->cp0_epc = pc;
72         regs->regs[29] = sp;
73 }
74
75 void exit_thread(void)
76 {
77 }
78
79 void flush_thread(void)
80 {
81 }
82
83 int copy_thread(unsigned long clone_flags, unsigned long usp,
84         unsigned long arg, struct task_struct *p)
85 {
86         struct thread_info *ti = task_thread_info(p);
87         struct pt_regs *childregs, *regs = current_pt_regs();
88         unsigned long childksp;
89         p->set_child_tid = p->clear_child_tid = NULL;
90
91         childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
92
93         preempt_disable();
94
95         if (is_msa_enabled())
96                 save_msa(p);
97         else if (is_fpu_owner())
98                 save_fp(p);
99
100         if (cpu_has_dsp)
101                 save_dsp(p);
102
103         preempt_enable();
104
105         /* set up new TSS. */
106         childregs = (struct pt_regs *) childksp - 1;
107         /*  Put the stack after the struct pt_regs.  */
108         childksp = (unsigned long) childregs;
109         p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
110         if (unlikely(p->flags & PF_KTHREAD)) {
111                 unsigned long status = p->thread.cp0_status;
112                 memset(childregs, 0, sizeof(struct pt_regs));
113                 ti->addr_limit = KERNEL_DS;
114                 p->thread.reg16 = usp; /* fn */
115                 p->thread.reg17 = arg;
116                 p->thread.reg29 = childksp;
117                 p->thread.reg31 = (unsigned long) ret_from_kernel_thread;
118 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
119                 status = (status & ~(ST0_KUP | ST0_IEP | ST0_IEC)) |
120                          ((status & (ST0_KUC | ST0_IEC)) << 2);
121 #else
122                 status |= ST0_EXL;
123 #endif
124                 childregs->cp0_status = status;
125                 return 0;
126         }
127         *childregs = *regs;
128         childregs->regs[7] = 0; /* Clear error flag */
129         childregs->regs[2] = 0; /* Child gets zero as return value */
130         if (usp)
131                 childregs->regs[29] = usp;
132         ti->addr_limit = USER_DS;
133
134         p->thread.reg29 = (unsigned long) childregs;
135         p->thread.reg31 = (unsigned long) ret_from_fork;
136
137         /*
138          * New tasks lose permission to use the fpu. This accelerates context
139          * switching for most programs since they don't use the fpu.
140          */
141         childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
142
143         clear_tsk_thread_flag(p, TIF_USEDFPU);
144
145 #ifdef CONFIG_MIPS_MT_FPAFF
146         clear_tsk_thread_flag(p, TIF_FPUBOUND);
147 #endif /* CONFIG_MIPS_MT_FPAFF */
148
149         if (clone_flags & CLONE_SETTLS)
150                 ti->tp_value = regs->regs[7];
151
152         return 0;
153 }
154
155 #ifdef CONFIG_CC_STACKPROTECTOR
156 #include <linux/stackprotector.h>
157 unsigned long __stack_chk_guard __read_mostly;
158 EXPORT_SYMBOL(__stack_chk_guard);
159 #endif
160
161 struct mips_frame_info {
162         void            *func;
163         unsigned long   func_size;
164         int             frame_size;
165         int             pc_offset;
166 };
167
168 #define J_TARGET(pc,target)     \
169                 (((unsigned long)(pc) & 0xf0000000) | ((target) << 2))
170
171 static inline int is_ra_save_ins(union mips_instruction *ip)
172 {
173 #ifdef CONFIG_CPU_MICROMIPS
174         union mips_instruction mmi;
175
176         /*
177          * swsp ra,offset
178          * swm16 reglist,offset(sp)
179          * swm32 reglist,offset(sp)
180          * sw32 ra,offset(sp)
181          * jradiussp - NOT SUPPORTED
182          *
183          * microMIPS is way more fun...
184          */
185         if (mm_insn_16bit(ip->halfword[0])) {
186                 mmi.word = (ip->halfword[0] << 16);
187                 return ((mmi.mm16_r5_format.opcode == mm_swsp16_op &&
188                          mmi.mm16_r5_format.rt == 31) ||
189                         (mmi.mm16_m_format.opcode == mm_pool16c_op &&
190                          mmi.mm16_m_format.func == mm_swm16_op));
191         }
192         else {
193                 mmi.halfword[0] = ip->halfword[1];
194                 mmi.halfword[1] = ip->halfword[0];
195                 return ((mmi.mm_m_format.opcode == mm_pool32b_op &&
196                          mmi.mm_m_format.rd > 9 &&
197                          mmi.mm_m_format.base == 29 &&
198                          mmi.mm_m_format.func == mm_swm32_func) ||
199                         (mmi.i_format.opcode == mm_sw32_op &&
200                          mmi.i_format.rs == 29 &&
201                          mmi.i_format.rt == 31));
202         }
203 #else
204         /* sw / sd $ra, offset($sp) */
205         return (ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
206                 ip->i_format.rs == 29 &&
207                 ip->i_format.rt == 31;
208 #endif
209 }
210
211 static inline int is_jump_ins(union mips_instruction *ip)
212 {
213 #ifdef CONFIG_CPU_MICROMIPS
214         /*
215          * jr16,jrc,jalr16,jalr16
216          * jal
217          * jalr/jr,jalr.hb/jr.hb,jalrs,jalrs.hb
218          * jraddiusp - NOT SUPPORTED
219          *
220          * microMIPS is kind of more fun...
221          */
222         union mips_instruction mmi;
223
224         mmi.word = (ip->halfword[0] << 16);
225
226         if ((mmi.mm16_r5_format.opcode == mm_pool16c_op &&
227             (mmi.mm16_r5_format.rt & mm_jr16_op) == mm_jr16_op) ||
228             ip->j_format.opcode == mm_jal32_op)
229                 return 1;
230         if (ip->r_format.opcode != mm_pool32a_op ||
231                         ip->r_format.func != mm_pool32axf_op)
232                 return 0;
233         return (((ip->u_format.uimmediate >> 6) & mm_jalr_op) == mm_jalr_op);
234 #else
235         if (ip->j_format.opcode == j_op)
236                 return 1;
237         if (ip->j_format.opcode == jal_op)
238                 return 1;
239         if (ip->r_format.opcode != spec_op)
240                 return 0;
241         return ip->r_format.func == jalr_op || ip->r_format.func == jr_op;
242 #endif
243 }
244
245 static inline int is_sp_move_ins(union mips_instruction *ip)
246 {
247 #ifdef CONFIG_CPU_MICROMIPS
248         /*
249          * addiusp -imm
250          * addius5 sp,-imm
251          * addiu32 sp,sp,-imm
252          * jradiussp - NOT SUPPORTED
253          *
254          * microMIPS is not more fun...
255          */
256         if (mm_insn_16bit(ip->halfword[0])) {
257                 union mips_instruction mmi;
258
259                 mmi.word = (ip->halfword[0] << 16);
260                 return ((mmi.mm16_r3_format.opcode == mm_pool16d_op &&
261                          mmi.mm16_r3_format.simmediate && mm_addiusp_func) ||
262                         (mmi.mm16_r5_format.opcode == mm_pool16d_op &&
263                          mmi.mm16_r5_format.rt == 29));
264         }
265         return (ip->mm_i_format.opcode == mm_addiu32_op &&
266                  ip->mm_i_format.rt == 29 && ip->mm_i_format.rs == 29);
267 #else
268         /* addiu/daddiu sp,sp,-imm */
269         if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
270                 return 0;
271         if (ip->i_format.opcode == addiu_op || ip->i_format.opcode == daddiu_op)
272                 return 1;
273 #endif
274         return 0;
275 }
276
277 static int get_frame_info(struct mips_frame_info *info)
278 {
279 #ifdef CONFIG_CPU_MICROMIPS
280         union mips_instruction *ip = (void *) (((char *) info->func) - 1);
281 #else
282         union mips_instruction *ip = info->func;
283 #endif
284         unsigned max_insns = info->func_size / sizeof(union mips_instruction);
285         unsigned i;
286
287         info->pc_offset = -1;
288         info->frame_size = 0;
289
290         if (!ip)
291                 goto err;
292
293         if (max_insns == 0)
294                 max_insns = 128U;       /* unknown function size */
295         max_insns = min(128U, max_insns);
296
297         for (i = 0; i < max_insns; i++, ip++) {
298
299                 if (is_jump_ins(ip))
300                         break;
301                 if (!info->frame_size) {
302                         if (is_sp_move_ins(ip))
303                         {
304 #ifdef CONFIG_CPU_MICROMIPS
305                                 if (mm_insn_16bit(ip->halfword[0]))
306                                 {
307                                         unsigned short tmp;
308
309                                         if (ip->halfword[0] & mm_addiusp_func)
310                                         {
311                                                 tmp = (((ip->halfword[0] >> 1) & 0x1ff) << 2);
312                                                 info->frame_size = -(signed short)(tmp | ((tmp & 0x100) ? 0xfe00 : 0));
313                                         } else {
314                                                 tmp = (ip->halfword[0] >> 1);
315                                                 info->frame_size = -(signed short)(tmp & 0xf);
316                                         }
317                                         ip = (void *) &ip->halfword[1];
318                                         ip--;
319                                 } else
320 #endif
321                                 info->frame_size = - ip->i_format.simmediate;
322                         }
323                         continue;
324                 }
325                 if (info->pc_offset == -1 && is_ra_save_ins(ip)) {
326                         info->pc_offset =
327                                 ip->i_format.simmediate / sizeof(long);
328                         break;
329                 }
330         }
331         if (info->frame_size && info->pc_offset >= 0) /* nested */
332                 return 0;
333         if (info->pc_offset < 0) /* leaf */
334                 return 1;
335         /* prologue seems boggus... */
336 err:
337         return -1;
338 }
339
340 static struct mips_frame_info schedule_mfi __read_mostly;
341
342 #ifdef CONFIG_KALLSYMS
343 static unsigned long get___schedule_addr(void)
344 {
345         return kallsyms_lookup_name("__schedule");
346 }
347 #else
348 static unsigned long get___schedule_addr(void)
349 {
350         union mips_instruction *ip = (void *)schedule;
351         int max_insns = 8;
352         int i;
353
354         for (i = 0; i < max_insns; i++, ip++) {
355                 if (ip->j_format.opcode == j_op)
356                         return J_TARGET(ip, ip->j_format.target);
357         }
358         return 0;
359 }
360 #endif
361
362 static int __init frame_info_init(void)
363 {
364         unsigned long size = 0;
365 #ifdef CONFIG_KALLSYMS
366         unsigned long ofs;
367 #endif
368         unsigned long addr;
369
370         addr = get___schedule_addr();
371         if (!addr)
372                 addr = (unsigned long)schedule;
373
374 #ifdef CONFIG_KALLSYMS
375         kallsyms_lookup_size_offset(addr, &size, &ofs);
376 #endif
377         schedule_mfi.func = (void *)addr;
378         schedule_mfi.func_size = size;
379
380         get_frame_info(&schedule_mfi);
381
382         /*
383          * Without schedule() frame info, result given by
384          * thread_saved_pc() and get_wchan() are not reliable.
385          */
386         if (schedule_mfi.pc_offset < 0)
387                 printk("Can't analyze schedule() prologue at %p\n", schedule);
388
389         return 0;
390 }
391
392 arch_initcall(frame_info_init);
393
394 /*
395  * Return saved PC of a blocked thread.
396  */
397 unsigned long thread_saved_pc(struct task_struct *tsk)
398 {
399         struct thread_struct *t = &tsk->thread;
400
401         /* New born processes are a special case */
402         if (t->reg31 == (unsigned long) ret_from_fork)
403                 return t->reg31;
404         if (schedule_mfi.pc_offset < 0)
405                 return 0;
406         return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
407 }
408
409
410 #ifdef CONFIG_KALLSYMS
411 /* generic stack unwinding function */
412 unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
413                                               unsigned long *sp,
414                                               unsigned long pc,
415                                               unsigned long *ra)
416 {
417         struct mips_frame_info info;
418         unsigned long size, ofs;
419         int leaf;
420         extern void ret_from_irq(void);
421         extern void ret_from_exception(void);
422
423         if (!stack_page)
424                 return 0;
425
426         /*
427          * If we reached the bottom of interrupt context,
428          * return saved pc in pt_regs.
429          */
430         if (pc == (unsigned long)ret_from_irq ||
431             pc == (unsigned long)ret_from_exception) {
432                 struct pt_regs *regs;
433                 if (*sp >= stack_page &&
434                     *sp + sizeof(*regs) <= stack_page + THREAD_SIZE - 32) {
435                         regs = (struct pt_regs *)*sp;
436                         pc = regs->cp0_epc;
437                         if (__kernel_text_address(pc)) {
438                                 *sp = regs->regs[29];
439                                 *ra = regs->regs[31];
440                                 return pc;
441                         }
442                 }
443                 return 0;
444         }
445         if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
446                 return 0;
447         /*
448          * Return ra if an exception occurred at the first instruction
449          */
450         if (unlikely(ofs == 0)) {
451                 pc = *ra;
452                 *ra = 0;
453                 return pc;
454         }
455
456         info.func = (void *)(pc - ofs);
457         info.func_size = ofs;   /* analyze from start to ofs */
458         leaf = get_frame_info(&info);
459         if (leaf < 0)
460                 return 0;
461
462         if (*sp < stack_page ||
463             *sp + info.frame_size > stack_page + THREAD_SIZE - 32)
464                 return 0;
465
466         if (leaf)
467                 /*
468                  * For some extreme cases, get_frame_info() can
469                  * consider wrongly a nested function as a leaf
470                  * one. In that cases avoid to return always the
471                  * same value.
472                  */
473                 pc = pc != *ra ? *ra : 0;
474         else
475                 pc = ((unsigned long *)(*sp))[info.pc_offset];
476
477         *sp += info.frame_size;
478         *ra = 0;
479         return __kernel_text_address(pc) ? pc : 0;
480 }
481 EXPORT_SYMBOL(unwind_stack_by_address);
482
483 /* used by show_backtrace() */
484 unsigned long unwind_stack(struct task_struct *task, unsigned long *sp,
485                            unsigned long pc, unsigned long *ra)
486 {
487         unsigned long stack_page = (unsigned long)task_stack_page(task);
488         return unwind_stack_by_address(stack_page, sp, pc, ra);
489 }
490 #endif
491
492 /*
493  * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
494  */
495 unsigned long get_wchan(struct task_struct *task)
496 {
497         unsigned long pc = 0;
498 #ifdef CONFIG_KALLSYMS
499         unsigned long sp;
500         unsigned long ra = 0;
501 #endif
502
503         if (!task || task == current || task->state == TASK_RUNNING)
504                 goto out;
505         if (!task_stack_page(task))
506                 goto out;
507
508         pc = thread_saved_pc(task);
509
510 #ifdef CONFIG_KALLSYMS
511         sp = task->thread.reg29 + schedule_mfi.frame_size;
512
513         while (in_sched_functions(pc))
514                 pc = unwind_stack(task, &sp, pc, &ra);
515 #endif
516
517 out:
518         return pc;
519 }
520
521 /*
522  * Don't forget that the stack pointer must be aligned on a 8 bytes
523  * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
524  */
525 unsigned long arch_align_stack(unsigned long sp)
526 {
527         if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
528                 sp -= get_random_int() & ~PAGE_MASK;
529
530         return sp & ALMASK;
531 }