1 /*- 2 * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * Portions of this software were developed by SRI International and the 6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract 7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme. 8 * 9 * Portions of this software were developed by the University of Cambridge 10 * Computer Laboratory as part of the CTSRD Project, with support from the 11 * UK Higher Education Innovation Fund (HEIF). 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/pioctl.h> 44 #include <sys/bus.h> 45 #include <sys/proc.h> 46 #include <sys/ptrace.h> 47 #include <sys/syscall.h> 48 #include <sys/sysent.h> 49 #ifdef KDB 50 #include <sys/kdb.h> 51 #endif 52 53 #include <vm/vm.h> 54 #include <vm/pmap.h> 55 #include <vm/vm_kern.h> 56 #include <vm/vm_map.h> 57 #include <vm/vm_param.h> 58 #include <vm/vm_extern.h> 59 60 #ifdef FPE 61 #include <machine/fpe.h> 62 #endif 63 #include <machine/frame.h> 64 #include <machine/pcb.h> 65 #include <machine/pcpu.h> 66 67 #include <machine/resource.h> 68 #include <machine/intr.h> 69 70 #ifdef KDTRACE_HOOKS 71 #include <sys/dtrace_bsd.h> 72 #endif 73 74 int (*dtrace_invop_jump_addr)(struct trapframe *); 75 76 extern register_t fsu_intr_fault; 77 78 /* Called from exception.S */ 79 void do_trap_supervisor(struct trapframe *); 80 void do_trap_user(struct trapframe *); 81 82 static __inline void 83 call_trapsignal(struct thread *td, int sig, int code, void *addr) 84 { 85 ksiginfo_t ksi; 86 87 ksiginfo_init_trap(&ksi); 88 ksi.ksi_signo = sig; 89 ksi.ksi_code = code; 90 ksi.ksi_addr = addr; 91 trapsignal(td, &ksi); 92 } 93 94 int 95 cpu_fetch_syscall_args(struct thread *td) 96 { 97 struct proc *p; 98 register_t *ap; 99 struct syscall_args *sa; 100 int nap; 101 102 nap = NARGREG; 103 p = td->td_proc; 104 sa = &td->td_sa; 105 ap = &td->td_frame->tf_a[0]; 106 107 sa->code = td->td_frame->tf_t[0]; 108 109 if (sa->code == SYS_syscall || sa->code == SYS___syscall) { 110 sa->code = *ap++; 111 nap--; 112 } 113 114 if (sa->code >= p->p_sysent->sv_size) 115 sa->callp = &p->p_sysent->sv_table[0]; 116 else 117 sa->callp = &p->p_sysent->sv_table[sa->code]; 118 119 sa->narg = sa->callp->sy_narg; 120 memcpy(sa->args, ap, nap * sizeof(register_t)); 121 if (sa->narg > nap) 122 panic("TODO: Could we have more then %d args?", NARGREG); 123 124 td->td_retval[0] = 0; 125 td->td_retval[1] = 0; 126 127 return (0); 128 } 129 130 #include "../../kern/subr_syscall.c" 131 132 static void 133 dump_regs(struct trapframe *frame) 134 { 135 int n; 136 int i; 137 138 n = (sizeof(frame->tf_t) / sizeof(frame->tf_t[0])); 139 for (i = 0; i < n; i++) 140 printf("t[%d] == 0x%016lx\n", i, frame->tf_t[i]); 141 142 n = (sizeof(frame->tf_s) / sizeof(frame->tf_s[0])); 143 for (i = 0; i < n; i++) 144 printf("s[%d] == 0x%016lx\n", i, frame->tf_s[i]); 145 146 n = (sizeof(frame->tf_a) / sizeof(frame->tf_a[0])); 147 for (i = 0; i < n; i++) 148 printf("a[%d] == 0x%016lx\n", i, frame->tf_a[i]); 149 150 printf("sepc == 0x%016lx\n", frame->tf_sepc); 151 printf("sstatus == 0x%016lx\n", frame->tf_sstatus); 152 } 153 154 static void 155 svc_handler(struct trapframe *frame) 156 { 157 struct thread *td; 158 int error; 159 160 td = curthread; 161 td->td_frame = frame; 162 163 error = syscallenter(td); 164 syscallret(td, error); 165 } 166 167 static void 168 data_abort(struct trapframe *frame, int usermode) 169 { 170 struct vm_map *map; 171 uint64_t stval; 172 struct thread *td; 173 struct pcb *pcb; 174 vm_prot_t ftype; 175 vm_offset_t va; 176 struct proc *p; 177 int error, sig, ucode; 178 179 #ifdef KDB 180 if (kdb_active) { 181 kdb_reenter(); 182 return; 183 } 184 #endif 185 186 td = curthread; 187 p = td->td_proc; 188 pcb = td->td_pcb; 189 stval = frame->tf_stval; 190 191 if (td->td_critnest != 0 || td->td_intr_nesting_level != 0 || 192 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL, 193 "Kernel page fault") != 0) 194 goto fatal; 195 196 if (usermode) 197 map = &td->td_proc->p_vmspace->vm_map; 198 else if (stval >= VM_MAX_USER_ADDRESS) 199 map = kernel_map; 200 else { 201 if (pcb->pcb_onfault == 0) 202 goto fatal; 203 map = &td->td_proc->p_vmspace->vm_map; 204 } 205 206 va = trunc_page(stval); 207 208 if ((frame->tf_scause == EXCP_FAULT_STORE) || 209 (frame->tf_scause == EXCP_STORE_PAGE_FAULT)) { 210 ftype = VM_PROT_WRITE; 211 } else if (frame->tf_scause == EXCP_INST_PAGE_FAULT) { 212 ftype = VM_PROT_EXECUTE; 213 } else { 214 ftype = VM_PROT_READ; 215 } 216 217 if (pmap_fault_fixup(map->pmap, va, ftype)) 218 goto done; 219 220 if (map != kernel_map) { 221 /* 222 * Keep swapout from messing with us during this 223 * critical time. 224 */ 225 PROC_LOCK(p); 226 ++p->p_lock; 227 PROC_UNLOCK(p); 228 229 /* Fault in the user page: */ 230 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 231 232 PROC_LOCK(p); 233 --p->p_lock; 234 PROC_UNLOCK(p); 235 } else { 236 /* 237 * Don't have to worry about process locking or stacks in the 238 * kernel. 239 */ 240 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 241 } 242 243 if (error != KERN_SUCCESS) { 244 if (usermode) { 245 sig = SIGSEGV; 246 if (error == KERN_PROTECTION_FAILURE) 247 ucode = SEGV_ACCERR; 248 else 249 ucode = SEGV_MAPERR; 250 call_trapsignal(td, sig, ucode, (void *)stval); 251 } else { 252 if (pcb->pcb_onfault != 0) { 253 frame->tf_a[0] = error; 254 frame->tf_sepc = pcb->pcb_onfault; 255 return; 256 } 257 goto fatal; 258 } 259 } 260 261 done: 262 if (usermode) 263 userret(td, frame); 264 return; 265 266 fatal: 267 dump_regs(frame); 268 panic("Fatal page fault at %#lx: %#016lx", frame->tf_sepc, stval); 269 } 270 271 void 272 do_trap_supervisor(struct trapframe *frame) 273 { 274 uint64_t exception; 275 uint64_t sstatus; 276 277 /* Ensure we came from supervisor mode, interrupts disabled */ 278 __asm __volatile("csrr %0, sstatus" : "=&r" (sstatus)); 279 KASSERT((sstatus & (SSTATUS_SPP | SSTATUS_SIE)) == SSTATUS_SPP, 280 ("We must came from S mode with interrupts disabled")); 281 282 exception = (frame->tf_scause & EXCP_MASK); 283 if (frame->tf_scause & EXCP_INTR) { 284 /* Interrupt */ 285 riscv_cpu_intr(frame); 286 return; 287 } 288 289 #ifdef KDTRACE_HOOKS 290 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception)) 291 return; 292 #endif 293 294 CTR3(KTR_TRAP, "do_trap_supervisor: curthread: %p, sepc: %lx, frame: %p", 295 curthread, frame->tf_sepc, frame); 296 297 switch(exception) { 298 case EXCP_FAULT_LOAD: 299 case EXCP_FAULT_STORE: 300 case EXCP_FAULT_FETCH: 301 case EXCP_STORE_PAGE_FAULT: 302 case EXCP_LOAD_PAGE_FAULT: 303 data_abort(frame, 0); 304 break; 305 case EXCP_BREAKPOINT: 306 #ifdef KDTRACE_HOOKS 307 if (dtrace_invop_jump_addr != 0) { 308 dtrace_invop_jump_addr(frame); 309 break; 310 } 311 #endif 312 #ifdef KDB 313 kdb_trap(exception, 0, frame); 314 #else 315 dump_regs(frame); 316 panic("No debugger in kernel.\n"); 317 #endif 318 break; 319 case EXCP_ILLEGAL_INSTRUCTION: 320 dump_regs(frame); 321 panic("Illegal instruction at 0x%016lx\n", frame->tf_sepc); 322 break; 323 default: 324 dump_regs(frame); 325 panic("Unknown kernel exception %x trap value %lx\n", 326 exception, frame->tf_stval); 327 } 328 } 329 330 void 331 do_trap_user(struct trapframe *frame) 332 { 333 uint64_t exception; 334 struct thread *td; 335 uint64_t sstatus; 336 struct pcb *pcb; 337 338 td = curthread; 339 td->td_frame = frame; 340 pcb = td->td_pcb; 341 342 /* Ensure we came from usermode, interrupts disabled */ 343 __asm __volatile("csrr %0, sstatus" : "=&r" (sstatus)); 344 KASSERT((sstatus & (SSTATUS_SPP | SSTATUS_SIE)) == 0, 345 ("We must came from U mode with interrupts disabled")); 346 347 exception = (frame->tf_scause & EXCP_MASK); 348 if (frame->tf_scause & EXCP_INTR) { 349 /* Interrupt */ 350 riscv_cpu_intr(frame); 351 return; 352 } 353 354 CTR3(KTR_TRAP, "do_trap_user: curthread: %p, sepc: %lx, frame: %p", 355 curthread, frame->tf_sepc, frame); 356 357 switch(exception) { 358 case EXCP_FAULT_LOAD: 359 case EXCP_FAULT_STORE: 360 case EXCP_FAULT_FETCH: 361 case EXCP_STORE_PAGE_FAULT: 362 case EXCP_LOAD_PAGE_FAULT: 363 case EXCP_INST_PAGE_FAULT: 364 data_abort(frame, 1); 365 break; 366 case EXCP_USER_ECALL: 367 frame->tf_sepc += 4; /* Next instruction */ 368 svc_handler(frame); 369 break; 370 case EXCP_ILLEGAL_INSTRUCTION: 371 #ifdef FPE 372 if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) { 373 /* 374 * May be a FPE trap. Enable FPE usage 375 * for this thread and try again. 376 */ 377 fpe_state_clear(); 378 frame->tf_sstatus &= ~SSTATUS_FS_MASK; 379 frame->tf_sstatus |= SSTATUS_FS_CLEAN; 380 pcb->pcb_fpflags |= PCB_FP_STARTED; 381 break; 382 } 383 #endif 384 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc); 385 userret(td, frame); 386 break; 387 case EXCP_BREAKPOINT: 388 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc); 389 userret(td, frame); 390 break; 391 default: 392 dump_regs(frame); 393 panic("Unknown userland exception %x, trap value %lx\n", 394 exception, frame->tf_stval); 395 } 396 } 397