xref: /openbsd/sys/arch/sh/sh/locore_c.c (revision e6d26cf8)
1 /*	$OpenBSD: locore_c.c,v 1.9 2010/06/26 23:24:44 guenther 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 #include <sys/proc.h>
112 
113 #include <uvm/uvm.h>
114 
115 #include <sh/locore.h>
116 #include <sh/cpu.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 int want_resched;
125 
126 /*
127  * Prepare context switch from oproc to nproc.
128  * This code is used by cpu_switchto.
129  */
130 void
131 cpu_switch_prepare(struct proc *oproc, struct proc *nproc)
132 {
133 	nproc->p_stat = SONPROC;
134 
135 	if (oproc && (oproc->p_md.md_flags & MDP_STEP))
136 		_reg_write_2(SH_(BBRB), 0);
137 
138 	curpcb = nproc->p_md.md_pcb;
139 	pmap_activate(nproc);
140 
141 	if (nproc->p_md.md_flags & MDP_STEP) {
142 		int pm_asid = nproc->p_vmspace->vm_map.pmap->pm_asid;
143 
144 		_reg_write_2(SH_(BBRB), 0);
145 		_reg_write_4(SH_(BARB), nproc->p_md.md_regs->tf_spc);
146 		_reg_write_1(SH_(BASRB), pm_asid);
147 		_reg_write_1(SH_(BAMRB), 0);
148 		_reg_write_2(SH_(BRCR), 0x0040);
149 		_reg_write_2(SH_(BBRB), 0x0014);
150 	}
151 
152 	curproc = nproc;
153 }
154 
155 void
156 cpu_exit(struct proc *p)
157 {
158 	if (p->p_md.md_flags & MDP_STEP)
159 		_reg_write_2(SH_(BBRB), 0);
160 
161 	pmap_deactivate(p);
162 	sched_exit(p);
163 }
164 
165 #ifndef P1_STACK
166 #ifdef SH3
167 /*
168  * void sh3_switch_setup(struct proc *p):
169  *	prepare kernel stack PTE table. TLB miss handler check these.
170  */
171 void
172 sh3_switch_setup(struct proc *p)
173 {
174 	pt_entry_t *pte;
175 	struct md_upte *md_upte = p->p_md.md_upte;
176 	uint32_t vpn;
177 	int i;
178 
179 	vpn = (uint32_t)p->p_addr;
180 	vpn &= ~PGOFSET;
181 	for (i = 0; i < UPAGES; i++, vpn += PAGE_SIZE, md_upte++) {
182 		pte = __pmap_kpte_lookup(vpn);
183 		KDASSERT(pte && *pte != 0);
184 
185 		md_upte->addr = vpn;
186 		md_upte->data = (*pte & PG_HW_BITS) | PG_D | PG_V;
187 	}
188 }
189 #endif /* SH3 */
190 
191 #ifdef SH4
192 /*
193  * void sh4_switch_setup(struct proc *p):
194  *	prepare kernel stack PTE table. sh4_switch_resume wired this PTE.
195  */
196 void
197 sh4_switch_setup(struct proc *p)
198 {
199 	pt_entry_t *pte;
200 	struct md_upte *md_upte = p->p_md.md_upte;
201 	uint32_t vpn;
202 	int i, e;
203 
204 	vpn = (uint32_t)p->p_addr;
205 	vpn &= ~PGOFSET;
206 	e = SH4_UTLB_ENTRY - UPAGES;
207 	for (i = 0; i < UPAGES; i++, e++, vpn += PAGE_SIZE) {
208 		pte = __pmap_kpte_lookup(vpn);
209 		KDASSERT(pte && *pte != 0);
210 		/* Address array */
211 		md_upte->addr = SH4_UTLB_AA | (e << SH4_UTLB_E_SHIFT);
212 		md_upte->data = vpn | SH4_UTLB_AA_D | SH4_UTLB_AA_V;
213 		md_upte++;
214 		/* Data array */
215 		md_upte->addr = SH4_UTLB_DA1 | (e << SH4_UTLB_E_SHIFT);
216 		md_upte->data = (*pte & PG_HW_BITS) |
217 		    SH4_UTLB_DA1_D | SH4_UTLB_DA1_V;
218 		md_upte++;
219 	}
220 }
221 #endif /* SH4 */
222 #endif /* !P1_STACK */
223 
224 /*
225  * copystr(caddr_t from, caddr_t to, size_t maxlen, size_t *lencopied);
226  * Copy a NUL-terminated string, at most maxlen characters long.  Return the
227  * number of characters copied (including the NUL) in *lencopied.  If the
228  * string is too long, return ENAMETOOLONG; else return 0.
229  */
230 int
231 copystr(const void *kfaddr, void *kdaddr, size_t maxlen, size_t *lencopied)
232 {
233 	const char *from = kfaddr;
234 	char *to = kdaddr;
235 	int i;
236 
237 	for (i = 0; maxlen-- > 0; i++) {
238 		if ((*to++ = *from++) == '\0') {
239 			if (lencopied)
240 				*lencopied = i + 1;
241 			return (0);
242 		}
243 	}
244 
245 	if (lencopied)
246 		*lencopied = i;
247 
248 	return (ENAMETOOLONG);
249 }
250