runtime: scan register backing store on ia64
[official-gcc.git] / libgo / go / runtime / os_darwin.go
blobec92370ad7097b56a3361e7e34ba162d1374fe73
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.
5 package runtime
7 import "unsafe"
9 type mOS struct {
10 machport uint32 // return address for mach ipc
11 waitsema uint32 // semaphore for parking on locks
14 //go:noescape
15 //extern mach_msg_trap
16 func mach_msg_trap(h unsafe.Pointer, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32
18 //extern mach_reply_port
19 func mach_reply_port() uint32
21 //extern mach_task_self
22 func mach_task_self() uint32
24 func unimplemented(name string) {
25 println(name, "not implemented")
26 *(*int)(unsafe.Pointer(uintptr(1231))) = 1231
29 //go:nosplit
30 func semawakeup(mp *m) {
31 mach_semrelease(mp.mos.waitsema)
34 //go:nosplit
35 func semacreate(mp *m) {
36 if mp.mos.waitsema != 0 {
37 return
39 systemstack(func() {
40 mp.mos.waitsema = mach_semcreate()
44 // Mach IPC, to get at semaphores
45 // Definitions are in /usr/include/mach on a Mac.
47 func macherror(r int32, fn string) {
48 print("mach error ", fn, ": ", r, "\n")
49 throw("mach error")
52 const _DebugMach = false
54 var zerondr machndr
56 func mach_msgh_bits(a, b uint32) uint32 {
57 return a | b<<8
60 func 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)
65 // Mach RPC (MIG)
66 const (
67 _MinMachMsg = 48
68 _MachReply = 100
71 type codemsg struct {
72 h machheader
73 ndr machndr
74 code int32
77 func 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
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
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")
100 if i%8 != 0 {
101 print("\n")
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")
109 return ret
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")
120 if i%8 != 0 {
121 print("\n")
124 if h.msgh_id != id+_MachReply {
125 if _DebugMach {
126 print("mach_msg _MachReply id mismatch ", h.msgh_id, " != ", id+_MachReply, "\n")
128 return -303 // MIG_REPLY_MISMATCH
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")
141 return c.code
143 if h.msgh_size != uint32(rxsize) {
144 if _DebugMach {
145 print("mach_msg _MachReply size mismatch ", h.msgh_size, " != ", rxsize, "\n")
147 return -307 // MIG_ARRAY_TOO_LARGE
149 return 0
152 // Semaphores!
154 const (
155 tmach_semcreate = 3418
156 rmach_semcreate = tmach_semcreate + _MachReply
158 tmach_semdestroy = 3419
159 rmach_semdestroy = tmach_semdestroy + _MachReply
161 _KERN_ABORTED = 14
162 _KERN_OPERATION_TIMED_OUT = 49
165 type tmach_semcreatemsg struct {
166 h machheader
167 ndr machndr
168 policy int32
169 value int32
172 type rmach_semcreatemsg struct {
173 h machheader
174 body machbody
175 semaphore machport
178 type tmach_semdestroymsg struct {
179 h machheader
180 body machbody
181 semaphore machport
184 func mach_semcreate() uint32 {
185 var m [256]uint8
186 tx := (*tmach_semcreatemsg)(unsafe.Pointer(&m))
187 rx := (*rmach_semcreatemsg)(unsafe.Pointer(&m))
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
195 tx.policy = 0 // 0 = SYNC_POLICY_FIFO
196 tx.value = 0
198 for {
199 r := machcall(&tx.h, int32(unsafe.Sizeof(m)), int32(unsafe.Sizeof(*rx)))
200 if r == 0 {
201 break
203 if r == _KERN_ABORTED { // interrupted
204 continue
206 macherror(r, "semaphore_create")
208 if rx.body.msgh_descriptor_count != 1 {
209 unimplemented("mach_semcreate desc count")
211 return rx.semaphore.name
214 func mach_semdestroy(sem uint32) {
215 var m [256]uint8
216 tx := (*tmach_semdestroymsg)(unsafe.Pointer(&m))
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
227 for {
228 r := machcall(&tx.h, int32(unsafe.Sizeof(m)), 0)
229 if r == 0 {
230 break
232 if r == _KERN_ABORTED { // interrupted
233 continue
235 macherror(r, "semaphore_destroy")
239 //extern semaphore_wait
240 func mach_semaphore_wait(sema uint32) int32
242 //extern semaphore_timedwait
243 func mach_semaphore_timedwait(sema, sec, nsec uint32) int32
245 //extern semaphore_signal
246 func mach_semaphore_signal(sema uint32) int32
248 //extern semaphore_signal_all
249 func mach_semaphore_signal_all(sema uint32) int32
251 func semasleep1(ns int64) int32 {
252 _g_ := getg()
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
261 if r != 0 {
262 macherror(r, "semaphore_wait")
264 return 0
267 for {
268 r := mach_semaphore_wait(_g_.m.mos.waitsema)
269 if r == 0 {
270 break
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
278 macherror(r, "semaphore_wait")
280 return 0
283 //go:nosplit
284 func semasleep(ns int64) int32 {
285 var r int32
286 systemstack(func() {
287 r = semasleep1(ns)
289 return r
292 //go:nosplit
293 func mach_semrelease(sem uint32) {
294 for {
295 r := mach_semaphore_signal(sem)
296 if r == 0 {
297 break
299 if r == _KERN_ABORTED { // interrupted
300 continue
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") })
311 type 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
320 type 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