syscall_sysv.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: Apache-2.0
  2. // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
  3. //go:build darwin || freebsd || (linux && (amd64 || arm64))
  4. package purego
  5. import (
  6. "reflect"
  7. "runtime"
  8. "sync"
  9. "unsafe"
  10. )
  11. var syscall15XABI0 uintptr
  12. //go:nosplit
  13. func syscall_syscall15X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr) (r1, r2, err uintptr) {
  14. args := syscall15Args{
  15. fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15,
  16. a1, a2, a3, a4, a5, a6, a7, a8,
  17. 0,
  18. }
  19. runtime_cgocall(syscall15XABI0, unsafe.Pointer(&args))
  20. return args.a1, args.a2, 0
  21. }
  22. // NewCallback converts a Go function to a function pointer conforming to the C calling convention.
  23. // This is useful when interoperating with C code requiring callbacks. The argument is expected to be a
  24. // function with zero or one uintptr-sized result. The function must not have arguments with size larger than the size
  25. // of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated
  26. // for these callbacks is never released. At least 2000 callbacks can always be created. Although this function
  27. // provides similar functionality to windows.NewCallback it is distinct.
  28. func NewCallback(fn interface{}) uintptr {
  29. ty := reflect.TypeOf(fn)
  30. for i := 0; i < ty.NumIn(); i++ {
  31. in := ty.In(i)
  32. if !in.AssignableTo(reflect.TypeOf(CDecl{})) {
  33. continue
  34. }
  35. if i != 0 {
  36. panic("purego: CDecl must be the first argument")
  37. }
  38. }
  39. return compileCallback(fn)
  40. }
  41. // maxCb is the maximum number of callbacks
  42. // only increase this if you have added more to the callbackasm function
  43. const maxCB = 2000
  44. var cbs struct {
  45. lock sync.Mutex
  46. numFn int // the number of functions currently in cbs.funcs
  47. funcs [maxCB]reflect.Value // the saved callbacks
  48. }
  49. type callbackArgs struct {
  50. index uintptr
  51. // args points to the argument block.
  52. //
  53. // The structure of the arguments goes
  54. // float registers followed by the
  55. // integer registers followed by the stack.
  56. //
  57. // This variable is treated as a continuous
  58. // block of memory containing all of the arguments
  59. // for this callback.
  60. args unsafe.Pointer
  61. // Below are out-args from callbackWrap
  62. result uintptr
  63. }
  64. func compileCallback(fn interface{}) uintptr {
  65. val := reflect.ValueOf(fn)
  66. if val.Kind() != reflect.Func {
  67. panic("purego: the type must be a function but was not")
  68. }
  69. if val.IsNil() {
  70. panic("purego: function must not be nil")
  71. }
  72. ty := val.Type()
  73. for i := 0; i < ty.NumIn(); i++ {
  74. in := ty.In(i)
  75. switch in.Kind() {
  76. case reflect.Struct:
  77. if i == 0 && in.AssignableTo(reflect.TypeOf(CDecl{})) {
  78. continue
  79. }
  80. fallthrough
  81. case reflect.Interface, reflect.Func, reflect.Slice,
  82. reflect.Chan, reflect.Complex64, reflect.Complex128,
  83. reflect.String, reflect.Map, reflect.Invalid:
  84. panic("purego: unsupported argument type: " + in.Kind().String())
  85. }
  86. }
  87. output:
  88. switch {
  89. case ty.NumOut() == 1:
  90. switch ty.Out(0).Kind() {
  91. case reflect.Pointer, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  92. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
  93. reflect.Bool, reflect.UnsafePointer:
  94. break output
  95. }
  96. panic("purego: unsupported return type: " + ty.String())
  97. case ty.NumOut() > 1:
  98. panic("purego: callbacks can only have one return")
  99. }
  100. cbs.lock.Lock()
  101. defer cbs.lock.Unlock()
  102. if cbs.numFn >= maxCB {
  103. panic("purego: the maximum number of callbacks has been reached")
  104. }
  105. cbs.funcs[cbs.numFn] = val
  106. cbs.numFn++
  107. return callbackasmAddr(cbs.numFn - 1)
  108. }
  109. const ptrSize = unsafe.Sizeof((*int)(nil))
  110. const callbackMaxFrame = 64 * ptrSize
  111. // callbackasm is implemented in zcallback_GOOS_GOARCH.s
  112. //
  113. //go:linkname __callbackasm callbackasm
  114. var __callbackasm byte
  115. var callbackasmABI0 = uintptr(unsafe.Pointer(&__callbackasm))
  116. // callbackWrap_call allows the calling of the ABIInternal wrapper
  117. // which is required for runtime.cgocallback without the
  118. // <ABIInternal> tag which is only allowed in the runtime.
  119. // This closure is used inside sys_darwin_GOARCH.s
  120. var callbackWrap_call = callbackWrap
  121. // callbackWrap is called by assembly code which determines which Go function to call.
  122. // This function takes the arguments and passes them to the Go function and returns the result.
  123. func callbackWrap(a *callbackArgs) {
  124. cbs.lock.Lock()
  125. fn := cbs.funcs[a.index]
  126. cbs.lock.Unlock()
  127. fnType := fn.Type()
  128. args := make([]reflect.Value, fnType.NumIn())
  129. frame := (*[callbackMaxFrame]uintptr)(a.args)
  130. var floatsN int // floatsN represents the number of float arguments processed
  131. var intsN int // intsN represents the number of integer arguments processed
  132. // stack points to the index into frame of the current stack element.
  133. // The stack begins after the float and integer registers.
  134. stack := numOfIntegerRegisters() + numOfFloats
  135. for i := range args {
  136. var pos int
  137. switch fnType.In(i).Kind() {
  138. case reflect.Float32, reflect.Float64:
  139. if floatsN >= numOfFloats {
  140. pos = stack
  141. stack++
  142. } else {
  143. pos = floatsN
  144. }
  145. floatsN++
  146. case reflect.Struct:
  147. // This is the CDecl field
  148. args[i] = reflect.Zero(fnType.In(i))
  149. continue
  150. default:
  151. if intsN >= numOfIntegerRegisters() {
  152. pos = stack
  153. stack++
  154. } else {
  155. // the integers begin after the floats in frame
  156. pos = intsN + numOfFloats
  157. }
  158. intsN++
  159. }
  160. args[i] = reflect.NewAt(fnType.In(i), unsafe.Pointer(&frame[pos])).Elem()
  161. }
  162. ret := fn.Call(args)
  163. if len(ret) > 0 {
  164. switch k := ret[0].Kind(); k {
  165. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uintptr:
  166. a.result = uintptr(ret[0].Uint())
  167. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  168. a.result = uintptr(ret[0].Int())
  169. case reflect.Bool:
  170. if ret[0].Bool() {
  171. a.result = 1
  172. } else {
  173. a.result = 0
  174. }
  175. case reflect.Pointer:
  176. a.result = ret[0].Pointer()
  177. case reflect.UnsafePointer:
  178. a.result = ret[0].Pointer()
  179. default:
  180. panic("purego: unsupported kind: " + k.String())
  181. }
  182. }
  183. }
  184. // callbackasmAddr returns address of runtime.callbackasm
  185. // function adjusted by i.
  186. // On x86 and amd64, runtime.callbackasm is a series of CALL instructions,
  187. // and we want callback to arrive at
  188. // correspondent call instruction instead of start of
  189. // runtime.callbackasm.
  190. // On ARM, runtime.callbackasm is a series of mov and branch instructions.
  191. // R12 is loaded with the callback index. Each entry is two instructions,
  192. // hence 8 bytes.
  193. func callbackasmAddr(i int) uintptr {
  194. var entrySize int
  195. switch runtime.GOARCH {
  196. default:
  197. panic("purego: unsupported architecture")
  198. case "386", "amd64":
  199. entrySize = 5
  200. case "arm", "arm64":
  201. // On ARM and ARM64, each entry is a MOV instruction
  202. // followed by a branch instruction
  203. entrySize = 8
  204. }
  205. return callbackasmABI0 + uintptr(i*entrySize)
  206. }