9 #include <botan/internal/os_utils.h>
10 #include <botan/cpuid.h>
11 #include <botan/exceptn.h>
12 #include <botan/mem_ops.h>
15 #if defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
16 #include <sys/types.h>
18 #include <sys/resource.h>
24 #if defined(BOTAN_TARGET_OS_IS_WINDOWS) || defined(BOTAN_TARGET_OS_IS_MINGW)
33 #if defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
35 #elif defined(BOTAN_TARGET_OS_IS_WINDOWS) || defined(BOTAN_TARGET_OS_IS_MINGW)
36 return ::GetCurrentProcessId();
37 #elif defined(BOTAN_TARGET_OS_TYPE_IS_UNIKERNEL)
40 #error "Missing get_process_id"
46 #if defined(BOTAN_TARGET_OS_HAS_QUERY_PERF_COUNTER)
48 ::QueryPerformanceCounter(&tv);
51 #elif defined(BOTAN_USE_GCC_INLINE_ASM)
53 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
54 if(CPUID::has_rdtsc())
56 uint32_t rtc_low = 0, rtc_high = 0;
57 asm volatile(
"rdtsc" :
"=d" (rtc_high),
"=a" (rtc_low));
58 return (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
61 #elif defined(BOTAN_TARGET_ARCH_IS_PPC64)
62 uint32_t rtc_low = 0, rtc_high = 0;
63 asm volatile(
"mftbu %0; mftb %1" :
"=r" (rtc_high),
"=r" (rtc_low));
69 if(rtc_high > 0 || rtc_low > 0)
71 return (static_cast<uint64_t>(rtc_high) << 32) | rtc_low;
74 #elif defined(BOTAN_TARGET_ARCH_IS_ALPHA)
76 asm volatile(
"rpcc %0" :
"=r" (rtc));
80 #elif defined(BOTAN_TARGET_ARCH_IS_SPARC64) && !defined(BOTAN_TARGET_OS_IS_OPENBSD)
82 asm volatile(
"rd %%tick, %0" :
"=r" (rtc));
85 #elif defined(BOTAN_TARGET_ARCH_IS_IA64)
87 asm volatile(
"mov %0=ar.itc" :
"=r" (rtc));
90 #elif defined(BOTAN_TARGET_ARCH_IS_S390X)
92 asm volatile(
"stck 0(%0)" : :
"a" (&rtc) :
"memory",
"cc");
95 #elif defined(BOTAN_TARGET_ARCH_IS_HPPA)
97 asm volatile(
"mfctl 16,%0" :
"=r" (rtc));
121 #if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
124 const clockid_t clock_types[] = {
125 #if defined(CLOCK_MONOTONIC_HR)
128 #if defined(CLOCK_MONOTONIC_RAW)
131 #if defined(CLOCK_MONOTONIC)
134 #if defined(CLOCK_PROCESS_CPUTIME_ID)
135 CLOCK_PROCESS_CPUTIME_ID,
137 #if defined(CLOCK_THREAD_CPUTIME_ID)
138 CLOCK_THREAD_CPUTIME_ID,
142 for(clockid_t clock : clock_types)
145 if(::clock_gettime(clock, &ts) == 0)
147 return (static_cast<uint64_t>(ts.tv_sec) * 1000000000) +
static_cast<uint64_t
>(ts.tv_nsec);
153 auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
154 return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
159 #if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
161 if(::clock_gettime(CLOCK_REALTIME, &ts) == 0)
163 return (static_cast<uint64_t>(ts.tv_sec) * 1000000000) +
static_cast<uint64_t
>(ts.tv_nsec);
167 auto now = std::chrono::system_clock::now().time_since_epoch();
168 return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
173 #if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
183 size_t mlock_requested = BOTAN_MLOCK_ALLOCATOR_MAX_LOCKED_KB;
188 if(
const char* env = ::getenv(
"BOTAN_MLOCK_POOL_SIZE"))
192 const size_t user_req = std::stoul(env,
nullptr);
193 mlock_requested =
std::min(user_req, mlock_requested);
195 catch(std::exception&) { }
198 #if defined(RLIMIT_MEMLOCK)
199 if(mlock_requested > 0)
201 struct ::rlimit limits;
203 ::getrlimit(RLIMIT_MEMLOCK, &limits);
205 if(limits.rlim_cur < limits.rlim_max)
207 limits.rlim_cur = limits.rlim_max;
208 ::setrlimit(RLIMIT_MEMLOCK, &limits);
209 ::getrlimit(RLIMIT_MEMLOCK, &limits);
212 return std::min<size_t>(limits.rlim_cur, mlock_requested * 1024);
222 #elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK) && defined(BOTAN_BUILD_COMPILER_IS_MSVC)
223 SIZE_T working_min = 0, working_max = 0;
224 DWORD working_flags = 0;
225 if(!::GetProcessWorkingSetSizeEx(::GetCurrentProcess(), &working_min, &working_max, &working_flags))
230 SYSTEM_INFO sSysInfo;
231 ::GetSystemInfo(&sSysInfo);
239 size_t overhead = sSysInfo.dwPageSize * 11ULL;
240 if(working_min > overhead)
242 size_t lockable_bytes = working_min - overhead;
243 if(lockable_bytes < (BOTAN_MLOCK_ALLOCATOR_MAX_LOCKED_KB * 1024ULL))
245 return lockable_bytes;
249 return BOTAN_MLOCK_ALLOCATOR_MAX_LOCKED_KB * 1024ULL;
259 #if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
261 #if !defined(MAP_NOCORE)
265 #if !defined(MAP_ANONYMOUS)
266 #define MAP_ANONYMOUS MAP_ANON
269 void* ptr = ::mmap(
nullptr,
271 PROT_READ | PROT_WRITE,
272 MAP_ANONYMOUS | MAP_SHARED | MAP_NOCORE,
276 if(ptr == MAP_FAILED)
281 #if defined(MADV_DONTDUMP)
282 ::madvise(ptr, length, MADV_DONTDUMP);
285 if(::mlock(ptr, length) != 0)
287 ::munmap(ptr, length);
291 ::memset(ptr, 0, length);
294 #elif defined BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK
295 LPVOID ptr = ::VirtualAlloc(
nullptr, length, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
301 if(::VirtualLock(ptr, length) == 0)
303 ::VirtualFree(ptr, 0, MEM_RELEASE);
316 if(ptr ==
nullptr || length == 0)
319 #if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
321 ::munlock(ptr, length);
322 ::munmap(ptr, length);
323 #elif defined BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK
325 ::VirtualUnlock(ptr, length);
326 ::VirtualFree(ptr, 0, MEM_RELEASE);
333 #if defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
336 static ::sigjmp_buf g_sigill_jmp_buf;
338 void botan_sigill_handler(
int)
340 ::siglongjmp(g_sigill_jmp_buf, 1);
348 volatile int probe_result = -3;
350 #if defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
351 struct sigaction old_sigaction;
352 struct sigaction sigaction;
354 sigaction.sa_handler = botan_sigill_handler;
355 sigemptyset(&sigaction.sa_mask);
356 sigaction.sa_flags = 0;
358 int rc = ::sigaction(SIGILL, &sigaction, &old_sigaction);
361 throw Exception(
"run_cpu_instruction_probe sigaction failed");
363 rc = ::sigsetjmp(g_sigill_jmp_buf, 1);
368 probe_result = probe_fn();
377 rc = ::sigaction(SIGILL, &old_sigaction,
nullptr);
379 throw Exception(
"run_cpu_instruction_probe sigaction restore failed");
381 #elif defined(BOTAN_TARGET_OS_IS_WINDOWS) && defined(BOTAN_TARGET_COMPILER_IS_MSVC)
386 probe_result = probe_fn();
388 __except(::GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION ?
389 EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
void secure_scrub_memory(void *ptr, size_t n)
int BOTAN_DLL run_cpu_instruction_probe(std::function< int()> probe_fn)
uint32_t BOTAN_DLL get_process_id()
void * allocate_locked_pages(size_t length)
size_t get_memory_locking_limit()
uint64_t BOTAN_DLL get_processor_timestamp()
uint64_t BOTAN_DLL get_system_timestamp_ns()
void free_locked_pages(void *ptr, size_t length)
uint64_t BOTAN_DLL get_high_resolution_clock()