runtime: use mincore to detect physical page size as last resort on Android

Fixes #18041.

Change-Id: Iad1439b2dd56b113c8829699eda467d1367b0e15
Reviewed-on: https://go-review.googlesource.com/34610
Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
Shenghou Ma 2016-12-19 09:48:07 -05:00 committed by Minux Ma
parent ddd558e7e4
commit a0667be8ef

View file

@ -208,6 +208,26 @@ func sysargs(argc int32, argv **byte) {
// Fall back to /proc/self/auxv.
fd := open(&procAuxv[0], 0 /* O_RDONLY */, 0)
if fd < 0 {
// On Android, /proc/self/auxv might be unreadable (issue 9229), so we fallback to
// try using mincore to detect the physical page size.
// mincore should return EINVAL when address is not a multiple of system page size.
const size = 256 << 10 // size of memory region to allocate
p := mmap(nil, size, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
if uintptr(p) < 4096 {
return
}
var n uintptr
for n = 4 << 10; n < size; n <<= 1 {
err := mincore(unsafe.Pointer(uintptr(p)+n), 1, &addrspace_vec[0])
if err == 0 {
physPageSize = n
break
}
}
if physPageSize == 0 {
physPageSize = size
}
munmap(p, size)
return
}
var buf [128]uintptr