github.com/aloncn/graphics-go@v0.0.1/src/runtime/sys_linux_amd64.s (about)

     1  // Copyright 2009 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  //
     6  // System calls and other sys.stuff for AMD64, Linux
     7  //
     8  
     9  #include "go_asm.h"
    10  #include "go_tls.h"
    11  #include "textflag.h"
    12  
    13  TEXT runtime·exit(SB),NOSPLIT,$0-4
    14  	MOVL	code+0(FP), DI
    15  	MOVL	$231, AX	// exitgroup - force all os threads to exit
    16  	SYSCALL
    17  	RET
    18  
    19  TEXT runtime·exit1(SB),NOSPLIT,$0-4
    20  	MOVL	code+0(FP), DI
    21  	MOVL	$60, AX	// exit - exit the current os thread
    22  	SYSCALL
    23  	RET
    24  
    25  TEXT runtime·open(SB),NOSPLIT,$0-20
    26  	MOVQ	name+0(FP), DI
    27  	MOVL	mode+8(FP), SI
    28  	MOVL	perm+12(FP), DX
    29  	MOVL	$2, AX			// syscall entry
    30  	SYSCALL
    31  	CMPQ	AX, $0xfffffffffffff001
    32  	JLS	2(PC)
    33  	MOVL	$-1, AX
    34  	MOVL	AX, ret+16(FP)
    35  	RET
    36  
    37  TEXT runtime·closefd(SB),NOSPLIT,$0-12
    38  	MOVL	fd+0(FP), DI
    39  	MOVL	$3, AX			// syscall entry
    40  	SYSCALL
    41  	CMPQ	AX, $0xfffffffffffff001
    42  	JLS	2(PC)
    43  	MOVL	$-1, AX
    44  	MOVL	AX, ret+8(FP)
    45  	RET
    46  
    47  TEXT runtime·write(SB),NOSPLIT,$0-28
    48  	MOVQ	fd+0(FP), DI
    49  	MOVQ	p+8(FP), SI
    50  	MOVL	n+16(FP), DX
    51  	MOVL	$1, AX			// syscall entry
    52  	SYSCALL
    53  	CMPQ	AX, $0xfffffffffffff001
    54  	JLS	2(PC)
    55  	MOVL	$-1, AX
    56  	MOVL	AX, ret+24(FP)
    57  	RET
    58  
    59  TEXT runtime·read(SB),NOSPLIT,$0-28
    60  	MOVL	fd+0(FP), DI
    61  	MOVQ	p+8(FP), SI
    62  	MOVL	n+16(FP), DX
    63  	MOVL	$0, AX			// syscall entry
    64  	SYSCALL
    65  	CMPQ	AX, $0xfffffffffffff001
    66  	JLS	2(PC)
    67  	MOVL	$-1, AX
    68  	MOVL	AX, ret+24(FP)
    69  	RET
    70  
    71  TEXT runtime·getrlimit(SB),NOSPLIT,$0-20
    72  	MOVL	kind+0(FP), DI
    73  	MOVQ	limit+8(FP), SI
    74  	MOVL	$97, AX			// syscall entry
    75  	SYSCALL
    76  	MOVL	AX, ret+16(FP)
    77  	RET
    78  
    79  TEXT runtime·usleep(SB),NOSPLIT,$16
    80  	MOVL	$0, DX
    81  	MOVL	usec+0(FP), AX
    82  	MOVL	$1000000, CX
    83  	DIVL	CX
    84  	MOVQ	AX, 0(SP)
    85  	MOVQ	DX, 8(SP)
    86  
    87  	// select(0, 0, 0, 0, &tv)
    88  	MOVL	$0, DI
    89  	MOVL	$0, SI
    90  	MOVL	$0, DX
    91  	MOVL	$0, R10
    92  	MOVQ	SP, R8
    93  	MOVL	$23, AX
    94  	SYSCALL
    95  	RET
    96  
    97  TEXT runtime·gettid(SB),NOSPLIT,$0-4
    98  	MOVL	$186, AX	// syscall - gettid
    99  	SYSCALL
   100  	MOVL	AX, ret+0(FP)
   101  	RET
   102  
   103  TEXT runtime·raise(SB),NOSPLIT,$0
   104  	MOVL	$186, AX	// syscall - gettid
   105  	SYSCALL
   106  	MOVL	AX, DI	// arg 1 tid
   107  	MOVL	sig+0(FP), SI	// arg 2
   108  	MOVL	$200, AX	// syscall - tkill
   109  	SYSCALL
   110  	RET
   111  
   112  TEXT runtime·raiseproc(SB),NOSPLIT,$0
   113  	MOVL	$39, AX	// syscall - getpid
   114  	SYSCALL
   115  	MOVL	AX, DI	// arg 1 pid
   116  	MOVL	sig+0(FP), SI	// arg 2
   117  	MOVL	$62, AX	// syscall - kill
   118  	SYSCALL
   119  	RET
   120  
   121  TEXT runtime·setitimer(SB),NOSPLIT,$0-24
   122  	MOVL	mode+0(FP), DI
   123  	MOVQ	new+8(FP), SI
   124  	MOVQ	old+16(FP), DX
   125  	MOVL	$38, AX			// syscall entry
   126  	SYSCALL
   127  	RET
   128  
   129  TEXT runtime·mincore(SB),NOSPLIT,$0-28
   130  	MOVQ	addr+0(FP), DI
   131  	MOVQ	n+8(FP), SI
   132  	MOVQ	dst+16(FP), DX
   133  	MOVL	$27, AX			// syscall entry
   134  	SYSCALL
   135  	MOVL	AX, ret+24(FP)
   136  	RET
   137  
   138  // func now() (sec int64, nsec int32)
   139  TEXT time·now(SB),NOSPLIT,$16
   140  	// Be careful. We're calling a function with gcc calling convention here.
   141  	// We're guaranteed 128 bytes on entry, and we've taken 16, and the
   142  	// call uses another 8.
   143  	// That leaves 104 for the gettime code to use. Hope that's enough!
   144  	MOVQ	runtime·__vdso_clock_gettime_sym(SB), AX
   145  	CMPQ	AX, $0
   146  	JEQ	fallback
   147  	MOVL	$0, DI // CLOCK_REALTIME
   148  	LEAQ	0(SP), SI
   149  	CALL	AX
   150  	MOVQ	0(SP), AX	// sec
   151  	MOVQ	8(SP), DX	// nsec
   152  	MOVQ	AX, sec+0(FP)
   153  	MOVL	DX, nsec+8(FP)
   154  	RET
   155  fallback:
   156  	LEAQ	0(SP), DI
   157  	MOVQ	$0, SI
   158  	MOVQ	runtime·__vdso_gettimeofday_sym(SB), AX
   159  	CALL	AX
   160  	MOVQ	0(SP), AX	// sec
   161  	MOVL	8(SP), DX	// usec
   162  	IMULQ	$1000, DX
   163  	MOVQ	AX, sec+0(FP)
   164  	MOVL	DX, nsec+8(FP)
   165  	RET
   166  
   167  TEXT runtime·nanotime(SB),NOSPLIT,$16
   168  	// Duplicate time.now here to avoid using up precious stack space.
   169  	// See comment above in time.now.
   170  	MOVQ	runtime·__vdso_clock_gettime_sym(SB), AX
   171  	CMPQ	AX, $0
   172  	JEQ	fallback
   173  	MOVL	$1, DI // CLOCK_MONOTONIC
   174  	LEAQ	0(SP), SI
   175  	CALL	AX
   176  	MOVQ	0(SP), AX	// sec
   177  	MOVQ	8(SP), DX	// nsec
   178  	// sec is in AX, nsec in DX
   179  	// return nsec in AX
   180  	IMULQ	$1000000000, AX
   181  	ADDQ	DX, AX
   182  	MOVQ	AX, ret+0(FP)
   183  	RET
   184  fallback:
   185  	LEAQ	0(SP), DI
   186  	MOVQ	$0, SI
   187  	MOVQ	runtime·__vdso_gettimeofday_sym(SB), AX
   188  	CALL	AX
   189  	MOVQ	0(SP), AX	// sec
   190  	MOVL	8(SP), DX	// usec
   191  	IMULQ	$1000, DX
   192  	// sec is in AX, nsec in DX
   193  	// return nsec in AX
   194  	IMULQ	$1000000000, AX
   195  	ADDQ	DX, AX
   196  	MOVQ	AX, ret+0(FP)
   197  	RET
   198  
   199  TEXT runtime·rtsigprocmask(SB),NOSPLIT,$0-28
   200  	MOVL	sig+0(FP), DI
   201  	MOVQ	new+8(FP), SI
   202  	MOVQ	old+16(FP), DX
   203  	MOVL	size+24(FP), R10
   204  	MOVL	$14, AX			// syscall entry
   205  	SYSCALL
   206  	CMPQ	AX, $0xfffffffffffff001
   207  	JLS	2(PC)
   208  	MOVL	$0xf1, 0xf1  // crash
   209  	RET
   210  
   211  TEXT runtime·rt_sigaction(SB),NOSPLIT,$0-36
   212  	MOVQ	sig+0(FP), DI
   213  	MOVQ	new+8(FP), SI
   214  	MOVQ	old+16(FP), DX
   215  	MOVQ	size+24(FP), R10
   216  	MOVL	$13, AX			// syscall entry
   217  	SYSCALL
   218  	MOVL	AX, ret+32(FP)
   219  	RET
   220  
   221  TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
   222  	MOVL	sig+8(FP), DI
   223  	MOVQ	info+16(FP), SI
   224  	MOVQ	ctx+24(FP), DX
   225  	MOVQ	fn+0(FP), AX
   226  	CALL	AX
   227  	RET
   228  
   229  TEXT runtime·sigtramp(SB),NOSPLIT,$24
   230  	MOVQ	DI, 0(SP)   // signum
   231  	MOVQ	SI, 8(SP)   // info
   232  	MOVQ	DX, 16(SP)  // ctx
   233  	MOVQ	$runtime·sigtrampgo(SB), AX
   234  	CALL AX
   235  	RET
   236  
   237  TEXT runtime·sigreturn(SB),NOSPLIT,$0
   238  	MOVL	$15, AX	// rt_sigreturn
   239  	SYSCALL
   240  	INT $3	// not reached
   241  
   242  TEXT runtime·sysMmap(SB),NOSPLIT,$0
   243  	MOVQ	addr+0(FP), DI
   244  	MOVQ	n+8(FP), SI
   245  	MOVL	prot+16(FP), DX
   246  	MOVL	flags+20(FP), R10
   247  	MOVL	fd+24(FP), R8
   248  	MOVL	off+28(FP), R9
   249  
   250  	MOVL	$9, AX			// mmap
   251  	SYSCALL
   252  	CMPQ	AX, $0xfffffffffffff001
   253  	JLS	3(PC)
   254  	NOTQ	AX
   255  	INCQ	AX
   256  	MOVQ	AX, ret+32(FP)
   257  	RET
   258  
   259  // Call the function stored in _cgo_mmap using the GCC calling convention.
   260  // This must be called on the system stack.
   261  TEXT runtime·callCgoMmap(SB),NOSPLIT,$16
   262  	MOVQ	addr+0(FP), DI
   263  	MOVQ	n+8(FP), SI
   264  	MOVL	prot+16(FP), DX
   265  	MOVL	flags+20(FP), CX
   266  	MOVL	fd+24(FP), R8
   267  	MOVL	off+28(FP), R9
   268  	MOVQ	_cgo_mmap(SB), AX
   269  	MOVQ	SP, BX
   270  	ANDQ	$~15, SP	// alignment as per amd64 psABI
   271  	MOVQ	BX, 0(SP)
   272  	CALL	AX
   273  	MOVQ	0(SP), SP
   274  	MOVQ	AX, ret+32(FP)
   275  	RET
   276  
   277  TEXT runtime·munmap(SB),NOSPLIT,$0
   278  	MOVQ	addr+0(FP), DI
   279  	MOVQ	n+8(FP), SI
   280  	MOVQ	$11, AX	// munmap
   281  	SYSCALL
   282  	CMPQ	AX, $0xfffffffffffff001
   283  	JLS	2(PC)
   284  	MOVL	$0xf1, 0xf1  // crash
   285  	RET
   286  
   287  TEXT runtime·madvise(SB),NOSPLIT,$0
   288  	MOVQ	addr+0(FP), DI
   289  	MOVQ	n+8(FP), SI
   290  	MOVL	flags+16(FP), DX
   291  	MOVQ	$28, AX	// madvise
   292  	SYSCALL
   293  	// ignore failure - maybe pages are locked
   294  	RET
   295  
   296  // int64 futex(int32 *uaddr, int32 op, int32 val,
   297  //	struct timespec *timeout, int32 *uaddr2, int32 val2);
   298  TEXT runtime·futex(SB),NOSPLIT,$0
   299  	MOVQ	addr+0(FP), DI
   300  	MOVL	op+8(FP), SI
   301  	MOVL	val+12(FP), DX
   302  	MOVQ	ts+16(FP), R10
   303  	MOVQ	addr2+24(FP), R8
   304  	MOVL	val3+32(FP), R9
   305  	MOVL	$202, AX
   306  	SYSCALL
   307  	MOVL	AX, ret+40(FP)
   308  	RET
   309  
   310  // int32 clone(int32 flags, void *stack, M *mp, G *gp, void (*fn)(void));
   311  TEXT runtime·clone(SB),NOSPLIT,$0
   312  	MOVL	flags+0(FP), DI
   313  	MOVQ	stack+8(FP), SI
   314  	MOVQ	$0, DX
   315  	MOVQ	$0, R10
   316  
   317  	// Copy mp, gp, fn off parent stack for use by child.
   318  	// Careful: Linux system call clobbers CX and R11.
   319  	MOVQ	mp+16(FP), R8
   320  	MOVQ	gp+24(FP), R9
   321  	MOVQ	fn+32(FP), R12
   322  
   323  	MOVL	$56, AX
   324  	SYSCALL
   325  
   326  	// In parent, return.
   327  	CMPQ	AX, $0
   328  	JEQ	3(PC)
   329  	MOVL	AX, ret+40(FP)
   330  	RET
   331  
   332  	// In child, on new stack.
   333  	MOVQ	SI, SP
   334  
   335  	// If g or m are nil, skip Go-related setup.
   336  	CMPQ	R8, $0    // m
   337  	JEQ	nog
   338  	CMPQ	R9, $0    // g
   339  	JEQ	nog
   340  
   341  	// Initialize m->procid to Linux tid
   342  	MOVL	$186, AX	// gettid
   343  	SYSCALL
   344  	MOVQ	AX, m_procid(R8)
   345  
   346  	// Set FS to point at m->tls.
   347  	LEAQ	m_tls(R8), DI
   348  	CALL	runtime·settls(SB)
   349  
   350  	// In child, set up new stack
   351  	get_tls(CX)
   352  	MOVQ	R8, g_m(R9)
   353  	MOVQ	R9, g(CX)
   354  	CALL	runtime·stackcheck(SB)
   355  
   356  nog:
   357  	// Call fn
   358  	CALL	R12
   359  
   360  	// It shouldn't return.  If it does, exit that thread.
   361  	MOVL	$111, DI
   362  	MOVL	$60, AX
   363  	SYSCALL
   364  	JMP	-3(PC)	// keep exiting
   365  
   366  TEXT runtime·sigaltstack(SB),NOSPLIT,$-8
   367  	MOVQ	new+8(SP), DI
   368  	MOVQ	old+16(SP), SI
   369  	MOVQ	$131, AX
   370  	SYSCALL
   371  	CMPQ	AX, $0xfffffffffffff001
   372  	JLS	2(PC)
   373  	MOVL	$0xf1, 0xf1  // crash
   374  	RET
   375  
   376  // set tls base to DI
   377  TEXT runtime·settls(SB),NOSPLIT,$32
   378  #ifdef GOOS_android
   379  	// Same as in sys_darwin_386.s:/ugliness, different constant.
   380  	// DI currently holds m->tls, which must be fs:0x1d0.
   381  	// See cgo/gcc_android_amd64.c for the derivation of the constant.
   382  	SUBQ	$0x1d0, DI  // In android, the tls base 
   383  #else
   384  	ADDQ	$8, DI	// ELF wants to use -8(FS)
   385  #endif
   386  	MOVQ	DI, SI
   387  	MOVQ	$0x1002, DI	// ARCH_SET_FS
   388  	MOVQ	$158, AX	// arch_prctl
   389  	SYSCALL
   390  	CMPQ	AX, $0xfffffffffffff001
   391  	JLS	2(PC)
   392  	MOVL	$0xf1, 0xf1  // crash
   393  	RET
   394  
   395  TEXT runtime·osyield(SB),NOSPLIT,$0
   396  	MOVL	$24, AX
   397  	SYSCALL
   398  	RET
   399  
   400  TEXT runtime·sched_getaffinity(SB),NOSPLIT,$0
   401  	MOVQ	pid+0(FP), DI
   402  	MOVQ	len+8(FP), SI
   403  	MOVQ	buf+16(FP), DX
   404  	MOVL	$204, AX			// syscall entry
   405  	SYSCALL
   406  	MOVL	AX, ret+24(FP)
   407  	RET
   408  
   409  // int32 runtime·epollcreate(int32 size);
   410  TEXT runtime·epollcreate(SB),NOSPLIT,$0
   411  	MOVL    size+0(FP), DI
   412  	MOVL    $213, AX                        // syscall entry
   413  	SYSCALL
   414  	MOVL	AX, ret+8(FP)
   415  	RET
   416  
   417  // int32 runtime·epollcreate1(int32 flags);
   418  TEXT runtime·epollcreate1(SB),NOSPLIT,$0
   419  	MOVL	flags+0(FP), DI
   420  	MOVL	$291, AX			// syscall entry
   421  	SYSCALL
   422  	MOVL	AX, ret+8(FP)
   423  	RET
   424  
   425  // func epollctl(epfd, op, fd int32, ev *epollEvent) int
   426  TEXT runtime·epollctl(SB),NOSPLIT,$0
   427  	MOVL	epfd+0(FP), DI
   428  	MOVL	op+4(FP), SI
   429  	MOVL	fd+8(FP), DX
   430  	MOVQ	ev+16(FP), R10
   431  	MOVL	$233, AX			// syscall entry
   432  	SYSCALL
   433  	MOVL	AX, ret+24(FP)
   434  	RET
   435  
   436  // int32 runtime·epollwait(int32 epfd, EpollEvent *ev, int32 nev, int32 timeout);
   437  TEXT runtime·epollwait(SB),NOSPLIT,$0
   438  	MOVL	epfd+0(FP), DI
   439  	MOVQ	ev+8(FP), SI
   440  	MOVL	nev+16(FP), DX
   441  	MOVL	timeout+20(FP), R10
   442  	MOVL	$232, AX			// syscall entry
   443  	SYSCALL
   444  	MOVL	AX, ret+24(FP)
   445  	RET
   446  
   447  // void runtime·closeonexec(int32 fd);
   448  TEXT runtime·closeonexec(SB),NOSPLIT,$0
   449  	MOVL    fd+0(FP), DI  // fd
   450  	MOVQ    $2, SI  // F_SETFD
   451  	MOVQ    $1, DX  // FD_CLOEXEC
   452  	MOVL	$72, AX  // fcntl
   453  	SYSCALL
   454  	RET
   455  
   456  
   457  // int access(const char *name, int mode)
   458  TEXT runtime·access(SB),NOSPLIT,$0
   459  	MOVQ	name+0(FP), DI
   460  	MOVL	mode+8(FP), SI
   461  	MOVL	$21, AX  // syscall entry
   462  	SYSCALL
   463  	MOVL	AX, ret+16(FP)
   464  	RET
   465  
   466  // int connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
   467  TEXT runtime·connect(SB),NOSPLIT,$0-28
   468  	MOVL	fd+0(FP), DI
   469  	MOVQ	addr+8(FP), SI
   470  	MOVL	addrlen+16(FP), DX
   471  	MOVL	$42, AX  // syscall entry
   472  	SYSCALL
   473  	MOVL	AX, ret+24(FP)
   474  	RET
   475  
   476  // int socket(int domain, int type, int protocol)
   477  TEXT runtime·socket(SB),NOSPLIT,$0-20
   478  	MOVL	domain+0(FP), DI
   479  	MOVL	type+4(FP), SI
   480  	MOVL	protocol+8(FP), DX
   481  	MOVL	$41, AX  // syscall entry
   482  	SYSCALL
   483  	MOVL	AX, ret+16(FP)
   484  	RET