xref: /openbmc/linux/arch/microblaze/kernel/entry.S (revision ce3266c047389443d5f433d605c769e878cbe46e)
1ca54502bSMichal Simek/*
2ca54502bSMichal Simek * Low-level system-call handling, trap handlers and context-switching
3ca54502bSMichal Simek *
4ca54502bSMichal Simek * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5ca54502bSMichal Simek * Copyright (C) 2008-2009 PetaLogix
6ca54502bSMichal Simek * Copyright (C) 2003		John Williams <jwilliams@itee.uq.edu.au>
7ca54502bSMichal Simek * Copyright (C) 2001,2002	NEC Corporation
8ca54502bSMichal Simek * Copyright (C) 2001,2002	Miles Bader <miles@gnu.org>
9ca54502bSMichal Simek *
10ca54502bSMichal Simek * This file is subject to the terms and conditions of the GNU General
11ca54502bSMichal Simek * Public License. See the file COPYING in the main directory of this
12ca54502bSMichal Simek * archive for more details.
13ca54502bSMichal Simek *
14ca54502bSMichal Simek * Written by Miles Bader <miles@gnu.org>
15ca54502bSMichal Simek * Heavily modified by John Williams for Microblaze
16ca54502bSMichal Simek */
17ca54502bSMichal Simek
18ca54502bSMichal Simek#include <linux/sys.h>
19ca54502bSMichal Simek#include <linux/linkage.h>
20ca54502bSMichal Simek
21ca54502bSMichal Simek#include <asm/entry.h>
22ca54502bSMichal Simek#include <asm/current.h>
23ca54502bSMichal Simek#include <asm/processor.h>
24ca54502bSMichal Simek#include <asm/exceptions.h>
25ca54502bSMichal Simek#include <asm/asm-offsets.h>
26ca54502bSMichal Simek#include <asm/thread_info.h>
27ca54502bSMichal Simek
28ca54502bSMichal Simek#include <asm/page.h>
29ca54502bSMichal Simek#include <asm/unistd.h>
30ca54502bSMichal Simek
31ca54502bSMichal Simek#include <linux/errno.h>
32ca54502bSMichal Simek#include <asm/signal.h>
33ca54502bSMichal Simek
3411d51360SMichal Simek#undef DEBUG
3511d51360SMichal Simek
36ca54502bSMichal Simek/* The size of a state save frame. */
37ca54502bSMichal Simek#define STATE_SAVE_SIZE		(PT_SIZE + STATE_SAVE_ARG_SPACE)
38ca54502bSMichal Simek
39ca54502bSMichal Simek/* The offset of the struct pt_regs in a `state save frame' on the stack. */
40ca54502bSMichal Simek#define PTO	STATE_SAVE_ARG_SPACE /* 24 the space for args */
41ca54502bSMichal Simek
42ca54502bSMichal Simek#define C_ENTRY(name)	.globl name; .align 4; name
43ca54502bSMichal Simek
44ca54502bSMichal Simek/*
45ca54502bSMichal Simek * Various ways of setting and clearing BIP in flags reg.
46ca54502bSMichal Simek * This is mucky, but necessary using microblaze version that
47ca54502bSMichal Simek * allows msr ops to write to BIP
48ca54502bSMichal Simek */
49ca54502bSMichal Simek#if CONFIG_XILINX_MICROBLAZE0_USE_MSR_INSTR
50ca54502bSMichal Simek	.macro	clear_bip
51ca54502bSMichal Simek	msrclr	r11, MSR_BIP
52ca54502bSMichal Simek	nop
53ca54502bSMichal Simek	.endm
54ca54502bSMichal Simek
55ca54502bSMichal Simek	.macro	set_bip
56ca54502bSMichal Simek	msrset	r11, MSR_BIP
57ca54502bSMichal Simek	nop
58ca54502bSMichal Simek	.endm
59ca54502bSMichal Simek
60ca54502bSMichal Simek	.macro	clear_eip
61ca54502bSMichal Simek	msrclr	r11, MSR_EIP
62ca54502bSMichal Simek	nop
63ca54502bSMichal Simek	.endm
64ca54502bSMichal Simek
65ca54502bSMichal Simek	.macro	set_ee
66ca54502bSMichal Simek	msrset	r11, MSR_EE
67ca54502bSMichal Simek	nop
68ca54502bSMichal Simek	.endm
69ca54502bSMichal Simek
70ca54502bSMichal Simek	.macro	disable_irq
71ca54502bSMichal Simek	msrclr	r11, MSR_IE
72ca54502bSMichal Simek	nop
73ca54502bSMichal Simek	.endm
74ca54502bSMichal Simek
75ca54502bSMichal Simek	.macro	enable_irq
76ca54502bSMichal Simek	msrset	r11, MSR_IE
77ca54502bSMichal Simek	nop
78ca54502bSMichal Simek	.endm
79ca54502bSMichal Simek
80ca54502bSMichal Simek	.macro	set_ums
81ca54502bSMichal Simek	msrset	r11, MSR_UMS
82ca54502bSMichal Simek	nop
83ca54502bSMichal Simek	msrclr	r11, MSR_VMS
84ca54502bSMichal Simek	nop
85ca54502bSMichal Simek	.endm
86ca54502bSMichal Simek
87ca54502bSMichal Simek	.macro	set_vms
88ca54502bSMichal Simek	msrclr	r11, MSR_UMS
89ca54502bSMichal Simek	nop
90ca54502bSMichal Simek	msrset	r11, MSR_VMS
91ca54502bSMichal Simek	nop
92ca54502bSMichal Simek	.endm
93ca54502bSMichal Simek
94ca54502bSMichal Simek	.macro	clear_vms_ums
95ca54502bSMichal Simek	msrclr	r11, MSR_VMS
96ca54502bSMichal Simek	nop
97ca54502bSMichal Simek	msrclr	r11, MSR_UMS
98ca54502bSMichal Simek	nop
99ca54502bSMichal Simek	.endm
100ca54502bSMichal Simek#else
101ca54502bSMichal Simek	.macro	clear_bip
102ca54502bSMichal Simek	mfs	r11, rmsr
103ca54502bSMichal Simek	nop
104ca54502bSMichal Simek	andi	r11, r11, ~MSR_BIP
105ca54502bSMichal Simek	mts	rmsr, r11
106ca54502bSMichal Simek	nop
107ca54502bSMichal Simek	.endm
108ca54502bSMichal Simek
109ca54502bSMichal Simek	.macro	set_bip
110ca54502bSMichal Simek	mfs	r11, rmsr
111ca54502bSMichal Simek	nop
112ca54502bSMichal Simek	ori	r11, r11, MSR_BIP
113ca54502bSMichal Simek	mts	rmsr, r11
114ca54502bSMichal Simek	nop
115ca54502bSMichal Simek	.endm
116ca54502bSMichal Simek
117ca54502bSMichal Simek	.macro	clear_eip
118ca54502bSMichal Simek	mfs	r11, rmsr
119ca54502bSMichal Simek	nop
120ca54502bSMichal Simek	andi	r11, r11, ~MSR_EIP
121ca54502bSMichal Simek	mts	rmsr, r11
122ca54502bSMichal Simek	nop
123ca54502bSMichal Simek	.endm
124ca54502bSMichal Simek
125ca54502bSMichal Simek	.macro	set_ee
126ca54502bSMichal Simek	mfs	r11, rmsr
127ca54502bSMichal Simek	nop
128ca54502bSMichal Simek	ori	r11, r11, MSR_EE
129ca54502bSMichal Simek	mts	rmsr, r11
130ca54502bSMichal Simek	nop
131ca54502bSMichal Simek	.endm
132ca54502bSMichal Simek
133ca54502bSMichal Simek	.macro	disable_irq
134ca54502bSMichal Simek	mfs	r11, rmsr
135ca54502bSMichal Simek	nop
136ca54502bSMichal Simek	andi	r11, r11, ~MSR_IE
137ca54502bSMichal Simek	mts	rmsr, r11
138ca54502bSMichal Simek	nop
139ca54502bSMichal Simek	.endm
140ca54502bSMichal Simek
141ca54502bSMichal Simek	.macro	enable_irq
142ca54502bSMichal Simek	mfs	r11, rmsr
143ca54502bSMichal Simek	nop
144ca54502bSMichal Simek	ori	r11, r11, MSR_IE
145ca54502bSMichal Simek	mts	rmsr, r11
146ca54502bSMichal Simek	nop
147ca54502bSMichal Simek	.endm
148ca54502bSMichal Simek
149ca54502bSMichal Simek	.macro set_ums
150ca54502bSMichal Simek	mfs	r11, rmsr
151ca54502bSMichal Simek	nop
152ca54502bSMichal Simek	ori	r11, r11, MSR_VMS
153ca54502bSMichal Simek	andni	r11, r11, MSR_UMS
154ca54502bSMichal Simek	mts	rmsr, r11
155ca54502bSMichal Simek	nop
156ca54502bSMichal Simek	.endm
157ca54502bSMichal Simek
158ca54502bSMichal Simek	.macro	set_vms
159ca54502bSMichal Simek	mfs	r11, rmsr
160ca54502bSMichal Simek	nop
161ca54502bSMichal Simek	ori	r11, r11, MSR_VMS
162ca54502bSMichal Simek	andni	r11, r11, MSR_UMS
163ca54502bSMichal Simek	mts	rmsr, r11
164ca54502bSMichal Simek	nop
165ca54502bSMichal Simek	.endm
166ca54502bSMichal Simek
167ca54502bSMichal Simek	.macro	clear_vms_ums
168ca54502bSMichal Simek	mfs	r11, rmsr
169ca54502bSMichal Simek	nop
170ca54502bSMichal Simek	andni	r11, r11, (MSR_VMS|MSR_UMS)
171ca54502bSMichal Simek	mts	rmsr,r11
172ca54502bSMichal Simek	nop
173ca54502bSMichal Simek	.endm
174ca54502bSMichal Simek#endif
175ca54502bSMichal Simek
176ca54502bSMichal Simek/* Define how to call high-level functions. With MMU, virtual mode must be
177ca54502bSMichal Simek * enabled when calling the high-level function. Clobbers R11.
178ca54502bSMichal Simek * VM_ON, VM_OFF, DO_JUMP_BIPCLR, DO_CALL
179ca54502bSMichal Simek */
180ca54502bSMichal Simek
181ca54502bSMichal Simek/* turn on virtual protected mode save */
182ca54502bSMichal Simek#define VM_ON		\
183ca54502bSMichal Simek	set_ums;		\
184ca54502bSMichal Simek	rted	r0, 2f;	\
185ca54502bSMichal Simek2: nop;
186ca54502bSMichal Simek
187ca54502bSMichal Simek/* turn off virtual protected mode save and user mode save*/
188ca54502bSMichal Simek#define VM_OFF			\
189ca54502bSMichal Simek	clear_vms_ums;			\
190ca54502bSMichal Simek	rted	r0, TOPHYS(1f);	\
191ca54502bSMichal Simek1: nop;
192ca54502bSMichal Simek
193ca54502bSMichal Simek#define SAVE_REGS \
194ca54502bSMichal Simek	swi	r2, r1, PTO+PT_R2;	/* Save SDA */			\
195ca54502bSMichal Simek	swi	r5, r1, PTO+PT_R5;					\
196ca54502bSMichal Simek	swi	r6, r1, PTO+PT_R6;					\
197ca54502bSMichal Simek	swi	r7, r1, PTO+PT_R7;					\
198ca54502bSMichal Simek	swi	r8, r1, PTO+PT_R8;					\
199ca54502bSMichal Simek	swi	r9, r1, PTO+PT_R9;					\
200ca54502bSMichal Simek	swi	r10, r1, PTO+PT_R10;					\
201ca54502bSMichal Simek	swi	r11, r1, PTO+PT_R11;	/* save clobbered regs after rval */\
202ca54502bSMichal Simek	swi	r12, r1, PTO+PT_R12;					\
203ca54502bSMichal Simek	swi	r13, r1, PTO+PT_R13;	/* Save SDA2 */			\
204ca54502bSMichal Simek	swi	r14, r1, PTO+PT_PC;	/* PC, before IRQ/trap */	\
205ca54502bSMichal Simek	swi	r15, r1, PTO+PT_R15;	/* Save LP */			\
206ca54502bSMichal Simek	swi	r18, r1, PTO+PT_R18;	/* Save asm scratch reg */	\
207ca54502bSMichal Simek	swi	r19, r1, PTO+PT_R19;					\
208ca54502bSMichal Simek	swi	r20, r1, PTO+PT_R20;					\
209ca54502bSMichal Simek	swi	r21, r1, PTO+PT_R21;					\
210ca54502bSMichal Simek	swi	r22, r1, PTO+PT_R22;					\
211ca54502bSMichal Simek	swi	r23, r1, PTO+PT_R23;					\
212ca54502bSMichal Simek	swi	r24, r1, PTO+PT_R24;					\
213ca54502bSMichal Simek	swi	r25, r1, PTO+PT_R25;					\
214ca54502bSMichal Simek	swi	r26, r1, PTO+PT_R26;					\
215ca54502bSMichal Simek	swi	r27, r1, PTO+PT_R27;					\
216ca54502bSMichal Simek	swi	r28, r1, PTO+PT_R28;					\
217ca54502bSMichal Simek	swi	r29, r1, PTO+PT_R29;					\
218ca54502bSMichal Simek	swi	r30, r1, PTO+PT_R30;					\
219ca54502bSMichal Simek	swi	r31, r1, PTO+PT_R31;	/* Save current task reg */	\
220ca54502bSMichal Simek	mfs	r11, rmsr;		/* save MSR */			\
221ca54502bSMichal Simek	nop;								\
222ca54502bSMichal Simek	swi	r11, r1, PTO+PT_MSR;
223ca54502bSMichal Simek
224ca54502bSMichal Simek#define RESTORE_REGS \
225ca54502bSMichal Simek	lwi	r11, r1, PTO+PT_MSR;					\
226ca54502bSMichal Simek	mts	rmsr , r11;						\
227ca54502bSMichal Simek	nop;								\
228ca54502bSMichal Simek	lwi	r2, r1, PTO+PT_R2;	/* restore SDA */		\
229ca54502bSMichal Simek	lwi	r5, r1, PTO+PT_R5;					\
230ca54502bSMichal Simek	lwi	r6, r1, PTO+PT_R6;					\
231ca54502bSMichal Simek	lwi	r7, r1, PTO+PT_R7;					\
232ca54502bSMichal Simek	lwi	r8, r1, PTO+PT_R8;					\
233ca54502bSMichal Simek	lwi	r9, r1, PTO+PT_R9;					\
234ca54502bSMichal Simek	lwi	r10, r1, PTO+PT_R10;					\
235ca54502bSMichal Simek	lwi	r11, r1, PTO+PT_R11;	/* restore clobbered regs after rval */\
236ca54502bSMichal Simek	lwi	r12, r1, PTO+PT_R12;					\
237ca54502bSMichal Simek	lwi	r13, r1, PTO+PT_R13;	/* restore SDA2 */		\
238ca54502bSMichal Simek	lwi	r14, r1, PTO+PT_PC;	/* RESTORE_LINK PC, before IRQ/trap */\
239ca54502bSMichal Simek	lwi	r15, r1, PTO+PT_R15;	/* restore LP */		\
240ca54502bSMichal Simek	lwi	r18, r1, PTO+PT_R18;	/* restore asm scratch reg */	\
241ca54502bSMichal Simek	lwi	r19, r1, PTO+PT_R19;					\
242ca54502bSMichal Simek	lwi	r20, r1, PTO+PT_R20;					\
243ca54502bSMichal Simek	lwi	r21, r1, PTO+PT_R21;					\
244ca54502bSMichal Simek	lwi	r22, r1, PTO+PT_R22;					\
245ca54502bSMichal Simek	lwi	r23, r1, PTO+PT_R23;					\
246ca54502bSMichal Simek	lwi	r24, r1, PTO+PT_R24;					\
247ca54502bSMichal Simek	lwi	r25, r1, PTO+PT_R25;					\
248ca54502bSMichal Simek	lwi	r26, r1, PTO+PT_R26;					\
249ca54502bSMichal Simek	lwi	r27, r1, PTO+PT_R27;					\
250ca54502bSMichal Simek	lwi	r28, r1, PTO+PT_R28;					\
251ca54502bSMichal Simek	lwi	r29, r1, PTO+PT_R29;					\
252ca54502bSMichal Simek	lwi	r30, r1, PTO+PT_R30;					\
253ca54502bSMichal Simek	lwi	r31, r1, PTO+PT_R31;	/* Restore cur task reg */
254ca54502bSMichal Simek
255ca54502bSMichal Simek.text
256ca54502bSMichal Simek
257ca54502bSMichal Simek/*
258ca54502bSMichal Simek * User trap.
259ca54502bSMichal Simek *
260ca54502bSMichal Simek * System calls are handled here.
261ca54502bSMichal Simek *
262ca54502bSMichal Simek * Syscall protocol:
263ca54502bSMichal Simek * Syscall number in r12, args in r5-r10
264ca54502bSMichal Simek * Return value in r3
265ca54502bSMichal Simek *
266ca54502bSMichal Simek * Trap entered via brki instruction, so BIP bit is set, and interrupts
267ca54502bSMichal Simek * are masked. This is nice, means we don't have to CLI before state save
268ca54502bSMichal Simek */
269ca54502bSMichal SimekC_ENTRY(_user_exception):
270ca54502bSMichal Simek	swi	r1, r0, TOPHYS(PER_CPU(ENTRY_SP)) /* save stack */
271ca54502bSMichal Simek	addi	r14, r14, 4	/* return address is 4 byte after call */
272ca54502bSMichal Simek	swi	r11, r0, TOPHYS(r0_ram + PTO + PT_R11);	/* Save r11 */
273ca54502bSMichal Simek
274ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(KM));/* See if already in kernel mode.*/
275ca54502bSMichal Simek	beqi	r11, 1f;		/* Jump ahead if coming from user */
276ca54502bSMichal Simek/* Kernel-mode state save. */
277ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(ENTRY_SP)); /* Reload kernel stack-ptr*/
278ca54502bSMichal Simek	tophys(r1,r11);
279ca54502bSMichal Simek	swi	r11, r1, (PT_R1-PT_SIZE); /* Save original SP. */
280ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(r0_ram + PTO + PT_R11); /* restore r11 */
281ca54502bSMichal Simek
282ca54502bSMichal Simek	addik	r1, r1, -STATE_SAVE_SIZE; /* Make room on the stack. */
283ca54502bSMichal Simek	SAVE_REGS
284ca54502bSMichal Simek
285ca54502bSMichal Simek	addi	r11, r0, 1; 		/* Was in kernel-mode. */
286ca54502bSMichal Simek	swi	r11, r1, PTO+PT_MODE; /* pt_regs -> kernel mode */
287ca54502bSMichal Simek	brid	2f;
288ca54502bSMichal Simek	nop;				/* Fill delay slot */
289ca54502bSMichal Simek
290ca54502bSMichal Simek/* User-mode state save.  */
291ca54502bSMichal Simek1:
292ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(r0_ram + PTO + PT_R11);	/* restore r11 */
293ca54502bSMichal Simek	lwi	r1, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); /* get saved current */
294ca54502bSMichal Simek	tophys(r1,r1);
295ca54502bSMichal Simek	lwi	r1, r1, TS_THREAD_INFO;	/* get stack from task_struct */
296ca54502bSMichal Simek/* calculate kernel stack pointer from task struct 8k */
297ca54502bSMichal Simek	addik	r1, r1, THREAD_SIZE;
298ca54502bSMichal Simek	tophys(r1,r1);
299ca54502bSMichal Simek
300ca54502bSMichal Simek	addik	r1, r1, -STATE_SAVE_SIZE; /* Make room on the stack.  */
301ca54502bSMichal Simek	SAVE_REGS
302ca54502bSMichal Simek
303ca54502bSMichal Simek	swi	r0, r1, PTO+PT_MODE;			/* Was in user-mode. */
304ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(ENTRY_SP));
305ca54502bSMichal Simek	swi	r11, r1, PTO+PT_R1;		/* Store user SP.  */
306ca54502bSMichal Simek	addi	r11, r0, 1;
307ca54502bSMichal Simek	swi	r11, r0, TOPHYS(PER_CPU(KM));	/* Now we're in kernel-mode.  */
308b1d70c62SMichal Simek2:	lwi	CURRENT_TASK, r0, TOPHYS(PER_CPU(CURRENT_SAVE));
309ca54502bSMichal Simek	/* Save away the syscall number.  */
310ca54502bSMichal Simek	swi	r12, r1, PTO+PT_R0;
311ca54502bSMichal Simek	tovirt(r1,r1)
312ca54502bSMichal Simek
313ca54502bSMichal Simek/* where the trap should return need -8 to adjust for rtsd r15, 8*/
314ca54502bSMichal Simek/* Jump to the appropriate function for the system call number in r12
315ca54502bSMichal Simek * (r12 is not preserved), or return an error if r12 is not valid. The LP
316ca54502bSMichal Simek * register should point to the location where
317ca54502bSMichal Simek * the called function should return.  [note that MAKE_SYS_CALL uses label 1] */
31823575483SMichal Simek
31923575483SMichal Simek	# Step into virtual mode.
32023575483SMichal Simek	set_vms;
32123575483SMichal Simek	addik	r11, r0, 3f
32223575483SMichal Simek	rtid	r11, 0
32323575483SMichal Simek	nop
32423575483SMichal Simek3:
325b1d70c62SMichal Simek	lwi	r11, CURRENT_TASK, TS_THREAD_INFO /* get thread info */
32623575483SMichal Simek	lwi	r11, r11, TI_FLAGS	 /* get flags in thread info */
32723575483SMichal Simek	andi	r11, r11, _TIF_WORK_SYSCALL_MASK
32823575483SMichal Simek	beqi	r11, 4f
32923575483SMichal Simek
33023575483SMichal Simek	addik	r3, r0, -ENOSYS
33123575483SMichal Simek	swi	r3, r1, PTO + PT_R3
33223575483SMichal Simek	brlid	r15, do_syscall_trace_enter
33323575483SMichal Simek	addik	r5, r1, PTO + PT_R0
33423575483SMichal Simek
33523575483SMichal Simek	# do_syscall_trace_enter returns the new syscall nr.
33623575483SMichal Simek	addk	r12, r0, r3
33723575483SMichal Simek	lwi	r5, r1, PTO+PT_R5;
33823575483SMichal Simek	lwi	r6, r1, PTO+PT_R6;
33923575483SMichal Simek	lwi	r7, r1, PTO+PT_R7;
34023575483SMichal Simek	lwi	r8, r1, PTO+PT_R8;
34123575483SMichal Simek	lwi	r9, r1, PTO+PT_R9;
34223575483SMichal Simek	lwi	r10, r1, PTO+PT_R10;
34323575483SMichal Simek4:
34423575483SMichal Simek/* Jump to the appropriate function for the system call number in r12
34523575483SMichal Simek * (r12 is not preserved), or return an error if r12 is not valid.
34623575483SMichal Simek * The LP register should point to the location where the called function
34723575483SMichal Simek * should return.  [note that MAKE_SYS_CALL uses label 1] */
34823575483SMichal Simek	/* See if the system call number is valid */
349ca54502bSMichal Simek	addi	r11, r12, -__NR_syscalls;
35023575483SMichal Simek	bgei	r11,5f;
351ca54502bSMichal Simek	/* Figure out which function to use for this system call.  */
352ca54502bSMichal Simek	/* Note Microblaze barrel shift is optional, so don't rely on it */
353ca54502bSMichal Simek	add	r12, r12, r12;			/* convert num -> ptr */
354ca54502bSMichal Simek	add	r12, r12, r12;
355ca54502bSMichal Simek
35611d51360SMichal Simek#ifdef DEBUG
357ca54502bSMichal Simek	/* Trac syscalls and stored them to r0_ram */
35823575483SMichal Simek	lwi	r3, r12, 0x400 + r0_ram
359ca54502bSMichal Simek	addi	r3, r3, 1
36023575483SMichal Simek	swi	r3, r12, 0x400 + r0_ram
36111d51360SMichal Simek#endif
362ca54502bSMichal Simek
36323575483SMichal Simek	# Find and jump into the syscall handler.
36423575483SMichal Simek	lwi	r12, r12, sys_call_table
36523575483SMichal Simek	/* where the trap should return need -8 to adjust for rtsd r15, 8 */
36623575483SMichal Simek	la	r15, r0, ret_from_trap-8
36723575483SMichal Simek	bra	r12
36823575483SMichal Simek
369ca54502bSMichal Simek	/* The syscall number is invalid, return an error.  */
37023575483SMichal Simek5:
371ca54502bSMichal Simek	addi	r3, r0, -ENOSYS;
372ca54502bSMichal Simek	rtsd	r15,8;		/* looks like a normal subroutine return */
373ca54502bSMichal Simek	or 	r0, r0, r0
374ca54502bSMichal Simek
375ca54502bSMichal Simek
37623575483SMichal Simek/* Entry point used to return from a syscall/trap */
377ca54502bSMichal Simek/* We re-enable BIP bit before state restore */
378ca54502bSMichal SimekC_ENTRY(ret_from_trap):
379ca54502bSMichal Simek	set_bip;			/*  Ints masked for state restore*/
380ca54502bSMichal Simek	lwi	r11, r1, PTO+PT_MODE;
381ca54502bSMichal Simek/* See if returning to kernel mode, if so, skip resched &c.  */
382ca54502bSMichal Simek	bnei	r11, 2f;
383ca54502bSMichal Simek
384b1d70c62SMichal Simek	swi	r3, r1, PTO + PT_R3
385b1d70c62SMichal Simek	swi	r4, r1, PTO + PT_R4
386b1d70c62SMichal Simek
387ca54502bSMichal Simek	/* We're returning to user mode, so check for various conditions that
388ca54502bSMichal Simek	 * trigger rescheduling. */
389b1d70c62SMichal Simek	/* FIXME: Restructure all these flag checks. */
390b1d70c62SMichal Simek	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;	/* get thread info */
39123575483SMichal Simek	lwi	r11, r11, TI_FLAGS;		/* get flags in thread info */
39223575483SMichal Simek	andi	r11, r11, _TIF_WORK_SYSCALL_MASK
39323575483SMichal Simek	beqi	r11, 1f
39423575483SMichal Simek
39523575483SMichal Simek	brlid	r15, do_syscall_trace_leave
39623575483SMichal Simek	addik	r5, r1, PTO + PT_R0
39723575483SMichal Simek1:
39823575483SMichal Simek	/* We're returning to user mode, so check for various conditions that
39923575483SMichal Simek	 * trigger rescheduling. */
400b1d70c62SMichal Simek	/* get thread info from current task */
401b1d70c62SMichal Simek	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;
402ca54502bSMichal Simek	lwi	r11, r11, TI_FLAGS;		/* get flags in thread info */
403ca54502bSMichal Simek	andi	r11, r11, _TIF_NEED_RESCHED;
404ca54502bSMichal Simek	beqi	r11, 5f;
405ca54502bSMichal Simek
406ca54502bSMichal Simek	bralid	r15, schedule;	/* Call scheduler */
407ca54502bSMichal Simek	nop;				/* delay slot */
408ca54502bSMichal Simek
409ca54502bSMichal Simek	/* Maybe handle a signal */
410b1d70c62SMichal Simek5:	/* get thread info from current task*/
411b1d70c62SMichal Simek	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;
412ca54502bSMichal Simek	lwi	r11, r11, TI_FLAGS;	/* get flags in thread info */
413ca54502bSMichal Simek	andi	r11, r11, _TIF_SIGPENDING;
414ca54502bSMichal Simek	beqi	r11, 1f;		/* Signals to handle, handle them */
415ca54502bSMichal Simek
416ca54502bSMichal Simek	la	r5, r1, PTO;		/* Arg 1: struct pt_regs *regs */
417ca54502bSMichal Simek	addi	r7, r0, 1;		/* Arg 3: int in_syscall */
418ca54502bSMichal Simek	bralid	r15, do_signal;	/* Handle any signals */
419841d6e8cSMichal Simek	add	r6, r0, r0;		/* Arg 2: sigset_t *oldset */
420b1d70c62SMichal Simek
421b1d70c62SMichal Simek/* Finally, return to user state.  */
422b1d70c62SMichal Simek1:
423ca54502bSMichal Simek	lwi	r3, r1, PTO + PT_R3; /* restore syscall result */
424ca54502bSMichal Simek	lwi	r4, r1, PTO + PT_R4;
425ca54502bSMichal Simek
426b1d70c62SMichal Simek	swi	r0, r0, PER_CPU(KM);	/* Now officially in user state. */
4278633bebcSMichal Simek	swi	CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE); /* save current */
428ca54502bSMichal Simek	VM_OFF;
429ca54502bSMichal Simek	tophys(r1,r1);
430ca54502bSMichal Simek	RESTORE_REGS;
431ca54502bSMichal Simek	addik	r1, r1, STATE_SAVE_SIZE		/* Clean up stack space.  */
432ca54502bSMichal Simek	lwi	r1, r1, PT_R1 - PT_SIZE;/* Restore user stack pointer. */
433ca54502bSMichal Simek	bri	6f;
434ca54502bSMichal Simek
435ca54502bSMichal Simek/* Return to kernel state.  */
436ca54502bSMichal Simek2:	VM_OFF;
437ca54502bSMichal Simek	tophys(r1,r1);
438ca54502bSMichal Simek	RESTORE_REGS;
439ca54502bSMichal Simek	addik	r1, r1, STATE_SAVE_SIZE		/* Clean up stack space.  */
440ca54502bSMichal Simek	tovirt(r1,r1);
441ca54502bSMichal Simek6:
442ca54502bSMichal SimekTRAP_return:		/* Make global symbol for debugging */
443ca54502bSMichal Simek	rtbd	r14, 0;	/* Instructions to return from an IRQ */
444ca54502bSMichal Simek	nop;
445ca54502bSMichal Simek
446ca54502bSMichal Simek
447ca54502bSMichal Simek/* These syscalls need access to the struct pt_regs on the stack, so we
448ca54502bSMichal Simek   implement them in assembly (they're basically all wrappers anyway).  */
449ca54502bSMichal Simek
450ca54502bSMichal SimekC_ENTRY(sys_fork_wrapper):
451ca54502bSMichal Simek	addi	r5, r0, SIGCHLD			/* Arg 0: flags */
452ca54502bSMichal Simek	lwi	r6, r1, PTO+PT_R1	/* Arg 1: child SP (use parent's) */
453ca54502bSMichal Simek	la	r7, r1, PTO			/* Arg 2: parent context */
454ca54502bSMichal Simek	add	r8. r0, r0			/* Arg 3: (unused) */
455ca54502bSMichal Simek	add	r9, r0, r0;			/* Arg 4: (unused) */
456ca54502bSMichal Simek	add	r10, r0, r0;			/* Arg 5: (unused) */
457ca54502bSMichal Simek	brid	do_fork		/* Do real work (tail-call) */
458ca54502bSMichal Simek	nop;
459ca54502bSMichal Simek
460ca54502bSMichal Simek/* This the initial entry point for a new child thread, with an appropriate
461ca54502bSMichal Simek   stack in place that makes it look the the child is in the middle of an
462ca54502bSMichal Simek   syscall.  This function is actually `returned to' from switch_thread
463ca54502bSMichal Simek   (copy_thread makes ret_from_fork the return address in each new thread's
464ca54502bSMichal Simek   saved context).  */
465ca54502bSMichal SimekC_ENTRY(ret_from_fork):
466ca54502bSMichal Simek	bralid	r15, schedule_tail; /* ...which is schedule_tail's arg */
467ca54502bSMichal Simek	add	r3, r5, r0;	/* switch_thread returns the prev task */
468ca54502bSMichal Simek				/* ( in the delay slot ) */
469ca54502bSMichal Simek	add	r3, r0, r0;	/* Child's fork call should return 0. */
470ca54502bSMichal Simek	brid	ret_from_trap;	/* Do normal trap return */
471ca54502bSMichal Simek	nop;
472ca54502bSMichal Simek
473e513588fSArnd BergmannC_ENTRY(sys_vfork):
474e513588fSArnd Bergmann	brid	microblaze_vfork	/* Do real work (tail-call) */
475ca54502bSMichal Simek	la	r5, r1, PTO
476ca54502bSMichal Simek
477e513588fSArnd BergmannC_ENTRY(sys_clone):
478ca54502bSMichal Simek	bnei	r6, 1f;			/* See if child SP arg (arg 1) is 0. */
479ca54502bSMichal Simek	lwi	r6, r1, PTO + PT_R1;	/* If so, use paret's stack ptr */
480570e3e23SMichal Simek1:	add	r10, r0, r9;		/* Arg 6: (child_tidptr) */
481570e3e23SMichal Simek	add	r9, r0, r8;		/* Arg 5: (parent_tidptr) */
482570e3e23SMichal Simek	add	r8, r0, r7;		/* Arg 4: (stack_size) */
483570e3e23SMichal Simek	la	r7, r1, PTO;		/* Arg 3: pt_regs  */
484ca54502bSMichal Simek	brid	do_fork			/* Do real work (tail-call) */
485570e3e23SMichal Simek	nop
486ca54502bSMichal Simek
487e513588fSArnd BergmannC_ENTRY(sys_execve):
488ca54502bSMichal Simek	la	r8, r1, PTO;		/* add user context as 4th arg */
489e513588fSArnd Bergmann	brid	microblaze_execve;	/* Do real work (tail-call).*/
490ca54502bSMichal Simek	nop;
491ca54502bSMichal Simek
492ca54502bSMichal SimekC_ENTRY(sys_rt_sigreturn_wrapper):
493ca54502bSMichal Simek	swi	r3, r1, PTO+PT_R3; /* restore saved r3, r4 registers */
494ca54502bSMichal Simek	swi	r4, r1, PTO+PT_R4;
495ca54502bSMichal Simek	la	r5, r1, PTO;		/* add user context as 1st arg */
496ca54502bSMichal Simek	brlid	r15, sys_rt_sigreturn	/* Do real work */
497ca54502bSMichal Simek	nop;
498ca54502bSMichal Simek	lwi	r3, r1, PTO+PT_R3; /* restore saved r3, r4 registers */
499ca54502bSMichal Simek	lwi	r4, r1, PTO+PT_R4;
500ca54502bSMichal Simek	bri ret_from_trap /* fall through will not work here due to align */
501ca54502bSMichal Simek	nop;
502ca54502bSMichal Simek
503ca54502bSMichal Simek/*
504ca54502bSMichal Simek * HW EXCEPTION rutine start
505ca54502bSMichal Simek */
506ca54502bSMichal Simek
507ca54502bSMichal Simek#define SAVE_STATE	\
508ca54502bSMichal Simek	swi	r11, r0, TOPHYS(r0_ram + PTO + PT_R11); /* Save r11 */	\
509ca54502bSMichal Simek	set_bip;	/*equalize initial state for all possible entries*/\
510ca54502bSMichal Simek	clear_eip;							\
511ca54502bSMichal Simek	enable_irq;							\
512ca54502bSMichal Simek	set_ee;								\
513ca54502bSMichal Simek	/* See if already in kernel mode.*/				\
514ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(KM));				\
515ca54502bSMichal Simek	beqi	r11, 1f;		/* Jump ahead if coming from user */\
516ca54502bSMichal Simek	/* Kernel-mode state save.  */					\
517ca54502bSMichal Simek	/* Reload kernel stack-ptr. */					\
518ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(ENTRY_SP));			\
519ca54502bSMichal Simek	tophys(r1,r11);							\
520ca54502bSMichal Simek	swi	r11, r1, (PT_R1-PT_SIZE); /* Save original SP. */	\
521ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(r0_ram + PTO + PT_R11); /* restore r11 */\
522ca54502bSMichal Simek	addik	r1, r1, -STATE_SAVE_SIZE; /* Make room on the stack.  */\
523ca54502bSMichal Simek	/* store return registers separately because			\
524ca54502bSMichal Simek	 * this macros is use for others exceptions */			\
525ca54502bSMichal Simek	swi	r3, r1, PTO + PT_R3;					\
526ca54502bSMichal Simek	swi	r4, r1, PTO + PT_R4;					\
527ca54502bSMichal Simek	SAVE_REGS							\
528ca54502bSMichal Simek	/* PC, before IRQ/trap - this is one instruction above */	\
529ca54502bSMichal Simek	swi	r17, r1, PTO+PT_PC;					\
530ca54502bSMichal Simek									\
531ca54502bSMichal Simek	addi	r11, r0, 1; 		/* Was in kernel-mode.  */	\
532ca54502bSMichal Simek	swi	r11, r1, PTO+PT_MODE; 	 				\
533ca54502bSMichal Simek	brid	2f;							\
534ca54502bSMichal Simek	nop;				/* Fill delay slot */		\
535ca54502bSMichal Simek1:	/* User-mode state save.  */					\
536ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(r0_ram + PTO + PT_R11); /* restore r11 */\
537ca54502bSMichal Simek	lwi	r1, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); /* get saved current */\
538ca54502bSMichal Simek	tophys(r1,r1);							\
539ca54502bSMichal Simek	lwi	r1, r1, TS_THREAD_INFO;	/* get the thread info */	\
540ca54502bSMichal Simek	addik	r1, r1, THREAD_SIZE;	/* calculate kernel stack pointer */\
541ca54502bSMichal Simek	tophys(r1,r1);							\
542ca54502bSMichal Simek									\
543ca54502bSMichal Simek	addik	r1, r1, -STATE_SAVE_SIZE; /* Make room on the stack.  */\
544ca54502bSMichal Simek	/* store return registers separately because this macros	\
545ca54502bSMichal Simek	 * is use for others exceptions */				\
546ca54502bSMichal Simek	swi	r3, r1, PTO + PT_R3; 					\
547ca54502bSMichal Simek	swi	r4, r1, PTO + PT_R4;					\
548ca54502bSMichal Simek	SAVE_REGS							\
549ca54502bSMichal Simek	/* PC, before IRQ/trap - this is one instruction above FIXME*/	\
550ca54502bSMichal Simek	swi	r17, r1, PTO+PT_PC;					\
551ca54502bSMichal Simek									\
552ca54502bSMichal Simek	swi	r0, r1, PTO+PT_MODE; /* Was in user-mode.  */		\
553ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(ENTRY_SP));			\
554ca54502bSMichal Simek	swi	r11, r1, PTO+PT_R1; /* Store user SP.  */		\
555ca54502bSMichal Simek	addi	r11, r0, 1;						\
556ca54502bSMichal Simek	swi	r11, r0, TOPHYS(PER_CPU(KM)); /* Now we're in kernel-mode.*/\
557b1d70c62SMichal Simek2:	lwi	CURRENT_TASK, r0, TOPHYS(PER_CPU(CURRENT_SAVE));	\
558ca54502bSMichal Simek	/* Save away the syscall number.  */				\
559ca54502bSMichal Simek	swi	r0, r1, PTO+PT_R0;					\
560ca54502bSMichal Simek	tovirt(r1,r1)
561ca54502bSMichal Simek
562ca54502bSMichal SimekC_ENTRY(full_exception_trap):
563ca54502bSMichal Simek	swi	r1, r0, TOPHYS(PER_CPU(ENTRY_SP)) /* save stack */
564ca54502bSMichal Simek	/* adjust exception address for privileged instruction
565ca54502bSMichal Simek	 * for finding where is it */
566ca54502bSMichal Simek	addik	r17, r17, -4
567ca54502bSMichal Simek	SAVE_STATE /* Save registers */
568ca54502bSMichal Simek	/* FIXME this can be store directly in PT_ESR reg.
569ca54502bSMichal Simek	 * I tested it but there is a fault */
570ca54502bSMichal Simek	/* where the trap should return need -8 to adjust for rtsd r15, 8 */
571ca54502bSMichal Simek	la	r15, r0, ret_from_exc - 8
572ca54502bSMichal Simek	la	r5, r1, PTO		 /* parameter struct pt_regs * regs */
573ca54502bSMichal Simek	mfs	r6, resr
574ca54502bSMichal Simek	nop
575ca54502bSMichal Simek	mfs	r7, rfsr;		/* save FSR */
576ca54502bSMichal Simek	nop
577131e4e97SMichal Simek	mts	rfsr, r0;	/* Clear sticky fsr */
578131e4e97SMichal Simek	nop
579ca54502bSMichal Simek	la	r12, r0, full_exception
580ca54502bSMichal Simek	set_vms;
581ca54502bSMichal Simek	rtbd	r12, 0;
582ca54502bSMichal Simek	nop;
583ca54502bSMichal Simek
584ca54502bSMichal Simek/*
585ca54502bSMichal Simek * Unaligned data trap.
586ca54502bSMichal Simek *
587ca54502bSMichal Simek * Unaligned data trap last on 4k page is handled here.
588ca54502bSMichal Simek *
589ca54502bSMichal Simek * Trap entered via exception, so EE bit is set, and interrupts
590ca54502bSMichal Simek * are masked.  This is nice, means we don't have to CLI before state save
591ca54502bSMichal Simek *
592ca54502bSMichal Simek * The assembler routine is in "arch/microblaze/kernel/hw_exception_handler.S"
593ca54502bSMichal Simek */
594ca54502bSMichal SimekC_ENTRY(unaligned_data_trap):
595ca54502bSMichal Simek	swi	r1, r0, TOPHYS(PER_CPU(ENTRY_SP)) /* save stack */
596ca54502bSMichal Simek	SAVE_STATE		/* Save registers.*/
597ca54502bSMichal Simek	/* where the trap should return need -8 to adjust for rtsd r15, 8 */
598ca54502bSMichal Simek	la	r15, r0, ret_from_exc-8
599ca54502bSMichal Simek	mfs	r3, resr		/* ESR */
600ca54502bSMichal Simek	nop
601ca54502bSMichal Simek	mfs	r4, rear		/* EAR */
602ca54502bSMichal Simek	nop
603ca54502bSMichal Simek	la	r7, r1, PTO		/* parameter struct pt_regs * regs */
604ca54502bSMichal Simek	la	r12, r0, _unaligned_data_exception
605ca54502bSMichal Simek	set_vms;
606ca54502bSMichal Simek	rtbd	r12, 0;	/* interrupts enabled */
607ca54502bSMichal Simek	nop;
608ca54502bSMichal Simek
609ca54502bSMichal Simek/*
610ca54502bSMichal Simek * Page fault traps.
611ca54502bSMichal Simek *
612ca54502bSMichal Simek * If the real exception handler (from hw_exception_handler.S) didn't find
613ca54502bSMichal Simek * the mapping for the process, then we're thrown here to handle such situation.
614ca54502bSMichal Simek *
615ca54502bSMichal Simek * Trap entered via exceptions, so EE bit is set, and interrupts
616ca54502bSMichal Simek * are masked.  This is nice, means we don't have to CLI before state save
617ca54502bSMichal Simek *
618ca54502bSMichal Simek * Build a standard exception frame for TLB Access errors.  All TLB exceptions
619ca54502bSMichal Simek * will bail out to this point if they can't resolve the lightweight TLB fault.
620ca54502bSMichal Simek *
621ca54502bSMichal Simek * The C function called is in "arch/microblaze/mm/fault.c", declared as:
622ca54502bSMichal Simek * void do_page_fault(struct pt_regs *regs,
623ca54502bSMichal Simek *				unsigned long address,
624ca54502bSMichal Simek *				unsigned long error_code)
625ca54502bSMichal Simek */
626ca54502bSMichal Simek/* data and intruction trap - which is choose is resolved int fault.c */
627ca54502bSMichal SimekC_ENTRY(page_fault_data_trap):
628ca54502bSMichal Simek	swi	r1, r0, TOPHYS(PER_CPU(ENTRY_SP)) /* save stack */
629ca54502bSMichal Simek	SAVE_STATE		/* Save registers.*/
630ca54502bSMichal Simek	/* where the trap should return need -8 to adjust for rtsd r15, 8 */
631ca54502bSMichal Simek	la	r15, r0, ret_from_exc-8
632ca54502bSMichal Simek	la	r5, r1, PTO		/* parameter struct pt_regs * regs */
633ca54502bSMichal Simek	mfs	r6, rear		/* parameter unsigned long address */
634ca54502bSMichal Simek	nop
635ca54502bSMichal Simek	mfs	r7, resr		/* parameter unsigned long error_code */
636ca54502bSMichal Simek	nop
637ca54502bSMichal Simek	la	r12, r0, do_page_fault
638ca54502bSMichal Simek	set_vms;
639ca54502bSMichal Simek	rtbd	r12, 0;	/* interrupts enabled */
640ca54502bSMichal Simek	nop;
641ca54502bSMichal Simek
642ca54502bSMichal SimekC_ENTRY(page_fault_instr_trap):
643ca54502bSMichal Simek	swi	r1, r0, TOPHYS(PER_CPU(ENTRY_SP)) /* save stack */
644ca54502bSMichal Simek	SAVE_STATE		/* Save registers.*/
645ca54502bSMichal Simek	/* where the trap should return need -8 to adjust for rtsd r15, 8 */
646ca54502bSMichal Simek	la	r15, r0, ret_from_exc-8
647ca54502bSMichal Simek	la	r5, r1, PTO		/* parameter struct pt_regs * regs */
648ca54502bSMichal Simek	mfs	r6, rear		/* parameter unsigned long address */
649ca54502bSMichal Simek	nop
650ca54502bSMichal Simek	ori	r7, r0, 0		/* parameter unsigned long error_code */
651ca54502bSMichal Simek	la	r12, r0, do_page_fault
652ca54502bSMichal Simek	set_vms;
653ca54502bSMichal Simek	rtbd	r12, 0;	/* interrupts enabled */
654ca54502bSMichal Simek	nop;
655ca54502bSMichal Simek
656ca54502bSMichal Simek/* Entry point used to return from an exception.  */
657ca54502bSMichal SimekC_ENTRY(ret_from_exc):
658ca54502bSMichal Simek	set_bip;			/*  Ints masked for state restore*/
659ca54502bSMichal Simek	lwi	r11, r1, PTO+PT_MODE;
660ca54502bSMichal Simek	bnei	r11, 2f;		/* See if returning to kernel mode, */
661ca54502bSMichal Simek					/* ... if so, skip resched &c.  */
662ca54502bSMichal Simek
663ca54502bSMichal Simek	/* We're returning to user mode, so check for various conditions that
664ca54502bSMichal Simek	   trigger rescheduling. */
665b1d70c62SMichal Simek	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;	/* get thread info */
666ca54502bSMichal Simek	lwi	r11, r11, TI_FLAGS;	/* get flags in thread info */
667ca54502bSMichal Simek	andi	r11, r11, _TIF_NEED_RESCHED;
668ca54502bSMichal Simek	beqi	r11, 5f;
669ca54502bSMichal Simek
670ca54502bSMichal Simek/* Call the scheduler before returning from a syscall/trap. */
671ca54502bSMichal Simek	bralid	r15, schedule;	/* Call scheduler */
672ca54502bSMichal Simek	nop;				/* delay slot */
673ca54502bSMichal Simek
674ca54502bSMichal Simek	/* Maybe handle a signal */
675b1d70c62SMichal Simek5:	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;	/* get thread info */
676ca54502bSMichal Simek	lwi	r11, r11, TI_FLAGS;	/* get flags in thread info */
677ca54502bSMichal Simek	andi	r11, r11, _TIF_SIGPENDING;
678ca54502bSMichal Simek	beqi	r11, 1f;		/* Signals to handle, handle them */
679ca54502bSMichal Simek
680ca54502bSMichal Simek	/*
681ca54502bSMichal Simek	 * Handle a signal return; Pending signals should be in r18.
682ca54502bSMichal Simek	 *
683ca54502bSMichal Simek	 * Not all registers are saved by the normal trap/interrupt entry
684ca54502bSMichal Simek	 * points (for instance, call-saved registers (because the normal
685ca54502bSMichal Simek	 * C-compiler calling sequence in the kernel makes sure they're
686ca54502bSMichal Simek	 * preserved), and call-clobbered registers in the case of
687ca54502bSMichal Simek	 * traps), but signal handlers may want to examine or change the
688ca54502bSMichal Simek	 * complete register state.  Here we save anything not saved by
689ca54502bSMichal Simek	 * the normal entry sequence, so that it may be safely restored
690ca54502bSMichal Simek	 * (in a possibly modified form) after do_signal returns.
691ca54502bSMichal Simek	 * store return registers separately because this macros is use
692ca54502bSMichal Simek	 * for others exceptions */
693ca54502bSMichal Simek	la	r5, r1, PTO;		/* Arg 1: struct pt_regs *regs */
694ca54502bSMichal Simek	addi	r7, r0, 0;		/* Arg 3: int in_syscall */
695ca54502bSMichal Simek	bralid	r15, do_signal;	/* Handle any signals */
696841d6e8cSMichal Simek	add	r6, r0, r0;		/* Arg 2: sigset_t *oldset */
697ca54502bSMichal Simek
698ca54502bSMichal Simek/* Finally, return to user state.  */
699ca54502bSMichal Simek1:	swi	r0, r0, PER_CPU(KM);	/* Now officially in user state. */
7008633bebcSMichal Simek	swi	CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE); /* save current */
701ca54502bSMichal Simek	VM_OFF;
702ca54502bSMichal Simek	tophys(r1,r1);
703ca54502bSMichal Simek
704ca54502bSMichal Simek	lwi	r3, r1, PTO+PT_R3; /* restore saved r3, r4 registers */
705ca54502bSMichal Simek	lwi	r4, r1, PTO+PT_R4;
706ca54502bSMichal Simek	RESTORE_REGS;
707ca54502bSMichal Simek	addik	r1, r1, STATE_SAVE_SIZE		/* Clean up stack space.  */
708ca54502bSMichal Simek
709ca54502bSMichal Simek	lwi	r1, r1, PT_R1 - PT_SIZE; /* Restore user stack pointer. */
710ca54502bSMichal Simek	bri	6f;
711ca54502bSMichal Simek/* Return to kernel state.  */
712ca54502bSMichal Simek2:	VM_OFF;
713ca54502bSMichal Simek	tophys(r1,r1);
714ca54502bSMichal Simek	lwi	r3, r1, PTO+PT_R3; /* restore saved r3, r4 registers */
715ca54502bSMichal Simek	lwi	r4, r1, PTO+PT_R4;
716ca54502bSMichal Simek	RESTORE_REGS;
717ca54502bSMichal Simek	addik	r1, r1, STATE_SAVE_SIZE		/* Clean up stack space.  */
718ca54502bSMichal Simek
719ca54502bSMichal Simek	tovirt(r1,r1);
720ca54502bSMichal Simek6:
721ca54502bSMichal SimekEXC_return:		/* Make global symbol for debugging */
722ca54502bSMichal Simek	rtbd	r14, 0;	/* Instructions to return from an IRQ */
723ca54502bSMichal Simek	nop;
724ca54502bSMichal Simek
725ca54502bSMichal Simek/*
726ca54502bSMichal Simek * HW EXCEPTION rutine end
727ca54502bSMichal Simek */
728ca54502bSMichal Simek
729ca54502bSMichal Simek/*
730ca54502bSMichal Simek * Hardware maskable interrupts.
731ca54502bSMichal Simek *
732ca54502bSMichal Simek * The stack-pointer (r1) should have already been saved to the memory
733ca54502bSMichal Simek * location PER_CPU(ENTRY_SP).
734ca54502bSMichal Simek */
735ca54502bSMichal SimekC_ENTRY(_interrupt):
736ca54502bSMichal Simek/* MS: we are in physical address */
737ca54502bSMichal Simek/* Save registers, switch to proper stack, convert SP to virtual.*/
738ca54502bSMichal Simek	swi	r1, r0, TOPHYS(PER_CPU(ENTRY_SP))
739ca54502bSMichal Simek	swi	r11, r0, TOPHYS(PER_CPU(R11_SAVE));
740ca54502bSMichal Simek	/* MS: See if already in kernel mode. */
741ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(KM));
742ca54502bSMichal Simek	beqi	r11, 1f; /* MS: Jump ahead if coming from user */
743ca54502bSMichal Simek
744ca54502bSMichal Simek/* Kernel-mode state save. */
745ca54502bSMichal Simek	or	r11, r1, r0
746ca54502bSMichal Simek	tophys(r1,r11); /* MS: I have in r1 physical address where stack is */
747ca54502bSMichal Simek/* MS: Save original SP - position PT_R1 to next stack frame 4 *1 - 152*/
748ca54502bSMichal Simek	swi	r11, r1, (PT_R1 - PT_SIZE);
749ca54502bSMichal Simek/* MS: restore r11 because of saving in SAVE_REGS */
750ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(R11_SAVE));
751ca54502bSMichal Simek	/* save registers */
752ca54502bSMichal Simek/* MS: Make room on the stack -> activation record */
753ca54502bSMichal Simek	addik	r1, r1, -STATE_SAVE_SIZE;
754ca54502bSMichal Simek/* MS: store return registers separately because
755ca54502bSMichal Simek * this macros is use for others exceptions */
756ca54502bSMichal Simek	swi	r3, r1, PTO + PT_R3;
757ca54502bSMichal Simek	swi	r4, r1, PTO + PT_R4;
758ca54502bSMichal Simek	SAVE_REGS
759ca54502bSMichal Simek	/* MS: store mode */
760ca54502bSMichal Simek	addi	r11, r0, 1; /* MS: Was in kernel-mode. */
761ca54502bSMichal Simek	swi	r11, r1, PTO + PT_MODE; /* MS: and save it */
762ca54502bSMichal Simek	brid	2f;
763ca54502bSMichal Simek	nop; /* MS: Fill delay slot */
764ca54502bSMichal Simek
765ca54502bSMichal Simek1:
766ca54502bSMichal Simek/* User-mode state save. */
767ca54502bSMichal Simek/* MS: restore r11 -> FIXME move before SAVE_REG */
768ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(R11_SAVE));
769ca54502bSMichal Simek /* MS: get the saved current */
770ca54502bSMichal Simek	lwi	r1, r0, TOPHYS(PER_CPU(CURRENT_SAVE));
771ca54502bSMichal Simek	tophys(r1,r1);
772ca54502bSMichal Simek	lwi	r1, r1, TS_THREAD_INFO;
773ca54502bSMichal Simek	addik	r1, r1, THREAD_SIZE;
774ca54502bSMichal Simek	tophys(r1,r1);
775ca54502bSMichal Simek	/* save registers */
776ca54502bSMichal Simek	addik	r1, r1, -STATE_SAVE_SIZE;
777ca54502bSMichal Simek	swi	r3, r1, PTO+PT_R3;
778ca54502bSMichal Simek	swi	r4, r1, PTO+PT_R4;
779ca54502bSMichal Simek	SAVE_REGS
780ca54502bSMichal Simek	/* calculate mode */
781ca54502bSMichal Simek	swi	r0, r1, PTO + PT_MODE;
782ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(ENTRY_SP));
783ca54502bSMichal Simek	swi	r11, r1, PTO+PT_R1;
784ca54502bSMichal Simek	/* setup kernel mode to KM */
785ca54502bSMichal Simek	addi	r11, r0, 1;
786ca54502bSMichal Simek	swi	r11, r0, TOPHYS(PER_CPU(KM));
787ca54502bSMichal Simek
788ca54502bSMichal Simek2:
789b1d70c62SMichal Simek	lwi	CURRENT_TASK, r0, TOPHYS(PER_CPU(CURRENT_SAVE));
790ca54502bSMichal Simek	swi	r0, r1, PTO + PT_R0;
791ca54502bSMichal Simek	tovirt(r1,r1)
792ca54502bSMichal Simek	la	r5, r1, PTO;
793ca54502bSMichal Simek	set_vms;
794ca54502bSMichal Simek	la	r11, r0, do_IRQ;
795ca54502bSMichal Simek	la	r15, r0, irq_call;
796ca54502bSMichal Simekirq_call:rtbd	r11, 0;
797ca54502bSMichal Simek	nop;
798ca54502bSMichal Simek
799ca54502bSMichal Simek/* MS: we are in virtual mode */
800ca54502bSMichal Simekret_from_irq:
801ca54502bSMichal Simek	lwi	r11, r1, PTO + PT_MODE;
802ca54502bSMichal Simek	bnei	r11, 2f;
803ca54502bSMichal Simek
804b1d70c62SMichal Simek	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;
805ca54502bSMichal Simek	lwi	r11, r11, TI_FLAGS; /* MS: get flags from thread info */
806ca54502bSMichal Simek	andi	r11, r11, _TIF_NEED_RESCHED;
807ca54502bSMichal Simek	beqi	r11, 5f
808ca54502bSMichal Simek	bralid	r15, schedule;
809ca54502bSMichal Simek	nop; /* delay slot */
810ca54502bSMichal Simek
811ca54502bSMichal Simek    /* Maybe handle a signal */
812b1d70c62SMichal Simek5:	lwi	r11, CURRENT_TASK, TS_THREAD_INFO; /* MS: get thread info */
813ca54502bSMichal Simek	lwi	r11, r11, TI_FLAGS; /* get flags in thread info */
814ca54502bSMichal Simek	andi	r11, r11, _TIF_SIGPENDING;
815ca54502bSMichal Simek	beqid	r11, no_intr_resched
816ca54502bSMichal Simek/* Handle a signal return; Pending signals should be in r18. */
817ca54502bSMichal Simek	addi	r7, r0, 0; /* Arg 3: int in_syscall */
818ca54502bSMichal Simek	la	r5, r1, PTO; /* Arg 1: struct pt_regs *regs */
819ca54502bSMichal Simek	bralid	r15, do_signal;	/* Handle any signals */
820ca54502bSMichal Simek	add	r6, r0, r0; /* Arg 2: sigset_t *oldset */
821ca54502bSMichal Simek
822ca54502bSMichal Simek/* Finally, return to user state. */
823ca54502bSMichal Simekno_intr_resched:
824ca54502bSMichal Simek    /* Disable interrupts, we are now committed to the state restore */
825ca54502bSMichal Simek	disable_irq
826ca54502bSMichal Simek	swi	r0, r0, PER_CPU(KM); /* MS: Now officially in user state. */
8278633bebcSMichal Simek	swi	CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE);
828ca54502bSMichal Simek	VM_OFF;
829ca54502bSMichal Simek	tophys(r1,r1);
830ca54502bSMichal Simek	lwi	r3, r1, PTO + PT_R3; /* MS: restore saved r3, r4 registers */
831ca54502bSMichal Simek	lwi	r4, r1, PTO + PT_R4;
832ca54502bSMichal Simek	RESTORE_REGS
833ca54502bSMichal Simek	addik	r1, r1, STATE_SAVE_SIZE /* MS: Clean up stack space. */
834ca54502bSMichal Simek	lwi	r1, r1, PT_R1 - PT_SIZE;
835ca54502bSMichal Simek	bri	6f;
836ca54502bSMichal Simek/* MS: Return to kernel state. */
83777753790SMichal Simek2:
83877753790SMichal Simek#ifdef CONFIG_PREEMPT
839b1d70c62SMichal Simek	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;
84077753790SMichal Simek	/* MS: get preempt_count from thread info */
84177753790SMichal Simek	lwi	r5, r11, TI_PREEMPT_COUNT;
84277753790SMichal Simek	bgti	r5, restore;
84377753790SMichal Simek
84477753790SMichal Simek	lwi	r5, r11, TI_FLAGS;		/* get flags in thread info */
84577753790SMichal Simek	andi	r5, r5, _TIF_NEED_RESCHED;
84677753790SMichal Simek	beqi	r5, restore /* if zero jump over */
84777753790SMichal Simek
84877753790SMichal Simekpreempt:
84977753790SMichal Simek	/* interrupts are off that's why I am calling preempt_chedule_irq */
85077753790SMichal Simek	bralid	r15, preempt_schedule_irq
85177753790SMichal Simek	nop
852b1d70c62SMichal Simek	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;	/* get thread info */
85377753790SMichal Simek	lwi	r5, r11, TI_FLAGS;		/* get flags in thread info */
85477753790SMichal Simek	andi	r5, r5, _TIF_NEED_RESCHED;
85577753790SMichal Simek	bnei	r5, preempt /* if non zero jump to resched */
85677753790SMichal Simekrestore:
85777753790SMichal Simek#endif
85877753790SMichal Simek	VM_OFF /* MS: turn off MMU */
859ca54502bSMichal Simek	tophys(r1,r1)
860ca54502bSMichal Simek	lwi	r3, r1, PTO + PT_R3; /* MS: restore saved r3, r4 registers */
861ca54502bSMichal Simek	lwi	r4, r1, PTO + PT_R4;
862ca54502bSMichal Simek	RESTORE_REGS
863ca54502bSMichal Simek	addik	r1, r1, STATE_SAVE_SIZE	/* MS: Clean up stack space. */
864ca54502bSMichal Simek	tovirt(r1,r1);
865ca54502bSMichal Simek6:
866ca54502bSMichal SimekIRQ_return: /* MS: Make global symbol for debugging */
867ca54502bSMichal Simek	rtid	r14, 0
868ca54502bSMichal Simek	nop
869ca54502bSMichal Simek
870ca54502bSMichal Simek/*
871ca54502bSMichal Simek * `Debug' trap
872ca54502bSMichal Simek *  We enter dbtrap in "BIP" (breakpoint) mode.
873ca54502bSMichal Simek *  So we exit the breakpoint mode with an 'rtbd' and proceed with the
874ca54502bSMichal Simek *  original dbtrap.
875ca54502bSMichal Simek *  however, wait to save state first
876ca54502bSMichal Simek */
877ca54502bSMichal SimekC_ENTRY(_debug_exception):
878ca54502bSMichal Simek	/* BIP bit is set on entry, no interrupts can occur */
879ca54502bSMichal Simek	swi	r1, r0, TOPHYS(PER_CPU(ENTRY_SP))
880ca54502bSMichal Simek
881ca54502bSMichal Simek	swi	r11, r0, TOPHYS(r0_ram + PTO + PT_R11); /* Save r11 */
882ca54502bSMichal Simek	set_bip;	/*equalize initial state for all possible entries*/
883ca54502bSMichal Simek	clear_eip;
884ca54502bSMichal Simek	enable_irq;
885ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(KM));/* See if already in kernel mode.*/
886ca54502bSMichal Simek	beqi	r11, 1f;		/* Jump ahead if coming from user */
887ca54502bSMichal Simek	/* Kernel-mode state save.  */
888ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(ENTRY_SP)); /* Reload kernel stack-ptr*/
889ca54502bSMichal Simek	tophys(r1,r11);
890ca54502bSMichal Simek	swi	r11, r1, (PT_R1-PT_SIZE); /* Save original SP. */
891ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(r0_ram + PTO + PT_R11); /* restore r11 */
892ca54502bSMichal Simek
893ca54502bSMichal Simek	addik	r1, r1, -STATE_SAVE_SIZE; /* Make room on the stack.  */
894ca54502bSMichal Simek	swi	r3, r1, PTO + PT_R3;
895ca54502bSMichal Simek	swi	r4, r1, PTO + PT_R4;
896ca54502bSMichal Simek	SAVE_REGS;
897ca54502bSMichal Simek
898ca54502bSMichal Simek	addi	r11, r0, 1; 		/* Was in kernel-mode.  */
899ca54502bSMichal Simek	swi	r11, r1, PTO + PT_MODE;
900ca54502bSMichal Simek	brid	2f;
901ca54502bSMichal Simek	nop;				/* Fill delay slot */
902ca54502bSMichal Simek1:      /* User-mode state save.  */
903ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(r0_ram + PTO + PT_R11); /* restore r11 */
904ca54502bSMichal Simek	lwi	r1, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); /* get saved current */
905ca54502bSMichal Simek	tophys(r1,r1);
906ca54502bSMichal Simek	lwi	r1, r1, TS_THREAD_INFO;	/* get the thread info */
907ca54502bSMichal Simek	addik	r1, r1, THREAD_SIZE;	/* calculate kernel stack pointer */
908ca54502bSMichal Simek	tophys(r1,r1);
909ca54502bSMichal Simek
910ca54502bSMichal Simek	addik	r1, r1, -STATE_SAVE_SIZE; /* Make room on the stack.  */
911ca54502bSMichal Simek	swi	r3, r1, PTO + PT_R3;
912ca54502bSMichal Simek	swi	r4, r1, PTO + PT_R4;
913ca54502bSMichal Simek	SAVE_REGS;
914ca54502bSMichal Simek
915ca54502bSMichal Simek	swi	r0, r1, PTO+PT_MODE; /* Was in user-mode.  */
916ca54502bSMichal Simek	lwi	r11, r0, TOPHYS(PER_CPU(ENTRY_SP));
917ca54502bSMichal Simek	swi	r11, r1, PTO+PT_R1; /* Store user SP.  */
918ca54502bSMichal Simek	addi	r11, r0, 1;
919ca54502bSMichal Simek	swi	r11, r0, TOPHYS(PER_CPU(KM));	/* Now we're in kernel-mode.  */
920b1d70c62SMichal Simek2:	lwi	CURRENT_TASK, r0, TOPHYS(PER_CPU(CURRENT_SAVE));
921ca54502bSMichal Simek	/* Save away the syscall number.  */
922ca54502bSMichal Simek	swi	r0, r1, PTO+PT_R0;
923ca54502bSMichal Simek	tovirt(r1,r1)
924ca54502bSMichal Simek
925ca54502bSMichal Simek	addi	r5, r0, SIGTRAP		     /* send the trap signal */
926ca54502bSMichal Simek	add	r6, r0, CURRENT_TASK; /* Get current task ptr into r11 */
927ca54502bSMichal Simek	addk	r7, r0, r0		     /* 3rd param zero */
928ca54502bSMichal Simek
929ca54502bSMichal Simek	set_vms;
930ca54502bSMichal Simek	la	r11, r0, send_sig;
931ca54502bSMichal Simek	la	r15, r0, dbtrap_call;
932ca54502bSMichal Simekdbtrap_call:	rtbd	r11, 0;
933ca54502bSMichal Simek	nop;
934ca54502bSMichal Simek
935ca54502bSMichal Simek	set_bip;			/*  Ints masked for state restore*/
936ca54502bSMichal Simek	lwi	r11, r1, PTO+PT_MODE;
937ca54502bSMichal Simek	bnei	r11, 2f;
938ca54502bSMichal Simek
939ca54502bSMichal Simek	/* Get current task ptr into r11 */
940b1d70c62SMichal Simek	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;	/* get thread info */
941ca54502bSMichal Simek	lwi	r11, r11, TI_FLAGS;	/* get flags in thread info */
942ca54502bSMichal Simek	andi	r11, r11, _TIF_NEED_RESCHED;
943ca54502bSMichal Simek	beqi	r11, 5f;
944ca54502bSMichal Simek
945ca54502bSMichal Simek/* Call the scheduler before returning from a syscall/trap. */
946ca54502bSMichal Simek
947ca54502bSMichal Simek	bralid	r15, schedule;	/* Call scheduler */
948ca54502bSMichal Simek	nop;				/* delay slot */
949ca54502bSMichal Simek	/* XXX Is PT_DTRACE handling needed here? */
950ca54502bSMichal Simek	/* XXX m68knommu also checks TASK_STATE & TASK_COUNTER here.  */
951ca54502bSMichal Simek
952ca54502bSMichal Simek	/* Maybe handle a signal */
953b1d70c62SMichal Simek5:	lwi	r11, CURRENT_TASK, TS_THREAD_INFO;	/* get thread info */
954ca54502bSMichal Simek	lwi	r11, r11, TI_FLAGS;	/* get flags in thread info */
955ca54502bSMichal Simek	andi	r11, r11, _TIF_SIGPENDING;
956ca54502bSMichal Simek	beqi	r11, 1f;		/* Signals to handle, handle them */
957ca54502bSMichal Simek
958ca54502bSMichal Simek/* Handle a signal return; Pending signals should be in r18.  */
959ca54502bSMichal Simek	/* Not all registers are saved by the normal trap/interrupt entry
960ca54502bSMichal Simek	   points (for instance, call-saved registers (because the normal
961ca54502bSMichal Simek	   C-compiler calling sequence in the kernel makes sure they're
962ca54502bSMichal Simek	   preserved), and call-clobbered registers in the case of
963ca54502bSMichal Simek	   traps), but signal handlers may want to examine or change the
964ca54502bSMichal Simek	   complete register state.  Here we save anything not saved by
965ca54502bSMichal Simek	   the normal entry sequence, so that it may be safely restored
966ca54502bSMichal Simek	   (in a possibly modified form) after do_signal returns.  */
967ca54502bSMichal Simek
968ca54502bSMichal Simek	la	r5, r1, PTO;		/* Arg 1: struct pt_regs *regs */
969ca54502bSMichal Simek	addi  r7, r0, 0;	/* Arg 3: int in_syscall */
970ca54502bSMichal Simek	bralid	r15, do_signal;	/* Handle any signals */
971841d6e8cSMichal Simek	add	r6, r0, r0;		/* Arg 2: sigset_t *oldset */
972ca54502bSMichal Simek
973ca54502bSMichal Simek
974ca54502bSMichal Simek/* Finally, return to user state.  */
975ca54502bSMichal Simek1:	swi	r0, r0, PER_CPU(KM);	/* Now officially in user state. */
9768633bebcSMichal Simek	swi	CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE); /* save current */
977ca54502bSMichal Simek	VM_OFF;
978ca54502bSMichal Simek	tophys(r1,r1);
979ca54502bSMichal Simek
980ca54502bSMichal Simek	lwi	r3, r1, PTO+PT_R3; /* restore saved r3, r4 registers */
981ca54502bSMichal Simek	lwi	r4, r1, PTO+PT_R4;
982ca54502bSMichal Simek	RESTORE_REGS
983ca54502bSMichal Simek	addik	r1, r1, STATE_SAVE_SIZE		/* Clean up stack space.  */
984ca54502bSMichal Simek
985ca54502bSMichal Simek
986ca54502bSMichal Simek	lwi	r1, r1, PT_R1 - PT_SIZE;
987ca54502bSMichal Simek					/* Restore user stack pointer. */
988ca54502bSMichal Simek	bri	6f;
989ca54502bSMichal Simek
990ca54502bSMichal Simek/* Return to kernel state.  */
991ca54502bSMichal Simek2:	VM_OFF;
992ca54502bSMichal Simek	tophys(r1,r1);
993ca54502bSMichal Simek	lwi	r3, r1, PTO+PT_R3; /* restore saved r3, r4 registers */
994ca54502bSMichal Simek	lwi	r4, r1, PTO+PT_R4;
995ca54502bSMichal Simek	RESTORE_REGS
996ca54502bSMichal Simek	addik	r1, r1, STATE_SAVE_SIZE		/* Clean up stack space.  */
997ca54502bSMichal Simek
998ca54502bSMichal Simek	tovirt(r1,r1);
999ca54502bSMichal Simek6:
1000ca54502bSMichal SimekDBTRAP_return:		/* Make global symbol for debugging */
1001ca54502bSMichal Simek	rtbd	r14, 0;	/* Instructions to return from an IRQ */
1002ca54502bSMichal Simek	nop;
1003ca54502bSMichal Simek
1004ca54502bSMichal Simek
1005ca54502bSMichal Simek
1006ca54502bSMichal SimekENTRY(_switch_to)
1007ca54502bSMichal Simek	/* prepare return value */
1008b1d70c62SMichal Simek	addk	r3, r0, CURRENT_TASK
1009ca54502bSMichal Simek
1010ca54502bSMichal Simek	/* save registers in cpu_context */
1011ca54502bSMichal Simek	/* use r11 and r12, volatile registers, as temp register */
1012ca54502bSMichal Simek	/* give start of cpu_context for previous process */
1013ca54502bSMichal Simek	addik	r11, r5, TI_CPU_CONTEXT
1014ca54502bSMichal Simek	swi	r1, r11, CC_R1
1015ca54502bSMichal Simek	swi	r2, r11, CC_R2
1016ca54502bSMichal Simek	/* skip volatile registers.
1017ca54502bSMichal Simek	 * they are saved on stack when we jumped to _switch_to() */
1018ca54502bSMichal Simek	/* dedicated registers */
1019ca54502bSMichal Simek	swi	r13, r11, CC_R13
1020ca54502bSMichal Simek	swi	r14, r11, CC_R14
1021ca54502bSMichal Simek	swi	r15, r11, CC_R15
1022ca54502bSMichal Simek	swi	r16, r11, CC_R16
1023ca54502bSMichal Simek	swi	r17, r11, CC_R17
1024ca54502bSMichal Simek	swi	r18, r11, CC_R18
1025ca54502bSMichal Simek	/* save non-volatile registers */
1026ca54502bSMichal Simek	swi	r19, r11, CC_R19
1027ca54502bSMichal Simek	swi	r20, r11, CC_R20
1028ca54502bSMichal Simek	swi	r21, r11, CC_R21
1029ca54502bSMichal Simek	swi	r22, r11, CC_R22
1030ca54502bSMichal Simek	swi	r23, r11, CC_R23
1031ca54502bSMichal Simek	swi	r24, r11, CC_R24
1032ca54502bSMichal Simek	swi	r25, r11, CC_R25
1033ca54502bSMichal Simek	swi	r26, r11, CC_R26
1034ca54502bSMichal Simek	swi	r27, r11, CC_R27
1035ca54502bSMichal Simek	swi	r28, r11, CC_R28
1036ca54502bSMichal Simek	swi	r29, r11, CC_R29
1037ca54502bSMichal Simek	swi	r30, r11, CC_R30
1038ca54502bSMichal Simek	/* special purpose registers */
1039ca54502bSMichal Simek	mfs	r12, rmsr
1040ca54502bSMichal Simek	nop
1041ca54502bSMichal Simek	swi	r12, r11, CC_MSR
1042ca54502bSMichal Simek	mfs	r12, rear
1043ca54502bSMichal Simek	nop
1044ca54502bSMichal Simek	swi	r12, r11, CC_EAR
1045ca54502bSMichal Simek	mfs	r12, resr
1046ca54502bSMichal Simek	nop
1047ca54502bSMichal Simek	swi	r12, r11, CC_ESR
1048ca54502bSMichal Simek	mfs	r12, rfsr
1049ca54502bSMichal Simek	nop
1050ca54502bSMichal Simek	swi	r12, r11, CC_FSR
1051ca54502bSMichal Simek
1052b1d70c62SMichal Simek	/* update r31, the current-give me pointer to task which will be next */
1053b1d70c62SMichal Simek	lwi	CURRENT_TASK, r6, TI_TASK
1054ca54502bSMichal Simek	/* stored it to current_save too */
1055b1d70c62SMichal Simek	swi	CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE)
1056ca54502bSMichal Simek
1057ca54502bSMichal Simek	/* get new process' cpu context and restore */
1058ca54502bSMichal Simek	/* give me start where start context of next task */
1059ca54502bSMichal Simek	addik	r11, r6, TI_CPU_CONTEXT
1060ca54502bSMichal Simek
1061ca54502bSMichal Simek	/* non-volatile registers */
1062ca54502bSMichal Simek	lwi	r30, r11, CC_R30
1063ca54502bSMichal Simek	lwi	r29, r11, CC_R29
1064ca54502bSMichal Simek	lwi	r28, r11, CC_R28
1065ca54502bSMichal Simek	lwi	r27, r11, CC_R27
1066ca54502bSMichal Simek	lwi	r26, r11, CC_R26
1067ca54502bSMichal Simek	lwi	r25, r11, CC_R25
1068ca54502bSMichal Simek	lwi	r24, r11, CC_R24
1069ca54502bSMichal Simek	lwi	r23, r11, CC_R23
1070ca54502bSMichal Simek	lwi	r22, r11, CC_R22
1071ca54502bSMichal Simek	lwi	r21, r11, CC_R21
1072ca54502bSMichal Simek	lwi	r20, r11, CC_R20
1073ca54502bSMichal Simek	lwi	r19, r11, CC_R19
1074ca54502bSMichal Simek	/* dedicated registers */
1075ca54502bSMichal Simek	lwi	r18, r11, CC_R18
1076ca54502bSMichal Simek	lwi	r17, r11, CC_R17
1077ca54502bSMichal Simek	lwi	r16, r11, CC_R16
1078ca54502bSMichal Simek	lwi	r15, r11, CC_R15
1079ca54502bSMichal Simek	lwi	r14, r11, CC_R14
1080ca54502bSMichal Simek	lwi	r13, r11, CC_R13
1081ca54502bSMichal Simek	/* skip volatile registers */
1082ca54502bSMichal Simek	lwi	r2, r11, CC_R2
1083ca54502bSMichal Simek	lwi	r1, r11, CC_R1
1084ca54502bSMichal Simek
1085ca54502bSMichal Simek	/* special purpose registers */
1086ca54502bSMichal Simek	lwi	r12, r11, CC_FSR
1087ca54502bSMichal Simek	mts	rfsr, r12
1088ca54502bSMichal Simek	nop
1089ca54502bSMichal Simek	lwi	r12, r11, CC_MSR
1090ca54502bSMichal Simek	mts	rmsr, r12
1091ca54502bSMichal Simek	nop
1092ca54502bSMichal Simek
1093ca54502bSMichal Simek	rtsd	r15, 8
1094ca54502bSMichal Simek	nop
1095ca54502bSMichal Simek
1096ca54502bSMichal SimekENTRY(_reset)
1097ca54502bSMichal Simek	brai	0x70; /* Jump back to FS-boot */
1098ca54502bSMichal Simek
1099ca54502bSMichal SimekENTRY(_break)
1100ca54502bSMichal Simek	mfs	r5, rmsr
1101ca54502bSMichal Simek	nop
1102ca54502bSMichal Simek	swi	r5, r0, 0x250 + TOPHYS(r0_ram)
1103ca54502bSMichal Simek	mfs	r5, resr
1104ca54502bSMichal Simek	nop
1105ca54502bSMichal Simek	swi	r5, r0, 0x254 + TOPHYS(r0_ram)
1106ca54502bSMichal Simek	bri	0
1107ca54502bSMichal Simek
1108ca54502bSMichal Simek	/* These are compiled and loaded into high memory, then
1109ca54502bSMichal Simek	 * copied into place in mach_early_setup */
1110ca54502bSMichal Simek	.section	.init.ivt, "ax"
1111ca54502bSMichal Simek	.org	0x0
1112ca54502bSMichal Simek	/* this is very important - here is the reset vector */
1113ca54502bSMichal Simek	/* in current MMU branch you don't care what is here - it is
1114ca54502bSMichal Simek	 * used from bootloader site - but this is correct for FS-BOOT */
1115ca54502bSMichal Simek	brai	0x70
1116ca54502bSMichal Simek	nop
1117ca54502bSMichal Simek	brai	TOPHYS(_user_exception); /* syscall handler */
1118ca54502bSMichal Simek	brai	TOPHYS(_interrupt);	/* Interrupt handler */
1119ca54502bSMichal Simek	brai	TOPHYS(_break);		/* nmi trap handler */
1120ca54502bSMichal Simek	brai	TOPHYS(_hw_exception_handler);	/* HW exception handler */
1121ca54502bSMichal Simek
1122ca54502bSMichal Simek	.org	0x60
1123ca54502bSMichal Simek	brai	TOPHYS(_debug_exception);	/* debug trap handler*/
1124ca54502bSMichal Simek
1125ca54502bSMichal Simek.section .rodata,"a"
1126ca54502bSMichal Simek#include "syscall_table.S"
1127ca54502bSMichal Simek
1128ca54502bSMichal Simeksyscall_table_size=(.-sys_call_table)
1129ca54502bSMichal Simek
1130*ce3266c0SSteven J. Magnanitype_SYSCALL:
1131*ce3266c0SSteven J. Magnani	.ascii "SYSCALL\0"
1132*ce3266c0SSteven J. Magnanitype_IRQ:
1133*ce3266c0SSteven J. Magnani	.ascii "IRQ\0"
1134*ce3266c0SSteven J. Magnanitype_IRQ_PREEMPT:
1135*ce3266c0SSteven J. Magnani	.ascii "IRQ (PREEMPTED)\0"
1136*ce3266c0SSteven J. Magnanitype_SYSCALL_PREEMPT:
1137*ce3266c0SSteven J. Magnani	.ascii " SYSCALL (PREEMPTED)\0"
1138*ce3266c0SSteven J. Magnani
1139*ce3266c0SSteven J. Magnani	/*
1140*ce3266c0SSteven J. Magnani	 * Trap decoding for stack unwinder
1141*ce3266c0SSteven J. Magnani	 * Tuples are (start addr, end addr, string)
1142*ce3266c0SSteven J. Magnani	 * If return address lies on [start addr, end addr],
1143*ce3266c0SSteven J. Magnani	 * unwinder displays 'string'
1144*ce3266c0SSteven J. Magnani	 */
1145*ce3266c0SSteven J. Magnani
1146*ce3266c0SSteven J. Magnani	.align 4
1147*ce3266c0SSteven J. Magnani.global microblaze_trap_handlers
1148*ce3266c0SSteven J. Magnanimicroblaze_trap_handlers:
1149*ce3266c0SSteven J. Magnani	/* Exact matches come first */
1150*ce3266c0SSteven J. Magnani	.word ret_from_trap; .word ret_from_trap   ; .word type_SYSCALL
1151*ce3266c0SSteven J. Magnani	.word ret_from_irq ; .word ret_from_irq    ; .word type_IRQ
1152*ce3266c0SSteven J. Magnani	/* Fuzzy matches go here */
1153*ce3266c0SSteven J. Magnani	.word ret_from_irq ; .word no_intr_resched ; .word type_IRQ_PREEMPT
1154*ce3266c0SSteven J. Magnani	.word ret_from_trap; .word TRAP_return     ; .word type_SYSCALL_PREEMPT
1155*ce3266c0SSteven J. Magnani	/* End of table */
1156*ce3266c0SSteven J. Magnani	.word 0               ; .word 0               ; .word 0
1157