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 39*beb24065SJonathan T. Looney #include <machine/cpufunc.h> 4072d44f31SMarcel Moolenaar #include <machine/frame.h> 4172d44f31SMarcel Moolenaar #include <machine/gdb_machdep.h> 42a649898dSPeter Wemm #include <machine/pcb.h> 4372d44f31SMarcel Moolenaar #include <machine/psl.h> 4472d44f31SMarcel Moolenaar #include <machine/reg.h> 45*beb24065SJonathan T. Looney #include <machine/specialreg.h> 4672d44f31SMarcel Moolenaar #include <machine/trap.h> 47a649898dSPeter Wemm #include <machine/frame.h> 48a649898dSPeter Wemm #include <machine/endian.h> 4972d44f31SMarcel Moolenaar 5072d44f31SMarcel Moolenaar #include <gdb/gdb.h> 5172d44f31SMarcel Moolenaar 5272d44f31SMarcel Moolenaar void * 5372d44f31SMarcel Moolenaar gdb_cpu_getreg(int regnum, size_t *regsz) 5472d44f31SMarcel Moolenaar { 555eb95e11SJohn Baldwin static uint32_t _kcodesel = GSEL(GCODE_SEL, SEL_KPL); 565eb95e11SJohn Baldwin static uint32_t _kdatasel = GSEL(GDATA_SEL, SEL_KPL); 5772d44f31SMarcel Moolenaar 5872d44f31SMarcel Moolenaar *regsz = gdb_cpu_regsz(regnum); 59a649898dSPeter Wemm 60a649898dSPeter Wemm if (kdb_thread == curthread) { 6172d44f31SMarcel Moolenaar switch (regnum) { 62a649898dSPeter Wemm case 0: return (&kdb_frame->tf_rax); 63a649898dSPeter Wemm case 2: return (&kdb_frame->tf_rcx); 64a649898dSPeter Wemm case 3: return (&kdb_frame->tf_rdx); 65a649898dSPeter Wemm case 4: return (&kdb_frame->tf_rsi); 66a649898dSPeter Wemm case 5: return (&kdb_frame->tf_rdi); 67a649898dSPeter Wemm case 8: return (&kdb_frame->tf_r8); 68a649898dSPeter Wemm case 9: return (&kdb_frame->tf_r9); 69a649898dSPeter Wemm case 10: return (&kdb_frame->tf_r10); 70a649898dSPeter Wemm case 11: return (&kdb_frame->tf_r11); 711acc225fSPeter Wemm case 17: return (&kdb_frame->tf_rflags); 72a649898dSPeter Wemm case 18: return (&kdb_frame->tf_cs); 73a649898dSPeter Wemm case 19: return (&kdb_frame->tf_ss); 74a649898dSPeter Wemm } 75a649898dSPeter Wemm } 76a649898dSPeter Wemm switch (regnum) { 77a649898dSPeter Wemm case 1: return (&kdb_thrctx->pcb_rbx); 78a649898dSPeter Wemm case 6: return (&kdb_thrctx->pcb_rbp); 79a649898dSPeter Wemm case 7: return (&kdb_thrctx->pcb_rsp); 80a649898dSPeter Wemm case 12: return (&kdb_thrctx->pcb_r12); 81a649898dSPeter Wemm case 13: return (&kdb_thrctx->pcb_r13); 82a649898dSPeter Wemm case 14: return (&kdb_thrctx->pcb_r14); 83a649898dSPeter Wemm case 15: return (&kdb_thrctx->pcb_r15); 84a649898dSPeter Wemm case 16: return (&kdb_thrctx->pcb_rip); 855eb95e11SJohn Baldwin case 18: return (&_kcodesel); 865eb95e11SJohn Baldwin case 19: return (&_kdatasel); 8772d44f31SMarcel Moolenaar } 8872d44f31SMarcel Moolenaar return (NULL); 8972d44f31SMarcel Moolenaar } 9072d44f31SMarcel Moolenaar 9172d44f31SMarcel Moolenaar void 92bcc5241cSMarcel Moolenaar gdb_cpu_setreg(int regnum, void *val) 9372d44f31SMarcel Moolenaar { 9472d44f31SMarcel Moolenaar 9572d44f31SMarcel Moolenaar switch (regnum) { 96a649898dSPeter Wemm case GDB_REG_PC: 97bcc5241cSMarcel Moolenaar kdb_thrctx->pcb_rip = *(register_t *)val; 98a649898dSPeter Wemm if (kdb_thread == curthread) 99bcc5241cSMarcel Moolenaar kdb_frame->tf_rip = *(register_t *)val; 10072d44f31SMarcel Moolenaar } 10172d44f31SMarcel Moolenaar } 102b1fb1bb1SMarcel Moolenaar 103b1fb1bb1SMarcel Moolenaar int 104b1fb1bb1SMarcel Moolenaar gdb_cpu_signal(int type, int code) 105b1fb1bb1SMarcel Moolenaar { 106b1fb1bb1SMarcel Moolenaar 107b1fb1bb1SMarcel Moolenaar switch (type & ~T_USER) { 108b1fb1bb1SMarcel Moolenaar case 0: return (SIGFPE); /* Divide by zero. */ 109b1fb1bb1SMarcel Moolenaar case 1: return (SIGTRAP); /* Debug exception. */ 110b1fb1bb1SMarcel Moolenaar case 3: return (SIGTRAP); /* Breakpoint. */ 111b1fb1bb1SMarcel Moolenaar case 4: return (SIGSEGV); /* into instr. (overflow). */ 112b1fb1bb1SMarcel Moolenaar case 5: return (SIGURG); /* bound instruction. */ 113b1fb1bb1SMarcel Moolenaar case 6: return (SIGILL); /* Invalid opcode. */ 114b1fb1bb1SMarcel Moolenaar case 7: return (SIGFPE); /* Coprocessor not present. */ 115b1fb1bb1SMarcel Moolenaar case 8: return (SIGEMT); /* Double fault. */ 116b1fb1bb1SMarcel Moolenaar case 9: return (SIGSEGV); /* Coprocessor segment overrun. */ 117b1fb1bb1SMarcel Moolenaar case 10: return (SIGTRAP); /* Invalid TSS (also single-step). */ 118b1fb1bb1SMarcel Moolenaar case 11: return (SIGSEGV); /* Segment not present. */ 119b1fb1bb1SMarcel Moolenaar case 12: return (SIGSEGV); /* Stack exception. */ 120b1fb1bb1SMarcel Moolenaar case 13: return (SIGSEGV); /* General protection. */ 121b1fb1bb1SMarcel Moolenaar case 14: return (SIGSEGV); /* Page fault. */ 122b1fb1bb1SMarcel Moolenaar case 16: return (SIGEMT); /* Coprocessor error. */ 123b1fb1bb1SMarcel Moolenaar } 124b1fb1bb1SMarcel Moolenaar return (SIGEMT); 125b1fb1bb1SMarcel Moolenaar } 126*beb24065SJonathan T. Looney 127*beb24065SJonathan T. Looney void * 128*beb24065SJonathan T. Looney gdb_begin_write(void) 129*beb24065SJonathan T. Looney { 130*beb24065SJonathan T. Looney u_long cr0save; 131*beb24065SJonathan T. Looney 132*beb24065SJonathan T. Looney cr0save = rcr0(); 133*beb24065SJonathan T. Looney load_cr0(cr0save & ~CR0_WP); 134*beb24065SJonathan T. Looney return ((void *)cr0save); 135*beb24065SJonathan T. Looney } 136*beb24065SJonathan T. Looney 137*beb24065SJonathan T. Looney void 138*beb24065SJonathan T. Looney gdb_end_write(void *arg) 139*beb24065SJonathan T. Looney { 140*beb24065SJonathan T. Looney 141*beb24065SJonathan T. Looney load_cr0((u_long)arg); 142*beb24065SJonathan T. Looney } 143*beb24065SJonathan T. Looney 144