stubs.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. package runtime
  5. import "unsafe"
  6. // Declarations for runtime services implemented in C or assembly.
  7. const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const
  8. const regSize = 4 << (^uintreg(0) >> 63) // unsafe.Sizeof(uintreg(0)) but an ideal const
  9. // Should be a built-in for unsafe.Pointer?
  10. //go:nosplit
  11. func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
  12. return unsafe.Pointer(uintptr(p) + x)
  13. }
  14. // n must be a power of 2
  15. func roundup(p unsafe.Pointer, n uintptr) unsafe.Pointer {
  16. delta := -uintptr(p) & (n - 1)
  17. return unsafe.Pointer(uintptr(p) + delta)
  18. }
  19. // in runtime.c
  20. func getg() *g
  21. func acquirem() *m
  22. func releasem(mp *m)
  23. func gomcache() *mcache
  24. func readgstatus(*g) uint32 // proc.c
  25. // mcall switches from the g to the g0 stack and invokes fn(g),
  26. // where g is the goroutine that made the call.
  27. // mcall saves g's current PC/SP in g->sched so that it can be restored later.
  28. // It is up to fn to arrange for that later execution, typically by recording
  29. // g in a data structure, causing something to call ready(g) later.
  30. // mcall returns to the original goroutine g later, when g has been rescheduled.
  31. // fn must not return at all; typically it ends by calling schedule, to let the m
  32. // run other goroutines.
  33. //
  34. // mcall can only be called from g stacks (not g0, not gsignal).
  35. //go:noescape
  36. func mcall(fn func(*g))
  37. // onM switches from the g to the g0 stack and invokes fn().
  38. // When fn returns, onM switches back to the g and returns,
  39. // continuing execution on the g stack.
  40. // If arguments must be passed to fn, they can be written to
  41. // g->m->ptrarg (pointers) and g->m->scalararg (non-pointers)
  42. // before the call and then consulted during fn.
  43. // Similarly, fn can pass return values back in those locations.
  44. // If fn is written in Go, it can be a closure, which avoids the need for
  45. // ptrarg and scalararg entirely.
  46. // After reading values out of ptrarg and scalararg it is conventional
  47. // to zero them to avoid (memory or information) leaks.
  48. //
  49. // If onM is called from a g0 stack, it invokes fn and returns,
  50. // without any stack switches.
  51. //
  52. // If onM is called from a gsignal stack, it crashes the program.
  53. // The implication is that functions used in signal handlers must
  54. // not use onM.
  55. //
  56. // NOTE(rsc): We could introduce a separate onMsignal that is
  57. // like onM but if called from a gsignal stack would just run fn on
  58. // that stack. The caller of onMsignal would be required to save the
  59. // old values of ptrarg/scalararg and restore them when the call
  60. // was finished, in case the signal interrupted an onM sequence
  61. // in progress on the g or g0 stacks. Until there is a clear need for this,
  62. // we just reject onM in signal handling contexts entirely.
  63. //
  64. //go:noescape
  65. func onM(fn func())
  66. // onMsignal is like onM but is allowed to be used in code that
  67. // might run on the gsignal stack. Code running on a signal stack
  68. // may be interrupting an onM sequence on the main stack, so
  69. // if the onMsignal calling sequence writes to ptrarg/scalararg,
  70. // it must first save the old values and then restore them when
  71. // finished. As an exception to the rule, it is fine not to save and
  72. // restore the values if the program is trying to crash rather than
  73. // return from the signal handler.
  74. // Once all the runtime is written in Go, there will be no ptrarg/scalararg
  75. // and the distinction between onM and onMsignal (and perhaps mcall)
  76. // can go away.
  77. //
  78. // If onMsignal is called from a gsignal stack, it invokes fn directly,
  79. // without a stack switch. Otherwise onMsignal behaves like onM.
  80. //
  81. //go:noescape
  82. func onM_signalok(fn func())
  83. func badonm() {
  84. gothrow("onM called from signal goroutine")
  85. }
  86. // C functions that run on the M stack.
  87. // Call using mcall.
  88. func gosched_m(*g)
  89. func park_m(*g)
  90. func recovery_m(*g)
  91. // More C functions that run on the M stack.
  92. // Call using onM.
  93. func mcacheRefill_m()
  94. func largeAlloc_m()
  95. func gc_m()
  96. func scavenge_m()
  97. func setFinalizer_m()
  98. func removeFinalizer_m()
  99. func markallocated_m()
  100. func unrollgcprog_m()
  101. func unrollgcproginplace_m()
  102. func setgcpercent_m()
  103. func setmaxthreads_m()
  104. func ready_m()
  105. func deferproc_m()
  106. func goexit_m()
  107. func startpanic_m()
  108. func dopanic_m()
  109. func readmemstats_m()
  110. func writeheapdump_m()
  111. // memclr clears n bytes starting at ptr.
  112. // in memclr_*.s
  113. //go:noescape
  114. func memclr(ptr unsafe.Pointer, n uintptr)
  115. // memmove copies n bytes from "from" to "to".
  116. // in memmove_*.s
  117. //go:noescape
  118. func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)
  119. func starttheworld()
  120. func stoptheworld()
  121. func newextram()
  122. func lockOSThread()
  123. func unlockOSThread()
  124. // exported value for testing
  125. var hashLoad = loadFactor
  126. // in asm_*.s
  127. func fastrand1() uint32
  128. // in asm_*.s
  129. //go:noescape
  130. func memeq(a, b unsafe.Pointer, size uintptr) bool
  131. // noescape hides a pointer from escape analysis. noescape is
  132. // the identity function but escape analysis doesn't think the
  133. // output depends on the input. noescape is inlined and currently
  134. // compiles down to a single xor instruction.
  135. // USE CAREFULLY!
  136. //go:nosplit
  137. func noescape(p unsafe.Pointer) unsafe.Pointer {
  138. x := uintptr(p)
  139. return unsafe.Pointer(x ^ 0)
  140. }
  141. func entersyscall()
  142. func reentersyscall(pc uintptr, sp unsafe.Pointer)
  143. func entersyscallblock()
  144. func exitsyscall()
  145. func cgocallback(fn, frame unsafe.Pointer, framesize uintptr)
  146. func gogo(buf *gobuf)
  147. func gosave(buf *gobuf)
  148. func read(fd int32, p unsafe.Pointer, n int32) int32
  149. func close(fd int32) int32
  150. func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
  151. //go:noescape
  152. func jmpdefer(fv *funcval, argp uintptr)
  153. func exit1(code int32)
  154. func asminit()
  155. func setg(gg *g)
  156. func exit(code int32)
  157. func breakpoint()
  158. func nanotime() int64
  159. func usleep(usec uint32)
  160. // careful: cputicks is not guaranteed to be monotonic! In particular, we have
  161. // noticed drift between cpus on certain os/arch combinations. See issue 8976.
  162. func cputicks() int64
  163. func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer
  164. func munmap(addr unsafe.Pointer, n uintptr)
  165. func madvise(addr unsafe.Pointer, n uintptr, flags int32)
  166. func reflectcall(fn, arg unsafe.Pointer, n uint32, retoffset uint32)
  167. func osyield()
  168. func procyield(cycles uint32)
  169. func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr)
  170. func readgogc() int32
  171. func purgecachedstats(c *mcache)
  172. func gostringnocopy(b *byte) string
  173. func goexit()
  174. //go:noescape
  175. func write(fd uintptr, p unsafe.Pointer, n int32) int32
  176. //go:noescape
  177. func cas(ptr *uint32, old, new uint32) bool
  178. //go:noescape
  179. func casp(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool
  180. //go:noescape
  181. func casuintptr(ptr *uintptr, old, new uintptr) bool
  182. //go:noescape
  183. func atomicstoreuintptr(ptr *uintptr, new uintptr)
  184. //go:noescape
  185. func atomicloaduintptr(ptr *uintptr) uintptr
  186. //go:noescape
  187. func atomicloaduint(ptr *uint) uint
  188. //go:noescape
  189. func setcallerpc(argp unsafe.Pointer, pc uintptr)
  190. // getcallerpc returns the program counter (PC) of its caller's caller.
  191. // getcallersp returns the stack pointer (SP) of its caller's caller.
  192. // For both, the argp must be a pointer to the caller's first function argument.
  193. // The implementation may or may not use argp, depending on
  194. // the architecture.
  195. //
  196. // For example:
  197. //
  198. // func f(arg1, arg2, arg3 int) {
  199. // pc := getcallerpc(unsafe.Pointer(&arg1))
  200. // sp := getcallerpc(unsafe.Pointer(&arg2))
  201. // }
  202. //
  203. // These two lines find the PC and SP immediately following
  204. // the call to f (where f will return).
  205. //
  206. // The call to getcallerpc and getcallersp must be done in the
  207. // frame being asked about. It would not be correct for f to pass &arg1
  208. // to another function g and let g call getcallerpc/getcallersp.
  209. // The call inside g might return information about g's caller or
  210. // information about f's caller or complete garbage.
  211. //
  212. // The result of getcallersp is correct at the time of the return,
  213. // but it may be invalidated by any subsequent call to a function
  214. // that might relocate the stack in order to grow or shrink it.
  215. // A general rule is that the result of getcallersp should be used
  216. // immediately and can only be passed to nosplit functions.
  217. //go:noescape
  218. func getcallerpc(argp unsafe.Pointer) uintptr
  219. //go:noescape
  220. func getcallersp(argp unsafe.Pointer) uintptr
  221. //go:noescape
  222. func asmcgocall(fn, arg unsafe.Pointer)
  223. //go:noescape
  224. func asmcgocall_errno(fn, arg unsafe.Pointer) int32
  225. //go:noescape
  226. func open(name *byte, mode, perm int32) int32
  227. //go:noescape
  228. func gotraceback(*bool) int32
  229. const _NoArgs = ^uintptr(0)
  230. func newstack()
  231. func newproc()
  232. func morestack()
  233. func mstart()
  234. func rt0_go()
  235. // return0 is a stub used to return 0 from deferproc.
  236. // It is called at the very end of deferproc to signal
  237. // the calling Go function that it should not jump
  238. // to deferreturn.
  239. // in asm_*.s
  240. func return0()
  241. // thunk to call time.now.
  242. func timenow() (sec int64, nsec int32)
  243. // in asm_*.s
  244. // not called directly; definitions here supply type information for traceback.
  245. func call16(fn, arg unsafe.Pointer, n, retoffset uint32)
  246. func call32(fn, arg unsafe.Pointer, n, retoffset uint32)
  247. func call64(fn, arg unsafe.Pointer, n, retoffset uint32)
  248. func call128(fn, arg unsafe.Pointer, n, retoffset uint32)
  249. func call256(fn, arg unsafe.Pointer, n, retoffset uint32)
  250. func call512(fn, arg unsafe.Pointer, n, retoffset uint32)
  251. func call1024(fn, arg unsafe.Pointer, n, retoffset uint32)
  252. func call2048(fn, arg unsafe.Pointer, n, retoffset uint32)
  253. func call4096(fn, arg unsafe.Pointer, n, retoffset uint32)
  254. func call8192(fn, arg unsafe.Pointer, n, retoffset uint32)
  255. func call16384(fn, arg unsafe.Pointer, n, retoffset uint32)
  256. func call32768(fn, arg unsafe.Pointer, n, retoffset uint32)
  257. func call65536(fn, arg unsafe.Pointer, n, retoffset uint32)
  258. func call131072(fn, arg unsafe.Pointer, n, retoffset uint32)
  259. func call262144(fn, arg unsafe.Pointer, n, retoffset uint32)
  260. func call524288(fn, arg unsafe.Pointer, n, retoffset uint32)
  261. func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32)
  262. func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32)
  263. func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32)
  264. func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32)
  265. func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32)
  266. func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32)
  267. func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32)
  268. func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32)
  269. func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32)
  270. func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32)
  271. func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32)