]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/asm_s390x.s
Revert "runtime/cgo: store M for C-created thread in pthread key"
[gostls13.git] / src / runtime / asm_s390x.s
1 // Copyright 2016 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_s390x_lib is common startup code for s390x systems when
11 // using -buildmode=c-archive or -buildmode=c-shared. The linker will
12 // arrange to invoke this function as a global constructor (for
13 // c-archive) or when the shared library is loaded (for c-shared).
14 // We expect argc and argv to be passed in the usual C ABI registers
15 // R2 and R3.
16 TEXT _rt0_s390x_lib(SB), NOSPLIT|NOFRAME, $0
17         STMG    R6, R15, 48(R15)
18         MOVD    R2, _rt0_s390x_lib_argc<>(SB)
19         MOVD    R3, _rt0_s390x_lib_argv<>(SB)
20
21         // Save R6-R15 in the register save area of the calling function.
22         STMG    R6, R15, 48(R15)
23
24         // Allocate 80 bytes on the stack.
25         MOVD    $-80(R15), R15
26
27         // Save F8-F15 in our stack frame.
28         FMOVD   F8, 16(R15)
29         FMOVD   F9, 24(R15)
30         FMOVD   F10, 32(R15)
31         FMOVD   F11, 40(R15)
32         FMOVD   F12, 48(R15)
33         FMOVD   F13, 56(R15)
34         FMOVD   F14, 64(R15)
35         FMOVD   F15, 72(R15)
36
37         // Synchronous initialization.
38         MOVD    $runtime·libpreinit(SB), R1
39         BL      R1
40
41         // Create a new thread to finish Go runtime initialization.
42         MOVD    _cgo_sys_thread_create(SB), R1
43         CMP     R1, $0
44         BEQ     nocgo
45         MOVD    $_rt0_s390x_lib_go(SB), R2
46         MOVD    $0, R3
47         BL      R1
48         BR      restore
49
50 nocgo:
51         MOVD    $0x800000, R1              // stacksize
52         MOVD    R1, 0(R15)
53         MOVD    $_rt0_s390x_lib_go(SB), R1
54         MOVD    R1, 8(R15)                 // fn
55         MOVD    $runtime·newosproc(SB), R1
56         BL      R1
57
58 restore:
59         // Restore F8-F15 from our stack frame.
60         FMOVD   16(R15), F8
61         FMOVD   24(R15), F9
62         FMOVD   32(R15), F10
63         FMOVD   40(R15), F11
64         FMOVD   48(R15), F12
65         FMOVD   56(R15), F13
66         FMOVD   64(R15), F14
67         FMOVD   72(R15), F15
68         MOVD    $80(R15), R15
69
70         // Restore R6-R15.
71         LMG     48(R15), R6, R15
72         RET
73
74 // _rt0_s390x_lib_go initializes the Go runtime.
75 // This is started in a separate thread by _rt0_s390x_lib.
76 TEXT _rt0_s390x_lib_go(SB), NOSPLIT|NOFRAME, $0
77         MOVD    _rt0_s390x_lib_argc<>(SB), R2
78         MOVD    _rt0_s390x_lib_argv<>(SB), R3
79         MOVD    $runtime·rt0_go(SB), R1
80         BR      R1
81
82 DATA _rt0_s390x_lib_argc<>(SB)/8, $0
83 GLOBL _rt0_s390x_lib_argc<>(SB), NOPTR, $8
84 DATA _rt0_s90x_lib_argv<>(SB)/8, $0
85 GLOBL _rt0_s390x_lib_argv<>(SB), NOPTR, $8
86
87 TEXT runtime·rt0_go(SB),NOSPLIT|TOPFRAME,$0
88         // R2 = argc; R3 = argv; R11 = temp; R13 = g; R15 = stack pointer
89         // C TLS base pointer in AR0:AR1
90
91         // initialize essential registers
92         XOR     R0, R0
93
94         SUB     $24, R15
95         MOVW    R2, 8(R15) // argc
96         MOVD    R3, 16(R15) // argv
97
98         // create istack out of the given (operating system) stack.
99         // _cgo_init may update stackguard.
100         MOVD    $runtime·g0(SB), g
101         MOVD    R15, R11
102         SUB     $(64*1024), R11
103         MOVD    R11, g_stackguard0(g)
104         MOVD    R11, g_stackguard1(g)
105         MOVD    R11, (g_stack+stack_lo)(g)
106         MOVD    R15, (g_stack+stack_hi)(g)
107
108         // if there is a _cgo_init, call it using the gcc ABI.
109         MOVD    _cgo_init(SB), R11
110         CMPBEQ  R11, $0, nocgo
111         MOVW    AR0, R4                 // (AR0 << 32 | AR1) is the TLS base pointer; MOVD is translated to EAR
112         SLD     $32, R4, R4
113         MOVW    AR1, R4                 // arg 2: TLS base pointer
114         MOVD    $setg_gcc<>(SB), R3     // arg 1: setg
115         MOVD    g, R2                   // arg 0: G
116         // C functions expect 160 bytes of space on caller stack frame
117         // and an 8-byte aligned stack pointer
118         MOVD    R15, R9                 // save current stack (R9 is preserved in the Linux ABI)
119         SUB     $160, R15               // reserve 160 bytes
120         MOVD    $~7, R6
121         AND     R6, R15                 // 8-byte align
122         BL      R11                     // this call clobbers volatile registers according to Linux ABI (R0-R5, R14)
123         MOVD    R9, R15                 // restore stack
124         XOR     R0, R0                  // zero R0
125
126 nocgo:
127         // update stackguard after _cgo_init
128         MOVD    (g_stack+stack_lo)(g), R2
129         ADD     $const_stackGuard, R2
130         MOVD    R2, g_stackguard0(g)
131         MOVD    R2, g_stackguard1(g)
132
133         // set the per-goroutine and per-mach "registers"
134         MOVD    $runtime·m0(SB), R2
135
136         // save m->g0 = g0
137         MOVD    g, m_g0(R2)
138         // save m0 to g0->m
139         MOVD    R2, g_m(g)
140
141         BL      runtime·check(SB)
142
143         // argc/argv are already prepared on stack
144         BL      runtime·args(SB)
145         BL      runtime·osinit(SB)
146         BL      runtime·schedinit(SB)
147
148         // create a new goroutine to start program
149         MOVD    $runtime·mainPC(SB), R2                // entry
150         SUB     $16, R15
151         MOVD    R2, 8(R15)
152         MOVD    $0, 0(R15)
153         BL      runtime·newproc(SB)
154         ADD     $16, R15
155
156         // start this M
157         BL      runtime·mstart(SB)
158
159         MOVD    $0, 1(R0)
160         RET
161
162 DATA    runtime·mainPC+0(SB)/8,$runtime·main(SB)
163 GLOBL   runtime·mainPC(SB),RODATA,$8
164
165 TEXT runtime·breakpoint(SB),NOSPLIT|NOFRAME,$0-0
166         BRRK
167         RET
168
169 TEXT runtime·asminit(SB),NOSPLIT|NOFRAME,$0-0
170         RET
171
172 TEXT runtime·mstart(SB),NOSPLIT|TOPFRAME,$0
173         CALL    runtime·mstart0(SB)
174         RET // not reached
175
176 /*
177  *  go-routine
178  */
179
180 // void gogo(Gobuf*)
181 // restore state from Gobuf; longjmp
182 TEXT runtime·gogo(SB), NOSPLIT|NOFRAME, $0-8
183         MOVD    buf+0(FP), R5
184         MOVD    gobuf_g(R5), R6
185         MOVD    0(R6), R7       // make sure g != nil
186         BR      gogo<>(SB)
187
188 TEXT gogo<>(SB), NOSPLIT|NOFRAME, $0
189         MOVD    R6, g
190         BL      runtime·save_g(SB)
191
192         MOVD    0(g), R4
193         MOVD    gobuf_sp(R5), R15
194         MOVD    gobuf_lr(R5), LR
195         MOVD    gobuf_ret(R5), R3
196         MOVD    gobuf_ctxt(R5), R12
197         MOVD    $0, gobuf_sp(R5)
198         MOVD    $0, gobuf_ret(R5)
199         MOVD    $0, gobuf_lr(R5)
200         MOVD    $0, gobuf_ctxt(R5)
201         CMP     R0, R0 // set condition codes for == test, needed by stack split
202         MOVD    gobuf_pc(R5), R6
203         BR      (R6)
204
205 // void mcall(fn func(*g))
206 // Switch to m->g0's stack, call fn(g).
207 // Fn must never return.  It should gogo(&g->sched)
208 // to keep running g.
209 TEXT runtime·mcall(SB), NOSPLIT, $-8-8
210         // Save caller state in g->sched
211         MOVD    R15, (g_sched+gobuf_sp)(g)
212         MOVD    LR, (g_sched+gobuf_pc)(g)
213         MOVD    $0, (g_sched+gobuf_lr)(g)
214
215         // Switch to m->g0 & its stack, call fn.
216         MOVD    g, R3
217         MOVD    g_m(g), R8
218         MOVD    m_g0(R8), g
219         BL      runtime·save_g(SB)
220         CMP     g, R3
221         BNE     2(PC)
222         BR      runtime·badmcall(SB)
223         MOVD    fn+0(FP), R12                   // context
224         MOVD    0(R12), R4                      // code pointer
225         MOVD    (g_sched+gobuf_sp)(g), R15      // sp = m->g0->sched.sp
226         SUB     $16, R15
227         MOVD    R3, 8(R15)
228         MOVD    $0, 0(R15)
229         BL      (R4)
230         BR      runtime·badmcall2(SB)
231
232 // systemstack_switch is a dummy routine that systemstack leaves at the bottom
233 // of the G stack.  We need to distinguish the routine that
234 // lives at the bottom of the G stack from the one that lives
235 // at the top of the system stack because the one at the top of
236 // the system stack terminates the stack walk (see topofstack()).
237 TEXT runtime·systemstack_switch(SB), NOSPLIT, $0-0
238         UNDEF
239         BL      (LR)    // make sure this function is not leaf
240         RET
241
242 // func systemstack(fn func())
243 TEXT runtime·systemstack(SB), NOSPLIT, $0-8
244         MOVD    fn+0(FP), R3    // R3 = fn
245         MOVD    R3, R12         // context
246         MOVD    g_m(g), R4      // R4 = m
247
248         MOVD    m_gsignal(R4), R5       // R5 = gsignal
249         CMPBEQ  g, R5, noswitch
250
251         MOVD    m_g0(R4), R5    // R5 = g0
252         CMPBEQ  g, R5, noswitch
253
254         MOVD    m_curg(R4), R6
255         CMPBEQ  g, R6, switch
256
257         // Bad: g is not gsignal, not g0, not curg. What is it?
258         // Hide call from linker nosplit analysis.
259         MOVD    $runtime·badsystemstack(SB), R3
260         BL      (R3)
261         BL      runtime·abort(SB)
262
263 switch:
264         // save our state in g->sched.  Pretend to
265         // be systemstack_switch if the G stack is scanned.
266         BL      gosave_systemstack_switch<>(SB)
267
268         // switch to g0
269         MOVD    R5, g
270         BL      runtime·save_g(SB)
271         MOVD    (g_sched+gobuf_sp)(g), R15
272
273         // call target function
274         MOVD    0(R12), R3      // code pointer
275         BL      (R3)
276
277         // switch back to g
278         MOVD    g_m(g), R3
279         MOVD    m_curg(R3), g
280         BL      runtime·save_g(SB)
281         MOVD    (g_sched+gobuf_sp)(g), R15
282         MOVD    $0, (g_sched+gobuf_sp)(g)
283         RET
284
285 noswitch:
286         // already on m stack, just call directly
287         // Using a tail call here cleans up tracebacks since we won't stop
288         // at an intermediate systemstack.
289         MOVD    0(R12), R3      // code pointer
290         MOVD    0(R15), LR      // restore LR
291         ADD     $8, R15
292         BR      (R3)
293
294 /*
295  * support for morestack
296  */
297
298 // Called during function prolog when more stack is needed.
299 // Caller has already loaded:
300 // R3: framesize, R4: argsize, R5: LR
301 //
302 // The traceback routines see morestack on a g0 as being
303 // the top of a stack (for example, morestack calling newstack
304 // calling the scheduler calling newm calling gc), so we must
305 // record an argument size. For that purpose, it has no arguments.
306 TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0
307         // Cannot grow scheduler stack (m->g0).
308         MOVD    g_m(g), R7
309         MOVD    m_g0(R7), R8
310         CMPBNE  g, R8, 3(PC)
311         BL      runtime·badmorestackg0(SB)
312         BL      runtime·abort(SB)
313
314         // Cannot grow signal stack (m->gsignal).
315         MOVD    m_gsignal(R7), R8
316         CMP     g, R8
317         BNE     3(PC)
318         BL      runtime·badmorestackgsignal(SB)
319         BL      runtime·abort(SB)
320
321         // Called from f.
322         // Set g->sched to context in f.
323         MOVD    R15, (g_sched+gobuf_sp)(g)
324         MOVD    LR, R8
325         MOVD    R8, (g_sched+gobuf_pc)(g)
326         MOVD    R5, (g_sched+gobuf_lr)(g)
327         MOVD    R12, (g_sched+gobuf_ctxt)(g)
328
329         // Called from f.
330         // Set m->morebuf to f's caller.
331         MOVD    R5, (m_morebuf+gobuf_pc)(R7)    // f's caller's PC
332         MOVD    R15, (m_morebuf+gobuf_sp)(R7)   // f's caller's SP
333         MOVD    g, (m_morebuf+gobuf_g)(R7)
334
335         // Call newstack on m->g0's stack.
336         MOVD    m_g0(R7), g
337         BL      runtime·save_g(SB)
338         MOVD    (g_sched+gobuf_sp)(g), R15
339         // Create a stack frame on g0 to call newstack.
340         MOVD    $0, -8(R15)     // Zero saved LR in frame
341         SUB     $8, R15
342         BL      runtime·newstack(SB)
343
344         // Not reached, but make sure the return PC from the call to newstack
345         // is still in this function, and not the beginning of the next.
346         UNDEF
347
348 TEXT runtime·morestack_noctxt(SB),NOSPLIT|NOFRAME,$0-0
349         // Force SPWRITE. This function doesn't actually write SP,
350         // but it is called with a special calling convention where
351         // the caller doesn't save LR on stack but passes it as a
352         // register (R5), and the unwinder currently doesn't understand.
353         // Make it SPWRITE to stop unwinding. (See issue 54332)
354         MOVD    R15, R15
355
356         MOVD    $0, R12
357         BR      runtime·morestack(SB)
358
359 // reflectcall: call a function with the given argument list
360 // func call(stackArgsType *_type, f *FuncVal, stackArgs *byte, stackArgsSize, stackRetOffset, frameSize uint32, regArgs *abi.RegArgs).
361 // we don't have variable-sized frames, so we use a small number
362 // of constant-sized-frame functions to encode a few bits of size in the pc.
363 // Caution: ugly multiline assembly macros in your future!
364
365 #define DISPATCH(NAME,MAXSIZE)          \
366         MOVD    $MAXSIZE, R4;           \
367         CMP     R3, R4;         \
368         BGT     3(PC);                  \
369         MOVD    $NAME(SB), R5;  \
370         BR      (R5)
371 // Note: can't just "BR NAME(SB)" - bad inlining results.
372
373 TEXT ·reflectcall(SB), NOSPLIT, $-8-48
374         MOVWZ   frameSize+32(FP), R3
375         DISPATCH(runtime·call16, 16)
376         DISPATCH(runtime·call32, 32)
377         DISPATCH(runtime·call64, 64)
378         DISPATCH(runtime·call128, 128)
379         DISPATCH(runtime·call256, 256)
380         DISPATCH(runtime·call512, 512)
381         DISPATCH(runtime·call1024, 1024)
382         DISPATCH(runtime·call2048, 2048)
383         DISPATCH(runtime·call4096, 4096)
384         DISPATCH(runtime·call8192, 8192)
385         DISPATCH(runtime·call16384, 16384)
386         DISPATCH(runtime·call32768, 32768)
387         DISPATCH(runtime·call65536, 65536)
388         DISPATCH(runtime·call131072, 131072)
389         DISPATCH(runtime·call262144, 262144)
390         DISPATCH(runtime·call524288, 524288)
391         DISPATCH(runtime·call1048576, 1048576)
392         DISPATCH(runtime·call2097152, 2097152)
393         DISPATCH(runtime·call4194304, 4194304)
394         DISPATCH(runtime·call8388608, 8388608)
395         DISPATCH(runtime·call16777216, 16777216)
396         DISPATCH(runtime·call33554432, 33554432)
397         DISPATCH(runtime·call67108864, 67108864)
398         DISPATCH(runtime·call134217728, 134217728)
399         DISPATCH(runtime·call268435456, 268435456)
400         DISPATCH(runtime·call536870912, 536870912)
401         DISPATCH(runtime·call1073741824, 1073741824)
402         MOVD    $runtime·badreflectcall(SB), R5
403         BR      (R5)
404
405 #define CALLFN(NAME,MAXSIZE)                    \
406 TEXT NAME(SB), WRAPPER, $MAXSIZE-48;            \
407         NO_LOCAL_POINTERS;                      \
408         /* copy arguments to stack */           \
409         MOVD    stackArgs+16(FP), R4;                   \
410         MOVWZ   stackArgsSize+24(FP), R5;               \
411         MOVD    $stack-MAXSIZE(SP), R6;         \
412 loopArgs: /* copy 256 bytes at a time */        \
413         CMP     R5, $256;                       \
414         BLT     tailArgs;                       \
415         SUB     $256, R5;                       \
416         MVC     $256, 0(R4), 0(R6);             \
417         MOVD    $256(R4), R4;                   \
418         MOVD    $256(R6), R6;                   \
419         BR      loopArgs;                       \
420 tailArgs: /* copy remaining bytes */            \
421         CMP     R5, $0;                         \
422         BEQ     callFunction;                   \
423         SUB     $1, R5;                         \
424         EXRL    $callfnMVC<>(SB), R5;           \
425 callFunction:                                   \
426         MOVD    f+8(FP), R12;                   \
427         MOVD    (R12), R8;                      \
428         PCDATA  $PCDATA_StackMapIndex, $0;      \
429         BL      (R8);                           \
430         /* copy return values back */           \
431         MOVD    stackArgsType+0(FP), R7;                \
432         MOVD    stackArgs+16(FP), R6;                   \
433         MOVWZ   stackArgsSize+24(FP), R5;                       \
434         MOVD    $stack-MAXSIZE(SP), R4;         \
435         MOVWZ   stackRetOffset+28(FP), R1;              \
436         ADD     R1, R4;                         \
437         ADD     R1, R6;                         \
438         SUB     R1, R5;                         \
439         BL      callRet<>(SB);                  \
440         RET
441
442 // callRet copies return values back at the end of call*. This is a
443 // separate function so it can allocate stack space for the arguments
444 // to reflectcallmove. It does not follow the Go ABI; it expects its
445 // arguments in registers.
446 TEXT callRet<>(SB), NOSPLIT, $40-0
447         MOVD    R7, 8(R15)
448         MOVD    R6, 16(R15)
449         MOVD    R4, 24(R15)
450         MOVD    R5, 32(R15)
451         MOVD    $0, 40(R15)
452         BL      runtime·reflectcallmove(SB)
453         RET
454
455 CALLFN(·call16, 16)
456 CALLFN(·call32, 32)
457 CALLFN(·call64, 64)
458 CALLFN(·call128, 128)
459 CALLFN(·call256, 256)
460 CALLFN(·call512, 512)
461 CALLFN(·call1024, 1024)
462 CALLFN(·call2048, 2048)
463 CALLFN(·call4096, 4096)
464 CALLFN(·call8192, 8192)
465 CALLFN(·call16384, 16384)
466 CALLFN(·call32768, 32768)
467 CALLFN(·call65536, 65536)
468 CALLFN(·call131072, 131072)
469 CALLFN(·call262144, 262144)
470 CALLFN(·call524288, 524288)
471 CALLFN(·call1048576, 1048576)
472 CALLFN(·call2097152, 2097152)
473 CALLFN(·call4194304, 4194304)
474 CALLFN(·call8388608, 8388608)
475 CALLFN(·call16777216, 16777216)
476 CALLFN(·call33554432, 33554432)
477 CALLFN(·call67108864, 67108864)
478 CALLFN(·call134217728, 134217728)
479 CALLFN(·call268435456, 268435456)
480 CALLFN(·call536870912, 536870912)
481 CALLFN(·call1073741824, 1073741824)
482
483 // Not a function: target for EXRL (execute relative long) instruction.
484 TEXT callfnMVC<>(SB),NOSPLIT|NOFRAME,$0-0
485         MVC     $1, 0(R4), 0(R6)
486
487 TEXT runtime·procyield(SB),NOSPLIT,$0-0
488         RET
489
490 // Save state of caller into g->sched,
491 // but using fake PC from systemstack_switch.
492 // Must only be called from functions with no locals ($0)
493 // or else unwinding from systemstack_switch is incorrect.
494 // Smashes R1.
495 TEXT gosave_systemstack_switch<>(SB),NOSPLIT|NOFRAME,$0
496         MOVD    $runtime·systemstack_switch(SB), R1
497         ADD     $16, R1 // get past prologue
498         MOVD    R1, (g_sched+gobuf_pc)(g)
499         MOVD    R15, (g_sched+gobuf_sp)(g)
500         MOVD    $0, (g_sched+gobuf_lr)(g)
501         MOVD    $0, (g_sched+gobuf_ret)(g)
502         // Assert ctxt is zero. See func save.
503         MOVD    (g_sched+gobuf_ctxt)(g), R1
504         CMPBEQ  R1, $0, 2(PC)
505         BL      runtime·abort(SB)
506         RET
507
508 // func asmcgocall(fn, arg unsafe.Pointer) int32
509 // Call fn(arg) on the scheduler stack,
510 // aligned appropriately for the gcc ABI.
511 // See cgocall.go for more details.
512 TEXT ·asmcgocall(SB),NOSPLIT,$0-20
513         // R2 = argc; R3 = argv; R11 = temp; R13 = g; R15 = stack pointer
514         // C TLS base pointer in AR0:AR1
515         MOVD    fn+0(FP), R3
516         MOVD    arg+8(FP), R4
517
518         MOVD    R15, R2         // save original stack pointer
519         MOVD    g, R5
520
521         // Figure out if we need to switch to m->g0 stack.
522         // We get called to create new OS threads too, and those
523         // come in on the m->g0 stack already. Or we might already
524         // be on the m->gsignal stack.
525         MOVD    g_m(g), R6
526         MOVD    m_gsignal(R6), R7
527         CMPBEQ  R7, g, g0
528         MOVD    m_g0(R6), R7
529         CMPBEQ  R7, g, g0
530         BL      gosave_systemstack_switch<>(SB)
531         MOVD    R7, g
532         BL      runtime·save_g(SB)
533         MOVD    (g_sched+gobuf_sp)(g), R15
534
535         // Now on a scheduling stack (a pthread-created stack).
536 g0:
537         // Save room for two of our pointers, plus 160 bytes of callee
538         // save area that lives on the caller stack.
539         SUB     $176, R15
540         MOVD    $~7, R6
541         AND     R6, R15                 // 8-byte alignment for gcc ABI
542         MOVD    R5, 168(R15)             // save old g on stack
543         MOVD    (g_stack+stack_hi)(R5), R5
544         SUB     R2, R5
545         MOVD    R5, 160(R15)             // save depth in old g stack (can't just save SP, as stack might be copied during a callback)
546         MOVD    $0, 0(R15)              // clear back chain pointer (TODO can we give it real back trace information?)
547         MOVD    R4, R2                  // arg in R2
548         BL      R3                      // can clobber: R0-R5, R14, F0-F3, F5, F7-F15
549
550         XOR     R0, R0                  // set R0 back to 0.
551         // Restore g, stack pointer.
552         MOVD    168(R15), g
553         BL      runtime·save_g(SB)
554         MOVD    (g_stack+stack_hi)(g), R5
555         MOVD    160(R15), R6
556         SUB     R6, R5
557         MOVD    R5, R15
558
559         MOVW    R2, ret+16(FP)
560         RET
561
562 // cgocallback(fn, frame unsafe.Pointer, ctxt uintptr)
563 // See cgocall.go for more details.
564 TEXT ·cgocallback(SB),NOSPLIT,$24-24
565         NO_LOCAL_POINTERS
566
567         // Load m and g from thread-local storage.
568         MOVB    runtime·iscgo(SB), R3
569         CMPBEQ  R3, $0, nocgo
570         BL      runtime·load_g(SB)
571
572 nocgo:
573         // If g is nil, Go did not create the current thread.
574         // Call needm to obtain one for temporary use.
575         // In this case, we're running on the thread stack, so there's
576         // lots of space, but the linker doesn't know. Hide the call from
577         // the linker analysis by using an indirect call.
578         CMPBEQ  g, $0, needm
579
580         MOVD    g_m(g), R8
581         MOVD    R8, savedm-8(SP)
582         BR      havem
583
584 needm:
585         MOVD    g, savedm-8(SP) // g is zero, so is m.
586         MOVD    $runtime·needm(SB), R3
587         BL      (R3)
588
589         // Set m->sched.sp = SP, so that if a panic happens
590         // during the function we are about to execute, it will
591         // have a valid SP to run on the g0 stack.
592         // The next few lines (after the havem label)
593         // will save this SP onto the stack and then write
594         // the same SP back to m->sched.sp. That seems redundant,
595         // but if an unrecovered panic happens, unwindm will
596         // restore the g->sched.sp from the stack location
597         // and then systemstack will try to use it. If we don't set it here,
598         // that restored SP will be uninitialized (typically 0) and
599         // will not be usable.
600         MOVD    g_m(g), R8
601         MOVD    m_g0(R8), R3
602         MOVD    R15, (g_sched+gobuf_sp)(R3)
603
604 havem:
605         // Now there's a valid m, and we're running on its m->g0.
606         // Save current m->g0->sched.sp on stack and then set it to SP.
607         // Save current sp in m->g0->sched.sp in preparation for
608         // switch back to m->curg stack.
609         // NOTE: unwindm knows that the saved g->sched.sp is at 8(R1) aka savedsp-16(SP).
610         MOVD    m_g0(R8), R3
611         MOVD    (g_sched+gobuf_sp)(R3), R4
612         MOVD    R4, savedsp-24(SP)      // must match frame size
613         MOVD    R15, (g_sched+gobuf_sp)(R3)
614
615         // Switch to m->curg stack and call runtime.cgocallbackg.
616         // Because we are taking over the execution of m->curg
617         // but *not* resuming what had been running, we need to
618         // save that information (m->curg->sched) so we can restore it.
619         // We can restore m->curg->sched.sp easily, because calling
620         // runtime.cgocallbackg leaves SP unchanged upon return.
621         // To save m->curg->sched.pc, we push it onto the curg stack and
622         // open a frame the same size as cgocallback's g0 frame.
623         // Once we switch to the curg stack, the pushed PC will appear
624         // to be the return PC of cgocallback, so that the traceback
625         // will seamlessly trace back into the earlier calls.
626         MOVD    m_curg(R8), g
627         BL      runtime·save_g(SB)
628         MOVD    (g_sched+gobuf_sp)(g), R4 // prepare stack as R4
629         MOVD    (g_sched+gobuf_pc)(g), R5
630         MOVD    R5, -(24+8)(R4) // "saved LR"; must match frame size
631         // Gather our arguments into registers.
632         MOVD    fn+0(FP), R1
633         MOVD    frame+8(FP), R2
634         MOVD    ctxt+16(FP), R3
635         MOVD    $-(24+8)(R4), R15       // switch stack; must match frame size
636         MOVD    R1, 8(R15)
637         MOVD    R2, 16(R15)
638         MOVD    R3, 24(R15)
639         BL      runtime·cgocallbackg(SB)
640
641         // Restore g->sched (== m->curg->sched) from saved values.
642         MOVD    0(R15), R5
643         MOVD    R5, (g_sched+gobuf_pc)(g)
644         MOVD    $(24+8)(R15), R4        // must match frame size
645         MOVD    R4, (g_sched+gobuf_sp)(g)
646
647         // Switch back to m->g0's stack and restore m->g0->sched.sp.
648         // (Unlike m->curg, the g0 goroutine never uses sched.pc,
649         // so we do not have to restore it.)
650         MOVD    g_m(g), R8
651         MOVD    m_g0(R8), g
652         BL      runtime·save_g(SB)
653         MOVD    (g_sched+gobuf_sp)(g), R15
654         MOVD    savedsp-24(SP), R4      // must match frame size
655         MOVD    R4, (g_sched+gobuf_sp)(g)
656
657         // If the m on entry was nil, we called needm above to borrow an m
658         // for the duration of the call. Since the call is over, return it with dropm.
659         MOVD    savedm-8(SP), R6
660         CMPBNE  R6, $0, droppedm
661         MOVD    $runtime·dropm(SB), R3
662         BL      (R3)
663 droppedm:
664
665         // Done!
666         RET
667
668 // void setg(G*); set g. for use by needm.
669 TEXT runtime·setg(SB), NOSPLIT, $0-8
670         MOVD    gg+0(FP), g
671         // This only happens if iscgo, so jump straight to save_g
672         BL      runtime·save_g(SB)
673         RET
674
675 // void setg_gcc(G*); set g in C TLS.
676 // Must obey the gcc calling convention.
677 TEXT setg_gcc<>(SB),NOSPLIT|NOFRAME,$0-0
678         // The standard prologue clobbers LR (R14), which is callee-save in
679         // the C ABI, so we have to use NOFRAME and save LR ourselves.
680         MOVD    LR, R1
681         // Also save g, R10, and R11 since they're callee-save in C ABI
682         MOVD    R10, R3
683         MOVD    g, R4
684         MOVD    R11, R5
685
686         MOVD    R2, g
687         BL      runtime·save_g(SB)
688
689         MOVD    R5, R11
690         MOVD    R4, g
691         MOVD    R3, R10
692         MOVD    R1, LR
693         RET
694
695 TEXT runtime·abort(SB),NOSPLIT|NOFRAME,$0-0
696         MOVW    (R0), R0
697         UNDEF
698
699 // int64 runtime·cputicks(void)
700 TEXT runtime·cputicks(SB),NOSPLIT,$0-8
701         // The TOD clock on s390 counts from the year 1900 in ~250ps intervals.
702         // This means that since about 1972 the msb has been set, making the
703         // result of a call to STORE CLOCK (stck) a negative number.
704         // We clear the msb to make it positive.
705         STCK    ret+0(FP)      // serialises before and after call
706         MOVD    ret+0(FP), R3  // R3 will wrap to 0 in the year 2043
707         SLD     $1, R3
708         SRD     $1, R3
709         MOVD    R3, ret+0(FP)
710         RET
711
712 // AES hashing not implemented for s390x
713 TEXT runtime·memhash(SB),NOSPLIT|NOFRAME,$0-32
714         JMP     runtime·memhashFallback(SB)
715 TEXT runtime·strhash(SB),NOSPLIT|NOFRAME,$0-24
716         JMP     runtime·strhashFallback(SB)
717 TEXT runtime·memhash32(SB),NOSPLIT|NOFRAME,$0-24
718         JMP     runtime·memhash32Fallback(SB)
719 TEXT runtime·memhash64(SB),NOSPLIT|NOFRAME,$0-24
720         JMP     runtime·memhash64Fallback(SB)
721
722 TEXT runtime·return0(SB), NOSPLIT, $0
723         MOVW    $0, R3
724         RET
725
726 // Called from cgo wrappers, this function returns g->m->curg.stack.hi.
727 // Must obey the gcc calling convention.
728 TEXT _cgo_topofstack(SB),NOSPLIT|NOFRAME,$0
729         // g (R13), R10, R11 and LR (R14) are callee-save in the C ABI, so save them
730         MOVD    g, R1
731         MOVD    R10, R3
732         MOVD    LR, R4
733         MOVD    R11, R5
734
735         BL      runtime·load_g(SB)     // clobbers g (R13), R10, R11
736         MOVD    g_m(g), R2
737         MOVD    m_curg(R2), R2
738         MOVD    (g_stack+stack_hi)(R2), R2
739
740         MOVD    R1, g
741         MOVD    R3, R10
742         MOVD    R4, LR
743         MOVD    R5, R11
744         RET
745
746 // The top-most function running on a goroutine
747 // returns to goexit+PCQuantum.
748 TEXT runtime·goexit(SB),NOSPLIT|NOFRAME|TOPFRAME,$0-0
749         BYTE $0x07; BYTE $0x00; // 2-byte nop
750         BL      runtime·goexit1(SB)    // does not return
751         // traceback from goexit1 must hit code range of goexit
752         BYTE $0x07; BYTE $0x00; // 2-byte nop
753
754 TEXT ·publicationBarrier(SB),NOSPLIT|NOFRAME,$0-0
755         // Stores are already ordered on s390x, so this is just a
756         // compile barrier.
757         RET
758
759 // This is called from .init_array and follows the platform, not Go, ABI.
760 // We are overly conservative. We could only save the registers we use.
761 // However, since this function is only called once per loaded module
762 // performance is unimportant.
763 TEXT runtime·addmoduledata(SB),NOSPLIT|NOFRAME,$0-0
764         // Save R6-R15 in the register save area of the calling function.
765         // Don't bother saving F8-F15 as we aren't doing any calls.
766         STMG    R6, R15, 48(R15)
767
768         // append the argument (passed in R2, as per the ELF ABI) to the
769         // moduledata linked list.
770         MOVD    runtime·lastmoduledatap(SB), R1
771         MOVD    R2, moduledata_next(R1)
772         MOVD    R2, runtime·lastmoduledatap(SB)
773
774         // Restore R6-R15.
775         LMG     48(R15), R6, R15
776         RET
777
778 TEXT ·checkASM(SB),NOSPLIT,$0-1
779         MOVB    $1, ret+0(FP)
780         RET
781
782 // gcWriteBarrier informs the GC about heap pointer writes.
783 //
784 // gcWriteBarrier does NOT follow the Go ABI. It accepts the
785 // number of bytes of buffer needed in R9, and returns a pointer
786 // to the buffer space in R9.
787 // It clobbers R10 (the temp register) and R1 (used by PLT stub).
788 // It does not clobber any other general-purpose registers,
789 // but may clobber others (e.g., floating point registers).
790 TEXT gcWriteBarrier<>(SB),NOSPLIT,$96
791         // Save the registers clobbered by the fast path.
792         MOVD    R4, 96(R15)
793 retry:
794         MOVD    g_m(g), R1
795         MOVD    m_p(R1), R1
796         // Increment wbBuf.next position.
797         MOVD    R9, R4
798         ADD     (p_wbBuf+wbBuf_next)(R1), R4
799         // Is the buffer full?
800         MOVD    (p_wbBuf+wbBuf_end)(R1), R10
801         CMPUBGT R4, R10, flush
802         // Commit to the larger buffer.
803         MOVD    R4, (p_wbBuf+wbBuf_next)(R1)
804         // Make return value (the original next position)
805         SUB     R9, R4, R9
806         // Restore registers.
807         MOVD    96(R15), R4
808         RET
809
810 flush:
811         // Save all general purpose registers since these could be
812         // clobbered by wbBufFlush and were not saved by the caller.
813         STMG    R2, R3, 8(R15)
814         MOVD    R0, 24(R15)
815         // R1 already saved.
816         // R4 already saved.
817         STMG    R5, R12, 32(R15) // save R5 - R12
818         // R13 is g.
819         // R14 is LR.
820         // R15 is SP.
821
822         CALL    runtime·wbBufFlush(SB)
823
824         LMG     8(R15), R2, R3   // restore R2 - R3
825         MOVD    24(R15), R0      // restore R0
826         LMG     32(R15), R5, R12 // restore R5 - R12
827         JMP     retry
828
829 TEXT runtime·gcWriteBarrier1<ABIInternal>(SB),NOSPLIT,$0
830         MOVD    $8, R9
831         JMP     gcWriteBarrier<>(SB)
832 TEXT runtime·gcWriteBarrier2<ABIInternal>(SB),NOSPLIT,$0
833         MOVD    $16, R9
834         JMP     gcWriteBarrier<>(SB)
835 TEXT runtime·gcWriteBarrier3<ABIInternal>(SB),NOSPLIT,$0
836         MOVD    $24, R9
837         JMP     gcWriteBarrier<>(SB)
838 TEXT runtime·gcWriteBarrier4<ABIInternal>(SB),NOSPLIT,$0
839         MOVD    $32, R9
840         JMP     gcWriteBarrier<>(SB)
841 TEXT runtime·gcWriteBarrier5<ABIInternal>(SB),NOSPLIT,$0
842         MOVD    $40, R9
843         JMP     gcWriteBarrier<>(SB)
844 TEXT runtime·gcWriteBarrier6<ABIInternal>(SB),NOSPLIT,$0
845         MOVD    $48, R9
846         JMP     gcWriteBarrier<>(SB)
847 TEXT runtime·gcWriteBarrier7<ABIInternal>(SB),NOSPLIT,$0
848         MOVD    $56, R9
849         JMP     gcWriteBarrier<>(SB)
850 TEXT runtime·gcWriteBarrier8<ABIInternal>(SB),NOSPLIT,$0
851         MOVD    $64, R9
852         JMP     gcWriteBarrier<>(SB)
853
854 // Note: these functions use a special calling convention to save generated code space.
855 // Arguments are passed in registers, but the space for those arguments are allocated
856 // in the caller's stack frame. These stubs write the args into that stack space and
857 // then tail call to the corresponding runtime handler.
858 // The tail call makes these stubs disappear in backtraces.
859 TEXT runtime·panicIndex(SB),NOSPLIT,$0-16
860         MOVD    R0, x+0(FP)
861         MOVD    R1, y+8(FP)
862         JMP     runtime·goPanicIndex(SB)
863 TEXT runtime·panicIndexU(SB),NOSPLIT,$0-16
864         MOVD    R0, x+0(FP)
865         MOVD    R1, y+8(FP)
866         JMP     runtime·goPanicIndexU(SB)
867 TEXT runtime·panicSliceAlen(SB),NOSPLIT,$0-16
868         MOVD    R1, x+0(FP)
869         MOVD    R2, y+8(FP)
870         JMP     runtime·goPanicSliceAlen(SB)
871 TEXT runtime·panicSliceAlenU(SB),NOSPLIT,$0-16
872         MOVD    R1, x+0(FP)
873         MOVD    R2, y+8(FP)
874         JMP     runtime·goPanicSliceAlenU(SB)
875 TEXT runtime·panicSliceAcap(SB),NOSPLIT,$0-16
876         MOVD    R1, x+0(FP)
877         MOVD    R2, y+8(FP)
878         JMP     runtime·goPanicSliceAcap(SB)
879 TEXT runtime·panicSliceAcapU(SB),NOSPLIT,$0-16
880         MOVD    R1, x+0(FP)
881         MOVD    R2, y+8(FP)
882         JMP     runtime·goPanicSliceAcapU(SB)
883 TEXT runtime·panicSliceB(SB),NOSPLIT,$0-16
884         MOVD    R0, x+0(FP)
885         MOVD    R1, y+8(FP)
886         JMP     runtime·goPanicSliceB(SB)
887 TEXT runtime·panicSliceBU(SB),NOSPLIT,$0-16
888         MOVD    R0, x+0(FP)
889         MOVD    R1, y+8(FP)
890         JMP     runtime·goPanicSliceBU(SB)
891 TEXT runtime·panicSlice3Alen(SB),NOSPLIT,$0-16
892         MOVD    R2, x+0(FP)
893         MOVD    R3, y+8(FP)
894         JMP     runtime·goPanicSlice3Alen(SB)
895 TEXT runtime·panicSlice3AlenU(SB),NOSPLIT,$0-16
896         MOVD    R2, x+0(FP)
897         MOVD    R3, y+8(FP)
898         JMP     runtime·goPanicSlice3AlenU(SB)
899 TEXT runtime·panicSlice3Acap(SB),NOSPLIT,$0-16
900         MOVD    R2, x+0(FP)
901         MOVD    R3, y+8(FP)
902         JMP     runtime·goPanicSlice3Acap(SB)
903 TEXT runtime·panicSlice3AcapU(SB),NOSPLIT,$0-16
904         MOVD    R2, x+0(FP)
905         MOVD    R3, y+8(FP)
906         JMP     runtime·goPanicSlice3AcapU(SB)
907 TEXT runtime·panicSlice3B(SB),NOSPLIT,$0-16
908         MOVD    R1, x+0(FP)
909         MOVD    R2, y+8(FP)
910         JMP     runtime·goPanicSlice3B(SB)
911 TEXT runtime·panicSlice3BU(SB),NOSPLIT,$0-16
912         MOVD    R1, x+0(FP)
913         MOVD    R2, y+8(FP)
914         JMP     runtime·goPanicSlice3BU(SB)
915 TEXT runtime·panicSlice3C(SB),NOSPLIT,$0-16
916         MOVD    R0, x+0(FP)
917         MOVD    R1, y+8(FP)
918         JMP     runtime·goPanicSlice3C(SB)
919 TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-16
920         MOVD    R0, x+0(FP)
921         MOVD    R1, y+8(FP)
922         JMP     runtime·goPanicSlice3CU(SB)
923 TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-16
924         MOVD    R2, x+0(FP)
925         MOVD    R3, y+8(FP)
926         JMP     runtime·goPanicSliceConvert(SB)