Make improvements

This change progresses our AARCH64 support:

- The AARCH64 build and tests are now passing
- Add 128-bit floating-point support to printf()
- Fix clone() so it initializes cosmo's x28 TLS register
- Fix TLS memory layout issue with aarch64 _Alignas vars
- Revamp microbenchmarking tools so they work on aarch64
- Make some subtle improvements to aarch64 crash reporting
- Make kisdangerous() memory checks more accurate on aarch64
- Remove sys_open() since it's not available on Linux AARCH64

This change makes general improvements to Cosmo and Redbean:

- Introduce GetHostIsa() function in Redbean
- You can now feature check using pledge(0, 0)
- You can now feature check using unveil("",0)
- Refactor some more x86-specific asm comments
- Refactor and write docs for some libm functions
- Make the mmap() API behave more similar to Linux
- Fix WIFSIGNALED() which wrongly returned true for zero
- Rename some obscure cosmo keywords from noFOO to dontFOO
This commit is contained in:
Justine Tunney 2023-06-03 08:12:13 -07:00
parent 5655c9a4e7
commit 8f522cb702
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
116 changed files with 1194 additions and 1025 deletions

View file

@ -1,6 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_BENCH_H_
#define COSMOPOLITAN_LIBC_BENCH_H_
#include "libc/intrin/safemacros.internal.h"
#include "libc/nexgen32e/bench.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -9,22 +8,39 @@ COSMOPOLITAN_C_START_
* @fileoverview Microbenchmarking Toolz.
*/
#define BENCHLOOPER(START, STOP, N, EXPR) \
({ \
long Iter = 1; \
long Toto = (N); \
uint64_t Time1 = START(); \
asm volatile("" ::: "memory"); \
for (; Iter < Toto; ++Iter) { \
asm volatile("" ::: "memory"); \
EXPR; \
asm volatile("" ::: "memory"); \
} \
asm volatile("" ::: "memory"); \
uint64_t Time2 = STOP(); \
(double)(long)(Time2 - Time1) / Iter; \
})
#ifndef BENCHLOOP
#define BENCHLOOP(START, STOP, N, INIT, EXPR) \
({ \
unsigned long Iter, Count; \
uint64_t Time1, Time2; \
double Average; \
for (Average = 1, Iter = 1, Count = (N); Iter < Count; ++Iter) { \
INIT; \
Time1 = START(); \
asm volatile("" ::: "memory"); \
EXPR; \
asm volatile("" ::: "memory"); \
Time2 = STOP(); \
Average += 1. / Iter * ((int)unsignedsubtract(Time2, Time1) - Average); \
} \
Average; \
/* TODO(jart): DELETE */
#define BENCHLOOP(START, STOP, N, INIT, EXPR) \
({ \
double Average; \
uint64_t Time1, Time2; \
unsigned long Iter, Count; \
for (Average = 1, Iter = 1, Count = (N); Iter < Count; ++Iter) { \
INIT; \
Time1 = START(); \
asm volatile("" ::: "memory"); \
EXPR; \
asm volatile("" ::: "memory"); \
Time2 = STOP(); \
Average += 1. / Iter * ((int)(Time2 - Time1) - Average); \
} \
Average; \
})
#endif /* BENCHLOOP */

View file

@ -1,5 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_TESTLIB_EZBENCH_H_
#define COSMOPOLITAN_LIBC_TESTLIB_EZBENCH_H_
#include "libc/macros.internal.h"
#include "libc/math.h"
#include "libc/nexgen32e/bench.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/testlib/bench.h"
@ -18,7 +20,7 @@ COSMOPOLITAN_C_START_
#define EZBENCH2(NAME, INIT, EXPR) \
do { \
int Core, Tries, Interrupts; \
int64_t Speculative, MemoryStrict; \
double Speculative, MemoryStrict; \
Tries = 0; \
do { \
__testlib_yield(); \
@ -53,14 +55,14 @@ COSMOPOLITAN_C_START_
__testlib_getinterrupts() > Interrupts)); \
if (Tries == EZBENCH_TRIES) __testlib_ezbenchwarn(" memory strict"); \
__testlib_ezbenchreport( \
NAME, MAX(0, Speculative - __testlib_ezbenchcontrol()), \
MAX(0, MemoryStrict - __testlib_ezbenchcontrol())); \
NAME, MAX(.001, Speculative - __testlib_ezbenchcontrol()), \
MAX(.001, MemoryStrict - __testlib_ezbenchcontrol())); \
} while (0)
#define EZBENCH3(NAME, NUM, INIT, EXPR) \
do { \
int Core, Tries, Interrupts; \
int64_t Speculative, MemoryStrict; \
double Speculative, MemoryStrict; \
Tries = 0; \
do { \
__testlib_yield(); \
@ -95,14 +97,14 @@ COSMOPOLITAN_C_START_
__testlib_getinterrupts() > Interrupts)); \
if (Tries == EZBENCH_TRIES) __testlib_ezbenchwarn(" memory strict"); \
__testlib_ezbenchreport( \
NAME, MAX(0, Speculative - __testlib_ezbenchcontrol()), \
MAX(0, MemoryStrict - __testlib_ezbenchcontrol())); \
NAME, MAX(.001, Speculative - __testlib_ezbenchcontrol()), \
MAX(.001, MemoryStrict - __testlib_ezbenchcontrol())); \
} while (0)
#define EZBENCH_C(NAME, CONTROL, EXPR) \
do { \
int Core, Tries, Interrupts; \
int64_t Control, Speculative, MemoryStrict; \
double Control, Speculative, MemoryStrict; \
Tries = 0; \
do { \
__testlib_yield(); \
@ -144,53 +146,48 @@ COSMOPOLITAN_C_START_
(__testlib_getcore() != Core && \
__testlib_getinterrupts() > Interrupts)); \
if (Tries == EZBENCH_TRIES) __testlib_ezbenchwarn(" memory strict"); \
__testlib_ezbenchreport(NAME, MAX(0, Speculative - Control), \
MAX(0, MemoryStrict - Control)); \
__testlib_ezbenchreport(NAME, MAX(.001, Speculative - Control), \
MAX(.001, MemoryStrict - Control)); \
} while (0)
#define EZBENCH_N(NAME, N, EXPR) \
do { \
int64_t Speculative, Toto; \
int Core, Tries, Interrupts; \
Tries = 0; \
do { \
__testlib_yield(); \
Core = __testlib_getcore(); \
Interrupts = __testlib_getinterrupts(); \
EXPR; \
Speculative = BENCHLOOP(__startbench, __endbench, 32, \
__polluteregisters(), (EXPR)); \
} while (++Tries < EZBENCH_TRIES && \
(__testlib_getcore() != Core && \
__testlib_getinterrupts() > Interrupts)); \
if (Tries == EZBENCH_TRIES) __testlib_ezbenchwarn(""); \
__testlib_ezbenchreport_n( \
NAME, 'n', N, MAX(0, Speculative - __testlib_ezbenchcontrol())); \
#define EZBENCH_N(NAME, N, EXPR) \
do { \
double Speculative, Toto; \
int Core, Tries, Interrupts; \
Tries = 0; \
do { \
__testlib_yield(); \
Core = __testlib_getcore(); \
Interrupts = __testlib_getinterrupts(); \
EXPR; \
Speculative = BENCHLOOPER(__startbench, __endbench, 32, (EXPR)); \
} while (++Tries < EZBENCH_TRIES && !Speculative); \
if (Tries == EZBENCH_TRIES) __testlib_ezbenchwarn(""); \
__testlib_ezbenchreport_n(NAME, 'n', N, Speculative); \
} while (0)
#define EZBENCH_K(NAME, K, EXPR) \
do { \
int Core; \
int64_t Speculative; \
do { \
__testlib_yield(); \
Core = __testlib_getcore(); \
EXPR; \
Speculative = BENCHLOOP(__startbench, __endbench, EZBENCH_COUNT, \
donothing, (EXPR)); \
} while (Core != __testlib_getcore()); \
__testlib_ezbenchreport_n( \
NAME, 'k', K, MAX(0, Speculative - __testlib_ezbenchcontrol())); \
#define EZBENCH_K(NAME, K, EXPR) \
do { \
int Core; \
double Speculative; \
do { \
__testlib_yield(); \
Core = __testlib_getcore(); \
EXPR; \
Speculative = \
BENCHLOOPER(__startbench, __endbench, EZBENCH_COUNT, (EXPR)); \
} while (Core != __testlib_getcore()); \
__testlib_ezbenchreport_n(NAME, 'k', K, Speculative); \
} while (0)
void __polluteregisters(void);
void __testlib_yield(void);
int __testlib_getcore(void);
int64_t __testlib_getinterrupts(void);
int64_t __testlib_ezbenchcontrol(void);
double __testlib_ezbenchcontrol(void);
void __testlib_ezbenchwarn(const char *);
void __testlib_ezbenchreport(const char *, uint64_t, uint64_t);
void __testlib_ezbenchreport_n(const char *, char, size_t, uint64_t);
void __testlib_ezbenchreport(const char *, double, double);
void __testlib_ezbenchreport_n(const char *, char, size_t, double);
#ifdef __STRICT_ANSI__
#undef EZBENCH2

View file

@ -21,9 +21,9 @@
#include "libc/testlib/testlib.h"
static bool once;
static int64_t g_ezbenchcontrol;
static double g_ezbenchcontrol;
int64_t __testlib_ezbenchcontrol(void) {
double __testlib_ezbenchcontrol(void) {
if (!once) {
int Core, Tries, Interrupts;
Tries = 0;
@ -38,7 +38,7 @@ int64_t __testlib_ezbenchcontrol(void) {
if (Tries == 10) {
fputs("warning: failed to accurately benchmark control\n", stderr);
}
fprintf(stderr, "will subtract benchmark overhead of %ld cycles\n\n",
fprintf(stderr, "will subtract benchmark overhead of %g cycles\n\n",
g_ezbenchcontrol);
once = true;
}

View file

@ -26,17 +26,17 @@
STATIC_YOINK("strnwidth");
void __testlib_ezbenchreport(const char *form, uint64_t c1, uint64_t c2) {
uint64_t ns1, ns2;
void __testlib_ezbenchreport(const char *form, double c1, double c2) {
long ns1, ns2;
__warn_if_powersave();
ns1 = rintl(ConvertTicksToNanos(c1));
ns2 = rintl(ConvertTicksToNanos(c2));
ns1 = lrintl(ConvertTicksToNanos(c1));
ns2 = lrintl(ConvertTicksToNanos(c2));
(fprintf)(stderr,
VEIL("r", " * %-19s l: %,9luc %,9luns m: %,9luc %,9luns\n"),
form, c1, ns1, c2, ns2);
form, lrint(c1), ns1, lrint(c2), ns2);
}
void __testlib_ezbenchreport_n(const char *form, char z, size_t n, uint64_t c) {
void __testlib_ezbenchreport_n(const char *form, char z, size_t n, double c) {
char msg[128];
uint64_t bps;
long double cn, lat;

View file

@ -25,6 +25,8 @@ __polluteregisters:
xor %eax,%eax
xor %ecx,%ecx
xor %edx,%edx
xor %edi,%edi
xor %esi,%esi
xor %r8d,%r8d
xor %r9d,%r9d
xor %r10d,%r10d
@ -49,6 +51,31 @@ __polluteregisters:
xorps %xmm6,%xmm6
xorps %xmm7,%xmm7
.leafepilogue
#elif defined(__aarch64__)
mov x0,#0
mov x1,#0
mov x2,#0
mov x3,#0
mov x4,#0
mov x5,#0
mov x6,#0
mov x7,#0
mov x9,#0
mov x10,#0
mov x11,#0
mov x12,#0
mov x13,#0
mov x14,#0
mov x15,#0
movi v0.16b,#0
movi v1.16b,#0
movi v2.16b,#0
movi v3.16b,#0
movi v4.16b,#0
movi v5.16b,#0
movi v6.16b,#0
movi v7.16b,#0
ret
#else
ret
#endif
@ -58,7 +85,7 @@ __polluteregisters:
// Fill registers with junk data to create false dependencies.
// Which shall create the problem that happens w/o vzeroupper.
// Or the Core Architecture errata regarding BSR/BSF w/ 64bit.
__polluteregisters:
__polluteregisters_old:
.leafprologue
mov $-1,%rax
mov %rax,%rcx
@ -96,4 +123,4 @@ __polluteregisters:
punpcklqdq %xmm0,%xmm6
punpcklqdq %xmm0,%xmm7
.leafepilogue
.endfn __polluteregisters,globl
.endfn __polluteregisters_old,globl

View file

@ -32,9 +32,9 @@ thrashcodecache:
i = 0xdeadbeef
0: .rept 32768/(3+7)
rex.wrb
.byte 0001|(i&030) # ADD/OR/... Evqp Gvqp
.byte 0300|(i&033) # %r8-%r11 to %r8-%r11
.byte 0x49,0x81,0360|(i&003) # XOR immed32,%r8-%r11
.byte 0001|(i&030) // ADD/OR/... Evqp Gvqp
.byte 0300|(i&033) // %r8-%r11 to %r8-%r11
.byte 0x49,0x81,0360|(i&003) // XOR immed32,%r8-%r11
.long i
i = ((i * 1103515245 + 12345) >> 16) & 0xffffffff
.endr