1/*
2 *  arch/ppc64/kernel/entry.S
3 *
4 *  PowerPC version
5 *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
7 *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
8 *  Adapted for Power Macintosh by Paul Mackerras.
9 *  Low-level exception handlers and MMU support
10 *  rewritten by Paul Mackerras.
11 *    Copyright (C) 1996 Paul Mackerras.
12 *  MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
13 *
14 *  This file contains the system call entry code, context switch
15 *  code, and exception/interrupt return code for PowerPC.
16 *
17 *  This program is free software; you can redistribute it and/or
18 *  modify it under the terms of the GNU General Public License
19 *  as published by the Free Software Foundation; either version
20 *  2 of the License, or (at your option) any later version.
21 */
22
23#include "ppc_asm.h"
24#include <asm/processor.h>
25#include <asm/page.h>
26#include <asm/mmu.h>
27#include <linux/errno.h>
28#include <linux/sys.h>
29#include <linux/config.h>
30
31#ifdef CONFIG_PPC_ISERIES
32#define DO_SOFT_DISABLE
33#endif
34
35#undef SHOW_SYSCALLS
36#undef SHOW_SYSCALLS_TASK
37
38#ifdef SHOW_SYSCALLS_TASK
39	.data
40show_syscalls_task:
41	.long	-1
42#endif
43
44/*
45 * Handle a system call.
46 */
47	.text
48_GLOBAL(DoSyscall)
49	std	r0,THREAD+LAST_SYSCALL(r13)
50	ld	r11,_CCR(r1)	/* Clear SO bit in CR */
51	lis	r10,0x1000
52	andc	r11,r11,r10
53	std	r11,_CCR(r1)
54#ifdef SHOW_SYSCALLS
55#ifdef SHOW_SYSCALLS_TASK
56	LOADBASE(r31,show_syscalls_task)
57	ld	r31,show_syscalls_task@l(r31)
58	cmp	0,r13,r31
59	bne	1f
60#endif
61	LOADADDR(r3,7f)
62	ld	r4,GPR0(r1)
63	ld	r5,GPR3(r1)
64	ld	r6,GPR4(r1)
65	ld	r7,GPR5(r1)
66	ld	r8,GPR6(r1)
67	ld	r9,GPR7(r1)
68	bl	.printk
69	LOADADDR(r3,77f)
70	ld	r4,GPR8(r1)
71	ld	r5,GPR9(r1)
72	mr	r6,r13
73	bl	.printk
74	ld	r0,GPR0(r1)
75	ld	r3,GPR3(r1)
76	ld	r4,GPR4(r1)
77	ld	r5,GPR5(r1)
78	ld	r6,GPR6(r1)
79	ld	r7,GPR7(r1)
80	ld	r8,GPR8(r1)
811:
82#endif /* SHOW_SYSCALLS */
83	ld	r10,TASK_PTRACE(r13)
84	andi.	r10,r10,PT_TRACESYS
85	bne-	50f
86	cmpli	0,r0,NR_syscalls
87	bge-	66f
88/* Ken Aaker: Need to vector to 32 Bit or default sys_call_table here,
89 *            based on caller's run-mode / personality.
90 *
91 */
92#ifdef CONFIG_BINFMT_ELF32
93	ld	r10,THREAD+THREAD_FLAGS(r13)
94	andi.	r10,r10,PPC_FLAG_32BIT
95	beq-	15f
96	LOADADDR(r10,.sys_call_table32)
97/* Now mung the first 4 parameters into shape, by making certain that
98 * the high bits (most significant 32 bits in 64 bit reg) are 0
99 * for the first 4 parameter regs(3-6).
100 */
101	clrldi	r3,r3,32
102	clrldi	r4,r4,32
103	clrldi	r5,r5,32
104	clrldi	r6,r6,32
105	b	17f
10615:
107#endif
108	LOADADDR(r10,.sys_call_table)
10917:
110	slwi	r0,r0,3
111	ldx	r10,r10,r0	/* Fetch system call handler [ptr] */
112	mtlr	r10
113	addi	r9,r1,STACK_FRAME_OVERHEAD
114	blrl			/* Call handler */
115_GLOBAL(ret_from_syscall_1)
11620:	std	r3,RESULT(r1)	/* Save result */
117#ifdef SHOW_SYSCALLS
118#ifdef SHOW_SYSCALLS_TASK
119	cmp	0,r13,r31
120	bne	91f
121#endif
122	mr	r4,r3
123	LOADADDR(r3,79f)
124	bl	.printk
125	ld	r3,RESULT(r1)
12691:
127#endif
128	li	r10,-_LAST_ERRNO
129	cmpl	0,r3,r10
130	blt	30f
131	neg	r3,r3
132	cmpi	0,r3,ERESTARTNOHAND
133	bne	22f
134	li	r3,EINTR
13522:	ld	r10,_CCR(r1)	/* Set SO bit in CR */
136	oris	r10,r10,0x1000
137	std	r10,_CCR(r1)
13830:	std	r3,GPR3(r1)	/* Update return value */
139	b	.ret_from_except
14066:	li	r3,ENOSYS
141	b	22b
142
143/* Traced system call support */
14450:	bl	.syscall_trace
145	ld	r0,GPR0(r1)	/* Restore original registers */
146	ld	r3,GPR3(r1)
147	ld	r4,GPR4(r1)
148	ld	r5,GPR5(r1)
149	ld	r6,GPR6(r1)
150	ld	r7,GPR7(r1)
151	ld	r8,GPR8(r1)
152	ld	r9,GPR9(r1)
153	cmpli	0,r0,NR_syscalls
154	bge-	66f
155#ifdef CONFIG_BINFMT_ELF32
156	ld	r10,THREAD+THREAD_FLAGS(r13)
157	andi.	r10,r10,PPC_FLAG_32BIT
158	beq-	55f
159	LOADADDR(r10,.sys_call_table32)
160/* Now mung the first 4 parameters into shape, by making certain that
161 * the high bits (most significant 32 bits in 64 bit reg) are 0
162 * for the first 4 parameter regs(3-6).
163 */
164	clrldi	r3,r3,32
165	clrldi	r4,r4,32
166	clrldi	r5,r5,32
167	clrldi	r6,r6,32
168	b	57f
16955:
170#endif
171	LOADADDR(r10,.sys_call_table)
17257:
173	slwi	r0,r0,3
174	ldx	r10,r10,r0	/* Fetch system call handler [ptr] */
175	mtlr	r10
176	addi	r9,r1,STACK_FRAME_OVERHEAD
177	blrl			/* Call handler */
178_GLOBAL(ret_from_syscall_2)
17958:	std	r3,RESULT(r1)	/* Save result */
180	std	r3,GPR0(r1)	/* temporary gross hack to make strace work */
181	li	r10,-_LAST_ERRNO
182	cmpl	0,r3,r10
183	blt	60f
184	neg	r3,r3
185	cmpi	0,r3,ERESTARTNOHAND
186	bne	57f
187	li	r3,EINTR
18857:	ld	r10,_CCR(r1)	/* Set SO bit in CR */
189	oris	r10,r10,0x1000
190	std	r10,_CCR(r1)
19160:	std	r3,GPR3(r1)	/* Update return value */
192	bl	.syscall_trace
193	b	.ret_from_except
19466:	li	r3,ENOSYS
195	b	57b
196#ifdef SHOW_SYSCALLS
1977:	.string	"syscall %d(%x, %x, %x, %x, %x, "
19877:	.string	"%x, %x), current=%p\n"
19979:	.string	" -> %x\n"
200	.align	2,0
201#endif
202
203_GLOBAL(ppc32_sigreturn)
204	bl	.sys32_sigreturn
205	b	80f
206
207_GLOBAL(ppc32_rt_sigreturn)
208	bl	.sys32_rt_sigreturn
209	b	80f
210
211_GLOBAL(ppc64_sigreturn)
212	bl	.sys_sigreturn
213	b	80f
214
215_GLOBAL(ppc64_rt_sigreturn)
216	bl	.sys_rt_sigreturn
217
21880:	ld	r10,TASK_PTRACE(r13)
219	andi.	r10,r10,PT_TRACESYS
220	bne-	81f
221	cmpi	0,r3,0
222	bge	.ret_from_except
223	b	20b
22481:	cmpi	0,r3,0
225	blt	58b
226	bl	.syscall_trace
227	b	.ret_from_except
228
229/*
230 * This routine switches between two different tasks.  The process
231 * state of one is saved on its kernel stack.  Then the state
232 * of the other is restored from its kernel stack.  The memory
233 * management hardware is updated to the second process's state.
234 * Finally, we can return to the second process, via ret_from_except.
235 * On entry, r3 points to the THREAD for the current task, r4
236 * points to the THREAD for the new task.
237 *
238 * Note: there are two ways to get to the "going out" portion
239 * of this code; either by coming in via the entry (_switch)
240 * or via "fork" which must set up an environment equivalent
241 * to the "_switch" path.  If you change this (or in particular, the
242 * SAVE_REGS macro), you'll have to change the fork code also.
243 *
244 * The code which creates the new task context is in 'copy_thread'
245 * in arch/ppc64/kernel/process.c
246 */
247_GLOBAL(_switch)
248	stdu	r1,-INT_FRAME_SIZE(r1)
249	ld	r6,0(r1)
250	std	r6,GPR1(r1)
251	/* r3-r13 are caller saved -- Cort */
252	SAVE_GPR(2, r1)
253	SAVE_8GPRS(14, r1)
254	SAVE_10GPRS(22, r1)
255	mflr	r20		/* Return to switch caller */
256	mfmsr	r22
257	li	r6,MSR_FP	/* Disable floating-point */
258	andc	r22,r22,r6
259	mtmsrd	r22
260	isync
261	std	r20,_NIP(r1)
262	std	r22,_MSR(r1)
263	std	r20,_LINK(r1)
264	mfcr	r20
265	std	r20,_CCR(r1)
266	li	r6,0x0ff0
267	std	r6,TRAP(r1)
268	std	r1,KSP(r3)	/* Set old stack pointer */
269
270	mfspr	r5,SPRG3	/* Get Paca */
271	addi	r3,r3,-THREAD	/* old 'current' for return value */
272	addi	r13,r4,-THREAD	/* Convert THREAD to 'current' */
273	std	r13,PACACURRENT(r5)	/* Set new 'current' */
274
275#ifdef CONFIG_PPC_ISERIES
276	ld	r7,THREAD_FLAGS(r4)	/* Get run light flag */
277	mfspr	r9,CTRLF
278	srdi	r7,r7,1		/* Align to run light bit in CTRL reg */
279	insrdi	r9,r7,1,63	/* Insert run light into CTRL */
280	mtspr	CTRLT,r9
281#endif
282	ld	r1,KSP(r4)	/* Load new stack pointer */
283	ld	r6,_CCR(r1)
284	mtcrf	0xFF,r6
285	/* r3-r13 are destroyed -- Cort */
286	REST_8GPRS(14, r1)
287	REST_10GPRS(22, r1)
288
289	ld	r7,_NIP(r1)	/* Return to _switch caller in new task */
290	ld	r1,GPR1(r1)
291	mtlr	r7
292	blr
293
294_GLOBAL(ret_from_fork)
295	bl	.schedule_tail
296	ld	r0,TASK_PTRACE(r13)
297	andi.	r0,r0,PT_TRACESYS
298	beq+	.ret_from_except
299	bl	.syscall_trace
300	b	.ret_from_except
301
302_GLOBAL(ret_from_except)
303#ifdef CONFIG_PPC_ISERIES
304	ld	r5,SOFTE(r1)
305	cmpdi	0,r5,0
306	beq	4f
307irq_recheck:
308	/* Check for pending interrupts (iSeries) */
309	CHECKANYINT(r3,r4)
310	beq+	4f	/* skip do_IRQ if no interrupts */
311
312	mfspr	r5,SPRG3
313	li	r3,0
314	stb	r3,PACAPROCENABLED(r5)	/* ensure we are disabled */
315	addi	r3,r1,STACK_FRAME_OVERHEAD
316	bl	.do_IRQ
317	b	irq_recheck	/* loop back and handle more */
3184:
319#endif
320_GLOBAL(do_bottom_half_ret)
321	ld	r3,_MSR(r1)	/* Returning to user mode? */
322	andi.	r3,r3,MSR_PR
323	beq+	restore		/* if so, check need_resched and signals */
324_GLOBAL(ret_to_user_hook)
325	nop
326	/* NEED_RESCHED is a volatile long (64-bits) */
327	ld	r3,NEED_RESCHED(r13)
328	cmpi	0,r3,0		/* check need_resched flag */
329	beq+	7f
330	bl	.schedule
331	/* SIGPENDING is an int (32-bits) */
3327:
333	lwz	r5,SIGPENDING(r13) /* Check for pending unblocked signals */
334	cmpwi	0,r5,0
335	beq+	restore
336	li	r3,0
337	addi	r4,r1,STACK_FRAME_OVERHEAD
338	bl	.do_signal
339_GLOBAL(do_signal_ret)
340restore:
341	ld	r3,_CTR(r1)
342	ld	r0,_LINK(r1)
343	mtctr	r3
344	mtlr	r0
345	ld	r3,_XER(r1)
346	mtspr	XER,r3
347
348	REST_8GPRS(5, r1)
349	REST_10GPRS(14, r1)
350	REST_8GPRS(24, r1)
351
352	/* make sure we hard disable here, even if rtl is active, to protect
353	 * SRR[01] and SPRG2 -- Cort
354	 */
355	mfmsr	r0		/* Get current interrupt state */
356	li	r4,0
357	ori	r4,r4,MSR_EE|MSR_RI
358	andc	r0,r0,r4	/* clear MSR_EE and MSR_RI */
359	mtmsrd	r0		/* Update machine state */
360#ifdef CONFIG_PPC_ISERIES
361	ld	r0,SOFTE(r1)
362	cmpi	0,r0,0
363	beq+	1f
364
365	CHECKANYINT(r4,r3)
366	beq+	1f
367	mfmsr	r0
368	ori	r0,r0,MSR_EE|MSR_RI
369	mtmsrd	r0
370	b	irq_recheck
371
3721:
373#endif
374	stdcx.	r0,0,r1		/* to clear the reservation */
375
376	mfspr	r4,SPRG3	/* current task's PACA */
377#ifdef DO_SOFT_DISABLE
378	ld	r0,SOFTE(r1)
379	stb	r0,PACAPROCENABLED(r4)
380#endif
381	/* if returning to user mode, save kernel SP */
382	ld	r0,_MSR(r1)
383	andi.	r0,r0,MSR_PR
384	beq+	1f
385	addi	r0,r1,INT_FRAME_SIZE	/* size of frame */
386	std	r0,THREAD+KSP(r13)	/* save kernel stack pointer */
387	std	r1,PACAKSAVE(r4)	/* save exception stack pointer */
3881:
389	ld	r0,_MSR(r1)
390	mtspr	SRR1,r0
391	ld	r2,_CCR(r1)
392	mtcrf	0xFF,r2
393	ld	r2,_NIP(r1)
394	mtspr	SRR0,r2
395	REST_GPR(13,r1)
396	ld	r0,GPR0(r1)
397	ld	r2,GPR2(r1)
398	ld	r3,GPR3(r1)
399	ld	r4,GPR4(r1)
400	ld	r1,GPR1(r1)
401
402	rfid
403
404/*
405 * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
406 * called with the MMU off.
407 *
408 * In addition, we need to be in 32b mode, at least for now.
409 *
410 * Note: r3 is an input parameter to rtas, so don't trash it...
411 */
412_GLOBAL(enter_rtas)
413	mflr	r0
414	std	r0,16(r1)
415        stdu	r1,-RTAS_FRAME_SIZE(r1)	/* Save SP and create stack space. */
416
417	/* Because RTAS is running in 32b mode, it clobbers the high order half
418	 * of all registers that it saves.  We therefore save those registers
419	 * RTAS might touch to the stack.  (r0, r3-r13 are caller saved)
420   	 */
421	SAVE_GPR(2, r1)			/* Save the TOC */
422	SAVE_GPR(13, r1)		/* Save current */
423	SAVE_8GPRS(14, r1)		/* Save the non-volatiles */
424	SAVE_10GPRS(22, r1)		/* ditto */
425
426	mfcr	r4
427	std	r4,_CCR(r1)
428	mfctr	r5
429	std	r5,_CTR(r1)
430	mfspr	r6,XER
431	std	r6,_XER(r1)
432	mfdar	r7
433	std	r7,_DAR(r1)
434	mfdsisr	r8
435	std	r8,_DSISR(r1)
436	mfsrr0	r9
437	std	r9,_SRR0(r1)
438	mfsrr1	r10
439	std	r10,_SRR1(r1)
440
441	/* Unfortunatly, the stack pointer and the MSR are also clobbered,
442	 * so they are saved in the PACA (SPRG3) which allows us to restore
443	 * our original state after RTAS returns.
444         */
445	mfspr	r4,SPRG3		/* Get PACA */
446	std	r1,PACAR1(r4)
447	mfmsr	r6
448        std	r6,PACASAVEDMSR(r4)
449
450	/* Setup our real return addr */
451	SET_REG_TO_LABEL(r4,.rtas_return_loc)
452	SET_REG_TO_CONST(r9,KERNELBASE)
453	sub	r4,r4,r9
454       	mtlr	r4
455
456	li	r0,0
457	ori	r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
458	andc	r0,r6,r0
459
460        li      r9,1
461        rldicr  r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
462	ori	r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI
463	andc	r6,r0,r9
464	sync				/* disable interrupts so SRR0/1 */
465	mtmsrd	r0			/* don't get trashed */
466
467	SET_REG_TO_LABEL(r4,rtas)
468	ld	r5,RTASENTRY(r4)	/* get the rtas->entry value */
469	ld	r4,RTASBASE(r4)		/* get the rtas->base value */
470
471	mtspr	SRR0,r5
472	mtspr	SRR1,r6
473	rfid
474
475_STATIC(rtas_return_loc)
476	/* relocation is off at this point */
477	mfspr	r4,SPRG3	        /* Get PACA */
478	SET_REG_TO_CONST(r5, KERNELBASE)
479        sub     r4,r4,r5                /* RELOC the PACA base pointer */
480
481        ld	r1,PACAR1(r4)           /* Restore our SP */
482	LOADADDR(r3,.rtas_restore_regs)
483        ld	r4,PACASAVEDMSR(r4)     /* Restore our MSR */
484
485	mtspr	SRR0,r3
486	mtspr	SRR1,r4
487	rfid
488
489_STATIC(rtas_restore_regs)
490	/* relocation is on at this point */
491	REST_GPR(2, r1)			/* Restore the TOC */
492	REST_GPR(13, r1)		/* Restore current */
493	REST_8GPRS(14, r1)		/* Restore the non-volatiles */
494	REST_10GPRS(22, r1)		/* ditto */
495
496	/* put back current in r13 */
497        mfspr	r4,SPRG3
498	ld	r13,PACACURRENT(r4)
499
500	ld	r4,_CCR(r1)
501	mtcr	r4
502	ld	r5,_CTR(r1)
503	mtctr	r5
504	ld	r6,_XER(r1)
505	mtspr	XER,r6
506	ld	r7,_DAR(r1)
507	mtdar	r7
508	ld	r8,_DSISR(r1)
509	mtdsisr	r8
510	ld	r9,_SRR0(r1)
511	mtsrr0	r9
512	ld	r10,_SRR1(r1)
513	mtsrr1	r10
514
515        addi	r1,r1,RTAS_FRAME_SIZE	/* Unstack our frame */
516	ld	r0,16(r1)		/* get return address */
517
518	mtlr    r0
519        blr				/* return to caller */
520
521_GLOBAL(enter_prom)
522	mflr	r0
523	std	r0,16(r1)
524        stdu	r1,-PROM_FRAME_SIZE(r1)	/* Save SP and create stack space */
525
526	/* Because PROM is running in 32b mode, it clobbers the high order half
527	 * of all registers that it saves.  We therefore save those registers
528	 * PROM might touch to the stack.  (r0, r3-r13 are caller saved)
529   	 */
530	SAVE_8GPRS(2, r1)		/* Save the TOC & incoming param(s) */
531	SAVE_GPR(13, r1)		/* Save current */
532	SAVE_8GPRS(14, r1)		/* Save the non-volatiles */
533	SAVE_10GPRS(22, r1)		/* ditto */
534
535	mfcr	r4
536	std	r4,_CCR(r1)
537	mfctr	r5
538	std	r5,_CTR(r1)
539	mfspr	r6,XER
540	std	r6,_XER(r1)
541	mfdar	r7
542	std	r7,_DAR(r1)
543	mfdsisr	r8
544	std	r8,_DSISR(r1)
545	mfsrr0	r9
546	std	r9,_SRR0(r1)
547	mfsrr1	r10
548	std	r10,_SRR1(r1)
549	mfmsr	r11
550	std	r11,_MSR(r1)
551
552	/* Unfortunatly, the stack pointer is also clobbered, so it is saved
553	 * in the SPRG2 which allows us to restore our original state after
554	 * PROM returns.
555         */
556	mtspr	SPRG2,r1
557
558        /* put a relocation offset into r3 */
559        bl      .reloc_offset
560	LOADADDR(r12,prom)
561	sub	r12,r12,r3
562	ld	r12,PROMENTRY(r12)	/* get the prom->entry value */
563	mtlr	r12
564
565        mfmsr   r11			/* grab the current MSR */
566        li      r12,1
567        rldicr  r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
568        andc    r11,r11,r12
569        li      r12,1
570        rldicr  r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
571        andc    r11,r11,r12
572        mtmsrd  r11
573        isync
574
575	REST_8GPRS(2, r1)		/* Restore the TOC & param(s) */
576	REST_GPR(13, r1)		/* Restore current */
577	REST_8GPRS(14, r1)		/* Restore the non-volatiles */
578	REST_10GPRS(22, r1)		/* ditto */
579	blrl				/* Entering PROM here... */
580
581	mfspr	r1,SPRG2		/* Restore the stack pointer */
582	ld	r6,_MSR(r1)		/* Restore the MSR */
583	mtmsrd	r6
584        isync
585
586	REST_GPR(2, r1)			/* Restore the TOC */
587	REST_GPR(13, r1)		/* Restore current */
588	REST_8GPRS(14, r1)		/* Restore the non-volatiles */
589	REST_10GPRS(22, r1)		/* ditto */
590
591	ld	r4,_CCR(r1)
592	mtcr	r4
593	ld	r5,_CTR(r1)
594	mtctr	r5
595	ld	r6,_XER(r1)
596	mtspr	XER,r6
597	ld	r7,_DAR(r1)
598	mtdar	r7
599	ld	r8,_DSISR(r1)
600	mtdsisr	r8
601	ld	r9,_SRR0(r1)
602	mtsrr0	r9
603	ld	r10,_SRR1(r1)
604	mtsrr1	r10
605        addi	r1,r1,PROM_FRAME_SIZE
606	ld	r0,16(r1)		/* get return address */
607
608	mtlr    r0
609        blr				/* return to caller */
610