Skip to content

Commit

Permalink
Add rdtsc/cpuid
Browse files Browse the repository at this point in the history
  • Loading branch information
vizee committed Feb 4, 2017
1 parent 7bb5bc1 commit 9a7c06e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
8 changes: 8 additions & 0 deletions asm_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ func Movoa(from *uint64, to *uint64)

//go:noescape
func Movou(from *uint64, to *uint64)

// misc

func CPUID(ax uint32, cx uint32) (a uint32, b uint32, d uint32, c uint32)

func Rdtsc() int64

func Rdtscp() (tsc int64, cpuid uint32)
50 changes: 38 additions & 12 deletions asm_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,16 @@ TEXT ·LockCmpxchg8(SB), NOSPLIT, $0-17
SETEQ ok+16(FP)
RET

#define LOCK_CMPXCHG16B_DI_M BYTE $0xf0; BYTE $0x48; BYTE $0x0f; BYTE $0xc7; BYTE $0x0f

// func LockCmpxchg16B(addr *uint128, cmplo uint64, cmphi uint64, vallo uint64, valhi uint64) (ok bool)
TEXT ·LockCmpxchg16B(SB), NOSPLIT, $0-41
MOVQ addr+0(FP), DI
MOVQ cmplo+8(FP), AX
MOVQ cmphi+16(FP), DX
MOVQ vallo+24(FP), BX
MOVQ valhi+32(FP), CX

// LOCK; CMPXCHG16B (DI)
BYTE $0xf0
BYTE $0x48
BYTE $0x0f
BYTE $0xc7
BYTE $0x0f
MOVQ addr+0(FP), DI
MOVQ cmplo+8(FP), AX
MOVQ cmphi+16(FP), DX
MOVQ vallo+24(FP), BX
MOVQ valhi+32(FP), CX
LOCK_CMPXCHG16B_DI_M
SETEQ ok+40(FP)
RET

Expand All @@ -77,3 +73,33 @@ TEXT ·Movou(SB), NOSPLIT, $0-16
MOVOU X0, (AX)
RET

// func CPUID(ax uint32, cx uint32) (a uint32, b uint32, d uint32, c uint32)
TEXT ·CPUID(SB), NOSPLIT, $0-24
MOVL ax+0(FP), AX
MOVL cx+4(FP), CX
CPUID
MOVL AX, a+8(FP)
MOVL BX, b+12(FP)
MOVL DX, d+16(FP)
MOVL CX, c+20(FP)
RET

// func Rdtsc() int64
TEXT ·Rdtsc(SB), NOSPLIT, $0-8
LFENCE
RDTSC
SHLQ $32, DX
ORQ AX, DX
MOVQ DX, ret+0(FP)
RET

#define RDTSCP BYTE $0x0f; BYTE $0x01; BYTE $0xf9

// func Rdtscp() (tsc int64, cpuid uint32)
TEXT ·Rdtscp(SB), NOSPLIT, $0-12
RDTSCP
SHLQ $32, DX
ORQ AX, DX
MOVQ DX, tsc+0(FP)
MOVL CX, cpuid+8(FP)
RET
19 changes: 19 additions & 0 deletions asm_amd64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package asm

import (
"testing"
"unsafe"
)

func TestStos8(t *testing.T) {
Expand Down Expand Up @@ -55,3 +56,21 @@ func TestMovou(t *testing.T) {
t.Log("v", v)
t.Log("v2", v2)
}

func TestCPUID(t *testing.T) {
var s [4]uint32
s[0], s[1], s[2], s[3] = CPUID(0, 0)

var magic [2]uintptr
magic[0] = uintptr(unsafe.Pointer(&s[1]))
magic[1] = 12
t.Log(s[0], *(*string)(unsafe.Pointer(&magic)))
}

func TestRdtsc(t *testing.T) {
t.Log(Rdtsc())
}

func TestRdtscp(t *testing.T) {
t.Log(Rdtscp())
}

0 comments on commit 9a7c06e

Please sign in to comment.