Coverage Report

Created: 2024-05-20 06:11

/src/FreeRDP/winpr/libwinpr/sysinfo/sysinfo.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * System Information
4
 *
5
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2013 Bernhard Miklautz <bernhard.miklautz@thincast.com>
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
#include <winpr/config.h>
22
23
#include <winpr/sysinfo.h>
24
#include <winpr/platform.h>
25
26
#if defined(ANDROID)
27
#include "cpufeatures/cpu-features.h"
28
#endif
29
30
#if defined(__linux__)
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
#include <fcntl.h>
34
#endif
35
36
#if !defined(_WIN32)
37
#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 199309L)
38
#include <time.h>
39
#elif !defined(__APPLE__)
40
#include <sys/time.h>
41
#include <sys/sysinfo.h>
42
#endif
43
#endif
44
45
#include "../log.h"
46
#define TAG WINPR_TAG("sysinfo")
47
48
0
#define FILETIME_TO_UNIX_OFFSET_S 11644473600UL
49
50
#if defined(__MACH__) && defined(__APPLE__)
51
52
#include <mach/mach_time.h>
53
54
static UINT64 scaleHighPrecision(UINT64 i, UINT32 numer, UINT32 denom)
55
{
56
  UINT64 high = (i >> 32) * numer;
57
  UINT64 low = (i & 0xffffffffull) * numer / denom;
58
  UINT64 highRem = ((high % denom) << 32) / denom;
59
  high /= denom;
60
  return (high << 32) + highRem + low;
61
}
62
63
static UINT64 mac_get_time_ns(void)
64
{
65
  mach_timebase_info_data_t timebase = { 0 };
66
  mach_timebase_info(&timebase);
67
  UINT64 t = mach_absolute_time();
68
  return scaleHighPrecision(t, timebase.numer, timebase.denom);
69
}
70
71
#endif
72
73
/**
74
 * api-ms-win-core-sysinfo-l1-1-1.dll:
75
 *
76
 * EnumSystemFirmwareTables
77
 * GetSystemFirmwareTable
78
 * GetLogicalProcessorInformation
79
 * GetLogicalProcessorInformationEx
80
 * GetProductInfo
81
 * GetSystemDirectoryA
82
 * GetSystemDirectoryW
83
 * GetSystemTimeAdjustment
84
 * GetSystemWindowsDirectoryA
85
 * GetSystemWindowsDirectoryW
86
 * GetWindowsDirectoryA
87
 * GetWindowsDirectoryW
88
 * GlobalMemoryStatusEx
89
 * SetComputerNameExW
90
 * VerSetConditionMask
91
 */
92
93
#ifndef _WIN32
94
95
#include <time.h>
96
#include <sys/time.h>
97
98
#ifdef WINPR_HAVE_UNISTD_H
99
#include <unistd.h>
100
#endif
101
102
#include <winpr/crt.h>
103
#include <winpr/platform.h>
104
105
#if defined(__MACOSX__) || defined(__IOS__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
106
    defined(__OpenBSD__) || defined(__DragonFly__)
