1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_capsicum.h" 38 #include "opt_ktrace.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/capsicum.h> 43 #include <sys/ktrace.h> 44 #include <sys/proc.h> 45 #include <sys/sysproto.h> 46 #include <sys/syscall.h> 47 #include <sys/sysent.h> 48 #include <vm/vm.h> 49 #include <vm/vm_extern.h> 50 51 #include <machine/cpu.h> 52 #include <machine/sysarch.h> 53 #include <machine/machdep.h> 54 #include <machine/vmparam.h> 55 56 #ifndef _SYS_SYSPROTO_H_ 57 struct sysarch_args { 58 int op; 59 char *parms; 60 }; 61 #endif 62 63 /* Prototypes */ 64 static int arm32_sync_icache (struct thread *, void *); 65 static int arm32_drain_writebuf(struct thread *, void *); 66 67 static int 68 sync_icache(uintptr_t addr, size_t len) 69 { 70 size_t size; 71 vm_offset_t rv; 72 73 /* Align starting address to cacheline size */ 74 len += addr & cpuinfo.dcache_line_mask; 75 addr &= ~cpuinfo.dcache_line_mask; 76 77 /* Break whole range to pages. */ 78 do { 79 size = PAGE_SIZE - (addr & PAGE_MASK); 80 size = min(size, len); 81 rv = dcache_wb_pou_checked(addr, size); 82 if (rv == 1) /* see dcache_wb_pou_checked() */ 83 rv = icache_inv_pou_checked(addr, size); 84 if (rv != 1) { 85 if (!useracc((void *)addr, size, VM_PROT_READ)) { 86 /* Invalid access */ 87 return (rv); 88 } 89 /* Valid but unmapped page - skip it. */ 90 } 91 len -= size; 92 addr += size; 93 } while (len > 0); 94 95 /* Invalidate branch predictor buffer. */ 96 bpb_inv_all(); 97 return (1); 98 } 99 100 static int 101 arm32_sync_icache(struct thread *td, void *args) 102 { 103 struct arm_sync_icache_args ua; 104 int error; 105 ksiginfo_t ksi; 106 vm_offset_t rv; 107 108 if ((error = copyin(args, &ua, sizeof(ua))) != 0) 109 return (error); 110 111 if (ua.len == 0) { 112 td->td_retval[0] = 0; 113 return (0); 114 } 115 116 /* 117 * Validate arguments. Address and length are unsigned, 118 * so we can use wrapped overflow check. 119 */ 120 if (((ua.addr + ua.len) < ua.addr) || 121 ((ua.addr + ua.len) > VM_MAXUSER_ADDRESS)) { 122 ksiginfo_init_trap(&ksi); 123 ksi.ksi_signo = SIGSEGV; 124 ksi.ksi_code = SEGV_ACCERR; 125 ksi.ksi_addr = (void *)max(ua.addr, VM_MAXUSER_ADDRESS); 126 trapsignal(td, &ksi); 127 return (EINVAL); 128 } 129 130 rv = sync_icache(ua.addr, ua.len); 131 if (rv != 1) { 132 ksiginfo_init_trap(&ksi); 133 ksi.ksi_signo = SIGSEGV; 134 ksi.ksi_code = SEGV_MAPERR; 135 ksi.ksi_addr = (void *)rv; 136 trapsignal(td, &ksi); 137 return (EINVAL); 138 } 139 140 td->td_retval[0] = 0; 141 return (0); 142 } 143 144 static int 145 arm32_drain_writebuf(struct thread *td, void *args) 146 { 147 /* No args. */ 148 149 dsb(); 150 cpu_l2cache_drain_writebuf(); 151 td->td_retval[0] = 0; 152 return (0); 153 } 154 155 static int 156 arm32_set_tp(struct thread *td, void *args) 157 { 158 159 set_tls(args); 160 return (0); 161 } 162 163 static int 164 arm32_get_tp(struct thread *td, void *args) 165 { 166 167 td->td_retval[0] = (register_t)get_tls(); 168 return (0); 169 } 170 171 int 172 sysarch(struct thread *td, struct sysarch_args *uap) 173 { 174 int error; 175 176 #ifdef CAPABILITY_MODE 177 /* 178 * When adding new operations, add a new case statement here to 179 * explicitly indicate whether or not the operation is safe to 180 * perform in capability mode. 181 */ 182 if (IN_CAPABILITY_MODE(td)) { 183 switch (uap->op) { 184 case ARM_SYNC_ICACHE: 185 case ARM_DRAIN_WRITEBUF: 186 case ARM_SET_TP: 187 case ARM_GET_TP: 188 case ARM_GET_VFPSTATE: 189 break; 190 191 default: 192 #ifdef KTRACE 193 if (KTRPOINT(td, KTR_CAPFAIL)) 194 ktrcapfail(CAPFAIL_SYSCALL, NULL, NULL); 195 #endif 196 return (ECAPMODE); 197 } 198 } 199 #endif 200 201 switch (uap->op) { 202 case ARM_SYNC_ICACHE: 203 error = arm32_sync_icache(td, uap->parms); 204 break; 205 case ARM_DRAIN_WRITEBUF: 206 error = arm32_drain_writebuf(td, uap->parms); 207 break; 208 case ARM_SET_TP: 209 error = arm32_set_tp(td, uap->parms); 210 break; 211 case ARM_GET_TP: 212 error = arm32_get_tp(td, uap->parms); 213 break; 214 case ARM_GET_VFPSTATE: 215 error = arm_get_vfpstate(td, uap->parms); 216 break; 217 default: 218 error = EINVAL; 219 break; 220 } 221 return (error); 222 } 223