1176771Sraj/*-
2176771Sraj * Copyright (C) 2001 Benno Rice
3176771Sraj * All rights reserved.
4176771Sraj *
5176771Sraj * Redistribution and use in source and binary forms, with or without
6176771Sraj * modification, are permitted provided that the following conditions
7176771Sraj * are met:
8176771Sraj * 1. Redistributions of source code must retain the above copyright
9176771Sraj *    notice, this list of conditions and the following disclaimer.
10176771Sraj * 2. Redistributions in binary form must reproduce the above copyright
11176771Sraj *    notice, this list of conditions and the following disclaimer in the
12176771Sraj *    documentation and/or other materials provided with the distribution.
13176771Sraj *
14176771Sraj * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15176771Sraj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16176771Sraj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17176771Sraj * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18176771Sraj * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19176771Sraj * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20176771Sraj * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21176771Sraj * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22176771Sraj * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23176771Sraj * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24176771Sraj *
25176771Sraj *	from: $NetBSD: locore.S,v 1.24 2000/05/31 05:09:17 thorpej Exp $
26176771Sraj *	from: $FreeBSD$
27176771Sraj *
28176771Sraj * $FreeBSD$
29176771Sraj */
30176771Sraj/*-
31176771Sraj * Copyright (C) 1995, 1996 Wolfgang Solfrank.
32176771Sraj * Copyright (C) 1995, 1996 TooLs GmbH.
33176771Sraj * All rights reserved.
34176771Sraj *
35176771Sraj * Redistribution and use in source and binary forms, with or without
36176771Sraj * modification, are permitted provided that the following conditions
37176771Sraj * are met:
38176771Sraj * 1. Redistributions of source code must retain the above copyright
39176771Sraj *    notice, this list of conditions and the following disclaimer.
40176771Sraj * 2. Redistributions in binary form must reproduce the above copyright
41176771Sraj *    notice, this list of conditions and the following disclaimer in the
42176771Sraj *    documentation and/or other materials provided with the distribution.
43176771Sraj * 3. All advertising materials mentioning features or use of this software
44176771Sraj *    must display the following acknowledgement:
45176771Sraj *	This product includes software developed by TooLs GmbH.
46176771Sraj * 4. The name of TooLs GmbH may not be used to endorse or promote products
47176771Sraj *    derived from this software without specific prior written permission.
48176771Sraj *
49176771Sraj * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
50176771Sraj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51176771Sraj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52176771Sraj * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53176771Sraj * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54176771Sraj * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
55176771Sraj * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56176771Sraj * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57176771Sraj * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58176771Sraj * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59176771Sraj */
60176771Sraj
61176771Sraj#include "assym.s"
62176771Sraj
63176771Sraj#include <sys/syscall.h>
64176771Sraj
65176771Sraj#include <machine/trap.h>
66176771Sraj#include <machine/param.h>
67176771Sraj#include <machine/asm.h>
68189100Sraj#include <machine/spr.h>
69176771Sraj
70176771Sraj/*
71191376Sraj * void cpu_throw(struct thread *old, struct thread *new)
72191376Sraj */
73191376SrajENTRY(cpu_throw)
74191376Sraj	mr	%r15, %r4
75191376Sraj	b	cpu_switchin
76191376Sraj
77191376Sraj/*
78191376Sraj * void cpu_switch(struct thread *old, struct thread *new, struct mutex *mtx);)
79176771Sraj *
80176771Sraj * Switch to a new thread saving the current state in the old thread.
81176771Sraj */
82176771SrajENTRY(cpu_switch)
83191376Sraj	stw	%r5, TD_LOCK(%r3)
84191376Sraj
85176771Sraj	lwz	%r5, TD_PCB(%r3)	/* Get the old thread's PCB ptr */
86176771Sraj
87176771Sraj	mr	%r12, %r2
88176771Sraj	stmw	%r12, PCB_CONTEXT(%r5)	/* Save the non-volatile GP regs.
89176771Sraj					   These can now be used for scratch */
90176771Sraj
91176771Sraj	mfcr	%r16			/* Save the condition register */
92176771Sraj	stw	%r16, PCB_CR(%r5)
93176771Sraj	mflr	%r16			/* Save the link register */
94176771Sraj	stw	%r16, PCB_LR(%r5)
95176771Sraj	mfctr	%r16
96176771Sraj	stw	%r16, PCB_BOOKE_CTR(%r5)
97176771Sraj	mfxer	%r16
98176771Sraj	stw	%r16, PCB_BOOKE_XER(%r5)
99189100Sraj	mfspr	%r16, SPR_DBCR0
100189100Sraj	stw	%r16, PCB_BOOKE_DBCR0(%r5)
101176771Sraj
102176771Sraj	stw	%r1, PCB_SP(%r5)	/* Save the stack pointer */
103176771Sraj
104176771Sraj	mr	%r14, %r3		/* Copy the old thread ptr... */
105176771Sraj	mr	%r15, %r4		/* and the new thread ptr in scratch */
106176771Sraj
107176771Sraj	bl	pmap_deactivate		/* Deactivate the current pmap */
108176771Sraj
109191376Srajcpu_switchin:
110176771Sraj	mr	%r3, %r15		/* Get new thread ptr */
111176771Sraj	bl	pmap_activate		/* Activate the new address space */
112176771Sraj
113176771Sraj	mfsprg	%r7, 0			/* Get the pcpu pointer */
114176771Sraj	stw	%r15, PC_CURTHREAD(%r7)	/* Store new current thread */
115176771Sraj	lwz	%r17, TD_PCB(%r15)	/* Store new current PCB */
116176771Sraj	stw	%r17, PC_CURPCB(%r7)
117176771Sraj
118176771Sraj	mr	%r3, %r17		/* Recover PCB ptr */
119176771Sraj	lmw	%r12, PCB_CONTEXT(%r3)	/* Load the non-volatile GP regs */
120176771Sraj	mr	%r2, %r12
121176771Sraj	lwz	%r5, PCB_CR(%r3)	/* Load the condition register */
122176771Sraj	mtcr	%r5
123176771Sraj	lwz	%r5, PCB_LR(%r3)	/* Load the link register */
124176771Sraj	mtlr	%r5
125176771Sraj	lwz	%r5, PCB_BOOKE_CTR(%r3)
126176771Sraj	mtctr	%r5
127176771Sraj	lwz	%r5, PCB_BOOKE_XER(%r3)
128176771Sraj	mtxer	%r5
129189100Sraj	lwz	%r5, PCB_BOOKE_DBCR0(%r3)
130189100Sraj	mtspr	SPR_DBCR0, %r5
131176771Sraj
132176771Sraj	lwz	%r1, PCB_SP(%r3)	/* Load the stack pointer */
133176771Sraj	blr
134176771Sraj
135176771Sraj/*
136176771Sraj * savectx(pcb)
137176771Sraj * Update pcb, saving current processor state
138176771Sraj */
139176771SrajENTRY(savectx)
140191378Sraj	mr	%r12, %r2
141191378Sraj	stmw	%r12, PCB_CONTEXT(%r3)	/* Save the non-volatile GP regs */
142176771Sraj	mfcr	%r4			/* Save the condition register */
143191378Sraj	stw	%r4, PCB_CONTEXT(%r3)
144176771Sraj	blr
145176771Sraj
146176771Sraj/*
147176771Sraj * fork_trampoline()
148176771Sraj * Set up the return from cpu_fork()
149176771Sraj */
150176771SrajENTRY(fork_trampoline)
151191378Sraj	lwz	%r3, CF_FUNC(%r1)
152191378Sraj	lwz	%r4, CF_ARG0(%r1)
153191378Sraj	lwz	%r5, CF_ARG1(%r1)
154176771Sraj	bl	fork_exit
155191378Sraj	addi	%r1, %r1, CF_SIZE-FSP	/* Allow 8 bytes in front of
156176771Sraj					   trapframe to simulate FRAME_SETUP
157176771Sraj					   does when allocating space for
158176771Sraj					   a frame pointer/saved LR */
159176771Sraj	b	trapexit
160