1 /* 2 * Copyright (c) 2004 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/kern/subr_unit.c 255057 2013-08-30 07:37:45Z kib $ 27 */ 28 /* 29 * This file and its contents are supplied under the terms of the 30 * Common Development and Distribution License ("CDDL"), version 1.0. 31 * You may only use this file in accordance with the terms of version 32 * 1.0 of the CDDL. 33 * 34 * A full copy of the text of the CDDL should have accompanied this 35 * source. A copy of the CDDL is also available via the Internet at 36 * http://www.illumos.org/license/CDDL. 37 * 38 * Copyright 2014 Pluribus Networks Inc. 39 * Copyright 2019 Joyent, Inc. 40 * Copyright 2020 Oxide Computer Company 41 */ 42 43 #include <sys/types.h> 44 #include <sys/archsystm.h> 45 #include <sys/cpuset.h> 46 #include <sys/fp.h> 47 #include <sys/malloc.h> 48 #include <sys/queue.h> 49 #include <sys/spl.h> 50 #include <sys/systm.h> 51 #include <sys/ddidmareq.h> 52 #include <sys/id_space.h> 53 #include <sys/psm_defs.h> 54 #include <sys/smp_impldefs.h> 55 #include <sys/modhash.h> 56 #include <sys/hma.h> 57 58 #include <sys/x86_archext.h> 59 60 #include <machine/cpufunc.h> 61 #include <machine/md_var.h> 62 #include <machine/specialreg.h> 63 #include <machine/vmm.h> 64 #include <machine/vmparam.h> 65 #include <sys/vmm_impl.h> 66 #include <sys/kernel.h> 67 68 #include <vm/as.h> 69 #include <vm/seg_kmem.h> 70 71 SET_DECLARE(sysinit_set, struct sysinit); 72 73 void 74 sysinit(void) 75 { 76 struct sysinit **si; 77 78 SET_FOREACH(si, sysinit_set) 79 (*si)->func((*si)->data); 80 } 81 82 uint8_t const bin2bcd_data[] = { 83 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 84 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 85 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 86 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 87 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 88 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 89 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 90 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 91 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 92 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99 93 }; 94 95 void 96 invalidate_cache_all(void) 97 { 98 cpuset_t cpuset; 99 100 kpreempt_disable(); 101 cpuset_all_but(&cpuset, CPU->cpu_id); 102 xc_call((xc_arg_t)NULL, (xc_arg_t)NULL, (xc_arg_t)NULL, 103 CPUSET2BV(cpuset), (xc_func_t)invalidate_cache); 104 invalidate_cache(); 105 kpreempt_enable(); 106 } 107 108 vm_paddr_t 109 vtophys(void *va) 110 { 111 pfn_t pfn; 112 113 /* 114 * Since hat_getpfnum() may block on an htable mutex, this is not at 115 * all safe to run from a critical_enter/kpreempt_disable context. 116 * The FreeBSD analog does not have the same locking constraints, so 117 * close attention must be paid wherever this is called. 118 */ 119 ASSERT(curthread->t_preempt == 0); 120 121 pfn = hat_getpfnum(kas.a_hat, (caddr_t)va); 122 ASSERT(pfn != PFN_INVALID); 123 return (pfn << PAGE_SHIFT) | ((uintptr_t)va & PAGE_MASK); 124 } 125 126 int 127 cpusetobj_ffs(const cpuset_t *set) 128 { 129 uint_t large, small; 130 131 /* 132 * Rather than reaching into the cpuset_t ourselves, leave that task to 133 * cpuset_bounds(). The simplicity is worth the extra wasted work to 134 * find the upper bound. 135 */ 136 cpuset_bounds(set, &small, &large); 137 138 if (small == CPUSET_NOTINSET) { 139 /* The FreeBSD version returns 0 if it find nothing */ 140 return (0); 141 } 142 143 ASSERT3U(small, <=, INT_MAX); 144 145 /* Least significant bit index starts at 1 for valid results */ 146 return (small + 1); 147 } 148 149 struct kmem_item { 150 void *addr; 151 size_t size; 152 }; 153 static kmutex_t kmem_items_lock; 154 155 static mod_hash_t *vmm_alloc_hash; 156 uint_t vmm_alloc_hash_nchains = 16381; 157 uint_t vmm_alloc_hash_size = PAGESIZE; 158 159 static void 160 vmm_alloc_hash_valdtor(mod_hash_val_t val) 161 { 162 struct kmem_item *i = (struct kmem_item *)val; 163 164 kmem_free(i->addr, i->size); 165 kmem_free(i, sizeof (struct kmem_item)); 166 } 167 168 static void 169 vmm_alloc_init(void) 170 { 171 vmm_alloc_hash = mod_hash_create_ptrhash("vmm_alloc_hash", 172 vmm_alloc_hash_nchains, vmm_alloc_hash_valdtor, 173 vmm_alloc_hash_size); 174 175 VERIFY(vmm_alloc_hash != NULL); 176 } 177 178 static uint_t 179 vmm_alloc_check(mod_hash_key_t key, mod_hash_val_t *val, void *unused) 180 { 181 struct kmem_item *i = (struct kmem_item *)val; 182 183 cmn_err(CE_PANIC, "!vmm_alloc_check: hash not empty: %p, %lu", i->addr, 184 i->size); 185 186 return (MH_WALK_TERMINATE); 187 } 188 189 static void 190 vmm_alloc_cleanup(void) 191 { 192 mod_hash_walk(vmm_alloc_hash, vmm_alloc_check, NULL); 193 mod_hash_destroy_ptrhash(vmm_alloc_hash); 194 } 195 196 void * 197 malloc(unsigned long size, struct malloc_type *mtp, int flags) 198 { 199 void *p; 200 struct kmem_item *i; 201 int kmem_flag = KM_SLEEP; 202 203 if (flags & M_NOWAIT) 204 kmem_flag = KM_NOSLEEP; 205 206 if (flags & M_ZERO) { 207 p = kmem_zalloc(size, kmem_flag); 208 } else { 209 p = kmem_alloc(size, kmem_flag); 210 } 211 212 if (p == NULL) 213 return (NULL); 214 215 i = kmem_zalloc(sizeof (struct kmem_item), kmem_flag); 216 217 if (i == NULL) { 218 kmem_free(p, size); 219 return (NULL); 220 } 221 222 mutex_enter(&kmem_items_lock); 223 i->addr = p; 224 i->size = size; 225 226 VERIFY(mod_hash_insert(vmm_alloc_hash, 227 (mod_hash_key_t)PHYS_TO_DMAP(vtophys(p)), (mod_hash_val_t)i) == 0); 228 229 mutex_exit(&kmem_items_lock); 230 231 return (p); 232 } 233 234 void 235 free(void *addr, struct malloc_type *mtp) 236 { 237 mutex_enter(&kmem_items_lock); 238 VERIFY(mod_hash_destroy(vmm_alloc_hash, 239 (mod_hash_key_t)PHYS_TO_DMAP(vtophys(addr))) == 0); 240 mutex_exit(&kmem_items_lock); 241 } 242 243 extern void *contig_alloc(size_t, ddi_dma_attr_t *, uintptr_t, int); 244 extern void contig_free(void *, size_t); 245 246 void * 247 contigmalloc(unsigned long size, struct malloc_type *type, int flags, 248 vm_paddr_t low, vm_paddr_t high, unsigned long alignment, 249 vm_paddr_t boundary) 250 { 251 ddi_dma_attr_t attr = { 252 /* Using fastboot_dma_attr as a guide... */ 253 DMA_ATTR_V0, 254 low, /* dma_attr_addr_lo */ 255 high, /* dma_attr_addr_hi */ 256 0x00000000FFFFFFFFULL, /* dma_attr_count_max */ 257 alignment, /* dma_attr_align */ 258 1, /* dma_attr_burstsize */ 259 1, /* dma_attr_minxfer */ 260 0x00000000FFFFFFFFULL, /* dma_attr_maxxfer */ 261 0x00000000FFFFFFFFULL, /* dma_attr_seg: any */ 262 1, /* dma_attr_sgllen */ 263 alignment, /* dma_attr_granular */ 264 0, /* dma_attr_flags */ 265 }; 266 int cansleep = (flags & M_WAITOK); 267 void *result; 268 269 ASSERT(alignment == PAGESIZE); 270 271 result = contig_alloc((size_t)size, &attr, alignment, cansleep); 272 273 if (result != NULL && (flags & M_ZERO) != 0) { 274 bzero(result, size); 275 } 276 return (result); 277 } 278 279 void 280 contigfree(void *addr, unsigned long size, struct malloc_type *type) 281 { 282 contig_free(addr, size); 283 } 284 285 void 286 critical_enter(void) 287 { 288 kpreempt_disable(); 289 } 290 291 void 292 critical_exit(void) 293 { 294 kpreempt_enable(); 295 } 296 297 298 static void 299 vmm_glue_callout_handler(void *arg) 300 { 301 struct callout *c = arg; 302 303 if (callout_active(c)) { 304 /* 305 * Record the handler fire time so that callout_pending() is 306 * able to detect if the callout becomes rescheduled during the 307 * course of the handler. 308 */ 309 c->c_fired = gethrtime(); 310 (c->c_func)(c->c_arg); 311 } 312 } 313 314 void 315 vmm_glue_callout_init(struct callout *c, int mpsafe) 316 { 317 cyc_handler_t hdlr; 318 cyc_time_t when; 319 320 hdlr.cyh_level = CY_LOW_LEVEL; 321 hdlr.cyh_func = vmm_glue_callout_handler; 322 hdlr.cyh_arg = c; 323 when.cyt_when = CY_INFINITY; 324 when.cyt_interval = CY_INFINITY; 325 bzero(c, sizeof (*c)); 326 327 mutex_enter(&cpu_lock); 328 c->c_cyc_id = cyclic_add(&hdlr, &when); 329 mutex_exit(&cpu_lock); 330 } 331 332 void 333 callout_reset_hrtime(struct callout *c, hrtime_t target, void (*func)(void *), 334 void *arg, int flags) 335 { 336 ASSERT(c->c_cyc_id != CYCLIC_NONE); 337 338 if ((flags & C_ABSOLUTE) == 0) { 339 target += gethrtime(); 340 } 341 342 c->c_func = func; 343 c->c_arg = arg; 344 c->c_target = target; 345 (void) cyclic_reprogram(c->c_cyc_id, target); 346 } 347 348 void 349 vmm_glue_callout_stop(struct callout *c) 350 { 351 ASSERT(c->c_cyc_id != CYCLIC_NONE); 352 353 c->c_target = 0; 354 (void) cyclic_reprogram(c->c_cyc_id, CY_INFINITY); 355 } 356 357 void 358 vmm_glue_callout_drain(struct callout *c) 359 { 360 ASSERT(c->c_cyc_id != CYCLIC_NONE); 361 362 c->c_target = 0; 363 mutex_enter(&cpu_lock); 364 cyclic_remove(c->c_cyc_id); 365 c->c_cyc_id = CYCLIC_NONE; 366 mutex_exit(&cpu_lock); 367 } 368 369 void 370 vmm_glue_callout_localize(struct callout *c) 371 { 372 mutex_enter(&cpu_lock); 373 cyclic_move_here(c->c_cyc_id); 374 mutex_exit(&cpu_lock); 375 } 376 377 /* 378 * Given an interval (in ns) and a frequency (in hz), calculate the number of 379 * "ticks" at that frequency which cover the interval. 380 */ 381 uint64_t 382 hrt_freq_count(hrtime_t interval, uint32_t freq) 383 { 384 ASSERT3S(interval, >=, 0); 385 const uint64_t sec = interval / NANOSEC; 386 const uint64_t nsec = interval % NANOSEC; 387 388 return ((sec * freq) + ((nsec * freq) / NANOSEC)); 389 } 390 391 /* 392 * Given a frequency (in hz) and number of "ticks", calculate the interval 393 * (in ns) which would be covered by those ticks. 394 */ 395 hrtime_t 396 hrt_freq_interval(uint32_t freq, uint64_t count) 397 { 398 const uint64_t sec = count / freq; 399 const uint64_t frac = count % freq; 400 401 return ((NANOSEC * sec) + ((frac * NANOSEC) / freq)); 402 } 403 404 405 uint_t cpu_high; /* Highest arg to CPUID */ 406 uint_t cpu_exthigh; /* Highest arg to extended CPUID */ 407 uint_t cpu_id; /* Stepping ID */ 408 char cpu_vendor[20]; /* CPU Origin code */ 409 410 static void 411 vmm_cpuid_init(void) 412 { 413 uint_t regs[4]; 414 415 do_cpuid(0, regs); 416 cpu_high = regs[0]; 417 ((uint_t *)&cpu_vendor)[0] = regs[1]; 418 ((uint_t *)&cpu_vendor)[1] = regs[3]; 419 ((uint_t *)&cpu_vendor)[2] = regs[2]; 420 cpu_vendor[12] = '\0'; 421 422 do_cpuid(1, regs); 423 cpu_id = regs[0]; 424 425 do_cpuid(0x80000000, regs); 426 cpu_exthigh = regs[0]; 427 } 428 429 void 430 vmm_sol_glue_init(void) 431 { 432 vmm_alloc_init(); 433 vmm_cpuid_init(); 434 } 435 436 void 437 vmm_sol_glue_cleanup(void) 438 { 439 vmm_alloc_cleanup(); 440 } 441 442 443 /* From FreeBSD's sys/kern/subr_clock.c */ 444 445 /*- 446 * Copyright (c) 1988 University of Utah. 447 * Copyright (c) 1982, 1990, 1993 448 * The Regents of the University of California. All rights reserved. 449 * 450 * This code is derived from software contributed to Berkeley by 451 * the Systems Programming Group of the University of Utah Computer 452 * Science Department. 453 * 454 * Redistribution and use in source and binary forms, with or without 455 * modification, are permitted provided that the following conditions 456 * are met: 457 * 1. Redistributions of source code must retain the above copyright 458 * notice, this list of conditions and the following disclaimer. 459 * 2. Redistributions in binary form must reproduce the above copyright 460 * notice, this list of conditions and the following disclaimer in the 461 * documentation and/or other materials provided with the distribution. 462 * 4. Neither the name of the University nor the names of its contributors 463 * may be used to endorse or promote products derived from this software 464 * without specific prior written permission. 465 * 466 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 467 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 468 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 469 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 470 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 471 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 472 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 473 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 474 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 475 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 476 * SUCH DAMAGE. 477 * 478 * from: Utah $Hdr: clock.c 1.18 91/01/21$ 479 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94 480 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp 481 * and 482 * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04 483 */ 484 485 #include <sys/clock.h> 486 487 /* 488 * Generic routines to convert between a POSIX date 489 * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec 490 * Derived from NetBSD arch/hp300/hp300/clock.c 491 */ 492 493 #define FEBRUARY 2 494 #define days_in_year(y) (leapyear(y) ? 366 : 365) 495 #define days_in_month(y, m) \ 496 (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0)) 497 /* Day of week. Days are counted from 1/1/1970, which was a Thursday */ 498 #define day_of_week(days) (((days) + 4) % 7) 499 500 static const int month_days[12] = { 501 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 502 }; 503 504 505 /* 506 * This inline avoids some unnecessary modulo operations 507 * as compared with the usual macro: 508 * ( ((year % 4) == 0 && 509 * (year % 100) != 0) || 510 * ((year % 400) == 0) ) 511 * It is otherwise equivalent. 512 */ 513 static int 514 leapyear(int year) 515 { 516 int rv = 0; 517 518 if ((year & 3) == 0) { 519 rv = 1; 520 if ((year % 100) == 0) { 521 rv = 0; 522 if ((year % 400) == 0) 523 rv = 1; 524 } 525 } 526 return (rv); 527 } 528 529 int 530 clock_ct_to_ts(struct clocktime *ct, struct timespec *ts) 531 { 532 int i, year, days; 533 534 year = ct->year; 535 536 #ifdef __FreeBSD__ 537 if (ct_debug) { 538 printf("ct_to_ts("); 539 print_ct(ct); 540 printf(")"); 541 } 542 #endif 543 544 /* Sanity checks. */ 545 if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 || 546 ct->day > days_in_month(year, ct->mon) || 547 ct->hour > 23 || ct->min > 59 || ct->sec > 59 || 548 (sizeof (time_t) == 4 && year > 2037)) { /* time_t overflow */ 549 #ifdef __FreeBSD__ 550 if (ct_debug) 551 printf(" = EINVAL\n"); 552 #endif 553 return (EINVAL); 554 } 555 556 /* 557 * Compute days since start of time 558 * First from years, then from months. 559 */ 560 days = 0; 561 for (i = POSIX_BASE_YEAR; i < year; i++) 562 days += days_in_year(i); 563 564 /* Months */ 565 for (i = 1; i < ct->mon; i++) 566 days += days_in_month(year, i); 567 days += (ct->day - 1); 568 569 ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 + 570 ct->sec; 571 ts->tv_nsec = ct->nsec; 572 573 #ifdef __FreeBSD__ 574 if (ct_debug) 575 printf(" = %ld.%09ld\n", (long)ts->tv_sec, (long)ts->tv_nsec); 576 #endif 577 return (0); 578 } 579 580 void 581 clock_ts_to_ct(struct timespec *ts, struct clocktime *ct) 582 { 583 int i, year, days; 584 time_t rsec; /* remainder seconds */ 585 time_t secs; 586 587 secs = ts->tv_sec; 588 days = secs / SECDAY; 589 rsec = secs % SECDAY; 590 591 ct->dow = day_of_week(days); 592 593 /* Subtract out whole years, counting them in i. */ 594 for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++) 595 days -= days_in_year(year); 596 ct->year = year; 597 598 /* Subtract out whole months, counting them in i. */ 599 for (i = 1; days >= days_in_month(year, i); i++) 600 days -= days_in_month(year, i); 601 ct->mon = i; 602 603 /* Days are what is left over (+1) from all that. */ 604 ct->day = days + 1; 605 606 /* Hours, minutes, seconds are easy */ 607 ct->hour = rsec / 3600; 608 rsec = rsec % 3600; 609 ct->min = rsec / 60; 610 rsec = rsec % 60; 611 ct->sec = rsec; 612 ct->nsec = ts->tv_nsec; 613 #ifdef __FreeBSD__ 614 if (ct_debug) { 615 printf("ts_to_ct(%ld.%09ld) = ", 616 (long)ts->tv_sec, (long)ts->tv_nsec); 617 print_ct(ct); 618 printf("\n"); 619 } 620 #endif 621 } 622 623 /* Equivalent to the FreeBSD rdtsc(), but with any necessary per-cpu offset */ 624 uint64_t 625 rdtsc_offset(void) 626 { 627 /* 628 * The timestamp logic will decide if a delta need be applied to the 629 * unscaled hrtime reading (effectively rdtsc), but we do require it be 630 * backed by the TSC itself. 631 */ 632 extern hrtime_t (*gethrtimeunscaledf)(void); 633 extern hrtime_t tsc_gethrtimeunscaled(void); 634 extern hrtime_t tsc_gethrtimeunscaled_delta(void); 635 636 ASSERT(*gethrtimeunscaledf == tsc_gethrtimeunscaled || 637 *gethrtimeunscaledf == tsc_gethrtimeunscaled_delta); 638 return ((uint64_t)gethrtimeunscaledf()); 639 } 640