Merge branch 'master' into ctl-shared

This commit is contained in:
Steven Dee (Jōshin) 2024-08-25 21:08:21 -07:00
commit 17589367b1
No known key found for this signature in database
936 changed files with 6268 additions and 2180 deletions

View file

@ -27,3 +27,5 @@ da8baf2aa5ce93b958aca90a0ae69f537806324b
369f9740de4534c28d0e81ab2afc99decbb9a3e6
# Get rid of .internal.h convention in LIBC_INTRIN
86d884cce24d773e298a2714c1e3d91ecab9be45
# Remove .internal from more header filenames
31194165d2afca36c2315a6e7ca2f0797dde09e3

24
.github/workflows/nightly-cosmocc.yml vendored Normal file
View file

@ -0,0 +1,24 @@
name: Nightly cosmocc
on:
schedule:
# https://crontab.guru/#37_4_*_*_*
- cron: "37 4 * * *"
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: true
jobs:
build-cosmocc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
sudo cp build/bootstrap/ape.elf /usr/bin/ape
sudo sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
- run: tool/cosmocc/package.sh
# https://github.com/actions/upload-artifact/issues/590
- uses: actions/upload-artifact@v4.3.5
with:
name: cosmocc
path: cosmocc
compression-level: 9

36
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,36 @@
{
"C_Cpp.default.compilerPath": ".cosmocc/3.7.1/bin/aarch64-linux-cosmo-c++",
"C_Cpp.default.compilerArgs": [
"-nostdinc",
"-nostdlib",
"-iquote.",
"-isystemlibc/isystem",
"-isystemthird_party/libcxx",
"-includelibc/integral/normalize.inc",
"-D_COSMO_SOURCE",
"-D__aarch64__"
],
"[c]": {
"editor.tabSize": 2,
"editor.insertSpaces": true
},
"[cpp]": {
"editor.tabSize": 2,
"editor.insertSpaces": true
},
"[makefile]": {
"editor.tabSize": 8,
"editor.insertSpaces": false
},
"[make]": {
"editor.tabSize": 8,
"editor.insertSpaces": false
},
"[assembly]": {
"editor.tabSize": 8,
"editor.insertSpaces": true
},
"files.associations": {
"log.h": "c"
}
}

View file

@ -132,7 +132,7 @@ endif
ifneq ($(findstring aarch64,$(MODE)),)
ARCH = aarch64
HOSTS ?= pi studio freebsdarm
HOSTS ?= pi pi5 studio freebsdarm
else
ARCH = x86_64
HOSTS ?= freebsd rhel7 xnu openbsd netbsd win10
@ -147,10 +147,10 @@ export MODE
export SOURCE_DATE_EPOCH
export TMPDIR
COSMOCC = .cosmocc/3.6.2
COSMOCC = .cosmocc/3.7.1
BOOTSTRAP = $(COSMOCC)/bin
TOOLCHAIN = $(COSMOCC)/bin/$(ARCH)-linux-cosmo-
DOWNLOAD := $(shell build/download-cosmocc.sh $(COSMOCC) 3.6.2 268aa82d9bfd774f76951b250f87b8edcefd5c754b8b409e1639641e8bd8d5bc)
DOWNLOAD := $(shell build/download-cosmocc.sh $(COSMOCC) 3.7.1 13b65b0e659b493bd82f3d0a319d0265d66f849839e484aa2a54191024711e85)
IGNORE := $(shell $(MKDIR) $(TMPDIR))

View file

@ -45,10 +45,10 @@ o/$(MODE)/ape: $(APE)
o/$(MODE)/ape/aarch64.lds: \
ape/aarch64.lds \
libc/zip.internal.h \
libc/zip.h \
libc/thread/tls.h \
libc/calls/struct/timespec.h \
libc/macros.internal.h \
libc/macros.h \
libc/str/str.h
APE_LOADER_LDFLAGS = \
@ -162,8 +162,8 @@ o/$(MODE)/ape/ape-no-modify-self.o: \
libc/dce.h \
libc/elf/def.h \
libc/thread/tls.h \
libc/macho.internal.h \
libc/macros.internal.h \
libc/macho.h \
libc/macros.h \
libc/nexgen32e/uart.internal.h \
libc/calls/metalfile.internal.h \
libc/nt/pedef.internal.h \
@ -188,8 +188,8 @@ o/$(MODE)/ape/ape-copy-self.o: \
libc/dce.h \
libc/elf/def.h \
libc/thread/tls.h \
libc/macho.internal.h \
libc/macros.internal.h \
libc/macho.h \
libc/macros.h \
libc/nexgen32e/uart.internal.h \
libc/calls/metalfile.internal.h \
libc/nt/pedef.internal.h \
@ -259,8 +259,8 @@ o/$(MODE)/ape/ape.o: \
libc/thread/tls.h \
ape/ape.internal.h \
ape/macros.internal.h \
libc/macho.internal.h \
libc/macros.internal.h \
libc/macho.h \
libc/macros.h \
libc/sysv/consts/prot.h \
libc/nt/pedef.internal.h \
libc/runtime/pc.internal.h \
@ -281,7 +281,7 @@ o/$(MODE)/ape/ape.lds: \
libc/dce.h \
libc/elf/def.h \
libc/elf/pf2prot.internal.h \
libc/macros.internal.h \
libc/macros.h \
libc/nt/pedef.internal.h \
libc/str/str.h \
libc/zip.internal.h
libc/zip.h

