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 | 9ddfb64 | 2013-07-16 16:24:09 -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 |
| 535 | CMPL CX, $0 |
| 536 | JNE 3(PC) |
| 537 | PUSHL $0 |
| 538 | JMP needm |
| 539 | #endif |
| 540 | MOVL m(CX), BP |
| 541 | PUSHL BP |
| 542 | CMPL BP, $0 |
| 543 | JNE havem |
| 544 | needm: |
| 545 | MOVL $runtime·needm(SB), AX |
| 546 | CALL AX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 547 | get_tls(CX) |
| 548 | MOVL m(CX), BP |
Russ Cox | 9b73238 | 2012-03-08 12:12:40 -0500 | [diff] [blame] | 549 | |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 550 | havem: |
| 551 | // Now there's a valid m, and we're running on its m->g0. |
| 552 | // Save current m->g0->sched.sp on stack and then set it to SP. |
| 553 | // Save current sp in m->g0->sched.sp in preparation for |
| 554 | // switch back to m->curg stack. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 555 | MOVL m_g0(BP), SI |
| 556 | PUSHL (g_sched+gobuf_sp)(SI) |
| 557 | MOVL SP, (g_sched+gobuf_sp)(SI) |
| 558 | |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 559 | // Switch to m->curg stack and call runtime.cgocallbackg |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 560 | // with the three arguments. Because we are taking over |
| 561 | // the execution of m->curg but *not* resuming what had |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 562 | // been running, we need to save that information (m->curg->sched) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 563 | // so that we can restore it when we're done. |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 564 | // We can restore m->curg->sched.sp easily, because calling |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 565 | // runtime.cgocallbackg leaves SP unchanged upon return. |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 566 | // To save m->curg->sched.pc, we push it onto the stack. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 567 | // This has the added benefit that it looks to the traceback |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 568 | // routine like cgocallbackg is going to return to that |
| 569 | // PC (because we defined cgocallbackg to have |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 570 | // a frame size of 12, the same amount that we use below), |
| 571 | // so that the traceback will seamlessly trace back into |
| 572 | // the earlier calls. |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 573 | MOVL fn+0(FP), AX |
| 574 | MOVL frame+4(FP), BX |
| 575 | MOVL framesize+8(FP), DX |
| 576 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 577 | MOVL m_curg(BP), SI |
| 578 | MOVL SI, g(CX) |
| 579 | MOVL (g_sched+gobuf_sp)(SI), DI // prepare stack as DI |
| 580 | |
| 581 | // Push gobuf.pc |
| 582 | MOVL (g_sched+gobuf_pc)(SI), BP |
| 583 | SUBL $4, DI |
| 584 | MOVL BP, 0(DI) |
| 585 | |
| 586 | // Push arguments to cgocallbackg. |
Russ Cox | 5d363c6 | 2013-07-16 09:41:38 -0400 | [diff] [blame] | 587 | // Frame size here must match the frame size above plus the pushes |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 588 | // to trick traceback routines into doing the right thing. |
Russ Cox | 5d363c6 | 2013-07-16 09:41:38 -0400 | [diff] [blame] | 589 | SUBL $20, DI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 590 | MOVL AX, 0(DI) |
| 591 | MOVL BX, 4(DI) |
| 592 | MOVL DX, 8(DI) |
| 593 | |
| 594 | // Switch stack and make the call. |
| 595 | MOVL DI, SP |
| 596 | CALL runtime·cgocallbackg(SB) |
| 597 | |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 598 | // Restore g->sched (== m->curg->sched) from saved values. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 599 | get_tls(CX) |
| 600 | MOVL g(CX), SI |
Russ Cox | 5d363c6 | 2013-07-16 09:41:38 -0400 | [diff] [blame] | 601 | MOVL 20(SP), BP |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 602 | MOVL BP, (g_sched+gobuf_pc)(SI) |
Russ Cox | 5d363c6 | 2013-07-16 09:41:38 -0400 | [diff] [blame] | 603 | LEAL (20+4)(SP), DI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 604 | MOVL DI, (g_sched+gobuf_sp)(SI) |
| 605 | |
| 606 | // Switch back to m->g0's stack and restore m->g0->sched.sp. |
| 607 | // (Unlike m->curg, the g0 goroutine never uses sched.pc, |
| 608 | // so we do not have to restore it.) |
| 609 | MOVL m(CX), BP |
| 610 | MOVL m_g0(BP), SI |
| 611 | MOVL SI, g(CX) |
| 612 | MOVL (g_sched+gobuf_sp)(SI), SP |
| 613 | POPL (g_sched+gobuf_sp)(SI) |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 614 | |
| 615 | // If the m on entry was nil, we called needm above to borrow an m |
| 616 | // for the duration of the call. Since the call is over, return it with dropm. |
| 617 | POPL BP |
| 618 | CMPL BP, $0 |
| 619 | JNE 3(PC) |
| 620 | MOVL $runtime·dropm(SB), AX |
| 621 | CALL AX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 622 | |
| 623 | // Done! |
| 624 | RET |
| 625 | |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 626 | // void setmg(M*, G*); set m and g. for use by needm. |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 627 | TEXT runtime·setmg(SB), 7, $0-8 |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 628 | #ifdef GOOS_windows |
| 629 | MOVL mm+0(FP), AX |
| 630 | CMPL AX, $0 |
| 631 | JNE settls |
| 632 | MOVL $0, 0x14(FS) |
| 633 | RET |
| 634 | settls: |
| 635 | LEAL m_tls(AX), AX |
| 636 | MOVL AX, 0x14(FS) |
| 637 | #endif |
| 638 | MOVL mm+0(FP), AX |
| 639 | get_tls(CX) |
| 640 | MOVL mm+0(FP), AX |
| 641 | MOVL AX, m(CX) |
| 642 | MOVL gg+4(FP), BX |
| 643 | MOVL BX, g(CX) |
| 644 | RET |
| 645 | |
Russ Cox | 6a70f9d | 2013-03-25 18:14:02 -0400 | [diff] [blame] | 646 | // 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] | 647 | TEXT setmg_gcc<>(SB), 7, $0 |
Russ Cox | 6a70f9d | 2013-03-25 18:14:02 -0400 | [diff] [blame] | 648 | get_tls(AX) |
| 649 | MOVL mm+0(FP), DX |
| 650 | MOVL DX, m(AX) |
| 651 | MOVL gg+4(FP), DX |
| 652 | MOVL DX,g (AX) |
| 653 | RET |
| 654 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 655 | // check that SP is in range [g->stackbase, g->stackguard) |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 656 | TEXT runtime·stackcheck(SB), 7, $0-0 |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 657 | get_tls(CX) |
| 658 | MOVL g(CX), AX |
| 659 | CMPL g_stackbase(AX), SP |
| 660 | JHI 2(PC) |
| 661 | INT $3 |
| 662 | CMPL SP, g_stackguard(AX) |
| 663 | JHI 2(PC) |
| 664 | INT $3 |
| 665 | RET |
| 666 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 667 | TEXT runtime·memclr(SB),7,$0-8 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 668 | MOVL 4(SP), DI // arg 1 addr |
| 669 | MOVL 8(SP), CX // arg 2 count |
Quan Yong Zhai | 47410a2 | 2011-07-23 15:46:58 -0400 | [diff] [blame] | 670 | MOVL CX, BX |
| 671 | ANDL $3, BX |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 672 | SHRL $2, CX |
| 673 | MOVL $0, AX |
| 674 | CLD |
| 675 | REP |
| 676 | STOSL |
Quan Yong Zhai | 47410a2 | 2011-07-23 15:46:58 -0400 | [diff] [blame] | 677 | MOVL BX, CX |
| 678 | REP |
| 679 | STOSB |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 680 | RET |
| 681 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 682 | TEXT runtime·getcallerpc(SB),7,$0-4 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 683 | MOVL x+0(FP),AX // addr of first arg |
| 684 | MOVL -4(AX),AX // get calling pc |
| 685 | RET |
| 686 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 687 | TEXT runtime·setcallerpc(SB),7,$0-8 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 688 | MOVL x+0(FP),AX // addr of first arg |
| 689 | MOVL x+4(FP), BX |
| 690 | MOVL BX, -4(AX) // set calling pc |
| 691 | RET |
| 692 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 693 | TEXT runtime·getcallersp(SB), 7, $0-4 |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 694 | MOVL sp+0(FP), AX |
| 695 | RET |
| 696 | |
Damian Gryski | 8e765da | 2012-02-02 14:09:27 -0500 | [diff] [blame] | 697 | // int64 runtime·cputicks(void), so really |
| 698 | // void runtime·cputicks(int64 *ticks) |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 699 | TEXT runtime·cputicks(SB),7,$0-4 |
Shenghou Ma | 6392b43 | 2012-02-06 12:49:28 -0500 | [diff] [blame] | 700 | RDTSC |
Damian Gryski | 8e765da | 2012-02-02 14:09:27 -0500 | [diff] [blame] | 701 | MOVL ret+0(FP), DI |
| 702 | MOVL AX, 0(DI) |
| 703 | MOVL DX, 4(DI) |
| 704 | RET |
| 705 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 706 | TEXT runtime·ldt0setup(SB),7,$16-0 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 707 | // set up ldt 7 to point at tls0 |
| 708 | // 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] | 709 | // 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] | 710 | MOVL $7, 0(SP) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 711 | LEAL runtime·tls0(SB), AX |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 712 | MOVL AX, 4(SP) |
| 713 | MOVL $32, 8(SP) // sizeof(tls array) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 714 | CALL runtime·setldt(SB) |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 715 | RET |
| 716 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 717 | TEXT runtime·emptyfunc(SB),0,$0-0 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 718 | RET |
| 719 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 720 | TEXT runtime·abort(SB),7,$0-0 |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 721 | INT $0x3 |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 722 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 723 | TEXT runtime·stackguard(SB),7,$0-8 |
Russ Cox | 9e5db8c | 2012-03-15 15:22:30 -0400 | [diff] [blame] | 724 | MOVL SP, DX |
| 725 | MOVL DX, sp+0(FP) |
| 726 | get_tls(CX) |
| 727 | MOVL g(CX), BX |
| 728 | MOVL g_stackguard(BX), DX |
Russ Cox | 07720b6 | 2013-03-22 12:57:55 -0400 | [diff] [blame] | 729 | MOVL DX, limit+4(FP) |
Russ Cox | 9e5db8c | 2012-03-15 15:22:30 -0400 | [diff] [blame] | 730 | RET |
| 731 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 732 | GLOBL runtime·tls0(SB), $32 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 733 | |
| 734 | // hash function using AES hardware instructions |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 735 | TEXT runtime·aeshash(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 736 | MOVL 4(SP), DX // ptr to hash value |
| 737 | MOVL 8(SP), CX // size |
| 738 | MOVL 12(SP), AX // ptr to data |
| 739 | JMP runtime·aeshashbody(SB) |
| 740 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 741 | TEXT runtime·aeshashstr(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 742 | MOVL 4(SP), DX // ptr to hash value |
| 743 | MOVL 12(SP), AX // ptr to string struct |
| 744 | MOVL 4(AX), CX // length of string |
| 745 | MOVL (AX), AX // string data |
| 746 | JMP runtime·aeshashbody(SB) |
| 747 | |
| 748 | // AX: data |
| 749 | // CX: length |
| 750 | // DX: ptr to seed input / hash output |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 751 | TEXT runtime·aeshashbody(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 752 | MOVL (DX), X0 // seed to low 32 bits of xmm0 |
| 753 | PINSRD $1, CX, X0 // size to next 32 bits of xmm0 |
Keith Randall | db53d97 | 2013-03-20 14:34:26 -0700 | [diff] [blame] | 754 | MOVO runtime·aeskeysched+0(SB), X2 |
| 755 | MOVO runtime·aeskeysched+16(SB), X3 |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 756 | CMPL CX, $16 |
| 757 | JB aessmall |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 758 | aesloop: |
| 759 | CMPL CX, $16 |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 760 | JBE aesloopend |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 761 | MOVOU (AX), X1 |
| 762 | AESENC X2, X0 |
| 763 | AESENC X1, X0 |
| 764 | SUBL $16, CX |
| 765 | ADDL $16, AX |
| 766 | JMP aesloop |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 767 | // 1-16 bytes remaining |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 768 | aesloopend: |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 769 | // This load may overlap with the previous load above. |
| 770 | // We'll hash some bytes twice, but that's ok. |
| 771 | MOVOU -16(AX)(CX*1), X1 |
| 772 | JMP partial |
| 773 | // 0-15 bytes |
| 774 | aessmall: |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 775 | TESTL CX, CX |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 776 | JE finalize // 0 bytes |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 777 | |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 778 | CMPB AX, $0xf0 |
| 779 | JA highpartial |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 780 | |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 781 | // 16 bytes loaded at this address won't cross |
| 782 | // a page boundary, so we can load it directly. |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 783 | MOVOU (AX), X1 |
| 784 | ADDL CX, CX |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 785 | PAND masks<>(SB)(CX*8), X1 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 786 | JMP partial |
| 787 | highpartial: |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 788 | // address ends in 1111xxxx. Might be up against |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 789 | // a page boundary, so load ending at last byte. |
| 790 | // Then shift bytes down using pshufb. |
| 791 | MOVOU -16(AX)(CX*1), X1 |
| 792 | ADDL CX, CX |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 793 | PSHUFB shifts<>(SB)(CX*8), X1 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 794 | partial: |
| 795 | // incorporate partial block into hash |
| 796 | AESENC X3, X0 |
| 797 | AESENC X1, X0 |
| 798 | finalize: |
| 799 | // finalize hash |
| 800 | AESENC X2, X0 |
| 801 | AESENC X3, X0 |
| 802 | AESENC X2, X0 |
| 803 | MOVL X0, (DX) |
| 804 | RET |
| 805 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 806 | TEXT runtime·aeshash32(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 807 | MOVL 4(SP), DX // ptr to hash value |
| 808 | MOVL 12(SP), AX // ptr to data |
| 809 | MOVL (DX), X0 // seed |
| 810 | PINSRD $1, (AX), X0 // data |
Keith Randall | db53d97 | 2013-03-20 14:34:26 -0700 | [diff] [blame] | 811 | AESENC runtime·aeskeysched+0(SB), X0 |
| 812 | AESENC runtime·aeskeysched+16(SB), X0 |
| 813 | AESENC runtime·aeskeysched+0(SB), X0 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 814 | MOVL X0, (DX) |
| 815 | RET |
| 816 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 817 | TEXT runtime·aeshash64(SB),7,$0-12 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 818 | MOVL 4(SP), DX // ptr to hash value |
| 819 | MOVL 12(SP), AX // ptr to data |
| 820 | MOVQ (AX), X0 // data |
| 821 | PINSRD $2, (DX), X0 // seed |
Keith Randall | db53d97 | 2013-03-20 14:34:26 -0700 | [diff] [blame] | 822 | AESENC runtime·aeskeysched+0(SB), X0 |
| 823 | AESENC runtime·aeskeysched+16(SB), X0 |
| 824 | AESENC runtime·aeskeysched+0(SB), X0 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 825 | MOVL X0, (DX) |
| 826 | RET |
| 827 | |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 828 | // 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] | 829 | DATA masks<>+0x00(SB)/4, $0x00000000 |
| 830 | DATA masks<>+0x04(SB)/4, $0x00000000 |
| 831 | DATA masks<>+0x08(SB)/4, $0x00000000 |
| 832 | DATA masks<>+0x0c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 833 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 834 | DATA masks<>+0x10(SB)/4, $0x000000ff |
| 835 | DATA masks<>+0x14(SB)/4, $0x00000000 |
| 836 | DATA masks<>+0x18(SB)/4, $0x00000000 |
| 837 | DATA masks<>+0x1c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 838 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 839 | DATA masks<>+0x20(SB)/4, $0x0000ffff |
| 840 | DATA masks<>+0x24(SB)/4, $0x00000000 |
| 841 | DATA masks<>+0x28(SB)/4, $0x00000000 |
| 842 | DATA masks<>+0x2c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 843 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 844 | DATA masks<>+0x30(SB)/4, $0x00ffffff |
| 845 | DATA masks<>+0x34(SB)/4, $0x00000000 |
| 846 | DATA masks<>+0x38(SB)/4, $0x00000000 |
| 847 | DATA masks<>+0x3c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 848 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 849 | DATA masks<>+0x40(SB)/4, $0xffffffff |
| 850 | DATA masks<>+0x44(SB)/4, $0x00000000 |
| 851 | DATA masks<>+0x48(SB)/4, $0x00000000 |
| 852 | DATA masks<>+0x4c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 853 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 854 | DATA masks<>+0x50(SB)/4, $0xffffffff |
| 855 | DATA masks<>+0x54(SB)/4, $0x000000ff |
| 856 | DATA masks<>+0x58(SB)/4, $0x00000000 |
| 857 | DATA masks<>+0x5c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 858 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 859 | DATA masks<>+0x60(SB)/4, $0xffffffff |
| 860 | DATA masks<>+0x64(SB)/4, $0x0000ffff |
| 861 | DATA masks<>+0x68(SB)/4, $0x00000000 |
| 862 | DATA masks<>+0x6c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 863 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 864 | DATA masks<>+0x70(SB)/4, $0xffffffff |
| 865 | DATA masks<>+0x74(SB)/4, $0x00ffffff |
| 866 | DATA masks<>+0x78(SB)/4, $0x00000000 |
| 867 | DATA masks<>+0x7c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 868 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 869 | DATA masks<>+0x80(SB)/4, $0xffffffff |
| 870 | DATA masks<>+0x84(SB)/4, $0xffffffff |
| 871 | DATA masks<>+0x88(SB)/4, $0x00000000 |
| 872 | DATA masks<>+0x8c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 873 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 874 | DATA masks<>+0x90(SB)/4, $0xffffffff |
| 875 | DATA masks<>+0x94(SB)/4, $0xffffffff |
| 876 | DATA masks<>+0x98(SB)/4, $0x000000ff |
| 877 | DATA masks<>+0x9c(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 878 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 879 | DATA masks<>+0xa0(SB)/4, $0xffffffff |
| 880 | DATA masks<>+0xa4(SB)/4, $0xffffffff |
| 881 | DATA masks<>+0xa8(SB)/4, $0x0000ffff |
| 882 | DATA masks<>+0xac(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 883 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 884 | DATA masks<>+0xb0(SB)/4, $0xffffffff |
| 885 | DATA masks<>+0xb4(SB)/4, $0xffffffff |
| 886 | DATA masks<>+0xb8(SB)/4, $0x00ffffff |
| 887 | DATA masks<>+0xbc(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 888 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 889 | DATA masks<>+0xc0(SB)/4, $0xffffffff |
| 890 | DATA masks<>+0xc4(SB)/4, $0xffffffff |
| 891 | DATA masks<>+0xc8(SB)/4, $0xffffffff |
| 892 | DATA masks<>+0xcc(SB)/4, $0x00000000 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 893 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 894 | DATA masks<>+0xd0(SB)/4, $0xffffffff |
| 895 | DATA masks<>+0xd4(SB)/4, $0xffffffff |
| 896 | DATA masks<>+0xd8(SB)/4, $0xffffffff |
| 897 | DATA masks<>+0xdc(SB)/4, $0x000000ff |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 898 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 899 | DATA masks<>+0xe0(SB)/4, $0xffffffff |
| 900 | DATA masks<>+0xe4(SB)/4, $0xffffffff |
| 901 | DATA masks<>+0xe8(SB)/4, $0xffffffff |
| 902 | DATA masks<>+0xec(SB)/4, $0x0000ffff |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 903 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 904 | DATA masks<>+0xf0(SB)/4, $0xffffffff |
| 905 | DATA masks<>+0xf4(SB)/4, $0xffffffff |
| 906 | DATA masks<>+0xf8(SB)/4, $0xffffffff |
| 907 | DATA masks<>+0xfc(SB)/4, $0x00ffffff |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 908 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 909 | GLOBL masks<>(SB),8,$256 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 910 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 911 | // these are arguments to pshufb. They move data down from |
| 912 | // the high bytes of the register to the low bytes of the register. |
| 913 | // index is how many bytes to move. |
| 914 | DATA shifts<>+0x00(SB)/4, $0x00000000 |
| 915 | DATA shifts<>+0x04(SB)/4, $0x00000000 |
| 916 | DATA shifts<>+0x08(SB)/4, $0x00000000 |
| 917 | DATA shifts<>+0x0c(SB)/4, $0x00000000 |
| 918 | |
| 919 | DATA shifts<>+0x10(SB)/4, $0xffffff0f |
| 920 | DATA shifts<>+0x14(SB)/4, $0xffffffff |
| 921 | DATA shifts<>+0x18(SB)/4, $0xffffffff |
| 922 | DATA shifts<>+0x1c(SB)/4, $0xffffffff |
| 923 | |
| 924 | DATA shifts<>+0x20(SB)/4, $0xffff0f0e |
| 925 | DATA shifts<>+0x24(SB)/4, $0xffffffff |
| 926 | DATA shifts<>+0x28(SB)/4, $0xffffffff |
| 927 | DATA shifts<>+0x2c(SB)/4, $0xffffffff |
| 928 | |
| 929 | DATA shifts<>+0x30(SB)/4, $0xff0f0e0d |
| 930 | DATA shifts<>+0x34(SB)/4, $0xffffffff |
| 931 | DATA shifts<>+0x38(SB)/4, $0xffffffff |
| 932 | DATA shifts<>+0x3c(SB)/4, $0xffffffff |
| 933 | |
| 934 | DATA shifts<>+0x40(SB)/4, $0x0f0e0d0c |
| 935 | DATA shifts<>+0x44(SB)/4, $0xffffffff |
| 936 | DATA shifts<>+0x48(SB)/4, $0xffffffff |
| 937 | DATA shifts<>+0x4c(SB)/4, $0xffffffff |
| 938 | |
| 939 | DATA shifts<>+0x50(SB)/4, $0x0e0d0c0b |
| 940 | DATA shifts<>+0x54(SB)/4, $0xffffff0f |
| 941 | DATA shifts<>+0x58(SB)/4, $0xffffffff |
| 942 | DATA shifts<>+0x5c(SB)/4, $0xffffffff |
| 943 | |
| 944 | DATA shifts<>+0x60(SB)/4, $0x0d0c0b0a |
| 945 | DATA shifts<>+0x64(SB)/4, $0xffff0f0e |
| 946 | DATA shifts<>+0x68(SB)/4, $0xffffffff |
| 947 | DATA shifts<>+0x6c(SB)/4, $0xffffffff |
| 948 | |
| 949 | DATA shifts<>+0x70(SB)/4, $0x0c0b0a09 |
| 950 | DATA shifts<>+0x74(SB)/4, $0xff0f0e0d |
| 951 | DATA shifts<>+0x78(SB)/4, $0xffffffff |
| 952 | DATA shifts<>+0x7c(SB)/4, $0xffffffff |
| 953 | |
| 954 | DATA shifts<>+0x80(SB)/4, $0x0b0a0908 |
| 955 | DATA shifts<>+0x84(SB)/4, $0x0f0e0d0c |
| 956 | DATA shifts<>+0x88(SB)/4, $0xffffffff |
| 957 | DATA shifts<>+0x8c(SB)/4, $0xffffffff |
| 958 | |
| 959 | DATA shifts<>+0x90(SB)/4, $0x0a090807 |
| 960 | DATA shifts<>+0x94(SB)/4, $0x0e0d0c0b |
| 961 | DATA shifts<>+0x98(SB)/4, $0xffffff0f |
| 962 | DATA shifts<>+0x9c(SB)/4, $0xffffffff |
| 963 | |
| 964 | DATA shifts<>+0xa0(SB)/4, $0x09080706 |
| 965 | DATA shifts<>+0xa4(SB)/4, $0x0d0c0b0a |
| 966 | DATA shifts<>+0xa8(SB)/4, $0xffff0f0e |
| 967 | DATA shifts<>+0xac(SB)/4, $0xffffffff |
| 968 | |
| 969 | DATA shifts<>+0xb0(SB)/4, $0x08070605 |
| 970 | DATA shifts<>+0xb4(SB)/4, $0x0c0b0a09 |
| 971 | DATA shifts<>+0xb8(SB)/4, $0xff0f0e0d |
| 972 | DATA shifts<>+0xbc(SB)/4, $0xffffffff |
| 973 | |
| 974 | DATA shifts<>+0xc0(SB)/4, $0x07060504 |
| 975 | DATA shifts<>+0xc4(SB)/4, $0x0b0a0908 |
| 976 | DATA shifts<>+0xc8(SB)/4, $0x0f0e0d0c |
| 977 | DATA shifts<>+0xcc(SB)/4, $0xffffffff |
| 978 | |
| 979 | DATA shifts<>+0xd0(SB)/4, $0x06050403 |
| 980 | DATA shifts<>+0xd4(SB)/4, $0x0a090807 |
| 981 | DATA shifts<>+0xd8(SB)/4, $0x0e0d0c0b |
| 982 | DATA shifts<>+0xdc(SB)/4, $0xffffff0f |
| 983 | |
| 984 | DATA shifts<>+0xe0(SB)/4, $0x05040302 |
| 985 | DATA shifts<>+0xe4(SB)/4, $0x09080706 |
| 986 | DATA shifts<>+0xe8(SB)/4, $0x0d0c0b0a |
| 987 | DATA shifts<>+0xec(SB)/4, $0xffff0f0e |
| 988 | |
| 989 | DATA shifts<>+0xf0(SB)/4, $0x04030201 |
| 990 | DATA shifts<>+0xf4(SB)/4, $0x08070605 |
| 991 | DATA shifts<>+0xf8(SB)/4, $0x0c0b0a09 |
| 992 | DATA shifts<>+0xfc(SB)/4, $0xff0f0e0d |
| 993 | |
| 994 | GLOBL shifts<>(SB),8,$256 |
| 995 | |
| 996 | TEXT runtime·memeq(SB),7,$0-12 |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 997 | MOVL a+0(FP), SI |
| 998 | MOVL b+4(FP), DI |
| 999 | MOVL count+8(FP), BX |
| 1000 | JMP runtime·memeqbody(SB) |
| 1001 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1002 | TEXT bytes·Equal(SB),7,$0-25 |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1003 | MOVL a_len+4(FP), BX |
| 1004 | MOVL b_len+16(FP), CX |
| 1005 | XORL AX, AX |
| 1006 | CMPL BX, CX |
| 1007 | JNE eqret |
| 1008 | MOVL a+0(FP), SI |
| 1009 | MOVL b+12(FP), DI |
| 1010 | CALL runtime·memeqbody(SB) |
| 1011 | eqret: |
| 1012 | MOVB AX, ret+24(FP) |
| 1013 | RET |
| 1014 | |
| 1015 | // a in SI |
| 1016 | // b in DI |
| 1017 | // count in BX |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1018 | TEXT runtime·memeqbody(SB),7,$0-0 |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1019 | XORL AX, AX |
| 1020 | |
| 1021 | CMPL BX, $4 |
| 1022 | JB small |
| 1023 | |
| 1024 | // 64 bytes at a time using xmm registers |
| 1025 | hugeloop: |
| 1026 | CMPL BX, $64 |
| 1027 | JB bigloop |
| 1028 | TESTL $0x4000000, runtime·cpuid_edx(SB) // check for sse2 |
| 1029 | JE bigloop |
| 1030 | MOVOU (SI), X0 |
| 1031 | MOVOU (DI), X1 |
| 1032 | MOVOU 16(SI), X2 |
| 1033 | MOVOU 16(DI), X3 |
| 1034 | MOVOU 32(SI), X4 |
| 1035 | MOVOU 32(DI), X5 |
| 1036 | MOVOU 48(SI), X6 |
| 1037 | MOVOU 48(DI), X7 |
| 1038 | PCMPEQB X1, X0 |
| 1039 | PCMPEQB X3, X2 |
| 1040 | PCMPEQB X5, X4 |
| 1041 | PCMPEQB X7, X6 |
| 1042 | PAND X2, X0 |
| 1043 | PAND X6, X4 |
| 1044 | PAND X4, X0 |
| 1045 | PMOVMSKB X0, DX |
| 1046 | ADDL $64, SI |
| 1047 | ADDL $64, DI |
| 1048 | SUBL $64, BX |
| 1049 | CMPL DX, $0xffff |
| 1050 | JEQ hugeloop |
| 1051 | RET |
| 1052 | |
| 1053 | // 4 bytes at a time using 32-bit register |
| 1054 | bigloop: |
| 1055 | CMPL BX, $4 |
| 1056 | JBE leftover |
| 1057 | MOVL (SI), CX |
| 1058 | MOVL (DI), DX |
| 1059 | ADDL $4, SI |
| 1060 | ADDL $4, DI |
| 1061 | SUBL $4, BX |
| 1062 | CMPL CX, DX |
| 1063 | JEQ bigloop |
| 1064 | RET |
| 1065 | |
| 1066 | // remaining 0-4 bytes |
| 1067 | leftover: |
| 1068 | MOVL -4(SI)(BX*1), CX |
| 1069 | MOVL -4(DI)(BX*1), DX |
| 1070 | CMPL CX, DX |
| 1071 | SETEQ AX |
| 1072 | RET |
| 1073 | |
| 1074 | small: |
| 1075 | CMPL BX, $0 |
| 1076 | JEQ equal |
| 1077 | |
| 1078 | LEAL 0(BX*8), CX |
| 1079 | NEGL CX |
| 1080 | |
| 1081 | MOVL SI, DX |
| 1082 | CMPB DX, $0xfc |
| 1083 | JA si_high |
| 1084 | |
| 1085 | // load at SI won't cross a page boundary. |
| 1086 | MOVL (SI), SI |
| 1087 | JMP si_finish |
| 1088 | si_high: |
| 1089 | // address ends in 111111xx. Load up to bytes we want, move to correct position. |
| 1090 | MOVL -4(SI)(BX*1), SI |
| 1091 | SHRL CX, SI |
| 1092 | si_finish: |
| 1093 | |
| 1094 | // same for DI. |
| 1095 | MOVL DI, DX |
| 1096 | CMPB DX, $0xfc |
| 1097 | JA di_high |
| 1098 | MOVL (DI), DI |
| 1099 | JMP di_finish |
| 1100 | di_high: |
| 1101 | MOVL -4(DI)(BX*1), DI |
| 1102 | SHRL CX, DI |
| 1103 | di_finish: |
| 1104 | |
| 1105 | SUBL SI, DI |
| 1106 | SHLL CX, DI |
| 1107 | equal: |
| 1108 | SETEQ AX |
| 1109 | RET |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1110 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1111 | TEXT runtime·cmpstring(SB),7,$0-20 |
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+8(FP), DI |
| 1115 | MOVL s2+12(FP), DX |
| 1116 | CALL runtime·cmpbody(SB) |
| 1117 | MOVL AX, res+16(FP) |
| 1118 | RET |
| 1119 | |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1120 | TEXT bytes·Compare(SB),7,$0-28 |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1121 | MOVL s1+0(FP), SI |
| 1122 | MOVL s1+4(FP), BX |
| 1123 | MOVL s2+12(FP), DI |
| 1124 | MOVL s2+16(FP), DX |
| 1125 | CALL runtime·cmpbody(SB) |
| 1126 | MOVL AX, res+24(FP) |
| 1127 | RET |
| 1128 | |
| 1129 | // input: |
| 1130 | // SI = a |
| 1131 | // DI = b |
| 1132 | // BX = alen |
| 1133 | // DX = blen |
| 1134 | // output: |
| 1135 | // AX = 1/0/-1 |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1136 | TEXT runtime·cmpbody(SB),7,$0-0 |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1137 | CMPL SI, DI |
| 1138 | JEQ cmp_allsame |
| 1139 | CMPL BX, DX |
| 1140 | MOVL DX, BP |
| 1141 | CMOVLLT BX, BP // BP = min(alen, blen) |
| 1142 | CMPL BP, $4 |
| 1143 | JB cmp_small |
| 1144 | TESTL $0x4000000, runtime·cpuid_edx(SB) // check for sse2 |
| 1145 | JE cmp_mediumloop |
| 1146 | cmp_largeloop: |
| 1147 | CMPL BP, $16 |
| 1148 | JB cmp_mediumloop |
| 1149 | MOVOU (SI), X0 |
| 1150 | MOVOU (DI), X1 |
| 1151 | PCMPEQB X0, X1 |
| 1152 | PMOVMSKB X1, AX |
| 1153 | XORL $0xffff, AX // convert EQ to NE |
| 1154 | JNE cmp_diff16 // branch if at least one byte is not equal |
| 1155 | ADDL $16, SI |
| 1156 | ADDL $16, DI |
| 1157 | SUBL $16, BP |
| 1158 | JMP cmp_largeloop |
| 1159 | |
| 1160 | cmp_diff16: |
| 1161 | BSFL AX, BX // index of first byte that differs |
| 1162 | XORL AX, AX |
| 1163 | MOVB (SI)(BX*1), CX |
| 1164 | CMPB CX, (DI)(BX*1) |
| 1165 | SETHI AX |
| 1166 | LEAL -1(AX*2), AX // convert 1/0 to +1/-1 |
| 1167 | RET |
| 1168 | |
| 1169 | cmp_mediumloop: |
| 1170 | CMPL BP, $4 |
| 1171 | JBE cmp_0through4 |
| 1172 | MOVL (SI), AX |
| 1173 | MOVL (DI), CX |
| 1174 | CMPL AX, CX |
| 1175 | JNE cmp_diff4 |
| 1176 | ADDL $4, SI |
| 1177 | ADDL $4, DI |
| 1178 | SUBL $4, BP |
| 1179 | JMP cmp_mediumloop |
| 1180 | |
| 1181 | cmp_0through4: |
| 1182 | MOVL -4(SI)(BP*1), AX |
| 1183 | MOVL -4(DI)(BP*1), CX |
| 1184 | CMPL AX, CX |
| 1185 | JEQ cmp_allsame |
| 1186 | |
| 1187 | cmp_diff4: |
| 1188 | BSWAPL AX // reverse order of bytes |
| 1189 | BSWAPL CX |
| 1190 | XORL AX, CX // find bit differences |
| 1191 | BSRL CX, CX // index of highest bit difference |
| 1192 | SHRL CX, AX // move a's bit to bottom |
| 1193 | ANDL $1, AX // mask bit |
| 1194 | LEAL -1(AX*2), AX // 1/0 => +1/-1 |
| 1195 | RET |
| 1196 | |
| 1197 | // 0-3 bytes in common |
| 1198 | cmp_small: |
| 1199 | LEAL (BP*8), CX |
| 1200 | NEGL CX |
| 1201 | JEQ cmp_allsame |
| 1202 | |
| 1203 | // load si |
| 1204 | CMPB SI, $0xfc |
| 1205 | JA cmp_si_high |
| 1206 | MOVL (SI), SI |
| 1207 | JMP cmp_si_finish |
| 1208 | cmp_si_high: |
| 1209 | MOVL -4(SI)(BP*1), SI |
| 1210 | SHRL CX, SI |
| 1211 | cmp_si_finish: |
| 1212 | SHLL CX, SI |
| 1213 | |
| 1214 | // same for di |
| 1215 | CMPB DI, $0xfc |
| 1216 | JA cmp_di_high |
| 1217 | MOVL (DI), DI |
| 1218 | JMP cmp_di_finish |
| 1219 | cmp_di_high: |
| 1220 | MOVL -4(DI)(BP*1), DI |
| 1221 | SHRL CX, DI |
| 1222 | cmp_di_finish: |
| 1223 | SHLL CX, DI |
| 1224 | |
| 1225 | BSWAPL SI // reverse order of bytes |
| 1226 | BSWAPL DI |
| 1227 | XORL SI, DI // find bit differences |
| 1228 | JEQ cmp_allsame |
| 1229 | BSRL DI, CX // index of highest bit difference |
| 1230 | SHRL CX, SI // move a's bit to bottom |
| 1231 | ANDL $1, SI // mask bit |
| 1232 | LEAL -1(SI*2), AX // 1/0 => +1/-1 |
| 1233 | RET |
| 1234 | |
| 1235 | // all the bytes in common are the same, so we just need |
| 1236 | // to compare the lengths. |
| 1237 | cmp_allsame: |
| 1238 | XORL AX, AX |
| 1239 | XORL CX, CX |
| 1240 | CMPL BX, DX |
| 1241 | SETGT AX // 1 if alen > blen |
| 1242 | SETEQ CX // 1 if alen == blen |
| 1243 | LEAL -1(CX)(AX*2), AX // 1,0,-1 result |
| 1244 | RET |