1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2024 Google LLC. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author(s): Kalesh Singh <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker #ifndef PGSIZE_HELPER_H
8*49cdfc7eSAndroid Build Coastguard Worker #define PGSIZE_HELPER_H
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #define MAX_PAGE_SIZE (64*1024)
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #if defined(__x86_64__)
17*49cdfc7eSAndroid Build Coastguard Worker /*
18*49cdfc7eSAndroid Build Coastguard Worker * Android emulates the userspace page size on some x86_64 emulators.
19*49cdfc7eSAndroid Build Coastguard Worker * The kernel page size still remains 4KiB (0x1000).
20*49cdfc7eSAndroid Build Coastguard Worker */
kernel_page_size(void)21*49cdfc7eSAndroid Build Coastguard Worker static inline size_t kernel_page_size(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker return 0x1000;
24*49cdfc7eSAndroid Build Coastguard Worker }
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker /* Handle upto MAX_PAGE_SIZE emulation on 4KiB kernel base page size system */
27*49cdfc7eSAndroid Build Coastguard Worker #define DECLARE_MINCORE_VECTOR(vec_name, num_pages) \
28*49cdfc7eSAndroid Build Coastguard Worker unsigned char vec_name[(num_pages) * (MAX_PAGE_SIZE / 4096)]
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker #else /* !defined(__x86_64__) */
31*49cdfc7eSAndroid Build Coastguard Worker
kernel_page_size(void)32*49cdfc7eSAndroid Build Coastguard Worker static inline size_t kernel_page_size(void)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker return getpagesize();
35*49cdfc7eSAndroid Build Coastguard Worker }
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #define DECLARE_MINCORE_VECTOR(vec_name, num_pages) unsigned char vec_name[(num_pages)]
38*49cdfc7eSAndroid Build Coastguard Worker #endif /* defined(__x86_64__) */
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker /*
41*49cdfc7eSAndroid Build Coastguard Worker * NOTE: For all cases except Android x86_64 page size emulators,
42*49cdfc7eSAndroid Build Coastguard Worker * kernel_page_size == page_size, and the below macros are effectively
43*49cdfc7eSAndroid Build Coastguard Worker * no-ops.
44*49cdfc7eSAndroid Build Coastguard Worker */
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker /* The number of kernel pages covered by size */
nr_kernel_pages(size_t size)47*49cdfc7eSAndroid Build Coastguard Worker static inline size_t nr_kernel_pages(size_t size)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker return size / kernel_page_size();
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker /* The number of kernel pages in a @nr_pages userspace pages */
nr_pgs_to_nr_kernel_pgs(size_t nr_pages)53*49cdfc7eSAndroid Build Coastguard Worker static size_t nr_pgs_to_nr_kernel_pgs(size_t nr_pages)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker return nr_pages * nr_kernel_pages(getpagesize());
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker /* The number of userspace pages in a @nr_pages kernel pages */
nr_kernel_pgs_to_nr_pgs(size_t nr_pages)59*49cdfc7eSAndroid Build Coastguard Worker static size_t nr_kernel_pgs_to_nr_pgs(size_t nr_pages)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker return nr_pages / nr_kernel_pages(getpagesize());
62*49cdfc7eSAndroid Build Coastguard Worker }
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker /*
65*49cdfc7eSAndroid Build Coastguard Worker * Test populating the last partial 4KiB page if the page size is emulated,
66*49cdfc7eSAndroid Build Coastguard Worker * instead of the first. This faults in the preceding pages for convenient
67*49cdfc7eSAndroid Build Coastguard Worker * test accounting.
68*49cdfc7eSAndroid Build Coastguard Worker */
69*49cdfc7eSAndroid Build Coastguard Worker #define MLOCK_PAGE_SIZE_EMULATION_OFFSET(tcases) \
70*49cdfc7eSAndroid Build Coastguard Worker do { \
71*49cdfc7eSAndroid Build Coastguard Worker if (getpagesize() != kernel_page_size()) { \
72*49cdfc7eSAndroid Build Coastguard Worker for (int i = 0; i < ARRAY_SIZE(tcases); i++) { \
73*49cdfc7eSAndroid Build Coastguard Worker struct tcase *test = tcases + i; \
74*49cdfc7eSAndroid Build Coastguard Worker if (test->offset > 0) test->offset = pgsz - kernel_page_size() + test->offset; \
75*49cdfc7eSAndroid Build Coastguard Worker } \
76*49cdfc7eSAndroid Build Coastguard Worker } \
77*49cdfc7eSAndroid Build Coastguard Worker } while (0)
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker /*
80*49cdfc7eSAndroid Build Coastguard Worker * Make the backing file large enough to cover the last corresponding kernel page.
81*49cdfc7eSAndroid Build Coastguard Worker *
82*49cdfc7eSAndroid Build Coastguard Worker * This is an artifact of x86_64 page size emulation on Android, to handle
83*49cdfc7eSAndroid Build Coastguard Worker * file_map_fault's, which does allow access to the partial page after the end
84*49cdfc7eSAndroid Build Coastguard Worker * of the file.
85*49cdfc7eSAndroid Build Coastguard Worker */
86*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_FILE_PRINTF_PGSIZE_EMULATION(file, str) \
87*49cdfc7eSAndroid Build Coastguard Worker do { \
88*49cdfc7eSAndroid Build Coastguard Worker if (kernel_page_size() == getpagesize()) { \
89*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(file, str); \
90*49cdfc7eSAndroid Build Coastguard Worker } else { \
91*49cdfc7eSAndroid Build Coastguard Worker int str_len = strlen(str); \
92*49cdfc7eSAndroid Build Coastguard Worker int nr_writes = ((kernel_page_size() \
93*49cdfc7eSAndroid Build Coastguard Worker * (nr_pgs_to_nr_kernel_pgs(1) - 1)) / str_len) + 1; \
94*49cdfc7eSAndroid Build Coastguard Worker int total_len = str_len * nr_writes; \
95*49cdfc7eSAndroid Build Coastguard Worker char *buffer = SAFE_MALLOC(total_len + 1); \
96*49cdfc7eSAndroid Build Coastguard Worker for (int i = 0; i < nr_writes; i++) \
97*49cdfc7eSAndroid Build Coastguard Worker strcat(buffer, str); \
98*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(file, buffer); \
99*49cdfc7eSAndroid Build Coastguard Worker free(buffer); \
100*49cdfc7eSAndroid Build Coastguard Worker } \
101*49cdfc7eSAndroid Build Coastguard Worker } while (0)
102*49cdfc7eSAndroid Build Coastguard Worker #endif /* PGSIZE_HELPER_H */
103