]> git.saurik.com Git - apple/xnu.git/blob - osfmk/mach/vm_param.h
1bd57e3326b6da71f67fc18433ec91f66c268c51
[apple/xnu.git] / osfmk / mach / vm_param.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * @OSF_COPYRIGHT@
25 */
26 /*
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
29 * All Rights Reserved.
30 *
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
36 *
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 *
41 * Carnegie Mellon requests users of this software to return to
42 *
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
47 *
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
50 */
51 /*
52 */
53 /*
54 * File: mach/vm_param.h
55 * Author: Avadis Tevanian, Jr., Michael Wayne Young
56 * Date: 1985
57 *
58 * Machine independent virtual memory parameters.
59 *
60 */
61
62 #ifndef _MACH_VM_PARAM_H_
63 #define _MACH_VM_PARAM_H_
64
65 #include <mach/machine/vm_param.h>
66
67 #ifdef KERNEL
68
69 #ifndef ASSEMBLER
70 #include <mach/vm_types.h>
71 #endif /* ASSEMBLER */
72
73 /*
74 * The machine independent pages are refered to as PAGES. A page
75 * is some number of hardware pages, depending on the target machine.
76 */
77
78 #ifndef ASSEMBLER
79
80 #define PAGE_SIZE_64 (unsigned long long)PAGE_SIZE /* pagesize in addr units */
81 #define PAGE_MASK_64 (unsigned long long)PAGE_MASK /* mask for off in page */
82
83 /*
84 * Convert addresses to pages and vice versa. No rounding is used.
85 * The atop_32 and ptoa_32 macros should not be use on 64 bit types.
86 * The round_page_64 and trunc_page_64 macros should be used instead.
87 */
88
89 #define atop_32(x) ((uint32_t)(x) >> PAGE_SHIFT)
90 #define ptoa_32(x) ((uint32_t)(x) << PAGE_SHIFT)
91 #define atop_64(x) ((uint64_t)(x) >> PAGE_SHIFT)
92 #define ptoa_64(x) ((uint64_t)(x) << PAGE_SHIFT)
93
94 /*
95 * While the following block is enabled, the legacy atop and ptoa
96 * macros will behave correctly. If not, they will generate
97 * invalid lvalue errors.
98 */
99
100 #if 1
101 #define atop(x) ((uint32_t)(x) >> PAGE_SHIFT)
102 #define ptoa(x) ((uint32_t)(x) << PAGE_SHIFT)
103 #else
104 #define atop(x) (0UL = 0)
105 #define ptoa(x) (0UL = 0)
106 #endif
107
108 /*
109 * Page-size rounding macros for the Public fixed-width VM types.
110 */
111 #define mach_vm_round_page(x) (((mach_vm_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
112 #define mach_vm_trunc_page(x) ((mach_vm_offset_t)(x) & ~((signed)PAGE_MASK))
113
114 #define memory_object_round_page(x) (((memory_object_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
115 #define memory_object_trunc_page(x) ((memory_object_offset_t)(x) & ~((signed)PAGE_MASK))
116
117 /*
118 * Rounding macros for the legacy (scalable with the current task's
119 * address space size) VM types.
120 */
121
122 #define round_page(x) (((vm_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
123 #define trunc_page(x) ((vm_offset_t)(x) & ~((signed)PAGE_MASK))
124
125 /*
126 * Round off or truncate to the nearest page. These will work
127 * for either addresses or counts. (i.e. 1 byte rounds to 1 page
128 * bytes. The round_page_32 and trunc_page_32 macros should not be
129 * use on 64 bit types. The round_page_64 and trunc_page_64 macros
130 * should be used instead.
131 *
132 * These should only be used in the rare case the size of the address
133 * or length is hard-coded as 32 or 64 bit. Otherwise, the macros
134 * associated with the specific VM type should be used.
135 */
136
137 #define round_page_32(x) (((uint32_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
138 #define trunc_page_32(x) ((uint32_t)(x) & ~((signed)PAGE_MASK))
139 #define round_page_64(x) (((uint64_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
140 #define trunc_page_64(x) ((uint64_t)(x) & ~((signed)PAGE_MASK))
141
142
143 /*
144 * Enable the following block to find uses of xxx_32 macros that should
145 * be xxx_64. These macros only work in C code, not C++. The resulting
146 * binaries are not functional. Look for invalid lvalue errors in
147 * the compiler output.
148 *
149 * Enabling the following block will also find use of the xxx_64 macros
150 * that have been passed pointers. The parameters should be case to an
151 * unsigned long type first. Look for invalid operands to binary + error
152 * in the compiler output.
153 */
154
155 #if 0
156 #undef atop_32
157 #undef ptoa_32
158 #undef round_page_32
159 #undef trunc_page_32
160 #undef atop_64
161 #undef ptoa_64
162 #undef round_page_64
163 #undef trunc_page_64
164
165 #ifndef __cplusplus
166
167 #define atop_32(x) \
168 (__builtin_choose_expr (sizeof(x) != sizeof(uint64_t), \
169 (*(long *)0), \
170 (0UL)) = 0)
171
172 #define ptoa_32(x) \
173 (__builtin_choose_expr (sizeof(x) != sizeof(uint64_t), \
174 (*(long *)0), \
175 (0UL)) = 0)
176
177 #define round_page_32(x) \
178 (__builtin_choose_expr (sizeof(x) != sizeof(uint64_t), \
179 (*(long *)0), \
180 (0UL)) = 0)
181
182 #define trunc_page_32(x) \
183 (__builtin_choose_expr (sizeof(x) != sizeof(uint64_t), \
184 (*(long *)0), \
185 (0UL)) = 0)
186 #else
187
188 #define atop_32(x) (0)
189 #define ptoa_32(x) (0)
190 #define round_page_32(x) (0)
191 #define trunc_page_32(x) (0)
192
193 #endif /* ! __cplusplus */
194
195 #define atop_64(x) ((uint64_t)((x) + (uint8_t *)0))
196 #define ptoa_64(x) ((uint64_t)((x) + (uint8_t *)0))
197 #define round_page_64(x) ((uint64_t)((x) + (uint8_t *)0))
198 #define trunc_page_64(x) ((uint64_t)((x) + (uint8_t *)0))
199
200 #endif
201
202 /*
203 * Determine whether an address is page-aligned, or a count is
204 * an exact page multiple.
205 */
206
207 #define page_aligned(x) ((((vm_object_offset_t) (x)) & PAGE_MASK) == 0)
208
209 extern vm_size_t mem_size; /* 32-bit size of memory - limited by maxmem - deprecated */
210 extern uint64_t max_mem; /* 64-bit size of memory - limited by maxmem */
211
212 #ifdef XNU_KERNEL_PRIVATE
213
214 extern uint64_t mem_actual; /* 64-bit size of memory - not limited by maxmem */
215 extern uint64_t sane_size; /* Memory size to use for defaults calculations */
216 extern addr64_t vm_last_addr; /* Highest kernel virtual address known to the VM system */
217
218 #endif /* XNU_KERNEL_PRIVATE */
219
220 extern vm_size_t page_size;
221 extern vm_size_t page_mask;
222 extern int page_shift;
223
224 /* We need a way to get rid of compiler warnings when we cast from */
225 /* a 64 bit value to an address that is 32 bits. */
226 /* We know at this point the cast is harmless but sometime in */
227 /* the future it may not be. */
228 /* When size of an int is no longer equal to size of uintptr_t then */
229 /* the compile will fail and we know we need to fix our cast. */
230 #include <stdint.h>
231 #ifndef __CAST_DOWN_CHECK
232 #define __CAST_DOWN_CHECK
233 typedef char __NEED_TO_CHANGE_CAST_DOWN[ sizeof(uintptr_t) == sizeof(int) ? 0 : -1 ];
234 #define CAST_DOWN( type, addr ) ( ((type)((uintptr_t) (addr))) )
235 #endif /* __CAST_DOWN_CHECK */
236
237 #endif /* ASSEMBLER */
238
239 #endif /* KERNEL */
240
241 #endif /* _MACH_VM_PARAM_H_ */