xref: /xnu-11215.41.3/bsd/kern/kern_cpc.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 // Copyright (c) 2023 Apple Inc. All rights reserved.
2 //
3 // @APPLE_OSREFERENCE_LICENSE_HEADER_START@
4 //
5 // This file contains Original Code and/or Modifications of Original Code
6 // as defined in and that are subject to the Apple Public Source License
7 // Version 2.0 (the 'License'). You may not use this file except in
8 // compliance with the License. The rights granted to you under the License
9 // may not be used to create, or enable the creation or redistribution of,
10 // unlawful or unlicensed copies of an Apple operating system, or to
11 // circumvent, violate, or enable the circumvention or violation of, any
12 // terms of an Apple operating system software license agreement.
13 //
14 // Please obtain a copy of the License at
15 // http://www.opensource.apple.com/apsl/ and read it before using this file.
16 //
17 // The Original Code and all software distributed under the License are
18 // distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
19 // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
20 // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
22 // Please see the License for the specific language governing rights and
23 // limitations under the License.
24 //
25 // @APPLE_OSREFERENCE_LICENSE_HEADER_END@
26 
27 #include <kern/cpc.h>
28 #include <sys/sysctl.h>
29 
30 SYSCTL_NODE(_kern, OID_AUTO, cpc, CTLFLAG_RD, 0,
31     "CPU Performance Counters subsystem");
32 
33 static int
34 _cpc_sysctl_secure SYSCTL_HANDLER_ARGS
35 {
36 	int enforced = cpc_is_secure();
37 	int changed = 0;
38 	int error = sysctl_io_number(req, enforced, sizeof(enforced), &enforced,
39 	    &changed);
40 	if (error != 0) {
41 		return error;
42 	}
43 	if (changed) {
44 #if CPC_INSECURE
45 		cpc_change_security(enforced);
46 #else // CPC_INSECURE
47 		return EPERM;
48 #endif // !CPC_INSECURE
49 	}
50 	return 0;
51 }
52 
53 #if CPC_INSECURE
54 #define CPC_SYSCTL_SECURE_PROT CTLFLAG_RW
55 #else // CPC_INSECURE
56 #define CPC_SYSCTL_SECURE_PROT CTLFLAG_RD
57 #endif // !CPC_INSECURE
58 
59 SYSCTL_PROC(_kern_cpc, OID_AUTO, secure, CPC_SYSCTL_SECURE_PROT | CTLTYPE_INT,
60     0, 0, _cpc_sysctl_secure, "I",
61     "Whether the CPU Performance Counters system is operating securely.");
62