Skip to content

Commit

Permalink
syscall.Getpagesize(): add test, implement for Linux and Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dkegel-fastly authored and aykevl committed Jun 11, 2022
1 parent caf405b commit 8754f64
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 8 deletions.
1 change: 1 addition & 0 deletions builder/musl.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ var Musl = Library{
"internal/libc.c",
"internal/syscall_ret.c",
"internal/vdso.c",
"legacy/*.c",
"malloc/*.c",
"mman/*.c",
"signal/*.c",
Expand Down
17 changes: 17 additions & 0 deletions src/os/getpagesize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build windows || darwin || (linux && !baremetal)
// +build windows darwin linux,!baremetal

package os_test

import (
"os"
"testing"
)

func TestGetpagesize(t *testing.T) {
pagesize := os.Getpagesize()
if pagesize == 0x1000 || pagesize == 0x4000 || pagesize == 0x10000 {
return
}
t.Errorf("os.Getpagesize() returns strange value %d", pagesize)
}
5 changes: 5 additions & 0 deletions src/os/types_anyos.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

package os

import "syscall"

// Getpagesize returns the underlying system's memory page size.
func Getpagesize() int { return syscall.Getpagesize() }

func (fs *fileStat) Name() string { return fs.name }
func (fs *fileStat) IsDir() bool { return fs.Mode().IsDir() }

Expand Down
8 changes: 8 additions & 0 deletions src/runtime/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ func markGlobals() {
start = (start + unsafe.Alignof(uintptr(0)) - 1) &^ (unsafe.Alignof(uintptr(0)) - 1) // align on word boundary
markRoots(start, end)
}

//export getpagesize
func libc_getpagesize() int

//go:linkname syscall_Getpagesize syscall.Getpagesize
func syscall_Getpagesize() int {
return libc_getpagesize()
}
23 changes: 23 additions & 0 deletions src/runtime/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,26 @@ func markGlobals() {
section = (*peSection)(unsafe.Pointer(uintptr(unsafe.Pointer(section)) + unsafe.Sizeof(peSection{})))
}
}

type systeminfo struct {
anon0 [4]byte
dwpagesize uint32
lpminimumapplicationaddress *byte
lpmaximumapplicationaddress *byte
dwactiveprocessormask uintptr
dwnumberofprocessors uint32
dwprocessortype uint32
dwallocationgranularity uint32
wprocessorlevel uint16
wprocessorrevision uint16
}

//export GetSystemInfo
func _GetSystemInfo(lpSystemInfo unsafe.Pointer)

//go:linkname syscall_Getpagesize syscall.Getpagesize
func syscall_Getpagesize() int {
var info systeminfo
_GetSystemInfo(unsafe.Pointer(&info))
return int(info.dwpagesize)
}
8 changes: 0 additions & 8 deletions src/syscall/syscall_libc.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,6 @@ func Mprotect(b []byte, prot int) (err error) {
return
}

func Getpagesize() int {
return int(libc_getpagesize())
}

func Environ() []string {

// This function combines all the environment into a single allocation.
Expand Down Expand Up @@ -372,10 +368,6 @@ func libc_munmap(addr unsafe.Pointer, length uintptr) int32
//export mprotect
func libc_mprotect(addr unsafe.Pointer, len uintptr, prot int32) int32

// int getpagesize();
//export getpagesize
func libc_getpagesize() int32

// int chdir(const char *pathname, mode_t mode);
//export chdir
func libc_chdir(pathname *byte) int32
Expand Down
8 changes: 8 additions & 0 deletions src/syscall/syscall_libc_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ func readdir_r(dir uintptr, entry *Dirent, result **Dirent) (err error) {
return
}

func Getpagesize() int {
return int(libc_getpagesize())
}

// int pipe(int32 *fds);
//export pipe
func libc_pipe(fds *int32) int32

// int getpagesize();
//export getpagesize
func libc_getpagesize() int32
4 changes: 4 additions & 0 deletions src/syscall/syscall_libc_nintendoswitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ func getErrno() error {
func Pipe2(p []int, flags int) (err error) {
return ENOSYS // TODO
}

func Getpagesize() int {
return 4096 // TODO
}
5 changes: 5 additions & 0 deletions src/syscall/syscall_libc_wasi.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ func Pipe2(p []int, flags int) (err error) {
return ENOSYS // TODO
}

func Getpagesize() int {
// per upstream
return 65536
}

// int stat(const char *path, struct stat * buf);
//export stat
func libc_stat(pathname *byte, ptr unsafe.Pointer) int32
Expand Down
6 changes: 6 additions & 0 deletions src/syscall/syscall_nonhosted.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,9 @@ type Timeval struct {
Sec int64
Usec int64
}

func Getpagesize() int {
// There is no right value to return here, but 4096 is a
// common assumption when pagesize is unknown
return 4096
}

0 comments on commit 8754f64

Please sign in to comment.