xref: /illumos-gate/usr/src/uts/i86pc/os/startup.c (revision 84ab085a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/signal.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/user.h>
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate #include <sys/vm.h>
387c478bd9Sstevel@tonic-gate #include <sys/conf.h>
397c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
407c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
417c478bd9Sstevel@tonic-gate #include <sys/disp.h>
427c478bd9Sstevel@tonic-gate #include <sys/class.h>
437c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <sys/proc.h>
487c478bd9Sstevel@tonic-gate #include <sys/buf.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
537c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <sys/cred.h>
567c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
577c478bd9Sstevel@tonic-gate #include <sys/file.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
607c478bd9Sstevel@tonic-gate #include <sys/acct.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
637c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
647c478bd9Sstevel@tonic-gate #include <sys/var.h>
657c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
667c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
677c478bd9Sstevel@tonic-gate #include <sys/debug.h>
687c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
717c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
727c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
737c478bd9Sstevel@tonic-gate #include <sys/promif.h>
747c478bd9Sstevel@tonic-gate #include <sys/prom_emul.h>	/* for create_prom_prop */
757c478bd9Sstevel@tonic-gate #include <sys/modctl.h>		/* for "procfs" hack */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
787c478bd9Sstevel@tonic-gate #include <sys/frame.h>
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
817c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
827c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
837c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
847c478bd9Sstevel@tonic-gate #include <sys/psw.h>
857c478bd9Sstevel@tonic-gate #include <sys/regset.h>
867c478bd9Sstevel@tonic-gate #include <sys/clock.h>
877c478bd9Sstevel@tonic-gate #include <sys/pte.h>
887c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
897c478bd9Sstevel@tonic-gate #include <sys/tss.h>
907c478bd9Sstevel@tonic-gate #include <sys/stack.h>
917c478bd9Sstevel@tonic-gate #include <sys/trap.h>
927c478bd9Sstevel@tonic-gate #include <sys/pic.h>
937c478bd9Sstevel@tonic-gate #include <sys/fp.h>
947c478bd9Sstevel@tonic-gate #include <vm/anon.h>
957c478bd9Sstevel@tonic-gate #include <vm/as.h>
967c478bd9Sstevel@tonic-gate #include <vm/page.h>
977c478bd9Sstevel@tonic-gate #include <vm/seg.h>
987c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
997c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
1007c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
1017c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
1027c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
1037c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
1047c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
1057c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
1067c478bd9Sstevel@tonic-gate #include <sys/swap.h>
1077c478bd9Sstevel@tonic-gate #include <sys/thread.h>
1087c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
1097c478bd9Sstevel@tonic-gate #include <sys/vm_machparam.h>
1107c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
1117c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
1127c478bd9Sstevel@tonic-gate #include <vm/hat.h>
1137c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
1147c478bd9Sstevel@tonic-gate #include <sys/pmem.h>
1157c478bd9Sstevel@tonic-gate #include <sys/instance.h>
1167c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1177c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
1187c478bd9Sstevel@tonic-gate #include <sys/segments.h>
1197c478bd9Sstevel@tonic-gate #include <sys/clconf.h>
1207c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
1217c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
1227c478bd9Sstevel@tonic-gate #include <sys/prom_emul.h>
1237c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
1247c478bd9Sstevel@tonic-gate #include <sys/chip.h>
1257c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
126*84ab085aSmws #include <sys/smbios.h>
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate extern void progressbar_init(void);
1297c478bd9Sstevel@tonic-gate extern void progressbar_start(void);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1337c478bd9Sstevel@tonic-gate  * interface.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * segkp
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate extern int segkp_fromheap;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static void kvm_init(void);
1437c478bd9Sstevel@tonic-gate static void startup_init(void);
1447c478bd9Sstevel@tonic-gate static void startup_memlist(void);
1457c478bd9Sstevel@tonic-gate static void startup_modules(void);
1467c478bd9Sstevel@tonic-gate static void startup_bop_gone(void);
1477c478bd9Sstevel@tonic-gate static void startup_vm(void);
1487c478bd9Sstevel@tonic-gate static void startup_end(void);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate pgcnt_t physmem = 0;	/* memory size in pages, patch if you want less */
1547c478bd9Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate char *kobj_file_buf;
1577c478bd9Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
1607c478bd9Sstevel@tonic-gate caddr_t	rm_platter_va;
1617c478bd9Sstevel@tonic-gate uint32_t rm_platter_pa;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate uintptr_t hole_start, hole_end;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * kpm mapping window
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate caddr_t kpm_vbase;
1727c478bd9Sstevel@tonic-gate size_t  kpm_size;
1737c478bd9Sstevel@tonic-gate static int kpm_desired = 0;		/* Do we want to try to use segkpm? */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * VA range that must be preserved for boot until we release all of its
1777c478bd9Sstevel@tonic-gate  * mappings.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate #if defined(__amd64)
1807c478bd9Sstevel@tonic-gate static void *kmem_setaside;
1817c478bd9Sstevel@tonic-gate #endif
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * Configuration parameters set at boot time.
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
1907c478bd9Sstevel@tonic-gate struct bootops		**bootopsp;
1917c478bd9Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate char bootblock_fstype[16];
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
1997c478bd9Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2007c478bd9Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * VM data structures
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
2087c478bd9Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2097c478bd9Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
2107c478bd9Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2117c478bd9Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
2127c478bd9Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
2137c478bd9Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
2147c478bd9Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
2177c478bd9Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate #if defined(__amd64)
2207c478bd9Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
2217c478bd9Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
2227c478bd9Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
2237c478bd9Sstevel@tonic-gate #else
2247c478bd9Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
2257c478bd9Sstevel@tonic-gate #endif
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
2287c478bd9Sstevel@tonic-gate #if defined(__amd64)
2297c478bd9Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
2307c478bd9Sstevel@tonic-gate #else
2317c478bd9Sstevel@tonic-gate pgcnt_t segkpsize = 0;
2327c478bd9Sstevel@tonic-gate #endif
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate struct memseg *memseg_base;
2357c478bd9Sstevel@tonic-gate struct vnode unused_pages_vp;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate #define	FOURGB	0x100000000LL
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate struct memlist *memlist;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
2427c478bd9Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
2437c478bd9Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
2447c478bd9Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
2457c478bd9Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
2467c478bd9Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
2477c478bd9Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
2487c478bd9Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
2517c478bd9Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate static void memlist_add(uint64_t, uint64_t, struct memlist *,
2547c478bd9Sstevel@tonic-gate 	struct memlist **);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate static pgcnt_t kphysm_init(page_t *, struct memseg *, pgcnt_t, pgcnt_t);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * a couple useful roundup macros
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
2677c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
2687c478bd9Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
2697c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
2707c478bd9Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
2717c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOURMB_PAGESIZE))
2727c478bd9Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
2737c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
2777c478bd9Sstevel@tonic-gate  *		+-----------------------+
2787c478bd9Sstevel@tonic-gate  *		|	psm 1-1 map	|
2797c478bd9Sstevel@tonic-gate  *		|	exec args area	|
2807c478bd9Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
2817c478bd9Sstevel@tonic-gate  *		|	debugger	|
2827c478bd9Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
2837c478bd9Sstevel@tonic-gate  *		|      Kernel Data	|
2847c478bd9Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
2857c478bd9Sstevel@tonic-gate  *              |      Kernel Text	|
2867c478bd9Sstevel@tonic-gate  * 0xFE800000  -|-----------------------|- KERNEL_TEXT
2877c478bd9Sstevel@tonic-gate  * 		|     LUFS sinkhole	|
2887c478bd9Sstevel@tonic-gate  * 0xFE000000  -|-----------------------|- lufs_addr
2897c478bd9Sstevel@tonic-gate  * ---         -|-----------------------|- valloc_base + valloc_sz
2907c478bd9Sstevel@tonic-gate  * 		|   early pp structures	|
2917c478bd9Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
2927c478bd9Sstevel@tonic-gate  * 		|   page hash, etc.	|
2937c478bd9Sstevel@tonic-gate  * ---	       -|-----------------------|- valloc_base (floating)
2947c478bd9Sstevel@tonic-gate  * 		|     ptable_va    	|
2957c478bd9Sstevel@tonic-gate  * 0xFDFFE000  -|-----------------------|- ekernelheap, ptable_va
2967c478bd9Sstevel@tonic-gate  *		|			|  (segkp is an arena under the heap)
2977c478bd9Sstevel@tonic-gate  *		|			|
2987c478bd9Sstevel@tonic-gate  *		|	kvseg		|
2997c478bd9Sstevel@tonic-gate  *		|			|
3007c478bd9Sstevel@tonic-gate  *		|			|
3017c478bd9Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
3027c478bd9Sstevel@tonic-gate  * 		|        Segkmap	|
3037c478bd9Sstevel@tonic-gate  * 0xC3002000  -|-----------------------|- segkmap_start (floating)
3047c478bd9Sstevel@tonic-gate  *		|	Red Zone	|
3057c478bd9Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
3067c478bd9Sstevel@tonic-gate  *		|			|			||
3077c478bd9Sstevel@tonic-gate  *		|     Shared objects	|			\/
3087c478bd9Sstevel@tonic-gate  *		|			|
3097c478bd9Sstevel@tonic-gate  *		:			:
3107c478bd9Sstevel@tonic-gate  *		|	user data	|
3117c478bd9Sstevel@tonic-gate  *		|-----------------------|
3127c478bd9Sstevel@tonic-gate  *		|	user text	|
3137c478bd9Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
3147c478bd9Sstevel@tonic-gate  *		|	user stack	|
3157c478bd9Sstevel@tonic-gate  *		:			:
3167c478bd9Sstevel@tonic-gate  *		|	invalid		|
3177c478bd9Sstevel@tonic-gate  * 0x00000000	+-----------------------+
3187c478bd9Sstevel@tonic-gate  *
3197c478bd9Sstevel@tonic-gate  *
3207c478bd9Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
3217c478bd9Sstevel@tonic-gate  *			+-----------------------+
3227c478bd9Sstevel@tonic-gate  *			|	psm 1-1 map	|
3237c478bd9Sstevel@tonic-gate  *			|	exec args area	|
3247c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
3257c478bd9Sstevel@tonic-gate  *			|	debugger (?)	|
3267c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
3277c478bd9Sstevel@tonic-gate  *			|      unused    	|
3287c478bd9Sstevel@tonic-gate  *			+-----------------------+
3297c478bd9Sstevel@tonic-gate  *			|      Kernel Data	|
3307c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
3317c478bd9Sstevel@tonic-gate  *			|      Kernel Text	|
3327c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
3337c478bd9Sstevel@tonic-gate  * 			|     LUFS sinkhole	|
3347c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FB000000 -|-----------------------|- lufs_addr
3357c478bd9Sstevel@tonic-gate  * ---                  |-----------------------|- valloc_base + valloc_sz
3367c478bd9Sstevel@tonic-gate  * 			|   early pp structures	|
3377c478bd9Sstevel@tonic-gate  * 			|   memsegs, memlists, 	|
3387c478bd9Sstevel@tonic-gate  * 			|   page hash, etc.	|
3397c478bd9Sstevel@tonic-gate  * ---                  |-----------------------|- valloc_base
3407c478bd9Sstevel@tonic-gate  * 			|     ptable_va    	|
3417c478bd9Sstevel@tonic-gate  * ---                  |-----------------------|- ptable_va
3427c478bd9Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
3437c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
3447c478bd9Sstevel@tonic-gate  *			|	 Kernel		|
3457c478bd9Sstevel@tonic-gate  *			|	  heap		|
3467c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
3477c478bd9Sstevel@tonic-gate  *			|	 segkmap	|
3487c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- segkmap_start (floating)
3497c478bd9Sstevel@tonic-gate  *			|    device mappings	|
3507c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
3517c478bd9Sstevel@tonic-gate  *			|	  segkp		|
3527c478bd9Sstevel@tonic-gate  * ---                  |-----------------------|- segkp_base
3537c478bd9Sstevel@tonic-gate  *			|	 segkpm		|
3547c478bd9Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
3557c478bd9Sstevel@tonic-gate  *			|	Red Zone	|
3567c478bd9Sstevel@tonic-gate  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE
3577c478bd9Sstevel@tonic-gate  *			|     User stack	|- User space memory
3587c478bd9Sstevel@tonic-gate  * 			|			|
3597c478bd9Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
3607c478bd9Sstevel@tonic-gate  *			:			:
3617c478bd9Sstevel@tonic-gate  * 			|			|
3627c478bd9Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
3637c478bd9Sstevel@tonic-gate  * 			|			|
3647c478bd9Sstevel@tonic-gate  * 			| VA Hole / unused	|
3657c478bd9Sstevel@tonic-gate  * 			|			|
3667c478bd9Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
3677c478bd9Sstevel@tonic-gate  *			|			|
3687c478bd9Sstevel@tonic-gate  *			|			|
3697c478bd9Sstevel@tonic-gate  *			:			:
3707c478bd9Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
3717c478bd9Sstevel@tonic-gate  *			|			|
3727c478bd9Sstevel@tonic-gate  *			|	user data	|
3737c478bd9Sstevel@tonic-gate  *			|-----------------------|
3747c478bd9Sstevel@tonic-gate  *			|	user text	|
3757c478bd9Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
3767c478bd9Sstevel@tonic-gate  *			|	invalid		|
3777c478bd9Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
3787c478bd9Sstevel@tonic-gate  *
3797c478bd9Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
3807c478bd9Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
3817c478bd9Sstevel@tonic-gate  *
3827c478bd9Sstevel@tonic-gate  * Floating values:
3837c478bd9Sstevel@tonic-gate  *
3847c478bd9Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
3857c478bd9Sstevel@tonic-gate  * structures.  This region contains page_t structures for the lowest 4GB
3867c478bd9Sstevel@tonic-gate  * of physical memory, memsegs, memlists, and the page hash.
3877c478bd9Sstevel@tonic-gate  *
3887c478bd9Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
3897c478bd9Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
3907c478bd9Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
3917c478bd9Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
3927c478bd9Sstevel@tonic-gate  * accessed using rip-relative addressing
3937c478bd9Sstevel@tonic-gate  *
3947c478bd9Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
3957c478bd9Sstevel@tonic-gate  *
3967c478bd9Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
3977c478bd9Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
3987c478bd9Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
3997c478bd9Sstevel@tonic-gate  *
4007c478bd9Sstevel@tonic-gate  * segkmap_start: start of segmap. The length of segmap can be modified
4017c478bd9Sstevel@tonic-gate  * by changing segmapsize in /etc/system (preferred) or eeprom (deprecated).
4027c478bd9Sstevel@tonic-gate  * The default length is 16MB on 32-bit systems and 64MB on 64-bit systems.
4037c478bd9Sstevel@tonic-gate  *
4047c478bd9Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
4057c478bd9Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
4067c478bd9Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
4077c478bd9Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
4087c478bd9Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
4097c478bd9Sstevel@tonic-gate  * segkp sizes).
4107c478bd9Sstevel@tonic-gate  *
4117c478bd9Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
4127c478bd9Sstevel@tonic-gate  *	size	page_t's			size
4137c478bd9Sstevel@tonic-gate  *	----	---------	----------	-----------
4147c478bd9Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
4157c478bd9Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
4167c478bd9Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
4177c478bd9Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
4187c478bd9Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
4197c478bd9Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
4207c478bd9Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
4217c478bd9Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
4227c478bd9Sstevel@tonic-gate  *
4237c478bd9Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
4247c478bd9Sstevel@tonic-gate  * configurations above 8gb.
4257c478bd9Sstevel@tonic-gate  *
4267c478bd9Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
4277c478bd9Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
4287c478bd9Sstevel@tonic-gate  */
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate void init_intr_threads(struct cpu *);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  * Dummy spl priority masks
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate static unsigned char	dummy_cpu_pri[MAXIPL + 1] = {
4367c478bd9Sstevel@tonic-gate 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
4377c478bd9Sstevel@tonic-gate 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf
4387c478bd9Sstevel@tonic-gate };
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /* real-time-clock initialization parameters */
4417c478bd9Sstevel@tonic-gate long gmt_lag;		/* offset in seconds of gmt to local time */
4427c478bd9Sstevel@tonic-gate extern long process_rtc_config_file(void);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate char		*final_kernelheap;
4457c478bd9Sstevel@tonic-gate char		*boot_kernelheap;
4467c478bd9Sstevel@tonic-gate uintptr_t	kernelbase;
4477c478bd9Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
4487c478bd9Sstevel@tonic-gate size_t		segmapsize;
4497c478bd9Sstevel@tonic-gate static uintptr_t segmap_reserved;
4507c478bd9Sstevel@tonic-gate uintptr_t	segkmap_start;
4517c478bd9Sstevel@tonic-gate int		segmapfreelists;
4527c478bd9Sstevel@tonic-gate pgcnt_t		boot_npages;
4537c478bd9Sstevel@tonic-gate pgcnt_t		npages;
4547c478bd9Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
4557c478bd9Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
4597c478bd9Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
4607c478bd9Sstevel@tonic-gate  * the bootstrap.
4617c478bd9Sstevel@tonic-gate  */
4627c478bd9Sstevel@tonic-gate static page_t *bootpages, *rd_pages;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate struct system_hardware system_hardware;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
4687c478bd9Sstevel@tonic-gate  *
4697c478bd9Sstevel@tonic-gate  * XX64 There should only be one print routine once memlist usage between
4707c478bd9Sstevel@tonic-gate  * vmx and the kernel is cleaned up and there is a single memlist structure
4717c478bd9Sstevel@tonic-gate  * shared between kernel and boot.
4727c478bd9Sstevel@tonic-gate  */
4737c478bd9Sstevel@tonic-gate static void
4747c478bd9Sstevel@tonic-gate print_boot_memlist(char *title, struct memlist *mp)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
4777c478bd9Sstevel@tonic-gate 	while (mp != NULL)  {
4787c478bd9Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
4797c478bd9Sstevel@tonic-gate 		    mp->address, mp->size);
4807c478bd9Sstevel@tonic-gate 		mp = mp->next;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate static void
4857c478bd9Sstevel@tonic-gate print_kernel_memlist(char *title, struct memlist *mp)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
4887c478bd9Sstevel@tonic-gate 	while (mp != NULL)  {
4897c478bd9Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
4907c478bd9Sstevel@tonic-gate 		    mp->address, mp->size);
4917c478bd9Sstevel@tonic-gate 		mp = mp->next;
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
4977c478bd9Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate int	l2cache_sz = 0x80000;
5007c478bd9Sstevel@tonic-gate int	l2cache_linesz = 0x40;
5017c478bd9Sstevel@tonic-gate int	l2cache_assoc = 1;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
5057c478bd9Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate #ifdef __amd64
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate vmem_t		*device_arena;
5107c478bd9Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
5117c478bd9Sstevel@tonic-gate size_t		toxic_size = 1 * 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate #else	/* __i386 */
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
5167c478bd9Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate #endif	/* __i386 */
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate /*
5217c478bd9Sstevel@tonic-gate  * Simple boot time debug facilities
5227c478bd9Sstevel@tonic-gate  */
5237c478bd9Sstevel@tonic-gate static char *prm_dbg_str[] = {
5247c478bd9Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%x\n",
5257c478bd9Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%llx\n"
5267c478bd9Sstevel@tonic-gate };
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate int prom_debug;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate #define	PRM_DEBUG(q)	if (prom_debug) 	\
5317c478bd9Sstevel@tonic-gate 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
5327c478bd9Sstevel@tonic-gate #define	PRM_POINT(q)	if (prom_debug) 	\
5337c478bd9Sstevel@tonic-gate 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
5377c478bd9Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
5387c478bd9Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
5397c478bd9Sstevel@tonic-gate  */
5407c478bd9Sstevel@tonic-gate #define	NUM_ALLOCATIONS 7
5417c478bd9Sstevel@tonic-gate int num_allocations = 0;
5427c478bd9Sstevel@tonic-gate struct {
5437c478bd9Sstevel@tonic-gate 	void **al_ptr;
5447c478bd9Sstevel@tonic-gate 	size_t al_size;
5457c478bd9Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
5467c478bd9Sstevel@tonic-gate size_t valloc_sz = 0;
5477c478bd9Sstevel@tonic-gate uintptr_t valloc_base;
5487c478bd9Sstevel@tonic-gate extern uintptr_t ptable_va;
5497c478bd9Sstevel@tonic-gate extern size_t ptable_sz;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
5527c478bd9Sstevel@tonic-gate 		size = ROUND_UP_PAGE(size);		 		\
5537c478bd9Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
5547c478bd9Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
5557c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
5567c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
5577c478bd9Sstevel@tonic-gate 		valloc_sz += size;					\
5587c478bd9Sstevel@tonic-gate 		++num_allocations;				 	\
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate static void
5627c478bd9Sstevel@tonic-gate perform_allocations(void)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	caddr_t mem;
5657c478bd9Sstevel@tonic-gate 	int i;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, BO_NO_ALIGN);
5687c478bd9Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
5697c478bd9Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
5707c478bd9Sstevel@tonic-gate 	bzero(mem, valloc_sz);
5717c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
5727c478bd9Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
5737c478bd9Sstevel@tonic-gate 		mem += allocations[i].al_size;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * Our world looks like this at startup time.
5797c478bd9Sstevel@tonic-gate  *
5807c478bd9Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
5817c478bd9Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
5827c478bd9Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
5837c478bd9Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
5847c478bd9Sstevel@tonic-gate  *
5857c478bd9Sstevel@tonic-gate  * On the text page:
5867c478bd9Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
5877c478bd9Sstevel@tonic-gate  *
5887c478bd9Sstevel@tonic-gate  * On the data page:
5897c478bd9Sstevel@tonic-gate  * unix/genunix/krtld/module data loads and space for page_t's.
5907c478bd9Sstevel@tonic-gate  */
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate  * Machine-dependent startup code
5937c478bd9Sstevel@tonic-gate  */
5947c478bd9Sstevel@tonic-gate void
5957c478bd9Sstevel@tonic-gate startup(void)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	extern void startup_bios_disk();
5987c478bd9Sstevel@tonic-gate 	/*
5997c478bd9Sstevel@tonic-gate 	 * Make sure that nobody tries to use sekpm until we have
6007c478bd9Sstevel@tonic-gate 	 * initialized it properly.
6017c478bd9Sstevel@tonic-gate 	 */
6027c478bd9Sstevel@tonic-gate #if defined(__amd64)
6037c478bd9Sstevel@tonic-gate 	kpm_desired = kpm_enable;
6047c478bd9Sstevel@tonic-gate #endif
6057c478bd9Sstevel@tonic-gate 	kpm_enable = 0;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	progressbar_init();
6087c478bd9Sstevel@tonic-gate 	startup_init();
6097c478bd9Sstevel@tonic-gate 	startup_memlist();
6107c478bd9Sstevel@tonic-gate 	startup_modules();
6117c478bd9Sstevel@tonic-gate 	startup_bios_disk();
6127c478bd9Sstevel@tonic-gate 	startup_bop_gone();
6137c478bd9Sstevel@tonic-gate 	startup_vm();
6147c478bd9Sstevel@tonic-gate 	startup_end();
6157c478bd9Sstevel@tonic-gate 	progressbar_start();
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static void
6197c478bd9Sstevel@tonic-gate startup_init()
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/*
6247c478bd9Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
6257c478bd9Sstevel@tonic-gate 	 */
6267c478bd9Sstevel@tonic-gate 	cpuid_pass2(CPU);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/*
6317c478bd9Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
6347c478bd9Sstevel@tonic-gate 		++prom_debug;
6357c478bd9Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	get_system_configuration();
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/*
6447c478bd9Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
6477c478bd9Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
6487c478bd9Sstevel@tonic-gate 		    CPU->cpu_brandstr);
6497c478bd9Sstevel@tonic-gate 		halt("This processor is not supported by this release "
6507c478bd9Sstevel@tonic-gate 		    "of Solaris.");
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/*
6547c478bd9Sstevel@tonic-gate 	 * Set up dummy values till psm spl code installed
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 	CPU->cpu_pri_data = dummy_cpu_pri;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
6637c478bd9Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
6647c478bd9Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
6657c478bd9Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate static void
6687c478bd9Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	uintptr_t va;
6717c478bd9Sstevel@tonic-gate 	uintptr_t next_va;
6727c478bd9Sstevel@tonic-gate 	pfn_t pfn;
6737c478bd9Sstevel@tonic-gate 	uint64_t pfn_addr;
6747c478bd9Sstevel@tonic-gate 	uint64_t pfn_eaddr;
6757c478bd9Sstevel@tonic-gate 	uint_t prot;
6767c478bd9Sstevel@tonic-gate 	size_t len;
6777c478bd9Sstevel@tonic-gate 	uint_t change;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (prom_debug)
6807c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
6817c478bd9Sstevel@tonic-gate 		    *addr, *size);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/*
6847c478bd9Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
6857c478bd9Sstevel@tonic-gate 	 */
6867c478bd9Sstevel@tonic-gate 	if (*addr == 0) {
6877c478bd9Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
6887c478bd9Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * First we trim from the front of the range. Since hat_boot_probe()
6937c478bd9Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
6947c478bd9Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
6957c478bd9Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
6967c478bd9Sstevel@tonic-gate 	 * but w < v.
6977c478bd9Sstevel@tonic-gate 	 */
6987c478bd9Sstevel@tonic-gate 	do {
6997c478bd9Sstevel@tonic-gate 		change = 0;
7007c478bd9Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
7017c478bd9Sstevel@tonic-gate 		    *size > 0 && hat_boot_probe(&va, &len, &pfn, &prot) != 0;
7027c478bd9Sstevel@tonic-gate 		    va = next_va) {
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 			next_va = va + len;
7057c478bd9Sstevel@tonic-gate 			pfn_addr = ptob((uint64_t)pfn);
7067c478bd9Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
7097c478bd9Sstevel@tonic-gate 				change = 1;
7107c478bd9Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
7117c478bd9Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
7127c478bd9Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
7137c478bd9Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
7147c478bd9Sstevel@tonic-gate 				}
7157c478bd9Sstevel@tonic-gate 			}
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 		if (change && prom_debug)
7187c478bd9Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
7197c478bd9Sstevel@tonic-gate 			    *addr, *size);
7207c478bd9Sstevel@tonic-gate 	} while (change);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * Trim pages from the end of the range.
7247c478bd9Sstevel@tonic-gate 	 */
7257c478bd9Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
7267c478bd9Sstevel@tonic-gate 	    *size > 0 && hat_boot_probe(&va, &len, &pfn, &prot) != 0;
7277c478bd9Sstevel@tonic-gate 	    va = next_va) {
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		next_va = va + len;
7307c478bd9Sstevel@tonic-gate 		pfn_addr = ptob((uint64_t)pfn);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
7337c478bd9Sstevel@tonic-gate 			*size = pfn_addr - *addr;
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	if (prom_debug)
7377c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
7387c478bd9Sstevel@tonic-gate 		    *addr, *size);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate static void
7427c478bd9Sstevel@tonic-gate kpm_init()
7437c478bd9Sstevel@tonic-gate {
7447c478bd9Sstevel@tonic-gate 	struct segkpm_crargs b;
7457c478bd9Sstevel@tonic-gate 	uintptr_t start, end;
7467c478bd9Sstevel@tonic-gate 	struct memlist	*pmem;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
7507c478bd9Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
7517c478bd9Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
7527c478bd9Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
7537c478bd9Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
7547c478bd9Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
7557c478bd9Sstevel@tonic-gate 	 * pagesize is used.
7567c478bd9Sstevel@tonic-gate 	 */
7577c478bd9Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
7587c478bd9Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
7597c478bd9Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
7607c478bd9Sstevel@tonic-gate 	kpmp2pshft = 0;
7617c478bd9Sstevel@tonic-gate 	kpmpnpgs = 1;
7627c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
7657c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
7687c478bd9Sstevel@tonic-gate 		panic("cannot attach segkpm");
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
7717c478bd9Sstevel@tonic-gate 	b.nvcolors = 1;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
7747c478bd9Sstevel@tonic-gate 		panic("segkpm_create segkpm");
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/*
7797c478bd9Sstevel@tonic-gate 	 * Map each of the memsegs into the kpm segment, coalesing adjacent
7807c478bd9Sstevel@tonic-gate 	 * memsegs to allow mapping with the largest possible pages.
7817c478bd9Sstevel@tonic-gate 	 */
7827c478bd9Sstevel@tonic-gate 	pmem = phys_install;
7837c478bd9Sstevel@tonic-gate 	start = pmem->address;
7847c478bd9Sstevel@tonic-gate 	end = start + pmem->size;
7857c478bd9Sstevel@tonic-gate 	for (;;) {
7867c478bd9Sstevel@tonic-gate 		if (pmem == NULL || pmem->address > end) {
7877c478bd9Sstevel@tonic-gate 			hat_devload(kas.a_hat, kpm_vbase + start,
7887c478bd9Sstevel@tonic-gate 			    end - start, mmu_btop(start),
7897c478bd9Sstevel@tonic-gate 			    PROT_READ | PROT_WRITE,
7907c478bd9Sstevel@tonic-gate 			    HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
7917c478bd9Sstevel@tonic-gate 			if (pmem == NULL)
7927c478bd9Sstevel@tonic-gate 				break;
7937c478bd9Sstevel@tonic-gate 			start = pmem->address;
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 		end = pmem->address + pmem->size;
7967c478bd9Sstevel@tonic-gate 		pmem = pmem->next;
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate /*
8017c478bd9Sstevel@tonic-gate  * The purpose of startup memlist is to get the system to the
8027c478bd9Sstevel@tonic-gate  * point where it can use kmem_alloc()'s that operate correctly
8037c478bd9Sstevel@tonic-gate  * relying on BOP_ALLOC(). This includes allocating page_ts,
8047c478bd9Sstevel@tonic-gate  * page hash table, vmem initialized, etc.
8057c478bd9Sstevel@tonic-gate  *
8067c478bd9Sstevel@tonic-gate  * Boot's versions of physinstalled and physavail are insufficient for
8077c478bd9Sstevel@tonic-gate  * the kernel's purposes. Specifically we don't know which pages that
8087c478bd9Sstevel@tonic-gate  * are not in physavail can be reclaimed after boot is gone.
8097c478bd9Sstevel@tonic-gate  *
8107c478bd9Sstevel@tonic-gate  * This code solves the problem by dividing the address space
8117c478bd9Sstevel@tonic-gate  * into 3 regions as it takes over the MMU from the booter.
8127c478bd9Sstevel@tonic-gate  *
8137c478bd9Sstevel@tonic-gate  * 1) Any (non-nucleus) pages that are mapped at addresses above KERNEL_TEXT
8147c478bd9Sstevel@tonic-gate  * can not be used by the kernel.
8157c478bd9Sstevel@tonic-gate  *
8167c478bd9Sstevel@tonic-gate  * 2) Any free page that happens to be mapped below kernelbase
8177c478bd9Sstevel@tonic-gate  * is protected until the boot loader is released, but will then be reclaimed.
8187c478bd9Sstevel@tonic-gate  *
8197c478bd9Sstevel@tonic-gate  * 3) Boot shouldn't use any address in the remaining area between kernelbase
8207c478bd9Sstevel@tonic-gate  * and KERNEL_TEXT.
8217c478bd9Sstevel@tonic-gate  *
8227c478bd9Sstevel@tonic-gate  * In the case of multiple mappings to the same page, region 1 has precedence
8237c478bd9Sstevel@tonic-gate  * over region 2.
8247c478bd9Sstevel@tonic-gate  */
8257c478bd9Sstevel@tonic-gate static void
8267c478bd9Sstevel@tonic-gate startup_memlist(void)
8277c478bd9Sstevel@tonic-gate {
8287c478bd9Sstevel@tonic-gate 	size_t memlist_sz;
8297c478bd9Sstevel@tonic-gate 	size_t memseg_sz;
8307c478bd9Sstevel@tonic-gate 	size_t pagehash_sz;
8317c478bd9Sstevel@tonic-gate 	size_t pp_sz;
8327c478bd9Sstevel@tonic-gate 	uintptr_t va;
8337c478bd9Sstevel@tonic-gate 	size_t len;
8347c478bd9Sstevel@tonic-gate 	uint_t prot;
8357c478bd9Sstevel@tonic-gate 	pfn_t pfn;
8367c478bd9Sstevel@tonic-gate 	int memblocks;
8377c478bd9Sstevel@tonic-gate 	caddr_t pagecolor_mem;
8387c478bd9Sstevel@tonic-gate 	size_t pagecolor_memsz;
8397c478bd9Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
8407c478bd9Sstevel@tonic-gate 	size_t page_ctrs_size;
8417c478bd9Sstevel@tonic-gate 	struct memlist *current;
8427c478bd9Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
8457c478bd9Sstevel@tonic-gate 	extern ulong_t cr4_value;
8467c478bd9Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
8477c478bd9Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/*
8527c478bd9Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
8537c478bd9Sstevel@tonic-gate 	 * For this to work properly, we first have to ask boot for its
8547c478bd9Sstevel@tonic-gate 	 * end address.
8557c478bd9Sstevel@tonic-gate 	 */
8567c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "memory-update") == 0)
8577c478bd9Sstevel@tonic-gate 		(void) BOP_GETPROP(bootops, "memory-update", NULL);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	/*
8607c478bd9Sstevel@tonic-gate 	 * find if the kernel is mapped on a large page
8617c478bd9Sstevel@tonic-gate 	 */
8627c478bd9Sstevel@tonic-gate 	va = KERNEL_TEXT;
8637c478bd9Sstevel@tonic-gate 	if (hat_boot_probe(&va, &len, &pfn, &prot) == 0)
8647c478bd9Sstevel@tonic-gate 		panic("Couldn't find kernel text boot mapping");
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/*
8677c478bd9Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
8687c478bd9Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
8697c478bd9Sstevel@tonic-gate 	 */
8707c478bd9Sstevel@tonic-gate 	if (len > MMU_PAGESIZE) {
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		moddata = (caddr_t)ROUND_UP_PAGE(e_data);
8737c478bd9Sstevel@tonic-gate 		e_moddata = (caddr_t)ROUND_UP_4MEG(e_data);
8747c478bd9Sstevel@tonic-gate 		if (e_moddata - moddata > MODDATA)
8757c478bd9Sstevel@tonic-gate 			e_moddata = moddata + MODDATA;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 		modtext = (caddr_t)ROUND_UP_PAGE(e_text);
8787c478bd9Sstevel@tonic-gate 		e_modtext = (caddr_t)ROUND_UP_4MEG(e_text);
8797c478bd9Sstevel@tonic-gate 		if (e_modtext - modtext > MODTEXT)
8807c478bd9Sstevel@tonic-gate 			e_modtext = modtext + MODTEXT;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	} else {
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		PRM_POINT("Kernel NOT loaded on Large Page!");
8867c478bd9Sstevel@tonic-gate 		e_moddata = moddata = (caddr_t)ROUND_UP_PAGE(e_data);
8877c478bd9Sstevel@tonic-gate 		e_modtext = modtext = (caddr_t)ROUND_UP_PAGE(e_text);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	econtig = e_moddata;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	PRM_DEBUG(modtext);
8937c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
8947c478bd9Sstevel@tonic-gate 	PRM_DEBUG(moddata);
8957c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
8967c478bd9Sstevel@tonic-gate 	PRM_DEBUG(econtig);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	/*
8997c478bd9Sstevel@tonic-gate 	 * For MP machines cr4_value must be set or the non-boot
9007c478bd9Sstevel@tonic-gate 	 * CPUs will not be able to start.
9017c478bd9Sstevel@tonic-gate 	 */
9027c478bd9Sstevel@tonic-gate 	if (x86_feature & X86_LARGEPAGE)
9037c478bd9Sstevel@tonic-gate 		cr4_value = getcr4();
9047c478bd9Sstevel@tonic-gate 	PRM_DEBUG(cr4_value);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/*
9077c478bd9Sstevel@tonic-gate 	 * Examine the boot loaders physical memory map to find out:
9087c478bd9Sstevel@tonic-gate 	 * - total memory in system - physinstalled
9097c478bd9Sstevel@tonic-gate 	 * - the max physical address - physmax
9107c478bd9Sstevel@tonic-gate 	 * - the number of segments the intsalled memory comes in
9117c478bd9Sstevel@tonic-gate 	 */
9127c478bd9Sstevel@tonic-gate 	if (prom_debug)
9137c478bd9Sstevel@tonic-gate 		print_boot_memlist("boot physinstalled",
9147c478bd9Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
9157c478bd9Sstevel@tonic-gate 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
9167c478bd9Sstevel@tonic-gate 	    &physinstalled, &memblocks);
9177c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9187c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9197c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	if (prom_debug)
9227c478bd9Sstevel@tonic-gate 		print_boot_memlist("boot physavail",
9237c478bd9Sstevel@tonic-gate 		    bootops->boot_mem->physavail);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
9277c478bd9Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
9287c478bd9Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
9297c478bd9Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
9307c478bd9Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	mmu_init();
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate #ifdef	__i386
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
9377c478bd9Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
9387c478bd9Sstevel@tonic-gate 	 */
9397c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
9407c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
9417c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
9427c478bd9Sstevel@tonic-gate 			physmax = PFN_64G - 1;
9437c478bd9Sstevel@tonic-gate 		}
9447c478bd9Sstevel@tonic-gate 	} else {
9457c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
9467c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
9477c478bd9Sstevel@tonic-gate 			physmax = PFN_4G - 1;
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate #endif
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
9557c478bd9Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
9567c478bd9Sstevel@tonic-gate 		char value[8];
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		if (len < 8)
9597c478bd9Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
9607c478bd9Sstevel@tonic-gate 		else
9617c478bd9Sstevel@tonic-gate 			(void) strcpy(value, "");
9627c478bd9Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
9637c478bd9Sstevel@tonic-gate 			mmu.pt_nx = 0;
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	/*
9687c478bd9Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
9697c478bd9Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
9707c478bd9Sstevel@tonic-gate 	 *
9717c478bd9Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
9727c478bd9Sstevel@tonic-gate 	 */
9737c478bd9Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
9747c478bd9Sstevel@tonic-gate 	obp_pages = 0;
9757c478bd9Sstevel@tonic-gate 	va = KERNEL_TEXT;
9767c478bd9Sstevel@tonic-gate 	while (hat_boot_probe(&va, &len, &pfn, &prot) != 0) {
9777c478bd9Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
9787c478bd9Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
9797c478bd9Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
9807c478bd9Sstevel@tonic-gate 		va += len;
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
9837c478bd9Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/*
9867c478bd9Sstevel@tonic-gate 	 * If physmem is patched to be non-zero, use it instead of
9877c478bd9Sstevel@tonic-gate 	 * the computed value unless it is larger than the real
9887c478bd9Sstevel@tonic-gate 	 * amount of memory on hand.
9897c478bd9Sstevel@tonic-gate 	 */
9907c478bd9Sstevel@tonic-gate 	if (physmem == 0 || physmem > npages)
9917c478bd9Sstevel@tonic-gate 		physmem = npages;
9927c478bd9Sstevel@tonic-gate 	else
9937c478bd9Sstevel@tonic-gate 		npages = physmem;
9947c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmem);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	/*
9977c478bd9Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
9987c478bd9Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
9997c478bd9Sstevel@tonic-gate 	 * include:
10007c478bd9Sstevel@tonic-gate 	 *	memsegs
10017c478bd9Sstevel@tonic-gate 	 *	memlists
10027c478bd9Sstevel@tonic-gate 	 *	page hash table
10037c478bd9Sstevel@tonic-gate 	 *	page_t's
10047c478bd9Sstevel@tonic-gate 	 *	page coloring data structs
10057c478bd9Sstevel@tonic-gate 	 */
10067c478bd9Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
10077c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
10087c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * Reserve space for phys_avail/phys_install memlists.
10127c478bd9Sstevel@tonic-gate 	 * There's no real good way to know exactly how much room we'll need,
10137c478bd9Sstevel@tonic-gate 	 * but this should be a good upper bound.
10147c478bd9Sstevel@tonic-gate 	 */
10157c478bd9Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
10167c478bd9Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
10177c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
10187c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	/*
10217c478bd9Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
10227c478bd9Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
10257c478bd9Sstevel@tonic-gate 	page_hashsz = 1 << highbit(page_hashsz);
10267c478bd9Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
10277c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
10287c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	/*
10317c478bd9Sstevel@tonic-gate 	 * Set aside room for the page structures themselves.  Note: on
10327c478bd9Sstevel@tonic-gate 	 * 64-bit systems we don't allocate page_t's for every page here.
10337c478bd9Sstevel@tonic-gate 	 * We just allocate enough to map the lowest 4GB of physical
10347c478bd9Sstevel@tonic-gate 	 * memory, minus those pages that are used for the "nucleus" kernel
10357c478bd9Sstevel@tonic-gate 	 * text and data.  The remaining pages are allocated once we can
10367c478bd9Sstevel@tonic-gate 	 * map around boot.
10377c478bd9Sstevel@tonic-gate 	 *
10387c478bd9Sstevel@tonic-gate 	 * boot_npages is used to allocate an area big enough for our
10397c478bd9Sstevel@tonic-gate 	 * initial page_t's. kphym_init may use less than that.
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	boot_npages = npages;
10427c478bd9Sstevel@tonic-gate #if defined(__amd64)
10437c478bd9Sstevel@tonic-gate 	if (npages > mmu_btop(FOURGB - (econtig - s_text)))
10447c478bd9Sstevel@tonic-gate 		boot_npages = mmu_btop(FOURGB - (econtig - s_text));
10457c478bd9Sstevel@tonic-gate #endif
10467c478bd9Sstevel@tonic-gate 	PRM_DEBUG(boot_npages);
10477c478bd9Sstevel@tonic-gate 	pp_sz = sizeof (struct page) * boot_npages;
10487c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
10497c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	/*
10527c478bd9Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
10537c478bd9Sstevel@tonic-gate 	 */
10547c478bd9Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
10557c478bd9Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
10567c478bd9Sstevel@tonic-gate 	pagecolor_memsz =
10577c478bd9Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
10587c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
10597c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
10627c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
10637c478bd9Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	/*
10667c478bd9Sstevel@tonic-gate 	 * valloc_base will be below kernel text
10677c478bd9Sstevel@tonic-gate 	 * The extra pages are for the HAT and kmdb to map page tables.
10687c478bd9Sstevel@tonic-gate 	 */
10697c478bd9Sstevel@tonic-gate 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
10707c478bd9Sstevel@tonic-gate 	valloc_base = KERNEL_TEXT - valloc_sz;
10717c478bd9Sstevel@tonic-gate 	PRM_DEBUG(valloc_base);
10727c478bd9Sstevel@tonic-gate 	ptable_va = valloc_base - ptable_sz;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate #if defined(__amd64)
10757c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
10767c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
10777c478bd9Sstevel@tonic-gate 		    "systems.");
10787c478bd9Sstevel@tonic-gate 	kernelbase = (uintptr_t)KERNELBASE;
10797c478bd9Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
10807c478bd9Sstevel@tonic-gate 	core_size = ptable_va - core_base;
10817c478bd9Sstevel@tonic-gate #else	/* __i386 */
10827c478bd9Sstevel@tonic-gate 	/*
10837c478bd9Sstevel@tonic-gate 	 * We configure kernelbase based on:
10847c478bd9Sstevel@tonic-gate 	 *
10857c478bd9Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
10867c478bd9Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
10877c478bd9Sstevel@tonic-gate 	 *
10887c478bd9Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
10897c478bd9Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
10907c478bd9Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
10917c478bd9Sstevel@tonic-gate 	 *
10927c478bd9Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
10937c478bd9Sstevel@tonic-gate 	 */
10947c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase) {
10957c478bd9Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
10967c478bd9Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
10977c478bd9Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
10987c478bd9Sstevel@tonic-gate 	} else {
10997c478bd9Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
11007c478bd9Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
11037c478bd9Sstevel@tonic-gate 	core_base = ptable_va;
11047c478bd9Sstevel@tonic-gate 	core_size = 0;
11057c478bd9Sstevel@tonic-gate #endif
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	PRM_DEBUG(kernelbase);
11087c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_base);
11097c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_size);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/*
11127c478bd9Sstevel@tonic-gate 	 * At this point, we can only use a portion of the kernelheap that
11137c478bd9Sstevel@tonic-gate 	 * will be available after we boot.  Both 32-bit and 64-bit systems
11147c478bd9Sstevel@tonic-gate 	 * have this limitation, although the reasons are completely
11157c478bd9Sstevel@tonic-gate 	 * different.
11167c478bd9Sstevel@tonic-gate 	 *
11177c478bd9Sstevel@tonic-gate 	 * On 64-bit systems, the booter only supports allocations in the
11187c478bd9Sstevel@tonic-gate 	 * upper 4GB of memory, so we have to work with a reduced kernel
11197c478bd9Sstevel@tonic-gate 	 * heap until we take over all allocations.  The booter also sits
11207c478bd9Sstevel@tonic-gate 	 * in the lower portion of that 4GB range, so we have to raise the
11217c478bd9Sstevel@tonic-gate 	 * bottom of the heap even further.
11227c478bd9Sstevel@tonic-gate 	 *
11237c478bd9Sstevel@tonic-gate 	 * On 32-bit systems we have to leave room to place segmap below
11247c478bd9Sstevel@tonic-gate 	 * the heap.  We don't yet know how large segmap will be, so we
11257c478bd9Sstevel@tonic-gate 	 * have to be very conservative.
11267c478bd9Sstevel@tonic-gate 	 */
11277c478bd9Sstevel@tonic-gate #if defined(__amd64)
11287c478bd9Sstevel@tonic-gate 	/*
11297c478bd9Sstevel@tonic-gate 	 * XX64: For now, we let boot have the lower 2GB of the top 4GB
11307c478bd9Sstevel@tonic-gate 	 * address range.  In the long run, that should be fixed.  It's
11317c478bd9Sstevel@tonic-gate 	 * insane for a booter to need 2 2GB address ranges.
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	boot_kernelheap = (caddr_t)(BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE);
11347c478bd9Sstevel@tonic-gate 	segmap_reserved = 0;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate #else	/* __i386 */
11377c478bd9Sstevel@tonic-gate 	segkp_fromheap = 1;
11387c478bd9Sstevel@tonic-gate 	segmap_reserved = ROUND_UP_LPAGE(MAX(segmapsize, SEGMAPMAX));
11397c478bd9Sstevel@tonic-gate 	boot_kernelheap = (caddr_t)(ROUND_UP_LPAGE(kernelbase) +
11407c478bd9Sstevel@tonic-gate 	    segmap_reserved);
11417c478bd9Sstevel@tonic-gate #endif
11427c478bd9Sstevel@tonic-gate 	PRM_DEBUG(boot_kernelheap);
11437c478bd9Sstevel@tonic-gate 	kernelheap = boot_kernelheap;
11447c478bd9Sstevel@tonic-gate 	ekernelheap = (char *)core_base;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	/*
11477c478bd9Sstevel@tonic-gate 	 * If segmap is too large we can push the bottom of the kernel heap
11487c478bd9Sstevel@tonic-gate 	 * higher than the base.  Or worse, it could exceed the top of the
11497c478bd9Sstevel@tonic-gate 	 * VA space entirely, causing it to wrap around.
11507c478bd9Sstevel@tonic-gate 	 */
11517c478bd9Sstevel@tonic-gate 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
11527c478bd9Sstevel@tonic-gate 		panic("too little memory available for kernelheap,"
11537c478bd9Sstevel@tonic-gate 			    " use a different kernelbase");
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	/*
11567c478bd9Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
11577c478bd9Sstevel@tonic-gate 	 * update variables that were initialized with a value of
11587c478bd9Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
11597c478bd9Sstevel@tonic-gate 	 *
11607c478bd9Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
11617c478bd9Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
11627c478bd9Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
11637c478bd9Sstevel@tonic-gate 	 *	just be declared as variables there?
11647c478bd9Sstevel@tonic-gate 	 */
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate #if defined(__amd64)
11677c478bd9Sstevel@tonic-gate 	ASSERT(_kernelbase == KERNELBASE);
11687c478bd9Sstevel@tonic-gate 	ASSERT(_userlimit == USERLIMIT);
11697c478bd9Sstevel@tonic-gate 	/*
11707c478bd9Sstevel@tonic-gate 	 * As one final sanity check, verify that the "red zone" between
11717c478bd9Sstevel@tonic-gate 	 * kernel and userspace is exactly the size we expected.
11727c478bd9Sstevel@tonic-gate 	 */
11737c478bd9Sstevel@tonic-gate 	ASSERT(_kernelbase == (_userlimit + (2 * 1024 * 1024)));
11747c478bd9Sstevel@tonic-gate #else
11757c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
11767c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
11777c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
11787c478bd9Sstevel@tonic-gate #endif
11797c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
11807c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
11817c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	/*
11847c478bd9Sstevel@tonic-gate 	 * do all the initial allocations
11857c478bd9Sstevel@tonic-gate 	 */
11867c478bd9Sstevel@tonic-gate 	perform_allocations();
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	/*
11897c478bd9Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
11907c478bd9Sstevel@tonic-gate 	 */
11917c478bd9Sstevel@tonic-gate 	kernelheap_init(kernelheap, ekernelheap, kernelheap + MMU_PAGESIZE,
11927c478bd9Sstevel@tonic-gate 	    (void *)core_base, (void *)ptable_va);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	/*
11957c478bd9Sstevel@tonic-gate 	 * Build phys_install and phys_avail in kernel memspace.
11967c478bd9Sstevel@tonic-gate 	 * - phys_install should be all memory in the system.
11977c478bd9Sstevel@tonic-gate 	 * - phys_avail is phys_install minus any memory mapped before this
11987c478bd9Sstevel@tonic-gate 	 *    point above KERNEL_TEXT.
11997c478bd9Sstevel@tonic-gate 	 */
12007c478bd9Sstevel@tonic-gate 	current = phys_install = memlist;
12017c478bd9Sstevel@tonic-gate 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
12027c478bd9Sstevel@tonic-gate 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
12037c478bd9Sstevel@tonic-gate 		panic("physinstalled was too big!");
12047c478bd9Sstevel@tonic-gate 	if (prom_debug)
12057c478bd9Sstevel@tonic-gate 		print_kernel_memlist("phys_install", phys_install);
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	phys_avail = current;
12087c478bd9Sstevel@tonic-gate 	PRM_POINT("Building phys_avail:\n");
12097c478bd9Sstevel@tonic-gate 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
12107c478bd9Sstevel@tonic-gate 	    avail_filter);
12117c478bd9Sstevel@tonic-gate 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
12127c478bd9Sstevel@tonic-gate 		panic("physavail was too big!");
12137c478bd9Sstevel@tonic-gate 	if (prom_debug)
12147c478bd9Sstevel@tonic-gate 		print_kernel_memlist("phys_avail", phys_avail);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	/*
12177c478bd9Sstevel@tonic-gate 	 * setup page coloring
12187c478bd9Sstevel@tonic-gate 	 */
12197c478bd9Sstevel@tonic-gate 	page_coloring_setup(pagecolor_mem);
12207c478bd9Sstevel@tonic-gate 	page_lock_init();	/* currently a no-op */
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	/*
12237c478bd9Sstevel@tonic-gate 	 * free page list counters
12247c478bd9Sstevel@tonic-gate 	 */
12257c478bd9Sstevel@tonic-gate 	(void) page_ctrs_alloc(page_ctrs_mem);
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	/*
12287c478bd9Sstevel@tonic-gate 	 * Initialize the page structures from the memory lists.
12297c478bd9Sstevel@tonic-gate 	 */
12307c478bd9Sstevel@tonic-gate 	availrmem_initial = availrmem = freemem = 0;
12317c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling kphysm_init()...");
12327c478bd9Sstevel@tonic-gate 	boot_npages = kphysm_init(pp_base, memseg_base, 0, boot_npages);
12337c478bd9Sstevel@tonic-gate 	PRM_POINT("kphysm_init() done");
12347c478bd9Sstevel@tonic-gate 	PRM_DEBUG(boot_npages);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/*
12377c478bd9Sstevel@tonic-gate 	 * Now that page_t's have been initialized, remove all the
12387c478bd9Sstevel@tonic-gate 	 * initial allocation pages from the kernel free page lists.
12397c478bd9Sstevel@tonic-gate 	 */
12407c478bd9Sstevel@tonic-gate 	boot_mapin((caddr_t)valloc_base, valloc_sz);
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	/*
12437c478bd9Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
12447c478bd9Sstevel@tonic-gate 	 */
12457c478bd9Sstevel@tonic-gate 	kmem_init();
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	/*
12487c478bd9Sstevel@tonic-gate 	 * print this out early so that we know what's going on
12497c478bd9Sstevel@tonic-gate 	 */
12507c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE);
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	/*
12537c478bd9Sstevel@tonic-gate 	 * Initialize bp_mapin().
12547c478bd9Sstevel@tonic-gate 	 */
12557c478bd9Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate #if defined(__i386)
12587c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
12597c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
12607c478bd9Sstevel@tonic-gate 		    "System using 0x%lx",
12617c478bd9Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
12627c478bd9Sstevel@tonic-gate #endif
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
12657c478bd9Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
12667c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
12677c478bd9Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate #endif
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_memlist() done");
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate static void
12757c478bd9Sstevel@tonic-gate startup_modules(void)
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	unsigned int i;
12787c478bd9Sstevel@tonic-gate 	extern void impl_setup_ddi(void);
12797c478bd9Sstevel@tonic-gate 	extern void prom_setup(void);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
12827c478bd9Sstevel@tonic-gate 	/*
12837c478bd9Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
12847c478bd9Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
12857c478bd9Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
12867c478bd9Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
12877c478bd9Sstevel@tonic-gate 	 */
12887c478bd9Sstevel@tonic-gate 	microfind();
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	/*
12917c478bd9Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
12927c478bd9Sstevel@tonic-gate 	 */
12937c478bd9Sstevel@tonic-gate 	gmt_lag = process_rtc_config_file();
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	/*
12967c478bd9Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
12977c478bd9Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
12987c478bd9Sstevel@tonic-gate 	 */
12997c478bd9Sstevel@tonic-gate 	param_calc(0);
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	mod_setup();
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * Setup machine check architecture on P6
13057c478bd9Sstevel@tonic-gate 	 */
13067c478bd9Sstevel@tonic-gate 	setup_mca();
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
13097c478bd9Sstevel@tonic-gate 	 * Initialize system parameters.
13107c478bd9Sstevel@tonic-gate 	 */
13117c478bd9Sstevel@tonic-gate 	param_init();
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
13157c478bd9Sstevel@tonic-gate 	 */
13167c478bd9Sstevel@tonic-gate 	maxmem = physmem;
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	/*
13197c478bd9Sstevel@tonic-gate 	 * Initialize the hat layer.
13207c478bd9Sstevel@tonic-gate 	 */
13217c478bd9Sstevel@tonic-gate 	hat_init();
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	/*
13247c478bd9Sstevel@tonic-gate 	 * Initialize segment management stuff.
13257c478bd9Sstevel@tonic-gate 	 */
13267c478bd9Sstevel@tonic-gate 	seg_init();
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
13297c478bd9Sstevel@tonic-gate 		halt("Can't load specfs");
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
13327c478bd9Sstevel@tonic-gate 		halt("Can't load devfs");
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	dispinit();
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	/*
13377c478bd9Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
13387c478bd9Sstevel@tonic-gate 	 */
13397c478bd9Sstevel@tonic-gate 	if ((i = modload("misc", "sysinit")) != (unsigned int)-1)
13407c478bd9Sstevel@tonic-gate 		(void) modunload(i);
13417c478bd9Sstevel@tonic-gate 	else
13427c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "sysinit load failed");
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	/* Read cluster configuration data. */
13457c478bd9Sstevel@tonic-gate 	clconf_init();
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/*
13487c478bd9Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
13497c478bd9Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
13507c478bd9Sstevel@tonic-gate 	 */
13517c478bd9Sstevel@tonic-gate 	setup_ddi();
13527c478bd9Sstevel@tonic-gate 	impl_setup_ddi();
13537c478bd9Sstevel@tonic-gate 	/*
13547c478bd9Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
13557c478bd9Sstevel@tonic-gate 	 */
13567c478bd9Sstevel@tonic-gate 	prom_setup();
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	/*
13597c478bd9Sstevel@tonic-gate 	 * Load all platform specific modules
13607c478bd9Sstevel@tonic-gate 	 */
13617c478bd9Sstevel@tonic-gate 	psm_modload();
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
13647c478bd9Sstevel@tonic-gate }
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate static void
13677c478bd9Sstevel@tonic-gate startup_bop_gone(void)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_bop_gone() starting...");
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	/*
13727c478bd9Sstevel@tonic-gate 	 * Do final allocations of HAT data structures that need to
13737c478bd9Sstevel@tonic-gate 	 * be allocated before quiescing the boot loader.
13747c478bd9Sstevel@tonic-gate 	 */
13757c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling hat_kern_alloc()...");
13767c478bd9Sstevel@tonic-gate 	hat_kern_alloc();
13777c478bd9Sstevel@tonic-gate 	PRM_POINT("hat_kern_alloc() done");
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	/*
13807c478bd9Sstevel@tonic-gate 	 * Setup MTRR (Memory type range registers)
13817c478bd9Sstevel@tonic-gate 	 */
13827c478bd9Sstevel@tonic-gate 	setup_mtrr();
13837c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_bop_gone() done");
13847c478bd9Sstevel@tonic-gate }
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate /*
13877c478bd9Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
13887c478bd9Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
13897c478bd9Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
13907c478bd9Sstevel@tonic-gate  */
13917c478bd9Sstevel@tonic-gate static void
13927c478bd9Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
13937c478bd9Sstevel@tonic-gate {
13947c478bd9Sstevel@tonic-gate 	uintptr_t va = low;
13957c478bd9Sstevel@tonic-gate 	size_t len;
13967c478bd9Sstevel@tonic-gate 	uint_t prot;
13977c478bd9Sstevel@tonic-gate 	pfn_t pfn;
13987c478bd9Sstevel@tonic-gate 	page_t *pp;
13997c478bd9Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	while (hat_boot_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
14027c478bd9Sstevel@tonic-gate 		if (va + len >= high)
14037c478bd9Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
14047c478bd9Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 		while (len > 0) {
14077c478bd9Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
14087c478bd9Sstevel@tonic-gate 			if (pp != NULL) {
14097c478bd9Sstevel@tonic-gate 				if (setaside == 0)
14107c478bd9Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
14117c478bd9Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
14127c478bd9Sstevel@tonic-gate 					    (void *)va, pfn);
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 				pp->p_next = bootpages;
14157c478bd9Sstevel@tonic-gate 				bootpages = pp;
14167c478bd9Sstevel@tonic-gate 				++boot_protect_cnt;
14177c478bd9Sstevel@tonic-gate 			}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 			++pfn;
14207c478bd9Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
14217c478bd9Sstevel@tonic-gate 			va += MMU_PAGESIZE;
14227c478bd9Sstevel@tonic-gate 		}
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate static void
14287c478bd9Sstevel@tonic-gate startup_vm(void)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate 	struct segmap_crargs a;
14317c478bd9Sstevel@tonic-gate 	extern void hat_kern_setup(void);
14327c478bd9Sstevel@tonic-gate 	pgcnt_t pages_left;
14337c478bd9Sstevel@tonic-gate 
1434beb1bda0Sdavemq 	extern int exec_lpg_disable, use_brk_lpg, use_stk_lpg, use_zmap_lpg;
1435beb1bda0Sdavemq 	extern pgcnt_t auto_lpg_min_physmem;
1436beb1bda0Sdavemq 
14377c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_vm() starting...");
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	/*
14407c478bd9Sstevel@tonic-gate 	 * The next two loops are done in distinct steps in order
14417c478bd9Sstevel@tonic-gate 	 * to be sure that any page that is doubly mapped (both above
14427c478bd9Sstevel@tonic-gate 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
14437c478bd9Sstevel@tonic-gate 	 * Note this may never happen, but it might someday.
14447c478bd9Sstevel@tonic-gate 	 */
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	bootpages = NULL;
14477c478bd9Sstevel@tonic-gate 	PRM_POINT("Protecting boot pages");
14487c478bd9Sstevel@tonic-gate 	/*
14497c478bd9Sstevel@tonic-gate 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
14507c478bd9Sstevel@tonic-gate 	 * page_t's. This can only happen if something weird allocated
14517c478bd9Sstevel@tonic-gate 	 * in this range (like kadb/kmdb).
14527c478bd9Sstevel@tonic-gate 	 */
14537c478bd9Sstevel@tonic-gate 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	/*
14567c478bd9Sstevel@tonic-gate 	 * Before we can take over memory allocation/mapping from the boot
14577c478bd9Sstevel@tonic-gate 	 * loader we must remove from our free page lists any boot pages that
14587c478bd9Sstevel@tonic-gate 	 * will stay mapped until release_bootstrap().
14597c478bd9Sstevel@tonic-gate 	 */
14607c478bd9Sstevel@tonic-gate 	protect_boot_range(0, kernelbase, 1);
14617c478bd9Sstevel@tonic-gate #if defined(__amd64)
14627c478bd9Sstevel@tonic-gate 	protect_boot_range(BOOT_DOUBLEMAP_BASE,
14637c478bd9Sstevel@tonic-gate 	    BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE, 0);
14647c478bd9Sstevel@tonic-gate #endif
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	/*
14677c478bd9Sstevel@tonic-gate 	 * Copy in boot's page tables, set up extra page tables for the kernel,
14687c478bd9Sstevel@tonic-gate 	 * and switch to the kernel's context.
14697c478bd9Sstevel@tonic-gate 	 */
14707c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling hat_kern_setup()...");
14717c478bd9Sstevel@tonic-gate 	hat_kern_setup();
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	/*
14747c478bd9Sstevel@tonic-gate 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
14757c478bd9Sstevel@tonic-gate 	 */
14767c478bd9Sstevel@tonic-gate 	bootops->bsys_alloc = NULL;
14777c478bd9Sstevel@tonic-gate 	PRM_POINT("hat_kern_setup() done");
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	hat_cpu_online(CPU);
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	/*
14827c478bd9Sstevel@tonic-gate 	 * Before we call kvm_init(), we need to establish the final size
14837c478bd9Sstevel@tonic-gate 	 * of the kernel's heap.  So, we need to figure out how much space
14847c478bd9Sstevel@tonic-gate 	 * to set aside for segkp, segkpm, and segmap.
14857c478bd9Sstevel@tonic-gate 	 */
14867c478bd9Sstevel@tonic-gate 	final_kernelheap = (caddr_t)ROUND_UP_LPAGE(kernelbase);
14877c478bd9Sstevel@tonic-gate #if defined(__amd64)
14887c478bd9Sstevel@tonic-gate 	if (kpm_desired) {
14897c478bd9Sstevel@tonic-gate 		/*
14907c478bd9Sstevel@tonic-gate 		 * Segkpm appears at the bottom of the kernel's address
14917c478bd9Sstevel@tonic-gate 		 * range.  To detect accidental overruns of the user
14927c478bd9Sstevel@tonic-gate 		 * address space, we leave a "red zone" of unmapped memory
14937c478bd9Sstevel@tonic-gate 		 * between kernelbase and the beginning of segkpm.
14947c478bd9Sstevel@tonic-gate 		 */
14957c478bd9Sstevel@tonic-gate 		kpm_vbase = final_kernelheap + KERNEL_REDZONE_SIZE;
14967c478bd9Sstevel@tonic-gate 		kpm_size = mmu_ptob(physmax);
14977c478bd9Sstevel@tonic-gate 		PRM_DEBUG(kpm_vbase);
14987c478bd9Sstevel@tonic-gate 		PRM_DEBUG(kpm_size);
14997c478bd9Sstevel@tonic-gate 		final_kernelheap =
15007c478bd9Sstevel@tonic-gate 		    (caddr_t)ROUND_UP_TOPLEVEL(kpm_vbase + kpm_size);
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	if (!segkp_fromheap) {
15047c478bd9Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 		/*
15077c478bd9Sstevel@tonic-gate 		 * determine size of segkp and adjust the bottom of the
15087c478bd9Sstevel@tonic-gate 		 * kernel's heap.
15097c478bd9Sstevel@tonic-gate 		 */
15107c478bd9Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
15117c478bd9Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
15127c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
15137c478bd9Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
15147c478bd9Sstevel@tonic-gate 			    mmu_btop(sz));
15157c478bd9Sstevel@tonic-gate 		}
15167c478bd9Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
15197c478bd9Sstevel@tonic-gate 		segkp_base = final_kernelheap;
15207c478bd9Sstevel@tonic-gate 		PRM_DEBUG(segkpsize);
15217c478bd9Sstevel@tonic-gate 		PRM_DEBUG(segkp_base);
15227c478bd9Sstevel@tonic-gate 		final_kernelheap = segkp_base + mmu_ptob(segkpsize);
15237c478bd9Sstevel@tonic-gate 		PRM_DEBUG(final_kernelheap);
15247c478bd9Sstevel@tonic-gate 	}
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	/*
15277c478bd9Sstevel@tonic-gate 	 * put the range of VA for device mappings next
15287c478bd9Sstevel@tonic-gate 	 */
15297c478bd9Sstevel@tonic-gate 	toxic_addr = (uintptr_t)final_kernelheap;
15307c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
15317c478bd9Sstevel@tonic-gate 	final_kernelheap = (char *)toxic_addr + toxic_size;
15327c478bd9Sstevel@tonic-gate #endif
15337c478bd9Sstevel@tonic-gate 	PRM_DEBUG(final_kernelheap);
15347c478bd9Sstevel@tonic-gate 	ASSERT(final_kernelheap < boot_kernelheap);
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	/*
15377c478bd9Sstevel@tonic-gate 	 * Users can change segmapsize through eeprom or /etc/system.
15387c478bd9Sstevel@tonic-gate 	 * If the variable is tuned through eeprom, there is no upper
15397c478bd9Sstevel@tonic-gate 	 * bound on the size of segmap.  If it is tuned through
15407c478bd9Sstevel@tonic-gate 	 * /etc/system on 32-bit systems, it must be no larger than we
15417c478bd9Sstevel@tonic-gate 	 * planned for in startup_memlist().
15427c478bd9Sstevel@tonic-gate 	 */
15437c478bd9Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
15447c478bd9Sstevel@tonic-gate 	segkmap_start = ROUND_UP_LPAGE((uintptr_t)final_kernelheap);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate #if defined(__i386)
15477c478bd9Sstevel@tonic-gate 	if (segmapsize > segmap_reserved) {
15487c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!segmapsize may not be set > 0x%lx in "
15497c478bd9Sstevel@tonic-gate 		    "/etc/system.  Use eeprom.", (long)SEGMAPMAX);
15507c478bd9Sstevel@tonic-gate 		segmapsize = segmap_reserved;
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate 	/*
15537c478bd9Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
15547c478bd9Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
15557c478bd9Sstevel@tonic-gate 	 * red zone just below the start of segmap.
15567c478bd9Sstevel@tonic-gate 	 */
15577c478bd9Sstevel@tonic-gate 	segkmap_start += KERNEL_REDZONE_SIZE;
15587c478bd9Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
15597c478bd9Sstevel@tonic-gate #endif
15607c478bd9Sstevel@tonic-gate 	final_kernelheap = (char *)(segkmap_start + segmapsize);
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segkmap_start);
15637c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segmapsize);
15647c478bd9Sstevel@tonic-gate 	PRM_DEBUG(final_kernelheap);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	/*
15677c478bd9Sstevel@tonic-gate 	 * Initialize VM system
15687c478bd9Sstevel@tonic-gate 	 */
15697c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
15707c478bd9Sstevel@tonic-gate 	kvm_init();
15717c478bd9Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	/*
15747c478bd9Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
15757c478bd9Sstevel@tonic-gate 	 */
15767c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
15777c478bd9Sstevel@tonic-gate 		kdi_dvec_vmready();
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	/*
15807c478bd9Sstevel@tonic-gate 	 * Mangle the brand string etc.
15817c478bd9Sstevel@tonic-gate 	 */
15827c478bd9Sstevel@tonic-gate 	cpuid_pass3(CPU);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	PRM_DEBUG(final_kernelheap);
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	/*
15877c478bd9Sstevel@tonic-gate 	 * Now that we can use memory outside the top 4GB (on 64-bit
15887c478bd9Sstevel@tonic-gate 	 * systems) and we know the size of segmap, we can set the final
15897c478bd9Sstevel@tonic-gate 	 * size of the kernel's heap.  Note: on 64-bit systems we still
15907c478bd9Sstevel@tonic-gate 	 * can't touch anything in the bottom half of the top 4GB range
15917c478bd9Sstevel@tonic-gate 	 * because boot still has pages mapped there.
15927c478bd9Sstevel@tonic-gate 	 */
15937c478bd9Sstevel@tonic-gate 	if (final_kernelheap < boot_kernelheap) {
15947c478bd9Sstevel@tonic-gate 		kernelheap_extend(final_kernelheap, boot_kernelheap);
15957c478bd9Sstevel@tonic-gate #if defined(__amd64)
15967c478bd9Sstevel@tonic-gate 		kmem_setaside = vmem_xalloc(heap_arena, BOOT_DOUBLEMAP_SIZE,
15977c478bd9Sstevel@tonic-gate 		    MMU_PAGESIZE, 0, 0, (void *)(BOOT_DOUBLEMAP_BASE),
15987c478bd9Sstevel@tonic-gate 		    (void *)(BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE),
15997c478bd9Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
16007c478bd9Sstevel@tonic-gate 		PRM_DEBUG(kmem_setaside);
16017c478bd9Sstevel@tonic-gate 		if (kmem_setaside == NULL)
16027c478bd9Sstevel@tonic-gate 			panic("Could not protect boot's memory");
16037c478bd9Sstevel@tonic-gate #endif
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 	/*
16067c478bd9Sstevel@tonic-gate 	 * Now that the kernel heap may have grown significantly, we need
16077c478bd9Sstevel@tonic-gate 	 * to make all the remaining page_t's available to back that memory.
16087c478bd9Sstevel@tonic-gate 	 *
16097c478bd9Sstevel@tonic-gate 	 * XX64 this should probably wait till after release boot-strap too.
16107c478bd9Sstevel@tonic-gate 	 */
16117c478bd9Sstevel@tonic-gate 	pages_left = npages - boot_npages;
16127c478bd9Sstevel@tonic-gate 	if (pages_left > 0) {
16137c478bd9Sstevel@tonic-gate 		PRM_DEBUG(pages_left);
16147c478bd9Sstevel@tonic-gate 		(void) kphysm_init(NULL, memseg_base, boot_npages, pages_left);
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate #if defined(__amd64)
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	/*
16207c478bd9Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
16217c478bd9Sstevel@tonic-gate 	 */
16227c478bd9Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
16237c478bd9Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate #else	/* __i386 */
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	/*
16287c478bd9Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
16297c478bd9Sstevel@tonic-gate 	 */
16307c478bd9Sstevel@tonic-gate 	toxic_bit_map_len = btop((ulong_t)(ptable_va - kernelbase));
16317c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
16327c478bd9Sstevel@tonic-gate 	toxic_bit_map =
16337c478bd9Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
16347c478bd9Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
16357c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate #endif	/* __i386 */
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	/*
16417c478bd9Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
16427c478bd9Sstevel@tonic-gate 	 * it, tell the debugger.
16437c478bd9Sstevel@tonic-gate 	 */
16447c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
16457c478bd9Sstevel@tonic-gate 		kdi_dvec_memavail();
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	/*
16487c478bd9Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
16497c478bd9Sstevel@tonic-gate 	 * to work around a pentium bug.
16507c478bd9Sstevel@tonic-gate 	 */
16517c478bd9Sstevel@tonic-gate #if !defined(__amd64)
16527c478bd9Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
16537c478bd9Sstevel@tonic-gate 		gate_desc_t *newidt;
16547c478bd9Sstevel@tonic-gate 		desctbr_t    newidt_r;
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
16577c478bd9Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 		bcopy(idt0, newidt, sizeof (idt0));
16607c478bd9Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
16617c478bd9Sstevel@tonic-gate 		    KCS_SEL, 0, SDT_SYSIGT, SEL_KPL);
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
16647c478bd9Sstevel@tonic-gate 		    PROT_READ|PROT_EXEC);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 		newidt_r.dtr_limit = sizeof (idt0) - 1;
16677c478bd9Sstevel@tonic-gate 		newidt_r.dtr_base = (uintptr_t)newidt;
16687c478bd9Sstevel@tonic-gate 		CPU->cpu_idt = newidt;
16697c478bd9Sstevel@tonic-gate 		wr_idtr(&newidt_r);
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate #endif	/* !__amd64 */
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	/*
16747c478bd9Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
16757c478bd9Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
16767c478bd9Sstevel@tonic-gate 	 */
16777c478bd9Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
16787c478bd9Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
16797c478bd9Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	/*
16827c478bd9Sstevel@tonic-gate 	 * If the following is true, someone has patched phsymem to be less
16837c478bd9Sstevel@tonic-gate 	 * than the number of pages that the system actually has.  Remove
16847c478bd9Sstevel@tonic-gate 	 * pages until system memory is limited to the requested amount.
16857c478bd9Sstevel@tonic-gate 	 * Since we have allocated page structures for all pages, we
16867c478bd9Sstevel@tonic-gate 	 * correct the amount of memory we want to remove by the size of
16877c478bd9Sstevel@tonic-gate 	 * the memory used to hold page structures for the non-used pages.
16887c478bd9Sstevel@tonic-gate 	 */
16897c478bd9Sstevel@tonic-gate 	if (physmem < npages) {
16907c478bd9Sstevel@tonic-gate 		uint_t diff;
16917c478bd9Sstevel@tonic-gate 		offset_t off;
16927c478bd9Sstevel@tonic-gate 		struct page *pp;
16937c478bd9Sstevel@tonic-gate 		caddr_t rand_vaddr;
16947c478bd9Sstevel@tonic-gate 		struct seg kseg;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "limiting physmem to %lu pages", physmem);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 		off = 0;
16997c478bd9Sstevel@tonic-gate 		diff = npages - physmem;
17007c478bd9Sstevel@tonic-gate 		diff -= mmu_btopr(diff * sizeof (struct page));
17017c478bd9Sstevel@tonic-gate 		kseg.s_as = &kas;
17027c478bd9Sstevel@tonic-gate 		while (diff--) {
17037c478bd9Sstevel@tonic-gate 			rand_vaddr = (caddr_t)
17047c478bd9Sstevel@tonic-gate 			    (((uintptr_t)&unused_pages_vp >> 7) ^
17057c478bd9Sstevel@tonic-gate 			    (uintptr_t)((u_offset_t)off >> MMU_PAGESHIFT));
17067c478bd9Sstevel@tonic-gate 			pp = page_create_va(&unused_pages_vp, off, MMU_PAGESIZE,
17077c478bd9Sstevel@tonic-gate 				PG_WAIT | PG_EXCL, &kseg, rand_vaddr);
17087c478bd9Sstevel@tonic-gate 			if (pp == NULL) {
17097c478bd9Sstevel@tonic-gate 				panic("limited physmem too much!");
17107c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
17117c478bd9Sstevel@tonic-gate 			}
17127c478bd9Sstevel@tonic-gate 			page_io_unlock(pp);
17137c478bd9Sstevel@tonic-gate 			page_downgrade(pp);
17147c478bd9Sstevel@tonic-gate 			availrmem--;
17157c478bd9Sstevel@tonic-gate 			off += MMU_PAGESIZE;
17167c478bd9Sstevel@tonic-gate 		}
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
17207c478bd9Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
17217c478bd9Sstevel@tonic-gate 
1722beb1bda0Sdavemq 	/* For small memory systems disable automatic large pages. */
1723beb1bda0Sdavemq 	if (physmem < auto_lpg_min_physmem) {
1724beb1bda0Sdavemq 		exec_lpg_disable = 1;
1725beb1bda0Sdavemq 		use_brk_lpg = 0;
1726beb1bda0Sdavemq 		use_stk_lpg = 0;
1727beb1bda0Sdavemq 		use_zmap_lpg = 0;
1728beb1bda0Sdavemq 	}
1729beb1bda0Sdavemq 
17307c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
17317c478bd9Sstevel@tonic-gate 	hat_init_finish();
17327c478bd9Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * Initialize the segkp segment type.
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
17387c478bd9Sstevel@tonic-gate 	if (!segkp_fromheap) {
17397c478bd9Sstevel@tonic-gate 		if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
17407c478bd9Sstevel@tonic-gate 		    segkp) < 0) {
17417c478bd9Sstevel@tonic-gate 			panic("startup: cannot attach segkp");
17427c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
17437c478bd9Sstevel@tonic-gate 		}
17447c478bd9Sstevel@tonic-gate 	} else {
17457c478bd9Sstevel@tonic-gate 		/*
17467c478bd9Sstevel@tonic-gate 		 * For 32 bit x86 systems, we will have segkp under the heap.
17477c478bd9Sstevel@tonic-gate 		 * There will not be a segkp segment.  We do, however, need
17487c478bd9Sstevel@tonic-gate 		 * to fill in the seg structure.
17497c478bd9Sstevel@tonic-gate 		 */
17507c478bd9Sstevel@tonic-gate 		segkp->s_as = &kas;
17517c478bd9Sstevel@tonic-gate 	}
17527c478bd9Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
17537c478bd9Sstevel@tonic-gate 		panic("startup: segkp_create failed");
17547c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
17557c478bd9Sstevel@tonic-gate 	}
17567c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segkp);
17577c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	/*
17607c478bd9Sstevel@tonic-gate 	 * kpm segment
17617c478bd9Sstevel@tonic-gate 	 */
17627c478bd9Sstevel@tonic-gate 	segmap_kpm = 0;
17637c478bd9Sstevel@tonic-gate 	if (kpm_desired) {
17647c478bd9Sstevel@tonic-gate 		kpm_init();
17657c478bd9Sstevel@tonic-gate 		kpm_enable = 1;
17667c478bd9Sstevel@tonic-gate 	}
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	/*
17697c478bd9Sstevel@tonic-gate 	 * Now create segmap segment.
17707c478bd9Sstevel@tonic-gate 	 */
17717c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
17727c478bd9Sstevel@tonic-gate 	if (seg_attach(&kas, (caddr_t)segkmap_start, segmapsize, segkmap) < 0) {
17737c478bd9Sstevel@tonic-gate 		panic("cannot attach segkmap");
17747c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segkmap);
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/*
17797c478bd9Sstevel@tonic-gate 	 * The 64 bit HAT permanently maps only segmap's page tables.
17807c478bd9Sstevel@tonic-gate 	 * The 32 bit HAT maps the heap's page tables too.
17817c478bd9Sstevel@tonic-gate 	 */
17827c478bd9Sstevel@tonic-gate #if defined(__amd64)
17837c478bd9Sstevel@tonic-gate 	hat_kmap_init(segkmap_start, segmapsize);
17847c478bd9Sstevel@tonic-gate #else /* __i386 */
17857c478bd9Sstevel@tonic-gate 	ASSERT(segkmap_start + segmapsize == (uintptr_t)final_kernelheap);
17867c478bd9Sstevel@tonic-gate 	hat_kmap_init(segkmap_start, (uintptr_t)ekernelheap - segkmap_start);
17877c478bd9Sstevel@tonic-gate #endif /* __i386 */
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
17907c478bd9Sstevel@tonic-gate 	a.shmsize = 0;
17917c478bd9Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	if (segmap_create(segkmap, (caddr_t)&a) != 0)
17947c478bd9Sstevel@tonic-gate 		panic("segmap_create segkmap");
17957c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	segdev_init();
18007c478bd9Sstevel@tonic-gate 	pmem_init();
18017c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
18027c478bd9Sstevel@tonic-gate }
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate static void
18057c478bd9Sstevel@tonic-gate startup_end(void)
18067c478bd9Sstevel@tonic-gate {
18077c478bd9Sstevel@tonic-gate 	extern void setx86isalist(void);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	/*
18127c478bd9Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
18137c478bd9Sstevel@tonic-gate 	 * initialization has been done but before the clock
18147c478bd9Sstevel@tonic-gate 	 * and other devices get started.
18157c478bd9Sstevel@tonic-gate 	 */
18167c478bd9Sstevel@tonic-gate 	kern_setup1();
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	/*
18197c478bd9Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
18207c478bd9Sstevel@tonic-gate 	 */
18217c478bd9Sstevel@tonic-gate 	kcpc_hw_init(CPU);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate #if defined(__amd64)
18247c478bd9Sstevel@tonic-gate 	/*
18257c478bd9Sstevel@tonic-gate 	 * Validate support for syscall/sysret
18267c478bd9Sstevel@tonic-gate 	 * XX64 -- include SSE, SSE2, etc. here too?
18277c478bd9Sstevel@tonic-gate 	 */
18287c478bd9Sstevel@tonic-gate 	if ((x86_feature & X86_ASYSC) == 0) {
18297c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
18307c478bd9Sstevel@tonic-gate 		    "cpu%d does not support syscall/sysret", CPU->cpu_id);
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate #endif
18337c478bd9Sstevel@tonic-gate 	/*
18347c478bd9Sstevel@tonic-gate 	 * Configure the system.
18357c478bd9Sstevel@tonic-gate 	 */
18367c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
18377c478bd9Sstevel@tonic-gate 	configure();		/* set up devices */
18387c478bd9Sstevel@tonic-gate 	PRM_POINT("configure() done");
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
18427c478bd9Sstevel@tonic-gate 	 * support.
18437c478bd9Sstevel@tonic-gate 	 */
18447c478bd9Sstevel@tonic-gate 	setx86isalist();
18457c478bd9Sstevel@tonic-gate 	init_intr_threads(CPU);
18467c478bd9Sstevel@tonic-gate 	psm_install();
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	/*
18497c478bd9Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
18507c478bd9Sstevel@tonic-gate 	 * we're still using bootsvcs.
18517c478bd9Sstevel@tonic-gate 	 */
18527c478bd9Sstevel@tonic-gate 	PRM_POINT("zeroing out bootops");
18537c478bd9Sstevel@tonic-gate 	*bootopsp = (struct bootops *)0;
18547c478bd9Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
18577c478bd9Sstevel@tonic-gate 	(*picinitf)();
18587c478bd9Sstevel@tonic-gate 	sti();
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
18617c478bd9Sstevel@tonic-gate 		"softlevel1", NULL, NULL); /* XXX to be moved later */
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
18647c478bd9Sstevel@tonic-gate }
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate extern char hw_serial[];
18677c478bd9Sstevel@tonic-gate char *_hs1107 = hw_serial;
18687c478bd9Sstevel@tonic-gate ulong_t  _bdhs34;
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate void
18717c478bd9Sstevel@tonic-gate post_startup(void)
18727c478bd9Sstevel@tonic-gate {
18737c478bd9Sstevel@tonic-gate 	/*
18747c478bd9Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
18757c478bd9Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
18767c478bd9Sstevel@tonic-gate 	 * instruction set extensions.
18777c478bd9Sstevel@tonic-gate 	 */
18787c478bd9Sstevel@tonic-gate 	bind_hwcap();
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	/*
1881*84ab085aSmws 	 * Load the System Management BIOS into the global ksmbios handle,
1882*84ab085aSmws 	 * if an SMBIOS is present on this system.
18837c478bd9Sstevel@tonic-gate 	 */
1884*84ab085aSmws 	ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	/*
1887*84ab085aSmws 	 * Startup memory scrubber.
18887c478bd9Sstevel@tonic-gate 	 */
1889*84ab085aSmws 	memscrub_init();
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	/*
1892*84ab085aSmws 	 * Perform forceloading tasks for /etc/system.
18937c478bd9Sstevel@tonic-gate 	 */
1894*84ab085aSmws 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	/*
18977c478bd9Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
18987c478bd9Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
18997c478bd9Sstevel@tonic-gate 	 */
19007c478bd9Sstevel@tonic-gate 	(void) modload("fs", "procfs");
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate #if defined(__i386)
19037c478bd9Sstevel@tonic-gate 	/*
19047c478bd9Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
19057c478bd9Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
19067c478bd9Sstevel@tonic-gate 	 */
19077c478bd9Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
19087c478bd9Sstevel@tonic-gate 		halt("No working FP hardware found");
19097c478bd9Sstevel@tonic-gate #endif
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	maxmem = freemem;
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	/*
19167c478bd9Sstevel@tonic-gate 	 * Perform the formal initialization of the boot chip,
19177c478bd9Sstevel@tonic-gate 	 * and associate the boot cpu with it.
19187c478bd9Sstevel@tonic-gate 	 * This must be done after the cpu node for CPU has been
19197c478bd9Sstevel@tonic-gate 	 * added to the device tree, when the necessary probing to
19207c478bd9Sstevel@tonic-gate 	 * know the chip type and chip "id" is performed.
19217c478bd9Sstevel@tonic-gate 	 */
19227c478bd9Sstevel@tonic-gate 	chip_cpu_init(CPU);
19237c478bd9Sstevel@tonic-gate 	chip_cpu_assign(CPU);
19247c478bd9Sstevel@tonic-gate }
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate static int
19277c478bd9Sstevel@tonic-gate pp_in_ramdisk(page_t *pp)
19287c478bd9Sstevel@tonic-gate {
19297c478bd9Sstevel@tonic-gate 	extern uint64_t ramdisk_start, ramdisk_end;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	return ((pp->p_pagenum >= btop(ramdisk_start)) &&
19327c478bd9Sstevel@tonic-gate 	    (pp->p_pagenum < btopr(ramdisk_end)));
19337c478bd9Sstevel@tonic-gate }
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate void
19367c478bd9Sstevel@tonic-gate release_bootstrap(void)
19377c478bd9Sstevel@tonic-gate {
19387c478bd9Sstevel@tonic-gate 	int root_is_ramdisk;
19397c478bd9Sstevel@tonic-gate 	pfn_t pfn;
19407c478bd9Sstevel@tonic-gate 	page_t *pp;
19417c478bd9Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
19427c478bd9Sstevel@tonic-gate 	extern dev_t rootdev;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
19457c478bd9Sstevel@tonic-gate 	kobj_boot_unmountroot();
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	/*
19487c478bd9Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
19497c478bd9Sstevel@tonic-gate 	 */
19507c478bd9Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
19517c478bd9Sstevel@tonic-gate 	clear_boot_mappings(0, kernelbase);
19527c478bd9Sstevel@tonic-gate #if defined(__amd64)
19537c478bd9Sstevel@tonic-gate 	PRM_POINT("Unmapping upper boot pages");
19547c478bd9Sstevel@tonic-gate 	clear_boot_mappings(BOOT_DOUBLEMAP_BASE,
19557c478bd9Sstevel@tonic-gate 	    BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE);
19567c478bd9Sstevel@tonic-gate #endif
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	/*
19597c478bd9Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
19607c478bd9Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
19617c478bd9Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
19627c478bd9Sstevel@tonic-gate 	 */
19637c478bd9Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
19647c478bd9Sstevel@tonic-gate 	if (!root_is_ramdisk) {
19657c478bd9Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
19667c478bd9Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
19677c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
19687c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
19727c478bd9Sstevel@tonic-gate 	while (bootpages) {
19737c478bd9Sstevel@tonic-gate 		pp = bootpages;
19747c478bd9Sstevel@tonic-gate 		bootpages = pp->p_next;
19757c478bd9Sstevel@tonic-gate 		if (root_is_ramdisk && pp_in_ramdisk(pp)) {
19767c478bd9Sstevel@tonic-gate 			pp->p_next = rd_pages;
19777c478bd9Sstevel@tonic-gate 			rd_pages = pp;
19787c478bd9Sstevel@tonic-gate 			continue;
19797c478bd9Sstevel@tonic-gate 		}
19807c478bd9Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
19817c478bd9Sstevel@tonic-gate 		page_free(pp, 1);
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	/*
19857c478bd9Sstevel@tonic-gate 	 * Find 1 page below 1 MB so that other processors can boot up.
19867c478bd9Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
19877c478bd9Sstevel@tonic-gate 	 * We should have just free'd one up.
19887c478bd9Sstevel@tonic-gate 	 */
19897c478bd9Sstevel@tonic-gate 	if (use_mp) {
19907c478bd9Sstevel@tonic-gate 		for (pfn = 1; pfn < btop(1*1024*1024); pfn++) {
19917c478bd9Sstevel@tonic-gate 			if (page_numtopp_alloc(pfn) == NULL)
19927c478bd9Sstevel@tonic-gate 				continue;
19937c478bd9Sstevel@tonic-gate 			rm_platter_va = i86devmap(pfn, 1,
19947c478bd9Sstevel@tonic-gate 			    PROT_READ | PROT_WRITE | PROT_EXEC);
19957c478bd9Sstevel@tonic-gate 			rm_platter_pa = ptob(pfn);
19967c478bd9Sstevel@tonic-gate 			hat_devload(kas.a_hat,
19977c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
19987c478bd9Sstevel@tonic-gate 			    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
19997c478bd9Sstevel@tonic-gate 			    HAT_LOAD_NOCONSIST);
20007c478bd9Sstevel@tonic-gate 			break;
20017c478bd9Sstevel@tonic-gate 		}
20027c478bd9Sstevel@tonic-gate 		if (pfn == btop(1*1024*1024))
20037c478bd9Sstevel@tonic-gate 			panic("No page available for starting "
20047c478bd9Sstevel@tonic-gate 			    "other processors");
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate #if defined(__amd64)
20087c478bd9Sstevel@tonic-gate 	PRM_POINT("Returning boot's VA space to kernel heap");
20097c478bd9Sstevel@tonic-gate 	if (kmem_setaside != NULL)
20107c478bd9Sstevel@tonic-gate 		vmem_free(heap_arena, kmem_setaside, BOOT_DOUBLEMAP_SIZE);
20117c478bd9Sstevel@tonic-gate #endif
20127c478bd9Sstevel@tonic-gate }
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate /*
20157c478bd9Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
20167c478bd9Sstevel@tonic-gate  */
20177c478bd9Sstevel@tonic-gate void
20187c478bd9Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
20197c478bd9Sstevel@tonic-gate {
20207c478bd9Sstevel@tonic-gate 	pp->p_pagenum = pnum;
20217c478bd9Sstevel@tonic-gate 	pp->p_mapping = NULL;
20227c478bd9Sstevel@tonic-gate 	pp->p_embed = 0;
20237c478bd9Sstevel@tonic-gate 	pp->p_share = 0;
20247c478bd9Sstevel@tonic-gate 	pp->p_mlentry = 0;
20257c478bd9Sstevel@tonic-gate }
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate /*
20287c478bd9Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
20297c478bd9Sstevel@tonic-gate  */
20307c478bd9Sstevel@tonic-gate static pgcnt_t
20317c478bd9Sstevel@tonic-gate kphysm_init(
20327c478bd9Sstevel@tonic-gate 	page_t *inpp,
20337c478bd9Sstevel@tonic-gate 	struct memseg *memsegp,
20347c478bd9Sstevel@tonic-gate 	pgcnt_t start,
20357c478bd9Sstevel@tonic-gate 	pgcnt_t npages)
20367c478bd9Sstevel@tonic-gate {
20377c478bd9Sstevel@tonic-gate 	struct memlist	*pmem;
20387c478bd9Sstevel@tonic-gate 	struct memseg	*cur_memseg;
20397c478bd9Sstevel@tonic-gate 	struct memseg	**memsegpp;
20407c478bd9Sstevel@tonic-gate 	pfn_t		base_pfn;
20417c478bd9Sstevel@tonic-gate 	pgcnt_t		num;
20427c478bd9Sstevel@tonic-gate 	pgcnt_t		total_skipped = 0;
20437c478bd9Sstevel@tonic-gate 	pgcnt_t		skipping = 0;
20447c478bd9Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
20457c478bd9Sstevel@tonic-gate 	pgcnt_t		largepgcnt;
20467c478bd9Sstevel@tonic-gate 	uint64_t	addr;
20477c478bd9Sstevel@tonic-gate 	uint64_t	size;
20487c478bd9Sstevel@tonic-gate 	page_t		*pp = inpp;
20497c478bd9Sstevel@tonic-gate 	int		dobreak = 0;
20507c478bd9Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	for (cur_memseg = memsegp; cur_memseg->pages != NULL; cur_memseg++);
20557c478bd9Sstevel@tonic-gate 	ASSERT(cur_memseg == memsegp || start > 0);
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
20587c478bd9Sstevel@tonic-gate 		/*
20597c478bd9Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
20607c478bd9Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
20617c478bd9Sstevel@tonic-gate 		 */
20627c478bd9Sstevel@tonic-gate 		addr = pmem->address;
20637c478bd9Sstevel@tonic-gate 		size = pmem->size;
20647c478bd9Sstevel@tonic-gate 		if (btop(addr) > physmax)
20657c478bd9Sstevel@tonic-gate 			continue;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 		/*
20687c478bd9Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
20697c478bd9Sstevel@tonic-gate 		 */
20707c478bd9Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
20717c478bd9Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
20727c478bd9Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
20737c478bd9Sstevel@tonic-gate 			size -= addr - pmem->address;
20747c478bd9Sstevel@tonic-gate 		}
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 		/* only process pages below physmax */
20777c478bd9Sstevel@tonic-gate 		if (btop(addr + size) > physmax)
20787c478bd9Sstevel@tonic-gate 			size = ptob(physmax - btop(addr));
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 		num = btop(size);
20817c478bd9Sstevel@tonic-gate 		if (num == 0)
20827c478bd9Sstevel@tonic-gate 			continue;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 		if (total_skipped < start) {
20857c478bd9Sstevel@tonic-gate 			if (start - total_skipped > num) {
20867c478bd9Sstevel@tonic-gate 				total_skipped += num;
20877c478bd9Sstevel@tonic-gate 				continue;
20887c478bd9Sstevel@tonic-gate 			}
20897c478bd9Sstevel@tonic-gate 			skipping = start - total_skipped;
20907c478bd9Sstevel@tonic-gate 			num -= skipping;
20917c478bd9Sstevel@tonic-gate 			addr += (MMU_PAGESIZE * skipping);
20927c478bd9Sstevel@tonic-gate 			total_skipped = start;
20937c478bd9Sstevel@tonic-gate 		}
20947c478bd9Sstevel@tonic-gate 		if (num == 0)
20957c478bd9Sstevel@tonic-gate 			continue;
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 		if (num > npages)
20987c478bd9Sstevel@tonic-gate 			num = npages;
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 		npages -= num;
21017c478bd9Sstevel@tonic-gate 		pages_done += num;
21027c478bd9Sstevel@tonic-gate 		base_pfn = btop(addr);
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 		/*
21057c478bd9Sstevel@tonic-gate 		 * If the caller didn't provide space for the page
21067c478bd9Sstevel@tonic-gate 		 * structures, carve them out of the memseg they will
21077c478bd9Sstevel@tonic-gate 		 * represent.
21087c478bd9Sstevel@tonic-gate 		 */
21097c478bd9Sstevel@tonic-gate 		if (pp == NULL) {
21107c478bd9Sstevel@tonic-gate 			pgcnt_t pp_pgs;
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 			if (num <= 1)
21137c478bd9Sstevel@tonic-gate 				continue;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 			/*
21167c478bd9Sstevel@tonic-gate 			 * Compute how many of the pages we need to use for
21177c478bd9Sstevel@tonic-gate 			 * page_ts
21187c478bd9Sstevel@tonic-gate 			 */
21197c478bd9Sstevel@tonic-gate 			pp_pgs = (num * sizeof (page_t)) / MMU_PAGESIZE + 1;
21207c478bd9Sstevel@tonic-gate 			while (mmu_ptob(pp_pgs - 1) / sizeof (page_t) >=
21217c478bd9Sstevel@tonic-gate 			    num - pp_pgs + 1)
21227c478bd9Sstevel@tonic-gate 				--pp_pgs;
21237c478bd9Sstevel@tonic-gate 			PRM_DEBUG(pp_pgs);
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 			pp = vmem_alloc(heap_arena, mmu_ptob(pp_pgs),
21267c478bd9Sstevel@tonic-gate 			    VM_NOSLEEP);
21277c478bd9Sstevel@tonic-gate 			if (pp == NULL) {
21287c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "Unable to add %ld pages to "
21297c478bd9Sstevel@tonic-gate 				    "the system.", num);
21307c478bd9Sstevel@tonic-gate 				continue;
21317c478bd9Sstevel@tonic-gate 			}
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 			hat_devload(kas.a_hat, (void *)pp, mmu_ptob(pp_pgs),
21347c478bd9Sstevel@tonic-gate 			    base_pfn, PROT_READ | PROT_WRITE | HAT_UNORDERED_OK,
21357c478bd9Sstevel@tonic-gate 			    HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
21367c478bd9Sstevel@tonic-gate 			bzero(pp, mmu_ptob(pp_pgs));
21377c478bd9Sstevel@tonic-gate 			num -= pp_pgs;
21387c478bd9Sstevel@tonic-gate 			base_pfn += pp_pgs;
21397c478bd9Sstevel@tonic-gate 		}
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 		if (prom_debug)
21427c478bd9Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
21437c478bd9Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
21447c478bd9Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 		/*
21477c478bd9Sstevel@tonic-gate 		 * drop pages below ddiphysmin to simplify ddi memory
21487c478bd9Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
21497c478bd9Sstevel@tonic-gate 		 */
21507c478bd9Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
21517c478bd9Sstevel@tonic-gate 			if (base_pfn + num <= ddiphysmin) {
21527c478bd9Sstevel@tonic-gate 				/* drop entire range below ddiphysmin */
21537c478bd9Sstevel@tonic-gate 				continue;
21547c478bd9Sstevel@tonic-gate 			}
21557c478bd9Sstevel@tonic-gate 			/* adjust range to ddiphysmin */
21567c478bd9Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
21577c478bd9Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
21587c478bd9Sstevel@tonic-gate 			base_pfn = ddiphysmin;
21597c478bd9Sstevel@tonic-gate 		}
21607c478bd9Sstevel@tonic-gate 		/*
21617c478bd9Sstevel@tonic-gate 		 * Build the memsegs entry
21627c478bd9Sstevel@tonic-gate 		 */
21637c478bd9Sstevel@tonic-gate 		cur_memseg->pages = pp;
21647c478bd9Sstevel@tonic-gate 		cur_memseg->epages = pp + num;
21657c478bd9Sstevel@tonic-gate 		cur_memseg->pages_base = base_pfn;
21667c478bd9Sstevel@tonic-gate 		cur_memseg->pages_end = base_pfn + num;
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 		/*
21697c478bd9Sstevel@tonic-gate 		 * insert in memseg list in decreasing pfn range order.
21707c478bd9Sstevel@tonic-gate 		 * Low memory is typically more fragmented such that this
21717c478bd9Sstevel@tonic-gate 		 * ordering keeps the larger ranges at the front of the list
21727c478bd9Sstevel@tonic-gate 		 * for code that searches memseg.
21737c478bd9Sstevel@tonic-gate 		 */
21747c478bd9Sstevel@tonic-gate 		memsegpp = &memsegs;
21757c478bd9Sstevel@tonic-gate 		for (;;) {
21767c478bd9Sstevel@tonic-gate 			if (*memsegpp == NULL) {
21777c478bd9Sstevel@tonic-gate 				/* empty memsegs */
21787c478bd9Sstevel@tonic-gate 				memsegs = cur_memseg;
21797c478bd9Sstevel@tonic-gate 				break;
21807c478bd9Sstevel@tonic-gate 			}
21817c478bd9Sstevel@tonic-gate 			/* check for continuity with start of memsegpp */
21827c478bd9Sstevel@tonic-gate 			if (cur_memseg->pages_end == (*memsegpp)->pages_base) {
21837c478bd9Sstevel@tonic-gate 				if (cur_memseg->epages == (*memsegpp)->pages) {
21847c478bd9Sstevel@tonic-gate 					/*
21857c478bd9Sstevel@tonic-gate 					 * contiguous pfn and page_t's. Merge
21867c478bd9Sstevel@tonic-gate 					 * cur_memseg into *memsegpp. Drop
21877c478bd9Sstevel@tonic-gate 					 * cur_memseg
21887c478bd9Sstevel@tonic-gate 					 */
21897c478bd9Sstevel@tonic-gate 					(*memsegpp)->pages_base =
21907c478bd9Sstevel@tonic-gate 					    cur_memseg->pages_base;
21917c478bd9Sstevel@tonic-gate 					(*memsegpp)->pages =
21927c478bd9Sstevel@tonic-gate 					    cur_memseg->pages;
21937c478bd9Sstevel@tonic-gate 					/*
21947c478bd9Sstevel@tonic-gate 					 * check if contiguous with the end of
21957c478bd9Sstevel@tonic-gate 					 * the next memseg.
21967c478bd9Sstevel@tonic-gate 					 */
21977c478bd9Sstevel@tonic-gate 					if ((*memsegpp)->next &&
21987c478bd9Sstevel@tonic-gate 					    ((*memsegpp)->pages_base ==
21997c478bd9Sstevel@tonic-gate 					    (*memsegpp)->next->pages_end)) {
22007c478bd9Sstevel@tonic-gate 						cur_memseg = *memsegpp;
22017c478bd9Sstevel@tonic-gate 						memsegpp = &((*memsegpp)->next);
22027c478bd9Sstevel@tonic-gate 						dobreak = 1;
22037c478bd9Sstevel@tonic-gate 					} else {
22047c478bd9Sstevel@tonic-gate 						break;
22057c478bd9Sstevel@tonic-gate 					}
22067c478bd9Sstevel@tonic-gate 				} else {
22077c478bd9Sstevel@tonic-gate 					/*
22087c478bd9Sstevel@tonic-gate 					 * contiguous pfn but not page_t's.
22097c478bd9Sstevel@tonic-gate 					 * drop last pfn/page_t in cur_memseg
22107c478bd9Sstevel@tonic-gate 					 * to prevent creation of large pages
22117c478bd9Sstevel@tonic-gate 					 * with noncontiguous page_t's if not
22127c478bd9Sstevel@tonic-gate 					 * aligned to largest page boundary.
22137c478bd9Sstevel@tonic-gate 					 */
22147c478bd9Sstevel@tonic-gate 					largepgcnt = page_get_pagecnt(
22157c478bd9Sstevel@tonic-gate 					    page_num_pagesizes() - 1);
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 					if (cur_memseg->pages_end &
22187c478bd9Sstevel@tonic-gate 					    (largepgcnt - 1)) {
22197c478bd9Sstevel@tonic-gate 						num--;
22207c478bd9Sstevel@tonic-gate 						cur_memseg->epages--;
22217c478bd9Sstevel@tonic-gate 						cur_memseg->pages_end--;
22227c478bd9Sstevel@tonic-gate 					}
22237c478bd9Sstevel@tonic-gate 				}
22247c478bd9Sstevel@tonic-gate 			}
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 			/* check for continuity with end of memsegpp */
22277c478bd9Sstevel@tonic-gate 			if (cur_memseg->pages_base == (*memsegpp)->pages_end) {
22287c478bd9Sstevel@tonic-gate 				if (cur_memseg->pages == (*memsegpp)->epages) {
22297c478bd9Sstevel@tonic-gate 					/*
22307c478bd9Sstevel@tonic-gate 					 * contiguous pfn and page_t's. Merge
22317c478bd9Sstevel@tonic-gate 					 * cur_memseg into *memsegpp. Drop
22327c478bd9Sstevel@tonic-gate 					 * cur_memseg.
22337c478bd9Sstevel@tonic-gate 					 */
22347c478bd9Sstevel@tonic-gate 					if (dobreak) {
22357c478bd9Sstevel@tonic-gate 						/* merge previously done */
22367c478bd9Sstevel@tonic-gate 						cur_memseg->pages =
22377c478bd9Sstevel@tonic-gate 						    (*memsegpp)->pages;
22387c478bd9Sstevel@tonic-gate 						cur_memseg->pages_base =
22397c478bd9Sstevel@tonic-gate 						    (*memsegpp)->pages_base;
22407c478bd9Sstevel@tonic-gate 						cur_memseg->next =
22417c478bd9Sstevel@tonic-gate 						    (*memsegpp)->next;
22427c478bd9Sstevel@tonic-gate 					} else {
22437c478bd9Sstevel@tonic-gate 						(*memsegpp)->pages_end =
22447c478bd9Sstevel@tonic-gate 						    cur_memseg->pages_end;
22457c478bd9Sstevel@tonic-gate 						(*memsegpp)->epages =
22467c478bd9Sstevel@tonic-gate 						    cur_memseg->epages;
22477c478bd9Sstevel@tonic-gate 					}
22487c478bd9Sstevel@tonic-gate 					break;
22497c478bd9Sstevel@tonic-gate 				}
22507c478bd9Sstevel@tonic-gate 				/*
22517c478bd9Sstevel@tonic-gate 				 * contiguous pfn but not page_t's.
22527c478bd9Sstevel@tonic-gate 				 * drop first pfn/page_t in cur_memseg
22537c478bd9Sstevel@tonic-gate 				 * to prevent creation of large pages
22547c478bd9Sstevel@tonic-gate 				 * with noncontiguous page_t's if not
22557c478bd9Sstevel@tonic-gate 				 * aligned to largest page boundary.
22567c478bd9Sstevel@tonic-gate 				 */
22577c478bd9Sstevel@tonic-gate 				largepgcnt = page_get_pagecnt(
22587c478bd9Sstevel@tonic-gate 				    page_num_pagesizes() - 1);
22597c478bd9Sstevel@tonic-gate 				if (base_pfn & (largepgcnt - 1)) {
22607c478bd9Sstevel@tonic-gate 					num--;
22617c478bd9Sstevel@tonic-gate 					base_pfn++;
22627c478bd9Sstevel@tonic-gate 					cur_memseg->pages++;
22637c478bd9Sstevel@tonic-gate 					cur_memseg->pages_base++;
22647c478bd9Sstevel@tonic-gate 					pp = cur_memseg->pages;
22657c478bd9Sstevel@tonic-gate 				}
22667c478bd9Sstevel@tonic-gate 				if (dobreak)
22677c478bd9Sstevel@tonic-gate 					break;
22687c478bd9Sstevel@tonic-gate 			}
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 			if (cur_memseg->pages_base >=
22717c478bd9Sstevel@tonic-gate 			    (*memsegpp)->pages_end) {
22727c478bd9Sstevel@tonic-gate 				cur_memseg->next = *memsegpp;
22737c478bd9Sstevel@tonic-gate 				*memsegpp = cur_memseg;
22747c478bd9Sstevel@tonic-gate 				break;
22757c478bd9Sstevel@tonic-gate 			}
22767c478bd9Sstevel@tonic-gate 			if ((*memsegpp)->next == NULL) {
22777c478bd9Sstevel@tonic-gate 				cur_memseg->next = NULL;
22787c478bd9Sstevel@tonic-gate 				(*memsegpp)->next = cur_memseg;
22797c478bd9Sstevel@tonic-gate 				break;
22807c478bd9Sstevel@tonic-gate 			}
22817c478bd9Sstevel@tonic-gate 			memsegpp = &((*memsegpp)->next);
22827c478bd9Sstevel@tonic-gate 			ASSERT(*memsegpp != NULL);
22837c478bd9Sstevel@tonic-gate 		}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 		/*
22867c478bd9Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
22877c478bd9Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
22887c478bd9Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
22897c478bd9Sstevel@tonic-gate 		 * it initializes them.
22907c478bd9Sstevel@tonic-gate 		 */
22917c478bd9Sstevel@tonic-gate 		add_physmem(pp, num, base_pfn);
22927c478bd9Sstevel@tonic-gate 		cur_memseg++;
22937c478bd9Sstevel@tonic-gate 		availrmem_initial += num;
22947c478bd9Sstevel@tonic-gate 		availrmem += num;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 		/*
22977c478bd9Sstevel@tonic-gate 		 * If the caller provided the page frames to us, then
22987c478bd9Sstevel@tonic-gate 		 * advance in that list.  Otherwise, prepare to allocate
22997c478bd9Sstevel@tonic-gate 		 * our own page frames for the next memseg.
23007c478bd9Sstevel@tonic-gate 		 */
23017c478bd9Sstevel@tonic-gate 		pp = (inpp == NULL) ? NULL : pp + num;
23027c478bd9Sstevel@tonic-gate 	}
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
23057c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
23067c478bd9Sstevel@tonic-gate 	PRM_DEBUG(freemem);
23077c478bd9Sstevel@tonic-gate 	build_pfn_hash();
23087c478bd9Sstevel@tonic-gate 	return (pages_done);
23097c478bd9Sstevel@tonic-gate }
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate /*
23127c478bd9Sstevel@tonic-gate  * Kernel VM initialization.
23137c478bd9Sstevel@tonic-gate  */
23147c478bd9Sstevel@tonic-gate static void
23157c478bd9Sstevel@tonic-gate kvm_init(void)
23167c478bd9Sstevel@tonic-gate {
23177c478bd9Sstevel@tonic-gate #ifdef DEBUG
23187c478bd9Sstevel@tonic-gate 	extern void _start();
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	ASSERT((caddr_t)_start == s_text);
23217c478bd9Sstevel@tonic-gate #endif
23227c478bd9Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	/*
23257c478bd9Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
23267c478bd9Sstevel@tonic-gate 	 */
23277c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
23287c478bd9Sstevel@tonic-gate 	as_avlinit(&kas);
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
23317c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
23347c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	/*
23377c478bd9Sstevel@tonic-gate 	 * We're about to map out /boot.  This is the beginning of the
23387c478bd9Sstevel@tonic-gate 	 * system resource management transition. We can no longer
23397c478bd9Sstevel@tonic-gate 	 * call into /boot for I/O or memory allocations.
23407c478bd9Sstevel@tonic-gate 	 *
23417c478bd9Sstevel@tonic-gate 	 * XX64 - Is this still correct with kernelheap_extend() being called
23427c478bd9Sstevel@tonic-gate 	 * later than this????
23437c478bd9Sstevel@tonic-gate 	 */
23447c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, final_kernelheap,
23457c478bd9Sstevel@tonic-gate 	    ekernelheap - final_kernelheap, &kvseg);
23467c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate #if defined(__amd64)
23497c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)core_base, core_size, &kvseg_core);
23507c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvseg_core);
23517c478bd9Sstevel@tonic-gate #endif
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)SEGDEBUGBASE, (size_t)SEGDEBUGSIZE,
23547c478bd9Sstevel@tonic-gate 	    &kdebugseg);
23557c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	/*
23607c478bd9Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
23617c478bd9Sstevel@tonic-gate 	 */
23627c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	/*
23657c478bd9Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
23667c478bd9Sstevel@tonic-gate 	 */
23677c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
23687c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 	/*
23717c478bd9Sstevel@tonic-gate 	 * Make data writable until end.
23727c478bd9Sstevel@tonic-gate 	 */
23737c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
23747c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
23757c478bd9Sstevel@tonic-gate }
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate /*
23787c478bd9Sstevel@tonic-gate  * These are MTTR registers supported by P6
23797c478bd9Sstevel@tonic-gate  */
23807c478bd9Sstevel@tonic-gate static struct	mtrrvar	mtrrphys_arr[MAX_MTRRVAR];
23817c478bd9Sstevel@tonic-gate static uint64_t mtrr64k, mtrr16k1, mtrr16k2;
23827c478bd9Sstevel@tonic-gate static uint64_t mtrr4k1, mtrr4k2, mtrr4k3;
23837c478bd9Sstevel@tonic-gate static uint64_t mtrr4k4, mtrr4k5, mtrr4k6;
23847c478bd9Sstevel@tonic-gate static uint64_t mtrr4k7, mtrr4k8, mtrrcap;
23857c478bd9Sstevel@tonic-gate uint64_t mtrrdef, pat_attr_reg;
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate /*
23887c478bd9Sstevel@tonic-gate  * Disable reprogramming of MTRRs by default.
23897c478bd9Sstevel@tonic-gate  */
23907c478bd9Sstevel@tonic-gate int	enable_relaxed_mtrr = 0;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate /*
23937c478bd9Sstevel@tonic-gate  * These must serve for Pentium, Pentium Pro (P6/Pentium II/Pentium III)
23947c478bd9Sstevel@tonic-gate  * and Pentium 4, and yes, they are named 0, 1, 2, 4, 3 in ascending
23957c478bd9Sstevel@tonic-gate  * address order (starting from 0x400).  The Pentium 4 only implements
23967c478bd9Sstevel@tonic-gate  * 4 sets, and while they are named 0-3 in the doc, the corresponding
23977c478bd9Sstevel@tonic-gate  * names for P6 are 0,1,2,4.  So define these arrays in address order
23987c478bd9Sstevel@tonic-gate  * so that they work for both pre-Pentium4 and Pentium 4 processors.
23997c478bd9Sstevel@tonic-gate  */
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate static uint_t	mci_ctl[] = {REG_MC0_CTL, REG_MC1_CTL, REG_MC2_CTL,
24027c478bd9Sstevel@tonic-gate 		    REG_MC4_CTL, REG_MC3_CTL};
24037c478bd9Sstevel@tonic-gate static uint_t	mci_status[] = {REG_MC0_STATUS, REG_MC1_STATUS, REG_MC2_STATUS,
24047c478bd9Sstevel@tonic-gate 		    REG_MC4_STATUS, REG_MC3_STATUS};
24057c478bd9Sstevel@tonic-gate static uint_t	mci_addr[] = {REG_MC0_ADDR, REG_MC1_ADDR, REG_MC2_ADDR,
24067c478bd9Sstevel@tonic-gate 		    REG_MC4_ADDR, REG_MC3_ADDR};
24077c478bd9Sstevel@tonic-gate static int	mca_cnt;
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate void
24117c478bd9Sstevel@tonic-gate setup_mca()
24127c478bd9Sstevel@tonic-gate {
24137c478bd9Sstevel@tonic-gate 	int 		i;
24147c478bd9Sstevel@tonic-gate 	uint64_t	allzeros;
24157c478bd9Sstevel@tonic-gate 	uint64_t	allones;
24167c478bd9Sstevel@tonic-gate 	uint64_t	mca_cap;
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	if (!(x86_feature & X86_MCA))
24197c478bd9Sstevel@tonic-gate 		return;
24207c478bd9Sstevel@tonic-gate 	(void) rdmsr(REG_MCG_CAP, &mca_cap);
24217c478bd9Sstevel@tonic-gate 	allones = 0xffffffffffffffffULL;
24227c478bd9Sstevel@tonic-gate 	if (mca_cap & MCG_CAP_CTL_P)
24237c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MCG_CTL, &allones);
24247c478bd9Sstevel@tonic-gate 	mca_cnt = mca_cap & MCG_CAP_COUNT_MASK;
24257c478bd9Sstevel@tonic-gate 	if (mca_cnt > P6_MCG_CAP_COUNT)
24267c478bd9Sstevel@tonic-gate 		mca_cnt = P6_MCG_CAP_COUNT;
24277c478bd9Sstevel@tonic-gate 	for (i = 1; i < mca_cnt; i++)
24287c478bd9Sstevel@tonic-gate 		(void) wrmsr(mci_ctl[i], &allones);
24297c478bd9Sstevel@tonic-gate 	allzeros = 0;
24307c478bd9Sstevel@tonic-gate 	for (i = 0; i < mca_cnt; i++)
24317c478bd9Sstevel@tonic-gate 		(void) wrmsr(mci_status[i], &allzeros);
24327c478bd9Sstevel@tonic-gate 	setcr4(getcr4() | CR4_MCE);
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate }
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate int
24377c478bd9Sstevel@tonic-gate mca_exception(struct regs *rp)
24387c478bd9Sstevel@tonic-gate {
24397c478bd9Sstevel@tonic-gate 	uint64_t	status, addr;
24407c478bd9Sstevel@tonic-gate 	uint64_t	allzeros;
24417c478bd9Sstevel@tonic-gate 	uint64_t	buf;
24427c478bd9Sstevel@tonic-gate 	int		i, ret = 1, errcode, mserrcode;
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	allzeros = 0;
24457c478bd9Sstevel@tonic-gate 	(void) rdmsr(REG_MCG_STATUS, &buf);
24467c478bd9Sstevel@tonic-gate 	status = buf;
24477c478bd9Sstevel@tonic-gate 	if (status & MCG_STATUS_RIPV)
24487c478bd9Sstevel@tonic-gate 		ret = 0;
24497c478bd9Sstevel@tonic-gate 	if (status & MCG_STATUS_EIPV)
24507c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "MCE at 0x%lx", rp->r_pc);
24517c478bd9Sstevel@tonic-gate 	(void) wrmsr(REG_MCG_STATUS, &allzeros);
24527c478bd9Sstevel@tonic-gate 	for (i = 0; i < mca_cnt; i++) {
24537c478bd9Sstevel@tonic-gate 		(void) rdmsr(mci_status[i], &buf);
24547c478bd9Sstevel@tonic-gate 		status = buf;
24557c478bd9Sstevel@tonic-gate 		/*
24567c478bd9Sstevel@tonic-gate 		 * If status register not valid skip this bank
24577c478bd9Sstevel@tonic-gate 		 */
24587c478bd9Sstevel@tonic-gate 		if (!(status & MCI_STATUS_VAL))
24597c478bd9Sstevel@tonic-gate 			continue;
24607c478bd9Sstevel@tonic-gate 		errcode = status & MCI_STATUS_ERRCODE;
24617c478bd9Sstevel@tonic-gate 		mserrcode = (status  >> MSERRCODE_SHFT) & MCI_STATUS_ERRCODE;
24627c478bd9Sstevel@tonic-gate 		if (status & MCI_STATUS_ADDRV) {
24637c478bd9Sstevel@tonic-gate 			/*
24647c478bd9Sstevel@tonic-gate 			 * If mci_addr contains the address where
24657c478bd9Sstevel@tonic-gate 			 * error occurred, display the address
24667c478bd9Sstevel@tonic-gate 			 */
24677c478bd9Sstevel@tonic-gate 			(void) rdmsr(mci_addr[i], &buf);
24687c478bd9Sstevel@tonic-gate 			addr = buf;
24697c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "MCE: Bank %d: error code 0x%x:"\
24707c478bd9Sstevel@tonic-gate 			    "addr = 0x%" PRIx64 ", model errcode = 0x%x", i,
24717c478bd9Sstevel@tonic-gate 			    errcode, addr, mserrcode);
24727c478bd9Sstevel@tonic-gate 		} else {
24737c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
24747c478bd9Sstevel@tonic-gate 			    "MCE: Bank %d: error code 0x%x, mserrcode = 0x%x",
24757c478bd9Sstevel@tonic-gate 			    i, errcode, mserrcode);
24767c478bd9Sstevel@tonic-gate 		}
24777c478bd9Sstevel@tonic-gate 		(void) wrmsr(mci_status[i], &allzeros);
24787c478bd9Sstevel@tonic-gate 	}
24797c478bd9Sstevel@tonic-gate 	return (ret);
24807c478bd9Sstevel@tonic-gate }
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate void
24837c478bd9Sstevel@tonic-gate setup_mtrr()
24847c478bd9Sstevel@tonic-gate {
24857c478bd9Sstevel@tonic-gate 	int i, ecx;
24867c478bd9Sstevel@tonic-gate 	int vcnt;
24877c478bd9Sstevel@tonic-gate 	struct	mtrrvar	*mtrrphys;
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	if (!(x86_feature & X86_MTRR))
24907c478bd9Sstevel@tonic-gate 		return;
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	(void) rdmsr(REG_MTRRCAP, &mtrrcap);
24937c478bd9Sstevel@tonic-gate 	(void) rdmsr(REG_MTRRDEF, &mtrrdef);
24947c478bd9Sstevel@tonic-gate 	if (mtrrcap & MTRRCAP_FIX) {
24957c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR64K, &mtrr64k);
24967c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR16K1, &mtrr16k1);
24977c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR16K2, &mtrr16k2);
24987c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR4K1, &mtrr4k1);
24997c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR4K2, &mtrr4k2);
25007c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR4K3, &mtrr4k3);
25017c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR4K4, &mtrr4k4);
25027c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR4K5, &mtrr4k5);
25037c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR4K6, &mtrr4k6);
25047c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR4K7, &mtrr4k7);
25057c478bd9Sstevel@tonic-gate 		(void) rdmsr(REG_MTRR4K8, &mtrr4k8);
25067c478bd9Sstevel@tonic-gate 	}
25077c478bd9Sstevel@tonic-gate 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
25087c478bd9Sstevel@tonic-gate 		vcnt = MAX_MTRRVAR;
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
25117c478bd9Sstevel@tonic-gate 		i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
25127c478bd9Sstevel@tonic-gate 		(void) rdmsr(ecx, &mtrrphys->mtrrphys_base);
25137c478bd9Sstevel@tonic-gate 		(void) rdmsr(ecx + 1, &mtrrphys->mtrrphys_mask);
25147c478bd9Sstevel@tonic-gate 		if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
25157c478bd9Sstevel@tonic-gate 			mtrrphys->mtrrphys_mask &= ~MTRRPHYSMASK_V;
25167c478bd9Sstevel@tonic-gate 		}
25177c478bd9Sstevel@tonic-gate 	}
25187c478bd9Sstevel@tonic-gate 	if (x86_feature & X86_PAT) {
25197c478bd9Sstevel@tonic-gate 		if (enable_relaxed_mtrr)
25207c478bd9Sstevel@tonic-gate 			mtrrdef = MTRR_TYPE_WB|MTRRDEF_FE|MTRRDEF_E;
25217c478bd9Sstevel@tonic-gate 		pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
25227c478bd9Sstevel@tonic-gate 	}
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	mtrr_sync();
25257c478bd9Sstevel@tonic-gate }
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate /*
25287c478bd9Sstevel@tonic-gate  * Sync current cpu mtrr with the incore copy of mtrr.
25297c478bd9Sstevel@tonic-gate  * This function has to be invoked with interrupts disabled
25307c478bd9Sstevel@tonic-gate  * Currently we do not capture other cpu's. This is invoked on cpu0
25317c478bd9Sstevel@tonic-gate  * just after reading /etc/system.
25327c478bd9Sstevel@tonic-gate  * On other cpu's its invoked from mp_startup().
25337c478bd9Sstevel@tonic-gate  */
25347c478bd9Sstevel@tonic-gate void
25357c478bd9Sstevel@tonic-gate mtrr_sync()
25367c478bd9Sstevel@tonic-gate {
25377c478bd9Sstevel@tonic-gate 	uint64_t my_mtrrdef;
25387c478bd9Sstevel@tonic-gate 	uint_t	crvalue, cr0_orig;
25397c478bd9Sstevel@tonic-gate 	int	vcnt, i, ecx;
25407c478bd9Sstevel@tonic-gate 	struct	mtrrvar	*mtrrphys;
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	cr0_orig = crvalue = getcr0();
25437c478bd9Sstevel@tonic-gate 	crvalue |= CR0_CD;
25447c478bd9Sstevel@tonic-gate 	crvalue &= ~CR0_NW;
25457c478bd9Sstevel@tonic-gate 	setcr0(crvalue);
25467c478bd9Sstevel@tonic-gate 	invalidate_cache();
25477c478bd9Sstevel@tonic-gate 	setcr3(getcr3());
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 	if (x86_feature & X86_PAT) {
25507c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRRPAT, &pat_attr_reg);
25517c478bd9Sstevel@tonic-gate 	}
25527c478bd9Sstevel@tonic-gate 	(void) rdmsr(REG_MTRRDEF, &my_mtrrdef);
25537c478bd9Sstevel@tonic-gate 	my_mtrrdef &= ~MTRRDEF_E;
25547c478bd9Sstevel@tonic-gate 	(void) wrmsr(REG_MTRRDEF, &my_mtrrdef);
25557c478bd9Sstevel@tonic-gate 	if (mtrrcap & MTRRCAP_FIX) {
25567c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR64K, &mtrr64k);
25577c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR16K1, &mtrr16k1);
25587c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR16K2, &mtrr16k2);
25597c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR4K1, &mtrr4k1);
25607c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR4K2, &mtrr4k2);
25617c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR4K3, &mtrr4k3);
25627c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR4K4, &mtrr4k4);
25637c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR4K5, &mtrr4k5);
25647c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR4K6, &mtrr4k6);
25657c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR4K7, &mtrr4k7);
25667c478bd9Sstevel@tonic-gate 		(void) wrmsr(REG_MTRR4K8, &mtrr4k8);
25677c478bd9Sstevel@tonic-gate 	}
25687c478bd9Sstevel@tonic-gate 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
25697c478bd9Sstevel@tonic-gate 		vcnt = MAX_MTRRVAR;
25707c478bd9Sstevel@tonic-gate 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
25717c478bd9Sstevel@tonic-gate 		i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
25727c478bd9Sstevel@tonic-gate 		(void) wrmsr(ecx, &mtrrphys->mtrrphys_base);
25737c478bd9Sstevel@tonic-gate 		(void) wrmsr(ecx + 1, &mtrrphys->mtrrphys_mask);
25747c478bd9Sstevel@tonic-gate 	}
25757c478bd9Sstevel@tonic-gate 	(void) wrmsr(REG_MTRRDEF, &mtrrdef);
25767c478bd9Sstevel@tonic-gate 	setcr3(getcr3());
25777c478bd9Sstevel@tonic-gate 	invalidate_cache();
25787c478bd9Sstevel@tonic-gate 	setcr0(cr0_orig);
25797c478bd9Sstevel@tonic-gate }
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate /*
25827c478bd9Sstevel@tonic-gate  * resync mtrr so that BIOS is happy. Called from mdboot
25837c478bd9Sstevel@tonic-gate  */
25847c478bd9Sstevel@tonic-gate void
25857c478bd9Sstevel@tonic-gate mtrr_resync()
25867c478bd9Sstevel@tonic-gate {
25877c478bd9Sstevel@tonic-gate 	if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
25887c478bd9Sstevel@tonic-gate 		/*
25897c478bd9Sstevel@tonic-gate 		 * We could have changed the default mtrr definition.
25907c478bd9Sstevel@tonic-gate 		 * Put it back to uncached which is what it is at power on
25917c478bd9Sstevel@tonic-gate 		 */
25927c478bd9Sstevel@tonic-gate 		mtrrdef = MTRR_TYPE_UC|MTRRDEF_FE|MTRRDEF_E;
25937c478bd9Sstevel@tonic-gate 		mtrr_sync();
25947c478bd9Sstevel@tonic-gate 	}
25957c478bd9Sstevel@tonic-gate }
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate void
25987c478bd9Sstevel@tonic-gate get_system_configuration()
25997c478bd9Sstevel@tonic-gate {
26007c478bd9Sstevel@tonic-gate 	char	prop[32];
26017c478bd9Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	if (((BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop)) ||
26047c478bd9Sstevel@tonic-gate 		(BOP_GETPROP(bootops, "nodes", prop) < 0) 	||
26057c478bd9Sstevel@tonic-gate 		(kobj_getvalue(prop, &nodes_ll) == -1) ||
26067c478bd9Sstevel@tonic-gate 		(nodes_ll > MAXNODES))			   ||
26077c478bd9Sstevel@tonic-gate 	    ((BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop)) ||
26087c478bd9Sstevel@tonic-gate 		(BOP_GETPROP(bootops, "cpus_pernode", prop) < 0) ||
26097c478bd9Sstevel@tonic-gate 		(kobj_getvalue(prop, &cpus_pernode_ll) == -1))) {
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
26127c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
26137c478bd9Sstevel@tonic-gate 	} else {
26147c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
26157c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop)) ||
26187c478bd9Sstevel@tonic-gate 		(BOP_GETPROP(bootops, "kernelbase", prop) < 0) 	||
26197c478bd9Sstevel@tonic-gate 		(kobj_getvalue(prop, &lvalue) == -1))
26207c478bd9Sstevel@tonic-gate 			eprom_kernelbase = NULL;
26217c478bd9Sstevel@tonic-gate 	else
26227c478bd9Sstevel@tonic-gate 			eprom_kernelbase = (uintptr_t)lvalue;
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop)) ||
26257c478bd9Sstevel@tonic-gate 	    (BOP_GETPROP(bootops, "segmapsize", prop) < 0) ||
26267c478bd9Sstevel@tonic-gate 	    (kobj_getvalue(prop, &lvalue) == -1)) {
26277c478bd9Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
26287c478bd9Sstevel@tonic-gate 	} else {
26297c478bd9Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
26307c478bd9Sstevel@tonic-gate 	}
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop)) ||
26337c478bd9Sstevel@tonic-gate 	    (BOP_GETPROP(bootops, "segmapfreelists", prop) < 0) ||
26347c478bd9Sstevel@tonic-gate 	    (kobj_getvalue(prop, &lvalue) == -1)) {
26357c478bd9Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
26367c478bd9Sstevel@tonic-gate 	} else {
26377c478bd9Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
26387c478bd9Sstevel@tonic-gate 	}
26397c478bd9Sstevel@tonic-gate }
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate /*
26427c478bd9Sstevel@tonic-gate  * Add to a memory list.
26437c478bd9Sstevel@tonic-gate  * start = start of new memory segment
26447c478bd9Sstevel@tonic-gate  * len = length of new memory segment in bytes
26457c478bd9Sstevel@tonic-gate  * new = pointer to a new struct memlist
26467c478bd9Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
26477c478bd9Sstevel@tonic-gate  */
26487c478bd9Sstevel@tonic-gate static void
26497c478bd9Sstevel@tonic-gate memlist_add(
26507c478bd9Sstevel@tonic-gate 	uint64_t start,
26517c478bd9Sstevel@tonic-gate 	uint64_t len,
26527c478bd9Sstevel@tonic-gate 	struct memlist *new,
26537c478bd9Sstevel@tonic-gate 	struct memlist **memlistp)
26547c478bd9Sstevel@tonic-gate {
26557c478bd9Sstevel@tonic-gate 	struct memlist *cur;
26567c478bd9Sstevel@tonic-gate 	uint64_t end = start + len;
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	new->address = start;
26597c478bd9Sstevel@tonic-gate 	new->size = len;
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 	cur = *memlistp;
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	while (cur) {
26647c478bd9Sstevel@tonic-gate 		if (cur->address >= end) {
26657c478bd9Sstevel@tonic-gate 			new->next = cur;
26667c478bd9Sstevel@tonic-gate 			*memlistp = new;
26677c478bd9Sstevel@tonic-gate 			new->prev = cur->prev;
26687c478bd9Sstevel@tonic-gate 			cur->prev = new;
26697c478bd9Sstevel@tonic-gate 			return;
26707c478bd9Sstevel@tonic-gate 		}
26717c478bd9Sstevel@tonic-gate 		ASSERT(cur->address + cur->size <= start);
26727c478bd9Sstevel@tonic-gate 		if (cur->next == NULL) {
26737c478bd9Sstevel@tonic-gate 			cur->next = new;
26747c478bd9Sstevel@tonic-gate 			new->prev = cur;
26757c478bd9Sstevel@tonic-gate 			new->next = NULL;
26767c478bd9Sstevel@tonic-gate 			return;
26777c478bd9Sstevel@tonic-gate 		}
26787c478bd9Sstevel@tonic-gate 		memlistp = &cur->next;
26797c478bd9Sstevel@tonic-gate 		cur = cur->next;
26807c478bd9Sstevel@tonic-gate 	}
26817c478bd9Sstevel@tonic-gate }
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate void
26847c478bd9Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
26857c478bd9Sstevel@tonic-gate {
26867c478bd9Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
26877c478bd9Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
26887c478bd9Sstevel@tonic-gate 
26897c478bd9Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
26907c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
26917c478bd9Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
26927c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
26937c478bd9Sstevel@tonic-gate }
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate caddr_t
26967c478bd9Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
26977c478bd9Sstevel@tonic-gate {
26987c478bd9Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
26997c478bd9Sstevel@tonic-gate }
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27027c478bd9Sstevel@tonic-gate caddr_t
27037c478bd9Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
27047c478bd9Sstevel@tonic-gate {
27057c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
27067c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
27077c478bd9Sstevel@tonic-gate 	return (0);
27087c478bd9Sstevel@tonic-gate }
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27117c478bd9Sstevel@tonic-gate void
27127c478bd9Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
27137c478bd9Sstevel@tonic-gate {
27147c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
27157c478bd9Sstevel@tonic-gate }
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate /*
27187c478bd9Sstevel@tonic-gate  * This is called just after configure() in startup().
27197c478bd9Sstevel@tonic-gate  *
27207c478bd9Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
27217c478bd9Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
27227c478bd9Sstevel@tonic-gate  * given that various parts of the instruction set may appear
27237c478bd9Sstevel@tonic-gate  * and disappear between different implementations.
27247c478bd9Sstevel@tonic-gate  *
27257c478bd9Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
27267c478bd9Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
27277c478bd9Sstevel@tonic-gate  * more flexibility.
27287c478bd9Sstevel@tonic-gate  *
27297c478bd9Sstevel@tonic-gate  * So, we just leave this alone.
27307c478bd9Sstevel@tonic-gate  */
27317c478bd9Sstevel@tonic-gate void
27327c478bd9Sstevel@tonic-gate setx86isalist(void)
27337c478bd9Sstevel@tonic-gate {
27347c478bd9Sstevel@tonic-gate 	char *tp;
27357c478bd9Sstevel@tonic-gate 	size_t len;
27367c478bd9Sstevel@tonic-gate 	extern char *isa_list;
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate #define	TBUFSIZE	1024
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
27417c478bd9Sstevel@tonic-gate 	*tp = '\0';
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate #if defined(__amd64)
27447c478bd9Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
27457c478bd9Sstevel@tonic-gate #endif
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	switch (x86_vendor) {
27487c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
27497c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
27507c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
27517c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
27527c478bd9Sstevel@tonic-gate 			/*
27537c478bd9Sstevel@tonic-gate 			 * Pentium Pro or later
27547c478bd9Sstevel@tonic-gate 			 */
27557c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
27567c478bd9Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
27577c478bd9Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
27587c478bd9Sstevel@tonic-gate 		}
27597c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
27607c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
27617c478bd9Sstevel@tonic-gate 		/*
27627c478bd9Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
27637c478bd9Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
27647c478bd9Sstevel@tonic-gate 		 */
27657c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
27667c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
27677c478bd9Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
27687c478bd9Sstevel@tonic-gate 			    "+mmx pentium " : " ");
27697c478bd9Sstevel@tonic-gate 		}
27707c478bd9Sstevel@tonic-gate 		break;
27717c478bd9Sstevel@tonic-gate 	default:
27727c478bd9Sstevel@tonic-gate 		break;
27737c478bd9Sstevel@tonic-gate 	}
27747c478bd9Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
27757c478bd9Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
27767c478bd9Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
27777c478bd9Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate #undef TBUFSIZE
27807c478bd9Sstevel@tonic-gate }
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate #ifdef __amd64
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate void *
27867c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
27877c478bd9Sstevel@tonic-gate {
27887c478bd9Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
27897c478bd9Sstevel@tonic-gate }
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate void
27927c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
27937c478bd9Sstevel@tonic-gate {
27947c478bd9Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
27957c478bd9Sstevel@tonic-gate }
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate #else
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate void *
28007c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
28017c478bd9Sstevel@tonic-gate {
28027c478bd9Sstevel@tonic-gate 	caddr_t	vaddr;
28037c478bd9Sstevel@tonic-gate 	uintptr_t v;
28047c478bd9Sstevel@tonic-gate 	size_t	start;
28057c478bd9Sstevel@tonic-gate 	size_t	end;
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
28087c478bd9Sstevel@tonic-gate 	if (vaddr == NULL)
28097c478bd9Sstevel@tonic-gate 		return (NULL);
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
28127c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
28137c478bd9Sstevel@tonic-gate 	ASSERT(v + size <= ptable_va);
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
28167c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
28177c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
28187c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 	while (start <= end) {
28217c478bd9Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
28227c478bd9Sstevel@tonic-gate 		++start;
28237c478bd9Sstevel@tonic-gate 	}
28247c478bd9Sstevel@tonic-gate 	return (vaddr);
28257c478bd9Sstevel@tonic-gate }
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate void
28287c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
28297c478bd9Sstevel@tonic-gate {
28307c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
28317c478bd9Sstevel@tonic-gate 	size_t	start;
28327c478bd9Sstevel@tonic-gate 	size_t	end;
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
28357c478bd9Sstevel@tonic-gate 	ASSERT(v + size <= ptable_va);
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
28387c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
28397c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
28407c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	while (start <= end) {
28437c478bd9Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
28447c478bd9Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
28457c478bd9Sstevel@tonic-gate 		++start;
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
28487c478bd9Sstevel@tonic-gate }
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate /*
28517c478bd9Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
28527c478bd9Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
28537c478bd9Sstevel@tonic-gate  */
28547c478bd9Sstevel@tonic-gate void *
28557c478bd9Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
28567c478bd9Sstevel@tonic-gate {
28577c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
28587c478bd9Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
28597c478bd9Sstevel@tonic-gate 	size_t start;
28607c478bd9Sstevel@tonic-gate 	size_t end;
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	/*
28637c478bd9Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
28647c478bd9Sstevel@tonic-gate 	 */
28657c478bd9Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
28667c478bd9Sstevel@tonic-gate 		return (NULL);
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 	/*
28697c478bd9Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
28707c478bd9Sstevel@tonic-gate 	 */
28717c478bd9Sstevel@tonic-gate 	if (v >= ptable_va || eaddr < kernelbase)
28727c478bd9Sstevel@tonic-gate 		return (NULL);
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	/*
28757c478bd9Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
28767c478bd9Sstevel@tonic-gate 	 */
28777c478bd9Sstevel@tonic-gate 	if (v < kernelbase)
28787c478bd9Sstevel@tonic-gate 		v = kernelbase;
28797c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
28807c478bd9Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
28817c478bd9Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
28827c478bd9Sstevel@tonic-gate 		end = toxic_bit_map_len;
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
28857c478bd9Sstevel@tonic-gate 		return (NULL);
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	v = kernelbase + ptob(start);
28887c478bd9Sstevel@tonic-gate 	if (len != NULL)
28897c478bd9Sstevel@tonic-gate 		*len = ptob(end - start);
28907c478bd9Sstevel@tonic-gate 	return ((void *)v);
28917c478bd9Sstevel@tonic-gate }
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate #endif
2894