Annotation of sys/arch/sh/sh/locore_c.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: locore_c.c,v 1.4 2007/05/18 14:41:55 art 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: * 3. All advertising materials mentioning features or use of this software
21: * must display the following acknowledgement:
22: * This product includes software developed by the NetBSD
23: * Foundation, Inc. and its contributors.
24: * 4. Neither the name of The NetBSD Foundation nor the names of its
25: * contributors may be used to endorse or promote products derived
26: * from this software without specific prior written permission.
27: *
28: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38: * POSSIBILITY OF SUCH DAMAGE.
39: */
40:
41: /*-
42: * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
43: * All rights reserved.
44: *
45: * This code is derived from software contributed to Berkeley by
46: * William Jolitz.
47: *
48: * Redistribution and use in source and binary forms, with or without
49: * modification, are permitted provided that the following conditions
50: * are met:
51: * 1. Redistributions of source code must retain the above copyright
52: * notice, this list of conditions and the following disclaimer.
53: * 2. Redistributions in binary form must reproduce the above copyright
54: * notice, this list of conditions and the following disclaimer in the
55: * documentation and/or other materials provided with the distribution.
56: * 3. Neither the name of the University nor the names of its contributors
57: * may be used to endorse or promote products derived from this software
58: * without specific prior written permission.
59: *
60: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70: * SUCH DAMAGE.
71: *
72: * @(#)Locore.c
73: */
74:
75: /*-
76: * Copyright (c) 1993, 1994, 1995, 1996, 1997
77: * Charles M. Hannum. All rights reserved.
78: * Copyright (c) 1992 Terrence R. Lambert.
79: *
80: * This code is derived from software contributed to Berkeley by
81: * William Jolitz.
82: *
83: * Redistribution and use in source and binary forms, with or without
84: * modification, are permitted provided that the following conditions
85: * are met:
86: * 1. Redistributions of source code must retain the above copyright
87: * notice, this list of conditions and the following disclaimer.
88: * 2. Redistributions in binary form must reproduce the above copyright
89: * notice, this list of conditions and the following disclaimer in the
90: * documentation and/or other materials provided with the distribution.
91: * 3. All advertising materials mentioning features or use of this software
92: * must display the following acknowledgement:
93: * This product includes software developed by the University of
94: * California, Berkeley and its contributors.
95: * 4. Neither the name of the University nor the names of its contributors
96: * may be used to endorse or promote products derived from this software
97: * without specific prior written permission.
98: *
99: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
100: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
102: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
103: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
105: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
106: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
107: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
108: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
109: * SUCH DAMAGE.
110: *
111: * @(#)Locore.c
112: */
113:
114: #include <sys/param.h>
115: #include <sys/systm.h>
116: #include <sys/user.h>
117: #include <sys/sched.h>
118: #include <sys/proc.h>
119:
120: #include <uvm/uvm_extern.h>
121:
122: #include <sh/locore.h>
123: #include <sh/cpu.h>
124: #include <sh/pmap.h>
125: #include <sh/mmu_sh3.h>
126: #include <sh/mmu_sh4.h>
127: #include <sh/ubcreg.h>
128:
129: void (*__sh_switch_resume)(struct proc *);
130: struct proc *cpu_switch_search(struct proc *);
131: struct proc *cpu_switch_prepare(struct proc *, struct proc *);
132: void switch_exit(struct proc *, void (*)(struct proc *));
133: void idle(void);
134: int want_resched;
135:
136: #ifdef LOCKDEBUG
137: #define SCHED_LOCK_IDLE() sched_lock_idle()
138: #define SCHED_UNLOCK_IDLE() sched_unlock_idle()
139: #else
140: #define SCHED_LOCK_IDLE() do {} while (/* CONSTCOND */ 0)
141: #define SCHED_UNLOCK_IDLE() do {} while (/* CONSTCOND */ 0)
142: #endif
143:
144:
145: /*
146: * Prepare context switch from oproc to nproc.
147: * This code is shared by cpu_switch and cpu_switchto.
148: */
149: struct proc *
150: cpu_switch_prepare(struct proc *oproc, struct proc *nproc)
151: {
152: nproc->p_stat = SONPROC;
153:
154: if (oproc && (oproc->p_md.md_flags & MDP_STEP))
155: _reg_write_2(SH_(BBRB), 0);
156:
157: if (nproc != oproc) {
158: curpcb = nproc->p_md.md_pcb;
159: pmap_activate(nproc);
160: }
161:
162: if (nproc->p_md.md_flags & MDP_STEP) {
163: int pm_asid = nproc->p_vmspace->vm_map.pmap->pm_asid;
164:
165: _reg_write_2(SH_(BBRB), 0);
166: _reg_write_4(SH_(BARB), nproc->p_md.md_regs->tf_spc);
167: _reg_write_1(SH_(BASRB), pm_asid);
168: _reg_write_1(SH_(BAMRB), 0);
169: _reg_write_2(SH_(BRCR), 0x0040);
170: _reg_write_2(SH_(BBRB), 0x0014);
171: }
172:
173: curproc = nproc;
174: return (nproc);
175: }
176:
177: /*
178: * Find the highest priority proc and prepare to switching to it.
179: */
180: struct proc *
181: cpu_switch_search(struct proc *oproc)
182: {
183: struct prochd *q;
184: struct proc *p;
185:
186: curproc = NULL;
187:
188: SCHED_LOCK_IDLE();
189: while (sched_is_idle()) {
190: SCHED_UNLOCK_IDLE();
191: idle();
192: SCHED_LOCK_IDLE();
193: }
194:
195: q = &qs[ffs(whichqs) - 1];
196: p = q->ph_link;
197: remrunqueue(p);
198: want_resched = 0;
199: SCHED_UNLOCK_IDLE();
200:
201: return (cpu_switch_prepare(oproc, p));
202: }
203:
204: void
205: cpu_exit(struct proc *p)
206: {
207: if (p->p_md.md_flags & MDP_STEP)
208: _reg_write_2(SH_(BBRB), 0);
209:
210: switch_exit(p, exit2);
211: }
212:
213: /*
214: * void idle(void):
215: * When no processes are on the run queue, wait for something to come
216: * ready. Separated function for profiling.
217: */
218: void
219: idle()
220: {
221: spl0();
222: uvm_pageidlezero();
223: __asm volatile("sleep");
224: splsched();
225: }
226:
227: #ifndef P1_STACK
228: #ifdef SH3
229: /*
230: * void sh3_switch_setup(struct proc *p):
231: * prepare kernel stack PTE table. TLB miss handler check these.
232: */
233: void
234: sh3_switch_setup(struct proc *p)
235: {
236: pt_entry_t *pte;
237: struct md_upte *md_upte = p->p_md.md_upte;
238: uint32_t vpn;
239: int i;
240:
241: vpn = (uint32_t)p->p_addr;
242: vpn &= ~PGOFSET;
243: for (i = 0; i < UPAGES; i++, vpn += PAGE_SIZE, md_upte++) {
244: pte = __pmap_kpte_lookup(vpn);
245: KDASSERT(pte && *pte != 0);
246:
247: md_upte->addr = vpn;
248: md_upte->data = (*pte & PG_HW_BITS) | PG_D | PG_V;
249: }
250: }
251: #endif /* SH3 */
252:
253: #ifdef SH4
254: /*
255: * void sh4_switch_setup(struct proc *p):
256: * prepare kernel stack PTE table. sh4_switch_resume wired this PTE.
257: */
258: void
259: sh4_switch_setup(struct proc *p)
260: {
261: pt_entry_t *pte;
262: struct md_upte *md_upte = p->p_md.md_upte;
263: uint32_t vpn;
264: int i, e;
265:
266: vpn = (uint32_t)p->p_addr;
267: vpn &= ~PGOFSET;
268: e = SH4_UTLB_ENTRY - UPAGES;
269: for (i = 0; i < UPAGES; i++, e++, vpn += PAGE_SIZE) {
270: pte = __pmap_kpte_lookup(vpn);
271: KDASSERT(pte && *pte != 0);
272: /* Address array */
273: md_upte->addr = SH4_UTLB_AA | (e << SH4_UTLB_E_SHIFT);
274: md_upte->data = vpn | SH4_UTLB_AA_D | SH4_UTLB_AA_V;
275: md_upte++;
276: /* Data array */
277: md_upte->addr = SH4_UTLB_DA1 | (e << SH4_UTLB_E_SHIFT);
278: md_upte->data = (*pte & PG_HW_BITS) |
279: SH4_UTLB_DA1_D | SH4_UTLB_DA1_V;
280: md_upte++;
281: }
282: }
283: #endif /* SH4 */
284: #endif /* !P1_STACK */
285:
286: /*
287: * copystr(caddr_t from, caddr_t to, size_t maxlen, size_t *lencopied);
288: * Copy a NUL-terminated string, at most maxlen characters long. Return the
289: * number of characters copied (including the NUL) in *lencopied. If the
290: * string is too long, return ENAMETOOLONG; else return 0.
291: */
292: int
293: copystr(const void *kfaddr, void *kdaddr, size_t maxlen, size_t *lencopied)
294: {
295: const char *from = kfaddr;
296: char *to = kdaddr;
297: int i;
298:
299: for (i = 0; maxlen-- > 0; i++) {
300: if ((*to++ = *from++) == '\0') {
301: if (lencopied)
302: *lencopied = i + 1;
303: return (0);
304: }
305: }
306:
307: if (lencopied)
308: *lencopied = i;
309:
310: return (ENAMETOOLONG);
311: }
CVSweb