1 //:build unix
2 3 package syscall
4 5 import (
6 "unsafe"
7 )
8 9 // Fixed extents in the ABI: buffers, not array fields.
10 //
11 // An array is a slice in Moxie, and a slice is a three-word header, while the
12 // kernel writes bytes at fixed offsets in a struct it is handed. The two cannot
13 // be the same memory. So the ABI's fixed extents are spelled out the way the
14 // kernel sees them: a BYTE BUFFER whose size is the struct's size, passed by
15 // address, with each field a window over its range. That is the same
16 // information an `[N]T` field carried (extent and offset), written where the
17 // boundary is rather than in the type system (the release notes W7a).
18 //
19 // A window is a VIEW. `b[off : off+n]` would copy under the copy rule, and the
20 // whole point of these is that a later read sees what the kernel wrote.
21 22 // abiWindow returns a view of n bytes at off in b, or nil when b is too short.
23 func abiWindow(b []byte, off, n int32) (w []byte) {
24 if off < 0 || n < 0 || int32(len(b)) < off+n {
25 return nil
26 }
27 p := unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), uintptr(off))
28 return unsafe.Slice((*byte)(p), n)
29 }
30 31 // abiCString returns the bytes of a fixed field up to its first NUL. The
32 // fields are NUL-padded C strings, and every reader of one wants exactly this.
33 func abiCString(w []byte) (s []byte) {
34 for i := int32(0); i < int32(len(w)); i++ {
35 if w[i] == 0 {
36 return w[:i]
37 }
38 }
39 return w
40 }
41 42 // abiBufferOf is the base address a kernel call is given for a buffer.
43 func abiBufferOf(b []byte) (p unsafe.Pointer) {
44 if len(b) == 0 {
45 return nil
46 }
47 return unsafe.Pointer(unsafe.SliceData(b))
48 }
49 50 // abiGet16 and abiGet32 read a NATIVE-endian integer at a fixed offset, which
51 // is what every ABI struct field is except the sockaddr port (network order,
52 // see sockSetPort). The window is at least pointer-aligned in practice; the
53 // unaligned load is legal on every target this tree builds for.
54 func abiGet16(b []byte, off int32) (v uint16) {
55 w := abiWindow(b, off, 2)
56 if w == nil {
57 return 0
58 }
59 return *(*uint16)(unsafe.Pointer(&w[0]))
60 }
61 62 func abiPut16(b []byte, off int32, v uint16) {
63 w := abiWindow(b, off, 2)
64 if w == nil {
65 return
66 }
67 *(*uint16)(unsafe.Pointer(&w[0])) = v
68 }
69 70 func abiGet8(b []byte, off int32) (v uint8) {
71 w := abiWindow(b, off, 1)
72 if w == nil {
73 return 0
74 }
75 return w[0]
76 }
77 78 func abiPut8(b []byte, off int32, v uint8) {
79 w := abiWindow(b, off, 1)
80 if w == nil {
81 return
82 }
83 w[0] = v
84 }
85 86 func abiGet32(b []byte, off int32) (v uint32) {
87 w := abiWindow(b, off, 4)
88 if w == nil {
89 return 0
90 }
91 return *(*uint32)(unsafe.Pointer(&w[0]))
92 }
93 94 func abiPut32(b []byte, off int32, v uint32) {
95 w := abiWindow(b, off, 4)
96 if w == nil {
97 return
98 }
99 *(*uint32)(unsafe.Pointer(&w[0])) = v
100 }
101 102 // abiPutBytes copies src into b at off, for a field whose extent the caller
103 // hands over as a slice.
104 func abiPutBytes(b []byte, off int32, src []byte) {
105 w := abiWindow(b, off, int32(len(src)))
106 if w == nil {
107 return
108 }
109 copy(w, src)
110 }
111 112 // Inline addresses. An address the kernel reads at a fixed offset inside a
113 // struct it is handed cannot be an `[4]byte` field (that is a slice header),
114 // and it cannot be a separate buffer either, because it sits BETWEEN other
115 // fields. So it is spelled the way the kernel sees it: exactly four, eight or
116 // sixteen bytes, one per field. Layout is the bytes; the accessors only give
117 // the two shapes callers have: copy in, copy out.
118 119 type abiAddr4 struct{ B0, B1, B2, B3 byte }
120 121 // Set copies an address in from its byte form; a short src leaves the
122 // remaining bytes as they were, which is what a partial address wants.
123 func (a *abiAddr4) Set(b []byte) { copy(a.bytes(), b) }
124 125 func (a *abiAddr4) bytes() (b []byte) {
126 return unsafe.Slice((*byte)(unsafe.Pointer(a)), 4)
127 }
128 129 type abiAddr16 struct{ B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12, B13, B14, B15 byte }
130 131 func (a *abiAddr16) Set(b []byte) { copy(a.bytes(), b) }
132 133 func (a *abiAddr16) bytes() (b []byte) {
134 return unsafe.Slice((*byte)(unsafe.Pointer(a)), 16)
135 }
136