xref: /freebsd/sys/amd64/amd64/gdb_machdep.c (revision 03b3d7bbec99742910e2d0d5e7c23754eca9b2c4)
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>
37b7924341SAndrew Turner #include <sys/reg.h>
3872d44f31SMarcel Moolenaar #include <sys/signal.h>
3972d44f31SMarcel Moolenaar 
40beb24065SJonathan T. Looney #include <machine/cpufunc.h>
4172d44f31SMarcel Moolenaar #include <machine/frame.h>
4272d44f31SMarcel Moolenaar #include <machine/gdb_machdep.h>
432337dc64SKonstantin Belousov #include <machine/md_var.h>
44a649898dSPeter Wemm #include <machine/pcb.h>
4572d44f31SMarcel Moolenaar #include <machine/psl.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>
527446b088SMitchell Horne #include <gdb/gdb_int.h>
5372d44f31SMarcel Moolenaar 
5472d44f31SMarcel Moolenaar void *
5572d44f31SMarcel Moolenaar gdb_cpu_getreg(int regnum, size_t *regsz)
5672d44f31SMarcel Moolenaar {
575eb95e11SJohn Baldwin 	static uint32_t _kcodesel = GSEL(GCODE_SEL, SEL_KPL);
585eb95e11SJohn Baldwin 	static uint32_t _kdatasel = GSEL(GDATA_SEL, SEL_KPL);
5972d44f31SMarcel Moolenaar 
6072d44f31SMarcel Moolenaar 	*regsz = gdb_cpu_regsz(regnum);
61a649898dSPeter Wemm 
62a649898dSPeter Wemm 	if (kdb_thread  == curthread) {
6372d44f31SMarcel Moolenaar 		switch (regnum) {
6472939459SMitchell Horne 		case GDB_REG_RAX: return (&kdb_frame->tf_rax);
6572939459SMitchell Horne 		case GDB_REG_RCX: return (&kdb_frame->tf_rcx);
6672939459SMitchell Horne 		case GDB_REG_RDX: return (&kdb_frame->tf_rdx);
6772939459SMitchell Horne 		case GDB_REG_RSI: return (&kdb_frame->tf_rsi);
6872939459SMitchell Horne 		case GDB_REG_RDI: return (&kdb_frame->tf_rdi);
6972939459SMitchell Horne 		case GDB_REG_R8:  return (&kdb_frame->tf_r8);
7072939459SMitchell Horne 		case GDB_REG_R9:  return (&kdb_frame->tf_r9);
7172939459SMitchell Horne 		case GDB_REG_R10: return (&kdb_frame->tf_r10);
7272939459SMitchell Horne 		case GDB_REG_R11: return (&kdb_frame->tf_r11);
7372939459SMitchell Horne 		case GDB_REG_RFLAGS: return (&kdb_frame->tf_rflags);
7472939459SMitchell Horne 		case GDB_REG_CS:  return (&kdb_frame->tf_cs);
7572939459SMitchell Horne 		case GDB_REG_SS:  return (&kdb_frame->tf_ss);
76a649898dSPeter Wemm 		}
77a649898dSPeter Wemm 	}
78a649898dSPeter Wemm 	switch (regnum) {
7972939459SMitchell Horne 	case GDB_REG_RBX: return (&kdb_thrctx->pcb_rbx);
8072939459SMitchell Horne 	case GDB_REG_RBP: return (&kdb_thrctx->pcb_rbp);
8172939459SMitchell Horne 	case GDB_REG_RSP: return (&kdb_thrctx->pcb_rsp);
8272939459SMitchell Horne 	case GDB_REG_R12: return (&kdb_thrctx->pcb_r12);
8372939459SMitchell Horne 	case GDB_REG_R13: return (&kdb_thrctx->pcb_r13);
8472939459SMitchell Horne 	case GDB_REG_R14: return (&kdb_thrctx->pcb_r14);
8572939459SMitchell Horne 	case GDB_REG_R15: return (&kdb_thrctx->pcb_r15);
8672939459SMitchell Horne 	case GDB_REG_PC:  return (&kdb_thrctx->pcb_rip);
8772939459SMitchell Horne 	case GDB_REG_CS:  return (&_kcodesel);
8872939459SMitchell Horne 	case GDB_REG_SS:  return (&_kdatasel);
8972d44f31SMarcel Moolenaar 	}
9072d44f31SMarcel Moolenaar 	return (NULL);
9172d44f31SMarcel Moolenaar }
9272d44f31SMarcel Moolenaar 
9372d44f31SMarcel Moolenaar void
94bcc5241cSMarcel Moolenaar gdb_cpu_setreg(int regnum, void *val)
9572d44f31SMarcel Moolenaar {
960ef474deSMitchell Horne 	register_t regval = *(register_t *)val;
9772d44f31SMarcel Moolenaar 
980ef474deSMitchell Horne 	/*
990ef474deSMitchell Horne 	 * Write registers to the trapframe and pcb, if applicable.
1000ef474deSMitchell Horne 	 * Some scratch registers are not tracked by the pcb.
1010ef474deSMitchell Horne 	 */
1020ef474deSMitchell Horne 	if (kdb_thread == curthread) {
10372d44f31SMarcel Moolenaar 		switch (regnum) {
1040ef474deSMitchell Horne 		case GDB_REG_RAX: kdb_frame->tf_rax = regval; break;
1050ef474deSMitchell Horne 		case GDB_REG_RBX: kdb_frame->tf_rbx = regval; break;
1060ef474deSMitchell Horne 		case GDB_REG_RCX: kdb_frame->tf_rcx = regval; break;
1070ef474deSMitchell Horne 		case GDB_REG_RDX: kdb_frame->tf_rdx = regval; break;
1080ef474deSMitchell Horne 		case GDB_REG_RSI: kdb_frame->tf_rsi = regval; break;
1090ef474deSMitchell Horne 		case GDB_REG_RDI: kdb_frame->tf_rdi = regval; break;
1100ef474deSMitchell Horne 		case GDB_REG_RBP: kdb_frame->tf_rbp = regval; break;
1110ef474deSMitchell Horne 		case GDB_REG_RSP: kdb_frame->tf_rsp = regval; break;
1120ef474deSMitchell Horne 		case GDB_REG_R8:  kdb_frame->tf_r8  = regval; break;
1130ef474deSMitchell Horne 		case GDB_REG_R9:  kdb_frame->tf_r9  = regval; break;
1140ef474deSMitchell Horne 		case GDB_REG_R10: kdb_frame->tf_r10 = regval; break;
1150ef474deSMitchell Horne 		case GDB_REG_R11: kdb_frame->tf_r11 = regval; break;
1160ef474deSMitchell Horne 		case GDB_REG_R12: kdb_frame->tf_r12 = regval; break;
1170ef474deSMitchell Horne 		case GDB_REG_R13: kdb_frame->tf_r13 = regval; break;
1180ef474deSMitchell Horne 		case GDB_REG_R14: kdb_frame->tf_r14 = regval; break;
1190ef474deSMitchell Horne 		case GDB_REG_R15: kdb_frame->tf_r15 = regval; break;
1200ef474deSMitchell Horne 		case GDB_REG_PC:  kdb_frame->tf_rip = regval; break;
1210ef474deSMitchell Horne 		}
1220ef474deSMitchell Horne 	}
1230ef474deSMitchell Horne 	switch (regnum) {
1240ef474deSMitchell Horne 	case GDB_REG_RBX: kdb_thrctx->pcb_rbx = regval; break;
1250ef474deSMitchell Horne 	case GDB_REG_RBP: kdb_thrctx->pcb_rbp = regval; break;
1260ef474deSMitchell Horne 	case GDB_REG_RSP: kdb_thrctx->pcb_rsp = regval; break;
1270ef474deSMitchell Horne 	case GDB_REG_R12: kdb_thrctx->pcb_r12 = regval; break;
1280ef474deSMitchell Horne 	case GDB_REG_R13: kdb_thrctx->pcb_r13 = regval; break;
1290ef474deSMitchell Horne 	case GDB_REG_R14: kdb_thrctx->pcb_r14 = regval; break;
1300ef474deSMitchell Horne 	case GDB_REG_R15: kdb_thrctx->pcb_r15 = regval; break;
1310ef474deSMitchell Horne 	case GDB_REG_PC:  kdb_thrctx->pcb_rip = regval; break;
13272d44f31SMarcel Moolenaar 	}
13372d44f31SMarcel Moolenaar }
134b1fb1bb1SMarcel Moolenaar 
135b1fb1bb1SMarcel Moolenaar int
136b1fb1bb1SMarcel Moolenaar gdb_cpu_signal(int type, int code)
137b1fb1bb1SMarcel Moolenaar {
138b1fb1bb1SMarcel Moolenaar 
139*03b3d7bbSMitchell Horne 	switch (type) {
140962c06c5SMitchell Horne 	case T_BPTFLT: return (SIGTRAP);
141962c06c5SMitchell Horne 	case T_ARITHTRAP: return (SIGFPE);
142962c06c5SMitchell Horne 	case T_PROTFLT: return (SIGSEGV);
143962c06c5SMitchell Horne 	case T_TRCTRAP: return (SIGTRAP);
144962c06c5SMitchell Horne 	case T_PAGEFLT: return (SIGSEGV);
145962c06c5SMitchell Horne 	case T_DIVIDE: return (SIGFPE);
146962c06c5SMitchell Horne 	case T_NMI: return (SIGTRAP);
147962c06c5SMitchell Horne 	case T_FPOPFLT: return (SIGILL);
148962c06c5SMitchell Horne 	case T_TSSFLT: return (SIGSEGV);
149962c06c5SMitchell Horne 	case T_SEGNPFLT: return (SIGSEGV);
150962c06c5SMitchell Horne 	case T_STKFLT: return (SIGSEGV);
151962c06c5SMitchell Horne 	case T_XMMFLT: return (SIGFPE);
152b1fb1bb1SMarcel Moolenaar 	}
153b1fb1bb1SMarcel Moolenaar 	return (SIGEMT);
154b1fb1bb1SMarcel Moolenaar }
155beb24065SJonathan T. Looney 
1567446b088SMitchell Horne void
1577446b088SMitchell Horne gdb_cpu_stop_reason(int type, int code)
1587446b088SMitchell Horne {
1597446b088SMitchell Horne 	uintmax_t val;
1607446b088SMitchell Horne 
1617446b088SMitchell Horne 	val = 0;
1627446b088SMitchell Horne 	if (type == T_TRCTRAP) {
1637446b088SMitchell Horne 		/* NB: 'code' contains the value of dr6 at the trap. */
1647446b088SMitchell Horne 		if ((code & DBREG_DR6_B(0)) != 0) {
1657446b088SMitchell Horne 			val = rdr0();
1667446b088SMitchell Horne 		}
1677446b088SMitchell Horne 		if ((code & DBREG_DR6_B(1)) != 0) {
1687446b088SMitchell Horne 			val = rdr1();
1697446b088SMitchell Horne 		}
1707446b088SMitchell Horne 		if ((code & DBREG_DR6_B(2)) != 0) {
1717446b088SMitchell Horne 			val = rdr2();
1727446b088SMitchell Horne 		}
1737446b088SMitchell Horne 		if ((code & DBREG_DR6_B(3)) != 0) {
1747446b088SMitchell Horne 			val = rdr3();
1757446b088SMitchell Horne 		}
1767446b088SMitchell Horne 
1777446b088SMitchell Horne 		/*
1787446b088SMitchell Horne 		 * TODO: validate the bits in DR7 to differentiate between a
1797446b088SMitchell Horne 		 * watchpoint trap and a hardware breakpoint trap (currently
1807446b088SMitchell Horne 		 * unsupported).
1817446b088SMitchell Horne 		 */
1827446b088SMitchell Horne 		if (val != 0) {
1837446b088SMitchell Horne 			gdb_tx_str("watch:");
1847446b088SMitchell Horne 			gdb_tx_varhex(val);
1857446b088SMitchell Horne 			gdb_tx_char(';');
1867446b088SMitchell Horne 		}
1877446b088SMitchell Horne 	}
1887446b088SMitchell Horne }
1897446b088SMitchell Horne 
190beb24065SJonathan T. Looney void *
191beb24065SJonathan T. Looney gdb_begin_write(void)
192beb24065SJonathan T. Looney {
193beb24065SJonathan T. Looney 
1942337dc64SKonstantin Belousov 	return (disable_wp() ? &gdb_begin_write : NULL);
195beb24065SJonathan T. Looney }
196beb24065SJonathan T. Looney 
197beb24065SJonathan T. Looney void
198beb24065SJonathan T. Looney gdb_end_write(void *arg)
199beb24065SJonathan T. Looney {
200beb24065SJonathan T. Looney 
2012337dc64SKonstantin Belousov 	restore_wp(arg != NULL);
202beb24065SJonathan T. Looney }
203