Text file
src/runtime/asm_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 #include "go_asm.h"
6 #include "go_tls.h"
7 #include "funcdata.h"
8 #include "textflag.h"
9
10 // _rt0_386 is common startup code for most 386 systems when using
11 // internal linking. This is the entry point for the program from the
12 // kernel for an ordinary -buildmode=exe program. The stack holds the
13 // number of arguments and the C-style argv.
14 TEXT _rt0_386(SB),NOSPLIT,$8
15 MOVL 8(SP), AX // argc
16 LEAL 12(SP), BX // argv
17 MOVL AX, 0(SP)
18 MOVL BX, 4(SP)
19 JMP runtime·rt0_go(SB)
20
21 // _rt0_386_lib is common startup code for most 386 systems when
22 // using -buildmode=c-archive or -buildmode=c-shared. The linker will
23 // arrange to invoke this function as a global constructor (for
24 // c-archive) or when the shared library is loaded (for c-shared).
25 // We expect argc and argv to be passed on the stack following the
26 // usual C ABI.
27 TEXT _rt0_386_lib(SB),NOSPLIT,$0
28 PUSHL BP
29 MOVL SP, BP
30 PUSHL BX
31 PUSHL SI
32 PUSHL DI
33
34 MOVL 8(BP), AX
35 MOVL AX, _rt0_386_lib_argc<>(SB)
36 MOVL 12(BP), AX
37 MOVL AX, _rt0_386_lib_argv<>(SB)
38
39 CALL runtime·libInit(SB)
40
41 POPL DI
42 POPL SI
43 POPL BX
44 POPL BP
45 RET
46
47 // rt0_lib_go initializes the Go runtime.
48 // This is started in a separate thread by _rt0_386_lib.
49 TEXT runtime·rt0_lib_go<ABIInternal>(SB),NOSPLIT,$8
50 MOVL _rt0_386_lib_argc<>(SB), AX
51 MOVL AX, 0(SP)
52 MOVL _rt0_386_lib_argv<>(SB), AX
53 MOVL AX, 4(SP)
54 JMP runtime·rt0_go(SB)
55
56 DATA _rt0_386_lib_argc<>(SB)/4, $0
57 GLOBL _rt0_386_lib_argc<>(SB),NOPTR, $4
58 DATA _rt0_386_lib_argv<>(SB)/4, $0
59 GLOBL _rt0_386_lib_argv<>(SB),NOPTR, $4
60
61 TEXT runtime·rt0_go(SB),NOSPLIT|NOFRAME|TOPFRAME,$0
62 // Copy arguments forward on an even stack.
63 // Users of this function jump to it, they don't call it.
64 MOVL 0(SP), AX
65 MOVL 4(SP), BX
66 SUBL $128, SP // plenty of scratch
67 ANDL $~15, SP
68 MOVL AX, 120(SP) // save argc, argv away
69 MOVL BX, 124(SP)
70
71 // set default stack bounds.
72 // _cgo_init may update stackguard.
73 MOVL $runtime·g0(SB), BP
74 LEAL (-64*1024+104)(SP), BX
75 MOVL BX, g_stackguard0(BP)
76 MOVL BX, g_stackguard1(BP)
77 MOVL BX, (g_stack+stack_lo)(BP)
78 MOVL SP, (g_stack+stack_hi)(BP)
79
80 // find out information about the processor we're on
81 // first see if CPUID instruction is supported.
82 PUSHFL
83 PUSHFL
84 XORL $(1<<21), 0(SP) // flip ID bit
85 POPFL
86 PUSHFL
87 POPL AX
88 XORL 0(SP), AX
89 POPFL // restore EFLAGS
90 TESTL $(1<<21), AX
91 JNE has_cpuid
92
93 bad_proc: // show that the program requires MMX.
94 MOVL $2, 0(SP)
95 MOVL $bad_proc_msg<>(SB), 4(SP)
96 MOVL $0x3d, 8(SP)
97 CALL runtime·write(SB)
98 MOVL $1, 0(SP)
99 CALL runtime·exit(SB)
100 CALL runtime·abort(SB)
101
102 has_cpuid:
103 MOVL $0, AX
104 CPUID
105 MOVL AX, SI
106 CMPL AX, $0
107 JE nocpuinfo
108
109 CMPL BX, $0x756E6547 // "Genu"
110 JNE notintel
111 CMPL DX, $0x49656E69 // "ineI"
112 JNE notintel
113 CMPL CX, $0x6C65746E // "ntel"
114 JNE notintel
115 MOVB $1, runtime·isIntel(SB)
116 notintel:
117
118 // Load EAX=1 cpuid flags
119 MOVL $1, AX
120 CPUID
121 MOVL CX, DI // Move to global variable clobbers CX when generating PIC
122 MOVL AX, runtime·processorVersionInfo(SB)
123
124 // Check for MMX support
125 TESTL $(1<<23), DX // MMX
126 JZ bad_proc
127
128 nocpuinfo:
129 // If there is an _cgo_init, call it to initialize and, on platforms
130 // that need it, set up GS. Otherwise, we set up GS ourselves.
131 MOVL _cgo_init(SB), AX
132 TESTL AX, AX
133 JZ needtls
134 #ifdef GOOS_android
135 // arg 4: TLS base, stored in slot 0 (Android's TLS_SLOT_SELF).
136 // Compensate for tls_g (+8).
137 MOVL -8(TLS), BX
138 MOVL BX, 12(SP)
139 MOVL $runtime·tls_g(SB), 8(SP) // arg 3: &tls_g
140 #else
141 MOVL $0, BX
142 MOVL BX, 12(SP) // arg 4: not used when using platform's TLS
143 MOVL BX, 8(SP) // arg 3: not used when using platform's TLS
144 #endif
145 MOVL $setg_gcc<>(SB), BX
146 MOVL BX, 4(SP) // arg 2: setg_gcc
147 MOVL BP, 0(SP) // arg 1: g0
148 CALL AX
149
150 // update stackguard after _cgo_init
151 MOVL $runtime·g0(SB), CX
152 MOVL (g_stack+stack_lo)(CX), AX
153 ADDL $const_stackGuard, AX
154 MOVL AX, g_stackguard0(CX)
155 MOVL AX, g_stackguard1(CX)
156
157 JMP ok
158 needtls:
159 #ifdef GOOS_openbsd
160 // skip runtime·ldt0setup(SB) and tls test on OpenBSD in all cases
161 JMP ok
162 #endif
163 #ifdef GOOS_plan9
164 // skip runtime·ldt0setup(SB) and tls test on Plan 9 in all cases
165 JMP ok
166 #endif
167 #ifdef GOOS_windows
168 // The Windows loader has already set up TLS.
169 JMP ok
170 #else
171 // Set up %gs.
172 CALL ldt0setup<>(SB)
173
174 // store through it, to make sure it works
175 get_tls(BX)
176 MOVL $0x123, g(BX)
177 MOVL runtime·m0+m_tls(SB), AX
178 CMPL AX, $0x123
179 JEQ ok
180 MOVL AX, 0 // abort
181 #endif
182 ok:
183 // set up m and g "registers"
184 get_tls(BX)
185 LEAL runtime·g0(SB), DX
186 MOVL DX, g(BX)
187 LEAL runtime·m0(SB), AX
188
189 // save m->g0 = g0
190 MOVL DX, m_g0(AX)
191 // save g0->m = m0
192 MOVL AX, g_m(DX)
193
194 CALL runtime·emptyfunc(SB) // fault if stack check is wrong
195
196 // convention is D is always cleared
197 CLD
198
199 CALL runtime·check(SB)
200
201 // saved argc, argv
202 MOVL 120(SP), AX
203 MOVL AX, 0(SP)
204 MOVL 124(SP), AX
205 MOVL AX, 4(SP)
206 CALL runtime·args(SB)
207 CALL runtime·osinit(SB)
208 CALL runtime·schedinit(SB)
209
210 // create a new goroutine to start program
211 PUSHL $runtime·mainPC(SB) // entry
212 CALL runtime·newproc(SB)
213 POPL AX
214
215 // start this M
216 CALL runtime·mstart(SB)
217
218 CALL runtime·abort(SB)
219 RET
220
221 DATA bad_proc_msg<>+0x00(SB)/61, $"This program can only be run on processors with MMX support.\n"
222 GLOBL bad_proc_msg<>(SB), RODATA, $61
223
224 DATA runtime·mainPC+0(SB)/4,$runtime·main(SB)
225 GLOBL runtime·mainPC(SB),RODATA,$4
226
227 TEXT runtime·breakpoint(SB),NOSPLIT,$0-0
228 INT $3
229 RET
230
231 TEXT runtime·asminit(SB),NOSPLIT,$0-0
232 // Linux and MinGW start the FPU in extended double precision.
233 // Other operating systems use double precision.
234 // Change to double precision to match them,
235 // and to match other hardware that only has double.
236 FLDCW runtime·controlWord64(SB)
237 RET
238
239 TEXT runtime·mstart(SB),NOSPLIT|TOPFRAME,$0
240 CALL runtime·mstart0(SB)
241 RET // not reached
242
243 /*
244 * go-routine
245 */
246
247 // void gogo(Gobuf*)
248 // restore state from Gobuf; longjmp
249 TEXT runtime·gogo(SB), NOSPLIT, $0-4
250 MOVL buf+0(FP), BX // gobuf
251 MOVL gobuf_g(BX), DX
252 MOVL 0(DX), CX // make sure g != nil
253 JMP gogo<>(SB)
254
255 TEXT gogo<>(SB), NOSPLIT, $0
256 get_tls(CX)
257 MOVL DX, g(CX)
258 MOVL gobuf_sp(BX), SP // restore SP
259 MOVL gobuf_ctxt(BX), DX
260 MOVL $0, gobuf_sp(BX) // clear to help garbage collector
261 MOVL $0, gobuf_ctxt(BX)
262 MOVL gobuf_pc(BX), BX
263 JMP BX
264
265 // func mcall(fn func(*g))
266 // Switch to m->g0's stack, call fn(g).
267 // Fn must never return. It should gogo(&g->sched)
268 // to keep running g.
269 TEXT runtime·mcall(SB), NOSPLIT, $0-4
270 MOVL fn+0(FP), DI
271
272 get_tls(DX)
273 MOVL g(DX), AX // save state in g->sched
274 MOVL 0(SP), BX // caller's PC
275 MOVL BX, (g_sched+gobuf_pc)(AX)
276 LEAL fn+0(FP), BX // caller's SP
277 MOVL BX, (g_sched+gobuf_sp)(AX)
278
279 // switch to m->g0 & its stack, call fn
280 MOVL g(DX), BX
281 MOVL g_m(BX), BX
282 MOVL m_g0(BX), SI
283 CMPL SI, AX // if g == m->g0 call badmcall
284 JNE 3(PC)
285 MOVL $runtime·badmcall(SB), AX
286 JMP AX
287 MOVL SI, g(DX) // g = m->g0
288 MOVL (g_sched+gobuf_sp)(SI), SP // sp = m->g0->sched.sp
289 PUSHL AX
290 MOVL DI, DX
291 MOVL 0(DI), DI
292 CALL DI
293 POPL AX
294 MOVL $runtime·badmcall2(SB), AX
295 JMP AX
296 RET
297
298 // systemstack_switch is a dummy routine that systemstack leaves at the bottom
299 // of the G stack. We need to distinguish the routine that
300 // lives at the bottom of the G stack from the one that lives
301 // at the top of the system stack because the one at the top of
302 // the system stack terminates the stack walk (see topofstack()).
303 TEXT runtime·systemstack_switch(SB), NOSPLIT, $0-0
304 RET
305
306 // func systemstack(fn func())
307 TEXT runtime·systemstack(SB), NOSPLIT, $0-4
308 MOVL fn+0(FP), DI // DI = fn
309 get_tls(CX)
310 MOVL g(CX), AX // AX = g
311 MOVL g_m(AX), BX // BX = m
312
313 CMPL AX, m_gsignal(BX)
314 JEQ noswitch
315
316 MOVL m_g0(BX), DX // DX = g0
317 CMPL AX, DX
318 JEQ noswitch
319
320 CMPL AX, m_curg(BX)
321 JNE bad
322
323 // switch stacks
324 // save our state in g->sched. Pretend to
325 // be systemstack_switch if the G stack is scanned.
326 CALL gosave_systemstack_switch<>(SB)
327
328 // switch to g0
329 get_tls(CX)
330 MOVL DX, g(CX)
331 MOVL (g_sched+gobuf_sp)(DX), BX
332 MOVL BX, SP
333
334 // call target function
335 MOVL DI, DX
336 MOVL 0(DI), DI
337 CALL DI
338
339 // switch back to g
340 get_tls(CX)
341 MOVL g(CX), AX
342 MOVL g_m(AX), BX
343 MOVL m_curg(BX), AX
344 MOVL AX, g(CX)
345 MOVL (g_sched+gobuf_sp)(AX), SP
346 MOVL $0, (g_sched+gobuf_sp)(AX)
347 RET
348
349 noswitch:
350 // already on system stack; tail call the function
351 // Using a tail call here cleans up tracebacks since we won't stop
352 // at an intermediate systemstack.
353 MOVL DI, DX
354 MOVL 0(DI), DI
355 JMP DI
356
357 bad:
358 // Bad: g is not gsignal, not g0, not curg. What is it?
359 // Hide call from linker nosplit analysis.
360 MOVL $runtime·badsystemstack(SB), AX
361 CALL AX
362 INT $3
363
364 // func switchToCrashStack0(fn func())
365 TEXT runtime·switchToCrashStack0(SB), NOSPLIT, $0-4
366 MOVL fn+0(FP), AX
367
368 get_tls2(CX, DI)
369 MOVL g(CX), BX // BX = g
370 MOVL g_m(BX), DX // DX = curm
371
372 // set g to gcrash
373 LEAL runtime·gcrash(SB), BX // g = &gcrash
374 MOVL DX, g_m(BX) // g.m = curm
375 MOVL BX, m_g0(DX) // curm.g0 = g
376 get_tls2(CX, DI)
377 MOVL BX, g(CX)
378
379 // switch to crashstack
380 MOVL (g_stack+stack_hi)(BX), DX
381 SUBL $(4*8), DX
382 MOVL DX, SP
383
384 // call target function
385 MOVL AX, DX
386 MOVL 0(AX), AX
387 CALL AX
388
389 // should never return
390 CALL runtime·abort(SB)
391 UNDEF
392
393 /*
394 * support for morestack
395 */
396
397 // Called during function prolog when more stack is needed.
398 //
399 // The traceback routines see morestack on a g0 as being
400 // the top of a stack (for example, morestack calling newstack
401 // calling the scheduler calling newm calling gc), so we must
402 // record an argument size. For that purpose, it has no arguments.
403 TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0
404 // Cannot grow scheduler stack (m->g0).
405 get_tls(CX)
406 MOVL g(CX), DI
407 MOVL g_m(DI), BX
408
409 // Set g->sched to context in f.
410 MOVL 0(SP), AX // f's PC
411 MOVL AX, (g_sched+gobuf_pc)(DI)
412 LEAL 4(SP), AX // f's SP
413 MOVL AX, (g_sched+gobuf_sp)(DI)
414 MOVL DX, (g_sched+gobuf_ctxt)(DI)
415
416 MOVL m_g0(BX), SI
417 CMPL g(CX), SI
418 JNE 3(PC)
419 CALL runtime·badmorestackg0(SB)
420 CALL runtime·abort(SB)
421
422 // Cannot grow signal stack.
423 MOVL m_gsignal(BX), SI
424 CMPL g(CX), SI
425 JNE 3(PC)
426 CALL runtime·badmorestackgsignal(SB)
427 CALL runtime·abort(SB)
428
429 // Called from f.
430 // Set m->morebuf to f's caller.
431 NOP SP // tell vet SP changed - stop checking offsets
432 MOVL 4(SP), DI // f's caller's PC
433 MOVL DI, (m_morebuf+gobuf_pc)(BX)
434 LEAL 8(SP), CX // f's caller's SP
435 MOVL CX, (m_morebuf+gobuf_sp)(BX)
436 get_tls(CX)
437 MOVL g(CX), SI
438 MOVL SI, (m_morebuf+gobuf_g)(BX)
439
440 // Call newstack on m->g0's stack.
441 MOVL m_g0(BX), BP
442 MOVL BP, g(CX)
443 MOVL (g_sched+gobuf_sp)(BP), AX
444 MOVL -4(AX), BX // fault if CALL would, before smashing SP
445 MOVL AX, SP
446 CALL runtime·newstack(SB)
447 CALL runtime·abort(SB) // crash if newstack returns
448 RET
449
450 TEXT runtime·morestack_noctxt(SB),NOSPLIT,$0-0
451 MOVL $0, DX
452 JMP runtime·morestack(SB)
453
454 // reflectcall: call a function with the given argument list
455 // func call(stackArgsType *_type, f *FuncVal, stackArgs *byte, stackArgsSize, stackRetOffset, frameSize uint32, regArgs *abi.RegArgs).
456 // we don't have variable-sized frames, so we use a small number
457 // of constant-sized-frame functions to encode a few bits of size in the pc.
458 // Caution: ugly multiline assembly macros in your future!
459
460 #define DISPATCH(NAME,MAXSIZE) \
461 CMPL CX, $MAXSIZE; \
462 JA 3(PC); \
463 MOVL $NAME(SB), AX; \
464 JMP AX
465 // Note: can't just "JMP NAME(SB)" - bad inlining results.
466
467 TEXT ·reflectcall(SB), NOSPLIT, $0-28
468 MOVL frameSize+20(FP), CX
469 DISPATCH(runtime·call16, 16)
470 DISPATCH(runtime·call32, 32)
471 DISPATCH(runtime·call64, 64)
472 DISPATCH(runtime·call128, 128)
473 DISPATCH(runtime·call256, 256)
474 DISPATCH(runtime·call512, 512)
475 DISPATCH(runtime·call1024, 1024)
476 DISPATCH(runtime·call2048, 2048)
477 DISPATCH(runtime·call4096, 4096)
478 DISPATCH(runtime·call8192, 8192)
479 DISPATCH(runtime·call16384, 16384)
480 DISPATCH(runtime·call32768, 32768)
481 DISPATCH(runtime·call65536, 65536)
482 DISPATCH(runtime·call131072, 131072)
483 DISPATCH(runtime·call262144, 262144)
484 DISPATCH(runtime·call524288, 524288)
485 DISPATCH(runtime·call1048576, 1048576)
486 DISPATCH(runtime·call2097152, 2097152)
487 DISPATCH(runtime·call4194304, 4194304)
488 DISPATCH(runtime·call8388608, 8388608)
489 DISPATCH(runtime·call16777216, 16777216)
490 DISPATCH(runtime·call33554432, 33554432)
491 DISPATCH(runtime·call67108864, 67108864)
492 DISPATCH(runtime·call134217728, 134217728)
493 DISPATCH(runtime·call268435456, 268435456)
494 DISPATCH(runtime·call536870912, 536870912)
495 DISPATCH(runtime·call1073741824, 1073741824)
496 MOVL $runtime·badreflectcall(SB), AX
497 JMP AX
498
499 #define CALLFN(NAME,MAXSIZE) \
500 TEXT NAME(SB), WRAPPER, $MAXSIZE-28; \
501 NO_LOCAL_POINTERS; \
502 /* copy arguments to stack */ \
503 MOVL stackArgs+8(FP), SI; \
504 MOVL stackArgsSize+12(FP), CX; \
505 MOVL SP, DI; \
506 REP;MOVSB; \
507 /* call function */ \
508 MOVL f+4(FP), DX; \
509 MOVL (DX), AX; \
510 PCDATA $PCDATA_StackMapIndex, $0; \
511 CALL AX; \
512 /* copy return values back */ \
513 MOVL stackArgsType+0(FP), DX; \
514 MOVL stackArgs+8(FP), DI; \
515 MOVL stackArgsSize+12(FP), CX; \
516 MOVL stackRetOffset+16(FP), BX; \
517 MOVL SP, SI; \
518 ADDL BX, DI; \
519 ADDL BX, SI; \
520 SUBL BX, CX; \
521 CALL callRet<>(SB); \
522 RET
523
524 // callRet copies return values back at the end of call*. This is a
525 // separate function so it can allocate stack space for the arguments
526 // to reflectcallmove. It does not follow the Go ABI; it expects its
527 // arguments in registers.
528 TEXT callRet<>(SB), NOSPLIT, $20-0
529 MOVL DX, 0(SP)
530 MOVL DI, 4(SP)
531 MOVL SI, 8(SP)
532 MOVL CX, 12(SP)
533 MOVL $0, 16(SP)
534 CALL runtime·reflectcallmove(SB)
535 RET
536
537 CALLFN(·call16, 16)
538 CALLFN(·call32, 32)
539 CALLFN(·call64, 64)
540 CALLFN(·call128, 128)
541 CALLFN(·call256, 256)
542 CALLFN(·call512, 512)
543 CALLFN(·call1024, 1024)
544 CALLFN(·call2048, 2048)
545 CALLFN(·call4096, 4096)
546 CALLFN(·call8192, 8192)
547 CALLFN(·call16384, 16384)
548 CALLFN(·call32768, 32768)
549 CALLFN(·call65536, 65536)
550 CALLFN(·call131072, 131072)
551 CALLFN(·call262144, 262144)
552 CALLFN(·call524288, 524288)
553 CALLFN(·call1048576, 1048576)
554 CALLFN(·call2097152, 2097152)
555 CALLFN(·call4194304, 4194304)
556 CALLFN(·call8388608, 8388608)
557 CALLFN(·call16777216, 16777216)
558 CALLFN(·call33554432, 33554432)
559 CALLFN(·call67108864, 67108864)
560 CALLFN(·call134217728, 134217728)
561 CALLFN(·call268435456, 268435456)
562 CALLFN(·call536870912, 536870912)
563 CALLFN(·call1073741824, 1073741824)
564
565 TEXT runtime·procyieldAsm(SB),NOSPLIT,$0-0
566 MOVL cycles+0(FP), AX
567 TESTL AX, AX
568 JZ done
569 again:
570 PAUSE
571 SUBL $1, AX
572 JNZ again
573 done:
574 RET
575
576 TEXT ·publicationBarrier(SB),NOSPLIT,$0-0
577 // Stores are already ordered on x86, so this is just a
578 // compile barrier.
579 RET
580
581 // Save state of caller into g->sched,
582 // but using fake PC from systemstack_switch.
583 // Must only be called from functions with no locals ($0)
584 // or else unwinding from systemstack_switch is incorrect.
585 TEXT gosave_systemstack_switch<>(SB),NOSPLIT,$0
586 PUSHL AX
587 PUSHL BX
588 get_tls(BX)
589 MOVL g(BX), BX
590 LEAL arg+0(FP), AX
591 MOVL AX, (g_sched+gobuf_sp)(BX)
592 MOVL $runtime·systemstack_switch(SB), AX
593 MOVL AX, (g_sched+gobuf_pc)(BX)
594 // Assert ctxt is zero. See func save.
595 MOVL (g_sched+gobuf_ctxt)(BX), AX
596 TESTL AX, AX
597 JZ 2(PC)
598 CALL runtime·abort(SB)
599 POPL BX
600 POPL AX
601 RET
602
603 // func asmcgocall_no_g(fn, arg unsafe.Pointer)
604 // Call fn(arg) aligned appropriately for the gcc ABI.
605 // Called on a system stack, and there may be no g yet (during needm).
606 TEXT ·asmcgocall_no_g(SB),NOSPLIT,$0-8
607 MOVL fn+0(FP), AX
608 MOVL arg+4(FP), BX
609 MOVL SP, DX
610 SUBL $32, SP
611 ANDL $~15, SP // alignment, perhaps unnecessary
612 MOVL DX, 8(SP) // save old SP
613 MOVL BX, 0(SP) // first argument in x86-32 ABI
614 CALL AX
615 MOVL 8(SP), DX
616 MOVL DX, SP
617 RET
618
619 // func asmcgocall(fn, arg unsafe.Pointer) int32
620 // Call fn(arg) on the scheduler stack,
621 // aligned appropriately for the gcc ABI.
622 // See cgocall.go for more details.
623 TEXT ·asmcgocall(SB),NOSPLIT,$0-12
624 MOVL fn+0(FP), AX
625 MOVL arg+4(FP), BX
626
627 MOVL SP, DX
628
629 // Figure out if we need to switch to m->g0 stack.
630 // We get called to create new OS threads too, and those
631 // come in on the m->g0 stack already. Or we might already
632 // be on the m->gsignal stack.
633 get_tls2(CX, DI)
634 MOVL g(CX), DI
635 CMPL DI, $0
636 JEQ nosave // Don't even have a G yet.
637 MOVL g_m(DI), BP
638 CMPL DI, m_gsignal(BP)
639 JEQ noswitch
640 MOVL m_g0(BP), SI
641 CMPL DI, SI
642 JEQ noswitch
643 CALL gosave_systemstack_switch<>(SB)
644 get_tls2(CX, BP)
645 MOVL SI, g(CX)
646 MOVL (g_sched+gobuf_sp)(SI), SP
647
648 noswitch:
649 // Now on a scheduling stack (a pthread-created stack).
650 SUBL $32, SP
651 ANDL $~15, SP // alignment, perhaps unnecessary
652 MOVL DI, 8(SP) // save g
653 MOVL (g_stack+stack_hi)(DI), DI
654 SUBL DX, DI
655 MOVL DI, 4(SP) // save depth in stack (can't just save SP, as stack might be copied during a callback)
656 MOVL BX, 0(SP) // first argument in x86-32 ABI
657 CALL AX
658
659 // Restore registers, g, stack pointer.
660 get_tls2(CX, DI)
661 MOVL 8(SP), DI
662 MOVL (g_stack+stack_hi)(DI), SI
663 SUBL 4(SP), SI
664 MOVL DI, g(CX)
665 MOVL SI, SP
666
667 MOVL AX, ret+8(FP)
668 RET
669 nosave:
670 // Now on a scheduling stack (a pthread-created stack).
671 SUBL $32, SP
672 ANDL $~15, SP // alignment, perhaps unnecessary
673 MOVL DX, 4(SP) // save original stack pointer
674 MOVL BX, 0(SP) // first argument in x86-32 ABI
675 CALL AX
676
677 MOVL 4(SP), CX // restore original stack pointer
678 MOVL CX, SP
679 MOVL AX, ret+8(FP)
680 RET
681
682 // cgocallback(fn, frame unsafe.Pointer, ctxt uintptr)
683 // See cgocall.go for more details.
684 TEXT ·cgocallback(SB),NOSPLIT,$12-12 // Frame size must match commented places below
685 NO_LOCAL_POINTERS
686
687 // Skip cgocallbackg, just dropm when fn is nil, and frame is the saved g.
688 // It is used to dropm while thread is exiting.
689 MOVL fn+0(FP), AX
690 CMPL AX, $0
691 JNE loadg
692 // Restore the g from frame.
693 get_tls(CX)
694 MOVL frame+4(FP), BX
695 MOVL BX, g(CX)
696 JMP dropm
697
698 loadg:
699 // If g is nil, Go did not create the current thread,
700 // or if this thread never called into Go on pthread platforms.
701 // Call needm to obtain one for temporary use.
702 // In this case, we're running on the thread stack, so there's
703 // lots of space, but the linker doesn't know. Hide the call from
704 // the linker analysis by using an indirect call through AX.
705 get_tls(CX)
706 MOVL g(CX), BP
707 CMPL BP, $0
708 JEQ needm
709 MOVL g_m(BP), BP
710 MOVL BP, savedm-4(SP) // saved copy of oldm
711 JMP havem
712 needm:
713 MOVL $runtime·needAndBindM(SB), AX
714 CALL AX
715 MOVL $0, savedm-4(SP)
716 get_tls(CX)
717 MOVL g(CX), BP
718 MOVL g_m(BP), BP
719
720 // Set m->sched.sp = SP, so that if a panic happens
721 // during the function we are about to execute, it will
722 // have a valid SP to run on the g0 stack.
723 // The next few lines (after the havem label)
724 // will save this SP onto the stack and then write
725 // the same SP back to m->sched.sp. That seems redundant,
726 // but if an unrecovered panic happens, unwindm will
727 // restore the g->sched.sp from the stack location
728 // and then systemstack will try to use it. If we don't set it here,
729 // that restored SP will be uninitialized (typically 0) and
730 // will not be usable.
731 MOVL m_g0(BP), SI
732 MOVL SP, (g_sched+gobuf_sp)(SI)
733
734 havem:
735 // Now there's a valid m, and we're running on its m->g0.
736 // Save current m->g0->sched.sp on stack and then set it to SP.
737 // Save current sp in m->g0->sched.sp in preparation for
738 // switch back to m->curg stack.
739 // NOTE: unwindm knows that the saved g->sched.sp is at 0(SP).
740 MOVL m_g0(BP), SI
741 MOVL (g_sched+gobuf_sp)(SI), AX
742 MOVL AX, 0(SP)
743 MOVL SP, (g_sched+gobuf_sp)(SI)
744
745 // Switch to m->curg stack and call runtime.cgocallbackg.
746 // Because we are taking over the execution of m->curg
747 // but *not* resuming what had been running, we need to
748 // save that information (m->curg->sched) so we can restore it.
749 // We can restore m->curg->sched.sp easily, because calling
750 // runtime.cgocallbackg leaves SP unchanged upon return.
751 // To save m->curg->sched.pc, we push it onto the curg stack and
752 // open a frame the same size as cgocallback's g0 frame.
753 // Once we switch to the curg stack, the pushed PC will appear
754 // to be the return PC of cgocallback, so that the traceback
755 // will seamlessly trace back into the earlier calls.
756 MOVL m_curg(BP), SI
757 MOVL SI, g(CX)
758 MOVL (g_sched+gobuf_sp)(SI), DI // prepare stack as DI
759 MOVL (g_sched+gobuf_pc)(SI), BP
760 MOVL BP, -4(DI) // "push" return PC on the g stack
761 // Gather our arguments into registers.
762 MOVL fn+0(FP), AX
763 MOVL frame+4(FP), BX
764 MOVL ctxt+8(FP), CX
765 LEAL -(4+12)(DI), SP // Must match declared frame size
766 MOVL AX, 0(SP)
767 MOVL BX, 4(SP)
768 MOVL CX, 8(SP)
769 CALL runtime·cgocallbackg(SB)
770
771 // Restore g->sched (== m->curg->sched) from saved values.
772 get_tls(CX)
773 MOVL g(CX), SI
774 MOVL 12(SP), BP // Must match declared frame size
775 MOVL BP, (g_sched+gobuf_pc)(SI)
776 LEAL (12+4)(SP), DI // Must match declared frame size
777 MOVL DI, (g_sched+gobuf_sp)(SI)
778
779 // Switch back to m->g0's stack and restore m->g0->sched.sp.
780 // (Unlike m->curg, the g0 goroutine never uses sched.pc,
781 // so we do not have to restore it.)
782 MOVL g(CX), BP
783 MOVL g_m(BP), BP
784 MOVL m_g0(BP), SI
785 MOVL SI, g(CX)
786 MOVL (g_sched+gobuf_sp)(SI), SP
787 MOVL 0(SP), AX
788 MOVL AX, (g_sched+gobuf_sp)(SI)
789
790 // If the m on entry was nil, we called needm above to borrow an m,
791 // 1. for the duration of the call on non-pthread platforms,
792 // 2. or the duration of the C thread alive on pthread platforms.
793 // If the m on entry wasn't nil,
794 // 1. the thread might be a Go thread,
795 // 2. or it wasn't the first call from a C thread on pthread platforms,
796 // since then we skip dropm to reuse the m in the first call.
797 MOVL savedm-4(SP), DX
798 CMPL DX, $0
799 JNE droppedm
800
801 // Skip dropm to reuse it in the next call, when a pthread key has been created.
802 MOVL _cgo_pthread_key_created(SB), DX
803 // It means cgo is disabled when _cgo_pthread_key_created is a nil pointer, need dropm.
804 CMPL DX, $0
805 JEQ dropm
806 CMPL (DX), $0
807 JNE droppedm
808
809 dropm:
810 MOVL $runtime·dropm(SB), AX
811 CALL AX
812 droppedm:
813
814 // Done!
815 RET
816
817 // void setg(G*); set g. for use by needm.
818 TEXT runtime·setg(SB), NOSPLIT, $0-4
819 MOVL gg+0(FP), BX
820 get_tls(CX)
821 MOVL BX, g(CX)
822 RET
823
824 // void setg_gcc(G*); set g. for use by gcc
825 TEXT setg_gcc<>(SB), NOSPLIT, $0
826 get_tls2(AX, DX)
827 MOVL gg+0(FP), DX
828 MOVL DX, g(AX)
829 RET
830
831 TEXT runtime·abort(SB),NOSPLIT,$0-0
832 INT $3
833 loop:
834 JMP loop
835
836 // check that SP is in range [g->stack.lo, g->stack.hi)
837 TEXT runtime·stackcheck(SB), NOSPLIT, $0-0
838 get_tls(CX)
839 MOVL g(CX), AX
840 CMPL (g_stack+stack_hi)(AX), SP
841 JHI 2(PC)
842 CALL runtime·abort(SB)
843 CMPL SP, (g_stack+stack_lo)(AX)
844 JHI 2(PC)
845 CALL runtime·abort(SB)
846 RET
847
848 // func cputicks() int64
849 TEXT runtime·cputicks(SB),NOSPLIT,$0-8
850 // LFENCE/MFENCE instruction support is dependent on SSE2.
851 // When no SSE2 support is present do not enforce any serialization
852 // since using CPUID to serialize the instruction stream is
853 // very costly.
854 #ifdef GO386_softfloat
855 JMP rdtsc // no fence instructions available
856 #endif
857 CMPB internal∕cpu·X86+const_offsetX86HasRDTSCP(SB), $1
858 JNE fences
859 // Instruction stream serializing RDTSCP is supported.
860 // RDTSCP is supported by Intel Nehalem (2008) and
861 // AMD K8 Rev. F (2006) and newer.
862 RDTSCP
863 done:
864 MOVL AX, ret_lo+0(FP)
865 MOVL DX, ret_hi+4(FP)
866 RET
867 fences:
868 // MFENCE is instruction stream serializing and flushes the
869 // store buffers on AMD. The serialization semantics of LFENCE on AMD
870 // are dependent on MSR C001_1029 and CPU generation.
871 // LFENCE on Intel does wait for all previous instructions to have executed.
872 // Intel recommends MFENCE;LFENCE in its manuals before RDTSC to have all
873 // previous instructions executed and all previous loads and stores to globally visible.
874 // Using MFENCE;LFENCE here aligns the serializing properties without
875 // runtime detection of CPU manufacturer.
876 MFENCE
877 LFENCE
878 rdtsc:
879 RDTSC
880 JMP done
881
882 #ifndef GOOS_windows
883 TEXT ldt0setup<>(SB),NOSPLIT,$16-0
884 // set up ldt 7 to point at m0.tls
885 // ldt 1 would be fine on Linux, but on OS X, 7 is as low as we can go.
886 // the entry number is just a hint. setldt will set up GS with what it used.
887 MOVL $7, 0(SP)
888 LEAL runtime·m0+m_tls(SB), AX
889 MOVL AX, 4(SP)
890 MOVL $32, 8(SP) // sizeof(tls array)
891 CALL runtime·setldt(SB)
892 RET
893 #endif
894
895 TEXT runtime·emptyfunc(SB),0,$0-0
896 RET
897
898 // Called from cgo wrappers, this function returns g->m->curg.stack.hi.
899 // Must obey the gcc calling convention.
900 TEXT _cgo_topofstack(SB),NOSPLIT,$0
901 get_tls(CX)
902 MOVL g(CX), AX
903 MOVL g_m(AX), AX
904 MOVL m_curg(AX), AX
905 MOVL (g_stack+stack_hi)(AX), AX
906 RET
907
908 // The top-most function running on a goroutine
909 // returns to goexit+PCQuantum.
910 TEXT runtime·goexit(SB),NOSPLIT|TOPFRAME,$0-0
911 BYTE $0x90 // NOP
912 CALL runtime·goexit1(SB) // does not return
913 // traceback from goexit1 must hit code range of goexit
914 BYTE $0x90 // NOP
915
916 // Add a module's moduledata to the linked list of moduledata objects. This
917 // is called from .init_array by a function generated in the linker and so
918 // follows the platform ABI wrt register preservation -- it only touches AX,
919 // CX (implicitly) and DX, but it does not follow the ABI wrt arguments:
920 // instead the pointer to the moduledata is passed in AX.
921 TEXT runtime·addmoduledata(SB),NOSPLIT,$0-0
922 MOVL runtime·lastmoduledatap(SB), DX
923 MOVL AX, moduledata_next(DX)
924 MOVL AX, runtime·lastmoduledatap(SB)
925 RET
926
927 TEXT runtime·uint32tofloat64(SB),NOSPLIT,$8-12
928 MOVL a+0(FP), AX
929 MOVL AX, 0(SP)
930 MOVL $0, 4(SP)
931 FMOVV 0(SP), F0
932 FMOVDP F0, ret+4(FP)
933 RET
934
935 TEXT runtime·float64touint32(SB),NOSPLIT,$12-12
936 FMOVD a+0(FP), F0
937 FSTCW 0(SP)
938 FLDCW runtime·controlWord64trunc(SB)
939 FMOVVP F0, 4(SP)
940 FLDCW 0(SP)
941 MOVL 4(SP), AX
942 MOVL AX, ret+8(FP)
943 RET
944
945 // gcWriteBarrier informs the GC about heap pointer writes.
946 //
947 // gcWriteBarrier returns space in a write barrier buffer which
948 // should be filled in by the caller.
949 // gcWriteBarrier does NOT follow the Go ABI. It accepts the
950 // number of bytes of buffer needed in DI, and returns a pointer
951 // to the buffer space in DI.
952 // It clobbers FLAGS. It does not clobber any general-purpose registers,
953 // but may clobber others (e.g., SSE registers).
954 // Typical use would be, when doing *(CX+88) = AX
955 // CMPL $0, runtime.writeBarrier(SB)
956 // JEQ dowrite
957 // CALL runtime.gcBatchBarrier2(SB)
958 // MOVL AX, (DI)
959 // MOVL 88(CX), DX
960 // MOVL DX, 4(DI)
961 // dowrite:
962 // MOVL AX, 88(CX)
963 TEXT gcWriteBarrier<>(SB),NOSPLIT,$28
964 // Save the registers clobbered by the fast path. This is slightly
965 // faster than having the caller spill these.
966 MOVL CX, 20(SP)
967 MOVL BX, 24(SP)
968 retry:
969 // TODO: Consider passing g.m.p in as an argument so they can be shared
970 // across a sequence of write barriers.
971 get_tls2(BX, CX) // Preserve AX by using the saved CX as scratch.
972 MOVL g(BX), BX
973 MOVL g_m(BX), BX
974 MOVL m_p(BX), BX
975 // Get current buffer write position.
976 MOVL (p_wbBuf+wbBuf_next)(BX), CX // original next position
977 ADDL DI, CX // new next position
978 // Is the buffer full?
979 CMPL CX, (p_wbBuf+wbBuf_end)(BX)
980 JA flush
981 // Commit to the larger buffer.
982 MOVL CX, (p_wbBuf+wbBuf_next)(BX)
983 // Make return value (the original next position)
984 SUBL DI, CX
985 MOVL CX, DI
986 // Restore registers.
987 MOVL 20(SP), CX
988 MOVL 24(SP), BX
989 RET
990
991 flush:
992 // Save all general purpose registers since these could be
993 // clobbered by wbBufFlush and were not saved by the caller.
994 MOVL DI, 0(SP)
995 MOVL AX, 4(SP)
996 // BX already saved
997 // CX already saved
998 MOVL DX, 8(SP)
999 MOVL BP, 12(SP)
1000 MOVL SI, 16(SP)
1001 // DI already saved
1002
1003 CALL runtime·wbBufFlush(SB)
1004
1005 MOVL 0(SP), DI
1006 MOVL 4(SP), AX
1007 MOVL 8(SP), DX
1008 MOVL 12(SP), BP
1009 MOVL 16(SP), SI
1010 JMP retry
1011
1012 TEXT runtime·gcWriteBarrier1<ABIInternal>(SB),NOSPLIT,$0
1013 MOVL $4, DI
1014 JMP gcWriteBarrier<>(SB)
1015 TEXT runtime·gcWriteBarrier2<ABIInternal>(SB),NOSPLIT,$0
1016 MOVL $8, DI
1017 JMP gcWriteBarrier<>(SB)
1018 TEXT runtime·gcWriteBarrier3<ABIInternal>(SB),NOSPLIT,$0
1019 MOVL $12, DI
1020 JMP gcWriteBarrier<>(SB)
1021 TEXT runtime·gcWriteBarrier4<ABIInternal>(SB),NOSPLIT,$0
1022 MOVL $16, DI
1023 JMP gcWriteBarrier<>(SB)
1024 TEXT runtime·gcWriteBarrier5<ABIInternal>(SB),NOSPLIT,$0
1025 MOVL $20, DI
1026 JMP gcWriteBarrier<>(SB)
1027 TEXT runtime·gcWriteBarrier6<ABIInternal>(SB),NOSPLIT,$0
1028 MOVL $24, DI
1029 JMP gcWriteBarrier<>(SB)
1030 TEXT runtime·gcWriteBarrier7<ABIInternal>(SB),NOSPLIT,$0
1031 MOVL $28, DI
1032 JMP gcWriteBarrier<>(SB)
1033 TEXT runtime·gcWriteBarrier8<ABIInternal>(SB),NOSPLIT,$0
1034 MOVL $32, DI
1035 JMP gcWriteBarrier<>(SB)
1036
1037 TEXT runtime·panicBounds<ABIInternal>(SB),NOSPLIT,$40-0
1038 NO_LOCAL_POINTERS
1039 // Save all int registers that could have an index in them.
1040 // They may be pointers, but if they are they are dead.
1041 MOVL AX, 8(SP)
1042 MOVL CX, 12(SP)
1043 MOVL DX, 16(SP)
1044 MOVL BX, 20(SP)
1045 // skip SP @ 24(SP)
1046 MOVL BP, 28(SP)
1047 MOVL SI, 32(SP)
1048 MOVL DI, 36(SP)
1049
1050 MOVL SP, AX // hide SP read from vet
1051 MOVL 40(AX), AX // PC immediately after call to panicBounds
1052 MOVL AX, 0(SP)
1053 LEAL 8(SP), AX
1054 MOVL AX, 4(SP)
1055 CALL runtime·panicBounds32<ABIInternal>(SB)
1056 RET
1057
1058 TEXT runtime·panicExtend<ABIInternal>(SB),NOSPLIT,$40-0
1059 NO_LOCAL_POINTERS
1060 // Save all int registers that could have an index in them.
1061 // They may be pointers, but if they are they are dead.
1062 MOVL AX, 8(SP)
1063 MOVL CX, 12(SP)
1064 MOVL DX, 16(SP)
1065 MOVL BX, 20(SP)
1066 // skip SP @ 24(SP)
1067 MOVL BP, 28(SP)
1068 MOVL SI, 32(SP)
1069 MOVL DI, 36(SP)
1070
1071 MOVL SP, AX // hide SP read from vet
1072 MOVL 40(AX), AX // PC immediately after call to panicExtend
1073 MOVL AX, 0(SP)
1074 LEAL 8(SP), AX
1075 MOVL AX, 4(SP)
1076 CALL runtime·panicBounds32X<ABIInternal>(SB)
1077 RET
1078
1079 #ifdef GOOS_android
1080 // Use the free TLS_SLOT_APP slot #2 on Android Q.
1081 // Earlier androids are set up in gcc_android.c.
1082 DATA runtime·tls_g+0(SB)/4, $8
1083 GLOBL runtime·tls_g+0(SB), NOPTR, $4
1084 #endif
1085 #ifdef GOOS_windows
1086 DATA runtime·tls_g+0(SB)/4, $0
1087 GLOBL runtime·tls_g+0(SB), NOPTR, $4
1088 #endif
1089
View as plain text