From 131454b5606f5f11f6926156504d8ec772484f1d Mon Sep 17 00:00:00 2001 From: Shawn Walker-Salas Date: Thu, 10 Sep 2015 12:29:23 -0700 Subject: [PATCH] unix: fix Getpagesize to return actual system value on Solaris In preparation for issues such as #10180, Getpagesize() needs to be fixed to return the actual system's page size instead of assuming it's always 4096. This is particularly important for future platform support on Solaris. Fixes #12076 Change-Id: I78205165909529215fe93ed6ba56e9c3ee1c2abb Reviewed-on: https://go-review.googlesource.com/14483 Reviewed-by: Brad Fitzpatrick --- unix/syscall_solaris.go | 16 ++++++++++++++++ unix/syscall_solaris_amd64.go | 2 -- unix/types_solaris.go | 4 ++++ unix/zsyscall_solaris_amd64.go | 14 +++++++++++++- unix/ztypes_solaris_amd64.go | 2 ++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/unix/syscall_solaris.go b/unix/syscall_solaris.go index ab54718f..6993cf39 100644 --- a/unix/syscall_solaris.go +++ b/unix/syscall_solaris.go @@ -13,6 +13,7 @@ package unix import ( + "sync/atomic" "syscall" "unsafe" ) @@ -548,3 +549,18 @@ func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, e func Munmap(b []byte) (err error) { return mapper.Munmap(b) } + +//sys sysconf(name int) (n int64, err error) + +// pageSize caches the value of Getpagesize, since it can't change +// once the system is booted. +var pageSize int64 // accessed atomically + +func Getpagesize() int { + n := atomic.LoadInt64(&pageSize) + if n == 0 { + n, _ = sysconf(_SC_PAGESIZE) + atomic.StoreInt64(&pageSize, n) + } + return int(n) +} diff --git a/unix/syscall_solaris_amd64.go b/unix/syscall_solaris_amd64.go index 9c173cd5..2e44630c 100644 --- a/unix/syscall_solaris_amd64.go +++ b/unix/syscall_solaris_amd64.go @@ -6,8 +6,6 @@ package unix -func Getpagesize() int { return 4096 } - func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) } func NsecToTimespec(nsec int64) (ts Timespec) { diff --git a/unix/types_solaris.go b/unix/types_solaris.go index 753c7996..be07c23f 100644 --- a/unix/types_solaris.go +++ b/unix/types_solaris.go @@ -217,6 +217,10 @@ type BpfTimeval C.struct_bpf_timeval type BpfHdr C.struct_bpf_hdr +// sysconf information + +const _SC_PAGESIZE = C._SC_PAGESIZE + // Terminal handling type Termios C.struct_termios diff --git a/unix/zsyscall_solaris_amd64.go b/unix/zsyscall_solaris_amd64.go index 95cb1f65..ea094abe 100644 --- a/unix/zsyscall_solaris_amd64.go +++ b/unix/zsyscall_solaris_amd64.go @@ -91,6 +91,7 @@ import ( //go:cgo_import_dynamic libsocket_setsockopt setsockopt "libsocket.so" //go:cgo_import_dynamic libsocket_recvfrom recvfrom "libsocket.so" //go:cgo_import_dynamic libsocket_recvmsg recvmsg "libsocket.so" +//go:cgo_import_dynamic libc_sysconf sysconf "libc.so" //go:linkname procgetgroups libc_getgroups //go:linkname procsetgroups libc_setgroups @@ -173,6 +174,7 @@ import ( //go:linkname procsetsockopt libsocket_setsockopt //go:linkname procrecvfrom libsocket_recvfrom //go:linkname procrecvmsg libsocket_recvmsg +//go:linkname procsysconf libc_sysconf var ( procgetgroups, @@ -255,7 +257,8 @@ var ( procgetsockname, procsetsockopt, procrecvfrom, - procrecvmsg syscallFunc + procrecvmsg, + procsysconf syscallFunc ) func getgroups(ngid int, gid *_Gid_t) (n int, err error) { @@ -1083,3 +1086,12 @@ func recvmsg(s int, msg *Msghdr, flags int) (n int, err error) { } return } + +func sysconf(name int) (n int64, err error) { + r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procsysconf)), 1, uintptr(name), 0, 0, 0, 0, 0) + n = int64(r0) + if e1 != 0 { + err = e1 + } + return +} diff --git a/unix/ztypes_solaris_amd64.go b/unix/ztypes_solaris_amd64.go index 45e9f422..bb906a9f 100644 --- a/unix/ztypes_solaris_amd64.go +++ b/unix/ztypes_solaris_amd64.go @@ -357,6 +357,8 @@ type BpfHdr struct { Pad_cgo_0 [2]byte } +const _SC_PAGESIZE = 0xb + type Termios struct { Iflag uint32 Oflag uint32