xref: /netbsd/sys/arch/sh3/sh3/locore_c.c (revision 6550d01e)
1 /*	$NetBSD: locore_c.c,v 1.31 2009/11/27 03:23:13 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997, 2002, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
35  * All rights reserved.
36  *
37  * This code is derived from software contributed to Berkeley by
38  * William Jolitz.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)Locore.c
65  */
66 
67 /*-
68  * Copyright (c) 1993, 1994, 1995, 1996, 1997
69  *	 Charles M. Hannum.  All rights reserved.
70  * Copyright (c) 1992 Terrence R. Lambert.
71  *
72  * This code is derived from software contributed to Berkeley by
73  * William Jolitz.
74  *
75  * Redistribution and use in source and binary forms, with or without
76  * modification, are permitted provided that the following conditions
77  * are met:
78  * 1. Redistributions of source code must retain the above copyright
79  *    notice, this list of conditions and the following disclaimer.
80  * 2. Redistributions in binary form must reproduce the above copyright
81  *    notice, this list of conditions and the following disclaimer in the
82  *    documentation and/or other materials provided with the distribution.
83  * 3. All advertising materials mentioning features or use of this software
84  *    must display the following acknowledgement:
85  *	This product includes software developed by the University of
86  *	California, Berkeley and its contributors.
87  * 4. Neither the name of the University nor the names of its contributors
88  *    may be used to endorse or promote products derived from this software
89  *    without specific prior written permission.
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
92  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
95  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
96  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
97  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
98  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
99  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
100  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
101  * SUCH DAMAGE.
102  *
103  *	@(#)Locore.c
104  */
105 
106 #include <sys/cdefs.h>
107 __KERNEL_RCSID(0, "$NetBSD: locore_c.c,v 1.31 2009/11/27 03:23:13 rmind Exp $");
108 
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/cpu.h>
112 #include <sys/sched.h>
113 #include <sys/proc.h>
114 #include <sys/ras.h>
115 
116 #include <uvm/uvm.h>
117 #include <uvm/uvm_extern.h>
118 
119 #include <sh3/locore.h>
120 #include <sh3/pmap.h>
121 #include <sh3/mmu_sh3.h>
122 #include <sh3/mmu_sh4.h>
123 
124 void cpu_switch_prepare(struct lwp *, struct lwp *);
125 
126 void (*__sh_switch_resume)(struct lwp *);
127 
128 /*
129  * Called from cpu_switchto after olwp's state has been saved.
130  * Prepare context switch to nlwp.
131  */
132 void
133 cpu_switch_prepare(struct lwp *olwp, struct lwp *nlwp)
134 {
135 	struct proc *p = nlwp->l_proc;
136 
137 	/* Check for Restartable Atomic Sequences. */
138 	if (p->p_raslist != NULL) {
139 		void *pc;
140 
141 		pc = ras_lookup(p, (void *)nlwp->l_md.md_regs->tf_spc);
142 		if (pc != (void *) -1)
143 			nlwp->l_md.md_regs->tf_spc = (int) pc;
144 	}
145 }
146 
147 
148 /*
149  * When no processes are on the run queue, wait for something to come
150  * ready.  Separate function for profiling.
151  */
152 void
153 cpu_idle(void)
154 {
155 
156 	__asm volatile("sleep");
157 }
158 
159 
160 #ifndef P1_STACK
161 #ifdef SH3
162 /*
163  * Nothing to do for sh3.
164  */
165 void
166 sh3_switch_setup(struct lwp *l)
167 {
168 
169     return;
170 }
171 #endif /* SH3 */
172 
173 
174 #ifdef SH4
175 /*
176  * Prepare kernel stack PTE table.  sh4_switch_resume wires these PTEs.
177  */
178 void
179 sh4_switch_setup(struct lwp *l)
180 {
181 	struct md_upte *md_upte;
182 	uint32_t vpn;
183 	pt_entry_t *pte;
184 	int i, e;
185 
186 	md_upte = l->l_md.md_upte;
187 	vpn = sh3_trunc_page(uvm_lwp_getuarea(l));
188 	e = SH4_UTLB_ENTRY - UPAGES;
189 
190 	for (i = 0; i < UPAGES; ++i) {
191 		pte = __pmap_kpte_lookup(vpn);
192 		KDASSERT(pte && *pte != 0);
193 
194 		/* Address array */
195 		md_upte->addr = SH4_UTLB_AA | (e << SH4_UTLB_E_SHIFT);
196 		md_upte->data = vpn | SH4_UTLB_AA_D | SH4_UTLB_AA_V;
197 		++md_upte;
198 
199 		/* Data array */
200 		md_upte->addr = SH4_UTLB_DA1 | (e << SH4_UTLB_E_SHIFT);
201 		md_upte->data = (*pte & PG_HW_BITS) |
202 		    SH4_UTLB_DA1_D | SH4_UTLB_DA1_V;
203 		++md_upte;
204 
205 		vpn += PAGE_SIZE;
206 		++e;
207 	}
208 }
209 #endif /* SH4 */
210 #endif /* !P1_STACK */
211 
212 
213 /*
214  * Copy a NUL-terminated string, at most maxlen characters long.
215  * Return the number of characters copied (including the NUL) in
216  * *lencopied.  If the string is too long, return ENAMETOOLONG,
217  * else return 0.
218  */
219 int
220 copystr(const void *kfaddr, void *kdaddr, size_t maxlen, size_t *lencopied)
221 {
222 	const char *from = kfaddr;
223 	char *to = kdaddr;
224 	int i;
225 
226 	for (i = 0; i < maxlen; i++) {
227 		if ((*to++ = *from++) == '\0') {
228 			if (lencopied)
229 				*lencopied = i + 1;
230 			return (0);
231 		}
232 	}
233 
234 	if (lencopied)
235 		*lencopied = i;
236 
237 	return (ENAMETOOLONG);
238 }
239