41
42
// know about packages). The gcc-compiled C function f calls GoF.
43
44
// GoF calls crosscall2(_cgoexp_GoF, frame, framesize). Crosscall2
44
// (in cgo/$GOOS.S, a gcc-compiled assembly file) is a two-argument
45
// (in cgo/gcc_$GOARCH.S, a gcc-compiled assembly file) is a two-argument
45
46
// adapter from the gcc function call ABI to the 6c function call ABI.
46
47
// It is called from gcc to call 6c functions. In this case it calls
47
48
// _cgoexp_GoF(frame, framesize), still running on m->g0's stack
82
83
// _cgoexp_GoF immediately returns to crosscall2, which restores the
83
84
// callee-save registers for gcc and returns to GoF, which returns to f.
85
void *initcgo; /* filled in by dynamic linker when Cgo is available */
87
static void unlockm(void);
86
void *_cgo_init; /* filled in by dynamic linker when Cgo is available */
87
static int64 cgosync; /* represents possible synchronization in C code */
89
// These two are only used by the architecture where TLS based storage isn't
90
// the default for g and m (e.g., ARM)
91
void *_cgo_load_gm; /* filled in by dynamic linker when Cgo is available */
92
void *_cgo_save_gm; /* filled in by dynamic linker when Cgo is available */
88
94
static void unwindm(void);
90
96
// Call from Go to C.
98
static void endcgo(void);
99
static FuncVal endcgoV = { endcgo };
101
// Gives a hint that the next syscall
102
// executed by the current goroutine will block.
103
// Currently used only on windows.
105
net·runtime_blockingSyscallHint(void)
107
g->blockingsyscall = true;
93
111
runtime·cgocall(void (*fn)(void*), void *arg)
116
runtime·asmcgocall(fn, arg);
97
120
if(!runtime·iscgo && !Windows)
98
121
runtime·throw("cgocall unavailable");
101
124
runtime·throw("cgocall nil");
127
runtime·racereleasemerge(&cgosync);
106
132
* Lock g to m to ensure we stay on the same stack if we do a
133
* cgo callback. Add entry to defer stack in case of panic.
110
if(m->lockedg == nil) {
135
runtime·lockOSThread();
139
d.argp = (void*)-1; // unused because unlockm never recovers
114
// Add entry to defer stack in case of panic.
115
d.fn = (byte*)unlockm;
118
d.argp = (void*)-1; // unused because unlockm never recovers
124
147
* Announce we are entering a system call
131
154
* so it is safe to call while "in a system call", outside
132
155
* the $GOMAXPROCS accounting.
134
runtime·entersyscall();
157
if(g->blockingsyscall) {
158
g->blockingsyscall = false;
159
runtime·entersyscallblock();
161
runtime·entersyscall();
135
162
runtime·asmcgocall(fn, arg);
136
163
runtime·exitsyscall();
139
if(g->defer != &d || d.fn != (byte*)unlockm)
140
runtime·throw("runtime: bad defer entry in cgocallback");
165
if(g->defer != &d || d.fn != &endcgoV)
166
runtime·throw("runtime: bad defer entry in cgocallback");
174
runtime·unlockOSThread();
177
// We are going back to Go and are not in a recursive
178
// call. Let the GC collect any memory allocated via
179
// _cgo_allocate that is no longer referenced.
184
runtime·raceacquire(&cgosync);
154
188
runtime·NumCgoCall(int64 ret)
159
for(m=runtime·atomicloadp(&runtime·allm); m; m=m->alllink)
193
for(mp=runtime·atomicloadp(&runtime·allm); mp; mp=mp->alllink)
189
223
// Call from C back to Go.
225
static FuncVal unwindmf = {unwindm};
192
runtime·cgocallbackg(void (*fn)(void), void *arg, uintptr argsize)
228
runtime·cgocallbackg(FuncVal *fn, void *arg, uintptr argsize)
233
reflect·call(fn, arg, argsize);
197
238
runtime·throw("runtime: bad g in cgocallback");
199
240
runtime·exitsyscall(); // coming out of cgo call
201
247
// Add entry to defer stack in case of panic.
202
d.fn = (byte*)unwindm;
204
250
d.link = g->defer;
205
251
d.argp = (void*)-1; // unused because unwindm never recovers
257
runtime·raceacquire(&cgosync);
209
259
// Invoke callback.
210
reflect·call((byte*)fn, arg, argsize);
260
reflect·call(fn, arg, argsize);
263
runtime·racereleasemerge(&cgosync);
213
266
// Do not unwind m->g0->sched.sp.
214
267
// Our caller, cgocallback, will do that.
215
if(g->defer != &d || d.fn != (byte*)unwindm)
268
if(g->defer != &d || d.fn != &unwindmf)
216
269
runtime·throw("runtime: bad defer entry in cgocallback");
217
270
g->defer = d.link;
246
300
runtime·throw("runtime: cgo not implemented");
303
// For cgo-using programs with external linking,
304
// export "main" (defined in assembly) so that libc can handle basic
305
// C runtime startup and call the Go program as if it were
306
// the C main function.
307
#pragma cgo_export_static main