Perform more low-level code cleanup

This commit is contained in:
Justine Tunney 2022-09-09 04:07:08 -07:00
parent c32e2d4486
commit 2d17ab016c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
42 changed files with 977 additions and 265 deletions

View file

@ -0,0 +1,63 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 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.
*/
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/fsgsbase.h"
#include "libc/nexgen32e/x86feature.h"
/**
* Returns true if FSGSBASE ISA can be used.
*
* If this function returns true (Linux 5.9+ or FreeBSD) then you should
* be able to read/write to the %gs / %fs x86 segment registers in about
* one or two clock cycles which gives you a free addition operation for
* all assembly ops that reference memory.
*
* The FSGSBASE ISA was introduced by Intel with Ivybridge (c. 2012) but
* the Linux Kernel didn't authorize us to use it until 2020, once Intel
* had to start backdooring customer kernels so that they could have it.
* AMD introduced support for the FSGSBASE ISA in Excavator, aka bdver4.
*
* @return boolean indicating if feature can be used
* @see _rdfsbase()
* @see _rdgsbase()
* @see _wrfsbase()
* @see _wrgsbase()
*/
privileged int _have_fsgsbase(void) {
// Linux 5.9 (c. 2020) introduced close_range() and fsgsbase support.
// it's cheaper to test for close_range() than handle an op crashing.
// Windows lets us use these instructions but they don't really work.
int ax;
if (X86_HAVE(FSGSBASE)) {
if (IsLinux()) {
asm volatile("syscall"
: "=a"(ax)
: "0"(436 /* close_range */), "D"(-1), "S"(-2), "d"(0)
: "rcx", "r11", "memory");
return ax == -22; // EINVAL
} else if (IsFreebsd()) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}

28
libc/intrin/_rdfsbase.c Normal file
View file

@ -0,0 +1,28 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 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.
*/
#include "libc/intrin/fsgsbase.h"
/**
* Reads `%fs` base address.
*
* @see _have_fsgsbase()
*/
void *(_rdfsbase)(void) {
return _rdfsbase();
}

28
libc/intrin/_rdgsbase.c Normal file
View file

@ -0,0 +1,28 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 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.
*/
#include "libc/intrin/fsgsbase.h"
/**
* Reads `%gs` base address.
*
* @see _have_fsgsbase()
*/
void *(_rdgsbase)(void) {
return _rdgsbase();
}

28
libc/intrin/_wrfsbase.c Normal file
View file

@ -0,0 +1,28 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 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.
*/
#include "libc/intrin/fsgsbase.h"
/**
* Changes `%fs` base address.
*
* @see _have_fsgsbase()
*/
void *(_wrfsbase)(void *p) {
return _wrfsbase(p);
}

28
libc/intrin/_wrgsbase.c Normal file
View file

@ -0,0 +1,28 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 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.
*/
#include "libc/intrin/fsgsbase.h"
/**
* Changes `%gs` base address.
*
* @see _have_fsgsbase()
*/
void *(_wrgsbase)(void *p) {
return _wrgsbase(p);
}

View file

@ -0,0 +1,30 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 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.
*/
#include "libc/calls/calls.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/describeflags.internal.h"
const char *(DescribeArchPrctlCode)(char buf[12], int x) {
if (x == ARCH_SET_FS) return "ARCH_SET_FS";
if (x == ARCH_GET_FS) return "ARCH_GET_FS";
if (x == ARCH_SET_GS) return "ARCH_SET_GS";
if (x == ARCH_GET_GS) return "ARCH_GET_GS";
FormatInt32(buf, x);
return buf;
}

View file

@ -12,6 +12,7 @@ struct thatispacked DescribeFlags {
const char *DescribeFlags(char *, size_t, struct DescribeFlags *, size_t,
const char *, unsigned);
const char *DescribeArchPrctlCode(char[12], int);
const char *DescribeCapability(char[20], int);
const char *DescribeClockName(char[32], int);
const char *DescribeDirfd(char[12], int);
@ -54,6 +55,7 @@ const char *DescribeSocketProtocol(char[12], int);
const char *DescribeSocketType(char[64], int);
const char *DescribeWhence(char[12], int);
#define DescribeArchPrctlCode(x) DescribeArchPrctlCode(alloca(12), x)
#define DescribeCapability(x) DescribeCapability(alloca(20), x)
#define DescribeClockName(x) DescribeClockName(alloca(32), x)
#define DescribeDirfd(x) DescribeDirfd(alloca(12), x)

47
libc/intrin/fsgsbase.h Normal file
View file

@ -0,0 +1,47 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_FSGSBASE_H_
#define COSMOPOLITAN_LIBC_RUNTIME_FSGSBASE_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
void *_rdfsbase(void);
void *_rdgsbase(void);
void *_wrfsbase(void *);
void *_wrgsbase(void *);
int _have_fsgsbase(void);
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define _rdfsbase() \
({ \
void *_p; \
asm("rdfsbase\t%0" : "=r"(_p)); \
_p; \
})
#define _rdgsbase() \
({ \
void *_p; \
asm("rdgsbase\t%0" : "=r"(_p)); \
_p; \
})
#define _wrfsbase(p) \
({ \
void *_p = p; \
asm volatile("wrfsbase\t%0" \
: /* no outputs */ \
: "r"(_p) \
: "memory"); \
_p; \
})
#define _wrgsbase(p) \
({ \
void *_p = p; \
asm volatile("wrgsbase\t%0" \
: /* no outputs */ \
: "r"(_p) \
: "memory"); \
_p; \
})
#endif /* GNUC && !ANSI */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_FSGSBASE_H_ */

View file

@ -0,0 +1,38 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 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.
*/
#include "libc/intrin/pthread.h"
#include "libc/runtime/stack.h"
/**
* Returns configuration for thread stack.
*
* This is a getter for a configuration attribute. By default, zeros are
* returned. If pthread_attr_setstack() was called earlier, then this'll
* return those earlier supplied values.
*
* @param stackaddr will be set to stack address in bytes
* @return 0 on success, or errno on error
* @see pthread_attr_setstacksize()
*/
int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr,
size_t *stacksize) {
*stackaddr = attr->stackaddr;
*stacksize = attr->stacksize;
return 0;
}

View file

@ -30,5 +30,8 @@ int pthread_attr_init(pthread_attr_t *attr) {
.stacksize = GetStackSize(),
.guardsize = PAGESIZE,
};
if (attr->stacksize >= 1048576) {
attr->guardsize = FRAMESIZE;
}
return 0;
}

View file

@ -16,21 +16,15 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/intrin/pthread.h"
#include "libc/macros.internal.h"
/**
* Sets size of unmapped pages at bottom of stack.
*
* Cosmopolitan Libc stack guards always default to 4096 bytes. Setting
* `guardsize` to zero will disable automatic creation of guard pages.
* Your `guardsize` will be rounded up to `PAGESIZE`.
*
* @param guardsize contains guard size in bytes
* @return 0 on success, or errno on error
*/
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize) {
attr->guardsize = ROUNDUP(guardsize, PAGESIZE);
attr->guardsize = guardsize;
return 0;
}

View file

@ -0,0 +1,79 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 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.
*/
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/pthread.h"
/**
* Configures custom allocated stack for thread, e.g.
*
* pthread_t id;
* pthread_attr_t attr;
* char *stk = _mapstack();
* pthread_attr_init(&attr);
* pthread_attr_setstack(&attr, stk, GetStackSize());
* pthread_create(&id, &attr, func, 0);
* pthread_attr_destroy(&attr);
* pthread_join(id, 0);
* _freestack(stk);
*
* Your stack must have at least `PTHREAD_STACK_MIN` bytes, which
* Cosmpolitan Libc defines as `GetStackSize()`. It's a link-time
* constant used by Actually Portable Executable that's 128 kb by
* default. See libc/runtime/stack.h for docs on your stack limit
* since the APE ELF phdrs are the one true source of truth here.
*
* Cosmpolitan Libc runtime magic (e.g. ftrace) and memory safety
* (e.g. kprintf) assumes that stack sizes are two-powers and are
* aligned to that two-power. Conformance isn't required since we
* say caveat emptor to those who don't maintain these invariants
* please consider using _mapstack() which always does it perfect
* or use `mmap(0, GetStackSize() << 1, ...)` for a bigger stack.
*
* Unlike pthread_attr_setstacksize(), this function permits just
* about any parameters and will change the values and allocation
* as needed to conform to the mandatory requirements of the host
* operating system even if it doesn't meet the stricter needs of
* Cosmopolitan Libc userspace libraries. For example with malloc
* allocations, things like page size alignment, shall be handled
* automatically for compatibility with existing codebases.
*
* @param stackaddr is address of stack allocated by caller, and
* may be NULL in which case default behavior is restored
* @param stacksize is size of caller allocated stack
* @return 0 on success, or errno on error
* @raise EINVAL if parameters were unacceptable
* @see pthread_attr_setstacksize()
*/
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
size_t stacksize) {
if (!stackaddr) {
attr->stackaddr = 0;
attr->stacksize = 0;
return 0;
}
if (stacksize < PTHREAD_STACK_MIN ||
(IsAsan() && !__asan_is_valid(stackaddr, stacksize))) {
return EINVAL;
}
attr->stackaddr = stackaddr;
attr->stacksize = stacksize;
return 0;
}

