Commit 8e765da9 authored by Damian Gryski's avatar Damian Gryski Committed by Russ Cox

runtime: add runtime.cputicks() and seed fastrand with it

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
parent 16ce2f93
...@@ -524,6 +524,15 @@ TEXT runtime·getcallersp(SB), 7, $0 ...@@ -524,6 +524,15 @@ TEXT runtime·getcallersp(SB), 7, $0
MOVL sp+0(FP), AX MOVL sp+0(FP), AX
RET 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 TEXT runtime·ldt0setup(SB),7,$16
// set up ldt 7 to point at tls0 // 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. // ldt 1 would be fine on Linux, but on OS X, 7 is as low as we can go.
......
...@@ -568,4 +568,11 @@ TEXT runtime·getcallersp(SB),7,$0 ...@@ -568,4 +568,11 @@ TEXT runtime·getcallersp(SB),7,$0
MOVQ sp+0(FP), AX MOVQ sp+0(FP), AX
RET RET
// int64 runtime·cputicks(void)
TEXT runtime·cputicks(SB),7,$0
RDTSC
SHLQ $32, DX
ADDQ DX, AX
RET
GLOBL runtime·tls0(SB), $64 GLOBL runtime·tls0(SB), $64
...@@ -265,6 +265,16 @@ TEXT runtime·getcallersp(SB),7,$-4 ...@@ -265,6 +265,16 @@ TEXT runtime·getcallersp(SB),7,$-4
TEXT runtime·emptyfunc(SB),0,$0 TEXT runtime·emptyfunc(SB),0,$0
RET 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 TEXT runtime·abort(SB),7,$-4
MOVW $0, R0 MOVW $0, R0
MOVW (R0), R1 MOVW (R0), R1
......
...@@ -332,7 +332,7 @@ mcommoninit(M *m) ...@@ -332,7 +332,7 @@ mcommoninit(M *m)
runtime·atomicstorep(&runtime·allm, m); runtime·atomicstorep(&runtime·allm, m);
m->id = runtime·sched.mcount++; m->id = runtime·sched.mcount++;
m->fastrand = 0x49f6428aUL + m->id; m->fastrand = 0x49f6428aUL + m->id + runtime·cputicks();
m->stackalloc = runtime·malloc(sizeof(*m->stackalloc)); m->stackalloc = runtime·malloc(sizeof(*m->stackalloc));
runtime·FixAlloc_Init(m->stackalloc, FixedStack, runtime·SysAlloc, nil, nil); runtime·FixAlloc_Init(m->stackalloc, FixedStack, runtime·SysAlloc, nil, nil);
......
...@@ -566,6 +566,7 @@ void runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp); ...@@ -566,6 +566,7 @@ void runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp);
void runtime·resetcpuprofiler(int32); void runtime·resetcpuprofiler(int32);
void runtime·setcpuprofilerate(void(*)(uintptr*, int32), int32); void runtime·setcpuprofilerate(void(*)(uintptr*, int32), int32);
void runtime·usleep(uint32); void runtime·usleep(uint32);
int64 runtime·cputicks(void);
#pragma varargck argpos runtime·printf 1 #pragma varargck argpos runtime·printf 1
#pragma varargck type "d" int32 #pragma varargck type "d" int32
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment