1 /* Get the frequency of the time base.
2    Copyright (C) 2012-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <stdint.h>
20 #include <string.h>
21 
22 #include <libc-internal.h>
23 #include <not-cancel.h>
24 #include <sysdep-vdso.h>
25 
26 static uint64_t
get_timebase_freq_fallback(void)27 get_timebase_freq_fallback (void)
28 {
29   hp_timing_t result = 0L;
30 
31   /* We read the information from the /proc filesystem.  /proc/cpuinfo
32      contains at least one line like:
33      timebase        : 33333333
34      We search for this line and convert the number into an integer.  */
35   int fd = __open_nocancel ("/proc/cpuinfo", O_RDONLY);
36   if (__glibc_unlikely (fd == -1))
37     return result;
38 
39   /* The timebase will be in the 1st 1024 bytes for systems with up
40      to 8 processors.  If the first read returns less then 1024
41      bytes read,  we have the whole cpuinfo and can start the scan.
42      Otherwise we will have to read more to insure we have the
43      timebase value in the scan.  */
44   char buf[1024];
45   ssize_t n;
46 
47   n = __read_nocancel (fd, buf, sizeof (buf));
48   if (n == sizeof (buf))
49     {
50       /* We are here because the 1st read returned exactly sizeof
51          (buf) bytes.  This implies that we are not at EOF and may
52          not have read the timebase value yet.  So we need to read
53          more bytes until we know we have EOF.  We copy the lower
54          half of buf to the upper half and read sizeof (buf)/2
55          bytes into the lower half of buf and repeat until we
56          reach EOF.  We can assume that the timebase will be in
57          the last 512 bytes of cpuinfo, so two 512 byte half_bufs
58          will be sufficient to contain the timebase and will
59          handle the case where the timebase spans the half_buf
60          boundry.  */
61       const ssize_t half_buf = sizeof (buf) / 2;
62       while (n >= half_buf)
63 	{
64 	  memcpy (buf, buf + half_buf, half_buf);
65 	  n = __read_nocancel (fd, buf + half_buf, half_buf);
66 	}
67       if (n >= 0)
68 	n += half_buf;
69     }
70   __close_nocancel (fd);
71 
72   if (__glibc_likely (n > 0))
73     {
74       char *mhz = memmem (buf, n, "timebase", 7);
75 
76       if (__glibc_likely (mhz != NULL))
77 	{
78 	  char *endp = buf + n;
79 
80 	  /* Search for the beginning of the string.  */
81 	  while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
82 	    ++mhz;
83 
84 	  while (mhz < endp && *mhz != '\n')
85 	    {
86 	      if (*mhz >= '0' && *mhz <= '9')
87 		{
88 		  result *= 10;
89 		  result += *mhz - '0';
90 		}
91 
92 	      ++mhz;
93 	    }
94 	}
95     }
96 
97   return result;
98 }
99 
100 uint64_t
__get_timebase_freq(void)101 __get_timebase_freq (void)
102 {
103   /* The vDSO does not have a fallback mechanism (such calling a syscall).  */
104   uint64_t (*vdsop)(void) = GLRO(dl_vdso_get_tbfreq);
105   if (vdsop == NULL)
106     return get_timebase_freq_fallback ();
107 
108   return INTERNAL_VSYSCALL_CALL_TYPE (vdsop, uint64_t, 0);
109 }
110 weak_alias (__get_timebase_freq, __ppc_get_timebase_freq)
111