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