rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/runtime/sys_darwin_arm64.s (about)

     1  // Copyright 2015 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  
     5  // System calls and other sys.stuff for ARM64, Darwin
     6  // See http://fxr.watson.org/fxr/source/bsd/kern/syscalls.c?v=xnu-1228
     7  // or /usr/include/sys/syscall.h (on a Mac) for system call numbers.
     8  
     9  #include "go_asm.h"
    10  #include "go_tls.h"
    11  #include "textflag.h"
    12  
    13  // Copied from /usr/include/sys/syscall.h
    14  #define	SYS_exit           1
    15  #define	SYS_read           3
    16  #define	SYS_write          4
    17  #define	SYS_open           5
    18  #define	SYS_close          6
    19  #define	SYS_mmap           197
    20  #define	SYS_munmap         73
    21  #define	SYS_madvise        75
    22  #define	SYS_mincore        78
    23  #define	SYS_gettimeofday   116
    24  #define	SYS_kill           37
    25  #define	SYS_getpid         20
    26  #define	SYS___pthread_kill 328
    27  #define	SYS_setitimer      83
    28  #define	SYS___sysctl       202
    29  #define	SYS_sigprocmask    48
    30  #define	SYS_sigaction      46
    31  #define	SYS_sigreturn      184
    32  #define	SYS_select         93
    33  #define	SYS_bsdthread_register 366
    34  #define	SYS_bsdthread_create 360
    35  #define	SYS_bsdthread_terminate 361
    36  #define	SYS_kqueue         362
    37  #define	SYS_kevent         363
    38  #define	SYS_fcntl          92
    39  
    40  TEXT notok<>(SB),NOSPLIT,$0
    41  	MOVD	$0, R8
    42  	MOVD	R8, (R8)
    43  	B	0(PC)
    44  
    45  TEXT runtime·open(SB),NOSPLIT,$0
    46  	MOVD	name+0(FP), R0
    47  	MOVW	mode+8(FP), R1
    48  	MOVW	perm+12(FP), R2
    49  	MOVD	$SYS_open, R16
    50  	SVC	$0x80
    51  	CSINV	LO, R0, ZR, R0
    52  	MOVW	R0, ret+16(FP)
    53  	RET
    54  
    55  TEXT runtime·closefd(SB),NOSPLIT,$0
    56  	MOVW	fd+0(FP), R0
    57  	MOVW	$SYS_close, R16
    58  	SVC	$0x80
    59  	CSINV	LO, R0, ZR, R0
    60  	MOVW	R0, ret+8(FP)
    61  	RET
    62  
    63  TEXT runtime·write(SB),NOSPLIT,$0
    64  	MOVW	fd+0(FP), R0
    65  	MOVD	p+8(FP), R1
    66  	MOVW	n+16(FP), R2
    67  	MOVW	$SYS_write, R16
    68  	SVC	$0x80
    69  	CSINV	LO, R0, ZR, R0
    70  	MOVW	R0, ret+24(FP)
    71  	RET
    72  
    73  TEXT runtime·read(SB),NOSPLIT,$0
    74  	MOVW	fd+0(FP), R0
    75  	MOVD	p+8(FP), R1
    76  	MOVW	n+16(FP), R2
    77  	MOVW	$SYS_read, R16
    78  	SVC	$0x80
    79  	CSINV	LO, R0, ZR, R0
    80  	MOVW	R0, ret+24(FP)
    81  	RET
    82  
    83  TEXT runtime·exit(SB),NOSPLIT,$-8
    84  	MOVW	n+0(FP), R0
    85  	MOVW	$SYS_exit, R16
    86  	SVC	$0x80
    87  	MOVD	$1234, R0
    88  	MOVD	$1002, R1
    89  	MOVD	R0, (R1)	// fail hard
    90  
    91  // Exit this OS thread (like pthread_exit, which eventually
    92  // calls __bsdthread_terminate).
    93  TEXT runtime·exit1(SB),NOSPLIT,$0
    94  	MOVW	$SYS_bsdthread_terminate, R16
    95  	SVC	$0x80
    96  	MOVD	$1234, R0
    97  	MOVD	$1003, R1
    98  	MOVD	R0, (R1)	// fail hard
    99  
   100  TEXT runtime·raise(SB),NOSPLIT,$0
   101  	MOVW	$SYS_getpid, R16
   102  	SVC	$0x80
   103  	// arg 1 pid already in R0 from getpid
   104  	MOVW	sig+0(FP), R1	// arg 2 - signal
   105  	MOVW	$1, R2	// arg 3 - posix
   106  	MOVW	$SYS_kill, R16
   107  	SVC	$0x80
   108  	RET
   109  
   110  TEXT runtime·mmap(SB),NOSPLIT,$0
   111  	MOVD	addr+0(FP), R0
   112  	MOVD	n+8(FP), R1
   113  	MOVW	prot+16(FP), R2
   114  	MOVW	flags+20(FP), R3
   115  	MOVW	fd+24(FP), R4
   116  	MOVW	off+28(FP), R5
   117  	MOVW	$SYS_mmap, R16
   118  	SVC	$0x80
   119  	MOVD	R0, ret+32(FP)
   120  	RET
   121  
   122  TEXT runtime·munmap(SB),NOSPLIT,$0
   123  	MOVD	addr+0(FP), R0
   124  	MOVD	n+8(FP), R1
   125  	MOVW	$SYS_munmap, R16
   126  	SVC	$0x80
   127  	BCC	2(PC)
   128  	BL	notok<>(SB)
   129  	RET
   130  
   131  TEXT runtime·madvise(SB),NOSPLIT,$0
   132  	MOVD	addr+0(FP), R0
   133  	MOVD	n+8(FP), R1
   134  	MOVW	flags+16(FP), R2
   135  	MOVW	$SYS_madvise, R16
   136  	SVC	$0x80
   137  	BCC	2(PC)
   138  	BL	notok<>(SB)
   139  	RET
   140  
   141  TEXT runtime·setitimer(SB),NOSPLIT,$0
   142  	MOVW	mode+0(FP), R0
   143  	MOVD	new+8(FP), R1
   144  	MOVD	old+16(FP), R2
   145  	MOVW	$SYS_setitimer, R16
   146  	SVC	$0x80
   147  	RET
   148  
   149  TEXT time·now(SB),NOSPLIT,$32-12
   150  	MOVD	RSP, R0	// timeval
   151  	MOVD	R0, R9	// this is how dyld calls gettimeofday
   152  	MOVW	$0, R1	// zone
   153  	MOVW	$SYS_gettimeofday, R16
   154  	SVC	$0x80	// Note: x0 is tv_sec, w1 is tv_usec
   155  
   156  	MOVD	R0, sec+0(FP)
   157  	MOVW	$1000, R3
   158  	MUL	R3, R1
   159  	MOVW	R1, nsec+8(FP)
   160  	RET
   161  
   162  TEXT runtime·nanotime(SB),NOSPLIT,$32
   163  	MOVD	RSP, R0	// timeval
   164  	MOVD	R0, R9	// this is how dyld calls gettimeofday
   165  	MOVW	$0, R1	// zone
   166  	MOVW	$SYS_gettimeofday, R16
   167  	SVC	$0x80	// Note: x0 is tv_sec, w1 is tv_usec
   168  
   169  	MOVW	$1000000000, R3
   170  	MUL	R3, R0
   171  	MOVW	$1000, R3
   172  	MUL	R3, R1
   173  	ADD	R1, R0
   174  
   175  	MOVD	R0, ret+0(FP)
   176  	RET
   177  
   178  // Sigtramp's job is to call the actual signal handler.
   179  // It is called with the following arguments on the stack:
   180  //	LR	"return address" - ignored
   181  //	R0	actual handler
   182  //	R1	siginfo style - ignored
   183  //	R2	signal number
   184  //	R3	siginfo
   185  //	R4	context
   186  TEXT runtime·sigtramp(SB),NOSPLIT,$0
   187  	// this might be called in external code context,
   188  	// where g is not set.
   189  	// first save R0, because runtime·load_g will clobber it
   190  	MOVD.W	R0, -16(RSP)	// note: stack must be 16-byte aligned
   191  	MOVB	runtime·iscgo(SB), R0
   192  	CMP	$0, R0
   193  	BEQ	2(PC)
   194  	BL	runtime·load_g(SB)
   195  
   196  	CMP	$0, g
   197  	BNE	cont
   198  	// fake function call stack frame for badsignal
   199  	// we only need to pass R2 (signal number), but
   200  	// badsignal will expect R2 at 8(RSP), so we also
   201  	// push R1 onto stack. turns out we do need R1
   202  	// to do sigreturn.
   203  	MOVD.W	R1, -16(RSP)
   204  	MOVD	R2, 8(RSP)
   205  	MOVD	R4, 24(RSP)	// save ucontext, badsignal might clobber R4
   206  	MOVD	$runtime·badsignal(SB), R26
   207  	BL	(R26)
   208  	MOVD	0(RSP), R1	// saved infostype
   209  	MOVD	24(RSP), R0	// the ucontext
   210  	ADD	$(16+16), RSP
   211  	B	ret
   212  
   213  cont:
   214  	// Restore R0
   215  	MOVD.P	16(RSP), R0
   216  
   217  	// NOTE: some Darwin/ARM kernels always use the main stack to run the
   218  	// signal handler. We need to switch to gsignal ourselves.
   219  	MOVD	g_m(g), R11
   220  	MOVD	m_gsignal(R11), R5
   221  	MOVD	(g_stack+stack_hi)(R5), R6
   222  	SUB	$64, R6
   223  
   224  	// copy arguments for call to sighandler
   225  	MOVD	R2, 8(R6)	// signal num
   226  	MOVD	R3, 16(R6)	// signal info
   227  	MOVD	R4, 24(R6)	// context
   228  	MOVD	g, 32(R6)	// old_g
   229  
   230  	// Backup ucontext and infostyle
   231  	MOVD	R4, 40(R6)
   232  	MOVD	R1, 48(R6)
   233  
   234  	// switch stack and g
   235  	MOVD	R6, RSP	// sigtramp can not re-entrant, so no need to back up RSP.
   236  	MOVD	R5, g
   237  
   238  	BL	(R0)
   239  
   240  	// call sigreturn
   241  	MOVD	40(RSP), R0	// saved ucontext
   242  	MOVD	48(RSP), R1	// saved infostyle
   243  ret:
   244  	MOVW	$SYS_sigreturn, R16 // sigreturn(ucontext, infostyle)
   245  	SVC	$0x80
   246  
   247  	// if sigreturn fails, we can do nothing but exit
   248  	B	runtime·exit(SB)
   249  
   250  TEXT runtime·sigprocmask(SB),NOSPLIT,$0
   251  	MOVW	sig+0(FP), R0
   252  	MOVD	new+8(FP), R1
   253  	MOVD	old+16(FP), R2
   254  	MOVW	$SYS_sigprocmask, R16
   255  	SVC	$0x80
   256  	BCC	2(PC)
   257  	BL	notok<>(SB)
   258  	RET
   259  
   260  TEXT runtime·sigaction(SB),NOSPLIT,$0
   261  	MOVW	mode+0(FP), R0
   262  	MOVD	new+8(FP), R1
   263  	MOVD	old+16(FP), R2
   264  	MOVW	$SYS_sigaction, R16
   265  	SVC	$0x80
   266  	BCC	2(PC)
   267  	BL	notok<>(SB)
   268  	RET
   269  
   270  TEXT runtime·usleep(SB),NOSPLIT,$12
   271  	MOVW	usec+0(FP), R0
   272  	MOVW	R0, R1
   273  	MOVW	$1000000, R2
   274  	UDIV	R2, R0
   275  	MUL	R0, R2
   276  	SUB	R2, R1
   277  	MOVD	R0, 0(RSP)
   278  	MOVW	R1, 8(RSP)
   279  
   280  	// select(0, 0, 0, 0, &tv)
   281  	MOVW	$0, R0
   282  	MOVW	$0, R1
   283  	MOVW	$0, R2
   284  	MOVW	$0, R3
   285  	MOVD	RSP, R4
   286  	MOVW	$SYS_select, R16
   287  	SVC	$0x80
   288  	RET
   289  
   290  TEXT runtime·sysctl(SB),NOSPLIT,$0
   291  	MOVD	mib+0(FP), R0
   292  	MOVW	miblen+8(FP), R1
   293  	MOVD	out+16(FP), R2
   294  	MOVD	size+24(FP), R3
   295  	MOVD	dst+32(FP), R4
   296  	MOVD	ndst+40(FP), R5
   297  	MOVW	$SYS___sysctl, R16
   298  	SVC	$0x80
   299  	BCC	ok
   300  	NEG	R0, R0
   301  	MOVW	R0, ret+48(FP)
   302  	RET
   303  ok:
   304  	MOVW	$0, R0
   305  	MOVW	R0, ret+48(FP)
   306  	RET
   307  
   308  // Thread related functions
   309  // Note: On darwin/arm64, it is no longer possible to use bsdthread_register
   310  // as the libc is always linked in. The runtime must use runtime/cgo to
   311  // create threads, so all thread related functions will just exit with a
   312  // unique status.
   313  // void bsdthread_create(void *stk, M *m, G *g, void (*fn)(void))
   314  TEXT runtime·bsdthread_create(SB),NOSPLIT,$0
   315  	MOVD	$44, R0
   316  	MOVW	$SYS_exit, R16
   317  	SVC	$0x80
   318  	RET
   319  
   320  // The thread that bsdthread_create creates starts executing here,
   321  // because we registered this function using bsdthread_register
   322  // at startup.
   323  //	R0 = "pthread"
   324  //	R1 = mach thread port
   325  //	R2 = "func" (= fn)
   326  //	R3 = "arg" (= m)
   327  //	R4 = stack
   328  //	R5 = flags (= 0)
   329  TEXT runtime·bsdthread_start(SB),NOSPLIT,$0
   330  	MOVD	$45, R0
   331  	MOVW	$SYS_exit, R16
   332  	SVC	$0x80
   333  	RET
   334  
   335  // int32 bsdthread_register(void)
   336  // registers callbacks for threadstart (see bsdthread_create above
   337  // and wqthread and pthsize (not used).  returns 0 on success.
   338  TEXT runtime·bsdthread_register(SB),NOSPLIT,$0
   339  	MOVD	$46, R0
   340  	MOVW	$SYS_exit, R16
   341  	SVC	$0x80
   342  	RET
   343  
   344  // uint32 mach_msg_trap(void*, uint32, uint32, uint32, uint32, uint32, uint32)
   345  TEXT runtime·mach_msg_trap(SB),NOSPLIT,$0
   346  	MOVD	h+0(FP), R0
   347  	MOVW	op+8(FP), R1
   348  	MOVW	send_size+12(FP), R2
   349  	MOVW	rcv_size+16(FP), R3
   350  	MOVW	rcv_name+20(FP), R4
   351  	MOVW	timeout+24(FP), R5
   352  	MOVW	notify+28(FP), R6
   353  	MOVN	$30, R16
   354  	SVC	$0x80
   355  	MOVW	R0, ret+32(FP)
   356  	RET
   357  
   358  TEXT runtime·mach_task_self(SB),NOSPLIT,$0
   359  	MOVN	$27, R16 // task_self_trap
   360  	SVC	$0x80
   361  	MOVW	R0, ret+0(FP)
   362  	RET
   363  
   364  TEXT runtime·mach_thread_self(SB),NOSPLIT,$0
   365  	MOVN	$26, R16 // thread_self_trap
   366  	SVC	$0x80
   367  	MOVW	R0, ret+0(FP)
   368  	RET
   369  
   370  TEXT runtime·mach_reply_port(SB),NOSPLIT,$0
   371  	MOVN	$25, R16	// mach_reply_port
   372  	SVC	$0x80
   373  	MOVW	R0, ret+0(FP)
   374  	RET
   375  
   376  // Mach provides trap versions of the semaphore ops,
   377  // instead of requiring the use of RPC.
   378  
   379  // uint32 mach_semaphore_wait(uint32)
   380  TEXT runtime·mach_semaphore_wait(SB),NOSPLIT,$0
   381  	MOVW	sema+0(FP), R0
   382  	MOVN	$35, R16	// semaphore_wait_trap
   383  	SVC	$0x80
   384  	MOVW	R0, ret+8(FP)
   385  	RET
   386  
   387  // uint32 mach_semaphore_timedwait(uint32, uint32, uint32)
   388  TEXT runtime·mach_semaphore_timedwait(SB),NOSPLIT,$0
   389  	MOVW	sema+0(FP), R0
   390  	MOVW	sec+4(FP), R1
   391  	MOVW	nsec+8(FP), R2
   392  	MOVN	$37, R16	// semaphore_timedwait_trap
   393  	SVC	$0x80
   394  	MOVW	R0, ret+16(FP)
   395  	RET
   396  
   397  // uint32 mach_semaphore_signal(uint32)
   398  TEXT runtime·mach_semaphore_signal(SB),NOSPLIT,$0
   399  	MOVW	sema+0(FP), R0
   400  	MOVN	$32, R16	// semaphore_signal_trap
   401  	SVC	$0x80
   402  	MOVW	R0, ret+8(FP)
   403  	RET
   404  
   405  // uint32 mach_semaphore_signal_all(uint32)
   406  TEXT runtime·mach_semaphore_signal_all(SB),NOSPLIT,$0
   407  	MOVW	sema+0(FP), R0
   408  	MOVN	$33, R16	// semaphore_signal_all_trap
   409  	SVC	$0x80
   410  	MOVW	R0, ret+8(FP)
   411  	RET
   412  
   413  // int32 runtime·kqueue(void)
   414  TEXT runtime·kqueue(SB),NOSPLIT,$0
   415  	MOVW	$SYS_kqueue, R16
   416  	SVC	$0x80
   417  	BCC	2(PC)
   418  	NEG	R0, R0
   419  	MOVW	R0, ret+0(FP)
   420  	RET
   421  
   422  // int32 runtime·kevent(int kq, Kevent *changelist, int nchanges, Kevent *eventlist, int events, Timespec *timeout)
   423  TEXT runtime·kevent(SB),NOSPLIT,$0
   424  	MOVW	kq+0(FP), R0
   425  	MOVD	changelist+8(FP), R1
   426  	MOVW	nchanges+16(FP), R2
   427  	MOVD	eventlist+24(FP), R3
   428  	MOVW	nevents+32(FP), R4
   429  	MOVD	timeout+40(FP), R5
   430  	MOVW	$SYS_kevent, R16
   431  	SVC	$0x80
   432  	BCC	2(PC)
   433  	NEG	R0, R0
   434  	MOVW	R0, ret+48(FP)
   435  	RET
   436  
   437  // int32 runtime·closeonexec(int32 fd)
   438  TEXT runtime·closeonexec(SB),NOSPLIT,$0
   439  	MOVW	fd+0(FP), R0
   440  	MOVW	$2, R1	// F_SETFD
   441  	MOVW	$1, R2	// FD_CLOEXEC
   442  	MOVW	$SYS_fcntl, R16
   443  	SVC	$0x80
   444  	RET
   445  
   446  // sigaltstack on some darwin/arm version is buggy and will always
   447  // run the signal handler on the main stack, so our sigtramp has
   448  // to do the stack switch ourselves.
   449  TEXT runtime·sigaltstack(SB),NOSPLIT,$0
   450  	RET