View file

@ -16,6 +16,12 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __APPLE__
#error "ape/ape-m1.c is for apple silicon. chances you want ape/loader.c"
#endif
#ifndef __aarch64__
#error "ape/ape-m1.c is for apple silicon; you want: make o//ape/ape.macho"
#endif
#include <assert.h>
#include <dispatch/dispatch.h>
#include <dlfcn.h>

View file

@ -37,7 +37,7 @@
#include "libc/calls/metalfile.internal.h"
#include "libc/dce.h"
#include "libc/elf/def.h"
#include "libc/macho.internal.h"
#include "libc/macho.h"
#include "libc/nexgen32e/uart.internal.h"
#include "libc/nt/pedef.internal.h"
#include "libc/runtime/pc.internal.h"

View file

@ -310,7 +310,7 @@ SECTIONS {
. = ALIGN(__privileged_end > __privileged_start ? CONSTANT(COMMONPAGESIZE) : 0);
/*END: morphable code */
__privileged_start = .;
*(.privileged)
*(.privileged .privileged.*)
__privileged_end = .;
KEEP(*(.ape.pad.text))

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
// Calls _start() function of loaded program.
//

View file

@ -16,10 +16,10 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macho.internal.h"
#include "libc/macho.h"
#include "libc/sysv/consts/prot.h"
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
// Apple Mach-O Executable Headers
// Fixups are applied by objbincopy

View file

@ -18,7 +18,7 @@
*/
#ifndef APE_MACROS_H_
#define APE_MACROS_H_
#include "libc/macros.internal.h"
#include "libc/macros.h"
#ifdef __ASSEMBLER__
/* clang-format off */

View file

@ -18,7 +18,7 @@
*/
#include "libc/dce.h"
#include "ape/ape.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#ifdef __aarch64__

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
// Invokes system call.
//

View file

@ -115,14 +115,10 @@ ifeq ($(ARCH), aarch64)
# - Cosmopolitan Libc uses x28 for thread-local storage because Apple
# forbids us from using tpidr_el0 too.
#
# - Cosmopolitan currently lacks an implementation of the runtime
# libraries needed by the -moutline-atomics flag
#
DEFAULT_COPTS += \
-ffixed-x18 \
-ffixed-x28 \
-fsigned-char \
-mno-outline-atomics
-fsigned-char
endif
MATHEMATICAL = \

View file

@ -1,6 +1,6 @@
#ifndef COSMOPOLITAN_DSP_CORE_C161_H_
#define COSMOPOLITAN_DSP_CORE_C161_H_
#include "libc/macros.internal.h"
#include "libc/macros.h"
#define EXTRA_SHARP 2

View file

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_DSP_CORE_C161S_H_
#define COSMOPOLITAN_DSP_CORE_C161S_H_
#include "dsp/core/c161.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
__funline signed char C161S(signed char al, signed char bl, signed char cl) {
short ax, bx, cx;

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsp/core/core.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/mem/mem.h"

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/str/str.h"

View file

@ -20,7 +20,7 @@
#include "libc/assert.h"
#include "libc/dce.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/str/str.h"

View file

@ -19,7 +19,7 @@
#include "dsp/core/core.h"
#include "dsp/core/q.h"
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/str/str.h"

View file

@ -1,6 +1,6 @@
#ifndef COSMOPOLITAN_DSP_CORE_HALF_H_
#define COSMOPOLITAN_DSP_CORE_HALF_H_
#include "libc/macros.internal.h"
#include "libc/macros.h"
/**
* Divides integer in half w/ rounding.

View file

@ -1,6 +1,6 @@
#ifndef COSMOPOLITAN_DSP_CORE_KS8_H_
#define COSMOPOLITAN_DSP_CORE_KS8_H_
#include "libc/macros.internal.h"
#include "libc/macros.h"
/**
* Performs 16-bit scaled rounded madd w/ eight coefficients or fewer.

View file

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_DSP_CORE_KSS8_H_
#define COSMOPOLITAN_DSP_CORE_KSS8_H_
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
/**
* Performs 16-bit scaled rounded saturated madd w/ eight coefficients or fewer.

View file

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_DSP_CORE_Q_H_
#define COSMOPOLITAN_DSP_CORE_Q_H_
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
/**

View file

@ -18,7 +18,7 @@
*/
#include "dsp/core/core.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "third_party/aarch64/arm_neon.internal.h"
#include "third_party/intel/emmintrin.internal.h"

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
clamp4int256$core:
.leafprologue

View file

@ -35,7 +35,7 @@
#include "libc/calls/struct/timespec.h"
#include "libc/fmt/conv.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"

View file

@ -28,7 +28,7 @@
SOFTWARE.
*/
#include "dsp/mpeg/mpeg.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
__static_yoink("pl_mpeg_notice");
/**

View file

@ -45,6 +45,12 @@ $(DSP_SCALE_A).pkg: \
$(DSP_SCALE_A_OBJS) \
$(foreach x,$(DSP_SCALE_A_DIRECTDEPS),$($(x)_A).pkg)
ifeq ($(ARCH),x86_64)
o/$(MODE)/dsp/scale/cdecimate2xuint8x8.o: private \
CFLAGS += \
-mssse3
endif
o/$(MODE)/dsp/scale/cdecimate2xuint8x8.o \
o/$(MODE)/dsp/scale/gyarados.o \
o/$(MODE)/dsp/scale/magikarp.o \

View file

@ -25,7 +25,7 @@
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"

View file

@ -20,7 +20,7 @@
#include "dsp/core/ks8.h"
#include "dsp/core/kss8.h"
#include "dsp/scale/cdecimate2xuint8x8.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/str/str.h"
#include "libc/x/x.h"

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
// TODO(jart): write me

View file

@ -21,7 +21,7 @@
#include "libc/assert.h"
#include "libc/limits.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/str/str.h"

View file

@ -18,7 +18,7 @@
*/
#include "dsp/tty/quant.h"
#include "libc/log/check.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
struct TtyRgb rgb2ttyi2f_(int r, int g, int b) {
return rgb2ttyf((ttyrgb_m128){r, g, b} / 255);

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsp/tty/quant.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
struct TtyRgb rgb2xterm24_(int r, int g, int b) {
return (struct TtyRgb){MAX(MIN(r, 255), 0), MAX(MIN(g, 255), 0),

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsp/tty/quant.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "third_party/intel/xmmintrin.internal.h"

View file

@ -26,7 +26,7 @@
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/runtime/runtime.h"

View file

@ -24,7 +24,7 @@
#include "libc/calls/termios.h"
#include "libc/calls/ucontext.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/mem/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
// Returns index of minimum uint16 in array.
//

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
// Returns index of minimum positive int16 in array.
//

View file

@ -53,6 +53,7 @@ EXAMPLES_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_NT_ADVAPI32 \
LIBC_NT_IPHLPAPI \
LIBC_NT_MEMORY \
LIBC_NT_KERNEL32 \
LIBC_NT_NTDLL \
LIBC_NT_USER32 \

View file

@ -10,7 +10,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/struct/timespec.h"
#include "libc/intrin/kprintf.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/enum/timezoneid.h"
#include "libc/nt/struct/timezoneinformation.h"
#include "libc/nt/time.h"

View file

@ -10,7 +10,7 @@
#include "libc/calls/calls.h"
#include "libc/fmt/conv.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/sock/struct/linger.h"

View file

@ -23,7 +23,7 @@
#include "libc/inttypes.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/mem/arraylist2.internal.h"
#include "libc/mem/mem.h"
@ -47,7 +47,7 @@
#include "libc/time.h"
#include "libc/x/xasprintf.h"
#include "libc/x/xsigaction.h"
#include "libc/zip.internal.h"
#include "libc/zip.h"
#include "third_party/getopt/getopt.internal.h"
#include "third_party/libcxx/vector"
#include "tool/viz/lib/knobs.h"

15
examples/nproc.c Normal file
View file

@ -0,0 +1,15 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include <cosmo.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("%d\n", __get_cpu_count());
}

View file

@ -1,4 +1,4 @@
#include "libc/macros.internal.h"
#include "libc/macros.h"
// Example assembly function.
//

1021
examples/romanize.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -41,7 +41,7 @@
#include "libc/fmt/conv.h"
#include "libc/intrin/bswap.h"
#include "libc/log/bsd.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/paths.h"
#include "libc/runtime/runtime.h"

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/cpuset.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
void CPU_AND(cpu_set_t *d, cpu_set_t *x, cpu_set_t *y) {
int i;

View file

@ -18,7 +18,7 @@
*/
#include "libc/calls/struct/cpuset.h"
#include "libc/intrin/popcnt.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
int CPU_COUNT(cpu_set_t *set) {
int i, c;

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/cpuset.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
void CPU_OR(cpu_set_t *d, cpu_set_t *x, cpu_set_t *y) {
int i;

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/cpuset.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
void CPU_XOR(cpu_set_t *d, cpu_set_t *x, cpu_set_t *y) {
int i;

View file

@ -18,7 +18,7 @@
*/
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
#include "libc/nt/process.h"

View file

@ -21,7 +21,7 @@
#include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/timeval.internal.h"
#include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/sysv/consts/clock.h"
#ifdef __x86_64__

View file

@ -17,13 +17,14 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/intrin/fds.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/weaken.h"
#include "libc/nt/enum/filetype.h"
#include "libc/nt/files.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/zipos.internal.h"
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sysv/consts/o.h"
@ -64,5 +65,7 @@ textwindows int sys_close_nt(int fd, int fildes) {
default:
break;
}
if (f->cursor)
__cursor_unref(f->cursor);
return CloseHandle(f->handle) ? 0 : __winerr();
}

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
/**

View file

@ -24,6 +24,7 @@
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/errno.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/weaken.h"
#include "libc/nt/files.h"
@ -82,6 +83,7 @@ static textwindows int sys_dup_nt_impl(int oldfd, int newfd, int flags,
g_fds.p[newfd] = g_fds.p[oldfd];
g_fds.p[newfd].handle = handle;
__cursor_ref(g_fds.p[newfd].cursor);
if (flags & _O_CLOEXEC) {
g_fds.p[newfd].flags |= _O_CLOEXEC;
} else {

View file

@ -20,18 +20,18 @@
#include "libc/calls/calls.h"
#include "libc/calls/createfileflags.internal.h"
#include "libc/calls/internal.h"
#include "libc/intrin/fds.h"
#include "libc/calls/struct/flock.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/calls/wincrash.internal.h"
#include "libc/errno.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/weaken.h"
#include "libc/limits.h"
#include "libc/log/backtrace.internal.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/mem/leaks.h"
#include "libc/mem/mem.h"
#include "libc/nt/createfile.h"
@ -151,7 +151,7 @@ static textwindows int sys_fcntl_nt_lock(struct Fd *f, int fd, int cmd,
case SEEK_SET:
break;
case SEEK_CUR:
off = f->pointer + off;
off = f->cursor->shared->pointer + off;
break;
case SEEK_END: {
int64_t size;
@ -351,9 +351,14 @@ textwindows int sys_fcntl_nt(int fd, int cmd, uintptr_t arg) {
}
rc = 0;
} else if (cmd == F_SETLK || cmd == F_SETLKW || cmd == F_GETLK) {
pthread_mutex_lock(&g_locks.mu);
rc = sys_fcntl_nt_lock(g_fds.p + fd, fd, cmd, arg);
pthread_mutex_unlock(&g_locks.mu);
struct Fd *f = g_fds.p + fd;
if (f->cursor) {
pthread_mutex_lock(&g_locks.mu);
rc = sys_fcntl_nt_lock(f, fd, cmd, arg);
pthread_mutex_unlock(&g_locks.mu);
} else {
rc = ebadf();
}
} else if (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC) {
rc = sys_fcntl_nt_dupfd(fd, cmd, arg);
} else {

View file

@ -27,7 +27,7 @@
#include "libc/intrin/atomic.h"
#include "libc/intrin/bsr.h"
#include "libc/intrin/strace.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/mem/alloca.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/fileinfobyhandleclass.h"

View file

@ -23,7 +23,7 @@
#include "libc/calls/struct/statfs.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/enum/fsinformationclass.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/files.h"

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
// Gets machine state.
//

View file

@ -30,39 +30,63 @@
int sys_getcpu(unsigned *opt_cpu, unsigned *opt_node, void *tcache);
/**
* Determines ID of CPU on which thread is currently scheduled.
*
* This is the same as sched_getcpu(), except it also supports returning
* the ID of the current NUMA node. On some platforms this functionality
* isn't available, in which case `out_opt_node` is always be set to 0.
*/
int getcpu(unsigned *out_opt_cpu, unsigned *out_opt_node) {
unsigned cpu;
unsigned node;
if (X86_HAVE(RDTSCP)) {
if (IsWindows()) {
struct NtProcessorNumber pn;
if (out_opt_cpu) {
GetCurrentProcessorNumberEx(&pn);
*out_opt_cpu = 64 * pn.Group + pn.Number;
}
if (out_opt_node) {
unsigned short node16;
if (GetNumaProcessorNodeEx(&pn, &node16)) {
*out_opt_node = node16;
} else {
return __winerr();
}
}
return 0;
}
#ifdef __x86_64__
if (X86_HAVE(RDTSCP) && (IsLinux() || IsFreebsd())) {
unsigned tsc_aux;
rdtscp(&tsc_aux);
cpu = TSC_AUX_CORE(tsc_aux);
node = TSC_AUX_NODE(tsc_aux);
} else if (IsWindows()) {
struct NtProcessorNumber pn;
GetCurrentProcessorNumberEx(&pn);
cpu = 64 * pn.Group + pn.Number;
unsigned short node16;
if (GetNumaProcessorNodeEx(&pn, &node16)) {
node = node16;
} else {
return __winerr();
if (out_opt_cpu)
*out_opt_cpu = TSC_AUX_CORE(tsc_aux);
if (out_opt_node)
*out_opt_node = TSC_AUX_NODE(tsc_aux);
return 0;
}
#endif
if (IsXnu() || IsOpenbsd() || IsNetbsd() || IsFreebsd()) {
if (out_opt_cpu) {
int rc = sched_getcpu();
if (rc == -1)
return -1;
*out_opt_cpu = rc;
}
} else if (IsAarch64()) {
long tpidr_el0;
asm("mrs\t%0,tpidr_el0" : "=r"(tpidr_el0));
cpu = tpidr_el0 & 255;
node = 0;
} else {
int rc = sys_getcpu(&cpu, &node, 0);
if (rc == -1)
return -1;
if (out_opt_node)
*out_opt_node = 0;
return 0;
}
if (out_opt_cpu) {
unsigned cpu, node;
int rc = sys_getcpu(&cpu, &node, 0);
if (rc == -1)
return -1;
if (out_opt_cpu)
*out_opt_cpu = cpu;
}
if (out_opt_node) {
if (out_opt_node)
*out_opt_node = node;
}
return 0;
}

View file

@ -20,7 +20,7 @@
#include "libc/calls/syscall_support-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/enum/computernameformat.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"

View file

@ -18,7 +18,7 @@
*/
#include "libc/calls/struct/rlimit.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/rlimit.h"

View file

@ -18,7 +18,7 @@
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/enum/computernameformat.h"
#include "libc/nt/systeminfo.h"
#include "libc/str/str.h"

View file

@ -21,7 +21,7 @@
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/dce.h"
#include "libc/fmt/conv.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/accounting.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/thread.h"

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
// Obtains WIN32 magic path, e.g. GetTempPathA.
//

View file

@ -27,7 +27,7 @@
#include "libc/intrin/getenv.h"
#include "libc/intrin/strace.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/runtime.h"
#include "libc/serialize.h"

View file

@ -32,7 +32,7 @@
#include "libc/intrin/asmflag.h"
#include "libc/intrin/strace.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nexgen32e/kcpuids.h"
#include "libc/nexgen32e/rdtsc.h"
#include "libc/nexgen32e/vendor.internal.h"

View file

@ -18,7 +18,7 @@
*/
#include "libc/calls/internal.h"
#include "libc/intrin/atomic.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/accounting.h"
#include "libc/str/str.h"

View file

@ -25,7 +25,7 @@
#include "libc/intrin/atomic.h"
#include "libc/intrin/strace.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"

View file

@ -5,9 +5,9 @@ COSMOPOLITAN_C_START_
int sys_getgroups(int size, uint32_t list[]);
int sys_setgroups(size_t size, const uint32_t list[]);
const char *DescribeGidList(char[128], int, int, const uint32_t list[]);
const char *_DescribeGidList(char[128], int, int, const uint32_t list[]);
#define DescribeGidList(rc, length, gidlist) \
DescribeGidList(alloca(128), rc, length, gidlist)
_DescribeGidList(alloca(128), rc, length, gidlist)
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_CALLS_GROUPS_INTERNAL_H_ */

View file

@ -4,7 +4,7 @@
#include "libc/intrin/fds.h"
#include "libc/calls/struct/sigval.h"
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/stdbool.h"
#define kSigactionMinRva 8 /* >SIG_{ERR,DFL,IGN,...} */

View file

@ -27,7 +27,7 @@
#include "libc/intrin/fds.h"
#include "libc/intrin/strace.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/mem/alloca.h"
#include "libc/mem/mem.h"
#include "libc/nt/console.h"

View file

@ -25,6 +25,5 @@
bool IsApeLoadable(char buf[8]) {
return READ32LE(buf) == READ32LE("\177ELF") ||
READ64LE(buf) == READ64LE("MZqFpD='") ||
READ64LE(buf) == READ64LE("jartsr='") ||
READ64LE(buf) == READ64LE("APEDBG='");
READ64LE(buf) == READ64LE("jartsr='");
}

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#define BYTES 64

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#define BYTES 64

View file

@ -19,6 +19,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/intrin/fds.h"
#include "libc/nt/enum/filetype.h"
#include "libc/nt/files.h"
#include "libc/nt/struct/byhandlefileinformation.h"
@ -31,7 +32,7 @@ static textwindows int64_t GetPosition(struct Fd *f, int whence) {
case SEEK_SET:
return 0;
case SEEK_CUR:
return f->pointer;
return f->cursor->shared->pointer;
case SEEK_END: {
struct NtByHandleFileInformation wst;
if (!GetFileInformationByHandle(f->handle, &wst)) {
@ -67,11 +68,14 @@ textwindows int64_t sys_lseek_nt(int fd, int64_t offset, int whence) {
} else if (__isfdkind(fd, kFdFile)) {
struct Fd *f = g_fds.p + fd;
int filetype = GetFileType(f->handle);
if (filetype != kNtFileTypePipe && filetype != kNtFileTypeChar) {
if (filetype != kNtFileTypePipe && //
filetype != kNtFileTypeChar && //
f->cursor->shared) {
int64_t res;
if ((res = Seek(f, offset, whence)) != -1) {
f->pointer = res;
}
__cursor_lock(f->cursor);
if ((res = Seek(f, offset, whence)) != -1)
f->cursor->shared->pointer = res;
__cursor_unlock(f->cursor);
return res;
} else {
return espipe();

View file

@ -32,7 +32,7 @@
#include "libc/intrin/directmap.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/runtime/pc.internal.h"
#include "libc/runtime/runtime.h"

View file

@ -24,7 +24,7 @@
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/calls/metalfile.internal.h"
.init.start 102,_init_metalfile

View file

@ -20,7 +20,7 @@
#include "libc/dce.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/systeminfo.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
@ -55,6 +55,19 @@ textwindows size_t __normntpath(char16_t *p, size_t n) {
// matched "/../" or "/..$"
while (j && p[j - 1] == '\\')
--j;
if (j && p[j - 1] == '.') {
// matched "." before
if (j >= 2 && p[j - 2] == '.' && //
(j == 2 || p[j - 3] == '\\')) {
// matched "^.." or "/.." before
p[++j] = '.';
++j;
continue;
} else if (j == 1 || p[j - 2] == '\\') {
// matched "^." or "/." before
continue;
}
}
while (j && p[j - 1] != '\\')
--j;
} else {

View file

@ -19,7 +19,7 @@
#include "libc/calls/internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/intrin/strace.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/files.h"
#include "libc/nt/thunk/msabi.h"

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
.privileged
__restore_rt_netbsd:

View file

@ -39,7 +39,7 @@
#include "libc/nt/struct/startupinfo.h"
#include "libc/nt/struct/startupinfoex.h"
#include "libc/proc/ntspawn.h"
#include "libc/stdalign.internal.h"
#include "libc/stdalign.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
#ifdef __x86_64__

View file

@ -24,7 +24,8 @@
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/intrin/fds.h"
#include "libc/macros.h"
#include "libc/nt/console.h"
#include "libc/nt/createfile.h"
#include "libc/nt/enum/accessmask.h"
@ -138,6 +139,7 @@ static textwindows int sys_open_nt_file(int dirfd, const char *file,
int64_t handle;
if ((handle = sys_open_nt_impl(dirfd, file, flags, mode,
kNtFileFlagOverlapped)) != -1) {
g_fds.p[fd].cursor = __cursor_new();
g_fds.p[fd].handle = handle;
g_fds.p[fd].kind = kFdFile;
g_fds.p[fd].flags = flags;
@ -170,15 +172,15 @@ static textwindows int sys_open_nt_no_handle(int fd, int flags, int mode,
static textwindows int sys_open_nt_dup(int fd, int flags, int mode, int oldfd) {
int64_t handle;
if (!__isfdopen(oldfd)) {
if (!__isfdopen(oldfd))
return enoent();
}
if (DuplicateHandle(GetCurrentProcess(), g_fds.p[oldfd].handle,
GetCurrentProcess(), &handle, 0, true,
kNtDuplicateSameAccess)) {
g_fds.p[fd] = g_fds.p[oldfd];
g_fds.p[fd].handle = handle;
g_fds.p[fd].mode = mode;
__cursor_ref(g_fds.p[fd].cursor);
if (!sys_fcntl_nt_setfl(fd, flags)) {
return fd;
} else {

View file

@ -23,7 +23,7 @@
#include "libc/calls/metalfile.internal.h"
#include "libc/intrin/directmap.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/runtime/pc.internal.h"
#include "libc/runtime/runtime.h"

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/pledge.internal.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/str/str.h"
static int FindPromise(const char *name) {

View file

@ -29,7 +29,7 @@
#include "libc/intrin/bsr.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/promises.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/sysv/consts/audit.h"

View file

@ -28,7 +28,7 @@
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/strace.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/filetype.h"

View file

@ -26,7 +26,7 @@
#include "libc/dce.h"
#include "libc/intrin/strace.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/zipos.internal.h"
#include "libc/stdio/sysparam.h"

View file

@ -18,8 +18,8 @@
*/
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/describeflags.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/kprintf.h"
static const char *__fdkind2str(int x) {
@ -55,7 +55,7 @@ void __printfds(struct Fd *fds, size_t fdslen) {
continue;
kprintf("%3d %s", i, __fdkind2str(fds[i].kind));
if (fds[i].flags) {
kprintf(" flags=%s", (DescribeOpenFlags)(buf, fds[i].flags));
kprintf(" flags=%s", _DescribeOpenFlags(buf, fds[i].flags));
}
if (fds[i].mode)
kprintf(" mode=%#o", fds[i].mode);

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
.init.start 305,_init_program_executable_name
push %rdi

View file

@ -26,7 +26,7 @@
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/stdio/sysparam.h"
#include "libc/sysv/errfuns.h"

View file

@ -34,7 +34,7 @@
#include "libc/intrin/nomultics.h"
#include "libc/intrin/strace.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/nt/console.h"
#include "libc/nt/createfile.h"
#include "libc/nt/enum/accessmask.h"

View file

@ -22,7 +22,7 @@
#include "libc/calls/struct/iovec.h"
#include "libc/calls/struct/iovec.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
#include "libc/vga/vga.internal.h"

View file

@ -19,9 +19,9 @@
#include "libc/calls/createfileflags.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/intrin/fds.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/weaken.h"
#include "libc/nt/enum/filetype.h"
#include "libc/nt/errors.h"
@ -51,39 +51,41 @@ sys_readwrite_nt(int fd, void *data, size_t size, ssize_t offset,
uint32_t exchanged;
struct Fd *f = g_fds.p + fd;
// win32 i/o apis generally take 32-bit values thus we implicitly
// truncate outrageously large sizes. linux actually does it too!
size = MIN(size, 0x7ffff000);
// pread() and pwrite() perform an implicit lseek() operation, so
// similar to the lseek() system call, they too raise ESPIPE when
// operating on a non-seekable file.
bool pwriting = offset != -1;
bool seekable =
(f->kind == kFdFile && GetFileType(handle) == kNtFileTypeDisk) ||
f->kind == kFdDevNull || f->kind == kFdDevRandom;
if (pwriting && !seekable) {
bool isdisk = f->kind == kFdFile && GetFileType(handle) == kNtFileTypeDisk;
bool seekable = isdisk || f->kind == kFdDevNull || f->kind == kFdDevRandom;
if (pwriting && !seekable)
return espipe();
}
// determine if we need to lock a file descriptor across processes
bool locked = isdisk && !pwriting && f->cursor;
if (locked)
__cursor_lock(f->cursor);
RestartOperation:
// when a file is opened in overlapped mode win32 requires that we
// take over full responsibility for managing our own file pointer
// which is fine, because the one win32 has was never very good in
// the sense that it behaves so differently from linux, that using
// win32 i/o required more compatibilty toil than doing it by hand
if (!pwriting) {
if (seekable) {
offset = f->pointer;
if (seekable && f->cursor) {
offset = f->cursor->shared->pointer;
} else {
offset = 0;
}
}
RestartOperation:
bool eagained = false;
// check for signals and cancelation
if (_check_cancel() == -1)
if (_check_cancel() == -1) {
if (locked)
__cursor_unlock(f->cursor);
return -1; // ECANCELED
}
if (_weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))) {
goto HandleInterrupt;
}
@ -114,16 +116,16 @@ RestartOperation:
}
ok = true;
}
if (ok) {
if (ok)
ok = GetOverlappedResult(handle, &overlap, &exchanged, true);
}
CloseHandle(overlap.hEvent);
// if i/o succeeded then return its result
if (ok) {
if (!pwriting && seekable) {
f->pointer = offset + exchanged;
}
if (!pwriting && seekable && f->cursor)
f->cursor->shared->pointer = offset + exchanged;
if (locked)
__cursor_unlock(f->cursor);
return exchanged;
}
@ -131,23 +133,32 @@ RestartOperation:
if (GetLastError() == kNtErrorOperationAborted) {
// raise EAGAIN if it's due to O_NONBLOCK mmode
if (eagained) {
if (locked)
__cursor_unlock(f->cursor);
return eagain();
}
// otherwise it must be due to a kill() via __sig_cancel()
if (_weaken(__sig_relay) && (sig = _weaken(__sig_get)(waitmask))) {
HandleInterrupt:
if (locked)
__cursor_unlock(f->cursor);
int handler_was_called = _weaken(__sig_relay)(sig, SI_KERNEL, waitmask);
if (_check_cancel() == -1)
return -1; // possible if we SIGTHR'd
if (locked)
__cursor_lock(f->cursor);
// read() is @restartable unless non-SA_RESTART hands were called
if (!(handler_was_called & SIG_HANDLED_NO_RESTART)) {
if (!(handler_was_called & SIG_HANDLED_NO_RESTART))
goto RestartOperation;
}
}
if (locked)
__cursor_unlock(f->cursor);
return eintr();
}
// read() and write() have generally different error-handling paths
if (locked)
__cursor_unlock(f->cursor);
return -2;
}

View file

@ -18,7 +18,7 @@
*/
#include "libc/calls/internal.h"
#include "libc/intrin/atomic.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
#include "libc/str/str.h"
// really want to avoid locking here so close() needn't block signals

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/macros.h"
.text.windows
// Restores thread to state before signal.

View file

@ -17,7 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/rusage.h"
#include "libc/macros.internal.h"
#include "libc/macros.h"
/**
* Accumulates resource statistics in `y` to `x`.

View file

@ -23,32 +23,82 @@
#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.
*
* This function is supported on the following platforms:
*
* - x86-64
*
* - Linux: rdtsc
* - FreeBSD: rdtsc
* - Windows: win32
* - OpenBSD: unsupported
* - NetBSD: unsupported
* - MacOS: unsupported
*
* - aarch64
*
* - Linux: syscall
* - FreeBSD: syscall
* - MacOS: supported
*
* @return cpu number on success, or -1 w/ errno
*/
int sched_getcpu(void) {
if (X86_HAVE(RDTSCP)) {
unsigned tsc_aux;
rdtscp(&tsc_aux);
return TSC_AUX_CORE(tsc_aux);
} else if (IsAarch64()) {
long tpidr_el0;
asm("mrs\t%0,tpidr_el0" : "=r"(tpidr_el0));
return tpidr_el0 & 255;
} else if (IsWindows()) {
if (IsWindows()) {
struct NtProcessorNumber pn;
GetCurrentProcessorNumberEx(&pn);
return 64 * pn.Group + pn.Number;
} else {
unsigned cpu = 0;
int rc = sys_getcpu(&cpu, 0, 0);
if (rc == -1)
return -1;
return cpu;
}
#ifdef __x86_64__
if (X86_HAVE(RDTSCP) && (IsLinux() || IsFreebsd())) {
// Only the Linux, FreeBSD, and Windows kernels can be counted upon
// to populate the TSC_AUX register with the current thread number.
unsigned tsc_aux;
rdtscp(&tsc_aux);
return TSC_AUX_CORE(tsc_aux);
}
#endif
#ifdef __aarch64__
if (IsXnu()) {
// pthread_cpu_number_np() is defined by MacOS 11.0+ (Big Sur) in
// the SDK pthread.h header file, even though there's no man page
if (__syslib && __syslib->__version >= 9) {
errno_t err;
size_t out = 0;
if ((err = __syslib->__pthread_cpu_number_np(&out))) {
errno = err;
return -1;
}
return out;
} else {
errno = ENOSYS; // upgrade your ape loader
return -1; // cc -o /usr/local/bin/ape ape/ape-m1.c
}
}
#endif
#ifdef __aarch64__
if (IsFreebsd()) {
register int x0 asm("x0");
register int x8 asm("x8") = 581; // sched_getcpu
asm volatile("svc\t0" : "=r"(x0) : "r"(x8) : "memory");
return x0;
}
#endif
unsigned cpu = 0;
int rc = sys_getcpu(&cpu, 0, 0);
if (rc == -1)
return -1;
return cpu;
}

Some files were not shown because too many files have changed in this diff Show more