xref: /onnv-gate/usr/src/uts/i86pc/os/startup.c (revision 5316:35a13307d903)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51455Sandrei  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223434Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/t_lock.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/signal.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/user.h>
350Sstevel@tonic-gate #include <sys/mman.h>
360Sstevel@tonic-gate #include <sys/vm.h>
370Sstevel@tonic-gate #include <sys/conf.h>
380Sstevel@tonic-gate #include <sys/avintr.h>
390Sstevel@tonic-gate #include <sys/autoconf.h>
400Sstevel@tonic-gate #include <sys/disp.h>
410Sstevel@tonic-gate #include <sys/class.h>
420Sstevel@tonic-gate #include <sys/bitmap.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/privregs.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/proc.h>
470Sstevel@tonic-gate #include <sys/buf.h>
480Sstevel@tonic-gate #include <sys/kmem.h>
493446Smrj #include <sys/mem.h>
500Sstevel@tonic-gate #include <sys/kstat.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <sys/reboot.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/cred.h>
550Sstevel@tonic-gate #include <sys/vnode.h>
560Sstevel@tonic-gate #include <sys/file.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/procfs.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <sys/vfs.h>
610Sstevel@tonic-gate #include <sys/cmn_err.h>
620Sstevel@tonic-gate #include <sys/utsname.h>
630Sstevel@tonic-gate #include <sys/debug.h>
640Sstevel@tonic-gate #include <sys/kdi.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/dumphdr.h>
670Sstevel@tonic-gate #include <sys/bootconf.h>
680Sstevel@tonic-gate #include <sys/varargs.h>
690Sstevel@tonic-gate #include <sys/promif.h>
705084Sjohnlev #include <sys/modctl.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <sys/sunddi.h>
730Sstevel@tonic-gate #include <sys/sunndi.h>
740Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
750Sstevel@tonic-gate #include <sys/ddidmareq.h>
760Sstevel@tonic-gate #include <sys/psw.h>
770Sstevel@tonic-gate #include <sys/regset.h>
780Sstevel@tonic-gate #include <sys/clock.h>
790Sstevel@tonic-gate #include <sys/pte.h>
800Sstevel@tonic-gate #include <sys/tss.h>
810Sstevel@tonic-gate #include <sys/stack.h>
820Sstevel@tonic-gate #include <sys/trap.h>
830Sstevel@tonic-gate #include <sys/fp.h>
845159Sjohnlev #include <vm/kboot_mmu.h>
850Sstevel@tonic-gate #include <vm/anon.h>
860Sstevel@tonic-gate #include <vm/as.h>
870Sstevel@tonic-gate #include <vm/page.h>
880Sstevel@tonic-gate #include <vm/seg.h>
890Sstevel@tonic-gate #include <vm/seg_dev.h>
900Sstevel@tonic-gate #include <vm/seg_kmem.h>
910Sstevel@tonic-gate #include <vm/seg_kpm.h>
920Sstevel@tonic-gate #include <vm/seg_map.h>
930Sstevel@tonic-gate #include <vm/seg_vn.h>
940Sstevel@tonic-gate #include <vm/seg_kp.h>
950Sstevel@tonic-gate #include <sys/memnode.h>
960Sstevel@tonic-gate #include <vm/vm_dep.h>
970Sstevel@tonic-gate #include <sys/thread.h>
980Sstevel@tonic-gate #include <sys/sysconf.h>
990Sstevel@tonic-gate #include <sys/vm_machparam.h>
1000Sstevel@tonic-gate #include <sys/archsystm.h>
1010Sstevel@tonic-gate #include <sys/machsystm.h>
1020Sstevel@tonic-gate #include <vm/hat.h>
1030Sstevel@tonic-gate #include <vm/hat_i86.h>
1040Sstevel@tonic-gate #include <sys/pmem.h>
1050Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1060Sstevel@tonic-gate #include <sys/x86_archext.h>
1070Sstevel@tonic-gate #include <sys/segments.h>
1080Sstevel@tonic-gate #include <sys/clconf.h>
1090Sstevel@tonic-gate #include <sys/kobj.h>
1100Sstevel@tonic-gate #include <sys/kobj_lex.h>
1110Sstevel@tonic-gate #include <sys/cpc_impl.h>
1120Sstevel@tonic-gate #include <sys/x86_archext.h>
1131414Scindi #include <sys/cpu_module.h>
114437Smws #include <sys/smbios.h>
1153446Smrj #include <sys/debug_info.h>
1165159Sjohnlev #include <sys/bootinfo.h>
1175107Seota #include <sys/ddi_timer.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);
1380Sstevel@tonic-gate 
1394878Sblakej extern int size_pse_array(pgcnt_t, int);
1404878Sblakej 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1430Sstevel@tonic-gate  * interface.
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * segkp
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate extern int segkp_fromheap;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate static void kvm_init(void);
1530Sstevel@tonic-gate static void startup_init(void);
1540Sstevel@tonic-gate static void startup_memlist(void);
1553446Smrj static void startup_kmem(void);
1560Sstevel@tonic-gate static void startup_modules(void);
1570Sstevel@tonic-gate static void startup_vm(void);
1580Sstevel@tonic-gate static void startup_end(void);
1594828Sjosephb static void layout_kernel_va(void);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1630Sstevel@tonic-gate  */
1641479Skchow #ifdef __i386
1654828Sjosephb 
1661479Skchow /*
1671479Skchow  * Due to virtual address space limitations running in 32 bit mode, restrict
1684828Sjosephb  * the amount of physical memory configured to a max of PHYSMEM pages (16g).
1691479Skchow  *
1701479Skchow  * If the physical max memory size of 64g were allowed to be configured, the
1711479Skchow  * size of user virtual address space will be less than 1g. A limited user
1721479Skchow  * address space greatly reduces the range of applications that can run.
1731479Skchow  *
1744828Sjosephb  * If more physical memory than PHYSMEM is required, users should preferably
1754828Sjosephb  * run in 64 bit mode which has far looser virtual address space limitations.
1761479Skchow  *
1771479Skchow  * If 64 bit mode is not available (as in IA32) and/or more physical memory
1784828Sjosephb  * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
1791479Skchow  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
1801479Skchow  * should also be carefully tuned to balance out the need of the user
1811479Skchow  * application while minimizing the risk of kernel heap exhaustion due to
1821479Skchow  * kernelbase being set too high.
1831479Skchow  */
1844828Sjosephb #define	PHYSMEM	0x400000
1854828Sjosephb 
1864828Sjosephb #else /* __amd64 */
1871479Skchow 
1884828Sjosephb /*
1894828Sjosephb  * For now we can handle memory with physical addresses up to about
1904828Sjosephb  * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
1914828Sjosephb  * half the VA space for seg_kpm. When systems get bigger than 64TB this
1924828Sjosephb  * code will need revisiting. There is an implicit assumption that there
1934828Sjosephb  * are no *huge* holes in the physical address space too.
1944828Sjosephb  */
1954828Sjosephb #define	TERABYTE		(1ul << 40)
1964828Sjosephb #define	PHYSMEM_MAX64		mmu_btop(64 * TERABYTE)
1974828Sjosephb #define	PHYSMEM			PHYSMEM_MAX64
1984828Sjosephb #define	AMD64_VA_HOLE_END	0xFFFF800000000000ul
1994828Sjosephb 
2004828Sjosephb #endif /* __amd64 */
2014828Sjosephb 
2024828Sjosephb pgcnt_t physmem = PHYSMEM;
2030Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate char *kobj_file_buf;
2060Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
2090Sstevel@tonic-gate caddr_t	rm_platter_va;
2100Sstevel@tonic-gate uint32_t rm_platter_pa;
2110Sstevel@tonic-gate 
212841Skchow int	auto_lpg_disable = 1;
213841Skchow 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate uintptr_t hole_start, hole_end;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate  * kpm mapping window
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate caddr_t kpm_vbase;
2230Sstevel@tonic-gate size_t  kpm_size;
2244828Sjosephb static int kpm_desired;
2254828Sjosephb #ifdef __amd64
2264828Sjosephb static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
2274828Sjosephb #endif
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * Configuration parameters set at boot time.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
2360Sstevel@tonic-gate struct bootops		**bootopsp;
2370Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate char bootblock_fstype[16];
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2443290Sjohansen  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
2453290Sjohansen  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
2463290Sjohansen  * zio buffers from their own segment, otherwise they are allocated from the
2473290Sjohansen  * heap.  The optimization of allocating zio buffers from their own segment is
2483290Sjohansen  * only valid on 64-bit kernels.
2493290Sjohansen  */
2503290Sjohansen #if defined(__amd64)
2513290Sjohansen int segzio_fromheap = 0;
2523290Sjohansen #else
2533290Sjohansen int segzio_fromheap = 1;
2543290Sjohansen #endif
2553290Sjohansen 
2563290Sjohansen /*
2570Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
2580Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2590Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2600Sstevel@tonic-gate  */
2610Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate  * VM data structures
2650Sstevel@tonic-gate  */
2660Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
2670Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2680Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
2694878Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
2704878Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
2714878Sblakej int pse_shift;			/* log2(pse_table_size) */
2720Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2730Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
2740Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
2750Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
2760Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
2793446Smrj static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
2803446Smrj 
2810Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate #if defined(__amd64)
2840Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
2850Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
2860Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
2870Sstevel@tonic-gate #else
2880Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
2890Sstevel@tonic-gate #endif
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
2923290Sjohansen caddr_t segzio_base;		/* Base address of segzio */
2930Sstevel@tonic-gate #if defined(__amd64)
2940Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
2950Sstevel@tonic-gate #else
2960Sstevel@tonic-gate pgcnt_t segkpsize = 0;
2970Sstevel@tonic-gate #endif
2983290Sjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
2990Sstevel@tonic-gate 
3003446Smrj /*
3013446Smrj  * VA range available to the debugger
3023446Smrj  */
3033446Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
3043446Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
3053446Smrj 
3060Sstevel@tonic-gate struct memseg *memseg_base;
3070Sstevel@tonic-gate struct vnode unused_pages_vp;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate #define	FOURGB	0x100000000LL
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate struct memlist *memlist;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
3140Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
3150Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
3160Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
3170Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
3180Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
3190Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
3200Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
3230Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
3270Sstevel@tonic-gate  */
3283446Smrj static pgcnt_t kphysm_init(page_t *, pgcnt_t);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate  * a couple useful roundup macros
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
3360Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
3370Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
3380Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
3390Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
3403446Smrj 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
3410Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
3420Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
3460Sstevel@tonic-gate  *		+-----------------------+
3473446Smrj  *		|			|
3480Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
3490Sstevel@tonic-gate  *		|	debugger	|
3500Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
3510Sstevel@tonic-gate  *		|      Kernel Data	|
3520Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
3530Sstevel@tonic-gate  *              |      Kernel Text	|
3545084Sjohnlev  * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
3553446Smrj  *		|---       GDT       ---|- GDT page (GDT_VA)
3563446Smrj  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
3573446Smrj  *		|			|
3583446Smrj  * 		|   page_t structures	|
3590Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
3600Sstevel@tonic-gate  * 		|   page hash, etc.	|
3613446Smrj  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
3623446Smrj  *		|			|  (segkp is just an arena in the heap)
3630Sstevel@tonic-gate  *		|			|
3640Sstevel@tonic-gate  *		|	kvseg		|
3650Sstevel@tonic-gate  *		|			|
3660Sstevel@tonic-gate  *		|			|
3670Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
3680Sstevel@tonic-gate  * 		|        Segkmap	|
3693446Smrj  * 0xC3002000  -|-----------------------|- segmap_start (floating)
3700Sstevel@tonic-gate  *		|	Red Zone	|
3710Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
3720Sstevel@tonic-gate  *		|			|			||
3730Sstevel@tonic-gate  *		|     Shared objects	|			\/
3740Sstevel@tonic-gate  *		|			|
3750Sstevel@tonic-gate  *		:			:
3760Sstevel@tonic-gate  *		|	user data	|
3770Sstevel@tonic-gate  *		|-----------------------|
3780Sstevel@tonic-gate  *		|	user text	|
3790Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
3800Sstevel@tonic-gate  *		|	user stack	|
3810Sstevel@tonic-gate  *		:			:
3820Sstevel@tonic-gate  *		|	invalid		|
3830Sstevel@tonic-gate  * 0x00000000	+-----------------------+
3840Sstevel@tonic-gate  *
3850Sstevel@tonic-gate  *
3860Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
3870Sstevel@tonic-gate  *			+-----------------------+
3883446Smrj  *			|			|
3890Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
3900Sstevel@tonic-gate  *			|	debugger (?)	|
3910Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
3920Sstevel@tonic-gate  *			|      unused    	|
3930Sstevel@tonic-gate  *			+-----------------------+
3940Sstevel@tonic-gate  *			|      Kernel Data	|
3950Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
3960Sstevel@tonic-gate  *			|      Kernel Text	|
3970Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
3983446Smrj  *			|---       GDT       ---|- GDT page (GDT_VA)
3993446Smrj  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
4003446Smrj  *			|			|
4010Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
4020Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
4030Sstevel@tonic-gate  *			|	 Kernel		|
4040Sstevel@tonic-gate  *			|	  heap		|
4050Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
4063446Smrj  *			|	 segmap		|
4073446Smrj  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
4080Sstevel@tonic-gate  *			|    device mappings	|
4090Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
4103290Sjohansen  *			|	  segzio	|
4113290Sjohansen  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
4120Sstevel@tonic-gate  *			|	  segkp		|
4133446Smrj  * ---                  |-----------------------|- segkp_base (floating)
4143446Smrj  * 			|   page_t structures	|  valloc_base + valloc_sz
4153446Smrj  * 			|   memsegs, memlists, 	|
4163446Smrj  * 			|   page hash, etc.	|
4174828Sjosephb  * 0xFFFFFF00.00000000  |-----------------------|- valloc_base (lower if > 1TB)
4180Sstevel@tonic-gate  *			|	 segkpm		|
4190Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
4200Sstevel@tonic-gate  *			|	Red Zone	|
4214828Sjosephb  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE (lower if > 1TB)
4220Sstevel@tonic-gate  *			|     User stack	|- User space memory
4230Sstevel@tonic-gate  * 			|			|
4240Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
4250Sstevel@tonic-gate  *			:			:
4260Sstevel@tonic-gate  * 			|			|
4270Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
4280Sstevel@tonic-gate  * 			|			|
4290Sstevel@tonic-gate  * 			| VA Hole / unused	|
4300Sstevel@tonic-gate  * 			|			|
4310Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
4320Sstevel@tonic-gate  *			|			|
4330Sstevel@tonic-gate  *			|			|
4340Sstevel@tonic-gate  *			:			:
4350Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
4360Sstevel@tonic-gate  *			|			|
4370Sstevel@tonic-gate  *			|	user data	|
4380Sstevel@tonic-gate  *			|-----------------------|
4390Sstevel@tonic-gate  *			|	user text	|
4400Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
4410Sstevel@tonic-gate  *			|	invalid		|
4420Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
4430Sstevel@tonic-gate  *
4440Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
4450Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
4460Sstevel@tonic-gate  *
4470Sstevel@tonic-gate  * Floating values:
4480Sstevel@tonic-gate  *
4490Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
4503446Smrj  * structures.  This region contains page_t structures for
4513446Smrj  * physical memory, memsegs, memlists, and the page hash.
4520Sstevel@tonic-gate  *
4530Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
4540Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
4550Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
4560Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
4570Sstevel@tonic-gate  * accessed using rip-relative addressing
4580Sstevel@tonic-gate  *
4590Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
4600Sstevel@tonic-gate  *
4610Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
4620Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
4630Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
4640Sstevel@tonic-gate  *
4653446Smrj  * segmap_start: start of segmap. The length of segmap can be modified
4660Sstevel@tonic-gate  * by changing segmapsize in /etc/system (preferred) or eeprom (deprecated).
4670Sstevel@tonic-gate  * The default length is 16MB on 32-bit systems and 64MB on 64-bit systems.
4680Sstevel@tonic-gate  *
4690Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
4700Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
4710Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
4720Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
4730Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
4740Sstevel@tonic-gate  * segkp sizes).
4750Sstevel@tonic-gate  *
4760Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
4770Sstevel@tonic-gate  *	size	page_t's			size
4780Sstevel@tonic-gate  *	----	---------	----------	-----------
4790Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
4800Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
4810Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
4820Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
4830Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
4840Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
4850Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
4860Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
4870Sstevel@tonic-gate  *
4880Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
4890Sstevel@tonic-gate  * configurations above 8gb.
4900Sstevel@tonic-gate  *
4910Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
4920Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
4930Sstevel@tonic-gate  */
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /* real-time-clock initialization parameters */
4963446Smrj extern time_t process_rtc_config_file(void);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate uintptr_t	kernelbase;
4993446Smrj uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
5000Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
5010Sstevel@tonic-gate size_t		segmapsize;
5023446Smrj uintptr_t	segmap_start;
5030Sstevel@tonic-gate int		segmapfreelists;
5040Sstevel@tonic-gate pgcnt_t		npages;
5053446Smrj pgcnt_t		orig_npages;
5060Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
5070Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate /*
5100Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
5110Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
5120Sstevel@tonic-gate  * the bootstrap.
5130Sstevel@tonic-gate  */
5143446Smrj static page_t *bootpages;
5153446Smrj 
5163446Smrj /*
5173446Smrj  * boot time pages that have a vnode from the ramdisk will keep that forever.
5183446Smrj  */
5193446Smrj static page_t *rd_pages;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate struct system_hardware system_hardware;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate /*
5240Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
5250Sstevel@tonic-gate  */
5260Sstevel@tonic-gate static void
5273446Smrj print_memlist(char *title, struct memlist *mp)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
5300Sstevel@tonic-gate 	while (mp != NULL)  {
5310Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
5320Sstevel@tonic-gate 		    mp->address, mp->size);
5330Sstevel@tonic-gate 		mp = mp->next;
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
5390Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate int	l2cache_sz = 0x80000;
5420Sstevel@tonic-gate int	l2cache_linesz = 0x40;
5430Sstevel@tonic-gate int	l2cache_assoc = 1;
5440Sstevel@tonic-gate 
5454426Saguzovsk static size_t	textrepl_min_gb = 10;
5464426Saguzovsk 
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
5490Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
5500Sstevel@tonic-gate  */
5510Sstevel@tonic-gate #ifdef __amd64
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate vmem_t		*device_arena;
5540Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
5553446Smrj size_t		toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate #else	/* __i386 */
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
5600Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate #endif	/* __i386 */
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * Simple boot time debug facilities
5660Sstevel@tonic-gate  */
5670Sstevel@tonic-gate static char *prm_dbg_str[] = {
5680Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%x\n",
5690Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%llx\n"
5700Sstevel@tonic-gate };
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate int prom_debug;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate #define	PRM_DEBUG(q)	if (prom_debug) 	\
5750Sstevel@tonic-gate 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
5760Sstevel@tonic-gate #define	PRM_POINT(q)	if (prom_debug) 	\
5770Sstevel@tonic-gate 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate /*
5800Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
5810Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
5820Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
5830Sstevel@tonic-gate  */
5840Sstevel@tonic-gate #define	NUM_ALLOCATIONS 7
5850Sstevel@tonic-gate int num_allocations = 0;
5860Sstevel@tonic-gate struct {
5870Sstevel@tonic-gate 	void **al_ptr;
5880Sstevel@tonic-gate 	size_t al_size;
5890Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
5900Sstevel@tonic-gate size_t valloc_sz = 0;
5910Sstevel@tonic-gate uintptr_t valloc_base;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
5940Sstevel@tonic-gate 		size = ROUND_UP_PAGE(size);		 		\
5950Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
5960Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
5970Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
5980Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
5990Sstevel@tonic-gate 		valloc_sz += size;					\
6000Sstevel@tonic-gate 		++num_allocations;				 	\
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6033446Smrj /*
6043446Smrj  * Allocate all the initial memory needed by the page allocator.
6053446Smrj  */
6060Sstevel@tonic-gate static void
6070Sstevel@tonic-gate perform_allocations(void)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate 	caddr_t mem;
6100Sstevel@tonic-gate 	int i;
6113446Smrj 	int valloc_align;
6120Sstevel@tonic-gate 
6133446Smrj 	PRM_DEBUG(valloc_base);
6143446Smrj 	PRM_DEBUG(valloc_sz);
6153446Smrj 	valloc_align = mmu.level_size[mmu.max_page_level > 0];
6163446Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
6170Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
6180Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
6190Sstevel@tonic-gate 	bzero(mem, valloc_sz);
6200Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
6210Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
6220Sstevel@tonic-gate 		mem += allocations[i].al_size;
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate /*
6270Sstevel@tonic-gate  * Our world looks like this at startup time.
6280Sstevel@tonic-gate  *
6290Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
6300Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
6310Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
6320Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
6330Sstevel@tonic-gate  *
6340Sstevel@tonic-gate  * On the text page:
6350Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
6360Sstevel@tonic-gate  *
6370Sstevel@tonic-gate  * On the data page:
6383446Smrj  * unix/genunix/krtld/module data loads.
6393446Smrj  *
6400Sstevel@tonic-gate  * Machine-dependent startup code
6410Sstevel@tonic-gate  */
6420Sstevel@tonic-gate void
6430Sstevel@tonic-gate startup(void)
6440Sstevel@tonic-gate {
6455084Sjohnlev #if !defined(__xpv)
6461494Sjg 	extern void startup_bios_disk(void);
6471494Sjg 	extern void startup_pci_bios(void);
6485084Sjohnlev #endif
6495084Sjohnlev 	/*
6505084Sjohnlev 	 * Make sure that nobody tries to use sekpm until we have
6515084Sjohnlev 	 * initialized it properly.
6525084Sjohnlev 	 */
6530Sstevel@tonic-gate #if defined(__amd64)
6544828Sjosephb 	kpm_desired = 1;
6550Sstevel@tonic-gate #endif
6560Sstevel@tonic-gate 	kpm_enable = 0;
6570Sstevel@tonic-gate 
6585084Sjohnlev #if defined(__xpv)	/* XXPV fix me! */
6595084Sjohnlev 	{
6605084Sjohnlev 		extern int segvn_use_regions;
6615084Sjohnlev 		segvn_use_regions = 0;
6625084Sjohnlev 	}
6635084Sjohnlev #endif
6640Sstevel@tonic-gate 	progressbar_init();
6650Sstevel@tonic-gate 	startup_init();
666*5316Sjohnlev #if defined(__xpv)
667*5316Sjohnlev 	startup_xen_version();
668*5316Sjohnlev #endif
6690Sstevel@tonic-gate 	startup_memlist();
6703446Smrj 	startup_kmem();
6714828Sjosephb 	startup_vm();
6725084Sjohnlev #if !defined(__xpv)
6731494Sjg 	startup_pci_bios();
6745084Sjohnlev #endif
6750Sstevel@tonic-gate 	startup_modules();
6765084Sjohnlev #if !defined(__xpv)
6770Sstevel@tonic-gate 	startup_bios_disk();
6785084Sjohnlev #endif
6790Sstevel@tonic-gate 	startup_end();
6800Sstevel@tonic-gate 	progressbar_start();
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate static void
6840Sstevel@tonic-gate startup_init()
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	/*
6890Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
6900Sstevel@tonic-gate 	 */
6910Sstevel@tonic-gate 	cpuid_pass2(CPU);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/*
6960Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
6970Sstevel@tonic-gate 	 */
6980Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
6990Sstevel@tonic-gate 		++prom_debug;
7000Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/*
7040Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
7050Sstevel@tonic-gate 	 */
7060Sstevel@tonic-gate 	get_system_configuration();
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/*
7090Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
7100Sstevel@tonic-gate 	 */
7110Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
7120Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
7130Sstevel@tonic-gate 		    CPU->cpu_brandstr);
7140Sstevel@tonic-gate 		halt("This processor is not supported by this release "
7150Sstevel@tonic-gate 		    "of Solaris.");
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate /*
7220Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
7230Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
7240Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
7250Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate static void
7280Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	uintptr_t va;
7310Sstevel@tonic-gate 	uintptr_t next_va;
7320Sstevel@tonic-gate 	pfn_t pfn;
7330Sstevel@tonic-gate 	uint64_t pfn_addr;
7340Sstevel@tonic-gate 	uint64_t pfn_eaddr;
7350Sstevel@tonic-gate 	uint_t prot;
7360Sstevel@tonic-gate 	size_t len;
7370Sstevel@tonic-gate 	uint_t change;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if (prom_debug)
7400Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
7410Sstevel@tonic-gate 		    *addr, *size);
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	/*
7440Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
7450Sstevel@tonic-gate 	 */
7460Sstevel@tonic-gate 	if (*addr == 0) {
7470Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
7480Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7523446Smrj 	 * First we trim from the front of the range. Since kbm_probe()
7530Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
7540Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
7550Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
7560Sstevel@tonic-gate 	 * but w < v.
7570Sstevel@tonic-gate 	 */
7580Sstevel@tonic-gate 	do {
7590Sstevel@tonic-gate 		change = 0;
7600Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
7613446Smrj 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7620Sstevel@tonic-gate 		    va = next_va) {
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 			next_va = va + len;
7653446Smrj 			pfn_addr = pfn_to_pa(pfn);
7660Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
7690Sstevel@tonic-gate 				change = 1;
7700Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
7710Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
7720Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
7730Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
7740Sstevel@tonic-gate 				}
7750Sstevel@tonic-gate 			}
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 		if (change && prom_debug)
7780Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
7790Sstevel@tonic-gate 			    *addr, *size);
7800Sstevel@tonic-gate 	} while (change);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	/*
7830Sstevel@tonic-gate 	 * Trim pages from the end of the range.
7840Sstevel@tonic-gate 	 */
7850Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
7863446Smrj 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7870Sstevel@tonic-gate 	    va = next_va) {
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 		next_va = va + len;
7903446Smrj 		pfn_addr = pfn_to_pa(pfn);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
7930Sstevel@tonic-gate 			*size = pfn_addr - *addr;
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	if (prom_debug)
7970Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
7980Sstevel@tonic-gate 		    *addr, *size);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate static void
8020Sstevel@tonic-gate kpm_init()
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate 	struct segkpm_crargs b;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
8080Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
8090Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
8100Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
8110Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
8120Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
8130Sstevel@tonic-gate 	 * pagesize is used.
8140Sstevel@tonic-gate 	 */
8150Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
8160Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
8170Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
8180Sstevel@tonic-gate 	kpmp2pshft = 0;
8190Sstevel@tonic-gate 	kpmpnpgs = 1;
8200Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
8230Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
8260Sstevel@tonic-gate 		panic("cannot attach segkpm");
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
8290Sstevel@tonic-gate 	b.nvcolors = 1;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
8320Sstevel@tonic-gate 		panic("segkpm_create segkpm");
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate /*
8383446Smrj  * The debug info page provides enough information to allow external
8393446Smrj  * inspectors (e.g. when running under a hypervisor) to bootstrap
8403446Smrj  * themselves into allowing full-blown kernel debugging.
8413446Smrj  */
8423446Smrj static void
8433446Smrj init_debug_info(void)
8443446Smrj {
8453446Smrj 	caddr_t mem;
8463446Smrj 	debug_info_t *di;
8473446Smrj 
8483446Smrj #ifndef __lint
8493446Smrj 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
8503446Smrj #endif
8513446Smrj 
8523446Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
8533446Smrj 	    MMU_PAGESIZE);
8543446Smrj 
8553446Smrj 	if (mem != (caddr_t)DEBUG_INFO_VA)
8563446Smrj 		panic("BOP_ALLOC() failed");
8573446Smrj 	bzero(mem, MMU_PAGESIZE);
8583446Smrj 
8593446Smrj 	di = (debug_info_t *)mem;
8603446Smrj 
8613446Smrj 	di->di_magic = DEBUG_INFO_MAGIC;
8623446Smrj 	di->di_version = DEBUG_INFO_VERSION;
8635084Sjohnlev 	di->di_modules = (uintptr_t)&modules;
8645084Sjohnlev 	di->di_s_text = (uintptr_t)s_text;
8655084Sjohnlev 	di->di_e_text = (uintptr_t)e_text;
8665084Sjohnlev 	di->di_s_data = (uintptr_t)s_data;
8675084Sjohnlev 	di->di_e_data = (uintptr_t)e_data;
8685084Sjohnlev 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
8695084Sjohnlev 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
8703446Smrj }
8713446Smrj 
8723446Smrj /*
8733446Smrj  * Build the memlists and other kernel essential memory system data structures.
8743446Smrj  * This is everything at valloc_base.
8750Sstevel@tonic-gate  */
8760Sstevel@tonic-gate static void
8770Sstevel@tonic-gate startup_memlist(void)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	size_t memlist_sz;
8800Sstevel@tonic-gate 	size_t memseg_sz;
8810Sstevel@tonic-gate 	size_t pagehash_sz;
8820Sstevel@tonic-gate 	size_t pp_sz;
8830Sstevel@tonic-gate 	uintptr_t va;
8840Sstevel@tonic-gate 	size_t len;
8850Sstevel@tonic-gate 	uint_t prot;
8860Sstevel@tonic-gate 	pfn_t pfn;
8870Sstevel@tonic-gate 	int memblocks;
8880Sstevel@tonic-gate 	caddr_t pagecolor_mem;
8890Sstevel@tonic-gate 	size_t pagecolor_memsz;
8900Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
8910Sstevel@tonic-gate 	size_t page_ctrs_size;
8924878Sblakej 	size_t pse_table_alloc_size;
8930Sstevel@tonic-gate 	struct memlist *current;
8940Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
8970Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
8980Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	/*
9030Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
9040Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
9050Sstevel@tonic-gate 	 */
9063446Smrj 	len = kbm_nucleus_size;
9073446Smrj 	ASSERT(len > MMU_PAGESIZE);
9080Sstevel@tonic-gate 
9093446Smrj 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
9103446Smrj 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
9113446Smrj 	if (e_moddata - moddata > MODDATA)
9123446Smrj 		e_moddata = moddata + MODDATA;
9130Sstevel@tonic-gate 
9143446Smrj 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
9153446Smrj 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
9163446Smrj 	if (e_modtext - modtext > MODTEXT)
9173446Smrj 		e_modtext = modtext + MODTEXT;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	econtig = e_moddata;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9220Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
9230Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9240Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
9250Sstevel@tonic-gate 	PRM_DEBUG(econtig);
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/*
9283446Smrj 	 * Examine the boot loader physical memory map to find out:
9290Sstevel@tonic-gate 	 * - total memory in system - physinstalled
9300Sstevel@tonic-gate 	 * - the max physical address - physmax
9313446Smrj 	 * - the number of discontiguous segments of memory.
9320Sstevel@tonic-gate 	 */
9330Sstevel@tonic-gate 	if (prom_debug)
9343446Smrj 		print_memlist("boot physinstalled",
9350Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
9360Sstevel@tonic-gate 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
9370Sstevel@tonic-gate 	    &physinstalled, &memblocks);
9380Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9390Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9400Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	/*
9430Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
9440Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
9450Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
9460Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
9470Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
9480Sstevel@tonic-gate 	 */
9490Sstevel@tonic-gate 	mmu_init();
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate #ifdef	__i386
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
9540Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
9550Sstevel@tonic-gate 	 */
9560Sstevel@tonic-gate 	if (mmu.pae_hat) {
9570Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
9580Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
9590Sstevel@tonic-gate 			physmax = PFN_64G - 1;
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 	} else {
9620Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
9630Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
9640Sstevel@tonic-gate 			physmax = PFN_4G - 1;
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 	}
9670Sstevel@tonic-gate #endif
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
9720Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
9730Sstevel@tonic-gate 		char value[8];
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		if (len < 8)
9760Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
9770Sstevel@tonic-gate 		else
9780Sstevel@tonic-gate 			(void) strcpy(value, "");
9790Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
9800Sstevel@tonic-gate 			mmu.pt_nx = 0;
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	/*
9850Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
9860Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
9870Sstevel@tonic-gate 	 *
9880Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
9890Sstevel@tonic-gate 	 */
9900Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
9910Sstevel@tonic-gate 	obp_pages = 0;
9920Sstevel@tonic-gate 	va = KERNEL_TEXT;
9933446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
9940Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
9950Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
9960Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
9970Sstevel@tonic-gate 		va += len;
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	PRM_DEBUG(npages);
10000Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	/*
10034828Sjosephb 	 * If physmem is patched to be non-zero, use it instead of the computed
10044828Sjosephb 	 * value unless it is larger than the actual amount of memory on hand.
10050Sstevel@tonic-gate 	 */
10061417Skchow 	if (physmem == 0 || physmem > npages) {
10070Sstevel@tonic-gate 		physmem = npages;
10081417Skchow 	} else if (physmem < npages) {
10091479Skchow 		orig_npages = npages;
10100Sstevel@tonic-gate 		npages = physmem;
10111417Skchow 	}
10120Sstevel@tonic-gate 	PRM_DEBUG(physmem);
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/*
10150Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
10160Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
10170Sstevel@tonic-gate 	 * include:
10180Sstevel@tonic-gate 	 *	memsegs
10190Sstevel@tonic-gate 	 *	memlists
10200Sstevel@tonic-gate 	 *	page hash table
10210Sstevel@tonic-gate 	 *	page_t's
10220Sstevel@tonic-gate 	 *	page coloring data structs
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
10250Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
10260Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	/*
10293446Smrj 	 * Reserve space for memlists. There's no real good way to know exactly
10303446Smrj 	 * how much room we'll need, but this should be a good upper bound.
10310Sstevel@tonic-gate 	 */
10320Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
10330Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
10340Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
10350Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	/*
10380Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
10390Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
10400Sstevel@tonic-gate 	 */
10410Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
10420Sstevel@tonic-gate 	page_hashsz = 1 << highbit(page_hashsz);
10430Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
10440Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
10450Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/*
10483446Smrj 	 * Set aside room for the page structures themselves.
10490Sstevel@tonic-gate 	 */
10503446Smrj 	PRM_DEBUG(npages);
10513446Smrj 	pp_sz = sizeof (struct page) * npages;
10520Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
10530Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	/*
10560Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
10570Sstevel@tonic-gate 	 */
10580Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
10590Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
10600Sstevel@tonic-gate 	pagecolor_memsz =
10610Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
10620Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
10630Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
10660Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
10670Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
10680Sstevel@tonic-gate 
10694878Sblakej 	/*
10704878Sblakej 	 * Allocate the array that protects pp->p_selock.
10714878Sblakej 	 */
10724878Sblakej 	pse_shift = size_pse_array(physmem, max_ncpus);
10734878Sblakej 	pse_table_size = 1 << pse_shift;
10744878Sblakej 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
10754878Sblakej 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
10764878Sblakej 
10773446Smrj #if defined(__amd64)
10783446Smrj 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
10793446Smrj 	valloc_base = VALLOC_BASE;
10804828Sjosephb 
10814828Sjosephb 	/*
10824828Sjosephb 	 * The default values of VALLOC_BASE and SEGKPM_BASE should work
10834828Sjosephb 	 * for values of physmax up to 1 Terabyte. They need adjusting when
10844828Sjosephb 	 * memory is at addresses above 1 TB.
10854828Sjosephb 	 */
10864828Sjosephb 	if (physmax + 1 > mmu_btop(TERABYTE)) {
10874828Sjosephb 		uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
10884828Sjosephb 
10894828Sjosephb 		/* Round to largest possible pagesize for now */
10904828Sjosephb 		kpm_resv_amount = P2ROUNDUP(kpm_resv_amount, ONE_GIG);
10914828Sjosephb 
10924828Sjosephb 		segkpm_base = -(2 * kpm_resv_amount); /* down from top VA */
10934828Sjosephb 
10944828Sjosephb 		/* make sure we leave some space for user apps above hole */
10954828Sjosephb 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
10964828Sjosephb 		if (segkpm_base > SEGKPM_BASE)
10974828Sjosephb 			segkpm_base = SEGKPM_BASE;
10984828Sjosephb 		PRM_DEBUG(segkpm_base);
10994828Sjosephb 
11004828Sjosephb 		valloc_base = segkpm_base + kpm_resv_amount;
11014828Sjosephb 		PRM_DEBUG(valloc_base);
11024828Sjosephb 	}
11033446Smrj #else	/* __i386 */
11043446Smrj 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
11053446Smrj 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
11064828Sjosephb 	PRM_DEBUG(valloc_base);
11073446Smrj #endif	/* __i386 */
11083446Smrj 
11090Sstevel@tonic-gate 	/*
11103446Smrj 	 * do all the initial allocations
11113446Smrj 	 */
11123446Smrj 	perform_allocations();
11133446Smrj 
11143446Smrj 	/*
11153446Smrj 	 * Build phys_install and phys_avail in kernel memspace.
11163446Smrj 	 * - phys_install should be all memory in the system.
11173446Smrj 	 * - phys_avail is phys_install minus any memory mapped before this
11183446Smrj 	 *    point above KERNEL_TEXT.
11193446Smrj 	 */
11203446Smrj 	current = phys_install = memlist;
11213446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
11223446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
11233446Smrj 		panic("physinstalled was too big!");
11243446Smrj 	if (prom_debug)
11253446Smrj 		print_memlist("phys_install", phys_install);
11263446Smrj 
11273446Smrj 	phys_avail = current;
11283446Smrj 	PRM_POINT("Building phys_avail:\n");
11293446Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
11303446Smrj 	    avail_filter);
11313446Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
11323446Smrj 		panic("physavail was too big!");
11333446Smrj 	if (prom_debug)
11343446Smrj 		print_memlist("phys_avail", phys_avail);
11353446Smrj 
11363446Smrj 	/*
11373446Smrj 	 * setup page coloring
11380Sstevel@tonic-gate 	 */
11393446Smrj 	page_coloring_setup(pagecolor_mem);
11403446Smrj 	page_lock_init();	/* currently a no-op */
11413446Smrj 
11423446Smrj 	/*
11433446Smrj 	 * free page list counters
11443446Smrj 	 */
11453446Smrj 	(void) page_ctrs_alloc(page_ctrs_mem);
11463446Smrj 
11473446Smrj 	/*
11483446Smrj 	 * Initialize the page structures from the memory lists.
11493446Smrj 	 */
11503446Smrj 	availrmem_initial = availrmem = freemem = 0;
11513446Smrj 	PRM_POINT("Calling kphysm_init()...");
11523446Smrj 	npages = kphysm_init(pp_base, npages);
11533446Smrj 	PRM_POINT("kphysm_init() done");
11543446Smrj 	PRM_DEBUG(npages);
11553446Smrj 
11563446Smrj 	init_debug_info();
11573446Smrj 
11583446Smrj 	/*
11593446Smrj 	 * Now that page_t's have been initialized, remove all the
11603446Smrj 	 * initial allocation pages from the kernel free page lists.
11613446Smrj 	 */
11623446Smrj 	boot_mapin((caddr_t)valloc_base, valloc_sz);
11633446Smrj 	boot_mapin((caddr_t)GDT_VA, MMU_PAGESIZE);
11643446Smrj 	boot_mapin((caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE);
11653446Smrj 	PRM_POINT("startup_memlist() done");
11663446Smrj 
11673446Smrj 	PRM_DEBUG(valloc_sz);
11684426Saguzovsk 
11694748Saguzovsk #if defined(__amd64)
11704748Saguzovsk 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
11714748Saguzovsk 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
11724748Saguzovsk 		extern size_t textrepl_size_thresh;
11734426Saguzovsk 		textrepl_size_thresh = (16 << 20) - 1;
11744426Saguzovsk 	}
11754748Saguzovsk #endif
11763446Smrj }
11773446Smrj 
11783446Smrj /*
11793446Smrj  * Layout the kernel's part of address space and initialize kmem allocator.
11803446Smrj  */
11813446Smrj static void
11823446Smrj startup_kmem(void)
11833446Smrj {
11843717Sdp78419 	extern void page_set_colorequiv_arr(void);
11853717Sdp78419 
11863446Smrj 	PRM_POINT("startup_kmem() starting...");
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate #if defined(__amd64)
11890Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
11900Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
11910Sstevel@tonic-gate 		    "systems.");
11924828Sjosephb 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
11930Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
11943446Smrj 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
11950Sstevel@tonic-gate #else	/* __i386 */
11960Sstevel@tonic-gate 	/*
11970Sstevel@tonic-gate 	 * We configure kernelbase based on:
11980Sstevel@tonic-gate 	 *
11990Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
12000Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
12010Sstevel@tonic-gate 	 *
12020Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
12030Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
12040Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
12050Sstevel@tonic-gate 	 *
12060Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
12070Sstevel@tonic-gate 	 */
12080Sstevel@tonic-gate 	if (eprom_kernelbase) {
12090Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
12100Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
12110Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
12120Sstevel@tonic-gate 	} else {
12130Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
12140Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
12150Sstevel@tonic-gate 	}
12160Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
12173446Smrj 	core_base = valloc_base;
12180Sstevel@tonic-gate 	core_size = 0;
12193446Smrj #endif	/* __i386 */
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	PRM_DEBUG(core_base);
12220Sstevel@tonic-gate 	PRM_DEBUG(core_size);
12233446Smrj 	PRM_DEBUG(kernelbase);
12240Sstevel@tonic-gate 
12254828Sjosephb #if defined(__i386)
12264828Sjosephb 	segkp_fromheap = 1;
12274828Sjosephb #endif	/* __i386 */
12283446Smrj 
12293446Smrj 	ekernelheap = (char *)core_base;
12303446Smrj 	PRM_DEBUG(ekernelheap);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	/*
12330Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
12340Sstevel@tonic-gate 	 * update variables that were initialized with a value of
12350Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
12360Sstevel@tonic-gate 	 *
12370Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
12380Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
12390Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
12400Sstevel@tonic-gate 	 *	just be declared as variables there?
12410Sstevel@tonic-gate 	 */
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
12440Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
12454875Sjosephb #if defined(__amd64)
12464875Sjosephb 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
12474875Sjosephb #else
12480Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
12490Sstevel@tonic-gate #endif
12500Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
12510Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
12520Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
12530Sstevel@tonic-gate 
12544828Sjosephb 	layout_kernel_va();
12554828Sjosephb 
12564828Sjosephb #if defined(__i386)
12574828Sjosephb 	/*
12584828Sjosephb 	 * If segmap is too large we can push the bottom of the kernel heap
12594828Sjosephb 	 * higher than the base.  Or worse, it could exceed the top of the
12604828Sjosephb 	 * VA space entirely, causing it to wrap around.
12614828Sjosephb 	 */
12624828Sjosephb 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
12634828Sjosephb 		panic("too little address space available for kernelheap,"
12644828Sjosephb 		    " use eeprom for lower kernelbase or smaller segmapsize");
12654828Sjosephb #endif	/* __i386 */
12664828Sjosephb 
12670Sstevel@tonic-gate 	/*
12680Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
12690Sstevel@tonic-gate 	 */
12704828Sjosephb 	kernelheap_init(kernelheap, ekernelheap,
12714828Sjosephb 	    kernelheap + MMU_PAGESIZE,
12723446Smrj 	    (void *)core_base, (void *)(core_base + core_size));
12730Sstevel@tonic-gate 
12745084Sjohnlev #if defined(__xpv)
12755084Sjohnlev 	/*
12765084Sjohnlev 	 * Link pending events struct into cpu struct
12775084Sjohnlev 	 */
12785084Sjohnlev 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
12795084Sjohnlev #endif
12800Sstevel@tonic-gate 	/*
12810Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
12820Sstevel@tonic-gate 	 */
12830Sstevel@tonic-gate 	kmem_init();
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	/*
12863717Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
12873717Sdp78419 	 */
12883717Sdp78419 	page_set_colorequiv_arr();
12893717Sdp78419 
12903717Sdp78419 	/*
12910Sstevel@tonic-gate 	 * print this out early so that we know what's going on
12920Sstevel@tonic-gate 	 */
12930Sstevel@tonic-gate 	cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * Initialize bp_mapin().
12970Sstevel@tonic-gate 	 */
12980Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
12990Sstevel@tonic-gate 
13001479Skchow 	/*
13011479Skchow 	 * orig_npages is non-zero if physmem has been configured for less
13021479Skchow 	 * than the available memory.
13031479Skchow 	 */
13041479Skchow 	if (orig_npages) {
13054828Sjosephb 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
13064828Sjosephb 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
13074828Sjosephb 		    npages, orig_npages);
13081479Skchow 	}
13090Sstevel@tonic-gate #if defined(__i386)
13100Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
13110Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
13120Sstevel@tonic-gate 		    "System using 0x%lx",
13130Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
13140Sstevel@tonic-gate #endif
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
13170Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
13180Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
13190Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
13200Sstevel@tonic-gate 	}
13210Sstevel@tonic-gate #endif
13220Sstevel@tonic-gate 
13235084Sjohnlev #ifdef __xpv
13245084Sjohnlev 	/*
13255084Sjohnlev 	 * Some of the xen start information has to be relocated up
13265084Sjohnlev 	 * into the kernel's permanent address space.
13275084Sjohnlev 	 */
13285084Sjohnlev 	PRM_POINT("calling xen_relocate_start_info()");
13295084Sjohnlev 	xen_relocate_start_info();
13305084Sjohnlev 	PRM_POINT("xen_relocate_start_info() done");
13315084Sjohnlev 
13325084Sjohnlev 	/*
13335084Sjohnlev 	 * (Update the vcpu pointer in our cpu structure to point into
13345084Sjohnlev 	 * the relocated shared info.)
13355084Sjohnlev 	 */
13365084Sjohnlev 	CPU->cpu_m.mcpu_vcpu_info =
13375084Sjohnlev 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
13385084Sjohnlev #endif
13395084Sjohnlev 
13403446Smrj 	PRM_POINT("startup_kmem() done");
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate static void
13440Sstevel@tonic-gate startup_modules(void)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	unsigned int i;
13470Sstevel@tonic-gate 	extern void prom_setup(void);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
13505084Sjohnlev 
13515084Sjohnlev #ifndef __xpv
13520Sstevel@tonic-gate 	/*
13530Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
13540Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
13550Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
13560Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
13570Sstevel@tonic-gate 	 */
13580Sstevel@tonic-gate 	microfind();
13595084Sjohnlev #endif
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	/*
13620Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
13630Sstevel@tonic-gate 	 */
13643446Smrj 	sgmtl(process_rtc_config_file());
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	/*
13670Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
13680Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
13690Sstevel@tonic-gate 	 */
13700Sstevel@tonic-gate 	param_calc(0);
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	mod_setup();
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	/*
13750Sstevel@tonic-gate 	 * Initialize system parameters.
13760Sstevel@tonic-gate 	 */
13770Sstevel@tonic-gate 	param_init();
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	/*
13802712Snn35248 	 * Initialize the default brands
13812712Snn35248 	 */
13822712Snn35248 	brand_init();
13832712Snn35248 
13842712Snn35248 	/*
13850Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
13860Sstevel@tonic-gate 	 */
13870Sstevel@tonic-gate 	maxmem = physmem;
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	/*
13900Sstevel@tonic-gate 	 * Initialize segment management stuff.
13910Sstevel@tonic-gate 	 */
13920Sstevel@tonic-gate 	seg_init();
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
13950Sstevel@tonic-gate 		halt("Can't load specfs");
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
13980Sstevel@tonic-gate 		halt("Can't load devfs");
13990Sstevel@tonic-gate 
14002621Sllai1 	if (modload("fs", "dev") == -1)
14012621Sllai1 		halt("Can't load dev");
14022621Sllai1 
14031676Sjpk 	(void) modloadonly("sys", "lbl_edition");
14041676Sjpk 
14050Sstevel@tonic-gate 	dispinit();
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	/*
14080Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
14090Sstevel@tonic-gate 	 */
14100Sstevel@tonic-gate 	if ((i = modload("misc", "sysinit")) != (unsigned int)-1)
14110Sstevel@tonic-gate 		(void) modunload(i);
14120Sstevel@tonic-gate 	else
14130Sstevel@tonic-gate 		cmn_err(CE_CONT, "sysinit load failed");
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	/* Read cluster configuration data. */
14160Sstevel@tonic-gate 	clconf_init();
14170Sstevel@tonic-gate 
14185084Sjohnlev #if defined(__xpv)
14195084Sjohnlev 	ec_init();
14205084Sjohnlev 	gnttab_init();
14215084Sjohnlev 	(void) xs_early_init();
14225084Sjohnlev #endif /* __xpv */
14235084Sjohnlev 
14240Sstevel@tonic-gate 	/*
14250Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
14260Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
14270Sstevel@tonic-gate 	 */
14280Sstevel@tonic-gate 	setup_ddi();
14291414Scindi 
14305254Sgavinm #ifndef __xpv
14315254Sgavinm 	{
14325254Sgavinm 		/*
14335254Sgavinm 		 * Set up the CPU module subsystem.  Modifies the device tree,
14345254Sgavinm 		 * so it must be done after setup_ddi().
14355254Sgavinm 		 */
14361414Scindi 
14375254Sgavinm 		cmi_hdl_t hdl;
14385254Sgavinm 
14395254Sgavinm 		if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
14405254Sgavinm 		    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
14415254Sgavinm 			if (x86_feature & X86_MCA)
14425254Sgavinm 				cmi_mca_init(hdl);
14435254Sgavinm 		}
14445254Sgavinm 	}
14455254Sgavinm #endif	/* __xpv */
14461414Scindi 
14470Sstevel@tonic-gate 	/*
14480Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
14490Sstevel@tonic-gate 	 */
14503446Smrj 	PRM_POINT("startup_modules: calling prom_setup...");
14510Sstevel@tonic-gate 	prom_setup();
14523446Smrj 	PRM_POINT("startup_modules: done");
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	/*
14550Sstevel@tonic-gate 	 * Load all platform specific modules
14560Sstevel@tonic-gate 	 */
14573446Smrj 	PRM_POINT("startup_modules: calling psm_modload...");
14580Sstevel@tonic-gate 	psm_modload();
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate 
14633446Smrj /*
14643446Smrj  * claim a "setaside" boot page for use in the kernel
14653446Smrj  */
14663446Smrj page_t *
14673446Smrj boot_claim_page(pfn_t pfn)
14680Sstevel@tonic-gate {
14693446Smrj 	page_t *pp;
14703446Smrj 
14713446Smrj 	pp = page_numtopp_nolock(pfn);
14723446Smrj 	ASSERT(pp != NULL);
14730Sstevel@tonic-gate 
14743446Smrj 	if (PP_ISBOOTPAGES(pp)) {
14753446Smrj 		if (pp->p_next != NULL)
14763446Smrj 			pp->p_next->p_prev = pp->p_prev;
14773446Smrj 		if (pp->p_prev == NULL)
14783446Smrj 			bootpages = pp->p_next;
14793446Smrj 		else
14803446Smrj 			pp->p_prev->p_next = pp->p_next;
14813446Smrj 	} else {
14823446Smrj 		/*
14833446Smrj 		 * htable_attach() expects a base pagesize page
14843446Smrj 		 */
14853446Smrj 		if (pp->p_szc != 0)
14863446Smrj 			page_boot_demote(pp);
14873446Smrj 		pp = page_numtopp(pfn, SE_EXCL);
14883446Smrj 	}
14893446Smrj 	return (pp);
14900Sstevel@tonic-gate }
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate /*
14930Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
14940Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
14950Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
14960Sstevel@tonic-gate  */
14970Sstevel@tonic-gate static void
14980Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
14990Sstevel@tonic-gate {
15000Sstevel@tonic-gate 	uintptr_t va = low;
15010Sstevel@tonic-gate 	size_t len;
15020Sstevel@tonic-gate 	uint_t prot;
15030Sstevel@tonic-gate 	pfn_t pfn;
15040Sstevel@tonic-gate 	page_t *pp;
15050Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
15060Sstevel@tonic-gate 
15073446Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
15080Sstevel@tonic-gate 		if (va + len >= high)
15090Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
15100Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 		while (len > 0) {
15130Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
15140Sstevel@tonic-gate 			if (pp != NULL) {
15150Sstevel@tonic-gate 				if (setaside == 0)
15160Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
15170Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
15180Sstevel@tonic-gate 					    (void *)va, pfn);
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 				pp->p_next = bootpages;
15213446Smrj 				pp->p_prev = NULL;
15223446Smrj 				PP_SETBOOTPAGES(pp);
15233446Smrj 				if (bootpages != NULL) {
15243446Smrj 					bootpages->p_prev = pp;
15253446Smrj 				}
15260Sstevel@tonic-gate 				bootpages = pp;
15270Sstevel@tonic-gate 				++boot_protect_cnt;
15280Sstevel@tonic-gate 			}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 			++pfn;
15310Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
15320Sstevel@tonic-gate 			va += MMU_PAGESIZE;
15330Sstevel@tonic-gate 		}
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate 
15383446Smrj /*
15394828Sjosephb  *
15403446Smrj  */
15410Sstevel@tonic-gate static void
15424828Sjosephb layout_kernel_va(void)
15430Sstevel@tonic-gate {
15444828Sjosephb 	PRM_POINT("layout_kernel_va() starting...");
15450Sstevel@tonic-gate 	/*
15463446Smrj 	 * Establish the final size of the kernel's heap, size of segmap,
15473446Smrj 	 * segkp, etc.
15480Sstevel@tonic-gate 	 */
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate #if defined(__amd64)
15510Sstevel@tonic-gate 
15524828Sjosephb 	kpm_vbase = (caddr_t)segkpm_base;
15534828Sjosephb 	kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
15544828Sjosephb 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
15554828Sjosephb 		panic("not enough room for kpm!");
15563446Smrj 	PRM_DEBUG(kpm_size);
15573446Smrj 	PRM_DEBUG(kpm_vbase);
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/*
15603446Smrj 	 * By default we create a seg_kp in 64 bit kernels, it's a little
15613446Smrj 	 * faster to access than embedding it in the heap.
15620Sstevel@tonic-gate 	 */
15633446Smrj 	segkp_base = (caddr_t)valloc_base + valloc_sz;
15640Sstevel@tonic-gate 	if (!segkp_fromheap) {
15650Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 		/*
15683446Smrj 		 * determine size of segkp
15690Sstevel@tonic-gate 		 */
15700Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
15710Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
15720Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
15730Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
15740Sstevel@tonic-gate 			    mmu_btop(sz));
15750Sstevel@tonic-gate 		}
15760Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
15790Sstevel@tonic-gate 	}
15803446Smrj 	PRM_DEBUG(segkp_base);
15813446Smrj 	PRM_DEBUG(segkpsize);
15820Sstevel@tonic-gate 
15834828Sjosephb 	/*
15844828Sjosephb 	 * segzio is used for ZFS cached data. It uses a distinct VA
15854828Sjosephb 	 * segment (from kernel heap) so that we can easily tell not to
15864828Sjosephb 	 * include it in kernel crash dumps on 64 bit kernels. The trick is
15874828Sjosephb 	 * to give it lots of VA, but not constrain the kernel heap.
15884828Sjosephb 	 * We scale the size of segzio linearly with physmem up to
15894828Sjosephb 	 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem.
15904828Sjosephb 	 */
15913446Smrj 	segzio_base = segkp_base + mmu_ptob(segkpsize);
15923446Smrj 	if (segzio_fromheap) {
15933446Smrj 		segziosize = 0;
15943446Smrj 	} else {
15954828Sjosephb 		size_t physmem_size = mmu_ptob(physmem);
15964828Sjosephb 		size_t size = (segziosize == 0) ?
15974828Sjosephb 		    physmem_size : mmu_ptob(segziosize);
15983290Sjohansen 
15994828Sjosephb 		if (size < SEGZIOMINSIZE)
16003290Sjohansen 			size = SEGZIOMINSIZE;
16014828Sjosephb 		if (size > SEGZIOMAXSIZE) {
16023552Sjohansen 			size = SEGZIOMAXSIZE;
16034828Sjosephb 			if (physmem_size > size)
16044828Sjosephb 				size += (physmem_size - size) / 2;
16053290Sjohansen 		}
16063290Sjohansen 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
16073290Sjohansen 	}
16083446Smrj 	PRM_DEBUG(segziosize);
16093446Smrj 	PRM_DEBUG(segzio_base);
16103290Sjohansen 
16110Sstevel@tonic-gate 	/*
16123446Smrj 	 * Put the range of VA for device mappings next, kmdb knows to not
16133446Smrj 	 * grep in this range of addresses.
16140Sstevel@tonic-gate 	 */
16153446Smrj 	toxic_addr =
16163446Smrj 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
16170Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
16183446Smrj 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
16193446Smrj #else /* __i386 */
16203446Smrj 	segmap_start = ROUND_UP_LPAGE(kernelbase);
16213446Smrj #endif /* __i386 */
16223446Smrj 	PRM_DEBUG(segmap_start);
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	/*
16250Sstevel@tonic-gate 	 * Users can change segmapsize through eeprom or /etc/system.
16260Sstevel@tonic-gate 	 * If the variable is tuned through eeprom, there is no upper
16270Sstevel@tonic-gate 	 * bound on the size of segmap.  If it is tuned through
16280Sstevel@tonic-gate 	 * /etc/system on 32-bit systems, it must be no larger than we
16290Sstevel@tonic-gate 	 * planned for in startup_memlist().
16300Sstevel@tonic-gate 	 */
16310Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate #if defined(__i386)
16340Sstevel@tonic-gate 	/*
16350Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
16360Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
16373446Smrj 	 * small red zone just below the start of segmap.
16380Sstevel@tonic-gate 	 */
16393446Smrj 	segmap_start += KERNEL_REDZONE_SIZE;
16400Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
16410Sstevel@tonic-gate #endif
16423446Smrj 
16433446Smrj 	PRM_DEBUG(segmap_start);
16443446Smrj 	PRM_DEBUG(segmapsize);
16454828Sjosephb 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
16464828Sjosephb 	PRM_DEBUG(kernelheap);
16474828Sjosephb 	PRM_POINT("layout_kernel_va() done...");
16484828Sjosephb }
16494828Sjosephb 
16504828Sjosephb /*
16514828Sjosephb  * Finish initializing the VM system, now that we are no longer
16524828Sjosephb  * relying on the boot time memory allocators.
16534828Sjosephb  */
16544828Sjosephb static void
16554828Sjosephb startup_vm(void)
16564828Sjosephb {
16574828Sjosephb 	struct segmap_crargs a;
16584828Sjosephb 
16594828Sjosephb 	extern int use_brk_lpg, use_stk_lpg;
16604828Sjosephb 
16614828Sjosephb 	PRM_POINT("startup_vm() starting...");
16624828Sjosephb 
16634828Sjosephb 	/*
16644828Sjosephb 	 * Initialize the hat layer.
16654828Sjosephb 	 */
16664828Sjosephb 	hat_init();
16673446Smrj 
16683446Smrj 	/*
16693446Smrj 	 * Do final allocations of HAT data structures that need to
16703446Smrj 	 * be allocated before quiescing the boot loader.
16713446Smrj 	 */
16723446Smrj 	PRM_POINT("Calling hat_kern_alloc()...");
16733446Smrj 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
16743446Smrj 	PRM_POINT("hat_kern_alloc() done");
16753446Smrj 
16765084Sjohnlev #ifndef __xpv
16773446Smrj 	/*
16785159Sjohnlev 	 * Setup Page Attribute Table
16793446Smrj 	 */
16805159Sjohnlev 	pat_sync();
16815084Sjohnlev #endif
16823446Smrj 
16833446Smrj 	/*
16843446Smrj 	 * The next two loops are done in distinct steps in order
16853446Smrj 	 * to be sure that any page that is doubly mapped (both above
16863446Smrj 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
16873446Smrj 	 * Note this may never happen, but it might someday.
16883446Smrj 	 */
16893446Smrj 	bootpages = NULL;
16903446Smrj 	PRM_POINT("Protecting boot pages");
16910Sstevel@tonic-gate 
16923446Smrj 	/*
16933446Smrj 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
16943446Smrj 	 * page_t's. This can only happen if something weird allocated
16953446Smrj 	 * in this range (like kadb/kmdb).
16963446Smrj 	 */
16973446Smrj 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
16983446Smrj 
16993446Smrj 	/*
17003446Smrj 	 * Before we can take over memory allocation/mapping from the boot
17013446Smrj 	 * loader we must remove from our free page lists any boot allocated
17023446Smrj 	 * pages that stay mapped until release_bootstrap().
17033446Smrj 	 */
17043446Smrj 	protect_boot_range(0, kernelbase, 1);
17053446Smrj 
17065084Sjohnlev 
17073446Smrj 	/*
17083446Smrj 	 * Switch to running on regular HAT (not boot_mmu)
17093446Smrj 	 */
17103446Smrj 	PRM_POINT("Calling hat_kern_setup()...");
17113446Smrj 	hat_kern_setup();
17123446Smrj 
17133446Smrj 	/*
17143446Smrj 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
17153446Smrj 	 */
17163446Smrj 	bop_no_more_mem();
17173446Smrj 
17183446Smrj 	PRM_POINT("hat_kern_setup() done");
17193446Smrj 
17203446Smrj 	hat_cpu_online(CPU);
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 	/*
17230Sstevel@tonic-gate 	 * Initialize VM system
17240Sstevel@tonic-gate 	 */
17250Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
17260Sstevel@tonic-gate 	kvm_init();
17270Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	/*
17300Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
17310Sstevel@tonic-gate 	 */
17320Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
17330Sstevel@tonic-gate 		kdi_dvec_vmready();
17340Sstevel@tonic-gate 
17355084Sjohnlev #if defined(__xpv)
17365084Sjohnlev 	/*
17375084Sjohnlev 	 * Populate the I/O pool on domain 0
17385084Sjohnlev 	 */
17395084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
17405084Sjohnlev 		extern long populate_io_pool(void);
17415084Sjohnlev 		long init_io_pool_cnt;
17425084Sjohnlev 
17435084Sjohnlev 		PRM_POINT("Populating reserve I/O page pool");
17445084Sjohnlev 		init_io_pool_cnt = populate_io_pool();
17455084Sjohnlev 		PRM_DEBUG(init_io_pool_cnt);
17465084Sjohnlev 	}
17475084Sjohnlev #endif
17480Sstevel@tonic-gate 	/*
17490Sstevel@tonic-gate 	 * Mangle the brand string etc.
17500Sstevel@tonic-gate 	 */
17510Sstevel@tonic-gate 	cpuid_pass3(CPU);
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate #if defined(__amd64)
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	/*
17560Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
17570Sstevel@tonic-gate 	 */
17580Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
17590Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate #else	/* __i386 */
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	/*
17640Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
17650Sstevel@tonic-gate 	 */
17663446Smrj 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
17670Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
17680Sstevel@tonic-gate 	toxic_bit_map =
17690Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
17700Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
17710Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate #endif	/* __i386 */
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	/*
17770Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
17780Sstevel@tonic-gate 	 * it, tell the debugger.
17790Sstevel@tonic-gate 	 */
17800Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
17810Sstevel@tonic-gate 		kdi_dvec_memavail();
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	/*
17840Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
17850Sstevel@tonic-gate 	 * to work around a pentium bug.
17860Sstevel@tonic-gate 	 */
17875084Sjohnlev #if !defined(__amd64) && !defined(__xpv)
17880Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
17893446Smrj 		desctbr_t idtr;
17900Sstevel@tonic-gate 		gate_desc_t *newidt;
17913446Smrj 		struct machcpu *mcpu = &CPU->cpu_m;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
17940Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 		bcopy(idt0, newidt, sizeof (idt0));
17970Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
17985084Sjohnlev 		    KCS_SEL, SDT_SYSIGT, TRP_KPL);
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
18010Sstevel@tonic-gate 		    PROT_READ|PROT_EXEC);
18020Sstevel@tonic-gate 
18033446Smrj 		mcpu->mcpu_idt = newidt;
18043446Smrj 		idtr.dtr_base = (uintptr_t)mcpu->mcpu_idt;
18053446Smrj 		idtr.dtr_limit = sizeof (idt0) - 1;
18063446Smrj 		wr_idtr(&idtr);
18070Sstevel@tonic-gate 	}
18080Sstevel@tonic-gate #endif	/* !__amd64 */
18090Sstevel@tonic-gate 
18105084Sjohnlev #if !defined(__xpv)
18110Sstevel@tonic-gate 	/*
18120Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
18130Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
18140Sstevel@tonic-gate 	 */
18150Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
18160Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
18170Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
18185084Sjohnlev #endif
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
18210Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
18220Sstevel@tonic-gate 
1823841Skchow 	/*
1824841Skchow 	 * disable automatic large pages for small memory systems or
1825841Skchow 	 * when the disable flag is set.
1826841Skchow 	 */
18272991Ssusans 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
18282991Ssusans 		max_uheap_lpsize = LEVEL_SIZE(1);
18292991Ssusans 		max_ustack_lpsize = LEVEL_SIZE(1);
18302991Ssusans 		max_privmap_lpsize = LEVEL_SIZE(1);
18312991Ssusans 		max_uidata_lpsize = LEVEL_SIZE(1);
18322991Ssusans 		max_utext_lpsize = LEVEL_SIZE(1);
18332991Ssusans 		max_shm_lpsize = LEVEL_SIZE(1);
18342991Ssusans 	}
18352991Ssusans 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
18362991Ssusans 	    auto_lpg_disable) {
1837423Sdavemq 		use_brk_lpg = 0;
1838423Sdavemq 		use_stk_lpg = 0;
18392991Ssusans 	}
18402991Ssusans 	if (mmu.max_page_level > 0) {
18412991Ssusans 		mcntl0_lpsize = LEVEL_SIZE(1);
1842423Sdavemq 	}
1843423Sdavemq 
18440Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
18450Sstevel@tonic-gate 	hat_init_finish();
18460Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	/*
18490Sstevel@tonic-gate 	 * Initialize the segkp segment type.
18500Sstevel@tonic-gate 	 */
18510Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
18523446Smrj 	PRM_POINT("Attaching segkp");
18533446Smrj 	if (segkp_fromheap) {
18540Sstevel@tonic-gate 		segkp->s_as = &kas;
18553446Smrj 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
18563446Smrj 	    segkp) < 0) {
18573446Smrj 		panic("startup: cannot attach segkp");
18583446Smrj 		/*NOTREACHED*/
18590Sstevel@tonic-gate 	}
18603446Smrj 	PRM_POINT("Doing segkp_create()");
18610Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
18620Sstevel@tonic-gate 		panic("startup: segkp_create failed");
18630Sstevel@tonic-gate 		/*NOTREACHED*/
18640Sstevel@tonic-gate 	}
18650Sstevel@tonic-gate 	PRM_DEBUG(segkp);
18660Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 	/*
18690Sstevel@tonic-gate 	 * kpm segment
18700Sstevel@tonic-gate 	 */
18710Sstevel@tonic-gate 	segmap_kpm = 0;
18720Sstevel@tonic-gate 	if (kpm_desired) {
18730Sstevel@tonic-gate 		kpm_init();
18740Sstevel@tonic-gate 		kpm_enable = 1;
18751841Spraks 		vpm_enable = 1;
18760Sstevel@tonic-gate 	}
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	/*
18790Sstevel@tonic-gate 	 * Now create segmap segment.
18800Sstevel@tonic-gate 	 */
18810Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
18823446Smrj 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
18833446Smrj 		panic("cannot attach segmap");
18840Sstevel@tonic-gate 		/*NOTREACHED*/
18850Sstevel@tonic-gate 	}
18863446Smrj 	PRM_DEBUG(segmap);
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
18890Sstevel@tonic-gate 	a.shmsize = 0;
18900Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
18910Sstevel@tonic-gate 
18923446Smrj 	if (segmap_create(segmap, (caddr_t)&a) != 0)
18933446Smrj 		panic("segmap_create segmap");
18940Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	segdev_init();
18995084Sjohnlev #if defined(__xpv)
19005084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
19015084Sjohnlev #endif
19025084Sjohnlev 		pmem_init();
19033446Smrj 
19040Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
19050Sstevel@tonic-gate }
19060Sstevel@tonic-gate 
19073446Smrj /*
19083446Smrj  * Load a tod module for the non-standard tod part found on this system.
19093446Smrj  */
19103446Smrj static void
19113446Smrj load_tod_module(char *todmod)
19123446Smrj {
19133446Smrj 	if (modload("tod", todmod) == -1)
19143446Smrj 		halt("Can't load TOD module");
19153446Smrj }
19163446Smrj 
19170Sstevel@tonic-gate static void
19180Sstevel@tonic-gate startup_end(void)
19190Sstevel@tonic-gate {
19205107Seota 	int i;
19210Sstevel@tonic-gate 	extern void setx86isalist(void);
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	/*
19260Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
19270Sstevel@tonic-gate 	 * initialization has been done but before the clock
19280Sstevel@tonic-gate 	 * and other devices get started.
19290Sstevel@tonic-gate 	 */
19300Sstevel@tonic-gate 	kern_setup1();
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	/*
19330Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
19340Sstevel@tonic-gate 	 */
19350Sstevel@tonic-gate 	kcpc_hw_init(CPU);
19360Sstevel@tonic-gate 
19371582Skchow #if defined(OPTERON_WORKAROUND_6323525)
19381582Skchow 	if (opteron_workaround_6323525)
19391582Skchow 		patch_workaround_6323525();
19401582Skchow #endif
19410Sstevel@tonic-gate 	/*
19423446Smrj 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
19433446Smrj 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
19443446Smrj 	 */
19453446Smrj 	if (tod_module_name != NULL) {
19463446Smrj 		PRM_POINT("load_tod_module()");
19473446Smrj 		load_tod_module(tod_module_name);
19483446Smrj 	}
19493446Smrj 
19505084Sjohnlev #if defined(__xpv)
19515084Sjohnlev 	/*
19525084Sjohnlev 	 * Forceload interposing TOD module for the hypervisor.
19535084Sjohnlev 	 */
19545084Sjohnlev 	PRM_POINT("load_tod_module()");
19555084Sjohnlev 	load_tod_module("xpvtod");
19565084Sjohnlev #endif
19575084Sjohnlev 
19583446Smrj 	/*
19590Sstevel@tonic-gate 	 * Configure the system.
19600Sstevel@tonic-gate 	 */
19610Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
19620Sstevel@tonic-gate 	configure();		/* set up devices */
19630Sstevel@tonic-gate 	PRM_POINT("configure() done");
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	/*
19660Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
19670Sstevel@tonic-gate 	 * support.
19680Sstevel@tonic-gate 	 */
19690Sstevel@tonic-gate 	setx86isalist();
19701455Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
19710Sstevel@tonic-gate 	psm_install();
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	/*
19740Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
19750Sstevel@tonic-gate 	 * we're still using bootsvcs.
19760Sstevel@tonic-gate 	 */
19773446Smrj 	PRM_POINT("NULLing out bootops");
19783446Smrj 	*bootopsp = (struct bootops *)NULL;
19790Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
19800Sstevel@tonic-gate 
19815084Sjohnlev #if defined(__xpv)
19825084Sjohnlev 	ec_init_debug_irq();
19835084Sjohnlev 	xs_domu_init();
19845084Sjohnlev #endif
19850Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
19860Sstevel@tonic-gate 	(*picinitf)();
19870Sstevel@tonic-gate 	sti();
19885084Sjohnlev #if defined(__xpv)
19895084Sjohnlev 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
19905084Sjohnlev 	xen_late_startup();
19915084Sjohnlev #endif
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
19944828Sjosephb 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
19950Sstevel@tonic-gate 
19965107Seota 	/*
19975107Seota 	 * Register these software interrupts for ddi timer.
19985107Seota 	 * Software interrupts up to the level 10 are supported.
19995107Seota 	 */
20005107Seota 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
20015107Seota 		char name[sizeof ("timer_softintr") + 2];
20025107Seota 		(void) sprintf(name, "timer_softintr%02d", i);
20035107Seota 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
20045107Seota 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
20055107Seota 	}
20065107Seota 
20070Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
20080Sstevel@tonic-gate }
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate extern char hw_serial[];
20110Sstevel@tonic-gate char *_hs1107 = hw_serial;
20120Sstevel@tonic-gate ulong_t  _bdhs34;
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate void
20150Sstevel@tonic-gate post_startup(void)
20160Sstevel@tonic-gate {
20170Sstevel@tonic-gate 	/*
20180Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
20190Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
20200Sstevel@tonic-gate 	 * instruction set extensions.
20210Sstevel@tonic-gate 	 */
20220Sstevel@tonic-gate 	bind_hwcap();
20230Sstevel@tonic-gate 
20245084Sjohnlev #ifdef __xpv
20255084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
20265084Sjohnlev #endif
20275084Sjohnlev 	{
20285084Sjohnlev 		/*
20295084Sjohnlev 		 * Load the System Management BIOS into the global ksmbios
20305084Sjohnlev 		 * handle, if an SMBIOS is present on this system.
20315084Sjohnlev 		 */
20325084Sjohnlev 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
20335084Sjohnlev 
20345084Sjohnlev #if defined(__xpv)
20355084Sjohnlev 		xpv_panic_init();
20365084Sjohnlev #else
20375084Sjohnlev 		/*
20385084Sjohnlev 		 * Startup the memory scrubber.
20395084Sjohnlev 		 * XXPV	This should be running somewhere ..
20405084Sjohnlev 		 */
20415084Sjohnlev 		memscrub_init();
20425084Sjohnlev #endif
20435084Sjohnlev 	}
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	/*
20461414Scindi 	 * Complete CPU module initialization
20471414Scindi 	 */
20485254Sgavinm 	cmi_post_startup();
20491414Scindi 
20501414Scindi 	/*
20510Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
20520Sstevel@tonic-gate 	 */
20530Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	/*
20560Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
20570Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
20580Sstevel@tonic-gate 	 */
20590Sstevel@tonic-gate 	(void) modload("fs", "procfs");
20600Sstevel@tonic-gate 
20615129Smarx 	(void) i_ddi_attach_hw_nodes("pit_beep");
20625129Smarx 
20630Sstevel@tonic-gate #if defined(__i386)
20640Sstevel@tonic-gate 	/*
20650Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
20660Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
20670Sstevel@tonic-gate 	 */
20680Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
20690Sstevel@tonic-gate 		halt("No working FP hardware found");
20700Sstevel@tonic-gate #endif
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	maxmem = freemem;
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
20750Sstevel@tonic-gate }
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate static int
20780Sstevel@tonic-gate pp_in_ramdisk(page_t *pp)
20790Sstevel@tonic-gate {
20800Sstevel@tonic-gate 	extern uint64_t ramdisk_start, ramdisk_end;
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	return ((pp->p_pagenum >= btop(ramdisk_start)) &&
20830Sstevel@tonic-gate 	    (pp->p_pagenum < btopr(ramdisk_end)));
20840Sstevel@tonic-gate }
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate void
20870Sstevel@tonic-gate release_bootstrap(void)
20880Sstevel@tonic-gate {
20890Sstevel@tonic-gate 	int root_is_ramdisk;
20900Sstevel@tonic-gate 	page_t *pp;
20910Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
20920Sstevel@tonic-gate 	extern dev_t rootdev;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
20950Sstevel@tonic-gate 	kobj_boot_unmountroot();
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	/*
20980Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
20990Sstevel@tonic-gate 	 */
21000Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
21013446Smrj 	clear_boot_mappings(0, _userlimit);
21023446Smrj 	postbootkernelbase = kernelbase;
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	/*
21050Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
21060Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
21070Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
21080Sstevel@tonic-gate 	 */
21090Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
21100Sstevel@tonic-gate 	if (!root_is_ramdisk) {
21110Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
21120Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
21130Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
21140Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
21150Sstevel@tonic-gate 	}
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
21180Sstevel@tonic-gate 	while (bootpages) {
21190Sstevel@tonic-gate 		pp = bootpages;
21200Sstevel@tonic-gate 		bootpages = pp->p_next;
21210Sstevel@tonic-gate 		if (root_is_ramdisk && pp_in_ramdisk(pp)) {
21220Sstevel@tonic-gate 			pp->p_next = rd_pages;
21230Sstevel@tonic-gate 			rd_pages = pp;
21240Sstevel@tonic-gate 			continue;
21250Sstevel@tonic-gate 		}
21260Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
21273446Smrj 		pp->p_prev = (struct page *)0;
21283446Smrj 		PP_CLRBOOTPAGES(pp);
21290Sstevel@tonic-gate 		page_free(pp, 1);
21300Sstevel@tonic-gate 	}
21313446Smrj 	PRM_POINT("Boot pages released");
21320Sstevel@tonic-gate 
21335084Sjohnlev #if !defined(__xpv)
21345084Sjohnlev /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
21350Sstevel@tonic-gate 	/*
21360Sstevel@tonic-gate 	 * Find 1 page below 1 MB so that other processors can boot up.
21370Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
21380Sstevel@tonic-gate 	 * We should have just free'd one up.
21390Sstevel@tonic-gate 	 */
21400Sstevel@tonic-gate 	if (use_mp) {
21413446Smrj 		pfn_t pfn;
21423446Smrj 
21430Sstevel@tonic-gate 		for (pfn = 1; pfn < btop(1*1024*1024); pfn++) {
21440Sstevel@tonic-gate 			if (page_numtopp_alloc(pfn) == NULL)
21450Sstevel@tonic-gate 				continue;
21460Sstevel@tonic-gate 			rm_platter_va = i86devmap(pfn, 1,
21470Sstevel@tonic-gate 			    PROT_READ | PROT_WRITE | PROT_EXEC);
21480Sstevel@tonic-gate 			rm_platter_pa = ptob(pfn);
21490Sstevel@tonic-gate 			hat_devload(kas.a_hat,
21500Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
21510Sstevel@tonic-gate 			    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
21520Sstevel@tonic-gate 			    HAT_LOAD_NOCONSIST);
21530Sstevel@tonic-gate 			break;
21540Sstevel@tonic-gate 		}
21550Sstevel@tonic-gate 		if (pfn == btop(1*1024*1024))
21560Sstevel@tonic-gate 			panic("No page available for starting "
21570Sstevel@tonic-gate 			    "other processors");
21580Sstevel@tonic-gate 	}
21595084Sjohnlev #endif	/* !__xpv */
21600Sstevel@tonic-gate }
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate /*
21630Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
21640Sstevel@tonic-gate  */
21650Sstevel@tonic-gate void
21660Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
21670Sstevel@tonic-gate {
21680Sstevel@tonic-gate 	pp->p_pagenum = pnum;
21690Sstevel@tonic-gate 	pp->p_mapping = NULL;
21700Sstevel@tonic-gate 	pp->p_embed = 0;
21710Sstevel@tonic-gate 	pp->p_share = 0;
21720Sstevel@tonic-gate 	pp->p_mlentry = 0;
21730Sstevel@tonic-gate }
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate /*
21760Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
21770Sstevel@tonic-gate  */
21780Sstevel@tonic-gate static pgcnt_t
21790Sstevel@tonic-gate kphysm_init(
21803446Smrj 	page_t *pp,
21810Sstevel@tonic-gate 	pgcnt_t npages)
21820Sstevel@tonic-gate {
21830Sstevel@tonic-gate 	struct memlist	*pmem;
21840Sstevel@tonic-gate 	struct memseg	*cur_memseg;
21850Sstevel@tonic-gate 	pfn_t		base_pfn;
21860Sstevel@tonic-gate 	pgcnt_t		num;
21870Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
21880Sstevel@tonic-gate 	uint64_t	addr;
21890Sstevel@tonic-gate 	uint64_t	size;
21900Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
21930Sstevel@tonic-gate 
21943446Smrj 	cur_memseg = memseg_base;
21950Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
21960Sstevel@tonic-gate 		/*
21970Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
21980Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
21990Sstevel@tonic-gate 		 */
22000Sstevel@tonic-gate 		addr = pmem->address;
22010Sstevel@tonic-gate 		size = pmem->size;
22020Sstevel@tonic-gate 		if (btop(addr) > physmax)
22030Sstevel@tonic-gate 			continue;
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 		/*
22060Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
22070Sstevel@tonic-gate 		 */
22080Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
22090Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
22100Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
22110Sstevel@tonic-gate 			size -= addr - pmem->address;
22120Sstevel@tonic-gate 		}
22130Sstevel@tonic-gate 
2214810Skchow 		/* only process pages below or equal to physmax */
2215810Skchow 		if ((btop(addr + size) - 1) > physmax)
2216810Skchow 			size = ptob(physmax - btop(addr) + 1);
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 		num = btop(size);
22190Sstevel@tonic-gate 		if (num == 0)
22200Sstevel@tonic-gate 			continue;
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 		if (num > npages)
22230Sstevel@tonic-gate 			num = npages;
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 		npages -= num;
22260Sstevel@tonic-gate 		pages_done += num;
22270Sstevel@tonic-gate 		base_pfn = btop(addr);
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 		if (prom_debug)
22300Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
22310Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
22320Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 		/*
22353446Smrj 		 * Ignore pages below ddiphysmin to simplify ddi memory
22360Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
22370Sstevel@tonic-gate 		 */
22380Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
22393446Smrj 			if (base_pfn + num <= ddiphysmin)
22400Sstevel@tonic-gate 				continue;
22410Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
22420Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
22430Sstevel@tonic-gate 			base_pfn = ddiphysmin;
22440Sstevel@tonic-gate 		}
22453446Smrj 
22460Sstevel@tonic-gate 		/*
22470Sstevel@tonic-gate 		 * Build the memsegs entry
22480Sstevel@tonic-gate 		 */
22490Sstevel@tonic-gate 		cur_memseg->pages = pp;
22500Sstevel@tonic-gate 		cur_memseg->epages = pp + num;
22510Sstevel@tonic-gate 		cur_memseg->pages_base = base_pfn;
22520Sstevel@tonic-gate 		cur_memseg->pages_end = base_pfn + num;
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 		/*
22553446Smrj 		 * Insert into memseg list in decreasing pfn range order.
22560Sstevel@tonic-gate 		 * Low memory is typically more fragmented such that this
22570Sstevel@tonic-gate 		 * ordering keeps the larger ranges at the front of the list
22580Sstevel@tonic-gate 		 * for code that searches memseg.
22593446Smrj 		 * This ASSERTS that the memsegs coming in from boot are in
22603446Smrj 		 * increasing physical address order and not contiguous.
22610Sstevel@tonic-gate 		 */
22623446Smrj 		if (memsegs != NULL) {
22633446Smrj 			ASSERT(cur_memseg->pages_base >= memsegs->pages_end);
22643446Smrj 			cur_memseg->next = memsegs;
22650Sstevel@tonic-gate 		}
22663446Smrj 		memsegs = cur_memseg;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 		/*
22690Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
22700Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
22710Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
22720Sstevel@tonic-gate 		 * it initializes them.
22730Sstevel@tonic-gate 		 */
22740Sstevel@tonic-gate 		add_physmem(pp, num, base_pfn);
22750Sstevel@tonic-gate 		cur_memseg++;
22760Sstevel@tonic-gate 		availrmem_initial += num;
22770Sstevel@tonic-gate 		availrmem += num;
22780Sstevel@tonic-gate 
22793446Smrj 		pp += num;
22800Sstevel@tonic-gate 	}
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
22830Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
22840Sstevel@tonic-gate 	PRM_DEBUG(freemem);
22850Sstevel@tonic-gate 	build_pfn_hash();
22860Sstevel@tonic-gate 	return (pages_done);
22870Sstevel@tonic-gate }
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate /*
22900Sstevel@tonic-gate  * Kernel VM initialization.
22910Sstevel@tonic-gate  */
22920Sstevel@tonic-gate static void
22930Sstevel@tonic-gate kvm_init(void)
22940Sstevel@tonic-gate {
22950Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	/*
22980Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
22990Sstevel@tonic-gate 	 */
23000Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
23010Sstevel@tonic-gate 	as_avlinit(&kas);
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
23040Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
23070Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
23080Sstevel@tonic-gate 
23094828Sjosephb 	(void) seg_attach(&kas, kernelheap,
23104828Sjosephb 	    ekernelheap - kernelheap, &kvseg);
23110Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
23120Sstevel@tonic-gate 
23133446Smrj 	if (core_size > 0) {
23143446Smrj 		PRM_POINT("attaching kvseg_core");
23153446Smrj 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
23163446Smrj 		    &kvseg_core);
23173446Smrj 		(void) segkmem_create(&kvseg_core);
23183446Smrj 	}
23193290Sjohansen 
23203446Smrj 	if (segziosize > 0) {
23213446Smrj 		PRM_POINT("attaching segzio");
23223290Sjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
23233290Sjohansen 		    &kzioseg);
23243290Sjohansen 		(void) segkmem_zio_create(&kzioseg);
23253290Sjohansen 
23263290Sjohansen 		/* create zio area covering new segment */
23273290Sjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
23283290Sjohansen 	}
23290Sstevel@tonic-gate 
23303446Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
23310Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	/*
23360Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
23370Sstevel@tonic-gate 	 */
23383446Smrj 	PRM_POINT("protecting redzone");
23390Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	/*
23420Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
23430Sstevel@tonic-gate 	 */
23440Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
23450Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	/*
23480Sstevel@tonic-gate 	 * Make data writable until end.
23490Sstevel@tonic-gate 	 */
23500Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
23510Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
23520Sstevel@tonic-gate }
23530Sstevel@tonic-gate 
23545084Sjohnlev #ifndef __xpv
23550Sstevel@tonic-gate /*
23565159Sjohnlev  * Solaris adds an entry for Write Combining caching to the PAT
23570Sstevel@tonic-gate  */
23585159Sjohnlev static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate void
23615159Sjohnlev pat_sync(void)
23620Sstevel@tonic-gate {
23635159Sjohnlev 	ulong_t	cr0, cr0_orig, cr4;
23640Sstevel@tonic-gate 
23655159Sjohnlev 	if (!(x86_feature & X86_PAT))
23660Sstevel@tonic-gate 		return;
23675159Sjohnlev 	cr0_orig = cr0 = getcr0();
23685159Sjohnlev 	cr4 = getcr4();
23690Sstevel@tonic-gate 
23705159Sjohnlev 	/* disable caching and flush all caches and TLBs */
23715159Sjohnlev 	cr0 |= CR0_CD;
23725159Sjohnlev 	cr0 &= ~CR0_NW;
23735159Sjohnlev 	setcr0(cr0);
23745159Sjohnlev 	invalidate_cache();
23755159Sjohnlev 	if (cr4 & CR4_PGE) {
23765159Sjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
23775159Sjohnlev 		setcr4(cr4);
23785159Sjohnlev 	} else {
23795159Sjohnlev 		reload_cr3();
23800Sstevel@tonic-gate 	}
23810Sstevel@tonic-gate 
23825159Sjohnlev 	/* add our entry to the PAT */
23835159Sjohnlev 	wrmsr(REG_PAT, pat_attr_reg);
2384770Skucharsk 
23855159Sjohnlev 	/* flush TLBs and cache again, then reenable cr0 caching */
23865159Sjohnlev 	if (cr4 & CR4_PGE) {
23875159Sjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
23885159Sjohnlev 		setcr4(cr4);
23895159Sjohnlev 	} else {
23905159Sjohnlev 		reload_cr3();
23910Sstevel@tonic-gate 	}
23920Sstevel@tonic-gate 	invalidate_cache();
23930Sstevel@tonic-gate 	setcr0(cr0_orig);
23940Sstevel@tonic-gate }
23950Sstevel@tonic-gate 
23965159Sjohnlev #endif /* !__xpv */
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate void
23991676Sjpk get_system_configuration(void)
24000Sstevel@tonic-gate {
24010Sstevel@tonic-gate 	char	prop[32];
24020Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
24030Sstevel@tonic-gate 
24044828Sjosephb 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
24054828Sjosephb 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
24064828Sjosephb 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
24074828Sjosephb 	    nodes_ll > MAXNODES ||
24084828Sjosephb 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
24094828Sjosephb 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
24104828Sjosephb 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
24110Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
24120Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
24130Sstevel@tonic-gate 	} else {
24140Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
24150Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
24160Sstevel@tonic-gate 	}
24174828Sjosephb 
24184828Sjosephb 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
24194828Sjosephb 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
24204828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
24214828Sjosephb 		eprom_kernelbase = NULL;
24220Sstevel@tonic-gate 	else
24234828Sjosephb 		eprom_kernelbase = (uintptr_t)lvalue;
24240Sstevel@tonic-gate 
24254828Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
24264828Sjosephb 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
24274828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
24280Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
24294828Sjosephb 	else
24300Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
24310Sstevel@tonic-gate 
24324828Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
24334828Sjosephb 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
24344828Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
24350Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
24364828Sjosephb 	else
24370Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
24381417Skchow 
24394004Sjosephb 	/* physmem used to be here, but moved much earlier to fakebop.c */
24400Sstevel@tonic-gate }
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate /*
24430Sstevel@tonic-gate  * Add to a memory list.
24440Sstevel@tonic-gate  * start = start of new memory segment
24450Sstevel@tonic-gate  * len = length of new memory segment in bytes
24460Sstevel@tonic-gate  * new = pointer to a new struct memlist
24470Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
24480Sstevel@tonic-gate  */
24493446Smrj void
24500Sstevel@tonic-gate memlist_add(
24510Sstevel@tonic-gate 	uint64_t start,
24520Sstevel@tonic-gate 	uint64_t len,
24530Sstevel@tonic-gate 	struct memlist *new,
24540Sstevel@tonic-gate 	struct memlist **memlistp)
24550Sstevel@tonic-gate {
24560Sstevel@tonic-gate 	struct memlist *cur;
24570Sstevel@tonic-gate 	uint64_t end = start + len;
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 	new->address = start;
24600Sstevel@tonic-gate 	new->size = len;
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	cur = *memlistp;
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 	while (cur) {
24650Sstevel@tonic-gate 		if (cur->address >= end) {
24660Sstevel@tonic-gate 			new->next = cur;
24670Sstevel@tonic-gate 			*memlistp = new;
24680Sstevel@tonic-gate 			new->prev = cur->prev;
24690Sstevel@tonic-gate 			cur->prev = new;
24700Sstevel@tonic-gate 			return;
24710Sstevel@tonic-gate 		}
24720Sstevel@tonic-gate 		ASSERT(cur->address + cur->size <= start);
24730Sstevel@tonic-gate 		if (cur->next == NULL) {
24740Sstevel@tonic-gate 			cur->next = new;
24750Sstevel@tonic-gate 			new->prev = cur;
24760Sstevel@tonic-gate 			new->next = NULL;
24770Sstevel@tonic-gate 			return;
24780Sstevel@tonic-gate 		}
24790Sstevel@tonic-gate 		memlistp = &cur->next;
24800Sstevel@tonic-gate 		cur = cur->next;
24810Sstevel@tonic-gate 	}
24820Sstevel@tonic-gate }
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate void
24850Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
24860Sstevel@tonic-gate {
24870Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
24880Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
24910Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
24920Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
24930Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
24940Sstevel@tonic-gate }
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate caddr_t
24970Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
24980Sstevel@tonic-gate {
24990Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
25000Sstevel@tonic-gate }
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate /*ARGSUSED*/
25030Sstevel@tonic-gate caddr_t
25040Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
25050Sstevel@tonic-gate {
25060Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
25070Sstevel@tonic-gate 	/*NOTREACHED*/
25080Sstevel@tonic-gate 	return (0);
25090Sstevel@tonic-gate }
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate /*ARGSUSED*/
25120Sstevel@tonic-gate void
25130Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
25140Sstevel@tonic-gate {
25150Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
25160Sstevel@tonic-gate }
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate /*
25190Sstevel@tonic-gate  * This is called just after configure() in startup().
25200Sstevel@tonic-gate  *
25210Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
25220Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
25230Sstevel@tonic-gate  * given that various parts of the instruction set may appear
25240Sstevel@tonic-gate  * and disappear between different implementations.
25250Sstevel@tonic-gate  *
25260Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
25270Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
25280Sstevel@tonic-gate  * more flexibility.
25290Sstevel@tonic-gate  *
25300Sstevel@tonic-gate  * So, we just leave this alone.
25310Sstevel@tonic-gate  */
25320Sstevel@tonic-gate void
25330Sstevel@tonic-gate setx86isalist(void)
25340Sstevel@tonic-gate {
25350Sstevel@tonic-gate 	char *tp;
25360Sstevel@tonic-gate 	size_t len;
25370Sstevel@tonic-gate 	extern char *isa_list;
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate #define	TBUFSIZE	1024
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
25420Sstevel@tonic-gate 	*tp = '\0';
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate #if defined(__amd64)
25450Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
25460Sstevel@tonic-gate #endif
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 	switch (x86_vendor) {
25490Sstevel@tonic-gate 	case X86_VENDOR_Intel:
25500Sstevel@tonic-gate 	case X86_VENDOR_AMD:
25510Sstevel@tonic-gate 	case X86_VENDOR_TM:
25520Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
25530Sstevel@tonic-gate 			/*
25540Sstevel@tonic-gate 			 * Pentium Pro or later
25550Sstevel@tonic-gate 			 */
25560Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
25570Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
25580Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
25590Sstevel@tonic-gate 		}
25600Sstevel@tonic-gate 		/*FALLTHROUGH*/
25610Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
25620Sstevel@tonic-gate 		/*
25630Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
25640Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
25650Sstevel@tonic-gate 		 */
25660Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
25670Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
25680Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
25690Sstevel@tonic-gate 			    "+mmx pentium " : " ");
25700Sstevel@tonic-gate 		}
25710Sstevel@tonic-gate 		break;
25720Sstevel@tonic-gate 	default:
25730Sstevel@tonic-gate 		break;
25740Sstevel@tonic-gate 	}
25750Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
25760Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
25770Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
25780Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate #undef TBUFSIZE
25810Sstevel@tonic-gate }
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate #ifdef __amd64
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate void *
25870Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
25880Sstevel@tonic-gate {
25890Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
25900Sstevel@tonic-gate }
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate void
25930Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
25940Sstevel@tonic-gate {
25950Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
25960Sstevel@tonic-gate }
25970Sstevel@tonic-gate 
25983446Smrj #else /* __i386 */
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate void *
26010Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
26020Sstevel@tonic-gate {
26030Sstevel@tonic-gate 	caddr_t	vaddr;
26040Sstevel@tonic-gate 	uintptr_t v;
26050Sstevel@tonic-gate 	size_t	start;
26060Sstevel@tonic-gate 	size_t	end;
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
26090Sstevel@tonic-gate 	if (vaddr == NULL)
26100Sstevel@tonic-gate 		return (NULL);
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
26130Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
26143446Smrj 	ASSERT(v + size <= valloc_base);
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	start = btop(v - kernelbase);
26170Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
26180Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
26190Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 	while (start <= end) {
26220Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
26230Sstevel@tonic-gate 		++start;
26240Sstevel@tonic-gate 	}
26250Sstevel@tonic-gate 	return (vaddr);
26260Sstevel@tonic-gate }
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate void
26290Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
26300Sstevel@tonic-gate {
26310Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
26320Sstevel@tonic-gate 	size_t	start;
26330Sstevel@tonic-gate 	size_t	end;
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
26363446Smrj 	ASSERT(v + size <= valloc_base);
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	start = btop(v - kernelbase);
26390Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
26400Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
26410Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 	while (start <= end) {
26440Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
26450Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
26460Sstevel@tonic-gate 		++start;
26470Sstevel@tonic-gate 	}
26480Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
26490Sstevel@tonic-gate }
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate /*
26520Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
26530Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
26540Sstevel@tonic-gate  */
26550Sstevel@tonic-gate void *
26560Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
26570Sstevel@tonic-gate {
26580Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
26590Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
26600Sstevel@tonic-gate 	size_t start;
26610Sstevel@tonic-gate 	size_t end;
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 	/*
26640Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
26650Sstevel@tonic-gate 	 */
26660Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
26670Sstevel@tonic-gate 		return (NULL);
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate 	/*
26700Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
26710Sstevel@tonic-gate 	 */
26723446Smrj 	if (v >= valloc_base || eaddr < kernelbase)
26730Sstevel@tonic-gate 		return (NULL);
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	/*
26760Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
26770Sstevel@tonic-gate 	 */
26780Sstevel@tonic-gate 	if (v < kernelbase)
26790Sstevel@tonic-gate 		v = kernelbase;
26800Sstevel@tonic-gate 	start = btop(v - kernelbase);
26810Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
26820Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
26830Sstevel@tonic-gate 		end = toxic_bit_map_len;
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
26860Sstevel@tonic-gate 		return (NULL);
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 	v = kernelbase + ptob(start);
26890Sstevel@tonic-gate 	if (len != NULL)
26900Sstevel@tonic-gate 		*len = ptob(end - start);
26910Sstevel@tonic-gate 	return ((void *)v);
26920Sstevel@tonic-gate }
26930Sstevel@tonic-gate 
26943446Smrj #endif	/* __i386 */
2695