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) { 6372939459SMitchell Horne case GDB_REG_RAX: return (&kdb_frame->tf_rax); 6472939459SMitchell Horne case GDB_REG_RCX: return (&kdb_frame->tf_rcx); 6572939459SMitchell Horne case GDB_REG_RDX: return (&kdb_frame->tf_rdx); 6672939459SMitchell Horne case GDB_REG_RSI: return (&kdb_frame->tf_rsi); 6772939459SMitchell Horne case GDB_REG_RDI: return (&kdb_frame->tf_rdi); 6872939459SMitchell Horne case GDB_REG_R8: return (&kdb_frame->tf_r8); 6972939459SMitchell Horne case GDB_REG_R9: return (&kdb_frame->tf_r9); 7072939459SMitchell Horne case GDB_REG_R10: return (&kdb_frame->tf_r10); 7172939459SMitchell Horne case GDB_REG_R11: return (&kdb_frame->tf_r11); 7272939459SMitchell Horne case GDB_REG_RFLAGS: return (&kdb_frame->tf_rflags); 7372939459SMitchell Horne case GDB_REG_CS: return (&kdb_frame->tf_cs); 7472939459SMitchell Horne case GDB_REG_SS: return (&kdb_frame->tf_ss); 75a649898dSPeter Wemm } 76a649898dSPeter Wemm } 77a649898dSPeter Wemm switch (regnum) { 7872939459SMitchell Horne case GDB_REG_RBX: return (&kdb_thrctx->pcb_rbx); 7972939459SMitchell Horne case GDB_REG_RBP: return (&kdb_thrctx->pcb_rbp); 8072939459SMitchell Horne case GDB_REG_RSP: return (&kdb_thrctx->pcb_rsp); 8172939459SMitchell Horne case GDB_REG_R12: return (&kdb_thrctx->pcb_r12); 8272939459SMitchell Horne case GDB_REG_R13: return (&kdb_thrctx->pcb_r13); 8372939459SMitchell Horne case GDB_REG_R14: return (&kdb_thrctx->pcb_r14); 8472939459SMitchell Horne case GDB_REG_R15: return (&kdb_thrctx->pcb_r15); 8572939459SMitchell Horne case GDB_REG_PC: return (&kdb_thrctx->pcb_rip); 8672939459SMitchell Horne case GDB_REG_CS: return (&_kcodesel); 8772939459SMitchell 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) { 139*962c06c5SMitchell Horne case T_BPTFLT: return (SIGTRAP); 140*962c06c5SMitchell Horne case T_ARITHTRAP: return (SIGFPE); 141*962c06c5SMitchell Horne case T_PROTFLT: return (SIGSEGV); 142*962c06c5SMitchell Horne case T_TRCTRAP: return (SIGTRAP); 143*962c06c5SMitchell Horne case T_PAGEFLT: return (SIGSEGV); 144*962c06c5SMitchell Horne case T_DIVIDE: return (SIGFPE); 145*962c06c5SMitchell Horne case T_NMI: return (SIGTRAP); 146*962c06c5SMitchell Horne case T_FPOPFLT: return (SIGILL); 147*962c06c5SMitchell Horne case T_TSSFLT: return (SIGSEGV); 148*962c06c5SMitchell Horne case T_SEGNPFLT: return (SIGSEGV); 149*962c06c5SMitchell Horne case T_STKFLT: return (SIGSEGV); 150*962c06c5SMitchell Horne case T_XMMFLT: return (SIGFPE); 151b1fb1bb1SMarcel Moolenaar } 152b1fb1bb1SMarcel Moolenaar return (SIGEMT); 153b1fb1bb1SMarcel Moolenaar } 154beb24065SJonathan T. Looney 155beb24065SJonathan T. Looney void * 156beb24065SJonathan T. Looney gdb_begin_write(void) 157beb24065SJonathan T. Looney { 158beb24065SJonathan T. Looney 1592337dc64SKonstantin Belousov return (disable_wp() ? &gdb_begin_write : NULL); 160beb24065SJonathan T. Looney } 161beb24065SJonathan T. Looney 162beb24065SJonathan T. Looney void 163beb24065SJonathan T. Looney gdb_end_write(void *arg) 164beb24065SJonathan T. Looney { 165beb24065SJonathan T. Looney 1662337dc64SKonstantin Belousov restore_wp(arg != NULL); 167beb24065SJonathan T. Looney } 168