View file

@ -20,34 +20,14 @@
#include "libc/intrin/pthread.h"
/**
* Sets size of thread stack.
* Defines minimum stack size for thread.
*
* Your stack must have at least `PTHREAD_STACK_MIN` bytes, which
* Cosmpolitan Libc defines as `GetStackSize()`. It's a link-time
* constant used by Actually Portable Executable that's 128 kb by
* default. See libc/runtime/stack.h for docs on your stack limit
* since the APE ELF phdrs are the one true source of truth here.
*
* Cosmpolitan Libc runtime magic (e.g. ftrace) and memory safety
* (e.g. kprintf) assumes that stack sizes are two-powers and are
* aligned to that two-power. Conformance isn't required since we
* say caveat emptor to those who don't maintain these invariants
*
* Unlike pthread_attr_setstack() this function should be used if
* you want the Cosmopolitan Libc runtime to allocate a stack for
* you. Since the runtime uses mmap(MAP_STACK) to do that, you'll
* need to choose a multiple of FRAMESIZE, due to Windows.
*
* If this function isn't called it'll default to GetStackSize().
*
* @param x contains stack size in bytes
* @param stacksize contains stack size in bytes
* @return 0 on success, or errno on error
* @raise EINVAL if `x` is less than `PTHREAD_STACK_MIN`
* @raise EINVAL if `stacksize` is less than `PTHREAD_STACK_MIN`
*/
int pthread_attr_setstacksize(pthread_attr_t *a, size_t x) {
if (x < PTHREAD_STACK_MIN) return EINVAL;
if (x & (FRAMESIZE - 1)) return EINVAL;
if (x < FRAMESIZE) return EINVAL;
a->stacksize = x;
int pthread_attr_setstacksize(pthread_attr_t *a, size_t stacksize) {
if (stacksize < PTHREAD_STACK_MIN) return EINVAL;
a->stacksize = stacksize;
return 0;
}

View file

@ -38,24 +38,24 @@ privileged int sys_gettid(void) {
asm("syscall" // xnu/osfmk/kern/ipc_tt.c
: "=a"(tid) // assume success
: "0"(0x1000000 | 27) // Mach thread_self_trap()
: "rcx", "r11", "memory", "cc");
: "rcx", "r8", "r9", "r10", "r11", "memory", "cc");
} else if (IsOpenbsd()) {
asm("syscall"
: "=a"(tid) // man says always succeeds
: "0"(299) // getthrid()
: "rcx", "r11", "memory", "cc");
: "rcx", "r8", "r9", "r10", "r11", "memory", "cc");
} else if (IsNetbsd()) {
asm("syscall"
: "=a"(tid) // man says always succeeds
: "0"(311) // _lwp_self()
: "rcx", "rdx", "r11", "memory", "cc");
: "rcx", "rdx", "r8", "r9", "r10", "r11", "memory", "cc");
} else if (IsFreebsd()) {
asm("syscall"
: "=a"(tid), // only fails w/ EFAULT, which isn't possible
"=m"(wut) // must be 64-bit
: "0"(432), // thr_self()
"D"(&wut) // but not actually 64-bit
: "rcx", "r11", "memory", "cc");
: "rcx", "r8", "r9", "r10", "r11", "memory", "cc");
tid = wut;
} else {
tid = __pid;