//:build unix package syscall import ( "unsafe" ) // Fixed extents in the ABI: buffers, not array fields. // // An array is a slice in Moxie, and a slice is a three-word header, while the // kernel writes bytes at fixed offsets in a struct it is handed. The two cannot // be the same memory. So the ABI's fixed extents are spelled out the way the // kernel sees them: a BYTE BUFFER whose size is the struct's size, passed by // address, with each field a window over its range. That is the same // information an `[N]T` field carried (extent and offset), written where the // boundary is rather than in the type system (the release notes W7a). // // A window is a VIEW. `b[off : off+n]` would copy under the copy rule, and the // whole point of these is that a later read sees what the kernel wrote. // abiWindow returns a view of n bytes at off in b, or nil when b is too short. func abiWindow(b []byte, off, n int32) (w []byte) { if off < 0 || n < 0 || int32(len(b)) < off+n { return nil } p := unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), uintptr(off)) return unsafe.Slice((*byte)(p), n) } // abiCString returns the bytes of a fixed field up to its first NUL. The // fields are NUL-padded C strings, and every reader of one wants exactly this. func abiCString(w []byte) (s []byte) { for i := int32(0); i < int32(len(w)); i++ { if w[i] == 0 { return w[:i] } } return w } // abiBufferOf is the base address a kernel call is given for a buffer. func abiBufferOf(b []byte) (p unsafe.Pointer) { if len(b) == 0 { return nil } return unsafe.Pointer(unsafe.SliceData(b)) } // abiGet16 and abiGet32 read a NATIVE-endian integer at a fixed offset, which // is what every ABI struct field is except the sockaddr port (network order, // see sockSetPort). The window is at least pointer-aligned in practice; the // unaligned load is legal on every target this tree builds for. func abiGet16(b []byte, off int32) (v uint16) { w := abiWindow(b, off, 2) if w == nil { return 0 } return *(*uint16)(unsafe.Pointer(&w[0])) } func abiPut16(b []byte, off int32, v uint16) { w := abiWindow(b, off, 2) if w == nil { return } *(*uint16)(unsafe.Pointer(&w[0])) = v } func abiGet8(b []byte, off int32) (v uint8) { w := abiWindow(b, off, 1) if w == nil { return 0 } return w[0] } func abiPut8(b []byte, off int32, v uint8) { w := abiWindow(b, off, 1) if w == nil { return } w[0] = v } func abiGet32(b []byte, off int32) (v uint32) { w := abiWindow(b, off, 4) if w == nil { return 0 } return *(*uint32)(unsafe.Pointer(&w[0])) } func abiPut32(b []byte, off int32, v uint32) { w := abiWindow(b, off, 4) if w == nil { return } *(*uint32)(unsafe.Pointer(&w[0])) = v } // abiPutBytes copies src into b at off, for a field whose extent the caller // hands over as a slice. func abiPutBytes(b []byte, off int32, src []byte) { w := abiWindow(b, off, int32(len(src))) if w == nil { return } copy(w, src) } // Inline addresses. An address the kernel reads at a fixed offset inside a // struct it is handed cannot be an `[4]byte` field (that is a slice header), // and it cannot be a separate buffer either, because it sits BETWEEN other // fields. So it is spelled the way the kernel sees it: exactly four, eight or // sixteen bytes, one per field. Layout is the bytes; the accessors only give // the two shapes callers have: copy in, copy out. type abiAddr4 struct{ B0, B1, B2, B3 byte } // Set copies an address in from its byte form; a short src leaves the // remaining bytes as they were, which is what a partial address wants. func (a *abiAddr4) Set(b []byte) { copy(a.bytes(), b) } func (a *abiAddr4) bytes() (b []byte) { return unsafe.Slice((*byte)(unsafe.Pointer(a)), 4) } type abiAddr16 struct{ B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12, B13, B14, B15 byte } func (a *abiAddr16) Set(b []byte) { copy(a.bytes(), b) } func (a *abiAddr16) bytes() (b []byte) { return unsafe.Slice((*byte)(unsafe.Pointer(a)), 16) }