xref: /xnu-8792.61.2/osfmk/x86_64/counter.c (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1 /* * Copyright (c) 2020 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 /*
28  * @OSF_COPYRIGHT@
29  */
30 /*
31  * Mach Operating System
32  * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
33  * All Rights Reserved.
34  *
35  * Permission to use, copy, modify and distribute this software and its
36  * documentation is hereby granted, provided that both the copyright
37  * notice and this permission notice appear in all copies of the
38  * software, derivative works or modified versions, and any portions
39  * thereof, and that both notices appear in supporting documentation.
40  *
41  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
42  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
43  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
44  *
45  * Carnegie Mellon requests users of this software to return to
46  *
47  *  Software Distribution Coordinator  or  [email protected]
48  *  School of Computer Science
49  *  Carnegie Mellon University
50  *  Pittsburgh PA 15213-3890
51  *
52  * any improvements or extensions that they make and grant Carnegie Mellon
53  * the rights to redistribute these changes.
54  */
55 #include <kern/assert.h>
56 #include <kern/cpu_data.h>
57 #include <kern/counter.h>
58 #include <kern/zalloc.h>
59 #include <machine/atomic.h>
60 #include <machine/machine_routines.h>
61 #include <machine/cpu_number.h>
62 
63 OS_OVERLOADABLE
64 void
counter_add(scalable_counter_t * counter,uint64_t amount)65 counter_add(scalable_counter_t *counter, uint64_t amount)
66 {
67 	disable_preemption();
68 	(*zpercpu_get(*counter)) += amount;
69 	enable_preemption();
70 }
71 
72 OS_OVERLOADABLE
73 void
counter_inc(scalable_counter_t * counter)74 counter_inc(scalable_counter_t *counter)
75 {
76 	disable_preemption();
77 	(*zpercpu_get(*counter))++;
78 	enable_preemption();
79 }
80 
81 OS_OVERLOADABLE
82 void
counter_dec(scalable_counter_t * counter)83 counter_dec(scalable_counter_t *counter)
84 {
85 	disable_preemption();
86 	(*zpercpu_get(*counter))--;
87 	enable_preemption();
88 }
89 
90 OS_OVERLOADABLE
91 void
counter_add_preemption_disabled(scalable_counter_t * counter,uint64_t amount)92 counter_add_preemption_disabled(scalable_counter_t *counter, uint64_t amount)
93 {
94 	(*zpercpu_get(*counter)) += amount;
95 }
96 
97 OS_OVERLOADABLE
98 void
counter_inc_preemption_disabled(scalable_counter_t * counter)99 counter_inc_preemption_disabled(scalable_counter_t *counter)
100 {
101 	(*zpercpu_get(*counter))++;
102 }
103 
104 OS_OVERLOADABLE
105 void
counter_dec_preemption_disabled(scalable_counter_t * counter)106 counter_dec_preemption_disabled(scalable_counter_t *counter)
107 {
108 	(*zpercpu_get(*counter))--;
109 }
110