1/* $NetBSD: sys_pmc.c,v 1.11 2014/01/25 21:11:49 christos Exp $ */
2
3/*
4 * Copyright (c) 2002 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Allen Briggs for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
39__KERNEL_RCSID(0, "$NetBSD: sys_pmc.c,v 1.11 2014/01/25 21:11:49 christos Exp $");
40
41#include "opt_perfctrs.h"
42
43#include <sys/param.h>
44#include <sys/proc.h>
45#include <sys/mount.h>
46#include <sys/systm.h>
47#include <sys/syscallargs.h>
48#include <sys/types.h>
49
50#if defined(PERFCTRS)
51#include <sys/pmc.h>
52#endif
53
54/*
55 * XXX We need a multiprocessor locking protocol!
56 */
57
58int
59sys_pmc_control(struct lwp *l, const struct sys_pmc_control_args *uap, register_t *retval)
60{
61 /* {
62 syscallarg(int) ctr;
63 syscallarg(int) op;
64 syscallarg(void *) args;
65 } */
66#ifndef PERFCTRS
67 return ENXIO;
68#else
69 struct pmc_counter_cfg cfg;
70 void *args;
71 int ctr, operation, error=0;
72
73 ctr = SCARG(uap, ctr);
74 __USE(ctr);
75 operation = SCARG(uap, op);
76
77 KERNEL_LOCK(1, NULL);
78 switch (operation) {
79 case PMC_OP_START:
80 if (!pmc_counter_isconfigured(l->l_proc, ctr)) {
81 error = ENXIO;
82 } else if (pmc_counter_isrunning(l->l_proc, ctr)) {
83 error = EINPROGRESS;
84 } else {
85 pmc_enable_counter(l->l_proc, ctr);
86 }
87 break;
88 case PMC_OP_STOP:
89 if (!pmc_counter_isconfigured(l->l_proc, ctr)) {
90 error = ENXIO;
91 } else if (pmc_counter_isrunning(l->l_proc, ctr)) {
92 pmc_disable_counter(l->l_proc, ctr);
93 }
94 break;
95 case PMC_OP_CONFIGURE:
96 args = SCARG(uap, args);
97
98 if (pmc_counter_isrunning(l->l_proc, ctr)) {
99 pmc_disable_counter(l->l_proc, ctr);
100 }
101 error = copyin(args, &cfg, sizeof(struct pmc_counter_cfg));
102 if (error == 0) {
103 error = pmc_configure_counter(l->l_proc, ctr, &cfg);
104 }
105 break;
106 case PMC_OP_PROFSTART:
107 args = SCARG(uap, args);
108
109 error = copyin(args, &cfg, sizeof(struct pmc_counter_cfg));
110 if (error == 0) {
111 error = pmc_start_profiling(ctr, &cfg);
112 }
113 break;
114 case PMC_OP_PROFSTOP:
115 error = pmc_stop_profiling(ctr);
116 break;
117 default:
118 error = EINVAL;
119 break;
120 }
121 KERNEL_UNLOCK_ONE(NULL);
122 return error;
123#endif
124}
125
126int
127sys_pmc_get_info(struct lwp *l, const struct sys_pmc_get_info_args *uap, register_t *retval)
128{
129 /* {
130 syscallarg(int) ctr;
131 syscallarg(int) op;
132 syscallarg(void *) args;
133 } */
134#ifndef PERFCTRS
135 return ENXIO;
136#else
137 uint64_t val;
138 void *args;
139 int nctrs, ctr, ctrt, request, error=0, flags=0;
140
141 ctr = SCARG(uap, ctr);
142 request = SCARG(uap, op);
143 args = SCARG(uap, args);
144 __USE(flags);
145
146 KERNEL_LOCK(1, NULL);
147 nctrs = pmc_get_num_counters();
148 switch (request) {
149 case PMC_INFO_NCOUNTERS: /* args should be (int *) */
150 error = copyout(&nctrs, args, sizeof(int));
151 break;
152
153 case PMC_INFO_CPUCTR_TYPE: /* args should be (int *) */
154 ctrt = pmc_get_counter_type(ctr);
155 error = copyout(&ctrt, args, sizeof(int));
156 break;
157 /* args should be (pmc_ctr_t *) */
158 case PMC_INFO_ACCUMULATED_COUNTER_VALUE:
159 flags = PMC_VALUE_FLAGS_CHILDREN;
160 /*FALLTHROUGH*/
161 case PMC_INFO_COUNTER_VALUE:
162 if (ctr < 0 || ctr >= nctrs) {
163 error = EINVAL;
164 break;
165 }
166 error = pmc_get_counter_value(l->l_proc, ctr, flags, &val);
167 if (error == 0) {
168 error = copyout(&val, args, sizeof(uint64_t));
169 }
170 break;
171 default:
172 error = EINVAL;
173 break;
174 }
175 KERNEL_UNLOCK_ONE(NULL);
176 return error;
177#endif
178}
179