1 /* $NetBSD: pmap_bootstrap.c,v 1.52 2026/04/05 14:35:49 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 * news68k/pmap_bootstrap.c - from hp300 and mvme68k 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: pmap_bootstrap.c,v 1.52 2026/04/05 14:35:49 thorpej Exp $"); 43 44 #include "opt_m68k_arch.h" 45 #include "opt_newsconf.h" 46 47 #include <sys/param.h> 48 #include <uvm/uvm_extern.h> 49 50 #include <machine/cpu.h> 51 #include <machine/pte.h> 52 #include <machine/vmparam.h> 53 54 #define RELOC(v, t) *((t*)((uintptr_t)&(v) + firstpa)) 55 56 extern char *etext; 57 58 extern vaddr_t kernel_reloc_offset; 59 60 /* 61 * Special purpose kernel virtual addresses, used for mapping 62 * physical pages for a variety of temporary or permanent purposes: 63 * 64 * CADDR1, CADDR2: pmap zero/copy operations 65 * vmmap: /dev/mem, crash dumps, parity error checking 66 * msgbufaddr: kernel message buffer 67 */ 68 void *CADDR1, *CADDR2; 69 char *vmmap; 70 extern void *msgbufaddr; 71 72 paddr_t pmap_bootstrap1(paddr_t, paddr_t); 73 74 /* 75 * Bootstrap the VM system. 76 * 77 * Called with MMU off so we must relocate all global references by `firstpa' 78 * (don't call any functions here!) `nextpa' is the first available physical 79 * memory address. Returns an updated first PA reflecting the memory we 80 * have allocated. MMU is still off when we return. 81 * 82 * XXX assumes sizeof(u_int) == sizeof(pt_entry_t) 83 * XXX a PIC compiler would make this much easier. 84 */ 85 paddr_t __attribute__((no_instrument_function)) 86 pmap_bootstrap1(paddr_t nextpa, paddr_t firstpa) 87 { 88 paddr_t lwp0upa, kstpa, kptmpa, kptpa; 89 u_int nptpages, kstsize; 90 st_entry_t protoste, *ste, *este; 91 pt_entry_t protopte, *pte, *epte; 92 #ifdef M68040 93 u_int stfree = 0; /* XXX: gcc -Wuninitialized */ 94 #endif 95 96 nextpa = m68k_round_page(nextpa); 97 98 /* 99 * Calculate important physical addresses: 100 * 101 * lwp0upa lwp0 u-area UPAGES pages 102 * 103 * kstpa kernel segment table 1 page (!040) 104 * N pages (040) 105 * 106 * kptmpa kernel PT map 1 page 107 * 108 * kptpa statically allocated 109 * kernel PT pages Sysptsize+ pages 110 * 111 * The KVA corresponding to any of these PAs is: 112 * (PA - firstpa + KERNBASE). 113 */ 114 RELOC(kernel_reloc_offset, vaddr_t) = firstpa; 115 116 lwp0upa = nextpa; 117 nextpa += USPACE; 118 #ifdef M68040 119 if (RELOC(mmutype, int) == MMU_68040) 120 kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE); 121 else 122 #endif 123 kstsize = 1; 124 kstpa = nextpa; 125 nextpa += kstsize * PAGE_SIZE; 126 kptmpa = nextpa; 127 nextpa += PAGE_SIZE; 128 kptpa = nextpa; 129 nptpages = RELOC(Sysptsize, int) + 130 howmany(RELOC(physmem, psize_t), NPTEPG); 131 nextpa += nptpages * PAGE_SIZE; 132 133 /* 134 * Clear all PTEs to zero 135 */ 136 for (pte = (pt_entry_t *)kstpa; pte < (pt_entry_t *)nextpa; pte++) 137 *pte = 0; 138 139 /* 140 * Initialize segment table and kernel page table map. 141 * 142 * On 68030s and earlier MMUs the two are identical except for 143 * the valid bits so both are initialized with essentially the 144 * same values. On the 68040, which has a mandatory 3-level 145 * structure, the segment table holds the level 1 table and part 146 * (or all) of the level 2 table and hence is considerably 147 * different. Here the first level consists of 128 descriptors 148 * (512 bytes) each mapping 32mb of address space. Each of these 149 * points to blocks of 128 second level descriptors (512 bytes) 150 * each mapping 256kb. Note that there may be additional "segment 151 * table" pages depending on how large MAXKL2SIZE is. 152 * 153 * Portions of the last segment of KVA space (0xBFC00000 - 154 * 0xBFFFFFFF) are mapped for the kernel page tables. 155 * 156 * The region 0xC0000000 - 0xCFFFFFFF is mapped via the %tt1 register 157 * for RAM accesses for PROM. 158 * The region 0xE0000000 - 0xFFFFFFFF is mapped via the %tt0 register 159 * for I/O accesses. 160 * 161 * XXX cramming two levels of mapping into the single "segment" 162 * table on the 68040 is intended as a temporary hack to get things 163 * working. The 224mb of address space that this allows will most 164 * likely be insufficient in the future (at least for the kernel). 165 */ 166 #ifdef M68040 167 if (RELOC(mmutype, int) == MMU_68040) { 168 int nl1desc, nl2desc, i; 169 170 /* 171 * First invalidate the entire "segment table" pages 172 * (levels 1 and 2 have the same "invalid" value). 173 */ 174 ste = (st_entry_t *)kstpa; 175 este = &ste[kstsize * NPTEPG]; 176 while (ste < este) 177 *ste++ = SG_NV; 178 /* 179 * Initialize level 2 descriptors (which immediately 180 * follow the level 1 table). We need: 181 * NPTEPG / SG4_LEV3SIZE 182 * level 2 descriptors to map each of the nptpages 183 * pages of PTEs. Note that we set the "used" bit 184 * now to save the HW the expense of doing it. 185 */ 186 nl2desc = nptpages * (NPTEPG / SG4_LEV3SIZE); 187 ste = (st_entry_t *)kstpa; 188 ste = &ste[SG4_LEV1SIZE]; 189 este = &ste[nl2desc]; 190 protoste = kptpa | SG_U | SG_RW | SG_V; 191 while (ste < este) { 192 *ste++ = protoste; 193 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 194 } 195 /* 196 * Initialize level 1 descriptors. We need: 197 * howmany(nl2desc, SG4_LEV2SIZE) 198 * level 1 descriptors to map the `nl2desc' level 2's. 199 */ 200 nl1desc = howmany(nl2desc, SG4_LEV2SIZE); 201 ste = (st_entry_t *)kstpa; 202 este = &ste[nl1desc]; 203 protoste = (paddr_t)&ste[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V; 204 while (ste < este) { 205 *ste++ = protoste; 206 protoste += (SG4_LEV2SIZE * sizeof(st_entry_t)); 207 } 208 /* 209 * Initialize the level 1 descriptor correspond to 210 * SYSMAP_VA to map the last block of level 2 descriptors 211 * for Sysptmap. 212 */ 213 ste = (st_entry_t *)kstpa; 214 ste = &ste[SYSMAP_VA >> SG4_SHIFT1]; 215 *ste = protoste; 216 /* 217 * Now initialize the portion of that block of 218 * descriptors to map Sysptmap. 219 */ 220 i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE); 221 ste = (st_entry_t *)kstpa; 222 ste = &ste[i + ((SYSMAP_VA & SG4_MASK2) >> SG4_SHIFT2)]; 223 este = &ste[NPTEPG / SG4_LEV3SIZE]; 224 protoste = kptmpa | SG_U | SG_RW | SG_V; 225 while (ste < este) { 226 *ste++ = protoste; 227 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 228 } 229 /* 230 * Calculate the free level 2 descriptor mask 231 * noting that we have used: 232 * 0: level 1 table 233 * 1 to nl1desc: map page tables 234 * nl1desc + 1: maps kptmpa and last-page page table 235 */ 236 /* mark an entry for level 1 table */ 237 stfree = ~l2tobm(0); 238 /* mark entries for map page tables */ 239 for (i = 1; i <= nl1desc; i++) 240 stfree &= ~l2tobm(i); 241 /* mark an entry for kptmpa and lkptpa */ 242 stfree &= ~l2tobm(i); 243 /* mark entries not available */ 244 for (i = MAXKL2SIZE; i < sizeof(stfree) * NBBY; i++) 245 stfree &= ~l2tobm(i); 246 247 /* 248 * Initialize Sysptmap 249 */ 250 pte = (pt_entry_t *)kptmpa; 251 epte = &pte[nptpages]; 252 protopte = kptpa | PG_RW | PG_CI | PG_V; 253 while (pte < epte) { 254 *pte++ = protopte; 255 protopte += PAGE_SIZE; 256 } 257 /* 258 * Invalidate all remaining entries. 259 */ 260 epte = (pt_entry_t *)kptmpa; 261 epte = &epte[TIB_SIZE]; 262 while (pte < epte) { 263 *pte++ = PG_NV; 264 } 265 /* 266 * Initialize the one corresponding to SYSMAP_VA 267 * to point to Sysptmap. 268 */ 269 pte = (pt_entry_t *)kptmpa; 270 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 271 *pte = kptmpa | PG_RW | PG_CI | PG_V; 272 } else 273 #endif 274 { 275 /* 276 * Map the page table pages in both the HW segment table 277 * and the software Sysptmap. 278 */ 279 ste = (st_entry_t *)kstpa; 280 pte = (pt_entry_t *)kptmpa; 281 epte = &pte[nptpages]; 282 protoste = kptpa | SG_RW | SG_V; 283 protopte = kptpa | PG_RW | PG_CI | PG_V; 284 while (pte < epte) { 285 *ste++ = protoste; 286 *pte++ = protopte; 287 protoste += PAGE_SIZE; 288 protopte += PAGE_SIZE; 289 } 290 /* 291 * Invalidate all remaining entries in both. 292 */ 293 este = (st_entry_t *)kstpa; 294 este = &este[TIA_SIZE]; 295 while (ste < este) 296 *ste++ = SG_NV; 297 epte = (pt_entry_t *)kptmpa; 298 epte = &epte[TIB_SIZE]; 299 while (pte < epte) 300 *pte++ = PG_NV; 301 /* 302 * Initialize the last one to point to Sysptmap. 303 */ 304 ste = (st_entry_t *)kstpa; 305 ste = &ste[SYSMAP_VA >> SEGSHIFT]; 306 pte = (pt_entry_t *)kptmpa; 307 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 308 *ste = kptmpa | SG_RW | SG_V; 309 *pte = kptmpa | PG_RW | PG_CI | PG_V; 310 } 311 312 /* 313 * Initialize kernel page table. 314 * Start by invalidating the `nptpages' that we have allocated. 315 */ 316 pte = (pt_entry_t *)kptpa; 317 epte = &pte[nptpages * NPTEPG]; 318 while (pte < epte) 319 *pte++ = PG_NV; 320 /* 321 * Validate PTEs for kernel text (RO). 322 */ 323 pte = (pt_entry_t *)kptpa; 324 pte = &pte[m68k_btop(KERNBASE)]; 325 epte = &pte[m68k_btop(m68k_trunc_page(&etext))]; 326 protopte = firstpa | PG_RO | PG_V; 327 while (pte < epte) { 328 *pte++ = protopte; 329 protopte += PAGE_SIZE; 330 } 331 /* 332 * Validate PTEs for kernel data/bss, dynamic data allocated 333 * by us so far (kstpa - firstpa bytes), and pages for lwp0 334 * u-area and page table allocated below (RW). 335 */ 336 epte = (pt_entry_t *)kptpa; 337 epte = &epte[m68k_btop(kstpa - firstpa)]; 338 protopte = (protopte & ~PG_PROT) | PG_RW; 339 /* 340 * Enable copy-back caching of data pages 341 */ 342 #ifdef M68040 343 if (RELOC(mmutype, int) == MMU_68040) 344 protopte |= PG_CCB; 345 #endif 346 while (pte < epte) { 347 *pte++ = protopte; 348 protopte += PAGE_SIZE; 349 } 350 /* 351 * Map the kernel segment table cache invalidated for 68040/68060. 352 * (for the 68040 not strictly necessary, but recommended by Motorola; 353 * for the 68060 mandatory) 354 */ 355 epte = (pt_entry_t *)kptpa; 356 epte = &epte[m68k_btop(nextpa - firstpa)]; 357 protopte = (protopte & ~PG_PROT) | PG_RW; 358 #ifdef M68040 359 if (RELOC(mmutype, int) == MMU_68040) { 360 protopte &= ~PG_CCB; 361 protopte |= PG_CIN; 362 } 363 #endif 364 while (pte < epte) { 365 *pte++ = protopte; 366 protopte += PAGE_SIZE; 367 } 368 369 /* 370 * Finally, validate the internal IO space PTEs (RW+CI). 371 */ 372 373 #define PTE2VA(pte) m68k_ptob(pte - ((pt_entry_t *)kptpa)) 374 375 RELOC(virtual_avail, vaddr_t) = PTE2VA(pte); 376 377 /* 378 * Calculate important exported kernel addresses and related values. 379 */ 380 /* 381 * Sysseg: base of kernel segment table 382 */ 383 RELOC(Sysseg, st_entry_t *) = (st_entry_t *)(kstpa - firstpa); 384 RELOC(Sysseg_pa, paddr_t) = kstpa; 385 #ifdef M68040 386 if (RELOC(mmutype, int) == MMU_68040) 387 RELOC(protostfree, u_int) = stfree; 388 #endif 389 /* 390 * Sysptmap: base of kernel page table map 391 */ 392 RELOC(Sysptmap, pt_entry_t *) = (pt_entry_t *)(kptmpa - firstpa); 393 /* 394 * Sysmap: kernel page table (as mapped through Sysptmap) 395 * Allocated at the end of KVA space. 396 */ 397 RELOC(Sysmap, pt_entry_t *) = (pt_entry_t *)SYSMAP_VA; 398 399 /* 400 * Remember the u-area address so it can be loaded in the lwp0 401 * via uvm_lwp_setuarea() later in pmap_bootstrap2(). 402 */ 403 RELOC(lwp0uarea, vaddr_t) = lwp0upa - firstpa; 404 405 RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS; 406 407 /* 408 * Allocate some fixed, special purpose kernel virtual addresses 409 */ 410 { 411 vaddr_t va = RELOC(virtual_avail, vaddr_t); 412 413 RELOC(CADDR1, void *) = (void *)va; 414 va += PAGE_SIZE; 415 RELOC(CADDR2, void *) = (void *)va; 416 va += PAGE_SIZE; 417 RELOC(vmmap, void *) = (void *)va; 418 va += PAGE_SIZE; 419 RELOC(msgbufaddr, void *) = (void *)va; 420 va += m68k_round_page(MSGBUFSIZE); 421 RELOC(virtual_avail, vaddr_t) = va; 422 } 423 424 return nextpa; 425 } 426