mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-05 02:38:31 +00:00
Make improvements
- Introduce portable sched_getcpu() api - Support GCC's __target_clones__ feature - Make fma() go faster on x86 in default mode - Remove some asan checks from core libraries - WinMain() now ensures $HOME and $USER are defined
This commit is contained in:
parent
d5225a693b
commit
2ab9e9f7fd
192 changed files with 2809 additions and 932 deletions
|
@ -41,6 +41,7 @@ LIBC_CALLS_A_DIRECTDEPS = \
|
|||
LIBC_INTRIN \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_NT_ADVAPI32 \
|
||||
LIBC_NT_BCRYPTPRIMITIVES \
|
||||
LIBC_NT_IPHLPAPI \
|
||||
LIBC_NT_KERNEL32 \
|
||||
LIBC_NT_NTDLL \
|
||||
|
@ -132,7 +133,8 @@ endif
|
|||
o/$(MODE)/libc/calls/pledge-linux.o: private \
|
||||
CFLAGS += \
|
||||
-Os \
|
||||
-fPIC
|
||||
-fPIC \
|
||||
-ffreestanding
|
||||
|
||||
# these assembly files are safe to build on aarch64
|
||||
o/$(MODE)/libc/calls/getcontext.o: libc/calls/getcontext.S
|
||||
|
|
|
@ -247,6 +247,8 @@ ssize_t tinyprint(int, const char *, ...) libcesque nullterminated();
|
|||
void shm_path_np(const char *, char[hasatleast 78]) libcesque;
|
||||
#endif /* _COSMO_SOURCE */
|
||||
|
||||
int system(const char *) libcesque;
|
||||
|
||||
int __wifstopped(int) libcesque pureconst;
|
||||
int __wifcontinued(int) libcesque pureconst;
|
||||
int __wifsignaled(int) libcesque pureconst;
|
||||
|
|
|
@ -21,12 +21,17 @@
|
|||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
#include "libc/nt/accounting.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/nt/thread.h"
|
||||
|
||||
#define _CLOCK_REALTIME 0
|
||||
#define _CLOCK_MONOTONIC 1
|
||||
#define _CLOCK_REALTIME_COARSE 2
|
||||
#define _CLOCK_BOOTTIME 3
|
||||
#define _CLOCK_REALTIME 0
|
||||
#define _CLOCK_MONOTONIC 1
|
||||
#define _CLOCK_REALTIME_COARSE 2
|
||||
#define _CLOCK_BOOTTIME 3
|
||||
#define _CLOCK_PROCESS_CPUTIME_ID 4
|
||||
#define _CLOCK_THREAD_CPUTIME_ID 5
|
||||
|
||||
static struct {
|
||||
uint64_t base;
|
||||
|
@ -35,7 +40,7 @@ static struct {
|
|||
|
||||
textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
|
||||
uint64_t t;
|
||||
struct NtFileTime ft;
|
||||
struct NtFileTime ft, ftExit, ftUser, ftKernel, ftCreation;
|
||||
switch (clock) {
|
||||
case _CLOCK_REALTIME:
|
||||
if (ts) {
|
||||
|
@ -61,6 +66,22 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
|
|||
*ts = timespec_frommillis(GetTickCount64());
|
||||
}
|
||||
return 0;
|
||||
case _CLOCK_PROCESS_CPUTIME_ID:
|
||||
if (ts) {
|
||||
GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel,
|
||||
&ftUser);
|
||||
*ts = WindowsDurationToTimeSpec(ReadFileTime(ftUser) +
|
||||
ReadFileTime(ftKernel));
|
||||
}
|
||||
return 0;
|
||||
case _CLOCK_THREAD_CPUTIME_ID:
|
||||
if (ts) {
|
||||
GetThreadTimes(GetCurrentThread(), &ftCreation, &ftExit, &ftKernel,
|
||||
&ftUser);
|
||||
*ts = WindowsDurationToTimeSpec(ReadFileTime(ftUser) +
|
||||
ReadFileTime(ftKernel));
|
||||
}
|
||||
return 0;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
|
|
@ -61,24 +61,13 @@ static int __clock_gettime_init(int clockid, struct timespec *ts) {
|
|||
/**
|
||||
* Returns nanosecond time.
|
||||
*
|
||||
* @param clock can be one of:
|
||||
* - `CLOCK_REALTIME`: universally supported
|
||||
* - `CLOCK_REALTIME_FAST`: ditto but faster on freebsd
|
||||
* - `CLOCK_REALTIME_PRECISE`: ditto but better on freebsd
|
||||
* - `CLOCK_REALTIME_COARSE`: : like `CLOCK_REALTIME_FAST` w/ Linux 2.6.32+
|
||||
* - `CLOCK_MONOTONIC`: universally supported (except on XNU/NT w/o INVTSC)
|
||||
* - `CLOCK_MONOTONIC_FAST`: ditto but faster on freebsd
|
||||
* - `CLOCK_MONOTONIC_PRECISE`: ditto but better on freebsd
|
||||
* - `CLOCK_MONOTONIC_COARSE`: : like `CLOCK_MONOTONIC_FAST` w/ Linux 2.6.32+
|
||||
* - `CLOCK_MONOTONIC_RAW`: is actually monotonic but needs Linux 2.6.28+
|
||||
* - `CLOCK_PROCESS_CPUTIME_ID`: linux and bsd (NetBSD permits OR'd PID)
|
||||
* - `CLOCK_THREAD_CPUTIME_ID`: linux and bsd (NetBSD permits OR'd TID)
|
||||
* - `CLOCK_MONOTONIC_COARSE`: linux, freebsd
|
||||
* - `CLOCK_PROF`: linux and netbsd
|
||||
* - `CLOCK_BOOTTIME`: linux and openbsd
|
||||
* - `CLOCK_REALTIME_ALARM`: linux-only
|
||||
* - `CLOCK_BOOTTIME_ALARM`: linux-only
|
||||
* - `CLOCK_TAI`: linux-only
|
||||
* @param clock supports the following values across OSes:
|
||||
* - `CLOCK_REALTIME`
|
||||
* - `CLOCK_MONOTONIC`
|
||||
* - `CLOCK_REALTIME_COARSE`
|
||||
* - `CLOCK_MONOTONIC_COARSE`
|
||||
* - `CLOCK_THREAD_CPUTIME_ID`
|
||||
* - `CLOCK_PROCESS_CPUTIME_ID`
|
||||
* @param ts is where the result is stored (or null to do clock check)
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @raise EFAULT if `ts` points to invalid memory
|
||||
|
|
|
@ -93,7 +93,7 @@ static int close_impl(int fd) {
|
|||
*/
|
||||
int close(int fd) {
|
||||
int rc;
|
||||
if (__isfdkind(fd, kFdZip)) { // XXX IsWindows()?
|
||||
if (__isfdkind(fd, kFdZip)) { // XXX IsWindows()?
|
||||
BLOCK_SIGNALS;
|
||||
__fds_lock();
|
||||
rc = close_impl(fd);
|
||||
|
|
|
@ -103,7 +103,7 @@ static ssize_t GetDevUrandom(char *p, size_t n) {
|
|||
ssize_t __getrandom(void *p, size_t n, unsigned f) {
|
||||
ssize_t rc;
|
||||
if (IsWindows()) {
|
||||
rc = RtlGenRandom(p, n) ? n : __winerr();
|
||||
rc = ProcessPrng(p, n) ? n : __winerr();
|
||||
} else if (have_getrandom) {
|
||||
if (IsXnu() || IsOpenbsd()) {
|
||||
rc = GetRandomBsd(p, n, GetRandomEntropy);
|
||||
|
@ -131,7 +131,7 @@ ssize_t __getrandom(void *p, size_t n, unsigned f) {
|
|||
*
|
||||
* This random number seed generator obtains information from:
|
||||
*
|
||||
* - RtlGenRandom() on Windows
|
||||
* - ProcessPrng() on Windows
|
||||
* - getentropy() on XNU and OpenBSD
|
||||
* - getrandom() on Linux, FreeBSD, and NetBSD
|
||||
* - sysctl(KERN_ARND) on older versions of FreeBSD and NetBSD
|
||||
|
|
|
@ -75,7 +75,9 @@ textstartup void InitializeMetalFile(void) {
|
|||
memcpy(copied_base, (void *)(BANE + IMAGE_BASE_PHYSICAL), size);
|
||||
__ape_com_base = copied_base;
|
||||
__ape_com_size = size;
|
||||
KINFOF("%s @ %p,+%#zx", APE_COM_NAME, copied_base, size);
|
||||
// TODO(tkchia): LIBC_CALLS doesn't depend on LIBC_VGA so references
|
||||
// to its functions need to be weak
|
||||
// KINFOF("%s @ %p,+%#zx", APE_COM_NAME, copied_base, size);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ static dontinline uint64_t rdrand_failover(void) {
|
|||
*
|
||||
* If RDRAND isn't available (we check CPUID and we also disable it
|
||||
* automatically for microarchitectures where it's slow or buggy) then
|
||||
* we try getrandom(), RtlGenRandom(), or sysctl(KERN_ARND). If those
|
||||
* we try getrandom(), ProcessPrng(), or sysctl(KERN_ARND). If those
|
||||
* aren't available then we try /dev/urandom and if that fails, we try
|
||||
* getauxval(AT_RANDOM), and if not we finally use RDTSC and getpid().
|
||||
*
|
||||
|
|
|
@ -157,6 +157,8 @@ static textwindows struct Keystroke *NewKeystroke(void) {
|
|||
struct Keystroke *k = KEYSTROKE_CONTAINER(e);
|
||||
dll_remove(&__keystroke.free, &k->elem);
|
||||
--__keystroke.freekeys;
|
||||
// TODO(jart): What's wrong with GCC 12.3?
|
||||
asm("" : "+r"(k));
|
||||
k->buflen = 0;
|
||||
return k;
|
||||
}
|
||||
|
|
|
@ -19,15 +19,19 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/cpuset.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/nexgen32e/rdtscp.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/nt/struct/processornumber.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/runtime/syslib.internal.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
int sys_getcpu(unsigned *opt_cpu, unsigned *opt_node, void *tcache);
|
||||
|
||||
/**
|
||||
* Returns ID of CPU on which thread is currently scheduled.
|
||||
* @return cpu number on success, or -1 w/ errno
|
||||
*/
|
||||
int sched_getcpu(void) {
|
||||
if (X86_HAVE(RDTSCP)) {
|
||||
|
@ -38,6 +42,19 @@ int sched_getcpu(void) {
|
|||
struct NtProcessorNumber pn;
|
||||
GetCurrentProcessorNumberEx(&pn);
|
||||
return 64 * pn.Group + pn.Number;
|
||||
} else if (IsXnuSilicon()) {
|
||||
if (__syslib->__version >= 9) {
|
||||
size_t cpu;
|
||||
errno_t err = __syslib->__pthread_cpu_number_np(&cpu);
|
||||
if (!err) {
|
||||
return cpu;
|
||||
} else {
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
return enosys();
|
||||
}
|
||||
} else {
|
||||
unsigned cpu = 0;
|
||||
int rc = sys_getcpu(&cpu, 0, 0);
|
||||
|
|
|
@ -441,8 +441,7 @@ textwindows void __sig_generate(int sig, int sic) {
|
|||
// to unblock our sig once the wait operation is completed; when
|
||||
// that's the case we can cancel the thread's i/o to deliver sig
|
||||
if (atomic_load_explicit(&pt->pt_blocker, memory_order_acquire) &&
|
||||
!(atomic_load_explicit(&pt->pt_blkmask, memory_order_relaxed) &
|
||||
(1ull << (sig - 1)))) {
|
||||
!(pt->pt_blkmask & (1ull << (sig - 1)))) {
|
||||
_pthread_ref(pt);
|
||||
mark = pt;
|
||||
break;
|
||||
|
|
|
@ -18,7 +18,7 @@ int sys_fcntl_nt_setfl(int, unsigned);
|
|||
int sys_pause_nt(void);
|
||||
int64_t __fix_enotdir(int64_t, char16_t *);
|
||||
int64_t __fix_enotdir3(int64_t, char16_t *, char16_t *);
|
||||
int64_t __winerr(void) nocallback privileged;
|
||||
int64_t __winerr(void) dontcallback privileged;
|
||||
int64_t ntreturn(uint32_t);
|
||||
void *GetProcAddressModule(const char *, const char *);
|
||||
void WinMainForked(void);
|
||||
|
|
|
@ -130,7 +130,7 @@ typedef struct ucontext ucontext_t;
|
|||
int getcontext(ucontext_t *) dontthrow;
|
||||
int setcontext(const ucontext_t *) dontthrow;
|
||||
int swapcontext(ucontext_t *, const ucontext_t *) dontthrow returnstwice;
|
||||
void makecontext(ucontext_t *, void (*)(), int, ...) dontthrow nocallback;
|
||||
void makecontext(ucontext_t *, void *, int, ...) dontthrow dontcallback;
|
||||
void __sig_restore(const ucontext_t *) wontreturn;
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
|
|
|
@ -82,15 +82,27 @@ static textwindows void GetNtName(char *name, int kind) {
|
|||
}
|
||||
|
||||
static inline textwindows int GetNtMajorVersion(void) {
|
||||
#ifdef __x86_64__
|
||||
return NtGetPeb()->OSMajorVersion;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline textwindows int GetNtMinorVersion(void) {
|
||||
#ifdef __x86_64__
|
||||
return NtGetPeb()->OSMinorVersion;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline textwindows int GetNtBuildNumber(void) {
|
||||
#ifdef __x86_64__
|
||||
return NtGetPeb()->OSBuildNumber;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static textwindows void GetNtVersion(char *p) {
|
||||
|
|
|
@ -4,6 +4,7 @@ COSMOPOLITAN_C_START_
|
|||
|
||||
errno_t cosmo_once(_Atomic(uint32_t) *, void (*)(void));
|
||||
int systemvpe(const char *, char *const[], char *const[]) libcesque;
|
||||
char *GetProgramExecutableName(void);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* COSMOPOLITAN_LIBC_COSMO_H_ */
|
||||
|
|
|
@ -159,7 +159,7 @@ typedef struct {
|
|||
#include "libc/integral/lp64arg.inc"
|
||||
#endif
|
||||
|
||||
#define libcesque dontthrow nocallback
|
||||
#define libcesque dontthrow dontcallback
|
||||
#define memcpyesque libcesque
|
||||
#define strlenesque libcesque nosideeffect paramsnonnull()
|
||||
#define vallocesque \
|
||||
|
@ -364,14 +364,14 @@ typedef struct {
|
|||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef nocallback
|
||||
#ifndef dontcallback
|
||||
#if !defined(__STRICT_ANSI__) && \
|
||||
(__has_attribute(__leaf__) || \
|
||||
(!defined(__llvm__) && \
|
||||
(__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 406))
|
||||
#define nocallback __attribute__((__leaf__))
|
||||
#define dontcallback __attribute__((__leaf__))
|
||||
#else
|
||||
#define nocallback
|
||||
#define dontcallback
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
@ -645,11 +645,23 @@ void abort(void) wontreturn;
|
|||
#define initarray _Section(".init_array")
|
||||
#endif
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wformat" /* todo: patch gcc */
|
||||
#ifndef __llvm__
|
||||
#pragma GCC diagnostic ignored "-Wformat=0" /* todo: patch gcc */
|
||||
#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
|
||||
#pragma GCC diagnostic warning "-Wunknown-pragmas"
|
||||
#else
|
||||
#pragma GCC diagnostic ignored "-Wformat"
|
||||
#pragma GCC diagnostic ignored "-Wconstant-logical-operand" /* what */
|
||||
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
|
||||
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
|
||||
#pragma GCC diagnostic ignored "-Wstring-plus-int" /* features 4 losers */
|
||||
#pragma GCC diagnostic ignored "-Wkeyword-compat" /* c++ upgrade */
|
||||
#pragma GCC diagnostic ignored "-Wuser-defined-literals" /* reserved for me */
|
||||
#endif
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wformat-extra-args" /* todo: patch gcc */
|
||||
#pragma GCC diagnostic ignored "-Wunused-function" /* contradicts dce */
|
||||
#pragma GCC diagnostic ignored "-Wunused-const-variable" /* sooo ridiculous */
|
||||
#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
|
||||
#ifndef __cplusplus
|
||||
#pragma GCC diagnostic ignored "-Wold-style-definition" /* orwellian bullsh */
|
||||
#endif
|
||||
|
@ -745,5 +757,18 @@ void abort(void) wontreturn;
|
|||
#define __funline static inline
|
||||
#endif
|
||||
|
||||
#if defined(__x86_64__) && (defined(__GNUC__) || defined(__llvm__)) && \
|
||||
!defined(__chibicc__) && 0 /* TODO: enable with toolchain upgrade */
|
||||
#define __target_clones(x) __attribute__((__target_clones__(x ",default")))
|
||||
#else
|
||||
#define __target_clones(x)
|
||||
#endif
|
||||
|
||||
#if !defined(TINY) && !defined(__AVX__) && 0
|
||||
#define __vex __target_clones("avx")
|
||||
#else
|
||||
#define __vex
|
||||
#endif
|
||||
|
||||
#define MACHINE_CODE_ANALYSIS_BEGIN_
|
||||
#define MACHINE_CODE_ANALYSIS_END_
|
||||
|
|
|
@ -86,6 +86,11 @@ o/$(MODE)/libc/intrin/memmove.o: private \
|
|||
CFLAGS += \
|
||||
-fpie
|
||||
|
||||
o/$(MODE)/libc/intrin/x86.o: private \
|
||||
CFLAGS += \
|
||||
-ffreestanding \
|
||||
-fno-jump-tables
|
||||
|
||||
# these assembly files are safe to build on aarch64
|
||||
o/$(MODE)/libc/intrin/aarch64/%.o: libc/intrin/aarch64/%.S
|
||||
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
|
||||
void __clear_cache2(const void *base, const void *end) {
|
||||
#ifdef __aarch64__
|
||||
int icache, dcache;
|
||||
const char *p, *pe = end;
|
||||
static unsigned int ctr_el0 = 0;
|
||||
if (!ctr_el0) asm volatile("mrs\t%0,ctr_el0" : "=r"(ctr_el0));
|
||||
icache = 4 << (ctr_el0 & 15);
|
||||
dcache = 4 << ((ctr_el0 >> 16) & 15);
|
||||
for (p = (const char *)((uintptr_t)base & -dcache); p < pe; p += dcache) {
|
||||
asm volatile("dc\tcvau,%0" : : "r"(p) : "memory");
|
||||
}
|
||||
asm volatile("dsb\tish" ::: "memory");
|
||||
for (p = (const char *)((uintptr_t)base & -icache); p < pe; p += icache) {
|
||||
asm volatile("ic\tivau,%0" : : "r"(p) : "memory");
|
||||
}
|
||||
asm volatile("dsb\tish\nisb" ::: "memory");
|
||||
#endif
|
||||
}
|
|
@ -24,7 +24,6 @@
|
|||
#include "libc/fmt/magnumstrs.internal.h"
|
||||
#include "libc/intrin/asmflag.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/serialize.h"
|
||||
#include "libc/intrin/getenv.internal.h"
|
||||
#include "libc/intrin/likely.h"
|
||||
#include "libc/intrin/nomultics.internal.h"
|
||||
|
@ -46,6 +45,7 @@
|
|||
#include "libc/runtime/memtrack.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/serialize.h"
|
||||
#include "libc/stdckdint.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tab.internal.h"
|
||||
|
|
|
@ -67,10 +67,9 @@ static inline const unsigned char *memchr_sse(const unsigned char *s,
|
|||
* @return is pointer to first instance of c or NULL if not found
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void *memchr(const void *s, int c, size_t n) {
|
||||
__vex void *memchr(const void *s, int c, size_t n) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
const void *r;
|
||||
if (IsAsan()) __asan_verify(s, n);
|
||||
r = memchr_sse(s, c, n);
|
||||
return (void *)r;
|
||||
#else
|
||||
|
|
|
@ -86,7 +86,7 @@ typedef long long xmm_a __attribute__((__vector_size__(16), __aligned__(16)));
|
|||
* @return dst
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void *memmove(void *dst, const void *src, size_t n) {
|
||||
__vex void *memmove(void *dst, const void *src, size_t n) {
|
||||
char *d;
|
||||
size_t i;
|
||||
const char *s;
|
||||
|
|
|
@ -67,7 +67,7 @@ static inline const unsigned char *memrchr_sse(const unsigned char *s,
|
|||
* @return is pointer to first instance of c or NULL if not found
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void *memrchr(const void *s, int c, size_t n) {
|
||||
__vex void *memrchr(const void *s, int c, size_t n) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
const void *r;
|
||||
r = memrchr_sse(s, c, n);
|
||||
|
|
|
@ -44,14 +44,16 @@
|
|||
#include "libc/runtime/runtime.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
#define INVERT(x) (BANE + PHYSICAL(x))
|
||||
#define NOPAGE ((uint64_t)-1)
|
||||
#define INVERT(x) (BANE + PHYSICAL((uintptr_t)(x)))
|
||||
#define NOPAGE ((uint64_t) - 1)
|
||||
|
||||
#define ABS64(x) \
|
||||
({ \
|
||||
int64_t vAddr; \
|
||||
__asm__("movabs\t%1,%0" : "=r"(vAddr) : "i"(x)); \
|
||||
vAddr; \
|
||||
#define APE_STACK_VADDR \
|
||||
({ \
|
||||
int64_t vAddr; \
|
||||
__asm__(".weak\tape_stack_vaddr\n\t" \
|
||||
"movabs\t$ape_stack_vaddr,%0" \
|
||||
: "=r"(vAddr)); \
|
||||
vAddr; \
|
||||
})
|
||||
|
||||
struct ReclaimedPage {
|
||||
|
@ -305,7 +307,6 @@ textreal void __map_phdrs(struct mman *mm, uint64_t *pml4t, uint64_t b,
|
|||
extern char ape_phdrs_end[] __attribute__((__weak__));
|
||||
extern char ape_stack_pf[] __attribute__((__weak__));
|
||||
extern char ape_stack_offset[] __attribute__((__weak__));
|
||||
extern char ape_stack_vaddr[] __attribute__((__weak__));
|
||||
extern char ape_stack_filesz[] __attribute__((__weak__));
|
||||
extern char ape_stack_memsz[] __attribute__((__weak__));
|
||||
__setup_mman(mm, pml4t, top);
|
||||
|
@ -318,7 +319,7 @@ textreal void __map_phdrs(struct mman *mm, uint64_t *pml4t, uint64_t b,
|
|||
.p_type = PT_LOAD,
|
||||
.p_flags = (uintptr_t)ape_stack_pf,
|
||||
.p_offset = (uintptr_t)ape_stack_offset,
|
||||
.p_vaddr = ABS64(ape_stack_vaddr),
|
||||
.p_vaddr = APE_STACK_VADDR,
|
||||
.p_filesz = (uintptr_t)ape_stack_filesz,
|
||||
.p_memsz = (uintptr_t)ape_stack_memsz,
|
||||
});
|
||||
|
|
|
@ -27,8 +27,6 @@
|
|||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
|
||||
static volatile size_t mapsize;
|
||||
|
||||
/**
|
||||
* Grows file descriptor array memory if needed.
|
||||
*
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_
|
||||
#define COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_
|
||||
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
||||
#ifdef _COSMO_SOURCE
|
||||
|
||||
/**
|
||||
* Reads scalar from memory, offset by segment.
|
||||
*
|
||||
* @return *(MEM) relative to segment
|
||||
* @see pushpop()
|
||||
*/
|
||||
#define fs(MEM) __peek("fs", MEM)
|
||||
#define gs(MEM) __peek("gs", MEM)
|
||||
|
||||
#define __peek(SEGMENT, ADDRESS) \
|
||||
({ \
|
||||
typeof(*(ADDRESS)) Pk; \
|
||||
asm("mov\t%%" SEGMENT ":%1,%0" : "=r"(Pk) : "m"(*(ADDRESS))); \
|
||||
Pk; \
|
||||
})
|
||||
|
||||
#endif /* _COSMO_SOURCE */
|
||||
#endif /* __GNUC__ && !__STRICT_ANSI__ */
|
||||
#endif /* COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_ */
|
|
@ -33,7 +33,7 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
|||
* @return pointer to nul byte
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
char *stpcpy(char *d, const char *s) {
|
||||
__vex char *stpcpy(char *d, const char *s) {
|
||||
size_t i = 0;
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
for (; (uintptr_t)(s + i) & 15; ++i) {
|
||||
|
|
|
@ -94,7 +94,7 @@ static inline const char *strchr_x64(const char *p, uint64_t c) {
|
|||
* @asyncsignalsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
char *strchr(const char *s, int c) {
|
||||
__vex char *strchr(const char *s, int c) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
const char *r;
|
||||
if (X86_HAVE(SSE)) {
|
||||
|
|
|
@ -92,7 +92,7 @@ static const char *strchrnul_x64(const char *p, uint64_t c) {
|
|||
* @return pointer to first instance of c, or pointer to
|
||||
* NUL terminator if c is not found
|
||||
*/
|
||||
char *strchrnul(const char *s, int c) {
|
||||
__vex char *strchrnul(const char *s, int c) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
const char *r;
|
||||
if (X86_HAVE(SSE)) {
|
||||
|
|
|
@ -32,7 +32,7 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
|||
* @return original dest
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
char *strcpy(char *d, const char *s) {
|
||||
__vex char *strcpy(char *d, const char *s) {
|
||||
size_t i = 0;
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
for (; (uintptr_t)(s + i) & 15; ++i) {
|
||||
|
|
|
@ -37,7 +37,7 @@ size_t strlen(const char *s) {
|
|||
while (!m) m = __builtin_ia32_pmovmskb128(*++p == z);
|
||||
return (const char *)p + __builtin_ctzl(m) - s;
|
||||
#else
|
||||
#define ONES ((word)-1 / 255)
|
||||
#define ONES ((word) - 1 / 255)
|
||||
#define BANE (ONES * (255 / 2 + 1))
|
||||
typedef unsigned long mayalias word;
|
||||
word w;
|
||||
|
@ -56,5 +56,4 @@ size_t strlen(const char *s) {
|
|||
return (const char *)p + (__builtin_ctzl(w) >> 3) - s;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* __aarch64__ */
|
||||
|
|
808
libc/intrin/x86.c
Normal file
808
libc/intrin/x86.c
Normal file
|
@ -0,0 +1,808 @@
|
|||
//===-- cpu_model/x86.c - Support for __cpu_model builtin --------*- C -*-===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file is based on LLVM's lib/Support/Host.cpp.
|
||||
// It implements the operating system Host concept and builtin
|
||||
// __cpu_model for the compiler_rt library for x86.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "third_party/compiler_rt/cpu_model.h"
|
||||
|
||||
enum VendorSignatures {
|
||||
SIG_INTEL = 0x756e6547, // Genu
|
||||
SIG_AMD = 0x68747541, // Auth
|
||||
};
|
||||
|
||||
enum ProcessorVendors {
|
||||
VENDOR_INTEL = 1,
|
||||
VENDOR_AMD,
|
||||
VENDOR_OTHER,
|
||||
VENDOR_MAX
|
||||
};
|
||||
|
||||
enum ProcessorTypes {
|
||||
INTEL_BONNELL = 1,
|
||||
INTEL_CORE2,
|
||||
INTEL_COREI7,
|
||||
AMDFAM10H,
|
||||
AMDFAM15H,
|
||||
INTEL_SILVERMONT,
|
||||
INTEL_KNL,
|
||||
AMD_BTVER1,
|
||||
AMD_BTVER2,
|
||||
AMDFAM17H,
|
||||
INTEL_KNM,
|
||||
INTEL_GOLDMONT,
|
||||
INTEL_GOLDMONT_PLUS,
|
||||
INTEL_TREMONT,
|
||||
AMDFAM19H,
|
||||
ZHAOXIN_FAM7H,
|
||||
INTEL_SIERRAFOREST,
|
||||
INTEL_GRANDRIDGE,
|
||||
INTEL_CLEARWATERFOREST,
|
||||
CPU_TYPE_MAX
|
||||
};
|
||||
|
||||
enum ProcessorSubtypes {
|
||||
INTEL_COREI7_NEHALEM = 1,
|
||||
INTEL_COREI7_WESTMERE,
|
||||
INTEL_COREI7_SANDYBRIDGE,
|
||||
AMDFAM10H_BARCELONA,
|
||||
AMDFAM10H_SHANGHAI,
|
||||
AMDFAM10H_ISTANBUL,
|
||||
AMDFAM15H_BDVER1,
|
||||
AMDFAM15H_BDVER2,
|
||||
AMDFAM15H_BDVER3,
|
||||
AMDFAM15H_BDVER4,
|
||||
AMDFAM17H_ZNVER1,
|
||||
INTEL_COREI7_IVYBRIDGE,
|
||||
INTEL_COREI7_HASWELL,
|
||||
INTEL_COREI7_BROADWELL,
|
||||
INTEL_COREI7_SKYLAKE,
|
||||
INTEL_COREI7_SKYLAKE_AVX512,
|
||||
INTEL_COREI7_CANNONLAKE,
|
||||
INTEL_COREI7_ICELAKE_CLIENT,
|
||||
INTEL_COREI7_ICELAKE_SERVER,
|
||||
AMDFAM17H_ZNVER2,
|
||||
INTEL_COREI7_CASCADELAKE,
|
||||
INTEL_COREI7_TIGERLAKE,
|
||||
INTEL_COREI7_COOPERLAKE,
|
||||
INTEL_COREI7_SAPPHIRERAPIDS,
|
||||
INTEL_COREI7_ALDERLAKE,
|
||||
AMDFAM19H_ZNVER3,
|
||||
INTEL_COREI7_ROCKETLAKE,
|
||||
ZHAOXIN_FAM7H_LUJIAZUI,
|
||||
AMDFAM19H_ZNVER4,
|
||||
INTEL_COREI7_GRANITERAPIDS,
|
||||
INTEL_COREI7_GRANITERAPIDS_D,
|
||||
INTEL_COREI7_ARROWLAKE,
|
||||
INTEL_COREI7_ARROWLAKE_S,
|
||||
INTEL_COREI7_PANTHERLAKE,
|
||||
CPU_SUBTYPE_MAX
|
||||
};
|
||||
|
||||
enum ProcessorFeatures {
|
||||
FEATURE_CMOV = 0,
|
||||
FEATURE_MMX,
|
||||
FEATURE_POPCNT,
|
||||
FEATURE_SSE,
|
||||
FEATURE_SSE2,
|
||||
FEATURE_SSE3,
|
||||
FEATURE_SSSE3,
|
||||
FEATURE_SSE4_1,
|
||||
FEATURE_SSE4_2,
|
||||
FEATURE_AVX,
|
||||
FEATURE_AVX2,
|
||||
FEATURE_SSE4_A,
|
||||
FEATURE_FMA4,
|
||||
FEATURE_XOP,
|
||||
FEATURE_FMA,
|
||||
FEATURE_AVX512F,
|
||||
FEATURE_BMI,
|
||||
FEATURE_BMI2,
|
||||
FEATURE_AES,
|
||||
FEATURE_PCLMUL,
|
||||
FEATURE_AVX512VL,
|
||||
FEATURE_AVX512BW,
|
||||
FEATURE_AVX512DQ,
|
||||
FEATURE_AVX512CD,
|
||||
FEATURE_AVX512ER,
|
||||
FEATURE_AVX512PF,
|
||||
FEATURE_AVX512VBMI,
|
||||
FEATURE_AVX512IFMA,
|
||||
FEATURE_AVX5124VNNIW,
|
||||
FEATURE_AVX5124FMAPS,
|
||||
FEATURE_AVX512VPOPCNTDQ,
|
||||
FEATURE_AVX512VBMI2,
|
||||
FEATURE_GFNI,
|
||||
FEATURE_VPCLMULQDQ,
|
||||
FEATURE_AVX512VNNI,
|
||||
FEATURE_AVX512BITALG,
|
||||
FEATURE_AVX512BF16,
|
||||
FEATURE_AVX512VP2INTERSECT,
|
||||
|
||||
FEATURE_CMPXCHG16B = 46,
|
||||
FEATURE_F16C = 49,
|
||||
FEATURE_LAHF_LM = 54,
|
||||
FEATURE_LM,
|
||||
FEATURE_WP,
|
||||
FEATURE_LZCNT,
|
||||
FEATURE_MOVBE,
|
||||
|
||||
FEATURE_AVX512FP16 = 94,
|
||||
FEATURE_X86_64_BASELINE,
|
||||
FEATURE_X86_64_V2,
|
||||
FEATURE_X86_64_V3,
|
||||
FEATURE_X86_64_V4,
|
||||
CPU_FEATURE_MAX
|
||||
};
|
||||
|
||||
// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
|
||||
// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
|
||||
// support. Consequently, for i386, the presence of CPUID is checked first
|
||||
// via the corresponding eflags bit.
|
||||
static bool isCpuIdSupported(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// This code is copied from lib/Support/Host.cpp.
|
||||
// Changes to either file should be mirrored in the other.
|
||||
|
||||
/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
|
||||
/// the specified arguments. If we can't run cpuid on the host, return true.
|
||||
static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
|
||||
unsigned *rECX, unsigned *rEDX) {
|
||||
// gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
|
||||
// FIXME: should we save this for Clang?
|
||||
__asm__("movq\t%%rbx, %%rsi\n\t"
|
||||
"cpuid\n\t"
|
||||
"xchgq\t%%rbx, %%rsi\n\t"
|
||||
: "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
|
||||
: "a"(value));
|
||||
return false;
|
||||
}
|
||||
|
||||
/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
|
||||
/// the 4 values in the specified arguments. If we can't run cpuid on the host,
|
||||
/// return true.
|
||||
static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
|
||||
unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
|
||||
unsigned *rEDX) {
|
||||
// gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
|
||||
// FIXME: should we save this for Clang?
|
||||
__asm__("movq\t%%rbx, %%rsi\n\t"
|
||||
"cpuid\n\t"
|
||||
"xchgq\t%%rbx, %%rsi\n\t"
|
||||
: "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
|
||||
: "a"(value), "c"(subleaf));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read control register 0 (XCR0). Used to detect features such as AVX.
|
||||
static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
|
||||
// Check xgetbv; this uses a .byte sequence instead of the instruction
|
||||
// directly because older assemblers do not include support for xgetbv and
|
||||
// there is no easy way to conditionally compile based on the assembler used.
|
||||
__asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
|
||||
return false;
|
||||
}
|
||||
|
||||
static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
|
||||
unsigned *Model) {
|
||||
*Family = (EAX >> 8) & 0xf; // Bits 8 - 11
|
||||
*Model = (EAX >> 4) & 0xf; // Bits 4 - 7
|
||||
if (*Family == 6 || *Family == 0xf) {
|
||||
if (*Family == 0xf)
|
||||
// Examine extended family ID if family ID is F.
|
||||
*Family += (EAX >> 20) & 0xff; // Bits 20 - 27
|
||||
// Examine extended model ID if family ID is 6 or F.
|
||||
*Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
|
||||
}
|
||||
}
|
||||
|
||||
static const char *getIntelProcessorTypeAndSubtype(unsigned Family,
|
||||
unsigned Model,
|
||||
const unsigned *Features,
|
||||
unsigned *Type,
|
||||
unsigned *Subtype) {
|
||||
#define testFeature(F) (Features[F / 32] & (1 << (F % 32))) != 0
|
||||
|
||||
// We select CPU strings to match the code in Host.cpp, but we don't use them
|
||||
// in compiler-rt.
|
||||
const char *CPU = 0;
|
||||
|
||||
switch (Family) {
|
||||
case 6:
|
||||
switch (Model) {
|
||||
case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
|
||||
// processor, Intel Core 2 Quad processor, Intel Core 2 Quad
|
||||
// mobile processor, Intel Core 2 Extreme processor, Intel
|
||||
// Pentium Dual-Core processor, Intel Xeon processor, model
|
||||
// 0Fh. All processors are manufactured using the 65 nm
|
||||
// process.
|
||||
case 0x16: // Intel Celeron processor model 16h. All processors are
|
||||
// manufactured using the 65 nm process
|
||||
CPU = "core2";
|
||||
*Type = INTEL_CORE2;
|
||||
break;
|
||||
case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor,
|
||||
// model 17h. All processors are manufactured using the 45
|
||||
// nm process.
|
||||
//
|
||||
// 45nm: Penryn , Wolfdale, Yorkfield (XE)
|
||||
case 0x1d: // Intel Xeon processor MP. All processors are manufactured
|
||||
// using the 45 nm process.
|
||||
CPU = "penryn";
|
||||
*Type = INTEL_CORE2;
|
||||
break;
|
||||
case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
|
||||
// processors are manufactured using the 45 nm process.
|
||||
case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
|
||||
// As found in a Summer 2010 model iMac.
|
||||
case 0x1f:
|
||||
case 0x2e: // Nehalem EX
|
||||
CPU = "nehalem";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_NEHALEM;
|
||||
break;
|
||||
case 0x25: // Intel Core i7, laptop version.
|
||||
case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
|
||||
// processors are manufactured using the 32 nm process.
|
||||
case 0x2f: // Westmere EX
|
||||
CPU = "westmere";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_WESTMERE;
|
||||
break;
|
||||
case 0x2a: // Intel Core i7 processor. All processors are manufactured
|
||||
// using the 32 nm process.
|
||||
case 0x2d:
|
||||
CPU = "sandybridge";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_SANDYBRIDGE;
|
||||
break;
|
||||
case 0x3a:
|
||||
case 0x3e: // Ivy Bridge EP
|
||||
CPU = "ivybridge";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_IVYBRIDGE;
|
||||
break;
|
||||
|
||||
// Haswell:
|
||||
case 0x3c:
|
||||
case 0x3f:
|
||||
case 0x45:
|
||||
case 0x46:
|
||||
CPU = "haswell";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_HASWELL;
|
||||
break;
|
||||
|
||||
// Broadwell:
|
||||
case 0x3d:
|
||||
case 0x47:
|
||||
case 0x4f:
|
||||
case 0x56:
|
||||
CPU = "broadwell";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_BROADWELL;
|
||||
break;
|
||||
|
||||
// Skylake:
|
||||
case 0x4e: // Skylake mobile
|
||||
case 0x5e: // Skylake desktop
|
||||
case 0x8e: // Kaby Lake mobile
|
||||
case 0x9e: // Kaby Lake desktop
|
||||
case 0xa5: // Comet Lake-H/S
|
||||
case 0xa6: // Comet Lake-U
|
||||
CPU = "skylake";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_SKYLAKE;
|
||||
break;
|
||||
|
||||
// Rocketlake:
|
||||
case 0xa7:
|
||||
CPU = "rocketlake";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_ROCKETLAKE;
|
||||
break;
|
||||
|
||||
// Skylake Xeon:
|
||||
case 0x55:
|
||||
*Type = INTEL_COREI7;
|
||||
if (testFeature(FEATURE_AVX512BF16)) {
|
||||
CPU = "cooperlake";
|
||||
*Subtype = INTEL_COREI7_COOPERLAKE;
|
||||
} else if (testFeature(FEATURE_AVX512VNNI)) {
|
||||
CPU = "cascadelake";
|
||||
*Subtype = INTEL_COREI7_CASCADELAKE;
|
||||
} else {
|
||||
CPU = "skylake-avx512";
|
||||
*Subtype = INTEL_COREI7_SKYLAKE_AVX512;
|
||||
}
|
||||
break;
|
||||
|
||||
// Cannonlake:
|
||||
case 0x66:
|
||||
CPU = "cannonlake";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_CANNONLAKE;
|
||||
break;
|
||||
|
||||
// Icelake:
|
||||
case 0x7d:
|
||||
case 0x7e:
|
||||
CPU = "icelake-client";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_ICELAKE_CLIENT;
|
||||
break;
|
||||
|
||||
// Tigerlake:
|
||||
case 0x8c:
|
||||
case 0x8d:
|
||||
CPU = "tigerlake";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_TIGERLAKE;
|
||||
break;
|
||||
|
||||
// Alderlake:
|
||||
case 0x97:
|
||||
case 0x9a:
|
||||
// Raptorlake:
|
||||
case 0xb7:
|
||||
case 0xba:
|
||||
case 0xbf:
|
||||
// Meteorlake:
|
||||
case 0xaa:
|
||||
case 0xac:
|
||||
// Gracemont:
|
||||
case 0xbe:
|
||||
CPU = "alderlake";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_ALDERLAKE;
|
||||
break;
|
||||
|
||||
// Arrowlake:
|
||||
case 0xc5:
|
||||
CPU = "arrowlake";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_ARROWLAKE;
|
||||
break;
|
||||
|
||||
// Arrowlake S:
|
||||
case 0xc6:
|
||||
// Lunarlake:
|
||||
case 0xbd:
|
||||
CPU = "arrowlake-s";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_ARROWLAKE_S;
|
||||
break;
|
||||
|
||||
// Pantherlake:
|
||||
case 0xcc:
|
||||
CPU = "pantherlake";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_PANTHERLAKE;
|
||||
break;
|
||||
|
||||
// Icelake Xeon:
|
||||
case 0x6a:
|
||||
case 0x6c:
|
||||
CPU = "icelake-server";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_ICELAKE_SERVER;
|
||||
break;
|
||||
|
||||
// Emerald Rapids:
|
||||
case 0xcf:
|
||||
// Sapphire Rapids:
|
||||
case 0x8f:
|
||||
CPU = "sapphirerapids";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_SAPPHIRERAPIDS;
|
||||
break;
|
||||
|
||||
// Granite Rapids:
|
||||
case 0xad:
|
||||
CPU = "graniterapids";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_GRANITERAPIDS;
|
||||
break;
|
||||
|
||||
// Granite Rapids D:
|
||||
case 0xae:
|
||||
CPU = "graniterapids-d";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_COREI7_GRANITERAPIDS_D;
|
||||
break;
|
||||
|
||||
case 0x1c: // Most 45 nm Intel Atom processors
|
||||
case 0x26: // 45 nm Atom Lincroft
|
||||
case 0x27: // 32 nm Atom Medfield
|
||||
case 0x35: // 32 nm Atom Midview
|
||||
case 0x36: // 32 nm Atom Midview
|
||||
CPU = "bonnell";
|
||||
*Type = INTEL_BONNELL;
|
||||
break;
|
||||
|
||||
// Atom Silvermont codes from the Intel software optimization guide.
|
||||
case 0x37:
|
||||
case 0x4a:
|
||||
case 0x4d:
|
||||
case 0x5a:
|
||||
case 0x5d:
|
||||
case 0x4c: // really airmont
|
||||
CPU = "silvermont";
|
||||
*Type = INTEL_SILVERMONT;
|
||||
break;
|
||||
// Goldmont:
|
||||
case 0x5c: // Apollo Lake
|
||||
case 0x5f: // Denverton
|
||||
CPU = "goldmont";
|
||||
*Type = INTEL_GOLDMONT;
|
||||
break; // "goldmont"
|
||||
case 0x7a:
|
||||
CPU = "goldmont-plus";
|
||||
*Type = INTEL_GOLDMONT_PLUS;
|
||||
break;
|
||||
case 0x86:
|
||||
case 0x8a: // Lakefield
|
||||
case 0x96: // Elkhart Lake
|
||||
case 0x9c: // Jasper Lake
|
||||
CPU = "tremont";
|
||||
*Type = INTEL_TREMONT;
|
||||
break;
|
||||
|
||||
// Sierraforest:
|
||||
case 0xaf:
|
||||
CPU = "sierraforest";
|
||||
*Type = INTEL_SIERRAFOREST;
|
||||
break;
|
||||
|
||||
// Grandridge:
|
||||
case 0xb6:
|
||||
CPU = "grandridge";
|
||||
*Type = INTEL_GRANDRIDGE;
|
||||
break;
|
||||
|
||||
// Clearwaterforest:
|
||||
case 0xdd:
|
||||
CPU = "clearwaterforest";
|
||||
*Type = INTEL_COREI7;
|
||||
*Subtype = INTEL_CLEARWATERFOREST;
|
||||
break;
|
||||
|
||||
case 0x57:
|
||||
CPU = "knl";
|
||||
*Type = INTEL_KNL;
|
||||
break;
|
||||
|
||||
case 0x85:
|
||||
CPU = "knm";
|
||||
*Type = INTEL_KNM;
|
||||
break;
|
||||
|
||||
default: // Unknown family 6 CPU.
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break; // Unknown.
|
||||
}
|
||||
|
||||
return CPU;
|
||||
}
|
||||
|
||||
static const char *getAMDProcessorTypeAndSubtype(unsigned Family,
|
||||
unsigned Model,
|
||||
const unsigned *Features,
|
||||
unsigned *Type,
|
||||
unsigned *Subtype) {
|
||||
// We select CPU strings to match the code in Host.cpp, but we don't use them
|
||||
// in compiler-rt.
|
||||
const char *CPU = 0;
|
||||
|
||||
switch (Family) {
|
||||
case 16:
|
||||
CPU = "amdfam10";
|
||||
*Type = AMDFAM10H;
|
||||
switch (Model) {
|
||||
case 2:
|
||||
*Subtype = AMDFAM10H_BARCELONA;
|
||||
break;
|
||||
case 4:
|
||||
*Subtype = AMDFAM10H_SHANGHAI;
|
||||
break;
|
||||
case 8:
|
||||
*Subtype = AMDFAM10H_ISTANBUL;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 20:
|
||||
CPU = "btver1";
|
||||
*Type = AMD_BTVER1;
|
||||
break;
|
||||
case 21:
|
||||
CPU = "bdver1";
|
||||
*Type = AMDFAM15H;
|
||||
if (Model >= 0x60 && Model <= 0x7f) {
|
||||
CPU = "bdver4";
|
||||
*Subtype = AMDFAM15H_BDVER4;
|
||||
break; // 60h-7Fh: Excavator
|
||||
}
|
||||
if (Model >= 0x30 && Model <= 0x3f) {
|
||||
CPU = "bdver3";
|
||||
*Subtype = AMDFAM15H_BDVER3;
|
||||
break; // 30h-3Fh: Steamroller
|
||||
}
|
||||
if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {
|
||||
CPU = "bdver2";
|
||||
*Subtype = AMDFAM15H_BDVER2;
|
||||
break; // 02h, 10h-1Fh: Piledriver
|
||||
}
|
||||
if (Model <= 0x0f) {
|
||||
*Subtype = AMDFAM15H_BDVER1;
|
||||
break; // 00h-0Fh: Bulldozer
|
||||
}
|
||||
break;
|
||||
case 22:
|
||||
CPU = "btver2";
|
||||
*Type = AMD_BTVER2;
|
||||
break;
|
||||
case 23:
|
||||
CPU = "znver1";
|
||||
*Type = AMDFAM17H;
|
||||
if ((Model >= 0x30 && Model <= 0x3f) || (Model == 0x47) ||
|
||||
(Model >= 0x60 && Model <= 0x67) ||
|
||||
(Model >= 0x68 && Model <= 0x6f) ||
|
||||
(Model >= 0x70 && Model <= 0x7f) ||
|
||||
(Model >= 0x84 && Model <= 0x87) ||
|
||||
(Model >= 0x90 && Model <= 0x97) ||
|
||||
(Model >= 0x98 && Model <= 0x9f) ||
|
||||
(Model >= 0xa0 && Model <= 0xaf)) {
|
||||
// Family 17h Models 30h-3Fh (Starship) Zen 2
|
||||
// Family 17h Models 47h (Cardinal) Zen 2
|
||||
// Family 17h Models 60h-67h (Renoir) Zen 2
|
||||
// Family 17h Models 68h-6Fh (Lucienne) Zen 2
|
||||
// Family 17h Models 70h-7Fh (Matisse) Zen 2
|
||||
// Family 17h Models 84h-87h (ProjectX) Zen 2
|
||||
// Family 17h Models 90h-97h (VanGogh) Zen 2
|
||||
// Family 17h Models 98h-9Fh (Mero) Zen 2
|
||||
// Family 17h Models A0h-AFh (Mendocino) Zen 2
|
||||
CPU = "znver2";
|
||||
*Subtype = AMDFAM17H_ZNVER2;
|
||||
break;
|
||||
}
|
||||
if ((Model >= 0x10 && Model <= 0x1f) ||
|
||||
(Model >= 0x20 && Model <= 0x2f)) {
|
||||
// Family 17h Models 10h-1Fh (Raven1) Zen
|
||||
// Family 17h Models 10h-1Fh (Picasso) Zen+
|
||||
// Family 17h Models 20h-2Fh (Raven2 x86) Zen
|
||||
*Subtype = AMDFAM17H_ZNVER1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 25:
|
||||
CPU = "znver3";
|
||||
*Type = AMDFAM19H;
|
||||
if ((Model <= 0x0f) || (Model >= 0x20 && Model <= 0x2f) ||
|
||||
(Model >= 0x30 && Model <= 0x3f) ||
|
||||
(Model >= 0x40 && Model <= 0x4f) ||
|
||||
(Model >= 0x50 && Model <= 0x5f)) {
|
||||
// Family 19h Models 00h-0Fh (Genesis, Chagall) Zen 3
|
||||
// Family 19h Models 20h-2Fh (Vermeer) Zen 3
|
||||
// Family 19h Models 30h-3Fh (Badami) Zen 3
|
||||
// Family 19h Models 40h-4Fh (Rembrandt) Zen 3+
|
||||
// Family 19h Models 50h-5Fh (Cezanne) Zen 3
|
||||
*Subtype = AMDFAM19H_ZNVER3;
|
||||
break;
|
||||
}
|
||||
if ((Model >= 0x10 && Model <= 0x1f) ||
|
||||
(Model >= 0x60 && Model <= 0x6f) ||
|
||||
(Model >= 0x70 && Model <= 0x77) ||
|
||||
(Model >= 0x78 && Model <= 0x7f) ||
|
||||
(Model >= 0xa0 && Model <= 0xaf)) {
|
||||
// Family 19h Models 10h-1Fh (Stones; Storm Peak) Zen 4
|
||||
// Family 19h Models 60h-6Fh (Raphael) Zen 4
|
||||
// Family 19h Models 70h-77h (Phoenix, Hawkpoint1) Zen 4
|
||||
// Family 19h Models 78h-7Fh (Phoenix 2, Hawkpoint2) Zen 4
|
||||
// Family 19h Models A0h-AFh (Stones-Dense) Zen 4
|
||||
CPU = "znver4";
|
||||
*Subtype = AMDFAM19H_ZNVER4;
|
||||
break; // "znver4"
|
||||
}
|
||||
break; // family 19h
|
||||
default:
|
||||
break; // Unknown AMD CPU.
|
||||
}
|
||||
|
||||
return CPU;
|
||||
}
|
||||
|
||||
static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
|
||||
unsigned *Features) {
|
||||
unsigned EAX = 0, EBX = 0;
|
||||
|
||||
#define hasFeature(F) ((Features[F / 32] >> (F % 32)) & 1)
|
||||
#define setFeature(F) Features[F / 32] |= 1U << (F % 32)
|
||||
|
||||
if ((EDX >> 15) & 1) setFeature(FEATURE_CMOV);
|
||||
if ((EDX >> 23) & 1) setFeature(FEATURE_MMX);
|
||||
if ((EDX >> 25) & 1) setFeature(FEATURE_SSE);
|
||||
if ((EDX >> 26) & 1) setFeature(FEATURE_SSE2);
|
||||
|
||||
if ((ECX >> 0) & 1) setFeature(FEATURE_SSE3);
|
||||
if ((ECX >> 1) & 1) setFeature(FEATURE_PCLMUL);
|
||||
if ((ECX >> 9) & 1) setFeature(FEATURE_SSSE3);
|
||||
if ((ECX >> 12) & 1) setFeature(FEATURE_FMA);
|
||||
if ((ECX >> 13) & 1) setFeature(FEATURE_CMPXCHG16B);
|
||||
if ((ECX >> 19) & 1) setFeature(FEATURE_SSE4_1);
|
||||
if ((ECX >> 20) & 1) setFeature(FEATURE_SSE4_2);
|
||||
if ((ECX >> 22) & 1) setFeature(FEATURE_MOVBE);
|
||||
if ((ECX >> 23) & 1) setFeature(FEATURE_POPCNT);
|
||||
if ((ECX >> 25) & 1) setFeature(FEATURE_AES);
|
||||
if ((ECX >> 29) & 1) setFeature(FEATURE_F16C);
|
||||
|
||||
// If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
|
||||
// indicates that the AVX registers will be saved and restored on context
|
||||
// switch, then we have full AVX support.
|
||||
const unsigned AVXBits = (1 << 27) | (1 << 28);
|
||||
bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
|
||||
((EAX & 0x6) == 0x6);
|
||||
#if defined(__APPLE__)
|
||||
// Darwin lazily saves the AVX512 context on first use: trust that the OS will
|
||||
// save the AVX512 context if we use AVX512 instructions, even the bit is not
|
||||
// set right now.
|
||||
bool HasAVX512Save = true;
|
||||
#else
|
||||
// AVX512 requires additional context to be saved by the OS.
|
||||
bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
|
||||
#endif
|
||||
|
||||
if (HasAVX) setFeature(FEATURE_AVX);
|
||||
|
||||
bool HasLeaf7 =
|
||||
MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
|
||||
|
||||
if (HasLeaf7) {
|
||||
if ((EBX >> 3) & 1) setFeature(FEATURE_BMI);
|
||||
if (((EBX >> 5) & 1) && HasAVX) setFeature(FEATURE_AVX2);
|
||||
if ((EBX >> 8) & 1) setFeature(FEATURE_BMI2);
|
||||
if (HasAVX512Save) {
|
||||
if ((EBX >> 16) & 1) setFeature(FEATURE_AVX512F);
|
||||
if ((EBX >> 17) & 1) setFeature(FEATURE_AVX512DQ);
|
||||
if ((EBX >> 21) & 1) setFeature(FEATURE_AVX512IFMA);
|
||||
if ((EBX >> 26) & 1) setFeature(FEATURE_AVX512PF);
|
||||
if ((EBX >> 27) & 1) setFeature(FEATURE_AVX512ER);
|
||||
if ((EBX >> 28) & 1) setFeature(FEATURE_AVX512CD);
|
||||
if ((EBX >> 30) & 1) setFeature(FEATURE_AVX512BW);
|
||||
if ((EBX >> 31) & 1) setFeature(FEATURE_AVX512VL);
|
||||
if ((ECX >> 1) & 1) setFeature(FEATURE_AVX512VBMI);
|
||||
if ((ECX >> 6) & 1) setFeature(FEATURE_AVX512VBMI2);
|
||||
if ((ECX >> 11) & 1) setFeature(FEATURE_AVX512VNNI);
|
||||
if ((ECX >> 12) & 1) setFeature(FEATURE_AVX512BITALG);
|
||||
if ((ECX >> 14) & 1) setFeature(FEATURE_AVX512VPOPCNTDQ);
|
||||
if ((EDX >> 2) & 1) setFeature(FEATURE_AVX5124VNNIW);
|
||||
if ((EDX >> 3) & 1) setFeature(FEATURE_AVX5124FMAPS);
|
||||
if ((EDX >> 8) & 1) setFeature(FEATURE_AVX512VP2INTERSECT);
|
||||
if ((EDX >> 23) & 1) setFeature(FEATURE_AVX512FP16);
|
||||
}
|
||||
if ((ECX >> 8) & 1) setFeature(FEATURE_GFNI);
|
||||
if (((ECX >> 10) & 1) && HasAVX) setFeature(FEATURE_VPCLMULQDQ);
|
||||
}
|
||||
|
||||
// EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't
|
||||
// return all 0s for invalid subleaves so check the limit.
|
||||
bool HasLeaf7Subleaf1 =
|
||||
HasLeaf7 && EAX >= 1 &&
|
||||
!getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
|
||||
if (HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save)
|
||||
setFeature(FEATURE_AVX512BF16);
|
||||
|
||||
unsigned MaxExtLevel;
|
||||
getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
|
||||
|
||||
bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
|
||||
!getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
|
||||
if (HasExtLeaf1) {
|
||||
if (ECX & 1) setFeature(FEATURE_LAHF_LM);
|
||||
if ((ECX >> 5) & 1) setFeature(FEATURE_LZCNT);
|
||||
if (((ECX >> 6) & 1)) setFeature(FEATURE_SSE4_A);
|
||||
if (((ECX >> 11) & 1)) setFeature(FEATURE_XOP);
|
||||
if (((ECX >> 16) & 1)) setFeature(FEATURE_FMA4);
|
||||
if (((EDX >> 29) & 1)) setFeature(FEATURE_LM);
|
||||
}
|
||||
|
||||
if (hasFeature(FEATURE_LM) && hasFeature(FEATURE_SSE2)) {
|
||||
setFeature(FEATURE_X86_64_BASELINE);
|
||||
if (hasFeature(FEATURE_CMPXCHG16B) && hasFeature(FEATURE_POPCNT) &&
|
||||
hasFeature(FEATURE_LAHF_LM) && hasFeature(FEATURE_SSE4_2)) {
|
||||
setFeature(FEATURE_X86_64_V2);
|
||||
if (hasFeature(FEATURE_AVX2) && hasFeature(FEATURE_BMI) &&
|
||||
hasFeature(FEATURE_BMI2) && hasFeature(FEATURE_F16C) &&
|
||||
hasFeature(FEATURE_FMA) && hasFeature(FEATURE_LZCNT) &&
|
||||
hasFeature(FEATURE_MOVBE)) {
|
||||
setFeature(FEATURE_X86_64_V3);
|
||||
if (hasFeature(FEATURE_AVX512BW) && hasFeature(FEATURE_AVX512CD) &&
|
||||
hasFeature(FEATURE_AVX512DQ) && hasFeature(FEATURE_AVX512VL))
|
||||
setFeature(FEATURE_X86_64_V4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#undef hasFeature
|
||||
#undef setFeature
|
||||
}
|
||||
|
||||
int __cpu_indicator_init(void) CONSTRUCTOR_ATTRIBUTE;
|
||||
|
||||
struct __processor_model {
|
||||
unsigned int __cpu_vendor;
|
||||
unsigned int __cpu_type;
|
||||
unsigned int __cpu_subtype;
|
||||
unsigned int __cpu_features[1];
|
||||
} __cpu_model = {0, 0, 0, {0}};
|
||||
|
||||
unsigned __cpu_features2[(CPU_FEATURE_MAX - 1) / 32];
|
||||
|
||||
// A constructor function that is sets __cpu_model and __cpu_features2 with
|
||||
// the right values. This needs to run only once. This constructor is
|
||||
// given the highest priority and it should run before constructors without
|
||||
// the priority set. However, it still runs after ifunc initializers and
|
||||
// needs to be called explicitly there.
|
||||
|
||||
int CONSTRUCTOR_ATTRIBUTE __cpu_indicator_init(void) {
|
||||
unsigned EAX, EBX, ECX, EDX;
|
||||
unsigned MaxLeaf = 5;
|
||||
unsigned Vendor;
|
||||
unsigned Model, Family;
|
||||
unsigned Features[(CPU_FEATURE_MAX + 31) / 32] = {0};
|
||||
_Static_assert(sizeof(Features) / sizeof(Features[0]) == 4, "");
|
||||
_Static_assert(sizeof(__cpu_features2) / sizeof(__cpu_features2[0]) == 3, "");
|
||||
|
||||
// This function needs to run just once.
|
||||
if (__cpu_model.__cpu_vendor) return 0;
|
||||
|
||||
if (!isCpuIdSupported() ||
|
||||
getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1) {
|
||||
__cpu_model.__cpu_vendor = VENDOR_OTHER;
|
||||
return -1;
|
||||
}
|
||||
|
||||
getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
|
||||
detectX86FamilyModel(EAX, &Family, &Model);
|
||||
|
||||
// Find available features.
|
||||
getAvailableFeatures(ECX, EDX, MaxLeaf, &Features[0]);
|
||||
|
||||
__cpu_model.__cpu_features[0] = Features[0];
|
||||
__cpu_features2[0] = Features[1];
|
||||
__cpu_features2[1] = Features[2];
|
||||
__cpu_features2[2] = Features[3];
|
||||
|
||||
if (Vendor == SIG_INTEL) {
|
||||
// Get CPU type.
|
||||
getIntelProcessorTypeAndSubtype(Family, Model, &Features[0],
|
||||
&(__cpu_model.__cpu_type),
|
||||
&(__cpu_model.__cpu_subtype));
|
||||
__cpu_model.__cpu_vendor = VENDOR_INTEL;
|
||||
} else if (Vendor == SIG_AMD) {
|
||||
// Get CPU type.
|
||||
getAMDProcessorTypeAndSubtype(Family, Model, &Features[0],
|
||||
&(__cpu_model.__cpu_type),
|
||||
&(__cpu_model.__cpu_subtype));
|
||||
__cpu_model.__cpu_vendor = VENDOR_AMD;
|
||||
} else {
|
||||
__cpu_model.__cpu_vendor = VENDOR_OTHER;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // __x86_64__ && (gnuc || clang)
|
|
@ -36,7 +36,7 @@ static struct {
|
|||
char buf[PATH_MAX];
|
||||
} g_addr2line;
|
||||
|
||||
const void GetAddr2linePathInit(void) {
|
||||
void GetAddr2linePathInit(void) {
|
||||
int e = errno;
|
||||
const char *path;
|
||||
if (!(path = getenv("ADDR2LINE"))) {
|
||||
|
|
|
@ -48,10 +48,10 @@ void __cxa_printexits(FILE *f, void *pred) {
|
|||
if (symbol) {
|
||||
snprintf(name, sizeof(name), "%s", symbol);
|
||||
} else {
|
||||
snprintf(name, sizeof(name), "0x%016lx", b->p[i].fp);
|
||||
snprintf(name, sizeof(name), "0x%016lx", (unsigned long)b->p[i].fp);
|
||||
}
|
||||
fprintf(f, "%-22s 0x%016lx 0x%016lx\n", name, b->p[i].arg,
|
||||
b->p[i].pred);
|
||||
fprintf(f, "%-22s 0x%016lx 0x%016lx\n", name,
|
||||
(unsigned long)b->p[i].arg, (unsigned long)b->p[i].pred);
|
||||
}
|
||||
}
|
||||
} while ((b = b->next));
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "libc/errno.h"
|
||||
#include "libc/intrin/describebacktrace.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/log/backtrace.internal.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
|
@ -51,5 +52,6 @@ relegated wontreturn void __die(void) {
|
|||
__nocolor ? "" : "\e[1;31m", program_invocation_short_name, host,
|
||||
getpid(), gettid(), __nocolor ? "" : "\e[0m", FindDebugBinary(),
|
||||
DescribeBacktrace(__builtin_frame_address(0)));
|
||||
ShowBacktrace(2, __builtin_frame_address(0));
|
||||
_Exit(77);
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ relegated int(gdbexec)(const char *cmd) {
|
|||
elf = "-q";
|
||||
}
|
||||
bp = __builtin_frame_address(0);
|
||||
sprintf(breakcmd, "%s *%#p", "break", bp->addr);
|
||||
sprintf(breakcmd, "%s *%#lx", "break", (unsigned long)bp->addr);
|
||||
if (!(pid = vfork())) {
|
||||
execv(gdb, (char *const[]){
|
||||
"gdb",
|
||||
|
|
|
@ -252,16 +252,9 @@ static relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
|||
}
|
||||
|
||||
relegated void __oncrash(int sig, struct siginfo *si, void *arg) {
|
||||
ucontext_t *ctx = arg;
|
||||
int gdbpid, err;
|
||||
err = errno;
|
||||
if ((gdbpid = IsDebuggerPresent(true))) {
|
||||
DebugBreak();
|
||||
}
|
||||
if (!(gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT))) {
|
||||
__restore_tty();
|
||||
ShowCrashReport(err, sig, si, ctx);
|
||||
}
|
||||
int err = errno;
|
||||
__restore_tty();
|
||||
ShowCrashReport(err, sig, si, arg);
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
|
@ -21,8 +21,8 @@
|
|||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
#define IS2POW(X) (!((X) & ((X)-1)))
|
||||
#define ROUNDUP(X, K) (((X) + (K)-1) & -(K))
|
||||
#define IS2POW(X) (!((X) & ((X) - 1)))
|
||||
#define ROUNDUP(X, K) (((X) + (K) - 1) & -(K))
|
||||
#define ROUNDDOWN(X, K) ((X) & -(K))
|
||||
#ifndef __ASSEMBLER__
|
||||
#define ABS(X) ((X) >= 0 ? (X) : -(X))
|
||||
|
@ -40,7 +40,7 @@
|
|||
#define STRINGIFY(A) __STRINGIFY(A)
|
||||
#define EQUIVALENT(X, Y) (__builtin_constant_p((X) == (Y)) && ((X) == (Y)))
|
||||
#define TYPE_BIT(type) (sizeof(type) * CHAR_BIT)
|
||||
#define TYPE_SIGNED(type) (((type)-1) < 0)
|
||||
#define TYPE_SIGNED(type) (((type) - 1) < 0)
|
||||
#define TYPE_INTEGRAL(type) (((type)0.5) != 0.5)
|
||||
|
||||
#define ARRAYLEN(A) \
|
||||
|
|
|
@ -54,8 +54,8 @@ asm(".include \"libc/disclaimer.inc\"");
|
|||
* (The default is pairwise merging.)
|
||||
*/
|
||||
|
||||
static void setup(uint8_t *, uint8_t *, size_t, size_t, int (*)(), void *);
|
||||
static void insertionsort(uint8_t *, size_t, size_t, int (*)(), void *);
|
||||
static void setup(uint8_t *, uint8_t *, size_t, size_t, int (*)(const void *, const void *, void *), void *);
|
||||
static void insertionsort(uint8_t *, size_t, size_t, int (*)(const void *, const void *, void *), void *);
|
||||
|
||||
#define ISIZE sizeof(int)
|
||||
#define PSIZE sizeof(uint8_t *)
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include "libc/nexgen32e/x86feature.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define TSC_AUX_CORE(MSR) ((MSR)&0xfff)
|
||||
#define TSC_AUX_CORE(MSR) ((MSR) & 0xfff)
|
||||
#define TSC_AUX_NODE(MSR) (((MSR) >> 12) & 0xfff)
|
||||
|
||||
/**
|
||||
|
@ -41,9 +41,10 @@ COSMOPOLITAN_C_START_
|
|||
asm volatile("rdpid\t%0" : "=r"(Msr) : /* no inputs */ : "memory"); \
|
||||
Ok = true; \
|
||||
} else if (IsLinux()) { \
|
||||
char *p = (char *)0x7b; \
|
||||
asm volatile(ZFLAG_ASM("lsl\t%2,%1") \
|
||||
: ZFLAG_CONSTRAINT(Ok), "=r"(Msr) \
|
||||
: "r"(0x7b) \
|
||||
: "r"(p) \
|
||||
: "memory"); \
|
||||
} \
|
||||
if (!Ok && X86_HAVE(RDTSCP)) { \
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
#include "libc/nt/codegen.h"
|
||||
.imp advapi32,__imp_SystemFunction036,SystemFunction036
|
||||
.imp BCryptPrimitives,__imp_ProcessPrng,ProcessPrng
|
||||
|
||||
.text.windows
|
||||
.ftrace1
|
||||
RtlGenRandom:
|
||||
ProcessPrng:
|
||||
.ftrace2
|
||||
#ifdef __x86_64__
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
mov __imp_SystemFunction036(%rip),%rax
|
||||
mov __imp_ProcessPrng(%rip),%rax
|
||||
jmp __sysv2nt
|
||||
#elif defined(__aarch64__)
|
||||
mov x0,#0
|
||||
ret
|
||||
#endif
|
||||
.endfn RtlGenRandom,globl
|
||||
.endfn ProcessPrng,globl
|
||||
.previous
|
|
@ -297,6 +297,24 @@ $(LIBC_NT_PSAPI_A).pkg: \
|
|||
|
||||
#───────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
LIBC_NT_ARTIFACTS += LIBC_NT_BCRYPTPRIMITIVES_A
|
||||
LIBC_NT_BCRYPTPRIMITIVES = $(LIBC_NT_BCRYPTPRIMITIVES_A_DEPS) $(LIBC_NT_BCRYPTPRIMITIVES_A)
|
||||
LIBC_NT_BCRYPTPRIMITIVES_A = o/$(MODE)/libc/nt/BCryptPrimitives.a
|
||||
LIBC_NT_BCRYPTPRIMITIVES_A_SRCS := $(wildcard libc/nt/BCryptPrimitives/*.S)
|
||||
LIBC_NT_BCRYPTPRIMITIVES_A_OBJS = $(LIBC_NT_BCRYPTPRIMITIVES_A_SRCS:%.S=o/$(MODE)/%.o)
|
||||
LIBC_NT_BCRYPTPRIMITIVES_A_CHECKS = $(LIBC_NT_BCRYPTPRIMITIVES_A).pkg
|
||||
LIBC_NT_BCRYPTPRIMITIVES_A_DIRECTDEPS = LIBC_NT_KERNEL32
|
||||
LIBC_NT_BCRYPTPRIMITIVES_A_DEPS := $(call uniq,$(foreach x,$(LIBC_NT_BCRYPTPRIMITIVES_A_DIRECTDEPS),$($(x))))
|
||||
$(LIBC_NT_BCRYPTPRIMITIVES_A): \
|
||||
libc/nt/BCryptPrimitives/ \
|
||||
$(LIBC_NT_BCRYPTPRIMITIVES_A).pkg \
|
||||
$(LIBC_NT_BCRYPTPRIMITIVES_A_OBJS)
|
||||
$(LIBC_NT_BCRYPTPRIMITIVES_A).pkg: \
|
||||
$(LIBC_NT_BCRYPTPRIMITIVES_A_OBJS) \
|
||||
$(foreach x,$(LIBC_NT_BCRYPTPRIMITIVES_A_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
#───────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
# let aarch64 compile these
|
||||
o/$(MODE)/libc/nt/%.o: libc/nt/%.S
|
||||
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
|
||||
|
|
|
@ -362,7 +362,6 @@ imp 'RegisterEventSource' RegisterEventSourceW advapi32 2
|
|||
imp 'ReportEvent' ReportEventW advapi32 9
|
||||
imp 'ReportEventA' ReportEventA advapi32 9
|
||||
imp 'RevertToSelf' RevertToSelf advapi32 0
|
||||
imp 'RtlGenRandom' SystemFunction036 advapi32 2
|
||||
imp 'TraceSetInformation' TraceSetInformation advapi32 # Windows 7+
|
||||
|
||||
# USER32.DLL
|
||||
|
@ -611,6 +610,11 @@ imp 'GetModuleBaseName' GetModuleBaseNameW psapi 4
|
|||
imp 'GetProcessImageFileName' GetProcessImageFileNameW psapi 3
|
||||
imp 'GetProcessMemoryInfo' GetProcessMemoryInfo psapi 3
|
||||
|
||||
# BCryptPrimitives.dll
|
||||
#
|
||||
# Name Actual DLL Arity
|
||||
imp 'ProcessPrng' ProcessPrng BCryptPrimitives 2
|
||||
|
||||
# API-MS-Win-Core-Synch-l1-2-0.dll (Windows 8+)
|
||||
#
|
||||
# Name Actual DLL Arity
|
||||
|
|
|
@ -36,11 +36,11 @@ bool32 TerminateProcess(int64_t hProcess, uint32_t uExitCode);
|
|||
void TerminateThisProcess(uint32_t dwWaitStatus) wontreturn;
|
||||
void ExitProcess(uint32_t uExitCode) wontreturn;
|
||||
uint32_t GetLastError(void) nosideeffect;
|
||||
bool32 CloseHandle(int64_t hObject) dontthrow nocallback;
|
||||
bool32 CloseHandle(int64_t hObject) dontthrow dontcallback;
|
||||
intptr_t GetStdHandle(uint32_t nStdHandle) nosideeffect;
|
||||
bool32 SetStdHandle(uint32_t nStdHandle, int64_t hHandle);
|
||||
bool32 SetDefaultDllDirectories(unsigned dirflags);
|
||||
bool32 RtlGenRandom(void *RandomBuffer, uint32_t RandomBufferLength);
|
||||
bool32 ProcessPrng(void *RandomBuffer, uint32_t RandomBufferLength);
|
||||
uint32_t GetModuleFileName(int64_t hModule, char16_t *lpFilename,
|
||||
uint32_t nSize);
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_NT_TEB_H_
|
||||
#define COSMOPOLITAN_LIBC_NT_TEB_H_
|
||||
#include "libc/intrin/segmentation.h"
|
||||
#include "libc/nt/struct/peb.h"
|
||||
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
||||
|
||||
|
@ -8,19 +7,19 @@
|
|||
* These macros address directly into NT's TEB a.k.a. TIB
|
||||
* Any function that does this needs the `dontasan` keyword
|
||||
*/
|
||||
#define NtGetPeb() gs((struct NtPeb **)(0x60ULL))
|
||||
#define NtGetTeb() gs((void **)(0x30)) /* %gs:0 linear address */
|
||||
#define NtGetPid() gs((uint32_t *)(0x40)) /* GetCurrentProcessId() */
|
||||
#define NtGetTid() gs((uint32_t *)(0x48)) /* GetCurrentThreadId() */
|
||||
#define NtGetErr() gs((int *)(0x68))
|
||||
#define _NtGetSeh() gs((void **)(0x00))
|
||||
#define _NtGetStackHigh() gs((void **)(0x08))
|
||||
#define _NtGetStackLow() gs((void **)(0x10))
|
||||
#define _NtGetSubsystemTib() gs((void **)(0x18))
|
||||
#define _NtGetFib() gs((void **)(0x20))
|
||||
#define _NtGetEnv() gs((char16_t **)(0x38))
|
||||
#define _NtGetRpc() gs((void **)(0x50))
|
||||
#define _NtGetTls() gs((void **)(0x58)) /* cf. gs((long *)0x1480 + i0..64) */
|
||||
#define NtGetPeb() ((__seg_gs struct NtPeb *)0x60)
|
||||
#define NtGetTeb() ((void *)*(__seg_gs uintptr_t *)0x30)
|
||||
#define NtGetPid() (*(__seg_gs uint32_t *)0x40)
|
||||
#define NtGetTid() (*(__seg_gs uint32_t *)0x48)
|
||||
#define NtGetErr() (*(__seg_gs int *)0x68)
|
||||
#define _NtGetSeh() ((void *)*(__seg_gs uintptr_t *)0x00)
|
||||
#define _NtGetStackHigh() ((void *)*(__seg_gs uintptr_t *)0x08)
|
||||
#define _NtGetStackLow() ((void *)*(__seg_gs uintptr_t *)0x10)
|
||||
#define _NtGetSubsystemTib() ((void *)*(__seg_gs uintptr_t *)0x18)
|
||||
#define _NtGetFib() ((void *)*(__seg_gs uintptr_t *)0x20)
|
||||
#define _NtGetEnv() ((char16_t *)*(__seg_gs intptr_t *)0x38)
|
||||
#define _NtGetRpc() ((void *)*(__seg_gs uintptr_t *)0x50)
|
||||
#define _NtGetTls() ((void *)*(__seg_gs uintptr_t *)0x58)
|
||||
|
||||
#endif /* __GNUC__ && !__STRICT_ANSI__ */
|
||||
#endif /* COSMOPOLITAN_LIBC_NT_TEB_H_ */
|
||||
|
|
|
@ -78,7 +78,9 @@ static textwindows char16_t *ParseInt(char16_t *p, int64_t *x) {
|
|||
}
|
||||
|
||||
static inline textwindows ssize_t ForkIo(int64_t h, char *p, size_t n,
|
||||
bool32 (*f)()) {
|
||||
bool32 (*f)(int64_t, void *, uint32_t,
|
||||
uint32_t *,
|
||||
struct NtOverlapped *)) {
|
||||
size_t i;
|
||||
uint32_t x;
|
||||
for (i = 0; i < n; i += x) {
|
||||
|
@ -90,8 +92,10 @@ static inline textwindows ssize_t ForkIo(int64_t h, char *p, size_t n,
|
|||
}
|
||||
|
||||
static dontinline textwindows bool ForkIo2(int64_t h, void *buf, size_t n,
|
||||
bool32 (*fn)(), const char *sf,
|
||||
bool ischild) {
|
||||
bool32 (*fn)(int64_t, void *,
|
||||
uint32_t, uint32_t *,
|
||||
struct NtOverlapped *),
|
||||
const char *sf, bool ischild) {
|
||||
ssize_t rc = ForkIo(h, buf, n, fn);
|
||||
if (ischild) __tls_enabled_set(false); // prevent tls crash in kprintf
|
||||
NTTRACE("%s(%ld, %p, %'zu) → %'zd% m", sf, h, buf, n, rc);
|
||||
|
@ -100,9 +104,9 @@ static dontinline textwindows bool ForkIo2(int64_t h, void *buf, size_t n,
|
|||
|
||||
static dontinline textwindows bool WriteAll(int64_t h, void *buf, size_t n) {
|
||||
bool ok;
|
||||
ok = ForkIo2(h, buf, n, WriteFile, "WriteFile", false);
|
||||
ok = ForkIo2(h, buf, n, (void *)WriteFile, "WriteFile", false);
|
||||
#ifndef NDEBUG
|
||||
if (ok) ok = ForkIo2(h, &n, sizeof(n), WriteFile, "WriteFile", false);
|
||||
if (ok) ok = ForkIo2(h, &n, sizeof(n), (void *)WriteFile, "WriteFile", false);
|
||||
#endif
|
||||
#if SYSDEBUG
|
||||
if (!ok) {
|
||||
|
|
|
@ -94,7 +94,7 @@ struct CloneArgs {
|
|||
void *arg;
|
||||
};
|
||||
|
||||
int sys_set_tls();
|
||||
int sys_set_tls(uintptr_t, void *);
|
||||
int __stack_call(void *, int, long, long, int (*)(void *, int), void *);
|
||||
|
||||
static struct CloneArgs *AllocateCloneArgs(char *stk, size_t stksz) {
|
||||
|
|
|
@ -149,7 +149,7 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1, char *exename,
|
|||
}
|
||||
|
||||
// check system call abi compatibility
|
||||
if (IsXnu() && __syslib->__version < SYSLIB_VERSION) {
|
||||
if (IsXnu() && __syslib->__version < SYSLIB_VERSION_MANDATORY) {
|
||||
sys_write(2, "need newer ape loader\n", 22);
|
||||
_Exit(127);
|
||||
}
|
||||
|
|
|
@ -30,6 +30,9 @@
|
|||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
#pragma GCC diagnostic ignored "-Warray-bounds"
|
||||
#pragma GCC diagnostic ignored "-Wstringop-overflow"
|
||||
|
||||
#ifdef __x86_64__
|
||||
|
||||
/* TODO: Why can't we change CR3? Could it really need PML5T? */
|
||||
|
@ -162,7 +165,7 @@ static void EfiInitAcpi(struct mman *mm, EFI_SYSTEM_TABLE *SystemTable) {
|
|||
* @see libc/dce.h
|
||||
*/
|
||||
__msabi EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
|
||||
EFI_SYSTEM_TABLE *SystemTable) {
|
||||
EFI_SYSTEM_TABLE *SystemTable) {
|
||||
struct mman *mm;
|
||||
uint32_t DescVersion;
|
||||
uintptr_t i, j, MapSize;
|
||||
|
@ -215,9 +218,8 @@ __msabi EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
|
|||
&kEfiLoadedImageProtocol, &ImgInfo);
|
||||
CmdLine = (const char16_t *)ImgInfo->LoadOptions;
|
||||
if (!CmdLine || !CmdLine[0]) CmdLine = u"BOOTX64.EFI";
|
||||
Args = GetDosArgv(CmdLine, ArgBlock->ArgBlock,
|
||||
sizeof(ArgBlock->ArgBlock), ArgBlock->Args,
|
||||
ARRAYLEN(ArgBlock->Args));
|
||||
Args = GetDosArgv(CmdLine, ArgBlock->ArgBlock, sizeof(ArgBlock->ArgBlock),
|
||||
ArgBlock->Args, ARRAYLEN(ArgBlock->Args));
|
||||
|
||||
/*
|
||||
* Gets information about our current video mode. Clears the screen.
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
#include "libc/dce.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/sysparam.h"
|
||||
#include "libc/sysv/consts/_posix.h"
|
||||
#include "libc/sysv/consts/limits.h"
|
||||
#include "libc/sysv/consts/rlimit.h"
|
||||
|
||||
|
@ -29,6 +31,7 @@
|
|||
* Returns expensive but more correct version of `ARG_MAX`.
|
||||
*/
|
||||
int __get_arg_max(void) {
|
||||
int res;
|
||||
if (IsLinux()) {
|
||||
// You might think that just returning a constant 128KiB (ARG_MAX)
|
||||
// would make sense, as this guy did:
|
||||
|
@ -57,10 +60,11 @@ int __get_arg_max(void) {
|
|||
// does. Right now (2019, Linux 5.3) that amounts to:
|
||||
uint64_t stacksz;
|
||||
stacksz = __get_rlimit(RLIMIT_STACK);
|
||||
return MAX(MIN(stacksz / 4, 3 * (8 * 1024 * 1024) / 4), _ARG_MAX);
|
||||
res = MAX(MIN(stacksz / 4, 3 * (8 * 1024 * 1024) / 4), _ARG_MAX);
|
||||
} else if (IsBsd()) {
|
||||
return __get_sysctl(CTL_KERN, KERN_ARGMAX);
|
||||
res = __get_sysctl(CTL_KERN, KERN_ARGMAX);
|
||||
} else {
|
||||
return _ARG_MAX;
|
||||
res = _ARG_MAX;
|
||||
}
|
||||
return MAX(res, _POSIX_ARG_MAX);
|
||||
}
|
||||
|
|
|
@ -164,9 +164,9 @@ forceinline pureconst bool OverlapsImageSpace(const void *p, size_t n) {
|
|||
const unsigned char *BegA, *EndA, *BegB, *EndB;
|
||||
if (n) {
|
||||
BegA = p;
|
||||
EndA = BegA + (n - 1);
|
||||
EndA = BegA + n;
|
||||
BegB = __executable_start;
|
||||
EndB = _end - 1;
|
||||
EndB = _end;
|
||||
return MAX(BegA, BegB) < MIN(EndA, EndB);
|
||||
} else {
|
||||
return 0;
|
||||
|
@ -177,9 +177,9 @@ forceinline pureconst bool OverlapsShadowSpace(const void *p, size_t n) {
|
|||
intptr_t BegA, EndA, BegB, EndB;
|
||||
if (n) {
|
||||
BegA = (intptr_t)p;
|
||||
EndA = BegA + (n - 1);
|
||||
EndA = BegA + n;
|
||||
BegB = 0x7fff0000;
|
||||
EndB = 0x10007fffffff;
|
||||
EndB = 0x100080000000;
|
||||
return MAX(BegA, BegB) < MIN(EndA, EndB);
|
||||
} else {
|
||||
return 0;
|
||||
|
|
|
@ -54,7 +54,7 @@ char *getlogin(void);
|
|||
int getlogin_r(char *, size_t);
|
||||
int login_tty(int);
|
||||
int getpagesize(void);
|
||||
int syncfs(int);
|
||||
int syncfs(int) dontthrow;
|
||||
int vhangup(void);
|
||||
int getdtablesize(void);
|
||||
int sethostname(const char *, size_t);
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
#define AMD64_SET_FSBASE 129
|
||||
#define AMD64_SET_GSBASE 131
|
||||
|
||||
int sys_set_tls();
|
||||
int sys_set_tls(uintptr_t, void *);
|
||||
|
||||
// we can't allow --ftrace here because cosmo_dlopen() calls this
|
||||
// function to fix the tls register, and ftrace needs it unbroken
|
||||
|
@ -47,12 +47,12 @@ dontinstrument textstartup void __set_tls(struct CosmoTib *tib) {
|
|||
// netbsd has sysarch(X86_SET_FSBASE) but we can't use that because
|
||||
// signal handlers will cause it to be reset due to not setting the
|
||||
// _mc_tlsbase field in struct mcontext_netbsd.
|
||||
sys_set_tls(tib);
|
||||
sys_set_tls((uintptr_t)tib, 0);
|
||||
} else if (IsOpenbsd()) {
|
||||
sys_set_tls(tib);
|
||||
sys_set_tls((uintptr_t)tib, 0);
|
||||
} else if (IsXnu()) {
|
||||
// thread_fast_set_cthread_self has a weird ABI
|
||||
sys_set_tls((intptr_t)tib - 0x30);
|
||||
sys_set_tls((intptr_t)tib - 0x30, 0);
|
||||
} else {
|
||||
uint64_t val = (uint64_t)tib;
|
||||
asm volatile("wrmsr"
|
||||
|
|
|
@ -10,8 +10,14 @@ COSMOPOLITAN_C_START_
|
|||
* `-errno` convention, and hence should be wrapped with `_sysret()`.
|
||||
*/
|
||||
|
||||
#define SYSLIB_MAGIC ('s' | 'l' << 8 | 'i' << 16 | 'b' << 24)
|
||||
#define SYSLIB_VERSION 8
|
||||
#define SYSLIB_MAGIC ('s' | 'l' << 8 | 'i' << 16 | 'b' << 24)
|
||||
|
||||
#define SYSLIB_VERSION 9 /* sync with ape/ape-m1.c */
|
||||
|
||||
/* if this number increases, then everyone on macos arm will need to
|
||||
reinstall ape loader in order to run newer ape binaries so please
|
||||
don't do this if it's sufficient to just check __syslib->version. */
|
||||
#define SYSLIB_VERSION_MANDATORY 8
|
||||
|
||||
typedef uint64_t dispatch_time_t;
|
||||
typedef uint64_t dispatch_semaphore_t;
|
||||
|
@ -69,11 +75,13 @@ struct Syslib {
|
|||
long (*__sem_trywait)(int *);
|
||||
long (*__getrlimit)(int, void *);
|
||||
long (*__setrlimit)(int, const void *);
|
||||
// v6 (2023-11-03)
|
||||
/* v6 (2023-11-03) */
|
||||
void *(*__dlopen)(const char *, int);
|
||||
void *(*__dlsym)(void *, const char *);
|
||||
int (*__dlclose)(void *);
|
||||
char *(*__dlerror)(void);
|
||||
/* v9 (2024-01-31) */
|
||||
int (*__pthread_cpu_number_np)(size_t *);
|
||||
};
|
||||
|
||||
extern struct Syslib *__syslib;
|
||||
|
|
|
@ -4,12 +4,17 @@
|
|||
COSMOPOLITAN_C_START_
|
||||
|
||||
struct WinArgs {
|
||||
char *argv[8192];
|
||||
char *envp[512];
|
||||
intptr_t auxv[2][2];
|
||||
char argv0buf[256];
|
||||
char argblock[32767];
|
||||
char envblock[32767];
|
||||
union {
|
||||
struct {
|
||||
char *argv[8192];
|
||||
char *envp[512];
|
||||
intptr_t auxv[2][2];
|
||||
char argv0buf[256];
|
||||
char argblock[32767];
|
||||
char envblock[32767];
|
||||
};
|
||||
char16_t tmp16[257];
|
||||
};
|
||||
} forcealign(16);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "libc/limits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nexgen32e/rdtsc.h"
|
||||
#include "libc/nt/accounting.h"
|
||||
#include "libc/nt/console.h"
|
||||
#include "libc/nt/enum/consolemodeflags.h"
|
||||
#include "libc/nt/enum/filemapflags.h"
|
||||
|
@ -59,6 +60,7 @@ __msabi extern typeof(GetEnvironmentStrings) *const __imp_GetEnvironmentStringsW
|
|||
__msabi extern typeof(GetEnvironmentVariable) *const __imp_GetEnvironmentVariableW;
|
||||
__msabi extern typeof(GetFileAttributes) *const __imp_GetFileAttributesW;
|
||||
__msabi extern typeof(GetStdHandle) *const __imp_GetStdHandle;
|
||||
__msabi extern typeof(GetUserName) *const __imp_GetUserNameW;
|
||||
__msabi extern typeof(MapViewOfFileEx) *const __imp_MapViewOfFileEx;
|
||||
__msabi extern typeof(SetConsoleCP) *const __imp_SetConsoleCP;
|
||||
__msabi extern typeof(SetConsoleMode) *const __imp_SetConsoleMode;
|
||||
|
@ -142,6 +144,11 @@ static abi void DeduplicateStdioHandles(void) {
|
|||
}
|
||||
}
|
||||
|
||||
static bool32 HasEnvironmentVariable(const char16_t *name) {
|
||||
char16_t buf[4];
|
||||
return __imp_GetEnvironmentVariableW(name, buf, ARRAYLEN(buf));
|
||||
}
|
||||
|
||||
// main function of windows init process
|
||||
// i.e. first process spawned that isn't forked
|
||||
static abi wontreturn void WinInit(const char16_t *cmdline) {
|
||||
|
@ -168,12 +175,6 @@ static abi wontreturn void WinInit(const char16_t *cmdline) {
|
|||
}
|
||||
}
|
||||
|
||||
// avoid programs like emacs nagging the user to define this
|
||||
char16_t var[8];
|
||||
if (!__imp_GetEnvironmentVariableW(u"TERM", var, 8)) {
|
||||
__imp_SetEnvironmentVariableW(u"TERM", u"xterm-256color");
|
||||
}
|
||||
|
||||
// allocate memory for stack and argument block
|
||||
_mmi.p = _mmi.s;
|
||||
_mmi.n = ARRAYLEN(_mmi.s);
|
||||
|
@ -200,6 +201,34 @@ static abi wontreturn void WinInit(const char16_t *cmdline) {
|
|||
struct WinArgs *wa =
|
||||
(struct WinArgs *)(stackaddr + (stacksize - sizeof(struct WinArgs)));
|
||||
|
||||
// define $TERM if it's not already present
|
||||
// programs like emacs will stop the world and nag if it's not set
|
||||
if (!HasEnvironmentVariable(u"TERM")) {
|
||||
__imp_SetEnvironmentVariableW(u"TERM", u"xterm-256color");
|
||||
}
|
||||
|
||||
// define $USER as GetUserName() if not set
|
||||
// Windows doesn't define this environment variable by default
|
||||
uint32_t vsize = ARRAYLEN(wa->tmp16);
|
||||
if (!HasEnvironmentVariable(u"USER") &&
|
||||
__imp_GetUserNameW(&wa->tmp16, &vsize)) {
|
||||
__imp_SetEnvironmentVariableW(u"USER", wa->tmp16);
|
||||
}
|
||||
|
||||
// define $HOME as $HOMEDRIVE$HOMEPATH if not set
|
||||
// Windows doesn't define this environment variable by default
|
||||
uint32_t vlen;
|
||||
if (!HasEnvironmentVariable(u"HOME") &&
|
||||
(vlen = __imp_GetEnvironmentVariableW(u"HOMEDRIVE", wa->tmp16,
|
||||
ARRAYLEN(wa->tmp16))) <
|
||||
ARRAYLEN(wa->tmp16) &&
|
||||
(vlen += __imp_GetEnvironmentVariableW(u"HOMEPATH", wa->tmp16 + vlen,
|
||||
ARRAYLEN(wa->tmp16) - vlen)) <
|
||||
ARRAYLEN(wa->tmp16) &&
|
||||
vlen) {
|
||||
__imp_SetEnvironmentVariableW(u"HOME", wa->tmp16);
|
||||
}
|
||||
|
||||
// parse utf-16 command into utf-8 argv array in argument block
|
||||
int count = GetDosArgv(cmdline, wa->argblock, ARRAYLEN(wa->argblock),
|
||||
wa->argv, ARRAYLEN(wa->argv));
|
||||
|
|
|
@ -2,15 +2,22 @@
|
|||
#define COSMOPOLITAN_LIBC_STDBOOL_H_
|
||||
|
||||
#ifndef __cplusplus
|
||||
#if __STDC_VERSION__ + 0 >= 201112
|
||||
|
||||
#define bool _Bool
|
||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ > 201710L
|
||||
#define true ((_Bool) + 1u)
|
||||
#define false ((_Bool) + 0u)
|
||||
#else
|
||||
#define bool unsigned char
|
||||
#endif
|
||||
#define true 1
|
||||
#define true 1
|
||||
#define false 0
|
||||
#endif
|
||||
|
||||
#else /* __cplusplus */
|
||||
|
||||
#define _Bool bool
|
||||
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#define __bool_true_false_are_defined
|
||||
#define __bool_true_false_are_defined 1
|
||||
|
||||
#endif /* COSMOPOLITAN_LIBC_STDBOOL_H_ */
|
||||
|
|
|
@ -43,7 +43,6 @@
|
|||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/divmod10.internal.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/serialize.h"
|
||||
#include "libc/intrin/bsr.h"
|
||||
#include "libc/intrin/nomultics.internal.h"
|
||||
#include "libc/intrin/safemacros.internal.h"
|
||||
|
@ -53,6 +52,7 @@
|
|||
#include "libc/mem/mem.h"
|
||||
#include "libc/mem/reverse.internal.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/serialize.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/strwidth.h"
|
||||
#include "libc/str/tab.internal.h"
|
||||
|
@ -800,7 +800,7 @@ int __fmt(void *fn, void *arg, const char *format, va_list va) {
|
|||
|
||||
x = 0;
|
||||
lasterr = errno;
|
||||
out = fn ? fn : __fmt_noop;
|
||||
out = fn ? fn : (void *)__fmt_noop;
|
||||
|
||||
while (*format) {
|
||||
if (*format != '%') {
|
||||
|
|
|
@ -50,5 +50,5 @@ int ftw(const char *dirpath,
|
|||
/* The following cast assumes that calling a function with one
|
||||
* argument more than it needs behaves as expected. This is
|
||||
* actually undefined, but works on all real-world machines. */
|
||||
return nftw(dirpath, (int (*)())fn, fd_limit, FTW_PHYS);
|
||||
return nftw(dirpath, (void *)fn, fd_limit, FTW_PHYS);
|
||||
}
|
||||
|
|
|
@ -84,6 +84,7 @@ static const char *FindNameById(const struct IdName *names, unsigned long id) {
|
|||
}
|
||||
|
||||
static void PrintDependencies(const char *prologue) {
|
||||
#ifdef __x86_64__
|
||||
struct NtLinkedList *head = &NtGetPeb()->Ldr->InLoadOrderModuleList;
|
||||
struct NtLinkedList *ldr = head->Next;
|
||||
do {
|
||||
|
@ -92,6 +93,7 @@ static void PrintDependencies(const char *prologue) {
|
|||
PRINT(" ☼ %.*!hs (%'zukb @ %p)", dll->FullDllName.Length,
|
||||
dll->FullDllName.Data, dll->SizeOfImage / 1024, dll->DllBase);
|
||||
} while ((ldr = ldr->Next) && ldr != head);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void Print(const char *prologue) {
|
||||
|
@ -624,6 +626,7 @@ textstartup void __printargs(const char *prologue) {
|
|||
if (GetConsoleMode(GetStdHandle(kNtStdErrorHandle), &cm))
|
||||
PRINT(" %s", DescribeNtConsoleOutFlags(cm));
|
||||
|
||||
#ifdef __x86_64__
|
||||
PRINT("");
|
||||
PRINT("TEB");
|
||||
PRINT(" ☼ gs:0x%02x %s = %p", 0x00, "NtGetSeh()", _NtGetSeh());
|
||||
|
@ -640,6 +643,7 @@ textstartup void __printargs(const char *prologue) {
|
|||
PRINT(" ☼ gs:0x%02x %s = %p", 0x58, "NtGetTls()", _NtGetTls());
|
||||
PRINT(" ☼ gs:0x%02x %s = %p", 0x60, "NtGetPeb()", NtGetPeb());
|
||||
PRINT(" ☼ gs:0x%02x %s = %p", 0x68, "NtGetErr()", NtGetErr());
|
||||
#endif
|
||||
|
||||
PRINT("");
|
||||
PRINT("DEPENDENCIES");
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
*
|
||||
* If RDSEED isn't available, we'll try RDRAND (which we automatically
|
||||
* disable for microarchitectures where it's known to be slow or buggy).
|
||||
* If RDRAND isn't available then we try getrandom(), RtlGenRandom(), or
|
||||
* If RDRAND isn't available then we try getrandom(), ProcessPrng(), or
|
||||
* sysctl(KERN_ARND). If those aren't available then we try /dev/urandom
|
||||
* and if that fails, we use RDTSC and getpid().
|
||||
*
|
||||
|
|
|
@ -45,9 +45,6 @@ dontasan void *rngset(void *b, size_t n, uint64_t seed(void), size_t reseed) {
|
|||
size_t m;
|
||||
uint64_t x, t = 0;
|
||||
unsigned char *p = b;
|
||||
if (IsAsan()) {
|
||||
__asan_verify(b, n);
|
||||
}
|
||||
if (!seed) {
|
||||
t = reseed;
|
||||
reseed = -1;
|
||||
|
|
|
@ -90,7 +90,7 @@ int fsetpos(FILE *, const fpos_t *) libcesque paramsnonnull();
|
|||
FILE *tmpfile(void) libcesque __wur;
|
||||
char *tmpnam(char *) libcesque __wur;
|
||||
char *tmpnam_r(char *) libcesque __wur;
|
||||
int system(const char *) libcesque;
|
||||
|
||||
FILE *popen(const char *, const char *) libcesque;
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -35,7 +34,6 @@ static char g_tmpnam[L_tmpnam];
|
|||
* is only mutated on success
|
||||
*/
|
||||
char *tmpnam(char *buf) {
|
||||
if (IsAsan()) __asan_verify(buf, L_tmpnam);
|
||||
char path[] = P_tmpdir "/tmpnam_XXXXXX";
|
||||
for (int t = 0; t < 100; ++t) {
|
||||
int w = _rand64();
|
||||
|
|
|
@ -88,7 +88,7 @@ o/$(MODE)/libc/str/windowstimetotimespec.o: private \
|
|||
-O2
|
||||
|
||||
$(LIBC_STR_A_OBJS): private \
|
||||
COPTS += \
|
||||
CFLAGS += \
|
||||
-fno-sanitize=all \
|
||||
-Wframe-larger-than=4096 \
|
||||
-Walloca-larger-than=4096
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdckdint.h"
|
||||
|
@ -28,11 +27,6 @@ void djbsort_avx2(int32_t *, long);
|
|||
* D.J. Bernstein's outrageously fast integer sorting algorithm.
|
||||
*/
|
||||
void djbsort(int32_t *a, size_t n) {
|
||||
size_t m;
|
||||
if (IsAsan()) {
|
||||
if (ckd_mul(&m, n, 4)) m = -1;
|
||||
__asan_verify(a, m);
|
||||
}
|
||||
if (n > 1) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
if (X86_HAVE(AVX2)) {
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/likely.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
|
@ -53,7 +52,6 @@ bool32 isutf8(const void *data, size_t size) {
|
|||
long c;
|
||||
const char *p, *e;
|
||||
if (size == -1) size = data ? strlen(data) : 0;
|
||||
if (IsAsan()) __asan_verify(data, size);
|
||||
p = data;
|
||||
e = p + size;
|
||||
while (p < e) {
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/likely.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
|
@ -32,16 +31,14 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
|||
* @param needlelen is its character count
|
||||
* @return pointer to first result or NULL if not found
|
||||
*/
|
||||
void *memmem(const void *haystack, size_t haystacklen,
|
||||
const void *needle, size_t needlelen) {
|
||||
__vex void *memmem(const void *haystack, size_t haystacklen, const void *needle,
|
||||
size_t needlelen) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
char c;
|
||||
xmm_t n;
|
||||
const xmm_t *v;
|
||||
unsigned i, k, m;
|
||||
const char *p, *q, *e;
|
||||
if (IsAsan()) __asan_verify(needle, needlelen);
|
||||
if (IsAsan()) __asan_verify(haystack, haystacklen);
|
||||
if (!needlelen) return (void *)haystack;
|
||||
if (UNLIKELY(needlelen > haystacklen)) return 0;
|
||||
q = needle;
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -36,8 +35,8 @@ static inline const char16_t *memrchr16_pure(const char16_t *s, char16_t c,
|
|||
}
|
||||
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
static inline const char16_t *memrchr16_sse(const char16_t *s,
|
||||
char16_t c, size_t n) {
|
||||
static inline const char16_t *memrchr16_sse(const char16_t *s, char16_t c,
|
||||
size_t n) {
|
||||
size_t i;
|
||||
unsigned m;
|
||||
xmm_t v, t = {c, c, c, c, c, c, c, c};
|
||||
|
@ -67,11 +66,10 @@ static inline const char16_t *memrchr16_sse(const char16_t *s,
|
|||
* @return is pointer to first instance of c or NULL if not found
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void *memrchr16(const void *s, int c, size_t n) {
|
||||
__vex void *memrchr16(const void *s, int c, size_t n) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
const void *r;
|
||||
if (!IsTiny() && X86_HAVE(SSE)) {
|
||||
if (IsAsan()) __asan_verify(s, n * 2);
|
||||
r = memrchr16_sse(s, c, n);
|
||||
} else {
|
||||
r = memrchr16_pure(s, c, n);
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
|
@ -33,8 +32,7 @@ static inline const unsigned char *rawmemchr_pure(const unsigned char *s,
|
|||
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
||||
static inline const char *rawmemchr_sse(const char *s,
|
||||
unsigned char c) {
|
||||
static inline const char *rawmemchr_sse(const char *s, unsigned char c) {
|
||||
unsigned k;
|
||||
unsigned m;
|
||||
const xmm_t *p;
|
||||
|
@ -67,11 +65,10 @@ static inline uint64_t UncheckedAlignedRead64(const unsigned char *p) {
|
|||
* @param c is search byte which is masked with 255
|
||||
* @return is pointer to first instance of c
|
||||
*/
|
||||
void *rawmemchr(const void *s, int c) {
|
||||
__vex void *rawmemchr(const void *s, int c) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
const void *r;
|
||||
if (X86_HAVE(SSE)) {
|
||||
if (IsAsan()) __asan_verify(s, 1);
|
||||
r = rawmemchr_sse(s, c);
|
||||
} else {
|
||||
r = rawmemchr_pure(s, c);
|
||||
|
|
|
@ -167,7 +167,7 @@ wint_t towctrans(wint_t, wctrans_t) libcesque;
|
|||
|
||||
int getsubopt(char **, char *const *, char **) libcesque paramsnonnull();
|
||||
char *strsignal(int) returnsnonnull libcesque;
|
||||
char *strerror(int) returnsnonnull dontthrow nocallback;
|
||||
char *strerror(int) returnsnonnull dontthrow dontcallback;
|
||||
errno_t strerror_r(int, char *, size_t) libcesque;
|
||||
char *__xpg_strerror_r(int, char *, size_t) libcesque;
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tab.internal.h"
|
||||
|
||||
|
@ -34,8 +33,6 @@ int strcasecmp(const char *a, const char *b) {
|
|||
size_t i = 0;
|
||||
uint64_t v, w;
|
||||
if (a == b) return 0;
|
||||
if (IsAsan()) __asan_verify_str(a);
|
||||
if (IsAsan()) __asan_verify_str(b);
|
||||
if (((uintptr_t)a & 7) == ((uintptr_t)b & 7)) {
|
||||
for (; (uintptr_t)(a + i) & 7; ++i) {
|
||||
CheckEm:
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/str/tab.internal.h"
|
||||
|
||||
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
||||
|
@ -35,15 +34,13 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
|||
* @asyncsignalsafe
|
||||
* @see strstr()
|
||||
*/
|
||||
char *strcasestr(const char *haystack, const char *needle) {
|
||||
__vex char *strcasestr(const char *haystack, const char *needle) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
char c;
|
||||
size_t i;
|
||||
unsigned k, m;
|
||||
const xmm_t *p;
|
||||
xmm_t v, n1, n2, z = {0};
|
||||
if (IsAsan()) __asan_verify(needle, 1);
|
||||
if (IsAsan()) __asan_verify(haystack, 1);
|
||||
if (haystack == needle || !*needle) return (char *)haystack;
|
||||
c = *needle;
|
||||
n1 = (xmm_t){c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
typedef char16_t xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
||||
|
@ -29,17 +28,15 @@ typedef char16_t xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
|||
* @return number of shorts (excluding NUL)
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
size_t strlen16(const char16_t *s) {
|
||||
__vex size_t strlen16(const char16_t *s) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
size_t n;
|
||||
xmm_t z = {0};
|
||||
unsigned m, k = (uintptr_t)s & 15;
|
||||
const xmm_t *p = (const xmm_t *)((uintptr_t)s & -16);
|
||||
if (IsAsan()) __asan_verify(s, 2);
|
||||
m = __builtin_ia32_pmovmskb128(*p == z) >> k << k;
|
||||
while (!m) m = __builtin_ia32_pmovmskb128(*++p == z);
|
||||
n = (const char16_t *)p + (__builtin_ctzl(m) >> 1) - s;
|
||||
if (IsAsan()) __asan_verify(s, n * 2);
|
||||
return n;
|
||||
#else
|
||||
size_t n = 0;
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
static size_t strnlen_s_x64(const char *s, size_t n, size_t i) {
|
||||
|
@ -48,7 +47,6 @@ static size_t strnlen_s_x64(const char *s, size_t n, size_t i) {
|
|||
size_t strnlen_s(const char *s, size_t n) {
|
||||
size_t i;
|
||||
if (!s) return 0;
|
||||
if (IsAsan()) __asan_verify(s, n);
|
||||
for (i = 0; (uintptr_t)(s + i) & 7; ++i) {
|
||||
if (i == n || !s[i]) return i;
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
|
||||
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
||||
|
||||
|
@ -35,14 +34,12 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
|||
* @see strcasestr()
|
||||
* @see memmem()
|
||||
*/
|
||||
char *strstr(const char *haystack, const char *needle) {
|
||||
__vex char *strstr(const char *haystack, const char *needle) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
size_t i;
|
||||
unsigned k, m;
|
||||
const xmm_t *p;
|
||||
xmm_t v, n, z = {0};
|
||||
if (IsAsan()) __asan_verify(needle, 1);
|
||||
if (IsAsan()) __asan_verify(haystack, 1);
|
||||
if (haystack == needle || !*needle) return (char *)haystack;
|
||||
n = (xmm_t){*needle, *needle, *needle, *needle, *needle, *needle,
|
||||
*needle, *needle, *needle, *needle, *needle, *needle,
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
typedef wchar_t xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
||||
|
@ -29,17 +28,15 @@ typedef wchar_t xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
|||
* @return number of wide characters (excluding NUL)
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
size_t wcslen(const wchar_t *s) {
|
||||
__vex size_t wcslen(const wchar_t *s) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
size_t n;
|
||||
xmm_t z = {0};
|
||||
unsigned m, k = (uintptr_t)s & 15;
|
||||
const xmm_t *p = (const xmm_t *)((uintptr_t)s & -16);
|
||||
if (IsAsan()) __asan_verify(s, 4);
|
||||
m = __builtin_ia32_pmovmskb128(*p == z) >> k << k;
|
||||
while (!m) m = __builtin_ia32_pmovmskb128(*++p == z);
|
||||
n = (const wchar_t *)p + (__builtin_ctzl(m) >> 2) - s;
|
||||
if (IsAsan()) __asan_verify(s, n);
|
||||
return n;
|
||||
#else
|
||||
size_t n = 0;
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/stdckdint.h"
|
||||
|
@ -38,7 +37,7 @@ static inline const wchar_t *wmemrchr_pure(const wchar_t *s, wchar_t c,
|
|||
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
static inline const wchar_t *wmemrchr_sse(const wchar_t *s, wchar_t c,
|
||||
size_t n) {
|
||||
size_t n) {
|
||||
size_t i;
|
||||
unsigned m;
|
||||
xmm_t v, t = {c, c, c, c};
|
||||
|
@ -68,16 +67,9 @@ static inline const wchar_t *wmemrchr_sse(const wchar_t *s, wchar_t c,
|
|||
* @return is pointer to first instance of c or NULL if not found
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void *wmemrchr(const wchar_t *s, wchar_t c, size_t n) {
|
||||
__vex void *wmemrchr(const wchar_t *s, wchar_t c, size_t n) {
|
||||
#if defined(__x86_64__) && !defined(__chibicc__)
|
||||
size_t bytes;
|
||||
const void *r;
|
||||
if (IsAsan()) {
|
||||
if (ckd_mul(&bytes, n, sizeof(wchar_t))) bytes = -1;
|
||||
__asan_verify(s, bytes);
|
||||
}
|
||||
r = wmemrchr_sse(s, c, n);
|
||||
return (void *)r;
|
||||
return (void *)wmemrchr_sse(s, c, n);
|
||||
#else
|
||||
return (void *)wmemrchr_pure(s, c, n);
|
||||
#endif
|
||||
|
|
|
@ -234,7 +234,9 @@ syscon mmap MAP_INHERIT -1 -1 -1 -1 -1 -1 0x00000080 -1 # make
|
|||
syscon mmap MAP_HASSEMAPHORE 0 0 0x00000200 0x00000200 0x00000200 0 0x00000200 0 # does it matter on x86?
|
||||
syscon mmap MAP_NOSYNC 0 0 0 0 0x00000800 0 0 0 # flush to physical media only when necessary rather than gratuitously; be sure to use write() rather than ftruncate() with this!
|
||||
syscon mmap MAP_CONCEAL 0 0 0 0 0x00020000 0x00008000 0x00008000 0 # omit from core dumps; MAP_NOCORE on FreeBSD
|
||||
syscon mmap MAP_JIT 0 0 0 0x00000800 0 0 0 0 # omit from core dumps; MAP_NOCORE on FreeBSD
|
||||
syscon mmap MAP_JIT 0 0 0 0x00000800 0 0 0 0 # allocate region used for just-in-time compilation
|
||||
syscon mmap MAP_NOCACHE 0 0 0x00000400 0x00000400 0 0 0 0 # don't cache pages for this mapping
|
||||
syscon mmap MAP_NOEXTEND 0 0 0x00000100 0x00000100 0 0 0 0 # for MAP_FILE, don't change file size
|
||||
syscon compat MAP_NOCORE 0 0 0 0 0x00020000 0x00008000 0x00008000 0 # use MAP_CONCEAL
|
||||
syscon compat MAP_ANON 0x00000020 0x00000020 0x00001000 0x00001000 0x00001000 0x00001000 0x00001000 0x00000020 # bsd consensus; faked nt
|
||||
syscon compat MAP_EXECUTABLE 0x00001000 0x00001000 0 0 0 0 0 0 # ignored
|
||||
|
@ -269,17 +271,18 @@ syscon madv MADV_WIPEONFORK 18 18 127 127 127 127 127 127 # T
|
|||
syscon madv MADV_KEEPONFORK 19 19 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_COLD 20 20 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_PAGEOUT 21 21 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_POPULATE_READ 22 22 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_POPULATE_WRITE 23 23 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_DONTNEED_LOCKED 24 24 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_POPULATE_READ 22 22 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_POPULATE_WRITE 23 23 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_DONTNEED_LOCKED 24 24 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_COLLAPSE 25 25 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_DOFORK 11 11 127 127 127 127 127 127 # TODO(jart): what is it?
|
||||
syscon madv MADV_DONTDUMP 16 16 127 127 127 127 127 127 # see MAP_CONCEAL in OpenBSD; TODO(jart): what is it?
|
||||
syscon madv MADV_DONTFORK 10 10 127 127 127 127 127 127 # TODO(jart): what is it?
|
||||
syscon madv MADV_HWPOISON 100 100 127 127 127 127 127 127 # TODO(jart): what is it?
|
||||
syscon madv MADV_SOFT_OFFLINE 101 101 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_SOFT_OFFLINE 101 101 127 127 127 127 127 127 # TODO: add support ?
|
||||
syscon madv MADV_REMOVE 9 9 127 127 127 127 127 127 # TODO(jart): what is it?
|
||||
syscon fadv POSIX_FADV_NOREUSE 5 5 127 127 5 127 5 127 # wut
|
||||
syscon madv MADV_REMOVE 9 9 127 127 127 127 127 127 # TODO(jart): what is it?
|
||||
|
||||
# mmap(), mprotect(), etc.
|
||||
# digital restrictions management for the people
|
||||
|
@ -579,19 +582,19 @@ syscon clock CLOCK_REALTIME 0 0 0 0 0 0 0 0 # consensus
|
|||
syscon clock CLOCK_REALTIME_PRECISE 0 0 0 0 9 0 0 0 #
|
||||
syscon clock CLOCK_REALTIME_FAST 0 0 0 0 10 0 0 0 #
|
||||
syscon clock CLOCK_REALTIME_COARSE 5 5 0 0 10 0 0 2 # Linux 2.6.32+; bsd consensus; not available on RHEL5
|
||||
syscon clock CLOCK_MONOTONIC 1 1 1 6 4 3 3 1 # XNU/NT faked; could move backwards if NTP introduces negative leap second
|
||||
syscon clock CLOCK_MONOTONIC_PRECISE 1 1 1 6 11 3 3 1 #
|
||||
syscon clock CLOCK_MONOTONIC_FAST 1 1 1 6 12 3 3 1 #
|
||||
syscon clock CLOCK_MONOTONIC_COARSE 6 6 1 6 12 3 3 1 # Linux 2.6.32+; bsd consensus; not available on RHEL5
|
||||
syscon clock CLOCK_MONOTONIC_RAW 4 4 127 4 127 127 127 127 # actually monotonic; not subject to NTP adjustments; Linux 2.6.28+; XNU/NT/FreeBSD/OpenBSD faked; not available on RHEL5
|
||||
syscon clock CLOCK_PROCESS_CPUTIME_ID 2 2 127 12 15 2 0x40000000 127 # NetBSD lets you bitwise a PID into clockid_t
|
||||
syscon clock CLOCK_THREAD_CPUTIME_ID 3 3 127 16 14 4 0x20000000 127 #
|
||||
syscon clock CLOCK_MONOTONIC 1 1 6 6 4 3 3 1 # XNU/NT faked; could move backwards if NTP introduces negative leap second
|
||||
syscon clock CLOCK_MONOTONIC_PRECISE 1 1 6 6 11 3 3 1 #
|
||||
syscon clock CLOCK_MONOTONIC_FAST 1 1 6 6 12 3 3 1 #
|
||||
syscon clock CLOCK_MONOTONIC_COARSE 6 6 5 5 12 3 3 1 # Linux 2.6.32+; bsd consensus; not available on RHEL5
|
||||
syscon clock CLOCK_MONOTONIC_RAW 4 4 4 4 127 127 127 127 # actually monotonic; not subject to NTP adjustments; Linux 2.6.28+; XNU/NT/FreeBSD/OpenBSD faked; not available on RHEL5
|
||||
syscon clock CLOCK_PROCESS_CPUTIME_ID 2 2 12 12 15 2 0x40000000 4 # NetBSD lets you bitwise a PID into clockid_t
|
||||
syscon clock CLOCK_THREAD_CPUTIME_ID 3 3 16 16 14 4 0x20000000 5 #
|
||||
syscon clock CLOCK_PROF 127 127 127 127 2 127 2 127 #
|
||||
syscon clock CLOCK_BOOTTIME 7 7 7 127 127 6 127 3 #
|
||||
syscon clock CLOCK_REALTIME_ALARM 8 8 127 127 127 127 127 127 #
|
||||
syscon clock CLOCK_BOOTTIME_ALARM 9 9 127 127 127 127 127 127 #
|
||||
syscon clock CLOCK_TAI 11 11 127 127 127 127 127 127 #
|
||||
syscon clock CLOCK_UPTIME 127 127 127 127 5 5 127 127 #
|
||||
syscon clock CLOCK_UPTIME 127 127 8 8 5 5 127 127 #
|
||||
syscon clock CLOCK_UPTIME_PRECISE 127 127 127 127 7 127 127 127 #
|
||||
syscon clock CLOCK_UPTIME_FAST 127 127 127 127 8 127 127 127 #
|
||||
syscon clock CLOCK_SECOND 127 127 127 127 13 127 127 127 #
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon clock,CLOCK_MONOTONIC,1,1,1,6,4,3,3,1
|
||||
.syscon clock,CLOCK_MONOTONIC,1,1,6,6,4,3,3,1
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon clock,CLOCK_MONOTONIC_COARSE,6,6,1,6,12,3,3,1
|
||||
.syscon clock,CLOCK_MONOTONIC_COARSE,6,6,5,5,12,3,3,1
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon clock,CLOCK_MONOTONIC_FAST,1,1,1,6,12,3,3,1
|
||||
.syscon clock,CLOCK_MONOTONIC_FAST,1,1,6,6,12,3,3,1
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon clock,CLOCK_MONOTONIC_PRECISE,1,1,1,6,11,3,3,1
|
||||
.syscon clock,CLOCK_MONOTONIC_PRECISE,1,1,6,6,11,3,3,1
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon clock,CLOCK_MONOTONIC_RAW,4,4,127,4,127,127,127,127
|
||||
.syscon clock,CLOCK_MONOTONIC_RAW,4,4,4,4,127,127,127,127
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon clock,CLOCK_PROCESS_CPUTIME_ID,2,2,127,12,15,2,0x40000000,127
|
||||
.syscon clock,CLOCK_PROCESS_CPUTIME_ID,2,2,12,12,15,2,0x40000000,4
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon clock,CLOCK_THREAD_CPUTIME_ID,3,3,127,16,14,4,0x20000000,127
|
||||
.syscon clock,CLOCK_THREAD_CPUTIME_ID,3,3,16,16,14,4,0x20000000,5
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon clock,CLOCK_UPTIME,127,127,127,127,5,5,127,127
|
||||
.syscon clock,CLOCK_UPTIME,127,127,8,8,5,5,127,127
|
||||
|
|
2
libc/sysv/consts/MAP_NOCACHE.S
Normal file
2
libc/sysv/consts/MAP_NOCACHE.S
Normal file
|
@ -0,0 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon mmap,MAP_NOCACHE,0,0,0x00000400,0x00000400,0,0,0,0
|
2
libc/sysv/consts/MAP_NOEXTEND.S
Normal file
2
libc/sysv/consts/MAP_NOEXTEND.S
Normal file
|
@ -0,0 +1,2 @@
|
|||
#include "libc/sysv/consts/syscon.internal.h"
|
||||
.syscon mmap,MAP_NOEXTEND,0,0,0x00000100,0x00000100,0,0,0,0
|
|
@ -16,6 +16,8 @@ extern const int MAP_HASSEMAPHORE;
|
|||
extern const int MAP_INHERIT;
|
||||
extern const int MAP_JIT;
|
||||
extern const int MAP_LOCKED;
|
||||
extern const int MAP_NOCACHE;
|
||||
extern const int MAP_NOEXTEND;
|
||||
extern const int MAP_NONBLOCK;
|
||||
extern const int MAP_NORESERVE;
|
||||
extern const int MAP_NOSYNC;
|
||||
|
@ -40,6 +42,7 @@ COSMOPOLITAN_C_END_
|
|||
#define MAP_FIXED_NOREPLACE MAP_FIXED_NOREPLACE
|
||||
#define MAP_HASSEMAPHORE MAP_HASSEMAPHORE
|
||||
#define MAP_POPULATE MAP_POPULATE
|
||||
#define MAP_NORESERVE MAP_NORESERVE
|
||||
|
||||
#define MAP_ANON MAP_ANONYMOUS
|
||||
#define MAP_NOCORE MAP_CONCEAL
|
||||
|
|
|
@ -42,7 +42,7 @@ double __testlib_ezbenchcontrol(void) {
|
|||
} while (++Tries < 10 && (__testlib_getcore() != Core &&
|
||||
__testlib_getinterrupts() > Interrupts));
|
||||
if (Tries == 10) {
|
||||
tinyprint(2, "warning: failed to accurately benchmark control\n");
|
||||
tinyprint(2, "warning: failed to accurately benchmark control\n", NULL);
|
||||
}
|
||||
strcpy(host, "unknown");
|
||||
gethostname(host, 64);
|
||||
|
|
|
@ -101,7 +101,7 @@ static void testlib_showerror_(int line, //
|
|||
_weaken(kvprintf)(fmt, va);
|
||||
tinyprint(2, "\n", NULL);
|
||||
} else {
|
||||
tinyprint(2, "\t[missing kvprintf]\n");
|
||||
tinyprint(2, "\t[missing kvprintf]\n", NULL);
|
||||
}
|
||||
}
|
||||
tinyprint(2, "\t", SUBTLE, strerror(e), RESET, "\n\t", SUBTLE,
|
||||
|
|
|
@ -167,22 +167,22 @@ void TearDownOnce(void);
|
|||
#define ASSERT_IN(NEEDLE, GOT) \
|
||||
assertContains(FILIFU sizeof(*(NEEDLE)), NEEDLE, GOT, #GOT, true)
|
||||
|
||||
#define ASSERT_BINEQ(WANT, GOT) \
|
||||
_Generic((WANT)[0], char \
|
||||
: assertBinaryEquals_hex, default \
|
||||
: assertBinaryEquals_cp437)(FILIFU WANT, GOT, -1, #GOT, true)
|
||||
#define ASSERT_BINNE(NOPE, GOT) \
|
||||
_Generic((NOPE)[0], char \
|
||||
: assertBinaryNotEquals_hex, default \
|
||||
: assertBinaryNotEquals_cp437)(FILIFU NOPE, GOT, -1, #GOT, true)
|
||||
#define ASSERT_BINEQN(WANT, GOT, N) \
|
||||
_Generic((WANT)[0], char \
|
||||
: assertBinaryEquals_hex, default \
|
||||
: assertBinaryEquals_cp437)(FILIFU WANT, GOT, N, #GOT, true)
|
||||
#define ASSERT_BINNEN(NOPE, GOT, N) \
|
||||
_Generic((NOPE)[0], char \
|
||||
: assertBinaryNotEquals_hex, default \
|
||||
: assertBinaryNotEquals_cp437)(FILIFU NOPE, GOT, -1, #GOT, true)
|
||||
#define ASSERT_BINEQ(WANT, GOT) \
|
||||
_Generic((WANT)[0], \
|
||||
char: assertBinaryEquals_hex, \
|
||||
default: assertBinaryEquals_cp437)(FILIFU WANT, GOT, -1, #GOT, true)
|
||||
#define ASSERT_BINNE(NOPE, GOT) \
|
||||
_Generic((NOPE)[0], \
|
||||
char: assertBinaryNotEquals_hex, \
|
||||
default: assertBinaryNotEquals_cp437)(FILIFU NOPE, GOT, -1, #GOT, true)
|
||||
#define ASSERT_BINEQN(WANT, GOT, N) \
|
||||
_Generic((WANT)[0], \
|
||||
char: assertBinaryEquals_hex, \
|
||||
default: assertBinaryEquals_cp437)(FILIFU WANT, GOT, N, #GOT, true)
|
||||
#define ASSERT_BINNEN(NOPE, GOT, N) \
|
||||
_Generic((NOPE)[0], \
|
||||
char: assertBinaryNotEquals_hex, \
|
||||
default: assertBinaryNotEquals_cp437)(FILIFU NOPE, GOT, -1, #GOT, true)
|
||||
|
||||
#define ASSERT_FLOAT_EQ(WANT, GOT) \
|
||||
assertLongDoubleEquals(FILIFU WANT, GOT, #GOT, true)
|
||||
|
@ -243,22 +243,22 @@ void TearDownOnce(void);
|
|||
#define EXPECT_IN(NEEDLE, GOT) \
|
||||
assertContains(FILIFU sizeof(*(NEEDLE)), NEEDLE, GOT, #GOT, false)
|
||||
|
||||
#define EXPECT_BINEQ(WANT, GOT) \
|
||||
_Generic((WANT)[0], char \
|
||||
: assertBinaryEquals_hex, default \
|
||||
: assertBinaryEquals_cp437)(FILIFU WANT, GOT, -1, #GOT, false)
|
||||
#define EXPECT_BINNE(NOPE, GOT) \
|
||||
_Generic((NOPE)[0], char \
|
||||
: assertBinaryNotEquals_hex, default \
|
||||
: assertBinaryNotEquals_cp437)(FILIFU NOPE, GOT, -1, #GOT, false)
|
||||
#define EXPECT_BINEQN(WANT, GOT, N) \
|
||||
_Generic((WANT)[0], char \
|
||||
: assertBinaryEquals_hex, default \
|
||||
: assertBinaryEquals_cp437)(FILIFU WANT, GOT, N, #GOT, false)
|
||||
#define EXPECT_BINNEN(NOPE, GOT, N) \
|
||||
_Generic((NOPE)[0], char \
|
||||
: assertBinaryNotEquals_hex, default \
|
||||
: assertBinaryNotEquals_cp437)(FILIFU NOPE, GOT, -1, #GOT, false)
|
||||
#define EXPECT_BINEQ(WANT, GOT) \
|
||||
_Generic((WANT)[0], \
|
||||
char: assertBinaryEquals_hex, \
|
||||
default: assertBinaryEquals_cp437)(FILIFU WANT, GOT, -1, #GOT, false)
|
||||
#define EXPECT_BINNE(NOPE, GOT) \
|
||||
_Generic((NOPE)[0], \
|
||||
char: assertBinaryNotEquals_hex, \
|
||||
default: assertBinaryNotEquals_cp437)(FILIFU NOPE, GOT, -1, #GOT, false)
|
||||
#define EXPECT_BINEQN(WANT, GOT, N) \
|
||||
_Generic((WANT)[0], \
|
||||
char: assertBinaryEquals_hex, \
|
||||
default: assertBinaryEquals_cp437)(FILIFU WANT, GOT, N, #GOT, false)
|
||||
#define EXPECT_BINNEN(NOPE, GOT, N) \
|
||||
_Generic((NOPE)[0], \
|
||||
char: assertBinaryNotEquals_hex, \
|
||||
default: assertBinaryNotEquals_cp437)(FILIFU NOPE, GOT, -1, #GOT, false)
|
||||
|
||||
#define EXPECT_FLOAT_EQ(WANT, GOT) \
|
||||
assertLongDoubleEquals(FILIFU WANT, GOT, #GOT, false)
|
||||
|
|
|
@ -30,7 +30,8 @@
|
|||
typedef double vect __attribute__((__vector_size__(16), __aligned__(16)));
|
||||
|
||||
struct Gadget {
|
||||
void (*func)();
|
||||
void (*func)(long, long, long, long, long, long, //
|
||||
vect, vect, vect, vect, vect, vect);
|
||||
long longs[6];
|
||||
vect vects[6];
|
||||
};
|
||||
|
@ -89,7 +90,7 @@ static void runcontext(struct Gadget *call, ucontext_t *link) {
|
|||
* @param argc is effectively ignored (see notes above)
|
||||
* @see setcontext(), getcontext(), swapcontext()
|
||||
*/
|
||||
void makecontext(ucontext_t *uc, void func(), int argc, ...) {
|
||||
void makecontext(ucontext_t *uc, void *func, int argc, ...) {
|
||||
va_list va;
|
||||
long sp, sb;
|
||||
struct Gadget *call;
|
||||
|
|
|
@ -39,7 +39,7 @@ struct CosmoTib {
|
|||
void **tib_keys;
|
||||
void *tib_nsync;
|
||||
void *tib_todo[7];
|
||||
};
|
||||
} __attribute__((__aligned__(64)));
|
||||
|
||||
extern int __threaded;
|
||||
extern char __tls_morphed;
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
│ │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
|
||||
asm(".ident\t\"\\n\\n\
|
||||
Musl libc (MIT License)\\n\
|
||||
|
@ -92,41 +93,51 @@ static void mul(uint64_t *hi, uint64_t *lo, uint64_t x, uint64_t y)
|
|||
*/
|
||||
double fma(double x, double y, double z)
|
||||
{
|
||||
#if defined(__x86_64__) && defined(__FMA__) && defined(__FAST_MATH__)
|
||||
#if defined(__x86_64__) && defined(__FMA__)
|
||||
|
||||
// Intel Haswell+ (c. 2013)
|
||||
// AMD Piledriver+ (c. 2011)
|
||||
asm("vfmadd132sd\t%1,%2,%0" : "+x"(x) : "x"(y), "x"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__x86_64__) && defined(__FMA4__) && defined(__FAST_MATH__)
|
||||
#elif defined(__x86_64__) && defined(__FMA4__)
|
||||
|
||||
// AMD Bulldozer+ (c. 2011)
|
||||
asm("vfmaddsd\t%3,%2,%1,%0" : "=x"(x) : "x"(x), "x"(y), "x"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__aarch64__) && defined(__FAST_MATH__)
|
||||
#elif defined(__aarch64__)
|
||||
|
||||
asm("fmadd\t%d0,%d1,%d2,%d3" : "=w"(x) : "w"(x), "w"(y), "w"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__powerpc64__) && defined(__FAST_MATH__)
|
||||
#elif defined(__powerpc64__)
|
||||
|
||||
asm("fmadd\t%0,%1,%2,%3" : "=d"(x) : "d"(x), "d"(y), "d"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__riscv) && __riscv_flen >= 64 && defined(__FAST_MATH__)
|
||||
#elif defined(__riscv) && __riscv_flen >= 64
|
||||
|
||||
asm("fmadd.d\t%0,%1,%2,%3" : "=f"(x) : "f"(x), "f"(y), "f"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__s390x__) && defined(__FAST_MATH__)
|
||||
#elif defined(__s390x__)
|
||||
|
||||
asm("madbr\t%0,\t%1,\t%2" : "+f"(z) : "f"(x), "f"(y));
|
||||
return z;
|
||||
|
||||
#else
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
/* #pragma STDC FENV_ACCESS ON */
|
||||
|
||||
#ifdef __x86_64__
|
||||
if (X86_HAVE(FMA)) {
|
||||
asm("vfmadd132sd\t%1,%2,%0" : "+x"(x) : "x"(y), "x"(z));
|
||||
return x;
|
||||
} else if (X86_HAVE(FMA4)) {
|
||||
asm("vfmaddsd\t%3,%2,%1,%0" : "=x"(x) : "x"(x), "x"(y), "x"(z));
|
||||
return x;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* normalize so top 10bits and last bit are 0 */
|
||||
struct num nx, ny, nz;
|
||||
|
@ -268,3 +279,7 @@ double fma(double x, double y, double z)
|
|||
|
||||
#endif /* __x86_64__ */
|
||||
}
|
||||
|
||||
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
||||
__weak_reference(fma, fmal);
|
||||
#endif
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
│ │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/runtime/fenv.h"
|
||||
|
||||
asm(".ident\t\"\\n\\n\
|
||||
|
@ -110,7 +111,7 @@ float fmaf(float x, float y, float z)
|
|||
so direct double-precision arithmetic suffices, except where
|
||||
double rounding occurs. */
|
||||
|
||||
/* #pragma STDC FENV_ACCESS ON */
|
||||
/* #pragma STDC FENV_ACCESS ON */
|
||||
double xy, result;
|
||||
union {double f; uint64_t i;} u;
|
||||
int e;
|
||||
|
|
287
libc/tinymath/fmal.c
Normal file
287
libc/tinymath/fmal.c
Normal file
|
@ -0,0 +1,287 @@
|
|||
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set noet ft=c ts=8 sw=8 fenc=utf-8 :vi │
|
||||
╚──────────────────────────────────────────────────────────────────────────────╝
|
||||
│ │
|
||||
│ Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG> │
|
||||
│ All rights reserved. │
|
||||
│ │
|
||||
│ Redistribution and use in source and binary forms, with or without │
|
||||
│ modification, are permitted provided that the following conditions │
|
||||
│ are met: │
|
||||
│ 1. Redistributions of source code must retain the above copyright │
|
||||
│ notice, this list of conditions and the following disclaimer. │
|
||||
│ 2. Redistributions in binary form must reproduce the above copyright │
|
||||
│ notice, this list of conditions and the following disclaimer in the │
|
||||
│ documentation and/or other materials provided with the distribution. │
|
||||
│ │
|
||||
│ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND │
|
||||
│ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE │
|
||||
│ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE │
|
||||
│ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE │
|
||||
│ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL │
|
||||
│ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS │
|
||||
│ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) │
|
||||
│ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT │
|
||||
│ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY │
|
||||
│ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF │
|
||||
│ SUCH DAMAGE. │
|
||||
│ │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/runtime/fenv.h"
|
||||
#include "libc/tinymath/freebsd.internal.h"
|
||||
#include "libc/tinymath/ldshape.internal.h"
|
||||
#if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
|
||||
|
||||
asm(".ident\t\"\\n\\n\
|
||||
FreeBSD libm (BSD-2 License)\\n\
|
||||
Copyright (c) 2005-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.\"");
|
||||
asm(".include \"libc/disclaimer.inc\"");
|
||||
// clang-format off
|
||||
|
||||
#if LDBL_MANT_DIG == 64
|
||||
#define LASTBIT(u) (u.i.m & 1)
|
||||
#define SPLIT (0x1p32L + 1)
|
||||
#elif LDBL_MANT_DIG == 113
|
||||
#define LASTBIT(u) (u.i.lo & 1)
|
||||
#define SPLIT (0x1p57L + 1)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* A struct dd represents a floating-point number with twice the precision
|
||||
* of a long double. We maintain the invariant that "hi" stores the high-order
|
||||
* bits of the result.
|
||||
*/
|
||||
struct dd {
|
||||
long double hi;
|
||||
long double lo;
|
||||
};
|
||||
|
||||
/*
|
||||
* Compute a+b exactly, returning the exact result in a struct dd. We assume
|
||||
* that both a and b are finite, but make no assumptions about their relative
|
||||
* magnitudes.
|
||||
*/
|
||||
static inline struct dd dd_add(long double a, long double b) {
|
||||
struct dd ret;
|
||||
long double s;
|
||||
ret.hi = a + b;
|
||||
s = ret.hi - a;
|
||||
ret.lo = (a - (ret.hi - s)) + (b - s);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute a+b, with a small tweak: The least significant bit of the
|
||||
* result is adjusted into a sticky bit summarizing all the bits that
|
||||
* were lost to rounding. This adjustment negates the effects of double
|
||||
* rounding when the result is added to another number with a higher
|
||||
* exponent. For an explanation of round and sticky bits, see any reference
|
||||
* on FPU design, e.g.,
|
||||
*
|
||||
* J. Coonen. An Implementation Guide to a Proposed Standard for
|
||||
* Floating-Point Arithmetic. Computer, vol. 13, no. 1, Jan 1980.
|
||||
*/
|
||||
static inline long double add_adjusted(long double a, long double b) {
|
||||
struct dd sum;
|
||||
union ldshape u;
|
||||
sum = dd_add(a, b);
|
||||
if (sum.lo != 0) {
|
||||
u.f = sum.hi;
|
||||
if (!LASTBIT(u)) sum.hi = nextafterl(sum.hi, INFINITY * sum.lo);
|
||||
}
|
||||
return (sum.hi);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute ldexp(a+b, scale) with a single rounding error. It is assumed
|
||||
* that the result will be subnormal, and care is taken to ensure that
|
||||
* double rounding does not occur.
|
||||
*/
|
||||
static inline long double add_and_denormalize(long double a, long double b,
|
||||
int scale) {
|
||||
struct dd sum;
|
||||
int bits_lost;
|
||||
union ldshape u;
|
||||
|
||||
sum = dd_add(a, b);
|
||||
|
||||
/*
|
||||
* If we are losing at least two bits of accuracy to denormalization,
|
||||
* then the first lost bit becomes a round bit, and we adjust the
|
||||
* lowest bit of sum.hi to make it a sticky bit summarizing all the
|
||||
* bits in sum.lo. With the sticky bit adjusted, the hardware will
|
||||
* break any ties in the correct direction.
|
||||
*
|
||||
* If we are losing only one bit to denormalization, however, we must
|
||||
* break the ties manually.
|
||||
*/
|
||||
if (sum.lo != 0) {
|
||||
u.f = sum.hi;
|
||||
bits_lost = -u.i.se - scale + 1;
|
||||
if ((bits_lost != 1) ^ LASTBIT(u))
|
||||
sum.hi = nextafterl(sum.hi, INFINITY * sum.lo);
|
||||
}
|
||||
return scalbnl(sum.hi, scale);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute a*b exactly, returning the exact result in a struct dd. We assume
|
||||
* that both a and b are normalized, so no underflow or overflow will occur.
|
||||
* The current rounding mode must be round-to-nearest.
|
||||
*/
|
||||
static inline struct dd dd_mul(long double a, long double b) {
|
||||
struct dd ret;
|
||||
long double ha, hb, la, lb, p, q;
|
||||
|
||||
p = a * SPLIT;
|
||||
ha = a - p;
|
||||
ha += p;
|
||||
la = a - ha;
|
||||
|
||||
p = b * SPLIT;
|
||||
hb = b - p;
|
||||
hb += p;
|
||||
lb = b - hb;
|
||||
|
||||
p = ha * hb;
|
||||
q = ha * lb + la * hb;
|
||||
|
||||
ret.hi = p + q;
|
||||
ret.lo = p - ret.hi + q + la * lb;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* Fused multiply-add: Compute x * y + z with a single rounding error.
|
||||
*
|
||||
* We use scaling to avoid overflow/underflow, along with the
|
||||
* canonical precision-doubling technique adapted from:
|
||||
*
|
||||
* Dekker, T. A Floating-Point Technique for Extending the
|
||||
* Available Precision. Numer. Math. 18, 224-242 (1971).
|
||||
*/
|
||||
long double fmal(long double x, long double y, long double z) {
|
||||
/* #pragma STDC FENV_ACCESS ON */
|
||||
long double xs, ys, zs, adj;
|
||||
struct dd xy, r;
|
||||
int oround;
|
||||
int ex, ey, ez;
|
||||
int spread;
|
||||
|
||||
/*
|
||||
* Handle special cases. The order of operations and the particular
|
||||
* return values here are crucial in handling special cases involving
|
||||
* infinities, NaNs, overflows, and signed zeroes correctly.
|
||||
*/
|
||||
if (!isfinite(x) || !isfinite(y)) return x * y + z;
|
||||
if (!isfinite(z)) return z;
|
||||
if (x == 0.0 || y == 0.0) return x * y + z;
|
||||
if (z == 0.0) return x * y;
|
||||
|
||||
xs = frexpl(x, &ex);
|
||||
ys = frexpl(y, &ey);
|
||||
zs = frexpl(z, &ez);
|
||||
oround = fegetround();
|
||||
spread = ex + ey - ez;
|
||||
|
||||
/*
|
||||
* If x * y and z are many orders of magnitude apart, the scaling
|
||||
* will overflow, so we handle these cases specially. Rounding
|
||||
* modes other than FE_TONEAREST are painful.
|
||||
*/
|
||||
if (spread < -LDBL_MANT_DIG) {
|
||||
#ifdef FE_INEXACT
|
||||
feraiseexcept(FE_INEXACT);
|
||||
#endif
|
||||
#ifdef FE_UNDERFLOW
|
||||
if (!isnormal(z)) feraiseexcept(FE_UNDERFLOW);
|
||||
#endif
|
||||
switch (oround) {
|
||||
default: /* FE_TONEAREST */
|
||||
return z;
|
||||
#ifdef FE_TOWARDZERO
|
||||
case FE_TOWARDZERO:
|
||||
if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0))
|
||||
return z;
|
||||
else
|
||||
return nextafterl(z, 0);
|
||||
#endif
|
||||
#ifdef FE_DOWNWARD
|
||||
case FE_DOWNWARD:
|
||||
if ((x > 0.0) ^ (y < 0.0))
|
||||
return (z);
|
||||
else
|
||||
return nextafterl(z, -INFINITY);
|
||||
#endif
|
||||
#ifdef FE_UPWARD
|
||||
case FE_UPWARD:
|
||||
if ((x > 0.0) ^ (y < 0.0))
|
||||
return nextafterl(z, INFINITY);
|
||||
else
|
||||
return (z);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
if (spread <= LDBL_MANT_DIG * 2)
|
||||
zs = scalbnl(zs, -spread);
|
||||
else
|
||||
zs = copysignl(LDBL_MIN, zs);
|
||||
|
||||
fesetround(FE_TONEAREST);
|
||||
|
||||
/*
|
||||
* Basic approach for round-to-nearest:
|
||||
*
|
||||
* (xy.hi, xy.lo) = x * y (exact)
|
||||
* (r.hi, r.lo) = xy.hi + z (exact)
|
||||
* adj = xy.lo + r.lo (inexact; low bit is sticky)
|
||||
* result = r.hi + adj (correctly rounded)
|
||||
*/
|
||||
xy = dd_mul(xs, ys);
|
||||
r = dd_add(xy.hi, zs);
|
||||
|
||||
spread = ex + ey;
|
||||
|
||||
if (r.hi == 0.0) {
|
||||
/*
|
||||
* When the addends cancel to 0, ensure that the result has
|
||||
* the correct sign.
|
||||
*/
|
||||
fesetround(oround);
|
||||
volatile long double vzs = zs; /* XXX gcc CSE bug workaround */
|
||||
return xy.hi + vzs + scalbnl(xy.lo, spread);
|
||||
}
|
||||
|
||||
if (oround != FE_TONEAREST) {
|
||||
/*
|
||||
* There is no need to worry about double rounding in directed
|
||||
* rounding modes.
|
||||
* But underflow may not be raised correctly, example in downward rounding:
|
||||
* fmal(0x1.0000000001p-16000L, 0x1.0000000001p-400L, -0x1p-16440L)
|
||||
*/
|
||||
long double ret;
|
||||
#if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
|
||||
int e = fetestexcept(FE_INEXACT);
|
||||
feclearexcept(FE_INEXACT);
|
||||
#endif
|
||||
fesetround(oround);
|
||||
adj = r.lo + xy.lo;
|
||||
ret = scalbnl(r.hi + adj, spread);
|
||||
#if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
|
||||
if (ilogbl(ret) < -16382 && fetestexcept(FE_INEXACT))
|
||||
feraiseexcept(FE_UNDERFLOW);
|
||||
else if (e)
|
||||
feraiseexcept(FE_INEXACT);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
adj = add_adjusted(r.lo, xy.lo);
|
||||
if (spread + ilogbl(r.hi) > -16383)
|
||||
return scalbnl(r.hi + adj, spread);
|
||||
else
|
||||
return add_and_denormalize(r.hi, adj, spread);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -40,7 +40,7 @@ asm(".include \"libc/disclaimer.inc\"");
|
|||
*/
|
||||
int ilogb(double x)
|
||||
{
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
/* #pragma STDC FENV_ACCESS ON */
|
||||
union {double f; uint64_t i;} u = {x};
|
||||
uint64_t i = u.i;
|
||||
int e = i>>52 & 0x7ff;
|
||||
|
|
|
@ -40,7 +40,7 @@ asm(".include \"libc/disclaimer.inc\"");
|
|||
*/
|
||||
int ilogbf(float x)
|
||||
{
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
/* #pragma STDC FENV_ACCESS ON */
|
||||
union {float f; uint32_t i;} u = {x};
|
||||
uint32_t i = u.i;
|
||||
int e = i>>23 & 0xff;
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue