github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/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_pthread_sigmask 329
    28  #define	SYS_setitimer      83
    29  #define	SYS___sysctl       202
    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	code+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  	// Ideally we'd send the signal to the current thread,
   102  	// not the whole process, but that's too hard on OS X.
   103  	JMP	runtime·raiseproc(SB)
   104  
   105  TEXT runtime·raiseproc(SB),NOSPLIT,$0
   106  	MOVW	$SYS_getpid, R16
   107  	SVC	$0x80
   108  	// arg 1 pid already in R0 from getpid
   109  	MOVW	sig+0(FP), R1	// arg 2 - signal
   110  	MOVW	$1, R2	// arg 3 - posix
   111  	MOVW	$SYS_kill, R16
   112  	SVC	$0x80
   113  	RET
   114  
   115  TEXT runtime·mmap(SB),NOSPLIT,$0
   116  	MOVD	addr+0(FP), R0
   117  	MOVD	n+8(FP), R1
   118  	MOVW	prot+16(FP), R2
   119  	MOVW	flags+20(FP), R3
   120  	MOVW	fd+24(FP), R4
   121  	MOVW	off+28(FP), R5
   122  	MOVW	$SYS_mmap, R16
   123  	SVC	$0x80
   124  	MOVD	R0, ret+32(FP)
   125  	RET
   126  
   127  TEXT runtime·munmap(SB),NOSPLIT,$0
   128  	MOVD	addr+0(FP), R0
   129  	MOVD	n+8(FP), R1
   130  	MOVW	$SYS_munmap, R16
   131  	SVC	$0x80
   132  	BCC	2(PC)
   133  	BL	notok<>(SB)
   134  	RET
   135  
   136  TEXT runtime·madvise(SB),NOSPLIT,$0
   137  	MOVD	addr+0(FP), R0
   138  	MOVD	n+8(FP), R1
   139  	MOVW	flags+16(FP), R2
   140  	MOVW	$SYS_madvise, R16
   141  	SVC	$0x80
   142  	BCC	2(PC)
   143  	BL	notok<>(SB)
   144  	RET
   145  
   146  TEXT runtime·setitimer(SB),NOSPLIT,$0
   147  	MOVW	mode+0(FP), R0
   148  	MOVD	new+8(FP), R1
   149  	MOVD	old+16(FP), R2
   150  	MOVW	$SYS_setitimer, R16
   151  	SVC	$0x80
   152  	RET
   153  
   154  TEXT time·now(SB),NOSPLIT,$40-12
   155  	MOVD	RSP, R0	// timeval
   156  	MOVD	R0, R9	// this is how dyld calls gettimeofday
   157  	MOVW	$0, R1	// zone
   158  	MOVD	$0, R2	// see issue 16570
   159  	MOVW	$SYS_gettimeofday, R16
   160  	SVC	$0x80	// Note: x0 is tv_sec, w1 is tv_usec
   161  	CMP	$0, R0
   162  	BNE	inreg
   163  	MOVD	0(RSP), R0
   164  	MOVW	8(RSP), R1
   165  inreg:
   166  	MOVD	R0, sec+0(FP)
   167  	MOVW	$1000, R3
   168  	MUL	R3, R1
   169  	MOVW	R1, nsec+8(FP)
   170  	RET
   171  
   172  TEXT runtime·nanotime(SB),NOSPLIT,$40
   173  	MOVD	RSP, R0	// timeval
   174  	MOVD	R0, R9	// this is how dyld calls gettimeofday
   175  	MOVW	$0, R1	// zone
   176  	MOVD	$0, R2	// see issue 16570
   177  	MOVW	$SYS_gettimeofday, R16
   178  	SVC	$0x80	// Note: x0 is tv_sec, w1 is tv_usec
   179  	CMP	$0, R0
   180  	BNE	inreg
   181  	MOVD	0(RSP), R0
   182  	MOVW	8(RSP), R1
   183  inreg:
   184  	MOVW	$1000000000, R3
   185  	MUL	R3, R0
   186  	MOVW	$1000, R3
   187  	MUL	R3, R1
   188  	ADD	R1, R0
   189  
   190  	MOVD	R0, ret+0(FP)
   191  	RET
   192  
   193  TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
   194  	MOVW	sig+8(FP), R0
   195  	MOVD	info+16(FP), R1
   196  	MOVD	ctx+24(FP), R2
   197  	MOVD	fn+0(FP), R11
   198  	BL	(R11)
   199  	RET
   200  
   201  // Sigtramp's job is to call the actual signal handler.
   202  // It is called with the following arguments on the stack:
   203  //	LR	"return address" - ignored
   204  //	R0	actual handler
   205  //	R1	siginfo style - ignored
   206  //	R2	signal number
   207  //	R3	siginfo
   208  //	R4	context
   209  TEXT runtime·sigtramp(SB),NOSPLIT,$0
   210  	// this might be called in external code context,
   211  	// where g is not set.
   212  	// first save R0, because runtime·load_g will clobber it
   213  	MOVD.W	R0, -16(RSP)	// note: stack must be 16-byte aligned
   214  	MOVB	runtime·iscgo(SB), R0
   215  	CMP	$0, R0
   216  	BEQ	2(PC)
   217  	BL	runtime·load_g(SB)
   218  
   219  	CMP	$0, g
   220  	BNE	cont
   221  	// fake function call stack frame for badsignal
   222  	// we only need to pass R2 (signal number), but
   223  	// badsignal will expect R2 at 8(RSP), so we also
   224  	// push R1 onto stack. turns out we do need R1
   225  	// to do sigreturn.
   226  	MOVD.W	R1, -16(RSP)
   227  	MOVD	R2, 8(RSP)
   228  	MOVD	R4, 24(RSP)	// save ucontext, badsignal might clobber R4
   229  	MOVD	$runtime·badsignal(SB), R26
   230  	BL	(R26)
   231  	MOVD	0(RSP), R1	// saved infostype
   232  	MOVD	24(RSP), R0	// the ucontext
   233  	ADD	$(16+16), RSP
   234  	B	ret
   235  
   236  cont:
   237  	// Restore R0
   238  	MOVD.P	16(RSP), R0
   239  
   240  	// NOTE: some Darwin/ARM kernels always use the main stack to run the
   241  	// signal handler. We need to switch to gsignal ourselves.
   242  	MOVD	g_m(g), R11
   243  	MOVD	m_gsignal(R11), R5
   244  	MOVD	(g_stack+stack_hi)(R5), R6
   245  	SUB	$64, R6
   246  
   247  	// copy arguments for call to sighandler
   248  	MOVD	R2, 8(R6)	// signal num
   249  	MOVD	R3, 16(R6)	// signal info
   250  	MOVD	R4, 24(R6)	// context
   251  	MOVD	g, 32(R6)	// old_g
   252  
   253  	// Backup ucontext and infostyle
   254  	MOVD	R4, 40(R6)
   255  	MOVD	R1, 48(R6)
   256  
   257  	// switch stack and g
   258  	MOVD	R6, RSP	// sigtramp is not re-entrant, so no need to back up RSP.
   259  	MOVD	R5, g
   260  
   261  	BL	(R0)
   262  
   263  	// call sigreturn
   264  	MOVD	40(RSP), R0	// saved ucontext
   265  	MOVD	48(RSP), R1	// saved infostyle
   266  ret:
   267  	MOVW	$SYS_sigreturn, R16 // sigreturn(ucontext, infostyle)
   268  	SVC	$0x80
   269  
   270  	// if sigreturn fails, we can do nothing but exit
   271  	B	runtime·exit(SB)
   272  
   273  TEXT runtime·sigprocmask(SB),NOSPLIT,$0
   274  	MOVW	sig+0(FP), R0
   275  	MOVD	new+8(FP), R1
   276  	MOVD	old+16(FP), R2
   277  	MOVW	$SYS_pthread_sigmask, R16
   278  	SVC	$0x80
   279  	BCC	2(PC)
   280  	BL	notok<>(SB)
   281  	RET
   282  
   283  TEXT runtime·sigaction(SB),NOSPLIT,$0
   284  	MOVW	mode+0(FP), R0
   285  	MOVD	new+8(FP), R1
   286  	MOVD	old+16(FP), R2
   287  	MOVW	$SYS_sigaction, R16
   288  	SVC	$0x80
   289  	BCC	2(PC)
   290  	BL	notok<>(SB)
   291  	RET
   292  
   293  TEXT runtime·usleep(SB),NOSPLIT,$24
   294  	MOVW	usec+0(FP), R0
   295  	MOVW	R0, R1
   296  	MOVW	$1000000, R2
   297  	UDIV	R2, R0
   298  	MUL	R0, R2
   299  	SUB	R2, R1
   300  	MOVD	R0, 0(RSP)
   301  	MOVW	R1, 8(RSP)
   302  
   303  	// select(0, 0, 0, 0, &tv)
   304  	MOVW	$0, R0
   305  	MOVW	$0, R1
   306  	MOVW	$0, R2
   307  	MOVW	$0, R3
   308  	MOVD	RSP, R4
   309  	MOVW	$SYS_select, R16
   310  	SVC	$0x80
   311  	RET
   312  
   313  TEXT runtime·sysctl(SB),NOSPLIT,$0
   314  	MOVD	mib+0(FP), R0
   315  	MOVW	miblen+8(FP), R1
   316  	MOVD	out+16(FP), R2
   317  	MOVD	size+24(FP), R3
   318  	MOVD	dst+32(FP), R4
   319  	MOVD	ndst+40(FP), R5
   320  	MOVW	$SYS___sysctl, R16
   321  	SVC	$0x80
   322  	BCC	ok
   323  	NEG	R0, R0
   324  	MOVW	R0, ret+48(FP)
   325  	RET
   326  ok:
   327  	MOVW	$0, R0
   328  	MOVW	R0, ret+48(FP)
   329  	RET
   330  
   331  // Thread related functions
   332  // Note: On darwin/arm64, it is no longer possible to use bsdthread_register
   333  // as the libc is always linked in. The runtime must use runtime/cgo to
   334  // create threads, so all thread related functions will just exit with a
   335  // unique status.
   336  // void bsdthread_create(void *stk, M *m, G *g, void (*fn)(void))
   337  TEXT runtime·bsdthread_create(SB),NOSPLIT,$0
   338  	MOVD	$44, R0
   339  	MOVW	$SYS_exit, R16
   340  	SVC	$0x80
   341  	RET
   342  
   343  // The thread that bsdthread_create creates starts executing here,
   344  // because we registered this function using bsdthread_register
   345  // at startup.
   346  //	R0 = "pthread"
   347  //	R1 = mach thread port
   348  //	R2 = "func" (= fn)
   349  //	R3 = "arg" (= m)
   350  //	R4 = stack
   351  //	R5 = flags (= 0)
   352  TEXT runtime·bsdthread_start(SB),NOSPLIT,$0
   353  	MOVD	$45, R0
   354  	MOVW	$SYS_exit, R16
   355  	SVC	$0x80
   356  	RET
   357  
   358  // int32 bsdthread_register(void)
   359  // registers callbacks for threadstart (see bsdthread_create above
   360  // and wqthread and pthsize (not used).  returns 0 on success.
   361  TEXT runtime·bsdthread_register(SB),NOSPLIT,$0
   362  	MOVD	$46, R0
   363  	MOVW	$SYS_exit, R16
   364  	SVC	$0x80
   365  	RET
   366  
   367  // uint32 mach_msg_trap(void*, uint32, uint32, uint32, uint32, uint32, uint32)
   368  TEXT runtime·mach_msg_trap(SB),NOSPLIT,$0
   369  	MOVD	h+0(FP), R0
   370  	MOVW	op+8(FP), R1
   371  	MOVW	send_size+12(FP), R2
   372  	MOVW	rcv_size+16(FP), R3
   373  	MOVW	rcv_name+20(FP), R4
   374  	MOVW	timeout+24(FP), R5
   375  	MOVW	notify+28(FP), R6
   376  	MOVN	$30, R16
   377  	SVC	$0x80
   378  	MOVW	R0, ret+32(FP)
   379  	RET
   380  
   381  TEXT runtime·mach_task_self(SB),NOSPLIT,$0
   382  	MOVN	$27, R16 // task_self_trap
   383  	SVC	$0x80
   384  	MOVW	R0, ret+0(FP)
   385  	RET
   386  
   387  TEXT runtime·mach_thread_self(SB),NOSPLIT,$0
   388  	MOVN	$26, R16 // thread_self_trap
   389  	SVC	$0x80
   390  	MOVW	R0, ret+0(FP)
   391  	RET
   392  
   393  TEXT runtime·mach_reply_port(SB),NOSPLIT,$0
   394  	MOVN	$25, R16	// mach_reply_port
   395  	SVC	$0x80
   396  	MOVW	R0, ret+0(FP)
   397  	RET
   398  
   399  // Mach provides trap versions of the semaphore ops,
   400  // instead of requiring the use of RPC.
   401  
   402  // uint32 mach_semaphore_wait(uint32)
   403  TEXT runtime·mach_semaphore_wait(SB),NOSPLIT,$0
   404  	MOVW	sema+0(FP), R0
   405  	MOVN	$35, R16	// semaphore_wait_trap
   406  	SVC	$0x80
   407  	MOVW	R0, ret+8(FP)
   408  	RET
   409  
   410  // uint32 mach_semaphore_timedwait(uint32, uint32, uint32)
   411  TEXT runtime·mach_semaphore_timedwait(SB),NOSPLIT,$0
   412  	MOVW	sema+0(FP), R0
   413  	MOVW	sec+4(FP), R1
   414  	MOVW	nsec+8(FP), R2
   415  	MOVN	$37, R16	// semaphore_timedwait_trap
   416  	SVC	$0x80
   417  	MOVW	R0, ret+16(FP)
   418  	RET
   419  
   420  // uint32 mach_semaphore_signal(uint32)
   421  TEXT runtime·mach_semaphore_signal(SB),NOSPLIT,$0
   422  	MOVW	sema+0(FP), R0
   423  	MOVN	$32, R16	// semaphore_signal_trap
   424  	SVC	$0x80
   425  	MOVW	R0, ret+8(FP)
   426  	RET
   427  
   428  // uint32 mach_semaphore_signal_all(uint32)
   429  TEXT runtime·mach_semaphore_signal_all(SB),NOSPLIT,$0
   430  	MOVW	sema+0(FP), R0
   431  	MOVN	$33, R16	// semaphore_signal_all_trap
   432  	SVC	$0x80
   433  	MOVW	R0, ret+8(FP)
   434  	RET
   435  
   436  // int32 runtime·kqueue(void)
   437  TEXT runtime·kqueue(SB),NOSPLIT,$0
   438  	MOVW	$SYS_kqueue, R16
   439  	SVC	$0x80
   440  	BCC	2(PC)
   441  	NEG	R0, R0
   442  	MOVW	R0, ret+0(FP)
   443  	RET
   444  
   445  // int32 runtime·kevent(int kq, Kevent *ch, int nch, Kevent *ev, int nev, Timespec *ts)
   446  TEXT runtime·kevent(SB),NOSPLIT,$0
   447  	MOVW	kq+0(FP), R0
   448  	MOVD	ch+8(FP), R1
   449  	MOVW	nch+16(FP), R2
   450  	MOVD	ev+24(FP), R3
   451  	MOVW	nev+32(FP), R4
   452  	MOVD	ts+40(FP), R5
   453  	MOVW	$SYS_kevent, R16
   454  	SVC	$0x80
   455  	BCC	2(PC)
   456  	NEG	R0, R0
   457  	MOVW	R0, ret+48(FP)
   458  	RET
   459  
   460  // int32 runtime·closeonexec(int32 fd)
   461  TEXT runtime·closeonexec(SB),NOSPLIT,$0
   462  	MOVW	fd+0(FP), R0
   463  	MOVW	$2, R1	// F_SETFD
   464  	MOVW	$1, R2	// FD_CLOEXEC
   465  	MOVW	$SYS_fcntl, R16
   466  	SVC	$0x80
   467  	RET
   468  
   469  // sigaltstack on some darwin/arm version is buggy and will always
   470  // run the signal handler on the main stack, so our sigtramp has
   471  // to do the stack switch ourselves.
   472  TEXT runtime·sigaltstack(SB),NOSPLIT,$0
   473  	RET