The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/riscv/riscv/trap.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2015 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: releng/11.0/sys/riscv/riscv/trap.c 300618 2016-05-24 16:41:37Z br $");
   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 #include <machine/frame.h>
   61 #include <machine/pcb.h>
   62 #include <machine/pcpu.h>
   63 
   64 #include <machine/resource.h>
   65 #include <machine/intr.h>
   66 
   67 #ifdef KDTRACE_HOOKS
   68 #include <sys/dtrace_bsd.h>
   69 #endif
   70 
   71 int (*dtrace_invop_jump_addr)(struct trapframe *);
   72 
   73 extern register_t fsu_intr_fault;
   74 
   75 /* Called from exception.S */
   76 void do_trap_supervisor(struct trapframe *);
   77 void do_trap_user(struct trapframe *);
   78 
   79 static __inline void
   80 call_trapsignal(struct thread *td, int sig, int code, void *addr)
   81 {
   82         ksiginfo_t ksi;
   83 
   84         ksiginfo_init_trap(&ksi);
   85         ksi.ksi_signo = sig;
   86         ksi.ksi_code = code;
   87         ksi.ksi_addr = addr;
   88         trapsignal(td, &ksi);
   89 }
   90 
   91 int
   92 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
   93 {
   94         struct proc *p;
   95         register_t *ap;
   96         int nap;
   97 
   98         nap = 8;
   99         p = td->td_proc;
  100         ap = &td->td_frame->tf_a[0];
  101 
  102         sa->code = td->td_frame->tf_t[0];
  103 
  104         if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
  105                 sa->code = *ap++;
  106                 nap--;
  107         }
  108 
  109         if (p->p_sysent->sv_mask)
  110                 sa->code &= p->p_sysent->sv_mask;
  111         if (sa->code >= p->p_sysent->sv_size)
  112                 sa->callp = &p->p_sysent->sv_table[0];
  113         else
  114                 sa->callp = &p->p_sysent->sv_table[sa->code];
  115 
  116         sa->narg = sa->callp->sy_narg;
  117         memcpy(sa->args, ap, nap * sizeof(register_t));
  118         if (sa->narg > nap)
  119                 panic("TODO: Could we have more then 8 args?");
  120 
  121         td->td_retval[0] = 0;
  122         td->td_retval[1] = 0;
  123 
  124         return (0);
  125 }
  126 
  127 #include "../../kern/subr_syscall.c"
  128 
  129 static void
  130 dump_regs(struct trapframe *frame)
  131 {
  132         int n;
  133         int i;
  134 
  135         n = (sizeof(frame->tf_t) / sizeof(frame->tf_t[0]));
  136         for (i = 0; i < n; i++)
  137                 printf("t[%d] == 0x%016lx\n", i, frame->tf_t[i]);
  138 
  139         n = (sizeof(frame->tf_s) / sizeof(frame->tf_s[0]));
  140         for (i = 0; i < n; i++)
  141                 printf("s[%d] == 0x%016lx\n", i, frame->tf_s[i]);
  142 
  143         n = (sizeof(frame->tf_a) / sizeof(frame->tf_a[0]));
  144         for (i = 0; i < n; i++)
  145                 printf("a[%d] == 0x%016lx\n", i, frame->tf_a[i]);
  146 
  147         printf("sepc == 0x%016lx\n", frame->tf_sepc);
  148         printf("sstatus == 0x%016lx\n", frame->tf_sstatus);
  149 }
  150 
  151 static void
  152 svc_handler(struct trapframe *frame)
  153 {
  154         struct syscall_args sa;
  155         struct thread *td;
  156         int error;
  157 
  158         td = curthread;
  159         td->td_frame = frame;
  160 
  161         error = syscallenter(td, &sa);
  162         syscallret(td, error, &sa);
  163 }
  164 
  165 static void
  166 data_abort(struct trapframe *frame, int lower)
  167 {
  168         struct vm_map *map;
  169         uint64_t sbadaddr;
  170         struct thread *td;
  171         struct pcb *pcb;
  172         vm_prot_t ftype;
  173         vm_offset_t va;
  174         struct proc *p;
  175         int ucode;
  176         int error;
  177         int sig;
  178 
  179 #ifdef KDB
  180         if (kdb_active) {
  181                 kdb_reenter();
  182                 return;
  183         }
  184 #endif
  185 
  186         td = curthread;
  187         pcb = td->td_pcb;
  188 
  189         /*
  190          * Special case for fuswintr and suswintr. These can't sleep so
  191          * handle them early on in the trap handler.
  192          */
  193         if (__predict_false(pcb->pcb_onfault == (vm_offset_t)&fsu_intr_fault)) {
  194                 frame->tf_sepc = pcb->pcb_onfault;
  195                 return;
  196         }
  197 
  198         sbadaddr = frame->tf_sbadaddr;
  199 
  200         p = td->td_proc;
  201 
  202         if (lower)
  203                 map = &td->td_proc->p_vmspace->vm_map;
  204         else {
  205                 /* The top bit tells us which range to use */
  206                 if ((sbadaddr >> 63) == 1)
  207                         map = kernel_map;
  208                 else
  209                         map = &td->td_proc->p_vmspace->vm_map;
  210         }
  211 
  212         va = trunc_page(sbadaddr);
  213 
  214         if (frame->tf_scause == EXCP_STORE_ACCESS_FAULT) {
  215                 ftype = (VM_PROT_READ | VM_PROT_WRITE);
  216         } else {
  217                 ftype = (VM_PROT_READ);
  218         }
  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 (lower) {
  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 *)sbadaddr);
  251                 } else {
  252                         if (td->td_intr_nesting_level == 0 &&
  253                             pcb->pcb_onfault != 0) {
  254                                 frame->tf_a[0] = error;
  255                                 frame->tf_sepc = pcb->pcb_onfault;
  256                                 return;
  257                         }
  258                         dump_regs(frame);
  259                         panic("vm_fault failed: %lx, va 0x%016lx",
  260                                 frame->tf_sepc, sbadaddr);
  261                 }
  262         }
  263 
  264         if (lower)
  265                 userret(td, frame);
  266 }
  267 
  268 void
  269 do_trap_supervisor(struct trapframe *frame)
  270 {
  271         uint64_t exception;
  272 
  273         exception = (frame->tf_scause & EXCP_MASK);
  274         if (frame->tf_scause & EXCP_INTR) {
  275                 /* Interrupt */
  276                 riscv_cpu_intr(frame);
  277                 return;
  278         }
  279 
  280 #ifdef KDTRACE_HOOKS
  281         if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
  282                 return;
  283 #endif
  284 
  285         CTR3(KTR_TRAP, "do_trap_supervisor: curthread: %p, sepc: %lx, frame: %p",
  286             curthread, frame->tf_sepc, frame);
  287 
  288         switch(exception) {
  289         case EXCP_LOAD_ACCESS_FAULT:
  290         case EXCP_STORE_ACCESS_FAULT:
  291         case EXCP_INSTR_ACCESS_FAULT:
  292                 data_abort(frame, 0);
  293                 break;
  294         case EXCP_INSTR_BREAKPOINT:
  295 #ifdef KDTRACE_HOOKS
  296                 if (dtrace_invop_jump_addr != 0) {
  297                         dtrace_invop_jump_addr(frame);
  298                         break;
  299                 }
  300 #endif
  301 #ifdef KDB
  302                 kdb_trap(exception, 0, frame);
  303 #else
  304                 dump_regs(frame);
  305                 panic("No debugger in kernel.\n");
  306 #endif
  307                 break;
  308         case EXCP_INSTR_ILLEGAL:
  309                 dump_regs(frame);
  310                 panic("Illegal instruction at 0x%016lx\n", frame->tf_sepc);
  311                 break;
  312         default:
  313                 dump_regs(frame);
  314                 panic("Unknown kernel exception %x badaddr %lx\n",
  315                         exception, frame->tf_sbadaddr);
  316         }
  317 }
  318 
  319 void
  320 do_trap_user(struct trapframe *frame)
  321 {
  322         uint64_t exception;
  323         struct thread *td;
  324 
  325         td = curthread;
  326         td->td_frame = frame;
  327 
  328         exception = (frame->tf_scause & EXCP_MASK);
  329         if (frame->tf_scause & EXCP_INTR) {
  330                 /* Interrupt */
  331                 riscv_cpu_intr(frame);
  332                 return;
  333         }
  334 
  335         CTR3(KTR_TRAP, "do_trap_user: curthread: %p, sepc: %lx, frame: %p",
  336             curthread, frame->tf_sepc, frame);
  337 
  338         switch(exception) {
  339         case EXCP_LOAD_ACCESS_FAULT:
  340         case EXCP_STORE_ACCESS_FAULT:
  341         case EXCP_INSTR_ACCESS_FAULT:
  342                 data_abort(frame, 1);
  343                 break;
  344         case EXCP_UMODE_ENV_CALL:
  345                 frame->tf_sepc += 4;    /* Next instruction */
  346                 svc_handler(frame);
  347                 break;
  348         case EXCP_INSTR_ILLEGAL:
  349                 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc);
  350                 userret(td, frame);
  351                 break;
  352         case EXCP_INSTR_BREAKPOINT:
  353                 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc);
  354                 userret(td, frame);
  355                 break;
  356         default:
  357                 dump_regs(frame);
  358                 panic("Unknown userland exception %x badaddr %lx\n",
  359                         exception, frame->tf_sbadaddr);
  360         }
  361 }

Cache object: a497f18f19fda3ad5c8d1f91c1241939


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.