1/*
2 *  arch/s390/kernel/entry64.S
3 *    S390 low-level entry points.
4 *
5 *    Copyright (C) IBM Corp. 1999,2012
6 *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7 *		 Hartmut Penner (hp@de.ibm.com),
8 *		 Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
9 *		 Heiko Carstens <heiko.carstens@de.ibm.com>
10 */
11
12#include <linux/init.h>
13#include <linux/linkage.h>
14#include <asm/cache.h>
15#include <asm/errno.h>
16#include <asm/ptrace.h>
17#include <asm/thread_info.h>
18#include <asm/asm-offsets.h>
19#include <asm/unistd.h>
20#include <asm/page.h>
21
22__PT_R0      =	__PT_GPRS
23__PT_R1      =	__PT_GPRS + 8
24__PT_R2      =	__PT_GPRS + 16
25__PT_R3      =	__PT_GPRS + 24
26__PT_R4      =	__PT_GPRS + 32
27__PT_R5      =	__PT_GPRS + 40
28__PT_R6      =	__PT_GPRS + 48
29__PT_R7      =	__PT_GPRS + 56
30__PT_R8      =	__PT_GPRS + 64
31__PT_R9      =	__PT_GPRS + 72
32__PT_R10     =	__PT_GPRS + 80
33__PT_R11     =	__PT_GPRS + 88
34__PT_R12     =	__PT_GPRS + 96
35__PT_R13     =	__PT_GPRS + 104
36__PT_R14     =	__PT_GPRS + 112
37__PT_R15     =	__PT_GPRS + 120
38
39STACK_SHIFT = PAGE_SHIFT + THREAD_ORDER
40STACK_SIZE  = 1 << STACK_SHIFT
41
42_TIF_WORK_SVC = (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_NEED_RESCHED | \
43		 _TIF_MCCK_PENDING | _TIF_PER_TRAP )
44_TIF_WORK_INT = (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_NEED_RESCHED | \
45		 _TIF_MCCK_PENDING)
46_TIF_TRACE    = (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \
47		 _TIF_SYSCALL_TRACEPOINT)
48_TIF_EXIT_SIE = (_TIF_SIGPENDING | _TIF_NEED_RESCHED | _TIF_MCCK_PENDING)
49
50#define BASED(name) name-system_call(%r13)
51
52	.macro	TRACE_IRQS_ON
53#ifdef CONFIG_TRACE_IRQFLAGS
54	basr	%r2,%r0
55	brasl	%r14,trace_hardirqs_on_caller
56#endif
57	.endm
58
59	.macro	TRACE_IRQS_OFF
60#ifdef CONFIG_TRACE_IRQFLAGS
61	basr	%r2,%r0
62	brasl	%r14,trace_hardirqs_off_caller
63#endif
64	.endm
65
66	.macro	LOCKDEP_SYS_EXIT
67#ifdef CONFIG_LOCKDEP
68	tm	__PT_PSW+1(%r11),0x01	# returning to user ?
69	jz	.+10
70	brasl	%r14,lockdep_sys_exit
71#endif
72	.endm
73
74	.macro SPP newpp
75#if defined(CONFIG_KVM) || defined(CONFIG_KVM_MODULE)
76	tm	__LC_MACHINE_FLAGS+6,0x20	# MACHINE_FLAG_SPP
77	jz	.+8
78	.insn	s,0xb2800000,\newpp
79#endif
80	.endm
81
82	.macro	HANDLE_SIE_INTERCEPT scratch
83#if defined(CONFIG_KVM) || defined(CONFIG_KVM_MODULE)
84	tm	__TI_flags+6(%r12),_TIF_SIE>>8
85	jz	.+42
86	tm	__LC_MACHINE_FLAGS+6,0x20	# MACHINE_FLAG_SPP
87	jz	.+8
88	.insn	s,0xb2800000,BASED(.Lhost_id)	# set host id
89	lgr	\scratch,%r9
90	slg	\scratch,BASED(.Lsie_loop)
91	clg	\scratch,BASED(.Lsie_length)
92	jhe	.+10
93	lg	%r9,BASED(.Lsie_loop)
94#endif
95	.endm
96
97	.macro	CHECK_STACK stacksize,savearea
98#ifdef CONFIG_CHECK_STACK
99	tml	%r15,\stacksize - CONFIG_STACK_GUARD
100	lghi	%r14,\savearea
101	jz	stack_overflow
102#endif
103	.endm
104
105	.macro	SWITCH_ASYNC savearea,stack,shift
106	tmhh	%r8,0x0001		# interrupting from user ?
107	jnz	1f
108	lgr	%r14,%r9
109	slg	%r14,BASED(.Lcritical_start)
110	clg	%r14,BASED(.Lcritical_length)
111	jhe	0f
112	lghi	%r11,\savearea		# inside critical section, do cleanup
113	brasl	%r14,cleanup_critical
114	tmhh	%r8,0x0001		# retest problem state after cleanup
115	jnz	1f
1160:	lg	%r14,\stack		# are we already on the target stack?
117	slgr	%r14,%r15
118	srag	%r14,%r14,\shift
119	jnz	1f
120	CHECK_STACK 1<<\shift,\savearea
121	j	2f
1221:	lg	%r15,\stack		# load target stack
1232:	aghi	%r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
124	la	%r11,STACK_FRAME_OVERHEAD(%r15)
125	.endm
126
127	.macro UPDATE_VTIME scratch,enter_timer
128	lg	\scratch,__LC_EXIT_TIMER
129	slg	\scratch,\enter_timer
130	alg	\scratch,__LC_USER_TIMER
131	stg	\scratch,__LC_USER_TIMER
132	lg	\scratch,__LC_LAST_UPDATE_TIMER
133	slg	\scratch,__LC_EXIT_TIMER
134	alg	\scratch,__LC_SYSTEM_TIMER
135	stg	\scratch,__LC_SYSTEM_TIMER
136	mvc	__LC_LAST_UPDATE_TIMER(8),\enter_timer
137	.endm
138
139	.macro	LAST_BREAK scratch
140	srag	\scratch,%r10,23
141	jz	.+10
142	stg	%r10,__TI_last_break(%r12)
143	.endm
144
145	.macro REENABLE_IRQS
146	stg	%r8,__LC_RETURN_PSW
147	ni	__LC_RETURN_PSW,0xbf
148	ssm	__LC_RETURN_PSW
149	.endm
150
151	.section .kprobes.text, "ax"
152
153/*
154 * Scheduler resume function, called by switch_to
155 *  gpr2 = (task_struct *) prev
156 *  gpr3 = (task_struct *) next
157 * Returns:
158 *  gpr2 = prev
159 */
160ENTRY(__switch_to)
161	lg	%r4,__THREAD_info(%r2)		# get thread_info of prev
162	lg	%r5,__THREAD_info(%r3)		# get thread_info of next
163	tm	__TI_flags+7(%r4),_TIF_MCCK_PENDING # machine check pending?
164	jz	0f
165	ni	__TI_flags+7(%r4),255-_TIF_MCCK_PENDING	# clear flag in prev
166	oi	__TI_flags+7(%r5),_TIF_MCCK_PENDING	# set it in next
1670:	stmg	%r6,%r15,__SF_GPRS(%r15)	# store gprs of prev task
168	stg	%r15,__THREAD_ksp(%r2)		# store kernel stack of prev
169	lg	%r15,__THREAD_ksp(%r3)		# load kernel stack of next
170	lctl	%c4,%c4,__TASK_pid(%r3)		# load pid to control reg. 4
171	lmg	%r6,%r15,__SF_GPRS(%r15)	# load gprs of next task
172	stg	%r3,__LC_CURRENT		# store task struct of next
173	mvc	__LC_CURRENT_PID+4(4,%r0),__TASK_pid(%r3) # store pid of next
174	stg	%r5,__LC_THREAD_INFO		# store thread info of next
175	aghi	%r5,STACK_SIZE			# end of kernel stack of next
176	stg	%r5,__LC_KERNEL_STACK		# store end of kernel stack
177	br	%r14
178
179__critical_start:
180/*
181 * SVC interrupt handler routine. System calls are synchronous events and
182 * are executed with interrupts enabled.
183 */
184
185ENTRY(system_call)
186	stpt	__LC_SYNC_ENTER_TIMER
187sysc_stmg:
188	stmg	%r8,%r15,__LC_SAVE_AREA_SYNC
189	lg	%r10,__LC_LAST_BREAK
190	lg	%r12,__LC_THREAD_INFO
191	larl	%r13,system_call
192sysc_per:
193	lg	%r15,__LC_KERNEL_STACK
194	aghi	%r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
195	la	%r11,STACK_FRAME_OVERHEAD(%r15)	# pointer to pt_regs
196sysc_vtime:
197	UPDATE_VTIME %r13,__LC_SYNC_ENTER_TIMER
198	LAST_BREAK %r13
199	stmg	%r0,%r7,__PT_R0(%r11)
200	mvc	__PT_R8(64,%r11),__LC_SAVE_AREA_SYNC
201	mvc	__PT_PSW(16,%r11),__LC_SVC_OLD_PSW
202	mvc	__PT_INT_CODE(4,%r11),__LC_SVC_ILC
203sysc_do_svc:
204	oi	__TI_flags+7(%r12),_TIF_SYSCALL
205	llgh	%r8,__PT_INT_CODE+2(%r11)
206	slag	%r8,%r8,2			# shift and test for svc 0
207	jnz	sysc_nr_ok
208	# svc 0: system call number in %r1
209	llgfr	%r1,%r1				# clear high word in r1
210	cghi	%r1,NR_syscalls
211	jnl	sysc_nr_ok
212	sth	%r1,__PT_INT_CODE+2(%r11)
213	slag	%r8,%r1,2
214sysc_nr_ok:
215	larl	%r10,sys_call_table		# 64 bit system call table
216#ifdef CONFIG_COMPAT
217	tm	__TI_flags+5(%r12),(_TIF_31BIT>>16)
218	jno	sysc_noemu
219	larl	%r10,sys_call_table_emu		# 31 bit system call table
220sysc_noemu:
221#endif
222	xc	__SF_BACKCHAIN(8,%r15),__SF_BACKCHAIN(%r15)
223	stg	%r2,__PT_ORIG_GPR2(%r11)
224	stg	%r7,STACK_FRAME_OVERHEAD(%r15)
225	lgf	%r9,0(%r8,%r10)			# get system call add.
226	tm	__TI_flags+6(%r12),_TIF_TRACE >> 8
227	jnz	sysc_tracesys
228	basr	%r14,%r9			# call sys_xxxx
229	stg	%r2,__PT_R2(%r11)		# store return value
230
231sysc_return:
232	LOCKDEP_SYS_EXIT
233sysc_tif:
234	tm	__PT_PSW+1(%r11),0x01		# returning to user ?
235	jno	sysc_restore
236	tm	__TI_flags+7(%r12),_TIF_WORK_SVC
237	jnz	sysc_work			# check for work
238	ni	__TI_flags+7(%r12),255-_TIF_SYSCALL
239sysc_restore:
240	lg	%r14,__LC_VDSO_PER_CPU
241	lmg	%r0,%r10,__PT_R0(%r11)
242	mvc	__LC_RETURN_PSW(16),__PT_PSW(%r11)
243	stpt	__LC_EXIT_TIMER
244	mvc	__VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
245	lmg	%r11,%r15,__PT_R11(%r11)
246	lpswe	__LC_RETURN_PSW
247sysc_done:
248
249#
250# One of the work bits is on. Find out which one.
251#
252sysc_work:
253	tm	__TI_flags+7(%r12),_TIF_MCCK_PENDING
254	jo	sysc_mcck_pending
255	tm	__TI_flags+7(%r12),_TIF_NEED_RESCHED
256	jo	sysc_reschedule
257	tm	__TI_flags+7(%r12),_TIF_SIGPENDING
258	jo	sysc_sigpending
259	tm	__TI_flags+7(%r12),_TIF_NOTIFY_RESUME
260	jo	sysc_notify_resume
261	tm	__TI_flags+7(%r12),_TIF_PER_TRAP
262	jo	sysc_singlestep
263	j	sysc_return		# beware of critical section cleanup
264
265#
266# _TIF_NEED_RESCHED is set, call schedule
267#
268sysc_reschedule:
269	larl	%r14,sysc_return
270	jg	schedule
271
272#
273# _TIF_MCCK_PENDING is set, call handler
274#
275sysc_mcck_pending:
276	larl	%r14,sysc_return
277	jg	s390_handle_mcck	# TIF bit will be cleared by handler
278
279#
280# _TIF_SIGPENDING is set, call do_signal
281#
282sysc_sigpending:
283	ni	__TI_flags+7(%r12),255-_TIF_PER_TRAP # clear TIF_PER_TRAP
284	lgr	%r2,%r11		# pass pointer to pt_regs
285	brasl	%r14,do_signal
286	tm	__TI_flags+7(%r12),_TIF_SYSCALL
287	jno	sysc_return
288	lmg	%r2,%r7,__PT_R2(%r11)	# load svc arguments
289	lghi	%r8,0			# svc 0 returns -ENOSYS
290	lh	%r1,__PT_INT_CODE+2(%r11)	# load new svc number
291	cghi	%r1,NR_syscalls
292	jnl	sysc_nr_ok		# invalid svc number -> do svc 0
293	slag	%r8,%r1,2
294	j	sysc_nr_ok		# restart svc
295
296#
297# _TIF_NOTIFY_RESUME is set, call do_notify_resume
298#
299sysc_notify_resume:
300	lgr	%r2,%r11		# pass pointer to pt_regs
301	larl	%r14,sysc_return
302	jg	do_notify_resume
303
304#
305# _TIF_PER_TRAP is set, call do_per_trap
306#
307sysc_singlestep:
308	ni	__TI_flags+7(%r12),255-(_TIF_SYSCALL | _TIF_PER_TRAP)
309	lgr	%r2,%r11		# pass pointer to pt_regs
310	larl	%r14,sysc_return
311	jg	do_per_trap
312
313#
314# call tracehook_report_syscall_entry/tracehook_report_syscall_exit before
315# and after the system call
316#
317sysc_tracesys:
318	lgr	%r2,%r11		# pass pointer to pt_regs
319	la	%r3,0
320	llgh	%r0,__PT_INT_CODE+2(%r11)
321	stg	%r0,__PT_R2(%r11)
322	brasl	%r14,do_syscall_trace_enter
323	lghi	%r0,NR_syscalls
324	clgr	%r0,%r2
325	jnh	sysc_tracenogo
326	sllg	%r8,%r2,2
327	lgf	%r9,0(%r8,%r10)
328sysc_tracego:
329	lmg	%r3,%r7,__PT_R3(%r11)
330	stg	%r7,STACK_FRAME_OVERHEAD(%r15)
331	lg	%r2,__PT_ORIG_GPR2(%r11)
332	basr	%r14,%r9		# call sys_xxx
333	stg	%r2,__PT_R2(%r11)	# store return value
334sysc_tracenogo:
335	tm	__TI_flags+6(%r12),_TIF_TRACE >> 8
336	jz	sysc_return
337	lgr	%r2,%r11		# pass pointer to pt_regs
338	larl	%r14,sysc_return
339	jg	do_syscall_trace_exit
340
341#
342# a new process exits the kernel with ret_from_fork
343#
344ENTRY(ret_from_fork)
345	la	%r11,STACK_FRAME_OVERHEAD(%r15)
346	lg	%r12,__LC_THREAD_INFO
347	tm	__PT_PSW+1(%r11),0x01	# forking a kernel thread ?
348	jo	0f
349	stg	%r15,__PT_R15(%r11)	# store stack pointer for new kthread
3500:	brasl	%r14,schedule_tail
351	TRACE_IRQS_ON
352	ssm	__LC_SVC_NEW_PSW	# reenable interrupts
353	j	sysc_tracenogo
354
355#
356# kernel_execve function needs to deal with pt_regs that is not
357# at the usual place
358#
359ENTRY(kernel_execve)
360	stmg	%r12,%r15,96(%r15)
361	lgr	%r14,%r15
362	aghi	%r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
363	stg	%r14,__SF_BACKCHAIN(%r15)
364	la	%r12,STACK_FRAME_OVERHEAD(%r15)
365	xc	0(__PT_SIZE,%r12),0(%r12)
366	lgr	%r5,%r12
367	brasl	%r14,do_execve
368	ltgfr	%r2,%r2
369	je	0f
370	aghi	%r15,(STACK_FRAME_OVERHEAD + __PT_SIZE)
371	lmg	%r12,%r15,96(%r15)
372	br	%r14
373	# execve succeeded.
3740:	ssm	__LC_PGM_NEW_PSW	# disable I/O and ext. interrupts
375	lg	%r15,__LC_KERNEL_STACK	# load ksp
376	aghi	%r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
377	la	%r11,STACK_FRAME_OVERHEAD(%r15)
378	mvc	0(__PT_SIZE,%r11),0(%r12)	# copy pt_regs
379	lg	%r12,__LC_THREAD_INFO
380	xc	__SF_BACKCHAIN(8,%r15),__SF_BACKCHAIN(%r15)
381	ssm	__LC_SVC_NEW_PSW	# reenable interrupts
382	brasl	%r14,execve_tail
383	j	sysc_return
384
385/*
386 * Program check handler routine
387 */
388
389ENTRY(pgm_check_handler)
390	stpt	__LC_SYNC_ENTER_TIMER
391	stmg	%r8,%r15,__LC_SAVE_AREA_SYNC
392	lg	%r10,__LC_LAST_BREAK
393	lg	%r12,__LC_THREAD_INFO
394	larl	%r13,system_call
395	lmg	%r8,%r9,__LC_PGM_OLD_PSW
396	HANDLE_SIE_INTERCEPT %r14
397	tmhh	%r8,0x0001		# test problem state bit
398	jnz	1f			# -> fault in user space
399	tmhh	%r8,0x4000		# PER bit set in old PSW ?
400	jnz	0f			# -> enabled, can't be a double fault
401	tm	__LC_PGM_ILC+3,0x80	# check for per exception
402	jnz	pgm_svcper		# -> single stepped svc
4030:	CHECK_STACK STACK_SIZE,__LC_SAVE_AREA_SYNC
404	j	2f
4051:	UPDATE_VTIME %r14,__LC_SYNC_ENTER_TIMER
406	LAST_BREAK %r14
407	lg	%r15,__LC_KERNEL_STACK
4082:	aghi	%r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
409	la	%r11,STACK_FRAME_OVERHEAD(%r15)
410	stmg	%r0,%r7,__PT_R0(%r11)
411	mvc	__PT_R8(64,%r11),__LC_SAVE_AREA_SYNC
412	stmg	%r8,%r9,__PT_PSW(%r11)
413	mvc	__PT_INT_CODE(4,%r11),__LC_PGM_ILC
414	mvc	__PT_INT_PARM_LONG(8,%r11),__LC_TRANS_EXC_CODE
415	stg	%r10,__PT_ARGS(%r11)
416	tm	__LC_PGM_ILC+3,0x80	# check for per exception
417	jz	0f
418	lg	%r1,__TI_task(%r12)
419	tmhh	%r8,0x0001		# kernel per event ?
420	jz	pgm_kprobe
421	oi	__TI_flags+7(%r12),_TIF_PER_TRAP
422	mvc	__THREAD_per_address(8,%r1),__LC_PER_ADDRESS
423	mvc	__THREAD_per_cause(2,%r1),__LC_PER_CAUSE
424	mvc	__THREAD_per_paid(1,%r1),__LC_PER_PAID
4250:	REENABLE_IRQS
426	xc	__SF_BACKCHAIN(8,%r15),__SF_BACKCHAIN(%r15)
427	larl	%r1,pgm_check_table
428	llgh	%r10,__PT_INT_CODE+2(%r11)
429	nill	%r10,0x007f
430	sll	%r10,3
431	je	sysc_return
432	lg	%r1,0(%r10,%r1)		# load address of handler routine
433	lgr	%r2,%r11		# pass pointer to pt_regs
434	basr	%r14,%r1		# branch to interrupt-handler
435	j	sysc_return
436
437#
438# PER event in supervisor state, must be kprobes
439#
440pgm_kprobe:
441	REENABLE_IRQS
442	xc	__SF_BACKCHAIN(8,%r15),__SF_BACKCHAIN(%r15)
443	lgr	%r2,%r11		# pass pointer to pt_regs
444	brasl	%r14,do_per_trap
445	j	sysc_return
446
447#
448# single stepped system call
449#
450pgm_svcper:
451	oi	__TI_flags+7(%r12),_TIF_PER_TRAP
452	mvc	__LC_RETURN_PSW(8),__LC_SVC_NEW_PSW
453	larl	%r14,sysc_per
454	stg	%r14,__LC_RETURN_PSW+8
455	lpswe	__LC_RETURN_PSW		# branch to sysc_per and enable irqs
456
457/*
458 * IO interrupt handler routine
459 */
460ENTRY(io_int_handler)
461	stck	__LC_INT_CLOCK
462	stpt	__LC_ASYNC_ENTER_TIMER
463	stmg	%r8,%r15,__LC_SAVE_AREA_ASYNC
464	lg	%r10,__LC_LAST_BREAK
465	lg	%r12,__LC_THREAD_INFO
466	larl	%r13,system_call
467	lmg	%r8,%r9,__LC_IO_OLD_PSW
468	HANDLE_SIE_INTERCEPT %r14
469	SWITCH_ASYNC __LC_SAVE_AREA_ASYNC,__LC_ASYNC_STACK,STACK_SHIFT
470	tmhh	%r8,0x0001		# interrupting from user?
471	jz	io_skip
472	UPDATE_VTIME %r14,__LC_ASYNC_ENTER_TIMER
473	LAST_BREAK %r14
474io_skip:
475	stmg	%r0,%r7,__PT_R0(%r11)
476	mvc	__PT_R8(64,%r11),__LC_SAVE_AREA_ASYNC
477	stmg	%r8,%r9,__PT_PSW(%r11)
478	TRACE_IRQS_OFF
479	xc	__SF_BACKCHAIN(8,%r15),__SF_BACKCHAIN(%r15)
480	lgr	%r2,%r11		# pass pointer to pt_regs
481	brasl	%r14,do_IRQ
482io_return:
483	LOCKDEP_SYS_EXIT
484	TRACE_IRQS_ON
485io_tif:
486	tm	__TI_flags+7(%r12),_TIF_WORK_INT
487	jnz	io_work 		# there is work to do (signals etc.)
488io_restore:
489	lg	%r14,__LC_VDSO_PER_CPU
490	lmg	%r0,%r10,__PT_R0(%r11)
491	mvc	__LC_RETURN_PSW(16),__PT_PSW(%r11)
492	stpt	__LC_EXIT_TIMER
493	mvc	__VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
494	lmg	%r11,%r15,__PT_R11(%r11)
495	lpswe	__LC_RETURN_PSW
496io_done:
497
498#
499# There is work todo, find out in which context we have been interrupted:
500# 1) if we return to user space we can do all _TIF_WORK_INT work
501# 2) if we return to kernel code and kvm is enabled check if we need to
502#    modify the psw to leave SIE
503# 3) if we return to kernel code and preemptive scheduling is enabled check
504#    the preemption counter and if it is zero call preempt_schedule_irq
505# Before any work can be done, a switch to the kernel stack is required.
506#
507io_work:
508	tm	__PT_PSW+1(%r11),0x01	# returning to user ?
509	jo	io_work_user		# yes -> do resched & signal
510#ifdef CONFIG_PREEMPT
511	# check for preemptive scheduling
512	icm	%r0,15,__TI_precount(%r12)
513	jnz	io_restore		# preemption is disabled
514	tm	__TI_flags+7(%r12),_TIF_NEED_RESCHED
515	jno	io_restore
516	# switch to kernel stack
517	lg	%r1,__PT_R15(%r11)
518	aghi	%r1,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
519	mvc	STACK_FRAME_OVERHEAD(__PT_SIZE,%r1),0(%r11)
520	xc	__SF_BACKCHAIN(8,%r1),__SF_BACKCHAIN(%r1)
521	la	%r11,STACK_FRAME_OVERHEAD(%r1)
522	lgr	%r15,%r1
523	# TRACE_IRQS_ON already done at io_return, call
524	# TRACE_IRQS_OFF to keep things symmetrical
525	TRACE_IRQS_OFF
526	brasl	%r14,preempt_schedule_irq
527	j	io_return
528#else
529	j	io_restore
530#endif
531
532#
533# Need to do work before returning to userspace, switch to kernel stack
534#
535io_work_user:
536	lg	%r1,__LC_KERNEL_STACK
537	aghi	%r1,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
538	mvc	STACK_FRAME_OVERHEAD(__PT_SIZE,%r1),0(%r11)
539	xc	__SF_BACKCHAIN(8,%r1),__SF_BACKCHAIN(%r1)
540	la	%r11,STACK_FRAME_OVERHEAD(%r1)
541	lgr	%r15,%r1
542
543#
544# One of the work bits is on. Find out which one.
545# Checked are: _TIF_SIGPENDING, _TIF_NOTIFY_RESUME, _TIF_NEED_RESCHED
546#	       and _TIF_MCCK_PENDING
547#
548io_work_tif:
549	tm	__TI_flags+7(%r12),_TIF_MCCK_PENDING
550	jo	io_mcck_pending
551	tm	__TI_flags+7(%r12),_TIF_NEED_RESCHED
552	jo	io_reschedule
553	tm	__TI_flags+7(%r12),_TIF_SIGPENDING
554	jo	io_sigpending
555	tm	__TI_flags+7(%r12),_TIF_NOTIFY_RESUME
556	jo	io_notify_resume
557	j	io_return		# beware of critical section cleanup
558
559#
560# _TIF_MCCK_PENDING is set, call handler
561#
562io_mcck_pending:
563	# TRACE_IRQS_ON already done at io_return
564	brasl	%r14,s390_handle_mcck	# TIF bit will be cleared by handler
565	TRACE_IRQS_OFF
566	j	io_return
567
568#
569# _TIF_NEED_RESCHED is set, call schedule
570#
571io_reschedule:
572	# TRACE_IRQS_ON already done at io_return
573	ssm	__LC_SVC_NEW_PSW	# reenable interrupts
574	brasl	%r14,schedule		# call scheduler
575	ssm	__LC_PGM_NEW_PSW	# disable I/O and ext. interrupts
576	TRACE_IRQS_OFF
577	j	io_return
578
579#
580# _TIF_SIGPENDING or is set, call do_signal
581#
582io_sigpending:
583	# TRACE_IRQS_ON already done at io_return
584	ssm	__LC_SVC_NEW_PSW	# reenable interrupts
585	lgr	%r2,%r11		# pass pointer to pt_regs
586	brasl	%r14,do_signal
587	ssm	__LC_PGM_NEW_PSW	# disable I/O and ext. interrupts
588	TRACE_IRQS_OFF
589	j	io_return
590
591#
592# _TIF_NOTIFY_RESUME or is set, call do_notify_resume
593#
594io_notify_resume:
595	# TRACE_IRQS_ON already done at io_return
596	ssm	__LC_SVC_NEW_PSW	# reenable interrupts
597	lgr	%r2,%r11		# pass pointer to pt_regs
598	brasl	%r14,do_notify_resume
599	ssm	__LC_PGM_NEW_PSW	# disable I/O and ext. interrupts
600	TRACE_IRQS_OFF
601	j	io_return
602
603/*
604 * External interrupt handler routine
605 */
606ENTRY(ext_int_handler)
607	stck	__LC_INT_CLOCK
608	stpt	__LC_ASYNC_ENTER_TIMER
609	stmg	%r8,%r15,__LC_SAVE_AREA_ASYNC
610	lg	%r10,__LC_LAST_BREAK
611	lg	%r12,__LC_THREAD_INFO
612	larl	%r13,system_call
613	lmg	%r8,%r9,__LC_EXT_OLD_PSW
614	HANDLE_SIE_INTERCEPT %r14
615	SWITCH_ASYNC __LC_SAVE_AREA_ASYNC,__LC_ASYNC_STACK,STACK_SHIFT
616	tmhh	%r8,0x0001		# interrupting from user ?
617	jz	ext_skip
618	UPDATE_VTIME %r14,__LC_ASYNC_ENTER_TIMER
619	LAST_BREAK %r14
620ext_skip:
621	stmg	%r0,%r7,__PT_R0(%r11)
622	mvc	__PT_R8(64,%r11),__LC_SAVE_AREA_ASYNC
623	stmg	%r8,%r9,__PT_PSW(%r11)
624	TRACE_IRQS_OFF
625	lghi	%r1,4096
626	lgr	%r2,%r11		# pass pointer to pt_regs
627	llgf	%r3,__LC_EXT_CPU_ADDR	# get cpu address + interruption code
628	llgf	%r4,__LC_EXT_PARAMS	# get external parameter
629	lg	%r5,__LC_EXT_PARAMS2-4096(%r1)	# get 64 bit external parameter
630	brasl	%r14,do_extint
631	j	io_return
632
633/*
634 * Load idle PSW. The second "half" of this function is in cleanup_idle.
635 */
636ENTRY(psw_idle)
637	stg	%r4,__SF_EMPTY(%r15)
638	larl	%r1,psw_idle_lpsw+4
639	stg	%r1,__SF_EMPTY+8(%r15)
640	larl	%r1,.Lvtimer_max
641	stck	__IDLE_ENTER(%r2)
642	ltr	%r5,%r5
643	stpt	__VQ_IDLE_ENTER(%r3)
644	jz	psw_idle_lpsw
645	spt	0(%r1)
646psw_idle_lpsw:
647	lpswe	__SF_EMPTY(%r15)
648	br	%r14
649psw_idle_end:
650
651__critical_end:
652
653/*
654 * Machine check handler routines
655 */
656ENTRY(mcck_int_handler)
657	stck	__LC_MCCK_CLOCK
658	la	%r1,4095		# revalidate r1
659	spt	__LC_CPU_TIMER_SAVE_AREA-4095(%r1)	# revalidate cpu timer
660	lmg	%r0,%r15,__LC_GPREGS_SAVE_AREA-4095(%r1)# revalidate gprs
661	lg	%r10,__LC_LAST_BREAK
662	lg	%r12,__LC_THREAD_INFO
663	larl	%r13,system_call
664	lmg	%r8,%r9,__LC_MCK_OLD_PSW
665	HANDLE_SIE_INTERCEPT %r14
666	tm	__LC_MCCK_CODE,0x80	# system damage?
667	jo	mcck_panic		# yes -> rest of mcck code invalid
668	lghi	%r14,__LC_CPU_TIMER_SAVE_AREA
669	mvc	__LC_MCCK_ENTER_TIMER(8),0(%r14)
670	tm	__LC_MCCK_CODE+5,0x02	# stored cpu timer value valid?
671	jo	3f
672	la	%r14,__LC_SYNC_ENTER_TIMER
673	clc	0(8,%r14),__LC_ASYNC_ENTER_TIMER
674	jl	0f
675	la	%r14,__LC_ASYNC_ENTER_TIMER
6760:	clc	0(8,%r14),__LC_EXIT_TIMER
677	jl	1f
678	la	%r14,__LC_EXIT_TIMER
6791:	clc	0(8,%r14),__LC_LAST_UPDATE_TIMER
680	jl	2f
681	la	%r14,__LC_LAST_UPDATE_TIMER
6822:	spt	0(%r14)
683	mvc	__LC_MCCK_ENTER_TIMER(8),0(%r14)
6843:	tm	__LC_MCCK_CODE+2,0x09	# mwp + ia of old psw valid?
685	jno	mcck_panic		# no -> skip cleanup critical
686	SWITCH_ASYNC __LC_GPREGS_SAVE_AREA+64,__LC_PANIC_STACK,PAGE_SHIFT
687	tm	%r8,0x0001		# interrupting from user ?
688	jz	mcck_skip
689	UPDATE_VTIME %r14,__LC_MCCK_ENTER_TIMER
690	LAST_BREAK %r14
691mcck_skip:
692	lghi	%r14,__LC_GPREGS_SAVE_AREA+64
693	stmg	%r0,%r7,__PT_R0(%r11)
694	mvc	__PT_R8(64,%r11),0(%r14)
695	stmg	%r8,%r9,__PT_PSW(%r11)
696	xc	__SF_BACKCHAIN(8,%r15),__SF_BACKCHAIN(%r15)
697	lgr	%r2,%r11		# pass pointer to pt_regs
698	brasl	%r14,s390_do_machine_check
699	tm	__PT_PSW+1(%r11),0x01	# returning to user ?
700	jno	mcck_return
701	lg	%r1,__LC_KERNEL_STACK	# switch to kernel stack
702	aghi	%r1,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
703	mvc	STACK_FRAME_OVERHEAD(__PT_SIZE,%r1),0(%r11)
704	xc	__SF_BACKCHAIN(8,%r1),__SF_BACKCHAIN(%r1)
705	la	%r11,STACK_FRAME_OVERHEAD(%r1)
706	lgr	%r15,%r1
707	ssm	__LC_PGM_NEW_PSW	# turn dat on, keep irqs off
708	tm	__TI_flags+7(%r12),_TIF_MCCK_PENDING
709	jno	mcck_return
710	TRACE_IRQS_OFF
711	brasl	%r14,s390_handle_mcck
712	TRACE_IRQS_ON
713mcck_return:
714	lg	%r14,__LC_VDSO_PER_CPU
715	lmg	%r0,%r10,__PT_R0(%r11)
716	mvc	__LC_RETURN_MCCK_PSW(16),__PT_PSW(%r11) # move return PSW
717	tm	__LC_RETURN_MCCK_PSW+1,0x01 # returning to user ?
718	jno	0f
719	stpt	__LC_EXIT_TIMER
720	mvc	__VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
7210:	lmg	%r11,%r15,__PT_R11(%r11)
722	lpswe	__LC_RETURN_MCCK_PSW
723
724mcck_panic:
725	lg	%r14,__LC_PANIC_STACK
726	slgr	%r14,%r15
727	srag	%r14,%r14,PAGE_SHIFT
728	jz	0f
729	lg	%r15,__LC_PANIC_STACK
7300:	aghi	%r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
731	j	mcck_skip
732
733#
734# PSW restart interrupt handler
735#
736ENTRY(restart_int_handler)
737	stg	%r15,__LC_SAVE_AREA_RESTART
738	lg	%r15,__LC_RESTART_STACK
739	aghi	%r15,-__PT_SIZE			# create pt_regs on stack
740	xc	0(__PT_SIZE,%r15),0(%r15)
741	stmg	%r0,%r14,__PT_R0(%r15)
742	mvc	__PT_R15(8,%r15),__LC_SAVE_AREA_RESTART
743	mvc	__PT_PSW(16,%r15),__LC_RST_OLD_PSW # store restart old psw
744	aghi	%r15,-STACK_FRAME_OVERHEAD	# create stack frame on stack
745	xc	0(STACK_FRAME_OVERHEAD,%r15),0(%r15)
746	lmg	%r1,%r3,__LC_RESTART_FN		# load fn, parm & source cpu
747	ltgr	%r3,%r3				# test source cpu address
748	jm	1f				# negative -> skip source stop
7490:	sigp	%r4,%r3,1			# sigp sense to source cpu
750	brc	10,0b				# wait for status stored
7511:	basr	%r14,%r1			# call function
752	stap	__SF_EMPTY(%r15)		# store cpu address
753	llgh	%r3,__SF_EMPTY(%r15)
7542:	sigp	%r4,%r3,5			# sigp stop to current cpu
755	brc	2,2b
7563:	j	3b
757
758	.section .kprobes.text, "ax"
759
760#ifdef CONFIG_CHECK_STACK
761/*
762 * The synchronous or the asynchronous stack overflowed. We are dead.
763 * No need to properly save the registers, we are going to panic anyway.
764 * Setup a pt_regs so that show_trace can provide a good call trace.
765 */
766stack_overflow:
767	lg	%r11,__LC_PANIC_STACK	# change to panic stack
768	aghi	%r11,-__PT_SIZE		# create pt_regs
769	stmg	%r0,%r7,__PT_R0(%r11)
770	stmg	%r8,%r9,__PT_PSW(%r11)
771	mvc	__PT_R8(64,%r11),0(%r14)
772	stg	%r10,__PT_ORIG_GPR2(%r11) # store last break to orig_gpr2
773	lgr	%r15,%r11
774	aghi	%r15,-STACK_FRAME_OVERHEAD
775	xc	__SF_BACKCHAIN(8,%r15),__SF_BACKCHAIN(%r15)
776	lgr	%r2,%r11		# pass pointer to pt_regs
777	jg	kernel_stack_overflow
778#endif
779
780	.align	8
781cleanup_table:
782	.quad	system_call
783	.quad	sysc_do_svc
784	.quad	sysc_tif
785	.quad	sysc_restore
786	.quad	sysc_done
787	.quad	io_tif
788	.quad	io_restore
789	.quad	io_done
790	.quad	psw_idle
791	.quad	psw_idle_end
792
793cleanup_critical:
794	clg	%r9,BASED(cleanup_table)	# system_call
795	jl	0f
796	clg	%r9,BASED(cleanup_table+8)	# sysc_do_svc
797	jl	cleanup_system_call
798	clg	%r9,BASED(cleanup_table+16)	# sysc_tif
799	jl	0f
800	clg	%r9,BASED(cleanup_table+24)	# sysc_restore
801	jl	cleanup_sysc_tif
802	clg	%r9,BASED(cleanup_table+32)	# sysc_done
803	jl	cleanup_sysc_restore
804	clg	%r9,BASED(cleanup_table+40)	# io_tif
805	jl	0f
806	clg	%r9,BASED(cleanup_table+48)	# io_restore
807	jl	cleanup_io_tif
808	clg	%r9,BASED(cleanup_table+56)	# io_done
809	jl	cleanup_io_restore
810	clg	%r9,BASED(cleanup_table+64)	# psw_idle
811	jl	0f
812	clg	%r9,BASED(cleanup_table+72)	# psw_idle_end
813	jl	cleanup_idle
8140:	br	%r14
815
816
817cleanup_system_call:
818	# check if stpt has been executed
819	clg	%r9,BASED(cleanup_system_call_insn)
820	jh	0f
821	mvc	__LC_SYNC_ENTER_TIMER(8),__LC_ASYNC_ENTER_TIMER
822	cghi	%r11,__LC_SAVE_AREA_ASYNC
823	je	0f
824	mvc	__LC_SYNC_ENTER_TIMER(8),__LC_MCCK_ENTER_TIMER
8250:	# check if stmg has been executed
826	clg	%r9,BASED(cleanup_system_call_insn+8)
827	jh	0f
828	mvc	__LC_SAVE_AREA_SYNC(64),0(%r11)
8290:	# check if base register setup + TIF bit load has been done
830	clg	%r9,BASED(cleanup_system_call_insn+16)
831	jhe	0f
832	# set up saved registers r10 and r12
833	stg	%r10,16(%r11)		# r10 last break
834	stg	%r12,32(%r11)		# r12 thread-info pointer
8350:	# check if the user time update has been done
836	clg	%r9,BASED(cleanup_system_call_insn+24)
837	jh	0f
838	lg	%r15,__LC_EXIT_TIMER
839	slg	%r15,__LC_SYNC_ENTER_TIMER
840	alg	%r15,__LC_USER_TIMER
841	stg	%r15,__LC_USER_TIMER
8420:	# check if the system time update has been done
843	clg	%r9,BASED(cleanup_system_call_insn+32)
844	jh	0f
845	lg	%r15,__LC_LAST_UPDATE_TIMER
846	slg	%r15,__LC_EXIT_TIMER
847	alg	%r15,__LC_SYSTEM_TIMER
848	stg	%r15,__LC_SYSTEM_TIMER
8490:	# update accounting time stamp
850	mvc	__LC_LAST_UPDATE_TIMER(8),__LC_SYNC_ENTER_TIMER
851	# do LAST_BREAK
852	lg	%r9,16(%r11)
853	srag	%r9,%r9,23
854	jz	0f
855	mvc	__TI_last_break(8,%r12),16(%r11)
8560:	# set up saved register r11
857	lg	%r15,__LC_KERNEL_STACK
858	aghi	%r15,-__PT_SIZE
859	stg	%r15,24(%r11)		# r11 pt_regs pointer
860	# fill pt_regs
861	mvc	__PT_R8(64,%r15),__LC_SAVE_AREA_SYNC
862	stmg	%r0,%r7,__PT_R0(%r15)
863	mvc	__PT_PSW(16,%r15),__LC_SVC_OLD_PSW
864	mvc	__PT_INT_CODE(4,%r15),__LC_SVC_ILC
865	# setup saved register r15
866	aghi	%r15,-STACK_FRAME_OVERHEAD
867	stg	%r15,56(%r11)		# r15 stack pointer
868	# set new psw address and exit
869	larl	%r9,sysc_do_svc
870	br	%r14
871cleanup_system_call_insn:
872	.quad	system_call
873	.quad	sysc_stmg
874	.quad	sysc_per
875	.quad	sysc_vtime+18
876	.quad	sysc_vtime+42
877
878cleanup_sysc_tif:
879	larl	%r9,sysc_tif
880	br	%r14
881
882cleanup_sysc_restore:
883	clg	%r9,BASED(cleanup_sysc_restore_insn)
884	je	0f
885	lg	%r9,24(%r11)		# get saved pointer to pt_regs
886	mvc	__LC_RETURN_PSW(16),__PT_PSW(%r9)
887	mvc	0(64,%r11),__PT_R8(%r9)
888	lmg	%r0,%r7,__PT_R0(%r9)
8890:	lmg	%r8,%r9,__LC_RETURN_PSW
890	br	%r14
891cleanup_sysc_restore_insn:
892	.quad	sysc_done - 4
893
894cleanup_io_tif:
895	larl	%r9,io_tif
896	br	%r14
897
898cleanup_io_restore:
899	clg	%r9,BASED(cleanup_io_restore_insn)
900	je	0f
901	lg	%r9,24(%r11)		# get saved r11 pointer to pt_regs
902	mvc	__LC_RETURN_PSW(16),__PT_PSW(%r9)
903	mvc	0(64,%r11),__PT_R8(%r9)
904	lmg	%r0,%r7,__PT_R0(%r9)
9050:	lmg	%r8,%r9,__LC_RETURN_PSW
906	br	%r14
907cleanup_io_restore_insn:
908	.quad	io_done - 4
909
910cleanup_idle:
911	# copy interrupt clock & cpu timer
912	mvc	__IDLE_EXIT(8,%r2),__LC_INT_CLOCK
913	mvc	__VQ_IDLE_EXIT(8,%r3),__LC_ASYNC_ENTER_TIMER
914	cghi	%r11,__LC_SAVE_AREA_ASYNC
915	je	0f
916	mvc	__IDLE_EXIT(8,%r2),__LC_MCCK_CLOCK
917	mvc	__VQ_IDLE_EXIT(8,%r3),__LC_MCCK_ENTER_TIMER
9180:	# check if stck & stpt have been executed
919	clg	%r9,BASED(cleanup_idle_insn)
920	jhe	1f
921	mvc	__IDLE_ENTER(8,%r2),__IDLE_EXIT(%r2)
922	mvc	__VQ_IDLE_ENTER(8,%r3),__VQ_IDLE_EXIT(%r3)
923	j	2f
9241:	# check if the cpu timer has been reprogrammed
925	ltr	%r5,%r5
926	jz	2f
927	spt	__VQ_IDLE_ENTER(%r3)
9282:	# account system time going idle
929	lg	%r9,__LC_STEAL_TIMER
930	alg	%r9,__IDLE_ENTER(%r2)
931	slg	%r9,__LC_LAST_UPDATE_CLOCK
932	stg	%r9,__LC_STEAL_TIMER
933	mvc	__LC_LAST_UPDATE_CLOCK(8),__IDLE_EXIT(%r2)
934	lg	%r9,__LC_SYSTEM_TIMER
935	alg	%r9,__LC_LAST_UPDATE_TIMER
936	slg	%r9,__VQ_IDLE_ENTER(%r3)
937	stg	%r9,__LC_SYSTEM_TIMER
938	mvc	__LC_LAST_UPDATE_TIMER(8),__VQ_IDLE_EXIT(%r3)
939	# prepare return psw
940	nihh	%r8,0xfffd		# clear wait state bit
941	lg	%r9,48(%r11)		# return from psw_idle
942	br	%r14
943cleanup_idle_insn:
944	.quad	psw_idle_lpsw
945
946/*
947 * Integer constants
948 */
949	.align	8
950.Lcritical_start:
951	.quad	__critical_start
952.Lcritical_length:
953	.quad	__critical_end - __critical_start
954.Lvtimer_max:
955	.quad	0x7fffffffffffffff
956
957
958#if defined(CONFIG_KVM) || defined(CONFIG_KVM_MODULE)
959/*
960 * sie64a calling convention:
961 * %r2 pointer to sie control block
962 * %r3 guest register save area
963 */
964ENTRY(sie64a)
965	stmg	%r6,%r14,__SF_GPRS(%r15)	# save kernel registers
966	stg	%r2,__SF_EMPTY(%r15)		# save control block pointer
967	stg	%r3,__SF_EMPTY+8(%r15)		# save guest register save area
968	xc	__SF_EMPTY+16(8,%r15),__SF_EMPTY+16(%r15) # host id == 0
969	lmg	%r0,%r13,0(%r3)			# load guest gprs 0-13
970	lg	%r14,__LC_THREAD_INFO		# pointer thread_info struct
971	oi	__TI_flags+6(%r14),_TIF_SIE>>8
972sie_loop:
973	lg	%r14,__LC_THREAD_INFO		# pointer thread_info struct
974	tm	__TI_flags+7(%r14),_TIF_EXIT_SIE
975	jnz	sie_exit
976	lg	%r14,__LC_GMAP			# get gmap pointer
977	ltgr	%r14,%r14
978	jz	sie_gmap
979	lctlg	%c1,%c1,__GMAP_ASCE(%r14)	# load primary asce
980sie_gmap:
981	lg	%r14,__SF_EMPTY(%r15)		# get control block pointer
982	SPP	__SF_EMPTY(%r15)		# set guest id
983	sie	0(%r14)
984sie_done:
985	SPP	__SF_EMPTY+16(%r15)		# set host id
986	lg	%r14,__LC_THREAD_INFO		# pointer thread_info struct
987sie_exit:
988	lctlg	%c1,%c1,__LC_USER_ASCE		# load primary asce
989	ni	__TI_flags+6(%r14),255-(_TIF_SIE>>8)
990	lg	%r14,__SF_EMPTY+8(%r15)		# load guest register save area
991	stmg	%r0,%r13,0(%r14)		# save guest gprs 0-13
992	lmg	%r6,%r14,__SF_GPRS(%r15)	# restore kernel registers
993	lghi	%r2,0
994	br	%r14
995sie_fault:
996	lctlg	%c1,%c1,__LC_USER_ASCE		# load primary asce
997	lg	%r14,__LC_THREAD_INFO		# pointer thread_info struct
998	ni	__TI_flags+6(%r14),255-(_TIF_SIE>>8)
999	lg	%r14,__SF_EMPTY+8(%r15)		# load guest register save area
1000	stmg	%r0,%r13,0(%r14)		# save guest gprs 0-13
1001	lmg	%r6,%r14,__SF_GPRS(%r15)	# restore kernel registers
1002	lghi	%r2,-EFAULT
1003	br	%r14
1004
1005	.align	8
1006.Lsie_loop:
1007	.quad	sie_loop
1008.Lsie_length:
1009	.quad	sie_done - sie_loop
1010.Lhost_id:
1011	.quad	0
1012
1013	.section __ex_table,"a"
1014	.quad	sie_loop,sie_fault
1015	.previous
1016#endif
1017
1018		.section .rodata, "a"
1019#define SYSCALL(esa,esame,emu)	.long esame
1020	.globl	sys_call_table
1021sys_call_table:
1022#include "syscalls.S"
1023#undef SYSCALL
1024
1025#ifdef CONFIG_COMPAT
1026
1027#define SYSCALL(esa,esame,emu)	.long emu
1028sys_call_table_emu:
1029#include "syscalls.S"
1030#undef SYSCALL
1031#endif
1032