Get the page size

This page lists the different ways to get the kernel page size used in the device. To connect to the device, you can use adb.

$ adb shell

Use programming code

In C++, prefer getpagesize(2):

# include <unistd.h>
...
... = getpagesize();

In Java, you can use system config:

import android.system.Os;
import android.system.OsConstants;

... = Os.sysconf(OsConstants._SC_PAGE_SIZE);

Use the getconf command

Use the getconf command to get the page size, shown as follows:

$ getconf PAGE_SIZE
4096

Use the /proc//smaps pseudo file

The KernelPageSize field in the pseudo file /proc/<pid>/smaps shows the page size, shown as follows:

$ grep KernelPageSize /proc/1/smaps
KernelPageSize:        4 kB

Use the LD_SHOW_AUXV=1 linker flag

Use the LD_SHOW_AUXV flag to print the auxiliary vector of the program that is about to be executed. The AT_PAGESZ field contains the page size, shown as follows:

$ LD_SHOW_AUXV=1 ls
AT_SYSINFO_EHDR      0x7250460000
AT_MINSIGSTKSZ       4720
AT_HWCAP             0b11111111111111111111111111111111
AT_PAGESZ            4096
AT_CLKTCK            100
AT_PHDR              0x5fda1e0040
AT_PHENT             56
AT_PHNUM             12
AT_BASE              0x72502f8000
AT_FLAGS             0
AT_ENTRY             0x5fda210000
AT_UID               0
AT_EUID              0
AT_GID               0
AT_EGID              0
AT_SECURE            0
AT_RANDOM            0x7fc59d66f8
AT_HWCAP2            0b100011001111111111
AT_EXECFN            "/system/bin/ls"
AT_PLATFORM          "aarch64"
data            dev.        init    vendor

Use the /proc/config.gz pseudo file

Check the kernel configuration for the page size in the pseudo file /proc/config.gz. The possible configurations for the page size are:

  • CONFIG_ARM64_4K_PAGES=y: the kernel uses 4096-byte pages.
  • CONFIG_ARM64_16K_PAGES=y: the kernel uses 16384-byte pages.
  • CONFIG_ARM64_64K_PAGES=y: the kernel uses 65536-byte pages.
$ zcat /proc/config.gz | grep "CONFIG_ARM64_[164K]*_PAGES=y"
CONFIG_ARM64_16K_PAGES=y

Use the auxiliary vector

When a program is executed, the kernel allocates and initializes the auxiliary vector with information, such as the page size, that is used by the dynamic linker. The auxiliary vector can be read from the pseudo file /proc/<pid>/auxv. The page size from the auxiliary vector of process 1 can be shown as follows:

$ od -N8 -j56 -td8 -An /proc/1/auxv
4096

Where:

  • od dumps files in hexadecimal, decimal or other formats.
  • -N8 -j56 dumps the 8 bytes starting at offset 56 into the file, corresponding to AT_PAGESZ.
  • -td8 formats the value as a decimal 8-byte integer.
  • -An causes just the value to be shown, not its address.

Read from a kernel image

You can read a kernel image's header to determine the page size. This is useful in the bootloader, if you need to know what type of kernel you're booting.

The page size is in the 25th byte of the kernel header:

 $ file Image
Image: Linux kernel ARM64 boot executable Image, little-endian, 16K pages

 $ hexdump -C Image -n 48
00000000  4d 5a 40 fa ff 7f 7b 14  00 00 00 00 00 00 00 00  |MZ@...{.........|
00000010  00 00 64 02 00 00 00 00  0c 00 00 00 00 00 00 00  |..d.............|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030

In this example, the value is 0x0C = 0000 1100b. Bit 1 has the value 0, and bit 2 has the value 1. Reading these two bits, we get 10b or 2 in decimal. Based on the value of these bits, you can determine the kernel page size:

  • 0 - Unspecified
  • 1 - 4 KB
  • 2 - 16 KB
  • 3 - 64 KB

For more information, see the Linux kernel documentation