146280ae7SWarner Losh /*- 2c49761ddSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3c49761ddSPedro F. Giffuni * 472d44f31SMarcel Moolenaar * Copyright (c) 2004 Marcel Moolenaar 572d44f31SMarcel Moolenaar * All rights reserved. 672d44f31SMarcel Moolenaar * 772d44f31SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 872d44f31SMarcel Moolenaar * modification, are permitted provided that the following conditions 972d44f31SMarcel Moolenaar * are met: 1072d44f31SMarcel Moolenaar * 1172d44f31SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 1272d44f31SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 1372d44f31SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 1472d44f31SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 1572d44f31SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 1672d44f31SMarcel Moolenaar * 1772d44f31SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 1872d44f31SMarcel Moolenaar * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1972d44f31SMarcel Moolenaar * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2072d44f31SMarcel Moolenaar * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2172d44f31SMarcel Moolenaar * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2272d44f31SMarcel Moolenaar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2372d44f31SMarcel Moolenaar * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2472d44f31SMarcel Moolenaar * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2572d44f31SMarcel Moolenaar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2672d44f31SMarcel Moolenaar * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2772d44f31SMarcel Moolenaar */ 2872d44f31SMarcel Moolenaar 2972d44f31SMarcel Moolenaar #include <sys/cdefs.h> 3072d44f31SMarcel Moolenaar __FBSDID("$FreeBSD$"); 3172d44f31SMarcel Moolenaar 3272d44f31SMarcel Moolenaar #include <sys/param.h> 3372d44f31SMarcel Moolenaar #include <sys/systm.h> 3472d44f31SMarcel Moolenaar #include <sys/kdb.h> 3572d44f31SMarcel Moolenaar #include <sys/kernel.h> 3672d44f31SMarcel Moolenaar #include <sys/proc.h> 3772d44f31SMarcel Moolenaar #include <sys/signal.h> 3872d44f31SMarcel Moolenaar 39beb24065SJonathan T. Looney #include <machine/cpufunc.h> 4072d44f31SMarcel Moolenaar #include <machine/frame.h> 4172d44f31SMarcel Moolenaar #include <machine/gdb_machdep.h> 422337dc64SKonstantin Belousov #include <machine/md_var.h> 43a649898dSPeter Wemm #include <machine/pcb.h> 4472d44f31SMarcel Moolenaar #include <machine/psl.h> 4572d44f31SMarcel Moolenaar #include <machine/reg.h> 46beb24065SJonathan T. Looney #include <machine/specialreg.h> 4772d44f31SMarcel Moolenaar #include <machine/trap.h> 48a649898dSPeter Wemm #include <machine/frame.h> 49a649898dSPeter Wemm #include <machine/endian.h> 5072d44f31SMarcel Moolenaar 5172d44f31SMarcel Moolenaar #include <gdb/gdb.h> 5272d44f31SMarcel Moolenaar 5372d44f31SMarcel Moolenaar void * 5472d44f31SMarcel Moolenaar gdb_cpu_getreg(int regnum, size_t *regsz) 5572d44f31SMarcel Moolenaar { 565eb95e11SJohn Baldwin static uint32_t _kcodesel = GSEL(GCODE_SEL, SEL_KPL); 575eb95e11SJohn Baldwin static uint32_t _kdatasel = GSEL(GDATA_SEL, SEL_KPL); 5872d44f31SMarcel Moolenaar 5972d44f31SMarcel Moolenaar *regsz = gdb_cpu_regsz(regnum); 60a649898dSPeter Wemm 61a649898dSPeter Wemm if (kdb_thread == curthread) { 6272d44f31SMarcel Moolenaar switch (regnum) { 63*72939459SMitchell Horne case GDB_REG_RAX: return (&kdb_frame->tf_rax); 64*72939459SMitchell Horne case GDB_REG_RCX: return (&kdb_frame->tf_rcx); 65*72939459SMitchell Horne case GDB_REG_RDX: return (&kdb_frame->tf_rdx); 66*72939459SMitchell Horne case GDB_REG_RSI: return (&kdb_frame->tf_rsi); 67*72939459SMitchell Horne case GDB_REG_RDI: return (&kdb_frame->tf_rdi); 68*72939459SMitchell Horne case GDB_REG_R8: return (&kdb_frame->tf_r8); 69*72939459SMitchell Horne case GDB_REG_R9: return (&kdb_frame->tf_r9); 70*72939459SMitchell Horne case GDB_REG_R10: return (&kdb_frame->tf_r10); 71*72939459SMitchell Horne case GDB_REG_R11: return (&kdb_frame->tf_r11); 72*72939459SMitchell Horne case GDB_REG_RFLAGS: return (&kdb_frame->tf_rflags); 73*72939459SMitchell Horne case GDB_REG_CS: return (&kdb_frame->tf_cs); 74*72939459SMitchell Horne case GDB_REG_SS: return (&kdb_frame->tf_ss); 75a649898dSPeter Wemm } 76a649898dSPeter Wemm } 77a649898dSPeter Wemm switch (regnum) { 78*72939459SMitchell Horne case GDB_REG_RBX: return (&kdb_thrctx->pcb_rbx); 79*72939459SMitchell Horne case GDB_REG_RBP: return (&kdb_thrctx->pcb_rbp); 80*72939459SMitchell Horne case GDB_REG_RSP: return (&kdb_thrctx->pcb_rsp); 81*72939459SMitchell Horne case GDB_REG_R12: return (&kdb_thrctx->pcb_r12); 82*72939459SMitchell Horne case GDB_REG_R13: return (&kdb_thrctx->pcb_r13); 83*72939459SMitchell Horne case GDB_REG_R14: return (&kdb_thrctx->pcb_r14); 84*72939459SMitchell Horne case GDB_REG_R15: return (&kdb_thrctx->pcb_r15); 85*72939459SMitchell Horne case GDB_REG_PC: return (&kdb_thrctx->pcb_rip); 86*72939459SMitchell Horne case GDB_REG_CS: return (&_kcodesel); 87*72939459SMitchell Horne case GDB_REG_SS: return (&_kdatasel); 8872d44f31SMarcel Moolenaar } 8972d44f31SMarcel Moolenaar return (NULL); 9072d44f31SMarcel Moolenaar } 9172d44f31SMarcel Moolenaar 9272d44f31SMarcel Moolenaar void 93bcc5241cSMarcel Moolenaar gdb_cpu_setreg(int regnum, void *val) 9472d44f31SMarcel Moolenaar { 950ef474deSMitchell Horne register_t regval = *(register_t *)val; 9672d44f31SMarcel Moolenaar 970ef474deSMitchell Horne /* 980ef474deSMitchell Horne * Write registers to the trapframe and pcb, if applicable. 990ef474deSMitchell Horne * Some scratch registers are not tracked by the pcb. 1000ef474deSMitchell Horne */ 1010ef474deSMitchell Horne if (kdb_thread == curthread) { 10272d44f31SMarcel Moolenaar switch (regnum) { 1030ef474deSMitchell Horne case GDB_REG_RAX: kdb_frame->tf_rax = regval; break; 1040ef474deSMitchell Horne case GDB_REG_RBX: kdb_frame->tf_rbx = regval; break; 1050ef474deSMitchell Horne case GDB_REG_RCX: kdb_frame->tf_rcx = regval; break; 1060ef474deSMitchell Horne case GDB_REG_RDX: kdb_frame->tf_rdx = regval; break; 1070ef474deSMitchell Horne case GDB_REG_RSI: kdb_frame->tf_rsi = regval; break; 1080ef474deSMitchell Horne case GDB_REG_RDI: kdb_frame->tf_rdi = regval; break; 1090ef474deSMitchell Horne case GDB_REG_RBP: kdb_frame->tf_rbp = regval; break; 1100ef474deSMitchell Horne case GDB_REG_RSP: kdb_frame->tf_rsp = regval; break; 1110ef474deSMitchell Horne case GDB_REG_R8: kdb_frame->tf_r8 = regval; break; 1120ef474deSMitchell Horne case GDB_REG_R9: kdb_frame->tf_r9 = regval; break; 1130ef474deSMitchell Horne case GDB_REG_R10: kdb_frame->tf_r10 = regval; break; 1140ef474deSMitchell Horne case GDB_REG_R11: kdb_frame->tf_r11 = regval; break; 1150ef474deSMitchell Horne case GDB_REG_R12: kdb_frame->tf_r12 = regval; break; 1160ef474deSMitchell Horne case GDB_REG_R13: kdb_frame->tf_r13 = regval; break; 1170ef474deSMitchell Horne case GDB_REG_R14: kdb_frame->tf_r14 = regval; break; 1180ef474deSMitchell Horne case GDB_REG_R15: kdb_frame->tf_r15 = regval; break; 1190ef474deSMitchell Horne case GDB_REG_PC: kdb_frame->tf_rip = regval; break; 1200ef474deSMitchell Horne } 1210ef474deSMitchell Horne } 1220ef474deSMitchell Horne switch (regnum) { 1230ef474deSMitchell Horne case GDB_REG_RBX: kdb_thrctx->pcb_rbx = regval; break; 1240ef474deSMitchell Horne case GDB_REG_RBP: kdb_thrctx->pcb_rbp = regval; break; 1250ef474deSMitchell Horne case GDB_REG_RSP: kdb_thrctx->pcb_rsp = regval; break; 1260ef474deSMitchell Horne case GDB_REG_R12: kdb_thrctx->pcb_r12 = regval; break; 1270ef474deSMitchell Horne case GDB_REG_R13: kdb_thrctx->pcb_r13 = regval; break; 1280ef474deSMitchell Horne case GDB_REG_R14: kdb_thrctx->pcb_r14 = regval; break; 1290ef474deSMitchell Horne case GDB_REG_R15: kdb_thrctx->pcb_r15 = regval; break; 1300ef474deSMitchell Horne case GDB_REG_PC: kdb_thrctx->pcb_rip = regval; break; 13172d44f31SMarcel Moolenaar } 13272d44f31SMarcel Moolenaar } 133b1fb1bb1SMarcel Moolenaar 134b1fb1bb1SMarcel Moolenaar int 135b1fb1bb1SMarcel Moolenaar gdb_cpu_signal(int type, int code) 136b1fb1bb1SMarcel Moolenaar { 137b1fb1bb1SMarcel Moolenaar 138b1fb1bb1SMarcel Moolenaar switch (type & ~T_USER) { 139b1fb1bb1SMarcel Moolenaar case 0: return (SIGFPE); /* Divide by zero. */ 140b1fb1bb1SMarcel Moolenaar case 1: return (SIGTRAP); /* Debug exception. */ 141b1fb1bb1SMarcel Moolenaar case 3: return (SIGTRAP); /* Breakpoint. */ 142b1fb1bb1SMarcel Moolenaar case 4: return (SIGSEGV); /* into instr. (overflow). */ 143b1fb1bb1SMarcel Moolenaar case 5: return (SIGURG); /* bound instruction. */ 144b1fb1bb1SMarcel Moolenaar case 6: return (SIGILL); /* Invalid opcode. */ 145b1fb1bb1SMarcel Moolenaar case 7: return (SIGFPE); /* Coprocessor not present. */ 146b1fb1bb1SMarcel Moolenaar case 8: return (SIGEMT); /* Double fault. */ 147b1fb1bb1SMarcel Moolenaar case 9: return (SIGSEGV); /* Coprocessor segment overrun. */ 148b1fb1bb1SMarcel Moolenaar case 10: return (SIGTRAP); /* Invalid TSS (also single-step). */ 149b1fb1bb1SMarcel Moolenaar case 11: return (SIGSEGV); /* Segment not present. */ 150b1fb1bb1SMarcel Moolenaar case 12: return (SIGSEGV); /* Stack exception. */ 151b1fb1bb1SMarcel Moolenaar case 13: return (SIGSEGV); /* General protection. */ 152b1fb1bb1SMarcel Moolenaar case 14: return (SIGSEGV); /* Page fault. */ 153b1fb1bb1SMarcel Moolenaar case 16: return (SIGEMT); /* Coprocessor error. */ 154b1fb1bb1SMarcel Moolenaar } 155b1fb1bb1SMarcel Moolenaar return (SIGEMT); 156b1fb1bb1SMarcel Moolenaar } 157beb24065SJonathan T. Looney 158beb24065SJonathan T. Looney void * 159beb24065SJonathan T. Looney gdb_begin_write(void) 160beb24065SJonathan T. Looney { 161beb24065SJonathan T. Looney 1622337dc64SKonstantin Belousov return (disable_wp() ? &gdb_begin_write : NULL); 163beb24065SJonathan T. Looney } 164beb24065SJonathan T. Looney 165beb24065SJonathan T. Looney void 166beb24065SJonathan T. Looney gdb_end_write(void *arg) 167beb24065SJonathan T. Looney { 168beb24065SJonathan T. Looney 1692337dc64SKonstantin Belousov restore_wp(arg != NULL); 170beb24065SJonathan T. Looney } 171