107
#include <sys/sysctl.h>
108
#endif
109
110
static DWORD GetProcessorArchitecture(void)
111
11.4k
{
112
11.4k
  DWORD cpuArch = PROCESSOR_ARCHITECTURE_UNKNOWN;
113
#if defined(ANDROID)
114
  AndroidCpuFamily family = android_getCpuFamily();
115
116
  switch (family)
117
  {
118
    case ANDROID_CPU_FAMILY_ARM:
119
      return PROCESSOR_ARCHITECTURE_ARM;
120
121
    case ANDROID_CPU_FAMILY_X86:
122
      return PROCESSOR_ARCHITECTURE_INTEL;
123
124
    case ANDROID_CPU_FAMILY_MIPS:
125
      return PROCESSOR_ARCHITECTURE_MIPS;
126
127
    case ANDROID_CPU_FAMILY_ARM64:
128
      return PROCESSOR_ARCHITECTURE_ARM64;
129
130
    case ANDROID_CPU_FAMILY_X86_64:
131
      return PROCESSOR_ARCHITECTURE_AMD64;
132
133
    case ANDROID_CPU_FAMILY_MIPS64:
134
      return PROCESSOR_ARCHITECTURE_MIPS64;
135
136
    default:
137
      return PROCESSOR_ARCHITECTURE_UNKNOWN;
138
  }
139
140
#elif defined(_M_ARM)
141
  cpuArch = PROCESSOR_ARCHITECTURE_ARM;
142
#elif defined(_M_IX86)
143
  cpuArch = PROCESSOR_ARCHITECTURE_INTEL;
144
#elif defined(_M_MIPS64)
145
  /* Needs to be before __mips__ since the compiler defines both */
146
  cpuArch = PROCESSOR_ARCHITECTURE_MIPS64;
147
#elif defined(_M_MIPS)
148
  cpuArch = PROCESSOR_ARCHITECTURE_MIPS;
149
#elif defined(_M_ARM64)
150
  cpuArch = PROCESSOR_ARCHITECTURE_ARM64;
151
#elif defined(_M_AMD64)
152
11.4k
  cpuArch = PROCESSOR_ARCHITECTURE_AMD64;
153
#elif defined(_M_PPC)
154
  cpuArch = PROCESSOR_ARCHITECTURE_PPC;
155
#elif defined(_M_ALPHA)
156
  cpuArch = PROCESSOR_ARCHITECTURE_ALPHA;
157
#elif defined(_M_E2K)
158
  cpuArch = PROCESSOR_ARCHITECTURE_E2K;
159
#endif
160
11.4k
  return cpuArch;
161
11.4k
}
162
163
static DWORD GetNumberOfProcessors(void)
164
11.4k
{
165
11.4k
  DWORD numCPUs = 1;
166
#if defined(ANDROID)
167
  return android_getCpuCount();
168
  /* TODO: iOS */
169
#elif defined(__linux__) || defined(__sun) || defined(_AIX)
170
  numCPUs = (DWORD)sysconf(_SC_NPROCESSORS_ONLN);
171
#elif defined(__MACOSX__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
172
    defined(__OpenBSD__) || defined(__DragonFly__)
173
  {
174
    int mib[4];
175
    size_t length = sizeof(numCPUs);
176
    mib[0] = CTL_HW;
177
#if defined(__FreeBSD__) || defined(__OpenBSD__)
178
    mib[1] = HW_NCPU;
179
#else
180
    mib[1] = HW_AVAILCPU;
181
#endif
182
    sysctl(mib, 2, &numCPUs, &length, NULL, 0);
183
184
    if (numCPUs < 1)
185
    {
186
      mib[1] = HW_NCPU;
187
      sysctl(mib, 2, &numCPUs, &length, NULL, 0);
188
189
      if (numCPUs < 1)
190
        numCPUs = 1;
191
    }
192
  }
193
#elif defined(__hpux)
194
  numCPUs = (DWORD)mpctl(MPC_GETNUMSPUS, NULL, NULL);
195
#elif defined(__sgi)
196
  numCPUs = (DWORD)sysconf(_SC_NPROC_ONLN);
197
#endif
198
11.4k
  return numCPUs;
199
11.4k
}
200
201
static DWORD GetSystemPageSize(void)
202
11.4k
{
203
11.4k
  DWORD dwPageSize = 0;
204
11.4k
  long sc_page_size = -1;
205
11.4k
#if defined(_SC_PAGESIZE)
206
207
11.4k
  if (sc_page_size < 0)
208
11.4k
    sc_page_size = sysconf(_SC_PAGESIZE);
209
210
11.4k
#endif
211
11.4k
#if defined(_SC_PAGE_SIZE)
212
213
11.4k
  if (sc_page_size < 0)
214
0
    sc_page_size = sysconf(_SC_PAGE_SIZE);
215
216
11.4k
#endif
217
218
11.4k
  if (sc_page_size > 0)
219
11.4k
    dwPageSize = (DWORD)sc_page_size;
220
221
11.4k
  if (dwPageSize < 4096)
222
0
    dwPageSize = 4096;
223
224
11.4k
  return dwPageSize;
225
11.4k
}
226
227
void GetSystemInfo(LPSYSTEM_INFO lpSystemInfo)
228
11.4k
{
229
11.4k
  lpSystemInfo->wProcessorArchitecture = GetProcessorArchitecture();
230
11.4k
  lpSystemInfo->wReserved = 0;
231
11.4k
  lpSystemInfo->dwPageSize = GetSystemPageSize();
232
11.4k
  lpSystemInfo->lpMinimumApplicationAddress = NULL;
233
11.4k
  lpSystemInfo->lpMaximumApplicationAddress = NULL;
234
11.4k
  lpSystemInfo->dwActiveProcessorMask = 0;
235
11.4k
  lpSystemInfo->dwNumberOfProcessors = GetNumberOfProcessors();
236
11.4k
  lpSystemInfo->dwProcessorType = 0;
237
11.4k
  lpSystemInfo->dwAllocationGranularity = 0;
238
11.4k
  lpSystemInfo->wProcessorLevel = 0;
239
11.4k
  lpSystemInfo->wProcessorRevision = 0;
240
11.4k
}
241
242
void GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo)
243
11.4k
{
244
11.4k
  GetSystemInfo(lpSystemInfo);
245
11.4k
}
246
247
void GetSystemTime(LPSYSTEMTIME lpSystemTime)
248
0
{
249
0
  time_t ct = 0;
250
0
  struct tm tres;
251
0
  struct tm* stm = NULL;
252
0
  WORD wMilliseconds = 0;
253
0
  ct = time(NULL);
254
0
  wMilliseconds = (WORD)(GetTickCount() % 1000);
255
0
  stm = gmtime_r(&ct, &tres);
256
0
  ZeroMemory(lpSystemTime, sizeof(SYSTEMTIME));
257
258
0
  if (stm)
259
0
  {
260
0
    lpSystemTime->wYear = (WORD)(stm->tm_year + 1900);
261
0
    lpSystemTime->wMonth = (WORD)(stm->tm_mon + 1);
262
0
    lpSystemTime->wDayOfWeek = (WORD)stm->tm_wday;
263
0
    lpSystemTime->wDay = (WORD)stm->tm_mday;
264
0
    lpSystemTime->wHour = (WORD)stm->tm_hour;
265
0
    lpSystemTime->wMinute = (WORD)stm->tm_min;
266
0
    lpSystemTime->wSecond = (WORD)stm->tm_sec;
267
0
    lpSystemTime->wMilliseconds = wMilliseconds;
268
0
  }
269
0
}
270
271
BOOL SetSystemTime(CONST SYSTEMTIME* lpSystemTime)
272
0
{
273
  /* TODO: Implement */
274
0
  return FALSE;
275
0
}
276
277
VOID GetLocalTime(LPSYSTEMTIME lpSystemTime)
278
6.91M
{
279
6.91M
  time_t ct = 0;
280
6.91M
  struct tm tres;
281
6.91M
  struct tm* ltm = NULL;
282
6.91M
  WORD wMilliseconds = 0;
283
6.91M
  ct = time(NULL);
284
6.91M
  wMilliseconds = (WORD)(GetTickCount() % 1000);
285
6.91M
  ltm = localtime_r(&ct, &tres);
286
6.91M
  ZeroMemory(lpSystemTime, sizeof(SYSTEMTIME));
287
288
6.91M
  if (ltm)
289
6.91M
  {
290
6.91M
    lpSystemTime->wYear = (WORD)(ltm->tm_year + 1900);
291
6.91M
    lpSystemTime->wMonth = (WORD)(ltm->tm_mon + 1);
292
6.91M
    lpSystemTime->wDayOfWeek = (WORD)ltm->tm_wday;
293
6.91M
    lpSystemTime->wDay = (WORD)ltm->tm_mday;
294
6.91M
    lpSystemTime->wHour = (WORD)ltm->tm_hour;
295
6.91M
    lpSystemTime->wMinute = (WORD)ltm->tm_min;
296
6.91M
    lpSystemTime->wSecond = (WORD)ltm->tm_sec;
297
6.91M
    lpSystemTime->wMilliseconds = wMilliseconds;
298
6.91M
  }
299
6.91M
}
300
301
BOOL SetLocalTime(CONST SYSTEMTIME* lpSystemTime)
302
0
{
303
  /* TODO: Implement */
304
0
  return FALSE;
305
0
}
306
307
VOID GetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime)
308
0
{
309
0
  union
310
0
  {
311
0
    UINT64 u64;
312
0
    FILETIME ft;
313
0
  } t;
314
315
0
  t.u64 = (winpr_GetUnixTimeNS() / 100ull) + FILETIME_TO_UNIX_OFFSET_S * 10000000ull;
316
0
  *lpSystemTimeAsFileTime = t.ft;
317
0
}
318
319
BOOL GetSystemTimeAdjustment(PDWORD lpTimeAdjustment, PDWORD lpTimeIncrement,
320
                             PBOOL lpTimeAdjustmentDisabled)
