10754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot// Copyright 2009 The Go Authors. All rights reserved.
20754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot// Use of this source code is governed by a BSD-style
30754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot// license that can be found in the LICENSE file.
40754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
50754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#include <stdint.h>
60754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#include <stdlib.h>
70754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#include <stdio.h>
80754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
90754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#define nil ((void*)0)
100754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#define nelem(x) (sizeof(x)/sizeof((x)[0]))
110754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
120754ce079d936daf401bbf49cb69a318aa79828android-build-team Robottypedef uint32_t uint32;
130754ce079d936daf401bbf49cb69a318aa79828android-build-team Robottypedef uint64_t uint64;
140754ce079d936daf401bbf49cb69a318aa79828android-build-team Robottypedef uintptr_t uintptr;
150754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
160754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
170754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * The beginning of the per-goroutine structure,
180754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * as defined in ../pkg/runtime/runtime.h.
190754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Just enough to edit these two fields.
200754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
210754ce079d936daf401bbf49cb69a318aa79828android-build-team Robottypedef struct G G;
220754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotstruct G
230754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot{
240754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot	uintptr stacklo;
250754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot	uintptr stackhi;
260754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot};
270754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
280754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
290754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Arguments to the _cgo_thread_start call.
300754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Also known to ../pkg/runtime/runtime.h.
310754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
320754ce079d936daf401bbf49cb69a318aa79828android-build-team Robottypedef struct ThreadStart ThreadStart;
330754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotstruct ThreadStart
340754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot{
350754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot	G *g;
360754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot	uintptr *tls;
370754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot	void (*fn)(void);
380754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot};
390754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
400754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
410754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Called by 5c/6c/8c world.
420754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Makes a local copy of the ThreadStart and
430754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * calls _cgo_sys_thread_start(ts).
440754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
450754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotextern void (*_cgo_thread_start)(ThreadStart *ts);
460754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
470754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
480754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Creates a new operating system thread without updating any Go state
490754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * (OS dependent).
500754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
510754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotextern void (*_cgo_sys_thread_create)(void* (*func)(void*), void* arg);
520754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
530754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
540754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Creates the new operating system thread (OS, arch dependent).
550754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
560754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotvoid _cgo_sys_thread_start(ThreadStart *ts);
570754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
580754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
590754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Waits for the Go runtime to be initialized (OS dependent).
600754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * If runtime.SetCgoTraceback is used to set a context function,
610754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * calls the context function and returns the context value.
620754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
630754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotuintptr_t _cgo_wait_runtime_init_done();
640754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
650754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
660754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Call fn in the 6c world.
670754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
680754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotvoid crosscall_amd64(void (*fn)(void));
690754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
700754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
710754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Call fn in the 8c world.
720754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
730754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotvoid crosscall_386(void (*fn)(void));
740754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
750754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
760754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Prints error then calls abort. For linux and android.
770754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
780754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotvoid fatalf(const char* format, ...);
790754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
800754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
810754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Registers the current mach thread port for EXC_BAD_ACCESS processing.
820754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
830754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotvoid darwin_arm_init_thread_exception_port(void);
840754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
850754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
860754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * Starts a mach message server processing EXC_BAD_ACCESS.
870754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
880754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotvoid darwin_arm_init_mach_exception_handler(void);
890754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
900754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
910754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * The cgo context function. See runtime.SetCgoTraceback.
920754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
930754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotstruct context_arg {
940754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot	uintptr_t Context;
950754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot};
960754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotextern void (*(_cgo_get_context_function(void)))(struct context_arg*);
970754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
980754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot/*
990754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot * TSAN support.  This is only useful when building with
1000754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot *   CGO_CFLAGS="-fsanitize=thread" CGO_LDFLAGS="-fsanitize=thread" go install
1010754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot */
1020754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#undef CGO_TSAN
1030754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#if defined(__has_feature)
1040754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot# if __has_feature(thread_sanitizer)
1050754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#  define CGO_TSAN
1060754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot# endif
1070754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#elif defined(__SANITIZE_THREAD__)
1080754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot# define CGO_TSAN
1090754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#endif
1100754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
1110754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#ifdef CGO_TSAN
1120754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
1130754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot// These must match the definitions in yesTsanProlog in cmd/cgo/out.go.
1140754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot// In general we should call _cgo_tsan_acquire when we enter C code,
1150754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot// and call _cgo_tsan_release when we return to Go code.
1160754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot// This is only necessary when calling code that might be instrumented
1170754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot// by TSAN, which mostly means system library calls that TSAN intercepts.
1180754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot// See the comment in cmd/cgo/out.go for more details.
1190754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
1200754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotlong long _cgo_sync __attribute__ ((common));
1210754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
1220754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotextern void __tsan_acquire(void*);
1230754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotextern void __tsan_release(void*);
1240754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
1250754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot__attribute__ ((unused))
1260754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotstatic void _cgo_tsan_acquire() {
1270754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot	__tsan_acquire(&_cgo_sync);
1280754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot}
1290754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
1300754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot__attribute__ ((unused))
1310754ce079d936daf401bbf49cb69a318aa79828android-build-team Robotstatic void _cgo_tsan_release() {
1320754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot	__tsan_release(&_cgo_sync);
1330754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot}
1340754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
1350754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#else // !defined(CGO_TSAN)
1360754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
1370754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#define _cgo_tsan_acquire()
1380754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#define _cgo_tsan_release()
1390754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot
1400754ce079d936daf401bbf49cb69a318aa79828android-build-team Robot#endif // !defined(CGO_TSAN)
141