2009-11-17 09:20:58 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-05-15 14:47:53 -06:00
|
|
|
// TODO(rsc): Rewrite all nn(SP) references into name+(nn-8)(FP)
|
|
|
|
// so that go vet can check that they are correct.
|
|
|
|
|
2014-08-12 18:04:45 -06:00
|
|
|
#include "textflag.h"
|
2013-08-12 11:24:30 -06:00
|
|
|
|
2009-11-17 09:20:58 -07:00
|
|
|
//
|
|
|
|
// System call support for AMD64, FreeBSD
|
|
|
|
//
|
|
|
|
|
runtime, syscall: work around FreeBSD/amd64 kernel bug
The kernel implementation of the fast system call path,
the one invoked by the SYSCALL instruction, is broken for
restarting system calls. A C program demonstrating this is below.
Change the system calls to use INT $0x80 instead, because
that (perhaps slightly slower) system call path actually works.
I filed http://www.freebsd.org/cgi/query-pr.cgi?pr=182161.
The C program demonstrating that it is FreeBSD's fault is below.
It reports the same "Bad address" failures from wait.
#include <sys/time.h>
#include <sys/signal.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static void handler(int);
static void* looper(void*);
int
main(void)
{
int i;
struct sigaction sa;
pthread_cond_t cond;
pthread_mutex_t mu;
memset(&sa, 0, sizeof sa);
sa.sa_handler = handler;
sa.sa_flags = SA_RESTART;
memset(&sa.sa_mask, 0xff, sizeof sa.sa_mask);
sigaction(SIGCHLD, &sa, 0);
for(i=0; i<2; i++)
pthread_create(0, 0, looper, 0);
pthread_mutex_init(&mu, 0);
pthread_mutex_lock(&mu);
pthread_cond_init(&cond, 0);
for(;;)
pthread_cond_wait(&cond, &mu);
return 0;
}
static void
handler(int sig)
{
}
int
mywait4(int pid, int *stat, int options, struct rusage *rusage)
{
int result;
asm("movq %%rcx, %%r10; syscall"
: "=a" (result)
: "a" (7),
"D" (pid),
"S" (stat),
"d" (options),
"c" (rusage));
}
static void*
looper(void *v)
{
int pid, stat, out;
struct rusage rusage;
for(;;) {
if((pid = fork()) == 0)
_exit(0);
out = mywait4(pid, &stat, 0, &rusage);
if(out != pid) {
printf("wait4 returned %d\n", out);
}
}
}
Fixes #6372.
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/13582047
2013-09-16 12:04:32 -06:00
|
|
|
// The SYSCALL variant for invoking system calls is broken in FreeBSD.
|
|
|
|
// See comment at top of ../runtime/sys_freebsd_amd64.c and
|
|
|
|
// golang.org/issue/6372.
|
|
|
|
#define SYSCALL MOVQ R10, CX; INT $0x80
|
|
|
|
|
2009-11-17 09:20:58 -07:00
|
|
|
// func Syscall(trap int64, a1, a2, a3 int64) (r1, r2, err int64);
|
|
|
|
// func Syscall6(trap int64, a1, a2, a3, a4, a5, a6 int64) (r1, r2, err int64);
|
2012-06-25 18:26:19 -06:00
|
|
|
// func Syscall9(trap int64, a1, a2, a3, a4, a5, a6, a7, a8, a9 int64) (r1, r2, err int64)
|
2009-11-17 09:20:58 -07:00
|
|
|
// Trap # in AX, args in DI SI DX, return in AX DX
|
|
|
|
|
2014-05-15 14:47:53 -06:00
|
|
|
TEXT ·Syscall(SB),NOSPLIT,$0-56
|
2014-09-11 22:23:03 -06:00
|
|
|
GO_ARGS
|
2009-11-17 09:20:58 -07:00
|
|
|
CALL runtime·entersyscall(SB)
|
|
|
|
MOVQ 16(SP), DI
|
|
|
|
MOVQ 24(SP), SI
|
|
|
|
MOVQ 32(SP), DX
|
|
|
|
MOVQ $0, R10
|
|
|
|
MOVQ $0, R8
|
|
|
|
MOVQ $0, R9
|
|
|
|
MOVQ 8(SP), AX // syscall entry
|
|
|
|
SYSCALL
|
|
|
|
JCC ok
|
|
|
|
MOVQ $-1, 40(SP) // r1
|
|
|
|
MOVQ $0, 48(SP) // r2
|
|
|
|
MOVQ AX, 56(SP) // errno
|
|
|
|
CALL runtime·exitsyscall(SB)
|
|
|
|
RET
|
|
|
|
ok:
|
|
|
|
MOVQ AX, 40(SP) // r1
|
|
|
|
MOVQ DX, 48(SP) // r2
|
|
|
|
MOVQ $0, 56(SP) // errno
|
|
|
|
CALL runtime·exitsyscall(SB)
|
|
|
|
RET
|
|
|
|
|
2014-05-15 14:47:53 -06:00
|
|
|
TEXT ·Syscall6(SB),NOSPLIT,$0-80
|
2014-09-11 22:23:03 -06:00
|
|
|
GO_ARGS
|
2009-11-17 09:20:58 -07:00
|
|
|
CALL runtime·entersyscall(SB)
|
|
|
|
MOVQ 16(SP), DI
|
|
|
|
MOVQ 24(SP), SI
|
|
|
|
MOVQ 32(SP), DX
|
|
|
|
MOVQ 40(SP), R10
|
|
|
|
MOVQ 48(SP), R8
|
|
|
|
MOVQ 56(SP), R9
|
|
|
|
MOVQ 8(SP), AX // syscall entry
|
|
|
|
SYSCALL
|
|
|
|
JCC ok6
|
|
|
|
MOVQ $-1, 64(SP) // r1
|
|
|
|
MOVQ $0, 72(SP) // r2
|
|
|
|
MOVQ AX, 80(SP) // errno
|
|
|
|
CALL runtime·exitsyscall(SB)
|
|
|
|
RET
|
|
|
|
ok6:
|
|
|
|
MOVQ AX, 64(SP) // r1
|
|
|
|
MOVQ DX, 72(SP) // r2
|
|
|
|
MOVQ $0, 80(SP) // errno
|
|
|
|
CALL runtime·exitsyscall(SB)
|
|
|
|
RET
|
|
|
|
|
2014-05-15 14:47:53 -06:00
|
|
|
TEXT ·Syscall9(SB),NOSPLIT,$0-104
|
2014-09-11 22:23:03 -06:00
|
|
|
GO_ARGS
|
2012-06-25 18:26:19 -06:00
|
|
|
CALL runtime·entersyscall(SB)
|
|
|
|
MOVQ 8(SP), AX
|
|
|
|
MOVQ 16(SP), DI
|
|
|
|
MOVQ 24(SP), SI
|
|
|
|
MOVQ 32(SP), DX
|
|
|
|
MOVQ 40(SP), R10
|
|
|
|
MOVQ 48(SP), R8
|
|
|
|
MOVQ 56(SP), R9
|
|
|
|
|
|
|
|
// shift around the last three arguments so they're at the
|
|
|
|
// top of the stack when the syscall is called.
|
|
|
|
MOVQ 64(SP), R11 // arg 7
|
|
|
|
MOVQ R11, 8(SP)
|
|
|
|
MOVQ 72(SP), R11 // arg 8
|
|
|
|
MOVQ R11, 16(SP)
|
|
|
|
MOVQ 80(SP), R11 // arg 9
|
|
|
|
MOVQ R11, 24(SP)
|
|
|
|
|
|
|
|
SYSCALL
|
|
|
|
JCC ok9
|
|
|
|
MOVQ $-1, 88(SP) // r1
|
|
|
|
MOVQ $0, 96(SP) // r2
|
|
|
|
MOVQ AX, 104(SP) // errno
|
|
|
|
CALL runtime·exitsyscall(SB)
|
|
|
|
RET
|
|
|
|
ok9:
|
|
|
|
MOVQ AX, 88(SP) // r1
|
|
|
|
MOVQ DX, 96(SP) // r2
|
|
|
|
MOVQ $0, 104(SP) // errno
|
|
|
|
CALL runtime·exitsyscall(SB)
|
|
|
|
RET
|
|
|
|
|
2014-05-15 14:47:53 -06:00
|
|
|
TEXT ·RawSyscall(SB),NOSPLIT,$0-56
|
2014-09-11 22:23:03 -06:00
|
|
|
GO_ARGS
|
2009-11-17 09:20:58 -07:00
|
|
|
MOVQ 16(SP), DI
|
|
|
|
MOVQ 24(SP), SI
|
|
|
|
MOVQ 32(SP), DX
|
|
|
|
MOVQ $0, R10
|
|
|
|
MOVQ $0, R8
|
|
|
|
MOVQ $0, R9
|
|
|
|
MOVQ 8(SP), AX // syscall entry
|
|
|
|
SYSCALL
|
|
|
|
JCC ok1
|
|
|
|
MOVQ $-1, 40(SP) // r1
|
|
|
|
MOVQ $0, 48(SP) // r2
|
|
|
|
MOVQ AX, 56(SP) // errno
|
|
|
|
RET
|
|
|
|
ok1:
|
|
|
|
MOVQ AX, 40(SP) // r1
|
|
|
|
MOVQ DX, 48(SP) // r2
|
|
|
|
MOVQ $0, 56(SP) // errno
|
|
|
|
RET
|
2011-03-16 20:03:01 -06:00
|
|
|
|
2014-05-15 14:47:53 -06:00
|
|
|
TEXT ·RawSyscall6(SB),NOSPLIT,$0-80
|
2014-09-11 22:23:03 -06:00
|
|
|
GO_ARGS
|
2011-03-16 20:03:01 -06:00
|
|
|
MOVQ 16(SP), DI
|
|
|
|
MOVQ 24(SP), SI
|
|
|
|
MOVQ 32(SP), DX
|
|
|
|
MOVQ 40(SP), R10
|
|
|
|
MOVQ 48(SP), R8
|
|
|
|
MOVQ 56(SP), R9
|
|
|
|
MOVQ 8(SP), AX // syscall entry
|
|
|
|
SYSCALL
|
|
|
|
JCC ok2
|
|
|
|
MOVQ $-1, 64(SP) // r1
|
|
|
|
MOVQ $0, 72(SP) // r2
|
|
|
|
MOVQ AX, 80(SP) // errno
|
|
|
|
RET
|
|
|
|
ok2:
|
|
|
|
MOVQ AX, 64(SP) // r1
|
|
|
|
MOVQ DX, 72(SP) // r2
|
|
|
|
MOVQ $0, 80(SP) // errno
|
|
|
|
RET
|