321
0
{
322
  /* TODO: Implement */
323
0
  return FALSE;
324
0
}
325
326
#ifndef CLOCK_MONOTONIC_RAW
327
#define CLOCK_MONOTONIC_RAW 4
328
#endif
329
330
DWORD GetTickCount(void)
331
6.91M
{
332
6.91M
  return GetTickCount64();
333
6.91M
}
334
#endif // _WIN32
335
336
#if !defined(_WIN32) || defined(_UWP)
337
338
#if defined(WITH_WINPR_DEPRECATED)
339
/* OSVERSIONINFOEX Structure:
340
 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724833
341
 */
342
343
BOOL GetVersionExA(LPOSVERSIONINFOA lpVersionInformation)
344
{
345
#ifdef _UWP
346
347
  /* Windows 10 Version Info */
348
  if ((lpVersionInformation->dwOSVersionInfoSize == sizeof(OSVERSIONINFOA)) ||
349
      (lpVersionInformation->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA)))
350
  {
351
    lpVersionInformation->dwMajorVersion = 10;
352
    lpVersionInformation->dwMinorVersion = 0;
353
    lpVersionInformation->dwBuildNumber = 0;
354
    lpVersionInformation->dwPlatformId = VER_PLATFORM_WIN32_NT;
355
    ZeroMemory(lpVersionInformation->szCSDVersion, sizeof(lpVersionInformation->szCSDVersion));
356
357
    if (lpVersionInformation->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA))
358
    {
359
      LPOSVERSIONINFOEXA lpVersionInformationEx = (LPOSVERSIONINFOEXA)lpVersionInformation;
360
      lpVersionInformationEx->wServicePackMajor = 0;
361
      lpVersionInformationEx->wServicePackMinor = 0;
362
      lpVersionInformationEx->wSuiteMask = 0;
363
      lpVersionInformationEx->wProductType = VER_NT_WORKSTATION;
364
      lpVersionInformationEx->wReserved = 0;
365
    }
366
367
    return TRUE;
368
  }
369
370
#else
371
372
  /* Windows 7 SP1 Version Info */
373
  if ((lpVersionInformation->dwOSVersionInfoSize == sizeof(OSVERSIONINFOA)) ||
374
      (lpVersionInformation->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA)))
375
  {
376
    lpVersionInformation->dwMajorVersion = 6;
377
    lpVersionInformation->dwMinorVersion = 1;
378
    lpVersionInformation->dwBuildNumber = 7601;
379
    lpVersionInformation->dwPlatformId = VER_PLATFORM_WIN32_NT;
380
    ZeroMemory(lpVersionInformation->szCSDVersion, sizeof(lpVersionInformation->szCSDVersion));
381
382
    if (lpVersionInformation->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA))
383
    {
384
      LPOSVERSIONINFOEXA lpVersionInformationEx = (LPOSVERSIONINFOEXA)lpVersionInformation;
385
      lpVersionInformationEx->wServicePackMajor = 1;
386
      lpVersionInformationEx->wServicePackMinor = 0;
387
      lpVersionInformationEx->wSuiteMask = 0;
388
      lpVersionInformationEx->wProductType = VER_NT_WORKSTATION;
389
      lpVersionInformationEx->wReserved = 0;
390
    }
391
392
    return TRUE;
393
  }
