1/*- 2 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org> 3 * Copyright (c) 2016 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Konstantin Belousov 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD$"); 33 34#include <sys/param.h> 35#include "namespace.h" 36#include <sys/elf.h> 37#include <sys/fcntl.h> 38#include <sys/mman.h> 39#include <sys/time.h> 40#include <sys/vdso.h> 41#include <errno.h> 42#include <string.h> 43#include <unistd.h> 44#include "un-namespace.h" 45#include <machine/cpufunc.h> 46#include <machine/specialreg.h> 47#include <dev/acpica/acpi_hpet.h> 48#include "libc_private.h" 49 50static void 51lfence_mb(void) 52{ 53#if defined(__i386__) 54 static int lfence_works = -1; 55 u_int cpuid_supported, p[4]; 56 57 if (lfence_works == -1) { 58 __asm __volatile( 59 " pushfl\n" 60 " popl %%eax\n" 61 " movl %%eax,%%ecx\n" 62 " xorl $0x200000,%%eax\n" 63 " pushl %%eax\n" 64 " popfl\n" 65 " pushfl\n" 66 " popl %%eax\n" 67 " xorl %%eax,%%ecx\n" 68 " je 1f\n" 69 " movl $1,%0\n" 70 " jmp 2f\n" 71 "1: movl $0,%0\n" 72 "2:\n" 73 : "=r" (cpuid_supported) : : "eax", "ecx", "cc"); 74 if (cpuid_supported) { 75 __asm __volatile( 76 " pushl %%ebx\n" 77 " cpuid\n" 78 " movl %%ebx,%1\n" 79 " popl %%ebx\n" 80 : "=a" (p[0]), "=r" (p[1]), "=c" (p[2]), "=d" (p[3]) 81 : "0" (0x1)); 82 lfence_works = (p[3] & CPUID_SSE2) != 0; 83 } else 84 lfence_works = 0; 85 } 86 if (lfence_works == 1) 87 lfence(); 88#elif defined(__amd64__) 89 lfence(); 90#else 91#error "arch" 92#endif 93} 94 95static u_int 96__vdso_gettc_rdtsc_low(const struct vdso_timehands *th) 97{ 98 u_int rv; 99 100 lfence_mb(); 101 __asm __volatile("rdtsc; shrd %%cl, %%edx, %0" 102 : "=a" (rv) : "c" (th->th_x86_shift) : "edx"); 103 return (rv); 104} 105 106static u_int 107__vdso_rdtsc32(void) 108{ 109 110 lfence_mb(); 111 return (rdtsc32()); 112} 113 114static char *hpet_dev_map = NULL; 115static uint32_t hpet_idx = 0xffffffff; 116 117static void 118__vdso_init_hpet(uint32_t u) 119{ 120 static const char devprefix[] = "/dev/hpet"; 121 char devname[64], *c, *c1, t; 122 int fd; 123 124 c1 = c = stpcpy(devname, devprefix); 125 u = hpet_idx; 126 do { 127 *c++ = u % 10 + '0'; 128 u /= 10; 129 } while (u != 0); 130 *c = '\0'; 131 for (c--; c1 != c; c1++, c--) { 132 t = *c1; 133 *c1 = *c; 134 *c = t; 135 } 136 fd = _open(devname, O_RDONLY); 137 if (fd == -1) { 138 hpet_dev_map = MAP_FAILED; 139 return; 140 } 141 if (hpet_dev_map != NULL && hpet_dev_map != MAP_FAILED) 142 munmap(hpet_dev_map, PAGE_SIZE); 143 hpet_dev_map = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0); 144 _close(fd); 145} 146 147#pragma weak __vdso_gettc 148int 149__vdso_gettc(const struct vdso_timehands *th, u_int *tc) 150{ 151 uint32_t tmp; 152 153 switch (th->th_algo) { 154 case VDSO_TH_ALGO_X86_TSC: 155 *tc = th->th_x86_shift > 0 ? __vdso_gettc_rdtsc_low(th) : 156 __vdso_rdtsc32(); 157 return (0); 158 case VDSO_TH_ALGO_X86_HPET: 159 tmp = th->th_x86_hpet_idx; 160 if (hpet_dev_map == NULL || tmp != hpet_idx) { 161 hpet_idx = tmp; 162 __vdso_init_hpet(hpet_idx); 163 } 164 if (hpet_dev_map == MAP_FAILED) 165 return (ENOSYS); 166 *tc = *(volatile uint32_t *)(hpet_dev_map + HPET_MAIN_COUNTER); 167 return (0); 168 default: 169 return (ENOSYS); 170 } 171} 172 173#pragma weak __vdso_gettimekeep 174int 175__vdso_gettimekeep(struct vdso_timekeep **tk) 176{ 177 178 return (_elf_aux_info(AT_TIMEKEEP, tk, sizeof(*tk))); 179} 180