1 /* $NetBSD: pmap_bootstrap.c,v 1.18 2002/11/05 07:41:33 chs 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. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)pmap_bootstrap.c 8.1 (Berkeley) 6/10/93 40 */ 41 42 #include <sys/param.h> 43 #include <sys/kcore.h> 44 #include <machine/kcore.h> 45 #include <machine/pte.h> 46 #include <machine/vmparam.h> 47 #include <machine/cpu.h> 48 49 #include <mvme68k/mvme68k/seglist.h> 50 51 #include <uvm/uvm_extern.h> 52 53 #define RELOC(v, t) *((t*)((u_int)&(v) + firstpa)) 54 55 extern char *kernel_text, *etext; 56 extern int Sysptsize; 57 extern char *proc0paddr; 58 extern st_entry_t *Sysseg; 59 extern pt_entry_t *Sysptmap, *Sysmap; 60 61 extern int maxmem, physmem; 62 extern paddr_t avail_start, avail_end; 63 extern vaddr_t virtual_avail, virtual_end; 64 extern vsize_t mem_size; 65 extern phys_ram_seg_t mem_clusters[]; 66 extern int mem_cluster_cnt; 67 extern paddr_t msgbufpa; 68 extern int protection_codes[]; 69 70 /* 71 * Special purpose kernel virtual addresses, used for mapping 72 * physical pages for a variety of temporary or permanent purposes: 73 * 74 * CADDR1, CADDR2: pmap zero/copy operations 75 * vmmap: /dev/mem, crash dumps, parity error checking 76 * msgbufaddr: kernel message buffer 77 */ 78 caddr_t CADDR1, CADDR2, vmmap; 79 extern caddr_t msgbufaddr; 80 81 void pmap_bootstrap __P((paddr_t, paddr_t)); 82 83 /* 84 * Bootstrap the VM system. 85 * 86 * Called with MMU off so we must relocate all global references by `firstpa' 87 * (don't call any functions here!) `nextpa' is the first available physical 88 * memory address. Returns an updated first PA reflecting the memory we 89 * have allocated. MMU is still off when we return. 90 * 91 * XXX assumes sizeof(u_int) == sizeof(pt_entry_t) 92 * XXX a PIC compiler would make this much easier. 93 */ 94 void 95 pmap_bootstrap(nextpa, firstpa) 96 paddr_t nextpa; 97 paddr_t firstpa; 98 { 99 paddr_t kstpa, kptpa, iiopa, kptmpa, lkptpa, p0upa; 100 u_int nptpages, kstsize; 101 st_entry_t protoste, *ste; 102 pt_entry_t protopte, *pte, *epte; 103 psize_t size; 104 u_int iiomappages; 105 int i; 106 107 /* 108 * Calculate important physical addresses: 109 * 110 * kstpa kernel segment table 1 page (!040) 111 * N pages (040) 112 * 113 * kptpa statically allocated 114 * kernel PT pages Sysptsize+ pages 115 * 116 * iiopa internal IO space 117 * PT pages iiomappages pages 118 * 119 * [ Sysptsize is the number of pages of PT, iiomappages is the 120 * number of PTEs, hence we need to round the total to a page 121 * boundary with IO maps at the end. ] 122 * 123 * kptmpa kernel PT map 1 page 124 * 125 * lkptpa last kernel PT page 1 page 126 * 127 * p0upa proc 0 u-area UPAGES pages 128 * 129 * The KVA corresponding to any of these PAs is: 130 * (PA - firstpa + KERNBASE). 131 */ 132 iiomappages = m68k_btop(RELOC(intiotop_phys, u_int) - 133 RELOC(intiobase_phys, u_int)); 134 135 #if defined(M68040) || defined(M68060) 136 if (RELOC(mmutype, int) == MMU_68040) 137 kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE); 138 else 139 #endif 140 kstsize = 1; 141 kstpa = nextpa; 142 nextpa += kstsize * NBPG; 143 kptpa = nextpa; 144 nptpages = RELOC(Sysptsize, int) + 145 (iiomappages + NPTEPG - 1) / NPTEPG; 146 nextpa += nptpages * NBPG; 147 iiopa = nextpa - iiomappages * sizeof(pt_entry_t); 148 kptmpa = nextpa; 149 nextpa += NBPG; 150 lkptpa = nextpa; 151 nextpa += NBPG; 152 p0upa = nextpa; 153 nextpa += USPACE; 154 155 /* 156 * Clear all PTEs to zero 157 */ 158 for (pte = (pt_entry_t *)kstpa; pte < (pt_entry_t *)nextpa; pte++) 159 *pte = 0; 160 161 /* 162 * Initialize segment table and kernel page table map. 163 * 164 * On 68030s and earlier MMUs the two are identical except for 165 * the valid bits so both are initialized with essentially the 166 * same values. On the 68040, which has a mandatory 3-level 167 * structure, the segment table holds the level 1 table and part 168 * (or all) of the level 2 table and hence is considerably 169 * different. Here the first level consists of 128 descriptors 170 * (512 bytes) each mapping 32mb of address space. Each of these 171 * points to blocks of 128 second level descriptors (512 bytes) 172 * each mapping 256kb. Note that there may be additional "segment 173 * table" pages depending on how large MAXKL2SIZE is. 174 * 175 * Portions of the last segment of KVA space (0xFFF00000 - 176 * 0xFFFFFFFF) are mapped for a couple of purposes. 0xFFF00000 177 * for UPAGES is used for mapping the current process u-area 178 * (u + kernel stack). The very last page (0xFFFFF000) is mapped 179 * to the last physical page of RAM to give us a region in which 180 * PA == VA. We use the first part of this page for enabling 181 * and disabling mapping. The last part of this page also contains 182 * info left by the boot ROM. 183 * 184 * XXX cramming two levels of mapping into the single "segment" 185 * table on the 68040 is intended as a temporary hack to get things 186 * working. The 224mb of address space that this allows will most 187 * likely be insufficient in the future (at least for the kernel). 188 */ 189 #if defined(M68040) || defined(M68060) 190 if (RELOC(mmutype, int) == MMU_68040) { 191 int num; 192 193 /* 194 * First invalidate the entire "segment table" pages 195 * (levels 1 and 2 have the same "invalid" value). 196 */ 197 pte = (u_int *)kstpa; 198 epte = &pte[kstsize * NPTEPG]; 199 while (pte < epte) 200 *pte++ = SG_NV; 201 /* 202 * Initialize level 2 descriptors (which immediately 203 * follow the level 1 table). We need: 204 * NPTEPG / SG4_LEV3SIZE 205 * level 2 descriptors to map each of the nptpages+1 206 * pages of PTEs. Note that we set the "used" bit 207 * now to save the HW the expense of doing it. 208 */ 209 num = (nptpages + 1) * (NPTEPG / SG4_LEV3SIZE); 210 pte = &((u_int *)kstpa)[SG4_LEV1SIZE]; 211 epte = &pte[num]; 212 protoste = kptpa | SG_U | SG_RW | SG_V; 213 while (pte < epte) { 214 *pte++ = protoste; 215 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 216 } 217 /* 218 * Initialize level 1 descriptors. We need: 219 * roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE 220 * level 1 descriptors to map the `num' level 2's. 221 */ 222 pte = (u_int *)kstpa; 223 epte = &pte[roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE]; 224 protoste = (u_int)&pte[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V; 225 while (pte < epte) { 226 *pte++ = protoste; 227 protoste += (SG4_LEV2SIZE * sizeof(st_entry_t)); 228 } 229 /* 230 * Initialize the final level 1 descriptor to map the last 231 * block of level 2 descriptors. 232 */ 233 ste = &((u_int *)kstpa)[SG4_LEV1SIZE-1]; 234 pte = &((u_int *)kstpa)[kstsize*NPTEPG - SG4_LEV2SIZE]; 235 *ste = (u_int)pte | SG_U | SG_RW | SG_V; 236 /* 237 * Now initialize the final portion of that block of 238 * descriptors to map the "last PT page". 239 */ 240 pte = &((u_int *)kstpa)[kstsize*NPTEPG - NPTEPG/SG4_LEV3SIZE]; 241 epte = &pte[NPTEPG/SG4_LEV3SIZE]; 242 protoste = lkptpa | SG_U | SG_RW | SG_V; 243 while (pte < epte) { 244 *pte++ = protoste; 245 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 246 } 247 /* 248 * Initialize Sysptmap 249 */ 250 pte = (u_int *)kptmpa; 251 epte = &pte[nptpages+1]; 252 protopte = kptpa | PG_RW | PG_CI | PG_U | PG_V; 253 while (pte < epte) { 254 *pte++ = protopte; 255 protopte += NBPG; 256 } 257 /* 258 * Invalidate all but the last remaining entry. 259 */ 260 epte = &((u_int *)kptmpa)[NPTEPG-1]; 261 while (pte < epte) { 262 *pte++ = PG_NV; 263 } 264 *pte = lkptpa | PG_RW | PG_CI | PG_U | PG_V; 265 } else 266 #endif /* M68040 || M68060 */ 267 { 268 /* 269 * Map the page table pages in both the HW segment table 270 * and the software Sysptmap. Note that Sysptmap is also 271 * considered a PT page hence the +1. 272 */ 273 ste = (u_int *)kstpa; 274 pte = (u_int *)kptmpa; 275 epte = &pte[nptpages+1]; 276 protoste = kptpa | SG_RW | SG_V; 277 protopte = kptpa | PG_RW | PG_CI | PG_V; 278 while (pte < epte) { 279 *ste++ = protoste; 280 *pte++ = protopte; 281 protoste += NBPG; 282 protopte += NBPG; 283 } 284 /* 285 * Invalidate all but the last remaining entries in both. 286 */ 287 epte = &((u_int *)kptmpa)[NPTEPG-1]; 288 while (pte < epte) { 289 *ste++ = SG_NV; 290 *pte++ = PG_NV; 291 } 292 /* 293 * Initialize the last to point to point to the page 294 * table page allocated earlier. 295 */ 296 *ste = lkptpa | SG_RW | SG_V; 297 *pte = lkptpa | PG_RW | PG_CI | PG_V; 298 } 299 /* 300 * Invalidate all but the final entry in the last kernel PT page 301 * (u-area PTEs will be validated later). The final entry maps 302 * the last page of physical memory. 303 */ 304 pte = (u_int *)lkptpa; 305 epte = &pte[NPTEPG-1]; 306 while (pte < epte) 307 *pte++ = PG_NV; 308 309 /* 310 * Initialize kernel page table. 311 * Start by invalidating the `nptpages' that we have allocated. 312 */ 313 pte = (u_int *)kptpa; 314 epte = &pte[nptpages * NPTEPG]; 315 while (pte < epte) 316 *pte++ = PG_NV; 317 /* 318 * Validate PTEs for kernel text (RO) 319 */ 320 pte = &((u_int *)kptpa)[m68k_btop(KERNBASE)]; 321 epte = &pte[m68k_btop(m68k_trunc_page(&etext))]; 322 protopte = firstpa | PG_RO | PG_U | PG_V; 323 while (pte < epte) { 324 *pte++ = protopte; 325 protopte += NBPG; 326 } 327 /* 328 * Validate PTEs for kernel data/bss, dynamic data allocated 329 * by us so far (kstpa - firstpa bytes), and pages for proc0 330 * u-area and page table allocated below (RW). 331 */ 332 epte = &((u_int *)kptpa)[m68k_btop(kstpa - firstpa)]; 333 protopte = (protopte & ~PG_PROT) | PG_RW; 334 /* 335 * Enable copy-back caching of data pages 336 */ 337 if (RELOC(mmutype, int) == MMU_68040) 338 protopte |= PG_CCB; 339 while (pte < epte) { 340 *pte++ = protopte; 341 protopte += NBPG; 342 } 343 /* 344 * map the kernel segment table cache invalidated for 345 * these machines (for the 68040 not strictly necessary, but 346 * recommended by Motorola; for the 68060 mandatory) 347 */ 348 epte = &((u_int *)kptpa)[m68k_btop(nextpa - firstpa)]; 349 protopte = (protopte & ~PG_PROT) | PG_RW; 350 if (RELOC(mmutype, int) == MMU_68040) { 351 protopte &= ~PG_CMASK; 352 protopte |= PG_CI; 353 } 354 while (pte < epte) { 355 *pte++ = protopte; 356 protopte += NBPG; 357 } 358 /* 359 * Finally, validate the internal IO space PTEs (RW+CI). 360 */ 361 pte = (u_int *)iiopa; 362 epte = (u_int *)kptmpa; 363 protopte = RELOC(intiobase_phys, u_int) | PG_RW | PG_CI | PG_U | PG_V; 364 while (pte < epte) { 365 *pte++ = protopte; 366 protopte += NBPG; 367 } 368 369 /* 370 * Calculate important exported kernel virtual addresses 371 */ 372 /* 373 * Sysseg: base of kernel segment table 374 */ 375 RELOC(Sysseg, st_entry_t *) = 376 (st_entry_t *)(kstpa - firstpa); 377 /* 378 * Sysptmap: base of kernel page table map 379 */ 380 RELOC(Sysptmap, pt_entry_t *) = 381 (pt_entry_t *)(kptmpa - firstpa); 382 /* 383 * Sysmap: kernel page table (as mapped through Sysptmap) 384 * Immediately follows `nptpages' of static kernel page table. 385 */ 386 RELOC(Sysmap, pt_entry_t *) = 387 (pt_entry_t *)m68k_ptob(nptpages * NPTEPG); 388 /* 389 * intiobase, intiolimit: base and end of internal IO space. 390 * iiomappages pages prior to VMEbus IO space at end of static 391 * kernel page table. 392 */ 393 RELOC(intiobase, char *) = 394 (char *)m68k_ptob(nptpages*NPTEPG - iiomappages); 395 RELOC(intiolimit, char *) = 396 (char *)m68k_ptob(nptpages*NPTEPG); 397 398 /* 399 * Setup u-area for process 0. 400 */ 401 /* 402 * Zero the u-area. 403 * NOTE: `pte' and `epte' aren't PTEs here. 404 */ 405 pte = (u_int *)p0upa; 406 epte = (u_int *)(p0upa + USPACE); 407 while (pte < epte) 408 *pte++ = 0; 409 /* 410 * Remember the u-area address so it can be loaded in the 411 * proc struct p_addr field later. 412 */ 413 RELOC(proc0paddr, char *) = (char *)(p0upa - firstpa); 414 415 /* 416 * Initialize the mem_clusters[] array for the crash dump 417 * code. While we're at it, compute the total amount of 418 * physical memory in the system. 419 */ 420 for (i = 0; i < VM_PHYSSEG_MAX; i++) { 421 if (RELOC(phys_seg_list[i].ps_start, paddr_t) == 422 RELOC(phys_seg_list[i].ps_end, paddr_t)) { 423 /* 424 * No more memory. 425 */ 426 break; 427 } 428 429 /* 430 * Make sure these are properly rounded. 431 */ 432 RELOC(phys_seg_list[i].ps_start, paddr_t) = 433 m68k_round_page(RELOC(phys_seg_list[i].ps_start, 434 paddr_t)); 435 RELOC(phys_seg_list[i].ps_end, paddr_t) = 436 m68k_trunc_page(RELOC(phys_seg_list[i].ps_end, 437 paddr_t)); 438 439 size = RELOC(phys_seg_list[i].ps_end, paddr_t) - 440 RELOC(phys_seg_list[i].ps_start, paddr_t); 441 442 RELOC(mem_clusters[i].start, u_quad_t) = 443 RELOC(phys_seg_list[i].ps_start, paddr_t); 444 RELOC(mem_clusters[i].size, u_quad_t) = size; 445 446 RELOC(physmem, int) += size >> PGSHIFT; 447 448 RELOC(mem_cluster_cnt, int) += 1; 449 } 450 451 /* 452 * Scoot the start of available on-board RAM forward to 453 * account for: 454 * 455 * (1) The bootstrap programs in low memory (so 456 * that we can jump back to them without 457 * reloading). 458 * 459 * (2) The kernel text, data, and bss. 460 * 461 * (3) The pages we stole above for pmap data 462 * structures. 463 */ 464 RELOC(phys_seg_list[0].ps_start, paddr_t) = nextpa; 465 466 /* 467 * Reserve space at the end of on-board RAM for the message 468 * buffer. We force it into on-board RAM because VME RAM 469 * gets cleared very early on in locore.s (to initialise 470 * parity on boards that need it). This would clobber the 471 * messages from a previous running NetBSD system. 472 */ 473 RELOC(phys_seg_list[0].ps_end, paddr_t) -= 474 m68k_round_page(MSGBUFSIZE); 475 RELOC(msgbufpa, paddr_t) = 476 RELOC(phys_seg_list[0].ps_end, paddr_t); 477 478 /* 479 * Initialize avail_start and avail_end. 480 */ 481 i = RELOC(mem_cluster_cnt, int) - 1; 482 RELOC(avail_start, paddr_t) = 483 RELOC(phys_seg_list[0].ps_start, paddr_t); 484 RELOC(avail_end, paddr_t) = 485 RELOC(phys_seg_list[i].ps_end, paddr_t); 486 487 RELOC(mem_size, vsize_t) = m68k_ptob(RELOC(physmem, int)); 488 489 RELOC(virtual_avail, vaddr_t) = 490 VM_MIN_KERNEL_ADDRESS + (vaddr_t)(nextpa - firstpa); 491 RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS; 492 493 /* 494 * Initialize protection array. 495 * XXX don't use a switch statement, it might produce an 496 * absolute "jmp" table. 497 */ 498 { 499 int *kp; 500 501 kp = &RELOC(protection_codes, int); 502 kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_NONE] = 0; 503 kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_NONE] = PG_RO; 504 kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO; 505 kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO; 506 kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW; 507 kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW; 508 kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW; 509 kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW; 510 } 511 512 /* 513 * Kernel page/segment table allocated above, 514 * just initialize pointers. 515 */ 516 { 517 struct pmap *kpm = &RELOC(kernel_pmap_store, struct pmap); 518 519 kpm->pm_stab = RELOC(Sysseg, st_entry_t *); 520 kpm->pm_ptab = RELOC(Sysmap, pt_entry_t *); 521 simple_lock_init(&kpm->pm_lock); 522 kpm->pm_count = 1; 523 kpm->pm_stpa = (st_entry_t *)kstpa; 524 #if defined(M68040) || defined(M68060) 525 /* 526 * For the 040 we also initialize the free level 2 527 * descriptor mask noting that we have used: 528 * 0: level 1 table 529 * 1 to `num': map page tables 530 * MAXKL2SIZE-1: maps last-page page table 531 */ 532 if (RELOC(mmutype, int) == MMU_68040) { 533 int num; 534 535 kpm->pm_stfree = ~l2tobm(0); 536 num = roundup((nptpages + 1) * (NPTEPG / SG4_LEV3SIZE), 537 SG4_LEV2SIZE) / SG4_LEV2SIZE; 538 while (num) 539 kpm->pm_stfree &= ~l2tobm(num--); 540 kpm->pm_stfree &= ~l2tobm(MAXKL2SIZE-1); 541 for (num = MAXKL2SIZE; 542 num < sizeof(kpm->pm_stfree)*NBBY; 543 num++) 544 kpm->pm_stfree &= ~l2tobm(num); 545 } 546 #endif 547 } 548 549 /* 550 * Allocate some fixed, special purpose kernel virtual addresses 551 */ 552 { 553 vaddr_t va = RELOC(virtual_avail, vaddr_t); 554 555 RELOC(CADDR1, caddr_t) = (caddr_t)va; 556 va += NBPG; 557 RELOC(CADDR2, caddr_t) = (caddr_t)va; 558 va += NBPG; 559 RELOC(vmmap, caddr_t) = (caddr_t)va; 560 va += NBPG; 561 RELOC(msgbufaddr, caddr_t) = (caddr_t)va; 562 va += m68k_round_page(MSGBUFSIZE); 563 RELOC(virtual_avail, vaddr_t) = va; 564 } 565 } 566 567 void 568 pmap_init_md(void) 569 { 570 vaddr_t addr; 571 572 addr = (vaddr_t) intiobase; 573 if (uvm_map(kernel_map, &addr, 574 (intiotop_phys - intiobase_phys), 575 NULL, UVM_UNKNOWN_OFFSET, 0, 576 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, 577 UVM_INH_NONE, UVM_ADV_RANDOM, 578 UVM_FLAG_FIXED)) != 0) 579 panic("pmap_init_md: uvm_map failed"); 580 } 581