394
395
#endif
396
  return FALSE;
397
}
398
399
BOOL GetVersionExW(LPOSVERSIONINFOW lpVersionInformation)
400
{
401
  ZeroMemory(lpVersionInformation->szCSDVersion, sizeof(lpVersionInformation->szCSDVersion));
402
  return GetVersionExA((LPOSVERSIONINFOA)lpVersionInformation);
403
}
404
405
#endif
406
407
#endif
408
409
#if !defined(_WIN32) || defined(_UWP)
410
411
BOOL GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
412
0
{
413
0
  BOOL rc = 0;
414
0
  LPSTR buffer = NULL;
415
0
  if (!lpnSize || (*lpnSize > INT_MAX))
416
0
    return FALSE;
417
418
0
  if (*lpnSize > 0)
419
0
  {
420
0
    buffer = malloc(*lpnSize);
421
0
    if (!buffer)
422
0
      return FALSE;
423
0
  }
424
0
  rc = GetComputerNameA(buffer, lpnSize);
425
426
0
  if (rc && (*lpnSize > 0))
427
0
  {
428
0
    const SSIZE_T res = ConvertUtf8NToWChar(buffer, *lpnSize, lpBuffer, *lpnSize);
429
0
    rc = res > 0;
430
0
  }
431
432
0
  free(buffer);
433
434
0
  return rc;
435
0
}
436
437
BOOL GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
438
119k
{
439
119k
  char* dot = NULL;
440
119k
  size_t length = 0;
441
119k
  char hostname[256] = { 0 };
442
443
119k
  if (!lpnSize)
444
0
  {
445
0
    SetLastError(ERROR_BAD_ARGUMENTS);
446
0
    return FALSE;
447
0
  }
448
449
119k
  if (gethostname(hostname, sizeof(hostname)) == -1)
450
0
    return FALSE;
451
452
119k
  length = strnlen(hostname, sizeof(hostname));
453
119k
  dot = strchr(hostname, '.');
454
455
119k
  if (dot)
456
0
    length = (dot - hostname);
457
458
119k
  if ((*lpnSize <= (DWORD)length) || !lpBuffer)
459
0
  {
460
0
    SetLastError(ERROR_BUFFER_OVERFLOW);
461
0
    *lpnSize = (DWORD)(length + 1);
462
0
    return FALSE;
463
0
  }
464
465
119k
  CopyMemory(lpBuffer, hostname, length);
466
119k
  lpBuffer[length] = '\0';
467
119k
  *lpnSize = (DWORD)length;
468
119k
  return TRUE;
469
119k
}
470
471
BOOL GetComputerNameExA(COMPUTER_NAME_FORMAT NameType, LPSTR lpBuffer, LPDWORD lpnSize)
472
59.9k
{
473
59.9k
  size_t length = 0;
474
59.9k
  char hostname[256] = { 0 };
475
476
59.9k
  if (!lpnSize)
477
0
  {
478
0
    SetLastError(ERROR_BAD_ARGUMENTS);
479
0
    return FALSE;
480
0
  }
481
482
59.9k
  if ((NameType == ComputerNameNetBIOS) || (NameType == ComputerNamePhysicalNetBIOS))
483
59.9k
  {
484
59.9k
    BOOL rc = GetComputerNameA(lpBuffer, lpnSize);
485
486
59.9k
    if (!rc)
487
0
    {
488
0
      if (GetLastError() == ERROR_BUFFER_OVERFLOW)
489
0
        SetLastError(ERROR_MORE_DATA);
490
0
    }
491
492
59.9k
    return rc;
493
59.9k
  }
494
495
0
  if (gethostname(hostname, sizeof(hostname)) == -1)
496
0
    return FALSE;
497
498
0
  length = strnlen(hostname, sizeof(hostname));
499
500
0
  switch (NameType)
501
0
  {
502
0
    case ComputerNameDnsHostname:
503
0
    case ComputerNameDnsDomain:
504
0
    case ComputerNameDnsFullyQualified:
505
0
    case ComputerNamePhysicalDnsHostname:
506
0
    case ComputerNamePhysicalDnsDomain:
507
0
    case ComputerNamePhysicalDnsFullyQualified:
508
0
      if ((*lpnSize <= (DWORD)length) || !lpBuffer)
509
0
      {
510
0
        *lpnSize = (DWORD)(length + 1);
511
0
        SetLastError(ERROR_MORE_DATA);
512
0
        return FALSE;
513
0
      }
514
515
0
      CopyMemory(lpBuffer, hostname, length);
516
0
      lpBuffer[length] = '\0';
517
0
      *lpnSize = (DWORD)length;
518
0
      break;
519
520
0
    default:
521
0
      return FALSE;
522
0
  }
523
524
0
  return TRUE;
525
0
}
526
527
BOOL GetComputerNameExW(COMPUTER_NAME_FORMAT NameType, LPWSTR lpBuffer, LPDWORD lpnSize)
528
0
{
529
0
  BOOL rc = 0;
530
0
  LPSTR lpABuffer = NULL;
531
532
0
  if (!lpnSize)
533
0
  {
534
0
    SetLastError(ERROR_BAD_ARGUMENTS);
535
0
    return FALSE;
536
0
  }
537
538
0
  if (*lpnSize > 0)
539
0
  {
540
0
    lpABuffer = calloc(*lpnSize, sizeof(CHAR));
541
542
0
    if (!lpABuffer)
543
0
      return FALSE;
544
0
  }
545
546
0
  rc = GetComputerNameExA(NameType, lpABuffer, lpnSize);
547
548
0
  if (rc && (*lpnSize > 0))
549
0
  {
550
0
    const SSIZE_T res = ConvertUtf8NToWChar(lpABuffer, *lpnSize, lpBuffer, *lpnSize);
551
0
    rc = res > 0;
552
0
  }
553
554
0
  free(lpABuffer);
555
0
  return rc;
556
0
}
557
558
#endif
559
560
#if defined(_UWP)
561
562
DWORD GetTickCount(void)
563
{
564
  return (DWORD)GetTickCount64();
565
}
566
567
#endif
568
569
#if (!defined(_WIN32)) || (defined(_WIN32) && (_WIN32_WINNT < 0x0600))
570
571
ULONGLONG winpr_GetTickCount64(void)
572
8.37M
{
573
8.37M
  const UINT64 ns = winpr_GetTickCount64NS();
574
8.37M
  return WINPR_TIME_NS_TO_MS(ns);
575
8.37M
}
576
577
#endif
578
579
UINT64 winpr_GetTickCount64NS(void)
580
8.37M
{
581
8.37M
  UINT64 ticks = 0;
582
8.37M
#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 199309L)
583
8.37M
  struct timespec ts = { 0 };
584
585
8.37M
  if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == 0)
