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