xref: /onnv-gate/usr/src/uts/i86pc/os/startup.c (revision 11971:ddd5873f34a9)
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 /*
2211474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/t_lock.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <sys/signal.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/user.h>
330Sstevel@tonic-gate #include <sys/mman.h>
340Sstevel@tonic-gate #include <sys/vm.h>
350Sstevel@tonic-gate #include <sys/conf.h>
360Sstevel@tonic-gate #include <sys/avintr.h>
370Sstevel@tonic-gate #include <sys/autoconf.h>
380Sstevel@tonic-gate #include <sys/disp.h>
390Sstevel@tonic-gate #include <sys/class.h>
400Sstevel@tonic-gate #include <sys/bitmap.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/privregs.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/proc.h>
450Sstevel@tonic-gate #include <sys/buf.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
473446Smrj #include <sys/mem.h>
480Sstevel@tonic-gate #include <sys/kstat.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <sys/reboot.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <sys/cred.h>
530Sstevel@tonic-gate #include <sys/vnode.h>
540Sstevel@tonic-gate #include <sys/file.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <sys/procfs.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/vfs.h>
590Sstevel@tonic-gate #include <sys/cmn_err.h>
600Sstevel@tonic-gate #include <sys/utsname.h>
610Sstevel@tonic-gate #include <sys/debug.h>
620Sstevel@tonic-gate #include <sys/kdi.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <sys/dumphdr.h>
650Sstevel@tonic-gate #include <sys/bootconf.h>
660Sstevel@tonic-gate #include <sys/varargs.h>
670Sstevel@tonic-gate #include <sys/promif.h>
685084Sjohnlev #include <sys/modctl.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <sys/sunddi.h>
710Sstevel@tonic-gate #include <sys/sunndi.h>
720Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
730Sstevel@tonic-gate #include <sys/ddidmareq.h>
740Sstevel@tonic-gate #include <sys/psw.h>
750Sstevel@tonic-gate #include <sys/regset.h>
760Sstevel@tonic-gate #include <sys/clock.h>
770Sstevel@tonic-gate #include <sys/pte.h>
780Sstevel@tonic-gate #include <sys/tss.h>
790Sstevel@tonic-gate #include <sys/stack.h>
800Sstevel@tonic-gate #include <sys/trap.h>
810Sstevel@tonic-gate #include <sys/fp.h>
825159Sjohnlev #include <vm/kboot_mmu.h>
830Sstevel@tonic-gate #include <vm/anon.h>
840Sstevel@tonic-gate #include <vm/as.h>
850Sstevel@tonic-gate #include <vm/page.h>
860Sstevel@tonic-gate #include <vm/seg.h>
870Sstevel@tonic-gate #include <vm/seg_dev.h>
880Sstevel@tonic-gate #include <vm/seg_kmem.h>
890Sstevel@tonic-gate #include <vm/seg_kpm.h>
900Sstevel@tonic-gate #include <vm/seg_map.h>
910Sstevel@tonic-gate #include <vm/seg_vn.h>
920Sstevel@tonic-gate #include <vm/seg_kp.h>
930Sstevel@tonic-gate #include <sys/memnode.h>
940Sstevel@tonic-gate #include <vm/vm_dep.h>
950Sstevel@tonic-gate #include <sys/thread.h>
960Sstevel@tonic-gate #include <sys/sysconf.h>
970Sstevel@tonic-gate #include <sys/vm_machparam.h>
980Sstevel@tonic-gate #include <sys/archsystm.h>
990Sstevel@tonic-gate #include <sys/machsystm.h>
1000Sstevel@tonic-gate #include <vm/hat.h>
1010Sstevel@tonic-gate #include <vm/hat_i86.h>
1020Sstevel@tonic-gate #include <sys/pmem.h>
1030Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1040Sstevel@tonic-gate #include <sys/x86_archext.h>
1056336Sbholler #include <sys/cpuvar.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>
1111414Scindi #include <sys/cpu_module.h>
112437Smws #include <sys/smbios.h>
1133446Smrj #include <sys/debug_info.h>
1145159Sjohnlev #include <sys/bootinfo.h>
1155107Seota #include <sys/ddi_timer.h>
1168662SJordan.Vaughan@Sun.com #include <sys/systeminfo.h>
1177656SSherry.Moore@Sun.COM #include <sys/multiboot.h>
1183446Smrj 
1195084Sjohnlev #ifdef __xpv
1205159Sjohnlev 
1215084Sjohnlev #include <sys/hypervisor.h>
1225084Sjohnlev #include <sys/xen_mmu.h>
1235084Sjohnlev #include <sys/evtchn_impl.h>
1245084Sjohnlev #include <sys/gnttab.h>
1255084Sjohnlev #include <sys/xpv_panic.h>
1265084Sjohnlev #include <xen/sys/xenbus_comms.h>
1275084Sjohnlev #include <xen/public/physdev.h>
1285159Sjohnlev 
1295084Sjohnlev extern void xen_late_startup(void);
1303446Smrj 
1315159Sjohnlev struct xen_evt_data cpu0_evt_data;
1325159Sjohnlev 
1335159Sjohnlev #endif /* __xpv */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate extern void progressbar_init(void);
1362712Snn35248 extern void brand_init(void);
1376880Sdv142724 extern void pcf_init(void);
1388906SEric.Saxe@Sun.COM extern void pg_init(void);
1390Sstevel@tonic-gate 
1404878Sblakej extern int size_pse_array(pgcnt_t, int);
1414878Sblakej 
1427720SKen.Erickson@Sun.COM #if defined(_SOFT_HOSTID)
1437720SKen.Erickson@Sun.COM 
1447720SKen.Erickson@Sun.COM #include <sys/rtc.h>
1457720SKen.Erickson@Sun.COM 
1467720SKen.Erickson@Sun.COM static int32_t set_soft_hostid(void);
1477720SKen.Erickson@Sun.COM static char hostid_file[] = "/etc/hostid";
1487720SKen.Erickson@Sun.COM 
1497720SKen.Erickson@Sun.COM #endif
1507720SKen.Erickson@Sun.COM 
1518215SVikram.Hegde@Sun.COM void *gfx_devinfo_list;
1527720SKen.Erickson@Sun.COM 
15311600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
15411600SVikram.Hegde@Sun.COM extern void immu_startup(void);
15511600SVikram.Hegde@Sun.COM #endif
15611600SVikram.Hegde@Sun.COM 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1590Sstevel@tonic-gate  * interface.
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * segkp
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate extern int segkp_fromheap;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static void kvm_init(void);
1690Sstevel@tonic-gate static void startup_init(void);
1700Sstevel@tonic-gate static void startup_memlist(void);
1713446Smrj static void startup_kmem(void);
1720Sstevel@tonic-gate static void startup_modules(void);
1730Sstevel@tonic-gate static void startup_vm(void);
1740Sstevel@tonic-gate static void startup_end(void);
1754828Sjosephb static void layout_kernel_va(void);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1790Sstevel@tonic-gate  */
1801479Skchow #ifdef __i386
1814828Sjosephb 
1821479Skchow /*
1831479Skchow  * Due to virtual address space limitations running in 32 bit mode, restrict
1844828Sjosephb  * the amount of physical memory configured to a max of PHYSMEM pages (16g).
1851479Skchow  *
1861479Skchow  * If the physical max memory size of 64g were allowed to be configured, the
1871479Skchow  * size of user virtual address space will be less than 1g. A limited user
1881479Skchow  * address space greatly reduces the range of applications that can run.
1891479Skchow  *
1904828Sjosephb  * If more physical memory than PHYSMEM is required, users should preferably
1914828Sjosephb  * run in 64 bit mode which has far looser virtual address space limitations.
1921479Skchow  *
1931479Skchow  * If 64 bit mode is not available (as in IA32) and/or more physical memory
1944828Sjosephb  * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
1951479Skchow  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
1961479Skchow  * should also be carefully tuned to balance out the need of the user
1971479Skchow  * application while minimizing the risk of kernel heap exhaustion due to
1981479Skchow  * kernelbase being set too high.
1991479Skchow  */
2004828Sjosephb #define	PHYSMEM	0x400000
2014828Sjosephb 
2024828Sjosephb #else /* __amd64 */
2031479Skchow 
2044828Sjosephb /*
2054828Sjosephb  * For now we can handle memory with physical addresses up to about
2064828Sjosephb  * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
2074828Sjosephb  * half the VA space for seg_kpm. When systems get bigger than 64TB this
2084828Sjosephb  * code will need revisiting. There is an implicit assumption that there
2094828Sjosephb  * are no *huge* holes in the physical address space too.
2104828Sjosephb  */
2114828Sjosephb #define	TERABYTE		(1ul << 40)
2124828Sjosephb #define	PHYSMEM_MAX64		mmu_btop(64 * TERABYTE)
2134828Sjosephb #define	PHYSMEM			PHYSMEM_MAX64
2144828Sjosephb #define	AMD64_VA_HOLE_END	0xFFFF800000000000ul
2154828Sjosephb 
2164828Sjosephb #endif /* __amd64 */
2174828Sjosephb 
2184828Sjosephb pgcnt_t physmem = PHYSMEM;
2190Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate char *kobj_file_buf;
2220Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
2257656SSherry.Moore@Sun.COM caddr_t	rm_platter_va = 0;
2260Sstevel@tonic-gate uint32_t rm_platter_pa;
2270Sstevel@tonic-gate 
228841Skchow int	auto_lpg_disable = 1;
229841Skchow 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate uintptr_t hole_start, hole_end;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate  * kpm mapping window
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate caddr_t kpm_vbase;
2390Sstevel@tonic-gate size_t  kpm_size;
2404828Sjosephb static int kpm_desired;
2414828Sjosephb #ifdef __amd64
2424828Sjosephb static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
2434828Sjosephb #endif
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * Configuration parameters set at boot time.
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
2520Sstevel@tonic-gate struct bootops		**bootopsp;
2530Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate char bootblock_fstype[16];
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
2587656SSherry.Moore@Sun.COM char kern_bootfile[OBP_MAXPATHLEN];
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate /*
2613290Sjohansen  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
2623290Sjohansen  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
2633290Sjohansen  * zio buffers from their own segment, otherwise they are allocated from the
2643290Sjohansen  * heap.  The optimization of allocating zio buffers from their own segment is
2653290Sjohansen  * only valid on 64-bit kernels.
2663290Sjohansen  */
2673290Sjohansen #if defined(__amd64)
2683290Sjohansen int segzio_fromheap = 0;
2693290Sjohansen #else
2703290Sjohansen int segzio_fromheap = 1;
2713290Sjohansen #endif
2723290Sjohansen 
2733290Sjohansen /*
2740Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
2750Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2760Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * VM data structures
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
2840Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2850Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
2864878Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
2874878Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
2884878Sblakej int pse_shift;			/* log2(pse_table_size) */
2890Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2900Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
2910Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
2920Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
2930Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
2963446Smrj static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
2973446Smrj 
2980Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate #if defined(__amd64)
3010Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
3020Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
3030Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
3040Sstevel@tonic-gate #else
3050Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
3060Sstevel@tonic-gate #endif
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
3093290Sjohansen caddr_t segzio_base;		/* Base address of segzio */
3100Sstevel@tonic-gate #if defined(__amd64)
3110Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
3120Sstevel@tonic-gate #else
3130Sstevel@tonic-gate pgcnt_t segkpsize = 0;
3140Sstevel@tonic-gate #endif
3153290Sjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
3160Sstevel@tonic-gate 
3173446Smrj /*
3183446Smrj  * VA range available to the debugger
3193446Smrj  */
3203446Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
3213446Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
3223446Smrj 
3230Sstevel@tonic-gate struct memseg *memseg_base;
3240Sstevel@tonic-gate struct vnode unused_pages_vp;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate #define	FOURGB	0x100000000LL
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate struct memlist *memlist;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
3310Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
3320Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
3330Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
3340Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
3350Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
3360Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
3370Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
3400Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
3419940SVikram.Hegde@Sun.COM struct memlist *bios_rsvd;	/* Bios reserved memory */
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
3450Sstevel@tonic-gate  */
3463446Smrj static pgcnt_t kphysm_init(page_t *, pgcnt_t);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate  * a couple useful roundup macros
3520Sstevel@tonic-gate  */
3530Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
3540Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
3550Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
3560Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
3570Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
3583446Smrj 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
3590Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
3600Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*
3630Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
3640Sstevel@tonic-gate  *		+-----------------------+
3653446Smrj  *		|			|
3660Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
3670Sstevel@tonic-gate  *		|	debugger	|
3680Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
3690Sstevel@tonic-gate  *		|      Kernel Data	|
3700Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
3710Sstevel@tonic-gate  *              |      Kernel Text	|
3725084Sjohnlev  * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
3733446Smrj  *		|---       GDT       ---|- GDT page (GDT_VA)
3743446Smrj  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
3753446Smrj  *		|			|
3763446Smrj  * 		|   page_t structures	|
3770Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
3780Sstevel@tonic-gate  * 		|   page hash, etc.	|
3793446Smrj  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
3803446Smrj  *		|			|  (segkp is just an arena in the heap)
3810Sstevel@tonic-gate  *		|			|
3820Sstevel@tonic-gate  *		|	kvseg		|
3830Sstevel@tonic-gate  *		|			|
3840Sstevel@tonic-gate  *		|			|
3850Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
3860Sstevel@tonic-gate  * 		|        Segkmap	|
3873446Smrj  * 0xC3002000  -|-----------------------|- segmap_start (floating)
3880Sstevel@tonic-gate  *		|	Red Zone	|
3890Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
3900Sstevel@tonic-gate  *		|			|			||
3910Sstevel@tonic-gate  *		|     Shared objects	|			\/
3920Sstevel@tonic-gate  *		|			|
3930Sstevel@tonic-gate  *		:			:
3940Sstevel@tonic-gate  *		|	user data	|
3950Sstevel@tonic-gate  *		|-----------------------|
3960Sstevel@tonic-gate  *		|	user text	|
3970Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
3980Sstevel@tonic-gate  *		|	user stack	|
3990Sstevel@tonic-gate  *		:			:
4000Sstevel@tonic-gate  *		|	invalid		|
4010Sstevel@tonic-gate  * 0x00000000	+-----------------------+
4020Sstevel@tonic-gate  *
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
4050Sstevel@tonic-gate  *			+-----------------------+
4063446Smrj  *			|			|
4070Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
4080Sstevel@tonic-gate  *			|	debugger (?)	|
4090Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
4100Sstevel@tonic-gate  *			|      unused    	|
4110Sstevel@tonic-gate  *			+-----------------------+
4120Sstevel@tonic-gate  *			|      Kernel Data	|
4130Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
4140Sstevel@tonic-gate  *			|      Kernel Text	|
4150Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
4163446Smrj  *			|---       GDT       ---|- GDT page (GDT_VA)
4173446Smrj  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
4183446Smrj  *			|			|
4190Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
4200Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
4210Sstevel@tonic-gate  *			|	 Kernel		|
4220Sstevel@tonic-gate  *			|	  heap		|
4230Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
4243446Smrj  *			|	 segmap		|
4253446Smrj  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
4260Sstevel@tonic-gate  *			|    device mappings	|
4270Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
4283290Sjohansen  *			|	  segzio	|
4293290Sjohansen  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
4300Sstevel@tonic-gate  *			|	  segkp		|
4313446Smrj  * ---                  |-----------------------|- segkp_base (floating)
4323446Smrj  * 			|   page_t structures	|  valloc_base + valloc_sz
4333446Smrj  * 			|   memsegs, memlists, 	|
4343446Smrj  * 			|   page hash, etc.	|
4354828Sjosephb  * 0xFFFFFF00.00000000  |-----------------------|- valloc_base (lower if > 1TB)
4360Sstevel@tonic-gate  *			|	 segkpm		|
4370Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
4380Sstevel@tonic-gate  *			|	Red Zone	|
4394828Sjosephb  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE (lower if > 1TB)
4400Sstevel@tonic-gate  *			|     User stack	|- User space memory
4410Sstevel@tonic-gate  * 			|			|
4420Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
4430Sstevel@tonic-gate  *			:			:
4440Sstevel@tonic-gate  * 			|			|
4450Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
4460Sstevel@tonic-gate  * 			|			|
4470Sstevel@tonic-gate  * 			| VA Hole / unused	|
4480Sstevel@tonic-gate  * 			|			|
4490Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
4500Sstevel@tonic-gate  *			|			|
4510Sstevel@tonic-gate  *			|			|
4520Sstevel@tonic-gate  *			:			:
4530Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
4540Sstevel@tonic-gate  *			|			|
4550Sstevel@tonic-gate  *			|	user data	|
4560Sstevel@tonic-gate  *			|-----------------------|
4570Sstevel@tonic-gate  *			|	user text	|
4580Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
4590Sstevel@tonic-gate  *			|	invalid		|
4600Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
4610Sstevel@tonic-gate  *
4620Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
4630Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
4640Sstevel@tonic-gate  *
4650Sstevel@tonic-gate  * Floating values:
4660Sstevel@tonic-gate  *
4670Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
4683446Smrj  * structures.  This region contains page_t structures for
4693446Smrj  * physical memory, memsegs, memlists, and the page hash.
4700Sstevel@tonic-gate  *
4710Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
4720Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
4730Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
4740Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
4750Sstevel@tonic-gate  * accessed using rip-relative addressing
4760Sstevel@tonic-gate  *
4770Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
4780Sstevel@tonic-gate  *
4790Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
4800Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
4810Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
4820Sstevel@tonic-gate  *
4833446Smrj  * segmap_start: start of segmap. The length of segmap can be modified
4847692SAmrita.Sadhukhan@Sun.COM  * through eeprom. The default length is 16MB on 32-bit systems and 64MB
4857692SAmrita.Sadhukhan@Sun.COM  * on 64-bit systems.
4860Sstevel@tonic-gate  *
4870Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
4880Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
4890Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
4900Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
4910Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
4920Sstevel@tonic-gate  * segkp sizes).
4930Sstevel@tonic-gate  *
4940Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
4950Sstevel@tonic-gate  *	size	page_t's			size
4960Sstevel@tonic-gate  *	----	---------	----------	-----------
4970Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
4980Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
4990Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
5000Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
5010Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
5020Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
5030Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
5040Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
5050Sstevel@tonic-gate  *
5060Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
5070Sstevel@tonic-gate  * configurations above 8gb.
5080Sstevel@tonic-gate  *
5090Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
5100Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
5110Sstevel@tonic-gate  */
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate /* real-time-clock initialization parameters */
5143446Smrj extern time_t process_rtc_config_file(void);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate uintptr_t	kernelbase;
5173446Smrj uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
5180Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
5190Sstevel@tonic-gate size_t		segmapsize;
5203446Smrj uintptr_t	segmap_start;
5210Sstevel@tonic-gate int		segmapfreelists;
5220Sstevel@tonic-gate pgcnt_t		npages;
5233446Smrj pgcnt_t		orig_npages;
5240Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
5250Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate /*
5280Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
5290Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
5300Sstevel@tonic-gate  * the bootstrap.
5310Sstevel@tonic-gate  */
5323446Smrj static page_t *bootpages;
5333446Smrj 
5343446Smrj /*
5353446Smrj  * boot time pages that have a vnode from the ramdisk will keep that forever.
5363446Smrj  */
5373446Smrj static page_t *rd_pages;
5380Sstevel@tonic-gate 
5397656SSherry.Moore@Sun.COM /*
5407656SSherry.Moore@Sun.COM  * Lower 64K
5417656SSherry.Moore@Sun.COM  */
5427656SSherry.Moore@Sun.COM static page_t *lower_pages = NULL;
5437656SSherry.Moore@Sun.COM static int lower_pages_count = 0;
5447656SSherry.Moore@Sun.COM 
5450Sstevel@tonic-gate struct system_hardware system_hardware;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
5490Sstevel@tonic-gate  */
5500Sstevel@tonic-gate static void
5513446Smrj print_memlist(char *title, struct memlist *mp)
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
5540Sstevel@tonic-gate 	while (mp != NULL)  {
5550Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
55611474SJonathan.Adams@Sun.COM 		    mp->ml_address, mp->ml_size);
55711474SJonathan.Adams@Sun.COM 		mp = mp->ml_next;
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate /*
5620Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
5630Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
5640Sstevel@tonic-gate  */
5650Sstevel@tonic-gate int	l2cache_sz = 0x80000;
5660Sstevel@tonic-gate int	l2cache_linesz = 0x40;
5670Sstevel@tonic-gate int	l2cache_assoc = 1;
5680Sstevel@tonic-gate 
5694426Saguzovsk static size_t	textrepl_min_gb = 10;
5704426Saguzovsk 
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
5730Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
5740Sstevel@tonic-gate  */
5750Sstevel@tonic-gate #ifdef __amd64
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate vmem_t		*device_arena;
5780Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
5793446Smrj size_t		toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate #else	/* __i386 */
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
5840Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate #endif	/* __i386 */
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate /*
5890Sstevel@tonic-gate  * Simple boot time debug facilities
5900Sstevel@tonic-gate  */
5910Sstevel@tonic-gate static char *prm_dbg_str[] = {
5920Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%x\n",
5930Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%llx\n"
5940Sstevel@tonic-gate };
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate int prom_debug;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate #define	PRM_DEBUG(q)	if (prom_debug) 	\
5990Sstevel@tonic-gate 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
6000Sstevel@tonic-gate #define	PRM_POINT(q)	if (prom_debug) 	\
6010Sstevel@tonic-gate 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
6050Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
6060Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
6070Sstevel@tonic-gate  */
6089940SVikram.Hegde@Sun.COM #define	NUM_ALLOCATIONS 8
6090Sstevel@tonic-gate int num_allocations = 0;
6100Sstevel@tonic-gate struct {
6110Sstevel@tonic-gate 	void **al_ptr;
6120Sstevel@tonic-gate 	size_t al_size;
6130Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
6140Sstevel@tonic-gate size_t valloc_sz = 0;
6150Sstevel@tonic-gate uintptr_t valloc_base;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
6180Sstevel@tonic-gate 		size = ROUND_UP_PAGE(size);		 		\
6190Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
6200Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
6210Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
6220Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
6230Sstevel@tonic-gate 		valloc_sz += size;					\
6240Sstevel@tonic-gate 		++num_allocations;				 	\
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6273446Smrj /*
6283446Smrj  * Allocate all the initial memory needed by the page allocator.
6293446Smrj  */
6300Sstevel@tonic-gate static void
6310Sstevel@tonic-gate perform_allocations(void)
6320Sstevel@tonic-gate {
6330Sstevel@tonic-gate 	caddr_t mem;
6340Sstevel@tonic-gate 	int i;
6353446Smrj 	int valloc_align;
6360Sstevel@tonic-gate 
6373446Smrj 	PRM_DEBUG(valloc_base);
6383446Smrj 	PRM_DEBUG(valloc_sz);
6393446Smrj 	valloc_align = mmu.level_size[mmu.max_page_level > 0];
6403446Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
6410Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
6420Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
6430Sstevel@tonic-gate 	bzero(mem, valloc_sz);
6440Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
6450Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
6460Sstevel@tonic-gate 		mem += allocations[i].al_size;
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * Our world looks like this at startup time.
6520Sstevel@tonic-gate  *
6530Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
6540Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
6550Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
6560Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
6570Sstevel@tonic-gate  *
6580Sstevel@tonic-gate  * On the text page:
6590Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
6600Sstevel@tonic-gate  *
6610Sstevel@tonic-gate  * On the data page:
6623446Smrj  * unix/genunix/krtld/module data loads.
6633446Smrj  *
6640Sstevel@tonic-gate  * Machine-dependent startup code
6650Sstevel@tonic-gate  */
6660Sstevel@tonic-gate void
6670Sstevel@tonic-gate startup(void)
6680Sstevel@tonic-gate {
6695084Sjohnlev #if !defined(__xpv)
6701494Sjg 	extern void startup_pci_bios(void);
6717656SSherry.Moore@Sun.COM 	extern int post_fastreboot;
6725084Sjohnlev #endif
6736336Sbholler 	extern cpuset_t cpu_ready_set;
6746336Sbholler 
6755084Sjohnlev 	/*
6765084Sjohnlev 	 * Make sure that nobody tries to use sekpm until we have
6775084Sjohnlev 	 * initialized it properly.
6785084Sjohnlev 	 */
6790Sstevel@tonic-gate #if defined(__amd64)
6804828Sjosephb 	kpm_desired = 1;
6810Sstevel@tonic-gate #endif
6820Sstevel@tonic-gate 	kpm_enable = 0;
6836336Sbholler 	CPUSET_ONLY(cpu_ready_set, 0);	/* cpu 0 is boot cpu */
6840Sstevel@tonic-gate 
6855084Sjohnlev #if defined(__xpv)	/* XXPV fix me! */
6865084Sjohnlev 	{
6875084Sjohnlev 		extern int segvn_use_regions;
6885084Sjohnlev 		segvn_use_regions = 0;
6895084Sjohnlev 	}
6905084Sjohnlev #endif
6910Sstevel@tonic-gate 	progressbar_init();
6920Sstevel@tonic-gate 	startup_init();
6935316Sjohnlev #if defined(__xpv)
6945316Sjohnlev 	startup_xen_version();
6955316Sjohnlev #endif
6960Sstevel@tonic-gate 	startup_memlist();
6973446Smrj 	startup_kmem();
6984828Sjosephb 	startup_vm();
6995084Sjohnlev #if !defined(__xpv)
7007656SSherry.Moore@Sun.COM 	if (!post_fastreboot)
7017656SSherry.Moore@Sun.COM 		startup_pci_bios();
7025084Sjohnlev #endif
7037532SSean.Ye@Sun.COM #if defined(__xpv)
7047532SSean.Ye@Sun.COM 	startup_xen_mca();
7057532SSean.Ye@Sun.COM #endif
7060Sstevel@tonic-gate 	startup_modules();
70710321SLipeng.Sang@Sun.COM 
7080Sstevel@tonic-gate 	startup_end();
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate static void
7120Sstevel@tonic-gate startup_init()
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	/*
7170Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
7180Sstevel@tonic-gate 	 */
7190Sstevel@tonic-gate 	cpuid_pass2(CPU);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	/*
7240Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
7250Sstevel@tonic-gate 	 */
7260Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
7270Sstevel@tonic-gate 		++prom_debug;
7280Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/*
7320Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
7330Sstevel@tonic-gate 	 */
7340Sstevel@tonic-gate 	get_system_configuration();
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
7380Sstevel@tonic-gate 	 */
7390Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
7400Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
7410Sstevel@tonic-gate 		    CPU->cpu_brandstr);
7420Sstevel@tonic-gate 		halt("This processor is not supported by this release "
7430Sstevel@tonic-gate 		    "of Solaris.");
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate /*
7500Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
7510Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
7520Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
7530Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
7540Sstevel@tonic-gate  */
7550Sstevel@tonic-gate static void
7560Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	uintptr_t va;
7590Sstevel@tonic-gate 	uintptr_t next_va;
7600Sstevel@tonic-gate 	pfn_t pfn;
7610Sstevel@tonic-gate 	uint64_t pfn_addr;
7620Sstevel@tonic-gate 	uint64_t pfn_eaddr;
7630Sstevel@tonic-gate 	uint_t prot;
7640Sstevel@tonic-gate 	size_t len;
7650Sstevel@tonic-gate 	uint_t change;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if (prom_debug)
7680Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
7690Sstevel@tonic-gate 		    *addr, *size);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
7730Sstevel@tonic-gate 	 */
7740Sstevel@tonic-gate 	if (*addr == 0) {
7750Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
7760Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/*
7803446Smrj 	 * First we trim from the front of the range. Since kbm_probe()
7810Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
7820Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
7830Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
7840Sstevel@tonic-gate 	 * but w < v.
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	do {
7870Sstevel@tonic-gate 		change = 0;
7880Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
7893446Smrj 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7900Sstevel@tonic-gate 		    va = next_va) {
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 			next_va = va + len;
7933446Smrj 			pfn_addr = pfn_to_pa(pfn);
7940Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
7970Sstevel@tonic-gate 				change = 1;
7980Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
7990Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
8000Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
8010Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
8020Sstevel@tonic-gate 				}
8030Sstevel@tonic-gate 			}
8040Sstevel@tonic-gate 		}
8050Sstevel@tonic-gate 		if (change && prom_debug)
8060Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
8070Sstevel@tonic-gate 			    *addr, *size);
8080Sstevel@tonic-gate 	} while (change);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * Trim pages from the end of the range.
8120Sstevel@tonic-gate 	 */
8130Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
8143446Smrj 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
8150Sstevel@tonic-gate 	    va = next_va) {
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 		next_va = va + len;
8183446Smrj 		pfn_addr = pfn_to_pa(pfn);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
8210Sstevel@tonic-gate 			*size = pfn_addr - *addr;
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if (prom_debug)
8250Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
8260Sstevel@tonic-gate 		    *addr, *size);
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate static void
8300Sstevel@tonic-gate kpm_init()
8310Sstevel@tonic-gate {
8320Sstevel@tonic-gate 	struct segkpm_crargs b;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	/*
8350Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
8360Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
8370Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
8380Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
8390Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
8400Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
8410Sstevel@tonic-gate 	 * pagesize is used.
8420Sstevel@tonic-gate 	 */
8430Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
8440Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
8450Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
8460Sstevel@tonic-gate 	kpmp2pshft = 0;
8470Sstevel@tonic-gate 	kpmpnpgs = 1;
8480Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
8510Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
8540Sstevel@tonic-gate 		panic("cannot attach segkpm");
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
8570Sstevel@tonic-gate 	b.nvcolors = 1;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
8600Sstevel@tonic-gate 		panic("segkpm_create segkpm");
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate /*
8663446Smrj  * The debug info page provides enough information to allow external
8673446Smrj  * inspectors (e.g. when running under a hypervisor) to bootstrap
8683446Smrj  * themselves into allowing full-blown kernel debugging.
8693446Smrj  */
8703446Smrj static void
8713446Smrj init_debug_info(void)
8723446Smrj {
8733446Smrj 	caddr_t mem;
8743446Smrj 	debug_info_t *di;
8753446Smrj 
8763446Smrj #ifndef __lint
8773446Smrj 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
8783446Smrj #endif
8793446Smrj 
8803446Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
8813446Smrj 	    MMU_PAGESIZE);
8823446Smrj 
8833446Smrj 	if (mem != (caddr_t)DEBUG_INFO_VA)
8843446Smrj 		panic("BOP_ALLOC() failed");
8853446Smrj 	bzero(mem, MMU_PAGESIZE);
8863446Smrj 
8873446Smrj 	di = (debug_info_t *)mem;
8883446Smrj 
8893446Smrj 	di->di_magic = DEBUG_INFO_MAGIC;
8903446Smrj 	di->di_version = DEBUG_INFO_VERSION;
8915084Sjohnlev 	di->di_modules = (uintptr_t)&modules;
8925084Sjohnlev 	di->di_s_text = (uintptr_t)s_text;
8935084Sjohnlev 	di->di_e_text = (uintptr_t)e_text;
8945084Sjohnlev 	di->di_s_data = (uintptr_t)s_data;
8955084Sjohnlev 	di->di_e_data = (uintptr_t)e_data;
8965084Sjohnlev 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
8975084Sjohnlev 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
8983446Smrj }
8993446Smrj 
9003446Smrj /*
9013446Smrj  * Build the memlists and other kernel essential memory system data structures.
9023446Smrj  * This is everything at valloc_base.
9030Sstevel@tonic-gate  */
9040Sstevel@tonic-gate static void
9050Sstevel@tonic-gate startup_memlist(void)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	size_t memlist_sz;
9080Sstevel@tonic-gate 	size_t memseg_sz;
9090Sstevel@tonic-gate 	size_t pagehash_sz;
9100Sstevel@tonic-gate 	size_t pp_sz;
9110Sstevel@tonic-gate 	uintptr_t va;
9120Sstevel@tonic-gate 	size_t len;
9130Sstevel@tonic-gate 	uint_t prot;
9140Sstevel@tonic-gate 	pfn_t pfn;
9150Sstevel@tonic-gate 	int memblocks;
9169940SVikram.Hegde@Sun.COM 	pfn_t rsvd_high_pfn;
9179940SVikram.Hegde@Sun.COM 	pgcnt_t rsvd_pgcnt;
9189940SVikram.Hegde@Sun.COM 	size_t rsvdmemlist_sz;
9199940SVikram.Hegde@Sun.COM 	int rsvdmemblocks;
9200Sstevel@tonic-gate 	caddr_t pagecolor_mem;
9210Sstevel@tonic-gate 	size_t pagecolor_memsz;
9220Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
9230Sstevel@tonic-gate 	size_t page_ctrs_size;
9244878Sblakej 	size_t pse_table_alloc_size;
9250Sstevel@tonic-gate 	struct memlist *current;
9260Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
9290Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
9300Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	/*
9350Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
9360Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
9370Sstevel@tonic-gate 	 */
9383446Smrj 	len = kbm_nucleus_size;
9393446Smrj 	ASSERT(len > MMU_PAGESIZE);
9400Sstevel@tonic-gate 
9413446Smrj 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
9423446Smrj 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
9433446Smrj 	if (e_moddata - moddata > MODDATA)
9443446Smrj 		e_moddata = moddata + MODDATA;
9450Sstevel@tonic-gate 
9463446Smrj 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
9473446Smrj 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
9483446Smrj 	if (e_modtext - modtext > MODTEXT)
9493446Smrj 		e_modtext = modtext + MODTEXT;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	econtig = e_moddata;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9540Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
9550Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9560Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
9570Sstevel@tonic-gate 	PRM_DEBUG(econtig);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	/*
9603446Smrj 	 * Examine the boot loader physical memory map to find out:
9610Sstevel@tonic-gate 	 * - total memory in system - physinstalled
9620Sstevel@tonic-gate 	 * - the max physical address - physmax
9633446Smrj 	 * - the number of discontiguous segments of memory.
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	if (prom_debug)
9663446Smrj 		print_memlist("boot physinstalled",
9670Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
9680Sstevel@tonic-gate 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
9690Sstevel@tonic-gate 	    &physinstalled, &memblocks);
9700Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9710Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9720Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	/*
9759940SVikram.Hegde@Sun.COM 	 * Examine the bios reserved memory to find out:
9769940SVikram.Hegde@Sun.COM 	 * - the number of discontiguous segments of memory.
9779940SVikram.Hegde@Sun.COM 	 */
9789940SVikram.Hegde@Sun.COM 	if (prom_debug)
9799940SVikram.Hegde@Sun.COM 		print_memlist("boot reserved mem",
9809940SVikram.Hegde@Sun.COM 		    bootops->boot_mem->rsvdmem);
9819940SVikram.Hegde@Sun.COM 	installed_top_size(bootops->boot_mem->rsvdmem, &rsvd_high_pfn,
9829940SVikram.Hegde@Sun.COM 	    &rsvd_pgcnt, &rsvdmemblocks);
9839940SVikram.Hegde@Sun.COM 	PRM_DEBUG(rsvd_high_pfn);
9849940SVikram.Hegde@Sun.COM 	PRM_DEBUG(rsvd_pgcnt);
9859940SVikram.Hegde@Sun.COM 	PRM_DEBUG(rsvdmemblocks);
9869940SVikram.Hegde@Sun.COM 
9879940SVikram.Hegde@Sun.COM 	/*
9880Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
9890Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
9900Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
9910Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
9920Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
9930Sstevel@tonic-gate 	 */
9940Sstevel@tonic-gate 	mmu_init();
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate #ifdef	__i386
9970Sstevel@tonic-gate 	/*
9980Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
9990Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
10000Sstevel@tonic-gate 	 */
10010Sstevel@tonic-gate 	if (mmu.pae_hat) {
10020Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
10030Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
10040Sstevel@tonic-gate 			physmax = PFN_64G - 1;
10050Sstevel@tonic-gate 		}
10060Sstevel@tonic-gate 	} else {
10070Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
10080Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
10090Sstevel@tonic-gate 			physmax = PFN_4G - 1;
10100Sstevel@tonic-gate 		}
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate #endif
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
10170Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
10180Sstevel@tonic-gate 		char value[8];
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 		if (len < 8)
10210Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
10220Sstevel@tonic-gate 		else
10230Sstevel@tonic-gate 			(void) strcpy(value, "");
10240Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
10250Sstevel@tonic-gate 			mmu.pt_nx = 0;
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	/*
10300Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
10310Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
10320Sstevel@tonic-gate 	 *
10330Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
10340Sstevel@tonic-gate 	 */
10350Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
10360Sstevel@tonic-gate 	obp_pages = 0;
10370Sstevel@tonic-gate 	va = KERNEL_TEXT;
10383446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
10390Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
10400Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
10410Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
10420Sstevel@tonic-gate 		va += len;
10430Sstevel@tonic-gate 	}
10440Sstevel@tonic-gate 	PRM_DEBUG(npages);
10450Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/*
10484828Sjosephb 	 * If physmem is patched to be non-zero, use it instead of the computed
10494828Sjosephb 	 * value unless it is larger than the actual amount of memory on hand.
10500Sstevel@tonic-gate 	 */
10511417Skchow 	if (physmem == 0 || physmem > npages) {
10520Sstevel@tonic-gate 		physmem = npages;
10531417Skchow 	} else if (physmem < npages) {
10541479Skchow 		orig_npages = npages;
10550Sstevel@tonic-gate 		npages = physmem;
10561417Skchow 	}
10570Sstevel@tonic-gate 	PRM_DEBUG(physmem);
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	/*
10600Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
10610Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
10620Sstevel@tonic-gate 	 * include:
10630Sstevel@tonic-gate 	 *	memsegs
10640Sstevel@tonic-gate 	 *	memlists
10650Sstevel@tonic-gate 	 *	page hash table
10660Sstevel@tonic-gate 	 *	page_t's
10670Sstevel@tonic-gate 	 *	page coloring data structs
10680Sstevel@tonic-gate 	 */
10690Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
10700Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
10710Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	/*
10743446Smrj 	 * Reserve space for memlists. There's no real good way to know exactly
10753446Smrj 	 * how much room we'll need, but this should be a good upper bound.
10760Sstevel@tonic-gate 	 */
10770Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
10780Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
10790Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
10800Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/*
10839940SVikram.Hegde@Sun.COM 	 * Reserve space for bios reserved memlists.
10849940SVikram.Hegde@Sun.COM 	 */
10859940SVikram.Hegde@Sun.COM 	rsvdmemlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
10869940SVikram.Hegde@Sun.COM 	    (rsvdmemblocks + POSS_NEW_FRAGMENTS));
10879940SVikram.Hegde@Sun.COM 	ADD_TO_ALLOCATIONS(bios_rsvd, rsvdmemlist_sz);
10889940SVikram.Hegde@Sun.COM 	PRM_DEBUG(rsvdmemlist_sz);
10899940SVikram.Hegde@Sun.COM 
10909940SVikram.Hegde@Sun.COM 	/*
10910Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
10920Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
10930Sstevel@tonic-gate 	 */
10940Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
10950Sstevel@tonic-gate 	page_hashsz = 1 << highbit(page_hashsz);
10960Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
10970Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
10980Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	/*
11013446Smrj 	 * Set aside room for the page structures themselves.
11020Sstevel@tonic-gate 	 */
11033446Smrj 	PRM_DEBUG(npages);
11043446Smrj 	pp_sz = sizeof (struct page) * npages;
11050Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
11060Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	/*
11090Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
11100Sstevel@tonic-gate 	 */
11110Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
11120Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
11130Sstevel@tonic-gate 	pagecolor_memsz =
11140Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
11150Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
11160Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
11190Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
11200Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
11210Sstevel@tonic-gate 
11224878Sblakej 	/*
11234878Sblakej 	 * Allocate the array that protects pp->p_selock.
11244878Sblakej 	 */
11254878Sblakej 	pse_shift = size_pse_array(physmem, max_ncpus);
11264878Sblakej 	pse_table_size = 1 << pse_shift;
11274878Sblakej 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
11284878Sblakej 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
11294878Sblakej 
11303446Smrj #if defined(__amd64)
11313446Smrj 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
11323446Smrj 	valloc_base = VALLOC_BASE;
11334828Sjosephb 
11344828Sjosephb 	/*
11354828Sjosephb 	 * The default values of VALLOC_BASE and SEGKPM_BASE should work
11364828Sjosephb 	 * for values of physmax up to 1 Terabyte. They need adjusting when
113711721SKit.Chow@Sun.COM 	 * memory is at addresses above 1 TB. When adjusted, segkpm_base must
113811721SKit.Chow@Sun.COM 	 * be aligned on KERNEL_REDZONE_SIZE boundary (span of top level pte).
11394828Sjosephb 	 */
11404828Sjosephb 	if (physmax + 1 > mmu_btop(TERABYTE)) {
11414828Sjosephb 		uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
11424828Sjosephb 
114311721SKit.Chow@Sun.COM 		segkpm_base = -(P2ROUNDUP((2 * kpm_resv_amount),
114411721SKit.Chow@Sun.COM 		    KERNEL_REDZONE_SIZE));	/* down from top VA */
11454828Sjosephb 
11464828Sjosephb 		/* make sure we leave some space for user apps above hole */
11474828Sjosephb 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
11484828Sjosephb 		if (segkpm_base > SEGKPM_BASE)
11494828Sjosephb 			segkpm_base = SEGKPM_BASE;
11504828Sjosephb 		PRM_DEBUG(segkpm_base);
11514828Sjosephb 
115211721SKit.Chow@Sun.COM 		valloc_base = segkpm_base + P2ROUNDUP(kpm_resv_amount, ONE_GIG);
115311721SKit.Chow@Sun.COM 		if (valloc_base < segkpm_base)
115411721SKit.Chow@Sun.COM 			panic("not enough kernel VA to support memory size");
11554828Sjosephb 		PRM_DEBUG(valloc_base);
11564828Sjosephb 	}
11573446Smrj #else	/* __i386 */
11583446Smrj 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
11593446Smrj 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
11604828Sjosephb 	PRM_DEBUG(valloc_base);
11613446Smrj #endif	/* __i386 */
11623446Smrj 
11630Sstevel@tonic-gate 	/*
11643446Smrj 	 * do all the initial allocations
11653446Smrj 	 */
11663446Smrj 	perform_allocations();
11673446Smrj 
11683446Smrj 	/*
11693446Smrj 	 * Build phys_install and phys_avail in kernel memspace.
11703446Smrj 	 * - phys_install should be all memory in the system.
11713446Smrj 	 * - phys_avail is phys_install minus any memory mapped before this
11723446Smrj 	 *    point above KERNEL_TEXT.
11733446Smrj 	 */
11743446Smrj 	current = phys_install = memlist;
11753446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
11763446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
11773446Smrj 		panic("physinstalled was too big!");
11783446Smrj 	if (prom_debug)
11793446Smrj 		print_memlist("phys_install", phys_install);
11803446Smrj 
11813446Smrj 	phys_avail = current;
11823446Smrj 	PRM_POINT("Building phys_avail:\n");
11833446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
11843446Smrj 	    avail_filter);
11853446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
11863446Smrj 		panic("physavail was too big!");
11873446Smrj 	if (prom_debug)
11883446Smrj 		print_memlist("phys_avail", phys_avail);
11893446Smrj 
11903446Smrj 	/*
11919940SVikram.Hegde@Sun.COM 	 * Build bios reserved memspace
11929940SVikram.Hegde@Sun.COM 	 */
11939940SVikram.Hegde@Sun.COM 	current = bios_rsvd;
11949940SVikram.Hegde@Sun.COM 	copy_memlist_filter(bootops->boot_mem->rsvdmem, &current, NULL);
11959940SVikram.Hegde@Sun.COM 	if ((caddr_t)current > (caddr_t)bios_rsvd + rsvdmemlist_sz)
11969940SVikram.Hegde@Sun.COM 		panic("bios_rsvd was too big!");
11979940SVikram.Hegde@Sun.COM 	if (prom_debug)
11989940SVikram.Hegde@Sun.COM 		print_memlist("bios_rsvd", bios_rsvd);
11999940SVikram.Hegde@Sun.COM 
12009940SVikram.Hegde@Sun.COM 	/*
12013446Smrj 	 * setup page coloring
12020Sstevel@tonic-gate 	 */
12033446Smrj 	page_coloring_setup(pagecolor_mem);
12043446Smrj 	page_lock_init();	/* currently a no-op */
12053446Smrj 
12063446Smrj 	/*
12073446Smrj 	 * free page list counters
12083446Smrj 	 */
12093446Smrj 	(void) page_ctrs_alloc(page_ctrs_mem);
12103446Smrj 
12113446Smrj 	/*
12126880Sdv142724 	 * Size the pcf array based on the number of cpus in the box at
12136880Sdv142724 	 * boot time.
12146880Sdv142724 	 */
12156880Sdv142724 
12166880Sdv142724 	pcf_init();
12176880Sdv142724 
12186880Sdv142724 	/*
12193446Smrj 	 * Initialize the page structures from the memory lists.
12203446Smrj 	 */
12213446Smrj 	availrmem_initial = availrmem = freemem = 0;
12223446Smrj 	PRM_POINT("Calling kphysm_init()...");
12233446Smrj 	npages = kphysm_init(pp_base, npages);
12243446Smrj 	PRM_POINT("kphysm_init() done");
12253446Smrj 	PRM_DEBUG(npages);
12263446Smrj 
12273446Smrj 	init_debug_info();
12283446Smrj 
12293446Smrj 	/*
12303446Smrj 	 * Now that page_t's have been initialized, remove all the
12313446Smrj 	 * initial allocation pages from the kernel free page lists.
12323446Smrj 	 */
12333446Smrj 	boot_mapin((caddr_t)valloc_base, valloc_sz);
12345460Sjosephb 	boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
12353446Smrj 	PRM_POINT("startup_memlist() done");
12363446Smrj 
12373446Smrj 	PRM_DEBUG(valloc_sz);
12384426Saguzovsk 
12394748Saguzovsk #if defined(__amd64)
12404748Saguzovsk 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
12414748Saguzovsk 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
12424748Saguzovsk 		extern size_t textrepl_size_thresh;
12434426Saguzovsk 		textrepl_size_thresh = (16 << 20) - 1;
12444426Saguzovsk 	}
12454748Saguzovsk #endif
12463446Smrj }
12473446Smrj 
12483446Smrj /*
12493446Smrj  * Layout the kernel's part of address space and initialize kmem allocator.
12503446Smrj  */
12513446Smrj static void
12523446Smrj startup_kmem(void)
12533446Smrj {
12543717Sdp78419 	extern void page_set_colorequiv_arr(void);
12559370SKuriakose.Kuruvilla@Sun.COM 	const char *fmt = "?features: %b\n";
12563717Sdp78419 
12573446Smrj 	PRM_POINT("startup_kmem() starting...");
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate #if defined(__amd64)
12600Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
12610Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
12620Sstevel@tonic-gate 		    "systems.");
12634828Sjosephb 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
12640Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
12653446Smrj 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
12660Sstevel@tonic-gate #else	/* __i386 */
12670Sstevel@tonic-gate 	/*
12680Sstevel@tonic-gate 	 * We configure kernelbase based on:
12690Sstevel@tonic-gate 	 *
12700Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
12710Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
12720Sstevel@tonic-gate 	 *
12730Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
12740Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
12750Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
12760Sstevel@tonic-gate 	 *
12770Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
12780Sstevel@tonic-gate 	 */
12790Sstevel@tonic-gate 	if (eprom_kernelbase) {
12800Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
12810Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
12820Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
12830Sstevel@tonic-gate 	} else {
12840Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
12850Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
12860Sstevel@tonic-gate 	}
12870Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
12883446Smrj 	core_base = valloc_base;
12890Sstevel@tonic-gate 	core_size = 0;
12903446Smrj #endif	/* __i386 */
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	PRM_DEBUG(core_base);
12930Sstevel@tonic-gate 	PRM_DEBUG(core_size);
12943446Smrj 	PRM_DEBUG(kernelbase);
12950Sstevel@tonic-gate 
12964828Sjosephb #if defined(__i386)
12974828Sjosephb 	segkp_fromheap = 1;
12984828Sjosephb #endif	/* __i386 */
12993446Smrj 
13003446Smrj 	ekernelheap = (char *)core_base;
13013446Smrj 	PRM_DEBUG(ekernelheap);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	/*
13040Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
13050Sstevel@tonic-gate 	 * update variables that were initialized with a value of
13060Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
13070Sstevel@tonic-gate 	 *
13080Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
13090Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
13100Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
13110Sstevel@tonic-gate 	 *	just be declared as variables there?
13120Sstevel@tonic-gate 	 */
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
13150Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
13164875Sjosephb #if defined(__amd64)
13174875Sjosephb 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
13184875Sjosephb #else
13190Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
13200Sstevel@tonic-gate #endif
13210Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
13220Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
13230Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
13240Sstevel@tonic-gate 
13254828Sjosephb 	layout_kernel_va();
13264828Sjosephb 
13274828Sjosephb #if defined(__i386)
13284828Sjosephb 	/*
13294828Sjosephb 	 * If segmap is too large we can push the bottom of the kernel heap
13304828Sjosephb 	 * higher than the base.  Or worse, it could exceed the top of the
13314828Sjosephb 	 * VA space entirely, causing it to wrap around.
13324828Sjosephb 	 */
13334828Sjosephb 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
13344828Sjosephb 		panic("too little address space available for kernelheap,"
13354828Sjosephb 		    " use eeprom for lower kernelbase or smaller segmapsize");
13364828Sjosephb #endif	/* __i386 */
13374828Sjosephb 
13380Sstevel@tonic-gate 	/*
13390Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
13400Sstevel@tonic-gate 	 */
13414828Sjosephb 	kernelheap_init(kernelheap, ekernelheap,
13424828Sjosephb 	    kernelheap + MMU_PAGESIZE,
13433446Smrj 	    (void *)core_base, (void *)(core_base + core_size));
13440Sstevel@tonic-gate 
13455084Sjohnlev #if defined(__xpv)
13465084Sjohnlev 	/*
13475084Sjohnlev 	 * Link pending events struct into cpu struct
13485084Sjohnlev 	 */
13495084Sjohnlev 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
13505084Sjohnlev #endif
13510Sstevel@tonic-gate 	/*
13520Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
13530Sstevel@tonic-gate 	 */
13540Sstevel@tonic-gate 	kmem_init();
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	/*
13573717Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
13583717Sdp78419 	 */
13593717Sdp78419 	page_set_colorequiv_arr();
13603717Sdp78419 
13613717Sdp78419 	/*
13620Sstevel@tonic-gate 	 * print this out early so that we know what's going on
13630Sstevel@tonic-gate 	 */
13649370SKuriakose.Kuruvilla@Sun.COM 	cmn_err(CE_CONT, fmt, x86_feature, FMT_X86_FEATURE);
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	/*
13670Sstevel@tonic-gate 	 * Initialize bp_mapin().
13680Sstevel@tonic-gate 	 */
13690Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
13700Sstevel@tonic-gate 
13711479Skchow 	/*
13721479Skchow 	 * orig_npages is non-zero if physmem has been configured for less
13731479Skchow 	 * than the available memory.
13741479Skchow 	 */
13751479Skchow 	if (orig_npages) {
13764828Sjosephb 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
13774828Sjosephb 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
13784828Sjosephb 		    npages, orig_npages);
13791479Skchow 	}
13800Sstevel@tonic-gate #if defined(__i386)
13810Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
13820Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
13830Sstevel@tonic-gate 		    "System using 0x%lx",
13840Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
13850Sstevel@tonic-gate #endif
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
13880Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
13890Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
13900Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
13910Sstevel@tonic-gate 	}
13920Sstevel@tonic-gate #endif
13930Sstevel@tonic-gate 
13945084Sjohnlev #ifdef __xpv
13955084Sjohnlev 	/*
13965084Sjohnlev 	 * Some of the xen start information has to be relocated up
13975084Sjohnlev 	 * into the kernel's permanent address space.
13985084Sjohnlev 	 */
13995084Sjohnlev 	PRM_POINT("calling xen_relocate_start_info()");
14005084Sjohnlev 	xen_relocate_start_info();
14015084Sjohnlev 	PRM_POINT("xen_relocate_start_info() done");
14025084Sjohnlev 
14035084Sjohnlev 	/*
14045084Sjohnlev 	 * (Update the vcpu pointer in our cpu structure to point into
14055084Sjohnlev 	 * the relocated shared info.)
14065084Sjohnlev 	 */
14075084Sjohnlev 	CPU->cpu_m.mcpu_vcpu_info =
14085084Sjohnlev 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
14095084Sjohnlev #endif
14105084Sjohnlev 
14113446Smrj 	PRM_POINT("startup_kmem() done");
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate 
14145741Smrj #ifndef __xpv
14155741Smrj /*
14165741Smrj  * If we have detected that we are running in an HVM environment, we need
14175741Smrj  * to prepend the PV driver directory to the module search path.
14185741Smrj  */
14195741Smrj #define	HVM_MOD_DIR "/platform/i86hvm/kernel"
14205741Smrj static void
14215741Smrj update_default_path()
14225741Smrj {
14235741Smrj 	char *current, *newpath;
14245741Smrj 	int newlen;
14255741Smrj 
14265741Smrj 	/*
14275741Smrj 	 * We are about to resync with krtld.  krtld will reset its
14285741Smrj 	 * internal module search path iff Solaris has set default_path.
14295741Smrj 	 * We want to be sure we're prepending this new directory to the
14305741Smrj 	 * right search path.
14315741Smrj 	 */
14325741Smrj 	current = (default_path == NULL) ? kobj_module_path : default_path;
14335741Smrj 
1434*11971SStuart.Maybee@Sun.COM 	newlen = strlen(HVM_MOD_DIR) + strlen(current) + 2;
14355741Smrj 	newpath = kmem_alloc(newlen, KM_SLEEP);
14365741Smrj 	(void) strcpy(newpath, HVM_MOD_DIR);
14375741Smrj 	(void) strcat(newpath, " ");
14385741Smrj 	(void) strcat(newpath, current);
14395741Smrj 
14405741Smrj 	default_path = newpath;
14415741Smrj }
14425741Smrj #endif
14435741Smrj 
14440Sstevel@tonic-gate static void
14450Sstevel@tonic-gate startup_modules(void)
14460Sstevel@tonic-gate {
14477720SKen.Erickson@Sun.COM 	int cnt;
14480Sstevel@tonic-gate 	extern void prom_setup(void);
14497720SKen.Erickson@Sun.COM 	int32_t v, h;
14507720SKen.Erickson@Sun.COM 	char d[11];
14517720SKen.Erickson@Sun.COM 	char *cp;
14527532SSean.Ye@Sun.COM 	cmi_hdl_t hdl;
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
14555084Sjohnlev 
14565084Sjohnlev #ifndef __xpv
14570Sstevel@tonic-gate 	/*
14580Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
14590Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
14600Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
14610Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
14620Sstevel@tonic-gate 	 */
14630Sstevel@tonic-gate 	microfind();
14645741Smrj 
14659000SStuart.Maybee@Sun.COM 	if (get_hwenv() == HW_XEN_HVM)
14665741Smrj 		update_default_path();
14675084Sjohnlev #endif
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	/*
14700Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
14710Sstevel@tonic-gate 	 */
14723446Smrj 	sgmtl(process_rtc_config_file());
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	/*
14750Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
14760Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
14770Sstevel@tonic-gate 	 */
14780Sstevel@tonic-gate 	param_calc(0);
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	mod_setup();
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	/*
14830Sstevel@tonic-gate 	 * Initialize system parameters.
14840Sstevel@tonic-gate 	 */
14850Sstevel@tonic-gate 	param_init();
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/*
14882712Snn35248 	 * Initialize the default brands
14892712Snn35248 	 */
14902712Snn35248 	brand_init();
14912712Snn35248 
14922712Snn35248 	/*
14930Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
14940Sstevel@tonic-gate 	 */
14950Sstevel@tonic-gate 	maxmem = physmem;
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	/*
14980Sstevel@tonic-gate 	 * Initialize segment management stuff.
14990Sstevel@tonic-gate 	 */
15000Sstevel@tonic-gate 	seg_init();
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
15030Sstevel@tonic-gate 		halt("Can't load specfs");
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
15060Sstevel@tonic-gate 		halt("Can't load devfs");
15070Sstevel@tonic-gate 
15082621Sllai1 	if (modload("fs", "dev") == -1)
15092621Sllai1 		halt("Can't load dev");
15102621Sllai1 
151111173SJonathan.Adams@Sun.COM 	if (modload("fs", "procfs") == -1)
151211173SJonathan.Adams@Sun.COM 		halt("Can't load procfs");
151311173SJonathan.Adams@Sun.COM 
15141676Sjpk 	(void) modloadonly("sys", "lbl_edition");
15151676Sjpk 
15160Sstevel@tonic-gate 	dispinit();
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	/*
15190Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
15200Sstevel@tonic-gate 	 */
15218662SJordan.Vaughan@Sun.com 	if ((h = set_soft_hostid()) == HW_INVALID_HOSTID) {
15227720SKen.Erickson@Sun.COM 		cmn_err(CE_WARN, "Unable to set hostid");
15238662SJordan.Vaughan@Sun.com 	} else {
15247720SKen.Erickson@Sun.COM 		for (v = h, cnt = 0; cnt < 10; cnt++) {
15258662SJordan.Vaughan@Sun.com 			d[cnt] = (char)(v % 10);
15267720SKen.Erickson@Sun.COM 			v /= 10;
15277720SKen.Erickson@Sun.COM 			if (v == 0)
15287720SKen.Erickson@Sun.COM 				break;
15297720SKen.Erickson@Sun.COM 		}
15307720SKen.Erickson@Sun.COM 		for (cp = hw_serial; cnt >= 0; cnt--)
15317720SKen.Erickson@Sun.COM 			*cp++ = d[cnt] + '0';
15327720SKen.Erickson@Sun.COM 		*cp = 0;
15337720SKen.Erickson@Sun.COM 	}
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	/* Read cluster configuration data. */
15360Sstevel@tonic-gate 	clconf_init();
15370Sstevel@tonic-gate 
15385084Sjohnlev #if defined(__xpv)
153910175SStuart.Maybee@Sun.COM 	(void) ec_init();
15405084Sjohnlev 	gnttab_init();
15415084Sjohnlev 	(void) xs_early_init();
15425084Sjohnlev #endif /* __xpv */
15435084Sjohnlev 
15440Sstevel@tonic-gate 	/*
15450Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
15460Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
15470Sstevel@tonic-gate 	 */
15480Sstevel@tonic-gate 	setup_ddi();
15491414Scindi 
155010942STom.Pothier@Sun.COM #ifdef __xpv
155110942STom.Pothier@Sun.COM 	if (DOMAIN_IS_INITDOMAIN(xen_info))
155210942STom.Pothier@Sun.COM #endif
155310942STom.Pothier@Sun.COM 	{
155410942STom.Pothier@Sun.COM 		/*
155510942STom.Pothier@Sun.COM 		 * Load the System Management BIOS into the global ksmbios
155610942STom.Pothier@Sun.COM 		 * handle, if an SMBIOS is present on this system.
155710942STom.Pothier@Sun.COM 		 */
155810942STom.Pothier@Sun.COM 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
155910942STom.Pothier@Sun.COM 	}
156010942STom.Pothier@Sun.COM 
156110942STom.Pothier@Sun.COM 
15627532SSean.Ye@Sun.COM 	/*
15637532SSean.Ye@Sun.COM 	 * Set up the CPU module subsystem for the boot cpu in the native
15647532SSean.Ye@Sun.COM 	 * case, and all physical cpu resource in the xpv dom0 case.
15657532SSean.Ye@Sun.COM 	 * Modifies the device tree, so this must be done after
15667532SSean.Ye@Sun.COM 	 * setup_ddi().
15677532SSean.Ye@Sun.COM 	 */
15687532SSean.Ye@Sun.COM #ifdef __xpv
15697532SSean.Ye@Sun.COM 	/*
15707532SSean.Ye@Sun.COM 	 * If paravirtualized and on dom0 then we initialize all physical
15717532SSean.Ye@Sun.COM 	 * cpu handles now;  if paravirtualized on a domU then do not
15727532SSean.Ye@Sun.COM 	 * initialize.
15737532SSean.Ye@Sun.COM 	 */
15747532SSean.Ye@Sun.COM 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
15757532SSean.Ye@Sun.COM 		xen_mc_lcpu_cookie_t cpi;
15767532SSean.Ye@Sun.COM 
15777532SSean.Ye@Sun.COM 		for (cpi = xen_physcpu_next(NULL); cpi != NULL;
15787532SSean.Ye@Sun.COM 		    cpi = xen_physcpu_next(cpi)) {
15797532SSean.Ye@Sun.COM 			if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
15807532SSean.Ye@Sun.COM 			    xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
15817532SSean.Ye@Sun.COM 			    xen_physcpu_strandid(cpi))) != NULL &&
15827532SSean.Ye@Sun.COM 			    (x86_feature & X86_MCA))
15835254Sgavinm 				cmi_mca_init(hdl);
15845254Sgavinm 		}
15855254Sgavinm 	}
15867532SSean.Ye@Sun.COM #else
15877532SSean.Ye@Sun.COM 	/*
15887532SSean.Ye@Sun.COM 	 * Initialize a handle for the boot cpu - others will initialize
15897532SSean.Ye@Sun.COM 	 * as they startup.  Do not do this if we know we are in an HVM domU.
15907532SSean.Ye@Sun.COM 	 */
15919000SStuart.Maybee@Sun.COM 	if ((get_hwenv() != HW_XEN_HVM) &&
15927532SSean.Ye@Sun.COM 	    (hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
15937532SSean.Ye@Sun.COM 	    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL &&
15947532SSean.Ye@Sun.COM 	    (x86_feature & X86_MCA))
15957532SSean.Ye@Sun.COM 			cmi_mca_init(hdl);
15965254Sgavinm #endif	/* __xpv */
15971414Scindi 
15980Sstevel@tonic-gate 	/*
15990Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
16000Sstevel@tonic-gate 	 */
16013446Smrj 	PRM_POINT("startup_modules: calling prom_setup...");
16020Sstevel@tonic-gate 	prom_setup();
16033446Smrj 	PRM_POINT("startup_modules: done");
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	/*
16060Sstevel@tonic-gate 	 * Load all platform specific modules
16070Sstevel@tonic-gate 	 */
16083446Smrj 	PRM_POINT("startup_modules: calling psm_modload...");
16090Sstevel@tonic-gate 	psm_modload();
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
16120Sstevel@tonic-gate }
16130Sstevel@tonic-gate 
16143446Smrj /*
16153446Smrj  * claim a "setaside" boot page for use in the kernel
16163446Smrj  */
16173446Smrj page_t *
16183446Smrj boot_claim_page(pfn_t pfn)
16190Sstevel@tonic-gate {
16203446Smrj 	page_t *pp;
16213446Smrj 
16223446Smrj 	pp = page_numtopp_nolock(pfn);
16233446Smrj 	ASSERT(pp != NULL);
16240Sstevel@tonic-gate 
16253446Smrj 	if (PP_ISBOOTPAGES(pp)) {
16263446Smrj 		if (pp->p_next != NULL)
16273446Smrj 			pp->p_next->p_prev = pp->p_prev;
16283446Smrj 		if (pp->p_prev == NULL)
16293446Smrj 			bootpages = pp->p_next;
16303446Smrj 		else
16313446Smrj 			pp->p_prev->p_next = pp->p_next;
16323446Smrj 	} else {
16333446Smrj 		/*
16343446Smrj 		 * htable_attach() expects a base pagesize page
16353446Smrj 		 */
16363446Smrj 		if (pp->p_szc != 0)
16373446Smrj 			page_boot_demote(pp);
16383446Smrj 		pp = page_numtopp(pfn, SE_EXCL);
16393446Smrj 	}
16403446Smrj 	return (pp);
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate /*
16440Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
16450Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
16460Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
16470Sstevel@tonic-gate  */
16480Sstevel@tonic-gate static void
16490Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
16500Sstevel@tonic-gate {
16510Sstevel@tonic-gate 	uintptr_t va = low;
16520Sstevel@tonic-gate 	size_t len;
16530Sstevel@tonic-gate 	uint_t prot;
16540Sstevel@tonic-gate 	pfn_t pfn;
16550Sstevel@tonic-gate 	page_t *pp;
16560Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
16570Sstevel@tonic-gate 
16583446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
16590Sstevel@tonic-gate 		if (va + len >= high)
16600Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
16610Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 		while (len > 0) {
16640Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
16650Sstevel@tonic-gate 			if (pp != NULL) {
16660Sstevel@tonic-gate 				if (setaside == 0)
16670Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
16680Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
16690Sstevel@tonic-gate 					    (void *)va, pfn);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 				pp->p_next = bootpages;
16723446Smrj 				pp->p_prev = NULL;
16733446Smrj 				PP_SETBOOTPAGES(pp);
16743446Smrj 				if (bootpages != NULL) {
16753446Smrj 					bootpages->p_prev = pp;
16763446Smrj 				}
16770Sstevel@tonic-gate 				bootpages = pp;
16780Sstevel@tonic-gate 				++boot_protect_cnt;
16790Sstevel@tonic-gate 			}
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 			++pfn;
16820Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
16830Sstevel@tonic-gate 			va += MMU_PAGESIZE;
16840Sstevel@tonic-gate 		}
16850Sstevel@tonic-gate 	}
16860Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
16870Sstevel@tonic-gate }
16880Sstevel@tonic-gate 
16893446Smrj /*
16904828Sjosephb  *
16913446Smrj  */
16920Sstevel@tonic-gate static void
16934828Sjosephb layout_kernel_va(void)
16940Sstevel@tonic-gate {
16954828Sjosephb 	PRM_POINT("layout_kernel_va() starting...");
16960Sstevel@tonic-gate 	/*
16973446Smrj 	 * Establish the final size of the kernel's heap, size of segmap,
16983446Smrj 	 * segkp, etc.
16990Sstevel@tonic-gate 	 */
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate #if defined(__amd64)
17020Sstevel@tonic-gate 
17034828Sjosephb 	kpm_vbase = (caddr_t)segkpm_base;
17044828Sjosephb 	kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
17054828Sjosephb 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
17064828Sjosephb 		panic("not enough room for kpm!");
17073446Smrj 	PRM_DEBUG(kpm_size);
17083446Smrj 	PRM_DEBUG(kpm_vbase);
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	/*
17113446Smrj 	 * By default we create a seg_kp in 64 bit kernels, it's a little
17123446Smrj 	 * faster to access than embedding it in the heap.
17130Sstevel@tonic-gate 	 */
17143446Smrj 	segkp_base = (caddr_t)valloc_base + valloc_sz;
17150Sstevel@tonic-gate 	if (!segkp_fromheap) {
17160Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 		/*
17193446Smrj 		 * determine size of segkp
17200Sstevel@tonic-gate 		 */
17210Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
17220Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
17230Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
17240Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
17250Sstevel@tonic-gate 			    mmu_btop(sz));
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
17300Sstevel@tonic-gate 	}
17313446Smrj 	PRM_DEBUG(segkp_base);
17323446Smrj 	PRM_DEBUG(segkpsize);
17330Sstevel@tonic-gate 
17344828Sjosephb 	/*
17354828Sjosephb 	 * segzio is used for ZFS cached data. It uses a distinct VA
17364828Sjosephb 	 * segment (from kernel heap) so that we can easily tell not to
17374828Sjosephb 	 * include it in kernel crash dumps on 64 bit kernels. The trick is
17384828Sjosephb 	 * to give it lots of VA, but not constrain the kernel heap.
17394828Sjosephb 	 * We scale the size of segzio linearly with physmem up to
17404828Sjosephb 	 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem.
17414828Sjosephb 	 */
17423446Smrj 	segzio_base = segkp_base + mmu_ptob(segkpsize);
17433446Smrj 	if (segzio_fromheap) {
17443446Smrj 		segziosize = 0;
17453446Smrj 	} else {
17464828Sjosephb 		size_t physmem_size = mmu_ptob(physmem);
17474828Sjosephb 		size_t size = (segziosize == 0) ?
17484828Sjosephb 		    physmem_size : mmu_ptob(segziosize);
17493290Sjohansen 
17504828Sjosephb 		if (size < SEGZIOMINSIZE)
17513290Sjohansen 			size = SEGZIOMINSIZE;
17524828Sjosephb 		if (size > SEGZIOMAXSIZE) {
17533552Sjohansen 			size = SEGZIOMAXSIZE;
17544828Sjosephb 			if (physmem_size > size)
17554828Sjosephb 				size += (physmem_size - size) / 2;
17563290Sjohansen 		}
17573290Sjohansen 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
17583290Sjohansen 	}
17593446Smrj 	PRM_DEBUG(segziosize);
17603446Smrj 	PRM_DEBUG(segzio_base);
17613290Sjohansen 
17620Sstevel@tonic-gate 	/*
17633446Smrj 	 * Put the range of VA for device mappings next, kmdb knows to not
17643446Smrj 	 * grep in this range of addresses.
17650Sstevel@tonic-gate 	 */
17663446Smrj 	toxic_addr =
17673446Smrj 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
17680Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
17693446Smrj 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
17703446Smrj #else /* __i386 */
17713446Smrj 	segmap_start = ROUND_UP_LPAGE(kernelbase);
17723446Smrj #endif /* __i386 */
17733446Smrj 	PRM_DEBUG(segmap_start);
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	/*
17767692SAmrita.Sadhukhan@Sun.COM 	 * Users can change segmapsize through eeprom. If the variable
17777692SAmrita.Sadhukhan@Sun.COM 	 * is tuned through eeprom, there is no upper bound on the
17787720SKen.Erickson@Sun.COM 	 * size of segmap.
17790Sstevel@tonic-gate 	 */
17800Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate #if defined(__i386)
17830Sstevel@tonic-gate 	/*
17840Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
17850Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
17863446Smrj 	 * small red zone just below the start of segmap.
17870Sstevel@tonic-gate 	 */
17883446Smrj 	segmap_start += KERNEL_REDZONE_SIZE;
17890Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
17900Sstevel@tonic-gate #endif
17913446Smrj 
17923446Smrj 	PRM_DEBUG(segmap_start);
17933446Smrj 	PRM_DEBUG(segmapsize);
17944828Sjosephb 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
17954828Sjosephb 	PRM_DEBUG(kernelheap);
17964828Sjosephb 	PRM_POINT("layout_kernel_va() done...");
17974828Sjosephb }
17984828Sjosephb 
17994828Sjosephb /*
18004828Sjosephb  * Finish initializing the VM system, now that we are no longer
18014828Sjosephb  * relying on the boot time memory allocators.
18024828Sjosephb  */
18034828Sjosephb static void
18044828Sjosephb startup_vm(void)
18054828Sjosephb {
18064828Sjosephb 	struct segmap_crargs a;
18074828Sjosephb 
18084828Sjosephb 	extern int use_brk_lpg, use_stk_lpg;
18094828Sjosephb 
18104828Sjosephb 	PRM_POINT("startup_vm() starting...");
18114828Sjosephb 
18124828Sjosephb 	/*
18134828Sjosephb 	 * Initialize the hat layer.
18144828Sjosephb 	 */
18154828Sjosephb 	hat_init();
18163446Smrj 
18173446Smrj 	/*
18183446Smrj 	 * Do final allocations of HAT data structures that need to
18193446Smrj 	 * be allocated before quiescing the boot loader.
18203446Smrj 	 */
18213446Smrj 	PRM_POINT("Calling hat_kern_alloc()...");
18223446Smrj 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
18233446Smrj 	PRM_POINT("hat_kern_alloc() done");
18243446Smrj 
18255084Sjohnlev #ifndef __xpv
18263446Smrj 	/*
18275159Sjohnlev 	 * Setup Page Attribute Table
18283446Smrj 	 */
18295159Sjohnlev 	pat_sync();
18305084Sjohnlev #endif
18313446Smrj 
18323446Smrj 	/*
18333446Smrj 	 * The next two loops are done in distinct steps in order
18343446Smrj 	 * to be sure that any page that is doubly mapped (both above
18353446Smrj 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
18363446Smrj 	 * Note this may never happen, but it might someday.
18373446Smrj 	 */
18383446Smrj 	bootpages = NULL;
18393446Smrj 	PRM_POINT("Protecting boot pages");
18400Sstevel@tonic-gate 
18413446Smrj 	/*
18423446Smrj 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
18433446Smrj 	 * page_t's. This can only happen if something weird allocated
18443446Smrj 	 * in this range (like kadb/kmdb).
18453446Smrj 	 */
18463446Smrj 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
18473446Smrj 
18483446Smrj 	/*
18493446Smrj 	 * Before we can take over memory allocation/mapping from the boot
18503446Smrj 	 * loader we must remove from our free page lists any boot allocated
18513446Smrj 	 * pages that stay mapped until release_bootstrap().
18523446Smrj 	 */
18533446Smrj 	protect_boot_range(0, kernelbase, 1);
18543446Smrj 
18555084Sjohnlev 
18563446Smrj 	/*
18573446Smrj 	 * Switch to running on regular HAT (not boot_mmu)
18583446Smrj 	 */
18593446Smrj 	PRM_POINT("Calling hat_kern_setup()...");
18603446Smrj 	hat_kern_setup();
18613446Smrj 
18623446Smrj 	/*
18633446Smrj 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
18643446Smrj 	 */
18653446Smrj 	bop_no_more_mem();
18663446Smrj 
18673446Smrj 	PRM_POINT("hat_kern_setup() done");
18683446Smrj 
18693446Smrj 	hat_cpu_online(CPU);
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	/*
18720Sstevel@tonic-gate 	 * Initialize VM system
18730Sstevel@tonic-gate 	 */
18740Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
18750Sstevel@tonic-gate 	kvm_init();
18760Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	/*
18790Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
18800Sstevel@tonic-gate 	 */
18810Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
18820Sstevel@tonic-gate 		kdi_dvec_vmready();
18830Sstevel@tonic-gate 
18845084Sjohnlev #if defined(__xpv)
18855084Sjohnlev 	/*
18865084Sjohnlev 	 * Populate the I/O pool on domain 0
18875084Sjohnlev 	 */
18885084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
18895084Sjohnlev 		extern long populate_io_pool(void);
18905084Sjohnlev 		long init_io_pool_cnt;
18915084Sjohnlev 
18925084Sjohnlev 		PRM_POINT("Populating reserve I/O page pool");
18935084Sjohnlev 		init_io_pool_cnt = populate_io_pool();
18945084Sjohnlev 		PRM_DEBUG(init_io_pool_cnt);
18955084Sjohnlev 	}
18965084Sjohnlev #endif
18970Sstevel@tonic-gate 	/*
18980Sstevel@tonic-gate 	 * Mangle the brand string etc.
18990Sstevel@tonic-gate 	 */
19000Sstevel@tonic-gate 	cpuid_pass3(CPU);
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate #if defined(__amd64)
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	/*
19050Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
19060Sstevel@tonic-gate 	 */
19070Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
19080Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate #else	/* __i386 */
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	/*
19130Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
19140Sstevel@tonic-gate 	 */
19153446Smrj 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
19160Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
19170Sstevel@tonic-gate 	toxic_bit_map =
19180Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
19190Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
19200Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate #endif	/* __i386 */
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	/*
19260Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
19270Sstevel@tonic-gate 	 * it, tell the debugger.
19280Sstevel@tonic-gate 	 */
19290Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
19300Sstevel@tonic-gate 		kdi_dvec_memavail();
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	/*
19330Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
19340Sstevel@tonic-gate 	 * to work around a pentium bug.
19350Sstevel@tonic-gate 	 */
19365084Sjohnlev #if !defined(__amd64) && !defined(__xpv)
19370Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
19383446Smrj 		desctbr_t idtr;
19390Sstevel@tonic-gate 		gate_desc_t *newidt;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
19420Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
19430Sstevel@tonic-gate 
19445460Sjosephb 		bcopy(idt0, newidt, NIDT * sizeof (*idt0));
19450Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
19468679SSeth.Goldberg@Sun.COM 		    KCS_SEL, SDT_SYSIGT, TRP_KPL, 0);
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
19495460Sjosephb 		    PROT_READ | PROT_EXEC);
19500Sstevel@tonic-gate 
19515460Sjosephb 		CPU->cpu_idt = newidt;
19525460Sjosephb 		idtr.dtr_base = (uintptr_t)CPU->cpu_idt;
19535460Sjosephb 		idtr.dtr_limit = (NIDT * sizeof (*idt0)) - 1;
19543446Smrj 		wr_idtr(&idtr);
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate #endif	/* !__amd64 */
19570Sstevel@tonic-gate 
19585084Sjohnlev #if !defined(__xpv)
19590Sstevel@tonic-gate 	/*
19600Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
19610Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
19620Sstevel@tonic-gate 	 */
19630Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
19640Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
19650Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
19665084Sjohnlev #endif
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
19690Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
19700Sstevel@tonic-gate 
1971841Skchow 	/*
1972841Skchow 	 * disable automatic large pages for small memory systems or
1973841Skchow 	 * when the disable flag is set.
19745349Skchow 	 *
19755349Skchow 	 * Do not yet consider page sizes larger than 2m/4m.
1976841Skchow 	 */
19772991Ssusans 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
19782991Ssusans 		max_uheap_lpsize = LEVEL_SIZE(1);
19792991Ssusans 		max_ustack_lpsize = LEVEL_SIZE(1);
19802991Ssusans 		max_privmap_lpsize = LEVEL_SIZE(1);
19812991Ssusans 		max_uidata_lpsize = LEVEL_SIZE(1);
19822991Ssusans 		max_utext_lpsize = LEVEL_SIZE(1);
19832991Ssusans 		max_shm_lpsize = LEVEL_SIZE(1);
19842991Ssusans 	}
19852991Ssusans 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
19862991Ssusans 	    auto_lpg_disable) {
1987423Sdavemq 		use_brk_lpg = 0;
1988423Sdavemq 		use_stk_lpg = 0;
19892991Ssusans 	}
19905349Skchow 	mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
1991423Sdavemq 
19920Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
19930Sstevel@tonic-gate 	hat_init_finish();
19940Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 	/*
19970Sstevel@tonic-gate 	 * Initialize the segkp segment type.
19980Sstevel@tonic-gate 	 */
19990Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
20003446Smrj 	PRM_POINT("Attaching segkp");
20013446Smrj 	if (segkp_fromheap) {
20020Sstevel@tonic-gate 		segkp->s_as = &kas;
20033446Smrj 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
20043446Smrj 	    segkp) < 0) {
20053446Smrj 		panic("startup: cannot attach segkp");
20063446Smrj 		/*NOTREACHED*/
20070Sstevel@tonic-gate 	}
20083446Smrj 	PRM_POINT("Doing segkp_create()");
20090Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
20100Sstevel@tonic-gate 		panic("startup: segkp_create failed");
20110Sstevel@tonic-gate 		/*NOTREACHED*/
20120Sstevel@tonic-gate 	}
20130Sstevel@tonic-gate 	PRM_DEBUG(segkp);
20140Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	/*
20170Sstevel@tonic-gate 	 * kpm segment
20180Sstevel@tonic-gate 	 */
20190Sstevel@tonic-gate 	segmap_kpm = 0;
20200Sstevel@tonic-gate 	if (kpm_desired) {
20210Sstevel@tonic-gate 		kpm_init();
20220Sstevel@tonic-gate 		kpm_enable = 1;
20230Sstevel@tonic-gate 	}
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 	/*
20260Sstevel@tonic-gate 	 * Now create segmap segment.
20270Sstevel@tonic-gate 	 */
20280Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
20293446Smrj 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
20303446Smrj 		panic("cannot attach segmap");
20310Sstevel@tonic-gate 		/*NOTREACHED*/
20320Sstevel@tonic-gate 	}
20333446Smrj 	PRM_DEBUG(segmap);
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
20360Sstevel@tonic-gate 	a.shmsize = 0;
20370Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
20380Sstevel@tonic-gate 
20393446Smrj 	if (segmap_create(segmap, (caddr_t)&a) != 0)
20403446Smrj 		panic("segmap_create segmap");
20410Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	segdev_init();
20465084Sjohnlev #if defined(__xpv)
20475084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
20485084Sjohnlev #endif
20495084Sjohnlev 		pmem_init();
20503446Smrj 
20510Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
20520Sstevel@tonic-gate }
20530Sstevel@tonic-gate 
20543446Smrj /*
20553446Smrj  * Load a tod module for the non-standard tod part found on this system.
20563446Smrj  */
20573446Smrj static void
20583446Smrj load_tod_module(char *todmod)
20593446Smrj {
20603446Smrj 	if (modload("tod", todmod) == -1)
20613446Smrj 		halt("Can't load TOD module");
20623446Smrj }
20633446Smrj 
20640Sstevel@tonic-gate static void
20650Sstevel@tonic-gate startup_end(void)
20660Sstevel@tonic-gate {
20675107Seota 	int i;
20680Sstevel@tonic-gate 	extern void setx86isalist(void);
20699637SRandy.Fishel@Sun.COM 	extern void cpu_event_init(void);
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	/*
20740Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
20750Sstevel@tonic-gate 	 * initialization has been done but before the clock
20760Sstevel@tonic-gate 	 * and other devices get started.
20770Sstevel@tonic-gate 	 */
20780Sstevel@tonic-gate 	kern_setup1();
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	/*
20810Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
20820Sstevel@tonic-gate 	 */
20830Sstevel@tonic-gate 	kcpc_hw_init(CPU);
20840Sstevel@tonic-gate 
20859637SRandy.Fishel@Sun.COM 	/*
20869637SRandy.Fishel@Sun.COM 	 * Initialize cpu event framework.
20879637SRandy.Fishel@Sun.COM 	 */
20889637SRandy.Fishel@Sun.COM 	cpu_event_init();
20899637SRandy.Fishel@Sun.COM 
20901582Skchow #if defined(OPTERON_WORKAROUND_6323525)
20911582Skchow 	if (opteron_workaround_6323525)
20921582Skchow 		patch_workaround_6323525();
20931582Skchow #endif
20940Sstevel@tonic-gate 	/*
20953446Smrj 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
20963446Smrj 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
20973446Smrj 	 */
20983446Smrj 	if (tod_module_name != NULL) {
20993446Smrj 		PRM_POINT("load_tod_module()");
21003446Smrj 		load_tod_module(tod_module_name);
21013446Smrj 	}
21023446Smrj 
21035084Sjohnlev #if defined(__xpv)
21045084Sjohnlev 	/*
21055084Sjohnlev 	 * Forceload interposing TOD module for the hypervisor.
21065084Sjohnlev 	 */
21075084Sjohnlev 	PRM_POINT("load_tod_module()");
21085084Sjohnlev 	load_tod_module("xpvtod");
21095084Sjohnlev #endif
21105084Sjohnlev 
21113446Smrj 	/*
21120Sstevel@tonic-gate 	 * Configure the system.
21130Sstevel@tonic-gate 	 */
21140Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
21150Sstevel@tonic-gate 	configure();		/* set up devices */
21160Sstevel@tonic-gate 	PRM_POINT("configure() done");
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	/*
21190Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
21200Sstevel@tonic-gate 	 * support.
21210Sstevel@tonic-gate 	 */
21220Sstevel@tonic-gate 	setx86isalist();
21231455Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
21240Sstevel@tonic-gate 	psm_install();
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	/*
21270Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
21280Sstevel@tonic-gate 	 * we're still using bootsvcs.
21290Sstevel@tonic-gate 	 */
21303446Smrj 	PRM_POINT("NULLing out bootops");
21313446Smrj 	*bootopsp = (struct bootops *)NULL;
21320Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
21330Sstevel@tonic-gate 
21345084Sjohnlev #if defined(__xpv)
21355084Sjohnlev 	ec_init_debug_irq();
21365084Sjohnlev 	xs_domu_init();
21375084Sjohnlev #endif
21389940SVikram.Hegde@Sun.COM 
213911600SVikram.Hegde@Sun.COM #if defined(__amd64) && !defined(__xpv)
214011600SVikram.Hegde@Sun.COM 	/*
214111600SVikram.Hegde@Sun.COM 	 * Intel IOMMU has been setup/initialized in ddi_impl.c
214211600SVikram.Hegde@Sun.COM 	 * Start it up now.
214311600SVikram.Hegde@Sun.COM 	 */
214411600SVikram.Hegde@Sun.COM 	immu_startup();
21459940SVikram.Hegde@Sun.COM #endif
214611600SVikram.Hegde@Sun.COM 
21470Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
21480Sstevel@tonic-gate 	(*picinitf)();
21490Sstevel@tonic-gate 	sti();
21505084Sjohnlev #if defined(__xpv)
21515084Sjohnlev 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
21525084Sjohnlev 	xen_late_startup();
21535084Sjohnlev #endif
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
21564828Sjosephb 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
21570Sstevel@tonic-gate 
21585107Seota 	/*
21595107Seota 	 * Register these software interrupts for ddi timer.
21605107Seota 	 * Software interrupts up to the level 10 are supported.
21615107Seota 	 */
21625107Seota 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
21635107Seota 		char name[sizeof ("timer_softintr") + 2];
21645107Seota 		(void) sprintf(name, "timer_softintr%02d", i);
21655107Seota 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
21665107Seota 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
21675107Seota 	}
21685107Seota 
21697730SVikram.Hegde@Sun.COM #if !defined(__xpv)
21707730SVikram.Hegde@Sun.COM 	if (modload("drv", "amd_iommu") < 0) {
21717730SVikram.Hegde@Sun.COM 		PRM_POINT("No AMD IOMMU present\n");
21728215SVikram.Hegde@Sun.COM 	} else if (ddi_hold_installed_driver(ddi_name_to_major(
21738215SVikram.Hegde@Sun.COM 	    "amd_iommu")) == NULL) {
21747730SVikram.Hegde@Sun.COM 		prom_printf("ERROR: failed to attach AMD IOMMU\n");
21757730SVikram.Hegde@Sun.COM 	}
21767730SVikram.Hegde@Sun.COM #endif
21778930SBill.Holler@Sun.COM 	post_startup_cpu_fixups();
21787730SVikram.Hegde@Sun.COM 
21790Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
21800Sstevel@tonic-gate }
21810Sstevel@tonic-gate 
21827942SKen.Erickson@Sun.COM /*
21837942SKen.Erickson@Sun.COM  * Don't remove the following 2 variables.  They are necessary
21847942SKen.Erickson@Sun.COM  * for reading the hostid from the legacy file (/kernel/misc/sysinit).
21857942SKen.Erickson@Sun.COM  */
21860Sstevel@tonic-gate char *_hs1107 = hw_serial;
21870Sstevel@tonic-gate ulong_t  _bdhs34;
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate void
21900Sstevel@tonic-gate post_startup(void)
21910Sstevel@tonic-gate {
21928906SEric.Saxe@Sun.COM 	extern void cpupm_init(cpu_t *);
21939637SRandy.Fishel@Sun.COM 	extern void cpu_event_init_cpu(cpu_t *);
21948906SEric.Saxe@Sun.COM 
21950Sstevel@tonic-gate 	/*
21960Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
21970Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
21980Sstevel@tonic-gate 	 * instruction set extensions.
21990Sstevel@tonic-gate 	 */
22000Sstevel@tonic-gate 	bind_hwcap();
22010Sstevel@tonic-gate 
22025084Sjohnlev #ifdef __xpv
22035084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
22045084Sjohnlev #endif
22055084Sjohnlev 	{
22065084Sjohnlev #if defined(__xpv)
22075084Sjohnlev 		xpv_panic_init();
22085084Sjohnlev #else
22095084Sjohnlev 		/*
22105084Sjohnlev 		 * Startup the memory scrubber.
22115084Sjohnlev 		 * XXPV	This should be running somewhere ..
22125084Sjohnlev 		 */
22139000SStuart.Maybee@Sun.COM 		if (get_hwenv() != HW_XEN_HVM)
22147532SSean.Ye@Sun.COM 			memscrub_init();
22155084Sjohnlev #endif
22165084Sjohnlev 	}
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	/*
22191414Scindi 	 * Complete CPU module initialization
22201414Scindi 	 */
22215254Sgavinm 	cmi_post_startup();
22221414Scindi 
22231414Scindi 	/*
22240Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
22250Sstevel@tonic-gate 	 */
22260Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	/*
22290Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
22300Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
22310Sstevel@tonic-gate 	 */
22320Sstevel@tonic-gate 	(void) modload("fs", "procfs");
22330Sstevel@tonic-gate 
22345129Smarx 	(void) i_ddi_attach_hw_nodes("pit_beep");
22355129Smarx 
22360Sstevel@tonic-gate #if defined(__i386)
22370Sstevel@tonic-gate 	/*
22380Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
22390Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
22400Sstevel@tonic-gate 	 */
22410Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
22420Sstevel@tonic-gate 		halt("No working FP hardware found");
22430Sstevel@tonic-gate #endif
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	maxmem = freemem;
22460Sstevel@tonic-gate 
22479637SRandy.Fishel@Sun.COM 	cpu_event_init_cpu(CPU);
22488906SEric.Saxe@Sun.COM 	cpupm_init(CPU);
22499652SMichael.Corcoran@Sun.COM 	(void) mach_cpu_create_device_node(CPU, NULL);
22508906SEric.Saxe@Sun.COM 
22518906SEric.Saxe@Sun.COM 	pg_init();
22520Sstevel@tonic-gate }
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate static int
22557656SSherry.Moore@Sun.COM pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
22560Sstevel@tonic-gate {
22577656SSherry.Moore@Sun.COM 	return ((pp->p_pagenum >= btop(low_addr)) &&
22587656SSherry.Moore@Sun.COM 	    (pp->p_pagenum < btopr(high_addr)));
22590Sstevel@tonic-gate }
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate void
22620Sstevel@tonic-gate release_bootstrap(void)
22630Sstevel@tonic-gate {
22640Sstevel@tonic-gate 	int root_is_ramdisk;
22650Sstevel@tonic-gate 	page_t *pp;
22660Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
22670Sstevel@tonic-gate 	extern dev_t rootdev;
22687515SSeth.Goldberg@Sun.COM #if !defined(__xpv)
22697515SSeth.Goldberg@Sun.COM 	pfn_t	pfn;
22707515SSeth.Goldberg@Sun.COM #endif
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
22730Sstevel@tonic-gate 	kobj_boot_unmountroot();
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	/*
22760Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
22770Sstevel@tonic-gate 	 */
22780Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
22797656SSherry.Moore@Sun.COM 
22803446Smrj 	clear_boot_mappings(0, _userlimit);
22817656SSherry.Moore@Sun.COM 
22823446Smrj 	postbootkernelbase = kernelbase;
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	/*
22850Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
22860Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
22870Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
22880Sstevel@tonic-gate 	 */
22890Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
22900Sstevel@tonic-gate 	if (!root_is_ramdisk) {
22910Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
22920Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
22930Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
22940Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
22950Sstevel@tonic-gate 	}
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
22980Sstevel@tonic-gate 	while (bootpages) {
22997656SSherry.Moore@Sun.COM 		extern uint64_t ramdisk_start, ramdisk_end;
23000Sstevel@tonic-gate 		pp = bootpages;
23010Sstevel@tonic-gate 		bootpages = pp->p_next;
23027656SSherry.Moore@Sun.COM 
23037656SSherry.Moore@Sun.COM 
23047656SSherry.Moore@Sun.COM 		/* Keep pages for the lower 64K */
23057656SSherry.Moore@Sun.COM 		if (pp_in_range(pp, 0, 0x40000)) {
23067656SSherry.Moore@Sun.COM 			pp->p_next = lower_pages;
23077656SSherry.Moore@Sun.COM 			lower_pages = pp;
23087656SSherry.Moore@Sun.COM 			lower_pages_count++;
23097656SSherry.Moore@Sun.COM 			continue;
23107656SSherry.Moore@Sun.COM 		}
23117656SSherry.Moore@Sun.COM 
23127656SSherry.Moore@Sun.COM 
23137656SSherry.Moore@Sun.COM 		if (root_is_ramdisk && pp_in_range(pp, ramdisk_start,
23147656SSherry.Moore@Sun.COM 		    ramdisk_end)) {
23150Sstevel@tonic-gate 			pp->p_next = rd_pages;
23160Sstevel@tonic-gate 			rd_pages = pp;
23170Sstevel@tonic-gate 			continue;
23180Sstevel@tonic-gate 		}
23190Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
23203446Smrj 		pp->p_prev = (struct page *)0;
23213446Smrj 		PP_CLRBOOTPAGES(pp);
23220Sstevel@tonic-gate 		page_free(pp, 1);
23230Sstevel@tonic-gate 	}
23243446Smrj 	PRM_POINT("Boot pages released");
23250Sstevel@tonic-gate 
23265084Sjohnlev #if !defined(__xpv)
23275084Sjohnlev /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
23280Sstevel@tonic-gate 	/*
23297515SSeth.Goldberg@Sun.COM 	 * Find 1 page below 1 MB so that other processors can boot up or
23307515SSeth.Goldberg@Sun.COM 	 * so that any processor can resume.
23310Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
23320Sstevel@tonic-gate 	 * We should have just free'd one up.
23330Sstevel@tonic-gate 	 */
23347656SSherry.Moore@Sun.COM 
23357656SSherry.Moore@Sun.COM 	/*
23367656SSherry.Moore@Sun.COM 	 * 0x10 pages is 64K.  Leave the bottom 64K alone
23377656SSherry.Moore@Sun.COM 	 * for BIOS.
23387656SSherry.Moore@Sun.COM 	 */
23397656SSherry.Moore@Sun.COM 	for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
23407515SSeth.Goldberg@Sun.COM 		if (page_numtopp_alloc(pfn) == NULL)
23417515SSeth.Goldberg@Sun.COM 			continue;
23427515SSeth.Goldberg@Sun.COM 		rm_platter_va = i86devmap(pfn, 1,
23437515SSeth.Goldberg@Sun.COM 		    PROT_READ | PROT_WRITE | PROT_EXEC);
23447515SSeth.Goldberg@Sun.COM 		rm_platter_pa = ptob(pfn);
23457515SSeth.Goldberg@Sun.COM 		hat_devload(kas.a_hat,
23467515SSeth.Goldberg@Sun.COM 		    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
23477515SSeth.Goldberg@Sun.COM 		    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
23487515SSeth.Goldberg@Sun.COM 		    HAT_LOAD_NOCONSIST);
23497515SSeth.Goldberg@Sun.COM 		break;
23500Sstevel@tonic-gate 	}
23517515SSeth.Goldberg@Sun.COM 	if (pfn == btop(1*1024*1024) && use_mp)
23527515SSeth.Goldberg@Sun.COM 		panic("No page below 1M available for starting "
23537515SSeth.Goldberg@Sun.COM 		    "other processors or for resuming from system-suspend");
23545084Sjohnlev #endif	/* !__xpv */
23550Sstevel@tonic-gate }
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate /*
23580Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
23590Sstevel@tonic-gate  */
23600Sstevel@tonic-gate void
23610Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
23620Sstevel@tonic-gate {
23630Sstevel@tonic-gate 	pp->p_pagenum = pnum;
23640Sstevel@tonic-gate 	pp->p_mapping = NULL;
23650Sstevel@tonic-gate 	pp->p_embed = 0;
23660Sstevel@tonic-gate 	pp->p_share = 0;
23670Sstevel@tonic-gate 	pp->p_mlentry = 0;
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate /*
23710Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
23720Sstevel@tonic-gate  */
23730Sstevel@tonic-gate static pgcnt_t
23740Sstevel@tonic-gate kphysm_init(
23753446Smrj 	page_t *pp,
23760Sstevel@tonic-gate 	pgcnt_t npages)
23770Sstevel@tonic-gate {
23780Sstevel@tonic-gate 	struct memlist	*pmem;
23790Sstevel@tonic-gate 	struct memseg	*cur_memseg;
23800Sstevel@tonic-gate 	pfn_t		base_pfn;
23819732SKit.Chow@Sun.COM 	pfn_t		end_pfn;
23820Sstevel@tonic-gate 	pgcnt_t		num;
23830Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
23840Sstevel@tonic-gate 	uint64_t	addr;
23850Sstevel@tonic-gate 	uint64_t	size;
23860Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
23879732SKit.Chow@Sun.COM 	extern int	mnode_xwa;
23889732SKit.Chow@Sun.COM 	int		ms = 0, me = 0;
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
23910Sstevel@tonic-gate 
23923446Smrj 	cur_memseg = memseg_base;
239311474SJonathan.Adams@Sun.COM 	for (pmem = phys_avail; pmem && npages; pmem = pmem->ml_next) {
23940Sstevel@tonic-gate 		/*
23950Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
23960Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
23970Sstevel@tonic-gate 		 */
239811474SJonathan.Adams@Sun.COM 		addr = pmem->ml_address;
239911474SJonathan.Adams@Sun.COM 		size = pmem->ml_size;
24000Sstevel@tonic-gate 		if (btop(addr) > physmax)
24010Sstevel@tonic-gate 			continue;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 		/*
24040Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
24050Sstevel@tonic-gate 		 */
24060Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
24070Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
24080Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
240911474SJonathan.Adams@Sun.COM 			size -= addr - pmem->ml_address;
24100Sstevel@tonic-gate 		}
24110Sstevel@tonic-gate 
2412810Skchow 		/* only process pages below or equal to physmax */
2413810Skchow 		if ((btop(addr + size) - 1) > physmax)
2414810Skchow 			size = ptob(physmax - btop(addr) + 1);
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 		num = btop(size);
24170Sstevel@tonic-gate 		if (num == 0)
24180Sstevel@tonic-gate 			continue;
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 		if (num > npages)
24210Sstevel@tonic-gate 			num = npages;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 		npages -= num;
24240Sstevel@tonic-gate 		pages_done += num;
24250Sstevel@tonic-gate 		base_pfn = btop(addr);
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate 		if (prom_debug)
24280Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
24290Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
24300Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 		/*
24333446Smrj 		 * Ignore pages below ddiphysmin to simplify ddi memory
24340Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
24350Sstevel@tonic-gate 		 */
24360Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
24373446Smrj 			if (base_pfn + num <= ddiphysmin)
24380Sstevel@tonic-gate 				continue;
24390Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
24400Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
24410Sstevel@tonic-gate 			base_pfn = ddiphysmin;
24420Sstevel@tonic-gate 		}
24433446Smrj 
24440Sstevel@tonic-gate 		/*
24459732SKit.Chow@Sun.COM 		 * mnode_xwa is greater than 1 when large pages regions can
24469732SKit.Chow@Sun.COM 		 * cross memory node boundaries. To prevent the formation
24479732SKit.Chow@Sun.COM 		 * of these large pages, configure the memsegs based on the
24489732SKit.Chow@Sun.COM 		 * memory node ranges which had been made non-contiguous.
24490Sstevel@tonic-gate 		 */
24509732SKit.Chow@Sun.COM 		if (mnode_xwa > 1) {
24519732SKit.Chow@Sun.COM 
24529732SKit.Chow@Sun.COM 			end_pfn = base_pfn + num - 1;
24539732SKit.Chow@Sun.COM 			ms = PFN_2_MEM_NODE(base_pfn);
24549732SKit.Chow@Sun.COM 			me = PFN_2_MEM_NODE(end_pfn);
24559732SKit.Chow@Sun.COM 
24569732SKit.Chow@Sun.COM 			if (ms != me) {
24579732SKit.Chow@Sun.COM 				/*
24589732SKit.Chow@Sun.COM 				 * current range spans more than 1 memory node.
24599732SKit.Chow@Sun.COM 				 * Set num to only the pfn range in the start
24609732SKit.Chow@Sun.COM 				 * memory node.
24619732SKit.Chow@Sun.COM 				 */
24629732SKit.Chow@Sun.COM 				num = mem_node_config[ms].physmax - base_pfn
24639732SKit.Chow@Sun.COM 				    + 1;
24649732SKit.Chow@Sun.COM 				ASSERT(end_pfn > mem_node_config[ms].physmax);
24659732SKit.Chow@Sun.COM 			}
24660Sstevel@tonic-gate 		}
24679732SKit.Chow@Sun.COM 
24689732SKit.Chow@Sun.COM 		for (;;) {
24699732SKit.Chow@Sun.COM 			/*
24709732SKit.Chow@Sun.COM 			 * Build the memsegs entry
24719732SKit.Chow@Sun.COM 			 */
24729732SKit.Chow@Sun.COM 			cur_memseg->pages = pp;
24739732SKit.Chow@Sun.COM 			cur_memseg->epages = pp + num;
24749732SKit.Chow@Sun.COM 			cur_memseg->pages_base = base_pfn;
24759732SKit.Chow@Sun.COM 			cur_memseg->pages_end = base_pfn + num;
24769732SKit.Chow@Sun.COM 
24779732SKit.Chow@Sun.COM 			/*
24789732SKit.Chow@Sun.COM 			 * Insert into memseg list in decreasing pfn range
24799732SKit.Chow@Sun.COM 			 * order. Low memory is typically more fragmented such
24809732SKit.Chow@Sun.COM 			 * that this ordering keeps the larger ranges at the
24819732SKit.Chow@Sun.COM 			 * front of the list for code that searches memseg.
24829732SKit.Chow@Sun.COM 			 * This ASSERTS that the memsegs coming in from boot
24839732SKit.Chow@Sun.COM 			 * are in increasing physical address order and not
24849732SKit.Chow@Sun.COM 			 * contiguous.
24859732SKit.Chow@Sun.COM 			 */
24869732SKit.Chow@Sun.COM 			if (memsegs != NULL) {
24879732SKit.Chow@Sun.COM 				ASSERT(cur_memseg->pages_base >=
24889732SKit.Chow@Sun.COM 				    memsegs->pages_end);
24899732SKit.Chow@Sun.COM 				cur_memseg->next = memsegs;
24909732SKit.Chow@Sun.COM 			}
24919732SKit.Chow@Sun.COM 			memsegs = cur_memseg;
24929732SKit.Chow@Sun.COM 
24939732SKit.Chow@Sun.COM 			/*
24949732SKit.Chow@Sun.COM 			 * add_physmem() initializes the PSM part of the page
24959732SKit.Chow@Sun.COM 			 * struct by calling the PSM back with add_physmem_cb().
24969732SKit.Chow@Sun.COM 			 * In addition it coalesces pages into larger pages as
24979732SKit.Chow@Sun.COM 			 * it initializes them.
24989732SKit.Chow@Sun.COM 			 */
24999732SKit.Chow@Sun.COM 			add_physmem(pp, num, base_pfn);
25009732SKit.Chow@Sun.COM 			cur_memseg++;
25019732SKit.Chow@Sun.COM 			availrmem_initial += num;
25029732SKit.Chow@Sun.COM 			availrmem += num;
25039732SKit.Chow@Sun.COM 
25049732SKit.Chow@Sun.COM 			pp += num;
25059732SKit.Chow@Sun.COM 			if (ms >= me)
25069732SKit.Chow@Sun.COM 				break;
25079732SKit.Chow@Sun.COM 
25089732SKit.Chow@Sun.COM 			/* process next memory node range */
25099732SKit.Chow@Sun.COM 			ms++;
25109732SKit.Chow@Sun.COM 			base_pfn = mem_node_config[ms].physbase;
25119732SKit.Chow@Sun.COM 			num = MIN(mem_node_config[ms].physmax,
25129732SKit.Chow@Sun.COM 			    end_pfn) - base_pfn + 1;
25139732SKit.Chow@Sun.COM 		}
25140Sstevel@tonic-gate 	}
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
25170Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
25180Sstevel@tonic-gate 	PRM_DEBUG(freemem);
25190Sstevel@tonic-gate 	build_pfn_hash();
25200Sstevel@tonic-gate 	return (pages_done);
25210Sstevel@tonic-gate }
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate /*
25240Sstevel@tonic-gate  * Kernel VM initialization.
25250Sstevel@tonic-gate  */
25260Sstevel@tonic-gate static void
25270Sstevel@tonic-gate kvm_init(void)
25280Sstevel@tonic-gate {
25290Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	/*
25320Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
25330Sstevel@tonic-gate 	 */
25340Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
25350Sstevel@tonic-gate 	as_avlinit(&kas);
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
25380Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
25410Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
25420Sstevel@tonic-gate 
25434828Sjosephb 	(void) seg_attach(&kas, kernelheap,
25444828Sjosephb 	    ekernelheap - kernelheap, &kvseg);
25450Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
25460Sstevel@tonic-gate 
25473446Smrj 	if (core_size > 0) {
25483446Smrj 		PRM_POINT("attaching kvseg_core");
25493446Smrj 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
25503446Smrj 		    &kvseg_core);
25513446Smrj 		(void) segkmem_create(&kvseg_core);
25523446Smrj 	}
25533290Sjohansen 
25543446Smrj 	if (segziosize > 0) {
25553446Smrj 		PRM_POINT("attaching segzio");
25563290Sjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
25573290Sjohansen 		    &kzioseg);
25583290Sjohansen 		(void) segkmem_zio_create(&kzioseg);
25593290Sjohansen 
25603290Sjohansen 		/* create zio area covering new segment */
25613290Sjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
25623290Sjohansen 	}
25630Sstevel@tonic-gate 
25643446Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
25650Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	/*
25700Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
25710Sstevel@tonic-gate 	 */
25723446Smrj 	PRM_POINT("protecting redzone");
25730Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 	/*
25760Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
25770Sstevel@tonic-gate 	 */
25780Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
25790Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	/*
25820Sstevel@tonic-gate 	 * Make data writable until end.
25830Sstevel@tonic-gate 	 */
25840Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
25850Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
25860Sstevel@tonic-gate }
25870Sstevel@tonic-gate 
25885084Sjohnlev #ifndef __xpv
25890Sstevel@tonic-gate /*
25905159Sjohnlev  * Solaris adds an entry for Write Combining caching to the PAT
25910Sstevel@tonic-gate  */
25925159Sjohnlev static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate void
25955159Sjohnlev pat_sync(void)
25960Sstevel@tonic-gate {
25975159Sjohnlev 	ulong_t	cr0, cr0_orig, cr4;
25980Sstevel@tonic-gate 
25995159Sjohnlev 	if (!(x86_feature & X86_PAT))
26000Sstevel@tonic-gate 		return;
26015159Sjohnlev 	cr0_orig = cr0 = getcr0();
26025159Sjohnlev 	cr4 = getcr4();
26030Sstevel@tonic-gate 
26045159Sjohnlev 	/* disable caching and flush all caches and TLBs */
26055159Sjohnlev 	cr0 |= CR0_CD;
26065159Sjohnlev 	cr0 &= ~CR0_NW;
26075159Sjohnlev 	setcr0(cr0);
26085159Sjohnlev 	invalidate_cache();
26095159Sjohnlev 	if (cr4 & CR4_PGE) {
26105159Sjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
26115159Sjohnlev 		setcr4(cr4);
26125159Sjohnlev 	} else {
26135159Sjohnlev 		reload_cr3();
26140Sstevel@tonic-gate 	}
26150Sstevel@tonic-gate 
26165159Sjohnlev 	/* add our entry to the PAT */
26175159Sjohnlev 	wrmsr(REG_PAT, pat_attr_reg);
2618770Skucharsk 
26195159Sjohnlev 	/* flush TLBs and cache again, then reenable cr0 caching */
26205159Sjohnlev 	if (cr4 & CR4_PGE) {
26215159Sjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
26225159Sjohnlev 		setcr4(cr4);
26235159Sjohnlev 	} else {
26245159Sjohnlev 		reload_cr3();
26250Sstevel@tonic-gate 	}
26260Sstevel@tonic-gate 	invalidate_cache();
26270Sstevel@tonic-gate 	setcr0(cr0_orig);
26280Sstevel@tonic-gate }
26290Sstevel@tonic-gate 
26305159Sjohnlev #endif /* !__xpv */
26310Sstevel@tonic-gate 
26327720SKen.Erickson@Sun.COM #if defined(_SOFT_HOSTID)
26337720SKen.Erickson@Sun.COM /*
26347720SKen.Erickson@Sun.COM  * On platforms that do not have a hardware serial number, attempt
26357720SKen.Erickson@Sun.COM  * to set one based on the contents of /etc/hostid.  If this file does
26367720SKen.Erickson@Sun.COM  * not exist, assume that we are to generate a new hostid and set
26377720SKen.Erickson@Sun.COM  * it in the kernel, for subsequent saving by a userland process
26387720SKen.Erickson@Sun.COM  * once the system is up and the root filesystem is mounted r/w.
26397720SKen.Erickson@Sun.COM  *
26407942SKen.Erickson@Sun.COM  * In order to gracefully support upgrade on OpenSolaris, if
26417942SKen.Erickson@Sun.COM  * /etc/hostid does not exist, we will attempt to get a serial number
26427942SKen.Erickson@Sun.COM  * using the legacy method (/kernel/misc/sysinit).
26437942SKen.Erickson@Sun.COM  *
26447720SKen.Erickson@Sun.COM  * In an attempt to make the hostid less prone to abuse
26457720SKen.Erickson@Sun.COM  * (for license circumvention, etc), we store it in /etc/hostid
26467720SKen.Erickson@Sun.COM  * in rot47 format.
26477720SKen.Erickson@Sun.COM  */
26487720SKen.Erickson@Sun.COM extern volatile unsigned long tenmicrodata;
26497942SKen.Erickson@Sun.COM static int atoi(char *);
26507720SKen.Erickson@Sun.COM 
26517720SKen.Erickson@Sun.COM static int32_t
26527720SKen.Erickson@Sun.COM set_soft_hostid(void)
26537720SKen.Erickson@Sun.COM {
26547720SKen.Erickson@Sun.COM 	struct _buf *file;
26557720SKen.Erickson@Sun.COM 	char tokbuf[MAXNAMELEN];
26567720SKen.Erickson@Sun.COM 	token_t token;
26577720SKen.Erickson@Sun.COM 	int done = 0;
26587720SKen.Erickson@Sun.COM 	u_longlong_t tmp;
26597942SKen.Erickson@Sun.COM 	int i;
26608662SJordan.Vaughan@Sun.com 	int32_t hostid = (int32_t)HW_INVALID_HOSTID;
26617720SKen.Erickson@Sun.COM 	unsigned char *c;
26627720SKen.Erickson@Sun.COM 	hrtime_t tsc;
26637720SKen.Erickson@Sun.COM 
26647720SKen.Erickson@Sun.COM 	/*
26657720SKen.Erickson@Sun.COM 	 * If /etc/hostid file not found, we'd like to get a pseudo
26667720SKen.Erickson@Sun.COM 	 * random number to use at the hostid.  A nice way to do this
26677720SKen.Erickson@Sun.COM 	 * is to read the real time clock.  To remain xen-compatible,
26687720SKen.Erickson@Sun.COM 	 * we can't poke the real hardware, so we use tsc_read() to
26697720SKen.Erickson@Sun.COM 	 * read the real time clock.  However, there is an ominous
26707720SKen.Erickson@Sun.COM 	 * warning in tsc_read that says it can return zero, so we
26717720SKen.Erickson@Sun.COM 	 * deal with that possibility by falling back to using the
26727720SKen.Erickson@Sun.COM 	 * (hopefully random enough) value in tenmicrodata.
26737720SKen.Erickson@Sun.COM 	 */
26747720SKen.Erickson@Sun.COM 
26757720SKen.Erickson@Sun.COM 	if ((file = kobj_open_file(hostid_file)) == (struct _buf *)-1) {
26767942SKen.Erickson@Sun.COM 		/*
26777942SKen.Erickson@Sun.COM 		 * hostid file not found - try to load sysinit module
26787942SKen.Erickson@Sun.COM 		 * and see if it has a nonzero hostid value...use that
26797942SKen.Erickson@Sun.COM 		 * instead of generating a new hostid here if so.
26807942SKen.Erickson@Sun.COM 		 */
26817942SKen.Erickson@Sun.COM 		if ((i = modload("misc", "sysinit")) != -1) {
26827942SKen.Erickson@Sun.COM 			if (strlen(hw_serial) > 0)
26837942SKen.Erickson@Sun.COM 				hostid = (int32_t)atoi(hw_serial);
26847942SKen.Erickson@Sun.COM 			(void) modunload(i);
26857942SKen.Erickson@Sun.COM 		}
26868662SJordan.Vaughan@Sun.com 		if (hostid == HW_INVALID_HOSTID) {
26877942SKen.Erickson@Sun.COM 			tsc = tsc_read();
26887942SKen.Erickson@Sun.COM 			if (tsc == 0)	/* tsc_read can return zero sometimes */
26897942SKen.Erickson@Sun.COM 				hostid = (int32_t)tenmicrodata & 0x0CFFFFF;
26907942SKen.Erickson@Sun.COM 			else
26917942SKen.Erickson@Sun.COM 				hostid = (int32_t)tsc & 0x0CFFFFF;
26927942SKen.Erickson@Sun.COM 		}
26937720SKen.Erickson@Sun.COM 	} else {
26947720SKen.Erickson@Sun.COM 		/* hostid file found */
26957720SKen.Erickson@Sun.COM 		while (!done) {
26967720SKen.Erickson@Sun.COM 			token = kobj_lex(file, tokbuf, sizeof (tokbuf));
26977720SKen.Erickson@Sun.COM 
26987720SKen.Erickson@Sun.COM 			switch (token) {
26997720SKen.Erickson@Sun.COM 			case POUND:
27007720SKen.Erickson@Sun.COM 				/*
27017720SKen.Erickson@Sun.COM 				 * skip comments
27027720SKen.Erickson@Sun.COM 				 */
27037720SKen.Erickson@Sun.COM 				kobj_find_eol(file);
27047720SKen.Erickson@Sun.COM 				break;
27057720SKen.Erickson@Sun.COM 			case STRING:
27067720SKen.Erickson@Sun.COM 				/*
27077720SKen.Erickson@Sun.COM 				 * un-rot47 - obviously this
27087720SKen.Erickson@Sun.COM 				 * nonsense is ascii-specific
27097720SKen.Erickson@Sun.COM 				 */
27107720SKen.Erickson@Sun.COM 				for (c = (unsigned char *)tokbuf;
27117720SKen.Erickson@Sun.COM 				    *c != '\0'; c++) {
27127720SKen.Erickson@Sun.COM 					*c += 47;
27137720SKen.Erickson@Sun.COM 					if (*c > '~')
27147720SKen.Erickson@Sun.COM 						*c -= 94;
27157720SKen.Erickson@Sun.COM 					else if (*c < '!')
27167720SKen.Erickson@Sun.COM 						*c += 94;
27177720SKen.Erickson@Sun.COM 				}
27187720SKen.Erickson@Sun.COM 				/*
27197720SKen.Erickson@Sun.COM 				 * now we should have a real number
27207720SKen.Erickson@Sun.COM 				 */
27217720SKen.Erickson@Sun.COM 
27227720SKen.Erickson@Sun.COM 				if (kobj_getvalue(tokbuf, &tmp) != 0)
27237720SKen.Erickson@Sun.COM 					kobj_file_err(CE_WARN, file,
27247720SKen.Erickson@Sun.COM 					    "Bad value %s for hostid",
27257720SKen.Erickson@Sun.COM 					    tokbuf);
27267720SKen.Erickson@Sun.COM 				else
27277720SKen.Erickson@Sun.COM 					hostid = (int32_t)tmp;
27287720SKen.Erickson@Sun.COM 
27297720SKen.Erickson@Sun.COM 				break;
27307720SKen.Erickson@Sun.COM 			case EOF:
27317720SKen.Erickson@Sun.COM 				done = 1;
27327720SKen.Erickson@Sun.COM 				/* FALLTHROUGH */
27337720SKen.Erickson@Sun.COM 			case NEWLINE:
27347720SKen.Erickson@Sun.COM 				kobj_newline(file);
27357720SKen.Erickson@Sun.COM 				break;
27367720SKen.Erickson@Sun.COM 			default:
27377720SKen.Erickson@Sun.COM 				break;
27387720SKen.Erickson@Sun.COM 
27397720SKen.Erickson@Sun.COM 			}
27407720SKen.Erickson@Sun.COM 		}
27418662SJordan.Vaughan@Sun.com 		if (hostid == HW_INVALID_HOSTID) /* didn't find a hostid */
27427720SKen.Erickson@Sun.COM 			kobj_file_err(CE_WARN, file,
27437720SKen.Erickson@Sun.COM 			    "hostid missing or corrupt");
27447720SKen.Erickson@Sun.COM 
27457720SKen.Erickson@Sun.COM 		kobj_close_file(file);
27467720SKen.Erickson@Sun.COM 	}
27477720SKen.Erickson@Sun.COM 	/*
27487720SKen.Erickson@Sun.COM 	 * hostid is now the value read from /etc/hostid, or the
27498662SJordan.Vaughan@Sun.com 	 * new hostid we generated in this routine or HW_INVALID_HOSTID if not
27508662SJordan.Vaughan@Sun.com 	 * set.
27517720SKen.Erickson@Sun.COM 	 */
27527720SKen.Erickson@Sun.COM 	return (hostid);
27537720SKen.Erickson@Sun.COM }
27547942SKen.Erickson@Sun.COM 
27557942SKen.Erickson@Sun.COM static int
27567942SKen.Erickson@Sun.COM atoi(char *p)
27577942SKen.Erickson@Sun.COM {
27587942SKen.Erickson@Sun.COM 	int i = 0;
27597942SKen.Erickson@Sun.COM 
27607942SKen.Erickson@Sun.COM 	while (*p != '\0')
27617942SKen.Erickson@Sun.COM 		i = 10 * i + (*p++ - '0');
27627942SKen.Erickson@Sun.COM 
27637942SKen.Erickson@Sun.COM 	return (i);
27647942SKen.Erickson@Sun.COM }
27657942SKen.Erickson@Sun.COM 
27667720SKen.Erickson@Sun.COM #endif /* _SOFT_HOSTID */
27677720SKen.Erickson@Sun.COM 
27680Sstevel@tonic-gate void
27691676Sjpk get_system_configuration(void)
27700Sstevel@tonic-gate {
27710Sstevel@tonic-gate 	char	prop[32];
27720Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
27730Sstevel@tonic-gate 
27744828Sjosephb 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
27754828Sjosephb 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
27764828Sjosephb 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
27774828Sjosephb 	    nodes_ll > MAXNODES ||
27784828Sjosephb 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
27794828Sjosephb 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
27804828Sjosephb 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
27810Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
27820Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
27830Sstevel@tonic-gate 	} else {
27840Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
27850Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
27860Sstevel@tonic-gate 	}
27874828Sjosephb 
27884828Sjosephb 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
27894828Sjosephb 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
27904828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
27914828Sjosephb 		eprom_kernelbase = NULL;
27920Sstevel@tonic-gate 	else
27934828Sjosephb 		eprom_kernelbase = (uintptr_t)lvalue;
27940Sstevel@tonic-gate 
27954828Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
27964828Sjosephb 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
27974828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
27980Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
27994828Sjosephb 	else
28000Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
28010Sstevel@tonic-gate 
28024828Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
28034828Sjosephb 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
28044828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
28050Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
28064828Sjosephb 	else
28070Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
28081417Skchow 
28094004Sjosephb 	/* physmem used to be here, but moved much earlier to fakebop.c */
28100Sstevel@tonic-gate }
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate /*
28130Sstevel@tonic-gate  * Add to a memory list.
28140Sstevel@tonic-gate  * start = start of new memory segment
28150Sstevel@tonic-gate  * len = length of new memory segment in bytes
28160Sstevel@tonic-gate  * new = pointer to a new struct memlist
28170Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
28180Sstevel@tonic-gate  */
28193446Smrj void
28200Sstevel@tonic-gate memlist_add(
28210Sstevel@tonic-gate 	uint64_t start,
28220Sstevel@tonic-gate 	uint64_t len,
28230Sstevel@tonic-gate 	struct memlist *new,
28240Sstevel@tonic-gate 	struct memlist **memlistp)
28250Sstevel@tonic-gate {
28260Sstevel@tonic-gate 	struct memlist *cur;
28270Sstevel@tonic-gate 	uint64_t end = start + len;
28280Sstevel@tonic-gate 
282911474SJonathan.Adams@Sun.COM 	new->ml_address = start;
283011474SJonathan.Adams@Sun.COM 	new->ml_size = len;
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 	cur = *memlistp;
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 	while (cur) {
283511474SJonathan.Adams@Sun.COM 		if (cur->ml_address >= end) {
283611474SJonathan.Adams@Sun.COM 			new->ml_next = cur;
28370Sstevel@tonic-gate 			*memlistp = new;
283811474SJonathan.Adams@Sun.COM 			new->ml_prev = cur->ml_prev;
283911474SJonathan.Adams@Sun.COM 			cur->ml_prev = new;
28400Sstevel@tonic-gate 			return;
28410Sstevel@tonic-gate 		}
284211474SJonathan.Adams@Sun.COM 		ASSERT(cur->ml_address + cur->ml_size <= start);
284311474SJonathan.Adams@Sun.COM 		if (cur->ml_next == NULL) {
284411474SJonathan.Adams@Sun.COM 			cur->ml_next = new;
284511474SJonathan.Adams@Sun.COM 			new->ml_prev = cur;
284611474SJonathan.Adams@Sun.COM 			new->ml_next = NULL;
28470Sstevel@tonic-gate 			return;
28480Sstevel@tonic-gate 		}
284911474SJonathan.Adams@Sun.COM 		memlistp = &cur->ml_next;
285011474SJonathan.Adams@Sun.COM 		cur = cur->ml_next;
28510Sstevel@tonic-gate 	}
28520Sstevel@tonic-gate }
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate void
28550Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
28560Sstevel@tonic-gate {
28570Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
28580Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
28610Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
28620Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
28630Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
28640Sstevel@tonic-gate }
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate caddr_t
28670Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
28680Sstevel@tonic-gate {
28690Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
28700Sstevel@tonic-gate }
28710Sstevel@tonic-gate 
28720Sstevel@tonic-gate /*ARGSUSED*/
28730Sstevel@tonic-gate caddr_t
28740Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
28750Sstevel@tonic-gate {
28760Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
28770Sstevel@tonic-gate 	/*NOTREACHED*/
28780Sstevel@tonic-gate 	return (0);
28790Sstevel@tonic-gate }
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate /*ARGSUSED*/
28820Sstevel@tonic-gate void
28830Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
28840Sstevel@tonic-gate {
28850Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
28860Sstevel@tonic-gate }
28870Sstevel@tonic-gate 
28880Sstevel@tonic-gate /*
28890Sstevel@tonic-gate  * This is called just after configure() in startup().
28900Sstevel@tonic-gate  *
28910Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
28920Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
28930Sstevel@tonic-gate  * given that various parts of the instruction set may appear
28940Sstevel@tonic-gate  * and disappear between different implementations.
28950Sstevel@tonic-gate  *
28960Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
28970Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
28980Sstevel@tonic-gate  * more flexibility.
28990Sstevel@tonic-gate  *
29000Sstevel@tonic-gate  * So, we just leave this alone.
29010Sstevel@tonic-gate  */
29020Sstevel@tonic-gate void
29030Sstevel@tonic-gate setx86isalist(void)
29040Sstevel@tonic-gate {
29050Sstevel@tonic-gate 	char *tp;
29060Sstevel@tonic-gate 	size_t len;
29070Sstevel@tonic-gate 	extern char *isa_list;
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate #define	TBUFSIZE	1024
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
29120Sstevel@tonic-gate 	*tp = '\0';
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate #if defined(__amd64)
29150Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
29160Sstevel@tonic-gate #endif
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	switch (x86_vendor) {
29190Sstevel@tonic-gate 	case X86_VENDOR_Intel:
29200Sstevel@tonic-gate 	case X86_VENDOR_AMD:
29210Sstevel@tonic-gate 	case X86_VENDOR_TM:
29220Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
29230Sstevel@tonic-gate 			/*
29240Sstevel@tonic-gate 			 * Pentium Pro or later
29250Sstevel@tonic-gate 			 */
29260Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
29270Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
29280Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
29290Sstevel@tonic-gate 		}
29300Sstevel@tonic-gate 		/*FALLTHROUGH*/
29310Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
29320Sstevel@tonic-gate 		/*
29330Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
29340Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
29350Sstevel@tonic-gate 		 */
29360Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
29370Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
29380Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
29390Sstevel@tonic-gate 			    "+mmx pentium " : " ");
29400Sstevel@tonic-gate 		}
29410Sstevel@tonic-gate 		break;
29420Sstevel@tonic-gate 	default:
29430Sstevel@tonic-gate 		break;
29440Sstevel@tonic-gate 	}
29450Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
29460Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
29470Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
29480Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate #undef TBUFSIZE
29510Sstevel@tonic-gate }
29520Sstevel@tonic-gate 
29530Sstevel@tonic-gate 
29540Sstevel@tonic-gate #ifdef __amd64
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate void *
29570Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
29580Sstevel@tonic-gate {
29590Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
29600Sstevel@tonic-gate }
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate void
29630Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
29640Sstevel@tonic-gate {
29650Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
29660Sstevel@tonic-gate }
29670Sstevel@tonic-gate 
29683446Smrj #else /* __i386 */
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate void *
29710Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
29720Sstevel@tonic-gate {
29730Sstevel@tonic-gate 	caddr_t	vaddr;
29740Sstevel@tonic-gate 	uintptr_t v;
29750Sstevel@tonic-gate 	size_t	start;
29760Sstevel@tonic-gate 	size_t	end;
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
29790Sstevel@tonic-gate 	if (vaddr == NULL)
29800Sstevel@tonic-gate 		return (NULL);
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
29830Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
29843446Smrj 	ASSERT(v + size <= valloc_base);
29850Sstevel@tonic-gate 
29860Sstevel@tonic-gate 	start = btop(v - kernelbase);
29870Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
29880Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
29890Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate 	while (start <= end) {
29920Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
29930Sstevel@tonic-gate 		++start;
29940Sstevel@tonic-gate 	}
29950Sstevel@tonic-gate 	return (vaddr);
29960Sstevel@tonic-gate }
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate void
29990Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
30000Sstevel@tonic-gate {
30010Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
30020Sstevel@tonic-gate 	size_t	start;
30030Sstevel@tonic-gate 	size_t	end;
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
30063446Smrj 	ASSERT(v + size <= valloc_base);
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate 	start = btop(v - kernelbase);
30090Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
30100Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
30110Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
30120Sstevel@tonic-gate 
30130Sstevel@tonic-gate 	while (start <= end) {
30140Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
30150Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
30160Sstevel@tonic-gate 		++start;
30170Sstevel@tonic-gate 	}
30180Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
30190Sstevel@tonic-gate }
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate /*
30220Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
30230Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
30240Sstevel@tonic-gate  */
30250Sstevel@tonic-gate void *
30260Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
30270Sstevel@tonic-gate {
30280Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
30290Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
30300Sstevel@tonic-gate 	size_t start;
30310Sstevel@tonic-gate 	size_t end;
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate 	/*
30340Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
30350Sstevel@tonic-gate 	 */
30360Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
30370Sstevel@tonic-gate 		return (NULL);
30380Sstevel@tonic-gate 
30390Sstevel@tonic-gate 	/*
30400Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
30410Sstevel@tonic-gate 	 */
30423446Smrj 	if (v >= valloc_base || eaddr < kernelbase)
30430Sstevel@tonic-gate 		return (NULL);
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	/*
30460Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
30470Sstevel@tonic-gate 	 */
30480Sstevel@tonic-gate 	if (v < kernelbase)
30490Sstevel@tonic-gate 		v = kernelbase;
30500Sstevel@tonic-gate 	start = btop(v - kernelbase);
30510Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
30520Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
30530Sstevel@tonic-gate 		end = toxic_bit_map_len;
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
30560Sstevel@tonic-gate 		return (NULL);
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 	v = kernelbase + ptob(start);
30590Sstevel@tonic-gate 	if (len != NULL)
30600Sstevel@tonic-gate 		*len = ptob(end - start);
30610Sstevel@tonic-gate 	return ((void *)v);
30620Sstevel@tonic-gate }
30630Sstevel@tonic-gate 
30643446Smrj #endif	/* __i386 */
3065