1 /* $NetBSD: pmap_bootstrap.c,v 1.73 2026/11/15 15:16:25 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.73 2026/11/15 15:16:25 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 #include <arch/x68k/x68k/iodevice.h> 50 51 #define RELOC(v, t) *((t*)((uintptr_t)&(v) + firstpa)) 52 53 extern char *etext; 54 55 extern psize_t physmem; 56 extern vaddr_t kernel_reloc_offset; 57 58 /* 59 * Special purpose kernel virtual addresses, used for mapping 60 * physical pages for a variety of temporary or permanent purposes: 61 * 62 * CADDR1, CADDR2: pmap zero/copy operations 63 * msgbufaddr: kernel message buffer 64 */ 65 void *CADDR1, *CADDR2; 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) || defined(M68060) 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 * [ Sysptsize is the number of pages of PT, and IIOMAPSIZE 108 * is the number of PTEs, hence we need to round 109 * the total to a page boundary with IO maps at the end. ] 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 if (RELOC(mmutype, int) == MMU_68040) 119 kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE); 120 else 121 kstsize = 1; 122 kstpa = nextpa; 123 nextpa += kstsize * PAGE_SIZE; 124 kptmpa = nextpa; 125 nextpa += PAGE_SIZE; 126 kptpa = nextpa; 127 nptpages = RELOC(Sysptsize, int) + 128 howmany(RELOC(physmem, psize_t), NPTEPG) + 129 (IIOMAPSIZE + NPTEPG - 1) / NPTEPG; 130 nextpa += nptpages * PAGE_SIZE; 131 132 /* 133 * Clear all PTEs to zero 134 */ 135 for (pte = (pt_entry_t *)kstpa; pte < (pt_entry_t *)nextpa; pte++) 136 *pte = 0; 137 138 /* 139 * Initialize segment table and kernel page table map. 140 * 141 * On 68030s and earlier MMUs the two are identical except for 142 * the valid bits so both are initialized with essentially the 143 * same values. On the 68040, which has a mandatory 3-level 144 * structure, the segment table holds the level 1 table and part 145 * (or all) of the level 2 table and hence is considerably 146 * different. Here the first level consists of 128 descriptors 147 * (512 bytes) each mapping 32mb of address space. Each of these 148 * points to blocks of 128 second level descriptors (512 bytes) 149 * each mapping 256kb. Note that there may be additional "segment 150 * table" pages depending on how large MAXKL2SIZE is. 151 * 152 * Portions of the last segment of KVA space (0xFFC00000 - 153 * 0xFFFFFFFF) are mapped for the kernel page tables. 154 * 155 * XXX cramming two levels of mapping into the single "segment" 156 * table on the 68040 is intended as a temporary hack to get things 157 * working. The 224mb of address space that this allows will most 158 * likely be insufficient in the future (at least for the kernel). 159 */ 160 #if defined(M68040) || defined(M68060) 161 if (RELOC(mmutype, int) == MMU_68040) { 162 int nl1desc, nl2desc, i; 163 164 /* 165 * First invalidate the entire "segment table" pages 166 * (levels 1 and 2 have the same "invalid" value). 167 */ 168 ste = (st_entry_t *)kstpa; 169 este = &ste[kstsize * NPTEPG]; 170 while (ste < este) 171 *ste++ = SG_NV; 172 /* 173 * Initialize level 2 descriptors (which immediately 174 * follow the level 1 table). We need: 175 * NPTEPG / SG4_LEV3SIZE 176 * level 2 descriptors to map each of the nptpages 177 * pages of PTEs. Note that we set the "used" bit 178 * now to save the HW the expense of doing it. 179 */ 180 nl2desc = nptpages * (NPTEPG / SG4_LEV3SIZE); 181 ste = (st_entry_t *)kstpa; 182 ste = &ste[SG4_LEV1SIZE]; 183 este = &ste[nl2desc]; 184 protoste = kptpa | SG_U | SG_RW | SG_V; 185 while (ste < este) { 186 *ste++ = protoste; 187 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 188 } 189 /* 190 * Initialize level 1 descriptors. We need: 191 * howmany(nl2desc, SG4_LEV2SIZE) 192 * level 1 descriptors to map the `nl2desc' level 2's. 193 */ 194 nl1desc = howmany(nl2desc, SG4_LEV2SIZE); 195 ste = (st_entry_t *)kstpa; 196 este = &ste[nl1desc]; 197 protoste = (paddr_t)&ste[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V; 198 while (ste < este) { 199 *ste++ = protoste; 200 protoste += (SG4_LEV2SIZE * sizeof(st_entry_t)); 201 } 202 /* 203 * Initialize the final level 1 descriptor to map the next 204 * block of level 2 descriptors for Sysptmap. 205 */ 206 ste = (st_entry_t *)kstpa; 207 ste = &ste[SG4_LEV1SIZE - 1]; 208 *ste = protoste; 209 /* 210 * Now initialize the final portion of that block of 211 * descriptors to map Sysmap. 212 */ 213 i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE); 214 ste = (st_entry_t *)kstpa; 215 ste = &ste[i + SG4_LEV2SIZE - (NPTEPG / SG4_LEV3SIZE)]; 216 este = &ste[NPTEPG / SG4_LEV3SIZE]; 217 protoste = kptmpa | SG_U | SG_RW | SG_V; 218 while (ste < este) { 219 *ste++ = protoste; 220 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 221 } 222 /* 223 * Calculate the free level 2 descriptor mask 224 * noting that we have used: 225 * 0: level 1 table 226 * 1 to nl1desc: map page tables 227 * nl1desc + 1: maps kptmpa and last-page page table 228 */ 229 /* mark an entry for level 1 table */ 230 stfree = ~l2tobm(0); 231 /* mark entries for map page tables */ 232 for (i = 1; i <= nl1desc; i++) 233 stfree &= ~l2tobm(i); 234 /* mark an entry for kptmpa */ 235 stfree &= ~l2tobm(i); 236 /* mark entries not available */ 237 for (i = MAXKL2SIZE; i < sizeof(stfree) * NBBY; i++) 238 stfree &= ~l2tobm(i); 239 240 /* 241 * Initialize Sysptmap 242 */ 243 pte = (pt_entry_t *)kptmpa; 244 epte = &pte[nptpages]; 245 protopte = kptpa | PG_RW | PG_CI | PG_V; 246 while (pte < epte) { 247 *pte++ = protopte; 248 protopte += PAGE_SIZE; 249 } 250 /* 251 * Invalidate all remaining entries. 252 */ 253 epte = (pt_entry_t *)kptmpa; 254 epte = &epte[TIB_SIZE]; 255 while (pte < epte) { 256 *pte++ = PG_NV; 257 } 258 /* 259 * Initialize the last one to point to Sysptmap. 260 */ 261 pte = (pt_entry_t *)kptmpa; 262 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 263 *pte = kptmpa | PG_RW | PG_CI | PG_V; 264 } else 265 #endif /* M68040 || M68060 */ 266 { 267 /* 268 * Map the page table pages in both the HW segment table 269 * and the software Sysptmap. 270 */ 271 ste = (st_entry_t *)kstpa; 272 pte = (pt_entry_t *)kptmpa; 273 epte = &pte[nptpages]; 274 protoste = kptpa | SG_RW | SG_V; 275 protopte = kptpa | PG_RW | PG_CI | PG_V; 276 while (pte < epte) { 277 *ste++ = protoste; 278 *pte++ = protopte; 279 protoste += PAGE_SIZE; 280 protopte += PAGE_SIZE; 281 } 282 /* 283 * Invalidate all remaining entries in both. 284 */ 285 este = (st_entry_t *)kstpa; 286 este = &este[TIA_SIZE]; 287 while (ste < este) 288 *ste++ = SG_NV; 289 epte = (pt_entry_t *)kptmpa; 290 epte = &epte[TIB_SIZE]; 291 while (pte < epte) 292 *pte++ = PG_NV; 293 /* 294 * Initialize the last one to point to Sysptmap. 295 */ 296 ste = (st_entry_t *)kstpa; 297 ste = &ste[SYSMAP_VA >> SEGSHIFT]; 298 pte = (pt_entry_t *)kptmpa; 299 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 300 *ste = kptmpa | SG_RW | SG_V; 301 *pte = kptmpa | PG_RW | PG_CI | PG_V; 302 } 303 304 /* 305 * Initialize kernel page table. 306 * Start by invalidating the `nptpages' that we have allocated. 307 */ 308 pte = (pt_entry_t *)kptpa; 309 epte = &pte[nptpages * NPTEPG]; 310 while (pte < epte) 311 *pte++ = PG_NV; 312 /* 313 * Validate PTEs for kernel text (RO). 314 */ 315 pte = (pt_entry_t *)kptpa; 316 pte = &pte[m68k_btop(KERNBASE)]; 317 epte = &pte[m68k_btop(m68k_trunc_page(&etext))]; 318 protopte = firstpa | PG_RO | PG_V; 319 while (pte < epte) { 320 *pte++ = protopte; 321 protopte += PAGE_SIZE; 322 } 323 /* 324 * Validate PTEs for kernel data/bss, dynamic data allocated 325 * by us so far (kstpa - firstpa bytes), and pages for lwp0 326 * u-area and page table allocated below (RW). 327 */ 328 epte = (pt_entry_t *)kptpa; 329 epte = &epte[m68k_btop(kstpa - firstpa)]; 330 protopte = (protopte & ~PG_PROT) | PG_RW; 331 /* 332 * Enable copy-back caching of data pages 333 */ 334 if (RELOC(mmutype, int) == MMU_68040) 335 protopte |= PG_CCB; 336 while (pte < epte) { 337 *pte++ = protopte; 338 protopte += PAGE_SIZE; 339 } 340 /* 341 * Map the kernel segment table cache invalidated for 68040/68060. 342 * (for the 68040 not strictly necessary, but recommended by Motorola; 343 * for the 68060 mandatory) 344 */ 345 epte = (pt_entry_t *)kptpa; 346 epte = &epte[m68k_btop(nextpa - firstpa)]; 347 protopte = (protopte & ~PG_PROT) | PG_RW; 348 if (RELOC(mmutype, int) == MMU_68040) { 349 protopte &= ~PG_CCB; 350 protopte |= PG_CIN; 351 } 352 while (pte < epte) { 353 *pte++ = protopte; 354 protopte += PAGE_SIZE; 355 } 356 357 /* 358 * Finally, validate the internal IO space PTEs (RW+CI). 359 */ 360 361 #define PTE2VA(pte) m68k_ptob(pte - ((pt_entry_t *)kptpa)) 362 363 protopte = INTIOBASE | PG_RW | PG_CI | PG_V; 364 epte = &pte[IIOMAPSIZE]; 365 RELOC(intiobase, uint8_t *) = (uint8_t *)PTE2VA(pte); 366 while (pte < epte) { 367 *pte++ = protopte; 368 protopte += PAGE_SIZE; 369 } 370 RELOC(virtual_avail, vaddr_t) = PTE2VA(pte); 371 372 /* 373 * Calculate important exported kernel addresses and related values. 374 */ 375 /* 376 * Sysseg: base of kernel segment table 377 */ 378 RELOC(Sysseg, st_entry_t *) = (st_entry_t *)(kstpa - firstpa); 379 RELOC(Sysseg_pa, paddr_t) = kstpa; 380 #if defined(M68040) || defined(M68060) 381 if (RELOC(mmutype, int) == MMU_68040) 382 RELOC(protostfree, u_int) = stfree; 383 #endif 384 /* 385 * Sysptmap: base of kernel page table map 386 */ 387 RELOC(Sysptmap, pt_entry_t *) = (pt_entry_t *)(kptmpa - firstpa); 388 /* 389 * Sysmap: kernel page table (as mapped through Sysptmap) 390 * Allocated at the end of KVA space. 391 */ 392 RELOC(Sysmap, pt_entry_t *) = (pt_entry_t *)SYSMAP_VA; 393 394 /* 395 * Remember the u-area address so it can be loaded in the lwp0 396 * via uvm_lwp_setuarea() later in pmap_bootstrap2(). 397 */ 398 RELOC(lwp0uarea, vaddr_t) = lwp0upa - firstpa; 399 400 RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS; 401 402 /* 403 * Allocate some fixed, special purpose kernel virtual addresses 404 */ 405 { 406 vaddr_t va = RELOC(virtual_avail, vaddr_t); 407 408 RELOC(CADDR1, void *) = (void *)va; 409 va += PAGE_SIZE; 410 RELOC(CADDR2, void *) = (void *)va; 411 va += PAGE_SIZE; 412 RELOC(cpu_info_primary.ci_tmpva_vmmap, vaddr_t) = va; 413 va += PAGE_SIZE; 414 RELOC(msgbufaddr, void *) = (void *)va; 415 va += m68k_round_page(MSGBUFSIZE); 416 RELOC(virtual_avail, vaddr_t) = va; 417 } 418 419 return nextpa; 420 } 421