vfio: set errno on unsupported OS
[dpdk.git] / lib / eal / windows / include / rte_os_shim.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2
3 #ifndef _RTE_OS_SHIM_
4 #define _RTE_OS_SHIM_
5
6 #include <time.h>
7
8 #include <rte_os.h>
9 #include <rte_windows.h>
10
11 /**
12  * @file
13  * @internal
14  * Provides semi-standard OS facilities by convenient names.
15  */
16
17 #ifndef PATH_MAX
18 #define PATH_MAX _MAX_PATH
19 #endif
20
21 #define strdup(str) _strdup(str)
22 #define strtok_r(str, delim, saveptr) strtok_s(str, delim, saveptr)
23 #ifndef RTE_TOOLCHAIN_GCC
24 #define strcasecmp(s1, s2) _stricmp(s1, s2)
25 #define strncasecmp(s1, s2, count) _strnicmp(s1, s2, count)
26 #endif
27
28 #define open(...) _open(__VA_ARGS__)
29 #define read(fd, buf, n) _read(fd, buf, n)
30 #define write(fd, buf, n) _write(fd, buf, n)
31 #define close(fd) _close(fd)
32 #define unlink(path) _unlink(path)
33
34 #define IPVERSION       4
35
36 #define IPPROTO_IPIP    4
37 #define IPPROTO_GRE     47
38 #ifdef RTE_TOOLCHAIN_GCC
39 #define IPPROTO_SCTP    132
40 #endif
41
42 #ifndef IPDEFTTL
43 #define IPDEFTTL 64
44 #endif
45
46 #ifndef S_ISREG
47 #define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
48 #endif
49
50 #ifdef RTE_TOOLCHAIN_GCC
51
52 #define TIME_UTC 1
53
54 static inline int
55 rte_timespec_get(struct timespec *now, int base)
56 {
57         /* 100ns ticks from 1601-01-01 to 1970-01-01 */
58         static const uint64_t EPOCH = 116444736000000000ULL;
59         static const uint64_t TICKS_PER_SEC = 10000000;
60         static const uint64_t NS_PER_TICK = 100;
61
62         FILETIME ft;
63         uint64_t ticks;
64
65         if (base != TIME_UTC)
66                 return 0;
67
68         GetSystemTimePreciseAsFileTime(&ft);
69         ticks = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
70         ticks -= EPOCH;
71         now->tv_sec = ticks / TICKS_PER_SEC;
72         now->tv_nsec = (ticks - now->tv_sec * TICKS_PER_SEC) * NS_PER_TICK;
73         return base;
74 }
75
76 #define timespec_get(ts, base) rte_timespec_get(ts, base)
77
78 #endif /* RTE_TOOLCHAIN_GCC */
79
80 /* Identifier for system-wide realtime clock. */
81 #define CLOCK_REALTIME                  0
82 /* Monotonic system-wide clock. */
83 #define CLOCK_MONOTONIC                 1
84
85 typedef int clockid_t;
86
87 static inline int
88 rte_clock_gettime(clockid_t clock_id, struct timespec *tp)
89 {
90         const int NS_PER_SEC = 1E9;
91         LARGE_INTEGER pf, pc;
92         LONGLONG nsec;
93
94         switch (clock_id) {
95         case CLOCK_REALTIME:
96                 if (timespec_get(tp, TIME_UTC) != TIME_UTC)
97                         return -1;
98                 return 0;
99         case CLOCK_MONOTONIC:
100                 QueryPerformanceFrequency(&pf);
101                 QueryPerformanceCounter(&pc);
102
103                 nsec = pc.QuadPart * NS_PER_SEC / pf.QuadPart;
104                 tp->tv_sec = nsec / NS_PER_SEC;
105                 tp->tv_nsec = nsec - tp->tv_sec * NS_PER_SEC;
106                 return 0;
107         default:
108                 return -1;
109         }
110 }
111 #define clock_gettime(clock_id, tp) rte_clock_gettime(clock_id, tp)
112
113 #endif /* _RTE_OS_SHIM_ */