]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: add runtime.cputicks() and seed fastrand with it
authorDamian Gryski <dgryski@gmail.com>
Thu, 2 Feb 2012 19:09:27 +0000 (14:09 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 2 Feb 2012 19:09:27 +0000 (14:09 -0500)
This patch adds a function to get the current cpu ticks.  This is
deemed to be 'sufficiently random' to use to seed fastrand to mitigate
the algorithmic complexity attacks on the hash table implementation.

On AMD64 we use the RDTSC instruction.  For 386, this instruction,
while valid, is not recognized by 8a so I've inserted the opcode by
hand.  For ARM, this routine is currently stubbed to return a constant
0 value.

Future work: update 8a to recognize RDTSC.

Fixes #2630.

R=rsc
CC=golang-dev
https://golang.org/cl/5606048

src/pkg/runtime/asm_386.s
src/pkg/runtime/asm_amd64.s
src/pkg/runtime/asm_arm.s
src/pkg/runtime/proc.c
src/pkg/runtime/runtime.h

index 52400637be97941289594c60f0457b15344f804f..c242af82964c4d934b968b88ba3f486b6a1ff313 100644 (file)
@@ -524,6 +524,15 @@ TEXT runtime·getcallersp(SB), 7, $0
        MOVL    sp+0(FP), AX
        RET
 
+// int64 runtime·cputicks(void), so really
+// void runtime·cputicks(int64 *ticks)
+TEXT runtime·cputicks(SB),7,$0
+       BYTE    $0x0F; BYTE $0x31;     // RDTSC; not supported by 8a
+       MOVL    ret+0(FP), DI
+       MOVL    AX, 0(DI)
+       MOVL    DX, 4(DI)
+       RET
+
 TEXT runtime·ldt0setup(SB),7,$16
        // set up ldt 7 to point at tls0
        // ldt 1 would be fine on Linux, but on OS X, 7 is as low as we can go.
index 308a66036e9320f0eea66de81ba1d345b10ca5a6..9a660b5b739fa5f24feff5c9c7635fc821628d23 100644 (file)
@@ -568,4 +568,11 @@ TEXT runtime·getcallersp(SB),7,$0
        MOVQ    sp+0(FP), AX
        RET
 
+// int64 runtime·cputicks(void)
+TEXT runtime·cputicks(SB),7,$0
+       RDTSC
+       SHLQ    $32, DX
+       ADDQ    DX, AX
+       RET
+
 GLOBL runtime·tls0(SB), $64
index 42c7677e1e1332ed0f0235a453c550daea24f0c7..58b18626fe729b30e915a7a3d802553f41ecb1d0 100644 (file)
@@ -265,6 +265,16 @@ TEXT runtime·getcallersp(SB),7,$-4
 TEXT runtime·emptyfunc(SB),0,$0
        RET
 
+// int64 runtime·cputicks(), so really
+// void runtime·cputicks(int64 *ticks)
+// stubbed: return int64(0)
+TEXT runtime·cputicks(SB),7,$0
+       MOVW    0(FP), R1
+       MOVW    $0, R0
+       MOVW    R0, 0(R1)
+       MOVW    R0, 4(R1)
+       RET
+
 TEXT runtime·abort(SB),7,$-4
        MOVW    $0, R0
        MOVW    (R0), R1
index cb45a0c2992c2c1cc768ef2c1464b42b83ec6ee7..5bb690aaa8806c5854d10a24770586e0e05510d9 100644 (file)
@@ -332,7 +332,7 @@ mcommoninit(M *m)
        runtime·atomicstorep(&runtime·allm, m);
 
        m->id = runtime·sched.mcount++;
-       m->fastrand = 0x49f6428aUL + m->id;
+       m->fastrand = 0x49f6428aUL + m->id + runtime·cputicks();
        m->stackalloc = runtime·malloc(sizeof(*m->stackalloc));
        runtime·FixAlloc_Init(m->stackalloc, FixedStack, runtime·SysAlloc, nil, nil);
 
index df2cd149f25e59ca906974a2b6c1df5eec73c039..692992150a9dadb269fedbd54d483ec8b7daa44c 100644 (file)
@@ -566,6 +566,7 @@ void        runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp);
 void   runtime·resetcpuprofiler(int32);
 void   runtime·setcpuprofilerate(void(*)(uintptr*, int32), int32);
 void   runtime·usleep(uint32);
+int64  runtime·cputicks(void);
 
 #pragma        varargck        argpos  runtime·printf 1
 #pragma        varargck        type    "d"     int32