xref: /freebsd-14.2/lib/libc/gen/getpagesizes.c (revision 4025b5b5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Alan L. Cox <[email protected]>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/mman.h>
31 #include <sys/sysctl.h>
32 
33 #include <errno.h>
34 #include <link.h>
35 
36 #include "libc_private.h"
37 
38 /*
39  * Retrieves page size information from the system.  Specifically, returns the
40  * number of distinct page sizes that are supported by the system, if
41  * "pagesize" is NULL and "nelem" is 0.  Otherwise, assigns up to "nelem" of
42  * the system-supported page sizes to consecutive elements of the array
43  * referenced by "pagesize", and returns the number of such page sizes that it
44  * assigned to the array.  These page sizes are expressed in bytes.
45  *
46  * The implementation of this function does not directly or indirectly call
47  * malloc(3) or any other dynamic memory allocator that may itself call this
48  * function.
49  */
50 int
getpagesizes(size_t pagesize[],int nelem)51 getpagesizes(size_t pagesize[], int nelem)
52 {
53 	static u_long ps[MAXPAGESIZES];
54 	static int nops;
55 	size_t size;
56 	int error, i;
57 
58 	if (nelem < 0 || (nelem > 0 && pagesize == NULL)) {
59 		errno = EINVAL;
60 		return (-1);
61 	}
62 	/* Cache the result of the sysctl(2). */
63 	if (nops == 0) {
64 		error = _elf_aux_info(AT_PAGESIZES, ps, sizeof(ps));
65 		size = sizeof(ps);
66 		if (error != 0 || ps[0] == 0) {
67 			if (sysctlbyname("hw.pagesizes", ps, &size, NULL, 0)
68 			    == -1)
69 				return (-1);
70 		}
71 		/* Count the number of page sizes that are supported. */
72 		nops = size / sizeof(ps[0]);
73 		while (nops > 0 && ps[nops - 1] == 0)
74 			nops--;
75 	}
76 	if (pagesize == NULL)
77 		return (nops);
78 	/* Return up to "nelem" page sizes from the cached result. */
79 	if (nelem > nops)
80 		nelem = nops;
81 	for (i = 0; i < nelem; i++)
82 		pagesize[i] = ps[i];
83 	return (nelem);
84 }
85