Debian Codegraph @

internal/sys/syscall_windows.go

raw symbols SCIP
1 package sys
2
3 import (
4 "fmt"
5 "syscall"
6 "unsafe"
7
8 "golang.org/x/sys/windows"
9
10 "github.com/cilium/ebpf/internal"
11 "github.com/cilium/ebpf/internal/efw"
12 "github.com/cilium/ebpf/internal/unix"
13 )
14
15 // BPF calls the BPF syscall wrapper in ebpfapi.dll.
16 //
17 // Any pointers contained in attr must use the Pointer type from this package.
18 //
19 // The implementation lives in https://github.com/microsoft/ebpf-for-windows/blob/main/libs/api/bpf_syscall.cpp
20 func BPF(cmd Cmd, attr unsafe.Pointer, size uintptr) (uintptr, error) {
21 // On Linux we need to guard against preemption by the profiler here. On
22 // Windows it seems like a cgocall may not be preempted:
23 // https://github.com/golang/go/blob/8b51146c698bcfcc2c2b73fa9390db5230f2ce0a/src/runtime/os_windows.go#L1240-L1246
24
25 addr, err := efw.BPF.Find()
26 if err != nil {
27 return 0, err
28 }
29
30 // Using [LazyProc.Call] forces attr to escape, which isn't the case when using syscall.Syscall directly.
31 r1, _, lastError := syscall.SyscallN(addr, uintptr(cmd), uintptr(attr), size)
32
33 if ret := int(efw.Int(r1)); ret < 0 {
34 errNo := unix.Errno(-ret)
35 if errNo == unix.EINVAL && lastError == windows.ERROR_CALL_NOT_IMPLEMENTED {
36 return 0, internal.ErrNotSupportedOnOS
37 }
38 return 0, wrappedErrno{errNo}
39 }
40
41 return r1, nil
42 }
43
44 // ObjGetTyped retrieves an pinned object and its type.
45 func ObjGetTyped(attr *ObjGetAttr) (*FD, ObjType, error) {
46 fd, err := ObjGet(attr)
47 if err != nil {
48 return nil, 0, err
49 }
50
51 efwType, err := efw.EbpfObjectGetInfoByFd(fd.Int(), nil, nil)
52 if err != nil {
53 _ = fd.Close()
54 return nil, 0, err
55 }
56
57 switch efwType {
58 case efw.EBPF_OBJECT_UNKNOWN:
59 return fd, BPF_TYPE_UNSPEC, nil
60 case efw.EBPF_OBJECT_MAP:
61 return fd, BPF_TYPE_MAP, nil
62 case efw.EBPF_OBJECT_LINK:
63 return fd, BPF_TYPE_LINK, nil
64 case efw.EBPF_OBJECT_PROGRAM:
65 return fd, BPF_TYPE_PROG, nil
66 default:
67 return nil, 0, fmt.Errorf("unrecognized object type %v", efwType)
68 }
69 }