Print this page
OS-5192 need faster clock_gettime
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Joshua M. Clulow <jmc@joyent.com>
Reviewed by: Ryan Zezeski <ryan@zinascii.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/i86pc/os/mlsetup.c
+++ new/usr/src/uts/i86pc/os/mlsetup.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
|
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2012 Gary Mills
23 23 *
24 24 * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
25 25 * Copyright (c) 2011 by Delphix. All rights reserved.
26 + * Copyright 2016 Joyent, Inc.
26 27 */
27 28 /*
28 29 * Copyright (c) 2010, Intel Corporation.
29 30 * All rights reserved.
30 31 */
31 32
32 33 #include <sys/types.h>
33 34 #include <sys/sysmacros.h>
34 35 #include <sys/disp.h>
35 36 #include <sys/promif.h>
36 37 #include <sys/clock.h>
37 38 #include <sys/cpuvar.h>
38 39 #include <sys/stack.h>
39 40 #include <vm/as.h>
40 41 #include <vm/hat.h>
41 42 #include <sys/reboot.h>
42 43 #include <sys/avintr.h>
43 44 #include <sys/vtrace.h>
44 45 #include <sys/proc.h>
45 46 #include <sys/thread.h>
46 47 #include <sys/cpupart.h>
47 48 #include <sys/pset.h>
48 49 #include <sys/copyops.h>
49 50 #include <sys/pg.h>
50 51 #include <sys/disp.h>
51 52 #include <sys/debug.h>
52 53 #include <sys/sunddi.h>
53 54 #include <sys/x86_archext.h>
|
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
54 55 #include <sys/privregs.h>
55 56 #include <sys/machsystm.h>
56 57 #include <sys/ontrap.h>
57 58 #include <sys/bootconf.h>
58 59 #include <sys/boot_console.h>
59 60 #include <sys/kdi_machimpl.h>
60 61 #include <sys/archsystm.h>
61 62 #include <sys/promif.h>
62 63 #include <sys/pci_cfgspace.h>
63 64 #include <sys/bootvfs.h>
65 +#include <sys/tsc.h>
64 66 #ifdef __xpv
65 67 #include <sys/hypervisor.h>
66 68 #else
67 69 #include <sys/xpv_support.h>
68 70 #endif
69 71
70 72 /*
71 73 * some globals for patching the result of cpuid
72 74 * to solve problems w/ creative cpu vendors
73 75 */
74 76
75 77 extern uint32_t cpuid_feature_ecx_include;
76 78 extern uint32_t cpuid_feature_ecx_exclude;
77 79 extern uint32_t cpuid_feature_edx_include;
78 80 extern uint32_t cpuid_feature_edx_exclude;
79 81
80 82 /*
81 83 * Set console mode
82 84 */
83 85 static void
84 86 set_console_mode(uint8_t val)
85 87 {
86 88 struct bop_regs rp = {0};
87 89
88 90 rp.eax.byte.ah = 0x0;
89 91 rp.eax.byte.al = val;
90 92 rp.ebx.word.bx = 0x0;
91 93
92 94 BOP_DOINT(bootops, 0x10, &rp);
93 95 }
94 96
95 97
96 98 /*
97 99 * Setup routine called right before main(). Interposing this function
98 100 * before main() allows us to call it in a machine-independent fashion.
99 101 */
100 102 void
101 103 mlsetup(struct regs *rp)
102 104 {
103 105 u_longlong_t prop_value;
104 106 extern struct classfuncs sys_classfuncs;
105 107 extern disp_t cpu0_disp;
106 108 extern char t0stack[];
107 109 extern int post_fastreboot;
108 110 extern uint64_t plat_dr_options;
109 111
110 112 ASSERT_STACK_ALIGNED();
111 113
112 114 /*
113 115 * initialize cpu_self
114 116 */
115 117 cpu[0]->cpu_self = cpu[0];
116 118
117 119 #if defined(__xpv)
118 120 /*
119 121 * Point at the hypervisor's virtual cpu structure
120 122 */
121 123 cpu[0]->cpu_m.mcpu_vcpu_info = &HYPERVISOR_shared_info->vcpu_info[0];
122 124 #endif
123 125
124 126 /*
125 127 * check if we've got special bits to clear or set
126 128 * when checking cpu features
127 129 */
128 130
129 131 if (bootprop_getval("cpuid_feature_ecx_include", &prop_value) != 0)
130 132 cpuid_feature_ecx_include = 0;
131 133 else
132 134 cpuid_feature_ecx_include = (uint32_t)prop_value;
133 135
134 136 if (bootprop_getval("cpuid_feature_ecx_exclude", &prop_value) != 0)
135 137 cpuid_feature_ecx_exclude = 0;
136 138 else
137 139 cpuid_feature_ecx_exclude = (uint32_t)prop_value;
138 140
139 141 if (bootprop_getval("cpuid_feature_edx_include", &prop_value) != 0)
140 142 cpuid_feature_edx_include = 0;
141 143 else
142 144 cpuid_feature_edx_include = (uint32_t)prop_value;
143 145
144 146 if (bootprop_getval("cpuid_feature_edx_exclude", &prop_value) != 0)
145 147 cpuid_feature_edx_exclude = 0;
146 148 else
147 149 cpuid_feature_edx_exclude = (uint32_t)prop_value;
148 150
149 151 /*
150 152 * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss.
151 153 */
152 154 init_desctbls();
153 155
154 156 /*
155 157 * lgrp_init() and possibly cpuid_pass1() need PCI config
156 158 * space access
157 159 */
158 160 #if defined(__xpv)
159 161 if (DOMAIN_IS_INITDOMAIN(xen_info))
160 162 pci_cfgspace_init();
161 163 #else
162 164 pci_cfgspace_init();
163 165 /*
164 166 * Initialize the platform type from CPU 0 to ensure that
165 167 * determine_platform() is only ever called once.
166 168 */
167 169 determine_platform();
168 170 #endif
169 171
170 172 /*
171 173 * The first lightweight pass (pass0) through the cpuid data
172 174 * was done in locore before mlsetup was called. Do the next
173 175 * pass in C code.
174 176 *
175 177 * The x86_featureset is initialized here based on the capabilities
176 178 * of the boot CPU. Note that if we choose to support CPUs that have
177 179 * different feature sets (at which point we would almost certainly
178 180 * want to set the feature bits to correspond to the feature
179 181 * minimum) this value may be altered.
180 182 */
181 183 cpuid_pass1(cpu[0], x86_featureset);
182 184
183 185 #if !defined(__xpv)
184 186 if ((get_hwenv() & HW_XEN_HVM) != 0)
185 187 xen_hvm_init();
186 188
187 189 /*
188 190 * Before we do anything with the TSCs, we need to work around
189 191 * Intel erratum BT81. On some CPUs, warm reset does not
190 192 * clear the TSC. If we are on such a CPU, we will clear TSC ourselves
191 193 * here. Other CPUs will clear it when we boot them later, and the
192 194 * resulting skew will be handled by tsc_sync_master()/_slave();
193 195 * note that such skew already exists and has to be handled anyway.
194 196 *
195 197 * We do this only on metal. This same problem can occur with a
196 198 * hypervisor that does not happen to virtualise a TSC that starts from
197 199 * zero, regardless of CPU type; however, we do not expect hypervisors
198 200 * that do not virtualise TSC that way to handle writes to TSC
199 201 * correctly, either.
200 202 */
201 203 if (get_hwenv() == HW_NATIVE &&
202 204 cpuid_getvendor(CPU) == X86_VENDOR_Intel &&
203 205 cpuid_getfamily(CPU) == 6 &&
204 206 (cpuid_getmodel(CPU) == 0x2d || cpuid_getmodel(CPU) == 0x3e) &&
205 207 is_x86_feature(x86_featureset, X86FSET_TSC)) {
206 208 (void) wrmsr(REG_TSC, 0UL);
207 209 }
208 210
209 211 /*
210 212 * Patch the tsc_read routine with appropriate set of instructions,
211 213 * depending on the processor family and architecure, to read the
212 214 * time-stamp counter while ensuring no out-of-order execution.
213 215 * Patch it while the kernel text is still writable.
214 216 *
215 217 * Note: tsc_read is not patched for intel processors whose family
216 218 * is >6 and for amd whose family >f (in case they don't support rdtscp
217 219 * instruction, unlikely). By default tsc_read will use cpuid for
218 220 * serialization in such cases. The following code needs to be
219 221 * revisited if intel processors of family >= f retains the
|
↓ open down ↓ |
146 lines elided |
↑ open up ↑ |
220 222 * instruction serialization nature of mfence instruction.
221 223 * Note: tsc_read is not patched for x86 processors which do
222 224 * not support "mfence". By default tsc_read will use cpuid for
223 225 * serialization in such cases.
224 226 *
225 227 * The Xen hypervisor does not correctly report whether rdtscp is
226 228 * supported or not, so we must assume that it is not.
227 229 */
228 230 if ((get_hwenv() & HW_XEN_HVM) == 0 &&
229 231 is_x86_feature(x86_featureset, X86FSET_TSCP))
230 - patch_tsc_read(X86_HAVE_TSCP);
232 + patch_tsc_read(TSC_TSCP);
231 233 else if (cpuid_getvendor(CPU) == X86_VENDOR_AMD &&
232 234 cpuid_getfamily(CPU) <= 0xf &&
233 235 is_x86_feature(x86_featureset, X86FSET_SSE2))
234 - patch_tsc_read(X86_TSC_MFENCE);
236 + patch_tsc_read(TSC_RDTSC_MFENCE);
235 237 else if (cpuid_getvendor(CPU) == X86_VENDOR_Intel &&
236 238 cpuid_getfamily(CPU) <= 6 &&
237 239 is_x86_feature(x86_featureset, X86FSET_SSE2))
238 - patch_tsc_read(X86_TSC_LFENCE);
240 + patch_tsc_read(TSC_RDTSC_LFENCE);
239 241
240 242 #endif /* !__xpv */
241 243
242 244 #if defined(__i386) && !defined(__xpv)
243 245 /*
244 246 * Some i386 processors do not implement the rdtsc instruction,
245 247 * or at least they do not implement it correctly. Patch them to
246 248 * return 0.
247 249 */
248 250 if (!is_x86_feature(x86_featureset, X86FSET_TSC))
249 - patch_tsc_read(X86_NO_TSC);
251 + patch_tsc_read(TSC_NONE);
250 252 #endif /* __i386 && !__xpv */
251 253
252 254 #if defined(__amd64) && !defined(__xpv)
253 255 patch_memops(cpuid_getvendor(CPU));
254 256 #endif /* __amd64 && !__xpv */
255 257
256 258 #if !defined(__xpv)
257 259 /* XXPV what, if anything, should be dorked with here under xen? */
258 260
259 261 /*
260 262 * While we're thinking about the TSC, let's set up %cr4 so that
261 263 * userland can issue rdtsc, and initialize the TSC_AUX value
262 264 * (the cpuid) for the rdtscp instruction on appropriately
263 265 * capable hardware.
264 266 */
265 267 if (is_x86_feature(x86_featureset, X86FSET_TSC))
266 268 setcr4(getcr4() & ~CR4_TSD);
267 269
268 270 if (is_x86_feature(x86_featureset, X86FSET_TSCP))
269 271 (void) wrmsr(MSR_AMD_TSCAUX, 0);
270 272
271 273 /*
272 274 * Let's get the other %cr4 stuff while we're here. Note, we defer
273 275 * enabling CR4_SMAP until startup_end(); however, that's importantly
274 276 * before we start other CPUs. That ensures that it will be synced out
275 277 * to other CPUs.
276 278 */
277 279 if (is_x86_feature(x86_featureset, X86FSET_DE))
278 280 setcr4(getcr4() | CR4_DE);
279 281
280 282 if (is_x86_feature(x86_featureset, X86FSET_SMEP))
281 283 setcr4(getcr4() | CR4_SMEP);
282 284 #endif /* __xpv */
283 285
284 286 /*
285 287 * initialize t0
286 288 */
287 289 t0.t_stk = (caddr_t)rp - MINFRAME;
288 290 t0.t_stkbase = t0stack;
289 291 t0.t_pri = maxclsyspri - 3;
290 292 t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
291 293 t0.t_procp = &p0;
292 294 t0.t_plockp = &p0lock.pl_lock;
293 295 t0.t_lwp = &lwp0;
294 296 t0.t_forw = &t0;
295 297 t0.t_back = &t0;
296 298 t0.t_next = &t0;
297 299 t0.t_prev = &t0;
298 300 t0.t_cpu = cpu[0];
299 301 t0.t_disp_queue = &cpu0_disp;
300 302 t0.t_bind_cpu = PBIND_NONE;
301 303 t0.t_bind_pset = PS_NONE;
302 304 t0.t_bindflag = (uchar_t)default_binding_mode;
303 305 t0.t_cpupart = &cp_default;
304 306 t0.t_clfuncs = &sys_classfuncs.thread;
305 307 t0.t_copyops = NULL;
306 308 THREAD_ONPROC(&t0, CPU);
307 309
308 310 lwp0.lwp_thread = &t0;
309 311 lwp0.lwp_regs = (void *)rp;
310 312 lwp0.lwp_procp = &p0;
311 313 t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
312 314
313 315 p0.p_exec = NULL;
314 316 p0.p_stat = SRUN;
315 317 p0.p_flag = SSYS;
316 318 p0.p_tlist = &t0;
317 319 p0.p_stksize = 2*PAGESIZE;
318 320 p0.p_stkpageszc = 0;
319 321 p0.p_as = &kas;
320 322 p0.p_lockp = &p0lock;
321 323 p0.p_brkpageszc = 0;
322 324 p0.p_t1_lgrpid = LGRP_NONE;
323 325 p0.p_tr_lgrpid = LGRP_NONE;
324 326 sigorset(&p0.p_ignore, &ignoredefault);
325 327
326 328 CPU->cpu_thread = &t0;
327 329 bzero(&cpu0_disp, sizeof (disp_t));
328 330 CPU->cpu_disp = &cpu0_disp;
329 331 CPU->cpu_disp->disp_cpu = CPU;
330 332 CPU->cpu_dispthread = &t0;
331 333 CPU->cpu_idle_thread = &t0;
332 334 CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE;
333 335 CPU->cpu_dispatch_pri = t0.t_pri;
334 336
335 337 CPU->cpu_id = 0;
336 338
337 339 CPU->cpu_pri = 12; /* initial PIL for the boot CPU */
338 340
339 341 /*
340 342 * The kernel doesn't use LDTs unless a process explicitly requests one.
341 343 */
342 344 p0.p_ldt_desc = null_sdesc;
343 345
344 346 /*
345 347 * Initialize thread/cpu microstate accounting
346 348 */
347 349 init_mstate(&t0, LMS_SYSTEM);
348 350 init_cpu_mstate(CPU, CMS_SYSTEM);
349 351
350 352 /*
351 353 * Initialize lists of available and active CPUs.
352 354 */
353 355 cpu_list_init(CPU);
354 356
355 357 pg_cpu_bootstrap(CPU);
356 358
357 359 /*
358 360 * Now that we have taken over the GDT, IDT and have initialized
359 361 * active CPU list it's time to inform kmdb if present.
360 362 */
361 363 if (boothowto & RB_DEBUG)
362 364 kdi_idt_sync();
363 365
364 366 /*
365 367 * Explicitly set console to text mode (0x3) if this is a boot
366 368 * post Fast Reboot, and the console is set to CONS_SCREEN_TEXT.
367 369 */
368 370 if (post_fastreboot && boot_console_type(NULL) == CONS_SCREEN_TEXT)
369 371 set_console_mode(0x3);
370 372
371 373 /*
372 374 * If requested (boot -d) drop into kmdb.
373 375 *
374 376 * This must be done after cpu_list_init() on the 64-bit kernel
375 377 * since taking a trap requires that we re-compute gsbase based
376 378 * on the cpu list.
377 379 */
378 380 if (boothowto & RB_DEBUGENTER)
379 381 kmdb_enter();
380 382
381 383 cpu_vm_data_init(CPU);
382 384
383 385 rp->r_fp = 0; /* terminate kernel stack traces! */
384 386
385 387 prom_init("kernel", (void *)NULL);
386 388
387 389 /* User-set option overrides firmware value. */
388 390 if (bootprop_getval(PLAT_DR_OPTIONS_NAME, &prop_value) == 0) {
389 391 plat_dr_options = (uint64_t)prop_value;
390 392 }
391 393 #if defined(__xpv)
392 394 /* No support of DR operations on xpv */
393 395 plat_dr_options = 0;
394 396 #else /* __xpv */
395 397 /* Flag PLAT_DR_FEATURE_ENABLED should only be set by DR driver. */
396 398 plat_dr_options &= ~PLAT_DR_FEATURE_ENABLED;
397 399 #ifndef __amd64
398 400 /* Only enable CPU/memory DR on 64 bits kernel. */
399 401 plat_dr_options &= ~PLAT_DR_FEATURE_MEMORY;
400 402 plat_dr_options &= ~PLAT_DR_FEATURE_CPU;
401 403 #endif /* __amd64 */
402 404 #endif /* __xpv */
403 405
404 406 /*
405 407 * Get value of "plat_dr_physmax" boot option.
406 408 * It overrides values calculated from MSCT or SRAT table.
407 409 */
408 410 if (bootprop_getval(PLAT_DR_PHYSMAX_NAME, &prop_value) == 0) {
409 411 plat_dr_physmax = ((uint64_t)prop_value) >> PAGESHIFT;
410 412 }
411 413
412 414 /* Get value of boot_ncpus. */
413 415 if (bootprop_getval(BOOT_NCPUS_NAME, &prop_value) != 0) {
414 416 boot_ncpus = NCPU;
415 417 } else {
416 418 boot_ncpus = (int)prop_value;
417 419 if (boot_ncpus <= 0 || boot_ncpus > NCPU)
418 420 boot_ncpus = NCPU;
419 421 }
420 422
421 423 /*
422 424 * Set max_ncpus and boot_max_ncpus to boot_ncpus if platform doesn't
423 425 * support CPU DR operations.
424 426 */
425 427 if (plat_dr_support_cpu() == 0) {
426 428 max_ncpus = boot_max_ncpus = boot_ncpus;
427 429 } else {
428 430 if (bootprop_getval(PLAT_MAX_NCPUS_NAME, &prop_value) != 0) {
429 431 max_ncpus = NCPU;
430 432 } else {
431 433 max_ncpus = (int)prop_value;
432 434 if (max_ncpus <= 0 || max_ncpus > NCPU) {
433 435 max_ncpus = NCPU;
434 436 }
435 437 if (boot_ncpus > max_ncpus) {
436 438 boot_ncpus = max_ncpus;
437 439 }
438 440 }
439 441
440 442 if (bootprop_getval(BOOT_MAX_NCPUS_NAME, &prop_value) != 0) {
441 443 boot_max_ncpus = boot_ncpus;
442 444 } else {
443 445 boot_max_ncpus = (int)prop_value;
444 446 if (boot_max_ncpus <= 0 || boot_max_ncpus > NCPU) {
445 447 boot_max_ncpus = boot_ncpus;
446 448 } else if (boot_max_ncpus > max_ncpus) {
447 449 boot_max_ncpus = max_ncpus;
448 450 }
449 451 }
450 452 }
451 453
452 454 /*
453 455 * Initialize the lgrp framework
454 456 */
455 457 lgrp_init(LGRP_INIT_STAGE1);
456 458
457 459 if (boothowto & RB_HALT) {
458 460 prom_printf("unix: kernel halted by -h flag\n");
459 461 prom_enter_mon();
460 462 }
461 463
462 464 ASSERT_STACK_ALIGNED();
463 465
464 466 /*
465 467 * Fill out cpu_ucode_info. Update microcode if necessary.
466 468 */
467 469 ucode_check(CPU);
468 470
469 471 if (workaround_errata(CPU) != 0)
470 472 panic("critical workaround(s) missing for boot cpu");
471 473 }
472 474
473 475
474 476 void
475 477 mach_modpath(char *path, const char *filename)
476 478 {
477 479 /*
478 480 * Construct the directory path from the filename.
479 481 */
480 482
481 483 int len;
482 484 char *p;
483 485 const char isastr[] = "/amd64";
484 486 size_t isalen = strlen(isastr);
485 487
486 488 len = strlen(SYSTEM_BOOT_PATH "/kernel");
487 489 (void) strcpy(path, SYSTEM_BOOT_PATH "/kernel ");
488 490 path += len + 1;
489 491
490 492 if ((p = strrchr(filename, '/')) == NULL)
491 493 return;
492 494
493 495 while (p > filename && *(p - 1) == '/')
494 496 p--; /* remove trailing '/' characters */
495 497 if (p == filename)
496 498 p++; /* so "/" -is- the modpath in this case */
497 499
498 500 /*
499 501 * Remove optional isa-dependent directory name - the module
500 502 * subsystem will put this back again (!)
501 503 */
502 504 len = p - filename;
503 505 if (len > isalen &&
504 506 strncmp(&filename[len - isalen], isastr, isalen) == 0)
505 507 p -= isalen;
506 508
507 509 /*
508 510 * "/platform/mumblefrotz" + " " + MOD_DEFPATH
509 511 */
510 512 len += (p - filename) + 1 + strlen(MOD_DEFPATH) + 1;
511 513 (void) strncpy(path, filename, p - filename);
512 514 }
|
↓ open down ↓ |
253 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX