1 /* $NetBSD: pmap_bootstrap.c,v 1.4 2001/01/12 16:19:18 tsutsui 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 * news68k/pmap_bootstrap.c - from hp300 and mvme68k 43 */ 44 45 #include <sys/param.h> 46 47 #include <machine/cpu.h> 48 #include <machine/pte.h> 49 50 #include <uvm/uvm_extern.h> 51 52 #define RELOC(v, t) *((t*)((u_int)&(v) + firstpa)) 53 54 extern char *etext; 55 extern int Sysptsize; 56 extern char *extiobase, *proc0paddr; 57 extern char *cache_ctl, *cache_clr; 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 int protection_codes[]; 66 extern int pmap_aliasmask; 67 68 void pmap_bootstrap __P((paddr_t, paddr_t)); 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 /* 82 * Bootstrap the VM system. 83 * 84 * Called with MMU off so we must relocate all global references by `firstpa' 85 * (don't call any functions here!) `nextpa' is the first available physical 86 * memory address. Returns an updated first PA reflecting the memory we 87 * have allocated. MMU is still off when we return. 88 * 89 * XXX assumes sizeof(u_int) == sizeof(pt_entry_t) 90 * XXX a PIC compiler would make this much easier. 91 */ 92 void 93 pmap_bootstrap(nextpa, firstpa) 94 paddr_t nextpa; 95 paddr_t firstpa; 96 { 97 paddr_t kstpa, kptpa, iiopa, eiopa, kptmpa, p0upa; 98 u_int nptpages, kstsize; 99 st_entry_t protoste, *ste; 100 pt_entry_t protopte, *pte, *epte; 101 u_int iiomapsize, eiomapsize; 102 103 /* 104 * Calculate important physical addresses: 105 * 106 * kstpa kernel segment table 1 page (!040) 107 * N pages (040) 108 * 109 * kptpa statically allocated 110 * kernel PT pages Sysptsize+ pages 111 * 112 * iiopa internal IO space 113 * PT pages iiomapsize pages 114 * 115 * eiopa external IO space 116 * PT pages eiomapsize pages 117 * 118 * [ Sysptsize is the number of pages of PT, IIOMAPSIZE and 119 * EIOMAPSIZE are the number of PTEs, hence we need to round 120 * the total to a page boundary with IO maps at the end. ] 121 * 122 * kptmpa kernel PT map 1 page 123 * 124 * p0upa proc 0 u-area UPAGES pages 125 * 126 * The KVA corresponding to any of these PAs is: 127 * (PA - firstpa + KERNBASE). 128 */ 129 130 /* 131 * XXX now we are using tt0 register to map IIO. 132 */ 133 iiomapsize = m68k_btop(RELOC(intiotop_phys, u_int) - 134 RELOC(intiobase_phys, u_int)); 135 eiomapsize = m68k_btop(RELOC(extiotop_phys, u_int) - 136 RELOC(extiobase_phys, u_int)); 137 138 #ifdef M68040 139 if (RELOC(mmutype, int) == MMU_68040) 140 kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE); 141 else 142 kstsize = 1; 143 #else 144 kstsize = 1; 145 #endif 146 kstpa = nextpa; 147 nextpa += kstsize * NBPG; 148 kptpa = nextpa; 149 nptpages = RELOC(Sysptsize, int) + 150 (iiomapsize + eiomapsize + NPTEPG - 1) / NPTEPG; 151 nextpa += nptpages * NBPG; 152 eiopa = nextpa - eiomapsize * sizeof(pt_entry_t); 153 iiopa = eiopa - iiomapsize * sizeof(pt_entry_t); 154 kptmpa = nextpa; 155 nextpa += NBPG; 156 p0upa = nextpa; 157 nextpa += USPACE; 158 159 /* 160 * Clear all PTEs to zero 161 */ 162 #if 1 163 for (pte = (pt_entry_t *)kstpa; pte < (pt_entry_t *)nextpa; pte++) 164 *pte = 0; 165 #endif 166 167 /* 168 * Initialize segment table and kernel page table map. 169 * 170 * On 68030s and earlier MMUs the two are identical except for 171 * the valid bits so both are initialized with essentially the 172 * same values. On the 68040, which has a mandatory 3-level 173 * structure, the segment table holds the level 1 table and part 174 * (or all) of the level 2 table and hence is considerably 175 * different. Here the first level consists of 128 descriptors 176 * (512 bytes) each mapping 32mb of address space. Each of these 177 * points to blocks of 128 second level descriptors (512 bytes) 178 * each mapping 256kb. Note that there may be additional "segment 179 * table" pages depending on how large MAXKL2SIZE is. 180 * 181 * Portions of the last segment of KVA space (0xFFF00000 - 182 * 0xFFFFFFFF) are mapped for a couple of purposes. 0xFFF00000 183 * for UPAGES is used for mapping the current process u-area 184 * (u + kernel stack). The very last page (0xFFFFF000) is mapped 185 * to the last physical page of RAM to give us a region in which 186 * PA == VA. We use the first part of this page for enabling 187 * and disabling mapping. The last part of this page also contains 188 * info left by the boot ROM. 189 * 190 * XXX cramming two levels of mapping into the single "segment" 191 * table on the 68040 is intended as a temporary hack to get things 192 * working. The 224mb of address space that this allows will most 193 * likely be insufficient in the future (at least for the kernel). 194 */ 195 #ifdef M68040 196 if (RELOC(mmutype, int) == MMU_68040) { 197 int num; 198 199 /* 200 * First invalidate the entire "segment table" pages 201 * (levels 1 and 2 have the same "invalid" value). 202 */ 203 pte = (u_int *)kstpa; 204 epte = &pte[kstsize * NPTEPG]; 205 while (pte < epte) 206 *pte++ = SG_NV; 207 /* 208 * Initialize level 2 descriptors (which immediately 209 * follow the level 1 table). We need: 210 * NPTEPG / SG4_LEV3SIZE 211 * level 2 descriptors to map each of the nptpages+1 212 * pages of PTEs. Note that we set the "used" bit 213 * now to save the HW the expense of doing it. 214 */ 215 num = (nptpages + 1) * (NPTEPG / SG4_LEV3SIZE); 216 pte = &((u_int *)kstpa)[SG4_LEV1SIZE]; 217 epte = &pte[num]; 218 protoste = kptpa | SG_U | SG_RW | SG_V; 219 while (pte < epte) { 220 *pte++ = protoste; 221 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 222 } 223 /* 224 * Initialize level 1 descriptors. We need: 225 * roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE 226 * level 1 descriptors to map the `num' level 2's. 227 */ 228 pte = (u_int *)kstpa; 229 epte = &pte[roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE]; 230 protoste = (u_int)&pte[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V; 231 while (pte < epte) { 232 *pte++ = protoste; 233 protoste += (SG4_LEV2SIZE * sizeof(st_entry_t)); 234 } 235 /* 236 * Initialize the final level 1 descriptor to map the last 237 * block of level 2 descriptors. 238 */ 239 ste = &((u_int *)kstpa)[SG4_LEV1SIZE-1]; 240 pte = &((u_int *)kstpa)[kstsize*NPTEPG - SG4_LEV2SIZE]; 241 *ste = (u_int)pte | SG_U | SG_RW | SG_V; 242 /* 243 * Initialize Sysptmap 244 */ 245 pte = (u_int *)kptmpa; 246 epte = &pte[nptpages+1]; 247 protopte = kptpa | PG_RW | PG_CI | PG_V; 248 while (pte < epte) { 249 *pte++ = protopte; 250 protopte += NBPG; 251 } 252 /* 253 * Invalidate all but the last remaining entry. 254 */ 255 epte = &((u_int *)kptmpa)[NPTEPG]; 256 while (pte < epte) { 257 *pte++ = PG_NV; 258 } 259 } else 260 #endif 261 { 262 /* 263 * Map the page table pages in both the HW segment table 264 * and the software Sysptmap. Note that Sysptmap is also 265 * considered a PT page hence the +1. 266 */ 267 ste = (u_int *)kstpa; 268 pte = (u_int *)kptmpa; 269 epte = &pte[nptpages+1]; 270 protoste = kptpa | SG_RW | SG_V; 271 protopte = kptpa | PG_RW | PG_CI | PG_V; 272 while (pte < epte) { 273 *ste++ = protoste; 274 *pte++ = protopte; 275 protoste += NBPG; 276 protopte += NBPG; 277 } 278 /* 279 * Invalidate all but the last remaining entries in both. 280 */ 281 epte = &((u_int *)kptmpa)[NPTEPG]; 282 while (pte < epte) { 283 *ste++ = SG_NV; 284 *pte++ = PG_NV; 285 } 286 } 287 /* 288 * Initialize kernel page table. 289 * Start by invalidating the `nptpages' that we have allocated. 290 */ 291 pte = (u_int *)kptpa; 292 epte = &pte[nptpages * NPTEPG]; 293 while (pte < epte) 294 *pte++ = PG_NV; 295 296 /* 297 * Validate PTEs for kernel text (RO). 298 */ 299 pte = &((u_int *)kptpa)[m68k_btop(KERNBASE)]; 300 epte = &pte[m68k_btop(m68k_trunc_page(&etext))]; 301 protopte = firstpa | PG_RO | PG_V; 302 while (pte < epte) { 303 *pte++ = protopte; 304 protopte += NBPG; 305 } 306 /* 307 * Validate PTEs for kernel data/bss, dynamic data allocated 308 * by us so far (nextpa - firstpa bytes), and pages for proc0 309 * u-area and page table allocated below (RW). 310 */ 311 epte = &((u_int *)kptpa)[m68k_btop(nextpa - firstpa)]; 312 protopte = (protopte & ~PG_PROT) | PG_RW; 313 /* 314 * Enable copy-back caching of data pages 315 */ 316 #ifdef M68040 317 if (RELOC(mmutype, int) == MMU_68040) 318 protopte |= PG_CCB; 319 #endif 320 while (pte < epte) { 321 *pte++ = protopte; 322 protopte += NBPG; 323 } 324 /* 325 * Finally, validate the internal IO space PTEs (RW+CI). 326 */ 327 pte = (u_int *)iiopa; 328 epte = (u_int *)eiopa; 329 protopte = RELOC(intiobase_phys, u_int) | PG_RW | PG_CI | PG_V; 330 while (pte < epte) { 331 *pte++ = protopte; 332 protopte += NBPG; 333 } 334 335 /* 336 * Calculate important exported kernel virtual addresses 337 */ 338 /* 339 * Sysseg: base of kernel segment table 340 */ 341 RELOC(Sysseg, st_entry_t *) = 342 (st_entry_t *)(kstpa - firstpa); 343 /* 344 * Sysptmap: base of kernel page table map 345 */ 346 RELOC(Sysptmap, pt_entry_t *) = 347 (pt_entry_t *)(kptmpa - firstpa); 348 /* 349 * Sysmap: kernel page table (as mapped through Sysptmap) 350 * Immediately follows `nptpages' of static kernel page table. 351 */ 352 RELOC(Sysmap, pt_entry_t *) = 353 (pt_entry_t *)m68k_ptob(nptpages * NPTEPG); 354 /* 355 * intiobase, intiolimit: base and end of internal IO space. 356 */ 357 RELOC(intiobase, char *) = 358 (char *)m68k_ptob(nptpages*NPTEPG - (iiomapsize + eiomapsize)); 359 RELOC(intiolimit, char *) = 360 (char *)m68k_ptob(nptpages*NPTEPG - eiomapsize); 361 /* 362 * extiobase: base of external IO space. 363 * eiomapsize pages at the end of the static kernel page table. 364 */ 365 RELOC(extiobase, char *) = 366 (char *)m68k_ptob(nptpages*NPTEPG - eiomapsize); 367 368 /* 369 * Setup u-area for process 0. 370 */ 371 /* 372 * Zero the u-area. 373 * NOTE: `pte' and `epte' aren't PTEs here. 374 */ 375 pte = (u_int *)p0upa; 376 epte = (u_int *)(p0upa + USPACE); 377 while (pte < epte) 378 *pte++ = 0; 379 /* 380 * Remember the u-area address so it can be loaded in the 381 * proc struct p_addr field later. 382 */ 383 RELOC(proc0paddr, char *) = (char *)(p0upa - firstpa); 384 385 /* 386 * VM data structures are now initialized, set up data for 387 * the pmap module. 388 * 389 * Note about avail_end: msgbuf is initialized just after 390 * avail_end in machdep.c. 391 */ 392 RELOC(avail_start, paddr_t) = nextpa; 393 RELOC(avail_end, paddr_t) = m68k_ptob(RELOC(maxmem, int)) - 394 (m68k_round_page(MSGBUFSIZE)); 395 RELOC(mem_size, vsize_t) = m68k_ptob(RELOC(physmem, int)); 396 397 RELOC(virtual_avail, vaddr_t) = 398 VM_MIN_KERNEL_ADDRESS + (vaddr_t)(nextpa - firstpa); 399 RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS; 400 401 #if 0 402 /* 403 * Determine VA aliasing distance if any 404 * 405 * XXX Are there any models which have VAC? 406 */ 407 if (RELOC(ectype, int) == EC_VIRT) { 408 RELOC(pmap_aliasmask, int) = 0x3fff; /* 16k */ 409 } 410 #endif 411 #ifdef news1700 412 if (RELOC(systype, int) == NEWS1700) { 413 RELOC(cache_ctl, char *) = 0xe1300000 - INTIOBASE1700 + 414 RELOC(intiobase, char *); 415 RELOC(cache_clr, char *) = 0xe1900000 - INTIOBASE1700 + 416 RELOC(intiobase, char *); 417 } 418 #endif 419 420 /* 421 * Initialize protection array. 422 * XXX don't use a switch statement, it might produce an 423 * absolute "jmp" table. 424 */ 425 { 426 int *kp; 427 428 kp = &RELOC(protection_codes, int); 429 kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_NONE] = 0; 430 kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_NONE] = PG_RO; 431 kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO; 432 kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO; 433 kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW; 434 kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW; 435 kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW; 436 kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW; 437 } 438 439 /* 440 * Kernel page/segment table allocated above, 441 * just initialize pointers. 442 */ 443 { 444 struct pmap *kpm = &RELOC(kernel_pmap_store, struct pmap); 445 446 kpm->pm_stab = RELOC(Sysseg, st_entry_t *); 447 kpm->pm_ptab = RELOC(Sysmap, pt_entry_t *); 448 simple_lock_init(&kpm->pm_lock); 449 kpm->pm_count = 1; 450 kpm->pm_stpa = (st_entry_t *)kstpa; 451 /* 452 * For the 040 we also initialize the free level 2 453 * descriptor mask noting that we have used: 454 * 0: level 1 table 455 * 1 to `num': map page tables 456 * MAXKL2SIZE-1: maps last-page page table 457 */ 458 #ifdef M68040 459 if (RELOC(mmutype, int) == MMU_68040) { 460 int num; 461 462 kpm->pm_stfree = ~l2tobm(0); 463 num = roundup((nptpages + 1) * (NPTEPG / SG4_LEV3SIZE), 464 SG4_LEV2SIZE) / SG4_LEV2SIZE; 465 while (num) 466 kpm->pm_stfree &= ~l2tobm(num--); 467 kpm->pm_stfree &= ~l2tobm(MAXKL2SIZE-1); 468 for (num = MAXKL2SIZE; 469 num < sizeof(kpm->pm_stfree)*NBBY; 470 num++) 471 kpm->pm_stfree &= ~l2tobm(num); 472 } 473 #endif 474 } 475 476 /* 477 * Allocate some fixed, special purpose kernel virtual addresses 478 */ 479 { 480 vaddr_t va = RELOC(virtual_avail, vaddr_t); 481 482 RELOC(CADDR1, caddr_t) = (caddr_t)va; 483 va += NBPG; 484 RELOC(CADDR2, caddr_t) = (caddr_t)va; 485 va += NBPG; 486 RELOC(vmmap, caddr_t) = (caddr_t)va; 487 va += NBPG; 488 RELOC(msgbufaddr, caddr_t) = (caddr_t)va; 489 va += m68k_round_page(MSGBUFSIZE); 490 RELOC(virtual_avail, vaddr_t) = va; 491 } 492 } 493