topenbsd updates (thanks Pascal Stumpf) - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 20f5692b7fe1190e6c518ed82764ded41dd92986
 (DIR) parent c934984537a7673f980d731e10baec18d0c97c05
 (HTM) Author: Russ Cox <rsc@swtch.com>
       Date:   Sat, 14 Jul 2012 10:16:54 -0400
       
       openbsd updates (thanks Pascal Stumpf)
       
       R=rsc
       http://codereview.appspot.com/6401049
       
       Diffstat:
         M include/u.h                         |       2 ++
         M src/libthread/OpenBSD-386-asm.s     |      99 -------------------------------
         M src/libthread/OpenBSD-power-asm.S   |      37 -------------------------------
         A src/libthread/OpenBSD-x86_64-asm.S  |      44 +++++++++++++++++++++++++++++++
         A src/libthread/OpenBSD-x86_64.c      |      32 +++++++++++++++++++++++++++++++
         M src/libthread/mkfile                |       2 +-
         M src/libthread/sysofiles.sh          |       2 +-
         M src/libthread/threadimpl.h          |       2 ++
       
       8 files changed, 82 insertions(+), 138 deletions(-)
       ---
 (DIR) diff --git a/include/u.h b/include/u.h
       t@@ -116,6 +116,8 @@ typedef long p9jmp_buf[sizeof(sigjmp_buf)/sizeof(long)];
        #        undef _NEEDULONG
        #elif defined(__OpenBSD__)
        #        include <sys/types.h>
       +#        include <pthread.h>
       +#        define PLAN9PORT_USING_PTHREADS 1
        #        undef _NEEDUSHORT
        #        undef _NEEDUINT
        #        undef _NEEDULONG
 (DIR) diff --git a/src/libthread/OpenBSD-386-asm.s b/src/libthread/OpenBSD-386-asm.s
       t@@ -1,100 +1 @@
        #include "FreeBSD-386-asm.s"
       -
       -/*
       - * Copyright (c) 2000 Peter Wemm <peter@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 <sys/syscall.h>
       -#include <machine/asm.h>
       -
       -ENTRY(rfork_thread)
       -        pushl   %ebp
       -        movl    %esp, %ebp
       -        pushl   %esi
       -
       -        /*
       -        * Push thread info onto the new thread's stack
       -        */
       -        movl    12(%ebp), %esi  # get stack addr
       -
       -        subl    $4, %esi
       -        movl    20(%ebp), %eax  # get start argument
       -        movl    %eax, (%esi)
       -
       -        subl    $4, %esi
       -        movl    16(%ebp), %eax  # get start thread address
       -        movl    %eax, (%esi)
       -
       -        /*
       -        * Prepare and execute the thread creation syscall
       -        */
       -        pushl   8(%ebp)
       -        pushl   $0
       -        movl    $SYS_rfork, %eax
       -        int     $0x80
       -        jb      2f
       -
       -        /*
       -        * Check to see if we are in the parent or child
       -        */
       -        cmpl    $0, %edx
       -        jnz     1f
       -        addl    $8, %esp
       -        popl    %esi
       -        movl    %ebp, %esp
       -        popl    %ebp
       -        ret
       -        .p2align 2
       -
       -        /*
       -        * If we are in the child (new thread), then
       -        * set-up the call to the internal subroutine.  If it
       -        * returns, then call __exit.
       -        */
       -1:
       -        movl    %esi,%esp
       -        popl    %eax
       -        call    *%eax
       -        addl    $4, %esp
       -
       -        /*
       -        * Exit system call
       -        */
       -        pushl   %eax
       -        pushl   $0
       -        movl    $SYS_exit, %eax
       -        int     $0x80
       -
       -        /*
       -        * Branch here if the thread creation fails:
       -        */
       -2:
       -        addl    $8, %esp
       -        popl    %esi
       -        movl    %ebp, %esp
       -        popl    %ebp
       -        PIC_PROLOGUE
       -        jmp     PIC_PLT(_C_LABEL(__cerror))
       -
 (DIR) diff --git a/src/libthread/OpenBSD-power-asm.S b/src/libthread/OpenBSD-power-asm.S
       t@@ -1,6 +1,3 @@
       -#include <sys/syscall.h>
       -#include <machine/asm.h>
       -
        ENTRY(_tas)
                li        %r0, 0
                mr        %r4, %r3
       t@@ -89,37 +86,3 @@ ENTRY(_setmcontext)
        
                lwz        %r3,        6*4(%r3)
                blr
       -
       -ENTRY(rfork_thread)
       -        /* sanity check */
       -        cmpwi        %r4, 0
       -        beq        1f
       -        cmpwi        %r5, 0
       -        beq        1f
       -        
       -        mr        %r7,%r4
       -        
       -        /* call rfork */
       -        li        %r0, SYS_rfork
       -        sc
       -        cmpwi        %r0, 0
       -        bne        2f
       -        
       -        /* check if we are parent or child */
       -        cmpwi        %r3, 0
       -        bnelr
       -        
       -        /* child */
       -        mtlr        %r5        /* fp */
       -        mr        %r3, %r6        /* arg */
       -        mr        %r1, %r7        /* new sp */
       -        blrl
       -        
       -        /* child returned, call _exit */
       -        li        %r0, SYS_exit
       -        sc
       -1:
       -        li        %r3, -1
       -2:
       -        b PIC_PLT(_C_LABEL(__cerror))
       -
 (DIR) diff --git a/src/libthread/OpenBSD-x86_64-asm.S b/src/libthread/OpenBSD-x86_64-asm.S
       t@@ -0,0 +1,44 @@
       +.text
       +.align 8
       +
       +.globl libthread_getmcontext
       +libthread_getmcontext:
       +        movq        $1, 0*8(%rdi)  // rax
       +        movq        %rbx, 1*8(%rdi)
       +        movq        %rcx, 2*8(%rdi)
       +        movq        %rdx, 3*8(%rdi)
       +        movq        %rsi, 4*8(%rdi)
       +        movq        %rdi, 5*8(%rdi)
       +        movq        %rbp, 6*8(%rdi)
       +        movq        %rsp, 7*8(%rdi)
       +        movq        %r8, 8*8(%rdi)
       +        movq        %r9, 9*8(%rdi)
       +        movq        %r10, 10*8(%rdi)
       +        movq        %r11, 11*8(%rdi)
       +        movq        %r12, 12*8(%rdi)
       +        movq        %r13, 13*8(%rdi)
       +        movq        %r14, 14*8(%rdi)
       +        movq        %r15, 15*8(%rdi)
       +        movq        $0, %rax
       +        ret
       +
       +.globl libthread_setmcontext
       +libthread_setmcontext:
       +        movq        0*8(%rdi), %rax
       +        movq        1*8(%rdi), %rbx
       +        movq        2*8(%rdi), %rcx
       +        movq        3*8(%rdi), %rdx
       +        movq        4*8(%rdi), %rsi
       +        // %rdi later
       +        movq        6*8(%rdi), %rbp
       +        movq        7*8(%rdi), %rsp
       +        movq        8*8(%rdi), %r8
       +        movq        9*8(%rdi), %r9
       +        movq        10*8(%rdi), %r10
       +        movq        11*8(%rdi), %r11
       +        movq        12*8(%rdi), %r12
       +        movq        13*8(%rdi), %r13
       +        movq        14*8(%rdi), %r14
       +        movq        15*8(%rdi), %r15
       +        movq        5*8(%rdi), %rdi
       +        ret
 (DIR) diff --git a/src/libthread/OpenBSD-x86_64.c b/src/libthread/OpenBSD-x86_64.c
       t@@ -0,0 +1,32 @@
       +#include "threadimpl.h"
       +
       +void
       +makecontext(ucontext_t *uc, void (*fn)(void), int argc, ...)
       +{
       +        uintptr *sp;
       +        va_list arg;
       +
       +//fprint(2, "makecontext %d\n", argc);
       +        if(argc != 2)
       +                sysfatal("libthread: makecontext misused");
       +        va_start(arg, argc);
       +        uc->mc.di = va_arg(arg, uint);
       +        uc->mc.si = va_arg(arg, uint);
       +//fprint(2, "%ux %ux\n", uc->mc.di, uc->mc.si);
       +        va_end(arg);
       +
       +        sp = (uintptr*)((char*)uc->uc_stack.ss_sp+uc->uc_stack.ss_size);
       +        *--sp = 0;  // fn's return address
       +        *--sp = (uintptr)fn;  // return address of setcontext
       +        uc->mc.sp = (uintptr)sp;
       +}
       +
       +int
       +swapcontext(ucontext_t *oucp, ucontext_t *ucp)
       +{
       +        if(getcontext(oucp) == 0)
       +                setcontext(ucp);
       +        return 0;
       +}
       +
       +
 (DIR) diff --git a/src/libthread/mkfile b/src/libthread/mkfile
       t@@ -16,7 +16,7 @@ OFILES=\
        <$PLAN9/src/mksyslib
        
        HFILES=thread.h threadimpl.h
       -OpenBSD.$O FreeBSD.$O: BSD.c
       +FreeBSD.$O: BSD.c
        NetBSD.$O: Linux.c
        
        tprimes: test/tprimes.$O
 (DIR) diff --git a/src/libthread/sysofiles.sh b/src/libthread/sysofiles.sh
       t@@ -18,7 +18,7 @@ case "$tag" in
                echo ${SYSNAME}-${OBJTYPE}-asm.o $SYSNAME-${OBJTYPE}.o pthread.o
                ;;
        *-OpenBSD-*)
       -        echo ${SYSNAME}-${OBJTYPE}-asm.o ${SYSNAME}-${OBJTYPE}.o $SYSNAME.o
       +        echo ${SYSNAME}-${OBJTYPE}-asm.o ${SYSNAME}-${OBJTYPE}.o pthread.o
                ;;
        *)
                echo pthread.o
 (DIR) diff --git a/src/libthread/threadimpl.h b/src/libthread/threadimpl.h
       t@@ -56,6 +56,8 @@ extern        void                makecontext(ucontext_t*, void(*)(), int, ...);
        #        define ucontext_t libthread_ucontext_t
        #        if defined __i386__
        #                include "386-ucontext.h"
       +#        elif defined __amd64__
       +#                include "x86_64-ucontext.h"
        #        else
        #                include "power-ucontext.h"
        #        endif