146280ae7SWarner Losh /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
3072d44f31SMarcel Moolenaar #include <sys/systm.h>
3172d44f31SMarcel Moolenaar #include <sys/kdb.h>
3272d44f31SMarcel Moolenaar #include <sys/kernel.h>
3372d44f31SMarcel Moolenaar #include <sys/proc.h>
34b7924341SAndrew Turner #include <sys/reg.h>
3572d44f31SMarcel Moolenaar #include <sys/signal.h>
3672d44f31SMarcel Moolenaar
37beb24065SJonathan T. Looney #include <machine/cpufunc.h>
3872d44f31SMarcel Moolenaar #include <machine/frame.h>
3972d44f31SMarcel Moolenaar #include <machine/gdb_machdep.h>
402337dc64SKonstantin Belousov #include <machine/md_var.h>
41a649898dSPeter Wemm #include <machine/pcb.h>
4272d44f31SMarcel Moolenaar #include <machine/psl.h>
43beb24065SJonathan T. Looney #include <machine/specialreg.h>
4472d44f31SMarcel Moolenaar #include <machine/trap.h>
45a649898dSPeter Wemm #include <machine/frame.h>
46a649898dSPeter Wemm #include <machine/endian.h>
4772d44f31SMarcel Moolenaar
4872d44f31SMarcel Moolenaar #include <gdb/gdb.h>
497446b088SMitchell Horne #include <gdb/gdb_int.h>
5072d44f31SMarcel Moolenaar
5172d44f31SMarcel Moolenaar void *
gdb_cpu_getreg(int regnum,size_t * regsz)5272d44f31SMarcel Moolenaar gdb_cpu_getreg(int regnum, size_t *regsz)
5372d44f31SMarcel Moolenaar {
545eb95e11SJohn Baldwin static uint32_t _kcodesel = GSEL(GCODE_SEL, SEL_KPL);
555eb95e11SJohn Baldwin static uint32_t _kdatasel = GSEL(GDATA_SEL, SEL_KPL);
5672d44f31SMarcel Moolenaar
5772d44f31SMarcel Moolenaar *regsz = gdb_cpu_regsz(regnum);
58a649898dSPeter Wemm
59a649898dSPeter Wemm if (kdb_thread == curthread) {
6072d44f31SMarcel Moolenaar switch (regnum) {
6172939459SMitchell Horne case GDB_REG_RAX: return (&kdb_frame->tf_rax);
6272939459SMitchell Horne case GDB_REG_RCX: return (&kdb_frame->tf_rcx);
6372939459SMitchell Horne case GDB_REG_RDX: return (&kdb_frame->tf_rdx);
6472939459SMitchell Horne case GDB_REG_RSI: return (&kdb_frame->tf_rsi);
6572939459SMitchell Horne case GDB_REG_RDI: return (&kdb_frame->tf_rdi);
6672939459SMitchell Horne case GDB_REG_R8: return (&kdb_frame->tf_r8);
6772939459SMitchell Horne case GDB_REG_R9: return (&kdb_frame->tf_r9);
6872939459SMitchell Horne case GDB_REG_R10: return (&kdb_frame->tf_r10);
6972939459SMitchell Horne case GDB_REG_R11: return (&kdb_frame->tf_r11);
7072939459SMitchell Horne case GDB_REG_RFLAGS: return (&kdb_frame->tf_rflags);
7172939459SMitchell Horne case GDB_REG_CS: return (&kdb_frame->tf_cs);
7272939459SMitchell Horne case GDB_REG_SS: return (&kdb_frame->tf_ss);
73a649898dSPeter Wemm }
74a649898dSPeter Wemm }
75a649898dSPeter Wemm switch (regnum) {
7672939459SMitchell Horne case GDB_REG_RBX: return (&kdb_thrctx->pcb_rbx);
7772939459SMitchell Horne case GDB_REG_RBP: return (&kdb_thrctx->pcb_rbp);
7872939459SMitchell Horne case GDB_REG_RSP: return (&kdb_thrctx->pcb_rsp);
7972939459SMitchell Horne case GDB_REG_R12: return (&kdb_thrctx->pcb_r12);
8072939459SMitchell Horne case GDB_REG_R13: return (&kdb_thrctx->pcb_r13);
8172939459SMitchell Horne case GDB_REG_R14: return (&kdb_thrctx->pcb_r14);
8272939459SMitchell Horne case GDB_REG_R15: return (&kdb_thrctx->pcb_r15);
8372939459SMitchell Horne case GDB_REG_PC: return (&kdb_thrctx->pcb_rip);
8472939459SMitchell Horne case GDB_REG_CS: return (&_kcodesel);
8572939459SMitchell Horne case GDB_REG_SS: return (&_kdatasel);
8672d44f31SMarcel Moolenaar }
8772d44f31SMarcel Moolenaar return (NULL);
8872d44f31SMarcel Moolenaar }
8972d44f31SMarcel Moolenaar
9072d44f31SMarcel Moolenaar void
gdb_cpu_setreg(int regnum,void * val)91bcc5241cSMarcel Moolenaar gdb_cpu_setreg(int regnum, void *val)
9272d44f31SMarcel Moolenaar {
930ef474deSMitchell Horne register_t regval = *(register_t *)val;
9472d44f31SMarcel Moolenaar
950ef474deSMitchell Horne /*
960ef474deSMitchell Horne * Write registers to the trapframe and pcb, if applicable.
970ef474deSMitchell Horne * Some scratch registers are not tracked by the pcb.
980ef474deSMitchell Horne */
990ef474deSMitchell Horne if (kdb_thread == curthread) {
10072d44f31SMarcel Moolenaar switch (regnum) {
1010ef474deSMitchell Horne case GDB_REG_RAX: kdb_frame->tf_rax = regval; break;
1020ef474deSMitchell Horne case GDB_REG_RBX: kdb_frame->tf_rbx = regval; break;
1030ef474deSMitchell Horne case GDB_REG_RCX: kdb_frame->tf_rcx = regval; break;
1040ef474deSMitchell Horne case GDB_REG_RDX: kdb_frame->tf_rdx = regval; break;
1050ef474deSMitchell Horne case GDB_REG_RSI: kdb_frame->tf_rsi = regval; break;
1060ef474deSMitchell Horne case GDB_REG_RDI: kdb_frame->tf_rdi = regval; break;
1070ef474deSMitchell Horne case GDB_REG_RBP: kdb_frame->tf_rbp = regval; break;
1080ef474deSMitchell Horne case GDB_REG_RSP: kdb_frame->tf_rsp = regval; break;
1090ef474deSMitchell Horne case GDB_REG_R8: kdb_frame->tf_r8 = regval; break;
1100ef474deSMitchell Horne case GDB_REG_R9: kdb_frame->tf_r9 = regval; break;
1110ef474deSMitchell Horne case GDB_REG_R10: kdb_frame->tf_r10 = regval; break;
1120ef474deSMitchell Horne case GDB_REG_R11: kdb_frame->tf_r11 = regval; break;
1130ef474deSMitchell Horne case GDB_REG_R12: kdb_frame->tf_r12 = regval; break;
1140ef474deSMitchell Horne case GDB_REG_R13: kdb_frame->tf_r13 = regval; break;
1150ef474deSMitchell Horne case GDB_REG_R14: kdb_frame->tf_r14 = regval; break;
1160ef474deSMitchell Horne case GDB_REG_R15: kdb_frame->tf_r15 = regval; break;
1170ef474deSMitchell Horne case GDB_REG_PC: kdb_frame->tf_rip = regval; break;
1180ef474deSMitchell Horne }
1190ef474deSMitchell Horne }
1200ef474deSMitchell Horne switch (regnum) {
1210ef474deSMitchell Horne case GDB_REG_RBX: kdb_thrctx->pcb_rbx = regval; break;
1220ef474deSMitchell Horne case GDB_REG_RBP: kdb_thrctx->pcb_rbp = regval; break;
1230ef474deSMitchell Horne case GDB_REG_RSP: kdb_thrctx->pcb_rsp = regval; break;
1240ef474deSMitchell Horne case GDB_REG_R12: kdb_thrctx->pcb_r12 = regval; break;
1250ef474deSMitchell Horne case GDB_REG_R13: kdb_thrctx->pcb_r13 = regval; break;
1260ef474deSMitchell Horne case GDB_REG_R14: kdb_thrctx->pcb_r14 = regval; break;
1270ef474deSMitchell Horne case GDB_REG_R15: kdb_thrctx->pcb_r15 = regval; break;
1280ef474deSMitchell Horne case GDB_REG_PC: kdb_thrctx->pcb_rip = regval; break;
12972d44f31SMarcel Moolenaar }
13072d44f31SMarcel Moolenaar }
131b1fb1bb1SMarcel Moolenaar
132b1fb1bb1SMarcel Moolenaar int
gdb_cpu_signal(int type,int code)133b1fb1bb1SMarcel Moolenaar gdb_cpu_signal(int type, int code)
134b1fb1bb1SMarcel Moolenaar {
135b1fb1bb1SMarcel Moolenaar
13603b3d7bbSMitchell Horne switch (type) {
137962c06c5SMitchell Horne case T_BPTFLT: return (SIGTRAP);
138962c06c5SMitchell Horne case T_ARITHTRAP: return (SIGFPE);
139962c06c5SMitchell Horne case T_PROTFLT: return (SIGSEGV);
140962c06c5SMitchell Horne case T_TRCTRAP: return (SIGTRAP);
141962c06c5SMitchell Horne case T_PAGEFLT: return (SIGSEGV);
142962c06c5SMitchell Horne case T_DIVIDE: return (SIGFPE);
143962c06c5SMitchell Horne case T_NMI: return (SIGTRAP);
144962c06c5SMitchell Horne case T_FPOPFLT: return (SIGILL);
145962c06c5SMitchell Horne case T_TSSFLT: return (SIGSEGV);
146962c06c5SMitchell Horne case T_SEGNPFLT: return (SIGSEGV);
147962c06c5SMitchell Horne case T_STKFLT: return (SIGSEGV);
148962c06c5SMitchell Horne case T_XMMFLT: return (SIGFPE);
149b1fb1bb1SMarcel Moolenaar }
150b1fb1bb1SMarcel Moolenaar return (SIGEMT);
151b1fb1bb1SMarcel Moolenaar }
152beb24065SJonathan T. Looney
1537446b088SMitchell Horne void
gdb_cpu_stop_reason(int type,int code)1547446b088SMitchell Horne gdb_cpu_stop_reason(int type, int code)
1557446b088SMitchell Horne {
1567446b088SMitchell Horne uintmax_t val;
1577446b088SMitchell Horne
1587446b088SMitchell Horne val = 0;
1597446b088SMitchell Horne if (type == T_TRCTRAP) {
1607446b088SMitchell Horne /* NB: 'code' contains the value of dr6 at the trap. */
1617446b088SMitchell Horne if ((code & DBREG_DR6_B(0)) != 0) {
1627446b088SMitchell Horne val = rdr0();
1637446b088SMitchell Horne }
1647446b088SMitchell Horne if ((code & DBREG_DR6_B(1)) != 0) {
1657446b088SMitchell Horne val = rdr1();
1667446b088SMitchell Horne }
1677446b088SMitchell Horne if ((code & DBREG_DR6_B(2)) != 0) {
1687446b088SMitchell Horne val = rdr2();
1697446b088SMitchell Horne }
1707446b088SMitchell Horne if ((code & DBREG_DR6_B(3)) != 0) {
1717446b088SMitchell Horne val = rdr3();
1727446b088SMitchell Horne }
1737446b088SMitchell Horne
1747446b088SMitchell Horne /*
1757446b088SMitchell Horne * TODO: validate the bits in DR7 to differentiate between a
1767446b088SMitchell Horne * watchpoint trap and a hardware breakpoint trap (currently
1777446b088SMitchell Horne * unsupported).
1787446b088SMitchell Horne */
1797446b088SMitchell Horne if (val != 0) {
1807446b088SMitchell Horne gdb_tx_str("watch:");
1817446b088SMitchell Horne gdb_tx_varhex(val);
1827446b088SMitchell Horne gdb_tx_char(';');
1837446b088SMitchell Horne }
1847446b088SMitchell Horne }
1857446b088SMitchell Horne }
1867446b088SMitchell Horne
187beb24065SJonathan T. Looney void *
gdb_begin_write(void)188beb24065SJonathan T. Looney gdb_begin_write(void)
189beb24065SJonathan T. Looney {
190beb24065SJonathan T. Looney
1912337dc64SKonstantin Belousov return (disable_wp() ? &gdb_begin_write : NULL);
192beb24065SJonathan T. Looney }
193beb24065SJonathan T. Looney
194beb24065SJonathan T. Looney void
gdb_end_write(void * arg)195beb24065SJonathan T. Looney gdb_end_write(void *arg)
196beb24065SJonathan T. Looney {
197beb24065SJonathan T. Looney
1982337dc64SKonstantin Belousov restore_wp(arg != NULL);
199beb24065SJonathan T. Looney }
200