From c955bb2040e601c474e547b8badbe44677c9fbdf Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 14 Apr 2016 12:12:45 -0400 Subject: runtime: common auxv parser Currently several different Linux architectures have separate copies of the auxv parser. Bring these all together into a single copy of the parser that calls out to a per-arch handler for each tag/value pair. This is in preparation for handling common auxv tags in one place. For #9993. Change-Id: Iceebc3afad6b4133b70fca7003561ae370445c10 Reviewed-on: https://go-review.googlesource.com/22061 Run-TryBot: Brad Fitzpatrick Reviewed-by: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Michael Hudson-Doyle --- src/runtime/os_linux_arm.go | 48 ++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) (limited to 'src/runtime/os_linux_arm.go') diff --git a/src/runtime/os_linux_arm.go b/src/runtime/os_linux_arm.go index 8fdfb585ba..a61be916b6 100644 --- a/src/runtime/os_linux_arm.go +++ b/src/runtime/os_linux_arm.go @@ -4,13 +4,9 @@ package runtime -import ( - "runtime/internal/sys" - "unsafe" -) +import "unsafe" const ( - _AT_NULL = 0 _AT_PLATFORM = 15 // introduced in at least 2.6.11 _AT_HWCAP = 16 // introduced in at least 2.6.11 _AT_RANDOM = 25 // introduced in 2.6.29 @@ -36,33 +32,23 @@ func checkgoarm() { } } -func sysargs(argc int32, argv **byte) { - // skip over argv, envv to get to auxv - n := argc + 1 - for argv_index(argv, n) != nil { - n++ - } - n++ - auxv := (*[1 << 28]uint32)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize)) - - for i := 0; auxv[i] != _AT_NULL; i += 2 { - switch auxv[i] { - case _AT_RANDOM: // kernel provides a pointer to 16-bytes worth of random data - startupRandomData = (*[16]byte)(unsafe.Pointer(uintptr(auxv[i+1])))[:] - // the pointer provided may not be word aligned, so we must treat it - // as a byte array. - randomNumber = uint32(startupRandomData[4]) | uint32(startupRandomData[5])<<8 | - uint32(startupRandomData[6])<<16 | uint32(startupRandomData[7])<<24 - - case _AT_PLATFORM: // v5l, v6l, v7l - t := *(*uint8)(unsafe.Pointer(uintptr(auxv[i+1] + 1))) - if '5' <= t && t <= '7' { - armArch = t - '0' - } - - case _AT_HWCAP: // CPU capability bit flags - hwcap = auxv[i+1] +func archauxv(tag, val uintptr) { + switch tag { + case _AT_RANDOM: // kernel provides a pointer to 16-bytes worth of random data + startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:] + // the pointer provided may not be word aligned, so we must treat it + // as a byte array. + randomNumber = uint32(startupRandomData[4]) | uint32(startupRandomData[5])<<8 | + uint32(startupRandomData[6])<<16 | uint32(startupRandomData[7])<<24 + + case _AT_PLATFORM: // v5l, v6l, v7l + t := *(*uint8)(unsafe.Pointer(val + 1)) + if '5' <= t && t <= '7' { + armArch = t - '0' } + + case _AT_HWCAP: // CPU capability bit flags + hwcap = uint32(val) } } -- cgit v1.3-5-g9baa From 90addd3d41852192ba697d33c9b1660988b82ed7 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 14 Apr 2016 12:32:28 -0400 Subject: runtime: common handling of _AT_RANDOM auxv The Linux kernel provides 16 bytes of random data via the auxv vector at startup. Currently we consume this separately on 386, amd64, arm, and arm64. Now that we have a common auxv parser, handle _AT_RANDOM in the common path. Change-Id: Ib69549a1d37e2d07a351cf0f44007bcd24f0d20d Reviewed-on: https://go-review.googlesource.com/22062 Reviewed-by: Brad Fitzpatrick --- src/runtime/os_linux.go | 9 ++++++++- src/runtime/os_linux_386.go | 7 ------- src/runtime/os_linux_arm.go | 9 ++++----- src/runtime/os_linux_arm64.go | 14 ++++---------- src/runtime/vdso_linux_amd64.go | 4 ---- 5 files changed, 16 insertions(+), 27 deletions(-) (limited to 'src/runtime/os_linux_arm.go') diff --git a/src/runtime/os_linux.go b/src/runtime/os_linux.go index eeb30c7dd9..4645f1c33d 100644 --- a/src/runtime/os_linux.go +++ b/src/runtime/os_linux.go @@ -177,7 +177,8 @@ var failallocatestack = []byte("runtime: failed to allocate stack for the new OS var failthreadcreate = []byte("runtime: failed to create new OS thread\n") const ( - _AT_NULL = 0 // End of vector + _AT_NULL = 0 // End of vector + _AT_RANDOM = 25 // introduced in 2.6.29 ) func sysargs(argc int32, argv **byte) { @@ -195,6 +196,12 @@ func sysargs(argc int32, argv **byte) { auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize)) for i := 0; auxv[i] != _AT_NULL; i += 2 { tag, val := auxv[i], auxv[i+1] + switch tag { + case _AT_RANDOM: + // The kernel provides a pointer to 16-bytes + // worth of random data. + startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:] + } archauxv(tag, val) } } diff --git a/src/runtime/os_linux_386.go b/src/runtime/os_linux_386.go index 2383d962b2..cf031afe45 100644 --- a/src/runtime/os_linux_386.go +++ b/src/runtime/os_linux_386.go @@ -4,16 +4,9 @@ package runtime -import "unsafe" - const ( - _AT_RANDOM = 25 _AT_SYSINFO = 32 ) func archauxv(tag, val uintptr) { - switch tag { - case _AT_RANDOM: - startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:] - } } diff --git a/src/runtime/os_linux_arm.go b/src/runtime/os_linux_arm.go index a61be916b6..8e2765a413 100644 --- a/src/runtime/os_linux_arm.go +++ b/src/runtime/os_linux_arm.go @@ -9,7 +9,6 @@ import "unsafe" const ( _AT_PLATFORM = 15 // introduced in at least 2.6.11 _AT_HWCAP = 16 // introduced in at least 2.6.11 - _AT_RANDOM = 25 // introduced in 2.6.29 _HWCAP_VFP = 1 << 6 // introduced in at least 2.6.11 _HWCAP_VFPv3 = 1 << 13 // introduced in 2.6.30 @@ -34,10 +33,10 @@ func checkgoarm() { func archauxv(tag, val uintptr) { switch tag { - case _AT_RANDOM: // kernel provides a pointer to 16-bytes worth of random data - startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:] - // the pointer provided may not be word aligned, so we must treat it - // as a byte array. + case _AT_RANDOM: + // sysargs filled in startupRandomData, but that + // pointer may not be word aligned, so we must treat + // it as a byte array. randomNumber = uint32(startupRandomData[4]) | uint32(startupRandomData[5])<<8 | uint32(startupRandomData[6])<<16 | uint32(startupRandomData[7])<<24 diff --git a/src/runtime/os_linux_arm64.go b/src/runtime/os_linux_arm64.go index aa9d4d9885..43262aea14 100644 --- a/src/runtime/os_linux_arm64.go +++ b/src/runtime/os_linux_arm64.go @@ -4,20 +4,14 @@ package runtime -import "unsafe" - -const ( - _AT_RANDOM = 25 // introduced in 2.6.29 -) - var randomNumber uint32 func archauxv(tag, val uintptr) { switch tag { - case _AT_RANDOM: // kernel provides a pointer to 16-bytes worth of random data - startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:] - // the pointer provided may not be word aligned, so we must treat it - // as a byte array. + case _AT_RANDOM: + // sysargs filled in startupRandomData, but that + // pointer may not be word aligned, so we must treat + // it as a byte array. randomNumber = uint32(startupRandomData[4]) | uint32(startupRandomData[5])<<8 | uint32(startupRandomData[6])<<16 | uint32(startupRandomData[7])<<24 } diff --git a/src/runtime/vdso_linux_amd64.go b/src/runtime/vdso_linux_amd64.go index 1aae9b6570..8a970dfbe6 100644 --- a/src/runtime/vdso_linux_amd64.go +++ b/src/runtime/vdso_linux_amd64.go @@ -18,7 +18,6 @@ import "unsafe" // http://refspecs.linuxfoundation.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/symversion.html const ( - _AT_RANDOM = 25 _AT_SYSINFO_EHDR = 33 _PT_LOAD = 1 /* Loadable program segment */ @@ -303,8 +302,5 @@ func archauxv(tag, val uintptr) { info1 := (*vdso_info)(noescape(unsafe.Pointer(&info))) vdso_init_from_sysinfo_ehdr(info1, (*elf64Ehdr)(unsafe.Pointer(val))) vdso_parse_symbols(info1, vdso_find_version(info1, &linux26)) - - case _AT_RANDOM: - startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:] } } -- cgit v1.3-5-g9baa