Text file
src/runtime/asm_wasm32.s
1 // Copyright 2018 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 TEXT runtime·rt0_go(SB), NOSPLIT|NOFRAME|TOPFRAME, $0
11 // save m->g0 = g0
12 MOVW $runtime·g0(SB), runtime·m0+m_g0(SB)
13 // save m0 to g0->m
14 MOVW $runtime·m0(SB), runtime·g0+g_m(SB)
15 // set g to g0
16 MOVD $runtime·g0(SB), g
17 CALLNORESUME runtime·check(SB)
18 CALLNORESUME runtime·osinit(SB)
19 CALLNORESUME runtime·schedinit(SB)
20 MOVD $runtime·mainFunc(SB), R0
21 MOVW 0(R0), R0
22 MOVW R0, 0(SP)
23 CALLNORESUME runtime·newproc(SB)
24 CALL runtime·mstart(SB) // WebAssembly stack will unwind when switching to another goroutine
25 UNDEF
26
27 TEXT runtime·mstart(SB),NOSPLIT|TOPFRAME,$0
28 CALL runtime·mstart0(SB)
29 RET // not reached
30
31 TEXT runtime·gogo(SB), NOSPLIT, $0-4
32 MOVW buf+0(FP), R0
33 MOVW gobuf_g(R0), R1
34 MOVW 0(R1), R2 // make sure g != nil
35 MOVD R1, g
36 MOVW gobuf_sp(R0), SP
37
38 // Put the logical PC and PC_F at -8(SP); wasm_pc_f_loop will pick them up.
39 Get SP
40 I32Const $8
41 I32Sub
42 Get R0
43 I32WrapI64
44 I32Load $gobuf_pc
45 I32Store $0
46
47 Get SP
48 I32Const $8
49 I32Sub
50 Get R0
51 I32WrapI64
52 I32Load $gobuf_lr
53 I32Store $4
54
55 MOVW gobuf_ctxt(R0), CTXT
56 // clear to help garbage collector
57 MOVW $0, gobuf_sp(R0)
58 MOVW $0, gobuf_lr(R0)
59 MOVW $0, gobuf_ctxt(R0)
60
61 I32Const $1
62 Return
63
64 // func mcall(fn func(*g))
65 // Switch to m->g0's stack, call fn(g).
66 // Fn must never return. It should gogo(&g->sched)
67 // to keep running g.
68 TEXT runtime·mcall(SB), NOSPLIT, $0-4
69 // CTXT = fn
70 MOVW fn+0(FP), CTXT
71 // R1 = g.m
72 MOVW g_m(g), R1
73 // R2 = g0
74 MOVW m_g0(R1), R2
75
76 // save state in g->sched
77 MOVW 0(SP), g_sched+gobuf_pc(g) // caller's PC
78 MOVW 4(SP), g_sched+gobuf_lr(g) // caller's PC_F
79 MOVW $fn+0(FP), g_sched+gobuf_sp(g) // caller's SP
80
81 // if g == g0 call badmcall
82 Get g
83 Get R2
84 I64Eq
85 If
86 JMP runtime·badmcall(SB)
87 End
88
89 // switch to g0's stack
90 I64Load32U (g_sched+gobuf_sp)(R2)
91 I64Const $8
92 I64Sub
93 I32WrapI64
94 Set SP
95
96 // set arg to current g
97 MOVW g, 0(SP)
98
99 // switch to g0
100 MOVD R2, g
101
102 // call fn
103 Get CTXT
104 I32WrapI64
105 I64Load32U $0
106 CALL
107
108 Get SP
109 I32Const $8
110 I32Add
111 Set SP
112
113 JMP runtime·badmcall2(SB)
114
115 // func systemstack(fn func())
116 TEXT runtime·systemstack(SB), NOSPLIT, $0-4
117 // R0 = fn
118 MOVW fn+0(FP), R0
119 // R1 = g.m
120 MOVW g_m(g), R1
121 // R2 = g0
122 MOVW m_g0(R1), R2
123
124 // if g == g0
125 Get g
126 Get R2
127 I64Eq
128 If
129 // no switch:
130 MOVD R0, CTXT
131
132 Get CTXT
133 I32WrapI64
134 I64Load32U $0
135 JMP
136 End
137
138 // if g != m.curg
139 Get g
140 I64Load32U m_curg(R1)
141 I64Ne
142 If
143 CALLNORESUME runtime·badsystemstack(SB)
144 CALLNORESUME runtime·abort(SB)
145 End
146
147 // switch:
148
149 // save state in g->sched. Pretend to
150 // be systemstack_switch if the G stack is scanned.
151 MOVW $runtime·systemstack_switch(SB), g_sched+gobuf_pc(g)
152 I32Const $runtime·wasmSystemstackSwitchF(SB)
153 I32Load $0
154 I64ExtendI32U
155 Set R3
156 MOVW R3, g_sched+gobuf_lr(g)
157
158 MOVW SP, g_sched+gobuf_sp(g)
159
160 // switch to g0
161 MOVD R2, g
162
163 // make it look like mstart called systemstack on g0, to stop traceback
164 I64Load32U (g_sched+gobuf_sp)(R2)
165 I64Const $8
166 I64Sub
167 Set R3
168
169 MOVW $runtime·mstart(SB), 0(R3)
170 I32Const $runtime·wasmMstartF(SB)
171 I32Load $0
172 I64ExtendI32U
173 Set R4
174 MOVW R4, 4(R3)
175 MOVD R3, SP
176
177 // call fn
178 MOVD R0, CTXT
179
180 Get CTXT
181 I32WrapI64
182 I64Load32U $0
183 CALL
184
185 // switch back to g
186 MOVW g_m(g), R1
187 MOVW m_curg(R1), R2
188 MOVD R2, g
189 MOVW g_sched+gobuf_sp(R2), SP
190 MOVW $0, g_sched+gobuf_sp(R2)
191 RET
192
193 TEXT runtime·systemstack_switch(SB), NOSPLIT, $0-0
194 RET
195
196 TEXT runtime·abort(SB),NOSPLIT|NOFRAME,$0-0
197 UNDEF
198
199 TEXT runtime·asminit(SB), NOSPLIT, $0-0
200 // No per-thread init.
201 RET
202
203 TEXT ·publicationBarrier(SB), NOSPLIT, $0-0
204 RET
205
206 TEXT runtime·procyieldAsm(SB), NOSPLIT, $0-0 // FIXME
207 RET
208
209 TEXT runtime·breakpoint(SB), NOSPLIT, $0-0
210 UNDEF
211
212 // func switchToCrashStack0(fn func())
213 TEXT runtime·switchToCrashStack0(SB), NOSPLIT, $0-4
214 MOVW fn+0(FP), CTXT // context register
215 MOVW g_m(g), R2 // curm
216
217 // set g to gcrash
218 MOVD $runtime·gcrash(SB), g // g = &gcrash
219 MOVW R2, g_m(g) // g.m = curm
220 MOVW g, m_g0(R2) // curm.g0 = g
221
222 // switch to crashstack
223 I64Load32U (g_stack+stack_hi)(g)
224 I64Const $(-4*8)
225 I64Add
226 I32WrapI64
227 Set SP
228
229 // call target function
230 Get CTXT
231 I32WrapI64
232 I64Load32U $0
233 CALL
234
235 // should never return
236 CALL runtime·abort(SB)
237 UNDEF
238
239 // Called during function prolog when more stack is needed.
240 //
241 // The traceback routines see morestack on a g0 as being
242 // the top of a stack (for example, morestack calling newstack
243 // calling the scheduler calling newm calling gc), so we must
244 // record an argument size. For that purpose, it has no arguments.
245 TEXT runtime·morestack(SB), NOSPLIT, $0-0
246 // R1 = g.m
247 MOVW g_m(g), R1
248
249 // R2 = g0
250 MOVW m_g0(R1), R2
251
252 // Set g->sched to context in f.
253 NOP SP // tell vet SP changed - stop checking offsets
254 MOVW 0(SP), g_sched+gobuf_pc(g)
255 MOVW 4(SP), g_sched+gobuf_lr(g)
256 MOVW $8(SP), g_sched+gobuf_sp(g) // f's SP
257 MOVW CTXT, g_sched+gobuf_ctxt(g)
258
259 // Cannot grow scheduler stack (m->g0).
260 Get g
261 Get R2
262 I64Eq
263 If
264 CALLNORESUME runtime·badmorestackg0(SB)
265 CALLNORESUME runtime·abort(SB)
266 End
267
268 // Cannot grow signal stack (m->gsignal).
269 Get g
270 I64Load32U m_gsignal(R1)
271 I64Eq
272 If
273 CALLNORESUME runtime·badmorestackgsignal(SB)
274 CALLNORESUME runtime·abort(SB)
275 End
276
277 // Called from f.
278 // Set m->morebuf to f's caller.
279 MOVW 8(SP), m_morebuf+gobuf_pc(R1)
280 MOVW 12(SP), m_morebuf+gobuf_lr(R1)
281 MOVW $16(SP), m_morebuf+gobuf_sp(R1) // f's caller's SP
282 MOVW g, m_morebuf+gobuf_g(R1)
283
284 // Call newstack on m->g0's stack.
285 MOVD R2, g
286 MOVW g_sched+gobuf_sp(R2), SP
287 CALL runtime·newstack(SB)
288 UNDEF // crash if newstack returns
289
290 // morestack but not preserving ctxt.
291 TEXT runtime·morestack_noctxt(SB),NOSPLIT,$0
292 MOVD $0, CTXT
293 JMP runtime·morestack(SB)
294
295 TEXT ·asmcgocall(SB), NOSPLIT, $0-0
296 UNDEF
297
298 #define DISPATCH(NAME, MAXSIZE) \
299 Get R0; \
300 I64Const $MAXSIZE; \
301 I64LeU; \
302 If; \
303 JMP NAME(SB); \
304 End
305
306 TEXT ·reflectcall(SB), NOSPLIT, $0-28
307 I64Load32U fn+4(FP)
308 I64Eqz
309 If
310 CALLNORESUME runtime·sigpanic<ABIInternal>(SB)
311 End
312
313 MOVW frameSize+20(FP), R0
314
315 DISPATCH(runtime·call16, 16)
316 DISPATCH(runtime·call32, 32)
317 DISPATCH(runtime·call64, 64)
318 DISPATCH(runtime·call128, 128)
319 DISPATCH(runtime·call256, 256)
320 DISPATCH(runtime·call512, 512)
321 DISPATCH(runtime·call1024, 1024)
322 DISPATCH(runtime·call2048, 2048)
323 DISPATCH(runtime·call4096, 4096)
324 DISPATCH(runtime·call8192, 8192)
325 DISPATCH(runtime·call16384, 16384)
326 DISPATCH(runtime·call32768, 32768)
327 DISPATCH(runtime·call65536, 65536)
328 DISPATCH(runtime·call131072, 131072)
329 DISPATCH(runtime·call262144, 262144)
330 DISPATCH(runtime·call524288, 524288)
331 DISPATCH(runtime·call1048576, 1048576)
332 DISPATCH(runtime·call2097152, 2097152)
333 DISPATCH(runtime·call4194304, 4194304)
334 DISPATCH(runtime·call8388608, 8388608)
335 DISPATCH(runtime·call16777216, 16777216)
336 DISPATCH(runtime·call33554432, 33554432)
337 DISPATCH(runtime·call67108864, 67108864)
338 DISPATCH(runtime·call134217728, 134217728)
339 DISPATCH(runtime·call268435456, 268435456)
340 DISPATCH(runtime·call536870912, 536870912)
341 DISPATCH(runtime·call1073741824, 1073741824)
342 JMP runtime·badreflectcall(SB)
343
344 #define CALLFN(NAME, MAXSIZE) \
345 TEXT NAME(SB), WRAPPER, $MAXSIZE-28; \
346 NO_LOCAL_POINTERS; \
347 MOVW stackArgsSize+12(FP), R0; \
348 \
349 Get R0; \
350 I64Eqz; \
351 Not; \
352 If; \
353 Get SP; \
354 I64Load32U stackArgs+8(FP); \
355 I32WrapI64; \
356 I64Load32U stackArgsSize+12(FP); \
357 I32WrapI64; \
358 MemoryCopy; \
359 End; \
360 \
361 MOVW f+4(FP), CTXT; \
362 Get CTXT; \
363 I32WrapI64; \
364 I64Load32U $0; \
365 CALL; \
366 \
367 I64Load32U stackRetOffset+16(FP); \
368 Set R0; \
369 \
370 MOVW stackArgsType+0(FP), RET0; \
371 \
372 I64Load32U stackArgs+8(FP); \
373 Get R0; \
374 I64Add; \
375 Set RET1; \
376 \
377 Get SP; \
378 I64ExtendI32U; \
379 Get R0; \
380 I64Add; \
381 Set RET2; \
382 \
383 I64Load32U stackArgsSize+12(FP); \
384 Get R0; \
385 I64Sub; \
386 Set RET3; \
387 \
388 CALL callRet<>(SB); \
389 RET
390
391 // callRet copies return values back at the end of call*. This is a
392 // separate function so it can allocate stack space for the arguments
393 // to reflectcallmove. It does not follow the Go ABI; it expects its
394 // arguments in registers.
395 TEXT callRet<>(SB), NOSPLIT, $24-0
396 NO_LOCAL_POINTERS
397 MOVW RET0, 0(SP)
398 MOVW RET1, 4(SP)
399 MOVW RET2, 8(SP)
400 MOVW RET3, 12(SP)
401 MOVW $0, 16(SP)
402 CALL runtime·reflectcallmove(SB)
403 RET
404
405 CALLFN(·call16, 16)
406 CALLFN(·call32, 32)
407 CALLFN(·call64, 64)
408 CALLFN(·call128, 128)
409 CALLFN(·call256, 256)
410 CALLFN(·call512, 512)
411 CALLFN(·call1024, 1024)
412 CALLFN(·call2048, 2048)
413 CALLFN(·call4096, 4096)
414 CALLFN(·call8192, 8192)
415 CALLFN(·call16384, 16384)
416 CALLFN(·call32768, 32768)
417 CALLFN(·call65536, 65536)
418 CALLFN(·call131072, 131072)
419 CALLFN(·call262144, 262144)
420 CALLFN(·call524288, 524288)
421 CALLFN(·call1048576, 1048576)
422 CALLFN(·call2097152, 2097152)
423 CALLFN(·call4194304, 4194304)
424 CALLFN(·call8388608, 8388608)
425 CALLFN(·call16777216, 16777216)
426 CALLFN(·call33554432, 33554432)
427 CALLFN(·call67108864, 67108864)
428 CALLFN(·call134217728, 134217728)
429 CALLFN(·call268435456, 268435456)
430 CALLFN(·call536870912, 536870912)
431 CALLFN(·call1073741824, 1073741824)
432
433 TEXT runtime·goexit(SB), NOSPLIT|TOPFRAME, $0-0
434 NOP // first PC of goexit is skipped
435 CALL runtime·goexit1(SB) // does not return
436 UNDEF
437
438 TEXT runtime·cgocallback(SB), NOSPLIT, $0-12
439 UNDEF
440
441 // Called from generated code with the index high word, index low word,
442 // and bound in three 32-bit stack slots. PCDATA_PanicBounds describes
443 // them as registers 0, 1, and 2 in the save area for panicBounds32X.
444 TEXT runtime·panicExtend<ABIInternal>(SB), NOSPLIT, $72-0
445 NO_LOCAL_POINTERS
446 // The compiler passes these slots using a custom ABI, outside the Go declaration.
447 MOVD SP, R0
448 MOVW 80(R0), 8(SP)
449 MOVW 84(R0), 12(SP)
450 MOVW 88(R0), 16(SP)
451 MOVW 72(SP), 0(SP) // logical PC immediately after the call to panicExtend
452 MOVW $8(SP), 4(SP)
453 CALL runtime·panicBounds32X<ABIInternal>(SB)
454 UNDEF
455
456 // gcWriteBarrier informs the GC about heap pointer writes.
457 //
458 // gcWriteBarrier does NOT follow the Go ABI. It accepts the
459 // number of bytes of buffer needed as a wasm argument
460 // (put on the TOS by the caller, lives in local R0 in this body)
461 // and returns a pointer to the buffer space as a wasm result
462 // (left on the TOS in this body, appears on the wasm stack
463 // in the caller).
464 TEXT gcWriteBarrier<>(SB), NOSPLIT, $0
465 Loop
466 // R3 = g.m
467 MOVW g_m(g), R3
468 // R4 = p
469 MOVW m_p(R3), R4
470 // R5 = wbBuf.next
471 MOVW p_wbBuf+wbBuf_next(R4), R5
472
473 // Is there enough room?
474 I64Load32U (p_wbBuf+wbBuf_end)(R4)
475 Get R5
476 I64Sub
477 Get R0
478 I64GeU
479 If
480 // Increment wbBuf.next.
481 Get R5
482 Get R0
483 I64Add
484 Set R5
485
486 // Commit to the larger buffer.
487 MOVW R5, p_wbBuf+wbBuf_next(R4)
488
489 // Make return value (the original next position)
490 Get R5
491 Get R0
492 I64Sub
493
494 Return
495 End
496
497 // Flush
498 CALLNORESUME runtime·wbBufFlush(SB)
499
500 // Retry
501 Br $0
502 End
503
504 // gcWriteBarrierN requests space for N pointers (N*PtrSize bytes, PtrSize=4).
505 TEXT runtime·gcWriteBarrier1<ABIInternal>(SB),NOSPLIT,$0
506 I64Const $(1*4)
507 Call gcWriteBarrier<>(SB)
508 Return
509 TEXT runtime·gcWriteBarrier2<ABIInternal>(SB),NOSPLIT,$0
510 I64Const $(2*4)
511 Call gcWriteBarrier<>(SB)
512 Return
513 TEXT runtime·gcWriteBarrier3<ABIInternal>(SB),NOSPLIT,$0
514 I64Const $(3*4)
515 Call gcWriteBarrier<>(SB)
516 Return
517 TEXT runtime·gcWriteBarrier4<ABIInternal>(SB),NOSPLIT,$0
518 I64Const $(4*4)
519 Call gcWriteBarrier<>(SB)
520 Return
521 TEXT runtime·gcWriteBarrier5<ABIInternal>(SB),NOSPLIT,$0
522 I64Const $(5*4)
523 Call gcWriteBarrier<>(SB)
524 Return
525 TEXT runtime·gcWriteBarrier6<ABIInternal>(SB),NOSPLIT,$0
526 I64Const $(6*4)
527 Call gcWriteBarrier<>(SB)
528 Return
529 TEXT runtime·gcWriteBarrier7<ABIInternal>(SB),NOSPLIT,$0
530 I64Const $(7*4)
531 Call gcWriteBarrier<>(SB)
532 Return
533 TEXT runtime·gcWriteBarrier8<ABIInternal>(SB),NOSPLIT,$0
534 I64Const $(8*4)
535 Call gcWriteBarrier<>(SB)
536 Return
537
538 // func wasmFuncEntryAddr() uintptr
539 TEXT runtime·wasmFuncEntryAddr(SB), NOSPLIT, $0-4
540 I32Const $runtime·wasmFuncEntry(SB)
541 I64ExtendI32U
542 Set R0
543 MOVW R0, ret+0(FP)
544 RET
545
546 // func wasmFuncCount() uintptr
547 TEXT runtime·wasmFuncCount(SB), NOSPLIT, $0-4
548 I32Const $runtime·wasmFuncCountData(SB)
549 I32Load $0
550 I64ExtendI32U
551 Set R0
552 MOVW R0, ret+0(FP)
553 RET
554
555 // func wasmGoexitHandle() uintptr
556 TEXT runtime·wasmGoexitHandle(SB), NOSPLIT, $0-4
557 I32Const $runtime·wasmGoexitF(SB)
558 I32Load $0
559 I64ExtendI32U
560 Set R0
561 MOVW R0, ret+0(FP)
562 RET
563
564 TEXT wasm_pc_f_loop(SB),NOSPLIT,$0
565 // Call the function for the current PC_F. Repeat until PAUSE != 0 indicates pause or exit.
566 // The WebAssembly stack may unwind, e.g. when switching goroutines.
567 // The Go stack on the linear memory is then used to jump to the correct functions
568 // with this loop, without having to restore the full WebAssembly stack.
569 // It is expected to have a pending call before entering the loop, so check PAUSE first.
570 Get PAUSE
571 I32Eqz
572 If
573 loop:
574 Loop
575 // Get PC_F from -8(SP).
576 Get SP
577 I32Const $8
578 I32Sub
579 I32Load $4
580 Set R0
581
582 // PC_B = logical PC - function entry PC.
583 Get SP
584 I32Const $8
585 I32Sub
586 I32Load $0 // logical PC
587 I32Const $runtime·wasmFuncEntry(SB)
588 Get R0
589 I32Const $2
590 I32Shl
591 I32Add
592 I32Load $0
593 I32Sub
594
595 Get R0 // PC_F
596 CallIndirect $0
597 Drop
598
599 Get PAUSE
600 I32Eqz
601 BrIf loop
602 End
603 End
604
605 I32Const $0
606 Set PAUSE
607
608 Return
609
610 // wasm_pc_f_loop_export is like wasm_pc_f_loop, except that this takes an
611 // argument (on Wasm stack) that is a logical PC token, and the loop stops
612 // when we get to that continuation in a normal return (not unwinding).
613 // This is for handling an wasmexport function when it needs to switch the
614 // stack.
615 TEXT wasm_pc_f_loop_export(SB),NOSPLIT,$0
616 Get PAUSE
617 I32Eqz
618 outer:
619 If
620 // R1 is whether a function return normally (0) or unwinding (1).
621 // Start with unwinding.
622 I32Const $1
623 Set R1
624 loop:
625 Loop
626 // Get logical PC and PC_F from -8(SP)
627 Get SP
628 I32Const $8
629 I32Sub
630 I32Load $0 // logical PC
631 Tee R2
632
633 Get R0
634 I32Eq
635 If // logical PC == R0, we're at the stop continuation
636 Get R1
637 I32Eqz
638 // Break if it is a normal return
639 BrIf outer // actually jump to after the corresponding End
640 End
641
642 Get SP
643 I32Const $8
644 I32Sub
645 I32Load $4 // PC_F
646 Set R3
647
648 // PC_B = logical PC - function entry PC.
649 Get R2
650 I32Const $runtime·wasmFuncEntry(SB)
651 Get R3
652 I32Const $2
653 I32Shl
654 I32Add
655 I32Load $0
656 I32Sub
657
658 Get R3 // PC_F
659 CallIndirect $0
660 Set R1 // save return/unwinding state for next iteration
661
662 Get PAUSE
663 I32Eqz
664 BrIf loop
665 End
666 End
667
668 I32Const $0
669 Set PAUSE
670
671 Return
672
673 TEXT wasm_export_lib(SB),NOSPLIT,$0
674 UNDEF
675
676 TEXT runtime·pause(SB), NOSPLIT, $0-4
677 MOVW newsp+0(FP), SP
678 I32Const $1
679 Set PAUSE
680 RETUNWIND
681
682 // Called if a wasmexport function is called before runtime initialization
683 TEXT runtime·notInitialized(SB), NOSPLIT, $0
684 MOVD $runtime·wasmStack+(m0Stack__size-16-8)(SB), SP
685 I32Const $0 // entry PC_B
686 Call runtime·notInitialized1(SB)
687 Drop
688 I32Const $0 // entry PC_B
689 Call runtime·abort(SB)
690 UNDEF
691
View as plain text