xref: /openbsd/sys/arch/sh/sh/locore_c.c (revision 8bff8a49)
1 /*	$OpenBSD: locore_c.c,v 1.14 2023/01/06 19:10:18 miod Exp $	*/
2 /*	$NetBSD: locore_c.c,v 1.13 2006/03/04 01:13:35 uwe Exp $	*/
3 
4 /*-
5  * Copyright (c) 1996, 1997, 2002 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*-
35  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
36  * All rights reserved.
37  *
38  * This code is derived from software contributed to Berkeley by
39  * William Jolitz.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)Locore.c
66  */
67 
68 /*-
69  * Copyright (c) 1993, 1994, 1995, 1996, 1997
70  *	 Charles M. Hannum.  All rights reserved.
71  * Copyright (c) 1992 Terrence R. Lambert.
72  *
73  * This code is derived from software contributed to Berkeley by
74  * William Jolitz.
75  *
76  * Redistribution and use in source and binary forms, with or without
77  * modification, are permitted provided that the following conditions
78  * are met:
79  * 1. Redistributions of source code must retain the above copyright
80  *    notice, this list of conditions and the following disclaimer.
81  * 2. Redistributions in binary form must reproduce the above copyright
82  *    notice, this list of conditions and the following disclaimer in the
83  *    documentation and/or other materials provided with the distribution.
84  * 3. All advertising materials mentioning features or use of this software
85  *    must display the following acknowledgement:
86  *	This product includes software developed by the University of
87  *	California, Berkeley and its contributors.
88  * 4. Neither the name of the University nor the names of its contributors
89  *    may be used to endorse or promote products derived from this software
90  *    without specific prior written permission.
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
93  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
96  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
97  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
98  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
102  * SUCH DAMAGE.
103  *
104  *	@(#)Locore.c
105  */
106 
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/proc.h>
110 #include <sys/sched.h>
111 
112 #include <uvm/uvm_extern.h>
113 
114 #include <sh/locore.h>
115 #include <sh/cpu.h>
116 #include <sh/pcb.h>
117 #include <sh/pmap.h>
118 #include <sh/mmu_sh3.h>
119 #include <sh/mmu_sh4.h>
120 #include <sh/ubcreg.h>
121 
122 void (*__sh_switch_resume)(struct proc *);
123 void cpu_switch_prepare(struct proc *, struct proc *);
124 
125 /*
126  * Prepare context switch from oproc to nproc.
127  * This code is used by cpu_switchto.
128  */
129 void
cpu_switch_prepare(struct proc * oproc,struct proc * nproc)130 cpu_switch_prepare(struct proc *oproc, struct proc *nproc)
131 {
132 	nproc->p_stat = SONPROC;
133 
134 	if (oproc && (oproc->p_md.md_flags & MDP_STEP))
135 		_reg_write_2(SH_(BBRB), 0);
136 
137 	curpcb = nproc->p_md.md_pcb;
138 	pmap_activate(nproc);
139 
140 	if (nproc->p_md.md_flags & MDP_STEP) {
141 		int pm_asid = nproc->p_vmspace->vm_map.pmap->pm_asid;
142 
143 		_reg_write_2(SH_(BBRB), 0);
144 		_reg_write_4(SH_(BARB), nproc->p_md.md_regs->tf_spc);
145 		_reg_write_1(SH_(BASRB), pm_asid);
146 		_reg_write_1(SH_(BAMRB), 0);
147 		_reg_write_2(SH_(BRCR), 0x0040);
148 		_reg_write_2(SH_(BBRB), 0x0014);
149 	}
150 
151 	curproc = nproc;
152 }
153 
154 void
cpu_exit(struct proc * p)155 cpu_exit(struct proc *p)
156 {
157 	if (p->p_md.md_flags & MDP_STEP)
158 		_reg_write_2(SH_(BBRB), 0);
159 
160 	pmap_deactivate(p);
161 	sched_exit(p);
162 }
163 
164 #ifndef P1_STACK
165 #ifdef SH3
166 /*
167  * void sh3_switch_setup(struct proc *p):
168  *	prepare kernel stack PTE table. TLB miss handler check these.
169  */
170 void
sh3_switch_setup(struct proc * p)171 sh3_switch_setup(struct proc *p)
172 {
173 	pt_entry_t *pte;
174 	struct md_upte *md_upte = p->p_md.md_upte;
175 	uint32_t vpn;
176 	int i;
177 
178 	vpn = (uint32_t)p->p_addr;
179 	vpn &= ~PGOFSET;
180 	for (i = 0; i < UPAGES; i++, vpn += PAGE_SIZE, md_upte++) {
181 		pte = __pmap_kpte_lookup(vpn);
182 		KDASSERT(pte && *pte != 0);
183 
184 		md_upte->addr = vpn;
185 		md_upte->data = (*pte & PG_HW_BITS) | PG_D | PG_V;
186 	}
187 }
188 #endif /* SH3 */
189 
190 #ifdef SH4
191 /*
192  * void sh4_switch_setup(struct proc *p):
193  *	prepare kernel stack PTE table. sh4_switch_resume wired this PTE.
194  */
195 void
sh4_switch_setup(struct proc * p)196 sh4_switch_setup(struct proc *p)
197 {
198 	pt_entry_t *pte;
199 	struct md_upte *md_upte = p->p_md.md_upte;
200 	uint32_t vpn;
201 	int i, e;
202 
203 	vpn = (uint32_t)p->p_addr;
204 	vpn &= ~PGOFSET;
205 	e = SH4_UTLB_ENTRY - UPAGES;
206 	for (i = 0; i < UPAGES; i++, e++, vpn += PAGE_SIZE) {
207 		pte = __pmap_kpte_lookup(vpn);
208 		KDASSERT(pte && *pte != 0);
209 		/* Address array */
210 		md_upte->addr = SH4_UTLB_AA | (e << SH4_UTLB_E_SHIFT);
211 		md_upte->data = vpn | SH4_UTLB_AA_D | SH4_UTLB_AA_V;
212 		md_upte++;
213 		/* Data array */
214 		md_upte->addr = SH4_UTLB_DA1 | (e << SH4_UTLB_E_SHIFT);
215 		md_upte->data = (*pte & PG_HW_BITS) |
216 		    SH4_UTLB_DA1_D | SH4_UTLB_DA1_V;
217 		md_upte++;
218 	}
219 }
220 #endif /* SH4 */
221 #endif /* !P1_STACK */
222