xref: /freebsd/sys/amd64/amd64/gdb_machdep.c (revision 5eb95e11ba2e14acb0b86145bcaae2cfd25aa24c)
146280ae7SWarner Losh /*-
272d44f31SMarcel Moolenaar  * Copyright (c) 2004 Marcel Moolenaar
372d44f31SMarcel Moolenaar  * All rights reserved.
472d44f31SMarcel Moolenaar  *
572d44f31SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
672d44f31SMarcel Moolenaar  * modification, are permitted provided that the following conditions
772d44f31SMarcel Moolenaar  * are met:
872d44f31SMarcel Moolenaar  *
972d44f31SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
1072d44f31SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
1172d44f31SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
1272d44f31SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
1372d44f31SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
1472d44f31SMarcel Moolenaar  *
1572d44f31SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
1672d44f31SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1772d44f31SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1872d44f31SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
1972d44f31SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2072d44f31SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2172d44f31SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2272d44f31SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2372d44f31SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2472d44f31SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2572d44f31SMarcel Moolenaar  */
2672d44f31SMarcel Moolenaar 
2772d44f31SMarcel Moolenaar #include <sys/cdefs.h>
2872d44f31SMarcel Moolenaar __FBSDID("$FreeBSD$");
2972d44f31SMarcel Moolenaar 
3072d44f31SMarcel Moolenaar #include <sys/param.h>
3172d44f31SMarcel Moolenaar #include <sys/systm.h>
3272d44f31SMarcel Moolenaar #include <sys/kdb.h>
3372d44f31SMarcel Moolenaar #include <sys/kernel.h>
3472d44f31SMarcel Moolenaar #include <sys/proc.h>
3572d44f31SMarcel Moolenaar #include <sys/signal.h>
3672d44f31SMarcel Moolenaar 
3772d44f31SMarcel Moolenaar #include <machine/frame.h>
3872d44f31SMarcel Moolenaar #include <machine/gdb_machdep.h>
39a649898dSPeter Wemm #include <machine/pcb.h>
4072d44f31SMarcel Moolenaar #include <machine/psl.h>
4172d44f31SMarcel Moolenaar #include <machine/reg.h>
4272d44f31SMarcel Moolenaar #include <machine/trap.h>
43a649898dSPeter Wemm #include <machine/frame.h>
44a649898dSPeter Wemm #include <machine/endian.h>
4572d44f31SMarcel Moolenaar 
4672d44f31SMarcel Moolenaar #include <gdb/gdb.h>
4772d44f31SMarcel Moolenaar 
4872d44f31SMarcel Moolenaar void *
4972d44f31SMarcel Moolenaar gdb_cpu_getreg(int regnum, size_t *regsz)
5072d44f31SMarcel Moolenaar {
51*5eb95e11SJohn Baldwin 	static uint32_t _kcodesel = GSEL(GCODE_SEL, SEL_KPL);
52*5eb95e11SJohn Baldwin 	static uint32_t _kdatasel = GSEL(GDATA_SEL, SEL_KPL);
5372d44f31SMarcel Moolenaar 
5472d44f31SMarcel Moolenaar 	*regsz = gdb_cpu_regsz(regnum);
55a649898dSPeter Wemm 
56a649898dSPeter Wemm 	if (kdb_thread  == curthread) {
5772d44f31SMarcel Moolenaar 		switch (regnum) {
58a649898dSPeter Wemm 		case 0:	return (&kdb_frame->tf_rax);
59a649898dSPeter Wemm 		case 2:	return (&kdb_frame->tf_rcx);
60a649898dSPeter Wemm 		case 3:	return (&kdb_frame->tf_rdx);
61a649898dSPeter Wemm 		case 4:	return (&kdb_frame->tf_rsi);
62a649898dSPeter Wemm 		case 5:	return (&kdb_frame->tf_rdi);
63a649898dSPeter Wemm 		case 8: return (&kdb_frame->tf_r8);
64a649898dSPeter Wemm 		case 9: return (&kdb_frame->tf_r9);
65a649898dSPeter Wemm 		case 10: return (&kdb_frame->tf_r10);
66a649898dSPeter Wemm 		case 11: return (&kdb_frame->tf_r11);
671acc225fSPeter Wemm 		case 17: return (&kdb_frame->tf_rflags);
68a649898dSPeter Wemm 		case 18: return (&kdb_frame->tf_cs);
69a649898dSPeter Wemm 		case 19: return (&kdb_frame->tf_ss);
70a649898dSPeter Wemm 		}
71a649898dSPeter Wemm 	}
72a649898dSPeter Wemm 	switch (regnum) {
73a649898dSPeter Wemm 	case 1:  return (&kdb_thrctx->pcb_rbx);
74a649898dSPeter Wemm 	case 6:  return (&kdb_thrctx->pcb_rbp);
75a649898dSPeter Wemm 	case 7:  return (&kdb_thrctx->pcb_rsp);
76a649898dSPeter Wemm 	case 12: return (&kdb_thrctx->pcb_r12);
77a649898dSPeter Wemm 	case 13: return (&kdb_thrctx->pcb_r13);
78a649898dSPeter Wemm 	case 14: return (&kdb_thrctx->pcb_r14);
79a649898dSPeter Wemm 	case 15: return (&kdb_thrctx->pcb_r15);
80a649898dSPeter Wemm 	case 16: return (&kdb_thrctx->pcb_rip);
81*5eb95e11SJohn Baldwin 	case 18: return (&_kcodesel);
82*5eb95e11SJohn Baldwin 	case 19: return (&_kdatasel);
8372d44f31SMarcel Moolenaar 	}
8472d44f31SMarcel Moolenaar 	return (NULL);
8572d44f31SMarcel Moolenaar }
8672d44f31SMarcel Moolenaar 
8772d44f31SMarcel Moolenaar void
88bcc5241cSMarcel Moolenaar gdb_cpu_setreg(int regnum, void *val)
8972d44f31SMarcel Moolenaar {
9072d44f31SMarcel Moolenaar 
9172d44f31SMarcel Moolenaar 	switch (regnum) {
92a649898dSPeter Wemm 	case GDB_REG_PC:
93bcc5241cSMarcel Moolenaar 		kdb_thrctx->pcb_rip = *(register_t *)val;
94a649898dSPeter Wemm 		if (kdb_thread  == curthread)
95bcc5241cSMarcel Moolenaar 			kdb_frame->tf_rip = *(register_t *)val;
9672d44f31SMarcel Moolenaar 	}
9772d44f31SMarcel Moolenaar }
98b1fb1bb1SMarcel Moolenaar 
99b1fb1bb1SMarcel Moolenaar int
100b1fb1bb1SMarcel Moolenaar gdb_cpu_signal(int type, int code)
101b1fb1bb1SMarcel Moolenaar {
102b1fb1bb1SMarcel Moolenaar 
103b1fb1bb1SMarcel Moolenaar 	switch (type & ~T_USER) {
104b1fb1bb1SMarcel Moolenaar 	case 0: return (SIGFPE);	/* Divide by zero. */
105b1fb1bb1SMarcel Moolenaar 	case 1: return (SIGTRAP);	/* Debug exception. */
106b1fb1bb1SMarcel Moolenaar 	case 3: return (SIGTRAP);	/* Breakpoint. */
107b1fb1bb1SMarcel Moolenaar 	case 4: return (SIGSEGV);	/* into instr. (overflow). */
108b1fb1bb1SMarcel Moolenaar 	case 5: return (SIGURG);	/* bound instruction. */
109b1fb1bb1SMarcel Moolenaar 	case 6: return (SIGILL);	/* Invalid opcode. */
110b1fb1bb1SMarcel Moolenaar 	case 7: return (SIGFPE);	/* Coprocessor not present. */
111b1fb1bb1SMarcel Moolenaar 	case 8: return (SIGEMT);	/* Double fault. */
112b1fb1bb1SMarcel Moolenaar 	case 9: return (SIGSEGV);	/* Coprocessor segment overrun. */
113b1fb1bb1SMarcel Moolenaar 	case 10: return (SIGTRAP);	/* Invalid TSS (also single-step). */
114b1fb1bb1SMarcel Moolenaar 	case 11: return (SIGSEGV);	/* Segment not present. */
115b1fb1bb1SMarcel Moolenaar 	case 12: return (SIGSEGV);	/* Stack exception. */
116b1fb1bb1SMarcel Moolenaar 	case 13: return (SIGSEGV);	/* General protection. */
117b1fb1bb1SMarcel Moolenaar 	case 14: return (SIGSEGV);	/* Page fault. */
118b1fb1bb1SMarcel Moolenaar 	case 16: return (SIGEMT);	/* Coprocessor error. */
119b1fb1bb1SMarcel Moolenaar 	}
120b1fb1bb1SMarcel Moolenaar 	return (SIGEMT);
121b1fb1bb1SMarcel Moolenaar }
122