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