Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 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 | |
Russ Cox | 5588940 | 2011-12-19 15:51:13 -0500 | [diff] [blame] | 5 | #include "zasm_GOOS_GOARCH.h" |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 6 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 7 | TEXT _rt0_amd64(SB),7,$-8 |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 8 | // copy arguments forward on an even stack |
Devon H. O'Dell | 0489a26 | 2009-11-17 08:20:58 -0800 | [diff] [blame] | 9 | MOVQ 0(DI), AX // argc |
| 10 | LEAQ 8(DI), BX // argv |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 11 | SUBQ $(4*8+7), SP // 2args 2auto |
Ian Lance Taylor | a4f8d36 | 2010-04-09 14:15:15 -0700 | [diff] [blame] | 12 | ANDQ $~15, SP |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 13 | MOVQ AX, 16(SP) |
| 14 | MOVQ BX, 24(SP) |
Dmitriy Vyukov | 428062d | 2011-12-07 16:53:17 +0300 | [diff] [blame] | 15 | |
| 16 | // create istack out of the given (operating system) stack. |
| 17 | // initcgo may update stackguard. |
| 18 | MOVQ $runtime·g0(SB), DI |
Alex Brainman | 8d6958f | 2012-01-20 12:59:44 +1100 | [diff] [blame] | 19 | LEAQ (-64*1024+104)(SP), BX |
Dmitriy Vyukov | 428062d | 2011-12-07 16:53:17 +0300 | [diff] [blame] | 20 | MOVQ BX, g_stackguard(DI) |
| 21 | MOVQ SP, g_stackbase(DI) |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 22 | |
Ian Lance Taylor | a4f8d36 | 2010-04-09 14:15:15 -0700 | [diff] [blame] | 23 | // if there is an initcgo, call it. |
| 24 | MOVQ initcgo(SB), AX |
| 25 | TESTQ AX, AX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 26 | JZ needtls |
Alex Brainman | 8d6958f | 2012-01-20 12:59:44 +1100 | [diff] [blame] | 27 | // g0 already in DI |
| 28 | MOVQ DI, CX // Win64 uses CX for first parameter |
| 29 | CALL AX |
Wei Guangjing | 9f63659 | 2011-07-19 10:47:33 -0400 | [diff] [blame] | 30 | CMPL runtime·iswindows(SB), $0 |
| 31 | JEQ ok |
Ian Lance Taylor | a4f8d36 | 2010-04-09 14:15:15 -0700 | [diff] [blame] | 32 | |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 33 | needtls: |
Akshat Kumar | a72bebf | 2012-08-31 13:21:13 -0400 | [diff] [blame] | 34 | // skip TLS setup on Plan 9 |
| 35 | CMPL runtime·isplan9(SB), $1 |
| 36 | JEQ ok |
| 37 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 38 | LEAQ runtime·tls0(SB), DI |
| 39 | CALL runtime·settls(SB) |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 40 | |
| 41 | // store through it, to make sure it works |
| 42 | get_tls(BX) |
| 43 | MOVQ $0x123, g(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 44 | MOVQ runtime·tls0(SB), AX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 45 | CMPQ AX, $0x123 |
| 46 | JEQ 2(PC) |
| 47 | MOVL AX, 0 // abort |
| 48 | ok: |
| 49 | // set the per-goroutine and per-mach "registers" |
| 50 | get_tls(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 51 | LEAQ runtime·g0(SB), CX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 52 | MOVQ CX, g(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 53 | LEAQ runtime·m0(SB), AX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 54 | MOVQ AX, m(BX) |
| 55 | |
| 56 | // save m->g0 = g0 |
| 57 | MOVQ CX, m_g0(AX) |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 58 | |
Ken Thompson | 8f53bc0 | 2008-12-15 15:07:35 -0800 | [diff] [blame] | 59 | CLD // convention is D is always left cleared |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 60 | CALL runtime·check(SB) |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 61 | |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 62 | MOVL 16(SP), AX // copy argc |
| 63 | MOVL AX, 0(SP) |
| 64 | MOVQ 24(SP), AX // copy argv |
| 65 | MOVQ AX, 8(SP) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 66 | CALL runtime·args(SB) |
| 67 | CALL runtime·osinit(SB) |
| 68 | CALL runtime·schedinit(SB) |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 69 | |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 70 | // create a new goroutine to start program |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 71 | PUSHQ $runtime·main·f(SB) // entry |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 72 | PUSHQ $0 // arg size |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 73 | CALL runtime·newproc(SB) |
Russ Cox | ebd1eef | 2008-09-22 13:47:59 -0700 | [diff] [blame] | 74 | POPQ AX |
| 75 | POPQ AX |
Russ Cox | 79e1db2 | 2008-12-04 08:30:54 -0800 | [diff] [blame] | 76 | |
Russ Cox | ebd1eef | 2008-09-22 13:47:59 -0700 | [diff] [blame] | 77 | // start this M |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 78 | CALL runtime·mstart(SB) |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 79 | |
Russ Cox | 36aa7d4 | 2012-03-08 14:03:56 -0500 | [diff] [blame] | 80 | MOVL $0xf1, 0xf1 // crash |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 81 | RET |
| 82 | |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 83 | DATA runtime·main·f+0(SB)/8,$runtime·main(SB) |
| 84 | GLOBL runtime·main·f(SB),8,$8 |
| 85 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 86 | TEXT runtime·breakpoint(SB),7,$0 |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 87 | BYTE $0xcc |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 88 | RET |
| 89 | |
Russ Cox | 1707a99 | 2012-02-14 01:23:15 -0500 | [diff] [blame] | 90 | TEXT runtime·asminit(SB),7,$0 |
| 91 | // No per-thread init. |
| 92 | RET |
| 93 | |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 94 | /* |
| 95 | * go-routine |
| 96 | */ |
Rob Pike | d3204ef | 2008-06-30 14:39:47 -0700 | [diff] [blame] | 97 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 98 | // void gosave(Gobuf*) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 99 | // save state in Gobuf; setjmp |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 100 | TEXT runtime·gosave(SB), 7, $0 |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 101 | MOVQ 8(SP), AX // gobuf |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 102 | LEAQ 8(SP), BX // caller's SP |
| 103 | MOVQ BX, gobuf_sp(AX) |
| 104 | MOVQ 0(SP), BX // caller's PC |
| 105 | MOVQ BX, gobuf_pc(AX) |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 106 | get_tls(CX) |
| 107 | MOVQ g(CX), BX |
| 108 | MOVQ BX, gobuf_g(AX) |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 109 | RET |
| 110 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 111 | // void gogo(Gobuf*, uintptr) |
| 112 | // restore state from Gobuf; longjmp |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 113 | TEXT runtime·gogo(SB), 7, $0 |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 114 | MOVQ 16(SP), AX // return 2nd arg |
| 115 | MOVQ 8(SP), BX // gobuf |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 116 | MOVQ gobuf_g(BX), DX |
| 117 | MOVQ 0(DX), CX // make sure g != nil |
| 118 | get_tls(CX) |
| 119 | MOVQ DX, g(CX) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 120 | MOVQ gobuf_sp(BX), SP // restore SP |
| 121 | MOVQ gobuf_pc(BX), BX |
| 122 | JMP BX |
| 123 | |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 124 | // void gogocall(Gobuf*, void (*fn)(void), uintptr r0) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 125 | // restore state from Gobuf but then call fn. |
| 126 | // (call fn, returning to state in Gobuf) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 127 | TEXT runtime·gogocall(SB), 7, $0 |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 128 | MOVQ 24(SP), DX // context |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 129 | MOVQ 16(SP), AX // fn |
| 130 | MOVQ 8(SP), BX // gobuf |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 131 | MOVQ gobuf_g(BX), DI |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 132 | get_tls(CX) |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 133 | MOVQ DI, g(CX) |
| 134 | MOVQ 0(DI), CX // make sure g != nil |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 135 | MOVQ gobuf_sp(BX), SP // restore SP |
| 136 | MOVQ gobuf_pc(BX), BX |
| 137 | PUSHQ BX |
| 138 | JMP AX |
| 139 | POPQ BX // not reached |
| 140 | |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 141 | // void gogocallfn(Gobuf*, FuncVal*) |
| 142 | // restore state from Gobuf but then call fn. |
| 143 | // (call fn, returning to state in Gobuf) |
| 144 | TEXT runtime·gogocallfn(SB), 7, $0 |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 145 | MOVQ 16(SP), DX // fn |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 146 | MOVQ 8(SP), BX // gobuf |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 147 | MOVQ gobuf_g(BX), AX |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 148 | get_tls(CX) |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 149 | MOVQ AX, g(CX) |
| 150 | MOVQ 0(AX), CX // make sure g != nil |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 151 | MOVQ gobuf_sp(BX), SP // restore SP |
| 152 | MOVQ gobuf_pc(BX), BX |
| 153 | PUSHQ BX |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 154 | MOVQ 0(DX), BX |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 155 | JMP BX |
| 156 | POPQ BX // not reached |
| 157 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 158 | // void mcall(void (*fn)(G*)) |
| 159 | // Switch to m->g0's stack, call fn(g). |
Russ Cox | 370276a | 2011-04-27 23:21:12 -0400 | [diff] [blame] | 160 | // Fn must never return. It should gogo(&g->sched) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 161 | // to keep running g. |
| 162 | TEXT runtime·mcall(SB), 7, $0 |
| 163 | MOVQ fn+0(FP), DI |
| 164 | |
| 165 | get_tls(CX) |
| 166 | MOVQ g(CX), AX // save state in g->gobuf |
| 167 | MOVQ 0(SP), BX // caller's PC |
| 168 | MOVQ BX, (g_sched+gobuf_pc)(AX) |
| 169 | LEAQ 8(SP), BX // caller's SP |
| 170 | MOVQ BX, (g_sched+gobuf_sp)(AX) |
| 171 | MOVQ AX, (g_sched+gobuf_g)(AX) |
| 172 | |
| 173 | // switch to m->g0 & its stack, call fn |
| 174 | MOVQ m(CX), BX |
| 175 | MOVQ m_g0(BX), SI |
| 176 | CMPQ SI, AX // if g == m->g0 call badmcall |
| 177 | JNE 2(PC) |
| 178 | CALL runtime·badmcall(SB) |
| 179 | MOVQ SI, g(CX) // g = m->g0 |
| 180 | MOVQ (g_sched+gobuf_sp)(SI), SP // sp = m->g0->gobuf.sp |
| 181 | PUSHQ AX |
| 182 | CALL DI |
| 183 | POPQ AX |
| 184 | CALL runtime·badmcall2(SB) |
| 185 | RET |
| 186 | |
Rob Pike | 2da9783 | 2008-07-12 11:30:53 -0700 | [diff] [blame] | 187 | /* |
| 188 | * support for morestack |
| 189 | */ |
| 190 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 191 | // Called during function prolog when more stack is needed. |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 192 | // Caller has already done get_tls(CX); MOVQ m(CX), BX. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 193 | TEXT runtime·morestack(SB),7,$0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 194 | // Cannot grow scheduler stack (m->g0). |
| 195 | MOVQ m_g0(BX), SI |
| 196 | CMPQ g(CX), SI |
| 197 | JNE 2(PC) |
| 198 | INT $3 |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 199 | |
| 200 | MOVQ DX, m_cret(BX) |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 201 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 202 | // Called from f. |
| 203 | // Set m->morebuf to f's caller. |
| 204 | MOVQ 8(SP), AX // f's caller's PC |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 205 | MOVQ AX, (m_morebuf+gobuf_pc)(BX) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 206 | LEAQ 16(SP), AX // f's caller's SP |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 207 | MOVQ AX, (m_morebuf+gobuf_sp)(BX) |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 208 | MOVQ AX, m_moreargp(BX) |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 209 | get_tls(CX) |
| 210 | MOVQ g(CX), SI |
| 211 | MOVQ SI, (m_morebuf+gobuf_g)(BX) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 212 | |
| 213 | // Set m->morepc to f's PC. |
| 214 | MOVQ 0(SP), AX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 215 | MOVQ AX, m_morepc(BX) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 216 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 217 | // Call newstack on m->g0's stack. |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 218 | MOVQ m_g0(BX), BP |
| 219 | MOVQ BP, g(CX) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 220 | MOVQ (g_sched+gobuf_sp)(BP), SP |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 221 | CALL runtime·newstack(SB) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 222 | MOVQ $0, 0x1003 // crash if newstack returns |
| 223 | RET |
| 224 | |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 225 | // Called from reflection library. Mimics morestack, |
| 226 | // reuses stack growth code to create a frame |
| 227 | // with the desired args running the desired function. |
| 228 | // |
| 229 | // func call(fn *byte, arg *byte, argsize uint32). |
| 230 | TEXT reflect·call(SB), 7, $0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 231 | get_tls(CX) |
| 232 | MOVQ m(CX), BX |
| 233 | |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 234 | // Save our caller's state as the PC and SP to |
| 235 | // restore when returning from f. |
| 236 | MOVQ 0(SP), AX // our caller's PC |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 237 | MOVQ AX, (m_morebuf+gobuf_pc)(BX) |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 238 | LEAQ 8(SP), AX // our caller's SP |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 239 | MOVQ AX, (m_morebuf+gobuf_sp)(BX) |
| 240 | MOVQ g(CX), AX |
| 241 | MOVQ AX, (m_morebuf+gobuf_g)(BX) |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 242 | |
| 243 | // Set up morestack arguments to call f on a new stack. |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 244 | // We set f's frame size to 1, as a hint to newstack |
| 245 | // that this is a call from reflect·call. |
| 246 | // If it turns out that f needs a larger frame than |
| 247 | // the default stack, f's usual stack growth prolog will |
| 248 | // allocate a new segment (and recopy the arguments). |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 249 | MOVQ 8(SP), AX // fn |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 250 | MOVQ 16(SP), DX // arg frame |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 251 | MOVL 24(SP), CX // arg size |
| 252 | |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 253 | MOVQ AX, m_morepc(BX) // f's PC |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 254 | MOVQ DX, m_moreargp(BX) // argument frame pointer |
| 255 | MOVL CX, m_moreargsize(BX) // f's argument size |
| 256 | MOVL $1, m_moreframesize(BX) // f's frame size |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 257 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 258 | // Call newstack on m->g0's stack. |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 259 | MOVQ m_g0(BX), BP |
| 260 | get_tls(CX) |
| 261 | MOVQ BP, g(CX) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 262 | MOVQ (g_sched+gobuf_sp)(BP), SP |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 263 | CALL runtime·newstack(SB) |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 264 | MOVQ $0, 0x1103 // crash if newstack returns |
| 265 | RET |
| 266 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 267 | // Return point when leaving stack. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 268 | TEXT runtime·lessstack(SB), 7, $0 |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 269 | // Save return value in m->cret |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 270 | get_tls(CX) |
| 271 | MOVQ m(CX), BX |
| 272 | MOVQ AX, m_cret(BX) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 273 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 274 | // Call oldstack on m->g0's stack. |
| 275 | MOVQ m_g0(BX), BP |
| 276 | MOVQ BP, g(CX) |
| 277 | MOVQ (g_sched+gobuf_sp)(BP), SP |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 278 | CALL runtime·oldstack(SB) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 279 | MOVQ $0, 0x1004 // crash if oldstack returns |
| 280 | RET |
| 281 | |
Ken Thompson | 1ed7f18 | 2009-05-01 18:07:33 -0700 | [diff] [blame] | 282 | // morestack trampolines |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 283 | TEXT runtime·morestack00(SB),7,$0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 284 | get_tls(CX) |
| 285 | MOVQ m(CX), BX |
Ken Thompson | 1ed7f18 | 2009-05-01 18:07:33 -0700 | [diff] [blame] | 286 | MOVQ $0, AX |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 287 | MOVQ AX, m_moreframesize(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 288 | MOVQ $runtime·morestack(SB), AX |
Ken Thompson | 1ed7f18 | 2009-05-01 18:07:33 -0700 | [diff] [blame] | 289 | JMP AX |
| 290 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 291 | TEXT runtime·morestack01(SB),7,$0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 292 | get_tls(CX) |
| 293 | MOVQ m(CX), BX |
Ken Thompson | 1ed7f18 | 2009-05-01 18:07:33 -0700 | [diff] [blame] | 294 | SHLQ $32, AX |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 295 | MOVQ AX, m_moreframesize(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 296 | MOVQ $runtime·morestack(SB), AX |
Ken Thompson | 1ed7f18 | 2009-05-01 18:07:33 -0700 | [diff] [blame] | 297 | JMP AX |
| 298 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 299 | TEXT runtime·morestack10(SB),7,$0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 300 | get_tls(CX) |
| 301 | MOVQ m(CX), BX |
Ken Thompson | 1ed7f18 | 2009-05-01 18:07:33 -0700 | [diff] [blame] | 302 | MOVLQZX AX, AX |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 303 | MOVQ AX, m_moreframesize(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 304 | MOVQ $runtime·morestack(SB), AX |
Ken Thompson | 1ed7f18 | 2009-05-01 18:07:33 -0700 | [diff] [blame] | 305 | JMP AX |
| 306 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 307 | TEXT runtime·morestack11(SB),7,$0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 308 | get_tls(CX) |
| 309 | MOVQ m(CX), BX |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 310 | MOVQ AX, m_moreframesize(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 311 | MOVQ $runtime·morestack(SB), AX |
Ken Thompson | 1ed7f18 | 2009-05-01 18:07:33 -0700 | [diff] [blame] | 312 | JMP AX |
| 313 | |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 314 | // subcases of morestack01 |
| 315 | // with const of 8,16,...48 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 316 | TEXT runtime·morestack8(SB),7,$0 |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 317 | PUSHQ $1 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 318 | MOVQ $morestack<>(SB), AX |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 319 | JMP AX |
| 320 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 321 | TEXT runtime·morestack16(SB),7,$0 |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 322 | PUSHQ $2 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 323 | MOVQ $morestack<>(SB), AX |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 324 | JMP AX |
| 325 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 326 | TEXT runtime·morestack24(SB),7,$0 |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 327 | PUSHQ $3 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 328 | MOVQ $morestack<>(SB), AX |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 329 | JMP AX |
| 330 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 331 | TEXT runtime·morestack32(SB),7,$0 |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 332 | PUSHQ $4 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 333 | MOVQ $morestack<>(SB), AX |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 334 | JMP AX |
| 335 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 336 | TEXT runtime·morestack40(SB),7,$0 |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 337 | PUSHQ $5 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 338 | MOVQ $morestack<>(SB), AX |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 339 | JMP AX |
| 340 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 341 | TEXT runtime·morestack48(SB),7,$0 |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 342 | PUSHQ $6 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 343 | MOVQ $morestack<>(SB), AX |
Ken Thompson | 5963f59 | 2009-05-03 19:09:14 -0700 | [diff] [blame] | 344 | JMP AX |
| 345 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 346 | TEXT morestack<>(SB),7,$0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 347 | get_tls(CX) |
| 348 | MOVQ m(CX), BX |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 349 | POPQ AX |
| 350 | SHLQ $35, AX |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 351 | MOVQ AX, m_moreframesize(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 352 | MOVQ $runtime·morestack(SB), AX |
Rob Pike | 2da9783 | 2008-07-12 11:30:53 -0700 | [diff] [blame] | 353 | JMP AX |
| 354 | |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 355 | // bool cas(int32 *val, int32 old, int32 new) |
| 356 | // Atomically: |
| 357 | // if(*val == old){ |
| 358 | // *val = new; |
| 359 | // return 1; |
Ken Thompson | 1e1cc4e | 2009-01-27 12:03:53 -0800 | [diff] [blame] | 360 | // } else |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 361 | // return 0; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 362 | TEXT runtime·cas(SB), 7, $0 |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 363 | MOVQ 8(SP), BX |
| 364 | MOVL 16(SP), AX |
| 365 | MOVL 20(SP), CX |
| 366 | LOCK |
| 367 | CMPXCHGL CX, 0(BX) |
| 368 | JZ 3(PC) |
| 369 | MOVL $0, AX |
| 370 | RET |
| 371 | MOVL $1, AX |
| 372 | RET |
Ken Thompson | 1e1cc4e | 2009-01-27 12:03:53 -0800 | [diff] [blame] | 373 | |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 374 | // bool runtime·cas64(uint64 *val, uint64 *old, uint64 new) |
| 375 | // Atomically: |
| 376 | // if(*val == *old){ |
| 377 | // *val = new; |
| 378 | // return 1; |
| 379 | // } else { |
| 380 | // *old = *val |
| 381 | // return 0; |
| 382 | // } |
| 383 | TEXT runtime·cas64(SB), 7, $0 |
| 384 | MOVQ 8(SP), BX |
| 385 | MOVQ 16(SP), BP |
| 386 | MOVQ 0(BP), AX |
| 387 | MOVQ 24(SP), CX |
| 388 | LOCK |
| 389 | CMPXCHGQ CX, 0(BX) |
| 390 | JNZ cas64_fail |
| 391 | MOVL $1, AX |
| 392 | RET |
| 393 | cas64_fail: |
| 394 | MOVQ AX, 0(BP) |
| 395 | MOVL $0, AX |
| 396 | RET |
| 397 | |
Russ Cox | 6779350 | 2011-02-16 13:21:13 -0500 | [diff] [blame] | 398 | // bool casp(void **val, void *old, void *new) |
| 399 | // Atomically: |
| 400 | // if(*val == old){ |
| 401 | // *val = new; |
| 402 | // return 1; |
| 403 | // } else |
| 404 | // return 0; |
| 405 | TEXT runtime·casp(SB), 7, $0 |
| 406 | MOVQ 8(SP), BX |
| 407 | MOVQ 16(SP), AX |
| 408 | MOVQ 24(SP), CX |
| 409 | LOCK |
| 410 | CMPXCHGQ CX, 0(BX) |
| 411 | JZ 3(PC) |
| 412 | MOVL $0, AX |
| 413 | RET |
| 414 | MOVL $1, AX |
| 415 | RET |
| 416 | |
Dmitriy Vyukov | 491aa15 | 2011-07-15 11:27:16 -0400 | [diff] [blame] | 417 | // uint32 xadd(uint32 volatile *val, int32 delta) |
| 418 | // Atomically: |
| 419 | // *val += delta; |
| 420 | // return *val; |
| 421 | TEXT runtime·xadd(SB), 7, $0 |
| 422 | MOVQ 8(SP), BX |
| 423 | MOVL 16(SP), AX |
| 424 | MOVL AX, CX |
| 425 | LOCK |
| 426 | XADDL AX, 0(BX) |
| 427 | ADDL CX, AX |
| 428 | RET |
| 429 | |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 430 | TEXT runtime·xadd64(SB), 7, $0 |
| 431 | MOVQ 8(SP), BX |
| 432 | MOVQ 16(SP), AX |
| 433 | MOVQ AX, CX |
| 434 | LOCK |
| 435 | XADDQ AX, 0(BX) |
| 436 | ADDQ CX, AX |
| 437 | RET |
| 438 | |
Dmitriy Vyukov | 4e5086b | 2011-07-29 12:44:06 -0400 | [diff] [blame] | 439 | TEXT runtime·xchg(SB), 7, $0 |
| 440 | MOVQ 8(SP), BX |
| 441 | MOVL 16(SP), AX |
| 442 | XCHGL AX, 0(BX) |
| 443 | RET |
| 444 | |
| 445 | TEXT runtime·procyield(SB),7,$0 |
| 446 | MOVL 8(SP), AX |
| 447 | again: |
| 448 | PAUSE |
| 449 | SUBL $1, AX |
| 450 | JNZ again |
| 451 | RET |
| 452 | |
Dmitriy Vyukov | 86a659c | 2011-07-13 11:22:41 -0700 | [diff] [blame] | 453 | TEXT runtime·atomicstorep(SB), 7, $0 |
| 454 | MOVQ 8(SP), BX |
| 455 | MOVQ 16(SP), AX |
| 456 | XCHGQ AX, 0(BX) |
| 457 | RET |
| 458 | |
Dmitriy Vyukov | 91f0f18 | 2011-07-29 13:47:24 -0400 | [diff] [blame] | 459 | TEXT runtime·atomicstore(SB), 7, $0 |
| 460 | MOVQ 8(SP), BX |
| 461 | MOVL 16(SP), AX |
| 462 | XCHGL AX, 0(BX) |
| 463 | RET |
| 464 | |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 465 | TEXT runtime·atomicstore64(SB), 7, $0 |
| 466 | MOVQ 8(SP), BX |
| 467 | MOVQ 16(SP), AX |
| 468 | XCHGQ AX, 0(BX) |
| 469 | RET |
| 470 | |
Russ Cox | aa3222d8 | 2009-06-02 23:02:12 -0700 | [diff] [blame] | 471 | // void jmpdefer(fn, sp); |
| 472 | // called from deferreturn. |
Ken Thompson | 1e1cc4e | 2009-01-27 12:03:53 -0800 | [diff] [blame] | 473 | // 1. pop the caller |
| 474 | // 2. sub 5 bytes from the callers return |
| 475 | // 3. jmp to the argument |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 476 | TEXT runtime·jmpdefer(SB), 7, $0 |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 477 | MOVQ 8(SP), DX // fn |
Russ Cox | aa3222d8 | 2009-06-02 23:02:12 -0700 | [diff] [blame] | 478 | MOVQ 16(SP), BX // caller sp |
| 479 | LEAQ -8(BX), SP // caller sp after CALL |
| 480 | SUBQ $5, (SP) // return to CALL again |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 481 | MOVQ 0(DX), BX |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 482 | JMP BX // but first run the deferred function |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 483 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 484 | // Dummy function to use in saved gobuf.PC, |
| 485 | // to match SP pointing at a return address. |
| 486 | // The gobuf.PC is unused by the contortions here |
| 487 | // but setting it to return will make the traceback code work. |
| 488 | TEXT return<>(SB),7,$0 |
| 489 | RET |
| 490 | |
| 491 | // asmcgocall(void(*fn)(void*), void *arg) |
Russ Cox | add89dd | 2009-10-12 10:26:38 -0700 | [diff] [blame] | 492 | // Call fn(arg) on the scheduler stack, |
| 493 | // aligned appropriately for the gcc ABI. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 494 | // See cgocall.c for more details. |
| 495 | TEXT runtime·asmcgocall(SB),7,$0 |
| 496 | MOVQ fn+0(FP), AX |
| 497 | MOVQ arg+8(FP), BX |
| 498 | MOVQ SP, DX |
Russ Cox | add89dd | 2009-10-12 10:26:38 -0700 | [diff] [blame] | 499 | |
| 500 | // Figure out if we need to switch to m->g0 stack. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 501 | // We get called to create new OS threads too, and those |
| 502 | // come in on the m->g0 stack already. |
| 503 | get_tls(CX) |
| 504 | MOVQ m(CX), BP |
| 505 | MOVQ m_g0(BP), SI |
| 506 | MOVQ g(CX), DI |
| 507 | CMPQ SI, DI |
| 508 | JEQ 6(PC) |
| 509 | MOVQ SP, (g_sched+gobuf_sp)(DI) |
| 510 | MOVQ $return<>(SB), (g_sched+gobuf_pc)(DI) |
| 511 | MOVQ DI, (g_sched+gobuf_g)(DI) |
| 512 | MOVQ SI, g(CX) |
| 513 | MOVQ (g_sched+gobuf_sp)(SI), SP |
Russ Cox | add89dd | 2009-10-12 10:26:38 -0700 | [diff] [blame] | 514 | |
| 515 | // Now on a scheduling stack (a pthread-created stack). |
Alex Brainman | 7f075ec | 2012-09-03 12:12:51 +1000 | [diff] [blame] | 516 | // Make sure we have enough room for 4 stack-backed fast-call |
| 517 | // registers as per windows amd64 calling convention. |
| 518 | SUBQ $64, SP |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 519 | ANDQ $~15, SP // alignment for gcc ABI |
Alex Brainman | 7f075ec | 2012-09-03 12:12:51 +1000 | [diff] [blame] | 520 | MOVQ DI, 48(SP) // save g |
| 521 | MOVQ DX, 40(SP) // save SP |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 522 | MOVQ BX, DI // DI = first argument in AMD64 ABI |
Wei Guangjing | 9f63659 | 2011-07-19 10:47:33 -0400 | [diff] [blame] | 523 | MOVQ BX, CX // CX = first argument in Win64 |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 524 | CALL AX |
Russ Cox | add89dd | 2009-10-12 10:26:38 -0700 | [diff] [blame] | 525 | |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 526 | // Restore registers, g, stack pointer. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 527 | get_tls(CX) |
Alex Brainman | 7f075ec | 2012-09-03 12:12:51 +1000 | [diff] [blame] | 528 | MOVQ 48(SP), DI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 529 | MOVQ DI, g(CX) |
Alex Brainman | 7f075ec | 2012-09-03 12:12:51 +1000 | [diff] [blame] | 530 | MOVQ 40(SP), SP |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 531 | RET |
| 532 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 533 | // cgocallback(void (*fn)(void*), void *frame, uintptr framesize) |
Russ Cox | 3d2dfc5 | 2013-02-22 16:08:56 -0500 | [diff] [blame^] | 534 | // Turn the fn into a Go func (by taking its address) and call |
| 535 | // cgocallback_gofunc. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 536 | TEXT runtime·cgocallback(SB),7,$24 |
Russ Cox | 3d2dfc5 | 2013-02-22 16:08:56 -0500 | [diff] [blame^] | 537 | LEAQ fn+0(FP), AX |
| 538 | MOVQ AX, 0(SP) |
| 539 | MOVQ frame+8(FP), AX |
| 540 | MOVQ AX, 8(SP) |
| 541 | MOVQ framesize+16(FP), AX |
| 542 | MOVQ AX, 16(SP) |
| 543 | MOVQ $runtime·cgocallback_gofunc(SB), AX |
| 544 | CALL AX |
| 545 | RET |
| 546 | |
| 547 | // cgocallback_gofunc(FuncVal*, void *frame, uintptr framesize) |
| 548 | // See cgocall.c for more details. |
| 549 | TEXT runtime·cgocallback_gofunc(SB),7,$24 |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 550 | // If m is nil, Go did not create the current thread. |
| 551 | // Call needm to obtain one for temporary use. |
| 552 | // In this case, we're running on the thread stack, so there's |
| 553 | // lots of space, but the linker doesn't know. Hide the call from |
| 554 | // the linker analysis by using an indirect call through AX. |
| 555 | get_tls(CX) |
| 556 | #ifdef GOOS_windows |
| 557 | CMPQ CX, $0 |
| 558 | JNE 3(PC) |
| 559 | PUSHQ $0 |
| 560 | JMP needm |
| 561 | #endif |
| 562 | MOVQ m(CX), BP |
| 563 | PUSHQ BP |
| 564 | CMPQ BP, $0 |
| 565 | JNE havem |
| 566 | needm: |
| 567 | MOVQ $runtime·needm(SB), AX |
| 568 | CALL AX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 569 | get_tls(CX) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 570 | MOVQ m(CX), BP |
Russ Cox | 9b73238 | 2012-03-08 12:12:40 -0500 | [diff] [blame] | 571 | |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 572 | havem: |
| 573 | // Now there's a valid m, and we're running on its m->g0. |
| 574 | // Save current m->g0->sched.sp on stack and then set it to SP. |
| 575 | // Save current sp in m->g0->sched.sp in preparation for |
| 576 | // switch back to m->curg stack. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 577 | MOVQ m_g0(BP), SI |
| 578 | PUSHQ (g_sched+gobuf_sp)(SI) |
| 579 | MOVQ SP, (g_sched+gobuf_sp)(SI) |
Ian Lance Taylor | 2d0ff3f | 2010-04-09 13:30:11 -0700 | [diff] [blame] | 580 | |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 581 | // Switch to m->curg stack and call runtime.cgocallbackg |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 582 | // with the three arguments. Because we are taking over |
| 583 | // the execution of m->curg but *not* resuming what had |
| 584 | // been running, we need to save that information (m->curg->gobuf) |
| 585 | // so that we can restore it when we're done. |
| 586 | // We can restore m->curg->gobuf.sp easily, because calling |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 587 | // runtime.cgocallbackg leaves SP unchanged upon return. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 588 | // To save m->curg->gobuf.pc, we push it onto the stack. |
| 589 | // This has the added benefit that it looks to the traceback |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 590 | // routine like cgocallbackg is going to return to that |
| 591 | // PC (because we defined cgocallbackg to have |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 592 | // a frame size of 24, the same amount that we use below), |
| 593 | // so that the traceback will seamlessly trace back into |
| 594 | // the earlier calls. |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 595 | MOVQ fn+0(FP), AX |
| 596 | MOVQ frame+8(FP), BX |
| 597 | MOVQ framesize+16(FP), DX |
| 598 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 599 | MOVQ m_curg(BP), SI |
| 600 | MOVQ SI, g(CX) |
| 601 | MOVQ (g_sched+gobuf_sp)(SI), DI // prepare stack as DI |
Ian Lance Taylor | 2d0ff3f | 2010-04-09 13:30:11 -0700 | [diff] [blame] | 602 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 603 | // Push gobuf.pc |
| 604 | MOVQ (g_sched+gobuf_pc)(SI), BP |
| 605 | SUBQ $8, DI |
| 606 | MOVQ BP, 0(DI) |
| 607 | |
| 608 | // Push arguments to cgocallbackg. |
| 609 | // Frame size here must match the frame size above |
| 610 | // to trick traceback routines into doing the right thing. |
| 611 | SUBQ $24, DI |
| 612 | MOVQ AX, 0(DI) |
| 613 | MOVQ BX, 8(DI) |
| 614 | MOVQ DX, 16(DI) |
| 615 | |
| 616 | // Switch stack and make the call. |
| 617 | MOVQ DI, SP |
| 618 | CALL runtime·cgocallbackg(SB) |
| 619 | |
| 620 | // Restore g->gobuf (== m->curg->gobuf) from saved values. |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 621 | get_tls(CX) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 622 | MOVQ g(CX), SI |
| 623 | MOVQ 24(SP), BP |
| 624 | MOVQ BP, (g_sched+gobuf_pc)(SI) |
| 625 | LEAQ (24+8)(SP), DI |
| 626 | MOVQ DI, (g_sched+gobuf_sp)(SI) |
| 627 | |
| 628 | // Switch back to m->g0's stack and restore m->g0->sched.sp. |
| 629 | // (Unlike m->curg, the g0 goroutine never uses sched.pc, |
| 630 | // so we do not have to restore it.) |
| 631 | MOVQ m(CX), BP |
| 632 | MOVQ m_g0(BP), SI |
| 633 | MOVQ SI, g(CX) |
| 634 | MOVQ (g_sched+gobuf_sp)(SI), SP |
| 635 | POPQ (g_sched+gobuf_sp)(SI) |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 636 | |
| 637 | // If the m on entry was nil, we called needm above to borrow an m |
| 638 | // for the duration of the call. Since the call is over, return it with dropm. |
| 639 | POPQ BP |
| 640 | CMPQ BP, $0 |
| 641 | JNE 3(PC) |
| 642 | MOVQ $runtime·dropm(SB), AX |
| 643 | CALL AX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 644 | |
| 645 | // Done! |
Ian Lance Taylor | 2d0ff3f | 2010-04-09 13:30:11 -0700 | [diff] [blame] | 646 | RET |
| 647 | |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 648 | // void setmg(M*, G*); set m and g. for use by needm. |
| 649 | TEXT runtime·setmg(SB), 7, $0 |
| 650 | MOVQ mm+0(FP), AX |
| 651 | #ifdef GOOS_windows |
| 652 | CMPQ AX, $0 |
| 653 | JNE settls |
| 654 | MOVQ $0, 0x28(GS) |
| 655 | RET |
| 656 | settls: |
| 657 | LEAQ m_tls(AX), AX |
| 658 | MOVQ AX, 0x28(GS) |
| 659 | #endif |
| 660 | get_tls(CX) |
| 661 | MOVQ mm+0(FP), AX |
| 662 | MOVQ AX, m(CX) |
| 663 | MOVQ gg+8(FP), BX |
| 664 | MOVQ BX, g(CX) |
| 665 | RET |
| 666 | |
Devon H. O'Dell | 5a4a08f | 2009-12-08 18:19:30 -0800 | [diff] [blame] | 667 | // check that SP is in range [g->stackbase, g->stackguard) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 668 | TEXT runtime·stackcheck(SB), 7, $0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 669 | get_tls(CX) |
| 670 | MOVQ g(CX), AX |
| 671 | CMPQ g_stackbase(AX), SP |
Russ Cox | 01eaf78 | 2010-03-30 10:53:16 -0700 | [diff] [blame] | 672 | JHI 2(PC) |
| 673 | INT $3 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 674 | CMPQ SP, g_stackguard(AX) |
Russ Cox | 01eaf78 | 2010-03-30 10:53:16 -0700 | [diff] [blame] | 675 | JHI 2(PC) |
| 676 | INT $3 |
| 677 | RET |
| 678 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 679 | TEXT runtime·memclr(SB),7,$0 |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 680 | MOVQ 8(SP), DI // arg 1 addr |
Russ Cox | afc6928 | 2011-01-25 16:35:36 -0500 | [diff] [blame] | 681 | MOVQ 16(SP), CX // arg 2 count |
Quan Yong Zhai | 47410a2 | 2011-07-23 15:46:58 -0400 | [diff] [blame] | 682 | MOVQ CX, BX |
| 683 | ANDQ $7, BX |
Russ Cox | afc6928 | 2011-01-25 16:35:36 -0500 | [diff] [blame] | 684 | SHRQ $3, CX |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 685 | MOVQ $0, AX |
| 686 | CLD |
| 687 | REP |
| 688 | STOSQ |
Quan Yong Zhai | 47410a2 | 2011-07-23 15:46:58 -0400 | [diff] [blame] | 689 | MOVQ BX, CX |
| 690 | REP |
| 691 | STOSB |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 692 | RET |
| 693 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 694 | TEXT runtime·getcallerpc(SB),7,$0 |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 695 | MOVQ x+0(FP),AX // addr of first arg |
| 696 | MOVQ -8(AX),AX // get calling pc |
| 697 | RET |
| 698 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 699 | TEXT runtime·setcallerpc(SB),7,$0 |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 700 | MOVQ x+0(FP),AX // addr of first arg |
| 701 | MOVQ x+8(FP), BX |
| 702 | MOVQ BX, -8(AX) // set calling pc |
| 703 | RET |
| 704 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 705 | TEXT runtime·getcallersp(SB),7,$0 |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 706 | MOVQ sp+0(FP), AX |
| 707 | RET |
| 708 | |
Damian Gryski | 8e765da | 2012-02-02 14:09:27 -0500 | [diff] [blame] | 709 | // int64 runtime·cputicks(void) |
| 710 | TEXT runtime·cputicks(SB),7,$0 |
| 711 | RDTSC |
| 712 | SHLQ $32, DX |
| 713 | ADDQ DX, AX |
| 714 | RET |
| 715 | |
Russ Cox | 9e5db8c | 2012-03-15 15:22:30 -0400 | [diff] [blame] | 716 | TEXT runtime·stackguard(SB),7,$0 |
| 717 | MOVQ SP, DX |
| 718 | MOVQ DX, sp+0(FP) |
| 719 | get_tls(CX) |
| 720 | MOVQ g(CX), BX |
| 721 | MOVQ g_stackguard(BX), DX |
| 722 | MOVQ DX, guard+8(FP) |
| 723 | RET |
| 724 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 725 | GLOBL runtime·tls0(SB), $64 |