Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Created June 28, 2022 20:25
Show Gist options
  • Save ammarfaizi2/5fba03443e9949a5d8b1246abe180e34 to your computer and use it in GitHub Desktop.
Save ammarfaizi2/5fba03443e9949a5d8b1246abe180e34 to your computer and use it in GitHub Desktop.
static inline long get_page_size(void)
{
/*
* aarch64 supports three values of page size: 4K, 16K
* and 64K which are selected at kernel compilation time.
* Therefore, we can't hard code the page size for this
* arch. Use the mmap() syscall return value to find the
* page size.
*
* Start the hinting the kernel with a multiple of 4K.
* If the page size is greater than 4K, the kernel will
* round it down to the nearest address that is multiple
* of page size. When that happens, our "if" checks below
* will catch the right page size.
*/
void *hint = (void *)0x444444441000;
long page_size;
void *p;
long x;
p = __sys_mmap(hint, 1, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (IS_ERR(p))
return PTR_ERR(p);
x = (long) p;
page_size = 64 * 1024;
if ((x % page_size) == 0)
goto out;
page_size = 16 * 1024;
if ((x % page_size) == 0)
goto out;
page_size = 4 * 1024;
if ((x % page_size) == 0)
goto out;
/*
* Something goes wrong?
*/
page_size = -ENOTSUP;
out:
__sys_munmap(p, 1);
return page_size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment