]> git.saurik.com Git - apple/xnu.git/blame - osfmk/x86_64/monotonic_x86_64.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / x86_64 / monotonic_x86_64.c
CommitLineData
5ba3f43e
A
1/*
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <i386/cpu_data.h>
30#include <i386/cpuid.h>
31#include <i386/lapic.h>
d9a64523 32#include <i386/mp.h>
5ba3f43e
A
33#include <i386/proc_reg.h>
34#include <kern/assert.h> /* static_assert, assert */
35#include <kern/monotonic.h>
0a7de745 36#include <os/overflow.h>
5ba3f43e
A
37#include <sys/errno.h>
38#include <sys/monotonic.h>
0a7de745 39#include <x86_64/monotonic.h>
5ba3f43e
A
40
41/*
42 * Sanity check the compiler.
43 */
44
45#ifndef __has_builtin
46#define __has_builtin(x) 0
47#endif /* !defined(__has_builtin) */
48#if !__has_builtin(__builtin_ia32_rdpmc)
49#error requires __builtin_ia32_rdpmc builtin
50#endif /* !__has_builtin(__builtin_ia32_rdpmc) */
51
52#pragma mark core counters
53
54bool mt_core_supported = false;
55
56/*
57 * PMC[0-2]_{RD,WR} allow reading and writing the fixed PMCs.
58 *
59 * There are separate defines for access type because the read side goes through
60 * the rdpmc instruction, which has a different counter encoding than the msr
61 * path.
62 */
63#define PMC_FIXED_RD(CTR) ((UINT64_C(1) << 30) | (CTR))
64#define PMC_FIXED_WR(CTR) (MSR_IA32_PERF_FIXED_CTR0 + (CTR))
65#define PMC0_RD PMC_FIXED_RD(0)
66#define PMC0_WR PMC_FIXED_WR(0)
67#define PMC1_RD PMC_FIXED_RD(1)
68#define PMC1_WR PMC_FIXED_WR(1)
69#define PMC2_RD PMC_FIXED_RD(2)
70#define PMC2_WR PMC_FIXED_WR(2)
71
72struct mt_cpu *
73mt_cur_cpu(void)
74{
75 return &current_cpu_datap()->cpu_monotonic;
76}
77
78uint64_t
79mt_core_snap(unsigned int ctr)
80{
81 if (!mt_core_supported) {
82 return 0;
83 }
84
85 switch (ctr) {
86 case 0:
87 return __builtin_ia32_rdpmc(PMC0_RD);
88 case 1:
89 return __builtin_ia32_rdpmc(PMC1_RD);
90 case 2:
91 return __builtin_ia32_rdpmc(PMC2_RD);
92 default:
93 panic("monotonic: invalid core counter read: %u", ctr);
d9a64523 94 __builtin_unreachable();
5ba3f43e
A
95 }
96}
97
98void
99mt_core_set_snap(unsigned int ctr, uint64_t count)
100{
101 if (!mt_core_supported) {
102 return;
103 }
104
105 switch (ctr) {
106 case 0:
107 wrmsr64(PMC0_WR, count);
108 break;
109 case 1:
110 wrmsr64(PMC1_WR, count);
111 break;
112 case 2:
113 wrmsr64(PMC2_WR, count);
114 break;
115 default:
116 panic("monotonic: invalid core counter write: %u", ctr);
d9a64523 117 __builtin_unreachable();
5ba3f43e
A
118 }
119}
120
121/*
122 * FIXED_CTR_CTRL controls which rings fixed counters are enabled in and if they
123 * deliver PMIs.
124 *
125 * Each fixed counters has 4 bits: [0:1] controls which ring it's enabled in,
126 * [2] counts all hardware threads in each logical core (we don't want this),
127 * and [3] enables PMIs on overflow.
128 */
129
130#define FIXED_CTR_CTRL 0x38d
131
132/*
133 * Fixed counters are enabled in all rings, so hard-code this register state to
134 * enable in all rings and deliver PMIs.
135 */
d9a64523
A
136#define FIXED_CTR_CTRL_INIT (0x888)
137#define FIXED_CTR_CTRL_ENABLE (0x333)
5ba3f43e
A
138
139/*
140 * GLOBAL_CTRL controls which counters are enabled -- the high 32-bits control
141 * the fixed counters and the lower half is for the configurable counters.
142 */
143
144#define GLOBAL_CTRL 0x38f
145
146/*
147 * Fixed counters are always enabled -- and there are three of them.
148 */
149#define GLOBAL_CTRL_FIXED_EN (((UINT64_C(1) << 3) - 1) << 32)
150
151/*
152 * GLOBAL_STATUS reports the state of counters, like those that have overflowed.
153 */
154#define GLOBAL_STATUS 0x38e
155
156#define CTR_MAX ((UINT64_C(1) << 48) - 1)
157#define CTR_FIX_POS(CTR) ((UINT64_C(1) << (CTR)) << 32)
158
159#define GLOBAL_OVF 0x390
160
161static void
162core_down(cpu_data_t *cpu)
163{
164 if (!mt_core_supported) {
165 return;
166 }
167
168 assert(ml_get_interrupts_enabled() == FALSE);
169
170 wrmsr64(GLOBAL_CTRL, 0);
171 mt_mtc_update_fixed_counts(&cpu->cpu_monotonic, NULL, NULL);
172}
173
174static void
175core_up(cpu_data_t *cpu)
176{
177 struct mt_cpu *mtc;
178
179 if (!mt_core_supported) {
180 return;
181 }
182
183 assert(ml_get_interrupts_enabled() == FALSE);
184
185 mtc = &cpu->cpu_monotonic;
186
187 for (int i = 0; i < MT_CORE_NFIXED; i++) {
188 mt_core_set_snap(i, mtc->mtc_snaps[i]);
189 }
d9a64523 190 wrmsr64(FIXED_CTR_CTRL, FIXED_CTR_CTRL_INIT | FIXED_CTR_CTRL_ENABLE);
5ba3f43e
A
191 wrmsr64(GLOBAL_CTRL, GLOBAL_CTRL_FIXED_EN);
192}
193
194void
195mt_cpu_down(cpu_data_t *cpu)
196{
197 core_down(cpu);
198}
199
200void
201mt_cpu_up(cpu_data_t *cpu)
202{
203 boolean_t intrs_en;
204 intrs_en = ml_set_interrupts_enabled(FALSE);
205 core_up(cpu);
206 ml_set_interrupts_enabled(intrs_en);
207}
208
209static int
210mt_pmi_x86_64(x86_saved_state_t *state)
211{
212 uint64_t status;
213 struct mt_cpu *mtc;
5ba3f43e
A
214
215 assert(ml_get_interrupts_enabled() == FALSE);
216 mtc = mt_cur_cpu();
217 status = rdmsr64(GLOBAL_STATUS);
218
219 (void)atomic_fetch_add_explicit(&mt_pmis, 1, memory_order_relaxed);
220
d9a64523 221 for (unsigned int i = 0; i < MT_CORE_NFIXED; i++) {
5ba3f43e 222 if (status & CTR_FIX_POS(i)) {
d9a64523 223 uint64_t prior = CTR_MAX - mtc->mtc_snaps[i];
5ba3f43e
A
224 assert(prior <= CTR_MAX);
225 prior += 1; /* wrapped */
226
d9a64523
A
227 uint64_t delta = mt_mtc_update_count(mtc, i);
228 mtc->mtc_counts[i] += delta;
229
230 if (mt_microstackshots && mt_microstackshot_ctr == i) {
231 x86_saved_state64_t *state64 = saved_state64(state);
232 bool user_mode = (state64->isf.cs & 0x3) ? true : false;
233 KDBG_RELEASE(KDBG_EVENTID(DBG_MONOTONIC, DBG_MT_DEBUG, 1),
0a7de745 234 mt_microstackshot_ctr, user_mode);
d9a64523
A
235 mt_microstackshot_pmi_handler(user_mode, mt_microstackshot_ctx);
236 } else if (mt_debug) {
237 KDBG(KDBG_EVENTID(DBG_MONOTONIC, DBG_MT_DEBUG, 2),
0a7de745 238 mt_microstackshot_ctr, i);
d9a64523
A
239 }
240
241 mtc->mtc_snaps[i] = mt_core_reset_values[i];
242 mt_core_set_snap(i, mt_core_reset_values[i]);
5ba3f43e
A
243 }
244 }
245
246 /* if any of the configurable counters overflowed, tell kpc */
247 if (status & ((UINT64_C(1) << 4) - 1)) {
248 extern void kpc_pmi_handler(x86_saved_state_t *state);
249 kpc_pmi_handler(state);
250 }
251 return 0;
252}
253
d9a64523
A
254static void
255mt_microstackshot_start_remote(__unused void *arg)
256{
257 struct mt_cpu *mtc = mt_cur_cpu();
258
259 wrmsr64(FIXED_CTR_CTRL, FIXED_CTR_CTRL_INIT);
260
261 for (int i = 0; i < MT_CORE_NFIXED; i++) {
262 uint64_t delta = mt_mtc_update_count(mtc, i);
263 mtc->mtc_counts[i] += delta;
264 mt_core_set_snap(i, mt_core_reset_values[i]);
265 mtc->mtc_snaps[i] = mt_core_reset_values[i];
266 }
267
268 wrmsr64(FIXED_CTR_CTRL, FIXED_CTR_CTRL_INIT | FIXED_CTR_CTRL_ENABLE);
269}
270
271int
272mt_microstackshot_start_arch(uint64_t period)
5ba3f43e 273{
d9a64523
A
274 if (!mt_core_supported) {
275 return ENOTSUP;
276 }
5ba3f43e 277
0a7de745
A
278 uint64_t reset_value = 0;
279 int ovf = os_sub_overflow(CTR_MAX, period, &reset_value);
280 if (ovf) {
281 return ERANGE;
282 }
283
d9a64523
A
284 mt_core_reset_values[mt_microstackshot_ctr] = CTR_MAX - period;
285 mp_cpus_call(CPUMASK_ALL, ASYNC, mt_microstackshot_start_remote,
0a7de745 286 NULL);
d9a64523
A
287 return 0;
288}
5ba3f43e 289
d9a64523
A
290void
291mt_early_init(void)
292{
293 i386_cpu_info_t *info = cpuid_info();
294 if (info->cpuid_arch_perf_leaf.version >= 2) {
5ba3f43e
A
295 lapic_set_pmi_func((i386_intr_func_t)mt_pmi_x86_64);
296 mt_core_supported = true;
297 }
298}
299
300static int
d9a64523 301core_init(__unused mt_device_t dev)
5ba3f43e
A
302{
303 return ENOTSUP;
304}
305
306#pragma mark common hooks
307
d9a64523 308struct mt_device mt_devices[] = {
5ba3f43e 309 [0] = {
d9a64523 310 .mtd_name = "core",
5ba3f43e
A
311 .mtd_init = core_init
312 }
313};
314
315static_assert(
0a7de745
A
316 (sizeof(mt_devices) / sizeof(mt_devices[0])) == MT_NDEVS,
317 "MT_NDEVS macro should be same as the length of mt_devices");