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