Source file src/runtime/syscall_windows.go

     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 (
     8  	"internal/abi"
     9  	"internal/goarch"
    10  	"internal/runtime/syscall/windows"
    11  	"unsafe"
    12  )
    13  
    14  // cbs stores all registered Go callbacks.
    15  var cbs struct {
    16  	lock  mutex // use cbsLock / cbsUnlock for race instrumentation.
    17  	ctxt  [cb_max]winCallback
    18  	index map[winCallbackKey]int
    19  	n     int
    20  }
    21  
    22  func cbsLock() {
    23  	lock(&cbs.lock)
    24  	// compileCallback is used by goenvs prior to completion of schedinit.
    25  	// raceacquire involves a racecallback to get the proc, which is not
    26  	// safe prior to scheduler initialization. Thus avoid instrumentation
    27  	// until then.
    28  	if raceenabled && mainStarted {
    29  		raceacquire(unsafe.Pointer(&cbs.lock))
    30  	}
    31  }
    32  
    33  func cbsUnlock() {
    34  	if raceenabled && mainStarted {
    35  		racerelease(unsafe.Pointer(&cbs.lock))
    36  	}
    37  	unlock(&cbs.lock)
    38  }
    39  
    40  // winCallback records information about a registered Go callback.
    41  type winCallback struct {
    42  	fn     *funcval // Go function
    43  	retPop uintptr  // For 386 cdecl, how many bytes to pop on return
    44  	abiMap abiDesc
    45  }
    46  
    47  // abiPartKind is the action an abiPart should take.
    48  type abiPartKind int
    49  
    50  const (
    51  	abiPartBad   abiPartKind = iota
    52  	abiPartStack             // Move a value from memory to the stack.
    53  	abiPartReg               // Move a value from memory to a register.
    54  )
    55  
    56  // abiPart encodes a step in translating between calling ABIs.
    57  type abiPart struct {
    58  	kind           abiPartKind
    59  	srcStackOffset uintptr
    60  	dstStackOffset uintptr // used if kind == abiPartStack
    61  	dstRegister    int     // used if kind == abiPartReg
    62  	len            uintptr
    63  }
    64  
    65  func (a *abiPart) tryMerge(b abiPart) bool {
    66  	if a.kind != abiPartStack || b.kind != abiPartStack {
    67  		return false
    68  	}
    69  	if a.srcStackOffset+a.len == b.srcStackOffset && a.dstStackOffset+a.len == b.dstStackOffset {
    70  		a.len += b.len
    71  		return true
    72  	}
    73  	return false
    74  }
    75  
    76  // abiDesc specifies how to translate from a C frame to a Go
    77  // frame. This does not specify how to translate back because
    78  // the result is always a uintptr. If the C ABI is fastcall,
    79  // this assumes the four fastcall registers were first spilled
    80  // to the shadow space.
    81  type abiDesc struct {
    82  	parts []abiPart
    83  
    84  	srcStackSize uintptr // stdcall/fastcall stack space tracking
    85  	dstStackSize uintptr // Go stack space used
    86  	dstSpill     uintptr // Extra stack space for argument spill slots
    87  	dstRegisters int     // Go ABI int argument registers used
    88  
    89  	// retOffset is the offset of the uintptr-sized result in the Go
    90  	// frame.
    91  	retOffset uintptr
    92  }
    93  
    94  func (p *abiDesc) assignArg(t *_type) {
    95  	if t.Size_ > goarch.PtrSize {
    96  		// We don't support this right now. In
    97  		// stdcall/cdecl, 64-bit ints and doubles are
    98  		// passed as two words (little endian); and
    99  		// structs are pushed on the stack. In
   100  		// fastcall, arguments larger than the word
   101  		// size are passed by reference. On arm,
   102  		// 8-byte aligned arguments round up to the
   103  		// next even register and can be split across
   104  		// registers and the stack.
   105  		panic("compileCallback: argument size is larger than uintptr")
   106  	}
   107  	if k := t.Kind(); GOARCH != "386" && (k == abi.Float32 || k == abi.Float64) {
   108  		// In fastcall, floating-point arguments in
   109  		// the first four positions are passed in
   110  		// floating-point registers, which we don't
   111  		// currently spill. arm passes floating-point
   112  		// arguments in VFP registers, which we also
   113  		// don't support.
   114  		// So basically we only support 386.
   115  		panic("compileCallback: float arguments not supported")
   116  	}
   117  
   118  	if t.Size_ == 0 {
   119  		// The Go ABI aligns for zero-sized types.
   120  		p.dstStackSize = alignUp(p.dstStackSize, uintptr(t.Align_))
   121  		return
   122  	}
   123  
   124  	// In the C ABI, we're already on a word boundary.
   125  	// Also, sub-word-sized fastcall register arguments
   126  	// are stored to the least-significant bytes of the
   127  	// argument word and all supported Windows
   128  	// architectures are little endian, so srcStackOffset
   129  	// is already pointing to the right place for smaller
   130  	// arguments. The same is true on arm.
   131  
   132  	oldParts := p.parts
   133  	if p.tryRegAssignArg(t, 0) {
   134  		// Account for spill space.
   135  		//
   136  		// TODO(mknyszek): Remove this when we no longer have
   137  		// caller reserved spill space.
   138  		p.dstSpill = alignUp(p.dstSpill, uintptr(t.Align_))
   139  		p.dstSpill += t.Size_
   140  	} else {
   141  		// Register assignment failed.
   142  		// Undo the work and stack assign.
   143  		p.parts = oldParts
   144  
   145  		// The Go ABI aligns arguments.
   146  		p.dstStackSize = alignUp(p.dstStackSize, uintptr(t.Align_))
   147  
   148  		// Copy just the size of the argument. Note that this
   149  		// could be a small by-value struct, but C and Go
   150  		// struct layouts are compatible, so we can copy these
   151  		// directly, too.
   152  		part := abiPart{
   153  			kind:           abiPartStack,
   154  			srcStackOffset: p.srcStackSize,
   155  			dstStackOffset: p.dstStackSize,
   156  			len:            t.Size_,
   157  		}
   158  		// Add this step to the adapter.
   159  		if len(p.parts) == 0 || !p.parts[len(p.parts)-1].tryMerge(part) {
   160  			p.parts = append(p.parts, part)
   161  		}
   162  		// The Go ABI packs arguments.
   163  		p.dstStackSize += t.Size_
   164  	}
   165  
   166  	// cdecl, stdcall, fastcall, and arm pad arguments to word size.
   167  	// TODO(rsc): On arm and arm64 do we need to skip the caller's saved LR?
   168  	p.srcStackSize += goarch.PtrSize
   169  }
   170  
   171  // tryRegAssignArg tries to register-assign a value of type t.
   172  // If this type is nested in an aggregate type, then offset is the
   173  // offset of this type within its parent type.
   174  // Assumes t.size <= goarch.PtrSize and t.size != 0.
   175  //
   176  // Returns whether the assignment succeeded.
   177  func (p *abiDesc) tryRegAssignArg(t *_type, offset uintptr) bool {
   178  	switch k := t.Kind(); k {
   179  	case abi.Bool, abi.Int, abi.Int8, abi.Int16, abi.Int32, abi.Uint, abi.Uint8, abi.Uint16, abi.Uint32, abi.Uintptr, abi.Pointer, abi.UnsafePointer:
   180  		// Assign a register for all these types.
   181  		return p.assignReg(t.Size_, offset)
   182  	case abi.Int64, abi.Uint64:
   183  		// Only register-assign if the registers are big enough.
   184  		if goarch.PtrSize == 8 {
   185  			return p.assignReg(t.Size_, offset)
   186  		}
   187  	case abi.Array:
   188  		at := (*arraytype)(unsafe.Pointer(t))
   189  		if at.Len == 1 {
   190  			return p.tryRegAssignArg(at.Elem, offset) // TODO fix when runtime is fully commoned up w/ abi.Type
   191  		}
   192  	case abi.Struct:
   193  		st := (*structtype)(unsafe.Pointer(t))
   194  		for i := range st.Fields {
   195  			f := &st.Fields[i]
   196  			if !p.tryRegAssignArg(f.Typ, offset+f.Offset) {
   197  				return false
   198  			}
   199  		}
   200  		return true
   201  	}
   202  	// Pointer-sized types such as maps and channels are currently
   203  	// not supported.
   204  	panic("compileCallback: type " + toRType(t).string() + " is currently not supported for use in system callbacks")
   205  }
   206  
   207  // assignReg attempts to assign a single register for an
   208  // argument with the given size, at the given offset into the
   209  // value in the C ABI space.
   210  //
   211  // Returns whether the assignment was successful.
   212  func (p *abiDesc) assignReg(size, offset uintptr) bool {
   213  	if p.dstRegisters >= intArgRegs {
   214  		return false
   215  	}
   216  	p.parts = append(p.parts, abiPart{
   217  		kind:           abiPartReg,
   218  		srcStackOffset: p.srcStackSize + offset,
   219  		dstRegister:    p.dstRegisters,
   220  		len:            size,
   221  	})
   222  	p.dstRegisters++
   223  	return true
   224  }
   225  
   226  type winCallbackKey struct {
   227  	fn    *funcval
   228  	cdecl bool
   229  }
   230  
   231  func callbackasm()
   232  
   233  // callbackasmAddr returns address of runtime.callbackasm
   234  // function adjusted by i.
   235  // On x86 and amd64, runtime.callbackasm is a series of CALL instructions,
   236  // and we want callback to arrive at
   237  // correspondent call instruction instead of start of
   238  // runtime.callbackasm.
   239  // On ARM, runtime.callbackasm is a series of mov and branch instructions.
   240  // R12 is loaded with the callback index. Each entry is two instructions,
   241  // hence 8 bytes.
   242  func callbackasmAddr(i int) uintptr {
   243  	var entrySize int
   244  	switch GOARCH {
   245  	default:
   246  		panic("unsupported architecture")
   247  	case "386", "amd64":
   248  		entrySize = 5
   249  	case "arm", "arm64":
   250  		// On ARM and ARM64, each entry is a MOV instruction
   251  		// followed by a branch instruction
   252  		entrySize = 8
   253  	}
   254  	return abi.FuncPCABI0(callbackasm) + uintptr(i*entrySize)
   255  }
   256  
   257  const callbackMaxFrame = 64 * goarch.PtrSize
   258  
   259  // compileCallback converts a Go function fn into a C function pointer
   260  // that can be passed to Windows APIs.
   261  //
   262  // On 386, if cdecl is true, the returned C function will use the
   263  // cdecl calling convention; otherwise, it will use stdcall. On amd64,
   264  // it always uses fastcall. On arm, it always uses the ARM convention.
   265  //
   266  //go:linkname compileCallback syscall.compileCallback
   267  func compileCallback(fn eface, cdecl bool) (code uintptr) {
   268  	if GOARCH != "386" {
   269  		// cdecl is only meaningful on 386.
   270  		cdecl = false
   271  	}
   272  
   273  	if fn._type == nil || fn._type.Kind() != abi.Func {
   274  		panic("compileCallback: expected function with one uintptr-sized result")
   275  	}
   276  	ft := (*functype)(unsafe.Pointer(fn._type))
   277  
   278  	// Check arguments and construct ABI translation.
   279  	var abiMap abiDesc
   280  	for _, t := range ft.InSlice() {
   281  		abiMap.assignArg(t)
   282  	}
   283  	// The Go ABI aligns the result to the word size. src is
   284  	// already aligned.
   285  	abiMap.dstStackSize = alignUp(abiMap.dstStackSize, goarch.PtrSize)
   286  	abiMap.retOffset = abiMap.dstStackSize
   287  
   288  	if len(ft.OutSlice()) != 1 {
   289  		panic("compileCallback: expected function with one uintptr-sized result")
   290  	}
   291  	if ft.OutSlice()[0].Size_ != goarch.PtrSize {
   292  		panic("compileCallback: expected function with one uintptr-sized result")
   293  	}
   294  	if k := ft.OutSlice()[0].Kind(); k == abi.Float32 || k == abi.Float64 {
   295  		// In cdecl and stdcall, float results are returned in
   296  		// ST(0). In fastcall, they're returned in XMM0.
   297  		// Either way, it's not AX.
   298  		panic("compileCallback: float results not supported")
   299  	}
   300  	if intArgRegs == 0 {
   301  		// Make room for the uintptr-sized result.
   302  		// If there are argument registers, the return value will
   303  		// be passed in the first register.
   304  		abiMap.dstStackSize += goarch.PtrSize
   305  	}
   306  
   307  	// TODO(mknyszek): Remove dstSpill from this calculation when we no longer have
   308  	// caller reserved spill space.
   309  	frameSize := alignUp(abiMap.dstStackSize, goarch.PtrSize)
   310  	frameSize += abiMap.dstSpill
   311  	if frameSize > callbackMaxFrame {
   312  		panic("compileCallback: function argument frame too large")
   313  	}
   314  
   315  	// For cdecl, the callee is responsible for popping its
   316  	// arguments from the C stack.
   317  	var retPop uintptr
   318  	if cdecl {
   319  		retPop = abiMap.srcStackSize
   320  	}
   321  
   322  	key := winCallbackKey{(*funcval)(fn.data), cdecl}
   323  
   324  	cbsLock()
   325  
   326  	// Check if this callback is already registered.
   327  	if n, ok := cbs.index[key]; ok {
   328  		cbsUnlock()
   329  		return callbackasmAddr(n)
   330  	}
   331  
   332  	// Register the callback.
   333  	if cbs.index == nil {
   334  		cbs.index = make(map[winCallbackKey]int)
   335  	}
   336  	n := cbs.n
   337  	if n >= len(cbs.ctxt) {
   338  		cbsUnlock()
   339  		throw("too many callback functions")
   340  	}
   341  	c := winCallback{key.fn, retPop, abiMap}
   342  	cbs.ctxt[n] = c
   343  	cbs.index[key] = n
   344  	cbs.n++
   345  
   346  	cbsUnlock()
   347  	return callbackasmAddr(n)
   348  }
   349  
   350  type callbackArgs struct {
   351  	index uintptr
   352  	// args points to the argument block.
   353  	//
   354  	// For cdecl and stdcall, all arguments are on the stack.
   355  	//
   356  	// For fastcall, the trampoline spills register arguments to
   357  	// the reserved spill slots below the stack arguments,
   358  	// resulting in a layout equivalent to stdcall.
   359  	//
   360  	// For arm, the trampoline stores the register arguments just
   361  	// below the stack arguments, so again we can treat it as one
   362  	// big stack arguments frame.
   363  	args unsafe.Pointer
   364  	// Below are out-args from callbackWrap
   365  	result uintptr
   366  	retPop uintptr // For 386 cdecl, how many bytes to pop on return
   367  }
   368  
   369  // callbackWrap is called by callbackasm to invoke a registered C callback.
   370  func callbackWrap(a *callbackArgs) {
   371  	c := cbs.ctxt[a.index]
   372  	a.retPop = c.retPop
   373  
   374  	// Convert from C to Go ABI.
   375  	var regs abi.RegArgs
   376  	var frame [callbackMaxFrame]byte
   377  	goArgs := unsafe.Pointer(&frame)
   378  	for _, part := range c.abiMap.parts {
   379  		switch part.kind {
   380  		case abiPartStack:
   381  			memmove(add(goArgs, part.dstStackOffset), add(a.args, part.srcStackOffset), part.len)
   382  		case abiPartReg:
   383  			goReg := unsafe.Pointer(&regs.Ints[part.dstRegister])
   384  			memmove(goReg, add(a.args, part.srcStackOffset), part.len)
   385  		default:
   386  			panic("bad ABI description")
   387  		}
   388  	}
   389  
   390  	// TODO(mknyszek): Remove this when we no longer have
   391  	// caller reserved spill space.
   392  	frameSize := alignUp(c.abiMap.dstStackSize, goarch.PtrSize)
   393  	frameSize += c.abiMap.dstSpill
   394  
   395  	// Even though this is copying back results, we can pass a nil
   396  	// type because those results must not require write barriers.
   397  	reflectcall(nil, unsafe.Pointer(c.fn), noescape(goArgs), uint32(c.abiMap.dstStackSize), uint32(c.abiMap.retOffset), uint32(frameSize), &regs)
   398  
   399  	// Extract the result.
   400  	//
   401  	// There's always exactly one return value, one pointer in size.
   402  	// If it's on the stack, then we will have reserved space for it
   403  	// at the end of the frame, otherwise it was passed in a register.
   404  	if c.abiMap.dstStackSize != c.abiMap.retOffset {
   405  		a.result = *(*uintptr)(unsafe.Pointer(&frame[c.abiMap.retOffset]))
   406  	} else {
   407  		var zero int
   408  		// On architectures with no registers, Ints[0] would be a compile error,
   409  		// so we use a dynamic index. These architectures will never take this
   410  		// branch, so this won't cause a runtime panic.
   411  		a.result = regs.Ints[zero]
   412  	}
   413  }
   414  
   415  // syscall_syscalln calls fn with args[:n].
   416  // It is used to implement [syscall.SyscallN].
   417  // It shouldn't be used in the runtime package,
   418  // use [stdcall] instead.
   419  //
   420  //go:linkname syscall_syscalln syscall.syscalln
   421  //go:nosplit
   422  //go:uintptrkeepalive
   423  func syscall_syscalln(fn, n uintptr, args ...uintptr) (r1, r2, err uintptr) {
   424  	if n > uintptr(len(args)) {
   425  		panic("syscall: n > len(args)") // should not be reachable from user code
   426  	}
   427  	if n > windows.MaxArgs {
   428  		panic("runtime: SyscallN has too many arguments")
   429  	}
   430  
   431  	// The cgocall parameters are stored in m instead of in
   432  	// the stack because the stack can move during fn if it
   433  	// calls back into Go.
   434  	c := &getg().m.winsyscall
   435  	c.Fn = fn
   436  	c.N = n
   437  	if c.N != 0 {
   438  		c.Args = uintptr(noescape(unsafe.Pointer(&args[0])))
   439  	}
   440  	cgocall(asmstdcallAddr, unsafe.Pointer(c))
   441  	// cgocall may reschedule us on to a different M,
   442  	// but it copies the return values into the new M's
   443  	// so we can read them from there.
   444  	c = &getg().m.winsyscall
   445  	return c.R1, c.R2, c.Err
   446  }
   447  

View as plain text