| 1 | |
| 2 | /* |
| 3 | * Copyright (c) 2006 The DragonFly Project. All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to The DragonFly Project |
| 6 | * by Matthew Dillon <dillon@backplane.com> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * 3. Neither the name of The DragonFly Project nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific, prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 26 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 32 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 | * SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | #include "opt_ddb.h" |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/systm.h> |
| 39 | #include <sys/reboot.h> |
| 40 | #include <sys/kernel.h> |
| 41 | #include <sys/kthread.h> |
| 42 | #include <sys/reboot.h> |
| 43 | #include <ddb/ddb.h> |
| 44 | |
| 45 | #include <sys/thread2.h> |
| 46 | |
| 47 | #include <machine/trap.h> |
| 48 | #include <machine/md_var.h> |
| 49 | #include <machine/segments.h> |
| 50 | #include <machine/cpu.h> |
| 51 | #include <machine/smp.h> |
| 52 | |
| 53 | #include <err.h> |
| 54 | #include <signal.h> |
| 55 | #include <stdio.h> |
| 56 | #include <unistd.h> |
| 57 | |
| 58 | int _ucodesel = LSEL(LUCODE_SEL, SEL_UPL); |
| 59 | int _udatasel = LSEL(LUDATA_SEL, SEL_UPL); |
| 60 | |
| 61 | static void exc_segfault(int signo, siginfo_t *info, void *ctx); |
| 62 | #ifdef DDB |
| 63 | static void exc_debugger(int signo, siginfo_t *info, void *ctx); |
| 64 | #endif |
| 65 | |
| 66 | /* |
| 67 | * IPIs are 'fast' interrupts, so we deal with them directly from our |
| 68 | * signal handler. |
| 69 | * |
| 70 | * WARNING: Signals are not physically disabled here so we have to enter |
| 71 | * our critical section before bumping gd_intr_nesting_level or another |
| 72 | * interrupt can come along and get really confused. |
| 73 | */ |
| 74 | static |
| 75 | void |
| 76 | ipisig(int nada, siginfo_t *info, void *ctxp) |
| 77 | { |
| 78 | if (curthread->td_critcount == 0) { |
| 79 | ++curthread->td_critcount; |
| 80 | ++mycpu->gd_intr_nesting_level; |
| 81 | lwkt_process_ipiq(); |
| 82 | --mycpu->gd_intr_nesting_level; |
| 83 | --curthread->td_critcount; |
| 84 | } else { |
| 85 | need_ipiq(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Unconditionally stop or restart a cpu. |
| 91 | * |
| 92 | * Note: cpu_mask_all_signals() masks all signals except SIGXCPU itself. |
| 93 | * SIGXCPU itself is blocked on entry to stopsig() by the signal handler |
| 94 | * itself. |
| 95 | * |
| 96 | * WARNING: Signals are not physically disabled here so we have to enter |
| 97 | * our critical section before bumping gd_intr_nesting_level or another |
| 98 | * interrupt can come along and get really confused. |
| 99 | */ |
| 100 | static |
| 101 | void |
| 102 | stopsig(int nada, siginfo_t *info, void *ctxp) |
| 103 | { |
| 104 | sigset_t ss; |
| 105 | |
| 106 | sigemptyset(&ss); |
| 107 | sigaddset(&ss, SIGALRM); |
| 108 | sigaddset(&ss, SIGIO); |
| 109 | sigaddset(&ss, SIGQUIT); |
| 110 | sigaddset(&ss, SIGUSR1); |
| 111 | sigaddset(&ss, SIGUSR2); |
| 112 | sigaddset(&ss, SIGTERM); |
| 113 | sigaddset(&ss, SIGWINCH); |
| 114 | |
| 115 | ++curthread->td_critcount; |
| 116 | ++mycpu->gd_intr_nesting_level; |
| 117 | while (stopped_cpus & mycpu->gd_cpumask) { |
| 118 | sigsuspend(&ss); |
| 119 | } |
| 120 | --mycpu->gd_intr_nesting_level; |
| 121 | --curthread->td_critcount; |
| 122 | } |
| 123 | |
| 124 | #if 0 |
| 125 | |
| 126 | /* |
| 127 | * SIGIO is used by cothreads to signal back into the virtual kernel. |
| 128 | */ |
| 129 | static |
| 130 | void |
| 131 | iosig(int nada, siginfo_t *info, void *ctxp) |
| 132 | { |
| 133 | signalintr(4); |
| 134 | } |
| 135 | |
| 136 | #endif |
| 137 | |
| 138 | static |
| 139 | void |
| 140 | infosig(int nada, siginfo_t *info, void *ctxp) |
| 141 | { |
| 142 | ucontext_t *ctx = ctxp; |
| 143 | char buf[256]; |
| 144 | |
| 145 | snprintf(buf, sizeof(buf), "lwp %d pc=%p sp=%p\n", |
| 146 | (int)lwp_gettid(), |
| 147 | (void *)(intptr_t)ctx->uc_mcontext.mc_eip, |
| 148 | (void *)(intptr_t)ctx->uc_mcontext.mc_esp); |
| 149 | write(2, buf, strlen(buf)); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | init_exceptions(void) |
| 154 | { |
| 155 | struct sigaction sa; |
| 156 | |
| 157 | bzero(&sa, sizeof(sa)); |
| 158 | sa.sa_sigaction = exc_segfault; |
| 159 | sa.sa_flags |= SA_SIGINFO | SA_NODEFER; |
| 160 | sigemptyset(&sa.sa_mask); |
| 161 | sigaction(SIGBUS, &sa, NULL); |
| 162 | sigaction(SIGSEGV, &sa, NULL); |
| 163 | sigaction(SIGTRAP, &sa, NULL); |
| 164 | sigaction(SIGFPE, &sa, NULL); |
| 165 | |
| 166 | sa.sa_flags &= ~SA_NODEFER; |
| 167 | |
| 168 | #ifdef DDB |
| 169 | sa.sa_sigaction = exc_debugger; |
| 170 | sigaction(SIGQUIT, &sa, NULL); |
| 171 | #endif |
| 172 | sa.sa_sigaction = ipisig; |
| 173 | sigaction(SIGUSR1, &sa, NULL); |
| 174 | sa.sa_sigaction = stopsig; |
| 175 | sigaction(SIGXCPU, &sa, NULL); |
| 176 | #if 0 |
| 177 | sa.sa_sigaction = iosig; |
| 178 | sigaction(SIGIO, &sa, NULL); |
| 179 | #endif |
| 180 | sa.sa_sigaction = infosig; |
| 181 | sigaction(SIGINFO, &sa, NULL); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * This function handles a segmentation fault. |
| 186 | * |
| 187 | * XXX We assume that trapframe is a subset of ucontext. It is as of |
| 188 | * this writing. |
| 189 | */ |
| 190 | static void |
| 191 | exc_segfault(int signo, siginfo_t *info, void *ctxp) |
| 192 | { |
| 193 | ucontext_t *ctx = ctxp; |
| 194 | |
| 195 | #if 0 |
| 196 | kprintf("CAUGHT SEGFAULT EIP %08x ERR %08x TRAPNO %d err %d\n", |
| 197 | ctx->uc_mcontext.mc_eip, |
| 198 | ctx->uc_mcontext.mc_err, |
| 199 | ctx->uc_mcontext.mc_trapno & 0xFFFF, |
| 200 | ctx->uc_mcontext.mc_trapno >> 16); |
| 201 | #endif |
| 202 | kern_trap((struct trapframe *)&ctx->uc_mcontext.mc_gs); |
| 203 | splz(); |
| 204 | } |
| 205 | |
| 206 | #ifdef DDB |
| 207 | |
| 208 | static void |
| 209 | exc_debugger(int signo, siginfo_t *info, void *ctx) |
| 210 | { |
| 211 | Debugger("interrupt from console"); |
| 212 | } |
| 213 | |
| 214 | #endif |