Skip to content

Commit 8754f64

Browse files
dkegel-fastlyaykevl
authored andcommitted
syscall.Getpagesize(): add test, implement for Linux and Windows
1 parent caf405b commit 8754f64

10 files changed

+77
-8
lines changed

builder/musl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ var Musl = Library{
115115
"internal/libc.c",
116116
"internal/syscall_ret.c",
117117
"internal/vdso.c",
118+
"legacy/*.c",
118119
"malloc/*.c",
119120
"mman/*.c",
120121
"signal/*.c",

src/os/getpagesize_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build windows || darwin || (linux && !baremetal)
2+
// +build windows darwin linux,!baremetal
3+
4+
package os_test
5+
6+
import (
7+
"os"
8+
"testing"
9+
)
10+
11+
func TestGetpagesize(t *testing.T) {
12+
pagesize := os.Getpagesize()
13+
if pagesize == 0x1000 || pagesize == 0x4000 || pagesize == 0x10000 {
14+
return
15+
}
16+
t.Errorf("os.Getpagesize() returns strange value %d", pagesize)
17+
}

src/os/types_anyos.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
package os
99

10+
import "syscall"
11+
12+
// Getpagesize returns the underlying system's memory page size.
13+
func Getpagesize() int { return syscall.Getpagesize() }
14+
1015
func (fs *fileStat) Name() string { return fs.name }
1116
func (fs *fileStat) IsDir() bool { return fs.Mode().IsDir() }
1217

src/runtime/os_linux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,11 @@ func markGlobals() {
4040
start = (start + unsafe.Alignof(uintptr(0)) - 1) &^ (unsafe.Alignof(uintptr(0)) - 1) // align on word boundary
4141
markRoots(start, end)
4242
}
43+
44+
//export getpagesize
45+
func libc_getpagesize() int
46+
47+
//go:linkname syscall_Getpagesize syscall.Getpagesize
48+
func syscall_Getpagesize() int {
49+
return libc_getpagesize()
50+
}

src/runtime/os_windows.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,26 @@ func markGlobals() {
9090
section = (*peSection)(unsafe.Pointer(uintptr(unsafe.Pointer(section)) + unsafe.Sizeof(peSection{})))
9191
}
9292
}
93+
94+
type systeminfo struct {
95+
anon0 [4]byte
96+
dwpagesize uint32
97+
lpminimumapplicationaddress *byte
98+
lpmaximumapplicationaddress *byte
99+
dwactiveprocessormask uintptr
100+
dwnumberofprocessors uint32
101+
dwprocessortype uint32
102+
dwallocationgranularity uint32
103+
wprocessorlevel uint16
104+
wprocessorrevision uint16
105+
}
106+
107+
//export GetSystemInfo
108+
func _GetSystemInfo(lpSystemInfo unsafe.Pointer)
109+
110+
//go:linkname syscall_Getpagesize syscall.Getpagesize
111+
func syscall_Getpagesize() int {
112+
var info systeminfo
113+
_GetSystemInfo(unsafe.Pointer(&info))
114+
return int(info.dwpagesize)
115+
}

src/syscall/syscall_libc.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,6 @@ func Mprotect(b []byte, prot int) (err error) {
251251
return
252252
}
253253

254-
func Getpagesize() int {
255-
return int(libc_getpagesize())
256-
}
257-
258254
func Environ() []string {
259255

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

375-
// int getpagesize();
376-
//export getpagesize
377-
func libc_getpagesize() int32
378-
379371
// int chdir(const char *pathname, mode_t mode);
380372
//export chdir
381373
func libc_chdir(pathname *byte) int32

src/syscall/syscall_libc_darwin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ func readdir_r(dir uintptr, entry *Dirent, result **Dirent) (err error) {
267267
return
268268
}
269269

270+
func Getpagesize() int {
271+
return int(libc_getpagesize())
272+
}
273+
270274
// int pipe(int32 *fds);
271275
//export pipe
272276
func libc_pipe(fds *int32) int32
277+
278+
// int getpagesize();
279+
//export getpagesize
280+
func libc_getpagesize() int32

src/syscall/syscall_libc_nintendoswitch.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ func getErrno() error {
6767
func Pipe2(p []int, flags int) (err error) {
6868
return ENOSYS // TODO
6969
}
70+
71+
func Getpagesize() int {
72+
return 4096 // TODO
73+
}

src/syscall/syscall_libc_wasi.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ func Pipe2(p []int, flags int) (err error) {
296296
return ENOSYS // TODO
297297
}
298298

299+
func Getpagesize() int {
300+
// per upstream
301+
return 65536
302+
}
303+
299304
// int stat(const char *path, struct stat * buf);
300305
//export stat
301306
func libc_stat(pathname *byte, ptr unsafe.Pointer) int32

src/syscall/syscall_nonhosted.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,9 @@ type Timeval struct {
205205
Sec int64
206206
Usec int64
207207
}
208+
209+
func Getpagesize() int {
210+
// There is no right value to return here, but 4096 is a
211+
// common assumption when pagesize is unknown
212+
return 4096
213+
}

0 commit comments

Comments
 (0)