Commit c08d8834 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov Committed by Russ Cox

runtime: convert cgocall to Go

LGTM=khr, rsc
R=golang-codereviews, khr, rsc
CC=golang-codereviews
https://golang.org/cl/131670043
parent 503bcd46
...@@ -2,13 +2,6 @@ ...@@ -2,13 +2,6 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
#include "runtime.h"
#include "arch_GOARCH.h"
#include "stack.h"
#include "cgocall.h"
#include "race.h"
#include "../../cmd/ld/textflag.h"
// Cgo call and callback support. // Cgo call and callback support.
// //
// To call into the C function f from Go, the cgo-generated code calls // To call into the C function f from Go, the cgo-generated code calls
...@@ -84,56 +77,42 @@ ...@@ -84,56 +77,42 @@
// _cgoexp_GoF immediately returns to crosscall2, which restores the // _cgoexp_GoF immediately returns to crosscall2, which restores the
// callee-save registers for gcc and returns to GoF, which returns to f. // callee-save registers for gcc and returns to GoF, which returns to f.
void *_cgo_init; /* filled in by dynamic linker when Cgo is available */ package runtime
static int64 cgosync; /* represents possible synchronization in C code */
static void unwindm(void); import "unsafe"
// Call from Go to C. // Call from Go to C.
func cgocall(fn, arg unsafe.Pointer) {
static void endcgo(void); cgocall_errno(fn, arg)
static FuncVal endcgoV = { endcgo };
void
runtime·cgocall(void (*fn)(void*), void *arg)
{
runtime·cgocall_errno(fn, arg);
} }
int32 func cgocall_errno(fn, arg unsafe.Pointer) int32 {
runtime·cgocall_errno(void (*fn)(void*), void *arg) if !iscgo && GOOS != "solaris" && GOOS != "windows" {
{ gothrow("cgocall unavailable")
Defer d; }
int32 errno;
if(!runtime·iscgo && !Solaris && !Windows)
runtime·throw("cgocall unavailable");
if(fn == 0) if fn == nil {
runtime·throw("cgocall nil"); gothrow("cgocall nil")
}
if(raceenabled) if raceenabled {
runtime·racereleasemerge(&cgosync); racereleasemerge(unsafe.Pointer(&racecgosync))
}
// Create an extra M for callbacks on threads not created by Go on first cgo call. // Create an extra M for callbacks on threads not created by Go on first cgo call.
if(runtime·needextram && runtime·cas(&runtime·needextram, 1, 0)) if needextram == 1 && cas(&needextram, 1, 0) {
runtime·newextram(); newextram()
}
g->m->ncgocall++;
/* /*
* Mutex g to m to ensure we stay on the same stack if we do a * Lock g to m to ensure we stay on the same stack if we do a
* cgo callback. Add entry to defer stack in case of panic. * cgo callback. Add entry to defer stack in case of panic.
*/ */
runtime·lockOSThread(); lockOSThread()
d.fn = &endcgoV; mp := getg().m
d.siz = 0; mp.ncgocall++
d.link = g->defer; mp.ncgo++
d.argp = NoArgs; defer endcgo(mp)
d.special = true;
g->defer = &d;
g->m->ncgo++;
/* /*
* Announce we are entering a system call * Announce we are entering a system call
...@@ -146,182 +125,144 @@ runtime·cgocall_errno(void (*fn)(void*), void *arg) ...@@ -146,182 +125,144 @@ runtime·cgocall_errno(void (*fn)(void*), void *arg)
* so it is safe to call while "in a system call", outside * so it is safe to call while "in a system call", outside
* the $GOMAXPROCS accounting. * the $GOMAXPROCS accounting.
*/ */
runtime·entersyscall(); entersyscall()
errno = runtime·asmcgocall_errno(fn, arg); errno := asmcgocall_errno(fn, arg)
runtime·exitsyscall(); exitsyscall()
if(g->defer != &d || d.fn != &endcgoV) return errno
runtime·throw("runtime: bad defer entry in cgocallback");
g->defer = d.link;
endcgo();
return errno;
} }
static void func endcgo(mp *m) {
endcgo(void) mp.ncgo--
{ if mp.ncgo == 0 {
runtime·unlockOSThread();
g->m->ncgo--;
if(g->m->ncgo == 0) {
// We are going back to Go and are not in a recursive // We are going back to Go and are not in a recursive
// call. Let the GC collect any memory allocated via // call. Let the GC collect any memory allocated via
// _cgo_allocate that is no longer referenced. // _cgo_allocate that is no longer referenced.
g->m->cgomal = nil; mp.cgomal = nil
}
if raceenabled {
raceacquire(unsafe.Pointer(&racecgosync))
} }
if(raceenabled) unlockOSThread() // invalidates mp
runtime·raceacquire(&cgosync);
} }
// Helper functions for cgo code. // Helper functions for cgo code.
void (*_cgo_malloc)(void*); // Filled by schedinit from corresponding C variables,
void (*_cgo_free)(void*); // which are in turn filled in by dynamic linker when Cgo is available.
var cgoMalloc, cgoFree unsafe.Pointer
void*
runtime·cmalloc(uintptr n) func cmalloc(n uintptr) unsafe.Pointer {
{ var args struct {
struct { n uint64
uint64 n; ret unsafe.Pointer
void *ret; }
} a; args.n = uint64(n)
cgocall(cgoMalloc, unsafe.Pointer(&args))
a.n = n; if args.ret == nil {
a.ret = nil; gothrow("C malloc failed")
runtime·cgocall(_cgo_malloc, &a); }
if(a.ret == nil) return args.ret
runtime·throw("runtime: C malloc failed");
return a.ret;
} }
void func cfree(p unsafe.Pointer) {
runtime·cfree(void *p) cgocall(cgoFree, p)
{
runtime·cgocall(_cgo_free, p);
} }
// Call from C back to Go. // Call from C back to Go.
//go:nosplit
static FuncVal unwindmf = {unwindm}; func cgocallbackg() {
if gp := getg(); gp != gp.m.curg {
typedef struct CallbackArgs CallbackArgs; println("runtime: bad g in cgocallback")
struct CallbackArgs exit(2)
{
FuncVal *fn;
void *arg;
uintptr argsize;
};
// Location of callback arguments depends on stack frame layout
// and size of stack frame of cgocallback_gofunc.
// On arm, stack frame is two words and there's a saved LR between
// SP and the stack frame and between the stack frame and the arguments.
#ifdef GOARCH_arm
#define CBARGS (CallbackArgs*)((byte*)g->m->g0->sched.sp+4*sizeof(void*))
#endif
// On amd64, stack frame is one word, plus caller PC.
#ifdef GOARCH_amd64
#define CBARGS (CallbackArgs*)((byte*)g->m->g0->sched.sp+2*sizeof(void*))
#endif
// Unimplemented on amd64p32
#ifdef GOARCH_amd64p32
#define CBARGS (CallbackArgs*)(nil)
#endif
// On 386, stack frame is three words, plus caller PC.
#ifdef GOARCH_386
#define CBARGS (CallbackArgs*)((byte*)g->m->g0->sched.sp+4*sizeof(void*))
#endif
void runtime·cgocallbackg1(void);
#pragma textflag NOSPLIT
void
runtime·cgocallbackg(void)
{
if(g != g->m->curg) {
runtime·prints("runtime: bad g in cgocallback");
runtime·exit(2);
} }
runtime·exitsyscall(); // coming out of cgo call exitsyscall() // coming out of cgo call
runtime·cgocallbackg1(); cgocallbackg1()
runtime·entersyscall(); // going back to cgo call entersyscall() // going back to cgo call
} }
void func cgocallbackg1() {
runtime·cgocallbackg1(void) gp := getg()
{ if gp.m.needextram {
CallbackArgs *cb; gp.m.needextram = false
Defer d; newextram()
if(g->m->needextram) {
g->m->needextram = 0;
runtime·newextram();
} }
// Add entry to defer stack in case of panic. // Add entry to defer stack in case of panic.
d.fn = &unwindmf; restore := true
d.siz = 0; defer unwindm(&restore)
d.link = g->defer;
d.argp = NoArgs;
d.special = true;
g->defer = &d;
if(raceenabled) if raceenabled {
runtime·raceacquire(&cgosync); raceacquire(unsafe.Pointer(&racecgosync))
}
type args struct {
fn *funcval
arg unsafe.Pointer
argsize uintptr
}
var cb *args
// Location of callback arguments depends on stack frame layout
// and size of stack frame of cgocallback_gofunc.
sp := gp.m.g0.sched.sp
switch GOARCH {
default:
gothrow("cgocallbackg is unimplemented on arch")
case "arm":
// On arm, stack frame is two words and there's a saved LR between
// SP and the stack frame and between the stack frame and the arguments.
cb = (*args)(unsafe.Pointer(sp + 4*ptrSize))
case "amd64":
// On amd64, stack frame is one word, plus caller PC.
cb = (*args)(unsafe.Pointer(sp + 2*ptrSize))
case "386":
// On 386, stack frame is three words, plus caller PC.
cb = (*args)(unsafe.Pointer(sp + 4*ptrSize))
}
// Invoke callback. // Invoke callback.
cb = CBARGS; newstackcall(cb.fn, cb.arg, uint32(cb.argsize))
runtime·newstackcall(cb->fn, cb->arg, cb->argsize);
if(raceenabled) if raceenabled {
runtime·racereleasemerge(&cgosync); racereleasemerge(unsafe.Pointer(&racecgosync))
}
// Pop defer.
// Do not unwind m->g0->sched.sp. // Do not unwind m->g0->sched.sp.
// Our caller, cgocallback, will do that. // Our caller, cgocallback, will do that.
if(g->defer != &d || d.fn != &unwindmf) restore = false
runtime·throw("runtime: bad defer entry in cgocallback");
g->defer = d.link;
} }
static void func unwindm(restore *bool) {
unwindm(void) if !*restore {
{ return
}
// Restore sp saved by cgocallback during // Restore sp saved by cgocallback during
// unwind of g's stack (see comment at top of file). // unwind of g's stack (see comment at top of file).
switch(thechar){ mp := acquirem()
sched := &mp.g0.sched
switch GOARCH {
default: default:
runtime·throw("runtime: unwindm not implemented"); gothrow("unwindm not implemented")
case '8': case "386", "amd64":
case '6': sched.sp = *(*uintptr)(unsafe.Pointer(sched.sp))
g->m->g0->sched.sp = *(uintptr*)g->m->g0->sched.sp; case "arm":
break; sched.sp = *(*uintptr)(unsafe.Pointer(sched.sp + 4))
case '5':
g->m->g0->sched.sp = *(uintptr*)((byte*)g->m->g0->sched.sp + 4);
break;
} }
releasem(mp)
} }
void // called from assembly
runtime·badcgocallback(void) // called from assembly func badcgocallback() {
{ gothrow("misaligned stack in cgocallback")
runtime·throw("runtime: misaligned stack in cgocallback");
} }
void // called from (incomplete) assembly
runtime·cgounimpl(void) // called from (incomplete) assembly func cgounimpl() {
{ gothrow("cgo not implemented")
runtime·throw("runtime: cgo not implemented");
} }
// For cgo-using programs with external linking, var racecgosync uint64 // represents possible synchronization in C code
// export "main" (defined in assembly) so that libc can handle basic
// C runtime startup and call the Go program as if it were
// the C main function.
#pragma cgo_export_static main
...@@ -132,6 +132,21 @@ static void dropg(void); ...@@ -132,6 +132,21 @@ static void dropg(void);
extern String runtime·buildVersion; extern String runtime·buildVersion;
// For cgo-using programs with external linking,
// export "main" (defined in assembly) so that libc can handle basic
// C runtime startup and call the Go program as if it were
// the C main function.
#pragma cgo_export_static main
// Filled in by dynamic linker when Cgo is available.
void* _cgo_init;
void* _cgo_malloc;
void* _cgo_free;
// Copy for Go code.
void* runtime·cgoMalloc;
void* runtime·cgoFree;
// The bootstrap sequence is: // The bootstrap sequence is:
// //
// call osinit // call osinit
...@@ -192,6 +207,9 @@ runtime·schedinit(void) ...@@ -192,6 +207,9 @@ runtime·schedinit(void)
runtime·buildVersion.str = (uint8*)"unknown"; runtime·buildVersion.str = (uint8*)"unknown";
runtime·buildVersion.len = 7; runtime·buildVersion.len = 7;
} }
runtime·cgoMalloc = _cgo_malloc;
runtime·cgoFree = _cgo_free;
} }
extern void main·init(void); extern void main·init(void);
......
...@@ -32,6 +32,9 @@ func raceacquire(addr unsafe.Pointer) ...@@ -32,6 +32,9 @@ func raceacquire(addr unsafe.Pointer)
//go:noescape //go:noescape
func racerelease(addr unsafe.Pointer) func racerelease(addr unsafe.Pointer)
//go:noescape
func racereleasemerge(addr unsafe.Pointer)
//go:noescape //go:noescape
func raceacquireg(gp *g, addr unsafe.Pointer) func raceacquireg(gp *g, addr unsafe.Pointer)
...@@ -146,6 +149,9 @@ const ( ...@@ -146,6 +149,9 @@ const (
func gosched() func gosched()
func starttheworld() func starttheworld()
func stoptheworld() func stoptheworld()
func newextram()
func lockOSThread()
func unlockOSThread()
// exported value for testing // exported value for testing
var hashLoad = loadFactor var hashLoad = loadFactor
...@@ -282,6 +288,9 @@ func getcallersp(argp unsafe.Pointer) uintptr ...@@ -282,6 +288,9 @@ func getcallersp(argp unsafe.Pointer) uintptr
//go:noescape //go:noescape
func asmcgocall(fn, arg unsafe.Pointer) func asmcgocall(fn, arg unsafe.Pointer)
//go:noescape
func asmcgocall_errno(fn, arg unsafe.Pointer) int32
//go:noescape //go:noescape
func open(name *byte, mode, perm int32) int32 func open(name *byte, mode, perm int32) int32
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment