vmparam.h revision 1.14 1 1.14 skrll /* $NetBSD: vmparam.h,v 1.14 2023/05/07 12:41:48 skrll Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.9 skrll * Copyright (c) 2014, 2020 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.9 skrll * by Matt Thomas of 3am Software Foundry, and Nick Hudson.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #ifndef _RISCV_VMPARAM_H_
33 1.1 matt #define _RISCV_VMPARAM_H_
34 1.1 matt
35 1.1 matt #include <riscv/param.h>
36 1.1 matt
37 1.1 matt #ifdef _KERNEL_OPT
38 1.1 matt #include "opt_multiprocessor.h"
39 1.1 matt #endif
40 1.1 matt
41 1.1 matt /*
42 1.1 matt * Machine dependent VM constants for RISCV.
43 1.1 matt */
44 1.1 matt
45 1.9 skrll /*
46 1.9 skrll * We use a 4K page on both RV64 and RV32 systems.
47 1.9 skrll * Override PAGE_* definitions to compile-time constants.
48 1.9 skrll */
49 1.1 matt #define PAGE_SHIFT PGSHIFT
50 1.1 matt #define PAGE_SIZE (1 << PAGE_SHIFT)
51 1.1 matt #define PAGE_MASK (PAGE_SIZE - 1)
52 1.1 matt
53 1.1 matt /*
54 1.1 matt * USRSTACK is the top (end) of the user stack.
55 1.1 matt *
56 1.1 matt * USRSTACK needs to start a page below the maxuser address so that a memory
57 1.1 matt * access with a maximum displacement (0x7ff) won't cross into the kernel's
58 1.1 matt * address space. We use PAGE_SIZE instead of 0x800 since these need to be
59 1.1 matt * page-aligned.
60 1.1 matt */
61 1.14 skrll #define USRSTACK (VM_MAXUSER_ADDRESS - PAGE_SIZE) /* Start of user stack */
62 1.14 skrll #define USRSTACK32 ((uint32_t)VM_MAXUSER_ADDRESS32 - PAGE_SIZE)
63 1.1 matt
64 1.1 matt /*
65 1.1 matt * Virtual memory related constants, all in bytes
66 1.1 matt */
67 1.1 matt #ifndef MAXTSIZ
68 1.1 matt #define MAXTSIZ (128*1024*1024) /* max text size */
69 1.1 matt #endif
70 1.1 matt #ifndef DFLDSIZ
71 1.1 matt #define DFLDSIZ (256*1024*1024) /* initial data size limit */
72 1.1 matt #endif
73 1.1 matt #ifndef MAXDSIZ
74 1.1 matt #define MAXDSIZ (1536*1024*1024) /* max data size */
75 1.1 matt #endif
76 1.1 matt #ifndef DFLSSIZ
77 1.8 simonb #define DFLSSIZ (4*1024*1024) /* initial stack size limit */
78 1.1 matt #endif
79 1.1 matt #ifndef MAXSSIZ
80 1.1 matt #define MAXSSIZ (120*1024*1024) /* max stack size */
81 1.1 matt #endif
82 1.1 matt
83 1.1 matt /*
84 1.1 matt * Virtual memory related constants, all in bytes
85 1.1 matt */
86 1.1 matt #ifndef DFLDSIZ32
87 1.1 matt #define DFLDSIZ32 DFLDSIZ /* initial data size limit */
88 1.1 matt #endif
89 1.1 matt #ifndef MAXDSIZ32
90 1.1 matt #define MAXDSIZ32 MAXDSIZ /* max data size */
91 1.1 matt #endif
92 1.1 matt #ifndef DFLSSIZ32
93 1.1 matt #define DFLSSIZ32 DFLTSIZ /* initial stack size limit */
94 1.1 matt #endif
95 1.1 matt #ifndef MAXSSIZ32
96 1.1 matt #define MAXSSIZ32 MAXSSIZ /* max stack size */
97 1.1 matt #endif
98 1.1 matt
99 1.1 matt /*
100 1.1 matt * PTEs for mapping user space into the kernel for phyio operations.
101 1.1 matt * The default PTE number is enough to cover 8 disks * MAXBSIZE.
102 1.1 matt */
103 1.1 matt #ifndef USRIOSIZE
104 1.1 matt #define USRIOSIZE (MAXBSIZE/PAGE_SIZE * 8)
105 1.1 matt #endif
106 1.1 matt
107 1.5 maxv /*
108 1.5 maxv * User/kernel map constants.
109 1.5 maxv */
110 1.14 skrll #define VM_MIN_ADDRESS ((vaddr_t)PAGE_SIZE)
111 1.12 skrll #ifdef _LP64 /* Sv39 / Sv48 / Sv57 */
112 1.9 skrll /*
113 1.14 skrll * SV39 gives 1 << (39 - 1) address space to kernel and same to userland.
114 1.14 skrll * This is 256GiB each. Split the kernel space in two and use the top half
115 1.14 skrll * for direct map.
116 1.14 skrll *
117 1.9 skrll * kernel virtual space layout:
118 1.9 skrll * 0xffff_ffc0_0000_0000 - 64GiB KERNEL VM Space (inc. text/data/bss)
119 1.9 skrll * (0xffff_ffc0_4000_0000 +1GiB) KERNEL VM start of KVA
120 1.9 skrll * (0xffff_ffd0_0000_0000 64GiB) reserved
121 1.9 skrll * 0xffff_ffe0_0000_0000 - 128GiB direct mapping
122 1.9 skrll */
123 1.5 maxv #define VM_MAXUSER_ADDRESS ((vaddr_t)0x0000004000000000 - 16 * PAGE_SIZE)
124 1.7 skrll #define VM_MIN_KERNEL_ADDRESS ((vaddr_t)0xffffffc000000000)
125 1.9 skrll #define VM_MAX_KERNEL_ADDRESS ((vaddr_t)0xffffffd000000000)
126 1.9 skrll
127 1.5 maxv #else /* Sv32 */
128 1.14 skrll /*
129 1.14 skrll * kernel virtual space layout:
130 1.14 skrll * 0x8000_0000 - 64GiB KERNEL VM Space (inc. text/data/bss)
131 1.14 skrll * (0x4000_0000 +1GiB) KERNEL VM start of KVA
132 1.14 skrll * (0x0000_0000 64GiB) reserved
133 1.14 skrll */
134 1.14 skrll
135 1.14 skrll /*
136 1.14 skrll * kernel virtual space layout without direct map (common case)
137 1.14 skrll *
138 1.14 skrll * 0x8000_0000 - 256MB kernel text/data/bss
139 1.14 skrll * 0x9000_0000 - 1536MB Kernel VM Space
140 1.14 skrll * 0xf000_0000 - 256MB IO
141 1.14 skrll *
142 1.14 skrll * kernel virtual space layout with KASAN
143 1.14 skrll *
144 1.14 skrll * 0x8000_0000 - 256MB kernel text/data/bss
145 1.14 skrll * 0x9000_0000 - 768MB Kernel VM Space
146 1.14 skrll * 0xc000_0000 - 128MB (KASAN SHADOW MAP)
147 1.14 skrll * 0xc800_0000 - 640MB (spare)
148 1.14 skrll * 0xf000_0000 - 256MB IO
149 1.14 skrll *
150 1.14 skrll * kernel virtual space layout with direct map (1GB limited)
151 1.14 skrll * 0x8000_0000 - 1024MB kernel text/data/bss and direct map start
152 1.14 skrll * 0xc000_0000 - 768MB Kernel VM Space
153 1.14 skrll * 0xf000_0000 - 256MB IO
154 1.14 skrll *
155 1.14 skrll */
156 1.14 skrll
157 1.14 skrll
158 1.14 skrll
159 1.14 skrll #define VM_MAXUSER_ADDRESS ((vaddr_t)-0x7fffffff-1)/* 0xffff_ffff_8000_0000 */
160 1.14 skrll #define VM_MIN_KERNEL_ADDRESS ((vaddr_t)-0x7fffffff-1)/* 0xffff_ffff_8000_0000 */
161 1.14 skrll #define VM_MAX_KERNEL_ADDRESS ((vaddr_t)-0x10000000) /* 0xffff_ffff_f000_0000 */
162 1.9 skrll
163 1.1 matt #endif
164 1.10 skrll #define VM_KERNEL_BASE VM_MIN_KERNEL_ADDRESS
165 1.10 skrll #define VM_KERNEL_SIZE 0x2000000 /* 32 MiB (8 / 16 megapages) */
166 1.10 skrll #define VM_KERNEL_DTB_BASE (VM_KERNEL_BASE + VM_KERNEL_SIZE)
167 1.13 skrll #define VM_KERNEL_DTB_SIZE 0x1000000 /* 16 MiB (4 / 8 megapages) */
168 1.13 skrll #define VM_KERNEL_IO_BASE (VM_KERNEL_DTB_BASE + VM_KERNEL_DTB_SIZE)
169 1.13 skrll #define VM_KERNEL_IO_SIZE 0x1000000 /* 16 MiB (4 / 8 megapages) */
170 1.10 skrll
171 1.13 skrll #define VM_KERNEL_RESERVED (VM_KERNEL_SIZE + VM_KERNEL_DTB_SIZE + VM_KERNEL_IO_SIZE)
172 1.10 skrll
173 1.10 skrll #define VM_KERNEL_VM_BASE (VM_MIN_KERNEL_ADDRESS + VM_KERNEL_RESERVED)
174 1.10 skrll #define VM_KERNEL_VM_SIZE (VM_MAX_KERNEL_ADDRESS - VM_KERNEL_VM_BASE)
175 1.9 skrll
176 1.1 matt #define VM_MAX_ADDRESS VM_MAXUSER_ADDRESS
177 1.4 mrg #define VM_MAXUSER_ADDRESS32 ((vaddr_t)(1UL << 31))/* 0x0000000080000000 */
178 1.1 matt
179 1.9 skrll #ifdef _LP64
180 1.9 skrll /*
181 1.9 skrll * Since we have the address space, we map all of physical memory (RAM)
182 1.11 simonb * using gigapages on SV39, terapages on SV48 and petapages on SV57.
183 1.9 skrll */
184 1.9 skrll #define RISCV_DIRECTMAP_MASK ((vaddr_t) 0xffffffe000000000L)
185 1.11 simonb #define RISCV_DIRECTMAP_SIZE (-RISCV_DIRECTMAP_MASK - PAGE_SIZE) /* 128GiB */
186 1.9 skrll #define RISCV_DIRECTMAP_START RISCV_DIRECTMAP_MASK
187 1.9 skrll #define RISCV_DIRECTMAP_END (RISCV_DIRECTMAP_START + RISCV_DIRECTMAP_SIZE)
188 1.14 skrll #define RISCV_DIRECTMAP_P(va) (((vaddr_t) (va) & RISCV_DIRECTMAP_MASK) == RISCV_DIRECTMAP_MASK)
189 1.9 skrll #define RISCV_PA_TO_KVA(pa) ((vaddr_t) ((pa) | RISCV_DIRECTMAP_START))
190 1.9 skrll #define RISCV_KVA_TO_PA(va) ((paddr_t) ((va) & ~RISCV_DIRECTMAP_MASK))
191 1.9 skrll #endif
192 1.9 skrll
193 1.1 matt /*
194 1.1 matt * The address to which unspecified mapping requests default
195 1.1 matt */
196 1.1 matt #define __USE_TOPDOWN_VM
197 1.1 matt
198 1.1 matt #define VM_DEFAULT_ADDRESS_TOPDOWN(da, sz) \
199 1.2 joerg trunc_page(USRSTACK - MAXSSIZ - (sz) - user_stack_guard_size)
200 1.1 matt #define VM_DEFAULT_ADDRESS_BOTTOMUP(da, sz) \
201 1.1 matt round_page((vaddr_t)(da) + (vsize_t)maxdmap)
202 1.1 matt
203 1.1 matt #define VM_DEFAULT_ADDRESS32_TOPDOWN(da, sz) \
204 1.3 joerg trunc_page(USRSTACK32 - MAXSSIZ32 - (sz) - user_stack_guard_size)
205 1.1 matt #define VM_DEFAULT_ADDRESS32_BOTTOMUP(da, sz) \
206 1.1 matt round_page((vaddr_t)(da) + (vsize_t)MAXDSIZ32)
207 1.1 matt
208 1.1 matt /* virtual sizes (bytes) for various kernel submaps */
209 1.1 matt #define VM_PHYS_SIZE (USRIOSIZE*PAGE_SIZE)
210 1.1 matt
211 1.14 skrll /*
212 1.14 skrll * max number of non-contig chunks of physical RAM you can have
213 1.14 skrll */
214 1.14 skrll #define VM_PHYSSEG_MAX 64
215 1.14 skrll
216 1.14 skrll /*
217 1.14 skrll * when converting a physical address to a vm_page structure, we
218 1.14 skrll * want to use a binary search on the chunks of physical memory
219 1.14 skrll * to find our RAM
220 1.14 skrll */
221 1.1 matt #define VM_PHYSSEG_STRAT VM_PSTRAT_BSEARCH
222 1.1 matt
223 1.1 matt #ifndef VM_NFREELIST
224 1.1 matt #define VM_NFREELIST 2 /* 2 distinct memory segments */
225 1.1 matt #define VM_FREELIST_DEFAULT 0
226 1.1 matt #define VM_FREELIST_DIRECTMAP 1
227 1.1 matt #endif
228 1.1 matt
229 1.1 matt #ifdef _KERNEL
230 1.1 matt #ifdef _LP64
231 1.1 matt void * cpu_uarea_alloc(bool);
232 1.1 matt bool cpu_uarea_free(void *);
233 1.1 matt #endif
234 1.1 matt #endif
235 1.1 matt
236 1.1 matt #endif /* ! _RISCV_VMPARAM_H_ */
237