xref: /titanic_50/usr/src/uts/sun4/vm/vm_dep.c (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * UNIX machine dependent virtual memory support.
30  */
31 
32 #include <sys/vm.h>
33 #include <sys/exec.h>
34 
35 #include <sys/exechdr.h>
36 #include <vm/seg_kmem.h>
37 #include <sys/atomic.h>
38 #include <sys/archsystm.h>
39 #include <sys/machsystm.h>
40 #include <sys/kdi.h>
41 #include <sys/cpu_module.h>
42 
43 #include <vm/hat_sfmmu.h>
44 
45 #include <sys/memnode.h>
46 
47 #include <sys/mem_config.h>
48 #include <sys/mem_cage.h>
49 #include <vm/vm_dep.h>
50 #include <vm/page.h>
51 #include <sys/platform_module.h>
52 
53 /*
54  * These variables are set by module specific config routines.
55  * They are only set by modules which will use physical cache page coloring.
56  */
57 int do_pg_coloring = 0;
58 
59 /*
60  * These variables can be conveniently patched at kernel load time to
61  * prevent do_pg_coloring from being enabled by
62  * module specific config routines.
63  */
64 
65 int use_page_coloring = 1;
66 
67 /*
68  * initialized by page_coloring_init()
69  */
70 extern uint_t page_colors;
71 extern uint_t page_colors_mask;
72 extern uint_t page_coloring_shift;
73 int cpu_page_colors;
74 uint_t vac_colors = 0;
75 uint_t vac_colors_mask = 0;
76 
77 /* cpu specific coloring initialization */
78 extern void page_coloring_init_cpu();
79 #pragma weak page_coloring_init_cpu
80 
81 /*
82  * get the ecache setsize for the current cpu.
83  */
84 #define	CPUSETSIZE()	(cpunodes[CPU->cpu_id].ecache_setsize)
85 
86 plcnt_t		plcnt;		/* page list count */
87 
88 /*
89  * This variable is set by the cpu module to contain the lowest
90  * address not affected by the SF_ERRATA_57 workaround.  It should
91  * remain 0 if the workaround is not needed.
92  */
93 #if defined(SF_ERRATA_57)
94 caddr_t errata57_limit;
95 #endif
96 
97 extern void page_relocate_hash(page_t *, page_t *);
98 
99 /*
100  * these must be defined in platform specific areas
101  */
102 extern void map_addr_proc(caddr_t *, size_t, offset_t, int, caddr_t,
103 	struct proc *, uint_t);
104 extern page_t *page_get_freelist(struct vnode *, u_offset_t, struct seg *,
105 	caddr_t, size_t, uint_t, struct lgrp *);
106 /*
107  * Convert page frame number to an OBMEM page frame number
108  * (i.e. put in the type bits -- zero for this implementation)
109  */
110 pfn_t
111 impl_obmem_pfnum(pfn_t pf)
112 {
113 	return (pf);
114 }
115 
116 /*
117  * Use physmax to determine the highest physical page of DRAM memory
118  * It is assumed that any physical addresses above physmax is in IO space.
119  * We don't bother checking the low end because we assume that memory space
120  * begins at physical page frame 0.
121  *
122  * Return 1 if the page frame is onboard DRAM memory, else 0.
123  * Returns 0 for nvram so it won't be cached.
124  */
125 int
126 pf_is_memory(pfn_t pf)
127 {
128 	/* We must be IO space */
129 	if (pf > physmax)
130 		return (0);
131 
132 	/* We must be memory space */
133 	return (1);
134 }
135 
136 /*
137  * Handle a pagefault.
138  */
139 faultcode_t
140 pagefault(caddr_t addr, enum fault_type type, enum seg_rw rw, int iskernel)
141 {
142 	struct as *as;
143 	struct proc *p;
144 	faultcode_t res;
145 	caddr_t base;
146 	size_t len;
147 	int err;
148 
149 	if (INVALID_VADDR(addr))
150 		return (FC_NOMAP);
151 
152 	if (iskernel) {
153 		as = &kas;
154 	} else {
155 		p = curproc;
156 		as = p->p_as;
157 #if defined(SF_ERRATA_57)
158 		/*
159 		 * Prevent infinite loops due to a segment driver
160 		 * setting the execute permissions and the sfmmu hat
161 		 * silently ignoring them.
162 		 */
163 		if (rw == S_EXEC && AS_TYPE_64BIT(as) &&
164 		    addr < errata57_limit) {
165 			res = FC_NOMAP;
166 			goto out;
167 		}
168 #endif
169 	}
170 
171 	/*
172 	 * Dispatch pagefault.
173 	 */
174 	res = as_fault(as->a_hat, as, addr, 1, type, rw);
175 
176 	/*
177 	 * If this isn't a potential unmapped hole in the user's
178 	 * UNIX data or stack segments, just return status info.
179 	 */
180 	if (!(res == FC_NOMAP && iskernel == 0))
181 		goto out;
182 
183 	/*
184 	 * Check to see if we happened to faulted on a currently unmapped
185 	 * part of the UNIX data or stack segments.  If so, create a zfod
186 	 * mapping there and then try calling the fault routine again.
187 	 */
188 	base = p->p_brkbase;
189 	len = p->p_brksize;
190 
191 	if (addr < base || addr >= base + len) {		/* data seg? */
192 		base = (caddr_t)(p->p_usrstack - p->p_stksize);
193 		len = p->p_stksize;
194 		if (addr < base || addr >= p->p_usrstack) {	/* stack seg? */
195 			/* not in either UNIX data or stack segments */
196 			res = FC_NOMAP;
197 			goto out;
198 		}
199 	}
200 
201 	/* the rest of this function implements a 3.X 4.X 5.X compatibility */
202 	/* This code is probably not needed anymore */
203 
204 	/* expand the gap to the page boundaries on each side */
205 	len = (((uintptr_t)base + len + PAGEOFFSET) & PAGEMASK) -
206 	    ((uintptr_t)base & PAGEMASK);
207 	base = (caddr_t)((uintptr_t)base & PAGEMASK);
208 
209 	as_rangelock(as);
210 	as_purge(as);
211 	if (as_gap(as, PAGESIZE, &base, &len, AH_CONTAIN, addr) == 0) {
212 		err = as_map(as, base, len, segvn_create, zfod_argsp);
213 		as_rangeunlock(as);
214 		if (err) {
215 			res = FC_MAKE_ERR(err);
216 			goto out;
217 		}
218 	} else {
219 		/*
220 		 * This page is already mapped by another thread after we
221 		 * returned from as_fault() above.  We just fallthrough
222 		 * as_fault() below.
223 		 */
224 		as_rangeunlock(as);
225 	}
226 
227 	res = as_fault(as->a_hat, as, addr, 1, F_INVAL, rw);
228 
229 out:
230 
231 	return (res);
232 }
233 
234 /*
235  * This is the routine which defines the address limit implied
236  * by the flag '_MAP_LOW32'.  USERLIMIT32 matches the highest
237  * mappable address in a 32-bit process on this platform (though
238  * perhaps we should make it be UINT32_MAX here?)
239  */
240 void
241 map_addr(caddr_t *addrp, size_t len, offset_t off, int vacalign, uint_t flags)
242 {
243 	struct proc *p = curproc;
244 	caddr_t userlimit = flags & _MAP_LOW32 ?
245 		(caddr_t)USERLIMIT32 : p->p_as->a_userlimit;
246 	map_addr_proc(addrp, len, off, vacalign, userlimit, p, flags);
247 }
248 
249 /*
250  * Some V9 CPUs have holes in the middle of the 64-bit virtual address range.
251  */
252 caddr_t	hole_start, hole_end;
253 
254 /*
255  * kpm mapping window
256  */
257 caddr_t kpm_vbase;
258 size_t  kpm_size;
259 uchar_t kpm_size_shift;
260 
261 /*
262  * Determine whether [base, base+len] contains a mapable range of
263  * addresses at least minlen long. base and len are adjusted if
264  * required to provide a mapable range.
265  */
266 /* ARGSUSED */
267 int
268 valid_va_range(caddr_t *basep, size_t *lenp, size_t minlen, int dir)
269 {
270 	caddr_t hi, lo;
271 
272 	lo = *basep;
273 	hi = lo + *lenp;
274 
275 	/*
276 	 * If hi rolled over the top, try cutting back.
277 	 */
278 	if (hi < lo) {
279 		size_t newlen = 0 - (uintptr_t)lo - 1l;
280 
281 		if (newlen + (uintptr_t)hi < minlen)
282 			return (0);
283 		if (newlen < minlen)
284 			return (0);
285 		*lenp = newlen;
286 	} else if (hi - lo < minlen)
287 		return (0);
288 
289 	/*
290 	 * Deal with a possible hole in the address range between
291 	 * hole_start and hole_end that should never be mapped by the MMU.
292 	 */
293 	hi = lo + *lenp;
294 
295 	if (lo < hole_start) {
296 		if (hi > hole_start)
297 			if (hi < hole_end)
298 				hi = hole_start;
299 			else
300 				/* lo < hole_start && hi >= hole_end */
301 				if (dir == AH_LO) {
302 					/*
303 					 * prefer lowest range
304 					 */
305 					if (hole_start - lo >= minlen)
306 						hi = hole_start;
307 					else if (hi - hole_end >= minlen)
308 						lo = hole_end;
309 					else
310 						return (0);
311 				} else {
312 					/*
313 					 * prefer highest range
314 					 */
315 					if (hi - hole_end >= minlen)
316 						lo = hole_end;
317 					else if (hole_start - lo >= minlen)
318 						hi = hole_start;
319 					else
320 						return (0);
321 				}
322 	} else {
323 		/* lo >= hole_start */
324 		if (hi < hole_end)
325 			return (0);
326 		if (lo < hole_end)
327 			lo = hole_end;
328 	}
329 
330 	if (hi - lo < minlen)
331 		return (0);
332 
333 	*basep = lo;
334 	*lenp = hi - lo;
335 
336 	return (1);
337 }
338 
339 /*
340  * Determine whether [addr, addr+len] with protections `prot' are valid
341  * for a user address space.
342  */
343 /*ARGSUSED*/
344 int
345 valid_usr_range(caddr_t addr, size_t len, uint_t prot, struct as *as,
346     caddr_t userlimit)
347 {
348 	caddr_t eaddr = addr + len;
349 
350 	if (eaddr <= addr || addr >= userlimit || eaddr > userlimit)
351 		return (RANGE_BADADDR);
352 
353 	/*
354 	 * Determine if the address range falls within an illegal
355 	 * range of the MMU.
356 	 */
357 	if (eaddr > hole_start && addr < hole_end)
358 		return (RANGE_BADADDR);
359 
360 #if defined(SF_ERRATA_57)
361 	/*
362 	 * Make sure USERLIMIT isn't raised too high
363 	 */
364 	ASSERT64(addr <= (caddr_t)0xffffffff80000000ul ||
365 	    errata57_limit == 0);
366 
367 	if (AS_TYPE_64BIT(as) &&
368 	    (addr < errata57_limit) &&
369 	    (prot & PROT_EXEC))
370 		return (RANGE_BADPROT);
371 #endif /* SF_ERRATA57 */
372 	return (RANGE_OKAY);
373 }
374 
375 /*
376  * Routine used to check to see if an a.out can be executed
377  * by the current machine/architecture.
378  */
379 int
380 chkaout(struct exdata *exp)
381 {
382 	if (exp->ux_mach == M_SPARC)
383 		return (0);
384 	else
385 		return (ENOEXEC);
386 }
387 
388 /*
389  * The following functions return information about an a.out
390  * which is used when a program is executed.
391  */
392 
393 /*
394  * Return the load memory address for the data segment.
395  */
396 caddr_t
397 getdmem(struct exec *exp)
398 {
399 	/*
400 	 * XXX - Sparc Reference Hack approaching
401 	 * Remember that we are loading
402 	 * 8k executables into a 4k machine
403 	 * DATA_ALIGN == 2 * PAGESIZE
404 	 */
405 	if (exp->a_text)
406 		return ((caddr_t)(roundup(USRTEXT + exp->a_text, DATA_ALIGN)));
407 	else
408 		return ((caddr_t)USRTEXT);
409 }
410 
411 /*
412  * Return the starting disk address for the data segment.
413  */
414 ulong_t
415 getdfile(struct exec *exp)
416 {
417 	if (exp->a_magic == ZMAGIC)
418 		return (exp->a_text);
419 	else
420 		return (sizeof (struct exec) + exp->a_text);
421 }
422 
423 /*
424  * Return the load memory address for the text segment.
425  */
426 
427 /*ARGSUSED*/
428 caddr_t
429 gettmem(struct exec *exp)
430 {
431 	return ((caddr_t)USRTEXT);
432 }
433 
434 /*
435  * Return the file byte offset for the text segment.
436  */
437 uint_t
438 gettfile(struct exec *exp)
439 {
440 	if (exp->a_magic == ZMAGIC)
441 		return (0);
442 	else
443 		return (sizeof (struct exec));
444 }
445 
446 void
447 getexinfo(
448 	struct exdata *edp_in,
449 	struct exdata *edp_out,
450 	int *pagetext,
451 	int *pagedata)
452 {
453 	*edp_out = *edp_in;	/* structure copy */
454 
455 	if ((edp_in->ux_mag == ZMAGIC) &&
456 	    ((edp_in->vp->v_flag & VNOMAP) == 0)) {
457 		*pagetext = 1;
458 		*pagedata = 1;
459 	} else {
460 		*pagetext = 0;
461 		*pagedata = 0;
462 	}
463 }
464 
465 /*
466  * Return non 0 value if the address may cause a VAC alias with KPM mappings.
467  * KPM selects an address such that it's equal offset modulo shm_alignment and
468  * assumes it can't be in VAC conflict with any larger than PAGESIZE mapping.
469  */
470 int
471 map_addr_vacalign_check(caddr_t addr, u_offset_t off)
472 {
473 	if (vac) {
474 		return (((uintptr_t)addr ^ off) & shm_alignment - 1);
475 	} else {
476 		return (0);
477 	}
478 }
479 
480 /*
481  * Sanity control. Don't use large pages regardless of user
482  * settings if there's less than priv or shm_lpg_min_physmem memory installed.
483  * The units for this variable is 8K pages.
484  */
485 pgcnt_t shm_lpg_min_physmem = 131072;			/* 1GB */
486 pgcnt_t privm_lpg_min_physmem = 131072;			/* 1GB */
487 
488 static size_t
489 map_pgszheap(struct proc *p, caddr_t addr, size_t len)
490 {
491 	size_t		pgsz = MMU_PAGESIZE;
492 	int		szc;
493 
494 	/*
495 	 * If len is zero, retrieve from proc and don't demote the page size.
496 	 * Use atleast the default pagesize.
497 	 */
498 	if (len == 0) {
499 		len = p->p_brkbase + p->p_brksize - p->p_bssbase;
500 	}
501 	len = MAX(len, default_uheap_lpsize);
502 
503 	for (szc = mmu_page_sizes - 1; szc >= 0; szc--) {
504 		pgsz = hw_page_array[szc].hp_size;
505 		if ((disable_auto_data_large_pages & (1 << szc)) ||
506 		    pgsz > max_uheap_lpsize)
507 			continue;
508 		if (len >= pgsz) {
509 			break;
510 		}
511 	}
512 
513 	/*
514 	 * If addr == 0 we were called by memcntl() when the
515 	 * size code is 0.  Don't set pgsz less than current size.
516 	 */
517 	if (addr == 0 && (pgsz < hw_page_array[p->p_brkpageszc].hp_size)) {
518 		pgsz = hw_page_array[p->p_brkpageszc].hp_size;
519 	}
520 
521 	return (pgsz);
522 }
523 
524 static size_t
525 map_pgszstk(struct proc *p, caddr_t addr, size_t len)
526 {
527 	size_t		pgsz = MMU_PAGESIZE;
528 	int		szc;
529 
530 	/*
531 	 * If len is zero, retrieve from proc and don't demote the page size.
532 	 * Use atleast the default pagesize.
533 	 */
534 	if (len == 0) {
535 		len = p->p_stksize;
536 	}
537 	len = MAX(len, default_ustack_lpsize);
538 
539 	for (szc = mmu_page_sizes - 1; szc >= 0; szc--) {
540 		pgsz = hw_page_array[szc].hp_size;
541 		if ((disable_auto_data_large_pages & (1 << szc)) ||
542 		    pgsz > max_ustack_lpsize)
543 			continue;
544 		if (len >= pgsz) {
545 			break;
546 		}
547 	}
548 
549 	/*
550 	 * If addr == 0 we were called by memcntl() or exec_args() when the
551 	 * size code is 0.  Don't set pgsz less than current size.
552 	 */
553 	if (addr == 0 && (pgsz < hw_page_array[p->p_stkpageszc].hp_size)) {
554 		pgsz = hw_page_array[p->p_stkpageszc].hp_size;
555 	}
556 
557 	return (pgsz);
558 }
559 
560 static size_t
561 map_pgszism(caddr_t addr, size_t len)
562 {
563 	uint_t szc;
564 	size_t pgsz;
565 
566 	for (szc = mmu_page_sizes - 1; szc >= TTE4M; szc--) {
567 		if (disable_ism_large_pages & (1 << szc))
568 			continue;
569 
570 		pgsz = hw_page_array[szc].hp_size;
571 		if ((len >= pgsz) && IS_P2ALIGNED(addr, pgsz))
572 			return (pgsz);
573 	}
574 
575 	return (DEFAULT_ISM_PAGESIZE);
576 }
577 
578 /*
579  * Suggest a page size to be used to map a segment of type maptype and length
580  * len.  Returns a page size (not a size code).
581  */
582 /* ARGSUSED */
583 size_t
584 map_pgsz(int maptype, struct proc *p, caddr_t addr, size_t len, int memcntl)
585 {
586 	size_t	pgsz = MMU_PAGESIZE;
587 
588 	ASSERT(maptype != MAPPGSZ_VA);
589 
590 	if (maptype != MAPPGSZ_ISM && physmem < privm_lpg_min_physmem) {
591 		return (MMU_PAGESIZE);
592 	}
593 
594 	switch (maptype) {
595 	case MAPPGSZ_ISM:
596 		pgsz = map_pgszism(addr, len);
597 		break;
598 
599 	case MAPPGSZ_STK:
600 		if (max_ustack_lpsize > MMU_PAGESIZE) {
601 			pgsz = map_pgszstk(p, addr, len);
602 		}
603 		break;
604 
605 	case MAPPGSZ_HEAP:
606 		if (max_uheap_lpsize > MMU_PAGESIZE) {
607 			pgsz = map_pgszheap(p, addr, len);
608 		}
609 		break;
610 	}
611 	return (pgsz);
612 }
613 
614 
615 /* assumes TTE8K...TTE4M == szc */
616 
617 static uint_t
618 map_szcvec(caddr_t addr, size_t size, uintptr_t off, int disable_lpgs,
619     size_t max_lpsize, size_t min_physmem)
620 {
621 	caddr_t eaddr = addr + size;
622 	uint_t szcvec = 0;
623 	caddr_t raddr;
624 	caddr_t readdr;
625 	size_t pgsz;
626 	int i;
627 
628 	if (physmem < min_physmem || max_lpsize <= MMU_PAGESIZE) {
629 		return (0);
630 	}
631 	for (i = mmu_page_sizes - 1; i > 0; i--) {
632 		if (disable_lpgs & (1 << i)) {
633 			continue;
634 		}
635 		pgsz = page_get_pagesize(i);
636 		if (pgsz > max_lpsize) {
637 			continue;
638 		}
639 		raddr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
640 		readdr = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
641 		if (raddr < addr || raddr >= readdr) {
642 			continue;
643 		}
644 		if (P2PHASE((uintptr_t)addr ^ off, pgsz)) {
645 			continue;
646 		}
647 		szcvec |= (1 << i);
648 		/*
649 		 * And or in the remaining enabled page sizes.
650 		 */
651 		szcvec |= P2PHASE(~disable_lpgs, (1 << i));
652 		szcvec &= ~1; /* no need to return 8K pagesize */
653 		break;
654 	}
655 	return (szcvec);
656 }
657 
658 /*
659  * Return a bit vector of large page size codes that
660  * can be used to map [addr, addr + len) region.
661  */
662 /* ARGSUSED */
663 uint_t
664 map_pgszcvec(caddr_t addr, size_t size, uintptr_t off, int flags, int type,
665     int memcntl)
666 {
667 	if (flags & MAP_TEXT) {
668 	    return (map_szcvec(addr, size, off, disable_auto_text_large_pages,
669 		    max_utext_lpsize, shm_lpg_min_physmem));
670 
671 	} else if (flags & MAP_INITDATA) {
672 	    return (map_szcvec(addr, size, off, disable_auto_data_large_pages,
673 		    max_uidata_lpsize, privm_lpg_min_physmem));
674 
675 	} else if (type == MAPPGSZC_SHM) {
676 	    return (map_szcvec(addr, size, off, disable_auto_data_large_pages,
677 		    max_shm_lpsize, shm_lpg_min_physmem));
678 
679 	} else if (type == MAPPGSZC_HEAP) {
680 	    return (map_szcvec(addr, size, off, disable_auto_data_large_pages,
681 		    max_uheap_lpsize, privm_lpg_min_physmem));
682 
683 	} else if (type == MAPPGSZC_STACK) {
684 	    return (map_szcvec(addr, size, off, disable_auto_data_large_pages,
685 		    max_ustack_lpsize, privm_lpg_min_physmem));
686 
687 	} else {
688 	    return (map_szcvec(addr, size, off, disable_auto_data_large_pages,
689 		    max_privmap_lpsize, privm_lpg_min_physmem));
690 	}
691 }
692 
693 /*
694  * Anchored in the table below are counters used to keep track
695  * of free contiguous physical memory. Each element of the table contains
696  * the array of counters, the size of array which is allocated during
697  * startup based on physmax and a shift value used to convert a pagenum
698  * into a counter array index or vice versa. The table has page size
699  * for rows and region size for columns:
700  *
701  *	page_counters[page_size][region_size]
702  *
703  *	page_size: 	TTE size code of pages on page_size freelist.
704  *
705  *	region_size:	TTE size code of a candidate larger page made up
706  *			made up of contiguous free page_size pages.
707  *
708  * As you go across a page_size row increasing region_size each
709  * element keeps track of how many (region_size - 1) size groups
710  * made up of page_size free pages can be coalesced into a
711  * regsion_size page. Yuck! Lets try an example:
712  *
713  * 	page_counters[1][3] is the table element used for identifying
714  *	candidate 4M pages from contiguous pages off the 64K free list.
715  *	Each index in the page_counters[1][3].array spans 4M. Its the
716  *	number of free 512K size (regsion_size - 1) groups of contiguous
717  *	64K free pages.	So when page_counters[1][3].counters[n] == 8
718  *	we know we have a candidate 4M page made up of 512K size groups
719  *	of 64K free pages.
720  */
721 
722 /*
723  * Per page size free lists. 3rd (max_mem_nodes) and 4th (page coloring bins)
724  * dimensions are allocated dynamically.
725  */
726 page_t ***page_freelists[MMU_PAGE_SIZES][MAX_MEM_TYPES];
727 
728 /*
729  * For now there is only a single size cache list.
730  * Allocated dynamically.
731  */
732 page_t ***page_cachelists[MAX_MEM_TYPES];
733 
734 kmutex_t *fpc_mutex[NPC_MUTEX];
735 kmutex_t *cpc_mutex[NPC_MUTEX];
736 
737 caddr_t
738 alloc_page_freelists(int mnode, caddr_t alloc_base, int alloc_align)
739 {
740 	int	mtype;
741 	uint_t	szc;
742 
743 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, alloc_align);
744 
745 	/*
746 	 * We only support small pages in the cachelist.
747 	 */
748 	for (mtype = 0; mtype < MAX_MEM_TYPES; mtype++) {
749 		page_cachelists[mtype][mnode] = (page_t **)alloc_base;
750 		alloc_base += (sizeof (page_t *) * page_get_pagecolors(0));
751 		/*
752 		 * Allocate freelists bins for all
753 		 * supported page sizes.
754 		 */
755 		for (szc = 0; szc < mmu_page_sizes; szc++) {
756 			page_freelists[szc][mtype][mnode] =
757 			    (page_t **)alloc_base;
758 			alloc_base += ((sizeof (page_t *) *
759 			    page_get_pagecolors(szc)));
760 		}
761 	}
762 
763 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, alloc_align);
764 
765 	return (alloc_base);
766 }
767 
768 /*
769  * Allocate page_freelists bin headers for a memnode from the
770  * nucleus data area. This is the first time that mmu_page_sizes is
771  * used during sun4u bootup, so check mmu_page_sizes initialization.
772  */
773 int
774 ndata_alloc_page_freelists(struct memlist *ndata, int mnode)
775 {
776 	size_t alloc_sz;
777 	caddr_t alloc_base;
778 	caddr_t end;
779 	int	mtype;
780 	uint_t	szc;
781 	int32_t allp = 0;
782 
783 	if (&mmu_init_mmu_page_sizes) {
784 		if (!mmu_init_mmu_page_sizes(allp)) {
785 			cmn_err(CE_PANIC, "mmu_page_sizes %d not initialized",
786 			    mmu_page_sizes);
787 		}
788 	}
789 	ASSERT(mmu_page_sizes >= DEFAULT_MMU_PAGE_SIZES);
790 
791 	/* first time called - allocate max_mem_nodes dimension */
792 	if (mnode == 0) {
793 		int	i;
794 
795 		/* page_cachelists */
796 		alloc_sz = MAX_MEM_TYPES * max_mem_nodes *
797 		    sizeof (page_t **);
798 
799 		/* page_freelists */
800 		alloc_sz += MAX_MEM_TYPES * mmu_page_sizes * max_mem_nodes *
801 		    sizeof (page_t **);
802 
803 		/* fpc_mutex and cpc_mutex */
804 		alloc_sz += 2 * NPC_MUTEX * max_mem_nodes * sizeof (kmutex_t);
805 
806 		alloc_base = ndata_alloc(ndata, alloc_sz, ecache_alignsize);
807 		if (alloc_base == NULL)
808 			return (-1);
809 
810 		ASSERT(((uintptr_t)alloc_base & (ecache_alignsize - 1)) == 0);
811 
812 		for (mtype = 0; mtype < MAX_MEM_TYPES; mtype++) {
813 			page_cachelists[mtype] = (page_t ***)alloc_base;
814 			alloc_base += (max_mem_nodes * sizeof (page_t **));
815 			for (szc = 0; szc < mmu_page_sizes; szc++) {
816 				page_freelists[szc][mtype] =
817 				    (page_t ***)alloc_base;
818 				alloc_base += (max_mem_nodes *
819 				    sizeof (page_t **));
820 			}
821 		}
822 		for (i = 0; i < NPC_MUTEX; i++) {
823 			fpc_mutex[i] = (kmutex_t *)alloc_base;
824 			alloc_base += (sizeof (kmutex_t) * max_mem_nodes);
825 			cpc_mutex[i] = (kmutex_t *)alloc_base;
826 			alloc_base += (sizeof (kmutex_t) * max_mem_nodes);
827 		}
828 		alloc_sz = 0;
829 	}
830 
831 	/*
832 	 * Calculate the size needed by alloc_page_freelists().
833 	 */
834 	for (mtype = 0; mtype < MAX_MEM_TYPES; mtype++) {
835 		alloc_sz += sizeof (page_t *) * page_get_pagecolors(0);
836 
837 		for (szc = 0; szc < mmu_page_sizes; szc++)
838 			alloc_sz += sizeof (page_t *) *
839 			    page_get_pagecolors(szc);
840 	}
841 
842 	alloc_base = ndata_alloc(ndata, alloc_sz, ecache_alignsize);
843 	if (alloc_base == NULL)
844 		return (-1);
845 
846 	end = alloc_page_freelists(mnode, alloc_base, ecache_alignsize);
847 	ASSERT((uintptr_t)end == roundup((uintptr_t)alloc_base + alloc_sz,
848 	    ecache_alignsize));
849 
850 	return (0);
851 }
852 
853 /*
854  * To select our starting bin, we stride through the bins with a stride
855  * of 337.  Why 337?  It's prime, it's largeish, and it performs well both
856  * in simulation and practice for different workloads on varying cache sizes.
857  */
858 uint32_t color_start_current = 0;
859 uint32_t color_start_stride = 337;
860 int color_start_random = 0;
861 
862 /* ARGSUSED */
863 uint_t
864 get_color_start(struct as *as)
865 {
866 	uint32_t old, new;
867 
868 	if (consistent_coloring == 2 || color_start_random) {
869 		return ((uint_t)(((gettick()) << (vac_shift - MMU_PAGESHIFT)) &
870 		    (hw_page_array[0].hp_colors - 1)));
871 	}
872 
873 	do {
874 		old = color_start_current;
875 		new = old + (color_start_stride << (vac_shift - MMU_PAGESHIFT));
876 	} while (cas32(&color_start_current, old, new) != old);
877 
878 	return ((uint_t)(new));
879 }
880 
881 /*
882  * Called once at startup from kphysm_init() -- before memialloc()
883  * is invoked to do the 1st page_free()/page_freelist_add().
884  *
885  * initializes page_colors and page_colors_mask based on ecache_setsize.
886  *
887  * Also initializes the counter locks.
888  */
889 void
890 page_coloring_init()
891 {
892 	int	a, i;
893 	uint_t colors;
894 
895 	if (do_pg_coloring == 0) {
896 		page_colors = 1;
897 		for (i = 0; i < mmu_page_sizes; i++) {
898 			colorequivszc[i] = 0;
899 			hw_page_array[i].hp_colors = 1;
900 		}
901 		return;
902 	}
903 
904 	/*
905 	 * Calculate page_colors from ecache_setsize. ecache_setsize contains
906 	 * the max ecache setsize of all cpus configured in the system or, for
907 	 * cheetah+ systems, the max possible ecache setsize for all possible
908 	 * cheetah+ cpus.
909 	 */
910 	page_colors = ecache_setsize / MMU_PAGESIZE;
911 	page_colors_mask = page_colors - 1;
912 
913 	vac_colors = vac_size / MMU_PAGESIZE;
914 	vac_colors_mask = vac_colors -1;
915 
916 	page_coloring_shift = 0;
917 	a = ecache_setsize;
918 	while (a >>= 1) {
919 		page_coloring_shift++;
920 	}
921 
922 	/* initialize number of colors per page size */
923 	for (i = 0; i < mmu_page_sizes; i++) {
924 		hw_page_array[i].hp_colors = (page_colors_mask >>
925 		    (hw_page_array[i].hp_shift - hw_page_array[0].hp_shift))
926 		    + 1;
927 		colorequivszc[i] = 0;
928 	}
929 
930 	/*
931 	 * initialize cpu_page_colors if ecache setsizes are homogenous.
932 	 * cpu_page_colors set to -1 during DR operation or during startup
933 	 * if setsizes are heterogenous.
934 	 *
935 	 * The value of cpu_page_colors determines if additional color bins
936 	 * need to be checked for a particular color in the page_get routines.
937 	 */
938 	if (cpu_setsize > 0 && cpu_page_colors == 0 &&
939 	    cpu_setsize < ecache_setsize) {
940 		cpu_page_colors = cpu_setsize / MMU_PAGESIZE;
941 		a = lowbit(page_colors) - lowbit(cpu_page_colors);
942 		ASSERT(a > 0);
943 		ASSERT(a < 16);
944 
945 		for (i = 0; i < mmu_page_sizes; i++) {
946 			if ((colors = hw_page_array[i].hp_colors) <= 1) {
947 				continue;
948 			}
949 			while ((colors >> a) == 0)
950 				a--;
951 			ASSERT(a >= 0);
952 
953 			/* higher 4 bits encodes color equiv mask */
954 			colorequivszc[i] = (a << 4);
955 		}
956 	}
957 
958 	/* do cpu specific color initialization */
959 	if (&page_coloring_init_cpu) {
960 		page_coloring_init_cpu();
961 	}
962 }
963 
964 int
965 bp_color(struct buf *bp)
966 {
967 	int color = -1;
968 
969 	if (vac) {
970 		if ((bp->b_flags & B_PAGEIO) != 0) {
971 			color = sfmmu_get_ppvcolor(bp->b_pages);
972 		} else if (bp->b_un.b_addr != NULL) {
973 			color = sfmmu_get_addrvcolor(bp->b_un.b_addr);
974 		}
975 	}
976 	return (color < 0 ? 0 : ptob(color));
977 }
978 
979 /*
980  * Create & Initialise pageout scanner thread. The thread has to
981  * start at procedure with process pp and priority pri.
982  */
983 void
984 pageout_init(void (*procedure)(), proc_t *pp, pri_t pri)
985 {
986 	(void) thread_create(NULL, 0, procedure, NULL, 0, pp, TS_RUN, pri);
987 }
988 
989 /*
990  * Function for flushing D-cache when performing module relocations
991  * to an alternate mapping.  Stubbed out on all platforms except sun4u,
992  * at least for now.
993  */
994 void
995 dcache_flushall()
996 {
997 	sfmmu_cache_flushall();
998 }
999 
1000 static int
1001 kdi_range_overlap(uintptr_t va1, size_t sz1, uintptr_t va2, size_t sz2)
1002 {
1003 	if (va1 < va2 && va1 + sz1 <= va2)
1004 		return (0);
1005 
1006 	if (va2 < va1 && va2 + sz2 <= va1)
1007 		return (0);
1008 
1009 	return (1);
1010 }
1011 
1012 /*
1013  * Return the number of bytes, relative to the beginning of a given range, that
1014  * are non-toxic (can be read from and written to with relative impunity).
1015  */
1016 size_t
1017 kdi_range_is_nontoxic(uintptr_t va, size_t sz, int write)
1018 {
1019 	/* OBP reads are harmless, but we don't want people writing there */
1020 	if (write && kdi_range_overlap(va, sz, OFW_START_ADDR, OFW_END_ADDR -
1021 	    OFW_START_ADDR + 1))
1022 		return (va < OFW_START_ADDR ? OFW_START_ADDR - va : 0);
1023 
1024 	if (kdi_range_overlap(va, sz, PIOMAPBASE, PIOMAPSIZE))
1025 		return (va < PIOMAPBASE ? PIOMAPBASE - va : 0);
1026 
1027 	return (sz); /* no overlap */
1028 }
1029 
1030 /*
1031  * Minimum physmem required for enabling large pages for kernel heap
1032  * Currently we do not enable lp for kmem on systems with less
1033  * than 1GB of memory. This value can be changed via /etc/system
1034  */
1035 size_t segkmem_lpminphysmem = 0x40000000;	/* 1GB */
1036 
1037 /*
1038  * this function chooses large page size for kernel heap
1039  */
1040 size_t
1041 get_segkmem_lpsize(size_t lpsize)
1042 {
1043 	size_t memtotal = physmem * PAGESIZE;
1044 	size_t mmusz;
1045 	uint_t szc;
1046 
1047 	if (memtotal < segkmem_lpminphysmem)
1048 		return (PAGESIZE);
1049 
1050 	if (plat_lpkmem_is_supported != NULL &&
1051 	    plat_lpkmem_is_supported() == 0)
1052 		return (PAGESIZE);
1053 
1054 	mmusz = mmu_get_kernel_lpsize(lpsize);
1055 	szc = page_szc(mmusz);
1056 
1057 	while (szc) {
1058 		if (!(disable_large_pages & (1 << szc)))
1059 			return (page_get_pagesize(szc));
1060 		szc--;
1061 	}
1062 	return (PAGESIZE);
1063 }
1064