xref: /onnv-gate/usr/src/uts/i86pc/os/startup.c (revision 3717:8471164f3e47)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51455Sandrei  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223434Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/t_lock.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/signal.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/user.h>
350Sstevel@tonic-gate #include <sys/mman.h>
360Sstevel@tonic-gate #include <sys/vm.h>
370Sstevel@tonic-gate #include <sys/conf.h>
380Sstevel@tonic-gate #include <sys/avintr.h>
390Sstevel@tonic-gate #include <sys/autoconf.h>
400Sstevel@tonic-gate #include <sys/disp.h>
410Sstevel@tonic-gate #include <sys/class.h>
420Sstevel@tonic-gate #include <sys/bitmap.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/privregs.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/proc.h>
470Sstevel@tonic-gate #include <sys/buf.h>
480Sstevel@tonic-gate #include <sys/kmem.h>
493446Smrj #include <sys/mem.h>
500Sstevel@tonic-gate #include <sys/kstat.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <sys/reboot.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/cred.h>
550Sstevel@tonic-gate #include <sys/vnode.h>
560Sstevel@tonic-gate #include <sys/file.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/procfs.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <sys/vfs.h>
610Sstevel@tonic-gate #include <sys/cmn_err.h>
620Sstevel@tonic-gate #include <sys/utsname.h>
630Sstevel@tonic-gate #include <sys/debug.h>
640Sstevel@tonic-gate #include <sys/kdi.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/dumphdr.h>
670Sstevel@tonic-gate #include <sys/bootconf.h>
680Sstevel@tonic-gate #include <sys/varargs.h>
690Sstevel@tonic-gate #include <sys/promif.h>
700Sstevel@tonic-gate #include <sys/modctl.h>		/* for "procfs" hack */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <sys/sunddi.h>
730Sstevel@tonic-gate #include <sys/sunndi.h>
740Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
750Sstevel@tonic-gate #include <sys/ddidmareq.h>
760Sstevel@tonic-gate #include <sys/psw.h>
770Sstevel@tonic-gate #include <sys/regset.h>
780Sstevel@tonic-gate #include <sys/clock.h>
790Sstevel@tonic-gate #include <sys/pte.h>
800Sstevel@tonic-gate #include <sys/tss.h>
810Sstevel@tonic-gate #include <sys/stack.h>
820Sstevel@tonic-gate #include <sys/trap.h>
830Sstevel@tonic-gate #include <sys/fp.h>
840Sstevel@tonic-gate #include <vm/anon.h>
850Sstevel@tonic-gate #include <vm/as.h>
860Sstevel@tonic-gate #include <vm/page.h>
870Sstevel@tonic-gate #include <vm/seg.h>
880Sstevel@tonic-gate #include <vm/seg_dev.h>
890Sstevel@tonic-gate #include <vm/seg_kmem.h>
900Sstevel@tonic-gate #include <vm/seg_kpm.h>
910Sstevel@tonic-gate #include <vm/seg_map.h>
920Sstevel@tonic-gate #include <vm/seg_vn.h>
930Sstevel@tonic-gate #include <vm/seg_kp.h>
940Sstevel@tonic-gate #include <sys/memnode.h>
950Sstevel@tonic-gate #include <vm/vm_dep.h>
960Sstevel@tonic-gate #include <sys/thread.h>
970Sstevel@tonic-gate #include <sys/sysconf.h>
980Sstevel@tonic-gate #include <sys/vm_machparam.h>
990Sstevel@tonic-gate #include <sys/archsystm.h>
1000Sstevel@tonic-gate #include <sys/machsystm.h>
1010Sstevel@tonic-gate #include <vm/hat.h>
1020Sstevel@tonic-gate #include <vm/hat_i86.h>
1030Sstevel@tonic-gate #include <sys/pmem.h>
1040Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1050Sstevel@tonic-gate #include <sys/x86_archext.h>
1060Sstevel@tonic-gate #include <sys/segments.h>
1070Sstevel@tonic-gate #include <sys/clconf.h>
1080Sstevel@tonic-gate #include <sys/kobj.h>
1090Sstevel@tonic-gate #include <sys/kobj_lex.h>
1100Sstevel@tonic-gate #include <sys/cpc_impl.h>
1113434Sesaxe #include <sys/pg.h>
1120Sstevel@tonic-gate #include <sys/x86_archext.h>
1131414Scindi #include <sys/cpu_module.h>
114437Smws #include <sys/smbios.h>
1153446Smrj #include <sys/debug_info.h>
1163446Smrj 
1173446Smrj 
1183446Smrj #include <sys/bootinfo.h>
1193446Smrj #include <vm/kboot_mmu.h>
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate extern void progressbar_init(void);
1220Sstevel@tonic-gate extern void progressbar_start(void);
1232712Snn35248 extern void brand_init(void);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1270Sstevel@tonic-gate  * interface.
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * segkp
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate extern int segkp_fromheap;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static void kvm_init(void);
1370Sstevel@tonic-gate static void startup_init(void);
1380Sstevel@tonic-gate static void startup_memlist(void);
1393446Smrj static void startup_kmem(void);
1400Sstevel@tonic-gate static void startup_modules(void);
1410Sstevel@tonic-gate static void startup_vm(void);
1420Sstevel@tonic-gate static void startup_end(void);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1460Sstevel@tonic-gate  */
1471479Skchow #ifdef __i386
1481479Skchow /*
1491479Skchow  * Due to virtual address space limitations running in 32 bit mode, restrict
1501479Skchow  * the amount of physical memory configured to a max of PHYSMEM32 pages (16g).
1511479Skchow  *
1521479Skchow  * If the physical max memory size of 64g were allowed to be configured, the
1531479Skchow  * size of user virtual address space will be less than 1g. A limited user
1541479Skchow  * address space greatly reduces the range of applications that can run.
1551479Skchow  *
1561479Skchow  * If more physical memory than PHYSMEM32 is required, users should preferably
1571479Skchow  * run in 64 bit mode which has no virtual address space limitation issues.
1581479Skchow  *
1591479Skchow  * If 64 bit mode is not available (as in IA32) and/or more physical memory
1601479Skchow  * than PHYSMEM32 is required in 32 bit mode, physmem can be set to the desired
1611479Skchow  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
1621479Skchow  * should also be carefully tuned to balance out the need of the user
1631479Skchow  * application while minimizing the risk of kernel heap exhaustion due to
1641479Skchow  * kernelbase being set too high.
1651479Skchow  */
1661479Skchow #define	PHYSMEM32	0x400000
1671479Skchow 
1681479Skchow pgcnt_t physmem = PHYSMEM32;
1691479Skchow #else
1700Sstevel@tonic-gate pgcnt_t physmem = 0;	/* memory size in pages, patch if you want less */
1711479Skchow #endif
1720Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate char *kobj_file_buf;
1750Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
1780Sstevel@tonic-gate caddr_t	rm_platter_va;
1790Sstevel@tonic-gate uint32_t rm_platter_pa;
1800Sstevel@tonic-gate 
181841Skchow int	auto_lpg_disable = 1;
182841Skchow 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate uintptr_t hole_start, hole_end;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * kpm mapping window
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate caddr_t kpm_vbase;
1920Sstevel@tonic-gate size_t  kpm_size;
1930Sstevel@tonic-gate static int kpm_desired = 0;		/* Do we want to try to use segkpm? */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate  * Configuration parameters set at boot time.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
2020Sstevel@tonic-gate struct bootops		**bootopsp;
2030Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate char bootblock_fstype[16];
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2103290Sjohansen  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
2113290Sjohansen  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
2123290Sjohansen  * zio buffers from their own segment, otherwise they are allocated from the
2133290Sjohansen  * heap.  The optimization of allocating zio buffers from their own segment is
2143290Sjohansen  * only valid on 64-bit kernels.
2153290Sjohansen  */
2163290Sjohansen #if defined(__amd64)
2173290Sjohansen int segzio_fromheap = 0;
2183290Sjohansen #else
2193290Sjohansen int segzio_fromheap = 1;
2203290Sjohansen #endif
2213290Sjohansen 
2223290Sjohansen /*
2230Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
2240Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2250Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * VM data structures
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
2330Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2340Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
2350Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2360Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
2370Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
2380Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
2390Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
2423446Smrj static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
2433446Smrj 
2440Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate #if defined(__amd64)
2470Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
2480Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
2490Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
2500Sstevel@tonic-gate #else
2510Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
2520Sstevel@tonic-gate #endif
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
2553290Sjohansen caddr_t segzio_base;		/* Base address of segzio */
2560Sstevel@tonic-gate #if defined(__amd64)
2570Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
2580Sstevel@tonic-gate #else
2590Sstevel@tonic-gate pgcnt_t segkpsize = 0;
2600Sstevel@tonic-gate #endif
2613290Sjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
2620Sstevel@tonic-gate 
2633446Smrj /*
2643446Smrj  * VA range available to the debugger
2653446Smrj  */
2663446Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
2673446Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
2683446Smrj 
2690Sstevel@tonic-gate struct memseg *memseg_base;
2700Sstevel@tonic-gate struct vnode unused_pages_vp;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate #define	FOURGB	0x100000000LL
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate struct memlist *memlist;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
2770Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
2780Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
2790Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
2800Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
2810Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
2820Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
2830Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
2860Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
2900Sstevel@tonic-gate  */
2913446Smrj static pgcnt_t kphysm_init(page_t *, pgcnt_t);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate  * a couple useful roundup macros
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
2990Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
3000Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
3010Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
3020Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
3033446Smrj 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
3040Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
3050Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
3090Sstevel@tonic-gate  *		+-----------------------+
3103446Smrj  *		|			|
3110Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
3120Sstevel@tonic-gate  *		|	debugger	|
3130Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
3140Sstevel@tonic-gate  *		|      Kernel Data	|
3150Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
3160Sstevel@tonic-gate  *              |      Kernel Text	|
3170Sstevel@tonic-gate  * 0xFE800000  -|-----------------------|- KERNEL_TEXT
3183446Smrj  *		|---       GDT       ---|- GDT page (GDT_VA)
3193446Smrj  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
3203446Smrj  *		|			|
3213446Smrj  * 		|   page_t structures	|
3220Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
3230Sstevel@tonic-gate  * 		|   page hash, etc.	|
3243446Smrj  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
3253446Smrj  *		|			|  (segkp is just an arena in the heap)
3260Sstevel@tonic-gate  *		|			|
3270Sstevel@tonic-gate  *		|	kvseg		|
3280Sstevel@tonic-gate  *		|			|
3290Sstevel@tonic-gate  *		|			|
3300Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
3310Sstevel@tonic-gate  * 		|        Segkmap	|
3323446Smrj  * 0xC3002000  -|-----------------------|- segmap_start (floating)
3330Sstevel@tonic-gate  *		|	Red Zone	|
3340Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
3350Sstevel@tonic-gate  *		|			|			||
3360Sstevel@tonic-gate  *		|     Shared objects	|			\/
3370Sstevel@tonic-gate  *		|			|
3380Sstevel@tonic-gate  *		:			:
3390Sstevel@tonic-gate  *		|	user data	|
3400Sstevel@tonic-gate  *		|-----------------------|
3410Sstevel@tonic-gate  *		|	user text	|
3420Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
3430Sstevel@tonic-gate  *		|	user stack	|
3440Sstevel@tonic-gate  *		:			:
3450Sstevel@tonic-gate  *		|	invalid		|
3460Sstevel@tonic-gate  * 0x00000000	+-----------------------+
3470Sstevel@tonic-gate  *
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
3500Sstevel@tonic-gate  *			+-----------------------+
3513446Smrj  *			|			|
3520Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
3530Sstevel@tonic-gate  *			|	debugger (?)	|
3540Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
3550Sstevel@tonic-gate  *			|      unused    	|
3560Sstevel@tonic-gate  *			+-----------------------+
3570Sstevel@tonic-gate  *			|      Kernel Data	|
3580Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
3590Sstevel@tonic-gate  *			|      Kernel Text	|
3600Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
3613446Smrj  *			|---       GDT       ---|- GDT page (GDT_VA)
3623446Smrj  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
3633446Smrj  *			|			|
3640Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
3650Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
3660Sstevel@tonic-gate  *			|	 Kernel		|
3670Sstevel@tonic-gate  *			|	  heap		|
3680Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
3693446Smrj  *			|	 segmap		|
3703446Smrj  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
3710Sstevel@tonic-gate  *			|    device mappings	|
3720Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
3733290Sjohansen  *			|	  segzio	|
3743290Sjohansen  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
3750Sstevel@tonic-gate  *			|	  segkp		|
3763446Smrj  * ---                  |-----------------------|- segkp_base (floating)
3773446Smrj  * 			|   page_t structures	|  valloc_base + valloc_sz
3783446Smrj  * 			|   memsegs, memlists, 	|
3793446Smrj  * 			|   page hash, etc.	|
3803446Smrj  * 0xFFFFFF00.00000000  |-----------------------|- valloc_base
3810Sstevel@tonic-gate  *			|	 segkpm		|
3820Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
3830Sstevel@tonic-gate  *			|	Red Zone	|
3840Sstevel@tonic-gate  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE
3850Sstevel@tonic-gate  *			|     User stack	|- User space memory
3860Sstevel@tonic-gate  * 			|			|
3870Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
3880Sstevel@tonic-gate  *			:			:
3890Sstevel@tonic-gate  * 			|			|
3900Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
3910Sstevel@tonic-gate  * 			|			|
3920Sstevel@tonic-gate  * 			| VA Hole / unused	|
3930Sstevel@tonic-gate  * 			|			|
3940Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
3950Sstevel@tonic-gate  *			|			|
3960Sstevel@tonic-gate  *			|			|
3970Sstevel@tonic-gate  *			:			:
3980Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
3990Sstevel@tonic-gate  *			|			|
4000Sstevel@tonic-gate  *			|	user data	|
4010Sstevel@tonic-gate  *			|-----------------------|
4020Sstevel@tonic-gate  *			|	user text	|
4030Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
4040Sstevel@tonic-gate  *			|	invalid		|
4050Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
4060Sstevel@tonic-gate  *
4070Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
4080Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
4090Sstevel@tonic-gate  *
4100Sstevel@tonic-gate  * Floating values:
4110Sstevel@tonic-gate  *
4120Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
4133446Smrj  * structures.  This region contains page_t structures for
4143446Smrj  * physical memory, memsegs, memlists, and the page hash.
4150Sstevel@tonic-gate  *
4160Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
4170Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
4180Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
4190Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
4200Sstevel@tonic-gate  * accessed using rip-relative addressing
4210Sstevel@tonic-gate  *
4220Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
4230Sstevel@tonic-gate  *
4240Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
4250Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
4260Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
4270Sstevel@tonic-gate  *
4283446Smrj  * segmap_start: start of segmap. The length of segmap can be modified
4290Sstevel@tonic-gate  * by changing segmapsize in /etc/system (preferred) or eeprom (deprecated).
4300Sstevel@tonic-gate  * The default length is 16MB on 32-bit systems and 64MB on 64-bit systems.
4310Sstevel@tonic-gate  *
4320Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
4330Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
4340Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
4350Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
4360Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
4370Sstevel@tonic-gate  * segkp sizes).
4380Sstevel@tonic-gate  *
4390Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
4400Sstevel@tonic-gate  *	size	page_t's			size
4410Sstevel@tonic-gate  *	----	---------	----------	-----------
4420Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
4430Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
4440Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
4450Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
4460Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
4470Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
4480Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
4490Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
4500Sstevel@tonic-gate  *
4510Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
4520Sstevel@tonic-gate  * configurations above 8gb.
4530Sstevel@tonic-gate  *
4540Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
4550Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate /* real-time-clock initialization parameters */
4593446Smrj extern time_t process_rtc_config_file(void);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate char		*final_kernelheap;
4620Sstevel@tonic-gate char		*boot_kernelheap;
4630Sstevel@tonic-gate uintptr_t	kernelbase;
4643446Smrj uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
4650Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
4660Sstevel@tonic-gate size_t		segmapsize;
4670Sstevel@tonic-gate static uintptr_t segmap_reserved;
4683446Smrj uintptr_t	segmap_start;
4690Sstevel@tonic-gate int		segmapfreelists;
4700Sstevel@tonic-gate pgcnt_t		npages;
4713446Smrj pgcnt_t		orig_npages;
4720Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
4730Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
4770Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
4780Sstevel@tonic-gate  * the bootstrap.
4790Sstevel@tonic-gate  */
4803446Smrj static page_t *bootpages;
4813446Smrj 
4823446Smrj /*
4833446Smrj  * boot time pages that have a vnode from the ramdisk will keep that forever.
4843446Smrj  */
4853446Smrj static page_t *rd_pages;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate struct system_hardware system_hardware;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
4910Sstevel@tonic-gate  */
4920Sstevel@tonic-gate static void
4933446Smrj print_memlist(char *title, struct memlist *mp)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
4960Sstevel@tonic-gate 	while (mp != NULL)  {
4970Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
4980Sstevel@tonic-gate 		    mp->address, mp->size);
4990Sstevel@tonic-gate 		mp = mp->next;
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
5050Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
5060Sstevel@tonic-gate  */
5070Sstevel@tonic-gate int	l2cache_sz = 0x80000;
5080Sstevel@tonic-gate int	l2cache_linesz = 0x40;
5090Sstevel@tonic-gate int	l2cache_assoc = 1;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate /*
5120Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
5130Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate #ifdef __amd64
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate vmem_t		*device_arena;
5180Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
5193446Smrj size_t		toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate #else	/* __i386 */
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
5240Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate #endif	/* __i386 */
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate  * Simple boot time debug facilities
5300Sstevel@tonic-gate  */
5310Sstevel@tonic-gate static char *prm_dbg_str[] = {
5320Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%x\n",
5330Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%llx\n"
5340Sstevel@tonic-gate };
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate int prom_debug;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate #define	PRM_DEBUG(q)	if (prom_debug) 	\
5390Sstevel@tonic-gate 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
5400Sstevel@tonic-gate #define	PRM_POINT(q)	if (prom_debug) 	\
5410Sstevel@tonic-gate 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate /*
5440Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
5450Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
5460Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
5470Sstevel@tonic-gate  */
5480Sstevel@tonic-gate #define	NUM_ALLOCATIONS 7
5490Sstevel@tonic-gate int num_allocations = 0;
5500Sstevel@tonic-gate struct {
5510Sstevel@tonic-gate 	void **al_ptr;
5520Sstevel@tonic-gate 	size_t al_size;
5530Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
5540Sstevel@tonic-gate size_t valloc_sz = 0;
5550Sstevel@tonic-gate uintptr_t valloc_base;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
5580Sstevel@tonic-gate 		size = ROUND_UP_PAGE(size);		 		\
5590Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
5600Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
5610Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
5620Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
5630Sstevel@tonic-gate 		valloc_sz += size;					\
5640Sstevel@tonic-gate 		++num_allocations;				 	\
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5673446Smrj /*
5683446Smrj  * Allocate all the initial memory needed by the page allocator.
5693446Smrj  */
5700Sstevel@tonic-gate static void
5710Sstevel@tonic-gate perform_allocations(void)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	caddr_t mem;
5740Sstevel@tonic-gate 	int i;
5753446Smrj 	int valloc_align;
5760Sstevel@tonic-gate 
5773446Smrj 	PRM_DEBUG(valloc_base);
5783446Smrj 	PRM_DEBUG(valloc_sz);
5793446Smrj 	valloc_align = mmu.level_size[mmu.max_page_level > 0];
5803446Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
5810Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
5820Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
5830Sstevel@tonic-gate 	bzero(mem, valloc_sz);
5840Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
5850Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
5860Sstevel@tonic-gate 		mem += allocations[i].al_size;
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate /*
5910Sstevel@tonic-gate  * Our world looks like this at startup time.
5920Sstevel@tonic-gate  *
5930Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
5940Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
5950Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
5960Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
5970Sstevel@tonic-gate  *
5980Sstevel@tonic-gate  * On the text page:
5990Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
6000Sstevel@tonic-gate  *
6010Sstevel@tonic-gate  * On the data page:
6023446Smrj  * unix/genunix/krtld/module data loads.
6033446Smrj  *
6040Sstevel@tonic-gate  * Machine-dependent startup code
6050Sstevel@tonic-gate  */
6060Sstevel@tonic-gate void
6070Sstevel@tonic-gate startup(void)
6080Sstevel@tonic-gate {
6091494Sjg 	extern void startup_bios_disk(void);
6101494Sjg 	extern void startup_pci_bios(void);
6110Sstevel@tonic-gate 	/*
6120Sstevel@tonic-gate 	 * Make sure that nobody tries to use sekpm until we have
6130Sstevel@tonic-gate 	 * initialized it properly.
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate #if defined(__amd64)
6160Sstevel@tonic-gate 	kpm_desired = kpm_enable;
6170Sstevel@tonic-gate #endif
6180Sstevel@tonic-gate 	kpm_enable = 0;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	progressbar_init();
6210Sstevel@tonic-gate 	startup_init();
6220Sstevel@tonic-gate 	startup_memlist();
6233446Smrj 	startup_kmem();
6241494Sjg 	startup_pci_bios();
6250Sstevel@tonic-gate 	startup_modules();
6260Sstevel@tonic-gate 	startup_bios_disk();
6270Sstevel@tonic-gate 	startup_vm();
6280Sstevel@tonic-gate 	startup_end();
6290Sstevel@tonic-gate 	progressbar_start();
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate static void
6330Sstevel@tonic-gate startup_init()
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	/*
6380Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
6390Sstevel@tonic-gate 	 */
6400Sstevel@tonic-gate 	cpuid_pass2(CPU);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	/*
6450Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
6460Sstevel@tonic-gate 	 */
6470Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
6480Sstevel@tonic-gate 		++prom_debug;
6490Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	/*
6530Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
6540Sstevel@tonic-gate 	 */
6550Sstevel@tonic-gate 	get_system_configuration();
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	/*
6580Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
6590Sstevel@tonic-gate 	 */
6600Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
6610Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
6620Sstevel@tonic-gate 		    CPU->cpu_brandstr);
6630Sstevel@tonic-gate 		halt("This processor is not supported by this release "
6640Sstevel@tonic-gate 		    "of Solaris.");
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate /*
6710Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
6720Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
6730Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
6740Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate static void
6770Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	uintptr_t va;
6800Sstevel@tonic-gate 	uintptr_t next_va;
6810Sstevel@tonic-gate 	pfn_t pfn;
6820Sstevel@tonic-gate 	uint64_t pfn_addr;
6830Sstevel@tonic-gate 	uint64_t pfn_eaddr;
6840Sstevel@tonic-gate 	uint_t prot;
6850Sstevel@tonic-gate 	size_t len;
6860Sstevel@tonic-gate 	uint_t change;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	if (prom_debug)
6890Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
6900Sstevel@tonic-gate 		    *addr, *size);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
6940Sstevel@tonic-gate 	 */
6950Sstevel@tonic-gate 	if (*addr == 0) {
6960Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
6970Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	/*
7013446Smrj 	 * First we trim from the front of the range. Since kbm_probe()
7020Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
7030Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
7040Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
7050Sstevel@tonic-gate 	 * but w < v.
7060Sstevel@tonic-gate 	 */
7070Sstevel@tonic-gate 	do {
7080Sstevel@tonic-gate 		change = 0;
7090Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
7103446Smrj 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7110Sstevel@tonic-gate 		    va = next_va) {
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 			next_va = va + len;
7143446Smrj 			pfn_addr = pfn_to_pa(pfn);
7150Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
7180Sstevel@tonic-gate 				change = 1;
7190Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
7200Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
7210Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
7220Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
7230Sstevel@tonic-gate 				}
7240Sstevel@tonic-gate 			}
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 		if (change && prom_debug)
7270Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
7280Sstevel@tonic-gate 			    *addr, *size);
7290Sstevel@tonic-gate 	} while (change);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/*
7320Sstevel@tonic-gate 	 * Trim pages from the end of the range.
7330Sstevel@tonic-gate 	 */
7340Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
7353446Smrj 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7360Sstevel@tonic-gate 	    va = next_va) {
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		next_va = va + len;
7393446Smrj 		pfn_addr = pfn_to_pa(pfn);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
7420Sstevel@tonic-gate 			*size = pfn_addr - *addr;
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	if (prom_debug)
7460Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
7470Sstevel@tonic-gate 		    *addr, *size);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate static void
7510Sstevel@tonic-gate kpm_init()
7520Sstevel@tonic-gate {
7530Sstevel@tonic-gate 	struct segkpm_crargs b;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/*
7560Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
7570Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
7580Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
7590Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
7600Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
7610Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
7620Sstevel@tonic-gate 	 * pagesize is used.
7630Sstevel@tonic-gate 	 */
7640Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
7650Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
7660Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
7670Sstevel@tonic-gate 	kpmp2pshft = 0;
7680Sstevel@tonic-gate 	kpmpnpgs = 1;
7690Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
7720Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
7750Sstevel@tonic-gate 		panic("cannot attach segkpm");
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
7780Sstevel@tonic-gate 	b.nvcolors = 1;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
7810Sstevel@tonic-gate 		panic("segkpm_create segkpm");
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate /*
7873446Smrj  * The debug info page provides enough information to allow external
7883446Smrj  * inspectors (e.g. when running under a hypervisor) to bootstrap
7893446Smrj  * themselves into allowing full-blown kernel debugging.
7903446Smrj  */
7913446Smrj static void
7923446Smrj init_debug_info(void)
7933446Smrj {
7943446Smrj 	caddr_t mem;
7953446Smrj 	debug_info_t *di;
7963446Smrj 
7973446Smrj #ifndef __lint
7983446Smrj 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
7993446Smrj #endif
8003446Smrj 
8013446Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
8023446Smrj 	    MMU_PAGESIZE);
8033446Smrj 
8043446Smrj 	if (mem != (caddr_t)DEBUG_INFO_VA)
8053446Smrj 		panic("BOP_ALLOC() failed");
8063446Smrj 	bzero(mem, MMU_PAGESIZE);
8073446Smrj 
8083446Smrj 	di = (debug_info_t *)mem;
8093446Smrj 
8103446Smrj 	di->di_magic = DEBUG_INFO_MAGIC;
8113446Smrj 	di->di_version = DEBUG_INFO_VERSION;
8123446Smrj }
8133446Smrj 
8143446Smrj /*
8153446Smrj  * Build the memlists and other kernel essential memory system data structures.
8163446Smrj  * This is everything at valloc_base.
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate static void
8190Sstevel@tonic-gate startup_memlist(void)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate 	size_t memlist_sz;
8220Sstevel@tonic-gate 	size_t memseg_sz;
8230Sstevel@tonic-gate 	size_t pagehash_sz;
8240Sstevel@tonic-gate 	size_t pp_sz;
8250Sstevel@tonic-gate 	uintptr_t va;
8260Sstevel@tonic-gate 	size_t len;
8270Sstevel@tonic-gate 	uint_t prot;
8280Sstevel@tonic-gate 	pfn_t pfn;
8290Sstevel@tonic-gate 	int memblocks;
8300Sstevel@tonic-gate 	caddr_t pagecolor_mem;
8310Sstevel@tonic-gate 	size_t pagecolor_memsz;
8320Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
8330Sstevel@tonic-gate 	size_t page_ctrs_size;
8340Sstevel@tonic-gate 	struct memlist *current;
8350Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
8380Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
8390Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
8450Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
8460Sstevel@tonic-gate 	 */
8473446Smrj 	len = kbm_nucleus_size;
8483446Smrj 	ASSERT(len > MMU_PAGESIZE);
8490Sstevel@tonic-gate 
8503446Smrj 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
8513446Smrj 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
8523446Smrj 	if (e_moddata - moddata > MODDATA)
8533446Smrj 		e_moddata = moddata + MODDATA;
8540Sstevel@tonic-gate 
8553446Smrj 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
8563446Smrj 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
8573446Smrj 	if (e_modtext - modtext > MODTEXT)
8583446Smrj 		e_modtext = modtext + MODTEXT;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	econtig = e_moddata;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	PRM_DEBUG(modtext);
8630Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
8640Sstevel@tonic-gate 	PRM_DEBUG(moddata);
8650Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
8660Sstevel@tonic-gate 	PRM_DEBUG(econtig);
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	/*
8693446Smrj 	 * Examine the boot loader physical memory map to find out:
8700Sstevel@tonic-gate 	 * - total memory in system - physinstalled
8710Sstevel@tonic-gate 	 * - the max physical address - physmax
8723446Smrj 	 * - the number of discontiguous segments of memory.
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 	if (prom_debug)
8753446Smrj 		print_memlist("boot physinstalled",
8760Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
8770Sstevel@tonic-gate 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
8780Sstevel@tonic-gate 	    &physinstalled, &memblocks);
8790Sstevel@tonic-gate 	PRM_DEBUG(physmax);
8800Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
8810Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/*
8840Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
8850Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
8860Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
8870Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
8880Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
8890Sstevel@tonic-gate 	 */
8900Sstevel@tonic-gate 	mmu_init();
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate #ifdef	__i386
8930Sstevel@tonic-gate 	/*
8940Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
8950Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
8960Sstevel@tonic-gate 	 */
8970Sstevel@tonic-gate 	if (mmu.pae_hat) {
8980Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
8990Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
9000Sstevel@tonic-gate 			physmax = PFN_64G - 1;
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	} else {
9030Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
9040Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
9050Sstevel@tonic-gate 			physmax = PFN_4G - 1;
9060Sstevel@tonic-gate 		}
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate #endif
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
9130Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
9140Sstevel@tonic-gate 		char value[8];
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 		if (len < 8)
9170Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
9180Sstevel@tonic-gate 		else
9190Sstevel@tonic-gate 			(void) strcpy(value, "");
9200Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
9210Sstevel@tonic-gate 			mmu.pt_nx = 0;
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	/*
9260Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
9270Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
9280Sstevel@tonic-gate 	 *
9290Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
9300Sstevel@tonic-gate 	 */
9310Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
9320Sstevel@tonic-gate 	obp_pages = 0;
9330Sstevel@tonic-gate 	va = KERNEL_TEXT;
9343446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
9350Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
9360Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
9370Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
9380Sstevel@tonic-gate 		va += len;
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 	PRM_DEBUG(npages);
9410Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	/*
9440Sstevel@tonic-gate 	 * If physmem is patched to be non-zero, use it instead of
9450Sstevel@tonic-gate 	 * the computed value unless it is larger than the real
9460Sstevel@tonic-gate 	 * amount of memory on hand.
9470Sstevel@tonic-gate 	 */
9481417Skchow 	if (physmem == 0 || physmem > npages) {
9490Sstevel@tonic-gate 		physmem = npages;
9501417Skchow 	} else if (physmem < npages) {
9511479Skchow 		orig_npages = npages;
9520Sstevel@tonic-gate 		npages = physmem;
9531417Skchow 	}
9540Sstevel@tonic-gate 	PRM_DEBUG(physmem);
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	/*
9570Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
9580Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
9590Sstevel@tonic-gate 	 * include:
9600Sstevel@tonic-gate 	 *	memsegs
9610Sstevel@tonic-gate 	 *	memlists
9620Sstevel@tonic-gate 	 *	page hash table
9630Sstevel@tonic-gate 	 *	page_t's
9640Sstevel@tonic-gate 	 *	page coloring data structs
9650Sstevel@tonic-gate 	 */
9660Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
9670Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
9680Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/*
9713446Smrj 	 * Reserve space for memlists. There's no real good way to know exactly
9723446Smrj 	 * how much room we'll need, but this should be a good upper bound.
9730Sstevel@tonic-gate 	 */
9740Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
9750Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
9760Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
9770Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	/*
9800Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
9810Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
9820Sstevel@tonic-gate 	 */
9830Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
9840Sstevel@tonic-gate 	page_hashsz = 1 << highbit(page_hashsz);
9850Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
9860Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
9870Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	/*
9903446Smrj 	 * Set aside room for the page structures themselves.
9910Sstevel@tonic-gate 	 */
9923446Smrj 	PRM_DEBUG(npages);
9933446Smrj 	pp_sz = sizeof (struct page) * npages;
9940Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
9950Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/*
9980Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
9990Sstevel@tonic-gate 	 */
10000Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
10010Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
10020Sstevel@tonic-gate 	pagecolor_memsz =
10030Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
10040Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
10050Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
10080Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
10090Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
10100Sstevel@tonic-gate 
10113446Smrj #if defined(__amd64)
10123446Smrj 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
10133446Smrj 	valloc_base = VALLOC_BASE;
10143446Smrj #else	/* __i386 */
10153446Smrj 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
10163446Smrj 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
10173446Smrj #endif	/* __i386 */
10183446Smrj 	PRM_DEBUG(valloc_base);
10193446Smrj 
10200Sstevel@tonic-gate 	/*
10213446Smrj 	 * do all the initial allocations
10223446Smrj 	 */
10233446Smrj 	perform_allocations();
10243446Smrj 
10253446Smrj 	/*
10263446Smrj 	 * Build phys_install and phys_avail in kernel memspace.
10273446Smrj 	 * - phys_install should be all memory in the system.
10283446Smrj 	 * - phys_avail is phys_install minus any memory mapped before this
10293446Smrj 	 *    point above KERNEL_TEXT.
10303446Smrj 	 */
10313446Smrj 	current = phys_install = memlist;
10323446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
10333446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
10343446Smrj 		panic("physinstalled was too big!");
10353446Smrj 	if (prom_debug)
10363446Smrj 		print_memlist("phys_install", phys_install);
10373446Smrj 
10383446Smrj 	phys_avail = current;
10393446Smrj 	PRM_POINT("Building phys_avail:\n");
10403446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
10413446Smrj 	    avail_filter);
10423446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
10433446Smrj 		panic("physavail was too big!");
10443446Smrj 	if (prom_debug)
10453446Smrj 		print_memlist("phys_avail", phys_avail);
10463446Smrj 
10473446Smrj 	/*
10483446Smrj 	 * setup page coloring
10490Sstevel@tonic-gate 	 */
10503446Smrj 	page_coloring_setup(pagecolor_mem);
10513446Smrj 	page_lock_init();	/* currently a no-op */
10523446Smrj 
10533446Smrj 	/*
10543446Smrj 	 * free page list counters
10553446Smrj 	 */
10563446Smrj 	(void) page_ctrs_alloc(page_ctrs_mem);
10573446Smrj 
10583446Smrj 	/*
10593446Smrj 	 * Initialize the page structures from the memory lists.
10603446Smrj 	 */
10613446Smrj 	availrmem_initial = availrmem = freemem = 0;
10623446Smrj 	PRM_POINT("Calling kphysm_init()...");
10633446Smrj 	npages = kphysm_init(pp_base, npages);
10643446Smrj 	PRM_POINT("kphysm_init() done");
10653446Smrj 	PRM_DEBUG(npages);
10663446Smrj 
10673446Smrj 	init_debug_info();
10683446Smrj 
10693446Smrj 	/*
10703446Smrj 	 * Now that page_t's have been initialized, remove all the
10713446Smrj 	 * initial allocation pages from the kernel free page lists.
10723446Smrj 	 */
10733446Smrj 	boot_mapin((caddr_t)valloc_base, valloc_sz);
10743446Smrj 	boot_mapin((caddr_t)GDT_VA, MMU_PAGESIZE);
10753446Smrj 	boot_mapin((caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE);
10763446Smrj 	PRM_POINT("startup_memlist() done");
10773446Smrj 
10783446Smrj 	PRM_DEBUG(valloc_sz);
10793446Smrj }
10803446Smrj 
10813446Smrj /*
10823446Smrj  * Layout the kernel's part of address space and initialize kmem allocator.
10833446Smrj  */
10843446Smrj static void
10853446Smrj startup_kmem(void)
10863446Smrj {
1087*3717Sdp78419 	extern void page_set_colorequiv_arr(void);
1088*3717Sdp78419 
10893446Smrj 	PRM_POINT("startup_kmem() starting...");
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate #if defined(__amd64)
10920Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
10930Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
10940Sstevel@tonic-gate 		    "systems.");
10950Sstevel@tonic-gate 	kernelbase = (uintptr_t)KERNELBASE;
10960Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
10973446Smrj 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
10980Sstevel@tonic-gate #else	/* __i386 */
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * We configure kernelbase based on:
11010Sstevel@tonic-gate 	 *
11020Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
11030Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
11040Sstevel@tonic-gate 	 *
11050Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
11060Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
11070Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
11080Sstevel@tonic-gate 	 *
11090Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
11100Sstevel@tonic-gate 	 */
11110Sstevel@tonic-gate 	if (eprom_kernelbase) {
11120Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
11130Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
11140Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
11150Sstevel@tonic-gate 	} else {
11160Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
11170Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
11180Sstevel@tonic-gate 	}
11190Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
11203446Smrj 	core_base = valloc_base;
11210Sstevel@tonic-gate 	core_size = 0;
11223446Smrj #endif	/* __i386 */
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	PRM_DEBUG(core_base);
11250Sstevel@tonic-gate 	PRM_DEBUG(core_size);
11263446Smrj 	PRM_DEBUG(kernelbase);
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * At this point, we can only use a portion of the kernelheap that
11303446Smrj 	 * will be available after we boot.  32-bit systems have this
11313446Smrj 	 * limitation.
11320Sstevel@tonic-gate 	 *
11330Sstevel@tonic-gate 	 * On 32-bit systems we have to leave room to place segmap below
11340Sstevel@tonic-gate 	 * the heap.  We don't yet know how large segmap will be, so we
11350Sstevel@tonic-gate 	 * have to be very conservative.
11363446Smrj 	 *
11373446Smrj 	 * On 64 bit systems there should be LOTS of room so just use
11383446Smrj 	 * the next 4Gig below core_base.
11390Sstevel@tonic-gate 	 */
11400Sstevel@tonic-gate #if defined(__amd64)
11413446Smrj 
11423446Smrj 	boot_kernelheap = (caddr_t)core_base  - FOURGB;
11430Sstevel@tonic-gate 	segmap_reserved = 0;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate #else	/* __i386 */
11463446Smrj 
11470Sstevel@tonic-gate 	segkp_fromheap = 1;
11480Sstevel@tonic-gate 	segmap_reserved = ROUND_UP_LPAGE(MAX(segmapsize, SEGMAPMAX));
11493446Smrj 	boot_kernelheap =
11503446Smrj 	    (caddr_t)ROUND_UP_LPAGE(kernelbase) + segmap_reserved;
11513446Smrj 
11523446Smrj #endif	/* __i386 */
11530Sstevel@tonic-gate 	PRM_DEBUG(boot_kernelheap);
11543446Smrj 	ekernelheap = (char *)core_base;
11553446Smrj 	PRM_DEBUG(ekernelheap);
11560Sstevel@tonic-gate 	kernelheap = boot_kernelheap;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/*
11590Sstevel@tonic-gate 	 * If segmap is too large we can push the bottom of the kernel heap
11600Sstevel@tonic-gate 	 * higher than the base.  Or worse, it could exceed the top of the
11610Sstevel@tonic-gate 	 * VA space entirely, causing it to wrap around.
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
11640Sstevel@tonic-gate 		panic("too little memory available for kernelheap,"
11650Sstevel@tonic-gate 			    " use a different kernelbase");
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	/*
11680Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
11690Sstevel@tonic-gate 	 * update variables that were initialized with a value of
11700Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
11710Sstevel@tonic-gate 	 *
11720Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
11730Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
11740Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
11750Sstevel@tonic-gate 	 *	just be declared as variables there?
11760Sstevel@tonic-gate 	 */
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate #if defined(__amd64)
11790Sstevel@tonic-gate 	ASSERT(_kernelbase == KERNELBASE);
11800Sstevel@tonic-gate 	ASSERT(_userlimit == USERLIMIT);
11810Sstevel@tonic-gate #else
11820Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
11830Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
11840Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
11850Sstevel@tonic-gate #endif
11860Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
11870Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
11880Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	/*
11910Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
11920Sstevel@tonic-gate 	 */
11933446Smrj 	kernelheap_init(boot_kernelheap, ekernelheap,
11943446Smrj 	    boot_kernelheap + MMU_PAGESIZE,
11953446Smrj 	    (void *)core_base, (void *)(core_base + core_size));
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	/*
11980Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
11990Sstevel@tonic-gate 	 */
12000Sstevel@tonic-gate 	kmem_init();
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	/*
1203*3717Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
1204*3717Sdp78419 	 */
1205*3717Sdp78419 	page_set_colorequiv_arr();
1206*3717Sdp78419 
1207*3717Sdp78419 	/*
12080Sstevel@tonic-gate 	 * print this out early so that we know what's going on
12090Sstevel@tonic-gate 	 */
12100Sstevel@tonic-gate 	cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE);
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	/*
12130Sstevel@tonic-gate 	 * Initialize bp_mapin().
12140Sstevel@tonic-gate 	 */
12150Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
12160Sstevel@tonic-gate 
12171479Skchow 	/*
12181479Skchow 	 * orig_npages is non-zero if physmem has been configured for less
12191479Skchow 	 * than the available memory.
12201479Skchow 	 */
12211479Skchow 	if (orig_npages) {
12221479Skchow #ifdef __i386
12231479Skchow 		/*
12241479Skchow 		 * use npages for physmem in case it has been temporarily
12251479Skchow 		 * modified via /etc/system in kmem_init/mod_read_system_file.
12261479Skchow 		 */
12271479Skchow 		if (npages == PHYSMEM32) {
12283446Smrj 			cmn_err(CE_WARN, "!Due to 32-bit virtual"
12291479Skchow 			    " address space limitations, limiting"
12301479Skchow 			    " physmem to 0x%lx of 0x%lx available pages",
12311479Skchow 			    npages, orig_npages);
12321479Skchow 		} else {
12331479Skchow 			cmn_err(CE_WARN, "!limiting physmem to 0x%lx of"
12341479Skchow 			    " 0x%lx available pages", npages, orig_npages);
12351479Skchow 		}
12361479Skchow #else
12371479Skchow 		cmn_err(CE_WARN, "!limiting physmem to 0x%lx of"
12381479Skchow 		    " 0x%lx available pages", npages, orig_npages);
12391479Skchow #endif
12401479Skchow 	}
12410Sstevel@tonic-gate #if defined(__i386)
12420Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
12430Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
12440Sstevel@tonic-gate 		    "System using 0x%lx",
12450Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
12460Sstevel@tonic-gate #endif
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
12490Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
12500Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
12510Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
12520Sstevel@tonic-gate 	}
12530Sstevel@tonic-gate #endif
12540Sstevel@tonic-gate 
12553446Smrj 	PRM_POINT("startup_kmem() done");
12560Sstevel@tonic-gate }
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate static void
12590Sstevel@tonic-gate startup_modules(void)
12600Sstevel@tonic-gate {
12610Sstevel@tonic-gate 	unsigned int i;
12620Sstevel@tonic-gate 	extern void prom_setup(void);
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
12650Sstevel@tonic-gate 	/*
12660Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
12670Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
12680Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
12690Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
12700Sstevel@tonic-gate 	 */
12710Sstevel@tonic-gate 	microfind();
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	/*
12740Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
12750Sstevel@tonic-gate 	 */
12763446Smrj 	sgmtl(process_rtc_config_file());
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	/*
12790Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
12800Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
12810Sstevel@tonic-gate 	 */
12820Sstevel@tonic-gate 	param_calc(0);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	mod_setup();
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	/*
12870Sstevel@tonic-gate 	 * Initialize system parameters.
12880Sstevel@tonic-gate 	 */
12890Sstevel@tonic-gate 	param_init();
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	/*
12922712Snn35248 	 * Initialize the default brands
12932712Snn35248 	 */
12942712Snn35248 	brand_init();
12952712Snn35248 
12962712Snn35248 	/*
12970Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
12980Sstevel@tonic-gate 	 */
12990Sstevel@tonic-gate 	maxmem = physmem;
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	/*
13020Sstevel@tonic-gate 	 * Initialize the hat layer.
13030Sstevel@tonic-gate 	 */
13040Sstevel@tonic-gate 	hat_init();
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	/*
13070Sstevel@tonic-gate 	 * Initialize segment management stuff.
13080Sstevel@tonic-gate 	 */
13090Sstevel@tonic-gate 	seg_init();
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
13120Sstevel@tonic-gate 		halt("Can't load specfs");
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
13150Sstevel@tonic-gate 		halt("Can't load devfs");
13160Sstevel@tonic-gate 
13172621Sllai1 	if (modload("fs", "dev") == -1)
13182621Sllai1 		halt("Can't load dev");
13192621Sllai1 
13201676Sjpk 	(void) modloadonly("sys", "lbl_edition");
13211676Sjpk 
13220Sstevel@tonic-gate 	dispinit();
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/*
13250Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
13260Sstevel@tonic-gate 	 */
13270Sstevel@tonic-gate 	if ((i = modload("misc", "sysinit")) != (unsigned int)-1)
13280Sstevel@tonic-gate 		(void) modunload(i);
13290Sstevel@tonic-gate 	else
13300Sstevel@tonic-gate 		cmn_err(CE_CONT, "sysinit load failed");
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	/* Read cluster configuration data. */
13330Sstevel@tonic-gate 	clconf_init();
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
13370Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	setup_ddi();
13401414Scindi 
13411414Scindi 	/*
13421414Scindi 	 * Set up the CPU module subsystem.  Modifies the device tree, so it
13431414Scindi 	 * must be done after setup_ddi().
13441414Scindi 	 */
13451414Scindi 	cmi_init();
13461414Scindi 
13471414Scindi 	/*
13481414Scindi 	 * Initialize the MCA handlers
13491414Scindi 	 */
13501414Scindi 	if (x86_feature & X86_MCA)
13511414Scindi 		cmi_mca_init();
13521414Scindi 
13530Sstevel@tonic-gate 	/*
13540Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
13550Sstevel@tonic-gate 	 */
13563446Smrj 	PRM_POINT("startup_modules: calling prom_setup...");
13570Sstevel@tonic-gate 	prom_setup();
13583446Smrj 	PRM_POINT("startup_modules: done");
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	/*
13610Sstevel@tonic-gate 	 * Load all platform specific modules
13620Sstevel@tonic-gate 	 */
13633446Smrj 	PRM_POINT("startup_modules: calling psm_modload...");
13640Sstevel@tonic-gate 	psm_modload();
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate 
13693446Smrj /*
13703446Smrj  * claim a "setaside" boot page for use in the kernel
13713446Smrj  */
13723446Smrj page_t *
13733446Smrj boot_claim_page(pfn_t pfn)
13740Sstevel@tonic-gate {
13753446Smrj 	page_t *pp;
13763446Smrj 
13773446Smrj 	pp = page_numtopp_nolock(pfn);
13783446Smrj 	ASSERT(pp != NULL);
13790Sstevel@tonic-gate 
13803446Smrj 	if (PP_ISBOOTPAGES(pp)) {
13813446Smrj 		if (pp->p_next != NULL)
13823446Smrj 			pp->p_next->p_prev = pp->p_prev;
13833446Smrj 		if (pp->p_prev == NULL)
13843446Smrj 			bootpages = pp->p_next;
13853446Smrj 		else
13863446Smrj 			pp->p_prev->p_next = pp->p_next;
13873446Smrj 	} else {
13883446Smrj 		/*
13893446Smrj 		 * htable_attach() expects a base pagesize page
13903446Smrj 		 */
13913446Smrj 		if (pp->p_szc != 0)
13923446Smrj 			page_boot_demote(pp);
13933446Smrj 		pp = page_numtopp(pfn, SE_EXCL);
13943446Smrj 	}
13953446Smrj 	return (pp);
13960Sstevel@tonic-gate }
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate /*
13990Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
14000Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
14010Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
14020Sstevel@tonic-gate  */
14030Sstevel@tonic-gate static void
14040Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate 	uintptr_t va = low;
14070Sstevel@tonic-gate 	size_t len;
14080Sstevel@tonic-gate 	uint_t prot;
14090Sstevel@tonic-gate 	pfn_t pfn;
14100Sstevel@tonic-gate 	page_t *pp;
14110Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
14120Sstevel@tonic-gate 
14133446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
14140Sstevel@tonic-gate 		if (va + len >= high)
14150Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
14160Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 		while (len > 0) {
14190Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
14200Sstevel@tonic-gate 			if (pp != NULL) {
14210Sstevel@tonic-gate 				if (setaside == 0)
14220Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
14230Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
14240Sstevel@tonic-gate 					    (void *)va, pfn);
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 				pp->p_next = bootpages;
14273446Smrj 				pp->p_prev = NULL;
14283446Smrj 				PP_SETBOOTPAGES(pp);
14293446Smrj 				if (bootpages != NULL) {
14303446Smrj 					bootpages->p_prev = pp;
14313446Smrj 				}
14320Sstevel@tonic-gate 				bootpages = pp;
14330Sstevel@tonic-gate 				++boot_protect_cnt;
14340Sstevel@tonic-gate 			}
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 			++pfn;
14370Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
14380Sstevel@tonic-gate 			va += MMU_PAGESIZE;
14390Sstevel@tonic-gate 		}
14400Sstevel@tonic-gate 	}
14410Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate 
14443446Smrj /*
14453446Smrj  * Finish initializing the VM system, now that we are no longer
14463446Smrj  * relying on the boot time memory allocators.
14473446Smrj  */
14480Sstevel@tonic-gate static void
14490Sstevel@tonic-gate startup_vm(void)
14500Sstevel@tonic-gate {
14510Sstevel@tonic-gate 	struct segmap_crargs a;
14520Sstevel@tonic-gate 
14532991Ssusans 	extern int use_brk_lpg, use_stk_lpg;
1454423Sdavemq 
14550Sstevel@tonic-gate 	PRM_POINT("startup_vm() starting...");
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 	/*
14583446Smrj 	 * Establish the final size of the kernel's heap, size of segmap,
14593446Smrj 	 * segkp, etc.
14600Sstevel@tonic-gate 	 */
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate #if defined(__amd64)
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	/*
14653446Smrj 	 * Check if there is enough virtual address space in KPM region to
14663446Smrj 	 * map physmax.
14670Sstevel@tonic-gate 	 */
14683446Smrj 	kpm_vbase = (caddr_t)(uintptr_t)SEGKPM_BASE;
14693446Smrj 	kpm_size = 0;
14703446Smrj 	if (kpm_desired) {
14713446Smrj 		kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
14723446Smrj 		if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)VALLOC_BASE) {
14733446Smrj 			kpm_size = 0;
14743446Smrj 			kpm_desired = 0;
14753446Smrj 		}
14763446Smrj 	}
14770Sstevel@tonic-gate 
14783446Smrj 	PRM_DEBUG(kpm_size);
14793446Smrj 	PRM_DEBUG(kpm_vbase);
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	/*
14823446Smrj 	 * By default we create a seg_kp in 64 bit kernels, it's a little
14833446Smrj 	 * faster to access than embedding it in the heap.
14840Sstevel@tonic-gate 	 */
14853446Smrj 	segkp_base = (caddr_t)valloc_base + valloc_sz;
14860Sstevel@tonic-gate 	if (!segkp_fromheap) {
14870Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 		/*
14903446Smrj 		 * determine size of segkp
14910Sstevel@tonic-gate 		 */
14920Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
14930Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
14940Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
14950Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
14960Sstevel@tonic-gate 			    mmu_btop(sz));
14970Sstevel@tonic-gate 		}
14980Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
15010Sstevel@tonic-gate 	}
15023446Smrj 	PRM_DEBUG(segkp_base);
15033446Smrj 	PRM_DEBUG(segkpsize);
15040Sstevel@tonic-gate 
15053446Smrj 	segzio_base = segkp_base + mmu_ptob(segkpsize);
15063446Smrj 	if (segzio_fromheap) {
15073446Smrj 		segziosize = 0;
15083446Smrj 	} else {
15093290Sjohansen 		size_t size;
15103552Sjohansen 		size_t physmem_b = mmu_ptob(physmem);
15113290Sjohansen 
15123290Sjohansen 		/* size is in bytes, segziosize is in pages */
15133290Sjohansen 		if (segziosize == 0) {
15143552Sjohansen 			size = physmem_b;
15153290Sjohansen 		} else {
15163290Sjohansen 			size = mmu_ptob(segziosize);
15173290Sjohansen 		}
15183290Sjohansen 
15193290Sjohansen 		if (size < SEGZIOMINSIZE) {
15203290Sjohansen 			size = SEGZIOMINSIZE;
15213552Sjohansen 		} else if (size > SEGZIOMAXSIZE) {
15223552Sjohansen 			size = SEGZIOMAXSIZE;
15233552Sjohansen 			/*
15243552Sjohansen 			 * SEGZIOMAXSIZE is capped at 512gb so that segzio
15253552Sjohansen 			 * doesn't consume all of KVA.  However, if we have a
15263552Sjohansen 			 * system that has more thant 512gb of physical memory,
15273552Sjohansen 			 * we can actually consume about half of the difference
15283552Sjohansen 			 * between 512gb and the rest of the available physical
15293552Sjohansen 			 * memory.
15303552Sjohansen 			 */
15313552Sjohansen 			if (physmem_b > SEGZIOMAXSIZE) {
15323552Sjohansen 				size += (physmem_b - SEGZIOMAXSIZE) / 2;
15333552Sjohansen 			}
15343290Sjohansen 		}
15353290Sjohansen 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
15363290Sjohansen 	}
15373446Smrj 	PRM_DEBUG(segziosize);
15383446Smrj 	PRM_DEBUG(segzio_base);
15393290Sjohansen 
15400Sstevel@tonic-gate 	/*
15413446Smrj 	 * Put the range of VA for device mappings next, kmdb knows to not
15423446Smrj 	 * grep in this range of addresses.
15430Sstevel@tonic-gate 	 */
15443446Smrj 	toxic_addr =
15453446Smrj 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
15460Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
15473446Smrj 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
15483446Smrj #else /* __i386 */
15493446Smrj 	segmap_start = ROUND_UP_LPAGE(kernelbase);
15503446Smrj #endif /* __i386 */
15513446Smrj 	PRM_DEBUG(segmap_start);
15523446Smrj 	ASSERT((caddr_t)segmap_start < boot_kernelheap);
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	/*
15550Sstevel@tonic-gate 	 * Users can change segmapsize through eeprom or /etc/system.
15560Sstevel@tonic-gate 	 * If the variable is tuned through eeprom, there is no upper
15570Sstevel@tonic-gate 	 * bound on the size of segmap.  If it is tuned through
15580Sstevel@tonic-gate 	 * /etc/system on 32-bit systems, it must be no larger than we
15590Sstevel@tonic-gate 	 * planned for in startup_memlist().
15600Sstevel@tonic-gate 	 */
15610Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate #if defined(__i386)
15640Sstevel@tonic-gate 	if (segmapsize > segmap_reserved) {
15650Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!segmapsize may not be set > 0x%lx in "
15660Sstevel@tonic-gate 		    "/etc/system.  Use eeprom.", (long)SEGMAPMAX);
15670Sstevel@tonic-gate 		segmapsize = segmap_reserved;
15680Sstevel@tonic-gate 	}
15690Sstevel@tonic-gate 	/*
15700Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
15710Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
15723446Smrj 	 * small red zone just below the start of segmap.
15730Sstevel@tonic-gate 	 */
15743446Smrj 	segmap_start += KERNEL_REDZONE_SIZE;
15750Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
15760Sstevel@tonic-gate #endif
15773446Smrj 
15783446Smrj 	PRM_DEBUG(segmap_start);
15793446Smrj 	PRM_DEBUG(segmapsize);
15803446Smrj 	final_kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
15813446Smrj 	PRM_DEBUG(final_kernelheap);
15823446Smrj 
15833446Smrj 	/*
15843446Smrj 	 * Do final allocations of HAT data structures that need to
15853446Smrj 	 * be allocated before quiescing the boot loader.
15863446Smrj 	 */
15873446Smrj 	PRM_POINT("Calling hat_kern_alloc()...");
15883446Smrj 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
15893446Smrj 	PRM_POINT("hat_kern_alloc() done");
15903446Smrj 
15913446Smrj 	/*
15923446Smrj 	 * Setup MTRR (Memory type range registers)
15933446Smrj 	 */
15943446Smrj 	setup_mtrr();
15953446Smrj 
15963446Smrj 	/*
15973446Smrj 	 * The next two loops are done in distinct steps in order
15983446Smrj 	 * to be sure that any page that is doubly mapped (both above
15993446Smrj 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
16003446Smrj 	 * Note this may never happen, but it might someday.
16013446Smrj 	 */
16023446Smrj 	bootpages = NULL;
16033446Smrj 	PRM_POINT("Protecting boot pages");
16040Sstevel@tonic-gate 
16053446Smrj 	/*
16063446Smrj 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
16073446Smrj 	 * page_t's. This can only happen if something weird allocated
16083446Smrj 	 * in this range (like kadb/kmdb).
16093446Smrj 	 */
16103446Smrj 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
16113446Smrj 
16123446Smrj 	/*
16133446Smrj 	 * Before we can take over memory allocation/mapping from the boot
16143446Smrj 	 * loader we must remove from our free page lists any boot allocated
16153446Smrj 	 * pages that stay mapped until release_bootstrap().
16163446Smrj 	 */
16173446Smrj 	protect_boot_range(0, kernelbase, 1);
16183446Smrj 
16193446Smrj 	/*
16203446Smrj 	 * Switch to running on regular HAT (not boot_mmu)
16213446Smrj 	 */
16223446Smrj 	PRM_POINT("Calling hat_kern_setup()...");
16233446Smrj 	hat_kern_setup();
16243446Smrj 
16253446Smrj 	/*
16263446Smrj 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
16273446Smrj 	 */
16283446Smrj 	bop_no_more_mem();
16293446Smrj 
16303446Smrj 	PRM_POINT("hat_kern_setup() done");
16313446Smrj 
16323446Smrj 	hat_cpu_online(CPU);
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	/*
16350Sstevel@tonic-gate 	 * Initialize VM system
16360Sstevel@tonic-gate 	 */
16370Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
16380Sstevel@tonic-gate 	kvm_init();
16390Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	/*
16420Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
16430Sstevel@tonic-gate 	 */
16440Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
16450Sstevel@tonic-gate 		kdi_dvec_vmready();
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	/*
16480Sstevel@tonic-gate 	 * Mangle the brand string etc.
16490Sstevel@tonic-gate 	 */
16500Sstevel@tonic-gate 	cpuid_pass3(CPU);
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	/*
16530Sstevel@tonic-gate 	 * Now that we can use memory outside the top 4GB (on 64-bit
16540Sstevel@tonic-gate 	 * systems) and we know the size of segmap, we can set the final
16553446Smrj 	 * size of the kernel's heap.
16560Sstevel@tonic-gate 	 */
16570Sstevel@tonic-gate 	if (final_kernelheap < boot_kernelheap) {
16583446Smrj 		PRM_POINT("kernelheap_extend()");
16593446Smrj 		PRM_DEBUG(boot_kernelheap);
16603446Smrj 		PRM_DEBUG(final_kernelheap);
16610Sstevel@tonic-gate 		kernelheap_extend(final_kernelheap, boot_kernelheap);
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate #if defined(__amd64)
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	/*
16670Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
16680Sstevel@tonic-gate 	 */
16690Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
16700Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate #else	/* __i386 */
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	/*
16750Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
16760Sstevel@tonic-gate 	 */
16773446Smrj 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
16780Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
16790Sstevel@tonic-gate 	toxic_bit_map =
16800Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
16810Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
16820Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate #endif	/* __i386 */
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	/*
16880Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
16890Sstevel@tonic-gate 	 * it, tell the debugger.
16900Sstevel@tonic-gate 	 */
16910Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
16920Sstevel@tonic-gate 		kdi_dvec_memavail();
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	/*
16950Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
16960Sstevel@tonic-gate 	 * to work around a pentium bug.
16970Sstevel@tonic-gate 	 */
16980Sstevel@tonic-gate #if !defined(__amd64)
16990Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
17003446Smrj 		desctbr_t idtr;
17010Sstevel@tonic-gate 		gate_desc_t *newidt;
17023446Smrj 		struct machcpu *mcpu = &CPU->cpu_m;
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
17050Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 		bcopy(idt0, newidt, sizeof (idt0));
17080Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
17093446Smrj 		    KCS_SEL, SDT_SYSIGT, SEL_KPL);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
17120Sstevel@tonic-gate 		    PROT_READ|PROT_EXEC);
17130Sstevel@tonic-gate 
17143446Smrj 		mcpu->mcpu_idt = newidt;
17153446Smrj 		idtr.dtr_base = (uintptr_t)mcpu->mcpu_idt;
17163446Smrj 		idtr.dtr_limit = sizeof (idt0) - 1;
17173446Smrj 		wr_idtr(&idtr);
17180Sstevel@tonic-gate 	}
17190Sstevel@tonic-gate #endif	/* !__amd64 */
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	/*
17220Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
17230Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
17240Sstevel@tonic-gate 	 */
17250Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
17260Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
17270Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
17300Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
17310Sstevel@tonic-gate 
1732841Skchow 	/*
1733841Skchow 	 * disable automatic large pages for small memory systems or
1734841Skchow 	 * when the disable flag is set.
1735841Skchow 	 */
17362991Ssusans 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
17372991Ssusans 		max_uheap_lpsize = LEVEL_SIZE(1);
17382991Ssusans 		max_ustack_lpsize = LEVEL_SIZE(1);
17392991Ssusans 		max_privmap_lpsize = LEVEL_SIZE(1);
17402991Ssusans 		max_uidata_lpsize = LEVEL_SIZE(1);
17412991Ssusans 		max_utext_lpsize = LEVEL_SIZE(1);
17422991Ssusans 		max_shm_lpsize = LEVEL_SIZE(1);
17432991Ssusans 	}
17442991Ssusans 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
17452991Ssusans 	    auto_lpg_disable) {
1746423Sdavemq 		use_brk_lpg = 0;
1747423Sdavemq 		use_stk_lpg = 0;
17482991Ssusans 	}
17492991Ssusans 	if (mmu.max_page_level > 0) {
17502991Ssusans 		mcntl0_lpsize = LEVEL_SIZE(1);
1751423Sdavemq 	}
1752423Sdavemq 
17530Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
17540Sstevel@tonic-gate 	hat_init_finish();
17550Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	/*
17580Sstevel@tonic-gate 	 * Initialize the segkp segment type.
17590Sstevel@tonic-gate 	 */
17600Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
17613446Smrj 	PRM_POINT("Attaching segkp");
17623446Smrj 	if (segkp_fromheap) {
17630Sstevel@tonic-gate 		segkp->s_as = &kas;
17643446Smrj 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
17653446Smrj 	    segkp) < 0) {
17663446Smrj 		panic("startup: cannot attach segkp");
17673446Smrj 		/*NOTREACHED*/
17680Sstevel@tonic-gate 	}
17693446Smrj 	PRM_POINT("Doing segkp_create()");
17700Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
17710Sstevel@tonic-gate 		panic("startup: segkp_create failed");
17720Sstevel@tonic-gate 		/*NOTREACHED*/
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate 	PRM_DEBUG(segkp);
17750Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	/*
17780Sstevel@tonic-gate 	 * kpm segment
17790Sstevel@tonic-gate 	 */
17800Sstevel@tonic-gate 	segmap_kpm = 0;
17810Sstevel@tonic-gate 	if (kpm_desired) {
17820Sstevel@tonic-gate 		kpm_init();
17830Sstevel@tonic-gate 		kpm_enable = 1;
17841841Spraks 		vpm_enable = 1;
17850Sstevel@tonic-gate 	}
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	/*
17880Sstevel@tonic-gate 	 * Now create segmap segment.
17890Sstevel@tonic-gate 	 */
17900Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
17913446Smrj 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
17923446Smrj 		panic("cannot attach segmap");
17930Sstevel@tonic-gate 		/*NOTREACHED*/
17940Sstevel@tonic-gate 	}
17953446Smrj 	PRM_DEBUG(segmap);
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
17980Sstevel@tonic-gate 	a.shmsize = 0;
17990Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
18000Sstevel@tonic-gate 
18013446Smrj 	if (segmap_create(segmap, (caddr_t)&a) != 0)
18023446Smrj 		panic("segmap_create segmap");
18030Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	segdev_init();
18080Sstevel@tonic-gate 	pmem_init();
18093446Smrj 
18100Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
18110Sstevel@tonic-gate }
18120Sstevel@tonic-gate 
18133446Smrj /*
18143446Smrj  * Load a tod module for the non-standard tod part found on this system.
18153446Smrj  */
18163446Smrj static void
18173446Smrj load_tod_module(char *todmod)
18183446Smrj {
18193446Smrj 	if (modload("tod", todmod) == -1)
18203446Smrj 		halt("Can't load TOD module");
18213446Smrj }
18223446Smrj 
18230Sstevel@tonic-gate static void
18240Sstevel@tonic-gate startup_end(void)
18250Sstevel@tonic-gate {
18260Sstevel@tonic-gate 	extern void setx86isalist(void);
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 	/*
18310Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
18320Sstevel@tonic-gate 	 * initialization has been done but before the clock
18330Sstevel@tonic-gate 	 * and other devices get started.
18340Sstevel@tonic-gate 	 */
18350Sstevel@tonic-gate 	kern_setup1();
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	/*
18380Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
18390Sstevel@tonic-gate 	 */
18400Sstevel@tonic-gate 	kcpc_hw_init(CPU);
18410Sstevel@tonic-gate 
18421582Skchow #if defined(OPTERON_WORKAROUND_6323525)
18431582Skchow 	if (opteron_workaround_6323525)
18441582Skchow 		patch_workaround_6323525();
18451582Skchow #endif
18460Sstevel@tonic-gate 	/*
18473446Smrj 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
18483446Smrj 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
18493446Smrj 	 */
18503446Smrj 	if (tod_module_name != NULL) {
18513446Smrj 		PRM_POINT("load_tod_module()");
18523446Smrj 		load_tod_module(tod_module_name);
18533446Smrj 	}
18543446Smrj 
18553446Smrj 	/*
18560Sstevel@tonic-gate 	 * Configure the system.
18570Sstevel@tonic-gate 	 */
18580Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
18590Sstevel@tonic-gate 	configure();		/* set up devices */
18600Sstevel@tonic-gate 	PRM_POINT("configure() done");
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 	/*
18630Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
18640Sstevel@tonic-gate 	 * support.
18650Sstevel@tonic-gate 	 */
18660Sstevel@tonic-gate 	setx86isalist();
18671455Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
18680Sstevel@tonic-gate 	psm_install();
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 	/*
18710Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
18720Sstevel@tonic-gate 	 * we're still using bootsvcs.
18730Sstevel@tonic-gate 	 */
18743446Smrj 	PRM_POINT("NULLing out bootops");
18753446Smrj 	*bootopsp = (struct bootops *)NULL;
18760Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
18790Sstevel@tonic-gate 	(*picinitf)();
18800Sstevel@tonic-gate 	sti();
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
18830Sstevel@tonic-gate 		"softlevel1", NULL, NULL); /* XXX to be moved later */
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
18860Sstevel@tonic-gate }
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate extern char hw_serial[];
18890Sstevel@tonic-gate char *_hs1107 = hw_serial;
18900Sstevel@tonic-gate ulong_t  _bdhs34;
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate void
18930Sstevel@tonic-gate post_startup(void)
18940Sstevel@tonic-gate {
18950Sstevel@tonic-gate 	/*
18960Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
18970Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
18980Sstevel@tonic-gate 	 * instruction set extensions.
18990Sstevel@tonic-gate 	 */
19000Sstevel@tonic-gate 	bind_hwcap();
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	/*
19033446Smrj 	 * Load the System Management BIOS into the global ksmbios
19043446Smrj 	 * handle, if an SMBIOS is present on this system.
1905437Smws 	 */
1906437Smws 	ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
1907437Smws 
1908437Smws 	/*
19093446Smrj 	 * Startup the memory scrubber.
19100Sstevel@tonic-gate 	 */
1911437Smws 	memscrub_init();
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	/*
19141414Scindi 	 * Complete CPU module initialization
19151414Scindi 	 */
19161414Scindi 	cmi_post_init();
19171414Scindi 
19181414Scindi 	/*
19190Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
19200Sstevel@tonic-gate 	 */
19210Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	/*
19240Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
19250Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
19260Sstevel@tonic-gate 	 */
19270Sstevel@tonic-gate 	(void) modload("fs", "procfs");
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate #if defined(__i386)
19300Sstevel@tonic-gate 	/*
19310Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
19320Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
19330Sstevel@tonic-gate 	 */
19340Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
19350Sstevel@tonic-gate 		halt("No working FP hardware found");
19360Sstevel@tonic-gate #endif
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	maxmem = freemem;
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
19410Sstevel@tonic-gate }
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate static int
19440Sstevel@tonic-gate pp_in_ramdisk(page_t *pp)
19450Sstevel@tonic-gate {
19460Sstevel@tonic-gate 	extern uint64_t ramdisk_start, ramdisk_end;
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	return ((pp->p_pagenum >= btop(ramdisk_start)) &&
19490Sstevel@tonic-gate 	    (pp->p_pagenum < btopr(ramdisk_end)));
19500Sstevel@tonic-gate }
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate void
19530Sstevel@tonic-gate release_bootstrap(void)
19540Sstevel@tonic-gate {
19550Sstevel@tonic-gate 	int root_is_ramdisk;
19560Sstevel@tonic-gate 	page_t *pp;
19570Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
19580Sstevel@tonic-gate 	extern dev_t rootdev;
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
19610Sstevel@tonic-gate 	kobj_boot_unmountroot();
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	/*
19640Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
19650Sstevel@tonic-gate 	 */
19660Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
19673446Smrj 	clear_boot_mappings(0, _userlimit);
19683446Smrj 	postbootkernelbase = kernelbase;
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	/*
19710Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
19720Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
19730Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
19740Sstevel@tonic-gate 	 */
19750Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
19760Sstevel@tonic-gate 	if (!root_is_ramdisk) {
19770Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
19780Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
19790Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
19800Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
19810Sstevel@tonic-gate 	}
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
19840Sstevel@tonic-gate 	while (bootpages) {
19850Sstevel@tonic-gate 		pp = bootpages;
19860Sstevel@tonic-gate 		bootpages = pp->p_next;
19870Sstevel@tonic-gate 		if (root_is_ramdisk && pp_in_ramdisk(pp)) {
19880Sstevel@tonic-gate 			pp->p_next = rd_pages;
19890Sstevel@tonic-gate 			rd_pages = pp;
19900Sstevel@tonic-gate 			continue;
19910Sstevel@tonic-gate 		}
19920Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
19933446Smrj 		pp->p_prev = (struct page *)0;
19943446Smrj 		PP_CLRBOOTPAGES(pp);
19950Sstevel@tonic-gate 		page_free(pp, 1);
19960Sstevel@tonic-gate 	}
19973446Smrj 	PRM_POINT("Boot pages released");
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	/*
20000Sstevel@tonic-gate 	 * Find 1 page below 1 MB so that other processors can boot up.
20010Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
20020Sstevel@tonic-gate 	 * We should have just free'd one up.
20030Sstevel@tonic-gate 	 */
20040Sstevel@tonic-gate 	if (use_mp) {
20053446Smrj 		pfn_t pfn;
20063446Smrj 
20070Sstevel@tonic-gate 		for (pfn = 1; pfn < btop(1*1024*1024); pfn++) {
20080Sstevel@tonic-gate 			if (page_numtopp_alloc(pfn) == NULL)
20090Sstevel@tonic-gate 				continue;
20100Sstevel@tonic-gate 			rm_platter_va = i86devmap(pfn, 1,
20110Sstevel@tonic-gate 			    PROT_READ | PROT_WRITE | PROT_EXEC);
20120Sstevel@tonic-gate 			rm_platter_pa = ptob(pfn);
20130Sstevel@tonic-gate 			hat_devload(kas.a_hat,
20140Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
20150Sstevel@tonic-gate 			    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
20160Sstevel@tonic-gate 			    HAT_LOAD_NOCONSIST);
20170Sstevel@tonic-gate 			break;
20180Sstevel@tonic-gate 		}
20190Sstevel@tonic-gate 		if (pfn == btop(1*1024*1024))
20200Sstevel@tonic-gate 			panic("No page available for starting "
20210Sstevel@tonic-gate 			    "other processors");
20220Sstevel@tonic-gate 	}
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate /*
20270Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
20280Sstevel@tonic-gate  */
20290Sstevel@tonic-gate void
20300Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
20310Sstevel@tonic-gate {
20320Sstevel@tonic-gate 	pp->p_pagenum = pnum;
20330Sstevel@tonic-gate 	pp->p_mapping = NULL;
20340Sstevel@tonic-gate 	pp->p_embed = 0;
20350Sstevel@tonic-gate 	pp->p_share = 0;
20360Sstevel@tonic-gate 	pp->p_mlentry = 0;
20370Sstevel@tonic-gate }
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate /*
20400Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
20410Sstevel@tonic-gate  */
20420Sstevel@tonic-gate static pgcnt_t
20430Sstevel@tonic-gate kphysm_init(
20443446Smrj 	page_t *pp,
20450Sstevel@tonic-gate 	pgcnt_t npages)
20460Sstevel@tonic-gate {
20470Sstevel@tonic-gate 	struct memlist	*pmem;
20480Sstevel@tonic-gate 	struct memseg	*cur_memseg;
20490Sstevel@tonic-gate 	pfn_t		base_pfn;
20500Sstevel@tonic-gate 	pgcnt_t		num;
20510Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
20520Sstevel@tonic-gate 	uint64_t	addr;
20530Sstevel@tonic-gate 	uint64_t	size;
20540Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
20570Sstevel@tonic-gate 
20583446Smrj 	cur_memseg = memseg_base;
20590Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
20600Sstevel@tonic-gate 		/*
20610Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
20620Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
20630Sstevel@tonic-gate 		 */
20640Sstevel@tonic-gate 		addr = pmem->address;
20650Sstevel@tonic-gate 		size = pmem->size;
20660Sstevel@tonic-gate 		if (btop(addr) > physmax)
20670Sstevel@tonic-gate 			continue;
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 		/*
20700Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
20710Sstevel@tonic-gate 		 */
20720Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
20730Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
20740Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
20750Sstevel@tonic-gate 			size -= addr - pmem->address;
20760Sstevel@tonic-gate 		}
20770Sstevel@tonic-gate 
2078810Skchow 		/* only process pages below or equal to physmax */
2079810Skchow 		if ((btop(addr + size) - 1) > physmax)
2080810Skchow 			size = ptob(physmax - btop(addr) + 1);
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 		num = btop(size);
20830Sstevel@tonic-gate 		if (num == 0)
20840Sstevel@tonic-gate 			continue;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 		if (num > npages)
20870Sstevel@tonic-gate 			num = npages;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 		npages -= num;
20900Sstevel@tonic-gate 		pages_done += num;
20910Sstevel@tonic-gate 		base_pfn = btop(addr);
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 		if (prom_debug)
20940Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
20950Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
20960Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 		/*
20993446Smrj 		 * Ignore pages below ddiphysmin to simplify ddi memory
21000Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
21010Sstevel@tonic-gate 		 */
21020Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
21033446Smrj 			if (base_pfn + num <= ddiphysmin)
21040Sstevel@tonic-gate 				continue;
21050Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
21060Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
21070Sstevel@tonic-gate 			base_pfn = ddiphysmin;
21080Sstevel@tonic-gate 		}
21093446Smrj 
21100Sstevel@tonic-gate 		/*
21110Sstevel@tonic-gate 		 * Build the memsegs entry
21120Sstevel@tonic-gate 		 */
21130Sstevel@tonic-gate 		cur_memseg->pages = pp;
21140Sstevel@tonic-gate 		cur_memseg->epages = pp + num;
21150Sstevel@tonic-gate 		cur_memseg->pages_base = base_pfn;
21160Sstevel@tonic-gate 		cur_memseg->pages_end = base_pfn + num;
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 		/*
21193446Smrj 		 * Insert into memseg list in decreasing pfn range order.
21200Sstevel@tonic-gate 		 * Low memory is typically more fragmented such that this
21210Sstevel@tonic-gate 		 * ordering keeps the larger ranges at the front of the list
21220Sstevel@tonic-gate 		 * for code that searches memseg.
21233446Smrj 		 * This ASSERTS that the memsegs coming in from boot are in
21243446Smrj 		 * increasing physical address order and not contiguous.
21250Sstevel@tonic-gate 		 */
21263446Smrj 		if (memsegs != NULL) {
21273446Smrj 			ASSERT(cur_memseg->pages_base >= memsegs->pages_end);
21283446Smrj 			cur_memseg->next = memsegs;
21290Sstevel@tonic-gate 		}
21303446Smrj 		memsegs = cur_memseg;
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 		/*
21330Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
21340Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
21350Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
21360Sstevel@tonic-gate 		 * it initializes them.
21370Sstevel@tonic-gate 		 */
21380Sstevel@tonic-gate 		add_physmem(pp, num, base_pfn);
21390Sstevel@tonic-gate 		cur_memseg++;
21400Sstevel@tonic-gate 		availrmem_initial += num;
21410Sstevel@tonic-gate 		availrmem += num;
21420Sstevel@tonic-gate 
21433446Smrj 		pp += num;
21440Sstevel@tonic-gate 	}
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
21470Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
21480Sstevel@tonic-gate 	PRM_DEBUG(freemem);
21490Sstevel@tonic-gate 	build_pfn_hash();
21500Sstevel@tonic-gate 	return (pages_done);
21510Sstevel@tonic-gate }
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate /*
21540Sstevel@tonic-gate  * Kernel VM initialization.
21550Sstevel@tonic-gate  */
21560Sstevel@tonic-gate static void
21570Sstevel@tonic-gate kvm_init(void)
21580Sstevel@tonic-gate {
21590Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	/*
21620Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
21630Sstevel@tonic-gate 	 */
21640Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
21650Sstevel@tonic-gate 	as_avlinit(&kas);
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
21680Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
21710Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	/*
21740Sstevel@tonic-gate 	 * We're about to map out /boot.  This is the beginning of the
21750Sstevel@tonic-gate 	 * system resource management transition. We can no longer
21760Sstevel@tonic-gate 	 * call into /boot for I/O or memory allocations.
21770Sstevel@tonic-gate 	 */
21780Sstevel@tonic-gate 	(void) seg_attach(&kas, final_kernelheap,
21790Sstevel@tonic-gate 	    ekernelheap - final_kernelheap, &kvseg);
21800Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
21810Sstevel@tonic-gate 
21823446Smrj 	if (core_size > 0) {
21833446Smrj 		PRM_POINT("attaching kvseg_core");
21843446Smrj 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
21853446Smrj 		    &kvseg_core);
21863446Smrj 		(void) segkmem_create(&kvseg_core);
21873446Smrj 	}
21883290Sjohansen 
21893446Smrj 	if (segziosize > 0) {
21903446Smrj 		PRM_POINT("attaching segzio");
21913290Sjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
21923290Sjohansen 		    &kzioseg);
21933290Sjohansen 		(void) segkmem_zio_create(&kzioseg);
21943290Sjohansen 
21953290Sjohansen 		/* create zio area covering new segment */
21963290Sjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
21973290Sjohansen 	}
21980Sstevel@tonic-gate 
21993446Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
22000Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 	/*
22050Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
22060Sstevel@tonic-gate 	 */
22073446Smrj 	PRM_POINT("protecting redzone");
22080Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 	/*
22110Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
22120Sstevel@tonic-gate 	 */
22130Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
22140Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	/*
22170Sstevel@tonic-gate 	 * Make data writable until end.
22180Sstevel@tonic-gate 	 */
22190Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
22200Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
22210Sstevel@tonic-gate }
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate /*
22240Sstevel@tonic-gate  * These are MTTR registers supported by P6
22250Sstevel@tonic-gate  */
22260Sstevel@tonic-gate static struct	mtrrvar	mtrrphys_arr[MAX_MTRRVAR];
22270Sstevel@tonic-gate static uint64_t mtrr64k, mtrr16k1, mtrr16k2;
22280Sstevel@tonic-gate static uint64_t mtrr4k1, mtrr4k2, mtrr4k3;
22290Sstevel@tonic-gate static uint64_t mtrr4k4, mtrr4k5, mtrr4k6;
22300Sstevel@tonic-gate static uint64_t mtrr4k7, mtrr4k8, mtrrcap;
22310Sstevel@tonic-gate uint64_t mtrrdef, pat_attr_reg;
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate /*
22340Sstevel@tonic-gate  * Disable reprogramming of MTRRs by default.
22350Sstevel@tonic-gate  */
22360Sstevel@tonic-gate int	enable_relaxed_mtrr = 0;
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate void
22391676Sjpk setup_mtrr(void)
22400Sstevel@tonic-gate {
22410Sstevel@tonic-gate 	int i, ecx;
22420Sstevel@tonic-gate 	int vcnt;
22430Sstevel@tonic-gate 	struct	mtrrvar	*mtrrphys;
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	if (!(x86_feature & X86_MTRR))
22460Sstevel@tonic-gate 		return;
22470Sstevel@tonic-gate 
2248770Skucharsk 	mtrrcap = rdmsr(REG_MTRRCAP);
2249770Skucharsk 	mtrrdef = rdmsr(REG_MTRRDEF);
22500Sstevel@tonic-gate 	if (mtrrcap & MTRRCAP_FIX) {
2251770Skucharsk 		mtrr64k = rdmsr(REG_MTRR64K);
2252770Skucharsk 		mtrr16k1 = rdmsr(REG_MTRR16K1);
2253770Skucharsk 		mtrr16k2 = rdmsr(REG_MTRR16K2);
2254770Skucharsk 		mtrr4k1 = rdmsr(REG_MTRR4K1);
2255770Skucharsk 		mtrr4k2 = rdmsr(REG_MTRR4K2);
2256770Skucharsk 		mtrr4k3 = rdmsr(REG_MTRR4K3);
2257770Skucharsk 		mtrr4k4 = rdmsr(REG_MTRR4K4);
2258770Skucharsk 		mtrr4k5 = rdmsr(REG_MTRR4K5);
2259770Skucharsk 		mtrr4k6 = rdmsr(REG_MTRR4K6);
2260770Skucharsk 		mtrr4k7 = rdmsr(REG_MTRR4K7);
2261770Skucharsk 		mtrr4k8 = rdmsr(REG_MTRR4K8);
22620Sstevel@tonic-gate 	}
22630Sstevel@tonic-gate 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
22640Sstevel@tonic-gate 		vcnt = MAX_MTRRVAR;
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
22670Sstevel@tonic-gate 		i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
2268770Skucharsk 		mtrrphys->mtrrphys_base = rdmsr(ecx);
2269770Skucharsk 		mtrrphys->mtrrphys_mask = rdmsr(ecx + 1);
22700Sstevel@tonic-gate 		if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
22710Sstevel@tonic-gate 			mtrrphys->mtrrphys_mask &= ~MTRRPHYSMASK_V;
22720Sstevel@tonic-gate 		}
22730Sstevel@tonic-gate 	}
22740Sstevel@tonic-gate 	if (x86_feature & X86_PAT) {
22750Sstevel@tonic-gate 		if (enable_relaxed_mtrr)
22760Sstevel@tonic-gate 			mtrrdef = MTRR_TYPE_WB|MTRRDEF_FE|MTRRDEF_E;
22770Sstevel@tonic-gate 		pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
22780Sstevel@tonic-gate 	}
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 	mtrr_sync();
22810Sstevel@tonic-gate }
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate /*
22840Sstevel@tonic-gate  * Sync current cpu mtrr with the incore copy of mtrr.
22850Sstevel@tonic-gate  * This function has to be invoked with interrupts disabled
22860Sstevel@tonic-gate  * Currently we do not capture other cpu's. This is invoked on cpu0
22870Sstevel@tonic-gate  * just after reading /etc/system.
22880Sstevel@tonic-gate  * On other cpu's its invoked from mp_startup().
22890Sstevel@tonic-gate  */
22900Sstevel@tonic-gate void
22911676Sjpk mtrr_sync(void)
22920Sstevel@tonic-gate {
22930Sstevel@tonic-gate 	uint_t	crvalue, cr0_orig;
22940Sstevel@tonic-gate 	int	vcnt, i, ecx;
22950Sstevel@tonic-gate 	struct	mtrrvar	*mtrrphys;
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	cr0_orig = crvalue = getcr0();
22980Sstevel@tonic-gate 	crvalue |= CR0_CD;
22990Sstevel@tonic-gate 	crvalue &= ~CR0_NW;
23000Sstevel@tonic-gate 	setcr0(crvalue);
23010Sstevel@tonic-gate 	invalidate_cache();
23020Sstevel@tonic-gate 
23033446Smrj 	reload_cr3();
2304770Skucharsk 	if (x86_feature & X86_PAT)
2305770Skucharsk 		wrmsr(REG_MTRRPAT, pat_attr_reg);
2306770Skucharsk 
2307770Skucharsk 	wrmsr(REG_MTRRDEF, rdmsr(REG_MTRRDEF) &
2308770Skucharsk 	    ~((uint64_t)(uintptr_t)MTRRDEF_E));
2309770Skucharsk 
23100Sstevel@tonic-gate 	if (mtrrcap & MTRRCAP_FIX) {
2311770Skucharsk 		wrmsr(REG_MTRR64K, mtrr64k);
2312770Skucharsk 		wrmsr(REG_MTRR16K1, mtrr16k1);
2313770Skucharsk 		wrmsr(REG_MTRR16K2, mtrr16k2);
2314770Skucharsk 		wrmsr(REG_MTRR4K1, mtrr4k1);
2315770Skucharsk 		wrmsr(REG_MTRR4K2, mtrr4k2);
2316770Skucharsk 		wrmsr(REG_MTRR4K3, mtrr4k3);
2317770Skucharsk 		wrmsr(REG_MTRR4K4, mtrr4k4);
2318770Skucharsk 		wrmsr(REG_MTRR4K5, mtrr4k5);
2319770Skucharsk 		wrmsr(REG_MTRR4K6, mtrr4k6);
2320770Skucharsk 		wrmsr(REG_MTRR4K7, mtrr4k7);
2321770Skucharsk 		wrmsr(REG_MTRR4K8, mtrr4k8);
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
23240Sstevel@tonic-gate 		vcnt = MAX_MTRRVAR;
23250Sstevel@tonic-gate 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
2326770Skucharsk 	    i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
2327770Skucharsk 		wrmsr(ecx, mtrrphys->mtrrphys_base);
2328770Skucharsk 		wrmsr(ecx + 1, mtrrphys->mtrrphys_mask);
23290Sstevel@tonic-gate 	}
2330770Skucharsk 	wrmsr(REG_MTRRDEF, mtrrdef);
23313446Smrj 
23323446Smrj 	reload_cr3();
23330Sstevel@tonic-gate 	invalidate_cache();
23340Sstevel@tonic-gate 	setcr0(cr0_orig);
23350Sstevel@tonic-gate }
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate /*
23380Sstevel@tonic-gate  * resync mtrr so that BIOS is happy. Called from mdboot
23390Sstevel@tonic-gate  */
23400Sstevel@tonic-gate void
23411676Sjpk mtrr_resync(void)
23420Sstevel@tonic-gate {
23430Sstevel@tonic-gate 	if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
23440Sstevel@tonic-gate 		/*
23450Sstevel@tonic-gate 		 * We could have changed the default mtrr definition.
23460Sstevel@tonic-gate 		 * Put it back to uncached which is what it is at power on
23470Sstevel@tonic-gate 		 */
23480Sstevel@tonic-gate 		mtrrdef = MTRR_TYPE_UC|MTRRDEF_FE|MTRRDEF_E;
23490Sstevel@tonic-gate 		mtrr_sync();
23500Sstevel@tonic-gate 	}
23510Sstevel@tonic-gate }
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate void
23541676Sjpk get_system_configuration(void)
23550Sstevel@tonic-gate {
23560Sstevel@tonic-gate 	char	prop[32];
23570Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	if (((BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop)) ||
23600Sstevel@tonic-gate 		(BOP_GETPROP(bootops, "nodes", prop) < 0) 	||
23610Sstevel@tonic-gate 		(kobj_getvalue(prop, &nodes_ll) == -1) ||
23620Sstevel@tonic-gate 		(nodes_ll > MAXNODES))			   ||
23630Sstevel@tonic-gate 	    ((BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop)) ||
23640Sstevel@tonic-gate 		(BOP_GETPROP(bootops, "cpus_pernode", prop) < 0) ||
23650Sstevel@tonic-gate 		(kobj_getvalue(prop, &cpus_pernode_ll) == -1))) {
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
23680Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
23690Sstevel@tonic-gate 	} else {
23700Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
23710Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
23720Sstevel@tonic-gate 	}
23730Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop)) ||
23740Sstevel@tonic-gate 		(BOP_GETPROP(bootops, "kernelbase", prop) < 0) 	||
23750Sstevel@tonic-gate 		(kobj_getvalue(prop, &lvalue) == -1))
23760Sstevel@tonic-gate 			eprom_kernelbase = NULL;
23770Sstevel@tonic-gate 	else
23780Sstevel@tonic-gate 			eprom_kernelbase = (uintptr_t)lvalue;
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop)) ||
23810Sstevel@tonic-gate 	    (BOP_GETPROP(bootops, "segmapsize", prop) < 0) ||
23820Sstevel@tonic-gate 	    (kobj_getvalue(prop, &lvalue) == -1)) {
23830Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
23840Sstevel@tonic-gate 	} else {
23850Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
23860Sstevel@tonic-gate 	}
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop)) ||
23890Sstevel@tonic-gate 	    (BOP_GETPROP(bootops, "segmapfreelists", prop) < 0) ||
23900Sstevel@tonic-gate 	    (kobj_getvalue(prop, &lvalue) == -1)) {
23910Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
23920Sstevel@tonic-gate 	} else {
23930Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
23940Sstevel@tonic-gate 	}
23951417Skchow 
23961417Skchow 	if ((BOP_GETPROPLEN(bootops, "physmem") <= sizeof (prop)) &&
23971417Skchow 	    (BOP_GETPROP(bootops, "physmem", prop) >= 0) &&
23981417Skchow 	    (kobj_getvalue(prop, &lvalue) != -1)) {
23991417Skchow 		physmem = (uintptr_t)lvalue;
24001417Skchow 	}
24010Sstevel@tonic-gate }
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate /*
24040Sstevel@tonic-gate  * Add to a memory list.
24050Sstevel@tonic-gate  * start = start of new memory segment
24060Sstevel@tonic-gate  * len = length of new memory segment in bytes
24070Sstevel@tonic-gate  * new = pointer to a new struct memlist
24080Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
24090Sstevel@tonic-gate  */
24103446Smrj void
24110Sstevel@tonic-gate memlist_add(
24120Sstevel@tonic-gate 	uint64_t start,
24130Sstevel@tonic-gate 	uint64_t len,
24140Sstevel@tonic-gate 	struct memlist *new,
24150Sstevel@tonic-gate 	struct memlist **memlistp)
24160Sstevel@tonic-gate {
24170Sstevel@tonic-gate 	struct memlist *cur;
24180Sstevel@tonic-gate 	uint64_t end = start + len;
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	new->address = start;
24210Sstevel@tonic-gate 	new->size = len;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	cur = *memlistp;
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	while (cur) {
24260Sstevel@tonic-gate 		if (cur->address >= end) {
24270Sstevel@tonic-gate 			new->next = cur;
24280Sstevel@tonic-gate 			*memlistp = new;
24290Sstevel@tonic-gate 			new->prev = cur->prev;
24300Sstevel@tonic-gate 			cur->prev = new;
24310Sstevel@tonic-gate 			return;
24320Sstevel@tonic-gate 		}
24330Sstevel@tonic-gate 		ASSERT(cur->address + cur->size <= start);
24340Sstevel@tonic-gate 		if (cur->next == NULL) {
24350Sstevel@tonic-gate 			cur->next = new;
24360Sstevel@tonic-gate 			new->prev = cur;
24370Sstevel@tonic-gate 			new->next = NULL;
24380Sstevel@tonic-gate 			return;
24390Sstevel@tonic-gate 		}
24400Sstevel@tonic-gate 		memlistp = &cur->next;
24410Sstevel@tonic-gate 		cur = cur->next;
24420Sstevel@tonic-gate 	}
24430Sstevel@tonic-gate }
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate void
24460Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
24470Sstevel@tonic-gate {
24480Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
24490Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
24520Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
24530Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
24540Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate caddr_t
24580Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
24590Sstevel@tonic-gate {
24600Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
24610Sstevel@tonic-gate }
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate /*ARGSUSED*/
24640Sstevel@tonic-gate caddr_t
24650Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
24660Sstevel@tonic-gate {
24670Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
24680Sstevel@tonic-gate 	/*NOTREACHED*/
24690Sstevel@tonic-gate 	return (0);
24700Sstevel@tonic-gate }
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate /*ARGSUSED*/
24730Sstevel@tonic-gate void
24740Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
24750Sstevel@tonic-gate {
24760Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
24770Sstevel@tonic-gate }
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate /*
24800Sstevel@tonic-gate  * This is called just after configure() in startup().
24810Sstevel@tonic-gate  *
24820Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
24830Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
24840Sstevel@tonic-gate  * given that various parts of the instruction set may appear
24850Sstevel@tonic-gate  * and disappear between different implementations.
24860Sstevel@tonic-gate  *
24870Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
24880Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
24890Sstevel@tonic-gate  * more flexibility.
24900Sstevel@tonic-gate  *
24910Sstevel@tonic-gate  * So, we just leave this alone.
24920Sstevel@tonic-gate  */
24930Sstevel@tonic-gate void
24940Sstevel@tonic-gate setx86isalist(void)
24950Sstevel@tonic-gate {
24960Sstevel@tonic-gate 	char *tp;
24970Sstevel@tonic-gate 	size_t len;
24980Sstevel@tonic-gate 	extern char *isa_list;
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate #define	TBUFSIZE	1024
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
25030Sstevel@tonic-gate 	*tp = '\0';
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate #if defined(__amd64)
25060Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
25070Sstevel@tonic-gate #endif
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	switch (x86_vendor) {
25100Sstevel@tonic-gate 	case X86_VENDOR_Intel:
25110Sstevel@tonic-gate 	case X86_VENDOR_AMD:
25120Sstevel@tonic-gate 	case X86_VENDOR_TM:
25130Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
25140Sstevel@tonic-gate 			/*
25150Sstevel@tonic-gate 			 * Pentium Pro or later
25160Sstevel@tonic-gate 			 */
25170Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
25180Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
25190Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
25200Sstevel@tonic-gate 		}
25210Sstevel@tonic-gate 		/*FALLTHROUGH*/
25220Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
25230Sstevel@tonic-gate 		/*
25240Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
25250Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
25260Sstevel@tonic-gate 		 */
25270Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
25280Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
25290Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
25300Sstevel@tonic-gate 			    "+mmx pentium " : " ");
25310Sstevel@tonic-gate 		}
25320Sstevel@tonic-gate 		break;
25330Sstevel@tonic-gate 	default:
25340Sstevel@tonic-gate 		break;
25350Sstevel@tonic-gate 	}
25360Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
25370Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
25380Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
25390Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate #undef TBUFSIZE
25420Sstevel@tonic-gate }
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate #ifdef __amd64
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate void *
25480Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
25490Sstevel@tonic-gate {
25500Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
25510Sstevel@tonic-gate }
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate void
25540Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
25550Sstevel@tonic-gate {
25560Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
25570Sstevel@tonic-gate }
25580Sstevel@tonic-gate 
25593446Smrj #else /* __i386 */
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate void *
25620Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
25630Sstevel@tonic-gate {
25640Sstevel@tonic-gate 	caddr_t	vaddr;
25650Sstevel@tonic-gate 	uintptr_t v;
25660Sstevel@tonic-gate 	size_t	start;
25670Sstevel@tonic-gate 	size_t	end;
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
25700Sstevel@tonic-gate 	if (vaddr == NULL)
25710Sstevel@tonic-gate 		return (NULL);
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
25740Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
25753446Smrj 	ASSERT(v + size <= valloc_base);
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	start = btop(v - kernelbase);
25780Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
25790Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
25800Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 	while (start <= end) {
25830Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
25840Sstevel@tonic-gate 		++start;
25850Sstevel@tonic-gate 	}
25860Sstevel@tonic-gate 	return (vaddr);
25870Sstevel@tonic-gate }
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate void
25900Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
25910Sstevel@tonic-gate {
25920Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
25930Sstevel@tonic-gate 	size_t	start;
25940Sstevel@tonic-gate 	size_t	end;
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
25973446Smrj 	ASSERT(v + size <= valloc_base);
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 	start = btop(v - kernelbase);
26000Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
26010Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
26020Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	while (start <= end) {
26050Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
26060Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
26070Sstevel@tonic-gate 		++start;
26080Sstevel@tonic-gate 	}
26090Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
26100Sstevel@tonic-gate }
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate /*
26130Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
26140Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
26150Sstevel@tonic-gate  */
26160Sstevel@tonic-gate void *
26170Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
26180Sstevel@tonic-gate {
26190Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
26200Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
26210Sstevel@tonic-gate 	size_t start;
26220Sstevel@tonic-gate 	size_t end;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	/*
26250Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
26260Sstevel@tonic-gate 	 */
26270Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
26280Sstevel@tonic-gate 		return (NULL);
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate 	/*
26310Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
26320Sstevel@tonic-gate 	 */
26333446Smrj 	if (v >= valloc_base || eaddr < kernelbase)
26340Sstevel@tonic-gate 		return (NULL);
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 	/*
26370Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
26380Sstevel@tonic-gate 	 */
26390Sstevel@tonic-gate 	if (v < kernelbase)
26400Sstevel@tonic-gate 		v = kernelbase;
26410Sstevel@tonic-gate 	start = btop(v - kernelbase);
26420Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
26430Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
26440Sstevel@tonic-gate 		end = toxic_bit_map_len;
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
26470Sstevel@tonic-gate 		return (NULL);
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 	v = kernelbase + ptob(start);
26500Sstevel@tonic-gate 	if (len != NULL)
26510Sstevel@tonic-gate 		*len = ptob(end - start);
26520Sstevel@tonic-gate 	return ((void *)v);
26530Sstevel@tonic-gate }
26540Sstevel@tonic-gate 
26553446Smrj #endif	/* __i386 */
2656