xref: /onnv-gate/usr/src/uts/i86pc/os/startup.c (revision 7656:2621e50fdf4a)
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>
117*7656SSherry.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 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1440Sstevel@tonic-gate  * interface.
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * segkp
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate extern int segkp_fromheap;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static void kvm_init(void);
1540Sstevel@tonic-gate static void startup_init(void);
1550Sstevel@tonic-gate static void startup_memlist(void);
1563446Smrj static void startup_kmem(void);
1570Sstevel@tonic-gate static void startup_modules(void);
1580Sstevel@tonic-gate static void startup_vm(void);
1590Sstevel@tonic-gate static void startup_end(void);
1604828Sjosephb static void layout_kernel_va(void);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1640Sstevel@tonic-gate  */
1651479Skchow #ifdef __i386
1664828Sjosephb 
1671479Skchow /*
1681479Skchow  * Due to virtual address space limitations running in 32 bit mode, restrict
1694828Sjosephb  * the amount of physical memory configured to a max of PHYSMEM pages (16g).
1701479Skchow  *
1711479Skchow  * If the physical max memory size of 64g were allowed to be configured, the
1721479Skchow  * size of user virtual address space will be less than 1g. A limited user
1731479Skchow  * address space greatly reduces the range of applications that can run.
1741479Skchow  *
1754828Sjosephb  * If more physical memory than PHYSMEM is required, users should preferably
1764828Sjosephb  * run in 64 bit mode which has far looser virtual address space limitations.
1771479Skchow  *
1781479Skchow  * If 64 bit mode is not available (as in IA32) and/or more physical memory
1794828Sjosephb  * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
1801479Skchow  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
1811479Skchow  * should also be carefully tuned to balance out the need of the user
1821479Skchow  * application while minimizing the risk of kernel heap exhaustion due to
1831479Skchow  * kernelbase being set too high.
1841479Skchow  */
1854828Sjosephb #define	PHYSMEM	0x400000
1864828Sjosephb 
1874828Sjosephb #else /* __amd64 */
1881479Skchow 
1894828Sjosephb /*
1904828Sjosephb  * For now we can handle memory with physical addresses up to about
1914828Sjosephb  * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
1924828Sjosephb  * half the VA space for seg_kpm. When systems get bigger than 64TB this
1934828Sjosephb  * code will need revisiting. There is an implicit assumption that there
1944828Sjosephb  * are no *huge* holes in the physical address space too.
1954828Sjosephb  */
1964828Sjosephb #define	TERABYTE		(1ul << 40)
1974828Sjosephb #define	PHYSMEM_MAX64		mmu_btop(64 * TERABYTE)
1984828Sjosephb #define	PHYSMEM			PHYSMEM_MAX64
1994828Sjosephb #define	AMD64_VA_HOLE_END	0xFFFF800000000000ul
2004828Sjosephb 
2014828Sjosephb #endif /* __amd64 */
2024828Sjosephb 
2034828Sjosephb pgcnt_t physmem = PHYSMEM;
2040Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate char *kobj_file_buf;
2070Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
210*7656SSherry.Moore@Sun.COM caddr_t	rm_platter_va = 0;
2110Sstevel@tonic-gate uint32_t rm_platter_pa;
2120Sstevel@tonic-gate 
213841Skchow int	auto_lpg_disable = 1;
214841Skchow 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate uintptr_t hole_start, hole_end;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * kpm mapping window
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate caddr_t kpm_vbase;
2240Sstevel@tonic-gate size_t  kpm_size;
2254828Sjosephb static int kpm_desired;
2264828Sjosephb #ifdef __amd64
2274828Sjosephb static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
2284828Sjosephb #endif
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * Configuration parameters set at boot time.
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
2370Sstevel@tonic-gate struct bootops		**bootopsp;
2380Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate char bootblock_fstype[16];
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
243*7656SSherry.Moore@Sun.COM char kern_bootfile[OBP_MAXPATHLEN];
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2463290Sjohansen  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
2473290Sjohansen  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
2483290Sjohansen  * zio buffers from their own segment, otherwise they are allocated from the
2493290Sjohansen  * heap.  The optimization of allocating zio buffers from their own segment is
2503290Sjohansen  * only valid on 64-bit kernels.
2513290Sjohansen  */
2523290Sjohansen #if defined(__amd64)
2533290Sjohansen int segzio_fromheap = 0;
2543290Sjohansen #else
2553290Sjohansen int segzio_fromheap = 1;
2563290Sjohansen #endif
2573290Sjohansen 
2583290Sjohansen /*
2590Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
2600Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2610Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * VM data structures
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
2690Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2700Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
2714878Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
2724878Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
2734878Sblakej int pse_shift;			/* log2(pse_table_size) */
2740Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2750Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
2760Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
2770Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
2780Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
2813446Smrj static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
2823446Smrj 
2830Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate #if defined(__amd64)
2860Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
2870Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
2880Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
2890Sstevel@tonic-gate #else
2900Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
2910Sstevel@tonic-gate #endif
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
2943290Sjohansen caddr_t segzio_base;		/* Base address of segzio */
2950Sstevel@tonic-gate #if defined(__amd64)
2960Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
2970Sstevel@tonic-gate #else
2980Sstevel@tonic-gate pgcnt_t segkpsize = 0;
2990Sstevel@tonic-gate #endif
3003290Sjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
3010Sstevel@tonic-gate 
3023446Smrj /*
3033446Smrj  * VA range available to the debugger
3043446Smrj  */
3053446Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
3063446Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
3073446Smrj 
3080Sstevel@tonic-gate struct memseg *memseg_base;
3090Sstevel@tonic-gate struct vnode unused_pages_vp;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate #define	FOURGB	0x100000000LL
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate struct memlist *memlist;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
3160Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
3170Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
3180Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
3190Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
3200Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
3210Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
3220Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
3250Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*
3280Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
3290Sstevel@tonic-gate  */
3303446Smrj static pgcnt_t kphysm_init(page_t *, pgcnt_t);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate  * a couple useful roundup macros
3360Sstevel@tonic-gate  */
3370Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
3380Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
3390Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
3400Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
3410Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
3423446Smrj 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
3430Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
3440Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
3480Sstevel@tonic-gate  *		+-----------------------+
3493446Smrj  *		|			|
3500Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
3510Sstevel@tonic-gate  *		|	debugger	|
3520Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
3530Sstevel@tonic-gate  *		|      Kernel Data	|
3540Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
3550Sstevel@tonic-gate  *              |      Kernel Text	|
3565084Sjohnlev  * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
3573446Smrj  *		|---       GDT       ---|- GDT page (GDT_VA)
3583446Smrj  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
3593446Smrj  *		|			|
3603446Smrj  * 		|   page_t structures	|
3610Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
3620Sstevel@tonic-gate  * 		|   page hash, etc.	|
3633446Smrj  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
3643446Smrj  *		|			|  (segkp is just an arena in the heap)
3650Sstevel@tonic-gate  *		|			|
3660Sstevel@tonic-gate  *		|	kvseg		|
3670Sstevel@tonic-gate  *		|			|
3680Sstevel@tonic-gate  *		|			|
3690Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
3700Sstevel@tonic-gate  * 		|        Segkmap	|
3713446Smrj  * 0xC3002000  -|-----------------------|- segmap_start (floating)
3720Sstevel@tonic-gate  *		|	Red Zone	|
3730Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
3740Sstevel@tonic-gate  *		|			|			||
3750Sstevel@tonic-gate  *		|     Shared objects	|			\/
3760Sstevel@tonic-gate  *		|			|
3770Sstevel@tonic-gate  *		:			:
3780Sstevel@tonic-gate  *		|	user data	|
3790Sstevel@tonic-gate  *		|-----------------------|
3800Sstevel@tonic-gate  *		|	user text	|
3810Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
3820Sstevel@tonic-gate  *		|	user stack	|
3830Sstevel@tonic-gate  *		:			:
3840Sstevel@tonic-gate  *		|	invalid		|
3850Sstevel@tonic-gate  * 0x00000000	+-----------------------+
3860Sstevel@tonic-gate  *
3870Sstevel@tonic-gate  *
3880Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
3890Sstevel@tonic-gate  *			+-----------------------+
3903446Smrj  *			|			|
3910Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
3920Sstevel@tonic-gate  *			|	debugger (?)	|
3930Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
3940Sstevel@tonic-gate  *			|      unused    	|
3950Sstevel@tonic-gate  *			+-----------------------+
3960Sstevel@tonic-gate  *			|      Kernel Data	|
3970Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
3980Sstevel@tonic-gate  *			|      Kernel Text	|
3990Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
4003446Smrj  *			|---       GDT       ---|- GDT page (GDT_VA)
4013446Smrj  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
4023446Smrj  *			|			|
4030Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
4040Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
4050Sstevel@tonic-gate  *			|	 Kernel		|
4060Sstevel@tonic-gate  *			|	  heap		|
4070Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
4083446Smrj  *			|	 segmap		|
4093446Smrj  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
4100Sstevel@tonic-gate  *			|    device mappings	|
4110Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
4123290Sjohansen  *			|	  segzio	|
4133290Sjohansen  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
4140Sstevel@tonic-gate  *			|	  segkp		|
4153446Smrj  * ---                  |-----------------------|- segkp_base (floating)
4163446Smrj  * 			|   page_t structures	|  valloc_base + valloc_sz
4173446Smrj  * 			|   memsegs, memlists, 	|
4183446Smrj  * 			|   page hash, etc.	|
4194828Sjosephb  * 0xFFFFFF00.00000000  |-----------------------|- valloc_base (lower if > 1TB)
4200Sstevel@tonic-gate  *			|	 segkpm		|
4210Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
4220Sstevel@tonic-gate  *			|	Red Zone	|
4234828Sjosephb  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE (lower if > 1TB)
4240Sstevel@tonic-gate  *			|     User stack	|- User space memory
4250Sstevel@tonic-gate  * 			|			|
4260Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
4270Sstevel@tonic-gate  *			:			:
4280Sstevel@tonic-gate  * 			|			|
4290Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
4300Sstevel@tonic-gate  * 			|			|
4310Sstevel@tonic-gate  * 			| VA Hole / unused	|
4320Sstevel@tonic-gate  * 			|			|
4330Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
4340Sstevel@tonic-gate  *			|			|
4350Sstevel@tonic-gate  *			|			|
4360Sstevel@tonic-gate  *			:			:
4370Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
4380Sstevel@tonic-gate  *			|			|
4390Sstevel@tonic-gate  *			|	user data	|
4400Sstevel@tonic-gate  *			|-----------------------|
4410Sstevel@tonic-gate  *			|	user text	|
4420Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
4430Sstevel@tonic-gate  *			|	invalid		|
4440Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
4450Sstevel@tonic-gate  *
4460Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
4470Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
4480Sstevel@tonic-gate  *
4490Sstevel@tonic-gate  * Floating values:
4500Sstevel@tonic-gate  *
4510Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
4523446Smrj  * structures.  This region contains page_t structures for
4533446Smrj  * physical memory, memsegs, memlists, and the page hash.
4540Sstevel@tonic-gate  *
4550Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
4560Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
4570Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
4580Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
4590Sstevel@tonic-gate  * accessed using rip-relative addressing
4600Sstevel@tonic-gate  *
4610Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
4620Sstevel@tonic-gate  *
4630Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
4640Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
4650Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
4660Sstevel@tonic-gate  *
4673446Smrj  * segmap_start: start of segmap. The length of segmap can be modified
4680Sstevel@tonic-gate  * by changing segmapsize in /etc/system (preferred) or eeprom (deprecated).
4690Sstevel@tonic-gate  * The default length is 16MB on 32-bit systems and 64MB on 64-bit systems.
4700Sstevel@tonic-gate  *
4710Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
4720Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
4730Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
4740Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
4750Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
4760Sstevel@tonic-gate  * segkp sizes).
4770Sstevel@tonic-gate  *
4780Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
4790Sstevel@tonic-gate  *	size	page_t's			size
4800Sstevel@tonic-gate  *	----	---------	----------	-----------
4810Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
4820Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
4830Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
4840Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
4850Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
4860Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
4870Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
4880Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
4890Sstevel@tonic-gate  *
4900Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
4910Sstevel@tonic-gate  * configurations above 8gb.
4920Sstevel@tonic-gate  *
4930Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
4940Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
4950Sstevel@tonic-gate  */
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /* real-time-clock initialization parameters */
4983446Smrj extern time_t process_rtc_config_file(void);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate uintptr_t	kernelbase;
5013446Smrj uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
5020Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
5030Sstevel@tonic-gate size_t		segmapsize;
5043446Smrj uintptr_t	segmap_start;
5050Sstevel@tonic-gate int		segmapfreelists;
5060Sstevel@tonic-gate pgcnt_t		npages;
5073446Smrj pgcnt_t		orig_npages;
5080Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
5090Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate /*
5120Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
5130Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
5140Sstevel@tonic-gate  * the bootstrap.
5150Sstevel@tonic-gate  */
5163446Smrj static page_t *bootpages;
5173446Smrj 
5183446Smrj /*
5193446Smrj  * boot time pages that have a vnode from the ramdisk will keep that forever.
5203446Smrj  */
5213446Smrj static page_t *rd_pages;
5220Sstevel@tonic-gate 
523*7656SSherry.Moore@Sun.COM /*
524*7656SSherry.Moore@Sun.COM  * Lower 64K
525*7656SSherry.Moore@Sun.COM  */
526*7656SSherry.Moore@Sun.COM static page_t *lower_pages = NULL;
527*7656SSherry.Moore@Sun.COM static int lower_pages_count = 0;
528*7656SSherry.Moore@Sun.COM 
5290Sstevel@tonic-gate struct system_hardware system_hardware;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate /*
5325741Smrj  * Is this Solaris instance running in a fully virtualized xVM domain?
5335741Smrj  */
5345741Smrj int xpv_is_hvm = 0;
5355741Smrj 
5365741Smrj /*
5370Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
5380Sstevel@tonic-gate  */
5390Sstevel@tonic-gate static void
5403446Smrj print_memlist(char *title, struct memlist *mp)
5410Sstevel@tonic-gate {
5420Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
5430Sstevel@tonic-gate 	while (mp != NULL)  {
5440Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
5450Sstevel@tonic-gate 		    mp->address, mp->size);
5460Sstevel@tonic-gate 		mp = mp->next;
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate /*
5510Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
5520Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
5530Sstevel@tonic-gate  */
5540Sstevel@tonic-gate int	l2cache_sz = 0x80000;
5550Sstevel@tonic-gate int	l2cache_linesz = 0x40;
5560Sstevel@tonic-gate int	l2cache_assoc = 1;
5570Sstevel@tonic-gate 
5584426Saguzovsk static size_t	textrepl_min_gb = 10;
5594426Saguzovsk 
5600Sstevel@tonic-gate /*
5610Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
5620Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
5630Sstevel@tonic-gate  */
5640Sstevel@tonic-gate #ifdef __amd64
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate vmem_t		*device_arena;
5670Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
5683446Smrj size_t		toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate #else	/* __i386 */
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
5730Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate #endif	/* __i386 */
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  * Simple boot time debug facilities
5790Sstevel@tonic-gate  */
5800Sstevel@tonic-gate static char *prm_dbg_str[] = {
5810Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%x\n",
5820Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%llx\n"
5830Sstevel@tonic-gate };
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate int prom_debug;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate #define	PRM_DEBUG(q)	if (prom_debug) 	\
5880Sstevel@tonic-gate 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
5890Sstevel@tonic-gate #define	PRM_POINT(q)	if (prom_debug) 	\
5900Sstevel@tonic-gate 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
5940Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
5950Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
5960Sstevel@tonic-gate  */
5970Sstevel@tonic-gate #define	NUM_ALLOCATIONS 7
5980Sstevel@tonic-gate int num_allocations = 0;
5990Sstevel@tonic-gate struct {
6000Sstevel@tonic-gate 	void **al_ptr;
6010Sstevel@tonic-gate 	size_t al_size;
6020Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
6030Sstevel@tonic-gate size_t valloc_sz = 0;
6040Sstevel@tonic-gate uintptr_t valloc_base;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
6070Sstevel@tonic-gate 		size = ROUND_UP_PAGE(size);		 		\
6080Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
6090Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
6100Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
6110Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
6120Sstevel@tonic-gate 		valloc_sz += size;					\
6130Sstevel@tonic-gate 		++num_allocations;				 	\
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate 
6163446Smrj /*
6173446Smrj  * Allocate all the initial memory needed by the page allocator.
6183446Smrj  */
6190Sstevel@tonic-gate static void
6200Sstevel@tonic-gate perform_allocations(void)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	caddr_t mem;
6230Sstevel@tonic-gate 	int i;
6243446Smrj 	int valloc_align;
6250Sstevel@tonic-gate 
6263446Smrj 	PRM_DEBUG(valloc_base);
6273446Smrj 	PRM_DEBUG(valloc_sz);
6283446Smrj 	valloc_align = mmu.level_size[mmu.max_page_level > 0];
6293446Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
6300Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
6310Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
6320Sstevel@tonic-gate 	bzero(mem, valloc_sz);
6330Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
6340Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
6350Sstevel@tonic-gate 		mem += allocations[i].al_size;
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate /*
6400Sstevel@tonic-gate  * Our world looks like this at startup time.
6410Sstevel@tonic-gate  *
6420Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
6430Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
6440Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
6450Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
6460Sstevel@tonic-gate  *
6470Sstevel@tonic-gate  * On the text page:
6480Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
6490Sstevel@tonic-gate  *
6500Sstevel@tonic-gate  * On the data page:
6513446Smrj  * unix/genunix/krtld/module data loads.
6523446Smrj  *
6530Sstevel@tonic-gate  * Machine-dependent startup code
6540Sstevel@tonic-gate  */
6550Sstevel@tonic-gate void
6560Sstevel@tonic-gate startup(void)
6570Sstevel@tonic-gate {
6585084Sjohnlev #if !defined(__xpv)
6591494Sjg 	extern void startup_bios_disk(void);
6601494Sjg 	extern void startup_pci_bios(void);
661*7656SSherry.Moore@Sun.COM 	extern int post_fastreboot;
6625084Sjohnlev #endif
6636336Sbholler 	extern cpuset_t cpu_ready_set;
6646336Sbholler 
6655084Sjohnlev 	/*
6665084Sjohnlev 	 * Make sure that nobody tries to use sekpm until we have
6675084Sjohnlev 	 * initialized it properly.
6685084Sjohnlev 	 */
6690Sstevel@tonic-gate #if defined(__amd64)
6704828Sjosephb 	kpm_desired = 1;
6710Sstevel@tonic-gate #endif
6720Sstevel@tonic-gate 	kpm_enable = 0;
6736336Sbholler 	CPUSET_ONLY(cpu_ready_set, 0);	/* cpu 0 is boot cpu */
6740Sstevel@tonic-gate 
6755084Sjohnlev #if defined(__xpv)	/* XXPV fix me! */
6765084Sjohnlev 	{
6775084Sjohnlev 		extern int segvn_use_regions;
6785084Sjohnlev 		segvn_use_regions = 0;
6795084Sjohnlev 	}
6805084Sjohnlev #endif
6810Sstevel@tonic-gate 	progressbar_init();
6820Sstevel@tonic-gate 	startup_init();
6835316Sjohnlev #if defined(__xpv)
6845316Sjohnlev 	startup_xen_version();
6855316Sjohnlev #endif
6860Sstevel@tonic-gate 	startup_memlist();
6873446Smrj 	startup_kmem();
6884828Sjosephb 	startup_vm();
6895084Sjohnlev #if !defined(__xpv)
690*7656SSherry.Moore@Sun.COM 	if (!post_fastreboot)
691*7656SSherry.Moore@Sun.COM 		startup_pci_bios();
6925084Sjohnlev #endif
6937532SSean.Ye@Sun.COM #if defined(__xpv)
6947532SSean.Ye@Sun.COM 	startup_xen_mca();
6957532SSean.Ye@Sun.COM #endif
6960Sstevel@tonic-gate 	startup_modules();
6975084Sjohnlev #if !defined(__xpv)
698*7656SSherry.Moore@Sun.COM 	if (!post_fastreboot)
699*7656SSherry.Moore@Sun.COM 		startup_bios_disk();
7005084Sjohnlev #endif
7010Sstevel@tonic-gate 	startup_end();
7020Sstevel@tonic-gate 	progressbar_start();
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate static void
7060Sstevel@tonic-gate startup_init()
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/*
7110Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
7120Sstevel@tonic-gate 	 */
7130Sstevel@tonic-gate 	cpuid_pass2(CPU);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/*
7180Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
7190Sstevel@tonic-gate 	 */
7200Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
7210Sstevel@tonic-gate 		++prom_debug;
7220Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/*
7260Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
7270Sstevel@tonic-gate 	 */
7280Sstevel@tonic-gate 	get_system_configuration();
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	/*
7310Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
7320Sstevel@tonic-gate 	 */
7330Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
7340Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
7350Sstevel@tonic-gate 		    CPU->cpu_brandstr);
7360Sstevel@tonic-gate 		halt("This processor is not supported by this release "
7370Sstevel@tonic-gate 		    "of Solaris.");
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate /*
7440Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
7450Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
7460Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
7470Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
7480Sstevel@tonic-gate  */
7490Sstevel@tonic-gate static void
7500Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate 	uintptr_t va;
7530Sstevel@tonic-gate 	uintptr_t next_va;
7540Sstevel@tonic-gate 	pfn_t pfn;
7550Sstevel@tonic-gate 	uint64_t pfn_addr;
7560Sstevel@tonic-gate 	uint64_t pfn_eaddr;
7570Sstevel@tonic-gate 	uint_t prot;
7580Sstevel@tonic-gate 	size_t len;
7590Sstevel@tonic-gate 	uint_t change;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	if (prom_debug)
7620Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
7630Sstevel@tonic-gate 		    *addr, *size);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/*
7660Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	if (*addr == 0) {
7690Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
7700Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	/*
7743446Smrj 	 * First we trim from the front of the range. Since kbm_probe()
7750Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
7760Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
7770Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
7780Sstevel@tonic-gate 	 * but w < v.
7790Sstevel@tonic-gate 	 */
7800Sstevel@tonic-gate 	do {
7810Sstevel@tonic-gate 		change = 0;
7820Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
7833446Smrj 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7840Sstevel@tonic-gate 		    va = next_va) {
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 			next_va = va + len;
7873446Smrj 			pfn_addr = pfn_to_pa(pfn);
7880Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
7910Sstevel@tonic-gate 				change = 1;
7920Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
7930Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
7940Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
7950Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
7960Sstevel@tonic-gate 				}
7970Sstevel@tonic-gate 			}
7980Sstevel@tonic-gate 		}
7990Sstevel@tonic-gate 		if (change && prom_debug)
8000Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
8010Sstevel@tonic-gate 			    *addr, *size);
8020Sstevel@tonic-gate 	} while (change);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	/*
8050Sstevel@tonic-gate 	 * Trim pages from the end of the range.
8060Sstevel@tonic-gate 	 */
8070Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
8083446Smrj 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
8090Sstevel@tonic-gate 	    va = next_va) {
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 		next_va = va + len;
8123446Smrj 		pfn_addr = pfn_to_pa(pfn);
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
8150Sstevel@tonic-gate 			*size = pfn_addr - *addr;
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	if (prom_debug)
8190Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
8200Sstevel@tonic-gate 		    *addr, *size);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate static void
8240Sstevel@tonic-gate kpm_init()
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	struct segkpm_crargs b;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	/*
8290Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
8300Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
8310Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
8320Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
8330Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
8340Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
8350Sstevel@tonic-gate 	 * pagesize is used.
8360Sstevel@tonic-gate 	 */
8370Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
8380Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
8390Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
8400Sstevel@tonic-gate 	kpmp2pshft = 0;
8410Sstevel@tonic-gate 	kpmpnpgs = 1;
8420Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
8450Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
8480Sstevel@tonic-gate 		panic("cannot attach segkpm");
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
8510Sstevel@tonic-gate 	b.nvcolors = 1;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
8540Sstevel@tonic-gate 		panic("segkpm_create segkpm");
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate /*
8603446Smrj  * The debug info page provides enough information to allow external
8613446Smrj  * inspectors (e.g. when running under a hypervisor) to bootstrap
8623446Smrj  * themselves into allowing full-blown kernel debugging.
8633446Smrj  */
8643446Smrj static void
8653446Smrj init_debug_info(void)
8663446Smrj {
8673446Smrj 	caddr_t mem;
8683446Smrj 	debug_info_t *di;
8693446Smrj 
8703446Smrj #ifndef __lint
8713446Smrj 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
8723446Smrj #endif
8733446Smrj 
8743446Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
8753446Smrj 	    MMU_PAGESIZE);
8763446Smrj 
8773446Smrj 	if (mem != (caddr_t)DEBUG_INFO_VA)
8783446Smrj 		panic("BOP_ALLOC() failed");
8793446Smrj 	bzero(mem, MMU_PAGESIZE);
8803446Smrj 
8813446Smrj 	di = (debug_info_t *)mem;
8823446Smrj 
8833446Smrj 	di->di_magic = DEBUG_INFO_MAGIC;
8843446Smrj 	di->di_version = DEBUG_INFO_VERSION;
8855084Sjohnlev 	di->di_modules = (uintptr_t)&modules;
8865084Sjohnlev 	di->di_s_text = (uintptr_t)s_text;
8875084Sjohnlev 	di->di_e_text = (uintptr_t)e_text;
8885084Sjohnlev 	di->di_s_data = (uintptr_t)s_data;
8895084Sjohnlev 	di->di_e_data = (uintptr_t)e_data;
8905084Sjohnlev 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
8915084Sjohnlev 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
8923446Smrj }
8933446Smrj 
8943446Smrj /*
8953446Smrj  * Build the memlists and other kernel essential memory system data structures.
8963446Smrj  * This is everything at valloc_base.
8970Sstevel@tonic-gate  */
8980Sstevel@tonic-gate static void
8990Sstevel@tonic-gate startup_memlist(void)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	size_t memlist_sz;
9020Sstevel@tonic-gate 	size_t memseg_sz;
9030Sstevel@tonic-gate 	size_t pagehash_sz;
9040Sstevel@tonic-gate 	size_t pp_sz;
9050Sstevel@tonic-gate 	uintptr_t va;
9060Sstevel@tonic-gate 	size_t len;
9070Sstevel@tonic-gate 	uint_t prot;
9080Sstevel@tonic-gate 	pfn_t pfn;
9090Sstevel@tonic-gate 	int memblocks;
9100Sstevel@tonic-gate 	caddr_t pagecolor_mem;
9110Sstevel@tonic-gate 	size_t pagecolor_memsz;
9120Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
9130Sstevel@tonic-gate 	size_t page_ctrs_size;
9144878Sblakej 	size_t pse_table_alloc_size;
9150Sstevel@tonic-gate 	struct memlist *current;
9160Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
9190Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
9200Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	/*
9250Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
9260Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
9270Sstevel@tonic-gate 	 */
9283446Smrj 	len = kbm_nucleus_size;
9293446Smrj 	ASSERT(len > MMU_PAGESIZE);
9300Sstevel@tonic-gate 
9313446Smrj 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
9323446Smrj 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
9333446Smrj 	if (e_moddata - moddata > MODDATA)
9343446Smrj 		e_moddata = moddata + MODDATA;
9350Sstevel@tonic-gate 
9363446Smrj 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
9373446Smrj 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
9383446Smrj 	if (e_modtext - modtext > MODTEXT)
9393446Smrj 		e_modtext = modtext + MODTEXT;
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	econtig = e_moddata;
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9440Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
9450Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9460Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
9470Sstevel@tonic-gate 	PRM_DEBUG(econtig);
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	/*
9503446Smrj 	 * Examine the boot loader physical memory map to find out:
9510Sstevel@tonic-gate 	 * - total memory in system - physinstalled
9520Sstevel@tonic-gate 	 * - the max physical address - physmax
9533446Smrj 	 * - the number of discontiguous segments of memory.
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 	if (prom_debug)
9563446Smrj 		print_memlist("boot physinstalled",
9570Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
9580Sstevel@tonic-gate 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
9590Sstevel@tonic-gate 	    &physinstalled, &memblocks);
9600Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9610Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9620Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/*
9650Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
9660Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
9670Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
9680Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
9690Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
9700Sstevel@tonic-gate 	 */
9710Sstevel@tonic-gate 	mmu_init();
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate #ifdef	__i386
9740Sstevel@tonic-gate 	/*
9750Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
9760Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
9770Sstevel@tonic-gate 	 */
9780Sstevel@tonic-gate 	if (mmu.pae_hat) {
9790Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
9800Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
9810Sstevel@tonic-gate 			physmax = PFN_64G - 1;
9820Sstevel@tonic-gate 		}
9830Sstevel@tonic-gate 	} else {
9840Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
9850Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
9860Sstevel@tonic-gate 			physmax = PFN_4G - 1;
9870Sstevel@tonic-gate 		}
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate #endif
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
9940Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
9950Sstevel@tonic-gate 		char value[8];
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 		if (len < 8)
9980Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
9990Sstevel@tonic-gate 		else
10000Sstevel@tonic-gate 			(void) strcpy(value, "");
10010Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
10020Sstevel@tonic-gate 			mmu.pt_nx = 0;
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/*
10070Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
10080Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
10090Sstevel@tonic-gate 	 *
10100Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
10130Sstevel@tonic-gate 	obp_pages = 0;
10140Sstevel@tonic-gate 	va = KERNEL_TEXT;
10153446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
10160Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
10170Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
10180Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
10190Sstevel@tonic-gate 		va += len;
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 	PRM_DEBUG(npages);
10220Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	/*
10254828Sjosephb 	 * If physmem is patched to be non-zero, use it instead of the computed
10264828Sjosephb 	 * value unless it is larger than the actual amount of memory on hand.
10270Sstevel@tonic-gate 	 */
10281417Skchow 	if (physmem == 0 || physmem > npages) {
10290Sstevel@tonic-gate 		physmem = npages;
10301417Skchow 	} else if (physmem < npages) {
10311479Skchow 		orig_npages = npages;
10320Sstevel@tonic-gate 		npages = physmem;
10331417Skchow 	}
10340Sstevel@tonic-gate 	PRM_DEBUG(physmem);
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	/*
10370Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
10380Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
10390Sstevel@tonic-gate 	 * include:
10400Sstevel@tonic-gate 	 *	memsegs
10410Sstevel@tonic-gate 	 *	memlists
10420Sstevel@tonic-gate 	 *	page hash table
10430Sstevel@tonic-gate 	 *	page_t's
10440Sstevel@tonic-gate 	 *	page coloring data structs
10450Sstevel@tonic-gate 	 */
10460Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
10470Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
10480Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	/*
10513446Smrj 	 * Reserve space for memlists. There's no real good way to know exactly
10523446Smrj 	 * how much room we'll need, but this should be a good upper bound.
10530Sstevel@tonic-gate 	 */
10540Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
10550Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
10560Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
10570Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	/*
10600Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
10610Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
10620Sstevel@tonic-gate 	 */
10630Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
10640Sstevel@tonic-gate 	page_hashsz = 1 << highbit(page_hashsz);
10650Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
10660Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
10670Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	/*
10703446Smrj 	 * Set aside room for the page structures themselves.
10710Sstevel@tonic-gate 	 */
10723446Smrj 	PRM_DEBUG(npages);
10733446Smrj 	pp_sz = sizeof (struct page) * npages;
10740Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
10750Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	/*
10780Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
10790Sstevel@tonic-gate 	 */
10800Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
10810Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
10820Sstevel@tonic-gate 	pagecolor_memsz =
10830Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
10840Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
10850Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
10880Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
10890Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
10900Sstevel@tonic-gate 
10914878Sblakej 	/*
10924878Sblakej 	 * Allocate the array that protects pp->p_selock.
10934878Sblakej 	 */
10944878Sblakej 	pse_shift = size_pse_array(physmem, max_ncpus);
10954878Sblakej 	pse_table_size = 1 << pse_shift;
10964878Sblakej 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
10974878Sblakej 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
10984878Sblakej 
10993446Smrj #if defined(__amd64)
11003446Smrj 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
11013446Smrj 	valloc_base = VALLOC_BASE;
11024828Sjosephb 
11034828Sjosephb 	/*
11044828Sjosephb 	 * The default values of VALLOC_BASE and SEGKPM_BASE should work
11054828Sjosephb 	 * for values of physmax up to 1 Terabyte. They need adjusting when
11064828Sjosephb 	 * memory is at addresses above 1 TB.
11074828Sjosephb 	 */
11084828Sjosephb 	if (physmax + 1 > mmu_btop(TERABYTE)) {
11094828Sjosephb 		uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
11104828Sjosephb 
11114828Sjosephb 		/* Round to largest possible pagesize for now */
11124828Sjosephb 		kpm_resv_amount = P2ROUNDUP(kpm_resv_amount, ONE_GIG);
11134828Sjosephb 
11144828Sjosephb 		segkpm_base = -(2 * kpm_resv_amount); /* down from top VA */
11154828Sjosephb 
11164828Sjosephb 		/* make sure we leave some space for user apps above hole */
11174828Sjosephb 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
11184828Sjosephb 		if (segkpm_base > SEGKPM_BASE)
11194828Sjosephb 			segkpm_base = SEGKPM_BASE;
11204828Sjosephb 		PRM_DEBUG(segkpm_base);
11214828Sjosephb 
11224828Sjosephb 		valloc_base = segkpm_base + kpm_resv_amount;
11234828Sjosephb 		PRM_DEBUG(valloc_base);
11244828Sjosephb 	}
11253446Smrj #else	/* __i386 */
11263446Smrj 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
11273446Smrj 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
11284828Sjosephb 	PRM_DEBUG(valloc_base);
11293446Smrj #endif	/* __i386 */
11303446Smrj 
11310Sstevel@tonic-gate 	/*
11323446Smrj 	 * do all the initial allocations
11333446Smrj 	 */
11343446Smrj 	perform_allocations();
11353446Smrj 
11363446Smrj 	/*
11373446Smrj 	 * Build phys_install and phys_avail in kernel memspace.
11383446Smrj 	 * - phys_install should be all memory in the system.
11393446Smrj 	 * - phys_avail is phys_install minus any memory mapped before this
11403446Smrj 	 *    point above KERNEL_TEXT.
11413446Smrj 	 */
11423446Smrj 	current = phys_install = memlist;
11433446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
11443446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
11453446Smrj 		panic("physinstalled was too big!");
11463446Smrj 	if (prom_debug)
11473446Smrj 		print_memlist("phys_install", phys_install);
11483446Smrj 
11493446Smrj 	phys_avail = current;
11503446Smrj 	PRM_POINT("Building phys_avail:\n");
11513446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
11523446Smrj 	    avail_filter);
11533446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
11543446Smrj 		panic("physavail was too big!");
11553446Smrj 	if (prom_debug)
11563446Smrj 		print_memlist("phys_avail", phys_avail);
11573446Smrj 
11583446Smrj 	/*
11593446Smrj 	 * setup page coloring
11600Sstevel@tonic-gate 	 */
11613446Smrj 	page_coloring_setup(pagecolor_mem);
11623446Smrj 	page_lock_init();	/* currently a no-op */
11633446Smrj 
11643446Smrj 	/*
11653446Smrj 	 * free page list counters
11663446Smrj 	 */
11673446Smrj 	(void) page_ctrs_alloc(page_ctrs_mem);
11683446Smrj 
11693446Smrj 	/*
11706880Sdv142724 	 * Size the pcf array based on the number of cpus in the box at
11716880Sdv142724 	 * boot time.
11726880Sdv142724 	 */
11736880Sdv142724 
11746880Sdv142724 	pcf_init();
11756880Sdv142724 
11766880Sdv142724 	/*
11773446Smrj 	 * Initialize the page structures from the memory lists.
11783446Smrj 	 */
11793446Smrj 	availrmem_initial = availrmem = freemem = 0;
11803446Smrj 	PRM_POINT("Calling kphysm_init()...");
11813446Smrj 	npages = kphysm_init(pp_base, npages);
11823446Smrj 	PRM_POINT("kphysm_init() done");
11833446Smrj 	PRM_DEBUG(npages);
11843446Smrj 
11853446Smrj 	init_debug_info();
11863446Smrj 
11873446Smrj 	/*
11883446Smrj 	 * Now that page_t's have been initialized, remove all the
11893446Smrj 	 * initial allocation pages from the kernel free page lists.
11903446Smrj 	 */
11913446Smrj 	boot_mapin((caddr_t)valloc_base, valloc_sz);
11925460Sjosephb 	boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
11933446Smrj 	PRM_POINT("startup_memlist() done");
11943446Smrj 
11953446Smrj 	PRM_DEBUG(valloc_sz);
11964426Saguzovsk 
11974748Saguzovsk #if defined(__amd64)
11984748Saguzovsk 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
11994748Saguzovsk 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
12004748Saguzovsk 		extern size_t textrepl_size_thresh;
12014426Saguzovsk 		textrepl_size_thresh = (16 << 20) - 1;
12024426Saguzovsk 	}
12034748Saguzovsk #endif
12043446Smrj }
12053446Smrj 
12063446Smrj /*
12073446Smrj  * Layout the kernel's part of address space and initialize kmem allocator.
12083446Smrj  */
12093446Smrj static void
12103446Smrj startup_kmem(void)
12113446Smrj {
12123717Sdp78419 	extern void page_set_colorequiv_arr(void);
12133717Sdp78419 
12143446Smrj 	PRM_POINT("startup_kmem() starting...");
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate #if defined(__amd64)
12170Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
12180Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
12190Sstevel@tonic-gate 		    "systems.");
12204828Sjosephb 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
12210Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
12223446Smrj 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
12230Sstevel@tonic-gate #else	/* __i386 */
12240Sstevel@tonic-gate 	/*
12250Sstevel@tonic-gate 	 * We configure kernelbase based on:
12260Sstevel@tonic-gate 	 *
12270Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
12280Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
12290Sstevel@tonic-gate 	 *
12300Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
12310Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
12320Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
12330Sstevel@tonic-gate 	 *
12340Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
12350Sstevel@tonic-gate 	 */
12360Sstevel@tonic-gate 	if (eprom_kernelbase) {
12370Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
12380Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
12390Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
12400Sstevel@tonic-gate 	} else {
12410Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
12420Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
12453446Smrj 	core_base = valloc_base;
12460Sstevel@tonic-gate 	core_size = 0;
12473446Smrj #endif	/* __i386 */
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	PRM_DEBUG(core_base);
12500Sstevel@tonic-gate 	PRM_DEBUG(core_size);
12513446Smrj 	PRM_DEBUG(kernelbase);
12520Sstevel@tonic-gate 
12534828Sjosephb #if defined(__i386)
12544828Sjosephb 	segkp_fromheap = 1;
12554828Sjosephb #endif	/* __i386 */
12563446Smrj 
12573446Smrj 	ekernelheap = (char *)core_base;
12583446Smrj 	PRM_DEBUG(ekernelheap);
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	/*
12610Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
12620Sstevel@tonic-gate 	 * update variables that were initialized with a value of
12630Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
12640Sstevel@tonic-gate 	 *
12650Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
12660Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
12670Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
12680Sstevel@tonic-gate 	 *	just be declared as variables there?
12690Sstevel@tonic-gate 	 */
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
12720Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
12734875Sjosephb #if defined(__amd64)
12744875Sjosephb 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
12754875Sjosephb #else
12760Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
12770Sstevel@tonic-gate #endif
12780Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
12790Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
12800Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
12810Sstevel@tonic-gate 
12824828Sjosephb 	layout_kernel_va();
12834828Sjosephb 
12844828Sjosephb #if defined(__i386)
12854828Sjosephb 	/*
12864828Sjosephb 	 * If segmap is too large we can push the bottom of the kernel heap
12874828Sjosephb 	 * higher than the base.  Or worse, it could exceed the top of the
12884828Sjosephb 	 * VA space entirely, causing it to wrap around.
12894828Sjosephb 	 */
12904828Sjosephb 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
12914828Sjosephb 		panic("too little address space available for kernelheap,"
12924828Sjosephb 		    " use eeprom for lower kernelbase or smaller segmapsize");
12934828Sjosephb #endif	/* __i386 */
12944828Sjosephb 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
12970Sstevel@tonic-gate 	 */
12984828Sjosephb 	kernelheap_init(kernelheap, ekernelheap,
12994828Sjosephb 	    kernelheap + MMU_PAGESIZE,
13003446Smrj 	    (void *)core_base, (void *)(core_base + core_size));
13010Sstevel@tonic-gate 
13025084Sjohnlev #if defined(__xpv)
13035084Sjohnlev 	/*
13045084Sjohnlev 	 * Link pending events struct into cpu struct
13055084Sjohnlev 	 */
13065084Sjohnlev 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
13075084Sjohnlev #endif
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
13100Sstevel@tonic-gate 	 */
13110Sstevel@tonic-gate 	kmem_init();
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	/*
13143717Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
13153717Sdp78419 	 */
13163717Sdp78419 	page_set_colorequiv_arr();
13173717Sdp78419 
13183717Sdp78419 	/*
13190Sstevel@tonic-gate 	 * print this out early so that we know what's going on
13200Sstevel@tonic-gate 	 */
13210Sstevel@tonic-gate 	cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE);
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	/*
13240Sstevel@tonic-gate 	 * Initialize bp_mapin().
13250Sstevel@tonic-gate 	 */
13260Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
13270Sstevel@tonic-gate 
13281479Skchow 	/*
13291479Skchow 	 * orig_npages is non-zero if physmem has been configured for less
13301479Skchow 	 * than the available memory.
13311479Skchow 	 */
13321479Skchow 	if (orig_npages) {
13334828Sjosephb 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
13344828Sjosephb 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
13354828Sjosephb 		    npages, orig_npages);
13361479Skchow 	}
13370Sstevel@tonic-gate #if defined(__i386)
13380Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
13390Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
13400Sstevel@tonic-gate 		    "System using 0x%lx",
13410Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
13420Sstevel@tonic-gate #endif
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
13450Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
13460Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
13470Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate #endif
13500Sstevel@tonic-gate 
13515084Sjohnlev #ifdef __xpv
13525084Sjohnlev 	/*
13535084Sjohnlev 	 * Some of the xen start information has to be relocated up
13545084Sjohnlev 	 * into the kernel's permanent address space.
13555084Sjohnlev 	 */
13565084Sjohnlev 	PRM_POINT("calling xen_relocate_start_info()");
13575084Sjohnlev 	xen_relocate_start_info();
13585084Sjohnlev 	PRM_POINT("xen_relocate_start_info() done");
13595084Sjohnlev 
13605084Sjohnlev 	/*
13615084Sjohnlev 	 * (Update the vcpu pointer in our cpu structure to point into
13625084Sjohnlev 	 * the relocated shared info.)
13635084Sjohnlev 	 */
13645084Sjohnlev 	CPU->cpu_m.mcpu_vcpu_info =
13655084Sjohnlev 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
13665084Sjohnlev #endif
13675084Sjohnlev 
13683446Smrj 	PRM_POINT("startup_kmem() done");
13690Sstevel@tonic-gate }
13700Sstevel@tonic-gate 
13715741Smrj #ifndef __xpv
13725741Smrj /*
13735741Smrj  * If we have detected that we are running in an HVM environment, we need
13745741Smrj  * to prepend the PV driver directory to the module search path.
13755741Smrj  */
13765741Smrj #define	HVM_MOD_DIR "/platform/i86hvm/kernel"
13775741Smrj static void
13785741Smrj update_default_path()
13795741Smrj {
13805741Smrj 	char *current, *newpath;
13815741Smrj 	int newlen;
13825741Smrj 
13835741Smrj 	/*
13845741Smrj 	 * We are about to resync with krtld.  krtld will reset its
13855741Smrj 	 * internal module search path iff Solaris has set default_path.
13865741Smrj 	 * We want to be sure we're prepending this new directory to the
13875741Smrj 	 * right search path.
13885741Smrj 	 */
13895741Smrj 	current = (default_path == NULL) ? kobj_module_path : default_path;
13905741Smrj 
13915741Smrj 	newlen = strlen(HVM_MOD_DIR) + strlen(current) + 1;
13925741Smrj 	newpath = kmem_alloc(newlen, KM_SLEEP);
13935741Smrj 	(void) strcpy(newpath, HVM_MOD_DIR);
13945741Smrj 	(void) strcat(newpath, " ");
13955741Smrj 	(void) strcat(newpath, current);
13965741Smrj 
13975741Smrj 	default_path = newpath;
13985741Smrj }
13995741Smrj #endif
14005741Smrj 
14010Sstevel@tonic-gate static void
14020Sstevel@tonic-gate startup_modules(void)
14030Sstevel@tonic-gate {
14040Sstevel@tonic-gate 	unsigned int i;
14050Sstevel@tonic-gate 	extern void prom_setup(void);
14067532SSean.Ye@Sun.COM 	cmi_hdl_t hdl;
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
14095084Sjohnlev 
14105084Sjohnlev #ifndef __xpv
14110Sstevel@tonic-gate 	/*
14120Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
14130Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
14140Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
14150Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
14160Sstevel@tonic-gate 	 */
14170Sstevel@tonic-gate 	microfind();
14185741Smrj 
14195741Smrj 	if (xpv_is_hvm)
14205741Smrj 		update_default_path();
14215084Sjohnlev #endif
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	/*
14240Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
14250Sstevel@tonic-gate 	 */
14263446Smrj 	sgmtl(process_rtc_config_file());
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	/*
14290Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
14300Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
14310Sstevel@tonic-gate 	 */
14320Sstevel@tonic-gate 	param_calc(0);
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	mod_setup();
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	/*
14370Sstevel@tonic-gate 	 * Initialize system parameters.
14380Sstevel@tonic-gate 	 */
14390Sstevel@tonic-gate 	param_init();
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	/*
14422712Snn35248 	 * Initialize the default brands
14432712Snn35248 	 */
14442712Snn35248 	brand_init();
14452712Snn35248 
14462712Snn35248 	/*
14470Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
14480Sstevel@tonic-gate 	 */
14490Sstevel@tonic-gate 	maxmem = physmem;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	/*
14520Sstevel@tonic-gate 	 * Initialize segment management stuff.
14530Sstevel@tonic-gate 	 */
14540Sstevel@tonic-gate 	seg_init();
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
14570Sstevel@tonic-gate 		halt("Can't load specfs");
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
14600Sstevel@tonic-gate 		halt("Can't load devfs");
14610Sstevel@tonic-gate 
14622621Sllai1 	if (modload("fs", "dev") == -1)
14632621Sllai1 		halt("Can't load dev");
14642621Sllai1 
14651676Sjpk 	(void) modloadonly("sys", "lbl_edition");
14661676Sjpk 
14670Sstevel@tonic-gate 	dispinit();
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	/*
14700Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
14710Sstevel@tonic-gate 	 */
14720Sstevel@tonic-gate 	if ((i = modload("misc", "sysinit")) != (unsigned int)-1)
14730Sstevel@tonic-gate 		(void) modunload(i);
14740Sstevel@tonic-gate 	else
14750Sstevel@tonic-gate 		cmn_err(CE_CONT, "sysinit load failed");
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	/* Read cluster configuration data. */
14780Sstevel@tonic-gate 	clconf_init();
14790Sstevel@tonic-gate 
14805084Sjohnlev #if defined(__xpv)
14815084Sjohnlev 	ec_init();
14825084Sjohnlev 	gnttab_init();
14835084Sjohnlev 	(void) xs_early_init();
14845084Sjohnlev #endif /* __xpv */
14855084Sjohnlev 
14860Sstevel@tonic-gate 	/*
14870Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
14880Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
14890Sstevel@tonic-gate 	 */
14900Sstevel@tonic-gate 	setup_ddi();
14911414Scindi 
14927532SSean.Ye@Sun.COM 	/*
14937532SSean.Ye@Sun.COM 	 * Set up the CPU module subsystem for the boot cpu in the native
14947532SSean.Ye@Sun.COM 	 * case, and all physical cpu resource in the xpv dom0 case.
14957532SSean.Ye@Sun.COM 	 * Modifies the device tree, so this must be done after
14967532SSean.Ye@Sun.COM 	 * setup_ddi().
14977532SSean.Ye@Sun.COM 	 */
14987532SSean.Ye@Sun.COM #ifdef __xpv
14997532SSean.Ye@Sun.COM 	/*
15007532SSean.Ye@Sun.COM 	 * If paravirtualized and on dom0 then we initialize all physical
15017532SSean.Ye@Sun.COM 	 * cpu handles now;  if paravirtualized on a domU then do not
15027532SSean.Ye@Sun.COM 	 * initialize.
15037532SSean.Ye@Sun.COM 	 */
15047532SSean.Ye@Sun.COM 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
15057532SSean.Ye@Sun.COM 		xen_mc_lcpu_cookie_t cpi;
15067532SSean.Ye@Sun.COM 
15077532SSean.Ye@Sun.COM 		for (cpi = xen_physcpu_next(NULL); cpi != NULL;
15087532SSean.Ye@Sun.COM 		    cpi = xen_physcpu_next(cpi)) {
15097532SSean.Ye@Sun.COM 			if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
15107532SSean.Ye@Sun.COM 			    xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
15117532SSean.Ye@Sun.COM 			    xen_physcpu_strandid(cpi))) != NULL &&
15127532SSean.Ye@Sun.COM 			    (x86_feature & X86_MCA))
15135254Sgavinm 				cmi_mca_init(hdl);
15145254Sgavinm 		}
15155254Sgavinm 	}
15167532SSean.Ye@Sun.COM #else
15177532SSean.Ye@Sun.COM 	/*
15187532SSean.Ye@Sun.COM 	 * Initialize a handle for the boot cpu - others will initialize
15197532SSean.Ye@Sun.COM 	 * as they startup.  Do not do this if we know we are in an HVM domU.
15207532SSean.Ye@Sun.COM 	 */
15217532SSean.Ye@Sun.COM 	if (!xpv_is_hvm &&
15227532SSean.Ye@Sun.COM 	    (hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
15237532SSean.Ye@Sun.COM 	    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL &&
15247532SSean.Ye@Sun.COM 	    (x86_feature & X86_MCA))
15257532SSean.Ye@Sun.COM 			cmi_mca_init(hdl);
15265254Sgavinm #endif	/* __xpv */
15271414Scindi 
15280Sstevel@tonic-gate 	/*
15290Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
15300Sstevel@tonic-gate 	 */
15313446Smrj 	PRM_POINT("startup_modules: calling prom_setup...");
15320Sstevel@tonic-gate 	prom_setup();
15333446Smrj 	PRM_POINT("startup_modules: done");
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	/*
15360Sstevel@tonic-gate 	 * Load all platform specific modules
15370Sstevel@tonic-gate 	 */
15383446Smrj 	PRM_POINT("startup_modules: calling psm_modload...");
15390Sstevel@tonic-gate 	psm_modload();
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
15420Sstevel@tonic-gate }
15430Sstevel@tonic-gate 
15443446Smrj /*
15453446Smrj  * claim a "setaside" boot page for use in the kernel
15463446Smrj  */
15473446Smrj page_t *
15483446Smrj boot_claim_page(pfn_t pfn)
15490Sstevel@tonic-gate {
15503446Smrj 	page_t *pp;
15513446Smrj 
15523446Smrj 	pp = page_numtopp_nolock(pfn);
15533446Smrj 	ASSERT(pp != NULL);
15540Sstevel@tonic-gate 
15553446Smrj 	if (PP_ISBOOTPAGES(pp)) {
15563446Smrj 		if (pp->p_next != NULL)
15573446Smrj 			pp->p_next->p_prev = pp->p_prev;
15583446Smrj 		if (pp->p_prev == NULL)
15593446Smrj 			bootpages = pp->p_next;
15603446Smrj 		else
15613446Smrj 			pp->p_prev->p_next = pp->p_next;
15623446Smrj 	} else {
15633446Smrj 		/*
15643446Smrj 		 * htable_attach() expects a base pagesize page
15653446Smrj 		 */
15663446Smrj 		if (pp->p_szc != 0)
15673446Smrj 			page_boot_demote(pp);
15683446Smrj 		pp = page_numtopp(pfn, SE_EXCL);
15693446Smrj 	}
15703446Smrj 	return (pp);
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate /*
15740Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
15750Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
15760Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
15770Sstevel@tonic-gate  */
15780Sstevel@tonic-gate static void
15790Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate 	uintptr_t va = low;
15820Sstevel@tonic-gate 	size_t len;
15830Sstevel@tonic-gate 	uint_t prot;
15840Sstevel@tonic-gate 	pfn_t pfn;
15850Sstevel@tonic-gate 	page_t *pp;
15860Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
15870Sstevel@tonic-gate 
15883446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
15890Sstevel@tonic-gate 		if (va + len >= high)
15900Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
15910Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 		while (len > 0) {
15940Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
15950Sstevel@tonic-gate 			if (pp != NULL) {
15960Sstevel@tonic-gate 				if (setaside == 0)
15970Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
15980Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
15990Sstevel@tonic-gate 					    (void *)va, pfn);
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 				pp->p_next = bootpages;
16023446Smrj 				pp->p_prev = NULL;
16033446Smrj 				PP_SETBOOTPAGES(pp);
16043446Smrj 				if (bootpages != NULL) {
16053446Smrj 					bootpages->p_prev = pp;
16063446Smrj 				}
16070Sstevel@tonic-gate 				bootpages = pp;
16080Sstevel@tonic-gate 				++boot_protect_cnt;
16090Sstevel@tonic-gate 			}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 			++pfn;
16120Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
16130Sstevel@tonic-gate 			va += MMU_PAGESIZE;
16140Sstevel@tonic-gate 		}
16150Sstevel@tonic-gate 	}
16160Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate 
16193446Smrj /*
16204828Sjosephb  *
16213446Smrj  */
16220Sstevel@tonic-gate static void
16234828Sjosephb layout_kernel_va(void)
16240Sstevel@tonic-gate {
16254828Sjosephb 	PRM_POINT("layout_kernel_va() starting...");
16260Sstevel@tonic-gate 	/*
16273446Smrj 	 * Establish the final size of the kernel's heap, size of segmap,
16283446Smrj 	 * segkp, etc.
16290Sstevel@tonic-gate 	 */
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate #if defined(__amd64)
16320Sstevel@tonic-gate 
16334828Sjosephb 	kpm_vbase = (caddr_t)segkpm_base;
16344828Sjosephb 	kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
16354828Sjosephb 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
16364828Sjosephb 		panic("not enough room for kpm!");
16373446Smrj 	PRM_DEBUG(kpm_size);
16383446Smrj 	PRM_DEBUG(kpm_vbase);
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	/*
16413446Smrj 	 * By default we create a seg_kp in 64 bit kernels, it's a little
16423446Smrj 	 * faster to access than embedding it in the heap.
16430Sstevel@tonic-gate 	 */
16443446Smrj 	segkp_base = (caddr_t)valloc_base + valloc_sz;
16450Sstevel@tonic-gate 	if (!segkp_fromheap) {
16460Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 		/*
16493446Smrj 		 * determine size of segkp
16500Sstevel@tonic-gate 		 */
16510Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
16520Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
16530Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
16540Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
16550Sstevel@tonic-gate 			    mmu_btop(sz));
16560Sstevel@tonic-gate 		}
16570Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
16600Sstevel@tonic-gate 	}
16613446Smrj 	PRM_DEBUG(segkp_base);
16623446Smrj 	PRM_DEBUG(segkpsize);
16630Sstevel@tonic-gate 
16644828Sjosephb 	/*
16654828Sjosephb 	 * segzio is used for ZFS cached data. It uses a distinct VA
16664828Sjosephb 	 * segment (from kernel heap) so that we can easily tell not to
16674828Sjosephb 	 * include it in kernel crash dumps on 64 bit kernels. The trick is
16684828Sjosephb 	 * to give it lots of VA, but not constrain the kernel heap.
16694828Sjosephb 	 * We scale the size of segzio linearly with physmem up to
16704828Sjosephb 	 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem.
16714828Sjosephb 	 */
16723446Smrj 	segzio_base = segkp_base + mmu_ptob(segkpsize);
16733446Smrj 	if (segzio_fromheap) {
16743446Smrj 		segziosize = 0;
16753446Smrj 	} else {
16764828Sjosephb 		size_t physmem_size = mmu_ptob(physmem);
16774828Sjosephb 		size_t size = (segziosize == 0) ?
16784828Sjosephb 		    physmem_size : mmu_ptob(segziosize);
16793290Sjohansen 
16804828Sjosephb 		if (size < SEGZIOMINSIZE)
16813290Sjohansen 			size = SEGZIOMINSIZE;
16824828Sjosephb 		if (size > SEGZIOMAXSIZE) {
16833552Sjohansen 			size = SEGZIOMAXSIZE;
16844828Sjosephb 			if (physmem_size > size)
16854828Sjosephb 				size += (physmem_size - size) / 2;
16863290Sjohansen 		}
16873290Sjohansen 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
16883290Sjohansen 	}
16893446Smrj 	PRM_DEBUG(segziosize);
16903446Smrj 	PRM_DEBUG(segzio_base);
16913290Sjohansen 
16920Sstevel@tonic-gate 	/*
16933446Smrj 	 * Put the range of VA for device mappings next, kmdb knows to not
16943446Smrj 	 * grep in this range of addresses.
16950Sstevel@tonic-gate 	 */
16963446Smrj 	toxic_addr =
16973446Smrj 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
16980Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
16993446Smrj 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
17003446Smrj #else /* __i386 */
17013446Smrj 	segmap_start = ROUND_UP_LPAGE(kernelbase);
17023446Smrj #endif /* __i386 */
17033446Smrj 	PRM_DEBUG(segmap_start);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	/*
17060Sstevel@tonic-gate 	 * Users can change segmapsize through eeprom or /etc/system.
17070Sstevel@tonic-gate 	 * If the variable is tuned through eeprom, there is no upper
17080Sstevel@tonic-gate 	 * bound on the size of segmap.  If it is tuned through
17090Sstevel@tonic-gate 	 * /etc/system on 32-bit systems, it must be no larger than we
17100Sstevel@tonic-gate 	 * planned for in startup_memlist().
17110Sstevel@tonic-gate 	 */
17120Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate #if defined(__i386)
17150Sstevel@tonic-gate 	/*
17160Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
17170Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
17183446Smrj 	 * small red zone just below the start of segmap.
17190Sstevel@tonic-gate 	 */
17203446Smrj 	segmap_start += KERNEL_REDZONE_SIZE;
17210Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
17220Sstevel@tonic-gate #endif
17233446Smrj 
17243446Smrj 	PRM_DEBUG(segmap_start);
17253446Smrj 	PRM_DEBUG(segmapsize);
17264828Sjosephb 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
17274828Sjosephb 	PRM_DEBUG(kernelheap);
17284828Sjosephb 	PRM_POINT("layout_kernel_va() done...");
17294828Sjosephb }
17304828Sjosephb 
17314828Sjosephb /*
17324828Sjosephb  * Finish initializing the VM system, now that we are no longer
17334828Sjosephb  * relying on the boot time memory allocators.
17344828Sjosephb  */
17354828Sjosephb static void
17364828Sjosephb startup_vm(void)
17374828Sjosephb {
17384828Sjosephb 	struct segmap_crargs a;
17394828Sjosephb 
17404828Sjosephb 	extern int use_brk_lpg, use_stk_lpg;
17414828Sjosephb 
17424828Sjosephb 	PRM_POINT("startup_vm() starting...");
17434828Sjosephb 
17444828Sjosephb 	/*
17454828Sjosephb 	 * Initialize the hat layer.
17464828Sjosephb 	 */
17474828Sjosephb 	hat_init();
17483446Smrj 
17493446Smrj 	/*
17503446Smrj 	 * Do final allocations of HAT data structures that need to
17513446Smrj 	 * be allocated before quiescing the boot loader.
17523446Smrj 	 */
17533446Smrj 	PRM_POINT("Calling hat_kern_alloc()...");
17543446Smrj 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
17553446Smrj 	PRM_POINT("hat_kern_alloc() done");
17563446Smrj 
17575084Sjohnlev #ifndef __xpv
17583446Smrj 	/*
17595159Sjohnlev 	 * Setup Page Attribute Table
17603446Smrj 	 */
17615159Sjohnlev 	pat_sync();
17625084Sjohnlev #endif
17633446Smrj 
17643446Smrj 	/*
17653446Smrj 	 * The next two loops are done in distinct steps in order
17663446Smrj 	 * to be sure that any page that is doubly mapped (both above
17673446Smrj 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
17683446Smrj 	 * Note this may never happen, but it might someday.
17693446Smrj 	 */
17703446Smrj 	bootpages = NULL;
17713446Smrj 	PRM_POINT("Protecting boot pages");
17720Sstevel@tonic-gate 
17733446Smrj 	/*
17743446Smrj 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
17753446Smrj 	 * page_t's. This can only happen if something weird allocated
17763446Smrj 	 * in this range (like kadb/kmdb).
17773446Smrj 	 */
17783446Smrj 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
17793446Smrj 
17803446Smrj 	/*
17813446Smrj 	 * Before we can take over memory allocation/mapping from the boot
17823446Smrj 	 * loader we must remove from our free page lists any boot allocated
17833446Smrj 	 * pages that stay mapped until release_bootstrap().
17843446Smrj 	 */
17853446Smrj 	protect_boot_range(0, kernelbase, 1);
17863446Smrj 
17875084Sjohnlev 
17883446Smrj 	/*
17893446Smrj 	 * Switch to running on regular HAT (not boot_mmu)
17903446Smrj 	 */
17913446Smrj 	PRM_POINT("Calling hat_kern_setup()...");
17923446Smrj 	hat_kern_setup();
17933446Smrj 
17943446Smrj 	/*
17953446Smrj 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
17963446Smrj 	 */
17973446Smrj 	bop_no_more_mem();
17983446Smrj 
17993446Smrj 	PRM_POINT("hat_kern_setup() done");
18003446Smrj 
18013446Smrj 	hat_cpu_online(CPU);
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	/*
18040Sstevel@tonic-gate 	 * Initialize VM system
18050Sstevel@tonic-gate 	 */
18060Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
18070Sstevel@tonic-gate 	kvm_init();
18080Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	/*
18110Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
18120Sstevel@tonic-gate 	 */
18130Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
18140Sstevel@tonic-gate 		kdi_dvec_vmready();
18150Sstevel@tonic-gate 
18165084Sjohnlev #if defined(__xpv)
18175084Sjohnlev 	/*
18185084Sjohnlev 	 * Populate the I/O pool on domain 0
18195084Sjohnlev 	 */
18205084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
18215084Sjohnlev 		extern long populate_io_pool(void);
18225084Sjohnlev 		long init_io_pool_cnt;
18235084Sjohnlev 
18245084Sjohnlev 		PRM_POINT("Populating reserve I/O page pool");
18255084Sjohnlev 		init_io_pool_cnt = populate_io_pool();
18265084Sjohnlev 		PRM_DEBUG(init_io_pool_cnt);
18275084Sjohnlev 	}
18285084Sjohnlev #endif
18290Sstevel@tonic-gate 	/*
18300Sstevel@tonic-gate 	 * Mangle the brand string etc.
18310Sstevel@tonic-gate 	 */
18320Sstevel@tonic-gate 	cpuid_pass3(CPU);
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate #if defined(__amd64)
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	/*
18370Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
18380Sstevel@tonic-gate 	 */
18390Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
18400Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate #else	/* __i386 */
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	/*
18450Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
18460Sstevel@tonic-gate 	 */
18473446Smrj 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
18480Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
18490Sstevel@tonic-gate 	toxic_bit_map =
18500Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
18510Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
18520Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate #endif	/* __i386 */
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	/*
18580Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
18590Sstevel@tonic-gate 	 * it, tell the debugger.
18600Sstevel@tonic-gate 	 */
18610Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
18620Sstevel@tonic-gate 		kdi_dvec_memavail();
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 	/*
18650Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
18660Sstevel@tonic-gate 	 * to work around a pentium bug.
18670Sstevel@tonic-gate 	 */
18685084Sjohnlev #if !defined(__amd64) && !defined(__xpv)
18690Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
18703446Smrj 		desctbr_t idtr;
18710Sstevel@tonic-gate 		gate_desc_t *newidt;
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
18740Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
18750Sstevel@tonic-gate 
18765460Sjosephb 		bcopy(idt0, newidt, NIDT * sizeof (*idt0));
18770Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
18785084Sjohnlev 		    KCS_SEL, SDT_SYSIGT, TRP_KPL);
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
18815460Sjosephb 		    PROT_READ | PROT_EXEC);
18820Sstevel@tonic-gate 
18835460Sjosephb 		CPU->cpu_idt = newidt;
18845460Sjosephb 		idtr.dtr_base = (uintptr_t)CPU->cpu_idt;
18855460Sjosephb 		idtr.dtr_limit = (NIDT * sizeof (*idt0)) - 1;
18863446Smrj 		wr_idtr(&idtr);
18870Sstevel@tonic-gate 	}
18880Sstevel@tonic-gate #endif	/* !__amd64 */
18890Sstevel@tonic-gate 
18905084Sjohnlev #if !defined(__xpv)
18910Sstevel@tonic-gate 	/*
18920Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
18930Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
18940Sstevel@tonic-gate 	 */
18950Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
18960Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
18970Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
18985084Sjohnlev #endif
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
19010Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
19020Sstevel@tonic-gate 
1903841Skchow 	/*
1904841Skchow 	 * disable automatic large pages for small memory systems or
1905841Skchow 	 * when the disable flag is set.
19065349Skchow 	 *
19075349Skchow 	 * Do not yet consider page sizes larger than 2m/4m.
1908841Skchow 	 */
19092991Ssusans 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
19102991Ssusans 		max_uheap_lpsize = LEVEL_SIZE(1);
19112991Ssusans 		max_ustack_lpsize = LEVEL_SIZE(1);
19122991Ssusans 		max_privmap_lpsize = LEVEL_SIZE(1);
19132991Ssusans 		max_uidata_lpsize = LEVEL_SIZE(1);
19142991Ssusans 		max_utext_lpsize = LEVEL_SIZE(1);
19152991Ssusans 		max_shm_lpsize = LEVEL_SIZE(1);
19162991Ssusans 	}
19172991Ssusans 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
19182991Ssusans 	    auto_lpg_disable) {
1919423Sdavemq 		use_brk_lpg = 0;
1920423Sdavemq 		use_stk_lpg = 0;
19212991Ssusans 	}
19225349Skchow 	mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
1923423Sdavemq 
19240Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
19250Sstevel@tonic-gate 	hat_init_finish();
19260Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	/*
19290Sstevel@tonic-gate 	 * Initialize the segkp segment type.
19300Sstevel@tonic-gate 	 */
19310Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
19323446Smrj 	PRM_POINT("Attaching segkp");
19333446Smrj 	if (segkp_fromheap) {
19340Sstevel@tonic-gate 		segkp->s_as = &kas;
19353446Smrj 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
19363446Smrj 	    segkp) < 0) {
19373446Smrj 		panic("startup: cannot attach segkp");
19383446Smrj 		/*NOTREACHED*/
19390Sstevel@tonic-gate 	}
19403446Smrj 	PRM_POINT("Doing segkp_create()");
19410Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
19420Sstevel@tonic-gate 		panic("startup: segkp_create failed");
19430Sstevel@tonic-gate 		/*NOTREACHED*/
19440Sstevel@tonic-gate 	}
19450Sstevel@tonic-gate 	PRM_DEBUG(segkp);
19460Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	/*
19490Sstevel@tonic-gate 	 * kpm segment
19500Sstevel@tonic-gate 	 */
19510Sstevel@tonic-gate 	segmap_kpm = 0;
19520Sstevel@tonic-gate 	if (kpm_desired) {
19530Sstevel@tonic-gate 		kpm_init();
19540Sstevel@tonic-gate 		kpm_enable = 1;
19551841Spraks 		vpm_enable = 1;
19560Sstevel@tonic-gate 	}
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	/*
19590Sstevel@tonic-gate 	 * Now create segmap segment.
19600Sstevel@tonic-gate 	 */
19610Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
19623446Smrj 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
19633446Smrj 		panic("cannot attach segmap");
19640Sstevel@tonic-gate 		/*NOTREACHED*/
19650Sstevel@tonic-gate 	}
19663446Smrj 	PRM_DEBUG(segmap);
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
19690Sstevel@tonic-gate 	a.shmsize = 0;
19700Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
19710Sstevel@tonic-gate 
19723446Smrj 	if (segmap_create(segmap, (caddr_t)&a) != 0)
19733446Smrj 		panic("segmap_create segmap");
19740Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	segdev_init();
19795084Sjohnlev #if defined(__xpv)
19805084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
19815084Sjohnlev #endif
19825084Sjohnlev 		pmem_init();
19833446Smrj 
19840Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
19850Sstevel@tonic-gate }
19860Sstevel@tonic-gate 
19873446Smrj /*
19883446Smrj  * Load a tod module for the non-standard tod part found on this system.
19893446Smrj  */
19903446Smrj static void
19913446Smrj load_tod_module(char *todmod)
19923446Smrj {
19933446Smrj 	if (modload("tod", todmod) == -1)
19943446Smrj 		halt("Can't load TOD module");
19953446Smrj }
19963446Smrj 
19970Sstevel@tonic-gate static void
19980Sstevel@tonic-gate startup_end(void)
19990Sstevel@tonic-gate {
20005107Seota 	int i;
20010Sstevel@tonic-gate 	extern void setx86isalist(void);
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	/*
20060Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
20070Sstevel@tonic-gate 	 * initialization has been done but before the clock
20080Sstevel@tonic-gate 	 * and other devices get started.
20090Sstevel@tonic-gate 	 */
20100Sstevel@tonic-gate 	kern_setup1();
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 	/*
20130Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
20140Sstevel@tonic-gate 	 */
20150Sstevel@tonic-gate 	kcpc_hw_init(CPU);
20160Sstevel@tonic-gate 
20171582Skchow #if defined(OPTERON_WORKAROUND_6323525)
20181582Skchow 	if (opteron_workaround_6323525)
20191582Skchow 		patch_workaround_6323525();
20201582Skchow #endif
20210Sstevel@tonic-gate 	/*
20223446Smrj 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
20233446Smrj 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
20243446Smrj 	 */
20253446Smrj 	if (tod_module_name != NULL) {
20263446Smrj 		PRM_POINT("load_tod_module()");
20273446Smrj 		load_tod_module(tod_module_name);
20283446Smrj 	}
20293446Smrj 
20305084Sjohnlev #if defined(__xpv)
20315084Sjohnlev 	/*
20325084Sjohnlev 	 * Forceload interposing TOD module for the hypervisor.
20335084Sjohnlev 	 */
20345084Sjohnlev 	PRM_POINT("load_tod_module()");
20355084Sjohnlev 	load_tod_module("xpvtod");
20365084Sjohnlev #endif
20375084Sjohnlev 
20383446Smrj 	/*
20390Sstevel@tonic-gate 	 * Configure the system.
20400Sstevel@tonic-gate 	 */
20410Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
20420Sstevel@tonic-gate 	configure();		/* set up devices */
20430Sstevel@tonic-gate 	PRM_POINT("configure() done");
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	/*
20460Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
20470Sstevel@tonic-gate 	 * support.
20480Sstevel@tonic-gate 	 */
20490Sstevel@tonic-gate 	setx86isalist();
20501455Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
20510Sstevel@tonic-gate 	psm_install();
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	/*
20540Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
20550Sstevel@tonic-gate 	 * we're still using bootsvcs.
20560Sstevel@tonic-gate 	 */
20573446Smrj 	PRM_POINT("NULLing out bootops");
20583446Smrj 	*bootopsp = (struct bootops *)NULL;
20590Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
20600Sstevel@tonic-gate 
20615084Sjohnlev #if defined(__xpv)
20625084Sjohnlev 	ec_init_debug_irq();
20635084Sjohnlev 	xs_domu_init();
20645084Sjohnlev #endif
20650Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
20660Sstevel@tonic-gate 	(*picinitf)();
20670Sstevel@tonic-gate 	sti();
20685084Sjohnlev #if defined(__xpv)
20695084Sjohnlev 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
20705084Sjohnlev 	xen_late_startup();
20715084Sjohnlev #endif
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
20744828Sjosephb 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
20750Sstevel@tonic-gate 
20765107Seota 	/*
20775107Seota 	 * Register these software interrupts for ddi timer.
20785107Seota 	 * Software interrupts up to the level 10 are supported.
20795107Seota 	 */
20805107Seota 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
20815107Seota 		char name[sizeof ("timer_softintr") + 2];
20825107Seota 		(void) sprintf(name, "timer_softintr%02d", i);
20835107Seota 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
20845107Seota 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
20855107Seota 	}
20865107Seota 
20870Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
20880Sstevel@tonic-gate }
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate extern char hw_serial[];
20910Sstevel@tonic-gate char *_hs1107 = hw_serial;
20920Sstevel@tonic-gate ulong_t  _bdhs34;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate void
20950Sstevel@tonic-gate post_startup(void)
20960Sstevel@tonic-gate {
20970Sstevel@tonic-gate 	/*
20980Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
20990Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
21000Sstevel@tonic-gate 	 * instruction set extensions.
21010Sstevel@tonic-gate 	 */
21020Sstevel@tonic-gate 	bind_hwcap();
21030Sstevel@tonic-gate 
21045084Sjohnlev #ifdef __xpv
21055084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
21065084Sjohnlev #endif
21075084Sjohnlev 	{
21085084Sjohnlev 		/*
21095084Sjohnlev 		 * Load the System Management BIOS into the global ksmbios
21105084Sjohnlev 		 * handle, if an SMBIOS is present on this system.
21115084Sjohnlev 		 */
21125084Sjohnlev 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
21135084Sjohnlev 
21145084Sjohnlev #if defined(__xpv)
21155084Sjohnlev 		xpv_panic_init();
21165084Sjohnlev #else
21175084Sjohnlev 		/*
21185084Sjohnlev 		 * Startup the memory scrubber.
21195084Sjohnlev 		 * XXPV	This should be running somewhere ..
21205084Sjohnlev 		 */
21217532SSean.Ye@Sun.COM 		if (!xpv_is_hvm)
21227532SSean.Ye@Sun.COM 			memscrub_init();
21235084Sjohnlev #endif
21245084Sjohnlev 	}
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	/*
21271414Scindi 	 * Complete CPU module initialization
21281414Scindi 	 */
21295254Sgavinm 	cmi_post_startup();
21301414Scindi 
21311414Scindi 	/*
21320Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
21330Sstevel@tonic-gate 	 */
21340Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	/*
21370Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
21380Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
21390Sstevel@tonic-gate 	 */
21400Sstevel@tonic-gate 	(void) modload("fs", "procfs");
21410Sstevel@tonic-gate 
21425129Smarx 	(void) i_ddi_attach_hw_nodes("pit_beep");
21435129Smarx 
21440Sstevel@tonic-gate #if defined(__i386)
21450Sstevel@tonic-gate 	/*
21460Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
21470Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
21480Sstevel@tonic-gate 	 */
21490Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
21500Sstevel@tonic-gate 		halt("No working FP hardware found");
21510Sstevel@tonic-gate #endif
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	maxmem = freemem;
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
21560Sstevel@tonic-gate }
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate static int
2159*7656SSherry.Moore@Sun.COM pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
21600Sstevel@tonic-gate {
2161*7656SSherry.Moore@Sun.COM 	return ((pp->p_pagenum >= btop(low_addr)) &&
2162*7656SSherry.Moore@Sun.COM 	    (pp->p_pagenum < btopr(high_addr)));
21630Sstevel@tonic-gate }
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate void
21660Sstevel@tonic-gate release_bootstrap(void)
21670Sstevel@tonic-gate {
21680Sstevel@tonic-gate 	int root_is_ramdisk;
21690Sstevel@tonic-gate 	page_t *pp;
21700Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
21710Sstevel@tonic-gate 	extern dev_t rootdev;
21727515SSeth.Goldberg@Sun.COM #if !defined(__xpv)
21737515SSeth.Goldberg@Sun.COM 	pfn_t	pfn;
21747515SSeth.Goldberg@Sun.COM #endif
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
21770Sstevel@tonic-gate 	kobj_boot_unmountroot();
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	/*
21800Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
21810Sstevel@tonic-gate 	 */
21820Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
2183*7656SSherry.Moore@Sun.COM 
21843446Smrj 	clear_boot_mappings(0, _userlimit);
2185*7656SSherry.Moore@Sun.COM 
21863446Smrj 	postbootkernelbase = kernelbase;
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	/*
21890Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
21900Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
21910Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
21920Sstevel@tonic-gate 	 */
21930Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
21940Sstevel@tonic-gate 	if (!root_is_ramdisk) {
21950Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
21960Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
21970Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
21980Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
21990Sstevel@tonic-gate 	}
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
22020Sstevel@tonic-gate 	while (bootpages) {
2203*7656SSherry.Moore@Sun.COM 		extern uint64_t ramdisk_start, ramdisk_end;
22040Sstevel@tonic-gate 		pp = bootpages;
22050Sstevel@tonic-gate 		bootpages = pp->p_next;
2206*7656SSherry.Moore@Sun.COM 
2207*7656SSherry.Moore@Sun.COM 
2208*7656SSherry.Moore@Sun.COM 		/* Keep pages for the lower 64K */
2209*7656SSherry.Moore@Sun.COM 		if (pp_in_range(pp, 0, 0x40000)) {
2210*7656SSherry.Moore@Sun.COM 			pp->p_next = lower_pages;
2211*7656SSherry.Moore@Sun.COM 			lower_pages = pp;
2212*7656SSherry.Moore@Sun.COM 			lower_pages_count++;
2213*7656SSherry.Moore@Sun.COM 			continue;
2214*7656SSherry.Moore@Sun.COM 		}
2215*7656SSherry.Moore@Sun.COM 
2216*7656SSherry.Moore@Sun.COM 
2217*7656SSherry.Moore@Sun.COM 		if (root_is_ramdisk && pp_in_range(pp, ramdisk_start,
2218*7656SSherry.Moore@Sun.COM 		    ramdisk_end)) {
22190Sstevel@tonic-gate 			pp->p_next = rd_pages;
22200Sstevel@tonic-gate 			rd_pages = pp;
22210Sstevel@tonic-gate 			continue;
22220Sstevel@tonic-gate 		}
22230Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
22243446Smrj 		pp->p_prev = (struct page *)0;
22253446Smrj 		PP_CLRBOOTPAGES(pp);
22260Sstevel@tonic-gate 		page_free(pp, 1);
22270Sstevel@tonic-gate 	}
22283446Smrj 	PRM_POINT("Boot pages released");
22290Sstevel@tonic-gate 
22305084Sjohnlev #if !defined(__xpv)
22315084Sjohnlev /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
22320Sstevel@tonic-gate 	/*
22337515SSeth.Goldberg@Sun.COM 	 * Find 1 page below 1 MB so that other processors can boot up or
22347515SSeth.Goldberg@Sun.COM 	 * so that any processor can resume.
22350Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
22360Sstevel@tonic-gate 	 * We should have just free'd one up.
22370Sstevel@tonic-gate 	 */
2238*7656SSherry.Moore@Sun.COM 
2239*7656SSherry.Moore@Sun.COM 	/*
2240*7656SSherry.Moore@Sun.COM 	 * 0x10 pages is 64K.  Leave the bottom 64K alone
2241*7656SSherry.Moore@Sun.COM 	 * for BIOS.
2242*7656SSherry.Moore@Sun.COM 	 */
2243*7656SSherry.Moore@Sun.COM 	for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
22447515SSeth.Goldberg@Sun.COM 		if (page_numtopp_alloc(pfn) == NULL)
22457515SSeth.Goldberg@Sun.COM 			continue;
22467515SSeth.Goldberg@Sun.COM 		rm_platter_va = i86devmap(pfn, 1,
22477515SSeth.Goldberg@Sun.COM 		    PROT_READ | PROT_WRITE | PROT_EXEC);
22487515SSeth.Goldberg@Sun.COM 		rm_platter_pa = ptob(pfn);
22497515SSeth.Goldberg@Sun.COM 		hat_devload(kas.a_hat,
22507515SSeth.Goldberg@Sun.COM 		    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
22517515SSeth.Goldberg@Sun.COM 		    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
22527515SSeth.Goldberg@Sun.COM 		    HAT_LOAD_NOCONSIST);
22537515SSeth.Goldberg@Sun.COM 		break;
22540Sstevel@tonic-gate 	}
22557515SSeth.Goldberg@Sun.COM 	if (pfn == btop(1*1024*1024) && use_mp)
22567515SSeth.Goldberg@Sun.COM 		panic("No page below 1M available for starting "
22577515SSeth.Goldberg@Sun.COM 		    "other processors or for resuming from system-suspend");
22585084Sjohnlev #endif	/* !__xpv */
22590Sstevel@tonic-gate }
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate /*
22620Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
22630Sstevel@tonic-gate  */
22640Sstevel@tonic-gate void
22650Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
22660Sstevel@tonic-gate {
22670Sstevel@tonic-gate 	pp->p_pagenum = pnum;
22680Sstevel@tonic-gate 	pp->p_mapping = NULL;
22690Sstevel@tonic-gate 	pp->p_embed = 0;
22700Sstevel@tonic-gate 	pp->p_share = 0;
22710Sstevel@tonic-gate 	pp->p_mlentry = 0;
22720Sstevel@tonic-gate }
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate /*
22750Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
22760Sstevel@tonic-gate  */
22770Sstevel@tonic-gate static pgcnt_t
22780Sstevel@tonic-gate kphysm_init(
22793446Smrj 	page_t *pp,
22800Sstevel@tonic-gate 	pgcnt_t npages)
22810Sstevel@tonic-gate {
22820Sstevel@tonic-gate 	struct memlist	*pmem;
22830Sstevel@tonic-gate 	struct memseg	*cur_memseg;
22840Sstevel@tonic-gate 	pfn_t		base_pfn;
22850Sstevel@tonic-gate 	pgcnt_t		num;
22860Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
22870Sstevel@tonic-gate 	uint64_t	addr;
22880Sstevel@tonic-gate 	uint64_t	size;
22890Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
22920Sstevel@tonic-gate 
22933446Smrj 	cur_memseg = memseg_base;
22940Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
22950Sstevel@tonic-gate 		/*
22960Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
22970Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
22980Sstevel@tonic-gate 		 */
22990Sstevel@tonic-gate 		addr = pmem->address;
23000Sstevel@tonic-gate 		size = pmem->size;
23010Sstevel@tonic-gate 		if (btop(addr) > physmax)
23020Sstevel@tonic-gate 			continue;
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 		/*
23050Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
23060Sstevel@tonic-gate 		 */
23070Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
23080Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
23090Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
23100Sstevel@tonic-gate 			size -= addr - pmem->address;
23110Sstevel@tonic-gate 		}
23120Sstevel@tonic-gate 
2313810Skchow 		/* only process pages below or equal to physmax */
2314810Skchow 		if ((btop(addr + size) - 1) > physmax)
2315810Skchow 			size = ptob(physmax - btop(addr) + 1);
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 		num = btop(size);
23180Sstevel@tonic-gate 		if (num == 0)
23190Sstevel@tonic-gate 			continue;
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 		if (num > npages)
23220Sstevel@tonic-gate 			num = npages;
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 		npages -= num;
23250Sstevel@tonic-gate 		pages_done += num;
23260Sstevel@tonic-gate 		base_pfn = btop(addr);
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 		if (prom_debug)
23290Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
23300Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
23310Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 		/*
23343446Smrj 		 * Ignore pages below ddiphysmin to simplify ddi memory
23350Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
23360Sstevel@tonic-gate 		 */
23370Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
23383446Smrj 			if (base_pfn + num <= ddiphysmin)
23390Sstevel@tonic-gate 				continue;
23400Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
23410Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
23420Sstevel@tonic-gate 			base_pfn = ddiphysmin;
23430Sstevel@tonic-gate 		}
23443446Smrj 
23450Sstevel@tonic-gate 		/*
23460Sstevel@tonic-gate 		 * Build the memsegs entry
23470Sstevel@tonic-gate 		 */
23480Sstevel@tonic-gate 		cur_memseg->pages = pp;
23490Sstevel@tonic-gate 		cur_memseg->epages = pp + num;
23500Sstevel@tonic-gate 		cur_memseg->pages_base = base_pfn;
23510Sstevel@tonic-gate 		cur_memseg->pages_end = base_pfn + num;
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 		/*
23543446Smrj 		 * Insert into memseg list in decreasing pfn range order.
23550Sstevel@tonic-gate 		 * Low memory is typically more fragmented such that this
23560Sstevel@tonic-gate 		 * ordering keeps the larger ranges at the front of the list
23570Sstevel@tonic-gate 		 * for code that searches memseg.
23583446Smrj 		 * This ASSERTS that the memsegs coming in from boot are in
23593446Smrj 		 * increasing physical address order and not contiguous.
23600Sstevel@tonic-gate 		 */
23613446Smrj 		if (memsegs != NULL) {
23623446Smrj 			ASSERT(cur_memseg->pages_base >= memsegs->pages_end);
23633446Smrj 			cur_memseg->next = memsegs;
23640Sstevel@tonic-gate 		}
23653446Smrj 		memsegs = cur_memseg;
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 		/*
23680Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
23690Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
23700Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
23710Sstevel@tonic-gate 		 * it initializes them.
23720Sstevel@tonic-gate 		 */
23730Sstevel@tonic-gate 		add_physmem(pp, num, base_pfn);
23740Sstevel@tonic-gate 		cur_memseg++;
23750Sstevel@tonic-gate 		availrmem_initial += num;
23760Sstevel@tonic-gate 		availrmem += num;
23770Sstevel@tonic-gate 
23783446Smrj 		pp += num;
23790Sstevel@tonic-gate 	}
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
23820Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
23830Sstevel@tonic-gate 	PRM_DEBUG(freemem);
23840Sstevel@tonic-gate 	build_pfn_hash();
23850Sstevel@tonic-gate 	return (pages_done);
23860Sstevel@tonic-gate }
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate /*
23890Sstevel@tonic-gate  * Kernel VM initialization.
23900Sstevel@tonic-gate  */
23910Sstevel@tonic-gate static void
23920Sstevel@tonic-gate kvm_init(void)
23930Sstevel@tonic-gate {
23940Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 	/*
23970Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
23980Sstevel@tonic-gate 	 */
23990Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
24000Sstevel@tonic-gate 	as_avlinit(&kas);
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
24030Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
24060Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
24070Sstevel@tonic-gate 
24084828Sjosephb 	(void) seg_attach(&kas, kernelheap,
24094828Sjosephb 	    ekernelheap - kernelheap, &kvseg);
24100Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
24110Sstevel@tonic-gate 
24123446Smrj 	if (core_size > 0) {
24133446Smrj 		PRM_POINT("attaching kvseg_core");
24143446Smrj 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
24153446Smrj 		    &kvseg_core);
24163446Smrj 		(void) segkmem_create(&kvseg_core);
24173446Smrj 	}
24183290Sjohansen 
24193446Smrj 	if (segziosize > 0) {
24203446Smrj 		PRM_POINT("attaching segzio");
24213290Sjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
24223290Sjohansen 		    &kzioseg);
24233290Sjohansen 		(void) segkmem_zio_create(&kzioseg);
24243290Sjohansen 
24253290Sjohansen 		/* create zio area covering new segment */
24263290Sjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
24273290Sjohansen 	}
24280Sstevel@tonic-gate 
24293446Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
24300Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	/*
24350Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
24360Sstevel@tonic-gate 	 */
24373446Smrj 	PRM_POINT("protecting redzone");
24380Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	/*
24410Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
24420Sstevel@tonic-gate 	 */
24430Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
24440Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 	/*
24470Sstevel@tonic-gate 	 * Make data writable until end.
24480Sstevel@tonic-gate 	 */
24490Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
24500Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
24510Sstevel@tonic-gate }
24520Sstevel@tonic-gate 
24535084Sjohnlev #ifndef __xpv
24540Sstevel@tonic-gate /*
24555159Sjohnlev  * Solaris adds an entry for Write Combining caching to the PAT
24560Sstevel@tonic-gate  */
24575159Sjohnlev static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate void
24605159Sjohnlev pat_sync(void)
24610Sstevel@tonic-gate {
24625159Sjohnlev 	ulong_t	cr0, cr0_orig, cr4;
24630Sstevel@tonic-gate 
24645159Sjohnlev 	if (!(x86_feature & X86_PAT))
24650Sstevel@tonic-gate 		return;
24665159Sjohnlev 	cr0_orig = cr0 = getcr0();
24675159Sjohnlev 	cr4 = getcr4();
24680Sstevel@tonic-gate 
24695159Sjohnlev 	/* disable caching and flush all caches and TLBs */
24705159Sjohnlev 	cr0 |= CR0_CD;
24715159Sjohnlev 	cr0 &= ~CR0_NW;
24725159Sjohnlev 	setcr0(cr0);
24735159Sjohnlev 	invalidate_cache();
24745159Sjohnlev 	if (cr4 & CR4_PGE) {
24755159Sjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
24765159Sjohnlev 		setcr4(cr4);
24775159Sjohnlev 	} else {
24785159Sjohnlev 		reload_cr3();
24790Sstevel@tonic-gate 	}
24800Sstevel@tonic-gate 
24815159Sjohnlev 	/* add our entry to the PAT */
24825159Sjohnlev 	wrmsr(REG_PAT, pat_attr_reg);
2483770Skucharsk 
24845159Sjohnlev 	/* flush TLBs and cache again, then reenable cr0 caching */
24855159Sjohnlev 	if (cr4 & CR4_PGE) {
24865159Sjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
24875159Sjohnlev 		setcr4(cr4);
24885159Sjohnlev 	} else {
24895159Sjohnlev 		reload_cr3();
24900Sstevel@tonic-gate 	}
24910Sstevel@tonic-gate 	invalidate_cache();
24920Sstevel@tonic-gate 	setcr0(cr0_orig);
24930Sstevel@tonic-gate }
24940Sstevel@tonic-gate 
24955159Sjohnlev #endif /* !__xpv */
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate void
24981676Sjpk get_system_configuration(void)
24990Sstevel@tonic-gate {
25000Sstevel@tonic-gate 	char	prop[32];
25010Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
25020Sstevel@tonic-gate 
25034828Sjosephb 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
25044828Sjosephb 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
25054828Sjosephb 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
25064828Sjosephb 	    nodes_ll > MAXNODES ||
25074828Sjosephb 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
25084828Sjosephb 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
25094828Sjosephb 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
25100Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
25110Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
25120Sstevel@tonic-gate 	} else {
25130Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
25140Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
25150Sstevel@tonic-gate 	}
25164828Sjosephb 
25174828Sjosephb 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
25184828Sjosephb 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
25194828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
25204828Sjosephb 		eprom_kernelbase = NULL;
25210Sstevel@tonic-gate 	else
25224828Sjosephb 		eprom_kernelbase = (uintptr_t)lvalue;
25230Sstevel@tonic-gate 
25244828Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
25254828Sjosephb 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
25264828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
25270Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
25284828Sjosephb 	else
25290Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
25300Sstevel@tonic-gate 
25314828Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
25324828Sjosephb 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
25334828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
25340Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
25354828Sjosephb 	else
25360Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
25371417Skchow 
25384004Sjosephb 	/* physmem used to be here, but moved much earlier to fakebop.c */
25390Sstevel@tonic-gate }
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate /*
25420Sstevel@tonic-gate  * Add to a memory list.
25430Sstevel@tonic-gate  * start = start of new memory segment
25440Sstevel@tonic-gate  * len = length of new memory segment in bytes
25450Sstevel@tonic-gate  * new = pointer to a new struct memlist
25460Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
25470Sstevel@tonic-gate  */
25483446Smrj void
25490Sstevel@tonic-gate memlist_add(
25500Sstevel@tonic-gate 	uint64_t start,
25510Sstevel@tonic-gate 	uint64_t len,
25520Sstevel@tonic-gate 	struct memlist *new,
25530Sstevel@tonic-gate 	struct memlist **memlistp)
25540Sstevel@tonic-gate {
25550Sstevel@tonic-gate 	struct memlist *cur;
25560Sstevel@tonic-gate 	uint64_t end = start + len;
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate 	new->address = start;
25590Sstevel@tonic-gate 	new->size = len;
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	cur = *memlistp;
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate 	while (cur) {
25640Sstevel@tonic-gate 		if (cur->address >= end) {
25650Sstevel@tonic-gate 			new->next = cur;
25660Sstevel@tonic-gate 			*memlistp = new;
25670Sstevel@tonic-gate 			new->prev = cur->prev;
25680Sstevel@tonic-gate 			cur->prev = new;
25690Sstevel@tonic-gate 			return;
25700Sstevel@tonic-gate 		}
25710Sstevel@tonic-gate 		ASSERT(cur->address + cur->size <= start);
25720Sstevel@tonic-gate 		if (cur->next == NULL) {
25730Sstevel@tonic-gate 			cur->next = new;
25740Sstevel@tonic-gate 			new->prev = cur;
25750Sstevel@tonic-gate 			new->next = NULL;
25760Sstevel@tonic-gate 			return;
25770Sstevel@tonic-gate 		}
25780Sstevel@tonic-gate 		memlistp = &cur->next;
25790Sstevel@tonic-gate 		cur = cur->next;
25800Sstevel@tonic-gate 	}
25810Sstevel@tonic-gate }
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate void
25840Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
25850Sstevel@tonic-gate {
25860Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
25870Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
25900Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
25910Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
25920Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
25930Sstevel@tonic-gate }
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate caddr_t
25960Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
25970Sstevel@tonic-gate {
25980Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
25990Sstevel@tonic-gate }
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate /*ARGSUSED*/
26020Sstevel@tonic-gate caddr_t
26030Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
26040Sstevel@tonic-gate {
26050Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
26060Sstevel@tonic-gate 	/*NOTREACHED*/
26070Sstevel@tonic-gate 	return (0);
26080Sstevel@tonic-gate }
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate /*ARGSUSED*/
26110Sstevel@tonic-gate void
26120Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
26130Sstevel@tonic-gate {
26140Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
26150Sstevel@tonic-gate }
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate /*
26180Sstevel@tonic-gate  * This is called just after configure() in startup().
26190Sstevel@tonic-gate  *
26200Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
26210Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
26220Sstevel@tonic-gate  * given that various parts of the instruction set may appear
26230Sstevel@tonic-gate  * and disappear between different implementations.
26240Sstevel@tonic-gate  *
26250Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
26260Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
26270Sstevel@tonic-gate  * more flexibility.
26280Sstevel@tonic-gate  *
26290Sstevel@tonic-gate  * So, we just leave this alone.
26300Sstevel@tonic-gate  */
26310Sstevel@tonic-gate void
26320Sstevel@tonic-gate setx86isalist(void)
26330Sstevel@tonic-gate {
26340Sstevel@tonic-gate 	char *tp;
26350Sstevel@tonic-gate 	size_t len;
26360Sstevel@tonic-gate 	extern char *isa_list;
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate #define	TBUFSIZE	1024
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
26410Sstevel@tonic-gate 	*tp = '\0';
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate #if defined(__amd64)
26440Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
26450Sstevel@tonic-gate #endif
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	switch (x86_vendor) {
26480Sstevel@tonic-gate 	case X86_VENDOR_Intel:
26490Sstevel@tonic-gate 	case X86_VENDOR_AMD:
26500Sstevel@tonic-gate 	case X86_VENDOR_TM:
26510Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
26520Sstevel@tonic-gate 			/*
26530Sstevel@tonic-gate 			 * Pentium Pro or later
26540Sstevel@tonic-gate 			 */
26550Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
26560Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
26570Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
26580Sstevel@tonic-gate 		}
26590Sstevel@tonic-gate 		/*FALLTHROUGH*/
26600Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
26610Sstevel@tonic-gate 		/*
26620Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
26630Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
26640Sstevel@tonic-gate 		 */
26650Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
26660Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
26670Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
26680Sstevel@tonic-gate 			    "+mmx pentium " : " ");
26690Sstevel@tonic-gate 		}
26700Sstevel@tonic-gate 		break;
26710Sstevel@tonic-gate 	default:
26720Sstevel@tonic-gate 		break;
26730Sstevel@tonic-gate 	}
26740Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
26750Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
26760Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
26770Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate #undef TBUFSIZE
26800Sstevel@tonic-gate }
26810Sstevel@tonic-gate 
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate #ifdef __amd64
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate void *
26860Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
26870Sstevel@tonic-gate {
26880Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
26890Sstevel@tonic-gate }
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate void
26920Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
26930Sstevel@tonic-gate {
26940Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
26950Sstevel@tonic-gate }
26960Sstevel@tonic-gate 
26973446Smrj #else /* __i386 */
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate void *
27000Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
27010Sstevel@tonic-gate {
27020Sstevel@tonic-gate 	caddr_t	vaddr;
27030Sstevel@tonic-gate 	uintptr_t v;
27040Sstevel@tonic-gate 	size_t	start;
27050Sstevel@tonic-gate 	size_t	end;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
27080Sstevel@tonic-gate 	if (vaddr == NULL)
27090Sstevel@tonic-gate 		return (NULL);
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
27120Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
27133446Smrj 	ASSERT(v + size <= valloc_base);
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 	start = btop(v - kernelbase);
27160Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
27170Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
27180Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	while (start <= end) {
27210Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
27220Sstevel@tonic-gate 		++start;
27230Sstevel@tonic-gate 	}
27240Sstevel@tonic-gate 	return (vaddr);
27250Sstevel@tonic-gate }
27260Sstevel@tonic-gate 
27270Sstevel@tonic-gate void
27280Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
27290Sstevel@tonic-gate {
27300Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
27310Sstevel@tonic-gate 	size_t	start;
27320Sstevel@tonic-gate 	size_t	end;
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
27353446Smrj 	ASSERT(v + size <= valloc_base);
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate 	start = btop(v - kernelbase);
27380Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
27390Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
27400Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	while (start <= end) {
27430Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
27440Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
27450Sstevel@tonic-gate 		++start;
27460Sstevel@tonic-gate 	}
27470Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
27480Sstevel@tonic-gate }
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate /*
27510Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
27520Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
27530Sstevel@tonic-gate  */
27540Sstevel@tonic-gate void *
27550Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
27560Sstevel@tonic-gate {
27570Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
27580Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
27590Sstevel@tonic-gate 	size_t start;
27600Sstevel@tonic-gate 	size_t end;
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	/*
27630Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
27640Sstevel@tonic-gate 	 */
27650Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
27660Sstevel@tonic-gate 		return (NULL);
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 	/*
27690Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
27700Sstevel@tonic-gate 	 */
27713446Smrj 	if (v >= valloc_base || eaddr < kernelbase)
27720Sstevel@tonic-gate 		return (NULL);
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 	/*
27750Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
27760Sstevel@tonic-gate 	 */
27770Sstevel@tonic-gate 	if (v < kernelbase)
27780Sstevel@tonic-gate 		v = kernelbase;
27790Sstevel@tonic-gate 	start = btop(v - kernelbase);
27800Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
27810Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
27820Sstevel@tonic-gate 		end = toxic_bit_map_len;
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
27850Sstevel@tonic-gate 		return (NULL);
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	v = kernelbase + ptob(start);
27880Sstevel@tonic-gate 	if (len != NULL)
27890Sstevel@tonic-gate 		*len = ptob(end - start);
27900Sstevel@tonic-gate 	return ((void *)v);
27910Sstevel@tonic-gate }
27920Sstevel@tonic-gate 
27933446Smrj #endif	/* __i386 */
2794