Text file src/runtime/sys_linux_386.s

     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 386, Linux
     7  //
     8  
     9  #include "go_asm.h"
    10  #include "go_tls.h"
    11  #include "textflag.h"
    12  
    13  // Most linux systems use glibc's dynamic linker, which puts the
    14  // __kernel_vsyscall vdso helper at 0x10(GS) for easy access from position
    15  // independent code and setldt in runtime does the same in the statically
    16  // linked case. However, systems that use alternative libc such as Android's
    17  // bionic and musl, do not save the helper anywhere, and so the only way to
    18  // invoke a syscall from position independent code is boring old int $0x80
    19  // (which is also what syscall wrappers in bionic/musl use).
    20  //
    21  // The benchmarks also showed that using int $0x80 is as fast as calling
    22  // *%gs:0x10 except on AMD Opteron. See https://golang.org/cl/19833
    23  // for the benchmark program and raw data.
    24  //#define INVOKE_SYSCALL	CALL	0x10(GS) // non-portable
    25  #define INVOKE_SYSCALL	INT	$0x80
    26  
    27  #define SYS_exit		1
    28  #define SYS_read		3
    29  #define SYS_write		4
    30  #define SYS_open		5
    31  #define SYS_close		6
    32  #define SYS_getpid		20
    33  #define SYS_access		33
    34  #define SYS_kill		37
    35  #define SYS_brk 		45
    36  #define SYS_fcntl		55
    37  #define SYS_munmap		91
    38  #define SYS_socketcall		102
    39  #define SYS_setittimer		104
    40  #define SYS_clone		120
    41  #define SYS_sched_yield 	158
    42  #define SYS_nanosleep		162
    43  #define SYS_rt_sigreturn	173
    44  #define SYS_rt_sigaction	174
    45  #define SYS_rt_sigprocmask	175
    46  #define SYS_sigaltstack 	186
    47  #define SYS_mmap2		192
    48  #define SYS_mincore		218
    49  #define SYS_madvise		219
    50  #define SYS_gettid		224
    51  #define SYS_futex		240
    52  #define SYS_sched_getaffinity	242
    53  #define SYS_set_thread_area	243
    54  #define SYS_exit_group		252
    55  #define SYS_epoll_create	254
    56  #define SYS_epoll_ctl		255
    57  #define SYS_epoll_wait		256
    58  #define SYS_timer_create	259
    59  #define SYS_timer_settime	260
    60  #define SYS_timer_delete	263
    61  #define SYS_clock_gettime	265
    62  #define SYS_tgkill		270
    63  #define SYS_epoll_create1	329
    64  #define SYS_pipe2		331
    65  
    66  TEXT runtime·exit(SB),NOSPLIT,$0
    67  	MOVL	$SYS_exit_group, AX
    68  	MOVL	code+0(FP), BX
    69  	INVOKE_SYSCALL
    70  	INT $3	// not reached
    71  	RET
    72  
    73  TEXT exit1<>(SB),NOSPLIT,$0
    74  	MOVL	$SYS_exit, AX
    75  	MOVL	code+0(FP), BX
    76  	INVOKE_SYSCALL
    77  	INT $3	// not reached
    78  	RET
    79  
    80  // func exitThread(wait *atomic.Uint32)
    81  TEXT runtime·exitThread(SB),NOSPLIT,$0-4
    82  	MOVL	wait+0(FP), AX
    83  	// We're done using the stack.
    84  	MOVL	$0, (AX)
    85  	MOVL	$1, AX	// exit (just this thread)
    86  	MOVL	$0, BX	// exit code
    87  	INT	$0x80	// no stack; must not use CALL
    88  	// We may not even have a stack any more.
    89  	INT	$3
    90  	JMP	0(PC)
    91  
    92  TEXT runtime·open(SB),NOSPLIT,$0
    93  	MOVL	$SYS_open, AX
    94  	MOVL	name+0(FP), BX
    95  	MOVL	mode+4(FP), CX
    96  	MOVL	perm+8(FP), DX
    97  	INVOKE_SYSCALL
    98  	CMPL	AX, $0xfffff001
    99  	JLS	2(PC)
   100  	MOVL	$-1, AX
   101  	MOVL	AX, ret+12(FP)
   102  	RET
   103  
   104  TEXT runtime·closefd(SB),NOSPLIT,$0
   105  	MOVL	$SYS_close, AX
   106  	MOVL	fd+0(FP), BX
   107  	INVOKE_SYSCALL
   108  	CMPL	AX, $0xfffff001
   109  	JLS	2(PC)
   110  	MOVL	$-1, AX
   111  	MOVL	AX, ret+4(FP)
   112  	RET
   113  
   114  TEXT runtime·write1(SB),NOSPLIT,$0
   115  	MOVL	$SYS_write, AX
   116  	MOVL	fd+0(FP), BX
   117  	MOVL	p+4(FP), CX
   118  	MOVL	n+8(FP), DX
   119  	INVOKE_SYSCALL
   120  	MOVL	AX, ret+12(FP)
   121  	RET
   122  
   123  TEXT runtime·read(SB),NOSPLIT,$0
   124  	MOVL	$SYS_read, AX
   125  	MOVL	fd+0(FP), BX
   126  	MOVL	p+4(FP), CX
   127  	MOVL	n+8(FP), DX
   128  	INVOKE_SYSCALL
   129  	MOVL	AX, ret+12(FP)
   130  	RET
   131  
   132  // func pipe2(flags int32) (r, w int32, errno int32)
   133  TEXT runtime·pipe2(SB),NOSPLIT,$0-16
   134  	MOVL	$SYS_pipe2, AX
   135  	LEAL	r+4(FP), BX
   136  	MOVL	flags+0(FP), CX
   137  	INVOKE_SYSCALL
   138  	MOVL	AX, errno+12(FP)
   139  	RET
   140  
   141  TEXT runtime·usleep(SB),NOSPLIT,$8
   142  	MOVL	$0, DX
   143  	MOVL	usec+0(FP), AX
   144  	MOVL	$1000000, CX
   145  	DIVL	CX
   146  	MOVL	AX, 0(SP)
   147  	MOVL	$1000, AX	// usec to nsec
   148  	MULL	DX
   149  	MOVL	AX, 4(SP)
   150  
   151  	// nanosleep(&ts, 0)
   152  	MOVL	$SYS_nanosleep, AX
   153  	LEAL	0(SP), BX
   154  	MOVL	$0, CX
   155  	INVOKE_SYSCALL
   156  	RET
   157  
   158  TEXT runtime·gettid(SB),NOSPLIT,$0-4
   159  	MOVL	$SYS_gettid, AX
   160  	INVOKE_SYSCALL
   161  	MOVL	AX, ret+0(FP)
   162  	RET
   163  
   164  TEXT runtime·raise(SB),NOSPLIT,$12
   165  	MOVL	$SYS_getpid, AX
   166  	INVOKE_SYSCALL
   167  	MOVL	AX, BX	// arg 1 pid
   168  	MOVL	$SYS_gettid, AX
   169  	INVOKE_SYSCALL
   170  	MOVL	AX, CX	// arg 2 tid
   171  	MOVL	sig+0(FP), DX	// arg 3 signal
   172  	MOVL	$SYS_tgkill, AX
   173  	INVOKE_SYSCALL
   174  	RET
   175  
   176  TEXT runtime·raiseproc(SB),NOSPLIT,$12
   177  	MOVL	$SYS_getpid, AX
   178  	INVOKE_SYSCALL
   179  	MOVL	AX, BX	// arg 1 pid
   180  	MOVL	sig+0(FP), CX	// arg 2 signal
   181  	MOVL	$SYS_kill, AX
   182  	INVOKE_SYSCALL
   183  	RET
   184  
   185  TEXT ·getpid(SB),NOSPLIT,$0-4
   186  	MOVL	$SYS_getpid, AX
   187  	INVOKE_SYSCALL
   188  	MOVL	AX, ret+0(FP)
   189  	RET
   190  
   191  TEXT ·tgkill(SB),NOSPLIT,$0
   192  	MOVL	$SYS_tgkill, AX
   193  	MOVL	tgid+0(FP), BX
   194  	MOVL	tid+4(FP), CX
   195  	MOVL	sig+8(FP), DX
   196  	INVOKE_SYSCALL
   197  	RET
   198  
   199  TEXT runtime·setitimer(SB),NOSPLIT,$0-12
   200  	MOVL	$SYS_setittimer, AX
   201  	MOVL	mode+0(FP), BX
   202  	MOVL	new+4(FP), CX
   203  	MOVL	old+8(FP), DX
   204  	INVOKE_SYSCALL
   205  	RET
   206  
   207  TEXT runtime·timer_create(SB),NOSPLIT,$0-16
   208  	MOVL	$SYS_timer_create, AX
   209  	MOVL	clockid+0(FP), BX
   210  	MOVL	sevp+4(FP), CX
   211  	MOVL	timerid+8(FP), DX
   212  	INVOKE_SYSCALL
   213  	MOVL	AX, ret+12(FP)
   214  	RET
   215  
   216  TEXT runtime·timer_settime(SB),NOSPLIT,$0-20
   217  	MOVL	$SYS_timer_settime, AX
   218  	MOVL	timerid+0(FP), BX
   219  	MOVL	flags+4(FP), CX
   220  	MOVL	new+8(FP), DX
   221  	MOVL	old+12(FP), SI
   222  	INVOKE_SYSCALL
   223  	MOVL	AX, ret+16(FP)
   224  	RET
   225  
   226  TEXT runtime·timer_delete(SB),NOSPLIT,$0-8
   227  	MOVL	$SYS_timer_delete, AX
   228  	MOVL	timerid+0(FP), BX
   229  	INVOKE_SYSCALL
   230  	MOVL	AX, ret+4(FP)
   231  	RET
   232  
   233  TEXT runtime·mincore(SB),NOSPLIT,$0-16
   234  	MOVL	$SYS_mincore, AX
   235  	MOVL	addr+0(FP), BX
   236  	MOVL	n+4(FP), CX
   237  	MOVL	dst+8(FP), DX
   238  	INVOKE_SYSCALL
   239  	MOVL	AX, ret+12(FP)
   240  	RET
   241  
   242  // func walltime() (sec int64, nsec int32)
   243  TEXT runtime·walltime(SB), NOSPLIT, $8-12
   244  	// We don't know how much stack space the VDSO code will need,
   245  	// so switch to g0.
   246  
   247  	MOVL	SP, BP	// Save old SP; BP unchanged by C code.
   248  
   249  	get_tls(CX)
   250  	MOVL	g(CX), AX
   251  	MOVL	g_m(AX), SI // SI unchanged by C code.
   252  
   253  	// Set vdsoPC and vdsoSP for SIGPROF traceback.
   254  	// Save the old values on stack and restore them on exit,
   255  	// so this function is reentrant.
   256  	MOVL	m_vdsoPC(SI), CX
   257  	MOVL	m_vdsoSP(SI), DX
   258  	MOVL	CX, 0(SP)
   259  	MOVL	DX, 4(SP)
   260  
   261  	LEAL	sec+0(FP), DX
   262  	MOVL	-4(DX), CX
   263  	MOVL	CX, m_vdsoPC(SI)
   264  	MOVL	DX, m_vdsoSP(SI)
   265  
   266  	CMPL	AX, m_curg(SI)	// Only switch if on curg.
   267  	JNE	noswitch
   268  
   269  	MOVL	m_g0(SI), DX
   270  	MOVL	(g_sched+gobuf_sp)(DX), SP	// Set SP to g0 stack
   271  
   272  noswitch:
   273  	SUBL	$16, SP		// Space for results
   274  	ANDL	$~15, SP	// Align for C code
   275  
   276  	// Stack layout, depending on call path:
   277  	//  x(SP)   vDSO            INVOKE_SYSCALL
   278  	//    12    ts.tv_nsec      ts.tv_nsec
   279  	//     8    ts.tv_sec       ts.tv_sec
   280  	//     4    &ts             -
   281  	//     0    CLOCK_<id>      -
   282  
   283  	MOVL	runtime·vdsoClockgettimeSym(SB), AX
   284  	CMPL	AX, $0
   285  	JEQ	fallback
   286  
   287  	LEAL	8(SP), BX	// &ts (struct timespec)
   288  	MOVL	BX, 4(SP)
   289  	MOVL	$0, 0(SP)	// CLOCK_REALTIME
   290  	CALL	AX
   291  	JMP finish
   292  
   293  fallback:
   294  	MOVL	$SYS_clock_gettime, AX
   295  	MOVL	$0, BX		// CLOCK_REALTIME
   296  	LEAL	8(SP), CX
   297  	INVOKE_SYSCALL
   298  
   299  finish:
   300  	MOVL	8(SP), AX	// sec
   301  	MOVL	12(SP), BX	// nsec
   302  
   303  	MOVL	BP, SP		// Restore real SP
   304  	// Restore vdsoPC, vdsoSP
   305  	// We don't worry about being signaled between the two stores.
   306  	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   307  	// and no one will care about vdsoPC. If we are in a signal handler,
   308  	// we cannot receive another signal.
   309  	MOVL	4(SP), CX
   310  	MOVL	CX, m_vdsoSP(SI)
   311  	MOVL	0(SP), CX
   312  	MOVL	CX, m_vdsoPC(SI)
   313  
   314  	// sec is in AX, nsec in BX
   315  	MOVL	AX, sec_lo+0(FP)
   316  	MOVL	$0, sec_hi+4(FP)
   317  	MOVL	BX, nsec+8(FP)
   318  	RET
   319  
   320  // int64 nanotime(void) so really
   321  // void nanotime(int64 *nsec)
   322  TEXT runtime·nanotime1(SB), NOSPLIT, $8-8
   323  	// Switch to g0 stack. See comment above in runtime·walltime.
   324  
   325  	MOVL	SP, BP	// Save old SP; BP unchanged by C code.
   326  
   327  	get_tls(CX)
   328  	MOVL	g(CX), AX
   329  	MOVL	g_m(AX), SI // SI unchanged by C code.
   330  
   331  	// Set vdsoPC and vdsoSP for SIGPROF traceback.
   332  	// Save the old values on stack and restore them on exit,
   333  	// so this function is reentrant.
   334  	MOVL	m_vdsoPC(SI), CX
   335  	MOVL	m_vdsoSP(SI), DX
   336  	MOVL	CX, 0(SP)
   337  	MOVL	DX, 4(SP)
   338  
   339  	LEAL	ret+0(FP), DX
   340  	MOVL	-4(DX), CX
   341  	MOVL	CX, m_vdsoPC(SI)
   342  	MOVL	DX, m_vdsoSP(SI)
   343  
   344  	CMPL	AX, m_curg(SI)	// Only switch if on curg.
   345  	JNE	noswitch
   346  
   347  	MOVL	m_g0(SI), DX
   348  	MOVL	(g_sched+gobuf_sp)(DX), SP	// Set SP to g0 stack
   349  
   350  noswitch:
   351  	SUBL	$16, SP		// Space for results
   352  	ANDL	$~15, SP	// Align for C code
   353  
   354  	MOVL	runtime·vdsoClockgettimeSym(SB), AX
   355  	CMPL	AX, $0
   356  	JEQ	fallback
   357  
   358  	LEAL	8(SP), BX	// &ts (struct timespec)
   359  	MOVL	BX, 4(SP)
   360  	MOVL	$1, 0(SP)	// CLOCK_MONOTONIC
   361  	CALL	AX
   362  	JMP finish
   363  
   364  fallback:
   365  	MOVL	$SYS_clock_gettime, AX
   366  	MOVL	$1, BX		// CLOCK_MONOTONIC
   367  	LEAL	8(SP), CX
   368  	INVOKE_SYSCALL
   369  
   370  finish:
   371  	MOVL	8(SP), AX	// sec
   372  	MOVL	12(SP), BX	// nsec
   373  
   374  	MOVL	BP, SP		// Restore real SP
   375  	// Restore vdsoPC, vdsoSP
   376  	// We don't worry about being signaled between the two stores.
   377  	// If we are not in a signal handler, we'll restore vdsoSP to 0,
   378  	// and no one will care about vdsoPC. If we are in a signal handler,
   379  	// we cannot receive another signal.
   380  	MOVL	4(SP), CX
   381  	MOVL	CX, m_vdsoSP(SI)
   382  	MOVL	0(SP), CX
   383  	MOVL	CX, m_vdsoPC(SI)
   384  
   385  	// sec is in AX, nsec in BX
   386  	// convert to DX:AX nsec
   387  	MOVL	$1000000000, CX
   388  	MULL	CX
   389  	ADDL	BX, AX
   390  	ADCL	$0, DX
   391  
   392  	MOVL	AX, ret_lo+0(FP)
   393  	MOVL	DX, ret_hi+4(FP)
   394  	RET
   395  
   396  TEXT runtime·rtsigprocmask(SB),NOSPLIT,$0
   397  	MOVL	$SYS_rt_sigprocmask, AX
   398  	MOVL	how+0(FP), BX
   399  	MOVL	new+4(FP), CX
   400  	MOVL	old+8(FP), DX
   401  	MOVL	size+12(FP), SI
   402  	INVOKE_SYSCALL
   403  	CMPL	AX, $0xfffff001
   404  	JLS	2(PC)
   405  	INT $3
   406  	RET
   407  
   408  TEXT runtime·rt_sigaction(SB),NOSPLIT,$0
   409  	MOVL	$SYS_rt_sigaction, AX
   410  	MOVL	sig+0(FP), BX
   411  	MOVL	new+4(FP), CX
   412  	MOVL	old+8(FP), DX
   413  	MOVL	size+12(FP), SI
   414  	INVOKE_SYSCALL
   415  	MOVL	AX, ret+16(FP)
   416  	RET
   417  
   418  TEXT runtime·sigfwd(SB),NOSPLIT,$12-16
   419  	MOVL	fn+0(FP), AX
   420  	MOVL	sig+4(FP), BX
   421  	MOVL	info+8(FP), CX
   422  	MOVL	ctx+12(FP), DX
   423  	MOVL	SP, SI
   424  	SUBL	$32, SP
   425  	ANDL	$-15, SP	// align stack: handler might be a C function
   426  	MOVL	BX, 0(SP)
   427  	MOVL	CX, 4(SP)
   428  	MOVL	DX, 8(SP)
   429  	MOVL	SI, 12(SP)	// save SI: handler might be a Go function
   430  	CALL	AX
   431  	MOVL	12(SP), AX
   432  	MOVL	AX, SP
   433  	RET
   434  
   435  // Called using C ABI.
   436  TEXT runtime·sigtramp(SB),NOSPLIT|TOPFRAME,$28
   437  	// Save callee-saved C registers, since the caller may be a C signal handler.
   438  	MOVL	BX, bx-4(SP)
   439  	MOVL	BP, bp-8(SP)
   440  	MOVL	SI, si-12(SP)
   441  	MOVL	DI, di-16(SP)
   442  	// We don't save mxcsr or the x87 control word because sigtrampgo doesn't
   443  	// modify them.
   444  
   445  	MOVL	(28+4)(SP), BX
   446  	MOVL	BX, 0(SP)
   447  	MOVL	(28+8)(SP), BX
   448  	MOVL	BX, 4(SP)
   449  	MOVL	(28+12)(SP), BX
   450  	MOVL	BX, 8(SP)
   451  	CALL	runtime·sigtrampgo(SB)
   452  
   453  	MOVL	di-16(SP), DI
   454  	MOVL	si-12(SP), SI
   455  	MOVL	bp-8(SP),  BP
   456  	MOVL	bx-4(SP),  BX
   457  	RET
   458  
   459  TEXT runtime·cgoSigtramp(SB),NOSPLIT,$0
   460  	JMP	runtime·sigtramp(SB)
   461  
   462  TEXT runtime·sigreturn(SB),NOSPLIT,$0
   463  	MOVL	$SYS_rt_sigreturn, AX
   464  	// Sigreturn expects same SP as signal handler,
   465  	// so cannot CALL 0x10(GS) here.
   466  	INT	$0x80
   467  	INT	$3	// not reached
   468  	RET
   469  
   470  TEXT runtime·mmap(SB),NOSPLIT,$0
   471  	MOVL	$SYS_mmap2, AX
   472  	MOVL	addr+0(FP), BX
   473  	MOVL	n+4(FP), CX
   474  	MOVL	prot+8(FP), DX
   475  	MOVL	flags+12(FP), SI
   476  	MOVL	fd+16(FP), DI
   477  	MOVL	off+20(FP), BP
   478  	SHRL	$12, BP
   479  	INVOKE_SYSCALL
   480  	CMPL	AX, $0xfffff001
   481  	JLS	ok
   482  	NOTL	AX
   483  	INCL	AX
   484  	MOVL	$0, p+24(FP)
   485  	MOVL	AX, err+28(FP)
   486  	RET
   487  ok:
   488  	MOVL	AX, p+24(FP)
   489  	MOVL	$0, err+28(FP)
   490  	RET
   491  
   492  TEXT runtime·munmap(SB),NOSPLIT,$0
   493  	MOVL	$SYS_munmap, AX
   494  	MOVL	addr+0(FP), BX
   495  	MOVL	n+4(FP), CX
   496  	INVOKE_SYSCALL
   497  	CMPL	AX, $0xfffff001
   498  	JLS	2(PC)
   499  	INT $3
   500  	RET
   501  
   502  TEXT runtime·madvise(SB),NOSPLIT,$0
   503  	MOVL	$SYS_madvise, AX
   504  	MOVL	addr+0(FP), BX
   505  	MOVL	n+4(FP), CX
   506  	MOVL	flags+8(FP), DX
   507  	INVOKE_SYSCALL
   508  	MOVL	AX, ret+12(FP)
   509  	RET
   510  
   511  // int32 futex(int32 *uaddr, int32 op, int32 val,
   512  //	struct timespec *timeout, int32 *uaddr2, int32 val2);
   513  TEXT runtime·futex(SB),NOSPLIT,$0
   514  	MOVL	$SYS_futex, AX
   515  	MOVL	addr+0(FP), BX
   516  	MOVL	op+4(FP), CX
   517  	MOVL	val+8(FP), DX
   518  	MOVL	ts+12(FP), SI
   519  	MOVL	addr2+16(FP), DI
   520  	MOVL	val3+20(FP), BP
   521  	INVOKE_SYSCALL
   522  	MOVL	AX, ret+24(FP)
   523  	RET
   524  
   525  // int32 clone(int32 flags, void *stack, M *mp, G *gp, void (*fn)(void));
   526  TEXT runtime·clone(SB),NOSPLIT,$0
   527  	MOVL	$SYS_clone, AX
   528  	MOVL	flags+0(FP), BX
   529  	MOVL	stk+4(FP), CX
   530  	MOVL	$0, DX	// parent tid ptr
   531  	MOVL	$0, DI	// child tid ptr
   532  
   533  	// Copy mp, gp, fn off parent stack for use by child.
   534  	SUBL	$16, CX
   535  	MOVL	mp+8(FP), SI
   536  	MOVL	SI, 0(CX)
   537  	MOVL	gp+12(FP), SI
   538  	MOVL	SI, 4(CX)
   539  	MOVL	fn+16(FP), SI
   540  	MOVL	SI, 8(CX)
   541  	MOVL	$1234, 12(CX)
   542  
   543  	// cannot use CALL 0x10(GS) here, because the stack changes during the
   544  	// system call (after CALL 0x10(GS), the child is still using the
   545  	// parent's stack when executing its RET instruction).
   546  	INT	$0x80
   547  
   548  	// In parent, return.
   549  	CMPL	AX, $0
   550  	JEQ	3(PC)
   551  	MOVL	AX, ret+20(FP)
   552  	RET
   553  
   554  	// Paranoia: check that SP is as we expect.
   555  	NOP	SP // tell vet SP changed - stop checking offsets
   556  	MOVL	12(SP), BP
   557  	CMPL	BP, $1234
   558  	JEQ	2(PC)
   559  	INT	$3
   560  
   561  	// Initialize AX to Linux tid
   562  	MOVL	$SYS_gettid, AX
   563  	INVOKE_SYSCALL
   564  
   565  	MOVL	0(SP), BX	    // m
   566  	MOVL	4(SP), DX	    // g
   567  	MOVL	8(SP), SI	    // fn
   568  
   569  	CMPL	BX, $0
   570  	JEQ	nog
   571  	CMPL	DX, $0
   572  	JEQ	nog
   573  
   574  	MOVL	AX, m_procid(BX)	// save tid as m->procid
   575  
   576  	// set up ldt 7+id to point at m->tls.
   577  	LEAL	m_tls(BX), BP
   578  	MOVL	m_id(BX), DI
   579  	ADDL	$7, DI	// m0 is LDT#7. count up.
   580  	// setldt(tls#, &tls, sizeof tls)
   581  	PUSHAL	// save registers
   582  	PUSHL	$32	// sizeof tls
   583  	PUSHL	BP	// &tls
   584  	PUSHL	DI	// tls #
   585  	CALL	runtime·setldt(SB)
   586  	POPL	AX
   587  	POPL	AX
   588  	POPL	AX
   589  	POPAL
   590  
   591  	// Now segment is established. Initialize m, g.
   592  	get_tls(AX)
   593  	MOVL	DX, g(AX)
   594  	MOVL	BX, g_m(DX)
   595  
   596  	CALL	runtime·stackcheck(SB)	// smashes AX, CX
   597  	MOVL	0(DX), DX	// paranoia; check they are not nil
   598  	MOVL	0(BX), BX
   599  
   600  	// more paranoia; check that stack splitting code works
   601  	PUSHAL
   602  	CALL	runtime·emptyfunc(SB)
   603  	POPAL
   604  
   605  nog:
   606  	CALL	SI	// fn()
   607  	CALL	exit1<>(SB)
   608  	MOVL	$0x1234, 0x1005
   609  
   610  TEXT runtime·sigaltstack(SB),NOSPLIT,$-8
   611  	MOVL	$SYS_sigaltstack, AX
   612  	MOVL	new+0(FP), BX
   613  	MOVL	old+4(FP), CX
   614  	INVOKE_SYSCALL
   615  	CMPL	AX, $0xfffff001
   616  	JLS	2(PC)
   617  	INT	$3
   618  	RET
   619  
   620  // <asm-i386/ldt.h>
   621  // struct user_desc {
   622  //	unsigned int  entry_number;
   623  //	unsigned long base_addr;
   624  //	unsigned int  limit;
   625  //	unsigned int  seg_32bit:1;
   626  //	unsigned int  contents:2;
   627  //	unsigned int  read_exec_only:1;
   628  //	unsigned int  limit_in_pages:1;
   629  //	unsigned int  seg_not_present:1;
   630  //	unsigned int  useable:1;
   631  // };
   632  #define SEG_32BIT 0x01
   633  // contents are the 2 bits 0x02 and 0x04.
   634  #define CONTENTS_DATA 0x00
   635  #define CONTENTS_STACK 0x02
   636  #define CONTENTS_CODE 0x04
   637  #define READ_EXEC_ONLY 0x08
   638  #define LIMIT_IN_PAGES 0x10
   639  #define SEG_NOT_PRESENT 0x20
   640  #define USEABLE 0x40
   641  
   642  // `-1` means the kernel will pick a TLS entry on the first setldt call,
   643  // which happens during runtime init, and that we'll store back the saved
   644  // entry and reuse that on subsequent calls when creating new threads.
   645  DATA  runtime·tls_entry_number+0(SB)/4, $-1
   646  GLOBL runtime·tls_entry_number(SB), NOPTR, $4
   647  
   648  // setldt(int entry, int address, int limit)
   649  // We use set_thread_area, which mucks with the GDT, instead of modify_ldt,
   650  // which would modify the LDT, but is disabled on some kernels.
   651  // The name, setldt, is a misnomer, although we leave this name as it is for
   652  // the compatibility with other platforms.
   653  TEXT runtime·setldt(SB),NOSPLIT,$32
   654  	MOVL	base+4(FP), DX
   655  
   656  #ifdef GOOS_android
   657  	// Android stores the TLS offset in runtime·tls_g.
   658  	SUBL	runtime·tls_g(SB), DX
   659  	MOVL	DX, 0(DX)
   660  #else
   661  	/*
   662  	 * When linking against the system libraries,
   663  	 * we use its pthread_create and let it set up %gs
   664  	 * for us.  When we do that, the private storage
   665  	 * we get is not at 0(GS), but -4(GS).
   666  	 * To insulate the rest of the tool chain from this
   667  	 * ugliness, 8l rewrites 0(TLS) into -4(GS) for us.
   668  	 * To accommodate that rewrite, we translate
   669  	 * the address here and bump the limit to 0xffffffff (no limit)
   670  	 * so that -4(GS) maps to 0(address).
   671  	 * Also, the final 0(GS) (current 4(DX)) has to point
   672  	 * to itself, to mimic ELF.
   673  	 */
   674  	ADDL	$0x4, DX	// address
   675  	MOVL	DX, 0(DX)
   676  #endif
   677  
   678  	// get entry number
   679  	MOVL	runtime·tls_entry_number(SB), CX
   680  
   681  	// set up user_desc
   682  	LEAL	16(SP), AX	// struct user_desc
   683  	MOVL	CX, 0(AX)	// unsigned int entry_number
   684  	MOVL	DX, 4(AX)	// unsigned long base_addr
   685  	MOVL	$0xfffff, 8(AX)	// unsigned int limit
   686  	MOVL	$(SEG_32BIT|LIMIT_IN_PAGES|USEABLE|CONTENTS_DATA), 12(AX)	// flag bits
   687  
   688  	// call set_thread_area
   689  	MOVL	AX, BX	// user_desc
   690  	MOVL	$SYS_set_thread_area, AX
   691  	// We can't call this via 0x10(GS) because this is called from setldt0 to set that up.
   692  	INT     $0x80
   693  
   694  	// breakpoint on error
   695  	CMPL AX, $0xfffff001
   696  	JLS 2(PC)
   697  	INT $3
   698  
   699  	// read allocated entry number back out of user_desc
   700  	LEAL	16(SP), AX	// get our user_desc back
   701  	MOVL	0(AX), AX
   702  
   703  	// store entry number if the kernel allocated it
   704  	CMPL	CX, $-1
   705  	JNE	2(PC)
   706  	MOVL	AX, runtime·tls_entry_number(SB)
   707  
   708  	// compute segment selector - (entry*8+3)
   709  	SHLL	$3, AX
   710  	ADDL	$3, AX
   711  	MOVW	AX, GS
   712  
   713  	RET
   714  
   715  TEXT runtime·osyield(SB),NOSPLIT,$0
   716  	MOVL	$SYS_sched_yield, AX
   717  	INVOKE_SYSCALL
   718  	RET
   719  
   720  TEXT runtime·sched_getaffinity(SB),NOSPLIT,$0
   721  	MOVL	$SYS_sched_getaffinity, AX
   722  	MOVL	pid+0(FP), BX
   723  	MOVL	len+4(FP), CX
   724  	MOVL	buf+8(FP), DX
   725  	INVOKE_SYSCALL
   726  	MOVL	AX, ret+12(FP)
   727  	RET
   728  
   729  // int32 runtime·epollcreate(int32 size);
   730  TEXT runtime·epollcreate(SB),NOSPLIT,$0
   731  	MOVL    $SYS_epoll_create, AX
   732  	MOVL	size+0(FP), BX
   733  	INVOKE_SYSCALL
   734  	MOVL	AX, ret+4(FP)
   735  	RET
   736  
   737  // int32 runtime·epollcreate1(int32 flags);
   738  TEXT runtime·epollcreate1(SB),NOSPLIT,$0
   739  	MOVL    $SYS_epoll_create1, AX
   740  	MOVL	flags+0(FP), BX
   741  	INVOKE_SYSCALL
   742  	MOVL	AX, ret+4(FP)
   743  	RET
   744  
   745  // func epollctl(epfd, op, fd int32, ev *epollEvent) int
   746  TEXT runtime·epollctl(SB),NOSPLIT,$0
   747  	MOVL	$SYS_epoll_ctl, AX
   748  	MOVL	epfd+0(FP), BX
   749  	MOVL	op+4(FP), CX
   750  	MOVL	fd+8(FP), DX
   751  	MOVL	ev+12(FP), SI
   752  	INVOKE_SYSCALL
   753  	MOVL	AX, ret+16(FP)
   754  	RET
   755  
   756  // int32 runtime·epollwait(int32 epfd, EpollEvent *ev, int32 nev, int32 timeout);
   757  TEXT runtime·epollwait(SB),NOSPLIT,$0
   758  	MOVL	$SYS_epoll_wait, AX
   759  	MOVL	epfd+0(FP), BX
   760  	MOVL	ev+4(FP), CX
   761  	MOVL	nev+8(FP), DX
   762  	MOVL	timeout+12(FP), SI
   763  	INVOKE_SYSCALL
   764  	MOVL	AX, ret+16(FP)
   765  	RET
   766  
   767  // void runtime·closeonexec(int32 fd);
   768  TEXT runtime·closeonexec(SB),NOSPLIT,$0
   769  	MOVL	$SYS_fcntl, AX
   770  	MOVL	fd+0(FP), BX  // fd
   771  	MOVL	$2, CX  // F_SETFD
   772  	MOVL	$1, DX  // FD_CLOEXEC
   773  	INVOKE_SYSCALL
   774  	RET
   775  
   776  // int access(const char *name, int mode)
   777  TEXT runtime·access(SB),NOSPLIT,$0
   778  	MOVL	$SYS_access, AX
   779  	MOVL	name+0(FP), BX
   780  	MOVL	mode+4(FP), CX
   781  	INVOKE_SYSCALL
   782  	MOVL	AX, ret+8(FP)
   783  	RET
   784  
   785  // int connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
   786  TEXT runtime·connect(SB),NOSPLIT,$0-16
   787  	// connect is implemented as socketcall(NR_socket, 3, *(rest of args))
   788  	// stack already should have fd, addr, addrlen.
   789  	MOVL	$SYS_socketcall, AX
   790  	MOVL	$3, BX  // connect
   791  	LEAL	fd+0(FP), CX
   792  	INVOKE_SYSCALL
   793  	MOVL	AX, ret+12(FP)
   794  	RET
   795  
   796  // int socket(int domain, int type, int protocol)
   797  TEXT runtime·socket(SB),NOSPLIT,$0-16
   798  	// socket is implemented as socketcall(NR_socket, 1, *(rest of args))
   799  	// stack already should have domain, type, protocol.
   800  	MOVL	$SYS_socketcall, AX
   801  	MOVL	$1, BX  // socket
   802  	LEAL	domain+0(FP), CX
   803  	INVOKE_SYSCALL
   804  	MOVL	AX, ret+12(FP)
   805  	RET
   806  
   807  // func sbrk0() uintptr
   808  TEXT runtime·sbrk0(SB),NOSPLIT,$0-4
   809  	// Implemented as brk(NULL).
   810  	MOVL	$SYS_brk, AX
   811  	MOVL	$0, BX  // NULL
   812  	INVOKE_SYSCALL
   813  	MOVL	AX, ret+0(FP)
   814  	RET
   815  

View as plain text