rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/runtime/stubs.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime
     6  
     7  import "unsafe"
     8  
     9  // Declarations for runtime services implemented in C or assembly.
    10  
    11  const ptrSize = 4 << (^uintptr(0) >> 63)             // unsafe.Sizeof(uintptr(0)) but an ideal const
    12  const regSize = 4 << (^uintreg(0) >> 63)             // unsafe.Sizeof(uintreg(0)) but an ideal const
    13  const spAlign = 1*(1-goarch_arm64) + 16*goarch_arm64 // SP alignment: 1 normally, 16 for ARM64
    14  
    15  // Should be a built-in for unsafe.Pointer?
    16  //go:nosplit
    17  func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
    18  	return unsafe.Pointer(uintptr(p) + x)
    19  }
    20  
    21  // getg returns the pointer to the current g.
    22  // The compiler rewrites calls to this function into instructions
    23  // that fetch the g directly (from TLS or from the dedicated register).
    24  func getg() *g
    25  
    26  // mcall switches from the g to the g0 stack and invokes fn(g),
    27  // where g is the goroutine that made the call.
    28  // mcall saves g's current PC/SP in g->sched so that it can be restored later.
    29  // It is up to fn to arrange for that later execution, typically by recording
    30  // g in a data structure, causing something to call ready(g) later.
    31  // mcall returns to the original goroutine g later, when g has been rescheduled.
    32  // fn must not return at all; typically it ends by calling schedule, to let the m
    33  // run other goroutines.
    34  //
    35  // mcall can only be called from g stacks (not g0, not gsignal).
    36  //go:noescape
    37  func mcall(fn func(*g))
    38  
    39  // systemstack runs fn on a system stack.
    40  // If systemstack is called from the per-OS-thread (g0) stack, or
    41  // if systemstack is called from the signal handling (gsignal) stack,
    42  // systemstack calls fn directly and returns.
    43  // Otherwise, systemstack is being called from the limited stack
    44  // of an ordinary goroutine. In this case, systemstack switches
    45  // to the per-OS-thread stack, calls fn, and switches back.
    46  // It is common to use a func literal as the argument, in order
    47  // to share inputs and outputs with the code around the call
    48  // to system stack:
    49  //
    50  //	... set up y ...
    51  //	systemstack(func() {
    52  //		x = bigcall(y)
    53  //	})
    54  //	... use x ...
    55  //
    56  //go:noescape
    57  func systemstack(fn func())
    58  
    59  func badsystemstack() {
    60  	throw("systemstack called from unexpected goroutine")
    61  }
    62  
    63  // memclr clears n bytes starting at ptr.
    64  // in memclr_*.s
    65  //go:noescape
    66  func memclr(ptr unsafe.Pointer, n uintptr)
    67  
    68  //go:linkname reflect_memclr reflect.memclr
    69  func reflect_memclr(ptr unsafe.Pointer, n uintptr) {
    70  	memclr(ptr, n)
    71  }
    72  
    73  // memmove copies n bytes from "from" to "to".
    74  // in memmove_*.s
    75  //go:noescape
    76  func memmove(to, from unsafe.Pointer, n uintptr)
    77  
    78  //go:linkname reflect_memmove reflect.memmove
    79  func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
    80  	memmove(to, from, n)
    81  }
    82  
    83  // exported value for testing
    84  var hashLoad = loadFactor
    85  
    86  // in asm_*.s
    87  func fastrand1() uint32
    88  
    89  // in asm_*.s
    90  //go:noescape
    91  func memeq(a, b unsafe.Pointer, size uintptr) bool
    92  
    93  // noescape hides a pointer from escape analysis.  noescape is
    94  // the identity function but escape analysis doesn't think the
    95  // output depends on the input.  noescape is inlined and currently
    96  // compiles down to a single xor instruction.
    97  // USE CAREFULLY!
    98  //go:nosplit
    99  func noescape(p unsafe.Pointer) unsafe.Pointer {
   100  	x := uintptr(p)
   101  	return unsafe.Pointer(x ^ 0)
   102  }
   103  
   104  func cgocallback(fn, frame unsafe.Pointer, framesize uintptr)
   105  func gogo(buf *gobuf)
   106  func gosave(buf *gobuf)
   107  func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
   108  
   109  //go:noescape
   110  func jmpdefer(fv *funcval, argp uintptr)
   111  func exit1(code int32)
   112  func asminit()
   113  func setg(gg *g)
   114  func breakpoint()
   115  
   116  // reflectcall calls fn with a copy of the n argument bytes pointed at by arg.
   117  // After fn returns, reflectcall copies n-retoffset result bytes
   118  // back into arg+retoffset before returning. If copying result bytes back,
   119  // the caller should pass the argument frame type as argtype, so that
   120  // call can execute appropriate write barriers during the copy.
   121  // Package reflect passes a frame type. In package runtime, there is only
   122  // one call that copies results back, in cgocallbackg1, and it does NOT pass a
   123  // frame type, meaning there are no write barriers invoked. See that call
   124  // site for justification.
   125  func reflectcall(argtype *_type, fn, arg unsafe.Pointer, argsize uint32, retoffset uint32)
   126  
   127  func procyield(cycles uint32)
   128  func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr)
   129  func goexit()
   130  
   131  //go:noescape
   132  func cas(ptr *uint32, old, new uint32) bool
   133  
   134  // NO go:noescape annotation; see atomic_pointer.go.
   135  func casp1(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool
   136  
   137  func nop() // call to prevent inlining of function body
   138  
   139  //go:noescape
   140  func casuintptr(ptr *uintptr, old, new uintptr) bool
   141  
   142  //go:noescape
   143  func atomicstoreuintptr(ptr *uintptr, new uintptr)
   144  
   145  //go:noescape
   146  func atomicloaduintptr(ptr *uintptr) uintptr
   147  
   148  //go:noescape
   149  func atomicloaduint(ptr *uint) uint
   150  
   151  //go:noescape
   152  func setcallerpc(argp unsafe.Pointer, pc uintptr)
   153  
   154  // getcallerpc returns the program counter (PC) of its caller's caller.
   155  // getcallersp returns the stack pointer (SP) of its caller's caller.
   156  // For both, the argp must be a pointer to the caller's first function argument.
   157  // The implementation may or may not use argp, depending on
   158  // the architecture.
   159  //
   160  // For example:
   161  //
   162  //	func f(arg1, arg2, arg3 int) {
   163  //		pc := getcallerpc(unsafe.Pointer(&arg1))
   164  //		sp := getcallersp(unsafe.Pointer(&arg1))
   165  //	}
   166  //
   167  // These two lines find the PC and SP immediately following
   168  // the call to f (where f will return).
   169  //
   170  // The call to getcallerpc and getcallersp must be done in the
   171  // frame being asked about. It would not be correct for f to pass &arg1
   172  // to another function g and let g call getcallerpc/getcallersp.
   173  // The call inside g might return information about g's caller or
   174  // information about f's caller or complete garbage.
   175  //
   176  // The result of getcallersp is correct at the time of the return,
   177  // but it may be invalidated by any subsequent call to a function
   178  // that might relocate the stack in order to grow or shrink it.
   179  // A general rule is that the result of getcallersp should be used
   180  // immediately and can only be passed to nosplit functions.
   181  
   182  //go:noescape
   183  func getcallerpc(argp unsafe.Pointer) uintptr
   184  
   185  //go:noescape
   186  func getcallersp(argp unsafe.Pointer) uintptr
   187  
   188  //go:noescape
   189  func asmcgocall(fn, arg unsafe.Pointer)
   190  
   191  //go:noescape
   192  func asmcgocall_errno(fn, arg unsafe.Pointer) int32
   193  
   194  // argp used in Defer structs when there is no argp.
   195  const _NoArgs = ^uintptr(0)
   196  
   197  func morestack()
   198  func rt0_go()
   199  
   200  // return0 is a stub used to return 0 from deferproc.
   201  // It is called at the very end of deferproc to signal
   202  // the calling Go function that it should not jump
   203  // to deferreturn.
   204  // in asm_*.s
   205  func return0()
   206  
   207  //go:linkname time_now time.now
   208  func time_now() (sec int64, nsec int32)
   209  
   210  // in asm_*.s
   211  // not called directly; definitions here supply type information for traceback.
   212  func call16(fn, arg unsafe.Pointer, n, retoffset uint32)
   213  func call32(fn, arg unsafe.Pointer, n, retoffset uint32)
   214  func call64(fn, arg unsafe.Pointer, n, retoffset uint32)
   215  func call128(fn, arg unsafe.Pointer, n, retoffset uint32)
   216  func call256(fn, arg unsafe.Pointer, n, retoffset uint32)
   217  func call512(fn, arg unsafe.Pointer, n, retoffset uint32)
   218  func call1024(fn, arg unsafe.Pointer, n, retoffset uint32)
   219  func call2048(fn, arg unsafe.Pointer, n, retoffset uint32)
   220  func call4096(fn, arg unsafe.Pointer, n, retoffset uint32)
   221  func call8192(fn, arg unsafe.Pointer, n, retoffset uint32)
   222  func call16384(fn, arg unsafe.Pointer, n, retoffset uint32)
   223  func call32768(fn, arg unsafe.Pointer, n, retoffset uint32)
   224  func call65536(fn, arg unsafe.Pointer, n, retoffset uint32)
   225  func call131072(fn, arg unsafe.Pointer, n, retoffset uint32)
   226  func call262144(fn, arg unsafe.Pointer, n, retoffset uint32)
   227  func call524288(fn, arg unsafe.Pointer, n, retoffset uint32)
   228  func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32)
   229  func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32)
   230  func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32)
   231  func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32)
   232  func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32)
   233  func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32)
   234  func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32)
   235  func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32)
   236  func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32)
   237  func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32)
   238  func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32)
   239  
   240  func systemstack_switch()
   241  
   242  func prefetcht0(addr uintptr)
   243  func prefetcht1(addr uintptr)
   244  func prefetcht2(addr uintptr)
   245  func prefetchnta(addr uintptr)
   246  
   247  func unixnanotime() int64 {
   248  	sec, nsec := time_now()
   249  	return sec*1e9 + int64(nsec)
   250  }
   251  
   252  // round n up to a multiple of a.  a must be a power of 2.
   253  func round(n, a uintptr) uintptr {
   254  	return (n + a - 1) &^ (a - 1)
   255  }