xref: /openbsd-src/sys/kern/subr_evcount.c (revision 0d280c5f69f6ef21f5dca2b37e738de752505c51)
1 /*	$OpenBSD: subr_evcount.c,v 1.13 2022/08/14 01:58:28 jsg Exp $ */
2 /*
3  * Copyright (c) 2004 Artur Grabowski <art@openbsd.org>
4  * Copyright (c) 2004 Aaron Campbell <aaron@openbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/evcount.h>
30 #include <sys/systm.h>
31 #include <sys/sysctl.h>
32 
33 static TAILQ_HEAD(,evcount) evcount_list = TAILQ_HEAD_INITIALIZER(evcount_list);
34 
35 void
36 evcount_attach(struct evcount *ec, const char *name, void *data)
37 {
38 	static int nextid = 0;
39 
40 	memset(ec, 0, sizeof(*ec));
41 	ec->ec_name = name;
42 	ec->ec_id = ++nextid;
43 	ec->ec_data = data;
44 	TAILQ_INSERT_TAIL(&evcount_list, ec, next);
45 }
46 
47 void
48 evcount_detach(struct evcount *ec)
49 {
50 	TAILQ_REMOVE(&evcount_list, ec, next);
51 }
52 
53 #ifndef	SMALL_KERNEL
54 
55 int
56 evcount_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
57     void *newp, size_t newlen)
58 {
59 	int error = 0, s, nintr, i;
60 	struct evcount *ec;
61 	u_int64_t count;
62 
63 	if (newp != NULL)
64 		return (EPERM);
65 
66 	if (name[0] != KERN_INTRCNT_NUM) {
67 		if (namelen != 2)
68 			return (ENOTDIR);
69 		if (name[1] < 0)
70 			return (EINVAL);
71 		i = name[1];
72 	} else
73 		i = -1;
74 
75 	nintr = 0;
76 	TAILQ_FOREACH(ec, &evcount_list, next) {
77 		if (nintr++ == i)
78 			break;
79 	}
80 
81 	switch (name[0]) {
82 	case KERN_INTRCNT_NUM:
83 		error = sysctl_rdint(oldp, oldlenp, NULL, nintr);
84 		break;
85 	case KERN_INTRCNT_CNT:
86 		if (ec == NULL)
87 			return (ENOENT);
88 		s = splhigh();
89 		count = ec->ec_count;
90 		splx(s);
91 		error = sysctl_rdquad(oldp, oldlenp, NULL, count);
92 		break;
93 	case KERN_INTRCNT_NAME:
94 		if (ec == NULL)
95 			return (ENOENT);
96 		error = sysctl_rdstring(oldp, oldlenp, NULL, ec->ec_name);
97 		break;
98 	case KERN_INTRCNT_VECTOR:
99 		if (ec == NULL || ec->ec_data == NULL)
100 			return (ENOENT);
101 		error = sysctl_rdint(oldp, oldlenp, NULL,
102 		    *((int *)ec->ec_data));
103 		break;
104 	default:
105 		error = EOPNOTSUPP;
106 		break;
107 	}
108 
109 	return (error);
110 }
111 #endif	/* SMALL_KERNEL */
112