586
8.37M
    ticks = (ts.tv_sec * 1000000000ull) + ts.tv_nsec;
587
#elif defined(__MACH__) && defined(__APPLE__)
588
  ticks = mac_get_time_ns();
589
#elif defined(_WIN32)
590
  LARGE_INTEGER li = { 0 };
591
  LARGE_INTEGER freq = { 0 };
592
  if (QueryPerformanceFrequency(&freq) && QueryPerformanceCounter(&li))
593
    ticks = li.QuadPart * 1000000000ull / freq.QuadPart;
594
#else
595
  struct timeval tv = { 0 };
596
597
  if (gettimeofday(&tv, NULL) == 0)
598
    ticks = (tv.tv_sec * 1000000000ull) + (tv.tv_usec * 1000ull);
599
600
  /* We need to trick here:
601
   * this function should return the system uptime, but we need higher resolution.
602
   * so on first call get the actual timestamp along with the system uptime.
603
   *
604
   * return the uptime measured from now on (e.g. current measure - first measure + uptime at
605
   * first measure)
606
   */
607
  static UINT64 first = 0;
608
  static UINT64 uptime = 0;
609
  if (first == 0)
610
  {
611
    struct sysinfo info = { 0 };
612
    if (sysinfo(&info) == 0)
613
    {
614
      first = ticks;
615
      uptime = 1000000000ull * info.uptime;
616
    }
617
  }
618
619
  ticks = ticks - first + uptime;
620
#endif
621
8.37M
  return ticks;
622
8.37M
}
623
624
UINT64 winpr_GetUnixTimeNS(void)
625
0
{
626
#if defined(_WIN32)
627
628
  union
629
  {
630
    UINT64 u64;
631
    FILETIME ft;
632
  } t = { 0 };
633
  GetSystemTimeAsFileTime(&t.ft);
634
  return (t.u64 - FILETIME_TO_UNIX_OFFSET_S * 10000000ull) * 100ull;
635
#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 199309L)
636
  struct timespec ts = { 0 };
637
0
  if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
638
0
    return 0;
639
0
  return ts.tv_sec * 1000000000ull + ts.tv_nsec;
640
#else
641
  struct timeval tv = { 0 };
642
  if (gettimeofday(&tv, NULL) != 0)
643
    return 0;
644
  return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000ull;
645
#endif
646
0
}
647
648
/* If x86 */
649
#ifdef _M_IX86_AMD64
650
651
#if defined(__GNUC__)
652
#define xgetbv(_func_, _lo_, _hi_) \
653
0
  __asm__ __volatile__("xgetbv" : "=a"(_lo_), "=d"(_hi_) : "c"(_func_))
654
#elif defined(_MSC_VER)
655
#define xgetbv(_func_, _lo_, _hi_)              \
656
  {                                           \
657
    unsigned __int64 val = _xgetbv(_func_); \
658
    _lo_ = val & 0xFFFFFFFF;                \
659
    _hi_ = (val >> 32);                     \
660
  }
