subr_spldebug.c revision 1.1 1 /* $NetBSD: subr_spldebug.c,v 1.1 2009/11/03 05:23:28 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Young.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Interrupt priority level debugging code.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: subr_spldebug.c,v 1.1 2009/11/03 05:23:28 dyoung Exp $");
38
39 #include <sys/param.h>
40 #include <sys/spldebug.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/cpu.h>
44 #include <machine/return.h>
45
46 __strong_alias(spldebug_start, _spldebug_start);
47 __strong_alias(spldebug_stop, _spldebug_stop);
48
49 #define SPLRAISE_STACKLEN 32
50
51 void *splraise_retaddrs[MAXCPUS][SPLRAISE_STACKLEN][4];
52 int splraise_depth[MAXCPUS] = {0};
53 int spllowered_to[MAXCPUS] = {0};
54 void *spllowered_from[MAXCPUS][2] = {{0}};
55 bool spldebug = false;
56
57 void _spldebug_start(void);
58 void _spldebug_stop(void);
59
60 void
61 _spldebug_start(void)
62 {
63 spldebug = true;
64 }
65
66 void
67 _spldebug_stop(void)
68 {
69 spldebug = false;
70 }
71
72 void
73 spldebug_lower(int ipl)
74 {
75 struct cpu_info *ci;
76
77 if (!spldebug)
78 return;
79
80 if (ipl >= IPL_HIGH)
81 return;
82
83 if (cold)
84 ci = &cpu_info_primary;
85 else
86 ci = curcpu();
87
88 if (cpu_index(ci) > MAXCPUS)
89 return;
90
91 splraise_depth[cpu_index(ci)] = 0;
92 spllowered_to[cpu_index(ci)] = ipl;
93 #if 0
94 spllowered_from[cpu_index(ci)][0] = return_address(0);
95 spllowered_from[cpu_index(ci)][1] = return_address(1);
96 #endif
97 }
98
99 void
100 spldebug_raise(int ipl)
101 {
102 int i;
103 void **retaddrs;
104 struct cpu_info *ci;
105
106 if (!spldebug)
107 return;
108
109 if (ipl < IPL_HIGH)
110 return;
111
112 if (cold)
113 ci = &cpu_info_primary;
114 else
115 ci = curcpu();
116
117 if (cpu_index(ci) >= MAXCPUS)
118 return;
119
120 if (splraise_depth[cpu_index(ci)] >= SPLRAISE_STACKLEN)
121 return;
122
123 retaddrs = &splraise_retaddrs[cpu_index(ci)]
124 [splraise_depth[cpu_index(ci)]++][0];
125
126 retaddrs[0] = retaddrs[1] = retaddrs[2] = retaddrs[3] = NULL;
127
128 for (i = 0; i < 4; i++) {
129 retaddrs[i] = return_address(i);
130
131 if (retaddrs[i] == NULL)
132 break;
133 }
134 }
135