xref: /titanic_52/usr/src/uts/sun4/vm/vm_dep.c (revision 1ce1951135b81c803c8dcf2f3c756009b1b0170a)
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,
669 		    disable_auto_text_large_pages,
670 		    max_utext_lpsize, shm_lpg_min_physmem));
671 
672 	} else if (flags & MAP_INITDATA) {
673 		return (map_szcvec(addr, size, off,
674 		    disable_auto_data_large_pages,
675 		    max_uidata_lpsize, privm_lpg_min_physmem));
676 
677 	} else if (type == MAPPGSZC_SHM) {
678 		return (map_szcvec(addr, size, off,
679 		    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,
684 		    disable_auto_data_large_pages,
685 		    max_uheap_lpsize, privm_lpg_min_physmem));
686 
687 	} else if (type == MAPPGSZC_STACK) {
688 		return (map_szcvec(addr, size, off,
689 		    disable_auto_data_large_pages,
690 		    max_ustack_lpsize, privm_lpg_min_physmem));
691 
692 	} else {
693 		return (map_szcvec(addr, size, off,
694 		    disable_auto_data_large_pages,
695 		    max_privmap_lpsize, privm_lpg_min_physmem));
696 	}
697 }
698 
699 /*
700  * Anchored in the table below are counters used to keep track
701  * of free contiguous physical memory. Each element of the table contains
702  * the array of counters, the size of array which is allocated during
703  * startup based on physmax and a shift value used to convert a pagenum
704  * into a counter array index or vice versa. The table has page size
705  * for rows and region size for columns:
706  *
707  *	page_counters[page_size][region_size]
708  *
709  *	page_size: 	TTE size code of pages on page_size freelist.
710  *
711  *	region_size:	TTE size code of a candidate larger page made up
712  *			made up of contiguous free page_size pages.
713  *
714  * As you go across a page_size row increasing region_size each
715  * element keeps track of how many (region_size - 1) size groups
716  * made up of page_size free pages can be coalesced into a
717  * regsion_size page. Yuck! Lets try an example:
718  *
719  * 	page_counters[1][3] is the table element used for identifying
720  *	candidate 4M pages from contiguous pages off the 64K free list.
721  *	Each index in the page_counters[1][3].array spans 4M. Its the
722  *	number of free 512K size (regsion_size - 1) groups of contiguous
723  *	64K free pages.	So when page_counters[1][3].counters[n] == 8
724  *	we know we have a candidate 4M page made up of 512K size groups
725  *	of 64K free pages.
726  */
727 
728 /*
729  * Per page size free lists. 3rd (max_mem_nodes) and 4th (page coloring bins)
730  * dimensions are allocated dynamically.
731  */
732 page_t ***page_freelists[MMU_PAGE_SIZES][MAX_MEM_TYPES];
733 
734 /*
735  * For now there is only a single size cache list.
736  * Allocated dynamically.
737  */
738 page_t ***page_cachelists[MAX_MEM_TYPES];
739 
740 kmutex_t *fpc_mutex[NPC_MUTEX];
741 kmutex_t *cpc_mutex[NPC_MUTEX];
742 
743 /*
744  * Calculate space needed for page freelists and counters
745  */
746 size_t
747 calc_free_pagelist_sz(void)
748 {
749 	int szc;
750 	size_t alloc_sz, cache_sz, free_sz;
751 
752 	/*
753 	 * one cachelist per color, node, and type
754 	 */
755 	cache_sz = (page_get_pagecolors(0) * sizeof (page_t *)) +
756 	    sizeof (page_t **);
757 	cache_sz *= max_mem_nodes * MAX_MEM_TYPES;
758 
759 	/*
760 	 * one freelist per size, color, node, and type
761 	 */
762 	free_sz = sizeof (page_t **);
763 	for (szc = 0; szc < mmu_page_sizes; szc++)
764 		free_sz += sizeof (page_t *) * page_get_pagecolors(szc);
765 	free_sz *= max_mem_nodes * MAX_MEM_TYPES;
766 
767 	alloc_sz = cache_sz + free_sz + page_ctrs_sz();
768 	return (alloc_sz);
769 }
770 
771 caddr_t
772 alloc_page_freelists(caddr_t alloc_base)
773 {
774 	int	mnode, mtype;
775 	int	szc, clrs;
776 
777 	/*
778 	 * We only support small pages in the cachelist.
779 	 */
780 	for (mtype = 0; mtype < MAX_MEM_TYPES; mtype++) {
781 		page_cachelists[mtype] = (page_t ***)alloc_base;
782 		alloc_base += (max_mem_nodes * sizeof (page_t **));
783 		for (mnode = 0; mnode < max_mem_nodes; mnode++) {
784 			page_cachelists[mtype][mnode] = (page_t **)alloc_base;
785 			alloc_base +=
786 			    (page_get_pagecolors(0) * sizeof (page_t *));
787 		}
788 	}
789 
790 	/*
791 	 * Allocate freelists bins for all
792 	 * supported page sizes.
793 	 */
794 	for (szc = 0; szc < mmu_page_sizes; szc++) {
795 		clrs = page_get_pagecolors(szc);
796 		for (mtype = 0; mtype < MAX_MEM_TYPES; mtype++) {
797 			page_freelists[szc][mtype] = (page_t ***)alloc_base;
798 			alloc_base += (max_mem_nodes * sizeof (page_t **));
799 			for (mnode = 0; mnode < max_mem_nodes; mnode++) {
800 				page_freelists[szc][mtype][mnode] =
801 				    (page_t **)alloc_base;
802 				alloc_base += (clrs * (sizeof (page_t *)));
803 			}
804 		}
805 	}
806 
807 	alloc_base = page_ctrs_alloc(alloc_base);
808 	return (alloc_base);
809 }
810 
811 /*
812  * Allocate page_freelists locks for a memnode from the nucleus data
813  * area. This is the first time that mmu_page_sizes is used during
814  * bootup, so check mmu_page_sizes initialization.
815  */
816 int
817 ndata_alloc_page_mutexs(struct memlist *ndata)
818 {
819 	size_t alloc_sz;
820 	caddr_t alloc_base;
821 	int	i;
822 	void	page_coloring_init();
823 
824 	page_coloring_init();
825 	if (&mmu_init_mmu_page_sizes) {
826 		if (!mmu_init_mmu_page_sizes(0)) {
827 			cmn_err(CE_PANIC, "mmu_page_sizes %d not initialized",
828 			    mmu_page_sizes);
829 		}
830 	}
831 	ASSERT(mmu_page_sizes >= DEFAULT_MMU_PAGE_SIZES);
832 
833 	/* fpc_mutex and cpc_mutex */
834 	alloc_sz = 2 * NPC_MUTEX * max_mem_nodes * sizeof (kmutex_t);
835 
836 	alloc_base = ndata_alloc(ndata, alloc_sz, ecache_alignsize);
837 	if (alloc_base == NULL)
838 		return (-1);
839 
840 	ASSERT(((uintptr_t)alloc_base & (ecache_alignsize - 1)) == 0);
841 
842 	for (i = 0; i < NPC_MUTEX; i++) {
843 		fpc_mutex[i] = (kmutex_t *)alloc_base;
844 		alloc_base += (sizeof (kmutex_t) * max_mem_nodes);
845 		cpc_mutex[i] = (kmutex_t *)alloc_base;
846 		alloc_base += (sizeof (kmutex_t) * max_mem_nodes);
847 	}
848 	return (0);
849 }
850 
851 /*
852  * To select our starting bin, we stride through the bins with a stride
853  * of 337.  Why 337?  It's prime, it's largeish, and it performs well both
854  * in simulation and practice for different workloads on varying cache sizes.
855  */
856 uint32_t color_start_current = 0;
857 uint32_t color_start_stride = 337;
858 int color_start_random = 0;
859 
860 /* ARGSUSED */
861 uint_t
862 get_color_start(struct as *as)
863 {
864 	uint32_t old, new;
865 
866 	if (consistent_coloring == 2 || color_start_random) {
867 		return ((uint_t)(((gettick()) << (vac_shift - MMU_PAGESHIFT)) &
868 		    (hw_page_array[0].hp_colors - 1)));
869 	}
870 
871 	do {
872 		old = color_start_current;
873 		new = old + (color_start_stride << (vac_shift - MMU_PAGESHIFT));
874 	} while (cas32(&color_start_current, old, new) != old);
875 
876 	return ((uint_t)(new));
877 }
878 
879 /*
880  * Called once at startup from kphysm_init() -- before memialloc()
881  * is invoked to do the 1st page_free()/page_freelist_add().
882  *
883  * initializes page_colors and page_colors_mask based on ecache_setsize.
884  *
885  * Also initializes the counter locks.
886  */
887 void
888 page_coloring_init()
889 {
890 	int	a, i;
891 	uint_t colors;
892 
893 	if (do_pg_coloring == 0) {
894 		page_colors = 1;
895 		for (i = 0; i < mmu_page_sizes; i++) {
896 			colorequivszc[i] = 0;
897 			hw_page_array[i].hp_colors = 1;
898 		}
899 		return;
900 	}
901 
902 	/*
903 	 * Calculate page_colors from ecache_setsize. ecache_setsize contains
904 	 * the max ecache setsize of all cpus configured in the system or, for
905 	 * cheetah+ systems, the max possible ecache setsize for all possible
906 	 * cheetah+ cpus.
907 	 */
908 	page_colors = ecache_setsize / MMU_PAGESIZE;
909 	page_colors_mask = page_colors - 1;
910 
911 	vac_colors = vac_size / MMU_PAGESIZE;
912 	vac_colors_mask = vac_colors -1;
913 
914 	page_coloring_shift = 0;
915 	a = ecache_setsize;
916 	while (a >>= 1) {
917 		page_coloring_shift++;
918 	}
919 
920 	/* initialize number of colors per page size */
921 	for (i = 0; i < mmu_page_sizes; i++) {
922 		hw_page_array[i].hp_colors = (page_colors_mask >>
923 		    (hw_page_array[i].hp_shift - hw_page_array[0].hp_shift))
924 		    + 1;
925 		colorequivszc[i] = 0;
926 	}
927 
928 	/*
929 	 * initialize cpu_page_colors if ecache setsizes are homogenous.
930 	 * cpu_page_colors set to -1 during DR operation or during startup
931 	 * if setsizes are heterogenous.
932 	 *
933 	 * The value of cpu_page_colors determines if additional color bins
934 	 * need to be checked for a particular color in the page_get routines.
935 	 */
936 	if (cpu_setsize > 0 && cpu_page_colors == 0 &&
937 	    cpu_setsize < ecache_setsize) {
938 		cpu_page_colors = cpu_setsize / MMU_PAGESIZE;
939 		a = lowbit(page_colors) - lowbit(cpu_page_colors);
940 		ASSERT(a > 0);
941 		ASSERT(a < 16);
942 
943 		for (i = 0; i < mmu_page_sizes; i++) {
944 			if ((colors = hw_page_array[i].hp_colors) <= 1) {
945 				continue;
946 			}
947 			while ((colors >> a) == 0)
948 				a--;
949 			ASSERT(a >= 0);
950 
951 			/* higher 4 bits encodes color equiv mask */
952 			colorequivszc[i] = (a << 4);
953 		}
954 	}
955 
956 	/* do cpu specific color initialization */
957 	if (&page_coloring_init_cpu) {
958 		page_coloring_init_cpu();
959 	}
960 }
961 
962 int
963 bp_color(struct buf *bp)
964 {
965 	int color = -1;
966 
967 	if (vac) {
968 		if ((bp->b_flags & B_PAGEIO) != 0) {
969 			color = sfmmu_get_ppvcolor(bp->b_pages);
970 		} else if (bp->b_un.b_addr != NULL) {
971 			color = sfmmu_get_addrvcolor(bp->b_un.b_addr);
972 		}
973 	}
974 	return (color < 0 ? 0 : ptob(color));
975 }
976 
977 /*
978  * Create & Initialise pageout scanner thread. The thread has to
979  * start at procedure with process pp and priority pri.
980  */
981 void
982 pageout_init(void (*procedure)(), proc_t *pp, pri_t pri)
983 {
984 	(void) thread_create(NULL, 0, procedure, NULL, 0, pp, TS_RUN, pri);
985 }
986 
987 /*
988  * Function for flushing D-cache when performing module relocations
989  * to an alternate mapping.  Stubbed out on all platforms except sun4u,
990  * at least for now.
991  */
992 void
993 dcache_flushall()
994 {
995 	sfmmu_cache_flushall();
996 }
997 
998 static int
999 kdi_range_overlap(uintptr_t va1, size_t sz1, uintptr_t va2, size_t sz2)
1000 {
1001 	if (va1 < va2 && va1 + sz1 <= va2)
1002 		return (0);
1003 
1004 	if (va2 < va1 && va2 + sz2 <= va1)
1005 		return (0);
1006 
1007 	return (1);
1008 }
1009 
1010 /*
1011  * Return the number of bytes, relative to the beginning of a given range, that
1012  * are non-toxic (can be read from and written to with relative impunity).
1013  */
1014 size_t
1015 kdi_range_is_nontoxic(uintptr_t va, size_t sz, int write)
1016 {
1017 	/* OBP reads are harmless, but we don't want people writing there */
1018 	if (write && kdi_range_overlap(va, sz, OFW_START_ADDR, OFW_END_ADDR -
1019 	    OFW_START_ADDR + 1))
1020 		return (va < OFW_START_ADDR ? OFW_START_ADDR - va : 0);
1021 
1022 	if (kdi_range_overlap(va, sz, PIOMAPBASE, PIOMAPSIZE))
1023 		return (va < PIOMAPBASE ? PIOMAPBASE - va : 0);
1024 
1025 	return (sz); /* no overlap */
1026 }
1027 
1028 /*
1029  * Minimum physmem required for enabling large pages for kernel heap
1030  * Currently we do not enable lp for kmem on systems with less
1031  * than 1GB of memory. This value can be changed via /etc/system
1032  */
1033 size_t segkmem_lpminphysmem = 0x40000000;	/* 1GB */
1034 
1035 /*
1036  * this function chooses large page size for kernel heap
1037  */
1038 size_t
1039 get_segkmem_lpsize(size_t lpsize)
1040 {
1041 	size_t memtotal = physmem * PAGESIZE;
1042 	size_t mmusz;
1043 	uint_t szc;
1044 
1045 	if (memtotal < segkmem_lpminphysmem)
1046 		return (PAGESIZE);
1047 
1048 	if (plat_lpkmem_is_supported != NULL &&
1049 	    plat_lpkmem_is_supported() == 0)
1050 		return (PAGESIZE);
1051 
1052 	mmusz = mmu_get_kernel_lpsize(lpsize);
1053 	szc = page_szc(mmusz);
1054 
1055 	while (szc) {
1056 		if (!(disable_large_pages & (1 << szc)))
1057 			return (page_get_pagesize(szc));
1058 		szc--;
1059 	}
1060 	return (PAGESIZE);
1061 }
1062