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,110 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.internal.h"
.privileged
diagnose_syscall:
push %rbp
mov %rsp,%rbp
push %rbx
push %r12
push %r13
push %r14
push %r15
mov $0x7fffffff,%eax
add $4,%eax # set sf/of/pf
mov %rdi,%rax # nr
mov %rsi,%rdi # arg 1
mov %rdx,%rsi # arg 2
mov %rcx,%rdx # arg 3
mov %r8,%r10 # arg 4
mov %r9,%r8 # arg 5
mov 16(%rbp),%r9 # arg 6
push 24(%rbp) # arg 7
push %rax # fake ret addr
mov 32(%rbp),%r12 # ucontext before
mov 40(%rbp),%r13 # ucontext after
xor %ecx,%ecx
xor %r11d,%r11d
mov $0x5555555555555555,%r11
mov $0x5555555555555555,%r14
mov $0x5555555555555555,%r15
mov $0x5555555555555555,%rbx
// save machine state before system call
pushf
pop 176(%r12)
mov %r8,40(%r12)
mov %r9,48(%r12)
mov %r10,56(%r12)
mov %r11,64(%r12)
mov %r12,72(%r12)
mov %r13,80(%r12)
mov %r14,88(%r12)
mov %r15,96(%r12)
mov %rdi,104(%r12)
mov %rsi,112(%r12)
mov %rbp,120(%r12)
mov %rbx,128(%r12)
mov %rdx,136(%r12)
mov %rax,144(%r12)
mov %rcx,152(%r12)
push %rbx
lea 320(%r12),%rbx
mov %rbx,224(%r12) # set fpregs ptr
pop %rbx
syscall
// save machine state after system call
pushf
pop 176(%r13)
mov %r8,40(%r13)
mov %r9,48(%r13)
mov %r10,56(%r13)
mov %r11,64(%r13)
mov %r12,72(%r13)
mov %r13,80(%r13)
mov %r14,88(%r13)
mov %r15,96(%r13)
mov %rdi,104(%r13)
mov %rsi,112(%r13)
mov %rbp,120(%r13)
mov %rbx,128(%r13)
mov %rdx,136(%r13)
mov %rax,144(%r13)
mov %rcx,152(%r13)
push %rbx
lea 320(%r13),%rbx
mov %rbx,224(%r13) # set fpregs ptr
pop %rbx
pop %r13
pop %r13
pop %r15
pop %r14
pop %r13
pop %r12
pop %rbx
pop %rbp
ret
.endfn diagnose_syscall,globl

View file

