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