xref: /onnv-gate/usr/src/uts/i86pc/os/startup.c (revision 7720:8b0e0a8b0931)
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 /*
226336Sbholler  * Copyright 2008 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>
1110Sstevel@tonic-gate #include <sys/x86_archext.h>
1121414Scindi #include <sys/cpu_module.h>
113437Smws #include <sys/smbios.h>
1143446Smrj #include <sys/debug_info.h>
1155159Sjohnlev #include <sys/bootinfo.h>
1165107Seota #include <sys/ddi_timer.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);
1360Sstevel@tonic-gate extern void progressbar_start(void);
1372712Snn35248 extern void brand_init(void);
1386880Sdv142724 extern void pcf_init(void);
1390Sstevel@tonic-gate 
1404878Sblakej extern int size_pse_array(pgcnt_t, int);
1414878Sblakej 
142*7720SKen.Erickson@Sun.COM #if defined(_SOFT_HOSTID)
143*7720SKen.Erickson@Sun.COM 
144*7720SKen.Erickson@Sun.COM #include <sys/rtc.h>
145*7720SKen.Erickson@Sun.COM 
146*7720SKen.Erickson@Sun.COM static int32_t set_soft_hostid(void);
147*7720SKen.Erickson@Sun.COM extern char hw_serial[];
148*7720SKen.Erickson@Sun.COM static char hostid_file[] = "/etc/hostid";
149*7720SKen.Erickson@Sun.COM 
150*7720SKen.Erickson@Sun.COM #endif
151*7720SKen.Erickson@Sun.COM 
152*7720SKen.Erickson@Sun.COM 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1550Sstevel@tonic-gate  * interface.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate  * segkp
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate extern int segkp_fromheap;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static void kvm_init(void);
1650Sstevel@tonic-gate static void startup_init(void);
1660Sstevel@tonic-gate static void startup_memlist(void);
1673446Smrj static void startup_kmem(void);
1680Sstevel@tonic-gate static void startup_modules(void);
1690Sstevel@tonic-gate static void startup_vm(void);
1700Sstevel@tonic-gate static void startup_end(void);
1714828Sjosephb static void layout_kernel_va(void);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1750Sstevel@tonic-gate  */
1761479Skchow #ifdef __i386
1774828Sjosephb 
1781479Skchow /*
1791479Skchow  * Due to virtual address space limitations running in 32 bit mode, restrict
1804828Sjosephb  * the amount of physical memory configured to a max of PHYSMEM pages (16g).
1811479Skchow  *
1821479Skchow  * If the physical max memory size of 64g were allowed to be configured, the
1831479Skchow  * size of user virtual address space will be less than 1g. A limited user
1841479Skchow  * address space greatly reduces the range of applications that can run.
1851479Skchow  *
1864828Sjosephb  * If more physical memory than PHYSMEM is required, users should preferably
1874828Sjosephb  * run in 64 bit mode which has far looser virtual address space limitations.
1881479Skchow  *
1891479Skchow  * If 64 bit mode is not available (as in IA32) and/or more physical memory
1904828Sjosephb  * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
1911479Skchow  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
1921479Skchow  * should also be carefully tuned to balance out the need of the user
1931479Skchow  * application while minimizing the risk of kernel heap exhaustion due to
1941479Skchow  * kernelbase being set too high.
1951479Skchow  */
1964828Sjosephb #define	PHYSMEM	0x400000
1974828Sjosephb 
1984828Sjosephb #else /* __amd64 */
1991479Skchow 
2004828Sjosephb /*
2014828Sjosephb  * For now we can handle memory with physical addresses up to about
2024828Sjosephb  * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
2034828Sjosephb  * half the VA space for seg_kpm. When systems get bigger than 64TB this
2044828Sjosephb  * code will need revisiting. There is an implicit assumption that there
2054828Sjosephb  * are no *huge* holes in the physical address space too.
2064828Sjosephb  */
2074828Sjosephb #define	TERABYTE		(1ul << 40)
2084828Sjosephb #define	PHYSMEM_MAX64		mmu_btop(64 * TERABYTE)
2094828Sjosephb #define	PHYSMEM			PHYSMEM_MAX64
2104828Sjosephb #define	AMD64_VA_HOLE_END	0xFFFF800000000000ul
2114828Sjosephb 
2124828Sjosephb #endif /* __amd64 */
2134828Sjosephb 
2144828Sjosephb pgcnt_t physmem = PHYSMEM;
2150Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate char *kobj_file_buf;
2180Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
2217656SSherry.Moore@Sun.COM caddr_t	rm_platter_va = 0;
2220Sstevel@tonic-gate uint32_t rm_platter_pa;
2230Sstevel@tonic-gate 
224841Skchow int	auto_lpg_disable = 1;
225841Skchow 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
2280Sstevel@tonic-gate  */
2290Sstevel@tonic-gate uintptr_t hole_start, hole_end;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate  * kpm mapping window
2330Sstevel@tonic-gate  */
2340Sstevel@tonic-gate caddr_t kpm_vbase;
2350Sstevel@tonic-gate size_t  kpm_size;
2364828Sjosephb static int kpm_desired;
2374828Sjosephb #ifdef __amd64
2384828Sjosephb static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
2394828Sjosephb #endif
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * Configuration parameters set at boot time.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
2480Sstevel@tonic-gate struct bootops		**bootopsp;
2490Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate char bootblock_fstype[16];
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
2547656SSherry.Moore@Sun.COM char kern_bootfile[OBP_MAXPATHLEN];
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2573290Sjohansen  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
2583290Sjohansen  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
2593290Sjohansen  * zio buffers from their own segment, otherwise they are allocated from the
2603290Sjohansen  * heap.  The optimization of allocating zio buffers from their own segment is
2613290Sjohansen  * only valid on 64-bit kernels.
2623290Sjohansen  */
2633290Sjohansen #if defined(__amd64)
2643290Sjohansen int segzio_fromheap = 0;
2653290Sjohansen #else
2663290Sjohansen int segzio_fromheap = 1;
2673290Sjohansen #endif
2683290Sjohansen 
2693290Sjohansen /*
2700Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
2710Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2720Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2730Sstevel@tonic-gate  */
2740Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate  * VM data structures
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
2800Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2810Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
2824878Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
2834878Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
2844878Sblakej int pse_shift;			/* log2(pse_table_size) */
2850Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2860Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
2870Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
2880Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
2890Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
2923446Smrj static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
2933446Smrj 
2940Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate #if defined(__amd64)
2970Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
2980Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
2990Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
3000Sstevel@tonic-gate #else
3010Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
3020Sstevel@tonic-gate #endif
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
3053290Sjohansen caddr_t segzio_base;		/* Base address of segzio */
3060Sstevel@tonic-gate #if defined(__amd64)
3070Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
3080Sstevel@tonic-gate #else
3090Sstevel@tonic-gate pgcnt_t segkpsize = 0;
3100Sstevel@tonic-gate #endif
3113290Sjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
3120Sstevel@tonic-gate 
3133446Smrj /*
3143446Smrj  * VA range available to the debugger
3153446Smrj  */
3163446Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
3173446Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
3183446Smrj 
3190Sstevel@tonic-gate struct memseg *memseg_base;
3200Sstevel@tonic-gate struct vnode unused_pages_vp;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate #define	FOURGB	0x100000000LL
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate struct memlist *memlist;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
3270Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
3280Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
3290Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
3300Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
3310Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
3320Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
3330Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
3360Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
3400Sstevel@tonic-gate  */
3413446Smrj static pgcnt_t kphysm_init(page_t *, pgcnt_t);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * a couple useful roundup macros
3470Sstevel@tonic-gate  */
3480Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
3490Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
3500Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
3510Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
3520Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
3533446Smrj 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
3540Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
3550Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
3590Sstevel@tonic-gate  *		+-----------------------+
3603446Smrj  *		|			|
3610Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
3620Sstevel@tonic-gate  *		|	debugger	|
3630Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
3640Sstevel@tonic-gate  *		|      Kernel Data	|
3650Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
3660Sstevel@tonic-gate  *              |      Kernel Text	|
3675084Sjohnlev  * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
3683446Smrj  *		|---       GDT       ---|- GDT page (GDT_VA)
3693446Smrj  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
3703446Smrj  *		|			|
3713446Smrj  * 		|   page_t structures	|
3720Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
3730Sstevel@tonic-gate  * 		|   page hash, etc.	|
3743446Smrj  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
3753446Smrj  *		|			|  (segkp is just an arena in the heap)
3760Sstevel@tonic-gate  *		|			|
3770Sstevel@tonic-gate  *		|	kvseg		|
3780Sstevel@tonic-gate  *		|			|
3790Sstevel@tonic-gate  *		|			|
3800Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
3810Sstevel@tonic-gate  * 		|        Segkmap	|
3823446Smrj  * 0xC3002000  -|-----------------------|- segmap_start (floating)
3830Sstevel@tonic-gate  *		|	Red Zone	|
3840Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
3850Sstevel@tonic-gate  *		|			|			||
3860Sstevel@tonic-gate  *		|     Shared objects	|			\/
3870Sstevel@tonic-gate  *		|			|
3880Sstevel@tonic-gate  *		:			:
3890Sstevel@tonic-gate  *		|	user data	|
3900Sstevel@tonic-gate  *		|-----------------------|
3910Sstevel@tonic-gate  *		|	user text	|
3920Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
3930Sstevel@tonic-gate  *		|	user stack	|
3940Sstevel@tonic-gate  *		:			:
3950Sstevel@tonic-gate  *		|	invalid		|
3960Sstevel@tonic-gate  * 0x00000000	+-----------------------+
3970Sstevel@tonic-gate  *
3980Sstevel@tonic-gate  *
3990Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
4000Sstevel@tonic-gate  *			+-----------------------+
4013446Smrj  *			|			|
4020Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
4030Sstevel@tonic-gate  *			|	debugger (?)	|
4040Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
4050Sstevel@tonic-gate  *			|      unused    	|
4060Sstevel@tonic-gate  *			+-----------------------+
4070Sstevel@tonic-gate  *			|      Kernel Data	|
4080Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
4090Sstevel@tonic-gate  *			|      Kernel Text	|
4100Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
4113446Smrj  *			|---       GDT       ---|- GDT page (GDT_VA)
4123446Smrj  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
4133446Smrj  *			|			|
4140Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
4150Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
4160Sstevel@tonic-gate  *			|	 Kernel		|
4170Sstevel@tonic-gate  *			|	  heap		|
4180Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
4193446Smrj  *			|	 segmap		|
4203446Smrj  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
4210Sstevel@tonic-gate  *			|    device mappings	|
4220Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
4233290Sjohansen  *			|	  segzio	|
4243290Sjohansen  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
4250Sstevel@tonic-gate  *			|	  segkp		|
4263446Smrj  * ---                  |-----------------------|- segkp_base (floating)
4273446Smrj  * 			|   page_t structures	|  valloc_base + valloc_sz
4283446Smrj  * 			|   memsegs, memlists, 	|
4293446Smrj  * 			|   page hash, etc.	|
4304828Sjosephb  * 0xFFFFFF00.00000000  |-----------------------|- valloc_base (lower if > 1TB)
4310Sstevel@tonic-gate  *			|	 segkpm		|
4320Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
4330Sstevel@tonic-gate  *			|	Red Zone	|
4344828Sjosephb  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE (lower if > 1TB)
4350Sstevel@tonic-gate  *			|     User stack	|- User space memory
4360Sstevel@tonic-gate  * 			|			|
4370Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
4380Sstevel@tonic-gate  *			:			:
4390Sstevel@tonic-gate  * 			|			|
4400Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
4410Sstevel@tonic-gate  * 			|			|
4420Sstevel@tonic-gate  * 			| VA Hole / unused	|
4430Sstevel@tonic-gate  * 			|			|
4440Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
4450Sstevel@tonic-gate  *			|			|
4460Sstevel@tonic-gate  *			|			|
4470Sstevel@tonic-gate  *			:			:
4480Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
4490Sstevel@tonic-gate  *			|			|
4500Sstevel@tonic-gate  *			|	user data	|
4510Sstevel@tonic-gate  *			|-----------------------|
4520Sstevel@tonic-gate  *			|	user text	|
4530Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
4540Sstevel@tonic-gate  *			|	invalid		|
4550Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
4560Sstevel@tonic-gate  *
4570Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
4580Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
4590Sstevel@tonic-gate  *
4600Sstevel@tonic-gate  * Floating values:
4610Sstevel@tonic-gate  *
4620Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
4633446Smrj  * structures.  This region contains page_t structures for
4643446Smrj  * physical memory, memsegs, memlists, and the page hash.
4650Sstevel@tonic-gate  *
4660Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
4670Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
4680Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
4690Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
4700Sstevel@tonic-gate  * accessed using rip-relative addressing
4710Sstevel@tonic-gate  *
4720Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
4750Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
4760Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
4770Sstevel@tonic-gate  *
4783446Smrj  * segmap_start: start of segmap. The length of segmap can be modified
4797692SAmrita.Sadhukhan@Sun.COM  * through eeprom. The default length is 16MB on 32-bit systems and 64MB
4807692SAmrita.Sadhukhan@Sun.COM  * on 64-bit systems.
4810Sstevel@tonic-gate  *
4820Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
4830Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
4840Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
4850Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
4860Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
4870Sstevel@tonic-gate  * segkp sizes).
4880Sstevel@tonic-gate  *
4890Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
4900Sstevel@tonic-gate  *	size	page_t's			size
4910Sstevel@tonic-gate  *	----	---------	----------	-----------
4920Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
4930Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
4940Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
4950Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
4960Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
4970Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
4980Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
4990Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
5000Sstevel@tonic-gate  *
5010Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
5020Sstevel@tonic-gate  * configurations above 8gb.
5030Sstevel@tonic-gate  *
5040Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
5050Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
5060Sstevel@tonic-gate  */
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate /* real-time-clock initialization parameters */
5093446Smrj extern time_t process_rtc_config_file(void);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate uintptr_t	kernelbase;
5123446Smrj uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
5130Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
5140Sstevel@tonic-gate size_t		segmapsize;
5153446Smrj uintptr_t	segmap_start;
5160Sstevel@tonic-gate int		segmapfreelists;
5170Sstevel@tonic-gate pgcnt_t		npages;
5183446Smrj pgcnt_t		orig_npages;
5190Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
5200Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate /*
5230Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
5240Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
5250Sstevel@tonic-gate  * the bootstrap.
5260Sstevel@tonic-gate  */
5273446Smrj static page_t *bootpages;
5283446Smrj 
5293446Smrj /*
5303446Smrj  * boot time pages that have a vnode from the ramdisk will keep that forever.
5313446Smrj  */
5323446Smrj static page_t *rd_pages;
5330Sstevel@tonic-gate 
5347656SSherry.Moore@Sun.COM /*
5357656SSherry.Moore@Sun.COM  * Lower 64K
5367656SSherry.Moore@Sun.COM  */
5377656SSherry.Moore@Sun.COM static page_t *lower_pages = NULL;
5387656SSherry.Moore@Sun.COM static int lower_pages_count = 0;
5397656SSherry.Moore@Sun.COM 
5400Sstevel@tonic-gate struct system_hardware system_hardware;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate /*
5435741Smrj  * Is this Solaris instance running in a fully virtualized xVM domain?
5445741Smrj  */
5455741Smrj int xpv_is_hvm = 0;
5465741Smrj 
5475741Smrj /*
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",
5560Sstevel@tonic-gate 		    mp->address, mp->size);
5570Sstevel@tonic-gate 		mp = mp->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  */
6080Sstevel@tonic-gate #define	NUM_ALLOCATIONS 7
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_bios_disk(void);
6711494Sjg 	extern void startup_pci_bios(void);
6727656SSherry.Moore@Sun.COM 	extern int post_fastreboot;
6735084Sjohnlev #endif
6746336Sbholler 	extern cpuset_t cpu_ready_set;
6756336Sbholler 
6765084Sjohnlev 	/*
6775084Sjohnlev 	 * Make sure that nobody tries to use sekpm until we have
6785084Sjohnlev 	 * initialized it properly.
6795084Sjohnlev 	 */
6800Sstevel@tonic-gate #if defined(__amd64)
6814828Sjosephb 	kpm_desired = 1;
6820Sstevel@tonic-gate #endif
6830Sstevel@tonic-gate 	kpm_enable = 0;
6846336Sbholler 	CPUSET_ONLY(cpu_ready_set, 0);	/* cpu 0 is boot cpu */
6850Sstevel@tonic-gate 
6865084Sjohnlev #if defined(__xpv)	/* XXPV fix me! */
6875084Sjohnlev 	{
6885084Sjohnlev 		extern int segvn_use_regions;
6895084Sjohnlev 		segvn_use_regions = 0;
6905084Sjohnlev 	}
6915084Sjohnlev #endif
6920Sstevel@tonic-gate 	progressbar_init();
6930Sstevel@tonic-gate 	startup_init();
6945316Sjohnlev #if defined(__xpv)
6955316Sjohnlev 	startup_xen_version();
6965316Sjohnlev #endif
6970Sstevel@tonic-gate 	startup_memlist();
6983446Smrj 	startup_kmem();
6994828Sjosephb 	startup_vm();
7005084Sjohnlev #if !defined(__xpv)
7017656SSherry.Moore@Sun.COM 	if (!post_fastreboot)
7027656SSherry.Moore@Sun.COM 		startup_pci_bios();
7035084Sjohnlev #endif
7047532SSean.Ye@Sun.COM #if defined(__xpv)
7057532SSean.Ye@Sun.COM 	startup_xen_mca();
7067532SSean.Ye@Sun.COM #endif
7070Sstevel@tonic-gate 	startup_modules();
7085084Sjohnlev #if !defined(__xpv)
7097656SSherry.Moore@Sun.COM 	if (!post_fastreboot)
7107656SSherry.Moore@Sun.COM 		startup_bios_disk();
7115084Sjohnlev #endif
7120Sstevel@tonic-gate 	startup_end();
7130Sstevel@tonic-gate 	progressbar_start();
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate static void
7170Sstevel@tonic-gate startup_init()
7180Sstevel@tonic-gate {
7190Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/*
7220Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
7230Sstevel@tonic-gate 	 */
7240Sstevel@tonic-gate 	cpuid_pass2(CPU);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	/*
7290Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
7300Sstevel@tonic-gate 	 */
7310Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
7320Sstevel@tonic-gate 		++prom_debug;
7330Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
7380Sstevel@tonic-gate 	 */
7390Sstevel@tonic-gate 	get_system_configuration();
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	/*
7420Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
7430Sstevel@tonic-gate 	 */
7440Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
7450Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
7460Sstevel@tonic-gate 		    CPU->cpu_brandstr);
7470Sstevel@tonic-gate 		halt("This processor is not supported by this release "
7480Sstevel@tonic-gate 		    "of Solaris.");
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate /*
7550Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
7560Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
7570Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
7580Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
7590Sstevel@tonic-gate  */
7600Sstevel@tonic-gate static void
7610Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	uintptr_t va;
7640Sstevel@tonic-gate 	uintptr_t next_va;
7650Sstevel@tonic-gate 	pfn_t pfn;
7660Sstevel@tonic-gate 	uint64_t pfn_addr;
7670Sstevel@tonic-gate 	uint64_t pfn_eaddr;
7680Sstevel@tonic-gate 	uint_t prot;
7690Sstevel@tonic-gate 	size_t len;
7700Sstevel@tonic-gate 	uint_t change;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (prom_debug)
7730Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
7740Sstevel@tonic-gate 		    *addr, *size);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	/*
7770Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
7780Sstevel@tonic-gate 	 */
7790Sstevel@tonic-gate 	if (*addr == 0) {
7800Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
7810Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/*
7853446Smrj 	 * First we trim from the front of the range. Since kbm_probe()
7860Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
7870Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
7880Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
7890Sstevel@tonic-gate 	 * but w < v.
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	do {
7920Sstevel@tonic-gate 		change = 0;
7930Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
7943446Smrj 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7950Sstevel@tonic-gate 		    va = next_va) {
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 			next_va = va + len;
7983446Smrj 			pfn_addr = pfn_to_pa(pfn);
7990Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
8020Sstevel@tonic-gate 				change = 1;
8030Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
8040Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
8050Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
8060Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
8070Sstevel@tonic-gate 				}
8080Sstevel@tonic-gate 			}
8090Sstevel@tonic-gate 		}
8100Sstevel@tonic-gate 		if (change && prom_debug)
8110Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
8120Sstevel@tonic-gate 			    *addr, *size);
8130Sstevel@tonic-gate 	} while (change);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/*
8160Sstevel@tonic-gate 	 * Trim pages from the end of the range.
8170Sstevel@tonic-gate 	 */
8180Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
8193446Smrj 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
8200Sstevel@tonic-gate 	    va = next_va) {
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 		next_va = va + len;
8233446Smrj 		pfn_addr = pfn_to_pa(pfn);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
8260Sstevel@tonic-gate 			*size = pfn_addr - *addr;
8270Sstevel@tonic-gate 	}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	if (prom_debug)
8300Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
8310Sstevel@tonic-gate 		    *addr, *size);
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate static void
8350Sstevel@tonic-gate kpm_init()
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate 	struct segkpm_crargs b;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	/*
8400Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
8410Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
8420Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
8430Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
8440Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
8450Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
8460Sstevel@tonic-gate 	 * pagesize is used.
8470Sstevel@tonic-gate 	 */
8480Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
8490Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
8500Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
8510Sstevel@tonic-gate 	kpmp2pshft = 0;
8520Sstevel@tonic-gate 	kpmpnpgs = 1;
8530Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
8560Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
8590Sstevel@tonic-gate 		panic("cannot attach segkpm");
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
8620Sstevel@tonic-gate 	b.nvcolors = 1;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
8650Sstevel@tonic-gate 		panic("segkpm_create segkpm");
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate /*
8713446Smrj  * The debug info page provides enough information to allow external
8723446Smrj  * inspectors (e.g. when running under a hypervisor) to bootstrap
8733446Smrj  * themselves into allowing full-blown kernel debugging.
8743446Smrj  */
8753446Smrj static void
8763446Smrj init_debug_info(void)
8773446Smrj {
8783446Smrj 	caddr_t mem;
8793446Smrj 	debug_info_t *di;
8803446Smrj 
8813446Smrj #ifndef __lint
8823446Smrj 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
8833446Smrj #endif
8843446Smrj 
8853446Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
8863446Smrj 	    MMU_PAGESIZE);
8873446Smrj 
8883446Smrj 	if (mem != (caddr_t)DEBUG_INFO_VA)
8893446Smrj 		panic("BOP_ALLOC() failed");
8903446Smrj 	bzero(mem, MMU_PAGESIZE);
8913446Smrj 
8923446Smrj 	di = (debug_info_t *)mem;
8933446Smrj 
8943446Smrj 	di->di_magic = DEBUG_INFO_MAGIC;
8953446Smrj 	di->di_version = DEBUG_INFO_VERSION;
8965084Sjohnlev 	di->di_modules = (uintptr_t)&modules;
8975084Sjohnlev 	di->di_s_text = (uintptr_t)s_text;
8985084Sjohnlev 	di->di_e_text = (uintptr_t)e_text;
8995084Sjohnlev 	di->di_s_data = (uintptr_t)s_data;
9005084Sjohnlev 	di->di_e_data = (uintptr_t)e_data;
9015084Sjohnlev 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
9025084Sjohnlev 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
9033446Smrj }
9043446Smrj 
9053446Smrj /*
9063446Smrj  * Build the memlists and other kernel essential memory system data structures.
9073446Smrj  * This is everything at valloc_base.
9080Sstevel@tonic-gate  */
9090Sstevel@tonic-gate static void
9100Sstevel@tonic-gate startup_memlist(void)
9110Sstevel@tonic-gate {
9120Sstevel@tonic-gate 	size_t memlist_sz;
9130Sstevel@tonic-gate 	size_t memseg_sz;
9140Sstevel@tonic-gate 	size_t pagehash_sz;
9150Sstevel@tonic-gate 	size_t pp_sz;
9160Sstevel@tonic-gate 	uintptr_t va;
9170Sstevel@tonic-gate 	size_t len;
9180Sstevel@tonic-gate 	uint_t prot;
9190Sstevel@tonic-gate 	pfn_t pfn;
9200Sstevel@tonic-gate 	int memblocks;
9210Sstevel@tonic-gate 	caddr_t pagecolor_mem;
9220Sstevel@tonic-gate 	size_t pagecolor_memsz;
9230Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
9240Sstevel@tonic-gate 	size_t page_ctrs_size;
9254878Sblakej 	size_t pse_table_alloc_size;
9260Sstevel@tonic-gate 	struct memlist *current;
9270Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
9300Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
9310Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	/*
9360Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
9370Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
9380Sstevel@tonic-gate 	 */
9393446Smrj 	len = kbm_nucleus_size;
9403446Smrj 	ASSERT(len > MMU_PAGESIZE);
9410Sstevel@tonic-gate 
9423446Smrj 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
9433446Smrj 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
9443446Smrj 	if (e_moddata - moddata > MODDATA)
9453446Smrj 		e_moddata = moddata + MODDATA;
9460Sstevel@tonic-gate 
9473446Smrj 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
9483446Smrj 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
9493446Smrj 	if (e_modtext - modtext > MODTEXT)
9503446Smrj 		e_modtext = modtext + MODTEXT;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	econtig = e_moddata;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9550Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
9560Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9570Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
9580Sstevel@tonic-gate 	PRM_DEBUG(econtig);
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	/*
9613446Smrj 	 * Examine the boot loader physical memory map to find out:
9620Sstevel@tonic-gate 	 * - total memory in system - physinstalled
9630Sstevel@tonic-gate 	 * - the max physical address - physmax
9643446Smrj 	 * - the number of discontiguous segments of memory.
9650Sstevel@tonic-gate 	 */
9660Sstevel@tonic-gate 	if (prom_debug)
9673446Smrj 		print_memlist("boot physinstalled",
9680Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
9690Sstevel@tonic-gate 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
9700Sstevel@tonic-gate 	    &physinstalled, &memblocks);
9710Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9720Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9730Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	/*
9760Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
9770Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
9780Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
9790Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
9800Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
9810Sstevel@tonic-gate 	 */
9820Sstevel@tonic-gate 	mmu_init();
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate #ifdef	__i386
9850Sstevel@tonic-gate 	/*
9860Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
9870Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
9880Sstevel@tonic-gate 	 */
9890Sstevel@tonic-gate 	if (mmu.pae_hat) {
9900Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
9910Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
9920Sstevel@tonic-gate 			physmax = PFN_64G - 1;
9930Sstevel@tonic-gate 		}
9940Sstevel@tonic-gate 	} else {
9950Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
9960Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
9970Sstevel@tonic-gate 			physmax = PFN_4G - 1;
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate #endif
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
10050Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
10060Sstevel@tonic-gate 		char value[8];
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 		if (len < 8)
10090Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
10100Sstevel@tonic-gate 		else
10110Sstevel@tonic-gate 			(void) strcpy(value, "");
10120Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
10130Sstevel@tonic-gate 			mmu.pt_nx = 0;
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	/*
10180Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
10190Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
10200Sstevel@tonic-gate 	 *
10210Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
10220Sstevel@tonic-gate 	 */
10230Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
10240Sstevel@tonic-gate 	obp_pages = 0;
10250Sstevel@tonic-gate 	va = KERNEL_TEXT;
10263446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
10270Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
10280Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
10290Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
10300Sstevel@tonic-gate 		va += len;
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 	PRM_DEBUG(npages);
10330Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	/*
10364828Sjosephb 	 * If physmem is patched to be non-zero, use it instead of the computed
10374828Sjosephb 	 * value unless it is larger than the actual amount of memory on hand.
10380Sstevel@tonic-gate 	 */
10391417Skchow 	if (physmem == 0 || physmem > npages) {
10400Sstevel@tonic-gate 		physmem = npages;
10411417Skchow 	} else if (physmem < npages) {
10421479Skchow 		orig_npages = npages;
10430Sstevel@tonic-gate 		npages = physmem;
10441417Skchow 	}
10450Sstevel@tonic-gate 	PRM_DEBUG(physmem);
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/*
10480Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
10490Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
10500Sstevel@tonic-gate 	 * include:
10510Sstevel@tonic-gate 	 *	memsegs
10520Sstevel@tonic-gate 	 *	memlists
10530Sstevel@tonic-gate 	 *	page hash table
10540Sstevel@tonic-gate 	 *	page_t's
10550Sstevel@tonic-gate 	 *	page coloring data structs
10560Sstevel@tonic-gate 	 */
10570Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
10580Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
10590Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	/*
10623446Smrj 	 * Reserve space for memlists. There's no real good way to know exactly
10633446Smrj 	 * how much room we'll need, but this should be a good upper bound.
10640Sstevel@tonic-gate 	 */
10650Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
10660Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
10670Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
10680Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	/*
10710Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
10720Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
10730Sstevel@tonic-gate 	 */
10740Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
10750Sstevel@tonic-gate 	page_hashsz = 1 << highbit(page_hashsz);
10760Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
10770Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
10780Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	/*
10813446Smrj 	 * Set aside room for the page structures themselves.
10820Sstevel@tonic-gate 	 */
10833446Smrj 	PRM_DEBUG(npages);
10843446Smrj 	pp_sz = sizeof (struct page) * npages;
10850Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
10860Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	/*
10890Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
10900Sstevel@tonic-gate 	 */
10910Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
10920Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
10930Sstevel@tonic-gate 	pagecolor_memsz =
10940Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
10950Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
10960Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
10990Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
11000Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
11010Sstevel@tonic-gate 
11024878Sblakej 	/*
11034878Sblakej 	 * Allocate the array that protects pp->p_selock.
11044878Sblakej 	 */
11054878Sblakej 	pse_shift = size_pse_array(physmem, max_ncpus);
11064878Sblakej 	pse_table_size = 1 << pse_shift;
11074878Sblakej 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
11084878Sblakej 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
11094878Sblakej 
11103446Smrj #if defined(__amd64)
11113446Smrj 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
11123446Smrj 	valloc_base = VALLOC_BASE;
11134828Sjosephb 
11144828Sjosephb 	/*
11154828Sjosephb 	 * The default values of VALLOC_BASE and SEGKPM_BASE should work
11164828Sjosephb 	 * for values of physmax up to 1 Terabyte. They need adjusting when
11174828Sjosephb 	 * memory is at addresses above 1 TB.
11184828Sjosephb 	 */
11194828Sjosephb 	if (physmax + 1 > mmu_btop(TERABYTE)) {
11204828Sjosephb 		uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
11214828Sjosephb 
11224828Sjosephb 		/* Round to largest possible pagesize for now */
11234828Sjosephb 		kpm_resv_amount = P2ROUNDUP(kpm_resv_amount, ONE_GIG);
11244828Sjosephb 
11254828Sjosephb 		segkpm_base = -(2 * kpm_resv_amount); /* down from top VA */
11264828Sjosephb 
11274828Sjosephb 		/* make sure we leave some space for user apps above hole */
11284828Sjosephb 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
11294828Sjosephb 		if (segkpm_base > SEGKPM_BASE)
11304828Sjosephb 			segkpm_base = SEGKPM_BASE;
11314828Sjosephb 		PRM_DEBUG(segkpm_base);
11324828Sjosephb 
11334828Sjosephb 		valloc_base = segkpm_base + kpm_resv_amount;
11344828Sjosephb 		PRM_DEBUG(valloc_base);
11354828Sjosephb 	}
11363446Smrj #else	/* __i386 */
11373446Smrj 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
11383446Smrj 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
11394828Sjosephb 	PRM_DEBUG(valloc_base);
11403446Smrj #endif	/* __i386 */
11413446Smrj 
11420Sstevel@tonic-gate 	/*
11433446Smrj 	 * do all the initial allocations
11443446Smrj 	 */
11453446Smrj 	perform_allocations();
11463446Smrj 
11473446Smrj 	/*
11483446Smrj 	 * Build phys_install and phys_avail in kernel memspace.
11493446Smrj 	 * - phys_install should be all memory in the system.
11503446Smrj 	 * - phys_avail is phys_install minus any memory mapped before this
11513446Smrj 	 *    point above KERNEL_TEXT.
11523446Smrj 	 */
11533446Smrj 	current = phys_install = memlist;
11543446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
11553446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
11563446Smrj 		panic("physinstalled was too big!");
11573446Smrj 	if (prom_debug)
11583446Smrj 		print_memlist("phys_install", phys_install);
11593446Smrj 
11603446Smrj 	phys_avail = current;
11613446Smrj 	PRM_POINT("Building phys_avail:\n");
11623446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
11633446Smrj 	    avail_filter);
11643446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
11653446Smrj 		panic("physavail was too big!");
11663446Smrj 	if (prom_debug)
11673446Smrj 		print_memlist("phys_avail", phys_avail);
11683446Smrj 
11693446Smrj 	/*
11703446Smrj 	 * setup page coloring
11710Sstevel@tonic-gate 	 */
11723446Smrj 	page_coloring_setup(pagecolor_mem);
11733446Smrj 	page_lock_init();	/* currently a no-op */
11743446Smrj 
11753446Smrj 	/*
11763446Smrj 	 * free page list counters
11773446Smrj 	 */
11783446Smrj 	(void) page_ctrs_alloc(page_ctrs_mem);
11793446Smrj 
11803446Smrj 	/*
11816880Sdv142724 	 * Size the pcf array based on the number of cpus in the box at
11826880Sdv142724 	 * boot time.
11836880Sdv142724 	 */
11846880Sdv142724 
11856880Sdv142724 	pcf_init();
11866880Sdv142724 
11876880Sdv142724 	/*
11883446Smrj 	 * Initialize the page structures from the memory lists.
11893446Smrj 	 */
11903446Smrj 	availrmem_initial = availrmem = freemem = 0;
11913446Smrj 	PRM_POINT("Calling kphysm_init()...");
11923446Smrj 	npages = kphysm_init(pp_base, npages);
11933446Smrj 	PRM_POINT("kphysm_init() done");
11943446Smrj 	PRM_DEBUG(npages);
11953446Smrj 
11963446Smrj 	init_debug_info();
11973446Smrj 
11983446Smrj 	/*
11993446Smrj 	 * Now that page_t's have been initialized, remove all the
12003446Smrj 	 * initial allocation pages from the kernel free page lists.
12013446Smrj 	 */
12023446Smrj 	boot_mapin((caddr_t)valloc_base, valloc_sz);
12035460Sjosephb 	boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
12043446Smrj 	PRM_POINT("startup_memlist() done");
12053446Smrj 
12063446Smrj 	PRM_DEBUG(valloc_sz);
12074426Saguzovsk 
12084748Saguzovsk #if defined(__amd64)
12094748Saguzovsk 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
12104748Saguzovsk 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
12114748Saguzovsk 		extern size_t textrepl_size_thresh;
12124426Saguzovsk 		textrepl_size_thresh = (16 << 20) - 1;
12134426Saguzovsk 	}
12144748Saguzovsk #endif
12153446Smrj }
12163446Smrj 
12173446Smrj /*
12183446Smrj  * Layout the kernel's part of address space and initialize kmem allocator.
12193446Smrj  */
12203446Smrj static void
12213446Smrj startup_kmem(void)
12223446Smrj {
12233717Sdp78419 	extern void page_set_colorequiv_arr(void);
12243717Sdp78419 
12253446Smrj 	PRM_POINT("startup_kmem() starting...");
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate #if defined(__amd64)
12280Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
12290Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
12300Sstevel@tonic-gate 		    "systems.");
12314828Sjosephb 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
12320Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
12333446Smrj 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
12340Sstevel@tonic-gate #else	/* __i386 */
12350Sstevel@tonic-gate 	/*
12360Sstevel@tonic-gate 	 * We configure kernelbase based on:
12370Sstevel@tonic-gate 	 *
12380Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
12390Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
12400Sstevel@tonic-gate 	 *
12410Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
12420Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
12430Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
12440Sstevel@tonic-gate 	 *
12450Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
12460Sstevel@tonic-gate 	 */
12470Sstevel@tonic-gate 	if (eprom_kernelbase) {
12480Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
12490Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
12500Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
12510Sstevel@tonic-gate 	} else {
12520Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
12530Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
12540Sstevel@tonic-gate 	}
12550Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
12563446Smrj 	core_base = valloc_base;
12570Sstevel@tonic-gate 	core_size = 0;
12583446Smrj #endif	/* __i386 */
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	PRM_DEBUG(core_base);
12610Sstevel@tonic-gate 	PRM_DEBUG(core_size);
12623446Smrj 	PRM_DEBUG(kernelbase);
12630Sstevel@tonic-gate 
12644828Sjosephb #if defined(__i386)
12654828Sjosephb 	segkp_fromheap = 1;
12664828Sjosephb #endif	/* __i386 */
12673446Smrj 
12683446Smrj 	ekernelheap = (char *)core_base;
12693446Smrj 	PRM_DEBUG(ekernelheap);
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	/*
12720Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
12730Sstevel@tonic-gate 	 * update variables that were initialized with a value of
12740Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
12750Sstevel@tonic-gate 	 *
12760Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
12770Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
12780Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
12790Sstevel@tonic-gate 	 *	just be declared as variables there?
12800Sstevel@tonic-gate 	 */
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
12830Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
12844875Sjosephb #if defined(__amd64)
12854875Sjosephb 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
12864875Sjosephb #else
12870Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
12880Sstevel@tonic-gate #endif
12890Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
12900Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
12910Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
12920Sstevel@tonic-gate 
12934828Sjosephb 	layout_kernel_va();
12944828Sjosephb 
12954828Sjosephb #if defined(__i386)
12964828Sjosephb 	/*
12974828Sjosephb 	 * If segmap is too large we can push the bottom of the kernel heap
12984828Sjosephb 	 * higher than the base.  Or worse, it could exceed the top of the
12994828Sjosephb 	 * VA space entirely, causing it to wrap around.
13004828Sjosephb 	 */
13014828Sjosephb 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
13024828Sjosephb 		panic("too little address space available for kernelheap,"
13034828Sjosephb 		    " use eeprom for lower kernelbase or smaller segmapsize");
13044828Sjosephb #endif	/* __i386 */
13054828Sjosephb 
13060Sstevel@tonic-gate 	/*
13070Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
13080Sstevel@tonic-gate 	 */
13094828Sjosephb 	kernelheap_init(kernelheap, ekernelheap,
13104828Sjosephb 	    kernelheap + MMU_PAGESIZE,
13113446Smrj 	    (void *)core_base, (void *)(core_base + core_size));
13120Sstevel@tonic-gate 
13135084Sjohnlev #if defined(__xpv)
13145084Sjohnlev 	/*
13155084Sjohnlev 	 * Link pending events struct into cpu struct
13165084Sjohnlev 	 */
13175084Sjohnlev 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
13185084Sjohnlev #endif
13190Sstevel@tonic-gate 	/*
13200Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
13210Sstevel@tonic-gate 	 */
13220Sstevel@tonic-gate 	kmem_init();
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/*
13253717Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
13263717Sdp78419 	 */
13273717Sdp78419 	page_set_colorequiv_arr();
13283717Sdp78419 
13293717Sdp78419 	/*
13300Sstevel@tonic-gate 	 * print this out early so that we know what's going on
13310Sstevel@tonic-gate 	 */
13320Sstevel@tonic-gate 	cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE);
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	/*
13350Sstevel@tonic-gate 	 * Initialize bp_mapin().
13360Sstevel@tonic-gate 	 */
13370Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
13380Sstevel@tonic-gate 
13391479Skchow 	/*
13401479Skchow 	 * orig_npages is non-zero if physmem has been configured for less
13411479Skchow 	 * than the available memory.
13421479Skchow 	 */
13431479Skchow 	if (orig_npages) {
13444828Sjosephb 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
13454828Sjosephb 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
13464828Sjosephb 		    npages, orig_npages);
13471479Skchow 	}
13480Sstevel@tonic-gate #if defined(__i386)
13490Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
13500Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
13510Sstevel@tonic-gate 		    "System using 0x%lx",
13520Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
13530Sstevel@tonic-gate #endif
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
13560Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
13570Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
13580Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
13590Sstevel@tonic-gate 	}
13600Sstevel@tonic-gate #endif
13610Sstevel@tonic-gate 
13625084Sjohnlev #ifdef __xpv
13635084Sjohnlev 	/*
13645084Sjohnlev 	 * Some of the xen start information has to be relocated up
13655084Sjohnlev 	 * into the kernel's permanent address space.
13665084Sjohnlev 	 */
13675084Sjohnlev 	PRM_POINT("calling xen_relocate_start_info()");
13685084Sjohnlev 	xen_relocate_start_info();
13695084Sjohnlev 	PRM_POINT("xen_relocate_start_info() done");
13705084Sjohnlev 
13715084Sjohnlev 	/*
13725084Sjohnlev 	 * (Update the vcpu pointer in our cpu structure to point into
13735084Sjohnlev 	 * the relocated shared info.)
13745084Sjohnlev 	 */
13755084Sjohnlev 	CPU->cpu_m.mcpu_vcpu_info =
13765084Sjohnlev 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
13775084Sjohnlev #endif
13785084Sjohnlev 
13793446Smrj 	PRM_POINT("startup_kmem() done");
13800Sstevel@tonic-gate }
13810Sstevel@tonic-gate 
13825741Smrj #ifndef __xpv
13835741Smrj /*
13845741Smrj  * If we have detected that we are running in an HVM environment, we need
13855741Smrj  * to prepend the PV driver directory to the module search path.
13865741Smrj  */
13875741Smrj #define	HVM_MOD_DIR "/platform/i86hvm/kernel"
13885741Smrj static void
13895741Smrj update_default_path()
13905741Smrj {
13915741Smrj 	char *current, *newpath;
13925741Smrj 	int newlen;
13935741Smrj 
13945741Smrj 	/*
13955741Smrj 	 * We are about to resync with krtld.  krtld will reset its
13965741Smrj 	 * internal module search path iff Solaris has set default_path.
13975741Smrj 	 * We want to be sure we're prepending this new directory to the
13985741Smrj 	 * right search path.
13995741Smrj 	 */
14005741Smrj 	current = (default_path == NULL) ? kobj_module_path : default_path;
14015741Smrj 
14025741Smrj 	newlen = strlen(HVM_MOD_DIR) + strlen(current) + 1;
14035741Smrj 	newpath = kmem_alloc(newlen, KM_SLEEP);
14045741Smrj 	(void) strcpy(newpath, HVM_MOD_DIR);
14055741Smrj 	(void) strcat(newpath, " ");
14065741Smrj 	(void) strcat(newpath, current);
14075741Smrj 
14085741Smrj 	default_path = newpath;
14095741Smrj }
14105741Smrj #endif
14115741Smrj 
14120Sstevel@tonic-gate static void
14130Sstevel@tonic-gate startup_modules(void)
14140Sstevel@tonic-gate {
1415*7720SKen.Erickson@Sun.COM 	int cnt;
14160Sstevel@tonic-gate 	extern void prom_setup(void);
1417*7720SKen.Erickson@Sun.COM 	int32_t v, h;
1418*7720SKen.Erickson@Sun.COM 	char d[11];
1419*7720SKen.Erickson@Sun.COM 	char *cp;
14207532SSean.Ye@Sun.COM 	cmi_hdl_t hdl;
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
14235084Sjohnlev 
14245084Sjohnlev #ifndef __xpv
14250Sstevel@tonic-gate 	/*
14260Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
14270Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
14280Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
14290Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
14300Sstevel@tonic-gate 	 */
14310Sstevel@tonic-gate 	microfind();
14325741Smrj 
14335741Smrj 	if (xpv_is_hvm)
14345741Smrj 		update_default_path();
14355084Sjohnlev #endif
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	/*
14380Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
14390Sstevel@tonic-gate 	 */
14403446Smrj 	sgmtl(process_rtc_config_file());
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	/*
14430Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
14440Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
14450Sstevel@tonic-gate 	 */
14460Sstevel@tonic-gate 	param_calc(0);
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	mod_setup();
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	/*
14510Sstevel@tonic-gate 	 * Initialize system parameters.
14520Sstevel@tonic-gate 	 */
14530Sstevel@tonic-gate 	param_init();
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	/*
14562712Snn35248 	 * Initialize the default brands
14572712Snn35248 	 */
14582712Snn35248 	brand_init();
14592712Snn35248 
14602712Snn35248 	/*
14610Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
14620Sstevel@tonic-gate 	 */
14630Sstevel@tonic-gate 	maxmem = physmem;
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	/*
14660Sstevel@tonic-gate 	 * Initialize segment management stuff.
14670Sstevel@tonic-gate 	 */
14680Sstevel@tonic-gate 	seg_init();
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
14710Sstevel@tonic-gate 		halt("Can't load specfs");
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
14740Sstevel@tonic-gate 		halt("Can't load devfs");
14750Sstevel@tonic-gate 
14762621Sllai1 	if (modload("fs", "dev") == -1)
14772621Sllai1 		halt("Can't load dev");
14782621Sllai1 
14791676Sjpk 	(void) modloadonly("sys", "lbl_edition");
14801676Sjpk 
14810Sstevel@tonic-gate 	dispinit();
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	/*
14840Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
14850Sstevel@tonic-gate 	 */
1486*7720SKen.Erickson@Sun.COM 	if ((h = set_soft_hostid()) == -1)
1487*7720SKen.Erickson@Sun.COM 		cmn_err(CE_WARN, "Unable to set hostid");
1488*7720SKen.Erickson@Sun.COM 	else {
1489*7720SKen.Erickson@Sun.COM 		for (v = h, cnt = 0; cnt < 10; cnt++) {
1490*7720SKen.Erickson@Sun.COM 			d[cnt] = v % 10;
1491*7720SKen.Erickson@Sun.COM 			v /= 10;
1492*7720SKen.Erickson@Sun.COM 			if (v == 0)
1493*7720SKen.Erickson@Sun.COM 				break;
1494*7720SKen.Erickson@Sun.COM 		}
1495*7720SKen.Erickson@Sun.COM 		for (cp = hw_serial; cnt >= 0; cnt--)
1496*7720SKen.Erickson@Sun.COM 			*cp++ = d[cnt] + '0';
1497*7720SKen.Erickson@Sun.COM 		*cp = 0;
1498*7720SKen.Erickson@Sun.COM 	}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	/* Read cluster configuration data. */
15010Sstevel@tonic-gate 	clconf_init();
15020Sstevel@tonic-gate 
15035084Sjohnlev #if defined(__xpv)
15045084Sjohnlev 	ec_init();
15055084Sjohnlev 	gnttab_init();
15065084Sjohnlev 	(void) xs_early_init();
15075084Sjohnlev #endif /* __xpv */
15085084Sjohnlev 
15090Sstevel@tonic-gate 	/*
15100Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
15110Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
15120Sstevel@tonic-gate 	 */
15130Sstevel@tonic-gate 	setup_ddi();
15141414Scindi 
15157532SSean.Ye@Sun.COM 	/*
15167532SSean.Ye@Sun.COM 	 * Set up the CPU module subsystem for the boot cpu in the native
15177532SSean.Ye@Sun.COM 	 * case, and all physical cpu resource in the xpv dom0 case.
15187532SSean.Ye@Sun.COM 	 * Modifies the device tree, so this must be done after
15197532SSean.Ye@Sun.COM 	 * setup_ddi().
15207532SSean.Ye@Sun.COM 	 */
15217532SSean.Ye@Sun.COM #ifdef __xpv
15227532SSean.Ye@Sun.COM 	/*
15237532SSean.Ye@Sun.COM 	 * If paravirtualized and on dom0 then we initialize all physical
15247532SSean.Ye@Sun.COM 	 * cpu handles now;  if paravirtualized on a domU then do not
15257532SSean.Ye@Sun.COM 	 * initialize.
15267532SSean.Ye@Sun.COM 	 */
15277532SSean.Ye@Sun.COM 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
15287532SSean.Ye@Sun.COM 		xen_mc_lcpu_cookie_t cpi;
15297532SSean.Ye@Sun.COM 
15307532SSean.Ye@Sun.COM 		for (cpi = xen_physcpu_next(NULL); cpi != NULL;
15317532SSean.Ye@Sun.COM 		    cpi = xen_physcpu_next(cpi)) {
15327532SSean.Ye@Sun.COM 			if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
15337532SSean.Ye@Sun.COM 			    xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
15347532SSean.Ye@Sun.COM 			    xen_physcpu_strandid(cpi))) != NULL &&
15357532SSean.Ye@Sun.COM 			    (x86_feature & X86_MCA))
15365254Sgavinm 				cmi_mca_init(hdl);
15375254Sgavinm 		}
15385254Sgavinm 	}
15397532SSean.Ye@Sun.COM #else
15407532SSean.Ye@Sun.COM 	/*
15417532SSean.Ye@Sun.COM 	 * Initialize a handle for the boot cpu - others will initialize
15427532SSean.Ye@Sun.COM 	 * as they startup.  Do not do this if we know we are in an HVM domU.
15437532SSean.Ye@Sun.COM 	 */
15447532SSean.Ye@Sun.COM 	if (!xpv_is_hvm &&
15457532SSean.Ye@Sun.COM 	    (hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
15467532SSean.Ye@Sun.COM 	    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL &&
15477532SSean.Ye@Sun.COM 	    (x86_feature & X86_MCA))
15487532SSean.Ye@Sun.COM 			cmi_mca_init(hdl);
15495254Sgavinm #endif	/* __xpv */
15501414Scindi 
15510Sstevel@tonic-gate 	/*
15520Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
15530Sstevel@tonic-gate 	 */
15543446Smrj 	PRM_POINT("startup_modules: calling prom_setup...");
15550Sstevel@tonic-gate 	prom_setup();
15563446Smrj 	PRM_POINT("startup_modules: done");
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	/*
15590Sstevel@tonic-gate 	 * Load all platform specific modules
15600Sstevel@tonic-gate 	 */
15613446Smrj 	PRM_POINT("startup_modules: calling psm_modload...");
15620Sstevel@tonic-gate 	psm_modload();
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
15650Sstevel@tonic-gate }
15660Sstevel@tonic-gate 
15673446Smrj /*
15683446Smrj  * claim a "setaside" boot page for use in the kernel
15693446Smrj  */
15703446Smrj page_t *
15713446Smrj boot_claim_page(pfn_t pfn)
15720Sstevel@tonic-gate {
15733446Smrj 	page_t *pp;
15743446Smrj 
15753446Smrj 	pp = page_numtopp_nolock(pfn);
15763446Smrj 	ASSERT(pp != NULL);
15770Sstevel@tonic-gate 
15783446Smrj 	if (PP_ISBOOTPAGES(pp)) {
15793446Smrj 		if (pp->p_next != NULL)
15803446Smrj 			pp->p_next->p_prev = pp->p_prev;
15813446Smrj 		if (pp->p_prev == NULL)
15823446Smrj 			bootpages = pp->p_next;
15833446Smrj 		else
15843446Smrj 			pp->p_prev->p_next = pp->p_next;
15853446Smrj 	} else {
15863446Smrj 		/*
15873446Smrj 		 * htable_attach() expects a base pagesize page
15883446Smrj 		 */
15893446Smrj 		if (pp->p_szc != 0)
15903446Smrj 			page_boot_demote(pp);
15913446Smrj 		pp = page_numtopp(pfn, SE_EXCL);
15923446Smrj 	}
15933446Smrj 	return (pp);
15940Sstevel@tonic-gate }
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate /*
15970Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
15980Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
15990Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
16000Sstevel@tonic-gate  */
16010Sstevel@tonic-gate static void
16020Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
16030Sstevel@tonic-gate {
16040Sstevel@tonic-gate 	uintptr_t va = low;
16050Sstevel@tonic-gate 	size_t len;
16060Sstevel@tonic-gate 	uint_t prot;
16070Sstevel@tonic-gate 	pfn_t pfn;
16080Sstevel@tonic-gate 	page_t *pp;
16090Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
16100Sstevel@tonic-gate 
16113446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
16120Sstevel@tonic-gate 		if (va + len >= high)
16130Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
16140Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 		while (len > 0) {
16170Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
16180Sstevel@tonic-gate 			if (pp != NULL) {
16190Sstevel@tonic-gate 				if (setaside == 0)
16200Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
16210Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
16220Sstevel@tonic-gate 					    (void *)va, pfn);
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 				pp->p_next = bootpages;
16253446Smrj 				pp->p_prev = NULL;
16263446Smrj 				PP_SETBOOTPAGES(pp);
16273446Smrj 				if (bootpages != NULL) {
16283446Smrj 					bootpages->p_prev = pp;
16293446Smrj 				}
16300Sstevel@tonic-gate 				bootpages = pp;
16310Sstevel@tonic-gate 				++boot_protect_cnt;
16320Sstevel@tonic-gate 			}
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 			++pfn;
16350Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
16360Sstevel@tonic-gate 			va += MMU_PAGESIZE;
16370Sstevel@tonic-gate 		}
16380Sstevel@tonic-gate 	}
16390Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16423446Smrj /*
16434828Sjosephb  *
16443446Smrj  */
16450Sstevel@tonic-gate static void
16464828Sjosephb layout_kernel_va(void)
16470Sstevel@tonic-gate {
16484828Sjosephb 	PRM_POINT("layout_kernel_va() starting...");
16490Sstevel@tonic-gate 	/*
16503446Smrj 	 * Establish the final size of the kernel's heap, size of segmap,
16513446Smrj 	 * segkp, etc.
16520Sstevel@tonic-gate 	 */
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate #if defined(__amd64)
16550Sstevel@tonic-gate 
16564828Sjosephb 	kpm_vbase = (caddr_t)segkpm_base;
16574828Sjosephb 	kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
16584828Sjosephb 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
16594828Sjosephb 		panic("not enough room for kpm!");
16603446Smrj 	PRM_DEBUG(kpm_size);
16613446Smrj 	PRM_DEBUG(kpm_vbase);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	/*
16643446Smrj 	 * By default we create a seg_kp in 64 bit kernels, it's a little
16653446Smrj 	 * faster to access than embedding it in the heap.
16660Sstevel@tonic-gate 	 */
16673446Smrj 	segkp_base = (caddr_t)valloc_base + valloc_sz;
16680Sstevel@tonic-gate 	if (!segkp_fromheap) {
16690Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 		/*
16723446Smrj 		 * determine size of segkp
16730Sstevel@tonic-gate 		 */
16740Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
16750Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
16760Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
16770Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
16780Sstevel@tonic-gate 			    mmu_btop(sz));
16790Sstevel@tonic-gate 		}
16800Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
16830Sstevel@tonic-gate 	}
16843446Smrj 	PRM_DEBUG(segkp_base);
16853446Smrj 	PRM_DEBUG(segkpsize);
16860Sstevel@tonic-gate 
16874828Sjosephb 	/*
16884828Sjosephb 	 * segzio is used for ZFS cached data. It uses a distinct VA
16894828Sjosephb 	 * segment (from kernel heap) so that we can easily tell not to
16904828Sjosephb 	 * include it in kernel crash dumps on 64 bit kernels. The trick is
16914828Sjosephb 	 * to give it lots of VA, but not constrain the kernel heap.
16924828Sjosephb 	 * We scale the size of segzio linearly with physmem up to
16934828Sjosephb 	 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem.
16944828Sjosephb 	 */
16953446Smrj 	segzio_base = segkp_base + mmu_ptob(segkpsize);
16963446Smrj 	if (segzio_fromheap) {
16973446Smrj 		segziosize = 0;
16983446Smrj 	} else {
16994828Sjosephb 		size_t physmem_size = mmu_ptob(physmem);
17004828Sjosephb 		size_t size = (segziosize == 0) ?
17014828Sjosephb 		    physmem_size : mmu_ptob(segziosize);
17023290Sjohansen 
17034828Sjosephb 		if (size < SEGZIOMINSIZE)
17043290Sjohansen 			size = SEGZIOMINSIZE;
17054828Sjosephb 		if (size > SEGZIOMAXSIZE) {
17063552Sjohansen 			size = SEGZIOMAXSIZE;
17074828Sjosephb 			if (physmem_size > size)
17084828Sjosephb 				size += (physmem_size - size) / 2;
17093290Sjohansen 		}
17103290Sjohansen 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
17113290Sjohansen 	}
17123446Smrj 	PRM_DEBUG(segziosize);
17133446Smrj 	PRM_DEBUG(segzio_base);
17143290Sjohansen 
17150Sstevel@tonic-gate 	/*
17163446Smrj 	 * Put the range of VA for device mappings next, kmdb knows to not
17173446Smrj 	 * grep in this range of addresses.
17180Sstevel@tonic-gate 	 */
17193446Smrj 	toxic_addr =
17203446Smrj 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
17210Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
17223446Smrj 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
17233446Smrj #else /* __i386 */
17243446Smrj 	segmap_start = ROUND_UP_LPAGE(kernelbase);
17253446Smrj #endif /* __i386 */
17263446Smrj 	PRM_DEBUG(segmap_start);
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	/*
17297692SAmrita.Sadhukhan@Sun.COM 	 * Users can change segmapsize through eeprom. If the variable
17307692SAmrita.Sadhukhan@Sun.COM 	 * is tuned through eeprom, there is no upper bound on the
1731*7720SKen.Erickson@Sun.COM 	 * size of segmap.
17320Sstevel@tonic-gate 	 */
17330Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate #if defined(__i386)
17360Sstevel@tonic-gate 	/*
17370Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
17380Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
17393446Smrj 	 * small red zone just below the start of segmap.
17400Sstevel@tonic-gate 	 */
17413446Smrj 	segmap_start += KERNEL_REDZONE_SIZE;
17420Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
17430Sstevel@tonic-gate #endif
17443446Smrj 
17453446Smrj 	PRM_DEBUG(segmap_start);
17463446Smrj 	PRM_DEBUG(segmapsize);
17474828Sjosephb 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
17484828Sjosephb 	PRM_DEBUG(kernelheap);
17494828Sjosephb 	PRM_POINT("layout_kernel_va() done...");
17504828Sjosephb }
17514828Sjosephb 
17524828Sjosephb /*
17534828Sjosephb  * Finish initializing the VM system, now that we are no longer
17544828Sjosephb  * relying on the boot time memory allocators.
17554828Sjosephb  */
17564828Sjosephb static void
17574828Sjosephb startup_vm(void)
17584828Sjosephb {
17594828Sjosephb 	struct segmap_crargs a;
17604828Sjosephb 
17614828Sjosephb 	extern int use_brk_lpg, use_stk_lpg;
17624828Sjosephb 
17634828Sjosephb 	PRM_POINT("startup_vm() starting...");
17644828Sjosephb 
17654828Sjosephb 	/*
17664828Sjosephb 	 * Initialize the hat layer.
17674828Sjosephb 	 */
17684828Sjosephb 	hat_init();
17693446Smrj 
17703446Smrj 	/*
17713446Smrj 	 * Do final allocations of HAT data structures that need to
17723446Smrj 	 * be allocated before quiescing the boot loader.
17733446Smrj 	 */
17743446Smrj 	PRM_POINT("Calling hat_kern_alloc()...");
17753446Smrj 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
17763446Smrj 	PRM_POINT("hat_kern_alloc() done");
17773446Smrj 
17785084Sjohnlev #ifndef __xpv
17793446Smrj 	/*
17805159Sjohnlev 	 * Setup Page Attribute Table
17813446Smrj 	 */
17825159Sjohnlev 	pat_sync();
17835084Sjohnlev #endif
17843446Smrj 
17853446Smrj 	/*
17863446Smrj 	 * The next two loops are done in distinct steps in order
17873446Smrj 	 * to be sure that any page that is doubly mapped (both above
17883446Smrj 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
17893446Smrj 	 * Note this may never happen, but it might someday.
17903446Smrj 	 */
17913446Smrj 	bootpages = NULL;
17923446Smrj 	PRM_POINT("Protecting boot pages");
17930Sstevel@tonic-gate 
17943446Smrj 	/*
17953446Smrj 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
17963446Smrj 	 * page_t's. This can only happen if something weird allocated
17973446Smrj 	 * in this range (like kadb/kmdb).
17983446Smrj 	 */
17993446Smrj 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
18003446Smrj 
18013446Smrj 	/*
18023446Smrj 	 * Before we can take over memory allocation/mapping from the boot
18033446Smrj 	 * loader we must remove from our free page lists any boot allocated
18043446Smrj 	 * pages that stay mapped until release_bootstrap().
18053446Smrj 	 */
18063446Smrj 	protect_boot_range(0, kernelbase, 1);
18073446Smrj 
18085084Sjohnlev 
18093446Smrj 	/*
18103446Smrj 	 * Switch to running on regular HAT (not boot_mmu)
18113446Smrj 	 */
18123446Smrj 	PRM_POINT("Calling hat_kern_setup()...");
18133446Smrj 	hat_kern_setup();
18143446Smrj 
18153446Smrj 	/*
18163446Smrj 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
18173446Smrj 	 */
18183446Smrj 	bop_no_more_mem();
18193446Smrj 
18203446Smrj 	PRM_POINT("hat_kern_setup() done");
18213446Smrj 
18223446Smrj 	hat_cpu_online(CPU);
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	/*
18250Sstevel@tonic-gate 	 * Initialize VM system
18260Sstevel@tonic-gate 	 */
18270Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
18280Sstevel@tonic-gate 	kvm_init();
18290Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	/*
18320Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
18330Sstevel@tonic-gate 	 */
18340Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
18350Sstevel@tonic-gate 		kdi_dvec_vmready();
18360Sstevel@tonic-gate 
18375084Sjohnlev #if defined(__xpv)
18385084Sjohnlev 	/*
18395084Sjohnlev 	 * Populate the I/O pool on domain 0
18405084Sjohnlev 	 */
18415084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
18425084Sjohnlev 		extern long populate_io_pool(void);
18435084Sjohnlev 		long init_io_pool_cnt;
18445084Sjohnlev 
18455084Sjohnlev 		PRM_POINT("Populating reserve I/O page pool");
18465084Sjohnlev 		init_io_pool_cnt = populate_io_pool();
18475084Sjohnlev 		PRM_DEBUG(init_io_pool_cnt);
18485084Sjohnlev 	}
18495084Sjohnlev #endif
18500Sstevel@tonic-gate 	/*
18510Sstevel@tonic-gate 	 * Mangle the brand string etc.
18520Sstevel@tonic-gate 	 */
18530Sstevel@tonic-gate 	cpuid_pass3(CPU);
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate #if defined(__amd64)
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	/*
18580Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
18590Sstevel@tonic-gate 	 */
18600Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
18610Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate #else	/* __i386 */
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	/*
18660Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
18670Sstevel@tonic-gate 	 */
18683446Smrj 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
18690Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
18700Sstevel@tonic-gate 	toxic_bit_map =
18710Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
18720Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
18730Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate #endif	/* __i386 */
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	/*
18790Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
18800Sstevel@tonic-gate 	 * it, tell the debugger.
18810Sstevel@tonic-gate 	 */
18820Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
18830Sstevel@tonic-gate 		kdi_dvec_memavail();
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	/*
18860Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
18870Sstevel@tonic-gate 	 * to work around a pentium bug.
18880Sstevel@tonic-gate 	 */
18895084Sjohnlev #if !defined(__amd64) && !defined(__xpv)
18900Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
18913446Smrj 		desctbr_t idtr;
18920Sstevel@tonic-gate 		gate_desc_t *newidt;
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
18950Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
18960Sstevel@tonic-gate 
18975460Sjosephb 		bcopy(idt0, newidt, NIDT * sizeof (*idt0));
18980Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
18995084Sjohnlev 		    KCS_SEL, SDT_SYSIGT, TRP_KPL);
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
19025460Sjosephb 		    PROT_READ | PROT_EXEC);
19030Sstevel@tonic-gate 
19045460Sjosephb 		CPU->cpu_idt = newidt;
19055460Sjosephb 		idtr.dtr_base = (uintptr_t)CPU->cpu_idt;
19065460Sjosephb 		idtr.dtr_limit = (NIDT * sizeof (*idt0)) - 1;
19073446Smrj 		wr_idtr(&idtr);
19080Sstevel@tonic-gate 	}
19090Sstevel@tonic-gate #endif	/* !__amd64 */
19100Sstevel@tonic-gate 
19115084Sjohnlev #if !defined(__xpv)
19120Sstevel@tonic-gate 	/*
19130Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
19140Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
19150Sstevel@tonic-gate 	 */
19160Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
19170Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
19180Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
19195084Sjohnlev #endif
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
19220Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
19230Sstevel@tonic-gate 
1924841Skchow 	/*
1925841Skchow 	 * disable automatic large pages for small memory systems or
1926841Skchow 	 * when the disable flag is set.
19275349Skchow 	 *
19285349Skchow 	 * Do not yet consider page sizes larger than 2m/4m.
1929841Skchow 	 */
19302991Ssusans 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
19312991Ssusans 		max_uheap_lpsize = LEVEL_SIZE(1);
19322991Ssusans 		max_ustack_lpsize = LEVEL_SIZE(1);
19332991Ssusans 		max_privmap_lpsize = LEVEL_SIZE(1);
19342991Ssusans 		max_uidata_lpsize = LEVEL_SIZE(1);
19352991Ssusans 		max_utext_lpsize = LEVEL_SIZE(1);
19362991Ssusans 		max_shm_lpsize = LEVEL_SIZE(1);
19372991Ssusans 	}
19382991Ssusans 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
19392991Ssusans 	    auto_lpg_disable) {
1940423Sdavemq 		use_brk_lpg = 0;
1941423Sdavemq 		use_stk_lpg = 0;
19422991Ssusans 	}
19435349Skchow 	mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
1944423Sdavemq 
19450Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
19460Sstevel@tonic-gate 	hat_init_finish();
19470Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	/*
19500Sstevel@tonic-gate 	 * Initialize the segkp segment type.
19510Sstevel@tonic-gate 	 */
19520Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
19533446Smrj 	PRM_POINT("Attaching segkp");
19543446Smrj 	if (segkp_fromheap) {
19550Sstevel@tonic-gate 		segkp->s_as = &kas;
19563446Smrj 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
19573446Smrj 	    segkp) < 0) {
19583446Smrj 		panic("startup: cannot attach segkp");
19593446Smrj 		/*NOTREACHED*/
19600Sstevel@tonic-gate 	}
19613446Smrj 	PRM_POINT("Doing segkp_create()");
19620Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
19630Sstevel@tonic-gate 		panic("startup: segkp_create failed");
19640Sstevel@tonic-gate 		/*NOTREACHED*/
19650Sstevel@tonic-gate 	}
19660Sstevel@tonic-gate 	PRM_DEBUG(segkp);
19670Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	/*
19700Sstevel@tonic-gate 	 * kpm segment
19710Sstevel@tonic-gate 	 */
19720Sstevel@tonic-gate 	segmap_kpm = 0;
19730Sstevel@tonic-gate 	if (kpm_desired) {
19740Sstevel@tonic-gate 		kpm_init();
19750Sstevel@tonic-gate 		kpm_enable = 1;
19761841Spraks 		vpm_enable = 1;
19770Sstevel@tonic-gate 	}
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	/*
19800Sstevel@tonic-gate 	 * Now create segmap segment.
19810Sstevel@tonic-gate 	 */
19820Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
19833446Smrj 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
19843446Smrj 		panic("cannot attach segmap");
19850Sstevel@tonic-gate 		/*NOTREACHED*/
19860Sstevel@tonic-gate 	}
19873446Smrj 	PRM_DEBUG(segmap);
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
19900Sstevel@tonic-gate 	a.shmsize = 0;
19910Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
19920Sstevel@tonic-gate 
19933446Smrj 	if (segmap_create(segmap, (caddr_t)&a) != 0)
19943446Smrj 		panic("segmap_create segmap");
19950Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	segdev_init();
20005084Sjohnlev #if defined(__xpv)
20015084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
20025084Sjohnlev #endif
20035084Sjohnlev 		pmem_init();
20043446Smrj 
20050Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
20060Sstevel@tonic-gate }
20070Sstevel@tonic-gate 
20083446Smrj /*
20093446Smrj  * Load a tod module for the non-standard tod part found on this system.
20103446Smrj  */
20113446Smrj static void
20123446Smrj load_tod_module(char *todmod)
20133446Smrj {
20143446Smrj 	if (modload("tod", todmod) == -1)
20153446Smrj 		halt("Can't load TOD module");
20163446Smrj }
20173446Smrj 
20180Sstevel@tonic-gate static void
20190Sstevel@tonic-gate startup_end(void)
20200Sstevel@tonic-gate {
20215107Seota 	int i;
20220Sstevel@tonic-gate 	extern void setx86isalist(void);
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	/*
20270Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
20280Sstevel@tonic-gate 	 * initialization has been done but before the clock
20290Sstevel@tonic-gate 	 * and other devices get started.
20300Sstevel@tonic-gate 	 */
20310Sstevel@tonic-gate 	kern_setup1();
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 	/*
20340Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
20350Sstevel@tonic-gate 	 */
20360Sstevel@tonic-gate 	kcpc_hw_init(CPU);
20370Sstevel@tonic-gate 
20381582Skchow #if defined(OPTERON_WORKAROUND_6323525)
20391582Skchow 	if (opteron_workaround_6323525)
20401582Skchow 		patch_workaround_6323525();
20411582Skchow #endif
20420Sstevel@tonic-gate 	/*
20433446Smrj 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
20443446Smrj 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
20453446Smrj 	 */
20463446Smrj 	if (tod_module_name != NULL) {
20473446Smrj 		PRM_POINT("load_tod_module()");
20483446Smrj 		load_tod_module(tod_module_name);
20493446Smrj 	}
20503446Smrj 
20515084Sjohnlev #if defined(__xpv)
20525084Sjohnlev 	/*
20535084Sjohnlev 	 * Forceload interposing TOD module for the hypervisor.
20545084Sjohnlev 	 */
20555084Sjohnlev 	PRM_POINT("load_tod_module()");
20565084Sjohnlev 	load_tod_module("xpvtod");
20575084Sjohnlev #endif
20585084Sjohnlev 
20593446Smrj 	/*
20600Sstevel@tonic-gate 	 * Configure the system.
20610Sstevel@tonic-gate 	 */
20620Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
20630Sstevel@tonic-gate 	configure();		/* set up devices */
20640Sstevel@tonic-gate 	PRM_POINT("configure() done");
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	/*
20670Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
20680Sstevel@tonic-gate 	 * support.
20690Sstevel@tonic-gate 	 */
20700Sstevel@tonic-gate 	setx86isalist();
20711455Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
20720Sstevel@tonic-gate 	psm_install();
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	/*
20750Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
20760Sstevel@tonic-gate 	 * we're still using bootsvcs.
20770Sstevel@tonic-gate 	 */
20783446Smrj 	PRM_POINT("NULLing out bootops");
20793446Smrj 	*bootopsp = (struct bootops *)NULL;
20800Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
20810Sstevel@tonic-gate 
20825084Sjohnlev #if defined(__xpv)
20835084Sjohnlev 	ec_init_debug_irq();
20845084Sjohnlev 	xs_domu_init();
20855084Sjohnlev #endif
20860Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
20870Sstevel@tonic-gate 	(*picinitf)();
20880Sstevel@tonic-gate 	sti();
20895084Sjohnlev #if defined(__xpv)
20905084Sjohnlev 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
20915084Sjohnlev 	xen_late_startup();
20925084Sjohnlev #endif
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
20954828Sjosephb 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
20960Sstevel@tonic-gate 
20975107Seota 	/*
20985107Seota 	 * Register these software interrupts for ddi timer.
20995107Seota 	 * Software interrupts up to the level 10 are supported.
21005107Seota 	 */
21015107Seota 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
21025107Seota 		char name[sizeof ("timer_softintr") + 2];
21035107Seota 		(void) sprintf(name, "timer_softintr%02d", i);
21045107Seota 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
21055107Seota 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
21065107Seota 	}
21075107Seota 
21080Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
21090Sstevel@tonic-gate }
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate extern char hw_serial[];
21120Sstevel@tonic-gate char *_hs1107 = hw_serial;
21130Sstevel@tonic-gate ulong_t  _bdhs34;
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate void
21160Sstevel@tonic-gate post_startup(void)
21170Sstevel@tonic-gate {
21180Sstevel@tonic-gate 	/*
21190Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
21200Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
21210Sstevel@tonic-gate 	 * instruction set extensions.
21220Sstevel@tonic-gate 	 */
21230Sstevel@tonic-gate 	bind_hwcap();
21240Sstevel@tonic-gate 
21255084Sjohnlev #ifdef __xpv
21265084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
21275084Sjohnlev #endif
21285084Sjohnlev 	{
21295084Sjohnlev 		/*
21305084Sjohnlev 		 * Load the System Management BIOS into the global ksmbios
21315084Sjohnlev 		 * handle, if an SMBIOS is present on this system.
21325084Sjohnlev 		 */
21335084Sjohnlev 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
21345084Sjohnlev 
21355084Sjohnlev #if defined(__xpv)
21365084Sjohnlev 		xpv_panic_init();
21375084Sjohnlev #else
21385084Sjohnlev 		/*
21395084Sjohnlev 		 * Startup the memory scrubber.
21405084Sjohnlev 		 * XXPV	This should be running somewhere ..
21415084Sjohnlev 		 */
21427532SSean.Ye@Sun.COM 		if (!xpv_is_hvm)
21437532SSean.Ye@Sun.COM 			memscrub_init();
21445084Sjohnlev #endif
21455084Sjohnlev 	}
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 	/*
21481414Scindi 	 * Complete CPU module initialization
21491414Scindi 	 */
21505254Sgavinm 	cmi_post_startup();
21511414Scindi 
21521414Scindi 	/*
21530Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
21540Sstevel@tonic-gate 	 */
21550Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 	/*
21580Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
21590Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
21600Sstevel@tonic-gate 	 */
21610Sstevel@tonic-gate 	(void) modload("fs", "procfs");
21620Sstevel@tonic-gate 
21635129Smarx 	(void) i_ddi_attach_hw_nodes("pit_beep");
21645129Smarx 
21650Sstevel@tonic-gate #if defined(__i386)
21660Sstevel@tonic-gate 	/*
21670Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
21680Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
21690Sstevel@tonic-gate 	 */
21700Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
21710Sstevel@tonic-gate 		halt("No working FP hardware found");
21720Sstevel@tonic-gate #endif
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	maxmem = freemem;
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
21770Sstevel@tonic-gate }
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate static int
21807656SSherry.Moore@Sun.COM pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
21810Sstevel@tonic-gate {
21827656SSherry.Moore@Sun.COM 	return ((pp->p_pagenum >= btop(low_addr)) &&
21837656SSherry.Moore@Sun.COM 	    (pp->p_pagenum < btopr(high_addr)));
21840Sstevel@tonic-gate }
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate void
21870Sstevel@tonic-gate release_bootstrap(void)
21880Sstevel@tonic-gate {
21890Sstevel@tonic-gate 	int root_is_ramdisk;
21900Sstevel@tonic-gate 	page_t *pp;
21910Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
21920Sstevel@tonic-gate 	extern dev_t rootdev;
21937515SSeth.Goldberg@Sun.COM #if !defined(__xpv)
21947515SSeth.Goldberg@Sun.COM 	pfn_t	pfn;
21957515SSeth.Goldberg@Sun.COM #endif
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
21980Sstevel@tonic-gate 	kobj_boot_unmountroot();
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 	/*
22010Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
22020Sstevel@tonic-gate 	 */
22030Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
22047656SSherry.Moore@Sun.COM 
22053446Smrj 	clear_boot_mappings(0, _userlimit);
22067656SSherry.Moore@Sun.COM 
22073446Smrj 	postbootkernelbase = kernelbase;
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	/*
22100Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
22110Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
22120Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
22130Sstevel@tonic-gate 	 */
22140Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
22150Sstevel@tonic-gate 	if (!root_is_ramdisk) {
22160Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
22170Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
22180Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
22190Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
22200Sstevel@tonic-gate 	}
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
22230Sstevel@tonic-gate 	while (bootpages) {
22247656SSherry.Moore@Sun.COM 		extern uint64_t ramdisk_start, ramdisk_end;
22250Sstevel@tonic-gate 		pp = bootpages;
22260Sstevel@tonic-gate 		bootpages = pp->p_next;
22277656SSherry.Moore@Sun.COM 
22287656SSherry.Moore@Sun.COM 
22297656SSherry.Moore@Sun.COM 		/* Keep pages for the lower 64K */
22307656SSherry.Moore@Sun.COM 		if (pp_in_range(pp, 0, 0x40000)) {
22317656SSherry.Moore@Sun.COM 			pp->p_next = lower_pages;
22327656SSherry.Moore@Sun.COM 			lower_pages = pp;
22337656SSherry.Moore@Sun.COM 			lower_pages_count++;
22347656SSherry.Moore@Sun.COM 			continue;
22357656SSherry.Moore@Sun.COM 		}
22367656SSherry.Moore@Sun.COM 
22377656SSherry.Moore@Sun.COM 
22387656SSherry.Moore@Sun.COM 		if (root_is_ramdisk && pp_in_range(pp, ramdisk_start,
22397656SSherry.Moore@Sun.COM 		    ramdisk_end)) {
22400Sstevel@tonic-gate 			pp->p_next = rd_pages;
22410Sstevel@tonic-gate 			rd_pages = pp;
22420Sstevel@tonic-gate 			continue;
22430Sstevel@tonic-gate 		}
22440Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
22453446Smrj 		pp->p_prev = (struct page *)0;
22463446Smrj 		PP_CLRBOOTPAGES(pp);
22470Sstevel@tonic-gate 		page_free(pp, 1);
22480Sstevel@tonic-gate 	}
22493446Smrj 	PRM_POINT("Boot pages released");
22500Sstevel@tonic-gate 
22515084Sjohnlev #if !defined(__xpv)
22525084Sjohnlev /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
22530Sstevel@tonic-gate 	/*
22547515SSeth.Goldberg@Sun.COM 	 * Find 1 page below 1 MB so that other processors can boot up or
22557515SSeth.Goldberg@Sun.COM 	 * so that any processor can resume.
22560Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
22570Sstevel@tonic-gate 	 * We should have just free'd one up.
22580Sstevel@tonic-gate 	 */
22597656SSherry.Moore@Sun.COM 
22607656SSherry.Moore@Sun.COM 	/*
22617656SSherry.Moore@Sun.COM 	 * 0x10 pages is 64K.  Leave the bottom 64K alone
22627656SSherry.Moore@Sun.COM 	 * for BIOS.
22637656SSherry.Moore@Sun.COM 	 */
22647656SSherry.Moore@Sun.COM 	for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
22657515SSeth.Goldberg@Sun.COM 		if (page_numtopp_alloc(pfn) == NULL)
22667515SSeth.Goldberg@Sun.COM 			continue;
22677515SSeth.Goldberg@Sun.COM 		rm_platter_va = i86devmap(pfn, 1,
22687515SSeth.Goldberg@Sun.COM 		    PROT_READ | PROT_WRITE | PROT_EXEC);
22697515SSeth.Goldberg@Sun.COM 		rm_platter_pa = ptob(pfn);
22707515SSeth.Goldberg@Sun.COM 		hat_devload(kas.a_hat,
22717515SSeth.Goldberg@Sun.COM 		    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
22727515SSeth.Goldberg@Sun.COM 		    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
22737515SSeth.Goldberg@Sun.COM 		    HAT_LOAD_NOCONSIST);
22747515SSeth.Goldberg@Sun.COM 		break;
22750Sstevel@tonic-gate 	}
22767515SSeth.Goldberg@Sun.COM 	if (pfn == btop(1*1024*1024) && use_mp)
22777515SSeth.Goldberg@Sun.COM 		panic("No page below 1M available for starting "
22787515SSeth.Goldberg@Sun.COM 		    "other processors or for resuming from system-suspend");
22795084Sjohnlev #endif	/* !__xpv */
22800Sstevel@tonic-gate }
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate /*
22830Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
22840Sstevel@tonic-gate  */
22850Sstevel@tonic-gate void
22860Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
22870Sstevel@tonic-gate {
22880Sstevel@tonic-gate 	pp->p_pagenum = pnum;
22890Sstevel@tonic-gate 	pp->p_mapping = NULL;
22900Sstevel@tonic-gate 	pp->p_embed = 0;
22910Sstevel@tonic-gate 	pp->p_share = 0;
22920Sstevel@tonic-gate 	pp->p_mlentry = 0;
22930Sstevel@tonic-gate }
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate /*
22960Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
22970Sstevel@tonic-gate  */
22980Sstevel@tonic-gate static pgcnt_t
22990Sstevel@tonic-gate kphysm_init(
23003446Smrj 	page_t *pp,
23010Sstevel@tonic-gate 	pgcnt_t npages)
23020Sstevel@tonic-gate {
23030Sstevel@tonic-gate 	struct memlist	*pmem;
23040Sstevel@tonic-gate 	struct memseg	*cur_memseg;
23050Sstevel@tonic-gate 	pfn_t		base_pfn;
23060Sstevel@tonic-gate 	pgcnt_t		num;
23070Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
23080Sstevel@tonic-gate 	uint64_t	addr;
23090Sstevel@tonic-gate 	uint64_t	size;
23100Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
23130Sstevel@tonic-gate 
23143446Smrj 	cur_memseg = memseg_base;
23150Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
23160Sstevel@tonic-gate 		/*
23170Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
23180Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
23190Sstevel@tonic-gate 		 */
23200Sstevel@tonic-gate 		addr = pmem->address;
23210Sstevel@tonic-gate 		size = pmem->size;
23220Sstevel@tonic-gate 		if (btop(addr) > physmax)
23230Sstevel@tonic-gate 			continue;
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 		/*
23260Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
23270Sstevel@tonic-gate 		 */
23280Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
23290Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
23300Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
23310Sstevel@tonic-gate 			size -= addr - pmem->address;
23320Sstevel@tonic-gate 		}
23330Sstevel@tonic-gate 
2334810Skchow 		/* only process pages below or equal to physmax */
2335810Skchow 		if ((btop(addr + size) - 1) > physmax)
2336810Skchow 			size = ptob(physmax - btop(addr) + 1);
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate 		num = btop(size);
23390Sstevel@tonic-gate 		if (num == 0)
23400Sstevel@tonic-gate 			continue;
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 		if (num > npages)
23430Sstevel@tonic-gate 			num = npages;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 		npages -= num;
23460Sstevel@tonic-gate 		pages_done += num;
23470Sstevel@tonic-gate 		base_pfn = btop(addr);
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 		if (prom_debug)
23500Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
23510Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
23520Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 		/*
23553446Smrj 		 * Ignore pages below ddiphysmin to simplify ddi memory
23560Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
23570Sstevel@tonic-gate 		 */
23580Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
23593446Smrj 			if (base_pfn + num <= ddiphysmin)
23600Sstevel@tonic-gate 				continue;
23610Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
23620Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
23630Sstevel@tonic-gate 			base_pfn = ddiphysmin;
23640Sstevel@tonic-gate 		}
23653446Smrj 
23660Sstevel@tonic-gate 		/*
23670Sstevel@tonic-gate 		 * Build the memsegs entry
23680Sstevel@tonic-gate 		 */
23690Sstevel@tonic-gate 		cur_memseg->pages = pp;
23700Sstevel@tonic-gate 		cur_memseg->epages = pp + num;
23710Sstevel@tonic-gate 		cur_memseg->pages_base = base_pfn;
23720Sstevel@tonic-gate 		cur_memseg->pages_end = base_pfn + num;
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate 		/*
23753446Smrj 		 * Insert into memseg list in decreasing pfn range order.
23760Sstevel@tonic-gate 		 * Low memory is typically more fragmented such that this
23770Sstevel@tonic-gate 		 * ordering keeps the larger ranges at the front of the list
23780Sstevel@tonic-gate 		 * for code that searches memseg.
23793446Smrj 		 * This ASSERTS that the memsegs coming in from boot are in
23803446Smrj 		 * increasing physical address order and not contiguous.
23810Sstevel@tonic-gate 		 */
23823446Smrj 		if (memsegs != NULL) {
23833446Smrj 			ASSERT(cur_memseg->pages_base >= memsegs->pages_end);
23843446Smrj 			cur_memseg->next = memsegs;
23850Sstevel@tonic-gate 		}
23863446Smrj 		memsegs = cur_memseg;
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 		/*
23890Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
23900Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
23910Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
23920Sstevel@tonic-gate 		 * it initializes them.
23930Sstevel@tonic-gate 		 */
23940Sstevel@tonic-gate 		add_physmem(pp, num, base_pfn);
23950Sstevel@tonic-gate 		cur_memseg++;
23960Sstevel@tonic-gate 		availrmem_initial += num;
23970Sstevel@tonic-gate 		availrmem += num;
23980Sstevel@tonic-gate 
23993446Smrj 		pp += num;
24000Sstevel@tonic-gate 	}
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
24030Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
24040Sstevel@tonic-gate 	PRM_DEBUG(freemem);
24050Sstevel@tonic-gate 	build_pfn_hash();
24060Sstevel@tonic-gate 	return (pages_done);
24070Sstevel@tonic-gate }
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate /*
24100Sstevel@tonic-gate  * Kernel VM initialization.
24110Sstevel@tonic-gate  */
24120Sstevel@tonic-gate static void
24130Sstevel@tonic-gate kvm_init(void)
24140Sstevel@tonic-gate {
24150Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 	/*
24180Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
24190Sstevel@tonic-gate 	 */
24200Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
24210Sstevel@tonic-gate 	as_avlinit(&kas);
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
24240Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
24270Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
24280Sstevel@tonic-gate 
24294828Sjosephb 	(void) seg_attach(&kas, kernelheap,
24304828Sjosephb 	    ekernelheap - kernelheap, &kvseg);
24310Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
24320Sstevel@tonic-gate 
24333446Smrj 	if (core_size > 0) {
24343446Smrj 		PRM_POINT("attaching kvseg_core");
24353446Smrj 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
24363446Smrj 		    &kvseg_core);
24373446Smrj 		(void) segkmem_create(&kvseg_core);
24383446Smrj 	}
24393290Sjohansen 
24403446Smrj 	if (segziosize > 0) {
24413446Smrj 		PRM_POINT("attaching segzio");
24423290Sjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
24433290Sjohansen 		    &kzioseg);
24443290Sjohansen 		(void) segkmem_zio_create(&kzioseg);
24453290Sjohansen 
24463290Sjohansen 		/* create zio area covering new segment */
24473290Sjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
24483290Sjohansen 	}
24490Sstevel@tonic-gate 
24503446Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
24510Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 	/*
24560Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
24570Sstevel@tonic-gate 	 */
24583446Smrj 	PRM_POINT("protecting redzone");
24590Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 	/*
24620Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
24630Sstevel@tonic-gate 	 */
24640Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
24650Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	/*
24680Sstevel@tonic-gate 	 * Make data writable until end.
24690Sstevel@tonic-gate 	 */
24700Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
24710Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
24720Sstevel@tonic-gate }
24730Sstevel@tonic-gate 
24745084Sjohnlev #ifndef __xpv
24750Sstevel@tonic-gate /*
24765159Sjohnlev  * Solaris adds an entry for Write Combining caching to the PAT
24770Sstevel@tonic-gate  */
24785159Sjohnlev static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate void
24815159Sjohnlev pat_sync(void)
24820Sstevel@tonic-gate {
24835159Sjohnlev 	ulong_t	cr0, cr0_orig, cr4;
24840Sstevel@tonic-gate 
24855159Sjohnlev 	if (!(x86_feature & X86_PAT))
24860Sstevel@tonic-gate 		return;
24875159Sjohnlev 	cr0_orig = cr0 = getcr0();
24885159Sjohnlev 	cr4 = getcr4();
24890Sstevel@tonic-gate 
24905159Sjohnlev 	/* disable caching and flush all caches and TLBs */
24915159Sjohnlev 	cr0 |= CR0_CD;
24925159Sjohnlev 	cr0 &= ~CR0_NW;
24935159Sjohnlev 	setcr0(cr0);
24945159Sjohnlev 	invalidate_cache();
24955159Sjohnlev 	if (cr4 & CR4_PGE) {
24965159Sjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
24975159Sjohnlev 		setcr4(cr4);
24985159Sjohnlev 	} else {
24995159Sjohnlev 		reload_cr3();
25000Sstevel@tonic-gate 	}
25010Sstevel@tonic-gate 
25025159Sjohnlev 	/* add our entry to the PAT */
25035159Sjohnlev 	wrmsr(REG_PAT, pat_attr_reg);
2504770Skucharsk 
25055159Sjohnlev 	/* flush TLBs and cache again, then reenable cr0 caching */
25065159Sjohnlev 	if (cr4 & CR4_PGE) {
25075159Sjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
25085159Sjohnlev 		setcr4(cr4);
25095159Sjohnlev 	} else {
25105159Sjohnlev 		reload_cr3();
25110Sstevel@tonic-gate 	}
25120Sstevel@tonic-gate 	invalidate_cache();
25130Sstevel@tonic-gate 	setcr0(cr0_orig);
25140Sstevel@tonic-gate }
25150Sstevel@tonic-gate 
25165159Sjohnlev #endif /* !__xpv */
25170Sstevel@tonic-gate 
2518*7720SKen.Erickson@Sun.COM #if defined(_SOFT_HOSTID)
2519*7720SKen.Erickson@Sun.COM /*
2520*7720SKen.Erickson@Sun.COM  * On platforms that do not have a hardware serial number, attempt
2521*7720SKen.Erickson@Sun.COM  * to set one based on the contents of /etc/hostid.  If this file does
2522*7720SKen.Erickson@Sun.COM  * not exist, assume that we are to generate a new hostid and set
2523*7720SKen.Erickson@Sun.COM  * it in the kernel, for subsequent saving by a userland process
2524*7720SKen.Erickson@Sun.COM  * once the system is up and the root filesystem is mounted r/w.
2525*7720SKen.Erickson@Sun.COM  *
2526*7720SKen.Erickson@Sun.COM  * In an attempt to make the hostid less prone to abuse
2527*7720SKen.Erickson@Sun.COM  * (for license circumvention, etc), we store it in /etc/hostid
2528*7720SKen.Erickson@Sun.COM  * in rot47 format.
2529*7720SKen.Erickson@Sun.COM  */
2530*7720SKen.Erickson@Sun.COM extern volatile unsigned long tenmicrodata;
2531*7720SKen.Erickson@Sun.COM 
2532*7720SKen.Erickson@Sun.COM static int32_t
2533*7720SKen.Erickson@Sun.COM set_soft_hostid(void)
2534*7720SKen.Erickson@Sun.COM {
2535*7720SKen.Erickson@Sun.COM 	struct _buf *file;
2536*7720SKen.Erickson@Sun.COM 	char tokbuf[MAXNAMELEN];
2537*7720SKen.Erickson@Sun.COM 	token_t token;
2538*7720SKen.Erickson@Sun.COM 	int done = 0;
2539*7720SKen.Erickson@Sun.COM 	u_longlong_t tmp;
2540*7720SKen.Erickson@Sun.COM 	int32_t hostid = -1;
2541*7720SKen.Erickson@Sun.COM 	unsigned char *c;
2542*7720SKen.Erickson@Sun.COM 	hrtime_t tsc;
2543*7720SKen.Erickson@Sun.COM 
2544*7720SKen.Erickson@Sun.COM 	/*
2545*7720SKen.Erickson@Sun.COM 	 * If /etc/hostid file not found, we'd like to get a pseudo
2546*7720SKen.Erickson@Sun.COM 	 * random number to use at the hostid.  A nice way to do this
2547*7720SKen.Erickson@Sun.COM 	 * is to read the real time clock.  To remain xen-compatible,
2548*7720SKen.Erickson@Sun.COM 	 * we can't poke the real hardware, so we use tsc_read() to
2549*7720SKen.Erickson@Sun.COM 	 * read the real time clock.  However, there is an ominous
2550*7720SKen.Erickson@Sun.COM 	 * warning in tsc_read that says it can return zero, so we
2551*7720SKen.Erickson@Sun.COM 	 * deal with that possibility by falling back to using the
2552*7720SKen.Erickson@Sun.COM 	 * (hopefully random enough) value in tenmicrodata.
2553*7720SKen.Erickson@Sun.COM 	 */
2554*7720SKen.Erickson@Sun.COM 
2555*7720SKen.Erickson@Sun.COM 	if ((file = kobj_open_file(hostid_file)) == (struct _buf *)-1) {
2556*7720SKen.Erickson@Sun.COM 		/* hostid file not found */
2557*7720SKen.Erickson@Sun.COM 		tsc = tsc_read();
2558*7720SKen.Erickson@Sun.COM 		if (tsc == 0)	/* tsc_read can return zero sometimes */
2559*7720SKen.Erickson@Sun.COM 			hostid = (int32_t)tenmicrodata & 0x0CFFFFF;
2560*7720SKen.Erickson@Sun.COM 		else
2561*7720SKen.Erickson@Sun.COM 			hostid = (int32_t)tsc & 0x0CFFFFF;
2562*7720SKen.Erickson@Sun.COM 	} else {
2563*7720SKen.Erickson@Sun.COM 		/* hostid file found */
2564*7720SKen.Erickson@Sun.COM 		while (!done) {
2565*7720SKen.Erickson@Sun.COM 			token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2566*7720SKen.Erickson@Sun.COM 
2567*7720SKen.Erickson@Sun.COM 			switch (token) {
2568*7720SKen.Erickson@Sun.COM 			case POUND:
2569*7720SKen.Erickson@Sun.COM 				/*
2570*7720SKen.Erickson@Sun.COM 				 * skip comments
2571*7720SKen.Erickson@Sun.COM 				 */
2572*7720SKen.Erickson@Sun.COM 				kobj_find_eol(file);
2573*7720SKen.Erickson@Sun.COM 				break;
2574*7720SKen.Erickson@Sun.COM 			case STRING:
2575*7720SKen.Erickson@Sun.COM 				/*
2576*7720SKen.Erickson@Sun.COM 				 * un-rot47 - obviously this
2577*7720SKen.Erickson@Sun.COM 				 * nonsense is ascii-specific
2578*7720SKen.Erickson@Sun.COM 				 */
2579*7720SKen.Erickson@Sun.COM 				for (c = (unsigned char *)tokbuf;
2580*7720SKen.Erickson@Sun.COM 				    *c != '\0'; c++) {
2581*7720SKen.Erickson@Sun.COM 					*c += 47;
2582*7720SKen.Erickson@Sun.COM 					if (*c > '~')
2583*7720SKen.Erickson@Sun.COM 						*c -= 94;
2584*7720SKen.Erickson@Sun.COM 					else if (*c < '!')
2585*7720SKen.Erickson@Sun.COM 						*c += 94;
2586*7720SKen.Erickson@Sun.COM 				}
2587*7720SKen.Erickson@Sun.COM 				/*
2588*7720SKen.Erickson@Sun.COM 				 * now we should have a real number
2589*7720SKen.Erickson@Sun.COM 				 */
2590*7720SKen.Erickson@Sun.COM 
2591*7720SKen.Erickson@Sun.COM 				if (kobj_getvalue(tokbuf, &tmp) != 0)
2592*7720SKen.Erickson@Sun.COM 					kobj_file_err(CE_WARN, file,
2593*7720SKen.Erickson@Sun.COM 					    "Bad value %s for hostid",
2594*7720SKen.Erickson@Sun.COM 					    tokbuf);
2595*7720SKen.Erickson@Sun.COM 				else
2596*7720SKen.Erickson@Sun.COM 					hostid = (int32_t)tmp;
2597*7720SKen.Erickson@Sun.COM 
2598*7720SKen.Erickson@Sun.COM 				break;
2599*7720SKen.Erickson@Sun.COM 			case EOF:
2600*7720SKen.Erickson@Sun.COM 				done = 1;
2601*7720SKen.Erickson@Sun.COM 				/* FALLTHROUGH */
2602*7720SKen.Erickson@Sun.COM 			case NEWLINE:
2603*7720SKen.Erickson@Sun.COM 				kobj_newline(file);
2604*7720SKen.Erickson@Sun.COM 				break;
2605*7720SKen.Erickson@Sun.COM 			default:
2606*7720SKen.Erickson@Sun.COM 				break;
2607*7720SKen.Erickson@Sun.COM 
2608*7720SKen.Erickson@Sun.COM 			}
2609*7720SKen.Erickson@Sun.COM 		}
2610*7720SKen.Erickson@Sun.COM 		if (hostid == -1) /* didn't find a hostid string */
2611*7720SKen.Erickson@Sun.COM 			kobj_file_err(CE_WARN, file,
2612*7720SKen.Erickson@Sun.COM 			    "hostid missing or corrupt");
2613*7720SKen.Erickson@Sun.COM 
2614*7720SKen.Erickson@Sun.COM 		kobj_close_file(file);
2615*7720SKen.Erickson@Sun.COM 	}
2616*7720SKen.Erickson@Sun.COM 	/*
2617*7720SKen.Erickson@Sun.COM 	 * hostid is now the value read from /etc/hostid, or the
2618*7720SKen.Erickson@Sun.COM 	 * new hostid we generated in this routine or -1 if not set.
2619*7720SKen.Erickson@Sun.COM 	 */
2620*7720SKen.Erickson@Sun.COM 	return (hostid);
2621*7720SKen.Erickson@Sun.COM }
2622*7720SKen.Erickson@Sun.COM #endif /* _SOFT_HOSTID */
2623*7720SKen.Erickson@Sun.COM 
26240Sstevel@tonic-gate void
26251676Sjpk get_system_configuration(void)
26260Sstevel@tonic-gate {
26270Sstevel@tonic-gate 	char	prop[32];
26280Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
26290Sstevel@tonic-gate 
26304828Sjosephb 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
26314828Sjosephb 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
26324828Sjosephb 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
26334828Sjosephb 	    nodes_ll > MAXNODES ||
26344828Sjosephb 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
26354828Sjosephb 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
26364828Sjosephb 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
26370Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
26380Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
26390Sstevel@tonic-gate 	} else {
26400Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
26410Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
26420Sstevel@tonic-gate 	}
26434828Sjosephb 
26444828Sjosephb 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
26454828Sjosephb 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
26464828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
26474828Sjosephb 		eprom_kernelbase = NULL;
26480Sstevel@tonic-gate 	else
26494828Sjosephb 		eprom_kernelbase = (uintptr_t)lvalue;
26500Sstevel@tonic-gate 
26514828Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
26524828Sjosephb 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
26534828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
26540Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
26554828Sjosephb 	else
26560Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
26570Sstevel@tonic-gate 
26584828Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
26594828Sjosephb 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
26604828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
26610Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
26624828Sjosephb 	else
26630Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
26641417Skchow 
26654004Sjosephb 	/* physmem used to be here, but moved much earlier to fakebop.c */
26660Sstevel@tonic-gate }
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate /*
26690Sstevel@tonic-gate  * Add to a memory list.
26700Sstevel@tonic-gate  * start = start of new memory segment
26710Sstevel@tonic-gate  * len = length of new memory segment in bytes
26720Sstevel@tonic-gate  * new = pointer to a new struct memlist
26730Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
26740Sstevel@tonic-gate  */
26753446Smrj void
26760Sstevel@tonic-gate memlist_add(
26770Sstevel@tonic-gate 	uint64_t start,
26780Sstevel@tonic-gate 	uint64_t len,
26790Sstevel@tonic-gate 	struct memlist *new,
26800Sstevel@tonic-gate 	struct memlist **memlistp)
26810Sstevel@tonic-gate {
26820Sstevel@tonic-gate 	struct memlist *cur;
26830Sstevel@tonic-gate 	uint64_t end = start + len;
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	new->address = start;
26860Sstevel@tonic-gate 	new->size = len;
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 	cur = *memlistp;
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 	while (cur) {
26910Sstevel@tonic-gate 		if (cur->address >= end) {
26920Sstevel@tonic-gate 			new->next = cur;
26930Sstevel@tonic-gate 			*memlistp = new;
26940Sstevel@tonic-gate 			new->prev = cur->prev;
26950Sstevel@tonic-gate 			cur->prev = new;
26960Sstevel@tonic-gate 			return;
26970Sstevel@tonic-gate 		}
26980Sstevel@tonic-gate 		ASSERT(cur->address + cur->size <= start);
26990Sstevel@tonic-gate 		if (cur->next == NULL) {
27000Sstevel@tonic-gate 			cur->next = new;
27010Sstevel@tonic-gate 			new->prev = cur;
27020Sstevel@tonic-gate 			new->next = NULL;
27030Sstevel@tonic-gate 			return;
27040Sstevel@tonic-gate 		}
27050Sstevel@tonic-gate 		memlistp = &cur->next;
27060Sstevel@tonic-gate 		cur = cur->next;
27070Sstevel@tonic-gate 	}
27080Sstevel@tonic-gate }
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate void
27110Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
27120Sstevel@tonic-gate {
27130Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
27140Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
27170Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
27180Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
27190Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
27200Sstevel@tonic-gate }
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate caddr_t
27230Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
27240Sstevel@tonic-gate {
27250Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
27260Sstevel@tonic-gate }
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate /*ARGSUSED*/
27290Sstevel@tonic-gate caddr_t
27300Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
27310Sstevel@tonic-gate {
27320Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
27330Sstevel@tonic-gate 	/*NOTREACHED*/
27340Sstevel@tonic-gate 	return (0);
27350Sstevel@tonic-gate }
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate /*ARGSUSED*/
27380Sstevel@tonic-gate void
27390Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
27400Sstevel@tonic-gate {
27410Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
27420Sstevel@tonic-gate }
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate /*
27450Sstevel@tonic-gate  * This is called just after configure() in startup().
27460Sstevel@tonic-gate  *
27470Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
27480Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
27490Sstevel@tonic-gate  * given that various parts of the instruction set may appear
27500Sstevel@tonic-gate  * and disappear between different implementations.
27510Sstevel@tonic-gate  *
27520Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
27530Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
27540Sstevel@tonic-gate  * more flexibility.
27550Sstevel@tonic-gate  *
27560Sstevel@tonic-gate  * So, we just leave this alone.
27570Sstevel@tonic-gate  */
27580Sstevel@tonic-gate void
27590Sstevel@tonic-gate setx86isalist(void)
27600Sstevel@tonic-gate {
27610Sstevel@tonic-gate 	char *tp;
27620Sstevel@tonic-gate 	size_t len;
27630Sstevel@tonic-gate 	extern char *isa_list;
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate #define	TBUFSIZE	1024
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
27680Sstevel@tonic-gate 	*tp = '\0';
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate #if defined(__amd64)
27710Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
27720Sstevel@tonic-gate #endif
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 	switch (x86_vendor) {
27750Sstevel@tonic-gate 	case X86_VENDOR_Intel:
27760Sstevel@tonic-gate 	case X86_VENDOR_AMD:
27770Sstevel@tonic-gate 	case X86_VENDOR_TM:
27780Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
27790Sstevel@tonic-gate 			/*
27800Sstevel@tonic-gate 			 * Pentium Pro or later
27810Sstevel@tonic-gate 			 */
27820Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
27830Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
27840Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
27850Sstevel@tonic-gate 		}
27860Sstevel@tonic-gate 		/*FALLTHROUGH*/
27870Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
27880Sstevel@tonic-gate 		/*
27890Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
27900Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
27910Sstevel@tonic-gate 		 */
27920Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
27930Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
27940Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
27950Sstevel@tonic-gate 			    "+mmx pentium " : " ");
27960Sstevel@tonic-gate 		}
27970Sstevel@tonic-gate 		break;
27980Sstevel@tonic-gate 	default:
27990Sstevel@tonic-gate 		break;
28000Sstevel@tonic-gate 	}
28010Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
28020Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
28030Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
28040Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate #undef TBUFSIZE
28070Sstevel@tonic-gate }
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate #ifdef __amd64
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate void *
28130Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
28140Sstevel@tonic-gate {
28150Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
28160Sstevel@tonic-gate }
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate void
28190Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
28200Sstevel@tonic-gate {
28210Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
28220Sstevel@tonic-gate }
28230Sstevel@tonic-gate 
28243446Smrj #else /* __i386 */
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate void *
28270Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
28280Sstevel@tonic-gate {
28290Sstevel@tonic-gate 	caddr_t	vaddr;
28300Sstevel@tonic-gate 	uintptr_t v;
28310Sstevel@tonic-gate 	size_t	start;
28320Sstevel@tonic-gate 	size_t	end;
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
28350Sstevel@tonic-gate 	if (vaddr == NULL)
28360Sstevel@tonic-gate 		return (NULL);
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
28390Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
28403446Smrj 	ASSERT(v + size <= valloc_base);
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 	start = btop(v - kernelbase);
28430Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
28440Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
28450Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	while (start <= end) {
28480Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
28490Sstevel@tonic-gate 		++start;
28500Sstevel@tonic-gate 	}
28510Sstevel@tonic-gate 	return (vaddr);
28520Sstevel@tonic-gate }
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate void
28550Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
28560Sstevel@tonic-gate {
28570Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
28580Sstevel@tonic-gate 	size_t	start;
28590Sstevel@tonic-gate 	size_t	end;
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
28623446Smrj 	ASSERT(v + size <= valloc_base);
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	start = btop(v - kernelbase);
28650Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
28660Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
28670Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate 	while (start <= end) {
28700Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
28710Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
28720Sstevel@tonic-gate 		++start;
28730Sstevel@tonic-gate 	}
28740Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
28750Sstevel@tonic-gate }
28760Sstevel@tonic-gate 
28770Sstevel@tonic-gate /*
28780Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
28790Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
28800Sstevel@tonic-gate  */
28810Sstevel@tonic-gate void *
28820Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
28830Sstevel@tonic-gate {
28840Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
28850Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
28860Sstevel@tonic-gate 	size_t start;
28870Sstevel@tonic-gate 	size_t end;
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	/*
28900Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
28910Sstevel@tonic-gate 	 */
28920Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
28930Sstevel@tonic-gate 		return (NULL);
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate 	/*
28960Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
28970Sstevel@tonic-gate 	 */
28983446Smrj 	if (v >= valloc_base || eaddr < kernelbase)
28990Sstevel@tonic-gate 		return (NULL);
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate 	/*
29020Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
29030Sstevel@tonic-gate 	 */
29040Sstevel@tonic-gate 	if (v < kernelbase)
29050Sstevel@tonic-gate 		v = kernelbase;
29060Sstevel@tonic-gate 	start = btop(v - kernelbase);
29070Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
29080Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
29090Sstevel@tonic-gate 		end = toxic_bit_map_len;
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
29120Sstevel@tonic-gate 		return (NULL);
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate 	v = kernelbase + ptob(start);
29150Sstevel@tonic-gate 	if (len != NULL)
29160Sstevel@tonic-gate 		*len = ptob(end - start);
29170Sstevel@tonic-gate 	return ((void *)v);
29180Sstevel@tonic-gate }
29190Sstevel@tonic-gate 
29203446Smrj #endif	/* __i386 */
2921