xref: /netbsd/sys/arch/i386/i386/db_interface.c (revision bf9ec67e)
1 /*	$NetBSD: db_interface.c,v 1.35 2002/05/14 02:58:35 matt Exp $	*/
2 
3 /*
4  * Mach Operating System
5  * Copyright (c) 1991,1990 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie the
26  * rights to redistribute these changes.
27  *
28  *	db_interface.c,v 2.4 1991/02/05 17:11:13 mrt (CMU)
29  */
30 
31 /*
32  * Interface to new debugger.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.35 2002/05/14 02:58:35 matt Exp $");
37 
38 #include "opt_ddb.h"
39 
40 #include <sys/param.h>
41 #include <sys/proc.h>
42 #include <sys/reboot.h>
43 #include <sys/systm.h>
44 
45 #include <uvm/uvm_extern.h>
46 
47 #include <dev/cons.h>
48 
49 #include <machine/cpufunc.h>
50 #include <machine/db_machdep.h>
51 
52 #include <ddb/db_sym.h>
53 #include <ddb/db_command.h>
54 #include <ddb/db_extern.h>
55 #include <ddb/db_access.h>
56 #include <ddb/db_output.h>
57 #include <ddb/ddbvar.h>
58 
59 extern char *trap_type[];
60 extern int trap_types;
61 
62 int	db_active = 0;
63 db_regs_t ddb_regs;	/* register state */
64 
65 void kdbprinttrap __P((int, int));
66 
67 /*
68  * Print trap reason.
69  */
70 void
71 kdbprinttrap(type, code)
72 	int type, code;
73 {
74 	db_printf("kernel: ");
75 	if (type >= trap_types || type < 0)
76 		db_printf("type %d", type);
77 	else
78 		db_printf("%s", trap_type[type]);
79 	db_printf(" trap, code=%x\n", code);
80 }
81 
82 /*
83  *  kdb_trap - field a TRACE or BPT trap
84  */
85 int
86 kdb_trap(type, code, regs)
87 	int type, code;
88 	db_regs_t *regs;
89 {
90 	int s;
91 
92 	switch (type) {
93 	case T_BPTFLT:	/* breakpoint */
94 	case T_TRCTRAP:	/* single_step */
95 	case T_NMI:	/* NMI */
96 	case -1:	/* keyboard interrupt */
97 		break;
98 	default:
99 		if (!db_onpanic && db_recover==0)
100 			return (0);
101 
102 		kdbprinttrap(type, code);
103 		if (db_recover != 0) {
104 			db_error("Faulted in DDB; continuing...\n");
105 			/*NOTREACHED*/
106 		}
107 	}
108 
109 	/* XXX Should switch to kdb`s own stack here. */
110 
111 	ddb_regs = *regs;
112 	if (KERNELMODE(regs->tf_cs, regs->tf_eflags)) {
113 		/*
114 		 * Kernel mode - esp and ss not saved
115 		 */
116 		ddb_regs.tf_esp = (int)&regs->tf_esp;	/* kernel stack pointer */
117 		asm("movw %%ss,%w0" : "=r" (ddb_regs.tf_ss));
118 	}
119 
120 	ddb_regs.tf_cs &= 0xffff;
121 	ddb_regs.tf_ds &= 0xffff;
122 	ddb_regs.tf_es &= 0xffff;
123 	ddb_regs.tf_fs &= 0xffff;
124 	ddb_regs.tf_gs &= 0xffff;
125 	ddb_regs.tf_ss &= 0xffff;
126 	s = splhigh();
127 	db_active++;
128 	cnpollc(TRUE);
129 	db_trap(type, code);
130 	cnpollc(FALSE);
131 	db_active--;
132 	splx(s);
133 
134 	regs->tf_gs     = ddb_regs.tf_gs;
135 	regs->tf_fs     = ddb_regs.tf_fs;
136 	regs->tf_es     = ddb_regs.tf_es;
137 	regs->tf_ds     = ddb_regs.tf_ds;
138 	regs->tf_edi    = ddb_regs.tf_edi;
139 	regs->tf_esi    = ddb_regs.tf_esi;
140 	regs->tf_ebp    = ddb_regs.tf_ebp;
141 	regs->tf_ebx    = ddb_regs.tf_ebx;
142 	regs->tf_edx    = ddb_regs.tf_edx;
143 	regs->tf_ecx    = ddb_regs.tf_ecx;
144 	regs->tf_eax    = ddb_regs.tf_eax;
145 	regs->tf_eip    = ddb_regs.tf_eip;
146 	regs->tf_cs     = ddb_regs.tf_cs;
147 	regs->tf_eflags = ddb_regs.tf_eflags;
148 	if (!KERNELMODE(regs->tf_cs, regs->tf_eflags)) {
149 		/* ring transit - saved esp and ss valid */
150 		regs->tf_esp    = ddb_regs.tf_esp;
151 		regs->tf_ss     = ddb_regs.tf_ss;
152 	}
153 
154 	return (1);
155 }
156 
157 void
158 cpu_Debugger()
159 {
160 	breakpoint();
161 }
162