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