661
#endif
662
663
0
#define B_BIT_AVX2 (1 << 5)
664
0
#define B_BIT_AVX512F (1 << 16)
665
0
#define D_BIT_MMX (1 << 23)
666
0
#define D_BIT_SSE (1 << 25)
667
0
#define D_BIT_SSE2 (1 << 26)
668
0
#define D_BIT_3DN (1 << 30)
669
0
#define C_BIT_SSE3 (1 << 0)
670
0
#define C_BIT_PCLMULQDQ (1 << 1)
671
0
#define C81_BIT_LZCNT (1 << 5)
672
#define C_BIT_3DNP (1 << 8)
673
0
#define C_BIT_3DNP (1 << 8)
674
0
#define C_BIT_SSSE3 (1 << 9)
675
0
#define C_BIT_SSE41 (1 << 19)
676
0
#define C_BIT_SSE42 (1 << 20)
677
0
#define C_BIT_FMA (1 << 12)
678
0
#define C_BIT_AES (1 << 25)
679
0
#define C_BIT_XGETBV (1 << 27)
680
0
#define C_BIT_AVX (1 << 28)
681
0
#define E_BIT_XMM (1 << 1)
682
0
#define E_BIT_YMM (1 << 2)
683
0
#define E_BITS_AVX (E_BIT_XMM | E_BIT_YMM)
684
685
static void cpuid(unsigned info, unsigned* eax, unsigned* ebx, unsigned* ecx, unsigned* edx)
686
0
{
687
0
#ifdef __GNUC__
688
0
  *eax = *ebx = *ecx = *edx = 0;
689
0
  __asm volatile(
690
  /* The EBX (or RBX register on x86_64) is used for the PIC base address
691
   * and must not be corrupted by our inline assembly.
692
   */
693
#ifdef _M_IX86
694
      "mov %%ebx, %%esi;"
695
      "cpuid;"
696
      "xchg %%ebx, %%esi;"
697
#else
698
0
      "mov %%rbx, %%rsi;"
699
0
      "cpuid;"
700
0
      "xchg %%rbx, %%rsi;"
701
0
#endif
702
0
      : "=a"(*eax), "=S"(*ebx), "=c"(*ecx), "=d"(*edx)
703
0
      : "a"(info), "c"(0));
704
#elif defined(_MSC_VER)
705
  int a[4];
706
  __cpuid(a, info);
707
  *eax = a[0];
708
  *ebx = a[1];
709
  *ecx = a[2];
710
  *edx = a[3];
711
#endif
712
0
}
713
#elif defined(_M_ARM)
714
#if defined(__linux__)
715
// HWCAP flags from linux kernel - uapi/asm/hwcap.h
716
#define HWCAP_SWP (1 << 0)
717
#define HWCAP_HALF (1 << 1)
718
#define HWCAP_THUMB (1 << 2)
719
#define HWCAP_26BIT (1 << 3) /* Play it safe */
720
#define HWCAP_FAST_MULT (1 << 4)
721
#define HWCAP_FPA (1 << 5)
722
#define HWCAP_VFP (1 << 6)
723
#define HWCAP_EDSP (1 << 7)
724
#define HWCAP_JAVA (1 << 8)
725
#define HWCAP_IWMMXT (1 << 9)
726
#define HWCAP_CRUNCH (1 << 10)
727
#define HWCAP_THUMBEE (1 << 11)
728
#define HWCAP_NEON (1 << 12)
729
#define HWCAP_VFPv3 (1 << 13)
730
#define HWCAP_VFPv3D16 (1 << 14) /* also set for VFPv4-D16 */
731
#define HWCAP_TLS (1 << 15)
732
#define HWCAP_VFPv4 (1 << 16)
733
#define HWCAP_IDIVA (1 << 17)
734
#define HWCAP_IDIVT (1 << 18)
735
#define HWCAP_VFPD32 (1 << 19) /* set if VFP has 32 regs (not 16) */
736
#define HWCAP_IDIV (HWCAP_IDIVA | HWCAP_IDIVT)
737
738
// From linux kernel uapi/linux/auxvec.h
739
#define AT_HWCAP 16
740
741
static unsigned GetARMCPUCaps(void)
742
{
743
  unsigned caps = 0;
744
  int fd = open("/proc/self/auxv", O_RDONLY);
745
746
  if (fd == -1)
747
    return 0;
748
749
  static struct
750
  {
751
    unsigned a_type; /* Entry type */
752
    unsigned a_val;  /* Integer value */
753
  } auxvec;
754
755
  while (1)
756
  {
757
    int num;
758
    num = read(fd, (char*)&auxvec, sizeof(auxvec));
759
760
    if (num < 1 || (auxvec.a_type == 0 && auxvec.a_val == 0))
761
      break;
762
763
    if (auxvec.a_type == AT_HWCAP)
764
    {
765
      caps = auxvec.a_val;
766
    }
767
  }
768
769
  close(fd);
770
  return caps;
771
}
772
773
#endif // defined(__linux__)
774
#endif // _M_IX86_AMD64
775
776
#ifndef _WIN32
777
778
BOOL IsProcessorFeaturePresent(DWORD ProcessorFeature)
779
0
{
780
0
  BOOL ret = FALSE;
781
#if defined(ANDROID)
782
  const uint64_t features = android_getCpuFeatures();
783
784
  switch (ProcessorFeature)
785
  {
786
    case PF_ARM_NEON_INSTRUCTIONS_AVAILABLE:
787
    case PF_ARM_NEON:
788
      return features & ANDROID_CPU_ARM_FEATURE_NEON;
789
790
    default:
791
      return FALSE;
792
  }
793
794
#elif defined(_M_ARM)
795
#ifdef __linux__
796
  const unsigned caps = GetARMCPUCaps();
797
798
  switch (ProcessorFeature)
799
  {
800
    case PF_ARM_NEON_INSTRUCTIONS_AVAILABLE:
801
    case PF_ARM_NEON:
802
      if (caps & HWCAP_NEON)
803
        ret = TRUE;
804
805
      break;
806
807
    case PF_ARM_THUMB:
808
      if (caps & HWCAP_THUMB)
809
        ret = TRUE;
810
811
    case PF_ARM_VFP_32_REGISTERS_AVAILABLE:
812
      if (caps & HWCAP_VFPD32)
813
        ret = TRUE;
814
815
    case PF_ARM_DIVIDE_INSTRUCTION_AVAILABLE:
816
      if ((caps & HWCAP_IDIVA) || (caps & HWCAP_IDIVT))
817
        ret = TRUE;
818
819
    case PF_ARM_VFP3:
820
      if (caps & HWCAP_VFPv3)
821
        ret = TRUE;
822
823
      break;
824
825
    case PF_ARM_JAZELLE:
826
      if (caps & HWCAP_JAVA)
827
        ret = TRUE;
828
829
      break;
830
831
    case PF_ARM_DSP:
832
      if (caps & HWCAP_EDSP)
833
        ret = TRUE;
834
835
      break;
836
837
    case PF_ARM_MPU:
838
      if (caps & HWCAP_EDSP)
839
        ret = TRUE;
840
841
      break;
842
843
    case PF_ARM_THUMB2:
844
      if ((caps & HWCAP_IDIVT) || (caps & HWCAP_VFPv4))
845
        ret = TRUE;
846
847
      break;
848
849
    case PF_ARM_T2EE:
850
      if (caps & HWCAP_THUMBEE)
851
        ret = TRUE;
852
853
      break;
854
855
    case PF_ARM_INTEL_WMMX:
856
      if (caps & HWCAP_IWMMXT)
857
        ret = TRUE;
858
859
      break;
860
861
    default:
862
      break;
863
  }
864
865
#else // __linux__
866
867
  switch (ProcessorFeature)
868
  {
869
    case PF_ARM_NEON_INSTRUCTIONS_AVAILABLE:
870
    case PF_ARM_NEON:
871
#ifdef __ARM_NEON
872
      ret = TRUE;
873
#endif
874
      break;
875
    default:
876
      break;
877
  }
878
879
#endif // __linux__
880
#elif defined(_M_IX86_AMD64)
881
#ifdef __GNUC__
882
0
  unsigned a = 0;
883
0
  unsigned b = 0;
884
0
  unsigned c = 0;
885
0
  unsigned d = 0;
886
0
  cpuid(1, &a, &b, &c, &d);
887
888
0
  switch (ProcessorFeature)
889
0
  {
890
0
    case PF_MMX_INSTRUCTIONS_AVAILABLE:
891
0
      if (d & D_BIT_MMX)
892
0
        ret = TRUE;
893
894
0
      break;
895
896
0
    case PF_XMMI_INSTRUCTIONS_AVAILABLE:
897
0
      if (d & D_BIT_SSE)
898
0
        ret = TRUE;
899
900
0
      break;
901
902
0
    case PF_XMMI64_INSTRUCTIONS_AVAILABLE:
903
0
      if (d & D_BIT_SSE2)
904
0
        ret = TRUE;
905
906
0
      break;
907
908
0
    case PF_3DNOW_INSTRUCTIONS_AVAILABLE:
909
0
      if (d & D_BIT_3DN)
910
0
        ret = TRUE;
911
912
0
      break;
913
914
0
    case PF_SSE3_INSTRUCTIONS_AVAILABLE:
915
0
      if (c & C_BIT_SSE3)
916
0
        ret = TRUE;
917
918
0
      break;
919
920
0
    default:
921
0
      break;
922
0
  }
923
924
0
#endif // __GNUC__
925
#elif defined(_M_E2K)
926
  /* compiler flags on e2k arch determine CPU features */
927
  switch (ProcessorFeature)
928
  {
929
    case PF_MMX_INSTRUCTIONS_AVAILABLE:
930
#ifdef __MMX__
931
      ret = TRUE;
932
#endif
933
      break;
934
935
    case PF_3DNOW_INSTRUCTIONS_AVAILABLE:
936
#ifdef __3dNOW__
937
      ret = TRUE;
938
#endif
939
      break;
940
941
    case PF_SSE3_INSTRUCTIONS_AVAILABLE:
942
#ifdef __SSE3__
943
      ret = TRUE;
944
#endif
945
      break;
946
947
    default:
948
      break;
949
  }
950
951
#endif
952
0
  return ret;
953
0
}
954
955
#endif //_WIN32
956
957
DWORD GetTickCountPrecise(void)
958
0
{
959
#ifdef _WIN32
960
  LARGE_INTEGER freq;
961
  LARGE_INTEGER current;
962
  QueryPerformanceFrequency(&freq);
963
  QueryPerformanceCounter(&current);
964
  return (DWORD)(current.QuadPart * 1000LL / freq.QuadPart);
965
#else
966
0
  return GetTickCount();
967
0
#endif
968
0
}
969
970
BOOL IsProcessorFeaturePresentEx(DWORD ProcessorFeature)
971
0
{
972
0
  BOOL ret = FALSE;
973
#ifdef _M_ARM
974
#ifdef __linux__
975
  unsigned caps;
976
  caps = GetARMCPUCaps();
977
978
  switch (ProcessorFeature)
979
  {
980
    case PF_EX_ARM_VFP1:
981
      if (caps & HWCAP_VFP)
982
        ret = TRUE;
983
984
      break;
985
986
    case PF_EX_ARM_VFP3D16:
987
      if (caps & HWCAP_VFPv3D16)
988
        ret = TRUE;
989
990
      break;
991
992
    case PF_EX_ARM_VFP4:
993
      if (caps & HWCAP_VFPv4)
994
        ret = TRUE;
995
996
      break;
997
998
    case PF_EX_ARM_IDIVA:
999
      if (caps & HWCAP_IDIVA)
1000
        ret = TRUE;
1001
1002
      break;
1003
1004
    case PF_EX_ARM_IDIVT:
1005
      if (caps & HWCAP_IDIVT)
1006
        ret = TRUE;
1007
1008
      break;
1009
  }
1010
1011
#endif // __linux__
1012
#elif defined(_M_IX86_AMD64)
1013
  unsigned a = 0;
1014
0
  unsigned b = 0;
1015
0
  unsigned c = 0;
1016
0
  unsigned d = 0;
1017
0
  cpuid(1, &a, &b, &c, &d);
1018
1019
0
  switch (ProcessorFeature)
1020
0
  {
1021
0
    case PF_EX_LZCNT:
1022
0
    {
1023
0
      unsigned a81 = 0;
1024
0
      unsigned b81 = 0;
1025
0
      unsigned c81 = 0;
1026
0
      unsigned d81 = 0;
1027
0
      cpuid(0x80000001, &a81, &b81, &c81, &d81);
1028
1029
0
      if (c81 & C81_BIT_LZCNT)
1030
0
        ret = TRUE;
1031
0
    }
1032
0
    break;
1033
1034
0
    case PF_EX_3DNOW_PREFETCH:
1035
0
      if (c & C_BIT_3DNP)
1036
0
        ret = TRUE;
1037
1038
0
      break;
1039
1040
0
    case PF_EX_SSSE3:
1041
0
      if (c & C_BIT_SSSE3)
1042
0
        ret = TRUE;
1043
1044
0
      break;
1045
1046
0
    case PF_EX_SSE41:
1047
0
      if (c & C_BIT_SSE41)
1048
0
        ret = TRUE;
1049
1050
0
      break;
1051
1052
0
    case PF_EX_SSE42:
1053
0
      if (c & C_BIT_SSE42)
1054
0
        ret = TRUE;
1055
1056
0
      break;
1057
0
#if defined(__GNUC__) || defined(_MSC_VER)
1058
1059
0
    case PF_EX_AVX:
1060
0
    case PF_EX_AVX2:
1061
0
    case PF_EX_AVX512F:
1062
0
    case PF_EX_FMA:
1063
0
    case PF_EX_AVX_AES:
1064
0
    case PF_EX_AVX_PCLMULQDQ:
1065
0
    {
1066
      /* Check for general AVX support */
1067
0
      if (!(c & C_BIT_AVX))
1068
0
        break;
1069
1070
      /* Check for xgetbv support */
1071
0
      if (!(c & C_BIT_XGETBV))
1072
0
        break;
1073
1074
0
      int e = 0;
1075
0
      int f = 0;
1076
0
      xgetbv(0, e, f);
1077
1078
      /* XGETBV enabled for applications and XMM/YMM states enabled */
1079
0
      if ((e & E_BITS_AVX) == E_BITS_AVX)
1080
0
      {
1081
0
        switch (ProcessorFeature)
1082
0
        {
1083
0
          case PF_EX_AVX:
1084
0
            ret = TRUE;
1085
0
            break;
1086
1087
0
          case PF_EX_AVX2:
1088
0
          case PF_EX_AVX512F:
1089
0
            cpuid(7, &a, &b, &c, &d);
1090
0
            switch (ProcessorFeature)
1091
0
            {
1092
0
              case PF_EX_AVX2:
1093
0
                if (b & B_BIT_AVX2)
1094
0
                  ret = TRUE;
1095
0
                break;
1096
1097
0
              case PF_EX_AVX512F:
1098
0
                if (b & B_BIT_AVX512F)
1099
0
                  ret = TRUE;
1100
0
                break;
1101
1102
0
              default:
1103
0
                break;
1104
0
            }
1105
0
            break;
1106
1107
0
          case PF_EX_FMA:
1108
0
            if (c & C_BIT_FMA)
1109
0
              ret = TRUE;
1110
1111
0
            break;
1112
1113
0
          case PF_EX_AVX_AES:
1114
0
            if (c & C_BIT_AES)
1115
0
              ret = TRUE;
1116
1117
0
            break;
1118
1119
0
          case PF_EX_AVX_PCLMULQDQ:
1120
0
            if (c & C_BIT_PCLMULQDQ)
1121
0
              ret = TRUE;
1122
1123
0
            break;
1124
0
        }
1125
0
      }
1126
0
    }
1127
0
    break;
1128
0
#endif // __GNUC__ || _MSC_VER
1129
1130
0
    default:
1131
0
      break;
1132
0
  }
1133
#elif defined(_M_E2K)
1134
  /* compiler flags on e2k arch determine CPU features */
1135
  switch (ProcessorFeature)
1136
  {
1137
    case PF_EX_LZCNT:
1138
#ifdef __LZCNT__
1139
      ret = TRUE;
1140
#endif
1141
      break;
1142
1143
    case PF_EX_SSSE3:
1144
#ifdef __SSSE3__
1145
      ret = TRUE;
1146
#endif
1147
      break;
1148
1149
    case PF_EX_SSE41:
1150
#ifdef __SSE4_1__
1151
      ret = TRUE;
1152
#endif
1153
      break;
1154
1155
    case PF_EX_SSE42:
1156
#ifdef __SSE4_2__
1157
      ret = TRUE;
1158
#endif
1159
      break;
1160
1161
    case PF_EX_AVX:
1162
#ifdef __AVX__
1163
      ret = TRUE;
1164
#endif
1165
      break;
1166
1167
    case PF_EX_AVX2:
1168
#ifdef __AVX2__
1169
      ret = TRUE;
1170
#endif
1171
      break;
1172
1173
    case PF_EX_FMA:
1174
#ifdef __FMA__
1175
      ret = TRUE;
1176
#endif
1177
      break;
1178
1179
    default:
1180
      break;
1181
  }
1182
#endif
1183
0
  return ret;
1184
0
}