@ -20,10 +20,26 @@
// Gets machine state.
//
// @note please use longerjmp() and setlongerjmp() for fibers
// @note currently only gets general registers
// @see setcontext()
getcontext:
pushf
pop 176(%rdi)
movaps %xmm0,480(%rdi)
movaps %xmm1,496(%rdi)
movaps %xmm2,512(%rdi)
movaps %xmm3,528(%rdi)
movaps %xmm4,544(%rdi)
movaps %xmm5,560(%rdi)
movaps %xmm6,576(%rdi)
movaps %xmm7,592(%rdi)
movaps %xmm8,608(%rdi)
movaps %xmm9,624(%rdi)
movaps %xmm10,640(%rdi)
movaps %xmm11,656(%rdi)
movaps %xmm12,672(%rdi)
movaps %xmm13,688(%rdi)
movaps %xmm14,704(%rdi)
movaps %xmm15,720(%rdi)
mov %r8,40(%rdi)
mov %r9,48(%rdi)
mov %r10,56(%rdi)
@ -39,6 +55,8 @@ getcontext:
mov %rdx,136(%rdi)
mov %rax,144(%rdi)
mov %rcx,152(%rdi)
lea 320(%rdi),%rax
mov %rax,224(%rdi)
lea 8(%rsp),%rax
mov %rax,160(%rdi)
mov (%rsp),%rax
@ -50,6 +68,22 @@ getcontext:
.end
////////////////////////////////////////////////////////////////////////////////
noasan noubsan noinstrument int getcontext(ucontext_t *uc) {
asm volatile("movaps\t%%xmm0,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[0]));
asm volatile("movaps\t%%xmm1,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[1]));
asm volatile("movaps\t%%xmm2,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[2]));
asm volatile("movaps\t%%xmm3,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[3]));
asm volatile("movaps\t%%xmm4,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[4]));
asm volatile("movaps\t%%xmm5,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[5]));
asm volatile("movaps\t%%xmm6,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[6]));
asm volatile("movaps\t%%xmm7,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[7]));
asm volatile("movaps\t%%xmm8,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[8]));
asm volatile("movaps\t%%xmm9,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[9]));
asm volatile("movaps\t%%xmm10,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[10]));
asm volatile("movaps\t%%xmm11,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[11]));
asm volatile("movaps\t%%xmm12,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[12]));
asm volatile("movaps\t%%xmm13,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[13]));
asm volatile("movaps\t%%xmm14,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[14]));
asm volatile("movaps\t%%xmm15,%0" : /* no outputs */ : "m"(uc->__fpustate.xmm[15]));
asm volatile("mov\t%%r8,%0" : "=m"(uc->uc_mcontext.r8));
asm volatile("mov\t%%r9,%0" : "=m"(uc->uc_mcontext.r9));
asm volatile("mov\t%%r10,%0" : "=m"(uc->uc_mcontext.r10));
@ -65,6 +99,7 @@ noasan noubsan noinstrument int getcontext(ucontext_t *uc) {
asm volatile("mov\t%%rdx,%0" : "=m"(uc->uc_mcontext.rdx));
asm volatile("mov\t%%rax,%0" : "=m"(uc->uc_mcontext.rax));
asm volatile("mov\t%%rcx,%0" : "=m"(uc->uc_mcontext.rcx));
uc->uc_mcontext.fpregs = &uc->__fpustate;
asm volatile("lea\t8(%%rsp),%%rax\n\t"
"mov\t%%rax,%0"
: "=m"(uc->uc_mcontext.rsp)

View file

@ -20,10 +20,29 @@
// Sets machine state.
//
// @note please use longerjmp() and setlongerjmp() for fibers
// @note currently only sets general registers
// @see getcontext()
setcontext:
mov 224(%rdi),%rax
test %rax,%rax
je 1f
movaps 160(%rax),%xmm0
movaps 176(%rax),%xmm1
movaps 192(%rax),%xmm2
movaps 208(%rax),%xmm3
movaps 224(%rax),%xmm4
movaps 240(%rax),%xmm5
movaps 256(%rax),%xmm6
movaps 272(%rax),%xmm7
movaps 288(%rax),%xmm8
movaps 304(%rax),%xmm9
movaps 320(%rax),%xmm10
movaps 336(%rax),%xmm11
movaps 352(%rax),%xmm12
movaps 368(%rax),%xmm13
movaps 384(%rax),%xmm14
movaps 400(%rax),%xmm15
1: push 176(%rdi)
popf
mov 40(%rdi),%r8
mov 48(%rdi),%r9
mov 56(%rdi),%r10
@ -48,6 +67,24 @@ setcontext:
.end
////////////////////////////////////////////////////////////////////////////////
noasan noubsan int setcontext(const ucontext_t *uc) {
if (uc->uc_mcontext.fpregs) {
asm volatile("movaps\t%0,%%xmm0" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[0]));
asm volatile("movaps\t%0,%%xmm1" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[1]));
asm volatile("movaps\t%0,%%xmm2" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[2]));
asm volatile("movaps\t%0,%%xmm3" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[3]));
asm volatile("movaps\t%0,%%xmm4" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[4]));
asm volatile("movaps\t%0,%%xmm5" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[5]));
asm volatile("movaps\t%0,%%xmm6" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[6]));
asm volatile("movaps\t%0,%%xmm7" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[7]));
asm volatile("movaps\t%0,%%xmm8" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[8]));
asm volatile("movaps\t%0,%%xmm9" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[9]));
asm volatile("movaps\t%0,%%xmm10" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[10]));
asm volatile("movaps\t%0,%%xmm11" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[11]));
asm volatile("movaps\t%0,%%xmm12" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[12]));
asm volatile("movaps\t%0,%%xmm13" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[13]));
asm volatile("movaps\t%0,%%xmm14" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[14]));
asm volatile("movaps\t%0,%%xmm15" : /* no outputs */ : "m"(uc->uc_mcontext.fpregs->xmm[15]));
}
asm volatile("mov\t%0,%%r8" : /* no outputs */ : "m"(uc->uc_mcontext.r8));
asm volatile("mov\t%0,%%r9" : /* no outputs */ : "m"(uc->uc_mcontext.r9));
asm volatile("mov\t%0,%%r10" : /* no outputs */ : "m"(uc->uc_mcontext.r10));

View file

@ -1,9 +1,9 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_STRUCT_UCONTEXT_NETBSD_INTERNAL_H_
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_UCONTEXT_NETBSD_INTERNAL_H_
#include "libc/calls/ucontext.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
// clang-format off
#include "libc/calls/ucontext.h"
#define __UCONTEXT_SIZE 784
#define _UC_SIGMASK 0x01

View file

@ -65,7 +65,7 @@ struct MachineContext {
};
gregset_t gregs;
};
struct FpuState *fpregs;
struct FpuState *fpregs; /* zero when no fpu context */
uint64_t __pad1[8];
};
@ -77,6 +77,7 @@ struct ucontext {
stack_t uc_stack;
mcontext_t uc_mcontext; /* use this */
sigset_t uc_sigmask;
uint64_t __pad;
struct FpuState __fpustate; /* for cosmo on non-linux */
};