Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Russ Cox | 15ced2d | 2014-11-11 17:06:22 -0500 | [diff] [blame] | 5 | #include "go_asm.h" |
| 6 | #include "go_tls.h" |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 7 | #include "funcdata.h" |
Russ Cox | cb040d5 | 2014-09-04 23:05:18 -0400 | [diff] [blame] | 8 | #include "textflag.h" |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 9 | |
Ian Lance Taylor | cf3f771 | 2017-10-09 11:31:20 -0700 | [diff] [blame^] | 10 | // _rt0_amd64 is common startup code for most amd64 systems when using |
| 11 | // internal linking. This is the entry point for the program from the |
| 12 | // kernel for an ordinary -buildmode=exe program. The stack holds the |
| 13 | // number of arguments and the C-style argv. |
| 14 | TEXT _rt0_amd64(SB),NOSPLIT,$-8 |
| 15 | MOVQ 0(SP), DI // argc |
| 16 | LEAQ 8(SP), SI // argv |
| 17 | JMP runtime·rt0_go(SB) |
| 18 | |
| 19 | // main is common startup code for most amd64 systems when using |
| 20 | // external linking. The C startup code will call the symbol "main" |
| 21 | // passing argc and argv in the usual C ABI registers DI and SI. |
| 22 | TEXT main(SB),NOSPLIT,$-8 |
| 23 | JMP runtime·rt0_go(SB) |
| 24 | |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 25 | TEXT runtime·rt0_go(SB),NOSPLIT,$0 |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 26 | // copy arguments forward on an even stack |
Russ Cox | 36b414f | 2013-03-06 15:03:04 -0500 | [diff] [blame] | 27 | MOVQ DI, AX // argc |
| 28 | MOVQ SI, BX // argv |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 29 | SUBQ $(4*8+7), SP // 2args 2auto |
Ian Lance Taylor | a4f8d36 | 2010-04-09 14:15:15 -0700 | [diff] [blame] | 30 | ANDQ $~15, SP |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 31 | MOVQ AX, 16(SP) |
| 32 | MOVQ BX, 24(SP) |
Dmitriy Vyukov | 428062d | 2011-12-07 16:53:17 +0300 | [diff] [blame] | 33 | |
| 34 | // create istack out of the given (operating system) stack. |
Russ Cox | f8d49b5 | 2013-02-28 16:24:38 -0500 | [diff] [blame] | 35 | // _cgo_init may update stackguard. |
Dmitriy Vyukov | 428062d | 2011-12-07 16:53:17 +0300 | [diff] [blame] | 36 | MOVQ $runtime·g0(SB), DI |
Alex Brainman | 8d6958f | 2012-01-20 12:59:44 +1100 | [diff] [blame] | 37 | LEAQ (-64*1024+104)(SP), BX |
Russ Cox | e6d3511 | 2015-01-05 16:29:21 +0000 | [diff] [blame] | 38 | MOVQ BX, g_stackguard0(DI) |
| 39 | MOVQ BX, g_stackguard1(DI) |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 40 | MOVQ BX, (g_stack+stack_lo)(DI) |
| 41 | MOVQ SP, (g_stack+stack_hi)(DI) |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 42 | |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 43 | // find out information about the processor we're on |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 44 | MOVL $0, AX |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 45 | CPUID |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 46 | MOVL AX, SI |
| 47 | CMPL AX, $0 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 48 | JE nocpuinfo |
Dmitry Vyukov | 6e70fdd | 2015-02-17 14:25:49 +0300 | [diff] [blame] | 49 | |
| 50 | // Figure out how to serialize RDTSC. |
| 51 | // On Intel processors LFENCE is enough. AMD requires MFENCE. |
| 52 | // Don't know about the rest, so let's do MFENCE. |
| 53 | CMPL BX, $0x756E6547 // "Genu" |
| 54 | JNE notintel |
| 55 | CMPL DX, $0x49656E69 // "ineI" |
| 56 | JNE notintel |
| 57 | CMPL CX, $0x6C65746E // "ntel" |
| 58 | JNE notintel |
Martin Möhrmann | b64e817 | 2017-04-24 16:59:33 +0200 | [diff] [blame] | 59 | MOVB $1, runtime·isIntel(SB) |
Dmitry Vyukov | 6e70fdd | 2015-02-17 14:25:49 +0300 | [diff] [blame] | 60 | MOVB $1, runtime·lfenceBeforeRdtsc(SB) |
| 61 | notintel: |
| 62 | |
Keith Randall | 4b209db | 2016-03-29 21:25:33 -0700 | [diff] [blame] | 63 | // Load EAX=1 cpuid flags |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 64 | MOVL $1, AX |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 65 | CPUID |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 66 | MOVL AX, runtime·processorVersionInfo(SB) |
Keith Randall | 4b209db | 2016-03-29 21:25:33 -0700 | [diff] [blame] | 67 | |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 68 | TESTL $(1<<26), DX // SSE2 |
| 69 | SETNE runtime·support_sse2(SB) |
| 70 | |
| 71 | TESTL $(1<<9), CX // SSSE3 |
| 72 | SETNE runtime·support_ssse3(SB) |
| 73 | |
| 74 | TESTL $(1<<19), CX // SSE4.1 |
| 75 | SETNE runtime·support_sse41(SB) |
| 76 | |
| 77 | TESTL $(1<<20), CX // SSE4.2 |
| 78 | SETNE runtime·support_sse42(SB) |
| 79 | |
| 80 | TESTL $(1<<23), CX // POPCNT |
| 81 | SETNE runtime·support_popcnt(SB) |
| 82 | |
| 83 | TESTL $(1<<25), CX // AES |
| 84 | SETNE runtime·support_aes(SB) |
| 85 | |
| 86 | TESTL $(1<<27), CX // OSXSAVE |
| 87 | SETNE runtime·support_osxsave(SB) |
| 88 | |
| 89 | // If OS support for XMM and YMM is not present |
| 90 | // support_avx will be set back to false later. |
| 91 | TESTL $(1<<28), CX // AVX |
| 92 | SETNE runtime·support_avx(SB) |
| 93 | |
| 94 | eax7: |
Keith Randall | 4b209db | 2016-03-29 21:25:33 -0700 | [diff] [blame] | 95 | // Load EAX=7/ECX=0 cpuid flags |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 96 | CMPL SI, $7 |
| 97 | JLT osavx |
Keith Randall | 4b209db | 2016-03-29 21:25:33 -0700 | [diff] [blame] | 98 | MOVL $7, AX |
| 99 | MOVL $0, CX |
| 100 | CPUID |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 101 | |
| 102 | TESTL $(1<<3), BX // BMI1 |
| 103 | SETNE runtime·support_bmi1(SB) |
| 104 | |
| 105 | // If OS support for XMM and YMM is not present |
| 106 | // support_avx2 will be set back to false later. |
| 107 | TESTL $(1<<5), BX |
| 108 | SETNE runtime·support_avx2(SB) |
| 109 | |
| 110 | TESTL $(1<<8), BX // BMI2 |
| 111 | SETNE runtime·support_bmi2(SB) |
| 112 | |
| 113 | TESTL $(1<<9), BX // ERMS |
| 114 | SETNE runtime·support_erms(SB) |
| 115 | |
| 116 | osavx: |
| 117 | CMPB runtime·support_osxsave(SB), $1 |
| 118 | JNE noavx |
| 119 | MOVL $0, CX |
Ilya Tocar | 0e23ca4 | 2015-10-28 23:20:26 +0300 | [diff] [blame] | 120 | // For XGETBV, OSXSAVE bit is required and sufficient |
Ilya Tocar | 1d1f2fb | 2016-01-13 16:43:22 +0300 | [diff] [blame] | 121 | XGETBV |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 122 | ANDL $6, AX |
| 123 | CMPL AX, $6 // Check for OS support of XMM and YMM registers. |
| 124 | JE nocpuinfo |
Ilya Tocar | 0e23ca4 | 2015-10-28 23:20:26 +0300 | [diff] [blame] | 125 | noavx: |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 126 | MOVB $0, runtime·support_avx(SB) |
| 127 | MOVB $0, runtime·support_avx2(SB) |
| 128 | |
| 129 | nocpuinfo: |
Russ Cox | f8d49b5 | 2013-02-28 16:24:38 -0500 | [diff] [blame] | 130 | // if there is an _cgo_init, call it. |
| 131 | MOVQ _cgo_init(SB), AX |
Ian Lance Taylor | a4f8d36 | 2010-04-09 14:15:15 -0700 | [diff] [blame] | 132 | TESTQ AX, AX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 133 | JZ needtls |
Alex Brainman | 8d6958f | 2012-01-20 12:59:44 +1100 | [diff] [blame] | 134 | // g0 already in DI |
| 135 | MOVQ DI, CX // Win64 uses CX for first parameter |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 136 | MOVQ $setg_gcc<>(SB), SI |
Alex Brainman | 8d6958f | 2012-01-20 12:59:44 +1100 | [diff] [blame] | 137 | CALL AX |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 138 | |
Dmitriy Vyukov | f5becf4 | 2013-06-03 12:28:24 +0400 | [diff] [blame] | 139 | // update stackguard after _cgo_init |
| 140 | MOVQ $runtime·g0(SB), CX |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 141 | MOVQ (g_stack+stack_lo)(CX), AX |
Russ Cox | 15ced2d | 2014-11-11 17:06:22 -0500 | [diff] [blame] | 142 | ADDQ $const__StackGuard, AX |
Russ Cox | e6d3511 | 2015-01-05 16:29:21 +0000 | [diff] [blame] | 143 | MOVQ AX, g_stackguard0(CX) |
| 144 | MOVQ AX, g_stackguard1(CX) |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 145 | |
Matthew Dempsky | 8ee0fd8 | 2015-06-09 15:24:38 -0700 | [diff] [blame] | 146 | #ifndef GOOS_windows |
| 147 | JMP ok |
| 148 | #endif |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 149 | needtls: |
Matthew Dempsky | 8ee0fd8 | 2015-06-09 15:24:38 -0700 | [diff] [blame] | 150 | #ifdef GOOS_plan9 |
Akshat Kumar | a72bebf | 2012-08-31 13:21:13 -0400 | [diff] [blame] | 151 | // skip TLS setup on Plan 9 |
Matthew Dempsky | 8ee0fd8 | 2015-06-09 15:24:38 -0700 | [diff] [blame] | 152 | JMP ok |
| 153 | #endif |
| 154 | #ifdef GOOS_solaris |
Aram Hăvărneanu | a46b434 | 2014-01-17 17:58:10 +1300 | [diff] [blame] | 155 | // skip TLS setup on Solaris |
Matthew Dempsky | 8ee0fd8 | 2015-06-09 15:24:38 -0700 | [diff] [blame] | 156 | JMP ok |
| 157 | #endif |
Akshat Kumar | a72bebf | 2012-08-31 13:21:13 -0400 | [diff] [blame] | 158 | |
Matthew Dempsky | 7bb38f6 | 2015-11-12 15:35:50 -0800 | [diff] [blame] | 159 | LEAQ runtime·m0+m_tls(SB), DI |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 160 | CALL runtime·settls(SB) |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 161 | |
| 162 | // store through it, to make sure it works |
| 163 | get_tls(BX) |
| 164 | MOVQ $0x123, g(BX) |
Matthew Dempsky | 7bb38f6 | 2015-11-12 15:35:50 -0800 | [diff] [blame] | 165 | MOVQ runtime·m0+m_tls(SB), AX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 166 | CMPQ AX, $0x123 |
| 167 | JEQ 2(PC) |
| 168 | MOVL AX, 0 // abort |
| 169 | ok: |
| 170 | // set the per-goroutine and per-mach "registers" |
| 171 | get_tls(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 172 | LEAQ runtime·g0(SB), CX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 173 | MOVQ CX, g(BX) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 174 | LEAQ runtime·m0(SB), AX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 175 | |
| 176 | // save m->g0 = g0 |
| 177 | MOVQ CX, m_g0(AX) |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 178 | // save m0 to g0->m |
| 179 | MOVQ AX, g_m(CX) |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 180 | |
Ken Thompson | 8f53bc0 | 2008-12-15 15:07:35 -0800 | [diff] [blame] | 181 | CLD // convention is D is always left cleared |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 182 | CALL runtime·check(SB) |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 183 | |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 184 | MOVL 16(SP), AX // copy argc |
| 185 | MOVL AX, 0(SP) |
| 186 | MOVQ 24(SP), AX // copy argv |
| 187 | MOVQ AX, 8(SP) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 188 | CALL runtime·args(SB) |
| 189 | CALL runtime·osinit(SB) |
| 190 | CALL runtime·schedinit(SB) |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 191 | |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 192 | // create a new goroutine to start program |
Michael Hudson-Doyle | f78dc1d | 2015-03-29 23:38:20 +0000 | [diff] [blame] | 193 | MOVQ $runtime·mainPC(SB), AX // entry |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 194 | PUSHQ AX |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 195 | PUSHQ $0 // arg size |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 196 | CALL runtime·newproc(SB) |
Russ Cox | ebd1eef | 2008-09-22 13:47:59 -0700 | [diff] [blame] | 197 | POPQ AX |
| 198 | POPQ AX |
Russ Cox | 79e1db2 | 2008-12-04 08:30:54 -0800 | [diff] [blame] | 199 | |
Russ Cox | ebd1eef | 2008-09-22 13:47:59 -0700 | [diff] [blame] | 200 | // start this M |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 201 | CALL runtime·mstart(SB) |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 202 | |
Russ Cox | 36aa7d4 | 2012-03-08 14:03:56 -0500 | [diff] [blame] | 203 | MOVL $0xf1, 0xf1 // crash |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 204 | RET |
| 205 | |
Michael Hudson-Doyle | f78dc1d | 2015-03-29 23:38:20 +0000 | [diff] [blame] | 206 | DATA runtime·mainPC+0(SB)/8,$runtime·main(SB) |
| 207 | GLOBL runtime·mainPC(SB),RODATA,$8 |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 208 | |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 209 | TEXT runtime·breakpoint(SB),NOSPLIT,$0-0 |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 210 | BYTE $0xcc |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 211 | RET |
| 212 | |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 213 | TEXT runtime·asminit(SB),NOSPLIT,$0-0 |
Russ Cox | 1707a99 | 2012-02-14 01:23:15 -0500 | [diff] [blame] | 214 | // No per-thread init. |
| 215 | RET |
| 216 | |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 217 | /* |
| 218 | * go-routine |
| 219 | */ |
Rob Pike | d3204ef | 2008-06-30 14:39:47 -0700 | [diff] [blame] | 220 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 221 | // void gosave(Gobuf*) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 222 | // save state in Gobuf; setjmp |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 223 | TEXT runtime·gosave(SB), NOSPLIT, $0-8 |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 224 | MOVQ buf+0(FP), AX // gobuf |
| 225 | LEAQ buf+0(FP), BX // caller's SP |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 226 | MOVQ BX, gobuf_sp(AX) |
| 227 | MOVQ 0(SP), BX // caller's PC |
| 228 | MOVQ BX, gobuf_pc(AX) |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 229 | MOVQ $0, gobuf_ret(AX) |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 230 | MOVQ BP, gobuf_bp(AX) |
Austin Clements | 70c107c | 2016-10-19 15:49:31 -0400 | [diff] [blame] | 231 | // Assert ctxt is zero. See func save. |
| 232 | MOVQ gobuf_ctxt(AX), BX |
| 233 | TESTQ BX, BX |
| 234 | JZ 2(PC) |
| 235 | CALL runtime·badctxt(SB) |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 236 | get_tls(CX) |
| 237 | MOVQ g(CX), BX |
| 238 | MOVQ BX, gobuf_g(AX) |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 239 | RET |
| 240 | |
Ian Lance Taylor | 0627248 | 2013-06-12 15:05:10 -0700 | [diff] [blame] | 241 | // void gogo(Gobuf*) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 242 | // restore state from Gobuf; longjmp |
Austin Clements | 70c107c | 2016-10-19 15:49:31 -0400 | [diff] [blame] | 243 | TEXT runtime·gogo(SB), NOSPLIT, $16-8 |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 244 | MOVQ buf+0(FP), BX // gobuf |
Austin Clements | 70c107c | 2016-10-19 15:49:31 -0400 | [diff] [blame] | 245 | |
| 246 | // If ctxt is not nil, invoke deletion barrier before overwriting. |
| 247 | MOVQ gobuf_ctxt(BX), AX |
| 248 | TESTQ AX, AX |
| 249 | JZ nilctxt |
| 250 | LEAQ gobuf_ctxt(BX), AX |
| 251 | MOVQ AX, 0(SP) |
| 252 | MOVQ $0, 8(SP) |
| 253 | CALL runtime·writebarrierptr_prewrite(SB) |
| 254 | MOVQ buf+0(FP), BX |
| 255 | |
| 256 | nilctxt: |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 257 | MOVQ gobuf_g(BX), DX |
| 258 | MOVQ 0(DX), CX // make sure g != nil |
| 259 | get_tls(CX) |
| 260 | MOVQ DX, g(CX) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 261 | MOVQ gobuf_sp(BX), SP // restore SP |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 262 | MOVQ gobuf_ret(BX), AX |
| 263 | MOVQ gobuf_ctxt(BX), DX |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 264 | MOVQ gobuf_bp(BX), BP |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 265 | MOVQ $0, gobuf_sp(BX) // clear to help garbage collector |
| 266 | MOVQ $0, gobuf_ret(BX) |
| 267 | MOVQ $0, gobuf_ctxt(BX) |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 268 | MOVQ $0, gobuf_bp(BX) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 269 | MOVQ gobuf_pc(BX), BX |
| 270 | JMP BX |
| 271 | |
Russ Cox | 012ceed | 2014-09-03 11:35:22 -0400 | [diff] [blame] | 272 | // func mcall(fn func(*g)) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 273 | // Switch to m->g0's stack, call fn(g). |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 274 | // Fn must never return. It should gogo(&g->sched) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 275 | // to keep running g. |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 276 | TEXT runtime·mcall(SB), NOSPLIT, $0-8 |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 277 | MOVQ fn+0(FP), DI |
| 278 | |
| 279 | get_tls(CX) |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 280 | MOVQ g(CX), AX // save state in g->sched |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 281 | MOVQ 0(SP), BX // caller's PC |
| 282 | MOVQ BX, (g_sched+gobuf_pc)(AX) |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 283 | LEAQ fn+0(FP), BX // caller's SP |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 284 | MOVQ BX, (g_sched+gobuf_sp)(AX) |
| 285 | MOVQ AX, (g_sched+gobuf_g)(AX) |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 286 | MOVQ BP, (g_sched+gobuf_bp)(AX) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 287 | |
| 288 | // switch to m->g0 & its stack, call fn |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 289 | MOVQ g(CX), BX |
| 290 | MOVQ g_m(BX), BX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 291 | MOVQ m_g0(BX), SI |
| 292 | CMPQ SI, AX // if g == m->g0 call badmcall |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 293 | JNE 3(PC) |
Keith Randall | 32b770b | 2013-08-29 15:53:34 -0700 | [diff] [blame] | 294 | MOVQ $runtime·badmcall(SB), AX |
| 295 | JMP AX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 296 | MOVQ SI, g(CX) // g = m->g0 |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 297 | MOVQ (g_sched+gobuf_sp)(SI), SP // sp = m->g0->sched.sp |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 298 | PUSHQ AX |
Russ Cox | 012ceed | 2014-09-03 11:35:22 -0400 | [diff] [blame] | 299 | MOVQ DI, DX |
| 300 | MOVQ 0(DI), DI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 301 | CALL DI |
| 302 | POPQ AX |
Keith Randall | 32b770b | 2013-08-29 15:53:34 -0700 | [diff] [blame] | 303 | MOVQ $runtime·badmcall2(SB), AX |
| 304 | JMP AX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 305 | RET |
| 306 | |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 307 | // systemstack_switch is a dummy routine that systemstack leaves at the bottom |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 308 | // of the G stack. We need to distinguish the routine that |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 309 | // lives at the bottom of the G stack from the one that lives |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 310 | // at the top of the system stack because the one at the top of |
| 311 | // the system stack terminates the stack walk (see topofstack()). |
| 312 | TEXT runtime·systemstack_switch(SB), NOSPLIT, $0-0 |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 313 | RET |
| 314 | |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 315 | // func systemstack(fn func()) |
| 316 | TEXT runtime·systemstack(SB), NOSPLIT, $0-8 |
| 317 | MOVQ fn+0(FP), DI // DI = fn |
Russ Cox | 1d550b8 | 2014-09-11 12:08:30 -0400 | [diff] [blame] | 318 | get_tls(CX) |
| 319 | MOVQ g(CX), AX // AX = g |
| 320 | MOVQ g_m(AX), BX // BX = m |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 321 | |
Russ Cox | 1d550b8 | 2014-09-11 12:08:30 -0400 | [diff] [blame] | 322 | MOVQ m_gsignal(BX), DX // DX = gsignal |
| 323 | CMPQ AX, DX |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 324 | JEQ noswitch |
Russ Cox | 32ecf57 | 2014-09-04 00:10:10 -0400 | [diff] [blame] | 325 | |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 326 | MOVQ m_g0(BX), DX // DX = g0 |
| 327 | CMPQ AX, DX |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 328 | JEQ noswitch |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 329 | |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 330 | MOVQ m_curg(BX), R8 |
| 331 | CMPQ AX, R8 |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 332 | JEQ switch |
Russ Cox | 32ecf57 | 2014-09-04 00:10:10 -0400 | [diff] [blame] | 333 | |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 334 | // Bad: g is not gsignal, not g0, not curg. What is it? |
| 335 | MOVQ $runtime·badsystemstack(SB), AX |
Russ Cox | 32ecf57 | 2014-09-04 00:10:10 -0400 | [diff] [blame] | 336 | CALL AX |
| 337 | |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 338 | switch: |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 339 | // save our state in g->sched. Pretend to |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 340 | // be systemstack_switch if the G stack is scanned. |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 341 | MOVQ $runtime·systemstack_switch(SB), SI |
| 342 | MOVQ SI, (g_sched+gobuf_pc)(AX) |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 343 | MOVQ SP, (g_sched+gobuf_sp)(AX) |
| 344 | MOVQ AX, (g_sched+gobuf_g)(AX) |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 345 | MOVQ BP, (g_sched+gobuf_bp)(AX) |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 346 | |
| 347 | // switch to g0 |
| 348 | MOVQ DX, g(CX) |
Russ Cox | d16a2ad | 2014-09-04 22:48:08 -0400 | [diff] [blame] | 349 | MOVQ (g_sched+gobuf_sp)(DX), BX |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 350 | // make it look like mstart called systemstack on g0, to stop traceback |
Russ Cox | d16a2ad | 2014-09-04 22:48:08 -0400 | [diff] [blame] | 351 | SUBQ $8, BX |
| 352 | MOVQ $runtime·mstart(SB), DX |
| 353 | MOVQ DX, 0(BX) |
| 354 | MOVQ BX, SP |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 355 | |
| 356 | // call target function |
Russ Cox | 012ceed | 2014-09-03 11:35:22 -0400 | [diff] [blame] | 357 | MOVQ DI, DX |
| 358 | MOVQ 0(DI), DI |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 359 | CALL DI |
| 360 | |
| 361 | // switch back to g |
| 362 | get_tls(CX) |
| 363 | MOVQ g(CX), AX |
| 364 | MOVQ g_m(AX), BX |
| 365 | MOVQ m_curg(BX), AX |
| 366 | MOVQ AX, g(CX) |
| 367 | MOVQ (g_sched+gobuf_sp)(AX), SP |
| 368 | MOVQ $0, (g_sched+gobuf_sp)(AX) |
| 369 | RET |
| 370 | |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 371 | noswitch: |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 372 | // already on m stack, just call directly |
Russ Cox | 012ceed | 2014-09-03 11:35:22 -0400 | [diff] [blame] | 373 | MOVQ DI, DX |
| 374 | MOVQ 0(DI), DI |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 375 | CALL DI |
| 376 | RET |
| 377 | |
Rob Pike | 2da9783 | 2008-07-12 11:30:53 -0700 | [diff] [blame] | 378 | /* |
| 379 | * support for morestack |
| 380 | */ |
| 381 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 382 | // Called during function prolog when more stack is needed. |
Russ Cox | 58f12ff | 2013-07-18 16:53:45 -0400 | [diff] [blame] | 383 | // |
| 384 | // The traceback routines see morestack on a g0 as being |
| 385 | // the top of a stack (for example, morestack calling newstack |
| 386 | // calling the scheduler calling newm calling gc), so we must |
| 387 | // record an argument size. For that purpose, it has no arguments. |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 388 | TEXT runtime·morestack(SB),NOSPLIT,$0-0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 389 | // Cannot grow scheduler stack (m->g0). |
Anthony Martin | 2302b21 | 2014-09-10 06:25:05 -0700 | [diff] [blame] | 390 | get_tls(CX) |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 391 | MOVQ g(CX), BX |
| 392 | MOVQ g_m(BX), BX |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 393 | MOVQ m_g0(BX), SI |
| 394 | CMPQ g(CX), SI |
Austin Clements | 687d9d5 | 2016-10-13 10:44:57 -0400 | [diff] [blame] | 395 | JNE 3(PC) |
| 396 | CALL runtime·badmorestackg0(SB) |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 397 | INT $3 |
| 398 | |
Russ Cox | f8f630f | 2014-09-05 16:51:45 -0400 | [diff] [blame] | 399 | // Cannot grow signal stack (m->gsignal). |
| 400 | MOVQ m_gsignal(BX), SI |
| 401 | CMPQ g(CX), SI |
Austin Clements | 687d9d5 | 2016-10-13 10:44:57 -0400 | [diff] [blame] | 402 | JNE 3(PC) |
| 403 | CALL runtime·badmorestackgsignal(SB) |
Russ Cox | f8f630f | 2014-09-05 16:51:45 -0400 | [diff] [blame] | 404 | INT $3 |
| 405 | |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 406 | // Called from f. |
| 407 | // Set m->morebuf to f's caller. |
| 408 | MOVQ 8(SP), AX // f's caller's PC |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 409 | MOVQ AX, (m_morebuf+gobuf_pc)(BX) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 410 | LEAQ 16(SP), AX // f's caller's SP |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 411 | MOVQ AX, (m_morebuf+gobuf_sp)(BX) |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 412 | get_tls(CX) |
| 413 | MOVQ g(CX), SI |
| 414 | MOVQ SI, (m_morebuf+gobuf_g)(BX) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 415 | |
Russ Cox | 6fa3c89 | 2013-06-27 11:32:01 -0400 | [diff] [blame] | 416 | // Set g->sched to context in f. |
| 417 | MOVQ 0(SP), AX // f's PC |
| 418 | MOVQ AX, (g_sched+gobuf_pc)(SI) |
| 419 | MOVQ SI, (g_sched+gobuf_g)(SI) |
| 420 | LEAQ 8(SP), AX // f's SP |
| 421 | MOVQ AX, (g_sched+gobuf_sp)(SI) |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 422 | MOVQ BP, (g_sched+gobuf_bp)(SI) |
Austin Clements | bf9c71c | 2016-10-19 18:27:39 -0400 | [diff] [blame] | 423 | // newstack will fill gobuf.ctxt. |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 424 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 425 | // Call newstack on m->g0's stack. |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 426 | MOVQ m_g0(BX), BX |
| 427 | MOVQ BX, g(CX) |
| 428 | MOVQ (g_sched+gobuf_sp)(BX), SP |
Austin Clements | bf9c71c | 2016-10-19 18:27:39 -0400 | [diff] [blame] | 429 | PUSHQ DX // ctxt argument |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 430 | CALL runtime·newstack(SB) |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 431 | MOVQ $0, 0x1003 // crash if newstack returns |
Austin Clements | bf9c71c | 2016-10-19 18:27:39 -0400 | [diff] [blame] | 432 | POPQ DX // keep balance check happy |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 433 | RET |
| 434 | |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 435 | // morestack but not preserving ctxt. |
| 436 | TEXT runtime·morestack_noctxt(SB),NOSPLIT,$0 |
| 437 | MOVL $0, DX |
| 438 | JMP runtime·morestack(SB) |
| 439 | |
Keith Randall | 5263198 | 2014-09-08 10:14:41 -0700 | [diff] [blame] | 440 | // reflectcall: call a function with the given argument list |
Russ Cox | df027ac | 2014-12-30 13:59:55 -0500 | [diff] [blame] | 441 | // func call(argtype *_type, f *FuncVal, arg *byte, argsize, retoffset uint32). |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 442 | // we don't have variable-sized frames, so we use a small number |
| 443 | // of constant-sized-frame functions to encode a few bits of size in the pc. |
| 444 | // Caution: ugly multiline assembly macros in your future! |
| 445 | |
| 446 | #define DISPATCH(NAME,MAXSIZE) \ |
| 447 | CMPQ CX, $MAXSIZE; \ |
| 448 | JA 3(PC); \ |
Russ Cox | cb6f5ac | 2014-10-15 13:12:16 -0400 | [diff] [blame] | 449 | MOVQ $NAME(SB), AX; \ |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 450 | JMP AX |
Rob Pike | aff7883 | 2014-07-30 10:11:44 -0700 | [diff] [blame] | 451 | // Note: can't just "JMP NAME(SB)" - bad inlining results. |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 452 | |
Russ Cox | 7a524a1 | 2014-12-22 13:27:53 -0500 | [diff] [blame] | 453 | TEXT reflect·call(SB), NOSPLIT, $0-0 |
| 454 | JMP ·reflectcall(SB) |
| 455 | |
Russ Cox | df027ac | 2014-12-30 13:59:55 -0500 | [diff] [blame] | 456 | TEXT ·reflectcall(SB), NOSPLIT, $0-32 |
| 457 | MOVLQZX argsize+24(FP), CX |
Rob Pike | aff7883 | 2014-07-30 10:11:44 -0700 | [diff] [blame] | 458 | DISPATCH(runtime·call32, 32) |
| 459 | DISPATCH(runtime·call64, 64) |
| 460 | DISPATCH(runtime·call128, 128) |
| 461 | DISPATCH(runtime·call256, 256) |
| 462 | DISPATCH(runtime·call512, 512) |
| 463 | DISPATCH(runtime·call1024, 1024) |
| 464 | DISPATCH(runtime·call2048, 2048) |
| 465 | DISPATCH(runtime·call4096, 4096) |
| 466 | DISPATCH(runtime·call8192, 8192) |
| 467 | DISPATCH(runtime·call16384, 16384) |
| 468 | DISPATCH(runtime·call32768, 32768) |
| 469 | DISPATCH(runtime·call65536, 65536) |
| 470 | DISPATCH(runtime·call131072, 131072) |
| 471 | DISPATCH(runtime·call262144, 262144) |
| 472 | DISPATCH(runtime·call524288, 524288) |
| 473 | DISPATCH(runtime·call1048576, 1048576) |
| 474 | DISPATCH(runtime·call2097152, 2097152) |
| 475 | DISPATCH(runtime·call4194304, 4194304) |
| 476 | DISPATCH(runtime·call8388608, 8388608) |
| 477 | DISPATCH(runtime·call16777216, 16777216) |
| 478 | DISPATCH(runtime·call33554432, 33554432) |
| 479 | DISPATCH(runtime·call67108864, 67108864) |
| 480 | DISPATCH(runtime·call134217728, 134217728) |
| 481 | DISPATCH(runtime·call268435456, 268435456) |
| 482 | DISPATCH(runtime·call536870912, 536870912) |
| 483 | DISPATCH(runtime·call1073741824, 1073741824) |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 484 | MOVQ $runtime·badreflectcall(SB), AX |
| 485 | JMP AX |
| 486 | |
Keith Randall | 12e46e4 | 2013-08-06 14:33:55 -0700 | [diff] [blame] | 487 | #define CALLFN(NAME,MAXSIZE) \ |
Russ Cox | df027ac | 2014-12-30 13:59:55 -0500 | [diff] [blame] | 488 | TEXT NAME(SB), WRAPPER, $MAXSIZE-32; \ |
Russ Cox | cb6f5ac | 2014-10-15 13:12:16 -0400 | [diff] [blame] | 489 | NO_LOCAL_POINTERS; \ |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 490 | /* copy arguments to stack */ \ |
Russ Cox | df027ac | 2014-12-30 13:59:55 -0500 | [diff] [blame] | 491 | MOVQ argptr+16(FP), SI; \ |
| 492 | MOVLQZX argsize+24(FP), CX; \ |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 493 | MOVQ SP, DI; \ |
| 494 | REP;MOVSB; \ |
| 495 | /* call function */ \ |
Russ Cox | df027ac | 2014-12-30 13:59:55 -0500 | [diff] [blame] | 496 | MOVQ f+8(FP), DX; \ |
Keith Randall | cee8bca | 2014-05-21 14:28:34 -0700 | [diff] [blame] | 497 | PCDATA $PCDATA_StackMapIndex, $0; \ |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 498 | CALL (DX); \ |
| 499 | /* copy return values back */ \ |
Austin Clements | 79561a8 | 2016-10-20 22:45:18 -0400 | [diff] [blame] | 500 | MOVQ argtype+0(FP), DX; \ |
Russ Cox | df027ac | 2014-12-30 13:59:55 -0500 | [diff] [blame] | 501 | MOVQ argptr+16(FP), DI; \ |
| 502 | MOVLQZX argsize+24(FP), CX; \ |
Austin Clements | 79561a8 | 2016-10-20 22:45:18 -0400 | [diff] [blame] | 503 | MOVLQZX retoffset+28(FP), BX; \ |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 504 | MOVQ SP, SI; \ |
Russ Cox | 72c5d5e | 2014-04-08 11:11:35 -0400 | [diff] [blame] | 505 | ADDQ BX, DI; \ |
| 506 | ADDQ BX, SI; \ |
| 507 | SUBQ BX, CX; \ |
Austin Clements | 79561a8 | 2016-10-20 22:45:18 -0400 | [diff] [blame] | 508 | CALL callRet<>(SB); \ |
| 509 | RET |
| 510 | |
| 511 | // callRet copies return values back at the end of call*. This is a |
| 512 | // separate function so it can allocate stack space for the arguments |
| 513 | // to reflectcallmove. It does not follow the Go ABI; it expects its |
| 514 | // arguments in registers. |
| 515 | TEXT callRet<>(SB), NOSPLIT, $32-0 |
| 516 | NO_LOCAL_POINTERS |
| 517 | MOVQ DX, 0(SP) |
| 518 | MOVQ DI, 8(SP) |
| 519 | MOVQ SI, 16(SP) |
| 520 | MOVQ CX, 24(SP) |
| 521 | CALL runtime·reflectcallmove(SB) |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 522 | RET |
| 523 | |
Russ Cox | cb6f5ac | 2014-10-15 13:12:16 -0400 | [diff] [blame] | 524 | CALLFN(·call32, 32) |
| 525 | CALLFN(·call64, 64) |
| 526 | CALLFN(·call128, 128) |
| 527 | CALLFN(·call256, 256) |
| 528 | CALLFN(·call512, 512) |
| 529 | CALLFN(·call1024, 1024) |
| 530 | CALLFN(·call2048, 2048) |
| 531 | CALLFN(·call4096, 4096) |
| 532 | CALLFN(·call8192, 8192) |
| 533 | CALLFN(·call16384, 16384) |
| 534 | CALLFN(·call32768, 32768) |
| 535 | CALLFN(·call65536, 65536) |
| 536 | CALLFN(·call131072, 131072) |
| 537 | CALLFN(·call262144, 262144) |
| 538 | CALLFN(·call524288, 524288) |
| 539 | CALLFN(·call1048576, 1048576) |
| 540 | CALLFN(·call2097152, 2097152) |
| 541 | CALLFN(·call4194304, 4194304) |
| 542 | CALLFN(·call8388608, 8388608) |
| 543 | CALLFN(·call16777216, 16777216) |
| 544 | CALLFN(·call33554432, 33554432) |
| 545 | CALLFN(·call67108864, 67108864) |
| 546 | CALLFN(·call134217728, 134217728) |
| 547 | CALLFN(·call268435456, 268435456) |
| 548 | CALLFN(·call536870912, 536870912) |
| 549 | CALLFN(·call1073741824, 1073741824) |
Keith Randall | 9cd5706 | 2013-08-02 13:03:14 -0700 | [diff] [blame] | 550 | |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 551 | TEXT runtime·procyield(SB),NOSPLIT,$0-0 |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 552 | MOVL cycles+0(FP), AX |
Dmitriy Vyukov | 4e5086b | 2011-07-29 12:44:06 -0400 | [diff] [blame] | 553 | again: |
| 554 | PAUSE |
| 555 | SUBL $1, AX |
| 556 | JNZ again |
| 557 | RET |
| 558 | |
Russ Cox | 631d6a3 | 2015-03-19 19:42:16 -0400 | [diff] [blame] | 559 | |
Austin Clements | f5d494b | 2015-06-15 12:30:23 -0400 | [diff] [blame] | 560 | TEXT ·publicationBarrier(SB),NOSPLIT,$0-0 |
| 561 | // Stores are already ordered on x86, so this is just a |
| 562 | // compile barrier. |
| 563 | RET |
| 564 | |
Russ Cox | aa3222d8 | 2009-06-02 23:02:12 -0700 | [diff] [blame] | 565 | // void jmpdefer(fn, sp); |
| 566 | // called from deferreturn. |
Ken Thompson | 1e1cc4e | 2009-01-27 12:03:53 -0800 | [diff] [blame] | 567 | // 1. pop the caller |
| 568 | // 2. sub 5 bytes from the callers return |
| 569 | // 3. jmp to the argument |
Keith Randall | a97a91d | 2013-08-07 14:03:50 -0700 | [diff] [blame] | 570 | TEXT runtime·jmpdefer(SB), NOSPLIT, $0-16 |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 571 | MOVQ fv+0(FP), DX // fn |
| 572 | MOVQ argp+8(FP), BX // caller sp |
Russ Cox | aa3222d8 | 2009-06-02 23:02:12 -0700 | [diff] [blame] | 573 | LEAQ -8(BX), SP // caller sp after CALL |
Austin Clements | b92f423 | 2016-05-25 20:56:56 -0400 | [diff] [blame] | 574 | MOVQ -8(SP), BP // restore BP as if deferreturn returned (harmless if framepointers not in use) |
Russ Cox | aa3222d8 | 2009-06-02 23:02:12 -0700 | [diff] [blame] | 575 | SUBQ $5, (SP) // return to CALL again |
Russ Cox | 6066fdc | 2013-02-22 10:47:54 -0500 | [diff] [blame] | 576 | MOVQ 0(DX), BX |
Russ Cox | 1903ad7 | 2013-02-21 17:01:13 -0500 | [diff] [blame] | 577 | JMP BX // but first run the deferred function |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 578 | |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 579 | // Save state of caller into g->sched. Smashes R8, R9. |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 580 | TEXT gosave<>(SB),NOSPLIT,$0 |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 581 | get_tls(R8) |
| 582 | MOVQ g(R8), R8 |
| 583 | MOVQ 0(SP), R9 |
| 584 | MOVQ R9, (g_sched+gobuf_pc)(R8) |
| 585 | LEAQ 8(SP), R9 |
| 586 | MOVQ R9, (g_sched+gobuf_sp)(R8) |
| 587 | MOVQ $0, (g_sched+gobuf_ret)(R8) |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 588 | MOVQ BP, (g_sched+gobuf_bp)(R8) |
Austin Clements | 70c107c | 2016-10-19 15:49:31 -0400 | [diff] [blame] | 589 | // Assert ctxt is zero. See func save. |
| 590 | MOVQ (g_sched+gobuf_ctxt)(R8), R9 |
| 591 | TESTQ R9, R9 |
| 592 | JZ 2(PC) |
| 593 | CALL runtime·badctxt(SB) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 594 | RET |
| 595 | |
Alex Brainman | 9d968cb | 2015-04-27 17:32:23 +1000 | [diff] [blame] | 596 | // func asmcgocall(fn, arg unsafe.Pointer) int32 |
Russ Cox | add89dd | 2009-10-12 10:26:38 -0700 | [diff] [blame] | 597 | // Call fn(arg) on the scheduler stack, |
| 598 | // aligned appropriately for the gcc ABI. |
Alex Brainman | 9d968cb | 2015-04-27 17:32:23 +1000 | [diff] [blame] | 599 | // See cgocall.go for more details. |
| 600 | TEXT ·asmcgocall(SB),NOSPLIT,$0-20 |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 601 | MOVQ fn+0(FP), AX |
| 602 | MOVQ arg+8(FP), BX |
Russ Cox | cb76724 | 2014-09-04 00:01:55 -0400 | [diff] [blame] | 603 | |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 604 | MOVQ SP, DX |
Russ Cox | add89dd | 2009-10-12 10:26:38 -0700 | [diff] [blame] | 605 | |
| 606 | // Figure out if we need to switch to m->g0 stack. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 607 | // We get called to create new OS threads too, and those |
| 608 | // come in on the m->g0 stack already. |
| 609 | get_tls(CX) |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 610 | MOVQ g(CX), R8 |
Russ Cox | 3af29fb | 2015-11-19 15:51:39 -0500 | [diff] [blame] | 611 | CMPQ R8, $0 |
| 612 | JEQ nosave |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 613 | MOVQ g_m(R8), R8 |
| 614 | MOVQ m_g0(R8), SI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 615 | MOVQ g(CX), DI |
| 616 | CMPQ SI, DI |
Aram Hăvărneanu | a46b434 | 2014-01-17 17:58:10 +1300 | [diff] [blame] | 617 | JEQ nosave |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 618 | MOVQ m_gsignal(R8), SI |
Aram Hăvărneanu | a46b434 | 2014-01-17 17:58:10 +1300 | [diff] [blame] | 619 | CMPQ SI, DI |
| 620 | JEQ nosave |
| 621 | |
Russ Cox | 3af29fb | 2015-11-19 15:51:39 -0500 | [diff] [blame] | 622 | // Switch to system stack. |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 623 | MOVQ m_g0(R8), SI |
Russ Cox | d67e7e3 | 2013-06-12 15:22:26 -0400 | [diff] [blame] | 624 | CALL gosave<>(SB) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 625 | MOVQ SI, g(CX) |
| 626 | MOVQ (g_sched+gobuf_sp)(SI), SP |
Russ Cox | add89dd | 2009-10-12 10:26:38 -0700 | [diff] [blame] | 627 | |
| 628 | // Now on a scheduling stack (a pthread-created stack). |
Alex Brainman | 7f075ec | 2012-09-03 12:12:51 +1000 | [diff] [blame] | 629 | // Make sure we have enough room for 4 stack-backed fast-call |
| 630 | // registers as per windows amd64 calling convention. |
| 631 | SUBQ $64, SP |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 632 | ANDQ $~15, SP // alignment for gcc ABI |
Alex Brainman | 7f075ec | 2012-09-03 12:12:51 +1000 | [diff] [blame] | 633 | MOVQ DI, 48(SP) // save g |
Keith Randall | 47f251c | 2014-09-11 20:36:23 -0700 | [diff] [blame] | 634 | MOVQ (g_stack+stack_hi)(DI), DI |
| 635 | SUBQ DX, DI |
| 636 | MOVQ DI, 40(SP) // save depth in stack (can't just save SP, as stack might be copied during a callback) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 637 | MOVQ BX, DI // DI = first argument in AMD64 ABI |
Wei Guangjing | 9f63659 | 2011-07-19 10:47:33 -0400 | [diff] [blame] | 638 | MOVQ BX, CX // CX = first argument in Win64 |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 639 | CALL AX |
Russ Cox | add89dd | 2009-10-12 10:26:38 -0700 | [diff] [blame] | 640 | |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 641 | // Restore registers, g, stack pointer. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 642 | get_tls(CX) |
Alex Brainman | 7f075ec | 2012-09-03 12:12:51 +1000 | [diff] [blame] | 643 | MOVQ 48(SP), DI |
Keith Randall | 47f251c | 2014-09-11 20:36:23 -0700 | [diff] [blame] | 644 | MOVQ (g_stack+stack_hi)(DI), SI |
| 645 | SUBQ 40(SP), SI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 646 | MOVQ DI, g(CX) |
Keith Randall | 47f251c | 2014-09-11 20:36:23 -0700 | [diff] [blame] | 647 | MOVQ SI, SP |
Alex Brainman | 9d968cb | 2015-04-27 17:32:23 +1000 | [diff] [blame] | 648 | |
| 649 | MOVL AX, ret+16(FP) |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 650 | RET |
| 651 | |
Russ Cox | 3af29fb | 2015-11-19 15:51:39 -0500 | [diff] [blame] | 652 | nosave: |
| 653 | // Running on a system stack, perhaps even without a g. |
| 654 | // Having no g can happen during thread creation or thread teardown |
| 655 | // (see needm/dropm on Solaris, for example). |
| 656 | // This code is like the above sequence but without saving/restoring g |
| 657 | // and without worrying about the stack moving out from under us |
| 658 | // (because we're on a system stack, not a goroutine stack). |
| 659 | // The above code could be used directly if already on a system stack, |
| 660 | // but then the only path through this code would be a rare case on Solaris. |
| 661 | // Using this code for all "already on system stack" calls exercises it more, |
| 662 | // which should help keep it correct. |
| 663 | SUBQ $64, SP |
| 664 | ANDQ $~15, SP |
| 665 | MOVQ $0, 48(SP) // where above code stores g, in case someone looks during debugging |
| 666 | MOVQ DX, 40(SP) // save original stack pointer |
| 667 | MOVQ BX, DI // DI = first argument in AMD64 ABI |
| 668 | MOVQ BX, CX // CX = first argument in Win64 |
| 669 | CALL AX |
| 670 | MOVQ 40(SP), SI // restore original stack pointer |
| 671 | MOVQ SI, SP |
| 672 | MOVL AX, ret+16(FP) |
| 673 | RET |
| 674 | |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 675 | // cgocallback(void (*fn)(void*), void *frame, uintptr framesize, uintptr ctxt) |
Russ Cox | 3d2dfc5 | 2013-02-22 16:08:56 -0500 | [diff] [blame] | 676 | // Turn the fn into a Go func (by taking its address) and call |
| 677 | // cgocallback_gofunc. |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 678 | TEXT runtime·cgocallback(SB),NOSPLIT,$32-32 |
Russ Cox | 3d2dfc5 | 2013-02-22 16:08:56 -0500 | [diff] [blame] | 679 | LEAQ fn+0(FP), AX |
| 680 | MOVQ AX, 0(SP) |
| 681 | MOVQ frame+8(FP), AX |
| 682 | MOVQ AX, 8(SP) |
| 683 | MOVQ framesize+16(FP), AX |
| 684 | MOVQ AX, 16(SP) |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 685 | MOVQ ctxt+24(FP), AX |
| 686 | MOVQ AX, 24(SP) |
Russ Cox | 3d2dfc5 | 2013-02-22 16:08:56 -0500 | [diff] [blame] | 687 | MOVQ $runtime·cgocallback_gofunc(SB), AX |
| 688 | CALL AX |
| 689 | RET |
| 690 | |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 691 | // cgocallback_gofunc(FuncVal*, void *frame, uintptr framesize, uintptr ctxt) |
Alex Brainman | 9d968cb | 2015-04-27 17:32:23 +1000 | [diff] [blame] | 692 | // See cgocall.go for more details. |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 693 | TEXT ·cgocallback_gofunc(SB),NOSPLIT,$16-32 |
Russ Cox | e844f53 | 2014-09-12 07:46:11 -0400 | [diff] [blame] | 694 | NO_LOCAL_POINTERS |
| 695 | |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 696 | // If g is nil, Go did not create the current thread. |
| 697 | // Call needm to obtain one m for temporary use. |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 698 | // In this case, we're running on the thread stack, so there's |
| 699 | // lots of space, but the linker doesn't know. Hide the call from |
| 700 | // the linker analysis by using an indirect call through AX. |
| 701 | get_tls(CX) |
| 702 | #ifdef GOOS_windows |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 703 | MOVL $0, BX |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 704 | CMPQ CX, $0 |
Russ Cox | cefdb9c | 2013-07-23 22:59:32 -0400 | [diff] [blame] | 705 | JEQ 2(PC) |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 706 | #endif |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 707 | MOVQ g(CX), BX |
| 708 | CMPQ BX, $0 |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 709 | JEQ needm |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 710 | MOVQ g_m(BX), BX |
| 711 | MOVQ BX, R8 // holds oldm until end of function |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 712 | JMP havem |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 713 | needm: |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 714 | MOVQ $0, 0(SP) |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 715 | MOVQ $runtime·needm(SB), AX |
| 716 | CALL AX |
Russ Cox | f011282 | 2013-07-24 09:01:57 -0400 | [diff] [blame] | 717 | MOVQ 0(SP), R8 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 718 | get_tls(CX) |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 719 | MOVQ g(CX), BX |
| 720 | MOVQ g_m(BX), BX |
Russ Cox | c4efaac | 2014-10-28 21:53:09 -0400 | [diff] [blame] | 721 | |
| 722 | // Set m->sched.sp = SP, so that if a panic happens |
| 723 | // during the function we are about to execute, it will |
| 724 | // have a valid SP to run on the g0 stack. |
| 725 | // The next few lines (after the havem label) |
| 726 | // will save this SP onto the stack and then write |
| 727 | // the same SP back to m->sched.sp. That seems redundant, |
| 728 | // but if an unrecovered panic happens, unwindm will |
| 729 | // restore the g->sched.sp from the stack location |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 730 | // and then systemstack will try to use it. If we don't set it here, |
Russ Cox | c4efaac | 2014-10-28 21:53:09 -0400 | [diff] [blame] | 731 | // that restored SP will be uninitialized (typically 0) and |
| 732 | // will not be usable. |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 733 | MOVQ m_g0(BX), SI |
Russ Cox | c4efaac | 2014-10-28 21:53:09 -0400 | [diff] [blame] | 734 | MOVQ SP, (g_sched+gobuf_sp)(SI) |
Russ Cox | 9b73238 | 2012-03-08 12:12:40 -0500 | [diff] [blame] | 735 | |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 736 | havem: |
| 737 | // Now there's a valid m, and we're running on its m->g0. |
| 738 | // Save current m->g0->sched.sp on stack and then set it to SP. |
| 739 | // Save current sp in m->g0->sched.sp in preparation for |
| 740 | // switch back to m->curg stack. |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 741 | // NOTE: unwindm knows that the saved g->sched.sp is at 0(SP). |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 742 | MOVQ m_g0(BX), SI |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 743 | MOVQ (g_sched+gobuf_sp)(SI), AX |
| 744 | MOVQ AX, 0(SP) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 745 | MOVQ SP, (g_sched+gobuf_sp)(SI) |
Ian Lance Taylor | 2d0ff3f | 2010-04-09 13:30:11 -0700 | [diff] [blame] | 746 | |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 747 | // Switch to m->curg stack and call runtime.cgocallbackg. |
| 748 | // Because we are taking over the execution of m->curg |
| 749 | // but *not* resuming what had been running, we need to |
| 750 | // save that information (m->curg->sched) so we can restore it. |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 751 | // We can restore m->curg->sched.sp easily, because calling |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 752 | // runtime.cgocallbackg leaves SP unchanged upon return. |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 753 | // To save m->curg->sched.pc, we push it onto the stack. |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 754 | // This has the added benefit that it looks to the traceback |
Alex Brainman | 72e8348 | 2011-08-18 12:17:09 -0400 | [diff] [blame] | 755 | // routine like cgocallbackg is going to return to that |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 756 | // PC (because the frame we allocate below has the same |
| 757 | // size as cgocallback_gofunc's frame declared above) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 758 | // so that the traceback will seamlessly trace back into |
| 759 | // the earlier calls. |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 760 | // |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 761 | // In the new goroutine, 8(SP) holds the saved R8. |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 762 | MOVQ m_curg(BX), SI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 763 | MOVQ SI, g(CX) |
| 764 | MOVQ (g_sched+gobuf_sp)(SI), DI // prepare stack as DI |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 765 | MOVQ (g_sched+gobuf_pc)(SI), BX |
| 766 | MOVQ BX, -8(DI) |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 767 | // Compute the size of the frame, including return PC and, if |
Keith Randall | 1ea60c1 | 2016-12-02 15:17:52 -0800 | [diff] [blame] | 768 | // GOEXPERIMENT=framepointer, the saved base pointer |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 769 | MOVQ ctxt+24(FP), BX |
Rob Pike | c21f1d5 | 2015-02-19 13:44:06 -0800 | [diff] [blame] | 770 | LEAQ fv+0(FP), AX |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 771 | SUBQ SP, AX |
| 772 | SUBQ AX, DI |
| 773 | MOVQ DI, SP |
| 774 | |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 775 | MOVQ R8, 8(SP) |
| 776 | MOVQ BX, 0(SP) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 777 | CALL runtime·cgocallbackg(SB) |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 778 | MOVQ 8(SP), R8 |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 779 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 780 | // Compute the size of the frame again. FP and SP have |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 781 | // completely different values here than they did above, |
| 782 | // but only their difference matters. |
Rob Pike | c21f1d5 | 2015-02-19 13:44:06 -0800 | [diff] [blame] | 783 | LEAQ fv+0(FP), AX |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 784 | SUBQ SP, AX |
| 785 | |
Russ Cox | 528534c | 2013-06-05 07:16:53 -0400 | [diff] [blame] | 786 | // Restore g->sched (== m->curg->sched) from saved values. |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 787 | get_tls(CX) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 788 | MOVQ g(CX), SI |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 789 | MOVQ SP, DI |
| 790 | ADDQ AX, DI |
| 791 | MOVQ -8(DI), BX |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 792 | MOVQ BX, (g_sched+gobuf_pc)(SI) |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 793 | MOVQ DI, (g_sched+gobuf_sp)(SI) |
| 794 | |
| 795 | // Switch back to m->g0's stack and restore m->g0->sched.sp. |
| 796 | // (Unlike m->curg, the g0 goroutine never uses sched.pc, |
| 797 | // so we do not have to restore it.) |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 798 | MOVQ g(CX), BX |
| 799 | MOVQ g_m(BX), BX |
| 800 | MOVQ m_g0(BX), SI |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 801 | MOVQ SI, g(CX) |
| 802 | MOVQ (g_sched+gobuf_sp)(SI), SP |
Russ Cox | dba623b | 2013-07-23 18:40:02 -0400 | [diff] [blame] | 803 | MOVQ 0(SP), AX |
| 804 | MOVQ AX, (g_sched+gobuf_sp)(SI) |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 805 | |
| 806 | // If the m on entry was nil, we called needm above to borrow an m |
| 807 | // 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] | 808 | CMPQ R8, $0 |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 809 | JNE 3(PC) |
| 810 | MOVQ $runtime·dropm(SB), AX |
| 811 | CALL AX |
Russ Cox | f9ca3b5 | 2011-03-07 10:37:42 -0500 | [diff] [blame] | 812 | |
| 813 | // Done! |
Ian Lance Taylor | 2d0ff3f | 2010-04-09 13:30:11 -0700 | [diff] [blame] | 814 | RET |
| 815 | |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 816 | // void setg(G*); set g. for use by needm. |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 817 | TEXT runtime·setg(SB), NOSPLIT, $0-8 |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 818 | MOVQ gg+0(FP), BX |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 819 | #ifdef GOOS_windows |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 820 | CMPQ BX, $0 |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 821 | JNE settls |
| 822 | MOVQ $0, 0x28(GS) |
| 823 | RET |
| 824 | settls: |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 825 | MOVQ g_m(BX), AX |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 826 | LEAQ m_tls(AX), AX |
| 827 | MOVQ AX, 0x28(GS) |
| 828 | #endif |
| 829 | get_tls(CX) |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 830 | MOVQ BX, g(CX) |
| 831 | RET |
| 832 | |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 833 | // void setg_gcc(G*); set g called from gcc. |
| 834 | TEXT setg_gcc<>(SB),NOSPLIT,$0 |
Russ Cox | 6a70f9d | 2013-03-25 18:14:02 -0400 | [diff] [blame] | 835 | get_tls(AX) |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 836 | MOVQ DI, g(AX) |
Russ Cox | 6a70f9d | 2013-03-25 18:14:02 -0400 | [diff] [blame] | 837 | RET |
| 838 | |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 839 | // check that SP is in range [g->stack.lo, g->stack.hi) |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 840 | TEXT runtime·stackcheck(SB), NOSPLIT, $0-0 |
Russ Cox | e473f42 | 2010-08-04 17:50:22 -0700 | [diff] [blame] | 841 | get_tls(CX) |
| 842 | MOVQ g(CX), AX |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 843 | CMPQ (g_stack+stack_hi)(AX), SP |
Russ Cox | 01eaf78 | 2010-03-30 10:53:16 -0700 | [diff] [blame] | 844 | JHI 2(PC) |
| 845 | INT $3 |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 846 | CMPQ SP, (g_stack+stack_lo)(AX) |
Russ Cox | 01eaf78 | 2010-03-30 10:53:16 -0700 | [diff] [blame] | 847 | JHI 2(PC) |
| 848 | INT $3 |
| 849 | RET |
| 850 | |
Dmitry Vyukov | 6e70fdd | 2015-02-17 14:25:49 +0300 | [diff] [blame] | 851 | // func cputicks() int64 |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 852 | TEXT runtime·cputicks(SB),NOSPLIT,$0-0 |
Dmitry Vyukov | 6e70fdd | 2015-02-17 14:25:49 +0300 | [diff] [blame] | 853 | CMPB runtime·lfenceBeforeRdtsc(SB), $1 |
| 854 | JNE mfence |
Ilya Tocar | 1d1f2fb | 2016-01-13 16:43:22 +0300 | [diff] [blame] | 855 | LFENCE |
Dmitry Vyukov | 6e70fdd | 2015-02-17 14:25:49 +0300 | [diff] [blame] | 856 | JMP done |
| 857 | mfence: |
Ilya Tocar | 1d1f2fb | 2016-01-13 16:43:22 +0300 | [diff] [blame] | 858 | MFENCE |
Dmitry Vyukov | 6e70fdd | 2015-02-17 14:25:49 +0300 | [diff] [blame] | 859 | done: |
Damian Gryski | 8e765da | 2012-02-02 14:09:27 -0500 | [diff] [blame] | 860 | RDTSC |
| 861 | SHLQ $32, DX |
| 862 | ADDQ DX, AX |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 863 | MOVQ AX, ret+0(FP) |
Damian Gryski | 8e765da | 2012-02-02 14:09:27 -0500 | [diff] [blame] | 864 | RET |
| 865 | |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 866 | // hash function using AES hardware instructions |
Keith Randall | a2a9768 | 2014-07-31 15:07:05 -0700 | [diff] [blame] | 867 | TEXT runtime·aeshash(SB),NOSPLIT,$0-32 |
| 868 | MOVQ p+0(FP), AX // ptr to data |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 869 | MOVQ s+16(FP), CX // size |
| 870 | LEAQ ret+24(FP), DX |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 871 | JMP runtime·aeshashbody(SB) |
| 872 | |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 873 | TEXT runtime·aeshashstr(SB),NOSPLIT,$0-24 |
Keith Randall | a2a9768 | 2014-07-31 15:07:05 -0700 | [diff] [blame] | 874 | MOVQ p+0(FP), AX // ptr to string struct |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 875 | MOVQ 8(AX), CX // length of string |
| 876 | MOVQ (AX), AX // string data |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 877 | LEAQ ret+16(FP), DX |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 878 | JMP runtime·aeshashbody(SB) |
| 879 | |
| 880 | // AX: data |
| 881 | // CX: length |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 882 | // DX: address to put return value |
| 883 | TEXT runtime·aeshashbody(SB),NOSPLIT,$0-0 |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 884 | // Fill an SSE register with our seeds. |
| 885 | MOVQ h+8(FP), X0 // 64 bits of per-table hash seed |
| 886 | PINSRW $4, CX, X0 // 16 bits of length |
| 887 | PSHUFHW $0, X0, X0 // repeat length 4 times total |
| 888 | MOVO X0, X1 // save unscrambled seed |
| 889 | PXOR runtime·aeskeysched(SB), X0 // xor in per-process seed |
| 890 | AESENC X0, X0 // scramble seed |
| 891 | |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 892 | CMPQ CX, $16 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 893 | JB aes0to15 |
| 894 | JE aes16 |
| 895 | CMPQ CX, $32 |
| 896 | JBE aes17to32 |
| 897 | CMPQ CX, $64 |
| 898 | JBE aes33to64 |
| 899 | CMPQ CX, $128 |
| 900 | JBE aes65to128 |
| 901 | JMP aes129plus |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 902 | |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 903 | aes0to15: |
| 904 | TESTQ CX, CX |
| 905 | JE aes0 |
| 906 | |
| 907 | ADDQ $16, AX |
| 908 | TESTW $0xff0, AX |
| 909 | JE endofpage |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 910 | |
Keith Randall | ee66972 | 2013-05-15 09:40:14 -0700 | [diff] [blame] | 911 | // 16 bytes loaded at this address won't cross |
| 912 | // a page boundary, so we can load it directly. |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 913 | MOVOU -16(AX), X1 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 914 | ADDQ CX, CX |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 915 | MOVQ $masks<>(SB), AX |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 916 | PAND (AX)(CX*8), X1 |
| 917 | final1: |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 918 | PXOR X0, X1 // xor data with seed |
| 919 | AESENC X1, X1 // scramble combo 3 times |
| 920 | AESENC X1, X1 |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 921 | AESENC X1, X1 |
| 922 | MOVQ X1, (DX) |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 923 | RET |
| 924 | |
| 925 | endofpage: |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 926 | // address ends in 1111xxxx. Might be up against |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 927 | // a page boundary, so load ending at last byte. |
| 928 | // Then shift bytes down using pshufb. |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 929 | MOVOU -32(AX)(CX*1), X1 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 930 | ADDQ CX, CX |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 931 | MOVQ $shifts<>(SB), AX |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 932 | PSHUFB (AX)(CX*8), X1 |
| 933 | JMP final1 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 934 | |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 935 | aes0: |
Keith Randall | 731bdc5 | 2015-09-01 12:53:15 -0700 | [diff] [blame] | 936 | // Return scrambled input seed |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 937 | AESENC X0, X0 |
| 938 | MOVQ X0, (DX) |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 939 | RET |
| 940 | |
| 941 | aes16: |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 942 | MOVOU (AX), X1 |
| 943 | JMP final1 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 944 | |
| 945 | aes17to32: |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 946 | // make second starting seed |
| 947 | PXOR runtime·aeskeysched+16(SB), X1 |
| 948 | AESENC X1, X1 |
| 949 | |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 950 | // load data to be hashed |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 951 | MOVOU (AX), X2 |
| 952 | MOVOU -16(AX)(CX*1), X3 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 953 | |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 954 | // xor with seed |
| 955 | PXOR X0, X2 |
| 956 | PXOR X1, X3 |
| 957 | |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 958 | // scramble 3 times |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 959 | AESENC X2, X2 |
| 960 | AESENC X3, X3 |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 961 | AESENC X2, X2 |
| 962 | AESENC X3, X3 |
| 963 | AESENC X2, X2 |
| 964 | AESENC X3, X3 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 965 | |
| 966 | // combine results |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 967 | PXOR X3, X2 |
| 968 | MOVQ X2, (DX) |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 969 | RET |
| 970 | |
| 971 | aes33to64: |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 972 | // make 3 more starting seeds |
| 973 | MOVO X1, X2 |
| 974 | MOVO X1, X3 |
| 975 | PXOR runtime·aeskeysched+16(SB), X1 |
| 976 | PXOR runtime·aeskeysched+32(SB), X2 |
| 977 | PXOR runtime·aeskeysched+48(SB), X3 |
| 978 | AESENC X1, X1 |
| 979 | AESENC X2, X2 |
| 980 | AESENC X3, X3 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 981 | |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 982 | MOVOU (AX), X4 |
| 983 | MOVOU 16(AX), X5 |
| 984 | MOVOU -32(AX)(CX*1), X6 |
| 985 | MOVOU -16(AX)(CX*1), X7 |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 986 | |
| 987 | PXOR X0, X4 |
| 988 | PXOR X1, X5 |
| 989 | PXOR X2, X6 |
| 990 | PXOR X3, X7 |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 991 | |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 992 | AESENC X4, X4 |
| 993 | AESENC X5, X5 |
| 994 | AESENC X6, X6 |
| 995 | AESENC X7, X7 |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 996 | |
| 997 | AESENC X4, X4 |
| 998 | AESENC X5, X5 |
| 999 | AESENC X6, X6 |
| 1000 | AESENC X7, X7 |
| 1001 | |
| 1002 | AESENC X4, X4 |
| 1003 | AESENC X5, X5 |
| 1004 | AESENC X6, X6 |
| 1005 | AESENC X7, X7 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1006 | |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1007 | PXOR X6, X4 |
| 1008 | PXOR X7, X5 |
| 1009 | PXOR X5, X4 |
| 1010 | MOVQ X4, (DX) |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1011 | RET |
| 1012 | |
| 1013 | aes65to128: |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1014 | // make 7 more starting seeds |
| 1015 | MOVO X1, X2 |
| 1016 | MOVO X1, X3 |
| 1017 | MOVO X1, X4 |
| 1018 | MOVO X1, X5 |
| 1019 | MOVO X1, X6 |
| 1020 | MOVO X1, X7 |
| 1021 | PXOR runtime·aeskeysched+16(SB), X1 |
| 1022 | PXOR runtime·aeskeysched+32(SB), X2 |
| 1023 | PXOR runtime·aeskeysched+48(SB), X3 |
| 1024 | PXOR runtime·aeskeysched+64(SB), X4 |
| 1025 | PXOR runtime·aeskeysched+80(SB), X5 |
| 1026 | PXOR runtime·aeskeysched+96(SB), X6 |
| 1027 | PXOR runtime·aeskeysched+112(SB), X7 |
| 1028 | AESENC X1, X1 |
| 1029 | AESENC X2, X2 |
| 1030 | AESENC X3, X3 |
| 1031 | AESENC X4, X4 |
| 1032 | AESENC X5, X5 |
| 1033 | AESENC X6, X6 |
| 1034 | AESENC X7, X7 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1035 | |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1036 | // load data |
| 1037 | MOVOU (AX), X8 |
| 1038 | MOVOU 16(AX), X9 |
| 1039 | MOVOU 32(AX), X10 |
| 1040 | MOVOU 48(AX), X11 |
| 1041 | MOVOU -64(AX)(CX*1), X12 |
| 1042 | MOVOU -48(AX)(CX*1), X13 |
| 1043 | MOVOU -32(AX)(CX*1), X14 |
| 1044 | MOVOU -16(AX)(CX*1), X15 |
| 1045 | |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 1046 | // xor with seed |
| 1047 | PXOR X0, X8 |
| 1048 | PXOR X1, X9 |
| 1049 | PXOR X2, X10 |
| 1050 | PXOR X3, X11 |
| 1051 | PXOR X4, X12 |
| 1052 | PXOR X5, X13 |
| 1053 | PXOR X6, X14 |
| 1054 | PXOR X7, X15 |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1055 | |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 1056 | // scramble 3 times |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1057 | AESENC X8, X8 |
| 1058 | AESENC X9, X9 |
| 1059 | AESENC X10, X10 |
| 1060 | AESENC X11, X11 |
| 1061 | AESENC X12, X12 |
| 1062 | AESENC X13, X13 |
| 1063 | AESENC X14, X14 |
| 1064 | AESENC X15, X15 |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 1065 | |
| 1066 | AESENC X8, X8 |
| 1067 | AESENC X9, X9 |
| 1068 | AESENC X10, X10 |
| 1069 | AESENC X11, X11 |
| 1070 | AESENC X12, X12 |
| 1071 | AESENC X13, X13 |
| 1072 | AESENC X14, X14 |
| 1073 | AESENC X15, X15 |
| 1074 | |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1075 | AESENC X8, X8 |
| 1076 | AESENC X9, X9 |
| 1077 | AESENC X10, X10 |
| 1078 | AESENC X11, X11 |
| 1079 | AESENC X12, X12 |
| 1080 | AESENC X13, X13 |
| 1081 | AESENC X14, X14 |
| 1082 | AESENC X15, X15 |
| 1083 | |
| 1084 | // combine results |
| 1085 | PXOR X12, X8 |
| 1086 | PXOR X13, X9 |
| 1087 | PXOR X14, X10 |
| 1088 | PXOR X15, X11 |
| 1089 | PXOR X10, X8 |
| 1090 | PXOR X11, X9 |
| 1091 | PXOR X9, X8 |
| 1092 | MOVQ X8, (DX) |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1093 | RET |
| 1094 | |
| 1095 | aes129plus: |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1096 | // make 7 more starting seeds |
| 1097 | MOVO X1, X2 |
| 1098 | MOVO X1, X3 |
| 1099 | MOVO X1, X4 |
| 1100 | MOVO X1, X5 |
| 1101 | MOVO X1, X6 |
| 1102 | MOVO X1, X7 |
| 1103 | PXOR runtime·aeskeysched+16(SB), X1 |
| 1104 | PXOR runtime·aeskeysched+32(SB), X2 |
| 1105 | PXOR runtime·aeskeysched+48(SB), X3 |
| 1106 | PXOR runtime·aeskeysched+64(SB), X4 |
| 1107 | PXOR runtime·aeskeysched+80(SB), X5 |
| 1108 | PXOR runtime·aeskeysched+96(SB), X6 |
| 1109 | PXOR runtime·aeskeysched+112(SB), X7 |
| 1110 | AESENC X1, X1 |
| 1111 | AESENC X2, X2 |
| 1112 | AESENC X3, X3 |
| 1113 | AESENC X4, X4 |
| 1114 | AESENC X5, X5 |
| 1115 | AESENC X6, X6 |
| 1116 | AESENC X7, X7 |
| 1117 | |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1118 | // start with last (possibly overlapping) block |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1119 | MOVOU -128(AX)(CX*1), X8 |
| 1120 | MOVOU -112(AX)(CX*1), X9 |
| 1121 | MOVOU -96(AX)(CX*1), X10 |
| 1122 | MOVOU -80(AX)(CX*1), X11 |
| 1123 | MOVOU -64(AX)(CX*1), X12 |
| 1124 | MOVOU -48(AX)(CX*1), X13 |
| 1125 | MOVOU -32(AX)(CX*1), X14 |
| 1126 | MOVOU -16(AX)(CX*1), X15 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1127 | |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 1128 | // xor in seed |
| 1129 | PXOR X0, X8 |
| 1130 | PXOR X1, X9 |
| 1131 | PXOR X2, X10 |
| 1132 | PXOR X3, X11 |
| 1133 | PXOR X4, X12 |
| 1134 | PXOR X5, X13 |
| 1135 | PXOR X6, X14 |
| 1136 | PXOR X7, X15 |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1137 | |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1138 | // compute number of remaining 128-byte blocks |
| 1139 | DECQ CX |
| 1140 | SHRQ $7, CX |
| 1141 | |
| 1142 | aesloop: |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 1143 | // scramble state |
| 1144 | AESENC X8, X8 |
| 1145 | AESENC X9, X9 |
| 1146 | AESENC X10, X10 |
| 1147 | AESENC X11, X11 |
| 1148 | AESENC X12, X12 |
| 1149 | AESENC X13, X13 |
| 1150 | AESENC X14, X14 |
| 1151 | AESENC X15, X15 |
| 1152 | |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1153 | // scramble state, xor in a block |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1154 | MOVOU (AX), X0 |
| 1155 | MOVOU 16(AX), X1 |
| 1156 | MOVOU 32(AX), X2 |
| 1157 | MOVOU 48(AX), X3 |
| 1158 | AESENC X0, X8 |
| 1159 | AESENC X1, X9 |
| 1160 | AESENC X2, X10 |
| 1161 | AESENC X3, X11 |
| 1162 | MOVOU 64(AX), X4 |
| 1163 | MOVOU 80(AX), X5 |
| 1164 | MOVOU 96(AX), X6 |
| 1165 | MOVOU 112(AX), X7 |
| 1166 | AESENC X4, X12 |
| 1167 | AESENC X5, X13 |
| 1168 | AESENC X6, X14 |
| 1169 | AESENC X7, X15 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1170 | |
Keith Randall | c83e6f5 | 2016-05-26 08:56:49 -0700 | [diff] [blame] | 1171 | ADDQ $128, AX |
| 1172 | DECQ CX |
| 1173 | JNE aesloop |
| 1174 | |
| 1175 | // 3 more scrambles to finish |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1176 | AESENC X8, X8 |
| 1177 | AESENC X9, X9 |
| 1178 | AESENC X10, X10 |
| 1179 | AESENC X11, X11 |
| 1180 | AESENC X12, X12 |
| 1181 | AESENC X13, X13 |
| 1182 | AESENC X14, X14 |
| 1183 | AESENC X15, X15 |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1184 | AESENC X8, X8 |
| 1185 | AESENC X9, X9 |
| 1186 | AESENC X10, X10 |
| 1187 | AESENC X11, X11 |
| 1188 | AESENC X12, X12 |
| 1189 | AESENC X13, X13 |
| 1190 | AESENC X14, X14 |
| 1191 | AESENC X15, X15 |
| 1192 | AESENC X8, X8 |
| 1193 | AESENC X9, X9 |
| 1194 | AESENC X10, X10 |
| 1195 | AESENC X11, X11 |
| 1196 | AESENC X12, X12 |
| 1197 | AESENC X13, X13 |
| 1198 | AESENC X14, X14 |
| 1199 | AESENC X15, X15 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1200 | |
Keith Randall | 91059de | 2015-08-31 16:26:12 -0700 | [diff] [blame] | 1201 | PXOR X12, X8 |
| 1202 | PXOR X13, X9 |
| 1203 | PXOR X14, X10 |
| 1204 | PXOR X15, X11 |
| 1205 | PXOR X10, X8 |
| 1206 | PXOR X11, X9 |
| 1207 | PXOR X9, X8 |
| 1208 | MOVQ X8, (DX) |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1209 | RET |
| 1210 | |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 1211 | TEXT runtime·aeshash32(SB),NOSPLIT,$0-24 |
Keith Randall | a2a9768 | 2014-07-31 15:07:05 -0700 | [diff] [blame] | 1212 | MOVQ p+0(FP), AX // ptr to data |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 1213 | MOVQ h+8(FP), X0 // seed |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 1214 | PINSRD $2, (AX), X0 // data |
Keith Randall | db53d97 | 2013-03-20 14:34:26 -0700 | [diff] [blame] | 1215 | AESENC runtime·aeskeysched+0(SB), X0 |
| 1216 | AESENC runtime·aeskeysched+16(SB), X0 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1217 | AESENC runtime·aeskeysched+32(SB), X0 |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 1218 | MOVQ X0, ret+16(FP) |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 1219 | RET |
| 1220 | |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 1221 | TEXT runtime·aeshash64(SB),NOSPLIT,$0-24 |
Keith Randall | a2a9768 | 2014-07-31 15:07:05 -0700 | [diff] [blame] | 1222 | MOVQ p+0(FP), AX // ptr to data |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 1223 | MOVQ h+8(FP), X0 // seed |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 1224 | PINSRQ $1, (AX), X0 // data |
Keith Randall | db53d97 | 2013-03-20 14:34:26 -0700 | [diff] [blame] | 1225 | AESENC runtime·aeskeysched+0(SB), X0 |
| 1226 | AESENC runtime·aeskeysched+16(SB), X0 |
Keith Randall | 7a4a64e | 2014-12-10 14:20:17 -0800 | [diff] [blame] | 1227 | AESENC runtime·aeskeysched+32(SB), X0 |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 1228 | MOVQ X0, ret+16(FP) |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 1229 | RET |
| 1230 | |
| 1231 | // 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] | 1232 | DATA masks<>+0x00(SB)/8, $0x0000000000000000 |
| 1233 | DATA masks<>+0x08(SB)/8, $0x0000000000000000 |
| 1234 | DATA masks<>+0x10(SB)/8, $0x00000000000000ff |
| 1235 | DATA masks<>+0x18(SB)/8, $0x0000000000000000 |
| 1236 | DATA masks<>+0x20(SB)/8, $0x000000000000ffff |
| 1237 | DATA masks<>+0x28(SB)/8, $0x0000000000000000 |
| 1238 | DATA masks<>+0x30(SB)/8, $0x0000000000ffffff |
| 1239 | DATA masks<>+0x38(SB)/8, $0x0000000000000000 |
| 1240 | DATA masks<>+0x40(SB)/8, $0x00000000ffffffff |
| 1241 | DATA masks<>+0x48(SB)/8, $0x0000000000000000 |
| 1242 | DATA masks<>+0x50(SB)/8, $0x000000ffffffffff |
| 1243 | DATA masks<>+0x58(SB)/8, $0x0000000000000000 |
| 1244 | DATA masks<>+0x60(SB)/8, $0x0000ffffffffffff |
| 1245 | DATA masks<>+0x68(SB)/8, $0x0000000000000000 |
| 1246 | DATA masks<>+0x70(SB)/8, $0x00ffffffffffffff |
| 1247 | DATA masks<>+0x78(SB)/8, $0x0000000000000000 |
| 1248 | DATA masks<>+0x80(SB)/8, $0xffffffffffffffff |
| 1249 | DATA masks<>+0x88(SB)/8, $0x0000000000000000 |
| 1250 | DATA masks<>+0x90(SB)/8, $0xffffffffffffffff |
| 1251 | DATA masks<>+0x98(SB)/8, $0x00000000000000ff |
| 1252 | DATA masks<>+0xa0(SB)/8, $0xffffffffffffffff |
| 1253 | DATA masks<>+0xa8(SB)/8, $0x000000000000ffff |
| 1254 | DATA masks<>+0xb0(SB)/8, $0xffffffffffffffff |
| 1255 | DATA masks<>+0xb8(SB)/8, $0x0000000000ffffff |
| 1256 | DATA masks<>+0xc0(SB)/8, $0xffffffffffffffff |
| 1257 | DATA masks<>+0xc8(SB)/8, $0x00000000ffffffff |
| 1258 | DATA masks<>+0xd0(SB)/8, $0xffffffffffffffff |
| 1259 | DATA masks<>+0xd8(SB)/8, $0x000000ffffffffff |
| 1260 | DATA masks<>+0xe0(SB)/8, $0xffffffffffffffff |
| 1261 | DATA masks<>+0xe8(SB)/8, $0x0000ffffffffffff |
| 1262 | DATA masks<>+0xf0(SB)/8, $0xffffffffffffffff |
| 1263 | DATA masks<>+0xf8(SB)/8, $0x00ffffffffffffff |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 1264 | GLOBL masks<>(SB),RODATA,$256 |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 1265 | |
Shenghou Ma | 3583a44 | 2015-09-03 02:44:26 -0400 | [diff] [blame] | 1266 | TEXT ·checkASM(SB),NOSPLIT,$0-1 |
| 1267 | // check that masks<>(SB) and shifts<>(SB) are aligned to 16-byte |
| 1268 | MOVQ $masks<>(SB), AX |
| 1269 | MOVQ $shifts<>(SB), BX |
| 1270 | ORQ BX, AX |
| 1271 | TESTQ $15, AX |
| 1272 | SETEQ ret+0(FP) |
| 1273 | RET |
| 1274 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 1275 | // these are arguments to pshufb. They move data down from |
Russ Cox | 9ddfb64 | 2013-07-16 16:24:09 -0400 | [diff] [blame] | 1276 | // the high bytes of the register to the low bytes of the register. |
| 1277 | // index is how many bytes to move. |
| 1278 | DATA shifts<>+0x00(SB)/8, $0x0000000000000000 |
| 1279 | DATA shifts<>+0x08(SB)/8, $0x0000000000000000 |
| 1280 | DATA shifts<>+0x10(SB)/8, $0xffffffffffffff0f |
| 1281 | DATA shifts<>+0x18(SB)/8, $0xffffffffffffffff |
| 1282 | DATA shifts<>+0x20(SB)/8, $0xffffffffffff0f0e |
| 1283 | DATA shifts<>+0x28(SB)/8, $0xffffffffffffffff |
| 1284 | DATA shifts<>+0x30(SB)/8, $0xffffffffff0f0e0d |
| 1285 | DATA shifts<>+0x38(SB)/8, $0xffffffffffffffff |
| 1286 | DATA shifts<>+0x40(SB)/8, $0xffffffff0f0e0d0c |
| 1287 | DATA shifts<>+0x48(SB)/8, $0xffffffffffffffff |
| 1288 | DATA shifts<>+0x50(SB)/8, $0xffffff0f0e0d0c0b |
| 1289 | DATA shifts<>+0x58(SB)/8, $0xffffffffffffffff |
| 1290 | DATA shifts<>+0x60(SB)/8, $0xffff0f0e0d0c0b0a |
| 1291 | DATA shifts<>+0x68(SB)/8, $0xffffffffffffffff |
| 1292 | DATA shifts<>+0x70(SB)/8, $0xff0f0e0d0c0b0a09 |
| 1293 | DATA shifts<>+0x78(SB)/8, $0xffffffffffffffff |
| 1294 | DATA shifts<>+0x80(SB)/8, $0x0f0e0d0c0b0a0908 |
| 1295 | DATA shifts<>+0x88(SB)/8, $0xffffffffffffffff |
| 1296 | DATA shifts<>+0x90(SB)/8, $0x0e0d0c0b0a090807 |
| 1297 | DATA shifts<>+0x98(SB)/8, $0xffffffffffffff0f |
| 1298 | DATA shifts<>+0xa0(SB)/8, $0x0d0c0b0a09080706 |
| 1299 | DATA shifts<>+0xa8(SB)/8, $0xffffffffffff0f0e |
| 1300 | DATA shifts<>+0xb0(SB)/8, $0x0c0b0a0908070605 |
| 1301 | DATA shifts<>+0xb8(SB)/8, $0xffffffffff0f0e0d |
| 1302 | DATA shifts<>+0xc0(SB)/8, $0x0b0a090807060504 |
| 1303 | DATA shifts<>+0xc8(SB)/8, $0xffffffff0f0e0d0c |
| 1304 | DATA shifts<>+0xd0(SB)/8, $0x0a09080706050403 |
| 1305 | DATA shifts<>+0xd8(SB)/8, $0xffffff0f0e0d0c0b |
| 1306 | DATA shifts<>+0xe0(SB)/8, $0x0908070605040302 |
| 1307 | DATA shifts<>+0xe8(SB)/8, $0xffff0f0e0d0c0b0a |
| 1308 | DATA shifts<>+0xf0(SB)/8, $0x0807060504030201 |
| 1309 | DATA shifts<>+0xf8(SB)/8, $0xff0f0e0d0c0b0a09 |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 1310 | GLOBL shifts<>(SB),RODATA,$256 |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1311 | |
Keith Randall | bd70bd9 | 2016-02-22 13:20:38 -0800 | [diff] [blame] | 1312 | // memequal(p, q unsafe.Pointer, size uintptr) bool |
| 1313 | TEXT runtime·memequal(SB),NOSPLIT,$0-25 |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 1314 | MOVQ a+0(FP), SI |
| 1315 | MOVQ b+8(FP), DI |
Keith Randall | bd70bd9 | 2016-02-22 13:20:38 -0800 | [diff] [blame] | 1316 | CMPQ SI, DI |
| 1317 | JEQ eq |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 1318 | MOVQ size+16(FP), BX |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1319 | LEAQ ret+24(FP), AX |
| 1320 | JMP runtime·memeqbody(SB) |
Keith Randall | bd70bd9 | 2016-02-22 13:20:38 -0800 | [diff] [blame] | 1321 | eq: |
| 1322 | MOVB $1, ret+24(FP) |
| 1323 | RET |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 1324 | |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 1325 | // memequal_varlen(a, b unsafe.Pointer) bool |
| 1326 | TEXT runtime·memequal_varlen(SB),NOSPLIT,$0-17 |
| 1327 | MOVQ a+0(FP), SI |
| 1328 | MOVQ b+8(FP), DI |
| 1329 | CMPQ SI, DI |
| 1330 | JEQ eq |
| 1331 | MOVQ 8(DX), BX // compiler stores size at offset 8 in the closure |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1332 | LEAQ ret+16(FP), AX |
| 1333 | JMP runtime·memeqbody(SB) |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 1334 | eq: |
| 1335 | MOVB $1, ret+16(FP) |
| 1336 | RET |
| 1337 | |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1338 | // a in SI |
| 1339 | // b in DI |
| 1340 | // count in BX |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1341 | // address of result byte in AX |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 1342 | TEXT runtime·memeqbody(SB),NOSPLIT,$0-0 |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1343 | CMPQ BX, $8 |
| 1344 | JB small |
Ilya Tocar | 967564b | 2015-10-29 17:17:05 +0300 | [diff] [blame] | 1345 | CMPQ BX, $64 |
| 1346 | JB bigloop |
| 1347 | CMPB runtime·support_avx2(SB), $1 |
| 1348 | JE hugeloop_avx2 |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1349 | |
| 1350 | // 64 bytes at a time using xmm registers |
| 1351 | hugeloop: |
| 1352 | CMPQ BX, $64 |
| 1353 | JB bigloop |
| 1354 | MOVOU (SI), X0 |
| 1355 | MOVOU (DI), X1 |
| 1356 | MOVOU 16(SI), X2 |
| 1357 | MOVOU 16(DI), X3 |
| 1358 | MOVOU 32(SI), X4 |
| 1359 | MOVOU 32(DI), X5 |
| 1360 | MOVOU 48(SI), X6 |
| 1361 | MOVOU 48(DI), X7 |
| 1362 | PCMPEQB X1, X0 |
| 1363 | PCMPEQB X3, X2 |
| 1364 | PCMPEQB X5, X4 |
| 1365 | PCMPEQB X7, X6 |
| 1366 | PAND X2, X0 |
| 1367 | PAND X6, X4 |
| 1368 | PAND X4, X0 |
| 1369 | PMOVMSKB X0, DX |
| 1370 | ADDQ $64, SI |
| 1371 | ADDQ $64, DI |
| 1372 | SUBQ $64, BX |
| 1373 | CMPL DX, $0xffff |
| 1374 | JEQ hugeloop |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1375 | MOVB $0, (AX) |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1376 | RET |
| 1377 | |
Ilya Tocar | 967564b | 2015-10-29 17:17:05 +0300 | [diff] [blame] | 1378 | // 64 bytes at a time using ymm registers |
| 1379 | hugeloop_avx2: |
| 1380 | CMPQ BX, $64 |
| 1381 | JB bigloop_avx2 |
Russ Cox | 8d881b8 | 2016-01-22 22:25:15 -0500 | [diff] [blame] | 1382 | VMOVDQU (SI), Y0 |
| 1383 | VMOVDQU (DI), Y1 |
| 1384 | VMOVDQU 32(SI), Y2 |
| 1385 | VMOVDQU 32(DI), Y3 |
| 1386 | VPCMPEQB Y1, Y0, Y4 |
| 1387 | VPCMPEQB Y2, Y3, Y5 |
| 1388 | VPAND Y4, Y5, Y6 |
| 1389 | VPMOVMSKB Y6, DX |
Ilya Tocar | 967564b | 2015-10-29 17:17:05 +0300 | [diff] [blame] | 1390 | ADDQ $64, SI |
| 1391 | ADDQ $64, DI |
| 1392 | SUBQ $64, BX |
| 1393 | CMPL DX, $0xffffffff |
| 1394 | JEQ hugeloop_avx2 |
| 1395 | VZEROUPPER |
| 1396 | MOVB $0, (AX) |
| 1397 | RET |
| 1398 | |
| 1399 | bigloop_avx2: |
| 1400 | VZEROUPPER |
| 1401 | |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1402 | // 8 bytes at a time using 64-bit register |
| 1403 | bigloop: |
| 1404 | CMPQ BX, $8 |
| 1405 | JBE leftover |
| 1406 | MOVQ (SI), CX |
| 1407 | MOVQ (DI), DX |
| 1408 | ADDQ $8, SI |
| 1409 | ADDQ $8, DI |
| 1410 | SUBQ $8, BX |
| 1411 | CMPQ CX, DX |
| 1412 | JEQ bigloop |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1413 | MOVB $0, (AX) |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1414 | RET |
| 1415 | |
| 1416 | // remaining 0-8 bytes |
| 1417 | leftover: |
| 1418 | MOVQ -8(SI)(BX*1), CX |
| 1419 | MOVQ -8(DI)(BX*1), DX |
| 1420 | CMPQ CX, DX |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1421 | SETEQ (AX) |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1422 | RET |
| 1423 | |
| 1424 | small: |
| 1425 | CMPQ BX, $0 |
| 1426 | JEQ equal |
| 1427 | |
| 1428 | LEAQ 0(BX*8), CX |
| 1429 | NEGQ CX |
| 1430 | |
| 1431 | CMPB SI, $0xf8 |
| 1432 | JA si_high |
| 1433 | |
| 1434 | // load at SI won't cross a page boundary. |
| 1435 | MOVQ (SI), SI |
| 1436 | JMP si_finish |
| 1437 | si_high: |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 1438 | // address ends in 11111xxx. Load up to bytes we want, move to correct position. |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1439 | MOVQ -8(SI)(BX*1), SI |
| 1440 | SHRQ CX, SI |
| 1441 | si_finish: |
| 1442 | |
| 1443 | // same for DI. |
| 1444 | CMPB DI, $0xf8 |
| 1445 | JA di_high |
| 1446 | MOVQ (DI), DI |
| 1447 | JMP di_finish |
| 1448 | di_high: |
| 1449 | MOVQ -8(DI)(BX*1), DI |
| 1450 | SHRQ CX, DI |
| 1451 | di_finish: |
| 1452 | |
| 1453 | SUBQ SI, DI |
| 1454 | SHLQ CX, DI |
| 1455 | equal: |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1456 | SETEQ (AX) |
Keith Randall | 3d5daa2 | 2013-04-02 16:26:15 -0700 | [diff] [blame] | 1457 | RET |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1458 | |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 1459 | TEXT runtime·cmpstring(SB),NOSPLIT,$0-40 |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 1460 | MOVQ s1_base+0(FP), SI |
| 1461 | MOVQ s1_len+8(FP), BX |
| 1462 | MOVQ s2_base+16(FP), DI |
| 1463 | MOVQ s2_len+24(FP), DX |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1464 | LEAQ ret+32(FP), R9 |
| 1465 | JMP runtime·cmpbody(SB) |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1466 | |
Russ Cox | 7a524a1 | 2014-12-22 13:27:53 -0500 | [diff] [blame] | 1467 | TEXT bytes·Compare(SB),NOSPLIT,$0-56 |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1468 | MOVQ s1+0(FP), SI |
| 1469 | MOVQ s1+8(FP), BX |
| 1470 | MOVQ s2+24(FP), DI |
| 1471 | MOVQ s2+32(FP), DX |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1472 | LEAQ res+48(FP), R9 |
| 1473 | JMP runtime·cmpbody(SB) |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1474 | |
| 1475 | // input: |
| 1476 | // SI = a |
| 1477 | // DI = b |
| 1478 | // BX = alen |
| 1479 | // DX = blen |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1480 | // R9 = address of output word (stores -1/0/1 here) |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 1481 | TEXT runtime·cmpbody(SB),NOSPLIT,$0-0 |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1482 | CMPQ SI, DI |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1483 | JEQ allsame |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1484 | CMPQ BX, DX |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 1485 | MOVQ DX, R8 |
| 1486 | CMOVQLT BX, R8 // R8 = min(alen, blen) = # of bytes to compare |
| 1487 | CMPQ R8, $8 |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1488 | JB small |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1489 | |
Uttam C Pawar | 32add8d | 2015-07-02 11:43:46 -0700 | [diff] [blame] | 1490 | CMPQ R8, $63 |
Ilya Tocar | 0e23ca4 | 2015-10-28 23:20:26 +0300 | [diff] [blame] | 1491 | JBE loop |
| 1492 | CMPB runtime·support_avx2(SB), $1 |
| 1493 | JEQ big_loop_avx2 |
| 1494 | JMP big_loop |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1495 | loop: |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 1496 | CMPQ R8, $16 |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1497 | JBE _0through16 |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1498 | MOVOU (SI), X0 |
| 1499 | MOVOU (DI), X1 |
| 1500 | PCMPEQB X0, X1 |
| 1501 | PMOVMSKB X1, AX |
| 1502 | XORQ $0xffff, AX // convert EQ to NE |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1503 | JNE diff16 // branch if at least one byte is not equal |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1504 | ADDQ $16, SI |
| 1505 | ADDQ $16, DI |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 1506 | SUBQ $16, R8 |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1507 | JMP loop |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1508 | |
Uttam C Pawar | 32add8d | 2015-07-02 11:43:46 -0700 | [diff] [blame] | 1509 | diff64: |
| 1510 | ADDQ $48, SI |
| 1511 | ADDQ $48, DI |
| 1512 | JMP diff16 |
| 1513 | diff48: |
| 1514 | ADDQ $32, SI |
| 1515 | ADDQ $32, DI |
| 1516 | JMP diff16 |
| 1517 | diff32: |
| 1518 | ADDQ $16, SI |
| 1519 | ADDQ $16, DI |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1520 | // AX = bit mask of differences |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1521 | diff16: |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1522 | BSFQ AX, BX // index of first byte that differs |
| 1523 | XORQ AX, AX |
| 1524 | MOVB (SI)(BX*1), CX |
| 1525 | CMPB CX, (DI)(BX*1) |
| 1526 | SETHI AX |
| 1527 | LEAQ -1(AX*2), AX // convert 1/0 to +1/-1 |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1528 | MOVQ AX, (R9) |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1529 | RET |
| 1530 | |
| 1531 | // 0 through 16 bytes left, alen>=8, blen>=8 |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1532 | _0through16: |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 1533 | CMPQ R8, $8 |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1534 | JBE _0through8 |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1535 | MOVQ (SI), AX |
| 1536 | MOVQ (DI), CX |
| 1537 | CMPQ AX, CX |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1538 | JNE diff8 |
| 1539 | _0through8: |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 1540 | MOVQ -8(SI)(R8*1), AX |
| 1541 | MOVQ -8(DI)(R8*1), CX |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1542 | CMPQ AX, CX |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1543 | JEQ allsame |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1544 | |
| 1545 | // AX and CX contain parts of a and b that differ. |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1546 | diff8: |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1547 | BSWAPQ AX // reverse order of bytes |
| 1548 | BSWAPQ CX |
| 1549 | XORQ AX, CX |
| 1550 | BSRQ CX, CX // index of highest bit difference |
| 1551 | SHRQ CX, AX // move a's bit to bottom |
| 1552 | ANDQ $1, AX // mask bit |
| 1553 | LEAQ -1(AX*2), AX // 1/0 => +1/-1 |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1554 | MOVQ AX, (R9) |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1555 | RET |
| 1556 | |
| 1557 | // 0-7 bytes in common |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1558 | small: |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 1559 | LEAQ (R8*8), CX // bytes left -> bits left |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1560 | NEGQ CX // - bits lift (== 64 - bits left mod 64) |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1561 | JEQ allsame |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1562 | |
| 1563 | // load bytes of a into high bytes of AX |
| 1564 | CMPB SI, $0xf8 |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1565 | JA si_high |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1566 | MOVQ (SI), SI |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1567 | JMP si_finish |
| 1568 | si_high: |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 1569 | MOVQ -8(SI)(R8*1), SI |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1570 | SHRQ CX, SI |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1571 | si_finish: |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1572 | SHLQ CX, SI |
| 1573 | |
| 1574 | // load bytes of b in to high bytes of BX |
| 1575 | CMPB DI, $0xf8 |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1576 | JA di_high |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1577 | MOVQ (DI), DI |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1578 | JMP di_finish |
| 1579 | di_high: |
Austin Clements | 20a6ff7 | 2015-01-27 18:29:02 -0500 | [diff] [blame] | 1580 | MOVQ -8(DI)(R8*1), DI |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1581 | SHRQ CX, DI |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1582 | di_finish: |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1583 | SHLQ CX, DI |
| 1584 | |
| 1585 | BSWAPQ SI // reverse order of bytes |
| 1586 | BSWAPQ DI |
| 1587 | XORQ SI, DI // find bit differences |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1588 | JEQ allsame |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1589 | BSRQ DI, CX // index of highest bit difference |
| 1590 | SHRQ CX, SI // move a's bit to bottom |
| 1591 | ANDQ $1, SI // mask bit |
| 1592 | LEAQ -1(SI*2), AX // 1/0 => +1/-1 |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1593 | MOVQ AX, (R9) |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1594 | RET |
| 1595 | |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 1596 | allsame: |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1597 | XORQ AX, AX |
| 1598 | XORQ CX, CX |
| 1599 | CMPQ BX, DX |
| 1600 | SETGT AX // 1 if alen > blen |
| 1601 | SETEQ CX // 1 if alen == blen |
| 1602 | LEAQ -1(CX)(AX*2), AX // 1,0,-1 result |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1603 | MOVQ AX, (R9) |
Keith Randall | b3946dc | 2013-05-14 16:05:51 -0700 | [diff] [blame] | 1604 | RET |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 1605 | |
Uttam C Pawar | 32add8d | 2015-07-02 11:43:46 -0700 | [diff] [blame] | 1606 | // this works for >= 64 bytes of data. |
| 1607 | big_loop: |
| 1608 | MOVOU (SI), X0 |
| 1609 | MOVOU (DI), X1 |
| 1610 | PCMPEQB X0, X1 |
| 1611 | PMOVMSKB X1, AX |
| 1612 | XORQ $0xffff, AX |
| 1613 | JNE diff16 |
| 1614 | |
| 1615 | MOVOU 16(SI), X0 |
| 1616 | MOVOU 16(DI), X1 |
| 1617 | PCMPEQB X0, X1 |
| 1618 | PMOVMSKB X1, AX |
| 1619 | XORQ $0xffff, AX |
| 1620 | JNE diff32 |
| 1621 | |
| 1622 | MOVOU 32(SI), X0 |
| 1623 | MOVOU 32(DI), X1 |
| 1624 | PCMPEQB X0, X1 |
| 1625 | PMOVMSKB X1, AX |
| 1626 | XORQ $0xffff, AX |
| 1627 | JNE diff48 |
| 1628 | |
| 1629 | MOVOU 48(SI), X0 |
| 1630 | MOVOU 48(DI), X1 |
| 1631 | PCMPEQB X0, X1 |
| 1632 | PMOVMSKB X1, AX |
| 1633 | XORQ $0xffff, AX |
| 1634 | JNE diff64 |
| 1635 | |
| 1636 | ADDQ $64, SI |
| 1637 | ADDQ $64, DI |
| 1638 | SUBQ $64, R8 |
| 1639 | CMPQ R8, $64 |
| 1640 | JBE loop |
| 1641 | JMP big_loop |
| 1642 | |
Ilya Tocar | 0e23ca4 | 2015-10-28 23:20:26 +0300 | [diff] [blame] | 1643 | // Compare 64-bytes per loop iteration. |
| 1644 | // Loop is unrolled and uses AVX2. |
| 1645 | big_loop_avx2: |
Russ Cox | 8d881b8 | 2016-01-22 22:25:15 -0500 | [diff] [blame] | 1646 | VMOVDQU (SI), Y2 |
| 1647 | VMOVDQU (DI), Y3 |
| 1648 | VMOVDQU 32(SI), Y4 |
| 1649 | VMOVDQU 32(DI), Y5 |
| 1650 | VPCMPEQB Y2, Y3, Y0 |
| 1651 | VPMOVMSKB Y0, AX |
Ilya Tocar | 0e23ca4 | 2015-10-28 23:20:26 +0300 | [diff] [blame] | 1652 | XORL $0xffffffff, AX |
| 1653 | JNE diff32_avx2 |
Russ Cox | 8d881b8 | 2016-01-22 22:25:15 -0500 | [diff] [blame] | 1654 | VPCMPEQB Y4, Y5, Y6 |
| 1655 | VPMOVMSKB Y6, AX |
Ilya Tocar | 0e23ca4 | 2015-10-28 23:20:26 +0300 | [diff] [blame] | 1656 | XORL $0xffffffff, AX |
| 1657 | JNE diff64_avx2 |
| 1658 | |
| 1659 | ADDQ $64, SI |
| 1660 | ADDQ $64, DI |
| 1661 | SUBQ $64, R8 |
| 1662 | CMPQ R8, $64 |
| 1663 | JB big_loop_avx2_exit |
| 1664 | JMP big_loop_avx2 |
| 1665 | |
| 1666 | // Avoid AVX->SSE transition penalty and search first 32 bytes of 64 byte chunk. |
| 1667 | diff32_avx2: |
| 1668 | VZEROUPPER |
| 1669 | JMP diff16 |
| 1670 | |
| 1671 | // Same as diff32_avx2, but for last 32 bytes. |
| 1672 | diff64_avx2: |
| 1673 | VZEROUPPER |
| 1674 | JMP diff48 |
| 1675 | |
| 1676 | // For <64 bytes remainder jump to normal loop. |
| 1677 | big_loop_avx2_exit: |
| 1678 | VZEROUPPER |
| 1679 | JMP loop |
| 1680 | |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1681 | TEXT strings·indexShortStr(SB),NOSPLIT,$0-40 |
| 1682 | MOVQ s+0(FP), DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1683 | // We want len in DX and AX, because PCMPESTRI implicitly consumes them |
| 1684 | MOVQ s_len+8(FP), DX |
| 1685 | MOVQ c+16(FP), BP |
| 1686 | MOVQ c_len+24(FP), AX |
Ilya Tocar | 44f1854 | 2016-04-28 17:34:24 +0300 | [diff] [blame] | 1687 | MOVQ DI, R10 |
| 1688 | LEAQ ret+32(FP), R11 |
| 1689 | JMP runtime·indexShortStr(SB) |
| 1690 | |
| 1691 | TEXT bytes·indexShortStr(SB),NOSPLIT,$0-56 |
| 1692 | MOVQ s+0(FP), DI |
| 1693 | MOVQ s_len+8(FP), DX |
| 1694 | MOVQ c+24(FP), BP |
| 1695 | MOVQ c_len+32(FP), AX |
| 1696 | MOVQ DI, R10 |
| 1697 | LEAQ ret+48(FP), R11 |
| 1698 | JMP runtime·indexShortStr(SB) |
| 1699 | |
| 1700 | // AX: length of string, that we are searching for |
| 1701 | // DX: length of string, in which we are searching |
| 1702 | // DI: pointer to string, in which we are searching |
| 1703 | // BP: pointer to string, that we are searching for |
| 1704 | // R11: address, where to put return value |
| 1705 | TEXT runtime·indexShortStr(SB),NOSPLIT,$0 |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1706 | CMPQ AX, DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1707 | JA fail |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1708 | CMPQ DX, $16 |
| 1709 | JAE sse42 |
| 1710 | no_sse42: |
| 1711 | CMPQ AX, $2 |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1712 | JA _3_or_more |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1713 | MOVW (BP), BP |
| 1714 | LEAQ -1(DI)(DX*1), DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1715 | loop2: |
| 1716 | MOVW (DI), SI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1717 | CMPW SI,BP |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1718 | JZ success |
| 1719 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1720 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1721 | JB loop2 |
| 1722 | JMP fail |
| 1723 | _3_or_more: |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1724 | CMPQ AX, $3 |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1725 | JA _4_or_more |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1726 | MOVW 1(BP), BX |
| 1727 | MOVW (BP), BP |
| 1728 | LEAQ -2(DI)(DX*1), DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1729 | loop3: |
| 1730 | MOVW (DI), SI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1731 | CMPW SI,BP |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1732 | JZ partial_success3 |
| 1733 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1734 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1735 | JB loop3 |
| 1736 | JMP fail |
| 1737 | partial_success3: |
| 1738 | MOVW 1(DI), SI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1739 | CMPW SI,BX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1740 | JZ success |
| 1741 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1742 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1743 | JB loop3 |
| 1744 | JMP fail |
| 1745 | _4_or_more: |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1746 | CMPQ AX, $4 |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1747 | JA _5_or_more |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1748 | MOVL (BP), BP |
| 1749 | LEAQ -3(DI)(DX*1), DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1750 | loop4: |
| 1751 | MOVL (DI), SI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1752 | CMPL SI,BP |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1753 | JZ success |
| 1754 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1755 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1756 | JB loop4 |
| 1757 | JMP fail |
| 1758 | _5_or_more: |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1759 | CMPQ AX, $7 |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1760 | JA _8_or_more |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1761 | LEAQ 1(DI)(DX*1), DX |
| 1762 | SUBQ AX, DX |
| 1763 | MOVL -4(BP)(AX*1), BX |
| 1764 | MOVL (BP), BP |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1765 | loop5to7: |
| 1766 | MOVL (DI), SI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1767 | CMPL SI,BP |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1768 | JZ partial_success5to7 |
| 1769 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1770 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1771 | JB loop5to7 |
| 1772 | JMP fail |
| 1773 | partial_success5to7: |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1774 | MOVL -4(AX)(DI*1), SI |
| 1775 | CMPL SI,BX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1776 | JZ success |
| 1777 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1778 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1779 | JB loop5to7 |
| 1780 | JMP fail |
| 1781 | _8_or_more: |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1782 | CMPQ AX, $8 |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1783 | JA _9_or_more |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1784 | MOVQ (BP), BP |
| 1785 | LEAQ -7(DI)(DX*1), DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1786 | loop8: |
| 1787 | MOVQ (DI), SI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1788 | CMPQ SI,BP |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1789 | JZ success |
| 1790 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1791 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1792 | JB loop8 |
| 1793 | JMP fail |
| 1794 | _9_or_more: |
Ilya Tocar | 0cff219 | 2016-04-28 17:39:55 +0300 | [diff] [blame] | 1795 | CMPQ AX, $15 |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1796 | JA _16_or_more |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1797 | LEAQ 1(DI)(DX*1), DX |
| 1798 | SUBQ AX, DX |
| 1799 | MOVQ -8(BP)(AX*1), BX |
| 1800 | MOVQ (BP), BP |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1801 | loop9to15: |
| 1802 | MOVQ (DI), SI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1803 | CMPQ SI,BP |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1804 | JZ partial_success9to15 |
| 1805 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1806 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1807 | JB loop9to15 |
| 1808 | JMP fail |
| 1809 | partial_success9to15: |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1810 | MOVQ -8(AX)(DI*1), SI |
| 1811 | CMPQ SI,BX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1812 | JZ success |
| 1813 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1814 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1815 | JB loop9to15 |
| 1816 | JMP fail |
| 1817 | _16_or_more: |
Ilya Tocar | 429bbf3 | 2016-05-25 16:33:19 +0300 | [diff] [blame] | 1818 | CMPQ AX, $16 |
Ilya Tocar | 0cff219 | 2016-04-28 17:39:55 +0300 | [diff] [blame] | 1819 | JA _17_or_more |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1820 | MOVOU (BP), X1 |
| 1821 | LEAQ -15(DI)(DX*1), DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1822 | loop16: |
| 1823 | MOVOU (DI), X2 |
| 1824 | PCMPEQB X1, X2 |
| 1825 | PMOVMSKB X2, SI |
| 1826 | CMPQ SI, $0xffff |
| 1827 | JE success |
| 1828 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1829 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1830 | JB loop16 |
| 1831 | JMP fail |
Ilya Tocar | 0cff219 | 2016-04-28 17:39:55 +0300 | [diff] [blame] | 1832 | _17_or_more: |
| 1833 | CMPQ AX, $31 |
| 1834 | JA _32_or_more |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1835 | LEAQ 1(DI)(DX*1), DX |
| 1836 | SUBQ AX, DX |
| 1837 | MOVOU -16(BP)(AX*1), X0 |
| 1838 | MOVOU (BP), X1 |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1839 | loop17to31: |
| 1840 | MOVOU (DI), X2 |
| 1841 | PCMPEQB X1,X2 |
| 1842 | PMOVMSKB X2, SI |
| 1843 | CMPQ SI, $0xffff |
| 1844 | JE partial_success17to31 |
| 1845 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1846 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1847 | JB loop17to31 |
| 1848 | JMP fail |
| 1849 | partial_success17to31: |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1850 | MOVOU -16(AX)(DI*1), X3 |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1851 | PCMPEQB X0, X3 |
| 1852 | PMOVMSKB X3, SI |
| 1853 | CMPQ SI, $0xffff |
| 1854 | JE success |
| 1855 | ADDQ $1,DI |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1856 | CMPQ DI,DX |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1857 | JB loop17to31 |
Ilya Tocar | 0cff219 | 2016-04-28 17:39:55 +0300 | [diff] [blame] | 1858 | JMP fail |
| 1859 | // We can get here only when AVX2 is enabled and cutoff for indexShortStr is set to 63 |
| 1860 | // So no need to check cpuid |
| 1861 | _32_or_more: |
| 1862 | CMPQ AX, $32 |
| 1863 | JA _33_to_63 |
| 1864 | VMOVDQU (BP), Y1 |
| 1865 | LEAQ -31(DI)(DX*1), DX |
| 1866 | loop32: |
| 1867 | VMOVDQU (DI), Y2 |
| 1868 | VPCMPEQB Y1, Y2, Y3 |
| 1869 | VPMOVMSKB Y3, SI |
| 1870 | CMPL SI, $0xffffffff |
| 1871 | JE success_avx2 |
| 1872 | ADDQ $1,DI |
| 1873 | CMPQ DI,DX |
| 1874 | JB loop32 |
| 1875 | JMP fail_avx2 |
| 1876 | _33_to_63: |
| 1877 | LEAQ 1(DI)(DX*1), DX |
| 1878 | SUBQ AX, DX |
| 1879 | VMOVDQU -32(BP)(AX*1), Y0 |
| 1880 | VMOVDQU (BP), Y1 |
| 1881 | loop33to63: |
| 1882 | VMOVDQU (DI), Y2 |
| 1883 | VPCMPEQB Y1, Y2, Y3 |
| 1884 | VPMOVMSKB Y3, SI |
| 1885 | CMPL SI, $0xffffffff |
| 1886 | JE partial_success33to63 |
| 1887 | ADDQ $1,DI |
| 1888 | CMPQ DI,DX |
| 1889 | JB loop33to63 |
| 1890 | JMP fail_avx2 |
| 1891 | partial_success33to63: |
| 1892 | VMOVDQU -32(AX)(DI*1), Y3 |
| 1893 | VPCMPEQB Y0, Y3, Y4 |
| 1894 | VPMOVMSKB Y4, SI |
| 1895 | CMPL SI, $0xffffffff |
| 1896 | JE success_avx2 |
| 1897 | ADDQ $1,DI |
| 1898 | CMPQ DI,DX |
| 1899 | JB loop33to63 |
| 1900 | fail_avx2: |
| 1901 | VZEROUPPER |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1902 | fail: |
Ilya Tocar | 44f1854 | 2016-04-28 17:34:24 +0300 | [diff] [blame] | 1903 | MOVQ $-1, (R11) |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1904 | RET |
Ilya Tocar | 0cff219 | 2016-04-28 17:39:55 +0300 | [diff] [blame] | 1905 | success_avx2: |
| 1906 | VZEROUPPER |
| 1907 | JMP success |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1908 | sse42: |
Martin Möhrmann | 5a6c580 | 2017-04-27 08:30:27 +0200 | [diff] [blame] | 1909 | CMPB runtime·support_sse42(SB), $1 |
| 1910 | JNE no_sse42 |
Ilya Tocar | 6b02a19 | 2016-04-21 18:24:12 +0300 | [diff] [blame] | 1911 | CMPQ AX, $12 |
| 1912 | // PCMPESTRI is slower than normal compare, |
| 1913 | // so using it makes sense only if we advance 4+ bytes per compare |
| 1914 | // This value was determined experimentally and is the ~same |
| 1915 | // on Nehalem (first with SSE42) and Haswell. |
| 1916 | JAE _9_or_more |
| 1917 | LEAQ 16(BP), SI |
| 1918 | TESTW $0xff0, SI |
| 1919 | JEQ no_sse42 |
| 1920 | MOVOU (BP), X1 |
| 1921 | LEAQ -15(DI)(DX*1), SI |
| 1922 | MOVQ $16, R9 |
| 1923 | SUBQ AX, R9 // We advance by 16-len(sep) each iteration, so precalculate it into R9 |
| 1924 | loop_sse42: |
| 1925 | // 0x0c means: unsigned byte compare (bits 0,1 are 00) |
| 1926 | // for equality (bits 2,3 are 11) |
| 1927 | // result is not masked or inverted (bits 4,5 are 00) |
| 1928 | // and corresponds to first matching byte (bit 6 is 0) |
| 1929 | PCMPESTRI $0x0c, (DI), X1 |
| 1930 | // CX == 16 means no match, |
| 1931 | // CX > R9 means partial match at the end of the string, |
| 1932 | // otherwise sep is at offset CX from X1 start |
| 1933 | CMPQ CX, R9 |
| 1934 | JBE sse42_success |
| 1935 | ADDQ R9, DI |
| 1936 | CMPQ DI, SI |
| 1937 | JB loop_sse42 |
| 1938 | PCMPESTRI $0x0c, -1(SI), X1 |
| 1939 | CMPQ CX, R9 |
| 1940 | JA fail |
| 1941 | LEAQ -1(SI), DI |
| 1942 | sse42_success: |
| 1943 | ADDQ CX, DI |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1944 | success: |
Ilya Tocar | 44f1854 | 2016-04-28 17:34:24 +0300 | [diff] [blame] | 1945 | SUBQ R10, DI |
| 1946 | MOVQ DI, (R11) |
Ilya Tocar | 95333ae | 2015-10-28 18:05:05 +0300 | [diff] [blame] | 1947 | RET |
| 1948 | |
| 1949 | |
Shenghou Ma | 3b00197 | 2015-03-07 00:18:16 -0500 | [diff] [blame] | 1950 | TEXT bytes·IndexByte(SB),NOSPLIT,$0-40 |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 1951 | MOVQ s+0(FP), SI |
| 1952 | MOVQ s_len+8(FP), BX |
| 1953 | MOVB c+24(FP), AL |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1954 | LEAQ ret+32(FP), R8 |
| 1955 | JMP runtime·indexbytebody(SB) |
Brad Fitzpatrick | 598c789 | 2013-08-05 15:04:05 -0700 | [diff] [blame] | 1956 | |
Shenghou Ma | 3b00197 | 2015-03-07 00:18:16 -0500 | [diff] [blame] | 1957 | TEXT strings·IndexByte(SB),NOSPLIT,$0-32 |
Brad Fitzpatrick | 598c789 | 2013-08-05 15:04:05 -0700 | [diff] [blame] | 1958 | MOVQ s+0(FP), SI |
| 1959 | MOVQ s_len+8(FP), BX |
| 1960 | MOVB c+16(FP), AL |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1961 | LEAQ ret+24(FP), R8 |
| 1962 | JMP runtime·indexbytebody(SB) |
Brad Fitzpatrick | 598c789 | 2013-08-05 15:04:05 -0700 | [diff] [blame] | 1963 | |
| 1964 | // input: |
| 1965 | // SI: data |
| 1966 | // BX: data len |
| 1967 | // AL: byte sought |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 1968 | // R8: address to put result |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 1969 | TEXT runtime·indexbytebody(SB),NOSPLIT,$0 |
Keith Randall | 687abca | 2016-01-15 18:17:09 -0800 | [diff] [blame] | 1970 | // Shuffle X0 around so that each byte contains |
| 1971 | // the character we're looking for. |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 1972 | MOVD AX, X0 |
| 1973 | PUNPCKLBW X0, X0 |
| 1974 | PUNPCKLBW X0, X0 |
| 1975 | PSHUFL $0, X0, X0 |
Keith Randall | 687abca | 2016-01-15 18:17:09 -0800 | [diff] [blame] | 1976 | |
| 1977 | CMPQ BX, $16 |
| 1978 | JLT small |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 1979 | |
Keith Randall | 687abca | 2016-01-15 18:17:09 -0800 | [diff] [blame] | 1980 | MOVQ SI, DI |
| 1981 | |
| 1982 | CMPQ BX, $32 |
| 1983 | JA avx2 |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 1984 | sse: |
Keith Randall | 687abca | 2016-01-15 18:17:09 -0800 | [diff] [blame] | 1985 | LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes |
| 1986 | JMP sseloopentry |
| 1987 | |
| 1988 | sseloop: |
| 1989 | // Move the next 16-byte chunk of the data into X1. |
| 1990 | MOVOU (DI), X1 |
| 1991 | // Compare bytes in X0 to X1. |
| 1992 | PCMPEQB X0, X1 |
| 1993 | // Take the top bit of each byte in X1 and put the result in DX. |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 1994 | PMOVMSKB X1, DX |
Keith Randall | 687abca | 2016-01-15 18:17:09 -0800 | [diff] [blame] | 1995 | // Find first set bit, if any. |
| 1996 | BSFL DX, DX |
| 1997 | JNZ ssesuccess |
| 1998 | // Advance to next block. |
| 1999 | ADDQ $16, DI |
| 2000 | sseloopentry: |
| 2001 | CMPQ DI, AX |
| 2002 | JB sseloop |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 2003 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2004 | // Search the last 16-byte chunk. This chunk may overlap with the |
Keith Randall | 687abca | 2016-01-15 18:17:09 -0800 | [diff] [blame] | 2005 | // chunks we've already searched, but that's ok. |
| 2006 | MOVQ AX, DI |
| 2007 | MOVOU (AX), X1 |
| 2008 | PCMPEQB X0, X1 |
| 2009 | PMOVMSKB X1, DX |
| 2010 | BSFL DX, DX |
| 2011 | JNZ ssesuccess |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 2012 | |
| 2013 | failure: |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 2014 | MOVQ $-1, (R8) |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 2015 | RET |
| 2016 | |
Keith Randall | 687abca | 2016-01-15 18:17:09 -0800 | [diff] [blame] | 2017 | // We've found a chunk containing the byte. |
| 2018 | // The chunk was loaded from DI. |
| 2019 | // The index of the matching byte in the chunk is DX. |
| 2020 | // The start of the data is SI. |
| 2021 | ssesuccess: |
| 2022 | SUBQ SI, DI // Compute offset of chunk within data. |
| 2023 | ADDQ DX, DI // Add offset of byte within chunk. |
| 2024 | MOVQ DI, (R8) |
| 2025 | RET |
| 2026 | |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 2027 | // handle for lengths < 16 |
Russ Cox | b55791e | 2014-10-28 21:50:16 -0400 | [diff] [blame] | 2028 | small: |
Keith Randall | 687abca | 2016-01-15 18:17:09 -0800 | [diff] [blame] | 2029 | TESTQ BX, BX |
| 2030 | JEQ failure |
| 2031 | |
| 2032 | // Check if we'll load across a page boundary. |
| 2033 | LEAQ 16(SI), AX |
| 2034 | TESTW $0xff0, AX |
| 2035 | JEQ endofpage |
| 2036 | |
| 2037 | MOVOU (SI), X1 // Load data |
| 2038 | PCMPEQB X0, X1 // Compare target byte with each byte in data. |
| 2039 | PMOVMSKB X1, DX // Move result bits to integer register. |
| 2040 | BSFL DX, DX // Find first set bit. |
| 2041 | JZ failure // No set bit, failure. |
| 2042 | CMPL DX, BX |
| 2043 | JAE failure // Match is past end of data. |
| 2044 | MOVQ DX, (R8) |
| 2045 | RET |
| 2046 | |
| 2047 | endofpage: |
| 2048 | MOVOU -16(SI)(BX*1), X1 // Load data into the high end of X1. |
| 2049 | PCMPEQB X0, X1 // Compare target byte with each byte in data. |
| 2050 | PMOVMSKB X1, DX // Move result bits to integer register. |
| 2051 | MOVL BX, CX |
| 2052 | SHLL CX, DX |
| 2053 | SHRL $16, DX // Shift desired bits down to bottom of register. |
| 2054 | BSFL DX, DX // Find first set bit. |
| 2055 | JZ failure // No set bit, failure. |
| 2056 | MOVQ DX, (R8) |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 2057 | RET |
| 2058 | |
Ilya Tocar | 321a407 | 2015-10-29 18:52:22 +0300 | [diff] [blame] | 2059 | avx2: |
| 2060 | CMPB runtime·support_avx2(SB), $1 |
Keith Randall | 687abca | 2016-01-15 18:17:09 -0800 | [diff] [blame] | 2061 | JNE sse |
Ilya Tocar | 321a407 | 2015-10-29 18:52:22 +0300 | [diff] [blame] | 2062 | MOVD AX, X0 |
| 2063 | LEAQ -32(SI)(BX*1), R11 |
Russ Cox | 8d881b8 | 2016-01-22 22:25:15 -0500 | [diff] [blame] | 2064 | VPBROADCASTB X0, Y1 |
Ilya Tocar | 321a407 | 2015-10-29 18:52:22 +0300 | [diff] [blame] | 2065 | avx2_loop: |
Russ Cox | 8d881b8 | 2016-01-22 22:25:15 -0500 | [diff] [blame] | 2066 | VMOVDQU (DI), Y2 |
| 2067 | VPCMPEQB Y1, Y2, Y3 |
| 2068 | VPTEST Y3, Y3 |
Ilya Tocar | 321a407 | 2015-10-29 18:52:22 +0300 | [diff] [blame] | 2069 | JNZ avx2success |
| 2070 | ADDQ $32, DI |
| 2071 | CMPQ DI, R11 |
| 2072 | JLT avx2_loop |
| 2073 | MOVQ R11, DI |
Russ Cox | 8d881b8 | 2016-01-22 22:25:15 -0500 | [diff] [blame] | 2074 | VMOVDQU (DI), Y2 |
| 2075 | VPCMPEQB Y1, Y2, Y3 |
| 2076 | VPTEST Y3, Y3 |
Ilya Tocar | 321a407 | 2015-10-29 18:52:22 +0300 | [diff] [blame] | 2077 | JNZ avx2success |
| 2078 | VZEROUPPER |
| 2079 | MOVQ $-1, (R8) |
| 2080 | RET |
| 2081 | |
| 2082 | avx2success: |
Russ Cox | 8d881b8 | 2016-01-22 22:25:15 -0500 | [diff] [blame] | 2083 | VPMOVMSKB Y3, DX |
Ilya Tocar | 321a407 | 2015-10-29 18:52:22 +0300 | [diff] [blame] | 2084 | BSFL DX, DX |
| 2085 | SUBQ SI, DI |
| 2086 | ADDQ DI, DX |
| 2087 | MOVQ DX, (R8) |
| 2088 | VZEROUPPER |
| 2089 | RET |
| 2090 | |
Keith Randall | 5a54696 | 2013-08-07 10:23:24 -0700 | [diff] [blame] | 2091 | TEXT bytes·Equal(SB),NOSPLIT,$0-49 |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 2092 | MOVQ a_len+8(FP), BX |
| 2093 | MOVQ b_len+32(FP), CX |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 2094 | CMPQ BX, CX |
| 2095 | JNE eqret |
| 2096 | MOVQ a+0(FP), SI |
| 2097 | MOVQ b+24(FP), DI |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 2098 | LEAQ ret+48(FP), AX |
| 2099 | JMP runtime·memeqbody(SB) |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 2100 | eqret: |
Keith Randall | c526f3a | 2015-04-21 14:22:41 -0700 | [diff] [blame] | 2101 | MOVB $0, ret+48(FP) |
Brad Fitzpatrick | e2a1bd6 | 2013-08-01 16:11:19 -0700 | [diff] [blame] | 2102 | RET |
Keith Randall | 6c7cbf0 | 2014-04-01 12:51:02 -0700 | [diff] [blame] | 2103 | |
Josselin Costanzi | d206af1 | 2017-03-27 13:22:59 +0200 | [diff] [blame] | 2104 | |
| 2105 | TEXT bytes·countByte(SB),NOSPLIT,$0-40 |
| 2106 | MOVQ s+0(FP), SI |
| 2107 | MOVQ s_len+8(FP), BX |
| 2108 | MOVB c+24(FP), AL |
| 2109 | LEAQ ret+32(FP), R8 |
| 2110 | JMP runtime·countByte(SB) |
| 2111 | |
| 2112 | TEXT strings·countByte(SB),NOSPLIT,$0-32 |
| 2113 | MOVQ s+0(FP), SI |
| 2114 | MOVQ s_len+8(FP), BX |
| 2115 | MOVB c+16(FP), AL |
| 2116 | LEAQ ret+24(FP), R8 |
| 2117 | JMP runtime·countByte(SB) |
| 2118 | |
| 2119 | // input: |
| 2120 | // SI: data |
| 2121 | // BX: data len |
| 2122 | // AL: byte sought |
| 2123 | // R8: address to put result |
| 2124 | // This requires the POPCNT instruction |
| 2125 | TEXT runtime·countByte(SB),NOSPLIT,$0 |
| 2126 | // Shuffle X0 around so that each byte contains |
| 2127 | // the character we're looking for. |
| 2128 | MOVD AX, X0 |
| 2129 | PUNPCKLBW X0, X0 |
| 2130 | PUNPCKLBW X0, X0 |
| 2131 | PSHUFL $0, X0, X0 |
| 2132 | |
| 2133 | CMPQ BX, $16 |
| 2134 | JLT small |
| 2135 | |
| 2136 | MOVQ $0, R12 // Accumulator |
| 2137 | |
| 2138 | MOVQ SI, DI |
| 2139 | |
| 2140 | CMPQ BX, $32 |
| 2141 | JA avx2 |
| 2142 | sse: |
| 2143 | LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes |
| 2144 | JMP sseloopentry |
| 2145 | |
| 2146 | sseloop: |
| 2147 | // Move the next 16-byte chunk of the data into X1. |
| 2148 | MOVOU (DI), X1 |
| 2149 | // Compare bytes in X0 to X1. |
| 2150 | PCMPEQB X0, X1 |
| 2151 | // Take the top bit of each byte in X1 and put the result in DX. |
| 2152 | PMOVMSKB X1, DX |
| 2153 | // Count number of matching bytes |
| 2154 | POPCNTL DX, DX |
| 2155 | // Accumulate into R12 |
| 2156 | ADDQ DX, R12 |
| 2157 | // Advance to next block. |
| 2158 | ADDQ $16, DI |
| 2159 | sseloopentry: |
| 2160 | CMPQ DI, AX |
| 2161 | JBE sseloop |
| 2162 | |
| 2163 | // Get the number of bytes to consider in the last 16 bytes |
| 2164 | ANDQ $15, BX |
| 2165 | JZ end |
| 2166 | |
| 2167 | // Create mask to ignore overlap between previous 16 byte block |
| 2168 | // and the next. |
| 2169 | MOVQ $16,CX |
| 2170 | SUBQ BX, CX |
| 2171 | MOVQ $0xFFFF, R10 |
| 2172 | SARQ CL, R10 |
| 2173 | SALQ CL, R10 |
| 2174 | |
| 2175 | // Process the last 16-byte chunk. This chunk may overlap with the |
| 2176 | // chunks we've already searched so we need to mask part of it. |
| 2177 | MOVOU (AX), X1 |
| 2178 | PCMPEQB X0, X1 |
| 2179 | PMOVMSKB X1, DX |
| 2180 | // Apply mask |
| 2181 | ANDQ R10, DX |
| 2182 | POPCNTL DX, DX |
| 2183 | ADDQ DX, R12 |
| 2184 | end: |
| 2185 | MOVQ R12, (R8) |
| 2186 | RET |
| 2187 | |
| 2188 | // handle for lengths < 16 |
| 2189 | small: |
| 2190 | TESTQ BX, BX |
| 2191 | JEQ endzero |
| 2192 | |
| 2193 | // Check if we'll load across a page boundary. |
| 2194 | LEAQ 16(SI), AX |
| 2195 | TESTW $0xff0, AX |
| 2196 | JEQ endofpage |
| 2197 | |
| 2198 | // We must ignore high bytes as they aren't part of our slice. |
| 2199 | // Create mask. |
| 2200 | MOVB BX, CX |
| 2201 | MOVQ $1, R10 |
| 2202 | SALQ CL, R10 |
| 2203 | SUBQ $1, R10 |
| 2204 | |
| 2205 | // Load data |
| 2206 | MOVOU (SI), X1 |
| 2207 | // Compare target byte with each byte in data. |
| 2208 | PCMPEQB X0, X1 |
| 2209 | // Move result bits to integer register. |
| 2210 | PMOVMSKB X1, DX |
| 2211 | // Apply mask |
| 2212 | ANDQ R10, DX |
| 2213 | POPCNTL DX, DX |
| 2214 | // Directly return DX, we don't need to accumulate |
| 2215 | // since we have <16 bytes. |
| 2216 | MOVQ DX, (R8) |
| 2217 | RET |
| 2218 | endzero: |
| 2219 | MOVQ $0, (R8) |
| 2220 | RET |
| 2221 | |
| 2222 | endofpage: |
| 2223 | // We must ignore low bytes as they aren't part of our slice. |
| 2224 | MOVQ $16,CX |
| 2225 | SUBQ BX, CX |
| 2226 | MOVQ $0xFFFF, R10 |
| 2227 | SARQ CL, R10 |
| 2228 | SALQ CL, R10 |
| 2229 | |
| 2230 | // Load data into the high end of X1. |
| 2231 | MOVOU -16(SI)(BX*1), X1 |
| 2232 | // Compare target byte with each byte in data. |
| 2233 | PCMPEQB X0, X1 |
| 2234 | // Move result bits to integer register. |
| 2235 | PMOVMSKB X1, DX |
| 2236 | // Apply mask |
| 2237 | ANDQ R10, DX |
| 2238 | // Directly return DX, we don't need to accumulate |
| 2239 | // since we have <16 bytes. |
| 2240 | POPCNTL DX, DX |
| 2241 | MOVQ DX, (R8) |
| 2242 | RET |
| 2243 | |
| 2244 | avx2: |
| 2245 | CMPB runtime·support_avx2(SB), $1 |
| 2246 | JNE sse |
| 2247 | MOVD AX, X0 |
| 2248 | LEAQ -32(SI)(BX*1), R11 |
| 2249 | VPBROADCASTB X0, Y1 |
| 2250 | avx2_loop: |
| 2251 | VMOVDQU (DI), Y2 |
| 2252 | VPCMPEQB Y1, Y2, Y3 |
| 2253 | VPMOVMSKB Y3, DX |
| 2254 | POPCNTL DX, DX |
| 2255 | ADDQ DX, R12 |
| 2256 | ADDQ $32, DI |
| 2257 | CMPQ DI, R11 |
| 2258 | JLE avx2_loop |
| 2259 | |
| 2260 | // If last block is already processed, |
| 2261 | // skip to the end. |
| 2262 | CMPQ DI, R11 |
| 2263 | JEQ endavx |
| 2264 | |
| 2265 | // Load address of the last 32 bytes. |
| 2266 | // There is an overlap with the previous block. |
| 2267 | MOVQ R11, DI |
| 2268 | VMOVDQU (DI), Y2 |
| 2269 | VPCMPEQB Y1, Y2, Y3 |
| 2270 | VPMOVMSKB Y3, DX |
| 2271 | // Exit AVX mode. |
| 2272 | VZEROUPPER |
| 2273 | |
| 2274 | // Create mask to ignore overlap between previous 32 byte block |
| 2275 | // and the next. |
| 2276 | ANDQ $31, BX |
| 2277 | MOVQ $32,CX |
| 2278 | SUBQ BX, CX |
| 2279 | MOVQ $0xFFFFFFFF, R10 |
| 2280 | SARQ CL, R10 |
| 2281 | SALQ CL, R10 |
| 2282 | // Apply mask |
| 2283 | ANDQ R10, DX |
| 2284 | POPCNTL DX, DX |
| 2285 | ADDQ DX, R12 |
| 2286 | MOVQ R12, (R8) |
| 2287 | RET |
| 2288 | endavx: |
| 2289 | // Exit AVX mode. |
| 2290 | VZEROUPPER |
| 2291 | MOVQ R12, (R8) |
| 2292 | RET |
| 2293 | |
Keith Randall | f440737 | 2014-09-03 08:49:43 -0700 | [diff] [blame] | 2294 | TEXT runtime·return0(SB), NOSPLIT, $0 |
| 2295 | MOVL $0, AX |
| 2296 | RET |
Keith Randall | 1b6807b | 2014-09-25 07:59:01 -0700 | [diff] [blame] | 2297 | |
| 2298 | |
| 2299 | // Called from cgo wrappers, this function returns g->m->curg.stack.hi. |
| 2300 | // Must obey the gcc calling convention. |
Keith Randall | 1aa65fe | 2014-09-25 08:37:04 -0700 | [diff] [blame] | 2301 | TEXT _cgo_topofstack(SB),NOSPLIT,$0 |
Keith Randall | 1b6807b | 2014-09-25 07:59:01 -0700 | [diff] [blame] | 2302 | get_tls(CX) |
| 2303 | MOVQ g(CX), AX |
| 2304 | MOVQ g_m(AX), AX |
| 2305 | MOVQ m_curg(AX), AX |
| 2306 | MOVQ (g_stack+stack_hi)(AX), AX |
| 2307 | RET |
Russ Cox | a5a0733 | 2014-10-29 20:37:44 -0400 | [diff] [blame] | 2308 | |
| 2309 | // The top-most function running on a goroutine |
| 2310 | // returns to goexit+PCQuantum. |
| 2311 | TEXT runtime·goexit(SB),NOSPLIT,$0-0 |
| 2312 | BYTE $0x90 // NOP |
| 2313 | CALL runtime·goexit1(SB) // does not return |
Dmitry Vyukov | 894024f | 2015-02-20 20:07:02 +0300 | [diff] [blame] | 2314 | // traceback from goexit1 must hit code range of goexit |
| 2315 | BYTE $0x90 // NOP |
Russ Cox | 15ced2d | 2014-11-11 17:06:22 -0500 | [diff] [blame] | 2316 | |
Michael Hudson-Doyle | f616af2 | 2015-04-01 14:17:43 +1300 | [diff] [blame] | 2317 | // This is called from .init_array and follows the platform, not Go, ABI. |
Michael Hudson-Doyle | be0cb92 | 2015-05-12 11:59:14 +1200 | [diff] [blame] | 2318 | TEXT runtime·addmoduledata(SB),NOSPLIT,$0-0 |
| 2319 | PUSHQ R15 // The access to global variables below implicitly uses R15, which is callee-save |
Michael Hudson-Doyle | f616af2 | 2015-04-01 14:17:43 +1300 | [diff] [blame] | 2320 | MOVQ runtime·lastmoduledatap(SB), AX |
| 2321 | MOVQ DI, moduledata_next(AX) |
| 2322 | MOVQ DI, runtime·lastmoduledatap(SB) |
Michael Hudson-Doyle | be0cb92 | 2015-05-12 11:59:14 +1200 | [diff] [blame] | 2323 | POPQ R15 |
Michael Hudson-Doyle | f616af2 | 2015-04-01 14:17:43 +1300 | [diff] [blame] | 2324 | RET |