xref: /onnv-gate/usr/src/uts/i86pc/os/startup.c (revision 770:0eda482eb80f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <sys/signal.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/user.h>
360Sstevel@tonic-gate #include <sys/mman.h>
370Sstevel@tonic-gate #include <sys/vm.h>
380Sstevel@tonic-gate #include <sys/conf.h>
390Sstevel@tonic-gate #include <sys/avintr.h>
400Sstevel@tonic-gate #include <sys/autoconf.h>
410Sstevel@tonic-gate #include <sys/disp.h>
420Sstevel@tonic-gate #include <sys/class.h>
430Sstevel@tonic-gate #include <sys/bitmap.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/privregs.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/proc.h>
480Sstevel@tonic-gate #include <sys/buf.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/kstat.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <sys/reboot.h>
530Sstevel@tonic-gate #include <sys/uadmin.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <sys/cred.h>
560Sstevel@tonic-gate #include <sys/vnode.h>
570Sstevel@tonic-gate #include <sys/file.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <sys/procfs.h>
600Sstevel@tonic-gate #include <sys/acct.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #include <sys/vfs.h>
630Sstevel@tonic-gate #include <sys/dnlc.h>
640Sstevel@tonic-gate #include <sys/var.h>
650Sstevel@tonic-gate #include <sys/cmn_err.h>
660Sstevel@tonic-gate #include <sys/utsname.h>
670Sstevel@tonic-gate #include <sys/debug.h>
680Sstevel@tonic-gate #include <sys/kdi.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <sys/dumphdr.h>
710Sstevel@tonic-gate #include <sys/bootconf.h>
720Sstevel@tonic-gate #include <sys/varargs.h>
730Sstevel@tonic-gate #include <sys/promif.h>
740Sstevel@tonic-gate #include <sys/prom_emul.h>	/* for create_prom_prop */
750Sstevel@tonic-gate #include <sys/modctl.h>		/* for "procfs" hack */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #include <sys/consdev.h>
780Sstevel@tonic-gate #include <sys/frame.h>
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #include <sys/sunddi.h>
810Sstevel@tonic-gate #include <sys/sunndi.h>
820Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
830Sstevel@tonic-gate #include <sys/ddidmareq.h>
840Sstevel@tonic-gate #include <sys/psw.h>
850Sstevel@tonic-gate #include <sys/regset.h>
860Sstevel@tonic-gate #include <sys/clock.h>
870Sstevel@tonic-gate #include <sys/pte.h>
880Sstevel@tonic-gate #include <sys/mmu.h>
890Sstevel@tonic-gate #include <sys/tss.h>
900Sstevel@tonic-gate #include <sys/stack.h>
910Sstevel@tonic-gate #include <sys/trap.h>
920Sstevel@tonic-gate #include <sys/pic.h>
930Sstevel@tonic-gate #include <sys/fp.h>
940Sstevel@tonic-gate #include <vm/anon.h>
950Sstevel@tonic-gate #include <vm/as.h>
960Sstevel@tonic-gate #include <vm/page.h>
970Sstevel@tonic-gate #include <vm/seg.h>
980Sstevel@tonic-gate #include <vm/seg_dev.h>
990Sstevel@tonic-gate #include <vm/seg_kmem.h>
1000Sstevel@tonic-gate #include <vm/seg_kpm.h>
1010Sstevel@tonic-gate #include <vm/seg_map.h>
1020Sstevel@tonic-gate #include <vm/seg_vn.h>
1030Sstevel@tonic-gate #include <vm/seg_kp.h>
1040Sstevel@tonic-gate #include <sys/memnode.h>
1050Sstevel@tonic-gate #include <vm/vm_dep.h>
1060Sstevel@tonic-gate #include <sys/swap.h>
1070Sstevel@tonic-gate #include <sys/thread.h>
1080Sstevel@tonic-gate #include <sys/sysconf.h>
1090Sstevel@tonic-gate #include <sys/vm_machparam.h>
1100Sstevel@tonic-gate #include <sys/archsystm.h>
1110Sstevel@tonic-gate #include <sys/machsystm.h>
1120Sstevel@tonic-gate #include <vm/hat.h>
1130Sstevel@tonic-gate #include <vm/hat_i86.h>
1140Sstevel@tonic-gate #include <sys/pmem.h>
1150Sstevel@tonic-gate #include <sys/instance.h>
1160Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1170Sstevel@tonic-gate #include <sys/x86_archext.h>
1180Sstevel@tonic-gate #include <sys/segments.h>
1190Sstevel@tonic-gate #include <sys/clconf.h>
1200Sstevel@tonic-gate #include <sys/kobj.h>
1210Sstevel@tonic-gate #include <sys/kobj_lex.h>
1220Sstevel@tonic-gate #include <sys/prom_emul.h>
1230Sstevel@tonic-gate #include <sys/cpc_impl.h>
1240Sstevel@tonic-gate #include <sys/chip.h>
1250Sstevel@tonic-gate #include <sys/x86_archext.h>
126437Smws #include <sys/smbios.h>
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate extern void progressbar_init(void);
1290Sstevel@tonic-gate extern void progressbar_start(void);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1330Sstevel@tonic-gate  * interface.
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * segkp
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate extern int segkp_fromheap;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static void kvm_init(void);
1430Sstevel@tonic-gate static void startup_init(void);
1440Sstevel@tonic-gate static void startup_memlist(void);
1450Sstevel@tonic-gate static void startup_modules(void);
1460Sstevel@tonic-gate static void startup_bop_gone(void);
1470Sstevel@tonic-gate static void startup_vm(void);
1480Sstevel@tonic-gate static void startup_end(void);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate pgcnt_t physmem = 0;	/* memory size in pages, patch if you want less */
1540Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate char *kobj_file_buf;
1570Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
1600Sstevel@tonic-gate caddr_t	rm_platter_va;
1610Sstevel@tonic-gate uint32_t rm_platter_pa;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate uintptr_t hole_start, hole_end;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * kpm mapping window
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate caddr_t kpm_vbase;
1720Sstevel@tonic-gate size_t  kpm_size;
1730Sstevel@tonic-gate static int kpm_desired = 0;		/* Do we want to try to use segkpm? */
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate  * VA range that must be preserved for boot until we release all of its
1770Sstevel@tonic-gate  * mappings.
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate #if defined(__amd64)
1800Sstevel@tonic-gate static void *kmem_setaside;
1810Sstevel@tonic-gate #endif
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Configuration parameters set at boot time.
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
1900Sstevel@tonic-gate struct bootops		**bootopsp;
1910Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate char bootblock_fstype[16];
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate /*
1980Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
1990Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2000Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate  * VM data structures
2060Sstevel@tonic-gate  */
2070Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
2080Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2090Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
2100Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2110Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
2120Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
2130Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
2140Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
2170Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate #if defined(__amd64)
2200Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
2210Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
2220Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
2230Sstevel@tonic-gate #else
2240Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
2250Sstevel@tonic-gate #endif
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
2280Sstevel@tonic-gate #if defined(__amd64)
2290Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
2300Sstevel@tonic-gate #else
2310Sstevel@tonic-gate pgcnt_t segkpsize = 0;
2320Sstevel@tonic-gate #endif
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate struct memseg *memseg_base;
2350Sstevel@tonic-gate struct vnode unused_pages_vp;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate #define	FOURGB	0x100000000LL
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate struct memlist *memlist;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
2420Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
2430Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
2440Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
2450Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
2460Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
2470Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
2480Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
2510Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static void memlist_add(uint64_t, uint64_t, struct memlist *,
2540Sstevel@tonic-gate 	struct memlist **);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate static pgcnt_t kphysm_init(page_t *, struct memseg *, pgcnt_t, pgcnt_t);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate  * a couple useful roundup macros
2650Sstevel@tonic-gate  */
2660Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
2670Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
2680Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
2690Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
2700Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
2710Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOURMB_PAGESIZE))
2720Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
2730Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
2770Sstevel@tonic-gate  *		+-----------------------+
2780Sstevel@tonic-gate  *		|	psm 1-1 map	|
2790Sstevel@tonic-gate  *		|	exec args area	|
2800Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
2810Sstevel@tonic-gate  *		|	debugger	|
2820Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
2830Sstevel@tonic-gate  *		|      Kernel Data	|
2840Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
2850Sstevel@tonic-gate  *              |      Kernel Text	|
2860Sstevel@tonic-gate  * 0xFE800000  -|-----------------------|- KERNEL_TEXT
2870Sstevel@tonic-gate  * 		|     LUFS sinkhole	|
2880Sstevel@tonic-gate  * 0xFE000000  -|-----------------------|- lufs_addr
2890Sstevel@tonic-gate  * ---         -|-----------------------|- valloc_base + valloc_sz
2900Sstevel@tonic-gate  * 		|   early pp structures	|
2910Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
2920Sstevel@tonic-gate  * 		|   page hash, etc.	|
2930Sstevel@tonic-gate  * ---	       -|-----------------------|- valloc_base (floating)
2940Sstevel@tonic-gate  * 		|     ptable_va    	|
2950Sstevel@tonic-gate  * 0xFDFFE000  -|-----------------------|- ekernelheap, ptable_va
2960Sstevel@tonic-gate  *		|			|  (segkp is an arena under the heap)
2970Sstevel@tonic-gate  *		|			|
2980Sstevel@tonic-gate  *		|	kvseg		|
2990Sstevel@tonic-gate  *		|			|
3000Sstevel@tonic-gate  *		|			|
3010Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
3020Sstevel@tonic-gate  * 		|        Segkmap	|
3030Sstevel@tonic-gate  * 0xC3002000  -|-----------------------|- segkmap_start (floating)
3040Sstevel@tonic-gate  *		|	Red Zone	|
3050Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
3060Sstevel@tonic-gate  *		|			|			||
3070Sstevel@tonic-gate  *		|     Shared objects	|			\/
3080Sstevel@tonic-gate  *		|			|
3090Sstevel@tonic-gate  *		:			:
3100Sstevel@tonic-gate  *		|	user data	|
3110Sstevel@tonic-gate  *		|-----------------------|
3120Sstevel@tonic-gate  *		|	user text	|
3130Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
3140Sstevel@tonic-gate  *		|	user stack	|
3150Sstevel@tonic-gate  *		:			:
3160Sstevel@tonic-gate  *		|	invalid		|
3170Sstevel@tonic-gate  * 0x00000000	+-----------------------+
3180Sstevel@tonic-gate  *
3190Sstevel@tonic-gate  *
3200Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
3210Sstevel@tonic-gate  *			+-----------------------+
3220Sstevel@tonic-gate  *			|	psm 1-1 map	|
3230Sstevel@tonic-gate  *			|	exec args area	|
3240Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
3250Sstevel@tonic-gate  *			|	debugger (?)	|
3260Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
3270Sstevel@tonic-gate  *			|      unused    	|
3280Sstevel@tonic-gate  *			+-----------------------+
3290Sstevel@tonic-gate  *			|      Kernel Data	|
3300Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
3310Sstevel@tonic-gate  *			|      Kernel Text	|
3320Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
3330Sstevel@tonic-gate  * 			|     LUFS sinkhole	|
3340Sstevel@tonic-gate  * 0xFFFFFFFF.FB000000 -|-----------------------|- lufs_addr
3350Sstevel@tonic-gate  * ---                  |-----------------------|- valloc_base + valloc_sz
3360Sstevel@tonic-gate  * 			|   early pp structures	|
3370Sstevel@tonic-gate  * 			|   memsegs, memlists, 	|
3380Sstevel@tonic-gate  * 			|   page hash, etc.	|
3390Sstevel@tonic-gate  * ---                  |-----------------------|- valloc_base
3400Sstevel@tonic-gate  * 			|     ptable_va    	|
3410Sstevel@tonic-gate  * ---                  |-----------------------|- ptable_va
3420Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
3430Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
3440Sstevel@tonic-gate  *			|	 Kernel		|
3450Sstevel@tonic-gate  *			|	  heap		|
3460Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
3470Sstevel@tonic-gate  *			|	 segkmap	|
3480Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- segkmap_start (floating)
3490Sstevel@tonic-gate  *			|    device mappings	|
3500Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
3510Sstevel@tonic-gate  *			|	  segkp		|
3520Sstevel@tonic-gate  * ---                  |-----------------------|- segkp_base
3530Sstevel@tonic-gate  *			|	 segkpm		|
3540Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
3550Sstevel@tonic-gate  *			|	Red Zone	|
3560Sstevel@tonic-gate  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE
3570Sstevel@tonic-gate  *			|     User stack	|- User space memory
3580Sstevel@tonic-gate  * 			|			|
3590Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
3600Sstevel@tonic-gate  *			:			:
3610Sstevel@tonic-gate  * 			|			|
3620Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
3630Sstevel@tonic-gate  * 			|			|
3640Sstevel@tonic-gate  * 			| VA Hole / unused	|
3650Sstevel@tonic-gate  * 			|			|
3660Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
3670Sstevel@tonic-gate  *			|			|
3680Sstevel@tonic-gate  *			|			|
3690Sstevel@tonic-gate  *			:			:
3700Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
3710Sstevel@tonic-gate  *			|			|
3720Sstevel@tonic-gate  *			|	user data	|
3730Sstevel@tonic-gate  *			|-----------------------|
3740Sstevel@tonic-gate  *			|	user text	|
3750Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
3760Sstevel@tonic-gate  *			|	invalid		|
3770Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
3780Sstevel@tonic-gate  *
3790Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
3800Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
3810Sstevel@tonic-gate  *
3820Sstevel@tonic-gate  * Floating values:
3830Sstevel@tonic-gate  *
3840Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
3850Sstevel@tonic-gate  * structures.  This region contains page_t structures for the lowest 4GB
3860Sstevel@tonic-gate  * of physical memory, memsegs, memlists, and the page hash.
3870Sstevel@tonic-gate  *
3880Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
3890Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
3900Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
3910Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
3920Sstevel@tonic-gate  * accessed using rip-relative addressing
3930Sstevel@tonic-gate  *
3940Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
3950Sstevel@tonic-gate  *
3960Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
3970Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
3980Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
3990Sstevel@tonic-gate  *
4000Sstevel@tonic-gate  * segkmap_start: start of segmap. The length of segmap can be modified
4010Sstevel@tonic-gate  * by changing segmapsize in /etc/system (preferred) or eeprom (deprecated).
4020Sstevel@tonic-gate  * The default length is 16MB on 32-bit systems and 64MB on 64-bit systems.
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
4050Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
4060Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
4070Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
4080Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
4090Sstevel@tonic-gate  * segkp sizes).
4100Sstevel@tonic-gate  *
4110Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
4120Sstevel@tonic-gate  *	size	page_t's			size
4130Sstevel@tonic-gate  *	----	---------	----------	-----------
4140Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
4150Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
4160Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
4170Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
4180Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
4190Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
4200Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
4210Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
4240Sstevel@tonic-gate  * configurations above 8gb.
4250Sstevel@tonic-gate  *
4260Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
4270Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
4280Sstevel@tonic-gate  */
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate void init_intr_threads(struct cpu *);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate  * Dummy spl priority masks
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate static unsigned char	dummy_cpu_pri[MAXIPL + 1] = {
4360Sstevel@tonic-gate 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
4370Sstevel@tonic-gate 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf
4380Sstevel@tonic-gate };
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate /* real-time-clock initialization parameters */
4410Sstevel@tonic-gate long gmt_lag;		/* offset in seconds of gmt to local time */
4420Sstevel@tonic-gate extern long process_rtc_config_file(void);
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate char		*final_kernelheap;
4450Sstevel@tonic-gate char		*boot_kernelheap;
4460Sstevel@tonic-gate uintptr_t	kernelbase;
4470Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
4480Sstevel@tonic-gate size_t		segmapsize;
4490Sstevel@tonic-gate static uintptr_t segmap_reserved;
4500Sstevel@tonic-gate uintptr_t	segkmap_start;
4510Sstevel@tonic-gate int		segmapfreelists;
4520Sstevel@tonic-gate pgcnt_t		boot_npages;
4530Sstevel@tonic-gate pgcnt_t		npages;
4540Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
4550Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
4590Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
4600Sstevel@tonic-gate  * the bootstrap.
4610Sstevel@tonic-gate  */
4620Sstevel@tonic-gate static page_t *bootpages, *rd_pages;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate struct system_hardware system_hardware;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
4680Sstevel@tonic-gate  *
4690Sstevel@tonic-gate  * XX64 There should only be one print routine once memlist usage between
4700Sstevel@tonic-gate  * vmx and the kernel is cleaned up and there is a single memlist structure
4710Sstevel@tonic-gate  * shared between kernel and boot.
4720Sstevel@tonic-gate  */
4730Sstevel@tonic-gate static void
4740Sstevel@tonic-gate print_boot_memlist(char *title, struct memlist *mp)
4750Sstevel@tonic-gate {
4760Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
4770Sstevel@tonic-gate 	while (mp != NULL)  {
4780Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
4790Sstevel@tonic-gate 		    mp->address, mp->size);
4800Sstevel@tonic-gate 		mp = mp->next;
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate static void
4850Sstevel@tonic-gate print_kernel_memlist(char *title, struct memlist *mp)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
4880Sstevel@tonic-gate 	while (mp != NULL)  {
4890Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
4900Sstevel@tonic-gate 		    mp->address, mp->size);
4910Sstevel@tonic-gate 		mp = mp->next;
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
4970Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
4980Sstevel@tonic-gate  */
4990Sstevel@tonic-gate int	l2cache_sz = 0x80000;
5000Sstevel@tonic-gate int	l2cache_linesz = 0x40;
5010Sstevel@tonic-gate int	l2cache_assoc = 1;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
5050Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
5060Sstevel@tonic-gate  */
5070Sstevel@tonic-gate #ifdef __amd64
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate vmem_t		*device_arena;
5100Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
5110Sstevel@tonic-gate size_t		toxic_size = 1 * 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate #else	/* __i386 */
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
5160Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate #endif	/* __i386 */
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate  * Simple boot time debug facilities
5220Sstevel@tonic-gate  */
5230Sstevel@tonic-gate static char *prm_dbg_str[] = {
5240Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%x\n",
5250Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%llx\n"
5260Sstevel@tonic-gate };
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate int prom_debug;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate #define	PRM_DEBUG(q)	if (prom_debug) 	\
5310Sstevel@tonic-gate 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
5320Sstevel@tonic-gate #define	PRM_POINT(q)	if (prom_debug) 	\
5330Sstevel@tonic-gate 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate /*
5360Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
5370Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
5380Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
5390Sstevel@tonic-gate  */
5400Sstevel@tonic-gate #define	NUM_ALLOCATIONS 7
5410Sstevel@tonic-gate int num_allocations = 0;
5420Sstevel@tonic-gate struct {
5430Sstevel@tonic-gate 	void **al_ptr;
5440Sstevel@tonic-gate 	size_t al_size;
5450Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
5460Sstevel@tonic-gate size_t valloc_sz = 0;
5470Sstevel@tonic-gate uintptr_t valloc_base;
5480Sstevel@tonic-gate extern uintptr_t ptable_va;
5490Sstevel@tonic-gate extern size_t ptable_sz;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
5520Sstevel@tonic-gate 		size = ROUND_UP_PAGE(size);		 		\
5530Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
5540Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
5550Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
5560Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
5570Sstevel@tonic-gate 		valloc_sz += size;					\
5580Sstevel@tonic-gate 		++num_allocations;				 	\
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate static void
5620Sstevel@tonic-gate perform_allocations(void)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	caddr_t mem;
5650Sstevel@tonic-gate 	int i;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, BO_NO_ALIGN);
5680Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
5690Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
5700Sstevel@tonic-gate 	bzero(mem, valloc_sz);
5710Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
5720Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
5730Sstevel@tonic-gate 		mem += allocations[i].al_size;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  * Our world looks like this at startup time.
5790Sstevel@tonic-gate  *
5800Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
5810Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
5820Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
5830Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
5840Sstevel@tonic-gate  *
5850Sstevel@tonic-gate  * On the text page:
5860Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
5870Sstevel@tonic-gate  *
5880Sstevel@tonic-gate  * On the data page:
5890Sstevel@tonic-gate  * unix/genunix/krtld/module data loads and space for page_t's.
5900Sstevel@tonic-gate  */
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate  * Machine-dependent startup code
5930Sstevel@tonic-gate  */
5940Sstevel@tonic-gate void
5950Sstevel@tonic-gate startup(void)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	extern void startup_bios_disk();
5980Sstevel@tonic-gate 	/*
5990Sstevel@tonic-gate 	 * Make sure that nobody tries to use sekpm until we have
6000Sstevel@tonic-gate 	 * initialized it properly.
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate #if defined(__amd64)
6030Sstevel@tonic-gate 	kpm_desired = kpm_enable;
6040Sstevel@tonic-gate #endif
6050Sstevel@tonic-gate 	kpm_enable = 0;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	progressbar_init();
6080Sstevel@tonic-gate 	startup_init();
6090Sstevel@tonic-gate 	startup_memlist();
6100Sstevel@tonic-gate 	startup_modules();
6110Sstevel@tonic-gate 	startup_bios_disk();
6120Sstevel@tonic-gate 	startup_bop_gone();
6130Sstevel@tonic-gate 	startup_vm();
6140Sstevel@tonic-gate 	startup_end();
6150Sstevel@tonic-gate 	progressbar_start();
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate static void
6190Sstevel@tonic-gate startup_init()
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/*
6240Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
6250Sstevel@tonic-gate 	 */
6260Sstevel@tonic-gate 	cpuid_pass2(CPU);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	/*
6310Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
6320Sstevel@tonic-gate 	 */
6330Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
6340Sstevel@tonic-gate 		++prom_debug;
6350Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
6390Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
6400Sstevel@tonic-gate 	 */
6410Sstevel@tonic-gate 	get_system_configuration();
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/*
6440Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
6470Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
6480Sstevel@tonic-gate 		    CPU->cpu_brandstr);
6490Sstevel@tonic-gate 		halt("This processor is not supported by this release "
6500Sstevel@tonic-gate 		    "of Solaris.");
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/*
6540Sstevel@tonic-gate 	 * Set up dummy values till psm spl code installed
6550Sstevel@tonic-gate 	 */
6560Sstevel@tonic-gate 	CPU->cpu_pri_data = dummy_cpu_pri;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
6630Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
6640Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
6650Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
6660Sstevel@tonic-gate  */
6670Sstevel@tonic-gate static void
6680Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
6690Sstevel@tonic-gate {
6700Sstevel@tonic-gate 	uintptr_t va;
6710Sstevel@tonic-gate 	uintptr_t next_va;
6720Sstevel@tonic-gate 	pfn_t pfn;
6730Sstevel@tonic-gate 	uint64_t pfn_addr;
6740Sstevel@tonic-gate 	uint64_t pfn_eaddr;
6750Sstevel@tonic-gate 	uint_t prot;
6760Sstevel@tonic-gate 	size_t len;
6770Sstevel@tonic-gate 	uint_t change;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	if (prom_debug)
6800Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
6810Sstevel@tonic-gate 		    *addr, *size);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	/*
6840Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
6850Sstevel@tonic-gate 	 */
6860Sstevel@tonic-gate 	if (*addr == 0) {
6870Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
6880Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * First we trim from the front of the range. Since hat_boot_probe()
6930Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
6940Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
6950Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
6960Sstevel@tonic-gate 	 * but w < v.
6970Sstevel@tonic-gate 	 */
6980Sstevel@tonic-gate 	do {
6990Sstevel@tonic-gate 		change = 0;
7000Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
7010Sstevel@tonic-gate 		    *size > 0 && hat_boot_probe(&va, &len, &pfn, &prot) != 0;
7020Sstevel@tonic-gate 		    va = next_va) {
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 			next_va = va + len;
7050Sstevel@tonic-gate 			pfn_addr = ptob((uint64_t)pfn);
7060Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
7090Sstevel@tonic-gate 				change = 1;
7100Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
7110Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
7120Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
7130Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
7140Sstevel@tonic-gate 				}
7150Sstevel@tonic-gate 			}
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 		if (change && prom_debug)
7180Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
7190Sstevel@tonic-gate 			    *addr, *size);
7200Sstevel@tonic-gate 	} while (change);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/*
7230Sstevel@tonic-gate 	 * Trim pages from the end of the range.
7240Sstevel@tonic-gate 	 */
7250Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
7260Sstevel@tonic-gate 	    *size > 0 && hat_boot_probe(&va, &len, &pfn, &prot) != 0;
7270Sstevel@tonic-gate 	    va = next_va) {
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		next_va = va + len;
7300Sstevel@tonic-gate 		pfn_addr = ptob((uint64_t)pfn);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
7330Sstevel@tonic-gate 			*size = pfn_addr - *addr;
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (prom_debug)
7370Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
7380Sstevel@tonic-gate 		    *addr, *size);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate static void
7420Sstevel@tonic-gate kpm_init()
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	struct segkpm_crargs b;
7450Sstevel@tonic-gate 	uintptr_t start, end;
7460Sstevel@tonic-gate 	struct memlist	*pmem;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	/*
7490Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
7500Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
7510Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
7520Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
7530Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
7540Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
7550Sstevel@tonic-gate 	 * pagesize is used.
7560Sstevel@tonic-gate 	 */
7570Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
7580Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
7590Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
7600Sstevel@tonic-gate 	kpmp2pshft = 0;
7610Sstevel@tonic-gate 	kpmpnpgs = 1;
7620Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
7650Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
7680Sstevel@tonic-gate 		panic("cannot attach segkpm");
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
7710Sstevel@tonic-gate 	b.nvcolors = 1;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
7740Sstevel@tonic-gate 		panic("segkpm_create segkpm");
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/*
7790Sstevel@tonic-gate 	 * Map each of the memsegs into the kpm segment, coalesing adjacent
7800Sstevel@tonic-gate 	 * memsegs to allow mapping with the largest possible pages.
7810Sstevel@tonic-gate 	 */
7820Sstevel@tonic-gate 	pmem = phys_install;
7830Sstevel@tonic-gate 	start = pmem->address;
7840Sstevel@tonic-gate 	end = start + pmem->size;
7850Sstevel@tonic-gate 	for (;;) {
7860Sstevel@tonic-gate 		if (pmem == NULL || pmem->address > end) {
7870Sstevel@tonic-gate 			hat_devload(kas.a_hat, kpm_vbase + start,
7880Sstevel@tonic-gate 			    end - start, mmu_btop(start),
7890Sstevel@tonic-gate 			    PROT_READ | PROT_WRITE,
7900Sstevel@tonic-gate 			    HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
7910Sstevel@tonic-gate 			if (pmem == NULL)
7920Sstevel@tonic-gate 				break;
7930Sstevel@tonic-gate 			start = pmem->address;
7940Sstevel@tonic-gate 		}
7950Sstevel@tonic-gate 		end = pmem->address + pmem->size;
7960Sstevel@tonic-gate 		pmem = pmem->next;
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate /*
8010Sstevel@tonic-gate  * The purpose of startup memlist is to get the system to the
8020Sstevel@tonic-gate  * point where it can use kmem_alloc()'s that operate correctly
8030Sstevel@tonic-gate  * relying on BOP_ALLOC(). This includes allocating page_ts,
8040Sstevel@tonic-gate  * page hash table, vmem initialized, etc.
8050Sstevel@tonic-gate  *
8060Sstevel@tonic-gate  * Boot's versions of physinstalled and physavail are insufficient for
8070Sstevel@tonic-gate  * the kernel's purposes. Specifically we don't know which pages that
8080Sstevel@tonic-gate  * are not in physavail can be reclaimed after boot is gone.
8090Sstevel@tonic-gate  *
8100Sstevel@tonic-gate  * This code solves the problem by dividing the address space
8110Sstevel@tonic-gate  * into 3 regions as it takes over the MMU from the booter.
8120Sstevel@tonic-gate  *
8130Sstevel@tonic-gate  * 1) Any (non-nucleus) pages that are mapped at addresses above KERNEL_TEXT
8140Sstevel@tonic-gate  * can not be used by the kernel.
8150Sstevel@tonic-gate  *
8160Sstevel@tonic-gate  * 2) Any free page that happens to be mapped below kernelbase
8170Sstevel@tonic-gate  * is protected until the boot loader is released, but will then be reclaimed.
8180Sstevel@tonic-gate  *
8190Sstevel@tonic-gate  * 3) Boot shouldn't use any address in the remaining area between kernelbase
8200Sstevel@tonic-gate  * and KERNEL_TEXT.
8210Sstevel@tonic-gate  *
8220Sstevel@tonic-gate  * In the case of multiple mappings to the same page, region 1 has precedence
8230Sstevel@tonic-gate  * over region 2.
8240Sstevel@tonic-gate  */
8250Sstevel@tonic-gate static void
8260Sstevel@tonic-gate startup_memlist(void)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate 	size_t memlist_sz;
8290Sstevel@tonic-gate 	size_t memseg_sz;
8300Sstevel@tonic-gate 	size_t pagehash_sz;
8310Sstevel@tonic-gate 	size_t pp_sz;
8320Sstevel@tonic-gate 	uintptr_t va;
8330Sstevel@tonic-gate 	size_t len;
8340Sstevel@tonic-gate 	uint_t prot;
8350Sstevel@tonic-gate 	pfn_t pfn;
8360Sstevel@tonic-gate 	int memblocks;
8370Sstevel@tonic-gate 	caddr_t pagecolor_mem;
8380Sstevel@tonic-gate 	size_t pagecolor_memsz;
8390Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
8400Sstevel@tonic-gate 	size_t page_ctrs_size;
8410Sstevel@tonic-gate 	struct memlist *current;
8420Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
8450Sstevel@tonic-gate 	extern ulong_t cr4_value;
8460Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
8470Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	/*
8520Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
8530Sstevel@tonic-gate 	 * For this to work properly, we first have to ask boot for its
8540Sstevel@tonic-gate 	 * end address.
8550Sstevel@tonic-gate 	 */
8560Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "memory-update") == 0)
8570Sstevel@tonic-gate 		(void) BOP_GETPROP(bootops, "memory-update", NULL);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	/*
8600Sstevel@tonic-gate 	 * find if the kernel is mapped on a large page
8610Sstevel@tonic-gate 	 */
8620Sstevel@tonic-gate 	va = KERNEL_TEXT;
8630Sstevel@tonic-gate 	if (hat_boot_probe(&va, &len, &pfn, &prot) == 0)
8640Sstevel@tonic-gate 		panic("Couldn't find kernel text boot mapping");
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	/*
8670Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
8680Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
8690Sstevel@tonic-gate 	 */
8700Sstevel@tonic-gate 	if (len > MMU_PAGESIZE) {
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		moddata = (caddr_t)ROUND_UP_PAGE(e_data);
8730Sstevel@tonic-gate 		e_moddata = (caddr_t)ROUND_UP_4MEG(e_data);
8740Sstevel@tonic-gate 		if (e_moddata - moddata > MODDATA)
8750Sstevel@tonic-gate 			e_moddata = moddata + MODDATA;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		modtext = (caddr_t)ROUND_UP_PAGE(e_text);
8780Sstevel@tonic-gate 		e_modtext = (caddr_t)ROUND_UP_4MEG(e_text);
8790Sstevel@tonic-gate 		if (e_modtext - modtext > MODTEXT)
8800Sstevel@tonic-gate 			e_modtext = modtext + MODTEXT;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	} else {
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 		PRM_POINT("Kernel NOT loaded on Large Page!");
8860Sstevel@tonic-gate 		e_moddata = moddata = (caddr_t)ROUND_UP_PAGE(e_data);
8870Sstevel@tonic-gate 		e_modtext = modtext = (caddr_t)ROUND_UP_PAGE(e_text);
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 	econtig = e_moddata;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	PRM_DEBUG(modtext);
8930Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
8940Sstevel@tonic-gate 	PRM_DEBUG(moddata);
8950Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
8960Sstevel@tonic-gate 	PRM_DEBUG(econtig);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	/*
8990Sstevel@tonic-gate 	 * For MP machines cr4_value must be set or the non-boot
9000Sstevel@tonic-gate 	 * CPUs will not be able to start.
9010Sstevel@tonic-gate 	 */
9020Sstevel@tonic-gate 	if (x86_feature & X86_LARGEPAGE)
9030Sstevel@tonic-gate 		cr4_value = getcr4();
9040Sstevel@tonic-gate 	PRM_DEBUG(cr4_value);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	/*
9070Sstevel@tonic-gate 	 * Examine the boot loaders physical memory map to find out:
9080Sstevel@tonic-gate 	 * - total memory in system - physinstalled
9090Sstevel@tonic-gate 	 * - the max physical address - physmax
9100Sstevel@tonic-gate 	 * - the number of segments the intsalled memory comes in
9110Sstevel@tonic-gate 	 */
9120Sstevel@tonic-gate 	if (prom_debug)
9130Sstevel@tonic-gate 		print_boot_memlist("boot physinstalled",
9140Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
9150Sstevel@tonic-gate 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
9160Sstevel@tonic-gate 	    &physinstalled, &memblocks);
9170Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9180Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9190Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	if (prom_debug)
9220Sstevel@tonic-gate 		print_boot_memlist("boot physavail",
9230Sstevel@tonic-gate 		    bootops->boot_mem->physavail);
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	/*
9260Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
9270Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
9280Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
9290Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
9300Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
9310Sstevel@tonic-gate 	 */
9320Sstevel@tonic-gate 	mmu_init();
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate #ifdef	__i386
9350Sstevel@tonic-gate 	/*
9360Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
9370Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
9380Sstevel@tonic-gate 	 */
9390Sstevel@tonic-gate 	if (mmu.pae_hat) {
9400Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
9410Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
9420Sstevel@tonic-gate 			physmax = PFN_64G - 1;
9430Sstevel@tonic-gate 		}
9440Sstevel@tonic-gate 	} else {
9450Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
9460Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
9470Sstevel@tonic-gate 			physmax = PFN_4G - 1;
9480Sstevel@tonic-gate 		}
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate #endif
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
9550Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
9560Sstevel@tonic-gate 		char value[8];
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 		if (len < 8)
9590Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
9600Sstevel@tonic-gate 		else
9610Sstevel@tonic-gate 			(void) strcpy(value, "");
9620Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
9630Sstevel@tonic-gate 			mmu.pt_nx = 0;
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	/*
9680Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
9690Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
9700Sstevel@tonic-gate 	 *
9710Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
9720Sstevel@tonic-gate 	 */
9730Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
9740Sstevel@tonic-gate 	obp_pages = 0;
9750Sstevel@tonic-gate 	va = KERNEL_TEXT;
9760Sstevel@tonic-gate 	while (hat_boot_probe(&va, &len, &pfn, &prot) != 0) {
9770Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
9780Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
9790Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
9800Sstevel@tonic-gate 		va += len;
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 	PRM_DEBUG(npages);
9830Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	/*
9860Sstevel@tonic-gate 	 * If physmem is patched to be non-zero, use it instead of
9870Sstevel@tonic-gate 	 * the computed value unless it is larger than the real
9880Sstevel@tonic-gate 	 * amount of memory on hand.
9890Sstevel@tonic-gate 	 */
9900Sstevel@tonic-gate 	if (physmem == 0 || physmem > npages)
9910Sstevel@tonic-gate 		physmem = npages;
9920Sstevel@tonic-gate 	else
9930Sstevel@tonic-gate 		npages = physmem;
9940Sstevel@tonic-gate 	PRM_DEBUG(physmem);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
9980Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
9990Sstevel@tonic-gate 	 * include:
10000Sstevel@tonic-gate 	 *	memsegs
10010Sstevel@tonic-gate 	 *	memlists
10020Sstevel@tonic-gate 	 *	page hash table
10030Sstevel@tonic-gate 	 *	page_t's
10040Sstevel@tonic-gate 	 *	page coloring data structs
10050Sstevel@tonic-gate 	 */
10060Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
10070Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
10080Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	/*
10110Sstevel@tonic-gate 	 * Reserve space for phys_avail/phys_install memlists.
10120Sstevel@tonic-gate 	 * There's no real good way to know exactly how much room we'll need,
10130Sstevel@tonic-gate 	 * but this should be a good upper bound.
10140Sstevel@tonic-gate 	 */
10150Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
10160Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
10170Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
10180Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*
10210Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
10220Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
10250Sstevel@tonic-gate 	page_hashsz = 1 << highbit(page_hashsz);
10260Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
10270Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
10280Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	/*
10310Sstevel@tonic-gate 	 * Set aside room for the page structures themselves.  Note: on
10320Sstevel@tonic-gate 	 * 64-bit systems we don't allocate page_t's for every page here.
10330Sstevel@tonic-gate 	 * We just allocate enough to map the lowest 4GB of physical
10340Sstevel@tonic-gate 	 * memory, minus those pages that are used for the "nucleus" kernel
10350Sstevel@tonic-gate 	 * text and data.  The remaining pages are allocated once we can
10360Sstevel@tonic-gate 	 * map around boot.
10370Sstevel@tonic-gate 	 *
10380Sstevel@tonic-gate 	 * boot_npages is used to allocate an area big enough for our
10390Sstevel@tonic-gate 	 * initial page_t's. kphym_init may use less than that.
10400Sstevel@tonic-gate 	 */
10410Sstevel@tonic-gate 	boot_npages = npages;
10420Sstevel@tonic-gate #if defined(__amd64)
10430Sstevel@tonic-gate 	if (npages > mmu_btop(FOURGB - (econtig - s_text)))
10440Sstevel@tonic-gate 		boot_npages = mmu_btop(FOURGB - (econtig - s_text));
10450Sstevel@tonic-gate #endif
10460Sstevel@tonic-gate 	PRM_DEBUG(boot_npages);
10470Sstevel@tonic-gate 	pp_sz = sizeof (struct page) * boot_npages;
10480Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
10490Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	/*
10520Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
10530Sstevel@tonic-gate 	 */
10540Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
10550Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
10560Sstevel@tonic-gate 	pagecolor_memsz =
10570Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
10580Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
10590Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
10620Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
10630Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	/*
10660Sstevel@tonic-gate 	 * valloc_base will be below kernel text
10670Sstevel@tonic-gate 	 * The extra pages are for the HAT and kmdb to map page tables.
10680Sstevel@tonic-gate 	 */
10690Sstevel@tonic-gate 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
10700Sstevel@tonic-gate 	valloc_base = KERNEL_TEXT - valloc_sz;
10710Sstevel@tonic-gate 	PRM_DEBUG(valloc_base);
10720Sstevel@tonic-gate 	ptable_va = valloc_base - ptable_sz;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate #if defined(__amd64)
10750Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
10760Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
10770Sstevel@tonic-gate 		    "systems.");
10780Sstevel@tonic-gate 	kernelbase = (uintptr_t)KERNELBASE;
10790Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
10800Sstevel@tonic-gate 	core_size = ptable_va - core_base;
10810Sstevel@tonic-gate #else	/* __i386 */
10820Sstevel@tonic-gate 	/*
10830Sstevel@tonic-gate 	 * We configure kernelbase based on:
10840Sstevel@tonic-gate 	 *
10850Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
10860Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
10870Sstevel@tonic-gate 	 *
10880Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
10890Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
10900Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
10910Sstevel@tonic-gate 	 *
10920Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
10930Sstevel@tonic-gate 	 */
10940Sstevel@tonic-gate 	if (eprom_kernelbase) {
10950Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
10960Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
10970Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
10980Sstevel@tonic-gate 	} else {
10990Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
11000Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
11010Sstevel@tonic-gate 	}
11020Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
11030Sstevel@tonic-gate 	core_base = ptable_va;
11040Sstevel@tonic-gate 	core_size = 0;
11050Sstevel@tonic-gate #endif
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	PRM_DEBUG(kernelbase);
11080Sstevel@tonic-gate 	PRM_DEBUG(core_base);
11090Sstevel@tonic-gate 	PRM_DEBUG(core_size);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	/*
11120Sstevel@tonic-gate 	 * At this point, we can only use a portion of the kernelheap that
11130Sstevel@tonic-gate 	 * will be available after we boot.  Both 32-bit and 64-bit systems
11140Sstevel@tonic-gate 	 * have this limitation, although the reasons are completely
11150Sstevel@tonic-gate 	 * different.
11160Sstevel@tonic-gate 	 *
11170Sstevel@tonic-gate 	 * On 64-bit systems, the booter only supports allocations in the
11180Sstevel@tonic-gate 	 * upper 4GB of memory, so we have to work with a reduced kernel
11190Sstevel@tonic-gate 	 * heap until we take over all allocations.  The booter also sits
11200Sstevel@tonic-gate 	 * in the lower portion of that 4GB range, so we have to raise the
11210Sstevel@tonic-gate 	 * bottom of the heap even further.
11220Sstevel@tonic-gate 	 *
11230Sstevel@tonic-gate 	 * On 32-bit systems we have to leave room to place segmap below
11240Sstevel@tonic-gate 	 * the heap.  We don't yet know how large segmap will be, so we
11250Sstevel@tonic-gate 	 * have to be very conservative.
11260Sstevel@tonic-gate 	 */
11270Sstevel@tonic-gate #if defined(__amd64)
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * XX64: For now, we let boot have the lower 2GB of the top 4GB
11300Sstevel@tonic-gate 	 * address range.  In the long run, that should be fixed.  It's
11310Sstevel@tonic-gate 	 * insane for a booter to need 2 2GB address ranges.
11320Sstevel@tonic-gate 	 */
11330Sstevel@tonic-gate 	boot_kernelheap = (caddr_t)(BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE);
11340Sstevel@tonic-gate 	segmap_reserved = 0;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate #else	/* __i386 */
11370Sstevel@tonic-gate 	segkp_fromheap = 1;
11380Sstevel@tonic-gate 	segmap_reserved = ROUND_UP_LPAGE(MAX(segmapsize, SEGMAPMAX));
11390Sstevel@tonic-gate 	boot_kernelheap = (caddr_t)(ROUND_UP_LPAGE(kernelbase) +
11400Sstevel@tonic-gate 	    segmap_reserved);
11410Sstevel@tonic-gate #endif
11420Sstevel@tonic-gate 	PRM_DEBUG(boot_kernelheap);
11430Sstevel@tonic-gate 	kernelheap = boot_kernelheap;
11440Sstevel@tonic-gate 	ekernelheap = (char *)core_base;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	/*
11470Sstevel@tonic-gate 	 * If segmap is too large we can push the bottom of the kernel heap
11480Sstevel@tonic-gate 	 * higher than the base.  Or worse, it could exceed the top of the
11490Sstevel@tonic-gate 	 * VA space entirely, causing it to wrap around.
11500Sstevel@tonic-gate 	 */
11510Sstevel@tonic-gate 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
11520Sstevel@tonic-gate 		panic("too little memory available for kernelheap,"
11530Sstevel@tonic-gate 			    " use a different kernelbase");
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	/*
11560Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
11570Sstevel@tonic-gate 	 * update variables that were initialized with a value of
11580Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
11590Sstevel@tonic-gate 	 *
11600Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
11610Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
11620Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
11630Sstevel@tonic-gate 	 *	just be declared as variables there?
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate #if defined(__amd64)
11670Sstevel@tonic-gate 	ASSERT(_kernelbase == KERNELBASE);
11680Sstevel@tonic-gate 	ASSERT(_userlimit == USERLIMIT);
11690Sstevel@tonic-gate 	/*
11700Sstevel@tonic-gate 	 * As one final sanity check, verify that the "red zone" between
11710Sstevel@tonic-gate 	 * kernel and userspace is exactly the size we expected.
11720Sstevel@tonic-gate 	 */
11730Sstevel@tonic-gate 	ASSERT(_kernelbase == (_userlimit + (2 * 1024 * 1024)));
11740Sstevel@tonic-gate #else
11750Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
11760Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
11770Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
11780Sstevel@tonic-gate #endif
11790Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
11800Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
11810Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 	/*
11840Sstevel@tonic-gate 	 * do all the initial allocations
11850Sstevel@tonic-gate 	 */
11860Sstevel@tonic-gate 	perform_allocations();
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	/*
11890Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
11900Sstevel@tonic-gate 	 */
11910Sstevel@tonic-gate 	kernelheap_init(kernelheap, ekernelheap, kernelheap + MMU_PAGESIZE,
11920Sstevel@tonic-gate 	    (void *)core_base, (void *)ptable_va);
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	/*
11950Sstevel@tonic-gate 	 * Build phys_install and phys_avail in kernel memspace.
11960Sstevel@tonic-gate 	 * - phys_install should be all memory in the system.
11970Sstevel@tonic-gate 	 * - phys_avail is phys_install minus any memory mapped before this
11980Sstevel@tonic-gate 	 *    point above KERNEL_TEXT.
11990Sstevel@tonic-gate 	 */
12000Sstevel@tonic-gate 	current = phys_install = memlist;
12010Sstevel@tonic-gate 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
12020Sstevel@tonic-gate 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
12030Sstevel@tonic-gate 		panic("physinstalled was too big!");
12040Sstevel@tonic-gate 	if (prom_debug)
12050Sstevel@tonic-gate 		print_kernel_memlist("phys_install", phys_install);
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	phys_avail = current;
12080Sstevel@tonic-gate 	PRM_POINT("Building phys_avail:\n");
12090Sstevel@tonic-gate 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
12100Sstevel@tonic-gate 	    avail_filter);
12110Sstevel@tonic-gate 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
12120Sstevel@tonic-gate 		panic("physavail was too big!");
12130Sstevel@tonic-gate 	if (prom_debug)
12140Sstevel@tonic-gate 		print_kernel_memlist("phys_avail", phys_avail);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	/*
12170Sstevel@tonic-gate 	 * setup page coloring
12180Sstevel@tonic-gate 	 */
12190Sstevel@tonic-gate 	page_coloring_setup(pagecolor_mem);
12200Sstevel@tonic-gate 	page_lock_init();	/* currently a no-op */
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	/*
12230Sstevel@tonic-gate 	 * free page list counters
12240Sstevel@tonic-gate 	 */
12250Sstevel@tonic-gate 	(void) page_ctrs_alloc(page_ctrs_mem);
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	/*
12280Sstevel@tonic-gate 	 * Initialize the page structures from the memory lists.
12290Sstevel@tonic-gate 	 */
12300Sstevel@tonic-gate 	availrmem_initial = availrmem = freemem = 0;
12310Sstevel@tonic-gate 	PRM_POINT("Calling kphysm_init()...");
12320Sstevel@tonic-gate 	boot_npages = kphysm_init(pp_base, memseg_base, 0, boot_npages);
12330Sstevel@tonic-gate 	PRM_POINT("kphysm_init() done");
12340Sstevel@tonic-gate 	PRM_DEBUG(boot_npages);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	/*
12370Sstevel@tonic-gate 	 * Now that page_t's have been initialized, remove all the
12380Sstevel@tonic-gate 	 * initial allocation pages from the kernel free page lists.
12390Sstevel@tonic-gate 	 */
12400Sstevel@tonic-gate 	boot_mapin((caddr_t)valloc_base, valloc_sz);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	/*
12430Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
12440Sstevel@tonic-gate 	 */
12450Sstevel@tonic-gate 	kmem_init();
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	/*
12480Sstevel@tonic-gate 	 * print this out early so that we know what's going on
12490Sstevel@tonic-gate 	 */
12500Sstevel@tonic-gate 	cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	/*
12530Sstevel@tonic-gate 	 * Initialize bp_mapin().
12540Sstevel@tonic-gate 	 */
12550Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate #if defined(__i386)
12580Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
12590Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
12600Sstevel@tonic-gate 		    "System using 0x%lx",
12610Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
12620Sstevel@tonic-gate #endif
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
12650Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
12660Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
12670Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate #endif
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	PRM_POINT("startup_memlist() done");
12720Sstevel@tonic-gate }
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate static void
12750Sstevel@tonic-gate startup_modules(void)
12760Sstevel@tonic-gate {
12770Sstevel@tonic-gate 	unsigned int i;
12780Sstevel@tonic-gate 	extern void impl_setup_ddi(void);
12790Sstevel@tonic-gate 	extern void prom_setup(void);
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
12820Sstevel@tonic-gate 	/*
12830Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
12840Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
12850Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
12860Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
12870Sstevel@tonic-gate 	 */
12880Sstevel@tonic-gate 	microfind();
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	/*
12910Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
12920Sstevel@tonic-gate 	 */
12930Sstevel@tonic-gate 	gmt_lag = process_rtc_config_file();
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
12970Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
12980Sstevel@tonic-gate 	 */
12990Sstevel@tonic-gate 	param_calc(0);
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	mod_setup();
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	/*
13040Sstevel@tonic-gate 	 * Setup machine check architecture on P6
13050Sstevel@tonic-gate 	 */
13060Sstevel@tonic-gate 	setup_mca();
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * Initialize system parameters.
13100Sstevel@tonic-gate 	 */
13110Sstevel@tonic-gate 	param_init();
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	/*
13140Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
13150Sstevel@tonic-gate 	 */
13160Sstevel@tonic-gate 	maxmem = physmem;
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	/*
13190Sstevel@tonic-gate 	 * Initialize the hat layer.
13200Sstevel@tonic-gate 	 */
13210Sstevel@tonic-gate 	hat_init();
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	/*
13240Sstevel@tonic-gate 	 * Initialize segment management stuff.
13250Sstevel@tonic-gate 	 */
13260Sstevel@tonic-gate 	seg_init();
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
13290Sstevel@tonic-gate 		halt("Can't load specfs");
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
13320Sstevel@tonic-gate 		halt("Can't load devfs");
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	dispinit();
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	/*
13370Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	if ((i = modload("misc", "sysinit")) != (unsigned int)-1)
13400Sstevel@tonic-gate 		(void) modunload(i);
13410Sstevel@tonic-gate 	else
13420Sstevel@tonic-gate 		cmn_err(CE_CONT, "sysinit load failed");
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	/* Read cluster configuration data. */
13450Sstevel@tonic-gate 	clconf_init();
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	/*
13480Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
13490Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
13500Sstevel@tonic-gate 	 */
13510Sstevel@tonic-gate 	setup_ddi();
13520Sstevel@tonic-gate 	impl_setup_ddi();
13530Sstevel@tonic-gate 	/*
13540Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
13550Sstevel@tonic-gate 	 */
13560Sstevel@tonic-gate 	prom_setup();
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	/*
13590Sstevel@tonic-gate 	 * Load all platform specific modules
13600Sstevel@tonic-gate 	 */
13610Sstevel@tonic-gate 	psm_modload();
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate static void
13670Sstevel@tonic-gate startup_bop_gone(void)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate 	PRM_POINT("startup_bop_gone() starting...");
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	/*
13720Sstevel@tonic-gate 	 * Do final allocations of HAT data structures that need to
13730Sstevel@tonic-gate 	 * be allocated before quiescing the boot loader.
13740Sstevel@tonic-gate 	 */
13750Sstevel@tonic-gate 	PRM_POINT("Calling hat_kern_alloc()...");
13760Sstevel@tonic-gate 	hat_kern_alloc();
13770Sstevel@tonic-gate 	PRM_POINT("hat_kern_alloc() done");
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	/*
13800Sstevel@tonic-gate 	 * Setup MTRR (Memory type range registers)
13810Sstevel@tonic-gate 	 */
13820Sstevel@tonic-gate 	setup_mtrr();
13830Sstevel@tonic-gate 	PRM_POINT("startup_bop_gone() done");
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate /*
13870Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
13880Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
13890Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
13900Sstevel@tonic-gate  */
13910Sstevel@tonic-gate static void
13920Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
13930Sstevel@tonic-gate {
13940Sstevel@tonic-gate 	uintptr_t va = low;
13950Sstevel@tonic-gate 	size_t len;
13960Sstevel@tonic-gate 	uint_t prot;
13970Sstevel@tonic-gate 	pfn_t pfn;
13980Sstevel@tonic-gate 	page_t *pp;
13990Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	while (hat_boot_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
14020Sstevel@tonic-gate 		if (va + len >= high)
14030Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
14040Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 		while (len > 0) {
14070Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
14080Sstevel@tonic-gate 			if (pp != NULL) {
14090Sstevel@tonic-gate 				if (setaside == 0)
14100Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
14110Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
14120Sstevel@tonic-gate 					    (void *)va, pfn);
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 				pp->p_next = bootpages;
14150Sstevel@tonic-gate 				bootpages = pp;
14160Sstevel@tonic-gate 				++boot_protect_cnt;
14170Sstevel@tonic-gate 			}
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 			++pfn;
14200Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
14210Sstevel@tonic-gate 			va += MMU_PAGESIZE;
14220Sstevel@tonic-gate 		}
14230Sstevel@tonic-gate 	}
14240Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
14250Sstevel@tonic-gate }
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate static void
14280Sstevel@tonic-gate startup_vm(void)
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate 	struct segmap_crargs a;
14310Sstevel@tonic-gate 	extern void hat_kern_setup(void);
14320Sstevel@tonic-gate 	pgcnt_t pages_left;
14330Sstevel@tonic-gate 
1434423Sdavemq 	extern int exec_lpg_disable, use_brk_lpg, use_stk_lpg, use_zmap_lpg;
1435423Sdavemq 	extern pgcnt_t auto_lpg_min_physmem;
1436423Sdavemq 
14370Sstevel@tonic-gate 	PRM_POINT("startup_vm() starting...");
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	/*
14400Sstevel@tonic-gate 	 * The next two loops are done in distinct steps in order
14410Sstevel@tonic-gate 	 * to be sure that any page that is doubly mapped (both above
14420Sstevel@tonic-gate 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
14430Sstevel@tonic-gate 	 * Note this may never happen, but it might someday.
14440Sstevel@tonic-gate 	 */
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	bootpages = NULL;
14470Sstevel@tonic-gate 	PRM_POINT("Protecting boot pages");
14480Sstevel@tonic-gate 	/*
14490Sstevel@tonic-gate 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
14500Sstevel@tonic-gate 	 * page_t's. This can only happen if something weird allocated
14510Sstevel@tonic-gate 	 * in this range (like kadb/kmdb).
14520Sstevel@tonic-gate 	 */
14530Sstevel@tonic-gate 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	/*
14560Sstevel@tonic-gate 	 * Before we can take over memory allocation/mapping from the boot
14570Sstevel@tonic-gate 	 * loader we must remove from our free page lists any boot pages that
14580Sstevel@tonic-gate 	 * will stay mapped until release_bootstrap().
14590Sstevel@tonic-gate 	 */
14600Sstevel@tonic-gate 	protect_boot_range(0, kernelbase, 1);
14610Sstevel@tonic-gate #if defined(__amd64)
14620Sstevel@tonic-gate 	protect_boot_range(BOOT_DOUBLEMAP_BASE,
14630Sstevel@tonic-gate 	    BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE, 0);
14640Sstevel@tonic-gate #endif
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	/*
14670Sstevel@tonic-gate 	 * Copy in boot's page tables, set up extra page tables for the kernel,
14680Sstevel@tonic-gate 	 * and switch to the kernel's context.
14690Sstevel@tonic-gate 	 */
14700Sstevel@tonic-gate 	PRM_POINT("Calling hat_kern_setup()...");
14710Sstevel@tonic-gate 	hat_kern_setup();
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	/*
14740Sstevel@tonic-gate 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
14750Sstevel@tonic-gate 	 */
14760Sstevel@tonic-gate 	bootops->bsys_alloc = NULL;
14770Sstevel@tonic-gate 	PRM_POINT("hat_kern_setup() done");
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	hat_cpu_online(CPU);
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	/*
14820Sstevel@tonic-gate 	 * Before we call kvm_init(), we need to establish the final size
14830Sstevel@tonic-gate 	 * of the kernel's heap.  So, we need to figure out how much space
14840Sstevel@tonic-gate 	 * to set aside for segkp, segkpm, and segmap.
14850Sstevel@tonic-gate 	 */
14860Sstevel@tonic-gate 	final_kernelheap = (caddr_t)ROUND_UP_LPAGE(kernelbase);
14870Sstevel@tonic-gate #if defined(__amd64)
14880Sstevel@tonic-gate 	if (kpm_desired) {
14890Sstevel@tonic-gate 		/*
14900Sstevel@tonic-gate 		 * Segkpm appears at the bottom of the kernel's address
14910Sstevel@tonic-gate 		 * range.  To detect accidental overruns of the user
14920Sstevel@tonic-gate 		 * address space, we leave a "red zone" of unmapped memory
14930Sstevel@tonic-gate 		 * between kernelbase and the beginning of segkpm.
14940Sstevel@tonic-gate 		 */
14950Sstevel@tonic-gate 		kpm_vbase = final_kernelheap + KERNEL_REDZONE_SIZE;
14960Sstevel@tonic-gate 		kpm_size = mmu_ptob(physmax);
14970Sstevel@tonic-gate 		PRM_DEBUG(kpm_vbase);
14980Sstevel@tonic-gate 		PRM_DEBUG(kpm_size);
14990Sstevel@tonic-gate 		final_kernelheap =
15000Sstevel@tonic-gate 		    (caddr_t)ROUND_UP_TOPLEVEL(kpm_vbase + kpm_size);
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	if (!segkp_fromheap) {
15040Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 		/*
15070Sstevel@tonic-gate 		 * determine size of segkp and adjust the bottom of the
15080Sstevel@tonic-gate 		 * kernel's heap.
15090Sstevel@tonic-gate 		 */
15100Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
15110Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
15120Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
15130Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
15140Sstevel@tonic-gate 			    mmu_btop(sz));
15150Sstevel@tonic-gate 		}
15160Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
15190Sstevel@tonic-gate 		segkp_base = final_kernelheap;
15200Sstevel@tonic-gate 		PRM_DEBUG(segkpsize);
15210Sstevel@tonic-gate 		PRM_DEBUG(segkp_base);
15220Sstevel@tonic-gate 		final_kernelheap = segkp_base + mmu_ptob(segkpsize);
15230Sstevel@tonic-gate 		PRM_DEBUG(final_kernelheap);
15240Sstevel@tonic-gate 	}
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	/*
15270Sstevel@tonic-gate 	 * put the range of VA for device mappings next
15280Sstevel@tonic-gate 	 */
15290Sstevel@tonic-gate 	toxic_addr = (uintptr_t)final_kernelheap;
15300Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
15310Sstevel@tonic-gate 	final_kernelheap = (char *)toxic_addr + toxic_size;
15320Sstevel@tonic-gate #endif
15330Sstevel@tonic-gate 	PRM_DEBUG(final_kernelheap);
15340Sstevel@tonic-gate 	ASSERT(final_kernelheap < boot_kernelheap);
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	/*
15370Sstevel@tonic-gate 	 * Users can change segmapsize through eeprom or /etc/system.
15380Sstevel@tonic-gate 	 * If the variable is tuned through eeprom, there is no upper
15390Sstevel@tonic-gate 	 * bound on the size of segmap.  If it is tuned through
15400Sstevel@tonic-gate 	 * /etc/system on 32-bit systems, it must be no larger than we
15410Sstevel@tonic-gate 	 * planned for in startup_memlist().
15420Sstevel@tonic-gate 	 */
15430Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
15440Sstevel@tonic-gate 	segkmap_start = ROUND_UP_LPAGE((uintptr_t)final_kernelheap);
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate #if defined(__i386)
15470Sstevel@tonic-gate 	if (segmapsize > segmap_reserved) {
15480Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!segmapsize may not be set > 0x%lx in "
15490Sstevel@tonic-gate 		    "/etc/system.  Use eeprom.", (long)SEGMAPMAX);
15500Sstevel@tonic-gate 		segmapsize = segmap_reserved;
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 	/*
15530Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
15540Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
15550Sstevel@tonic-gate 	 * red zone just below the start of segmap.
15560Sstevel@tonic-gate 	 */
15570Sstevel@tonic-gate 	segkmap_start += KERNEL_REDZONE_SIZE;
15580Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
15590Sstevel@tonic-gate #endif
15600Sstevel@tonic-gate 	final_kernelheap = (char *)(segkmap_start + segmapsize);
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	PRM_DEBUG(segkmap_start);
15630Sstevel@tonic-gate 	PRM_DEBUG(segmapsize);
15640Sstevel@tonic-gate 	PRM_DEBUG(final_kernelheap);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	/*
15670Sstevel@tonic-gate 	 * Initialize VM system
15680Sstevel@tonic-gate 	 */
15690Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
15700Sstevel@tonic-gate 	kvm_init();
15710Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	/*
15740Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
15750Sstevel@tonic-gate 	 */
15760Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
15770Sstevel@tonic-gate 		kdi_dvec_vmready();
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	/*
15800Sstevel@tonic-gate 	 * Mangle the brand string etc.
15810Sstevel@tonic-gate 	 */
15820Sstevel@tonic-gate 	cpuid_pass3(CPU);
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	PRM_DEBUG(final_kernelheap);
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	/*
15870Sstevel@tonic-gate 	 * Now that we can use memory outside the top 4GB (on 64-bit
15880Sstevel@tonic-gate 	 * systems) and we know the size of segmap, we can set the final
15890Sstevel@tonic-gate 	 * size of the kernel's heap.  Note: on 64-bit systems we still
15900Sstevel@tonic-gate 	 * can't touch anything in the bottom half of the top 4GB range
15910Sstevel@tonic-gate 	 * because boot still has pages mapped there.
15920Sstevel@tonic-gate 	 */
15930Sstevel@tonic-gate 	if (final_kernelheap < boot_kernelheap) {
15940Sstevel@tonic-gate 		kernelheap_extend(final_kernelheap, boot_kernelheap);
15950Sstevel@tonic-gate #if defined(__amd64)
15960Sstevel@tonic-gate 		kmem_setaside = vmem_xalloc(heap_arena, BOOT_DOUBLEMAP_SIZE,
15970Sstevel@tonic-gate 		    MMU_PAGESIZE, 0, 0, (void *)(BOOT_DOUBLEMAP_BASE),
15980Sstevel@tonic-gate 		    (void *)(BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE),
15990Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
16000Sstevel@tonic-gate 		PRM_DEBUG(kmem_setaside);
16010Sstevel@tonic-gate 		if (kmem_setaside == NULL)
16020Sstevel@tonic-gate 			panic("Could not protect boot's memory");
16030Sstevel@tonic-gate #endif
16040Sstevel@tonic-gate 	}
16050Sstevel@tonic-gate 	/*
16060Sstevel@tonic-gate 	 * Now that the kernel heap may have grown significantly, we need
16070Sstevel@tonic-gate 	 * to make all the remaining page_t's available to back that memory.
16080Sstevel@tonic-gate 	 *
16090Sstevel@tonic-gate 	 * XX64 this should probably wait till after release boot-strap too.
16100Sstevel@tonic-gate 	 */
16110Sstevel@tonic-gate 	pages_left = npages - boot_npages;
16120Sstevel@tonic-gate 	if (pages_left > 0) {
16130Sstevel@tonic-gate 		PRM_DEBUG(pages_left);
16140Sstevel@tonic-gate 		(void) kphysm_init(NULL, memseg_base, boot_npages, pages_left);
16150Sstevel@tonic-gate 	}
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate #if defined(__amd64)
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	/*
16200Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
16210Sstevel@tonic-gate 	 */
16220Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
16230Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate #else	/* __i386 */
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	/*
16280Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
16290Sstevel@tonic-gate 	 */
16300Sstevel@tonic-gate 	toxic_bit_map_len = btop((ulong_t)(ptable_va - kernelbase));
16310Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
16320Sstevel@tonic-gate 	toxic_bit_map =
16330Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
16340Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
16350Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate #endif	/* __i386 */
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	/*
16410Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
16420Sstevel@tonic-gate 	 * it, tell the debugger.
16430Sstevel@tonic-gate 	 */
16440Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
16450Sstevel@tonic-gate 		kdi_dvec_memavail();
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	/*
16480Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
16490Sstevel@tonic-gate 	 * to work around a pentium bug.
16500Sstevel@tonic-gate 	 */
16510Sstevel@tonic-gate #if !defined(__amd64)
16520Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
16530Sstevel@tonic-gate 		gate_desc_t *newidt;
16540Sstevel@tonic-gate 		desctbr_t    newidt_r;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
16570Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 		bcopy(idt0, newidt, sizeof (idt0));
16600Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
16610Sstevel@tonic-gate 		    KCS_SEL, 0, SDT_SYSIGT, SEL_KPL);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
16640Sstevel@tonic-gate 		    PROT_READ|PROT_EXEC);
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 		newidt_r.dtr_limit = sizeof (idt0) - 1;
16670Sstevel@tonic-gate 		newidt_r.dtr_base = (uintptr_t)newidt;
16680Sstevel@tonic-gate 		CPU->cpu_idt = newidt;
16690Sstevel@tonic-gate 		wr_idtr(&newidt_r);
16700Sstevel@tonic-gate 	}
16710Sstevel@tonic-gate #endif	/* !__amd64 */
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	/*
16740Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
16750Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
16760Sstevel@tonic-gate 	 */
16770Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
16780Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
16790Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	/*
16820Sstevel@tonic-gate 	 * If the following is true, someone has patched phsymem to be less
16830Sstevel@tonic-gate 	 * than the number of pages that the system actually has.  Remove
16840Sstevel@tonic-gate 	 * pages until system memory is limited to the requested amount.
16850Sstevel@tonic-gate 	 * Since we have allocated page structures for all pages, we
16860Sstevel@tonic-gate 	 * correct the amount of memory we want to remove by the size of
16870Sstevel@tonic-gate 	 * the memory used to hold page structures for the non-used pages.
16880Sstevel@tonic-gate 	 */
16890Sstevel@tonic-gate 	if (physmem < npages) {
16900Sstevel@tonic-gate 		uint_t diff;
16910Sstevel@tonic-gate 		offset_t off;
16920Sstevel@tonic-gate 		struct page *pp;
16930Sstevel@tonic-gate 		caddr_t rand_vaddr;
16940Sstevel@tonic-gate 		struct seg kseg;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 		cmn_err(CE_WARN, "limiting physmem to %lu pages", physmem);
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 		off = 0;
16990Sstevel@tonic-gate 		diff = npages - physmem;
17000Sstevel@tonic-gate 		diff -= mmu_btopr(diff * sizeof (struct page));
17010Sstevel@tonic-gate 		kseg.s_as = &kas;
17020Sstevel@tonic-gate 		while (diff--) {
17030Sstevel@tonic-gate 			rand_vaddr = (caddr_t)
17040Sstevel@tonic-gate 			    (((uintptr_t)&unused_pages_vp >> 7) ^
17050Sstevel@tonic-gate 			    (uintptr_t)((u_offset_t)off >> MMU_PAGESHIFT));
17060Sstevel@tonic-gate 			pp = page_create_va(&unused_pages_vp, off, MMU_PAGESIZE,
17070Sstevel@tonic-gate 				PG_WAIT | PG_EXCL, &kseg, rand_vaddr);
17080Sstevel@tonic-gate 			if (pp == NULL) {
17090Sstevel@tonic-gate 				panic("limited physmem too much!");
17100Sstevel@tonic-gate 				/*NOTREACHED*/
17110Sstevel@tonic-gate 			}
17120Sstevel@tonic-gate 			page_io_unlock(pp);
17130Sstevel@tonic-gate 			page_downgrade(pp);
17140Sstevel@tonic-gate 			availrmem--;
17150Sstevel@tonic-gate 			off += MMU_PAGESIZE;
17160Sstevel@tonic-gate 		}
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
17200Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
17210Sstevel@tonic-gate 
1722423Sdavemq 	/* For small memory systems disable automatic large pages. */
1723423Sdavemq 	if (physmem < auto_lpg_min_physmem) {
1724423Sdavemq 		exec_lpg_disable = 1;
1725423Sdavemq 		use_brk_lpg = 0;
1726423Sdavemq 		use_stk_lpg = 0;
1727423Sdavemq 		use_zmap_lpg = 0;
1728423Sdavemq 	}
1729423Sdavemq 
17300Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
17310Sstevel@tonic-gate 	hat_init_finish();
17320Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 	/*
17350Sstevel@tonic-gate 	 * Initialize the segkp segment type.
17360Sstevel@tonic-gate 	 */
17370Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
17380Sstevel@tonic-gate 	if (!segkp_fromheap) {
17390Sstevel@tonic-gate 		if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
17400Sstevel@tonic-gate 		    segkp) < 0) {
17410Sstevel@tonic-gate 			panic("startup: cannot attach segkp");
17420Sstevel@tonic-gate 			/*NOTREACHED*/
17430Sstevel@tonic-gate 		}
17440Sstevel@tonic-gate 	} else {
17450Sstevel@tonic-gate 		/*
17460Sstevel@tonic-gate 		 * For 32 bit x86 systems, we will have segkp under the heap.
17470Sstevel@tonic-gate 		 * There will not be a segkp segment.  We do, however, need
17480Sstevel@tonic-gate 		 * to fill in the seg structure.
17490Sstevel@tonic-gate 		 */
17500Sstevel@tonic-gate 		segkp->s_as = &kas;
17510Sstevel@tonic-gate 	}
17520Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
17530Sstevel@tonic-gate 		panic("startup: segkp_create failed");
17540Sstevel@tonic-gate 		/*NOTREACHED*/
17550Sstevel@tonic-gate 	}
17560Sstevel@tonic-gate 	PRM_DEBUG(segkp);
17570Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	/*
17600Sstevel@tonic-gate 	 * kpm segment
17610Sstevel@tonic-gate 	 */
17620Sstevel@tonic-gate 	segmap_kpm = 0;
17630Sstevel@tonic-gate 	if (kpm_desired) {
17640Sstevel@tonic-gate 		kpm_init();
17650Sstevel@tonic-gate 		kpm_enable = 1;
17660Sstevel@tonic-gate 	}
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	/*
17690Sstevel@tonic-gate 	 * Now create segmap segment.
17700Sstevel@tonic-gate 	 */
17710Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
17720Sstevel@tonic-gate 	if (seg_attach(&kas, (caddr_t)segkmap_start, segmapsize, segkmap) < 0) {
17730Sstevel@tonic-gate 		panic("cannot attach segkmap");
17740Sstevel@tonic-gate 		/*NOTREACHED*/
17750Sstevel@tonic-gate 	}
17760Sstevel@tonic-gate 	PRM_DEBUG(segkmap);
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 	/*
17790Sstevel@tonic-gate 	 * The 64 bit HAT permanently maps only segmap's page tables.
17800Sstevel@tonic-gate 	 * The 32 bit HAT maps the heap's page tables too.
17810Sstevel@tonic-gate 	 */
17820Sstevel@tonic-gate #if defined(__amd64)
17830Sstevel@tonic-gate 	hat_kmap_init(segkmap_start, segmapsize);
17840Sstevel@tonic-gate #else /* __i386 */
17850Sstevel@tonic-gate 	ASSERT(segkmap_start + segmapsize == (uintptr_t)final_kernelheap);
17860Sstevel@tonic-gate 	hat_kmap_init(segkmap_start, (uintptr_t)ekernelheap - segkmap_start);
17870Sstevel@tonic-gate #endif /* __i386 */
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
17900Sstevel@tonic-gate 	a.shmsize = 0;
17910Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	if (segmap_create(segkmap, (caddr_t)&a) != 0)
17940Sstevel@tonic-gate 		panic("segmap_create segkmap");
17950Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	segdev_init();
18000Sstevel@tonic-gate 	pmem_init();
18010Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
18020Sstevel@tonic-gate }
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate static void
18050Sstevel@tonic-gate startup_end(void)
18060Sstevel@tonic-gate {
18070Sstevel@tonic-gate 	extern void setx86isalist(void);
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	/*
18120Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
18130Sstevel@tonic-gate 	 * initialization has been done but before the clock
18140Sstevel@tonic-gate 	 * and other devices get started.
18150Sstevel@tonic-gate 	 */
18160Sstevel@tonic-gate 	kern_setup1();
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	/*
18190Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
18200Sstevel@tonic-gate 	 */
18210Sstevel@tonic-gate 	kcpc_hw_init(CPU);
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate #if defined(__amd64)
18240Sstevel@tonic-gate 	/*
18250Sstevel@tonic-gate 	 * Validate support for syscall/sysret
18260Sstevel@tonic-gate 	 * XX64 -- include SSE, SSE2, etc. here too?
18270Sstevel@tonic-gate 	 */
18280Sstevel@tonic-gate 	if ((x86_feature & X86_ASYSC) == 0) {
18290Sstevel@tonic-gate 		cmn_err(CE_WARN,
18300Sstevel@tonic-gate 		    "cpu%d does not support syscall/sysret", CPU->cpu_id);
18310Sstevel@tonic-gate 	}
18320Sstevel@tonic-gate #endif
18330Sstevel@tonic-gate 	/*
18340Sstevel@tonic-gate 	 * Configure the system.
18350Sstevel@tonic-gate 	 */
18360Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
18370Sstevel@tonic-gate 	configure();		/* set up devices */
18380Sstevel@tonic-gate 	PRM_POINT("configure() done");
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 	/*
18410Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
18420Sstevel@tonic-gate 	 * support.
18430Sstevel@tonic-gate 	 */
18440Sstevel@tonic-gate 	setx86isalist();
18450Sstevel@tonic-gate 	init_intr_threads(CPU);
18460Sstevel@tonic-gate 	psm_install();
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	/*
18490Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
18500Sstevel@tonic-gate 	 * we're still using bootsvcs.
18510Sstevel@tonic-gate 	 */
18520Sstevel@tonic-gate 	PRM_POINT("zeroing out bootops");
18530Sstevel@tonic-gate 	*bootopsp = (struct bootops *)0;
18540Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
18570Sstevel@tonic-gate 	(*picinitf)();
18580Sstevel@tonic-gate 	sti();
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
18610Sstevel@tonic-gate 		"softlevel1", NULL, NULL); /* XXX to be moved later */
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
18640Sstevel@tonic-gate }
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate extern char hw_serial[];
18670Sstevel@tonic-gate char *_hs1107 = hw_serial;
18680Sstevel@tonic-gate ulong_t  _bdhs34;
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate void
18710Sstevel@tonic-gate post_startup(void)
18720Sstevel@tonic-gate {
18730Sstevel@tonic-gate 	/*
18740Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
18750Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
18760Sstevel@tonic-gate 	 * instruction set extensions.
18770Sstevel@tonic-gate 	 */
18780Sstevel@tonic-gate 	bind_hwcap();
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	/*
1881437Smws 	 * Load the System Management BIOS into the global ksmbios handle,
1882437Smws 	 * if an SMBIOS is present on this system.
1883437Smws 	 */
1884437Smws 	ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
1885437Smws 
1886437Smws 	/*
18870Sstevel@tonic-gate 	 * Startup memory scrubber.
18880Sstevel@tonic-gate 	 */
1889437Smws 	memscrub_init();
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	/*
18920Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
18930Sstevel@tonic-gate 	 */
18940Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	/*
18970Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
18980Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
18990Sstevel@tonic-gate 	 */
19000Sstevel@tonic-gate 	(void) modload("fs", "procfs");
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate #if defined(__i386)
19030Sstevel@tonic-gate 	/*
19040Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
19050Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
19060Sstevel@tonic-gate 	 */
19070Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
19080Sstevel@tonic-gate 		halt("No working FP hardware found");
19090Sstevel@tonic-gate #endif
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	maxmem = freemem;
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	/*
19160Sstevel@tonic-gate 	 * Perform the formal initialization of the boot chip,
19170Sstevel@tonic-gate 	 * and associate the boot cpu with it.
19180Sstevel@tonic-gate 	 * This must be done after the cpu node for CPU has been
19190Sstevel@tonic-gate 	 * added to the device tree, when the necessary probing to
19200Sstevel@tonic-gate 	 * know the chip type and chip "id" is performed.
19210Sstevel@tonic-gate 	 */
19220Sstevel@tonic-gate 	chip_cpu_init(CPU);
19230Sstevel@tonic-gate 	chip_cpu_assign(CPU);
19240Sstevel@tonic-gate }
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate static int
19270Sstevel@tonic-gate pp_in_ramdisk(page_t *pp)
19280Sstevel@tonic-gate {
19290Sstevel@tonic-gate 	extern uint64_t ramdisk_start, ramdisk_end;
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 	return ((pp->p_pagenum >= btop(ramdisk_start)) &&
19320Sstevel@tonic-gate 	    (pp->p_pagenum < btopr(ramdisk_end)));
19330Sstevel@tonic-gate }
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate void
19360Sstevel@tonic-gate release_bootstrap(void)
19370Sstevel@tonic-gate {
19380Sstevel@tonic-gate 	int root_is_ramdisk;
19390Sstevel@tonic-gate 	pfn_t pfn;
19400Sstevel@tonic-gate 	page_t *pp;
19410Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
19420Sstevel@tonic-gate 	extern dev_t rootdev;
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
19450Sstevel@tonic-gate 	kobj_boot_unmountroot();
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	/*
19480Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
19490Sstevel@tonic-gate 	 */
19500Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
19510Sstevel@tonic-gate 	clear_boot_mappings(0, kernelbase);
19520Sstevel@tonic-gate #if defined(__amd64)
19530Sstevel@tonic-gate 	PRM_POINT("Unmapping upper boot pages");
19540Sstevel@tonic-gate 	clear_boot_mappings(BOOT_DOUBLEMAP_BASE,
19550Sstevel@tonic-gate 	    BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE);
19560Sstevel@tonic-gate #endif
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	/*
19590Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
19600Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
19610Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
19620Sstevel@tonic-gate 	 */
19630Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
19640Sstevel@tonic-gate 	if (!root_is_ramdisk) {
19650Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
19660Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
19670Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
19680Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
19690Sstevel@tonic-gate 	}
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
19720Sstevel@tonic-gate 	while (bootpages) {
19730Sstevel@tonic-gate 		pp = bootpages;
19740Sstevel@tonic-gate 		bootpages = pp->p_next;
19750Sstevel@tonic-gate 		if (root_is_ramdisk && pp_in_ramdisk(pp)) {
19760Sstevel@tonic-gate 			pp->p_next = rd_pages;
19770Sstevel@tonic-gate 			rd_pages = pp;
19780Sstevel@tonic-gate 			continue;
19790Sstevel@tonic-gate 		}
19800Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
19810Sstevel@tonic-gate 		page_free(pp, 1);
19820Sstevel@tonic-gate 	}
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 	/*
19850Sstevel@tonic-gate 	 * Find 1 page below 1 MB so that other processors can boot up.
19860Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
19870Sstevel@tonic-gate 	 * We should have just free'd one up.
19880Sstevel@tonic-gate 	 */
19890Sstevel@tonic-gate 	if (use_mp) {
19900Sstevel@tonic-gate 		for (pfn = 1; pfn < btop(1*1024*1024); pfn++) {
19910Sstevel@tonic-gate 			if (page_numtopp_alloc(pfn) == NULL)
19920Sstevel@tonic-gate 				continue;
19930Sstevel@tonic-gate 			rm_platter_va = i86devmap(pfn, 1,
19940Sstevel@tonic-gate 			    PROT_READ | PROT_WRITE | PROT_EXEC);
19950Sstevel@tonic-gate 			rm_platter_pa = ptob(pfn);
19960Sstevel@tonic-gate 			hat_devload(kas.a_hat,
19970Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
19980Sstevel@tonic-gate 			    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
19990Sstevel@tonic-gate 			    HAT_LOAD_NOCONSIST);
20000Sstevel@tonic-gate 			break;
20010Sstevel@tonic-gate 		}
20020Sstevel@tonic-gate 		if (pfn == btop(1*1024*1024))
20030Sstevel@tonic-gate 			panic("No page available for starting "
20040Sstevel@tonic-gate 			    "other processors");
20050Sstevel@tonic-gate 	}
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate #if defined(__amd64)
20080Sstevel@tonic-gate 	PRM_POINT("Returning boot's VA space to kernel heap");
20090Sstevel@tonic-gate 	if (kmem_setaside != NULL)
20100Sstevel@tonic-gate 		vmem_free(heap_arena, kmem_setaside, BOOT_DOUBLEMAP_SIZE);
20110Sstevel@tonic-gate #endif
20120Sstevel@tonic-gate }
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate /*
20150Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
20160Sstevel@tonic-gate  */
20170Sstevel@tonic-gate void
20180Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
20190Sstevel@tonic-gate {
20200Sstevel@tonic-gate 	pp->p_pagenum = pnum;
20210Sstevel@tonic-gate 	pp->p_mapping = NULL;
20220Sstevel@tonic-gate 	pp->p_embed = 0;
20230Sstevel@tonic-gate 	pp->p_share = 0;
20240Sstevel@tonic-gate 	pp->p_mlentry = 0;
20250Sstevel@tonic-gate }
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate /*
20280Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
20290Sstevel@tonic-gate  */
20300Sstevel@tonic-gate static pgcnt_t
20310Sstevel@tonic-gate kphysm_init(
20320Sstevel@tonic-gate 	page_t *inpp,
20330Sstevel@tonic-gate 	struct memseg *memsegp,
20340Sstevel@tonic-gate 	pgcnt_t start,
20350Sstevel@tonic-gate 	pgcnt_t npages)
20360Sstevel@tonic-gate {
20370Sstevel@tonic-gate 	struct memlist	*pmem;
20380Sstevel@tonic-gate 	struct memseg	*cur_memseg;
20390Sstevel@tonic-gate 	struct memseg	**memsegpp;
20400Sstevel@tonic-gate 	pfn_t		base_pfn;
20410Sstevel@tonic-gate 	pgcnt_t		num;
20420Sstevel@tonic-gate 	pgcnt_t		total_skipped = 0;
20430Sstevel@tonic-gate 	pgcnt_t		skipping = 0;
20440Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
20450Sstevel@tonic-gate 	pgcnt_t		largepgcnt;
20460Sstevel@tonic-gate 	uint64_t	addr;
20470Sstevel@tonic-gate 	uint64_t	size;
20480Sstevel@tonic-gate 	page_t		*pp = inpp;
20490Sstevel@tonic-gate 	int		dobreak = 0;
20500Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 	for (cur_memseg = memsegp; cur_memseg->pages != NULL; cur_memseg++);
20550Sstevel@tonic-gate 	ASSERT(cur_memseg == memsegp || start > 0);
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
20580Sstevel@tonic-gate 		/*
20590Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
20600Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
20610Sstevel@tonic-gate 		 */
20620Sstevel@tonic-gate 		addr = pmem->address;
20630Sstevel@tonic-gate 		size = pmem->size;
20640Sstevel@tonic-gate 		if (btop(addr) > physmax)
20650Sstevel@tonic-gate 			continue;
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 		/*
20680Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
20690Sstevel@tonic-gate 		 */
20700Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
20710Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
20720Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
20730Sstevel@tonic-gate 			size -= addr - pmem->address;
20740Sstevel@tonic-gate 		}
20750Sstevel@tonic-gate 
20760Sstevel@tonic-gate 		/* only process pages below physmax */
20770Sstevel@tonic-gate 		if (btop(addr + size) > physmax)
20780Sstevel@tonic-gate 			size = ptob(physmax - btop(addr));
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 		num = btop(size);
20810Sstevel@tonic-gate 		if (num == 0)
20820Sstevel@tonic-gate 			continue;
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 		if (total_skipped < start) {
20850Sstevel@tonic-gate 			if (start - total_skipped > num) {
20860Sstevel@tonic-gate 				total_skipped += num;
20870Sstevel@tonic-gate 				continue;
20880Sstevel@tonic-gate 			}
20890Sstevel@tonic-gate 			skipping = start - total_skipped;
20900Sstevel@tonic-gate 			num -= skipping;
20910Sstevel@tonic-gate 			addr += (MMU_PAGESIZE * skipping);
20920Sstevel@tonic-gate 			total_skipped = start;
20930Sstevel@tonic-gate 		}
20940Sstevel@tonic-gate 		if (num == 0)
20950Sstevel@tonic-gate 			continue;
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 		if (num > npages)
20980Sstevel@tonic-gate 			num = npages;
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 		npages -= num;
21010Sstevel@tonic-gate 		pages_done += num;
21020Sstevel@tonic-gate 		base_pfn = btop(addr);
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 		/*
21050Sstevel@tonic-gate 		 * If the caller didn't provide space for the page
21060Sstevel@tonic-gate 		 * structures, carve them out of the memseg they will
21070Sstevel@tonic-gate 		 * represent.
21080Sstevel@tonic-gate 		 */
21090Sstevel@tonic-gate 		if (pp == NULL) {
21100Sstevel@tonic-gate 			pgcnt_t pp_pgs;
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 			if (num <= 1)
21130Sstevel@tonic-gate 				continue;
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 			/*
21160Sstevel@tonic-gate 			 * Compute how many of the pages we need to use for
21170Sstevel@tonic-gate 			 * page_ts
21180Sstevel@tonic-gate 			 */
21190Sstevel@tonic-gate 			pp_pgs = (num * sizeof (page_t)) / MMU_PAGESIZE + 1;
21200Sstevel@tonic-gate 			while (mmu_ptob(pp_pgs - 1) / sizeof (page_t) >=
21210Sstevel@tonic-gate 			    num - pp_pgs + 1)
21220Sstevel@tonic-gate 				--pp_pgs;
21230Sstevel@tonic-gate 			PRM_DEBUG(pp_pgs);
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 			pp = vmem_alloc(heap_arena, mmu_ptob(pp_pgs),
21260Sstevel@tonic-gate 			    VM_NOSLEEP);
21270Sstevel@tonic-gate 			if (pp == NULL) {
21280Sstevel@tonic-gate 				cmn_err(CE_WARN, "Unable to add %ld pages to "
21290Sstevel@tonic-gate 				    "the system.", num);
21300Sstevel@tonic-gate 				continue;
21310Sstevel@tonic-gate 			}
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 			hat_devload(kas.a_hat, (void *)pp, mmu_ptob(pp_pgs),
21340Sstevel@tonic-gate 			    base_pfn, PROT_READ | PROT_WRITE | HAT_UNORDERED_OK,
21350Sstevel@tonic-gate 			    HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
21360Sstevel@tonic-gate 			bzero(pp, mmu_ptob(pp_pgs));
21370Sstevel@tonic-gate 			num -= pp_pgs;
21380Sstevel@tonic-gate 			base_pfn += pp_pgs;
21390Sstevel@tonic-gate 		}
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 		if (prom_debug)
21420Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
21430Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
21440Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 		/*
21470Sstevel@tonic-gate 		 * drop pages below ddiphysmin to simplify ddi memory
21480Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
21490Sstevel@tonic-gate 		 */
21500Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
21510Sstevel@tonic-gate 			if (base_pfn + num <= ddiphysmin) {
21520Sstevel@tonic-gate 				/* drop entire range below ddiphysmin */
21530Sstevel@tonic-gate 				continue;
21540Sstevel@tonic-gate 			}
21550Sstevel@tonic-gate 			/* adjust range to ddiphysmin */
21560Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
21570Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
21580Sstevel@tonic-gate 			base_pfn = ddiphysmin;
21590Sstevel@tonic-gate 		}
21600Sstevel@tonic-gate 		/*
21610Sstevel@tonic-gate 		 * Build the memsegs entry
21620Sstevel@tonic-gate 		 */
21630Sstevel@tonic-gate 		cur_memseg->pages = pp;
21640Sstevel@tonic-gate 		cur_memseg->epages = pp + num;
21650Sstevel@tonic-gate 		cur_memseg->pages_base = base_pfn;
21660Sstevel@tonic-gate 		cur_memseg->pages_end = base_pfn + num;
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 		/*
21690Sstevel@tonic-gate 		 * insert in memseg list in decreasing pfn range order.
21700Sstevel@tonic-gate 		 * Low memory is typically more fragmented such that this
21710Sstevel@tonic-gate 		 * ordering keeps the larger ranges at the front of the list
21720Sstevel@tonic-gate 		 * for code that searches memseg.
21730Sstevel@tonic-gate 		 */
21740Sstevel@tonic-gate 		memsegpp = &memsegs;
21750Sstevel@tonic-gate 		for (;;) {
21760Sstevel@tonic-gate 			if (*memsegpp == NULL) {
21770Sstevel@tonic-gate 				/* empty memsegs */
21780Sstevel@tonic-gate 				memsegs = cur_memseg;
21790Sstevel@tonic-gate 				break;
21800Sstevel@tonic-gate 			}
21810Sstevel@tonic-gate 			/* check for continuity with start of memsegpp */
21820Sstevel@tonic-gate 			if (cur_memseg->pages_end == (*memsegpp)->pages_base) {
21830Sstevel@tonic-gate 				if (cur_memseg->epages == (*memsegpp)->pages) {
21840Sstevel@tonic-gate 					/*
21850Sstevel@tonic-gate 					 * contiguous pfn and page_t's. Merge
21860Sstevel@tonic-gate 					 * cur_memseg into *memsegpp. Drop
21870Sstevel@tonic-gate 					 * cur_memseg
21880Sstevel@tonic-gate 					 */
21890Sstevel@tonic-gate 					(*memsegpp)->pages_base =
21900Sstevel@tonic-gate 					    cur_memseg->pages_base;
21910Sstevel@tonic-gate 					(*memsegpp)->pages =
21920Sstevel@tonic-gate 					    cur_memseg->pages;
21930Sstevel@tonic-gate 					/*
21940Sstevel@tonic-gate 					 * check if contiguous with the end of
21950Sstevel@tonic-gate 					 * the next memseg.
21960Sstevel@tonic-gate 					 */
21970Sstevel@tonic-gate 					if ((*memsegpp)->next &&
21980Sstevel@tonic-gate 					    ((*memsegpp)->pages_base ==
21990Sstevel@tonic-gate 					    (*memsegpp)->next->pages_end)) {
22000Sstevel@tonic-gate 						cur_memseg = *memsegpp;
22010Sstevel@tonic-gate 						memsegpp = &((*memsegpp)->next);
22020Sstevel@tonic-gate 						dobreak = 1;
22030Sstevel@tonic-gate 					} else {
22040Sstevel@tonic-gate 						break;
22050Sstevel@tonic-gate 					}
22060Sstevel@tonic-gate 				} else {
22070Sstevel@tonic-gate 					/*
22080Sstevel@tonic-gate 					 * contiguous pfn but not page_t's.
22090Sstevel@tonic-gate 					 * drop last pfn/page_t in cur_memseg
22100Sstevel@tonic-gate 					 * to prevent creation of large pages
22110Sstevel@tonic-gate 					 * with noncontiguous page_t's if not
22120Sstevel@tonic-gate 					 * aligned to largest page boundary.
22130Sstevel@tonic-gate 					 */
22140Sstevel@tonic-gate 					largepgcnt = page_get_pagecnt(
22150Sstevel@tonic-gate 					    page_num_pagesizes() - 1);
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 					if (cur_memseg->pages_end &
22180Sstevel@tonic-gate 					    (largepgcnt - 1)) {
22190Sstevel@tonic-gate 						num--;
22200Sstevel@tonic-gate 						cur_memseg->epages--;
22210Sstevel@tonic-gate 						cur_memseg->pages_end--;
22220Sstevel@tonic-gate 					}
22230Sstevel@tonic-gate 				}
22240Sstevel@tonic-gate 			}
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 			/* check for continuity with end of memsegpp */
22270Sstevel@tonic-gate 			if (cur_memseg->pages_base == (*memsegpp)->pages_end) {
22280Sstevel@tonic-gate 				if (cur_memseg->pages == (*memsegpp)->epages) {
22290Sstevel@tonic-gate 					/*
22300Sstevel@tonic-gate 					 * contiguous pfn and page_t's. Merge
22310Sstevel@tonic-gate 					 * cur_memseg into *memsegpp. Drop
22320Sstevel@tonic-gate 					 * cur_memseg.
22330Sstevel@tonic-gate 					 */
22340Sstevel@tonic-gate 					if (dobreak) {
22350Sstevel@tonic-gate 						/* merge previously done */
22360Sstevel@tonic-gate 						cur_memseg->pages =
22370Sstevel@tonic-gate 						    (*memsegpp)->pages;
22380Sstevel@tonic-gate 						cur_memseg->pages_base =
22390Sstevel@tonic-gate 						    (*memsegpp)->pages_base;
22400Sstevel@tonic-gate 						cur_memseg->next =
22410Sstevel@tonic-gate 						    (*memsegpp)->next;
22420Sstevel@tonic-gate 					} else {
22430Sstevel@tonic-gate 						(*memsegpp)->pages_end =
22440Sstevel@tonic-gate 						    cur_memseg->pages_end;
22450Sstevel@tonic-gate 						(*memsegpp)->epages =
22460Sstevel@tonic-gate 						    cur_memseg->epages;
22470Sstevel@tonic-gate 					}
22480Sstevel@tonic-gate 					break;
22490Sstevel@tonic-gate 				}
22500Sstevel@tonic-gate 				/*
22510Sstevel@tonic-gate 				 * contiguous pfn but not page_t's.
22520Sstevel@tonic-gate 				 * drop first pfn/page_t in cur_memseg
22530Sstevel@tonic-gate 				 * to prevent creation of large pages
22540Sstevel@tonic-gate 				 * with noncontiguous page_t's if not
22550Sstevel@tonic-gate 				 * aligned to largest page boundary.
22560Sstevel@tonic-gate 				 */
22570Sstevel@tonic-gate 				largepgcnt = page_get_pagecnt(
22580Sstevel@tonic-gate 				    page_num_pagesizes() - 1);
22590Sstevel@tonic-gate 				if (base_pfn & (largepgcnt - 1)) {
22600Sstevel@tonic-gate 					num--;
22610Sstevel@tonic-gate 					base_pfn++;
22620Sstevel@tonic-gate 					cur_memseg->pages++;
22630Sstevel@tonic-gate 					cur_memseg->pages_base++;
22640Sstevel@tonic-gate 					pp = cur_memseg->pages;
22650Sstevel@tonic-gate 				}
22660Sstevel@tonic-gate 				if (dobreak)
22670Sstevel@tonic-gate 					break;
22680Sstevel@tonic-gate 			}
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 			if (cur_memseg->pages_base >=
22710Sstevel@tonic-gate 			    (*memsegpp)->pages_end) {
22720Sstevel@tonic-gate 				cur_memseg->next = *memsegpp;
22730Sstevel@tonic-gate 				*memsegpp = cur_memseg;
22740Sstevel@tonic-gate 				break;
22750Sstevel@tonic-gate 			}
22760Sstevel@tonic-gate 			if ((*memsegpp)->next == NULL) {
22770Sstevel@tonic-gate 				cur_memseg->next = NULL;
22780Sstevel@tonic-gate 				(*memsegpp)->next = cur_memseg;
22790Sstevel@tonic-gate 				break;
22800Sstevel@tonic-gate 			}
22810Sstevel@tonic-gate 			memsegpp = &((*memsegpp)->next);
22820Sstevel@tonic-gate 			ASSERT(*memsegpp != NULL);
22830Sstevel@tonic-gate 		}
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 		/*
22860Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
22870Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
22880Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
22890Sstevel@tonic-gate 		 * it initializes them.
22900Sstevel@tonic-gate 		 */
22910Sstevel@tonic-gate 		add_physmem(pp, num, base_pfn);
22920Sstevel@tonic-gate 		cur_memseg++;
22930Sstevel@tonic-gate 		availrmem_initial += num;
22940Sstevel@tonic-gate 		availrmem += num;
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 		/*
22970Sstevel@tonic-gate 		 * If the caller provided the page frames to us, then
22980Sstevel@tonic-gate 		 * advance in that list.  Otherwise, prepare to allocate
22990Sstevel@tonic-gate 		 * our own page frames for the next memseg.
23000Sstevel@tonic-gate 		 */
23010Sstevel@tonic-gate 		pp = (inpp == NULL) ? NULL : pp + num;
23020Sstevel@tonic-gate 	}
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
23050Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
23060Sstevel@tonic-gate 	PRM_DEBUG(freemem);
23070Sstevel@tonic-gate 	build_pfn_hash();
23080Sstevel@tonic-gate 	return (pages_done);
23090Sstevel@tonic-gate }
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate /*
23120Sstevel@tonic-gate  * Kernel VM initialization.
23130Sstevel@tonic-gate  */
23140Sstevel@tonic-gate static void
23150Sstevel@tonic-gate kvm_init(void)
23160Sstevel@tonic-gate {
23170Sstevel@tonic-gate #ifdef DEBUG
23180Sstevel@tonic-gate 	extern void _start();
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	ASSERT((caddr_t)_start == s_text);
23210Sstevel@tonic-gate #endif
23220Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	/*
23250Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
23260Sstevel@tonic-gate 	 */
23270Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
23280Sstevel@tonic-gate 	as_avlinit(&kas);
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
23310Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
23340Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	/*
23370Sstevel@tonic-gate 	 * We're about to map out /boot.  This is the beginning of the
23380Sstevel@tonic-gate 	 * system resource management transition. We can no longer
23390Sstevel@tonic-gate 	 * call into /boot for I/O or memory allocations.
23400Sstevel@tonic-gate 	 *
23410Sstevel@tonic-gate 	 * XX64 - Is this still correct with kernelheap_extend() being called
23420Sstevel@tonic-gate 	 * later than this????
23430Sstevel@tonic-gate 	 */
23440Sstevel@tonic-gate 	(void) seg_attach(&kas, final_kernelheap,
23450Sstevel@tonic-gate 	    ekernelheap - final_kernelheap, &kvseg);
23460Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate #if defined(__amd64)
23490Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)core_base, core_size, &kvseg_core);
23500Sstevel@tonic-gate 	(void) segkmem_create(&kvseg_core);
23510Sstevel@tonic-gate #endif
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)SEGDEBUGBASE, (size_t)SEGDEBUGSIZE,
23540Sstevel@tonic-gate 	    &kdebugseg);
23550Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	/*
23600Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
23610Sstevel@tonic-gate 	 */
23620Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 	/*
23650Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
23660Sstevel@tonic-gate 	 */
23670Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
23680Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 	/*
23710Sstevel@tonic-gate 	 * Make data writable until end.
23720Sstevel@tonic-gate 	 */
23730Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
23740Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
23750Sstevel@tonic-gate }
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate /*
23780Sstevel@tonic-gate  * These are MTTR registers supported by P6
23790Sstevel@tonic-gate  */
23800Sstevel@tonic-gate static struct	mtrrvar	mtrrphys_arr[MAX_MTRRVAR];
23810Sstevel@tonic-gate static uint64_t mtrr64k, mtrr16k1, mtrr16k2;
23820Sstevel@tonic-gate static uint64_t mtrr4k1, mtrr4k2, mtrr4k3;
23830Sstevel@tonic-gate static uint64_t mtrr4k4, mtrr4k5, mtrr4k6;
23840Sstevel@tonic-gate static uint64_t mtrr4k7, mtrr4k8, mtrrcap;
23850Sstevel@tonic-gate uint64_t mtrrdef, pat_attr_reg;
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate /*
23880Sstevel@tonic-gate  * Disable reprogramming of MTRRs by default.
23890Sstevel@tonic-gate  */
23900Sstevel@tonic-gate int	enable_relaxed_mtrr = 0;
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate /*
23930Sstevel@tonic-gate  * These must serve for Pentium, Pentium Pro (P6/Pentium II/Pentium III)
23940Sstevel@tonic-gate  * and Pentium 4, and yes, they are named 0, 1, 2, 4, 3 in ascending
23950Sstevel@tonic-gate  * address order (starting from 0x400).  The Pentium 4 only implements
23960Sstevel@tonic-gate  * 4 sets, and while they are named 0-3 in the doc, the corresponding
23970Sstevel@tonic-gate  * names for P6 are 0,1,2,4.  So define these arrays in address order
23980Sstevel@tonic-gate  * so that they work for both pre-Pentium4 and Pentium 4 processors.
23990Sstevel@tonic-gate  */
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate static uint_t	mci_ctl[] = {REG_MC0_CTL, REG_MC1_CTL, REG_MC2_CTL,
24020Sstevel@tonic-gate 		    REG_MC4_CTL, REG_MC3_CTL};
24030Sstevel@tonic-gate static uint_t	mci_status[] = {REG_MC0_STATUS, REG_MC1_STATUS, REG_MC2_STATUS,
24040Sstevel@tonic-gate 		    REG_MC4_STATUS, REG_MC3_STATUS};
24050Sstevel@tonic-gate static uint_t	mci_addr[] = {REG_MC0_ADDR, REG_MC1_ADDR, REG_MC2_ADDR,
24060Sstevel@tonic-gate 		    REG_MC4_ADDR, REG_MC3_ADDR};
24070Sstevel@tonic-gate static int	mca_cnt;
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate void
24110Sstevel@tonic-gate setup_mca()
24120Sstevel@tonic-gate {
24130Sstevel@tonic-gate 	int 		i;
24140Sstevel@tonic-gate 	uint64_t	mca_cap;
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 	if (!(x86_feature & X86_MCA))
24170Sstevel@tonic-gate 		return;
2418*770Skucharsk 	mca_cap = rdmsr(REG_MCG_CAP);
24190Sstevel@tonic-gate 	if (mca_cap & MCG_CAP_CTL_P)
2420*770Skucharsk 		wrmsr(REG_MCG_CTL, -1ULL);	/* all ones */
24210Sstevel@tonic-gate 	mca_cnt = mca_cap & MCG_CAP_COUNT_MASK;
24220Sstevel@tonic-gate 	if (mca_cnt > P6_MCG_CAP_COUNT)
24230Sstevel@tonic-gate 		mca_cnt = P6_MCG_CAP_COUNT;
24240Sstevel@tonic-gate 	for (i = 1; i < mca_cnt; i++)
2425*770Skucharsk 		wrmsr(mci_ctl[i], -1ULL);	/* all ones */
24260Sstevel@tonic-gate 	for (i = 0; i < mca_cnt; i++)
2427*770Skucharsk 		wrmsr(mci_status[i], 0ULL);
24280Sstevel@tonic-gate 	setcr4(getcr4() | CR4_MCE);
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate }
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate int
24330Sstevel@tonic-gate mca_exception(struct regs *rp)
24340Sstevel@tonic-gate {
24350Sstevel@tonic-gate 	uint64_t	status, addr;
24360Sstevel@tonic-gate 	int		i, ret = 1, errcode, mserrcode;
24370Sstevel@tonic-gate 
2438*770Skucharsk 	status = rdmsr(REG_MCG_STATUS);
24390Sstevel@tonic-gate 	if (status & MCG_STATUS_RIPV)
24400Sstevel@tonic-gate 		ret = 0;
24410Sstevel@tonic-gate 	if (status & MCG_STATUS_EIPV)
24420Sstevel@tonic-gate 		cmn_err(CE_WARN, "MCE at 0x%lx", rp->r_pc);
2443*770Skucharsk 	wrmsr(REG_MCG_STATUS, 0ULL);
24440Sstevel@tonic-gate 	for (i = 0; i < mca_cnt; i++) {
2445*770Skucharsk 		status = rdmsr(mci_status[i]);
24460Sstevel@tonic-gate 		/*
24470Sstevel@tonic-gate 		 * If status register not valid skip this bank
24480Sstevel@tonic-gate 		 */
24490Sstevel@tonic-gate 		if (!(status & MCI_STATUS_VAL))
24500Sstevel@tonic-gate 			continue;
24510Sstevel@tonic-gate 		errcode = status & MCI_STATUS_ERRCODE;
24520Sstevel@tonic-gate 		mserrcode = (status  >> MSERRCODE_SHFT) & MCI_STATUS_ERRCODE;
24530Sstevel@tonic-gate 		if (status & MCI_STATUS_ADDRV) {
24540Sstevel@tonic-gate 			/*
24550Sstevel@tonic-gate 			 * If mci_addr contains the address where
24560Sstevel@tonic-gate 			 * error occurred, display the address
24570Sstevel@tonic-gate 			 */
2458*770Skucharsk 			addr = rdmsr(mci_addr[i]);
24590Sstevel@tonic-gate 			cmn_err(CE_WARN, "MCE: Bank %d: error code 0x%x:"\
24600Sstevel@tonic-gate 			    "addr = 0x%" PRIx64 ", model errcode = 0x%x", i,
24610Sstevel@tonic-gate 			    errcode, addr, mserrcode);
24620Sstevel@tonic-gate 		} else {
24630Sstevel@tonic-gate 			cmn_err(CE_WARN,
24640Sstevel@tonic-gate 			    "MCE: Bank %d: error code 0x%x, mserrcode = 0x%x",
24650Sstevel@tonic-gate 			    i, errcode, mserrcode);
24660Sstevel@tonic-gate 		}
2467*770Skucharsk 		wrmsr(mci_status[i], 0ULL);
24680Sstevel@tonic-gate 	}
24690Sstevel@tonic-gate 	return (ret);
24700Sstevel@tonic-gate }
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate void
24730Sstevel@tonic-gate setup_mtrr()
24740Sstevel@tonic-gate {
24750Sstevel@tonic-gate 	int i, ecx;
24760Sstevel@tonic-gate 	int vcnt;
24770Sstevel@tonic-gate 	struct	mtrrvar	*mtrrphys;
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 	if (!(x86_feature & X86_MTRR))
24800Sstevel@tonic-gate 		return;
24810Sstevel@tonic-gate 
2482*770Skucharsk 	mtrrcap = rdmsr(REG_MTRRCAP);
2483*770Skucharsk 	mtrrdef = rdmsr(REG_MTRRDEF);
24840Sstevel@tonic-gate 	if (mtrrcap & MTRRCAP_FIX) {
2485*770Skucharsk 		mtrr64k = rdmsr(REG_MTRR64K);
2486*770Skucharsk 		mtrr16k1 = rdmsr(REG_MTRR16K1);
2487*770Skucharsk 		mtrr16k2 = rdmsr(REG_MTRR16K2);
2488*770Skucharsk 		mtrr4k1 = rdmsr(REG_MTRR4K1);
2489*770Skucharsk 		mtrr4k2 = rdmsr(REG_MTRR4K2);
2490*770Skucharsk 		mtrr4k3 = rdmsr(REG_MTRR4K3);
2491*770Skucharsk 		mtrr4k4 = rdmsr(REG_MTRR4K4);
2492*770Skucharsk 		mtrr4k5 = rdmsr(REG_MTRR4K5);
2493*770Skucharsk 		mtrr4k6 = rdmsr(REG_MTRR4K6);
2494*770Skucharsk 		mtrr4k7 = rdmsr(REG_MTRR4K7);
2495*770Skucharsk 		mtrr4k8 = rdmsr(REG_MTRR4K8);
24960Sstevel@tonic-gate 	}
24970Sstevel@tonic-gate 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
24980Sstevel@tonic-gate 		vcnt = MAX_MTRRVAR;
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
25010Sstevel@tonic-gate 		i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
2502*770Skucharsk 		mtrrphys->mtrrphys_base = rdmsr(ecx);
2503*770Skucharsk 		mtrrphys->mtrrphys_mask = rdmsr(ecx + 1);
25040Sstevel@tonic-gate 		if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
25050Sstevel@tonic-gate 			mtrrphys->mtrrphys_mask &= ~MTRRPHYSMASK_V;
25060Sstevel@tonic-gate 		}
25070Sstevel@tonic-gate 	}
25080Sstevel@tonic-gate 	if (x86_feature & X86_PAT) {
25090Sstevel@tonic-gate 		if (enable_relaxed_mtrr)
25100Sstevel@tonic-gate 			mtrrdef = MTRR_TYPE_WB|MTRRDEF_FE|MTRRDEF_E;
25110Sstevel@tonic-gate 		pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
25120Sstevel@tonic-gate 	}
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	mtrr_sync();
25150Sstevel@tonic-gate }
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate /*
25180Sstevel@tonic-gate  * Sync current cpu mtrr with the incore copy of mtrr.
25190Sstevel@tonic-gate  * This function has to be invoked with interrupts disabled
25200Sstevel@tonic-gate  * Currently we do not capture other cpu's. This is invoked on cpu0
25210Sstevel@tonic-gate  * just after reading /etc/system.
25220Sstevel@tonic-gate  * On other cpu's its invoked from mp_startup().
25230Sstevel@tonic-gate  */
25240Sstevel@tonic-gate void
25250Sstevel@tonic-gate mtrr_sync()
25260Sstevel@tonic-gate {
25270Sstevel@tonic-gate 	uint_t	crvalue, cr0_orig;
25280Sstevel@tonic-gate 	int	vcnt, i, ecx;
25290Sstevel@tonic-gate 	struct	mtrrvar	*mtrrphys;
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	cr0_orig = crvalue = getcr0();
25320Sstevel@tonic-gate 	crvalue |= CR0_CD;
25330Sstevel@tonic-gate 	crvalue &= ~CR0_NW;
25340Sstevel@tonic-gate 	setcr0(crvalue);
25350Sstevel@tonic-gate 	invalidate_cache();
25360Sstevel@tonic-gate 	setcr3(getcr3());
25370Sstevel@tonic-gate 
2538*770Skucharsk 	if (x86_feature & X86_PAT)
2539*770Skucharsk 		wrmsr(REG_MTRRPAT, pat_attr_reg);
2540*770Skucharsk 
2541*770Skucharsk 	wrmsr(REG_MTRRDEF, rdmsr(REG_MTRRDEF) &
2542*770Skucharsk 	    ~((uint64_t)(uintptr_t)MTRRDEF_E));
2543*770Skucharsk 
25440Sstevel@tonic-gate 	if (mtrrcap & MTRRCAP_FIX) {
2545*770Skucharsk 		wrmsr(REG_MTRR64K, mtrr64k);
2546*770Skucharsk 		wrmsr(REG_MTRR16K1, mtrr16k1);
2547*770Skucharsk 		wrmsr(REG_MTRR16K2, mtrr16k2);
2548*770Skucharsk 		wrmsr(REG_MTRR4K1, mtrr4k1);
2549*770Skucharsk 		wrmsr(REG_MTRR4K2, mtrr4k2);
2550*770Skucharsk 		wrmsr(REG_MTRR4K3, mtrr4k3);
2551*770Skucharsk 		wrmsr(REG_MTRR4K4, mtrr4k4);
2552*770Skucharsk 		wrmsr(REG_MTRR4K5, mtrr4k5);
2553*770Skucharsk 		wrmsr(REG_MTRR4K6, mtrr4k6);
2554*770Skucharsk 		wrmsr(REG_MTRR4K7, mtrr4k7);
2555*770Skucharsk 		wrmsr(REG_MTRR4K8, mtrr4k8);
25560Sstevel@tonic-gate 	}
25570Sstevel@tonic-gate 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
25580Sstevel@tonic-gate 		vcnt = MAX_MTRRVAR;
25590Sstevel@tonic-gate 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
2560*770Skucharsk 	    i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
2561*770Skucharsk 		wrmsr(ecx, mtrrphys->mtrrphys_base);
2562*770Skucharsk 		wrmsr(ecx + 1, mtrrphys->mtrrphys_mask);
25630Sstevel@tonic-gate 	}
2564*770Skucharsk 	wrmsr(REG_MTRRDEF, mtrrdef);
25650Sstevel@tonic-gate 	setcr3(getcr3());
25660Sstevel@tonic-gate 	invalidate_cache();
25670Sstevel@tonic-gate 	setcr0(cr0_orig);
25680Sstevel@tonic-gate }
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate /*
25710Sstevel@tonic-gate  * resync mtrr so that BIOS is happy. Called from mdboot
25720Sstevel@tonic-gate  */
25730Sstevel@tonic-gate void
25740Sstevel@tonic-gate mtrr_resync()
25750Sstevel@tonic-gate {
25760Sstevel@tonic-gate 	if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
25770Sstevel@tonic-gate 		/*
25780Sstevel@tonic-gate 		 * We could have changed the default mtrr definition.
25790Sstevel@tonic-gate 		 * Put it back to uncached which is what it is at power on
25800Sstevel@tonic-gate 		 */
25810Sstevel@tonic-gate 		mtrrdef = MTRR_TYPE_UC|MTRRDEF_FE|MTRRDEF_E;
25820Sstevel@tonic-gate 		mtrr_sync();
25830Sstevel@tonic-gate 	}
25840Sstevel@tonic-gate }
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate void
25870Sstevel@tonic-gate get_system_configuration()
25880Sstevel@tonic-gate {
25890Sstevel@tonic-gate 	char	prop[32];
25900Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	if (((BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop)) ||
25930Sstevel@tonic-gate 		(BOP_GETPROP(bootops, "nodes", prop) < 0) 	||
25940Sstevel@tonic-gate 		(kobj_getvalue(prop, &nodes_ll) == -1) ||
25950Sstevel@tonic-gate 		(nodes_ll > MAXNODES))			   ||
25960Sstevel@tonic-gate 	    ((BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop)) ||
25970Sstevel@tonic-gate 		(BOP_GETPROP(bootops, "cpus_pernode", prop) < 0) ||
25980Sstevel@tonic-gate 		(kobj_getvalue(prop, &cpus_pernode_ll) == -1))) {
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
26010Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
26020Sstevel@tonic-gate 	} else {
26030Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
26040Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
26050Sstevel@tonic-gate 	}
26060Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop)) ||
26070Sstevel@tonic-gate 		(BOP_GETPROP(bootops, "kernelbase", prop) < 0) 	||
26080Sstevel@tonic-gate 		(kobj_getvalue(prop, &lvalue) == -1))
26090Sstevel@tonic-gate 			eprom_kernelbase = NULL;
26100Sstevel@tonic-gate 	else
26110Sstevel@tonic-gate 			eprom_kernelbase = (uintptr_t)lvalue;
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop)) ||
26140Sstevel@tonic-gate 	    (BOP_GETPROP(bootops, "segmapsize", prop) < 0) ||
26150Sstevel@tonic-gate 	    (kobj_getvalue(prop, &lvalue) == -1)) {
26160Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
26170Sstevel@tonic-gate 	} else {
26180Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
26190Sstevel@tonic-gate 	}
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop)) ||
26220Sstevel@tonic-gate 	    (BOP_GETPROP(bootops, "segmapfreelists", prop) < 0) ||
26230Sstevel@tonic-gate 	    (kobj_getvalue(prop, &lvalue) == -1)) {
26240Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
26250Sstevel@tonic-gate 	} else {
26260Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
26270Sstevel@tonic-gate 	}
26280Sstevel@tonic-gate }
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate /*
26310Sstevel@tonic-gate  * Add to a memory list.
26320Sstevel@tonic-gate  * start = start of new memory segment
26330Sstevel@tonic-gate  * len = length of new memory segment in bytes
26340Sstevel@tonic-gate  * new = pointer to a new struct memlist
26350Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
26360Sstevel@tonic-gate  */
26370Sstevel@tonic-gate static void
26380Sstevel@tonic-gate memlist_add(
26390Sstevel@tonic-gate 	uint64_t start,
26400Sstevel@tonic-gate 	uint64_t len,
26410Sstevel@tonic-gate 	struct memlist *new,
26420Sstevel@tonic-gate 	struct memlist **memlistp)
26430Sstevel@tonic-gate {
26440Sstevel@tonic-gate 	struct memlist *cur;
26450Sstevel@tonic-gate 	uint64_t end = start + len;
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	new->address = start;
26480Sstevel@tonic-gate 	new->size = len;
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 	cur = *memlistp;
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate 	while (cur) {
26530Sstevel@tonic-gate 		if (cur->address >= end) {
26540Sstevel@tonic-gate 			new->next = cur;
26550Sstevel@tonic-gate 			*memlistp = new;
26560Sstevel@tonic-gate 			new->prev = cur->prev;
26570Sstevel@tonic-gate 			cur->prev = new;
26580Sstevel@tonic-gate 			return;
26590Sstevel@tonic-gate 		}
26600Sstevel@tonic-gate 		ASSERT(cur->address + cur->size <= start);
26610Sstevel@tonic-gate 		if (cur->next == NULL) {
26620Sstevel@tonic-gate 			cur->next = new;
26630Sstevel@tonic-gate 			new->prev = cur;
26640Sstevel@tonic-gate 			new->next = NULL;
26650Sstevel@tonic-gate 			return;
26660Sstevel@tonic-gate 		}
26670Sstevel@tonic-gate 		memlistp = &cur->next;
26680Sstevel@tonic-gate 		cur = cur->next;
26690Sstevel@tonic-gate 	}
26700Sstevel@tonic-gate }
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate void
26730Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
26740Sstevel@tonic-gate {
26750Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
26760Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
26790Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
26800Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
26810Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
26820Sstevel@tonic-gate }
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate caddr_t
26850Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
26860Sstevel@tonic-gate {
26870Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
26880Sstevel@tonic-gate }
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate /*ARGSUSED*/
26910Sstevel@tonic-gate caddr_t
26920Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
26930Sstevel@tonic-gate {
26940Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
26950Sstevel@tonic-gate 	/*NOTREACHED*/
26960Sstevel@tonic-gate 	return (0);
26970Sstevel@tonic-gate }
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate /*ARGSUSED*/
27000Sstevel@tonic-gate void
27010Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
27020Sstevel@tonic-gate {
27030Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
27040Sstevel@tonic-gate }
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate /*
27070Sstevel@tonic-gate  * This is called just after configure() in startup().
27080Sstevel@tonic-gate  *
27090Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
27100Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
27110Sstevel@tonic-gate  * given that various parts of the instruction set may appear
27120Sstevel@tonic-gate  * and disappear between different implementations.
27130Sstevel@tonic-gate  *
27140Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
27150Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
27160Sstevel@tonic-gate  * more flexibility.
27170Sstevel@tonic-gate  *
27180Sstevel@tonic-gate  * So, we just leave this alone.
27190Sstevel@tonic-gate  */
27200Sstevel@tonic-gate void
27210Sstevel@tonic-gate setx86isalist(void)
27220Sstevel@tonic-gate {
27230Sstevel@tonic-gate 	char *tp;
27240Sstevel@tonic-gate 	size_t len;
27250Sstevel@tonic-gate 	extern char *isa_list;
27260Sstevel@tonic-gate 
27270Sstevel@tonic-gate #define	TBUFSIZE	1024
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
27300Sstevel@tonic-gate 	*tp = '\0';
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate #if defined(__amd64)
27330Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
27340Sstevel@tonic-gate #endif
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate 	switch (x86_vendor) {
27370Sstevel@tonic-gate 	case X86_VENDOR_Intel:
27380Sstevel@tonic-gate 	case X86_VENDOR_AMD:
27390Sstevel@tonic-gate 	case X86_VENDOR_TM:
27400Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
27410Sstevel@tonic-gate 			/*
27420Sstevel@tonic-gate 			 * Pentium Pro or later
27430Sstevel@tonic-gate 			 */
27440Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
27450Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
27460Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
27470Sstevel@tonic-gate 		}
27480Sstevel@tonic-gate 		/*FALLTHROUGH*/
27490Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
27500Sstevel@tonic-gate 		/*
27510Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
27520Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
27530Sstevel@tonic-gate 		 */
27540Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
27550Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
27560Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
27570Sstevel@tonic-gate 			    "+mmx pentium " : " ");
27580Sstevel@tonic-gate 		}
27590Sstevel@tonic-gate 		break;
27600Sstevel@tonic-gate 	default:
27610Sstevel@tonic-gate 		break;
27620Sstevel@tonic-gate 	}
27630Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
27640Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
27650Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
27660Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate #undef TBUFSIZE
27690Sstevel@tonic-gate }
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 
27720Sstevel@tonic-gate #ifdef __amd64
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate void *
27750Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
27760Sstevel@tonic-gate {
27770Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
27780Sstevel@tonic-gate }
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate void
27810Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
27820Sstevel@tonic-gate {
27830Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
27840Sstevel@tonic-gate }
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate #else
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate void *
27890Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
27900Sstevel@tonic-gate {
27910Sstevel@tonic-gate 	caddr_t	vaddr;
27920Sstevel@tonic-gate 	uintptr_t v;
27930Sstevel@tonic-gate 	size_t	start;
27940Sstevel@tonic-gate 	size_t	end;
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
27970Sstevel@tonic-gate 	if (vaddr == NULL)
27980Sstevel@tonic-gate 		return (NULL);
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
28010Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
28020Sstevel@tonic-gate 	ASSERT(v + size <= ptable_va);
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	start = btop(v - kernelbase);
28050Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
28060Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
28070Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 	while (start <= end) {
28100Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
28110Sstevel@tonic-gate 		++start;
28120Sstevel@tonic-gate 	}
28130Sstevel@tonic-gate 	return (vaddr);
28140Sstevel@tonic-gate }
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate void
28170Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
28180Sstevel@tonic-gate {
28190Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
28200Sstevel@tonic-gate 	size_t	start;
28210Sstevel@tonic-gate 	size_t	end;
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
28240Sstevel@tonic-gate 	ASSERT(v + size <= ptable_va);
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 	start = btop(v - kernelbase);
28270Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
28280Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
28290Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 	while (start <= end) {
28320Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
28330Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
28340Sstevel@tonic-gate 		++start;
28350Sstevel@tonic-gate 	}
28360Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
28370Sstevel@tonic-gate }
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate /*
28400Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
28410Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
28420Sstevel@tonic-gate  */
28430Sstevel@tonic-gate void *
28440Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
28450Sstevel@tonic-gate {
28460Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
28470Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
28480Sstevel@tonic-gate 	size_t start;
28490Sstevel@tonic-gate 	size_t end;
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate 	/*
28520Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
28530Sstevel@tonic-gate 	 */
28540Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
28550Sstevel@tonic-gate 		return (NULL);
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	/*
28580Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
28590Sstevel@tonic-gate 	 */
28600Sstevel@tonic-gate 	if (v >= ptable_va || eaddr < kernelbase)
28610Sstevel@tonic-gate 		return (NULL);
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	/*
28640Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
28650Sstevel@tonic-gate 	 */
28660Sstevel@tonic-gate 	if (v < kernelbase)
28670Sstevel@tonic-gate 		v = kernelbase;
28680Sstevel@tonic-gate 	start = btop(v - kernelbase);
28690Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
28700Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
28710Sstevel@tonic-gate 		end = toxic_bit_map_len;
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
28740Sstevel@tonic-gate 		return (NULL);
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 	v = kernelbase + ptob(start);
28770Sstevel@tonic-gate 	if (len != NULL)
28780Sstevel@tonic-gate 		*len = ptob(end - start);
28790Sstevel@tonic-gate 	return ((void *)v);
28800Sstevel@tonic-gate }
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate #endif
2883