github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/runtime/sys_windows_386.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  #include "zasm_GOOS_GOARCH.h"
     6  #include "../../cmd/ld/textflag.h"
     7  
     8  // void runtime·asmstdcall(void *c);
     9  TEXT runtime·asmstdcall(SB),NOSPLIT,$0
    10  	MOVL	c+0(FP), BX
    11  
    12  	// SetLastError(0).
    13  	MOVL	$0, 0x34(FS)
    14  
    15  	// Copy args to the stack.
    16  	MOVL	SP, BP
    17  	MOVL	wincall_n(BX), CX	// words
    18  	MOVL	CX, AX
    19  	SALL	$2, AX
    20  	SUBL	AX, SP			// room for args
    21  	MOVL	SP, DI
    22  	MOVL	wincall_args(BX), SI
    23  	CLD
    24  	REP; MOVSL
    25  
    26  	// Call stdcall or cdecl function.
    27  	// DI SI BP BX are preserved, SP is not
    28  	CALL	wincall_fn(BX)
    29  	MOVL	BP, SP
    30  
    31  	// Return result.
    32  	MOVL	c+0(FP), BX
    33  	MOVL	AX, wincall_r1(BX)
    34  	MOVL	DX, wincall_r2(BX)
    35  
    36  	// GetLastError().
    37  	MOVL	0x34(FS), AX
    38  	MOVL	AX, wincall_err(BX)
    39  
    40  	RET
    41  
    42  TEXT	runtime·badsignal2(SB),NOSPLIT,$24
    43  	// stderr
    44  	MOVL	$-12, 0(SP)
    45  	MOVL	SP, BP
    46  	CALL	*runtime·GetStdHandle(SB)
    47  	MOVL	BP, SP
    48  
    49  	MOVL	AX, 0(SP)	// handle
    50  	MOVL	$runtime·badsignalmsg(SB), DX // pointer
    51  	MOVL	DX, 4(SP)
    52  	MOVL	runtime·badsignallen(SB), DX // count
    53  	MOVL	DX, 8(SP)
    54  	LEAL	20(SP), DX  // written count
    55  	MOVL	$0, 0(DX)
    56  	MOVL	DX, 12(SP)
    57  	MOVL	$0, 16(SP) // overlapped
    58  	CALL	*runtime·WriteFile(SB)
    59  	MOVL	BP, SI
    60  	RET
    61  
    62  // faster get/set last error
    63  TEXT runtime·getlasterror(SB),NOSPLIT,$0
    64  	MOVL	0x34(FS), AX
    65  	RET
    66  
    67  TEXT runtime·setlasterror(SB),NOSPLIT,$0
    68  	MOVL	err+0(FP), AX
    69  	MOVL	AX, 0x34(FS)
    70  	RET
    71  
    72  TEXT runtime·sigtramp(SB),NOSPLIT,$28
    73  	// unwinding?
    74  	MOVL	info+0(FP), CX
    75  	TESTL	$6, 4(CX)		// exception flags
    76  	MOVL	$1, AX
    77  	JNZ	sigdone
    78  
    79  	// copy arguments for call to sighandler
    80  	MOVL	CX, 0(SP)
    81  	MOVL	context+8(FP), CX
    82  	MOVL	CX, 4(SP)
    83  
    84  	get_tls(CX)
    85  
    86  	// check that m exists
    87  	MOVL	m(CX), AX
    88  	CMPL	AX, $0
    89  	JNE	2(PC)
    90  	CALL	runtime·badsignal2(SB)
    91  
    92  	MOVL	g(CX), CX
    93  	MOVL	CX, 8(SP)
    94  
    95  	MOVL	BX, 12(SP)
    96  	MOVL	BP, 16(SP)
    97  	MOVL	SI, 20(SP)
    98  	MOVL	DI, 24(SP)
    99  
   100  	CALL	runtime·sighandler(SB)
   101  	// AX is set to report result back to Windows
   102  
   103  	MOVL	24(SP), DI
   104  	MOVL	20(SP), SI
   105  	MOVL	16(SP), BP
   106  	MOVL	12(SP), BX
   107  sigdone:
   108  	RET
   109  
   110  TEXT runtime·ctrlhandler(SB),NOSPLIT,$0
   111  	PUSHL	$runtime·ctrlhandler1(SB)
   112  	CALL	runtime·externalthreadhandler(SB)
   113  	MOVL	4(SP), CX
   114  	ADDL	$12, SP
   115  	JMP	CX
   116  
   117  TEXT runtime·profileloop(SB),NOSPLIT,$0
   118  	PUSHL	$runtime·profileloop1(SB)
   119  	CALL	runtime·externalthreadhandler(SB)
   120  	MOVL	4(SP), CX
   121  	ADDL	$12, SP
   122  	JMP	CX
   123  
   124  TEXT runtime·externalthreadhandler(SB),NOSPLIT,$0
   125  	PUSHL	BP
   126  	MOVL	SP, BP
   127  	PUSHL	BX
   128  	PUSHL	SI
   129  	PUSHL	DI
   130  	PUSHL	0x14(FS)
   131  	MOVL	SP, DX
   132  
   133  	// setup dummy m, g
   134  	SUBL	$m_end, SP		// space for M
   135  	MOVL	SP, 0(SP)
   136  	MOVL	$m_end, 4(SP)
   137  	CALL	runtime·memclr(SB)	// smashes AX,BX,CX
   138  
   139  	LEAL	m_tls(SP), CX
   140  	MOVL	CX, 0x14(FS)
   141  	MOVL	SP, m(CX)
   142  	MOVL	SP, BX
   143  	SUBL	$g_end, SP		// space for G
   144  	MOVL	SP, g(CX)
   145  	MOVL	SP, m_g0(BX)
   146  
   147  	MOVL	SP, 0(SP)
   148  	MOVL	$g_end, 4(SP)
   149  	CALL	runtime·memclr(SB)	// smashes AX,BX,CX
   150  	LEAL	-4096(SP), CX
   151  	MOVL	CX, g_stackguard(SP)
   152  	MOVL	DX, g_stackbase(SP)
   153  
   154  	PUSHL	16(BP)			// arg for handler
   155  	CALL	8(BP)
   156  	POPL	CX
   157  
   158  	get_tls(CX)
   159  	MOVL	g(CX), CX
   160  	MOVL	g_stackbase(CX), SP
   161  	POPL	0x14(FS)
   162  	POPL	DI
   163  	POPL	SI
   164  	POPL	BX
   165  	POPL	BP
   166  	RET
   167  
   168  GLOBL runtime·cbctxts(SB), $4
   169  
   170  TEXT runtime·callbackasm1+0(SB),NOSPLIT,$0
   171    	MOVL	0(SP), AX	// will use to find our callback context
   172  
   173  	// remove return address from stack, we are not returning there
   174  	ADDL	$4, SP
   175  
   176  	// address to callback parameters into CX
   177  	LEAL	4(SP), CX
   178  
   179  	// save registers as required for windows callback
   180  	PUSHL	DI
   181  	PUSHL	SI
   182  	PUSHL	BP
   183  	PUSHL	BX
   184  
   185  	// set up SEH frame again
   186  	PUSHL	$runtime·sigtramp(SB)
   187  	PUSHL	0(FS)
   188  	MOVL	SP, 0(FS)
   189  
   190  	// determine index into runtime·cbctxts table
   191  	SUBL	$runtime·callbackasm(SB), AX
   192  	MOVL	$0, DX
   193  	MOVL	$5, BX	// divide by 5 because each call instruction in runtime·callbacks is 5 bytes long
   194  	DIVL	BX,
   195  
   196  	// find correspondent runtime·cbctxts table entry
   197  	MOVL	runtime·cbctxts(SB), BX
   198  	MOVL	-4(BX)(AX*4), BX
   199  
   200  	// extract callback context
   201  	MOVL	cbctxt_gobody(BX), AX
   202  	MOVL	cbctxt_argsize(BX), DX
   203  
   204  	// preserve whatever's at the memory location that
   205  	// the callback will use to store the return value
   206  	PUSHL	0(CX)(DX*1)
   207  
   208  	// extend argsize by size of return value
   209  	ADDL	$4, DX
   210  
   211  	// remember how to restore stack on return
   212  	MOVL	cbctxt_restorestack(BX), BX
   213  	PUSHL	BX
   214  
   215  	// call target Go function
   216  	PUSHL	DX			// argsize (including return value)
   217  	PUSHL	CX			// callback parameters
   218  	PUSHL	AX			// address of target Go function
   219  	CLD
   220  	CALL	runtime·cgocallback_gofunc(SB)
   221  	POPL	AX
   222  	POPL	CX
   223  	POPL	DX
   224  
   225  	// how to restore stack on return
   226  	POPL	BX
   227  
   228  	// return value into AX (as per Windows spec)
   229  	// and restore previously preserved value
   230  	MOVL	-4(CX)(DX*1), AX
   231  	POPL	-4(CX)(DX*1)
   232  
   233  	MOVL	BX, CX			// cannot use BX anymore
   234  
   235  	// pop SEH frame
   236  	POPL	0(FS)
   237  	POPL	BX
   238  
   239  	// restore registers as required for windows callback
   240  	POPL	BX
   241  	POPL	BP
   242  	POPL	SI
   243  	POPL	DI
   244  
   245  	// remove callback parameters before return (as per Windows spec)
   246  	POPL	DX
   247  	ADDL	CX, SP
   248  	PUSHL	DX
   249  
   250  	CLD
   251  
   252  	RET
   253  
   254  // void tstart(M *newm);
   255  TEXT runtime·tstart(SB),NOSPLIT,$0
   256  	MOVL	newm+4(SP), CX		// m
   257  	MOVL	m_g0(CX), DX		// g
   258  
   259  	// Layout new m scheduler stack on os stack.
   260  	MOVL	SP, AX
   261  	MOVL	AX, g_stackbase(DX)
   262  	SUBL	$(64*1024), AX		// stack size
   263  	MOVL	AX, g_stackguard(DX)
   264  
   265  	// Set up tls.
   266  	LEAL	m_tls(CX), SI
   267  	MOVL	SI, 0x14(FS)
   268  	MOVL	CX, m(SI)
   269  	MOVL	DX, g(SI)
   270  
   271  	// Someday the convention will be D is always cleared.
   272  	CLD
   273  
   274  	CALL	runtime·stackcheck(SB)	// clobbers AX,CX
   275  	CALL	runtime·mstart(SB)
   276  
   277  	RET
   278  
   279  // uint32 tstart_stdcall(M *newm);
   280  TEXT runtime·tstart_stdcall(SB),NOSPLIT,$0
   281  	MOVL	newm+4(SP), BX
   282  
   283  	PUSHL	BX
   284  	CALL	runtime·tstart(SB)
   285  	POPL	BX
   286  
   287  	// Adjust stack for stdcall to return properly.
   288  	MOVL	(SP), AX		// save return address
   289  	ADDL	$4, SP			// remove single parameter
   290  	MOVL	AX, (SP)		// restore return address
   291  
   292  	XORL	AX, AX			// return 0 == success
   293  
   294  	RET
   295  
   296  // setldt(int entry, int address, int limit)
   297  TEXT runtime·setldt(SB),NOSPLIT,$0
   298  	MOVL	address+4(FP), CX
   299  	MOVL	CX, 0x14(FS)
   300  	RET
   301  
   302  // void install_exception_handler()
   303  TEXT runtime·install_exception_handler(SB),NOSPLIT,$0
   304  	get_tls(CX)
   305  	MOVL	m(CX), CX		// m
   306  
   307  	// Set up SEH frame
   308  	MOVL	m_seh(CX), DX
   309  	MOVL	$runtime·sigtramp(SB), AX
   310  	MOVL	AX, seh_handler(DX)
   311  	MOVL	0(FS), AX
   312  	MOVL	AX, seh_prev(DX)
   313  
   314  	// Install it
   315  	MOVL	DX, 0(FS)
   316  
   317  	RET
   318  
   319  // void remove_exception_handler()
   320  TEXT runtime·remove_exception_handler(SB),NOSPLIT,$0
   321  	get_tls(CX)
   322  	MOVL	m(CX), CX		// m
   323  
   324  	// Remove SEH frame
   325  	MOVL	m_seh(CX), DX
   326  	MOVL	seh_prev(DX), AX
   327  	MOVL	AX, 0(FS)
   328  
   329  	RET
   330  
   331  // Sleep duration is in 100ns units.
   332  TEXT runtime·usleep1(SB),NOSPLIT,$0
   333  	MOVL	duration+0(FP), BX
   334  	MOVL	$runtime·usleep2(SB), AX // to hide from 8l
   335  
   336  	// Execute call on m->g0 stack, in case we are not actually
   337  	// calling a system call wrapper, like when running under WINE.
   338  	get_tls(CX)
   339  	CMPL	CX, $0
   340  	JNE	3(PC)
   341  	// Not a Go-managed thread. Do not switch stack.
   342  	CALL	AX
   343  	RET
   344  
   345  	MOVL	m(CX), BP
   346  	MOVL	m_g0(BP), SI
   347  	CMPL	g(CX), SI
   348  	JNE	3(PC)
   349  	// executing on m->g0 already
   350  	CALL	AX
   351  	RET
   352  
   353  	// Switch to m->g0 stack and back.
   354  	MOVL	(g_sched+gobuf_sp)(SI), SI
   355  	MOVL	SP, -4(SI)
   356  	LEAL	-4(SI), SP
   357  	CALL	AX
   358  	MOVL	0(SP), SP
   359  	RET
   360  
   361  // Runs on OS stack. duration (in 100ns units) is in BX.
   362  TEXT runtime·usleep2(SB),NOSPLIT,$20
   363  	// Want negative 100ns units.
   364  	NEGL	BX
   365  	MOVL	$-1, hi-4(SP)
   366  	MOVL	BX, lo-8(SP)
   367  	LEAL	lo-8(SP), BX
   368  	MOVL	BX, ptime-12(SP)
   369  	MOVL	$0, alertable-16(SP)
   370  	MOVL	$-1, handle-20(SP)
   371  	MOVL	SP, BP
   372  	MOVL	runtime·NtWaitForSingleObject(SB), AX
   373  	CALL	AX
   374  	MOVL	BP, SP
   375  	RET