golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/runtime/sys_darwin_arm.s (about)

     1  // Copyright 2014 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 ARM, 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  	MOVW	$0, R8
    42  	MOVW	R8, (R8)
    43  	B		0(PC)
    44  
    45  TEXT runtime·open(SB),NOSPLIT,$0
    46  	MOVW	name+0(FP), R0
    47  	MOVW	mode+4(FP), R1
    48  	MOVW	perm+8(FP), R2
    49  	MOVW	$SYS_open, R12
    50  	SWI	$0x80
    51  	MOVW.CS	$-1, R0
    52  	MOVW	R0, ret+12(FP)
    53  	RET
    54  
    55  TEXT runtime·closefd(SB),NOSPLIT,$0
    56  	MOVW	fd+0(FP), R0
    57  	MOVW	$SYS_close, R12
    58  	SWI	$0x80
    59  	MOVW.CS	$-1, R0
    60  	MOVW	R0, ret+4(FP)
    61  	RET
    62  
    63  TEXT runtime·write(SB),NOSPLIT,$0
    64  	MOVW	fd+0(FP), R0
    65  	MOVW	p+4(FP), R1
    66  	MOVW	n+8(FP), R2
    67  	MOVW	$SYS_write, R12
    68  	SWI	$0x80
    69  	MOVW.CS	$-1, R0
    70  	MOVW	R0, ret+12(FP)
    71  	RET
    72  
    73  TEXT runtime·read(SB),NOSPLIT,$0
    74  	MOVW	fd+0(FP), R0
    75  	MOVW	p+4(FP), R1
    76  	MOVW	n+8(FP), R2
    77  	MOVW	$SYS_read, R12
    78  	SWI	$0x80
    79  	MOVW.CS	$-1, R0
    80  	MOVW	R0, ret+12(FP)
    81  	RET
    82  
    83  TEXT runtime·exit(SB),NOSPLIT,$-4
    84  	MOVW	code+0(FP), R0
    85  	MOVW	$SYS_exit, R12
    86  	SWI	$0x80
    87  	MOVW	$1234, R0
    88  	MOVW	$1002, R1
    89  	MOVW	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, R12
    95  	SWI	$0x80
    96  	MOVW	$1234, R0
    97  	MOVW	$1003, R1
    98  	MOVW	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,$24
   106  	MOVW	$SYS_getpid, R12
   107  	SWI	$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, R12
   112  	SWI $0x80
   113  	RET
   114  
   115  TEXT runtime·mmap(SB),NOSPLIT,$0
   116  	MOVW	addr+0(FP), R0
   117  	MOVW	n+4(FP), R1
   118  	MOVW	prot+8(FP), R2
   119  	MOVW	flags+12(FP), R3
   120  	MOVW	fd+16(FP), R4
   121  	MOVW	off+20(FP), R5
   122  	MOVW	$0, R6 // off_t is uint64_t
   123  	MOVW	$SYS_mmap, R12
   124  	SWI	$0x80
   125  	MOVW	R0, ret+24(FP)
   126  	RET
   127  
   128  TEXT runtime·munmap(SB),NOSPLIT,$0
   129  	MOVW	addr+0(FP), R0
   130  	MOVW	n+4(FP), R1
   131  	MOVW	$SYS_munmap, R12
   132  	SWI	$0x80
   133  	BL.CS	notok<>(SB)
   134  	RET
   135  
   136  TEXT runtime·madvise(SB),NOSPLIT,$0
   137  	MOVW	addr+0(FP), R0
   138  	MOVW	n+4(FP), R1
   139  	MOVW	flags+8(FP), R2
   140  	MOVW	$SYS_madvise, R12
   141  	SWI	$0x80
   142  	BL.CS	notok<>(SB)
   143  	RET
   144  
   145  TEXT runtime·setitimer(SB),NOSPLIT,$0
   146  	MOVW	mode+0(FP), R0
   147  	MOVW	new+4(FP), R1
   148  	MOVW	old+8(FP), R2
   149  	MOVW	$SYS_setitimer, R12
   150  	SWI	$0x80
   151  	RET
   152  
   153  TEXT runtime·mincore(SB),NOSPLIT,$0
   154  	MOVW	addr+0(FP), R0
   155  	MOVW	n+4(FP), R1
   156  	MOVW	dst+8(FP), R2
   157  	MOVW	$SYS_mincore, R12
   158  	SWI	$0x80
   159  	MOVW	R0, ret+12(FP)
   160  	RET
   161  
   162  TEXT runtime·walltime(SB), 7, $32
   163  	MOVW	$8(R13), R0  // timeval
   164  	MOVW	$0, R1  // zone
   165  	MOVW	$0, R2	// see issue 16570
   166  	MOVW	$SYS_gettimeofday, R12
   167  	SWI	$0x80 // Note: R0 is tv_sec, R1 is tv_usec
   168  	CMP	$0, R0
   169  	BNE	inreg
   170  	MOVW	8(R13), R0
   171  	MOVW	12(R13), R1
   172  inreg:
   173  	MOVW    R1, R2  // usec
   174  	MOVW	R0, sec_lo+0(FP)
   175  	MOVW	$0, R1
   176  	MOVW	R1, sec_hi+4(FP)
   177  	MOVW	$1000, R3
   178  	MUL	R3, R2
   179  	MOVW	R2, nsec+8(FP)
   180  	RET
   181  
   182  TEXT runtime·nanotime(SB),NOSPLIT,$32
   183  	MOVW	$8(R13), R0  // timeval
   184  	MOVW	$0, R1  // zone
   185  	MOVW	$0, R2	// see issue 16570
   186  	MOVW	$SYS_gettimeofday, R12
   187  	SWI	$0x80 // Note: R0 is tv_sec, R1 is tv_usec
   188  	CMP	$0, R0
   189  	BNE	inreg
   190  	MOVW	8(R13), R0
   191  	MOVW	12(R13), R1
   192  inreg:
   193  	MOVW    R1, R2
   194  	MOVW	$1000000000, R3
   195  	MULLU	R0, R3, (R1, R0)
   196  	MOVW	$1000, R3
   197  	MOVW	$0, R4
   198  	MUL	R3, R2
   199  	ADD.S	R2, R0
   200  	ADC	R4, R1
   201  
   202  	MOVW	R0, ret_lo+0(FP)
   203  	MOVW	R1, ret_hi+4(FP)
   204  	RET
   205  
   206  TEXT runtime·sigfwd(SB),NOSPLIT,$0-16
   207  	MOVW	sig+4(FP), R0
   208  	MOVW	info+8(FP), R1
   209  	MOVW	ctx+12(FP), R2
   210  	MOVW	fn+0(FP), R11
   211  	MOVW	R13, R4
   212  	SUB	$24, R13
   213  	BIC	$0x7, R13 // alignment for ELF ABI
   214  	BL	(R11)
   215  	MOVW	R4, R13
   216  	RET
   217  
   218  // Sigtramp's job is to call the actual signal handler.
   219  // It is called with the following arguments on the stack:
   220  //	 LR  	"return address" - ignored
   221  //	 R0  	actual handler
   222  //	 R1  	siginfo style - ignored
   223  //	 R2   	signal number
   224  //	 R3   	siginfo
   225  //	 -4(FP)	context, beware that 0(FP) is the saved LR
   226  TEXT runtime·sigtramp(SB),NOSPLIT,$0
   227  	// this might be called in external code context,
   228  	// where g is not set.
   229  	// first save R0, because runtime·load_g will clobber it
   230  	MOVM.DB.W [R0], (R13)
   231  	MOVB	runtime·iscgo(SB), R0
   232  	CMP 	$0, R0
   233  	BL.NE	runtime·load_g(SB)
   234  
   235  	CMP 	$0, g
   236  	BNE 	cont
   237  	// fake function call stack frame for badsignal
   238  	// we only need to pass R2 (signal number), but
   239  	// badsignal will expect R2 at 4(R13), so we also
   240  	// push R1 onto stack. turns out we do need R1
   241  	// to do sigreturn.
   242  	MOVM.DB.W [R1,R2], (R13)
   243  	MOVW  	$runtime·badsignal(SB), R11
   244  	BL	(R11)
   245  	MOVM.IA.W [R1], (R13) // saved infostype
   246  	ADD		$(4+4), R13 // +4: also need to remove the pushed R0.
   247  	MOVW    ucontext-4(FP), R0 // load ucontext
   248  	B	ret
   249  
   250  cont:
   251  	// Restore R0
   252  	MOVM.IA.W (R13), [R0]
   253  
   254  	// NOTE: some Darwin/ARM kernels always use the main stack to run the
   255  	// signal handler. We need to switch to gsignal ourselves.
   256  	MOVW	g_m(g), R11
   257  	MOVW	m_gsignal(R11), R5
   258  	MOVW	(g_stack+stack_hi)(R5), R6
   259  	SUB		$28, R6
   260  
   261  	// copy arguments for call to sighandler
   262  	MOVW	R2, 4(R6) // signal num
   263  	MOVW	R3, 8(R6) // signal info
   264  	MOVW	g, 16(R6) // old_g
   265  	MOVW	context-4(FP), R4
   266  	MOVW	R4, 12(R6) // context
   267  
   268  	// Backup ucontext and infostyle
   269  	MOVW    R4, 20(R6)
   270  	MOVW    R1, 24(R6)
   271  
   272  	// switch stack and g
   273  	MOVW	R6, R13 // sigtramp is not re-entrant, so no need to back up R13.
   274  	MOVW	R5, g
   275  
   276  	BL	(R0)
   277  
   278  	// call sigreturn
   279  	MOVW	20(R13), R0	// saved ucontext
   280  	MOVW	24(R13), R1	// saved infostyle
   281  ret:
   282  	MOVW	$SYS_sigreturn, R12 // sigreturn(ucontext, infostyle)
   283  	SWI	$0x80
   284  
   285  	// if sigreturn fails, we can do nothing but exit
   286  	B	runtime·exit(SB)
   287  
   288  TEXT runtime·sigprocmask(SB),NOSPLIT,$0
   289  	MOVW	how+0(FP), R0
   290  	MOVW	new+4(FP), R1
   291  	MOVW	old+8(FP), R2
   292  	MOVW	$SYS_pthread_sigmask, R12
   293  	SWI	$0x80
   294  	BL.CS	notok<>(SB)
   295  	RET
   296  
   297  TEXT runtime·sigaction(SB),NOSPLIT,$0
   298  	MOVW	mode+0(FP), R0
   299  	MOVW	new+4(FP), R1
   300  	MOVW	old+8(FP), R2
   301  	MOVW	$SYS_sigaction, R12
   302  	SWI	$0x80
   303  	RET
   304  
   305  TEXT runtime·usleep(SB),NOSPLIT,$12
   306  	MOVW	usec+0(FP), R0
   307  	CALL	runtime·usplitR0(SB)
   308  	MOVW	R0, a-12(SP)
   309  	MOVW	R1, b-8(SP)
   310  
   311  	// select(0, 0, 0, 0, &tv)
   312  	MOVW	$0, R0
   313  	MOVW	$0, R1
   314  	MOVW	$0, R2
   315  	MOVW	$0, R3
   316  	MOVW	$a-12(SP), R4
   317  	MOVW	$SYS_select, R12
   318  	SWI	$0x80
   319  	RET
   320  
   321  TEXT ·publicationBarrier(SB),NOSPLIT,$-4-0
   322  	B	runtime·armPublicationBarrier(SB)
   323  
   324  TEXT runtime·sysctl(SB),NOSPLIT,$0
   325  	MOVW	mib+0(FP), R0
   326  	MOVW	miblen+4(FP), R1
   327  	MOVW	out+8(FP), R2
   328  	MOVW	size+12(FP), R3
   329  	MOVW	dst+16(FP), R4
   330  	MOVW	ndst+20(FP), R5
   331  	MOVW	$SYS___sysctl, R12 // syscall entry
   332  	SWI	$0x80
   333  	BCC     sysctl_ret
   334  	RSB     $0, R0, R0
   335  	MOVW	R0, ret+24(FP)
   336  	RET
   337  sysctl_ret:
   338  	MOVW	$0, R0
   339  	MOVW	R0, ret+24(FP)
   340  	RET
   341  
   342  // Thread related functions
   343  // func bsdthread_create(stk, arg unsafe.Pointer, fn uintptr) int32
   344  TEXT runtime·bsdthread_create(SB),NOSPLIT,$0
   345  	// Set up arguments to bsdthread_create system call.
   346  	// The ones in quotes pass through to the thread callback
   347  	// uninterpreted, so we can put whatever we want there.
   348  	MOVW    fn+8(FP),    R0 // "func"
   349  	MOVW    arg+4(FP),   R1 // "arg"
   350  	MOVW    stk+0(FP),   R2 // stack
   351  	MOVW	$0x01000000, R4	// flags = PTHREAD_START_CUSTOM
   352  	MOVW	$0,          R5 // paranoia
   353  	MOVW	$SYS_bsdthread_create, R12
   354  	SWI	$0x80
   355  	BCC		create_ret
   356  	RSB 	$0, R0, R0
   357  	MOVW	R0, ret+12(FP)
   358  	RET
   359  create_ret:
   360  	MOVW	$0, R0
   361  	MOVW	R0, ret+12(FP)
   362  	RET
   363  
   364  // The thread that bsdthread_create creates starts executing here,
   365  // because we registered this function using bsdthread_register
   366  // at startup.
   367  //	R0 = "pthread"
   368  //	R1 = mach thread port
   369  //	R2 = "func" (= fn)
   370  //	R3 = "arg" (= m)
   371  //	R4 = stack
   372  //	R5 = flags (= 0)
   373  // XXX: how to deal with R4/SP? ref: Libc-594.9.1/arm/pthreads/thread_start.s
   374  TEXT runtime·bsdthread_start(SB),NOSPLIT,$0
   375  	MOVW    R1, m_procid(R3) // thread port is m->procid
   376  	MOVW	m_g0(R3), g
   377  	MOVW	R3, g_m(g)
   378  	// ARM don't have runtime·stackcheck(SB)
   379  	// disable runfast mode of vfp
   380  	EOR     R12, R12
   381  	WORD    $0xeee1ca10 // fmxr	fpscr, ip
   382  	BL      (R2) // fn
   383  	BL      runtime·exit1(SB)
   384  	RET
   385  
   386  // int32 bsdthread_register(void)
   387  // registers callbacks for threadstart (see bsdthread_create above
   388  // and wqthread and pthsize (not used).  returns 0 on success.
   389  TEXT runtime·bsdthread_register(SB),NOSPLIT,$0
   390  	MOVW	$runtime·bsdthread_start(SB), R0	// threadstart
   391  	MOVW	$0, R1	// wqthread, not used by us
   392  	MOVW	$0, R2	// pthsize, not used by us
   393  	MOVW	$0, R3 	// dummy_value [sic]
   394  	MOVW	$0, R4	// targetconc_ptr
   395  	MOVW	$0, R5	// dispatchqueue_offset
   396  	MOVW	$SYS_bsdthread_register, R12	// bsdthread_register
   397  	SWI	$0x80
   398  	MOVW	R0, ret+0(FP)
   399  	RET
   400  
   401  // uint32 mach_msg_trap(void*, uint32, uint32, uint32, uint32, uint32, uint32)
   402  TEXT runtime·mach_msg_trap(SB),NOSPLIT,$0
   403  	MOVW    h+0(FP), R0
   404  	MOVW    op+4(FP), R1
   405  	MOVW    send_size+8(FP), R2
   406  	MOVW    rcv_size+12(FP), R3
   407  	MOVW    rcv_name+16(FP), R4
   408  	MOVW    timeout+20(FP), R5
   409  	MOVW    notify+24(FP), R6
   410  	MVN     $30, R12
   411  	SWI	$0x80
   412  	MOVW	R0, ret+28(FP)
   413  	RET
   414  
   415  TEXT runtime·mach_task_self(SB),NOSPLIT,$0
   416  	MVN     $27, R12 // task_self_trap
   417  	SWI	$0x80
   418  	MOVW	R0, ret+0(FP)
   419  	RET
   420  
   421  TEXT runtime·mach_thread_self(SB),NOSPLIT,$0
   422  	MVN 	$26, R12 // thread_self_trap
   423  	SWI	$0x80
   424  	MOVW	R0, ret+0(FP)
   425  	RET
   426  
   427  TEXT runtime·mach_reply_port(SB),NOSPLIT,$0
   428  	MVN 	$25, R12	// mach_reply_port
   429  	SWI	$0x80
   430  	MOVW	R0, ret+0(FP)
   431  	RET
   432  
   433  // Mach provides trap versions of the semaphore ops,
   434  // instead of requiring the use of RPC.
   435  
   436  // uint32 mach_semaphore_wait(uint32)
   437  TEXT runtime·mach_semaphore_wait(SB),NOSPLIT,$0
   438  	MOVW	sema+0(FP), R0
   439  	MVN 	$35, R12	// semaphore_wait_trap
   440  	SWI	$0x80
   441  	MOVW	R0, ret+4(FP)
   442  	RET
   443  
   444  // uint32 mach_semaphore_timedwait(uint32, uint32, uint32)
   445  TEXT runtime·mach_semaphore_timedwait(SB),NOSPLIT,$0
   446  	MOVW	sema+0(FP), R0
   447  	MOVW	sec+4(FP), R1
   448  	MOVW	nsec+8(FP), R2
   449  	MVN 	$37, R12	// semaphore_timedwait_trap
   450  	SWI	$0x80
   451  	MOVW	R0, ret+12(FP)
   452  	RET
   453  
   454  // uint32 mach_semaphore_signal(uint32)
   455  TEXT runtime·mach_semaphore_signal(SB),NOSPLIT,$0
   456  	MOVW    sema+0(FP), R0
   457  	MVN 	$32, R12	// semaphore_signal_trap
   458  	SWI	$0x80
   459  	MOVW	R0, ret+4(FP)
   460  	RET
   461  
   462  // uint32 mach_semaphore_signal_all(uint32)
   463  TEXT runtime·mach_semaphore_signal_all(SB),NOSPLIT,$0
   464  	MOVW	sema+0(FP), R0
   465  	MVN 	$33, R12	// semaphore_signal_all_trap
   466  	SWI	$0x80
   467  	MOVW	R0, ret+4(FP)
   468  	RET
   469  
   470  // int32 runtime·kqueue(void)
   471  TEXT runtime·kqueue(SB),NOSPLIT,$0
   472  	MOVW	$SYS_kqueue, R12
   473  	SWI	$0x80
   474  	RSB.CS	$0, R0, R0
   475  	MOVW	R0, ret+0(FP)
   476  	RET
   477  
   478  // int32 runtime·kevent(int kq, Kevent *changelist, int nchanges, Kevent *eventlist, int events, Timespec *timeout)
   479  TEXT runtime·kevent(SB),NOSPLIT,$0
   480  	MOVW	$SYS_kevent, R12
   481  	MOVW	kq+0(FP), R0
   482  	MOVW	ch+4(FP), R1
   483  	MOVW	nch+8(FP), R2
   484  	MOVW	ev+12(FP), R3
   485  	MOVW	nev+16(FP), R4
   486  	MOVW	ts+20(FP), R5
   487  	SWI	$0x80
   488  	RSB.CS	$0, R0, R0
   489  	MOVW	R0, ret+24(FP)
   490  	RET
   491  
   492  // int32 runtime·closeonexec(int32 fd)
   493  TEXT runtime·closeonexec(SB),NOSPLIT,$0
   494  	MOVW	$SYS_fcntl, R12
   495  	MOVW	fd+0(FP), R0
   496  	MOVW	$2, R1	// F_SETFD
   497  	MOVW	$1, R2	// FD_CLOEXEC
   498  	SWI	$0x80
   499  	RET
   500  
   501  // sigaltstack on some darwin/arm version is buggy and will always
   502  // run the signal handler on the main stack, so our sigtramp has
   503  // to do the stack switch ourselves.
   504  TEXT runtime·sigaltstack(SB),NOSPLIT,$0
   505  	RET