1 /* $NetBSD: pmap_bootstrap.c,v 1.44 2026/11/15 15:16:23 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.44 2026/11/15 15:16:23 thorpej Exp $"); 40 41 #include <sys/param.h> 42 #include <uvm/uvm_extern.h> 43 44 #include <machine/cpu.h> 45 #include <machine/pte.h> 46 #include <machine/vmparam.h> 47 48 #define RELOC(v, t) *((t*)((uintptr_t)&(v) + firstpa - KERNBASE)) 49 50 extern char *etext; 51 extern paddr_t avail_start, avail_end; 52 extern vaddr_t kernel_reloc_offset; 53 54 /* 55 * Special purpose kernel virtual addresses, used for mapping 56 * physical pages for a variety of temporary or permanent purposes: 57 * 58 * CADDR1, CADDR2: pmap zero/copy operations 59 * msgbufaddr: kernel message buffer 60 */ 61 void *CADDR1, *CADDR2; 62 extern void *msgbufaddr; 63 64 paddr_t pmap_bootstrap1(paddr_t, paddr_t); 65 66 /* 67 * Bootstrap the VM system. 68 * 69 * Called with MMU off so we must relocate all global references by `firstpa' 70 * (don't call any functions here!) `nextpa' is the first available physical 71 * memory address. Returns an updated first PA reflecting the memory we 72 * have allocated. MMU is still off when we return. 73 * 74 * XXX assumes sizeof(u_int) == sizeof(pt_entry_t) 75 * XXX a PIC compiler would make this much easier. 76 */ 77 paddr_t __attribute__((no_instrument_function)) 78 pmap_bootstrap1(paddr_t nextpa, paddr_t firstpa) 79 { 80 paddr_t lwp0upa, kstpa, kptmpa, kptpa; 81 paddr_t lkptpa; 82 u_int nptpages, kstsize; 83 st_entry_t protoste, *ste, *este; 84 pt_entry_t protopte, *pte, *epte; 85 u_int stfree = 0; /* XXX: gcc -Wuninitialized */ 86 87 nextpa = m68k_round_page(nextpa); 88 89 /* 90 * Calculate important physical addresses: 91 * 92 * lwp0upa lwp0 u-area UPAGES pages 93 * 94 * kstpa kernel segment table 1 page (!040) 95 * N pages (040) 96 * 97 * kptmpa kernel PT map 1 page 98 * 99 * lkptpa last kernel PT page 1 page 100 * 101 * kptpa statically allocated 102 * kernel PT pages Sysptsize+ pages 103 * 104 * [ Sysptsize is the number of pages of PT, and IIOMAPSIZE and 105 * EIOMAPSIZE are the number of PTEs, hence we need to round 106 * the total to a page boundary with IO maps at the end. ] 107 * 108 * The KVA corresponding to any of these PAs is: 109 * (PA - firstpa + KERNBASE). 110 */ 111 RELOC(kernel_reloc_offset, vaddr_t) = firstpa; 112 113 lwp0upa = nextpa; 114 nextpa += USPACE; 115 if (RELOC(mmutype, int) == MMU_68040) 116 kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE); 117 else 118 kstsize = 1; 119 kstpa = nextpa; 120 nextpa += kstsize * PAGE_SIZE; 121 kptmpa = nextpa; 122 nextpa += PAGE_SIZE; 123 lkptpa = nextpa; 124 nextpa += PAGE_SIZE; 125 kptpa = nextpa; 126 nptpages = RELOC(Sysptsize, int) + 127 howmany(RELOC(physmem, psize_t), NPTEPG); 128 nextpa += nptpages * PAGE_SIZE; 129 130 /* 131 * Initialize segment table and kernel page table map. 132 * 133 * On 68030s and earlier MMUs the two are identical except for 134 * the valid bits so both are initialized with essentially the 135 * same values. On the 68040, which has a mandatory 3-level 136 * structure, the segment table holds the level 1 table and part 137 * (or all) of the level 2 table and hence is considerably 138 * different. Here the first level consists of 128 descriptors 139 * (512 bytes) each mapping 32mb of address space. Each of these 140 * points to blocks of 128 second level descriptors (512 bytes) 141 * each mapping 256kb. Note that there may be additional "segment 142 * table" pages depending on how large MAXKL2SIZE is. 143 * 144 * Portions of the last two segment of KVA space (0xFF800000 - 145 * 0xFFFFFFFF) are mapped for a couple of purposes. 146 * The first segment (0xFF800000 - 0xFFBFFFFF) is mapped 147 * for the kernel page tables. 148 * 149 * XXX: It looks this was copied from hp300 and not sure if 150 * XXX: last physical page mapping is really needed on this port. 151 * The very last page (0xFFFFF000) in the second segment is mapped 152 * to the last physical page of RAM to give us a region in which 153 * PA == VA. We use the first part of this page for enabling 154 * and disabling mapping. The last part of this page also contains 155 * info left by the boot ROM. 156 * 157 * XXX cramming two levels of mapping into the single "segment" 158 * table on the 68040 is intended as a temporary hack to get things 159 * working. The 224mb of address space that this allows will most 160 * likely be insufficient in the future (at least for the kernel). 161 */ 162 if (RELOC(mmutype, int) == MMU_68040) { 163 int nl1desc, nl2desc, i; 164 165 /* 166 * First invalidate the entire "segment table" pages 167 * (levels 1 and 2 have the same "invalid" value). 168 */ 169 ste = (st_entry_t *)kstpa; 170 este = &ste[kstsize * NPTEPG]; 171 while (ste < este) 172 *ste++ = SG_NV; 173 /* 174 * Initialize level 2 descriptors (which immediately 175 * follow the level 1 table). We need: 176 * NPTEPG / SG4_LEV3SIZE 177 * level 2 descriptors to map each of the nptpages 178 * pages of PTEs. Note that we set the "used" bit 179 * now to save the HW the expense of doing it. 180 */ 181 nl2desc = nptpages * (NPTEPG / SG4_LEV3SIZE); 182 ste = (st_entry_t *)kstpa; 183 ste = &ste[SG4_LEV1SIZE]; 184 este = &ste[nl2desc]; 185 protoste = kptpa | SG_U | SG_RW | SG_V; 186 while (ste < este) { 187 *ste++ = protoste; 188 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 189 } 190 /* 191 * Initialize level 1 descriptors. We need: 192 * howmany(nl2desc, SG4_LEV2SIZE) 193 * level 1 descriptors to map the `nl2desc' level 2's. 194 */ 195 nl1desc = howmany(nl2desc, SG4_LEV2SIZE); 196 ste = (st_entry_t *)kstpa; 197 este = &ste[nl1desc]; 198 protoste = (paddr_t)&ste[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V; 199 while (ste < este) { 200 *ste++ = protoste; 201 protoste += (SG4_LEV2SIZE * sizeof(st_entry_t)); 202 } 203 /* 204 * Initialize the final level 1 descriptor to map the next 205 * block of level 2 descriptors for Sysptmap. 206 */ 207 ste = (st_entry_t *)kstpa; 208 ste = &ste[SG4_LEV1SIZE - 1]; 209 *ste = protoste; 210 /* 211 * Now initialize the final portion of that block of 212 * descriptors to map Sysmap and the "last PT page". 213 */ 214 i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE); 215 ste = (st_entry_t *)kstpa; 216 ste = &ste[i + SG4_LEV2SIZE - (NPTEPG / SG4_LEV3SIZE) * 2]; 217 este = &ste[NPTEPG / SG4_LEV3SIZE]; 218 protoste = kptmpa | SG_U | SG_RW | SG_V; 219 while (ste < este) { 220 *ste++ = protoste; 221 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 222 } 223 este = &ste[NPTEPG / SG4_LEV3SIZE]; 224 protoste = lkptpa | 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 last ones to point to Sysptmap and the page 267 * table page allocated earlier. 268 */ 269 pte = (pt_entry_t *)kptmpa; 270 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 271 *pte = kptmpa | PG_RW | PG_CI | PG_V; 272 pte++; /* XXX should use [MAXADDR >> SEGSHIFT] */ 273 *pte = lkptpa | PG_RW | PG_CI | PG_V; 274 } else { 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 ones to point to Sysptmap and the page 303 * table page allocated earlier. 304 */ 305 ste = (st_entry_t *)kstpa; 306 ste = &ste[SYSMAP_VA >> SEGSHIFT]; 307 pte = (pt_entry_t *)kptmpa; 308 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 309 *ste = kptmpa | SG_RW | SG_V; 310 *pte = kptmpa | PG_RW | PG_CI | PG_V; 311 ste++; /* XXX should use [MAXADDR >> SEGSHIFT] */ 312 pte++; /* XXX should use [MAXADDR >> SEGSHIFT] */ 313 *ste = lkptpa | SG_RW | SG_V; 314 *pte = lkptpa | PG_RW | PG_CI | PG_V; 315 } 316 317 /* 318 * Invalidate all but the final entry in the last kernel PT page. 319 * The final entry maps the last page of physical memory to 320 * prepare a page that is PA == VA to turn on the MMU. 321 * 322 * XXX: This looks copied from hp300 where PA != VA, but 323 * XXX: it's suspicious if this is also required on this port. 324 */ 325 pte = (pt_entry_t *)lkptpa; 326 epte = &pte[NPTEPG]; 327 while (pte < epte) 328 *pte++ = PG_NV; 329 /* 330 * Initialize kernel page table. 331 * Start by invalidating the `nptpages' that we have allocated. 332 */ 333 pte = (pt_entry_t *)kptpa; 334 epte = &pte[nptpages * NPTEPG]; 335 while (pte < epte) 336 *pte++ = PG_NV; 337 /* 338 * Validate PTEs for kernel text (RO). 339 */ 340 pte = (pt_entry_t *)kptpa; 341 pte = &pte[m68k_btop(KERNBASE)]; 342 epte = &((u_int *)kptpa)[m68k_btop(m68k_trunc_page(&etext))]; 343 protopte = firstpa | PG_RO | PG_V; 344 while (pte < epte) { 345 *pte++ = protopte; 346 protopte += PAGE_SIZE; 347 } 348 /* 349 * Validate PTEs for kernel data/bss, dynamic data allocated 350 * by us so far (kstpa - firstpa bytes), and pages for lwp0 351 * u-area and page table allocated below (RW). 352 */ 353 epte = (pt_entry_t *)kptpa; 354 epte = &epte[m68k_btop(KERNBASE + kstpa - firstpa)]; 355 protopte = (protopte & ~PG_PROT) | PG_RW; 356 /* 357 * Enable copy-back caching of data pages 358 */ 359 if (RELOC(mmutype, int) == MMU_68040) 360 protopte |= PG_CCB; 361 while (pte < epte) { 362 *pte++ = protopte; 363 protopte += PAGE_SIZE; 364 } 365 /* 366 * Map the kernel segment table cache invalidated for 68040/68060. 367 * (for the 68040 not strictly necessary, but recommended by Motorola; 368 * for the 68060 mandatory) 369 */ 370 epte = (pt_entry_t *)kptpa; 371 epte = &epte[m68k_btop(KERNBASE + nextpa - firstpa)]; 372 protopte = (protopte & ~PG_PROT) | PG_RW; 373 if (RELOC(mmutype, int) == MMU_68040) { 374 protopte &= ~PG_CCB; 375 protopte |= PG_CIN; 376 } 377 while (pte < epte) { 378 *pte++ = protopte; 379 protopte += PAGE_SIZE; 380 } 381 382 /* 383 * Calculate important exported kernel addresses and related values. 384 */ 385 /* 386 * Sysseg: base of kernel segment table 387 */ 388 RELOC(Sysseg, st_entry_t *) = 389 (st_entry_t *)(kstpa - firstpa + KERNBASE); 390 RELOC(Sysseg_pa, paddr_t) = kstpa; 391 if (RELOC(mmutype, int) == MMU_68040) 392 RELOC(protostfree, u_int) = stfree; 393 /* 394 * Sysptmap: base of kernel page table map 395 */ 396 RELOC(Sysptmap, pt_entry_t *) = 397 (pt_entry_t *)(kptmpa - firstpa + KERNBASE); 398 /* 399 * Sysmap: kernel page table (as mapped through Sysptmap) 400 * Allocated at the end of KVA space. 401 */ 402 RELOC(Sysmap, pt_entry_t *) = (pt_entry_t *)SYSMAP_VA; 403 404 /* 405 * Remember the u-area address so it can be loaded in the lwp0 406 * via uvm_lwp_setuarea() later in pmap_bootstrap2(). 407 */ 408 RELOC(lwp0uarea, vaddr_t) = lwp0upa - firstpa + KERNBASE; 409 410 /* 411 * VM data structures are now initialized, set up data for 412 * the pmap module. 413 * 414 * Note about avail_end: msgbuf is initialized just after 415 * avail_end in machdep.c. 416 * Since the last page is used for rebooting the system 417 * (code is copied there and execution continues from copied code 418 * before the MMU is disabled), the msgbuf will get trounced 419 * between reboots if it's placed in the last physical page. 420 * To work around this, we move avail_end back one more 421 * page so the msgbuf can be preserved. 422 */ 423 RELOC(virtual_avail, vaddr_t) = 424 KERNBASE + (nextpa - firstpa); 425 RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS; 426 427 /* 428 * Allocate some fixed, special purpose kernel virtual addresses 429 */ 430 { 431 vaddr_t va = RELOC(virtual_avail, vaddr_t); 432 433 RELOC(CADDR1, void *) = (void *)va; 434 va += PAGE_SIZE; 435 RELOC(CADDR2, void *) = (void *)va; 436 va += PAGE_SIZE; 437 RELOC(cpu_info_primary.ci_tmpva_vmmap, vaddr_t) = va; 438 va += PAGE_SIZE; 439 RELOC(msgbufaddr, void *) = (void *)va; 440 va += m68k_round_page(MSGBUFSIZE); 441 RELOC(virtual_avail, vaddr_t) = va; 442 } 443 444 return nextpa; 445 } 446