...
Run Format

Text file src/runtime/sys_linux_386.s

Documentation: runtime

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

View as plain text