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