Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -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" |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 6 | #include "funcdata.h" |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 7 | |
Russ Cox | 6120ef0 | 2013-06-11 16:49:24 -0400 | [diff] [blame] | 8 | TEXT _rt0_go(SB),7,$0 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 9 | // copy arguments forward on an even stack |
Russ Cox | dfc22e29 | 2013-03-07 19:57:10 -0800 | [diff] [blame] | 10 | MOVL argc+0(FP), AX |
| 11 | MOVL argv+4(FP), BX |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 12 | SUBL $128, SP // plenty of scratch |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 13 | ANDL $~15, SP |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 14 | MOVL AX, 120(SP) // save argc, argv away |
| 15 | MOVL BX, 124(SP) |
| 16 | |
Dmitriy Vyukov | 428062d | 2011-12-07 16:53:17 +0300 | [diff] [blame] | 17 | // set default stack bounds. |
Russ Cox | f8d49b5 | 2013-02-28 16:24:38 -0500 | [diff] [blame] | 18 | // _cgo_init may update stackguard. |
Dmitriy Vyukov | 428062d | 2011-12-07 16:53:17 +0300 | [diff] [blame] | 19 | MOVL $runtime·g0(SB), BP |
| 20 | LEAL (-64*1024+104)(SP), BX |
| 21 | MOVL BX, g_stackguard(BP) |
Dmitriy Vyukov | f5becf4 | 2013-06-03 12:28:24 +0400 | [diff] [blame] | 22 | MOVL BX, g_stackguard0(BP) |
Dmitriy Vyukov | 428062d | 2011-12-07 16:53:17 +0300 | [diff] [blame] | 23 | MOVL SP, g_stackbase(BP) |
| 24 | |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 25 | // find out information about the processor we're on |
| 26 | MOVL $0, AX |
| 27 | CPUID |
| 28 | CMPL AX, $0 |
| 29 | JE nocpuinfo |
| 30 | MOVL $1, AX |
| 31 | CPUID |
| 32 | MOVL CX, runtime·cpuid_ecx(SB) |
| 33 | MOVL DX, runtime·cpuid_edx(SB) |
| 34 | nocpuinfo: |
| 35 | |
Russ Cox | f8d49b5 | 2013-02-28 16:24:38 -0500 | [diff] [blame] | 36 | // if there is an _cgo_init, call it to let it |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 37 | // initialize and to set up GS. if not, |
| 38 | // we set up GS ourselves. |
Russ Cox | f8d49b5 | 2013-02-28 16:24:38 -0500 | [diff] [blame] | 39 | MOVL _cgo_init(SB), AX |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 40 | TESTL AX, AX |
Dmitriy Vyukov | fbfed49 | 2011-11-09 23:11:48 +0300 | [diff] [blame] | 41 | JZ needtls |
Russ Cox | 6a70f9d | 2013-03-25 18:14:02 -0400 | [diff] [blame] | 42 | MOVL $setmg_gcc<>(SB), BX |
| 43 | MOVL BX, 4(SP) |
Russ Cox | 3b85b72 | 2013-03-11 00:51:42 -0400 | [diff] [blame] | 44 | MOVL BP, 0(SP) |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 45 | CALL AX |
Dmitriy Vyukov | f5becf4 | 2013-06-03 12:28:24 +0400 | [diff] [blame] | 46 | // update stackguard after _cgo_init |
| 47 | MOVL $runtime·g0(SB), CX |
| 48 | MOVL g_stackguard0(CX), AX |
| 49 | MOVL AX, g_stackguard(CX) |
Russ Cox | f8d49b5 | 2013-02-28 16:24:38 -0500 | [diff] [blame] | 50 | // skip runtime·ldt0setup(SB) and tls test after _cgo_init for non-windows |
Wei Guangjing | 1aa2d88 | 2011-01-20 10:22:20 -0500 | [diff] [blame] | 51 | CMPL runtime·iswindows(SB), $0 |
| 52 | JEQ ok |
Dmitriy Vyukov | fbfed49 | 2011-11-09 23:11:48 +0300 | [diff] [blame] | 53 | needtls: |
Yuval Pavel Zholkover | 2aa2ceb | 2011-07-25 12:25:41 -0400 | [diff] [blame] | 54 | // skip runtime·ldt0setup(SB) and tls test on Plan 9 in all cases |
| 55 | CMPL runtime·isplan9(SB), $1 |
| 56 | JEQ ok |
| 57 | |
Russ Cox | 1b14bdb | 2009-09-22 16:28:32 -0700 | [diff] [blame] | 58 | // set up %gs |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 59 | CALL runtime·ldt0setup(SB) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 60 | |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 61 | // store through it, to make sure it works |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 62 | get_tls(BX) |
| 63 | MOVL $0x123, g(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 64 | MOVL runtime·tls0(SB), AX |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 65 | CMPL AX, $0x123 |
| 66 | JEQ ok |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 67 | MOVL AX, 0 // abort |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 68 | ok: |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 69 | // set up m and g "registers" |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 70 | get_tls(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 71 | LEAL runtime·g0(SB), CX |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 72 | MOVL CX, g(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 73 | LEAL runtime·m0(SB), AX |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 74 | MOVL AX, m(BX) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 75 | |
| 76 | // save m->g0 = g0 |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 77 | MOVL CX, m_g0(AX) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 78 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 79 | CALL runtime·emptyfunc(SB) // fault if stack check is wrong |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 80 | |
| 81 | // convention is D is always cleared |
| 82 | CLD |
| 83 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 84 | CALL runtime·check(SB) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 85 | |
| 86 | // saved argc, argv |
| 87 | MOVL 120(SP), AX |
| 88 | MOVL AX, 0(SP) |
| 89 | MOVL 124(SP), AX |
| 90 | MOVL AX, 4(SP) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 91 | CALL runtime·args(SB) |
| 92 | CALL runtime·osinit(SB) |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 93 | CALL runtime·hashinit(SB) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 94 | CALL runtime·schedinit(SB) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 95 | |
| 96 | // create a new goroutine to start program |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 97 | PUSHL $runtime·main·f(SB) // entry |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 98 | PUSHL $0 // arg size |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 99 | ARGSIZE(8) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 100 | CALL runtime·newproc(SB) |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 101 | ARGSIZE(-1) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 102 | POPL AX |
| 103 | POPL AX |
| 104 | |
| 105 | // start this M |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 106 | CALL runtime·mstart(SB) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 107 | |
| 108 | INT $3 |
| 109 | RET |
| 110 | |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 111 | DATA runtime·main·f+0(SB)/4,$runtime·main(SB) |
| 112 | GLOBL runtime·main·f(SB),8,$4 |
| 113 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 114 | TEXT runtime·breakpoint(SB),7,$0-0 |
Russ Cox | 1b14bdb | 2009-09-22 16:28:32 -0700 | [diff] [blame] | 115 | INT $3 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 116 | RET |
| 117 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 118 | TEXT runtime·asminit(SB),7,$0-0 |
Carl Shapiro | 019c8fc | 2013-04-02 13:45:56 -0700 | [diff] [blame] | 119 | // Linux and MinGW start the FPU in extended double precision. |
Russ Cox | 1707a99 | 2012-02-14 01:23:15 -0500 | [diff] [blame] | 120 | // Other operating systems use double precision. |
| 121 | // Change to double precision to match them, |
| 122 | // and to match other hardware that only has double. |
| 123 | PUSHL $0x27F |
| 124 | FLDCW 0(SP) |
| 125 | POPL AX |
| 126 | RET |
| 127 | |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 128 | /* |
| 129 | * go-routine |
| 130 | */ |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 131 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 132 | // void gosave(Gobuf*) |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 133 | // save state in Gobuf; setjmp |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 134 | TEXT runtime·gosave(SB), 7, $0-4 |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 135 | MOVL 4(SP), AX // gobuf |
| 136 | LEAL 4(SP), BX // caller's SP |
| 137 | MOVL BX, gobuf_sp(AX) |
| 138 | MOVL 0(SP), BX // caller's PC |
| 139 | MOVL BX, gobuf_pc(AX) |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 140 | MOVL $0, gobuf_ret(AX) |
| 141 | MOVL $0, gobuf_ctxt(AX) |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 142 | get_tls(CX) |
| 143 | MOVL g(CX), BX |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 144 | MOVL BX, gobuf_g(AX) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 145 | RET |
| 146 | |
Ian Lance Taylor | 0627248 | 2013-06-12 15:05:10 -0700 | [diff] [blame] | 147 | // void gogo(Gobuf*) |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 148 | // restore state from Gobuf; longjmp |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 149 | TEXT runtime·gogo(SB), 7, $0-4 |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 150 | MOVL 4(SP), BX // gobuf |
| 151 | MOVL gobuf_g(BX), DX |
| 152 | MOVL 0(DX), CX // make sure g != nil |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 153 | get_tls(CX) |
| 154 | MOVL DX, g(CX) |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 155 | MOVL gobuf_sp(BX), SP // restore SP |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 156 | MOVL gobuf_ret(BX), AX |
| 157 | MOVL gobuf_ctxt(BX), DX |
| 158 | MOVL $0, gobuf_sp(BX) // clear to help garbage collector |
| 159 | MOVL $0, gobuf_ret(BX) |
| 160 | MOVL $0, gobuf_ctxt(BX) |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 161 | MOVL gobuf_pc(BX), BX |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 162 | JMP BX |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 163 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 164 | // void mcall(void (*fn)(G*)) |
| 165 | // Switch to m->g0's stack, call fn(g). |
Russ Cox | 370276a | 2011-04-27 23:21:12 -0400 | [diff] [blame] | 166 | // Fn must never return. It should gogo(&g->sched) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 167 | // to keep running g. |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 168 | TEXT runtime·mcall(SB), 7, $0-4 |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 169 | MOVL fn+0(FP), DI |
| 170 | |
| 171 | get_tls(CX) |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 172 | MOVL g(CX), AX // save state in g->sched |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 173 | MOVL 0(SP), BX // caller's PC |
| 174 | MOVL BX, (g_sched+gobuf_pc)(AX) |
| 175 | LEAL 4(SP), BX // caller's SP |
| 176 | MOVL BX, (g_sched+gobuf_sp)(AX) |
| 177 | MOVL AX, (g_sched+gobuf_g)(AX) |
| 178 | |
| 179 | // switch to m->g0 & its stack, call fn |
| 180 | MOVL m(CX), BX |
| 181 | MOVL m_g0(BX), SI |
| 182 | CMPL SI, AX // if g == m->g0 call badmcall |
| 183 | JNE 2(PC) |
| 184 | CALL runtime·badmcall(SB) |
| 185 | MOVL SI, g(CX) // g = m->g0 |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 186 | MOVL (g_sched+gobuf_sp)(SI), SP // sp = m->g0->sched.sp |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 187 | PUSHL AX |
| 188 | CALL DI |
| 189 | POPL AX |
| 190 | CALL runtime·badmcall2(SB) |
| 191 | RET |
| 192 | |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 193 | /* |
| 194 | * support for morestack |
| 195 | */ |
| 196 | |
| 197 | // Called during function prolog when more stack is needed. |
Russ Cox | 58f12ff | 2013-07-18 16:53:45 -0400 | [diff] [blame] | 198 | // |
| 199 | // The traceback routines see morestack on a g0 as being |
| 200 | // the top of a stack (for example, morestack calling newstack |
| 201 | // calling the scheduler calling newm calling gc), so we must |
| 202 | // record an argument size. For that purpose, it has no arguments. |
| 203 | TEXT runtime·morestack(SB),7,$0-0 |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 204 | // Cannot grow scheduler stack (m->g0). |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 205 | get_tls(CX) |
| 206 | MOVL m(CX), BX |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 207 | MOVL m_g0(BX), SI |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 208 | CMPL g(CX), SI |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 209 | JNE 2(PC) |
| 210 | INT $3 |
| 211 | |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 212 | // frame size in DI |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 213 | // arg size in AX |
| 214 | // Save in m. |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 215 | MOVL DI, m_moreframesize(BX) |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 216 | MOVL AX, m_moreargsize(BX) |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 217 | |
| 218 | // Called from f. |
| 219 | // Set m->morebuf to f's caller. |
| 220 | MOVL 4(SP), DI // f's caller's PC |
| 221 | MOVL DI, (m_morebuf+gobuf_pc)(BX) |
| 222 | LEAL 8(SP), CX // f's caller's SP |
| 223 | MOVL CX, (m_morebuf+gobuf_sp)(BX) |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 224 | MOVL CX, m_moreargp(BX) |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 225 | get_tls(CX) |
| 226 | MOVL g(CX), SI |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 227 | MOVL SI, (m_morebuf+gobuf_g)(BX) |
| 228 | |
Russ Cox | 6fa3c89 | 2013-06-27 11:32:01 -0400 | [diff] [blame] | 229 | // Set g->sched to context in f. |
| 230 | MOVL 0(SP), AX // f's PC |
| 231 | MOVL AX, (g_sched+gobuf_pc)(SI) |
| 232 | MOVL SI, (g_sched+gobuf_g)(SI) |
| 233 | LEAL 4(SP), AX // f's SP |
| 234 | MOVL AX, (g_sched+gobuf_sp)(SI) |
| 235 | MOVL DX, (g_sched+gobuf_ctxt)(SI) |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 236 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 237 | // Call newstack on m->g0's stack. |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 238 | MOVL m_g0(BX), BP |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 239 | MOVL BP, g(CX) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 240 | MOVL (g_sched+gobuf_sp)(BP), AX |
Russ Cox | 7e14bd8 | 2010-12-07 17:19:36 -0500 | [diff] [blame] | 241 | MOVL -4(AX), BX // fault if CALL would, before smashing SP |
| 242 | MOVL AX, SP |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 243 | CALL runtime·newstack(SB) |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 244 | MOVL $0, 0x1003 // crash if newstack returns |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 245 | RET |
| 246 | |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 247 | // Called from reflection library. Mimics morestack, |
| 248 | // reuses stack growth code to create a frame |
| 249 | // with the desired args running the desired function. |
| 250 | // |
| 251 | // func call(fn *byte, arg *byte, argsize uint32). |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 252 | TEXT reflect·call(SB), 7, $0-12 |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 253 | get_tls(CX) |
| 254 | MOVL m(CX), BX |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 255 | |
| 256 | // Save our caller's state as the PC and SP to |
| 257 | // restore when returning from f. |
| 258 | MOVL 0(SP), AX // our caller's PC |
| 259 | MOVL AX, (m_morebuf+gobuf_pc)(BX) |
| 260 | LEAL 4(SP), AX // our caller's SP |
| 261 | MOVL AX, (m_morebuf+gobuf_sp)(BX) |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 262 | MOVL g(CX), AX |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 263 | MOVL AX, (m_morebuf+gobuf_g)(BX) |
| 264 | |
Russ Cox | f0d73fb | 2013-06-27 16:51:06 -0400 | [diff] [blame] | 265 | // Save our own state as the PC and SP to restore |
| 266 | // if this goroutine needs to be restarted. |
| 267 | MOVL $reflect·call(SB), (g_sched+gobuf_pc)(AX) |
| 268 | MOVL SP, (g_sched+gobuf_sp)(AX) |
| 269 | |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 270 | // Set up morestack arguments to call f on a new stack. |
Russ Cox | 83727cc | 2010-03-29 21:48:22 -0700 | [diff] [blame] | 271 | // We set f's frame size to 1, as a hint to newstack |
| 272 | // that this is a call from reflect·call. |
| 273 | // If it turns out that f needs a larger frame than |
| 274 | // the default stack, f's usual stack growth prolog will |
| 275 | // allocate a new segment (and recopy the arguments). |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 276 | MOVL 4(SP), AX // fn |
| 277 | MOVL 8(SP), DX // arg frame |
| 278 | MOVL 12(SP), CX // arg size |
| 279 | |
Russ Cox | 6fa3c89 | 2013-06-27 11:32:01 -0400 | [diff] [blame] | 280 | MOVL AX, m_cret(BX) // f's PC |
Russ Cox | 141a4a1 | 2011-01-14 14:05:20 -0500 | [diff] [blame] | 281 | MOVL DX, m_moreargp(BX) // f's argument pointer |
| 282 | MOVL CX, m_moreargsize(BX) // f's argument size |
| 283 | MOVL $1, m_moreframesize(BX) // f's frame size |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 284 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 285 | // Call newstack on m->g0's stack. |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 286 | MOVL m_g0(BX), BP |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 287 | get_tls(CX) |
| 288 | MOVL BP, g(CX) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 289 | MOVL (g_sched+gobuf_sp)(BP), SP |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 290 | CALL runtime·newstack(SB) |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 291 | MOVL $0, 0x1103 // crash if newstack returns |
| 292 | RET |
| 293 | |
| 294 | |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 295 | // Return point when leaving stack. |
Russ Cox | 58f12ff | 2013-07-18 16:53:45 -0400 | [diff] [blame] | 296 | // |
| 297 | // Lessstack can appear in stack traces for the same reason |
| 298 | // as morestack; in that context, it has 0 arguments. |
| 299 | TEXT runtime·lessstack(SB), 7, $0-0 |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 300 | // Save return value in m->cret |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 301 | get_tls(CX) |
| 302 | MOVL m(CX), BX |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 303 | MOVL AX, m_cret(BX) |
| 304 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 305 | // Call oldstack on m->g0's stack. |
| 306 | MOVL m_g0(BX), BP |
| 307 | MOVL BP, g(CX) |
| 308 | MOVL (g_sched+gobuf_sp)(BP), SP |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 309 | CALL runtime·oldstack(SB) |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 310 | MOVL $0, 0x1004 // crash if oldstack returns |
| 311 | RET |
| 312 | |
| 313 | |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 314 | // bool cas(int32 *val, int32 old, int32 new) |
| 315 | // Atomically: |
| 316 | // if(*val == old){ |
| 317 | // *val = new; |
| 318 | // return 1; |
| 319 | // }else |
| 320 | // return 0; |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 321 | TEXT runtime·cas(SB), 7, $0-12 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 322 | MOVL 4(SP), BX |
| 323 | MOVL 8(SP), AX |
| 324 | MOVL 12(SP), CX |
| 325 | LOCK |
| 326 | CMPXCHGL CX, 0(BX) |
| 327 | JZ 3(PC) |
| 328 | MOVL $0, AX |
| 329 | RET |
| 330 | MOVL $1, AX |
| 331 | RET |
| 332 | |
Russ Cox | f70a19f | 2013-07-12 00:42:46 -0400 | [diff] [blame] | 333 | // bool runtime·cas64(uint64 *val, uint64 old, uint64 new) |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 334 | // Atomically: |
| 335 | // if(*val == *old){ |
| 336 | // *val = new; |
| 337 | // return 1; |
| 338 | // } else { |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 339 | // return 0; |
| 340 | // } |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 341 | TEXT runtime·cas64(SB), 7, $0-20 |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 342 | MOVL 4(SP), BP |
Russ Cox | f70a19f | 2013-07-12 00:42:46 -0400 | [diff] [blame] | 343 | MOVL 8(SP), AX |
| 344 | MOVL 12(SP), DX |
| 345 | MOVL 16(SP), BX |
| 346 | MOVL 20(SP), CX |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 347 | LOCK |
| 348 | CMPXCHG8B 0(BP) |
| 349 | JNZ cas64_fail |
| 350 | MOVL $1, AX |
| 351 | RET |
| 352 | cas64_fail: |
Dmitriy Vyukov | fd04f05 | 2012-04-05 18:59:50 +0400 | [diff] [blame] | 353 | MOVL $0, AX |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 354 | RET |
| 355 | |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 356 | // bool casp(void **p, void *old, void *new) |
| 357 | // Atomically: |
| 358 | // if(*p == old){ |
| 359 | // *p = new; |
| 360 | // return 1; |
| 361 | // }else |
| 362 | // return 0; |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 363 | TEXT runtime·casp(SB), 7, $0-12 |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 364 | MOVL 4(SP), BX |
| 365 | MOVL 8(SP), AX |
| 366 | MOVL 12(SP), CX |
| 367 | LOCK |
| 368 | CMPXCHGL CX, 0(BX) |
| 369 | JZ 3(PC) |
| 370 | MOVL $0, AX |
| 371 | RET |
| 372 | MOVL $1, AX |
| 373 | RET |
| 374 | |
Dmitriy Vyukov | 491aa15 | 2011-07-15 11:27:16 -0400 | [diff] [blame] | 375 | // uint32 xadd(uint32 volatile *val, int32 delta) |
| 376 | // Atomically: |
| 377 | // *val += delta; |
| 378 | // return *val; |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 379 | TEXT runtime·xadd(SB), 7, $0-8 |
Dmitriy Vyukov | 491aa15 | 2011-07-15 11:27:16 -0400 | [diff] [blame] | 380 | MOVL 4(SP), BX |
| 381 | MOVL 8(SP), AX |
| 382 | MOVL AX, CX |
| 383 | LOCK |
| 384 | XADDL AX, 0(BX) |
| 385 | ADDL CX, AX |
| 386 | RET |
| 387 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 388 | TEXT runtime·xchg(SB), 7, $0-8 |
Dmitriy Vyukov | 4e5086b | 2011-07-29 12:44:06 -0400 | [diff] [blame] | 389 | MOVL 4(SP), BX |
| 390 | MOVL 8(SP), AX |
| 391 | XCHGL AX, 0(BX) |
| 392 | RET |
| 393 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 394 | TEXT runtime·procyield(SB),7,$0-0 |
Dmitriy Vyukov | 4e5086b | 2011-07-29 12:44:06 -0400 | [diff] [blame] | 395 | MOVL 4(SP), AX |
| 396 | again: |
| 397 | PAUSE |
| 398 | SUBL $1, AX |
| 399 | JNZ again |
| 400 | RET |
| 401 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 402 | TEXT runtime·atomicstorep(SB), 7, $0-8 |
Dmitriy Vyukov | 86a659c | 2011-07-13 11:22:41 -0700 | [diff] [blame] | 403 | MOVL 4(SP), BX |
| 404 | MOVL 8(SP), AX |
| 405 | XCHGL AX, 0(BX) |
| 406 | RET |
| 407 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 408 | TEXT runtime·atomicstore(SB), 7, $0-8 |
Dmitriy Vyukov | 91f0f18 | 2011-07-29 13:47:24 -0400 | [diff] [blame] | 409 | MOVL 4(SP), BX |
| 410 | MOVL 8(SP), AX |
| 411 | XCHGL AX, 0(BX) |
| 412 | RET |
| 413 | |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 414 | // uint64 atomicload64(uint64 volatile* addr); |
| 415 | // so actually |
| 416 | // void atomicload64(uint64 *res, uint64 volatile *addr); |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 417 | TEXT runtime·atomicload64(SB), 7, $0-8 |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 418 | MOVL 4(SP), BX |
| 419 | MOVL 8(SP), AX |
| 420 | // MOVQ (%EAX), %MM0 |
| 421 | BYTE $0x0f; BYTE $0x6f; BYTE $0x00 |
| 422 | // MOVQ %MM0, 0(%EBX) |
| 423 | BYTE $0x0f; BYTE $0x7f; BYTE $0x03 |
| 424 | // EMMS |
| 425 | BYTE $0x0F; BYTE $0x77 |
| 426 | RET |
| 427 | |
| 428 | // void runtime·atomicstore64(uint64 volatile* addr, uint64 v); |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 429 | TEXT runtime·atomicstore64(SB), 7, $0-12 |
Dmitriy Vyukov | 4667571 | 2012-04-05 18:47:43 +0400 | [diff] [blame] | 430 | MOVL 4(SP), AX |
| 431 | // MOVQ and EMMS were introduced on the Pentium MMX. |
| 432 | // MOVQ 0x8(%ESP), %MM0 |
| 433 | BYTE $0x0f; BYTE $0x6f; BYTE $0x44; BYTE $0x24; BYTE $0x08 |
| 434 | // MOVQ %MM0, (%EAX) |
| 435 | BYTE $0x0f; BYTE $0x7f; BYTE $0x00 |
| 436 | // EMMS |
| 437 | BYTE $0x0F; BYTE $0x77 |
| 438 | // This is essentially a no-op, but it provides required memory fencing. |
| 439 | // It can be replaced with MFENCE, but MFENCE was introduced only on the Pentium4 (SSE2). |
| 440 | MOVL $0, AX |
| 441 | LOCK |
| 442 | XADDL AX, (SP) |
| 443 | RET |
| 444 | |
Russ Cox | aa3222d8 | 2009-06-02 23:02:12 -0700 | [diff] [blame] | 445 | // void jmpdefer(fn, sp); |
| 446 | // called from deferreturn. |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 447 | // 1. pop the caller |
| 448 | // 2. sub 5 bytes from the callers return |
| 449 | // 3. jmp to the argument |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 450 | TEXT runtime·jmpdefer(SB), 7, $0 |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 451 | MOVL 4(SP), DX // fn |
Russ Cox | aa3222d8 | 2009-06-02 23:02:12 -0700 | [diff] [blame] | 452 | MOVL 8(SP), BX // caller sp |
| 453 | LEAL -4(BX), SP // caller sp after CALL |
| 454 | SUBL $5, (SP) // return to CALL again |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 455 | MOVL 0(DX), BX |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 456 | JMP BX // but first run the deferred function |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 457 | |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 458 | // Save state of caller into g->sched. |
| 459 | TEXT gosave<>(SB),7,$0 |
| 460 | PUSHL AX |
| 461 | PUSHL BX |
| 462 | get_tls(BX) |
| 463 | MOVL g(BX), BX |
| 464 | LEAL arg+0(FP), AX |
| 465 | MOVL AX, (g_sched+gobuf_sp)(BX) |
| 466 | MOVL -4(AX), AX |
| 467 | MOVL AX, (g_sched+gobuf_pc)(BX) |
| 468 | MOVL $0, (g_sched+gobuf_ret)(BX) |
| 469 | MOVL $0, (g_sched+gobuf_ctxt)(BX) |
| 470 | POPL BX |
| 471 | POPL AX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 472 | RET |
| 473 | |
| 474 | // asmcgocall(void(*fn)(void*), void *arg) |
| 475 | // Call fn(arg) on the scheduler stack, |
| 476 | // aligned appropriately for the gcc ABI. |
| 477 | // See cgocall.c for more details. |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 478 | TEXT runtime·asmcgocall(SB),7,$0-8 |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 479 | MOVL fn+0(FP), AX |
| 480 | MOVL arg+4(FP), BX |
| 481 | MOVL SP, DX |
| 482 | |
| 483 | // Figure out if we need to switch to m->g0 stack. |
| 484 | // We get called to create new OS threads too, and those |
| 485 | // come in on the m->g0 stack already. |
| 486 | get_tls(CX) |
| 487 | MOVL m(CX), BP |
| 488 | MOVL m_g0(BP), SI |
| 489 | MOVL g(CX), DI |
| 490 | CMPL SI, DI |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 491 | JEQ 4(PC) |
| 492 | CALL gosave<>(SB) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 493 | MOVL SI, g(CX) |
| 494 | MOVL (g_sched+gobuf_sp)(SI), SP |
| 495 | |
| 496 | // Now on a scheduling stack (a pthread-created stack). |
| 497 | SUBL $32, SP |
| 498 | ANDL $~15, SP // alignment, perhaps unnecessary |
| 499 | MOVL DI, 8(SP) // save g |
| 500 | MOVL DX, 4(SP) // save SP |
| 501 | MOVL BX, 0(SP) // first argument in x86-32 ABI |
| 502 | CALL AX |
| 503 | |
| 504 | // Restore registers, g, stack pointer. |
| 505 | get_tls(CX) |
| 506 | MOVL 8(SP), DI |
| 507 | MOVL DI, g(CX) |
| 508 | MOVL 4(SP), SP |
| 509 | RET |
| 510 | |
| 511 | // cgocallback(void (*fn)(void*), void *frame, uintptr framesize) |
Russ Cox | 3d2dfc5 | 2013-02-22 16:08:56 -0500 | [diff] [blame] | 512 | // Turn the fn into a Go func (by taking its address) and call |
| 513 | // cgocallback_gofunc. |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 514 | TEXT runtime·cgocallback(SB),7,$12-12 |
Russ Cox | 3d2dfc5 | 2013-02-22 16:08:56 -0500 | [diff] [blame] | 515 | LEAL fn+0(FP), AX |
| 516 | MOVL AX, 0(SP) |
| 517 | MOVL frame+4(FP), AX |
| 518 | MOVL AX, 4(SP) |
| 519 | MOVL framesize+8(FP), AX |
| 520 | MOVL AX, 8(SP) |
| 521 | MOVL $runtime·cgocallback_gofunc(SB), AX |
| 522 | CALL AX |
| 523 | RET |
| 524 | |
| 525 | // cgocallback_gofunc(FuncVal*, void *frame, uintptr framesize) |
| 526 | // See cgocall.c for more details. |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 527 | TEXT runtime·cgocallback_gofunc(SB),7,$12-12 |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 528 | // If m is nil, Go did not create the current thread. |
| 529 | // Call needm to obtain one for temporary use. |
| 530 | // In this case, we're running on the thread stack, so there's |
| 531 | // lots of space, but the linker doesn't know. Hide the call from |
| 532 | // the linker analysis by using an indirect call through AX. |
| 533 | get_tls(CX) |
| 534 | #ifdef GOOS_windows |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 535 | MOVL $0, BP |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 536 | CMPL CX, $0 |
Russ Cox | cefdb9c | 2013-07-23 22:59:32 -0400 | [diff] [blame] | 537 | JEQ 2(PC) |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 538 | #endif |
| 539 | MOVL m(CX), BP |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 540 | MOVL BP, DX // saved copy of oldm |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 541 | CMPL BP, $0 |
| 542 | JNE havem |
| 543 | needm: |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 544 | MOVL DX, 0(SP) |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 545 | MOVL $runtime·needm(SB), AX |
| 546 | CALL AX |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 547 | MOVL 0(SP), DX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 548 | get_tls(CX) |
| 549 | MOVL m(CX), BP |
Russ Cox | 9b73238 | 2012-03-08 12:12:40 -0500 | [diff] [blame] | 550 | |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 551 | havem: |
| 552 | // Now there's a valid m, and we're running on its m->g0. |
| 553 | // Save current m->g0->sched.sp on stack and then set it to SP. |
| 554 | // Save current sp in m->g0->sched.sp in preparation for |
| 555 | // switch back to m->curg stack. |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 556 | // NOTE: unwindm knows that the saved g->sched.sp is at 0(SP). |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 557 | // On Windows, the SEH is at 4(SP) and 8(SP). |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 558 | MOVL m_g0(BP), SI |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 559 | MOVL (g_sched+gobuf_sp)(SI), AX |
| 560 | MOVL AX, 0(SP) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 561 | MOVL SP, (g_sched+gobuf_sp)(SI) |
| 562 | |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 563 | // Switch to m->curg stack and call runtime.cgocallbackg. |
| 564 | // Because we are taking over the execution of m->curg |
| 565 | // but *not* resuming what had been running, we need to |
| 566 | // save that information (m->curg->sched) so we can restore it. |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 567 | // We can restore m->curg->sched.sp easily, because calling |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 568 | // runtime.cgocallbackg leaves SP unchanged upon return. |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 569 | // To save m->curg->sched.pc, we push it onto the stack. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 570 | // This has the added benefit that it looks to the traceback |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 571 | // routine like cgocallbackg is going to return to that |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 572 | // PC (because the frame we allocate below has the same |
| 573 | // size as cgocallback_gofunc's frame declared above) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 574 | // so that the traceback will seamlessly trace back into |
| 575 | // the earlier calls. |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 576 | // |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 577 | // In the new goroutine, 0(SP) holds the saved oldm (DX) register. |
| 578 | // 4(SP) and 8(SP) are unused. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 579 | MOVL m_curg(BP), SI |
| 580 | MOVL SI, g(CX) |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 581 | MOVL (g_sched+gobuf_sp)(SI), DI // prepare stack as DI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 582 | MOVL (g_sched+gobuf_pc)(SI), BP |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 583 | MOVL BP, -4(DI) |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 584 | LEAL -(4+12)(DI), SP |
| 585 | MOVL DX, 0(SP) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 586 | CALL runtime·cgocallbackg(SB) |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 587 | MOVL 0(SP), DX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 588 | |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 589 | // Restore g->sched (== m->curg->sched) from saved values. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 590 | get_tls(CX) |
| 591 | MOVL g(CX), SI |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 592 | MOVL 12(SP), BP |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 593 | MOVL BP, (g_sched+gobuf_pc)(SI) |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 594 | LEAL (12+4)(SP), DI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 595 | MOVL DI, (g_sched+gobuf_sp)(SI) |
| 596 | |
| 597 | // Switch back to m->g0's stack and restore m->g0->sched.sp. |
| 598 | // (Unlike m->curg, the g0 goroutine never uses sched.pc, |
| 599 | // so we do not have to restore it.) |
| 600 | MOVL m(CX), BP |
| 601 | MOVL m_g0(BP), SI |
| 602 | MOVL SI, g(CX) |
| 603 | MOVL (g_sched+gobuf_sp)(SI), SP |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 604 | MOVL 0(SP), AX |
| 605 | MOVL AX, (g_sched+gobuf_sp)(SI) |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 606 | |
| 607 | // If the m on entry was nil, we called needm above to borrow an m |
| 608 | // for the duration of the call. Since the call is over, return it with dropm. |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame^] | 609 | CMPL DX, $0 |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 610 | JNE 3(PC) |
| 611 | MOVL $runtime·dropm(SB), AX |
| 612 | CALL AX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 613 | |
| 614 | // Done! |
| 615 | RET |
| 616 | |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 617 | // void setmg(M*, G*); set m and g. for use by needm. |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 618 | TEXT runtime·setmg(SB), 7, $0-8 |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 619 | #ifdef GOOS_windows |
| 620 | MOVL mm+0(FP), AX |
| 621 | CMPL AX, $0 |
| 622 | JNE settls |
| 623 | MOVL $0, 0x14(FS) |
| 624 | RET |
| 625 | settls: |
| 626 | LEAL m_tls(AX), AX |
| 627 | MOVL AX, 0x14(FS) |
| 628 | #endif |
| 629 | MOVL mm+0(FP), AX |
| 630 | get_tls(CX) |
| 631 | MOVL mm+0(FP), AX |
| 632 | MOVL AX, m(CX) |
| 633 | MOVL gg+4(FP), BX |
| 634 | MOVL BX, g(CX) |
| 635 | RET |
| 636 | |
Russ Cox | 6a70f9d | 2013-03-25 18:14:02 -0400 | [diff] [blame] | 637 | // void setmg_gcc(M*, G*); set m and g. for use by gcc |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 638 | TEXT setmg_gcc<>(SB), 7, $0 |
Russ Cox | 6a70f9d | 2013-03-25 18:14:02 -0400 | [diff] [blame] | 639 | get_tls(AX) |
| 640 | MOVL mm+0(FP), DX |
| 641 | MOVL DX, m(AX) |
| 642 | MOVL gg+4(FP), DX |
| 643 | MOVL DX,g (AX) |
| 644 | RET |
| 645 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 646 | // check that SP is in range [g->stackbase, g->stackguard) |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 647 | TEXT runtime·stackcheck(SB), 7, $0-0 |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 648 | get_tls(CX) |
| 649 | MOVL g(CX), AX |
| 650 | CMPL g_stackbase(AX), SP |
| 651 | JHI 2(PC) |
| 652 | INT $3 |
| 653 | CMPL SP, g_stackguard(AX) |
| 654 | JHI 2(PC) |
| 655 | INT $3 |
| 656 | RET |
| 657 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 658 | TEXT runtime·memclr(SB),7,$0-8 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 659 | MOVL 4(SP), DI // arg 1 addr |
| 660 | MOVL 8(SP), CX // arg 2 count |
Quan Yong Zhai | 47410a2 | 2011-07-23 15:46:58 -0400 | [diff] [blame] | 661 | MOVL CX, BX |
| 662 | ANDL $3, BX |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 663 | SHRL $2, CX |
| 664 | MOVL $0, AX |
| 665 | CLD |
| 666 | REP |
| 667 | STOSL |
Quan Yong Zhai | 47410a2 | 2011-07-23 15:46:58 -0400 | [diff] [blame] | 668 | MOVL BX, CX |
| 669 | REP |
| 670 | STOSB |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 671 | RET |
| 672 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 673 | TEXT runtime·getcallerpc(SB),7,$0-4 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 674 | MOVL x+0(FP),AX // addr of first arg |
| 675 | MOVL -4(AX),AX // get calling pc |
| 676 | RET |
| 677 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 678 | TEXT runtime·setcallerpc(SB),7,$0-8 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 679 | MOVL x+0(FP),AX // addr of first arg |
| 680 | MOVL x+4(FP), BX |
| 681 | MOVL BX, -4(AX) // set calling pc |
| 682 | RET |
| 683 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 684 | TEXT runtime·getcallersp(SB), 7, $0-4 |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 685 | MOVL sp+0(FP), AX |
| 686 | RET |
| 687 | |
Damian Gryski | 8e765da | 2012-02-02 14:09:27 -0500 | [diff] [blame] | 688 | // int64 runtime·cputicks(void), so really |
| 689 | // void runtime·cputicks(int64 *ticks) |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 690 | TEXT runtime·cputicks(SB),7,$0-4 |
Shenghou Ma | 6392b43 | 2012-02-06 12:49:28 -0500 | [diff] [blame] | 691 | RDTSC |
Damian Gryski | 8e765da | 2012-02-02 14:09:27 -0500 | [diff] [blame] | 692 | MOVL ret+0(FP), DI |
| 693 | MOVL AX, 0(DI) |
| 694 | MOVL DX, 4(DI) |
| 695 | RET |
| 696 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 697 | TEXT runtime·ldt0setup(SB),7,$16-0 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 698 | // set up ldt 7 to point at tls0 |
| 699 | // ldt 1 would be fine on Linux, but on OS X, 7 is as low as we can go. |
Russ Cox | 1b14bdb | 2009-09-22 16:28:32 -0700 | [diff] [blame] | 700 | // the entry number is just a hint. setldt will set up GS with what it used. |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 701 | MOVL $7, 0(SP) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 702 | LEAL runtime·tls0(SB), AX |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 703 | MOVL AX, 4(SP) |
| 704 | MOVL $32, 8(SP) // sizeof(tls array) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 705 | CALL runtime·setldt(SB) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 706 | RET |
| 707 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 708 | TEXT runtime·emptyfunc(SB),0,$0-0 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 709 | RET |
| 710 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 711 | TEXT runtime·abort(SB),7,$0-0 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 712 | INT $0x3 |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 713 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 714 | TEXT runtime·stackguard(SB),7,$0-8 |
Russ Cox | 9e5db8c | 2012-03-15 15:22:30 -0400 | [diff] [blame] | 715 | MOVL SP, DX |
| 716 | MOVL DX, sp+0(FP) |
| 717 | get_tls(CX) |
| 718 | MOVL g(CX), BX |
| 719 | MOVL g_stackguard(BX), DX |
Russ Cox | 07720b6 | 2013-03-22 12:57:55 -0400 | [diff] [blame] | 720 | MOVL DX, limit+4(FP) |
Russ Cox | 9e5db8c | 2012-03-15 15:22:30 -0400 | [diff] [blame] | 721 | RET |
| 722 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 723 | GLOBL runtime·tls0(SB), $32 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 724 | |
| 725 | // hash function using AES hardware instructions |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 726 | TEXT runtime·aeshash(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 727 | MOVL 4(SP), DX // ptr to hash value |
| 728 | MOVL 8(SP), CX // size |
| 729 | MOVL 12(SP), AX // ptr to data |
| 730 | JMP runtime·aeshashbody(SB) |
| 731 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 732 | TEXT runtime·aeshashstr(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 733 | MOVL 4(SP), DX // ptr to hash value |
| 734 | MOVL 12(SP), AX // ptr to string struct |
| 735 | MOVL 4(AX), CX // length of string |
| 736 | MOVL (AX), AX // string data |
| 737 | JMP runtime·aeshashbody(SB) |
| 738 | |
| 739 | // AX: data |
| 740 | // CX: length |
| 741 | // DX: ptr to seed input / hash output |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 742 | TEXT runtime·aeshashbody(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 743 | MOVL (DX), X0 // seed to low 32 bits of xmm0 |
| 744 | PINSRD $1, CX, X0 // size to next 32 bits of xmm0 |
Keith Randall | db53d97 | 2013-03-20 14:34:26 -0700 | [diff] [blame] | 745 | MOVO runtime·aeskeysched+0(SB), X2 |
| 746 | MOVO runtime·aeskeysched+16(SB), X3 |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 747 | CMPL CX, $16 |
| 748 | JB aessmall |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 749 | aesloop: |
| 750 | CMPL CX, $16 |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 751 | JBE aesloopend |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 752 | MOVOU (AX), X1 |
| 753 | AESENC X2, X0 |
| 754 | AESENC X1, X0 |
| 755 | SUBL $16, CX |
| 756 | ADDL $16, AX |
| 757 | JMP aesloop |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 758 | // 1-16 bytes remaining |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 759 | aesloopend: |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 760 | // This load may overlap with the previous load above. |
| 761 | // We'll hash some bytes twice, but that's ok. |
| 762 | MOVOU -16(AX)(CX*1), X1 |
| 763 | JMP partial |
| 764 | // 0-15 bytes |
| 765 | aessmall: |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 766 | TESTL CX, CX |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 767 | JE finalize // 0 bytes |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 768 | |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 769 | CMPB AX, $0xf0 |
| 770 | JA highpartial |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 771 | |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 772 | // 16 bytes loaded at this address won't cross |
| 773 | // a page boundary, so we can load it directly. |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 774 | MOVOU (AX), X1 |
| 775 | ADDL CX, CX |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 776 | PAND masks<>(SB)(CX*8), X1 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 777 | JMP partial |
| 778 | highpartial: |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 779 | // address ends in 1111xxxx. Might be up against |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 780 | // a page boundary, so load ending at last byte. |
| 781 | // Then shift bytes down using pshufb. |
| 782 | MOVOU -16(AX)(CX*1), X1 |
| 783 | ADDL CX, CX |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 784 | PSHUFB shifts<>(SB)(CX*8), X1 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 785 | partial: |
| 786 | // incorporate partial block into hash |
| 787 | AESENC X3, X0 |
| 788 | AESENC X1, X0 |
| 789 | finalize: |
| 790 | // finalize hash |
| 791 | AESENC X2, X0 |
| 792 | AESENC X3, X0 |
| 793 | AESENC X2, X0 |
| 794 | MOVL X0, (DX) |
| 795 | RET |
| 796 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 797 | TEXT runtime·aeshash32(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 798 | MOVL 4(SP), DX // ptr to hash value |
| 799 | MOVL 12(SP), AX // ptr to data |
| 800 | MOVL (DX), X0 // seed |
| 801 | PINSRD $1, (AX), X0 // data |
Keith Randall | db53d97 | 2013-03-20 14:34:26 -0700 | [diff] [blame] | 802 | AESENC runtime·aeskeysched+0(SB), X0 |
| 803 | AESENC runtime·aeskeysched+16(SB), X0 |
| 804 | AESENC runtime·aeskeysched+0(SB), X0 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 805 | MOVL X0, (DX) |
| 806 | RET |
| 807 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 808 | TEXT runtime·aeshash64(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 809 | MOVL 4(SP), DX // ptr to hash value |
| 810 | MOVL 12(SP), AX // ptr to data |
| 811 | MOVQ (AX), X0 // data |
| 812 | PINSRD $2, (DX), X0 // seed |
Keith Randall | db53d97 | 2013-03-20 14:34:26 -0700 | [diff] [blame] | 813 | AESENC runtime·aeskeysched+0(SB), X0 |
| 814 | AESENC runtime·aeskeysched+16(SB), X0 |
| 815 | AESENC runtime·aeskeysched+0(SB), X0 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 816 | MOVL X0, (DX) |
| 817 | RET |
| 818 | |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 819 | // simple mask to get rid of data in the high part of the register. |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 820 | DATA masks<>+0x00(SB)/4, $0x00000000 |
| 821 | DATA masks<>+0x04(SB)/4, $0x00000000 |
| 822 | DATA masks<>+0x08(SB)/4, $0x00000000 |
| 823 | DATA masks<>+0x0c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 824 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 825 | DATA masks<>+0x10(SB)/4, $0x000000ff |
| 826 | DATA masks<>+0x14(SB)/4, $0x00000000 |
| 827 | DATA masks<>+0x18(SB)/4, $0x00000000 |
| 828 | DATA masks<>+0x1c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 829 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 830 | DATA masks<>+0x20(SB)/4, $0x0000ffff |
| 831 | DATA masks<>+0x24(SB)/4, $0x00000000 |
| 832 | DATA masks<>+0x28(SB)/4, $0x00000000 |
| 833 | DATA masks<>+0x2c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 834 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 835 | DATA masks<>+0x30(SB)/4, $0x00ffffff |
| 836 | DATA masks<>+0x34(SB)/4, $0x00000000 |
| 837 | DATA masks<>+0x38(SB)/4, $0x00000000 |
| 838 | DATA masks<>+0x3c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 839 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 840 | DATA masks<>+0x40(SB)/4, $0xffffffff |
| 841 | DATA masks<>+0x44(SB)/4, $0x00000000 |
| 842 | DATA masks<>+0x48(SB)/4, $0x00000000 |
| 843 | DATA masks<>+0x4c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 844 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 845 | DATA masks<>+0x50(SB)/4, $0xffffffff |
| 846 | DATA masks<>+0x54(SB)/4, $0x000000ff |
| 847 | DATA masks<>+0x58(SB)/4, $0x00000000 |
| 848 | DATA masks<>+0x5c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 849 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 850 | DATA masks<>+0x60(SB)/4, $0xffffffff |
| 851 | DATA masks<>+0x64(SB)/4, $0x0000ffff |
| 852 | DATA masks<>+0x68(SB)/4, $0x00000000 |
| 853 | DATA masks<>+0x6c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 854 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 855 | DATA masks<>+0x70(SB)/4, $0xffffffff |
| 856 | DATA masks<>+0x74(SB)/4, $0x00ffffff |
| 857 | DATA masks<>+0x78(SB)/4, $0x00000000 |
| 858 | DATA masks<>+0x7c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 859 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 860 | DATA masks<>+0x80(SB)/4, $0xffffffff |
| 861 | DATA masks<>+0x84(SB)/4, $0xffffffff |
| 862 | DATA masks<>+0x88(SB)/4, $0x00000000 |
| 863 | DATA masks<>+0x8c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 864 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 865 | DATA masks<>+0x90(SB)/4, $0xffffffff |
| 866 | DATA masks<>+0x94(SB)/4, $0xffffffff |
| 867 | DATA masks<>+0x98(SB)/4, $0x000000ff |
| 868 | DATA masks<>+0x9c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 869 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 870 | DATA masks<>+0xa0(SB)/4, $0xffffffff |
| 871 | DATA masks<>+0xa4(SB)/4, $0xffffffff |
| 872 | DATA masks<>+0xa8(SB)/4, $0x0000ffff |
| 873 | DATA masks<>+0xac(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 874 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 875 | DATA masks<>+0xb0(SB)/4, $0xffffffff |
| 876 | DATA masks<>+0xb4(SB)/4, $0xffffffff |
| 877 | DATA masks<>+0xb8(SB)/4, $0x00ffffff |
| 878 | DATA masks<>+0xbc(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 879 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 880 | DATA masks<>+0xc0(SB)/4, $0xffffffff |
| 881 | DATA masks<>+0xc4(SB)/4, $0xffffffff |
| 882 | DATA masks<>+0xc8(SB)/4, $0xffffffff |
| 883 | DATA masks<>+0xcc(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 884 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 885 | DATA masks<>+0xd0(SB)/4, $0xffffffff |
| 886 | DATA masks<>+0xd4(SB)/4, $0xffffffff |
| 887 | DATA masks<>+0xd8(SB)/4, $0xffffffff |
| 888 | DATA masks<>+0xdc(SB)/4, $0x000000ff |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 889 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 890 | DATA masks<>+0xe0(SB)/4, $0xffffffff |
| 891 | DATA masks<>+0xe4(SB)/4, $0xffffffff |
| 892 | DATA masks<>+0xe8(SB)/4, $0xffffffff |
| 893 | DATA masks<>+0xec(SB)/4, $0x0000ffff |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 894 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 895 | DATA masks<>+0xf0(SB)/4, $0xffffffff |
| 896 | DATA masks<>+0xf4(SB)/4, $0xffffffff |
| 897 | DATA masks<>+0xf8(SB)/4, $0xffffffff |
| 898 | DATA masks<>+0xfc(SB)/4, $0x00ffffff |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 899 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 900 | GLOBL masks<>(SB),8,$256 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 901 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 902 | // these are arguments to pshufb. They move data down from |
| 903 | // the high bytes of the register to the low bytes of the register. |
| 904 | // index is how many bytes to move. |
| 905 | DATA shifts<>+0x00(SB)/4, $0x00000000 |
| 906 | DATA shifts<>+0x04(SB)/4, $0x00000000 |
| 907 | DATA shifts<>+0x08(SB)/4, $0x00000000 |
| 908 | DATA shifts<>+0x0c(SB)/4, $0x00000000 |
| 909 | |
| 910 | DATA shifts<>+0x10(SB)/4, $0xffffff0f |
| 911 | DATA shifts<>+0x14(SB)/4, $0xffffffff |
| 912 | DATA shifts<>+0x18(SB)/4, $0xffffffff |
| 913 | DATA shifts<>+0x1c(SB)/4, $0xffffffff |
| 914 | |
| 915 | DATA shifts<>+0x20(SB)/4, $0xffff0f0e |
| 916 | DATA shifts<>+0x24(SB)/4, $0xffffffff |
| 917 | DATA shifts<>+0x28(SB)/4, $0xffffffff |
| 918 | DATA shifts<>+0x2c(SB)/4, $0xffffffff |
| 919 | |
| 920 | DATA shifts<>+0x30(SB)/4, $0xff0f0e0d |
| 921 | DATA shifts<>+0x34(SB)/4, $0xffffffff |
| 922 | DATA shifts<>+0x38(SB)/4, $0xffffffff |
| 923 | DATA shifts<>+0x3c(SB)/4, $0xffffffff |
| 924 | |
| 925 | DATA shifts<>+0x40(SB)/4, $0x0f0e0d0c |
| 926 | DATA shifts<>+0x44(SB)/4, $0xffffffff |
| 927 | DATA shifts<>+0x48(SB)/4, $0xffffffff |
| 928 | DATA shifts<>+0x4c(SB)/4, $0xffffffff |
| 929 | |
| 930 | DATA shifts<>+0x50(SB)/4, $0x0e0d0c0b |
| 931 | DATA shifts<>+0x54(SB)/4, $0xffffff0f |
| 932 | DATA shifts<>+0x58(SB)/4, $0xffffffff |
| 933 | DATA shifts<>+0x5c(SB)/4, $0xffffffff |
| 934 | |
| 935 | DATA shifts<>+0x60(SB)/4, $0x0d0c0b0a |
| 936 | DATA shifts<>+0x64(SB)/4, $0xffff0f0e |
| 937 | DATA shifts<>+0x68(SB)/4, $0xffffffff |
| 938 | DATA shifts<>+0x6c(SB)/4, $0xffffffff |
| 939 | |
| 940 | DATA shifts<>+0x70(SB)/4, $0x0c0b0a09 |
| 941 | DATA shifts<>+0x74(SB)/4, $0xff0f0e0d |
| 942 | DATA shifts<>+0x78(SB)/4, $0xffffffff |
| 943 | DATA shifts<>+0x7c(SB)/4, $0xffffffff |
| 944 | |
| 945 | DATA shifts<>+0x80(SB)/4, $0x0b0a0908 |
| 946 | DATA shifts<>+0x84(SB)/4, $0x0f0e0d0c |
| 947 | DATA shifts<>+0x88(SB)/4, $0xffffffff |
| 948 | DATA shifts<>+0x8c(SB)/4, $0xffffffff |
| 949 | |
| 950 | DATA shifts<>+0x90(SB)/4, $0x0a090807 |
| 951 | DATA shifts<>+0x94(SB)/4, $0x0e0d0c0b |
| 952 | DATA shifts<>+0x98(SB)/4, $0xffffff0f |
| 953 | DATA shifts<>+0x9c(SB)/4, $0xffffffff |
| 954 | |
| 955 | DATA shifts<>+0xa0(SB)/4, $0x09080706 |
| 956 | DATA shifts<>+0xa4(SB)/4, $0x0d0c0b0a |
| 957 | DATA shifts<>+0xa8(SB)/4, $0xffff0f0e |
| 958 | DATA shifts<>+0xac(SB)/4, $0xffffffff |
| 959 | |
| 960 | DATA shifts<>+0xb0(SB)/4, $0x08070605 |
| 961 | DATA shifts<>+0xb4(SB)/4, $0x0c0b0a09 |
| 962 | DATA shifts<>+0xb8(SB)/4, $0xff0f0e0d |
| 963 | DATA shifts<>+0xbc(SB)/4, $0xffffffff |
| 964 | |
| 965 | DATA shifts<>+0xc0(SB)/4, $0x07060504 |
| 966 | DATA shifts<>+0xc4(SB)/4, $0x0b0a0908 |
| 967 | DATA shifts<>+0xc8(SB)/4, $0x0f0e0d0c |
| 968 | DATA shifts<>+0xcc(SB)/4, $0xffffffff |
| 969 | |
| 970 | DATA shifts<>+0xd0(SB)/4, $0x06050403 |
| 971 | DATA shifts<>+0xd4(SB)/4, $0x0a090807 |
| 972 | DATA shifts<>+0xd8(SB)/4, $0x0e0d0c0b |
| 973 | DATA shifts<>+0xdc(SB)/4, $0xffffff0f |
| 974 | |
| 975 | DATA shifts<>+0xe0(SB)/4, $0x05040302 |
| 976 | DATA shifts<>+0xe4(SB)/4, $0x09080706 |
| 977 | DATA shifts<>+0xe8(SB)/4, $0x0d0c0b0a |
| 978 | DATA shifts<>+0xec(SB)/4, $0xffff0f0e |
| 979 | |
| 980 | DATA shifts<>+0xf0(SB)/4, $0x04030201 |
| 981 | DATA shifts<>+0xf4(SB)/4, $0x08070605 |
| 982 | DATA shifts<>+0xf8(SB)/4, $0x0c0b0a09 |
| 983 | DATA shifts<>+0xfc(SB)/4, $0xff0f0e0d |
| 984 | |
| 985 | GLOBL shifts<>(SB),8,$256 |
| 986 | |
| 987 | TEXT runtime·memeq(SB),7,$0-12 |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 988 | MOVL a+0(FP), SI |
| 989 | MOVL b+4(FP), DI |
| 990 | MOVL count+8(FP), BX |
| 991 | JMP runtime·memeqbody(SB) |
| 992 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 993 | TEXT bytes·Equal(SB),7,$0-25 |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 994 | MOVL a_len+4(FP), BX |
| 995 | MOVL b_len+16(FP), CX |
| 996 | XORL AX, AX |
| 997 | CMPL BX, CX |
| 998 | JNE eqret |
| 999 | MOVL a+0(FP), SI |
| 1000 | MOVL b+12(FP), DI |
| 1001 | CALL runtime·memeqbody(SB) |
| 1002 | eqret: |
| 1003 | MOVB AX, ret+24(FP) |
| 1004 | RET |
| 1005 | |
| 1006 | // a in SI |
| 1007 | // b in DI |
| 1008 | // count in BX |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1009 | TEXT runtime·memeqbody(SB),7,$0-0 |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1010 | XORL AX, AX |
| 1011 | |
| 1012 | CMPL BX, $4 |
| 1013 | JB small |
| 1014 | |
| 1015 | // 64 bytes at a time using xmm registers |
| 1016 | hugeloop: |
| 1017 | CMPL BX, $64 |
| 1018 | JB bigloop |
| 1019 | TESTL $0x4000000, runtime·cpuid_edx(SB) // check for sse2 |
| 1020 | JE bigloop |
| 1021 | MOVOU (SI), X0 |
| 1022 | MOVOU (DI), X1 |
| 1023 | MOVOU 16(SI), X2 |
| 1024 | MOVOU 16(DI), X3 |
| 1025 | MOVOU 32(SI), X4 |
| 1026 | MOVOU 32(DI), X5 |
| 1027 | MOVOU 48(SI), X6 |
| 1028 | MOVOU 48(DI), X7 |
| 1029 | PCMPEQB X1, X0 |
| 1030 | PCMPEQB X3, X2 |
| 1031 | PCMPEQB X5, X4 |
| 1032 | PCMPEQB X7, X6 |
| 1033 | PAND X2, X0 |
| 1034 | PAND X6, X4 |
| 1035 | PAND X4, X0 |
| 1036 | PMOVMSKB X0, DX |
| 1037 | ADDL $64, SI |
| 1038 | ADDL $64, DI |
| 1039 | SUBL $64, BX |
| 1040 | CMPL DX, $0xffff |
| 1041 | JEQ hugeloop |
| 1042 | RET |
| 1043 | |
| 1044 | // 4 bytes at a time using 32-bit register |
| 1045 | bigloop: |
| 1046 | CMPL BX, $4 |
| 1047 | JBE leftover |
| 1048 | MOVL (SI), CX |
| 1049 | MOVL (DI), DX |
| 1050 | ADDL $4, SI |
| 1051 | ADDL $4, DI |
| 1052 | SUBL $4, BX |
| 1053 | CMPL CX, DX |
| 1054 | JEQ bigloop |
| 1055 | RET |
| 1056 | |
| 1057 | // remaining 0-4 bytes |
| 1058 | leftover: |
| 1059 | MOVL -4(SI)(BX*1), CX |
| 1060 | MOVL -4(DI)(BX*1), DX |
| 1061 | CMPL CX, DX |
| 1062 | SETEQ AX |
| 1063 | RET |
| 1064 | |
| 1065 | small: |
| 1066 | CMPL BX, $0 |
| 1067 | JEQ equal |
| 1068 | |
| 1069 | LEAL 0(BX*8), CX |
| 1070 | NEGL CX |
| 1071 | |
| 1072 | MOVL SI, DX |
| 1073 | CMPB DX, $0xfc |
| 1074 | JA si_high |
| 1075 | |
| 1076 | // load at SI won't cross a page boundary. |
| 1077 | MOVL (SI), SI |
| 1078 | JMP si_finish |
| 1079 | si_high: |
| 1080 | // address ends in 111111xx. Load up to bytes we want, move to correct position. |
| 1081 | MOVL -4(SI)(BX*1), SI |
| 1082 | SHRL CX, SI |
| 1083 | si_finish: |
| 1084 | |
| 1085 | // same for DI. |
| 1086 | MOVL DI, DX |
| 1087 | CMPB DX, $0xfc |
| 1088 | JA di_high |
| 1089 | MOVL (DI), DI |
| 1090 | JMP di_finish |
| 1091 | di_high: |
| 1092 | MOVL -4(DI)(BX*1), DI |
| 1093 | SHRL CX, DI |
| 1094 | di_finish: |
| 1095 | |
| 1096 | SUBL SI, DI |
| 1097 | SHLL CX, DI |
| 1098 | equal: |
| 1099 | SETEQ AX |
| 1100 | RET |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1101 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1102 | TEXT runtime·cmpstring(SB),7,$0-20 |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1103 | MOVL s1+0(FP), SI |
| 1104 | MOVL s1+4(FP), BX |
| 1105 | MOVL s2+8(FP), DI |
| 1106 | MOVL s2+12(FP), DX |
| 1107 | CALL runtime·cmpbody(SB) |
| 1108 | MOVL AX, res+16(FP) |
| 1109 | RET |
| 1110 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1111 | TEXT bytes·Compare(SB),7,$0-28 |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1112 | MOVL s1+0(FP), SI |
| 1113 | MOVL s1+4(FP), BX |
| 1114 | MOVL s2+12(FP), DI |
| 1115 | MOVL s2+16(FP), DX |
| 1116 | CALL runtime·cmpbody(SB) |
| 1117 | MOVL AX, res+24(FP) |
| 1118 | RET |
| 1119 | |
| 1120 | // input: |
| 1121 | // SI = a |
| 1122 | // DI = b |
| 1123 | // BX = alen |
| 1124 | // DX = blen |
| 1125 | // output: |
| 1126 | // AX = 1/0/-1 |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1127 | TEXT runtime·cmpbody(SB),7,$0-0 |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1128 | CMPL SI, DI |
| 1129 | JEQ cmp_allsame |
| 1130 | CMPL BX, DX |
| 1131 | MOVL DX, BP |
| 1132 | CMOVLLT BX, BP // BP = min(alen, blen) |
| 1133 | CMPL BP, $4 |
| 1134 | JB cmp_small |
| 1135 | TESTL $0x4000000, runtime·cpuid_edx(SB) // check for sse2 |
| 1136 | JE cmp_mediumloop |
| 1137 | cmp_largeloop: |
| 1138 | CMPL BP, $16 |
| 1139 | JB cmp_mediumloop |
| 1140 | MOVOU (SI), X0 |
| 1141 | MOVOU (DI), X1 |
| 1142 | PCMPEQB X0, X1 |
| 1143 | PMOVMSKB X1, AX |
| 1144 | XORL $0xffff, AX // convert EQ to NE |
| 1145 | JNE cmp_diff16 // branch if at least one byte is not equal |
| 1146 | ADDL $16, SI |
| 1147 | ADDL $16, DI |
| 1148 | SUBL $16, BP |
| 1149 | JMP cmp_largeloop |
| 1150 | |
| 1151 | cmp_diff16: |
| 1152 | BSFL AX, BX // index of first byte that differs |
| 1153 | XORL AX, AX |
| 1154 | MOVB (SI)(BX*1), CX |
| 1155 | CMPB CX, (DI)(BX*1) |
| 1156 | SETHI AX |
| 1157 | LEAL -1(AX*2), AX // convert 1/0 to +1/-1 |
| 1158 | RET |
| 1159 | |
| 1160 | cmp_mediumloop: |
| 1161 | CMPL BP, $4 |
| 1162 | JBE cmp_0through4 |
| 1163 | MOVL (SI), AX |
| 1164 | MOVL (DI), CX |
| 1165 | CMPL AX, CX |
| 1166 | JNE cmp_diff4 |
| 1167 | ADDL $4, SI |
| 1168 | ADDL $4, DI |
| 1169 | SUBL $4, BP |
| 1170 | JMP cmp_mediumloop |
| 1171 | |
| 1172 | cmp_0through4: |
| 1173 | MOVL -4(SI)(BP*1), AX |
| 1174 | MOVL -4(DI)(BP*1), CX |
| 1175 | CMPL AX, CX |
| 1176 | JEQ cmp_allsame |
| 1177 | |
| 1178 | cmp_diff4: |
| 1179 | BSWAPL AX // reverse order of bytes |
| 1180 | BSWAPL CX |
| 1181 | XORL AX, CX // find bit differences |
| 1182 | BSRL CX, CX // index of highest bit difference |
| 1183 | SHRL CX, AX // move a's bit to bottom |
| 1184 | ANDL $1, AX // mask bit |
| 1185 | LEAL -1(AX*2), AX // 1/0 => +1/-1 |
| 1186 | RET |
| 1187 | |
| 1188 | // 0-3 bytes in common |
| 1189 | cmp_small: |
| 1190 | LEAL (BP*8), CX |
| 1191 | NEGL CX |
| 1192 | JEQ cmp_allsame |
| 1193 | |
| 1194 | // load si |
| 1195 | CMPB SI, $0xfc |
| 1196 | JA cmp_si_high |
| 1197 | MOVL (SI), SI |
| 1198 | JMP cmp_si_finish |
| 1199 | cmp_si_high: |
| 1200 | MOVL -4(SI)(BP*1), SI |
| 1201 | SHRL CX, SI |
| 1202 | cmp_si_finish: |
| 1203 | SHLL CX, SI |
| 1204 | |
| 1205 | // same for di |
| 1206 | CMPB DI, $0xfc |
| 1207 | JA cmp_di_high |
| 1208 | MOVL (DI), DI |
| 1209 | JMP cmp_di_finish |
| 1210 | cmp_di_high: |
| 1211 | MOVL -4(DI)(BP*1), DI |
| 1212 | SHRL CX, DI |
| 1213 | cmp_di_finish: |
| 1214 | SHLL CX, DI |
| 1215 | |
| 1216 | BSWAPL SI // reverse order of bytes |
| 1217 | BSWAPL DI |
| 1218 | XORL SI, DI // find bit differences |
| 1219 | JEQ cmp_allsame |
| 1220 | BSRL DI, CX // index of highest bit difference |
| 1221 | SHRL CX, SI // move a's bit to bottom |
| 1222 | ANDL $1, SI // mask bit |
| 1223 | LEAL -1(SI*2), AX // 1/0 => +1/-1 |
| 1224 | RET |
| 1225 | |
| 1226 | // all the bytes in common are the same, so we just need |
| 1227 | // to compare the lengths. |
| 1228 | cmp_allsame: |
| 1229 | XORL AX, AX |
| 1230 | XORL CX, CX |
| 1231 | CMPL BX, DX |
| 1232 | SETGT AX // 1 if alen > blen |
| 1233 | SETEQ CX // 1 if alen == blen |
| 1234 | LEAL -1(CX)(AX*2), AX // 1,0,-1 result |
| 1235 | RET |