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