1 /* $NetBSD: pmap_bootstrap.c,v 1.48 2026/04/05 14:35:48 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * the Systems Programming Group of the University of Utah Computer 9 * Science Department. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)pmap_bootstrap.c 8.1 (Berkeley) 6/10/93 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: pmap_bootstrap.c,v 1.48 2026/04/05 14:35:48 thorpej Exp $"); 40 41 #include "opt_m68k_arch.h" 42 43 #include <sys/param.h> 44 #include <uvm/uvm_extern.h> 45 46 #include <machine/cpu.h> 47 #include <machine/pte.h> 48 #include <machine/vmparam.h> 49 50 #define RELOC(v, t) *((t*)((uintptr_t)&(v) + firstpa)) 51 52 extern char *etext; 53 54 extern vaddr_t kernel_reloc_offset; 55 56 /* 57 * Special purpose kernel virtual addresses, used for mapping 58 * physical pages for a variety of temporary or permanent purposes: 59 * 60 * CADDR1, CADDR2: pmap zero/copy operations 61 * vmmap: /dev/mem, crash dumps, parity error checking 62 * msgbufaddr: kernel message buffer 63 */ 64 void *CADDR1, *CADDR2; 65 char *vmmap; 66 extern void *msgbufaddr; 67 68 paddr_t pmap_bootstrap1(paddr_t, paddr_t); 69 70 /* 71 * Bootstrap the VM system. 72 * 73 * Called with MMU off so we must relocate all global references by `firstpa' 74 * (don't call any functions here!) `nextpa' is the first available physical 75 * memory address. Returns an updated first PA reflecting the memory we 76 * have allocated. MMU is still off when we return. 77 * 78 * XXX assumes sizeof(u_int) == sizeof(pt_entry_t) 79 * XXX a PIC compiler would make this much easier. 80 */ 81 paddr_t __attribute__((no_instrument_function)) 82 pmap_bootstrap1(paddr_t nextpa, paddr_t firstpa) 83 { 84 paddr_t lwp0upa, kstpa, kptmpa, kptpa; 85 u_int nptpages, kstsize; 86 st_entry_t protoste, *ste, *este; 87 pt_entry_t protopte, *pte, *epte; 88 #if defined(M68040) 89 u_int stfree = 0; /* XXX: gcc -Wuninitialized */ 90 #endif 91 92 nextpa = m68k_round_page(nextpa); 93 94 /* 95 * Calculate important physical addresses: 96 * 97 * lwp0upa lwp0 u-area UPAGES pages 98 * 99 * kstpa kernel segment table 1 page (!040) 100 * N pages (040) 101 * 102 * kptmpa kernel PT map 1 page 103 * 104 * kptpa statically allocated 105 * kernel PT pages Sysptsize+ pages 106 * 107 * The KVA corresponding to any of these PAs is: 108 * (PA - firstpa + KERNBASE). 109 */ 110 RELOC(kernel_reloc_offset, vaddr_t) = firstpa; 111 112 lwp0upa = nextpa; 113 nextpa += USPACE; 114 if (RELOC(mmutype, int) == MMU_68040) 115 kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE); 116 else 117 kstsize = 1; 118 kstpa = nextpa; 119 nextpa += kstsize * PAGE_SIZE; 120 kptmpa = nextpa; 121 nextpa += PAGE_SIZE; 122 kptpa = nextpa; 123 nptpages = RELOC(Sysptsize, int) + 124 howmany(RELOC(physmem, psize_t), NPTEPG); 125 nextpa += nptpages * PAGE_SIZE; 126 127 /* 128 * Initialize segment table and kernel page table map. 129 * 130 * On 68030s and earlier MMUs the two are identical except for 131 * the valid bits so both are initialized with essentially the 132 * same values. On the 68040, which has a mandatory 3-level 133 * structure, the segment table holds the level 1 table and part 134 * (or all) of the level 2 table and hence is considerably 135 * different. Here the first level consists of 128 descriptors 136 * (512 bytes) each mapping 32mb of address space. Each of these 137 * points to blocks of 128 second level descriptors (512 bytes) 138 * each mapping 256kb. Note that there may be additional "segment 139 * table" pages depending on how large MAXKL2SIZE is. 140 * 141 * Portions of the last segment of KVA space (0x3FC00000 - 142 * 0x3FFFFFFF) are mapped for the kernel page tables. 143 * 144 * The rest of KVA space (0x40000000 - 0xFFFFFFFF) is mapped 145 * by tt0/tt1 registers for device I/O in locore.s. 146 * 147 * XXX cramming two levels of mapping into the single "segment" 148 * table on the 68040 is intended as a temporary hack to get things 149 * working. The 224mb of address space that this allows will most 150 * likely be insufficient in the future (at least for the kernel). 151 */ 152 #if defined(M68040) 153 if (RELOC(mmutype, int) == MMU_68040) { 154 int nl1desc, nl2desc, i; 155 156 /* 157 * First invalidate the entire "segment table" pages 158 * (levels 1 and 2 have the same "invalid" value). 159 */ 160 ste = (st_entry_t *)kstpa; 161 este = &ste[kstsize * NPTEPG]; 162 while (ste < este) 163 *ste++ = SG_NV; 164 /* 165 * Initialize level 2 descriptors (which immediately 166 * follow the level 1 table). We need: 167 * NPTEPG / SG4_LEV3SIZE 168 * level 2 descriptors to map each of the nptpages 169 * pages of PTEs. Note that we set the "used" bit 170 * now to save the HW the expense of doing it. 171 */ 172 nl2desc = nptpages * (NPTEPG / SG4_LEV3SIZE); 173 ste = (st_entry_t *)kstpa; 174 ste = &ste[SG4_LEV1SIZE]; 175 este = &ste[nl2desc]; 176 protoste = kptpa | SG_U | SG_RW | SG_V; 177 while (ste < este) { 178 *ste++ = protoste; 179 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 180 } 181 /* 182 * Initialize level 1 descriptors. We need: 183 * howmany(nl2desc, SG4_LEV2SIZE) 184 * level 1 descriptors to map the `nl2desc' level 2's. 185 */ 186 nl1desc = howmany(nl2desc, SG4_LEV2SIZE); 187 ste = (st_entry_t *)kstpa; 188 este = &ste[nl1desc]; 189 protoste = (paddr_t)&ste[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V; 190 while (ste < este) { 191 *ste++ = protoste; 192 protoste += (SG4_LEV2SIZE * sizeof(st_entry_t)); 193 } 194 /* 195 * Initialize the level 1 descriptor correspond to 196 * SYSMAP_VA to map the last block of level 2 descriptors. 197 */ 198 ste = (st_entry_t *)kstpa; 199 ste = &ste[SYSMAP_VA >> SG4_SHIFT1]; 200 *ste = protoste; 201 /* 202 * Now initialize the portion of that block of 203 * descriptors to map Sysptmap. 204 */ 205 i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE); 206 ste = (st_entry_t *)kstpa; 207 ste = &ste[i + ((SYSMAP_VA & SG4_MASK2) >> SG4_SHIFT2)]; 208 este = &ste[NPTEPG / SG4_LEV3SIZE]; 209 protoste = kptmpa | SG_U | SG_RW | SG_V; 210 while (ste < este) { 211 *ste++ = protoste; 212 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 213 } 214 /* 215 * Calculate the free level 2 descriptor mask 216 * noting that we have used: 217 * 0: level 1 table 218 * 1 to nl1desc: map page tables 219 * nl1desc + 1: maps kptmpa and last-page page table 220 */ 221 /* mark an entry for level 1 table */ 222 stfree = ~l2tobm(0); 223 /* mark entries for map page tables */ 224 for (i = 1; i <= nl1desc; i++) 225 stfree &= ~l2tobm(i); 226 /* mark an entry for kptmpa */ 227 stfree &= ~l2tobm(i); 228 /* mark entries not available */ 229 for (i = MAXKL2SIZE; i < sizeof(stfree) * NBBY; i++) 230 stfree &= ~l2tobm(i); 231 232 /* 233 * Initialize Sysptmap 234 */ 235 pte = (pt_entry_t *)kptmpa; 236 epte = &pte[nptpages]; 237 protopte = kptpa | PG_RW | PG_CI | PG_V; 238 while (pte < epte) { 239 *pte++ = protopte; 240 protopte += PAGE_SIZE; 241 } 242 /* 243 * Invalidate all remaining entries. 244 */ 245 epte = (pt_entry_t *)kptmpa; 246 epte = &epte[TIB_SIZE]; 247 while (pte < epte) { 248 *pte++ = PG_NV; 249 } 250 /* 251 * Initialize the one corresponding SYSMAP_VA 252 * to point to Sysptmap. 253 */ 254 pte = (pt_entry_t *)kptmpa; 255 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 256 *pte = kptmpa | PG_RW | PG_CI | PG_V; 257 } else 258 #endif 259 { 260 /* 261 * Map the page table pages in both the HW segment table 262 * and the software Sysptmap. 263 */ 264 ste = (st_entry_t *)kstpa; 265 pte = (pt_entry_t *)kptmpa; 266 epte = &pte[nptpages]; 267 protoste = kptpa | SG_RW | SG_V; 268 protopte = kptpa | PG_RW | PG_CI | PG_V; 269 while (pte < epte) { 270 *ste++ = protoste; 271 *pte++ = protopte; 272 protoste += PAGE_SIZE; 273 protopte += PAGE_SIZE; 274 } 275 /* 276 * Invalidate all remaining entries in both. 277 */ 278 este = (st_entry_t *)kstpa; 279 este = &este[TIA_SIZE]; 280 while (ste < este) 281 *ste++ = SG_NV; 282 epte = (pt_entry_t *)kptmpa; 283 epte = &epte[TIB_SIZE]; 284 while (pte < epte) 285 *pte++ = PG_NV; 286 /* 287 * Initialize the one corresponding to SYSMAP_VA 288 * to point to Sysptmap. 289 */ 290 ste = (st_entry_t *)kstpa; 291 ste = &ste[SYSMAP_VA >> SEGSHIFT]; 292 pte = (pt_entry_t *)kptmpa; 293 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 294 *ste = kptmpa | SG_RW | SG_V; 295 *pte = kptmpa | PG_RW | PG_CI | PG_V; 296 } 297 298 /* 299 * Initialize kernel page table. 300 * Start by invalidating the `nptpages' that we have allocated. 301 */ 302 pte = (pt_entry_t *)kptpa; 303 epte = &pte[nptpages * NPTEPG]; 304 while (pte < epte) 305 *pte++ = PG_NV; 306 /* 307 * Validate PTEs for kernel text (RO). 308 */ 309 pte = (pt_entry_t *)kptpa; 310 pte = &pte[m68k_btop(KERNBASE)]; 311 epte = &pte[m68k_btop(m68k_trunc_page(&etext))]; 312 protopte = firstpa | PG_RO | PG_V; 313 while (pte < epte) { 314 *pte++ = protopte; 315 protopte += PAGE_SIZE; 316 } 317 /* 318 * Validate PTEs for kernel data/bss, dynamic data allocated 319 * by us so far (kstpa - firstpa bytes), and pages for lwp0 320 * u-area and page table allocated below (RW). 321 */ 322 epte = (pt_entry_t *)kptpa; 323 epte = &epte[m68k_btop(kstpa - firstpa)]; 324 protopte = (protopte & ~PG_PROT) | PG_RW; 325 /* 326 * Enable copy-back caching of data pages 327 */ 328 if (RELOC(mmutype, int) == MMU_68040) 329 protopte |= PG_CCB; 330 while (pte < epte) { 331 *pte++ = protopte; 332 protopte += PAGE_SIZE; 333 } 334 /* 335 * Map the kernel segment table cache invalidated for 68040/68060. 336 * (for the 68040 not strictly necessary, but recommended by Motorola; 337 * for the 68060 mandatory) 338 */ 339 epte = (pt_entry_t *)kptpa; 340 epte = &epte[m68k_btop(nextpa - firstpa)]; 341 protopte = (protopte & ~PG_PROT) | PG_RW; 342 #if defined(M68040) 343 if (RELOC(mmutype, int) == MMU_68040) { 344 protopte &= ~PG_CCB; 345 protopte |= PG_CIN; 346 } 347 #endif 348 while (pte < epte) { 349 *pte++ = protopte; 350 protopte += PAGE_SIZE; 351 } 352 353 /* 354 * Finally, validate the internal IO space PTEs (RW+CI). 355 */ 356 357 #define PTE2VA(pte) m68k_ptob(pte - ((pt_entry_t *)kptpa)) 358 359 RELOC(virtual_avail, vaddr_t) = PTE2VA(pte); 360 361 /* 362 * Calculate important exported kernel addresses and related values. 363 */ 364 /* 365 * Sysseg: base of kernel segment table 366 */ 367 RELOC(Sysseg, st_entry_t *) = (st_entry_t *)(kstpa - firstpa); 368 RELOC(Sysseg_pa, paddr_t) = kstpa; 369 #if defined(M68040) 370 if (RELOC(mmutype, int) == MMU_68040) 371 RELOC(protostfree, u_int) = stfree; 372 #endif 373 /* 374 * Sysptmap: base of kernel page table map 375 */ 376 RELOC(Sysptmap, pt_entry_t *) = (pt_entry_t *)(kptmpa - firstpa); 377 /* 378 * Sysmap: kernel page table (as mapped through Sysptmap) 379 * Allocated at the end of KVA space. 380 */ 381 RELOC(Sysmap, pt_entry_t *) = (pt_entry_t *)SYSMAP_VA; 382 383 /* 384 * Remember the u-area address so it can be loaded in the lwp0 385 * via uvm_lwp_setuarea() later in pmap_bootstrap2(). 386 */ 387 RELOC(lwp0uarea, vaddr_t) = lwp0upa - firstpa; 388 389 RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS; 390 391 /* 392 * Allocate some fixed, special purpose kernel virtual addresses 393 */ 394 { 395 vaddr_t va = RELOC(virtual_avail, vaddr_t); 396 397 RELOC(CADDR1, void *) = (void *)va; 398 va += PAGE_SIZE; 399 RELOC(CADDR2, void *) = (void *)va; 400 va += PAGE_SIZE; 401 RELOC(vmmap, void *) = (void *)va; 402 va += PAGE_SIZE; 403 RELOC(msgbufaddr, void *) = (void *)va; 404 va += m68k_round_page(MSGBUFSIZE); 405 RELOC(virtual_avail, vaddr_t) = va; 406 } 407 408 return nextpa; 409 } 410