xref: /freebsd/sys/amd64/amd64/gdb_machdep.c (revision c49761dd572ca667babda4253e14498c7161e21a)
146280ae7SWarner Losh /*-
2*c49761ddSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*c49761ddSPedro 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 
3972d44f31SMarcel Moolenaar #include <machine/frame.h>
4072d44f31SMarcel Moolenaar #include <machine/gdb_machdep.h>
41a649898dSPeter Wemm #include <machine/pcb.h>
4272d44f31SMarcel Moolenaar #include <machine/psl.h>
4372d44f31SMarcel Moolenaar #include <machine/reg.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>
4972d44f31SMarcel Moolenaar 
5072d44f31SMarcel Moolenaar void *
5172d44f31SMarcel Moolenaar gdb_cpu_getreg(int regnum, size_t *regsz)
5272d44f31SMarcel Moolenaar {
535eb95e11SJohn Baldwin 	static uint32_t _kcodesel = GSEL(GCODE_SEL, SEL_KPL);
545eb95e11SJohn Baldwin 	static uint32_t _kdatasel = GSEL(GDATA_SEL, SEL_KPL);
5572d44f31SMarcel Moolenaar 
5672d44f31SMarcel Moolenaar 	*regsz = gdb_cpu_regsz(regnum);
57a649898dSPeter Wemm 
58a649898dSPeter Wemm 	if (kdb_thread  == curthread) {
5972d44f31SMarcel Moolenaar 		switch (regnum) {
60a649898dSPeter Wemm 		case 0:	return (&kdb_frame->tf_rax);
61a649898dSPeter Wemm 		case 2:	return (&kdb_frame->tf_rcx);
62a649898dSPeter Wemm 		case 3:	return (&kdb_frame->tf_rdx);
63a649898dSPeter Wemm 		case 4:	return (&kdb_frame->tf_rsi);
64a649898dSPeter Wemm 		case 5:	return (&kdb_frame->tf_rdi);
65a649898dSPeter Wemm 		case 8: return (&kdb_frame->tf_r8);
66a649898dSPeter Wemm 		case 9: return (&kdb_frame->tf_r9);
67a649898dSPeter Wemm 		case 10: return (&kdb_frame->tf_r10);
68a649898dSPeter Wemm 		case 11: return (&kdb_frame->tf_r11);
691acc225fSPeter Wemm 		case 17: return (&kdb_frame->tf_rflags);
70a649898dSPeter Wemm 		case 18: return (&kdb_frame->tf_cs);
71a649898dSPeter Wemm 		case 19: return (&kdb_frame->tf_ss);
72a649898dSPeter Wemm 		}
73a649898dSPeter Wemm 	}
74a649898dSPeter Wemm 	switch (regnum) {
75a649898dSPeter Wemm 	case 1:  return (&kdb_thrctx->pcb_rbx);
76a649898dSPeter Wemm 	case 6:  return (&kdb_thrctx->pcb_rbp);
77a649898dSPeter Wemm 	case 7:  return (&kdb_thrctx->pcb_rsp);
78a649898dSPeter Wemm 	case 12: return (&kdb_thrctx->pcb_r12);
79a649898dSPeter Wemm 	case 13: return (&kdb_thrctx->pcb_r13);
80a649898dSPeter Wemm 	case 14: return (&kdb_thrctx->pcb_r14);
81a649898dSPeter Wemm 	case 15: return (&kdb_thrctx->pcb_r15);
82a649898dSPeter Wemm 	case 16: return (&kdb_thrctx->pcb_rip);
835eb95e11SJohn Baldwin 	case 18: return (&_kcodesel);
845eb95e11SJohn Baldwin 	case 19: return (&_kdatasel);
8572d44f31SMarcel Moolenaar 	}
8672d44f31SMarcel Moolenaar 	return (NULL);
8772d44f31SMarcel Moolenaar }
8872d44f31SMarcel Moolenaar 
8972d44f31SMarcel Moolenaar void
90bcc5241cSMarcel Moolenaar gdb_cpu_setreg(int regnum, void *val)
9172d44f31SMarcel Moolenaar {
9272d44f31SMarcel Moolenaar 
9372d44f31SMarcel Moolenaar 	switch (regnum) {
94a649898dSPeter Wemm 	case GDB_REG_PC:
95bcc5241cSMarcel Moolenaar 		kdb_thrctx->pcb_rip = *(register_t *)val;
96a649898dSPeter Wemm 		if (kdb_thread  == curthread)
97bcc5241cSMarcel Moolenaar 			kdb_frame->tf_rip = *(register_t *)val;
9872d44f31SMarcel Moolenaar 	}
9972d44f31SMarcel Moolenaar }
100b1fb1bb1SMarcel Moolenaar 
101b1fb1bb1SMarcel Moolenaar int
102b1fb1bb1SMarcel Moolenaar gdb_cpu_signal(int type, int code)
103b1fb1bb1SMarcel Moolenaar {
104b1fb1bb1SMarcel Moolenaar 
105b1fb1bb1SMarcel Moolenaar 	switch (type & ~T_USER) {
106b1fb1bb1SMarcel Moolenaar 	case 0: return (SIGFPE);	/* Divide by zero. */
107b1fb1bb1SMarcel Moolenaar 	case 1: return (SIGTRAP);	/* Debug exception. */
108b1fb1bb1SMarcel Moolenaar 	case 3: return (SIGTRAP);	/* Breakpoint. */
109b1fb1bb1SMarcel Moolenaar 	case 4: return (SIGSEGV);	/* into instr. (overflow). */
110b1fb1bb1SMarcel Moolenaar 	case 5: return (SIGURG);	/* bound instruction. */
111b1fb1bb1SMarcel Moolenaar 	case 6: return (SIGILL);	/* Invalid opcode. */
112b1fb1bb1SMarcel Moolenaar 	case 7: return (SIGFPE);	/* Coprocessor not present. */
113b1fb1bb1SMarcel Moolenaar 	case 8: return (SIGEMT);	/* Double fault. */
114b1fb1bb1SMarcel Moolenaar 	case 9: return (SIGSEGV);	/* Coprocessor segment overrun. */
115b1fb1bb1SMarcel Moolenaar 	case 10: return (SIGTRAP);	/* Invalid TSS (also single-step). */
116b1fb1bb1SMarcel Moolenaar 	case 11: return (SIGSEGV);	/* Segment not present. */
117b1fb1bb1SMarcel Moolenaar 	case 12: return (SIGSEGV);	/* Stack exception. */
118b1fb1bb1SMarcel Moolenaar 	case 13: return (SIGSEGV);	/* General protection. */
119b1fb1bb1SMarcel Moolenaar 	case 14: return (SIGSEGV);	/* Page fault. */
120b1fb1bb1SMarcel Moolenaar 	case 16: return (SIGEMT);	/* Coprocessor error. */
121b1fb1bb1SMarcel Moolenaar 	}
122b1fb1bb1SMarcel Moolenaar 	return (SIGEMT);
123b1fb1bb1SMarcel Moolenaar }
124