1// Copyright 2009 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
5package runtime
6
7import "unsafe"
8
9type mOS struct {
10	machport uint32 // return address for mach ipc
11	waitsema uint32 // semaphore for parking on locks
12}
13
14//go:noescape
15//extern mach_msg_trap
16func mach_msg_trap(h unsafe.Pointer, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32
17
18//extern mach_reply_port
19func mach_reply_port() uint32
20
21//extern mach_task_self
22func mach_task_self() uint32
23
24func unimplemented(name string) {
25	println(name, "not implemented")
26	*(*int)(unsafe.Pointer(uintptr(1231))) = 1231
27}
28
29//go:nosplit
30func semawakeup(mp *m) {
31	mach_semrelease(mp.mos.waitsema)
32}
33
34//go:nosplit
35func semacreate(mp *m) {
36	if mp.mos.waitsema != 0 {
37		return
38	}
39	systemstack(func() {
40		mp.mos.waitsema = mach_semcreate()
41	})
42}
43
44// Mach IPC, to get at semaphores
45// Definitions are in /usr/include/mach on a Mac.
46
47func macherror(r int32, fn string) {
48	print("mach error ", fn, ": ", r, "\n")
49	throw("mach error")
50}
51
52const _DebugMach = false
53
54var zerondr machndr
55
56func mach_msgh_bits(a, b uint32) uint32 {
57	return a | b<<8
58}
59
60func mach_msg(h *machheader, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32 {
61	// TODO: Loop on interrupt.
62	return mach_msg_trap(unsafe.Pointer(h), op, send_size, rcv_size, rcv_name, timeout, notify)
63}
64
65// Mach RPC (MIG)
66const (
67	_MinMachMsg = 48
68	_MachReply  = 100
69)
70
71type codemsg struct {
72	h    machheader
73	ndr  machndr
74	code int32
75}
76
77func machcall(h *machheader, maxsize int32, rxsize int32) int32 {
78	_g_ := getg()
79	port := _g_.m.mos.machport
80	if port == 0 {
81		port = mach_reply_port()
82		_g_.m.mos.machport = port
83	}
84
85	h.msgh_bits |= mach_msgh_bits(_MACH_MSG_TYPE_COPY_SEND, _MACH_MSG_TYPE_MAKE_SEND_ONCE)
86	h.msgh_local_port = port
87	h.msgh_reserved = 0
88	id := h.msgh_id
89
90	if _DebugMach {
91		p := (*[10000]unsafe.Pointer)(unsafe.Pointer(h))
92		print("send:\t")
93		var i uint32
94		for i = 0; i < h.msgh_size/uint32(unsafe.Sizeof(p[0])); i++ {
95			print(" ", p[i])
96			if i%8 == 7 {
97				print("\n\t")
98			}
99		}
100		if i%8 != 0 {
101			print("\n")
102		}
103	}
104	ret := mach_msg(h, _MACH_SEND_MSG|_MACH_RCV_MSG, h.msgh_size, uint32(maxsize), port, 0, 0)
105	if ret != 0 {
106		if _DebugMach {
107			print("mach_msg error ", ret, "\n")
108		}
109		return ret
110	}
111	if _DebugMach {
112		p := (*[10000]unsafe.Pointer)(unsafe.Pointer(h))
113		var i uint32
114		for i = 0; i < h.msgh_size/uint32(unsafe.Sizeof(p[0])); i++ {
115			print(" ", p[i])
116			if i%8 == 7 {
117				print("\n\t")
118			}
119		}
120		if i%8 != 0 {
121			print("\n")
122		}
123	}
124	if h.msgh_id != id+_MachReply {
125		if _DebugMach {
126			print("mach_msg _MachReply id mismatch ", h.msgh_id, " != ", id+_MachReply, "\n")
127		}
128		return -303 // MIG_REPLY_MISMATCH
129	}
130	// Look for a response giving the return value.
131	// Any call can send this back with an error,
132	// and some calls only have return values so they
133	// send it back on success too. I don't quite see how
134	// you know it's one of these and not the full response
135	// format, so just look if the message is right.
136	c := (*codemsg)(unsafe.Pointer(h))
137	if uintptr(h.msgh_size) == unsafe.Sizeof(*c) && h.msgh_bits&_MACH_MSGH_BITS_COMPLEX == 0 {
138		if _DebugMach {
139			print("mig result ", c.code, "\n")
140		}
141		return c.code
142	}
143	if h.msgh_size != uint32(rxsize) {
144		if _DebugMach {
145			print("mach_msg _MachReply size mismatch ", h.msgh_size, " != ", rxsize, "\n")
146		}
147		return -307 // MIG_ARRAY_TOO_LARGE
148	}
149	return 0
150}
151
152// Semaphores!
153
154const (
155	tmach_semcreate = 3418
156	rmach_semcreate = tmach_semcreate + _MachReply
157
158	tmach_semdestroy = 3419
159	rmach_semdestroy = tmach_semdestroy + _MachReply
160
161	_KERN_ABORTED             = 14
162	_KERN_OPERATION_TIMED_OUT = 49
163)
164
165type tmach_semcreatemsg struct {
166	h      machheader
167	ndr    machndr
168	policy int32
169	value  int32
170}
171
172type rmach_semcreatemsg struct {
173	h         machheader
174	body      machbody
175	semaphore machport
176}
177
178type tmach_semdestroymsg struct {
179	h         machheader
180	body      machbody
181	semaphore machport
182}
183
184func mach_semcreate() uint32 {
185	var m [256]uint8
186	tx := (*tmach_semcreatemsg)(unsafe.Pointer(&m))
187	rx := (*rmach_semcreatemsg)(unsafe.Pointer(&m))
188
189	tx.h.msgh_bits = 0
190	tx.h.msgh_size = uint32(unsafe.Sizeof(*tx))
191	tx.h.msgh_remote_port = mach_task_self()
192	tx.h.msgh_id = tmach_semcreate
193	tx.ndr = zerondr
194
195	tx.policy = 0 // 0 = SYNC_POLICY_FIFO
196	tx.value = 0
197
198	for {
199		r := machcall(&tx.h, int32(unsafe.Sizeof(m)), int32(unsafe.Sizeof(*rx)))
200		if r == 0 {
201			break
202		}
203		if r == _KERN_ABORTED { // interrupted
204			continue
205		}
206		macherror(r, "semaphore_create")
207	}
208	if rx.body.msgh_descriptor_count != 1 {
209		unimplemented("mach_semcreate desc count")
210	}
211	return rx.semaphore.name
212}
213
214func mach_semdestroy(sem uint32) {
215	var m [256]uint8
216	tx := (*tmach_semdestroymsg)(unsafe.Pointer(&m))
217
218	tx.h.msgh_bits = _MACH_MSGH_BITS_COMPLEX
219	tx.h.msgh_size = uint32(unsafe.Sizeof(*tx))
220	tx.h.msgh_remote_port = mach_task_self()
221	tx.h.msgh_id = tmach_semdestroy
222	tx.body.msgh_descriptor_count = 1
223	tx.semaphore.name = sem
224	tx.semaphore.disposition = _MACH_MSG_TYPE_MOVE_SEND
225	tx.semaphore._type = 0
226
227	for {
228		r := machcall(&tx.h, int32(unsafe.Sizeof(m)), 0)
229		if r == 0 {
230			break
231		}
232		if r == _KERN_ABORTED { // interrupted
233			continue
234		}
235		macherror(r, "semaphore_destroy")
236	}
237}
238
239//extern semaphore_wait
240func mach_semaphore_wait(sema uint32) int32
241
242//extern semaphore_timedwait
243func mach_semaphore_timedwait(sema, sec, nsec uint32) int32
244
245//extern semaphore_signal
246func mach_semaphore_signal(sema uint32) int32
247
248//extern semaphore_signal_all
249func mach_semaphore_signal_all(sema uint32) int32
250
251func semasleep1(ns int64) int32 {
252	_g_ := getg()
253
254	if ns >= 0 {
255		var nsecs int32
256		secs := timediv(ns, 1000000000, &nsecs)
257		r := mach_semaphore_timedwait(_g_.m.mos.waitsema, uint32(secs), uint32(nsecs))
258		if r == _KERN_ABORTED || r == _KERN_OPERATION_TIMED_OUT {
259			return -1
260		}
261		if r != 0 {
262			macherror(r, "semaphore_wait")
263		}
264		return 0
265	}
266
267	for {
268		r := mach_semaphore_wait(_g_.m.mos.waitsema)
269		if r == 0 {
270			break
271		}
272		// Note: We don't know how this call (with no timeout) can get _KERN_OPERATION_TIMED_OUT,
273		// but it does reliably, though at a very low rate, on OS X 10.8, 10.9, 10.10, and 10.11.
274		// See golang.org/issue/17161.
275		if r == _KERN_ABORTED || r == _KERN_OPERATION_TIMED_OUT { // interrupted
276			continue
277		}
278		macherror(r, "semaphore_wait")
279	}
280	return 0
281}
282
283//go:nosplit
284func semasleep(ns int64) int32 {
285	var r int32
286	systemstack(func() {
287		r = semasleep1(ns)
288	})
289	return r
290}
291
292//go:nosplit
293func mach_semrelease(sem uint32) {
294	for {
295		r := mach_semaphore_signal(sem)
296		if r == 0 {
297			break
298		}
299		if r == _KERN_ABORTED { // interrupted
300			continue
301		}
302
303		// mach_semrelease must be completely nosplit,
304		// because it is called from Go code.
305		// If we're going to die, start that process on the system stack
306		// to avoid a Go stack split.
307		systemstack(func() { macherror(r, "semaphore_signal") })
308	}
309}
310
311type machheader struct {
312	msgh_bits        uint32
313	msgh_size        uint32
314	msgh_remote_port uint32
315	msgh_local_port  uint32
316	msgh_reserved    uint32
317	msgh_id          int32
318}
319
320type machndr struct {
321	mig_vers     uint8
322	if_vers      uint8
323	reserved1    uint8
324	mig_encoding uint8
325	int_rep      uint8
326	char_rep     uint8
327	float_rep    uint8
328	reserved2    uint8
329}
330