xref: /illumos-gate/usr/src/uts/i86pc/os/startup.c (revision 94f1124e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5100b72f4Sandrei  * Common Development and Distribution License (the "License").
6100b72f4Sandrei  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22a563a037Sbholler  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
307c478bd9Sstevel@tonic-gate #include <sys/signal.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/user.h>
337c478bd9Sstevel@tonic-gate #include <sys/mman.h>
347c478bd9Sstevel@tonic-gate #include <sys/vm.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
377c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
387c478bd9Sstevel@tonic-gate #include <sys/disp.h>
397c478bd9Sstevel@tonic-gate #include <sys/class.h>
407c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/proc.h>
457c478bd9Sstevel@tonic-gate #include <sys/buf.h>
467c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
47ae115bc7Smrj #include <sys/mem.h>
487c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/cred.h>
537c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
547c478bd9Sstevel@tonic-gate #include <sys/file.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
597c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
607c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
617c478bd9Sstevel@tonic-gate #include <sys/debug.h>
627c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
657c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
667c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
677c478bd9Sstevel@tonic-gate #include <sys/promif.h>
68843e1988Sjohnlev #include <sys/modctl.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
717c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
727c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
737c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
747c478bd9Sstevel@tonic-gate #include <sys/psw.h>
757c478bd9Sstevel@tonic-gate #include <sys/regset.h>
767c478bd9Sstevel@tonic-gate #include <sys/clock.h>
777c478bd9Sstevel@tonic-gate #include <sys/pte.h>
787c478bd9Sstevel@tonic-gate #include <sys/tss.h>
797c478bd9Sstevel@tonic-gate #include <sys/stack.h>
807c478bd9Sstevel@tonic-gate #include <sys/trap.h>
817c478bd9Sstevel@tonic-gate #include <sys/fp.h>
821d03c31eSjohnlev #include <vm/kboot_mmu.h>
837c478bd9Sstevel@tonic-gate #include <vm/anon.h>
847c478bd9Sstevel@tonic-gate #include <vm/as.h>
857c478bd9Sstevel@tonic-gate #include <vm/page.h>
867c478bd9Sstevel@tonic-gate #include <vm/seg.h>
877c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
887c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
897c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
907c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
917c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
927c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
937c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
947c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
957c478bd9Sstevel@tonic-gate #include <sys/thread.h>
967c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
977c478bd9Sstevel@tonic-gate #include <sys/vm_machparam.h>
987c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
997c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
1007c478bd9Sstevel@tonic-gate #include <vm/hat.h>
1017c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
1027c478bd9Sstevel@tonic-gate #include <sys/pmem.h>
1037c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1047c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
105a563a037Sbholler #include <sys/cpuvar.h>
1067c478bd9Sstevel@tonic-gate #include <sys/segments.h>
1077c478bd9Sstevel@tonic-gate #include <sys/clconf.h>
1087c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
1097c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
1107c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
1117c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
1127aec1d6eScindi #include <sys/cpu_module.h>
11384ab085aSmws #include <sys/smbios.h>
114ae115bc7Smrj #include <sys/debug_info.h>
1151d03c31eSjohnlev #include <sys/bootinfo.h>
116dd4eeefdSeota #include <sys/ddi_timer.h>
11719397407SSherry Moore #include <sys/multiboot.h>
118ae115bc7Smrj 
119843e1988Sjohnlev #ifdef __xpv
1201d03c31eSjohnlev 
121843e1988Sjohnlev #include <sys/hypervisor.h>
122843e1988Sjohnlev #include <sys/xen_mmu.h>
123843e1988Sjohnlev #include <sys/evtchn_impl.h>
124843e1988Sjohnlev #include <sys/gnttab.h>
125843e1988Sjohnlev #include <sys/xpv_panic.h>
126843e1988Sjohnlev #include <xen/sys/xenbus_comms.h>
127843e1988Sjohnlev #include <xen/public/physdev.h>
1281d03c31eSjohnlev 
129843e1988Sjohnlev extern void xen_late_startup(void);
130ae115bc7Smrj 
1311d03c31eSjohnlev struct xen_evt_data cpu0_evt_data;
1321d03c31eSjohnlev 
1331d03c31eSjohnlev #endif /* __xpv */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate extern void progressbar_init(void);
1367c478bd9Sstevel@tonic-gate extern void progressbar_start(void);
1379acbbeafSnn extern void brand_init(void);
13806fb6a36Sdv extern void pcf_init(void);
1397c478bd9Sstevel@tonic-gate 
140d7d93655Sblakej extern int size_pse_array(pgcnt_t, int);
141d7d93655Sblakej 
142ed5289f9SKen Erickson #if defined(_SOFT_HOSTID)
143ed5289f9SKen Erickson 
144ed5289f9SKen Erickson #include <sys/rtc.h>
145ed5289f9SKen Erickson 
146ed5289f9SKen Erickson static int32_t set_soft_hostid(void);
147ed5289f9SKen Erickson extern char hw_serial[];
148ed5289f9SKen Erickson static char hostid_file[] = "/etc/hostid";
149ed5289f9SKen Erickson 
150ed5289f9SKen Erickson #endif
151ed5289f9SKen Erickson 
152*94f1124eSVikram Hegde void *gfx_devinfo_list;
153*94f1124eSVikram Hegde int startup_amd_iommu_disable;
154*94f1124eSVikram Hegde char *startup_amd_iommu_disable_list;
155ed5289f9SKen Erickson 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1587c478bd9Sstevel@tonic-gate  * interface.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * segkp
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate extern int segkp_fromheap;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static void kvm_init(void);
1687c478bd9Sstevel@tonic-gate static void startup_init(void);
1697c478bd9Sstevel@tonic-gate static void startup_memlist(void);
170ae115bc7Smrj static void startup_kmem(void);
1717c478bd9Sstevel@tonic-gate static void startup_modules(void);
1727c478bd9Sstevel@tonic-gate static void startup_vm(void);
1737c478bd9Sstevel@tonic-gate static void startup_end(void);
17435b1ab99Sjosephb static void layout_kernel_va(void);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1787c478bd9Sstevel@tonic-gate  */
179f53ad214Skchow #ifdef __i386
18035b1ab99Sjosephb 
181f53ad214Skchow /*
182f53ad214Skchow  * Due to virtual address space limitations running in 32 bit mode, restrict
18335b1ab99Sjosephb  * the amount of physical memory configured to a max of PHYSMEM pages (16g).
184f53ad214Skchow  *
185f53ad214Skchow  * If the physical max memory size of 64g were allowed to be configured, the
186f53ad214Skchow  * size of user virtual address space will be less than 1g. A limited user
187f53ad214Skchow  * address space greatly reduces the range of applications that can run.
188f53ad214Skchow  *
18935b1ab99Sjosephb  * If more physical memory than PHYSMEM is required, users should preferably
19035b1ab99Sjosephb  * run in 64 bit mode which has far looser virtual address space limitations.
191f53ad214Skchow  *
192f53ad214Skchow  * If 64 bit mode is not available (as in IA32) and/or more physical memory
19335b1ab99Sjosephb  * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
194f53ad214Skchow  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
195f53ad214Skchow  * should also be carefully tuned to balance out the need of the user
196f53ad214Skchow  * application while minimizing the risk of kernel heap exhaustion due to
197f53ad214Skchow  * kernelbase being set too high.
198f53ad214Skchow  */
19935b1ab99Sjosephb #define	PHYSMEM	0x400000
200f53ad214Skchow 
20135b1ab99Sjosephb #else /* __amd64 */
20235b1ab99Sjosephb 
20335b1ab99Sjosephb /*
20435b1ab99Sjosephb  * For now we can handle memory with physical addresses up to about
20535b1ab99Sjosephb  * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
20635b1ab99Sjosephb  * half the VA space for seg_kpm. When systems get bigger than 64TB this
20735b1ab99Sjosephb  * code will need revisiting. There is an implicit assumption that there
20835b1ab99Sjosephb  * are no *huge* holes in the physical address space too.
20935b1ab99Sjosephb  */
21035b1ab99Sjosephb #define	TERABYTE		(1ul << 40)
21135b1ab99Sjosephb #define	PHYSMEM_MAX64		mmu_btop(64 * TERABYTE)
21235b1ab99Sjosephb #define	PHYSMEM			PHYSMEM_MAX64
21335b1ab99Sjosephb #define	AMD64_VA_HOLE_END	0xFFFF800000000000ul
21435b1ab99Sjosephb 
21535b1ab99Sjosephb #endif /* __amd64 */
21635b1ab99Sjosephb 
21735b1ab99Sjosephb pgcnt_t physmem = PHYSMEM;
2187c478bd9Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate char *kobj_file_buf;
2217c478bd9Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
22419397407SSherry Moore caddr_t	rm_platter_va = 0;
2257c478bd9Sstevel@tonic-gate uint32_t rm_platter_pa;
2267c478bd9Sstevel@tonic-gate 
22783f9b804Skchow int	auto_lpg_disable = 1;
22883f9b804Skchow 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate uintptr_t hole_start, hole_end;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * kpm mapping window
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate caddr_t kpm_vbase;
2387c478bd9Sstevel@tonic-gate size_t  kpm_size;
23935b1ab99Sjosephb static int kpm_desired;
24035b1ab99Sjosephb #ifdef __amd64
24135b1ab99Sjosephb static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
24235b1ab99Sjosephb #endif
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate  * Configuration parameters set at boot time.
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
2517c478bd9Sstevel@tonic-gate struct bootops		**bootopsp;
2527c478bd9Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate char bootblock_fstype[16];
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
25719397407SSherry Moore char kern_bootfile[OBP_MAXPATHLEN];
2587c478bd9Sstevel@tonic-gate 
259ad23a2dbSjohansen /*
260ad23a2dbSjohansen  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
261ad23a2dbSjohansen  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
262ad23a2dbSjohansen  * zio buffers from their own segment, otherwise they are allocated from the
263ad23a2dbSjohansen  * heap.  The optimization of allocating zio buffers from their own segment is
264ad23a2dbSjohansen  * only valid on 64-bit kernels.
265ad23a2dbSjohansen  */
266ad23a2dbSjohansen #if defined(__amd64)
267ad23a2dbSjohansen int segzio_fromheap = 0;
268ad23a2dbSjohansen #else
269ad23a2dbSjohansen int segzio_fromheap = 1;
270ad23a2dbSjohansen #endif
271ad23a2dbSjohansen 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
2747c478bd9Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2757c478bd9Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * VM data structures
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
2837c478bd9Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2847c478bd9Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
285d7d93655Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
286d7d93655Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
287d7d93655Sblakej int pse_shift;			/* log2(pse_table_size) */
2887c478bd9Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2897c478bd9Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
2907c478bd9Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
2917c478bd9Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
2927c478bd9Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
295ae115bc7Smrj static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
296ae115bc7Smrj 
2977c478bd9Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate #if defined(__amd64)
3007c478bd9Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
3017c478bd9Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
3027c478bd9Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
3037c478bd9Sstevel@tonic-gate #else
3047c478bd9Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
3057c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
308ad23a2dbSjohansen caddr_t segzio_base;		/* Base address of segzio */
3097c478bd9Sstevel@tonic-gate #if defined(__amd64)
3107c478bd9Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
3117c478bd9Sstevel@tonic-gate #else
3127c478bd9Sstevel@tonic-gate pgcnt_t segkpsize = 0;
3137c478bd9Sstevel@tonic-gate #endif
314ad23a2dbSjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
3157c478bd9Sstevel@tonic-gate 
316ae115bc7Smrj /*
317ae115bc7Smrj  * VA range available to the debugger
318ae115bc7Smrj  */
319ae115bc7Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
320ae115bc7Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
321ae115bc7Smrj 
3227c478bd9Sstevel@tonic-gate struct memseg *memseg_base;
3237c478bd9Sstevel@tonic-gate struct vnode unused_pages_vp;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate #define	FOURGB	0x100000000LL
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate struct memlist *memlist;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
3307c478bd9Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
3317c478bd9Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
3327c478bd9Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
3337c478bd9Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
3347c478bd9Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
3357c478bd9Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
3367c478bd9Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
3397c478bd9Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
3437c478bd9Sstevel@tonic-gate  */
344ae115bc7Smrj static pgcnt_t kphysm_init(page_t *, pgcnt_t);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * a couple useful roundup macros
3507c478bd9Sstevel@tonic-gate  */
3517c478bd9Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
3527c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
3537c478bd9Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
3547c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
3557c478bd9Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
356ae115bc7Smrj 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
3577c478bd9Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
3587c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
3627c478bd9Sstevel@tonic-gate  *		+-----------------------+
363ae115bc7Smrj  *		|			|
3647c478bd9Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
3657c478bd9Sstevel@tonic-gate  *		|	debugger	|
3667c478bd9Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
3677c478bd9Sstevel@tonic-gate  *		|      Kernel Data	|
3687c478bd9Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
3697c478bd9Sstevel@tonic-gate  *              |      Kernel Text	|
370843e1988Sjohnlev  * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
371ae115bc7Smrj  *		|---       GDT       ---|- GDT page (GDT_VA)
372ae115bc7Smrj  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
373ae115bc7Smrj  *		|			|
374ae115bc7Smrj  * 		|   page_t structures	|
3757c478bd9Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
3767c478bd9Sstevel@tonic-gate  * 		|   page hash, etc.	|
377ae115bc7Smrj  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
378ae115bc7Smrj  *		|			|  (segkp is just an arena in the heap)
3797c478bd9Sstevel@tonic-gate  *		|			|
3807c478bd9Sstevel@tonic-gate  *		|	kvseg		|
3817c478bd9Sstevel@tonic-gate  *		|			|
3827c478bd9Sstevel@tonic-gate  *		|			|
3837c478bd9Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
3847c478bd9Sstevel@tonic-gate  * 		|        Segkmap	|
385ae115bc7Smrj  * 0xC3002000  -|-----------------------|- segmap_start (floating)
3867c478bd9Sstevel@tonic-gate  *		|	Red Zone	|
3877c478bd9Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
3887c478bd9Sstevel@tonic-gate  *		|			|			||
3897c478bd9Sstevel@tonic-gate  *		|     Shared objects	|			\/
3907c478bd9Sstevel@tonic-gate  *		|			|
3917c478bd9Sstevel@tonic-gate  *		:			:
3927c478bd9Sstevel@tonic-gate  *		|	user data	|
3937c478bd9Sstevel@tonic-gate  *		|-----------------------|
3947c478bd9Sstevel@tonic-gate  *		|	user text	|
3957c478bd9Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
3967c478bd9Sstevel@tonic-gate  *		|	user stack	|
3977c478bd9Sstevel@tonic-gate  *		:			:
3987c478bd9Sstevel@tonic-gate  *		|	invalid		|
3997c478bd9Sstevel@tonic-gate  * 0x00000000	+-----------------------+
4007c478bd9Sstevel@tonic-gate  *
4017c478bd9Sstevel@tonic-gate  *
4027c478bd9Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
4037c478bd9Sstevel@tonic-gate  *			+-----------------------+
404ae115bc7Smrj  *			|			|
4057c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
4067c478bd9Sstevel@tonic-gate  *			|	debugger (?)	|
4077c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
4087c478bd9Sstevel@tonic-gate  *			|      unused    	|
4097c478bd9Sstevel@tonic-gate  *			+-----------------------+
4107c478bd9Sstevel@tonic-gate  *			|      Kernel Data	|
4117c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
4127c478bd9Sstevel@tonic-gate  *			|      Kernel Text	|
4137c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
414ae115bc7Smrj  *			|---       GDT       ---|- GDT page (GDT_VA)
415ae115bc7Smrj  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
416ae115bc7Smrj  *			|			|
4177c478bd9Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
4187c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
4197c478bd9Sstevel@tonic-gate  *			|	 Kernel		|
4207c478bd9Sstevel@tonic-gate  *			|	  heap		|
4217c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
422ae115bc7Smrj  *			|	 segmap		|
423ae115bc7Smrj  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
4247c478bd9Sstevel@tonic-gate  *			|    device mappings	|
4257c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
426ad23a2dbSjohansen  *			|	  segzio	|
427ad23a2dbSjohansen  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
4287c478bd9Sstevel@tonic-gate  *			|	  segkp		|
429ae115bc7Smrj  * ---                  |-----------------------|- segkp_base (floating)
430ae115bc7Smrj  * 			|   page_t structures	|  valloc_base + valloc_sz
431ae115bc7Smrj  * 			|   memsegs, memlists, 	|
432ae115bc7Smrj  * 			|   page hash, etc.	|
43335b1ab99Sjosephb  * 0xFFFFFF00.00000000  |-----------------------|- valloc_base (lower if > 1TB)
4347c478bd9Sstevel@tonic-gate  *			|	 segkpm		|
4357c478bd9Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
4367c478bd9Sstevel@tonic-gate  *			|	Red Zone	|
43735b1ab99Sjosephb  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE (lower if > 1TB)
4387c478bd9Sstevel@tonic-gate  *			|     User stack	|- User space memory
4397c478bd9Sstevel@tonic-gate  * 			|			|
4407c478bd9Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
4417c478bd9Sstevel@tonic-gate  *			:			:
4427c478bd9Sstevel@tonic-gate  * 			|			|
4437c478bd9Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
4447c478bd9Sstevel@tonic-gate  * 			|			|
4457c478bd9Sstevel@tonic-gate  * 			| VA Hole / unused	|
4467c478bd9Sstevel@tonic-gate  * 			|			|
4477c478bd9Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
4487c478bd9Sstevel@tonic-gate  *			|			|
4497c478bd9Sstevel@tonic-gate  *			|			|
4507c478bd9Sstevel@tonic-gate  *			:			:
4517c478bd9Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
4527c478bd9Sstevel@tonic-gate  *			|			|
4537c478bd9Sstevel@tonic-gate  *			|	user data	|
4547c478bd9Sstevel@tonic-gate  *			|-----------------------|
4557c478bd9Sstevel@tonic-gate  *			|	user text	|
4567c478bd9Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
4577c478bd9Sstevel@tonic-gate  *			|	invalid		|
4587c478bd9Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
4597c478bd9Sstevel@tonic-gate  *
4607c478bd9Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
4617c478bd9Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
4627c478bd9Sstevel@tonic-gate  *
4637c478bd9Sstevel@tonic-gate  * Floating values:
4647c478bd9Sstevel@tonic-gate  *
4657c478bd9Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
466ae115bc7Smrj  * structures.  This region contains page_t structures for
467ae115bc7Smrj  * physical memory, memsegs, memlists, and the page hash.
4687c478bd9Sstevel@tonic-gate  *
4697c478bd9Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
4707c478bd9Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
4717c478bd9Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
4727c478bd9Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
4737c478bd9Sstevel@tonic-gate  * accessed using rip-relative addressing
4747c478bd9Sstevel@tonic-gate  *
4757c478bd9Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
4767c478bd9Sstevel@tonic-gate  *
4777c478bd9Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
4787c478bd9Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
4797c478bd9Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
4807c478bd9Sstevel@tonic-gate  *
481ae115bc7Smrj  * segmap_start: start of segmap. The length of segmap can be modified
48252eef812SAmrita Sadhukhan  * through eeprom. The default length is 16MB on 32-bit systems and 64MB
48352eef812SAmrita Sadhukhan  * on 64-bit systems.
4847c478bd9Sstevel@tonic-gate  *
4857c478bd9Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
4867c478bd9Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
4877c478bd9Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
4887c478bd9Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
4897c478bd9Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
4907c478bd9Sstevel@tonic-gate  * segkp sizes).
4917c478bd9Sstevel@tonic-gate  *
4927c478bd9Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
4937c478bd9Sstevel@tonic-gate  *	size	page_t's			size
4947c478bd9Sstevel@tonic-gate  *	----	---------	----------	-----------
4957c478bd9Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
4967c478bd9Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
4977c478bd9Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
4987c478bd9Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
4997c478bd9Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
5007c478bd9Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
5017c478bd9Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
5027c478bd9Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
5037c478bd9Sstevel@tonic-gate  *
5047c478bd9Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
5057c478bd9Sstevel@tonic-gate  * configurations above 8gb.
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
5087c478bd9Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
5097c478bd9Sstevel@tonic-gate  */
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /* real-time-clock initialization parameters */
512ae115bc7Smrj extern time_t process_rtc_config_file(void);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate uintptr_t	kernelbase;
515ae115bc7Smrj uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
5167c478bd9Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
5177c478bd9Sstevel@tonic-gate size_t		segmapsize;
518ae115bc7Smrj uintptr_t	segmap_start;
5197c478bd9Sstevel@tonic-gate int		segmapfreelists;
5207c478bd9Sstevel@tonic-gate pgcnt_t		npages;
521ae115bc7Smrj pgcnt_t		orig_npages;
5227c478bd9Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
5237c478bd9Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate /*
5267c478bd9Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
5277c478bd9Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
5287c478bd9Sstevel@tonic-gate  * the bootstrap.
5297c478bd9Sstevel@tonic-gate  */
530ae115bc7Smrj static page_t *bootpages;
531ae115bc7Smrj 
532ae115bc7Smrj /*
533ae115bc7Smrj  * boot time pages that have a vnode from the ramdisk will keep that forever.
534ae115bc7Smrj  */
535ae115bc7Smrj static page_t *rd_pages;
5367c478bd9Sstevel@tonic-gate 
53719397407SSherry Moore /*
53819397407SSherry Moore  * Lower 64K
53919397407SSherry Moore  */
54019397407SSherry Moore static page_t *lower_pages = NULL;
54119397407SSherry Moore static int lower_pages_count = 0;
54219397407SSherry Moore 
5437c478bd9Sstevel@tonic-gate struct system_hardware system_hardware;
5447c478bd9Sstevel@tonic-gate 
545551bc2a6Smrj /*
546551bc2a6Smrj  * Is this Solaris instance running in a fully virtualized xVM domain?
547551bc2a6Smrj  */
548551bc2a6Smrj int xpv_is_hvm = 0;
549551bc2a6Smrj 
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
5527c478bd9Sstevel@tonic-gate  */
5537c478bd9Sstevel@tonic-gate static void
554ae115bc7Smrj print_memlist(char *title, struct memlist *mp)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
5577c478bd9Sstevel@tonic-gate 	while (mp != NULL)  {
5587c478bd9Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
5597c478bd9Sstevel@tonic-gate 		    mp->address, mp->size);
5607c478bd9Sstevel@tonic-gate 		mp = mp->next;
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
5667c478bd9Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate int	l2cache_sz = 0x80000;
5697c478bd9Sstevel@tonic-gate int	l2cache_linesz = 0x40;
5707c478bd9Sstevel@tonic-gate int	l2cache_assoc = 1;
5717c478bd9Sstevel@tonic-gate 
5722cb27123Saguzovsk static size_t	textrepl_min_gb = 10;
5732cb27123Saguzovsk 
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
5767c478bd9Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
5777c478bd9Sstevel@tonic-gate  */
5787c478bd9Sstevel@tonic-gate #ifdef __amd64
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate vmem_t		*device_arena;
5817c478bd9Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
582ae115bc7Smrj size_t		toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate #else	/* __i386 */
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
5877c478bd9Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate #endif	/* __i386 */
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate  * Simple boot time debug facilities
5937c478bd9Sstevel@tonic-gate  */
5947c478bd9Sstevel@tonic-gate static char *prm_dbg_str[] = {
5957c478bd9Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%x\n",
5967c478bd9Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%llx\n"
5977c478bd9Sstevel@tonic-gate };
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate int prom_debug;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate #define	PRM_DEBUG(q)	if (prom_debug) 	\
6027c478bd9Sstevel@tonic-gate 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
6037c478bd9Sstevel@tonic-gate #define	PRM_POINT(q)	if (prom_debug) 	\
6047c478bd9Sstevel@tonic-gate 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
6087c478bd9Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
6097c478bd9Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate #define	NUM_ALLOCATIONS 7
6127c478bd9Sstevel@tonic-gate int num_allocations = 0;
6137c478bd9Sstevel@tonic-gate struct {
6147c478bd9Sstevel@tonic-gate 	void **al_ptr;
6157c478bd9Sstevel@tonic-gate 	size_t al_size;
6167c478bd9Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
6177c478bd9Sstevel@tonic-gate size_t valloc_sz = 0;
6187c478bd9Sstevel@tonic-gate uintptr_t valloc_base;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
6217c478bd9Sstevel@tonic-gate 		size = ROUND_UP_PAGE(size);		 		\
6227c478bd9Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
6237c478bd9Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
6247c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
6257c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
6267c478bd9Sstevel@tonic-gate 		valloc_sz += size;					\
6277c478bd9Sstevel@tonic-gate 		++num_allocations;				 	\
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
630ae115bc7Smrj /*
631ae115bc7Smrj  * Allocate all the initial memory needed by the page allocator.
632ae115bc7Smrj  */
6337c478bd9Sstevel@tonic-gate static void
6347c478bd9Sstevel@tonic-gate perform_allocations(void)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	caddr_t mem;
6377c478bd9Sstevel@tonic-gate 	int i;
638ae115bc7Smrj 	int valloc_align;
6397c478bd9Sstevel@tonic-gate 
640ae115bc7Smrj 	PRM_DEBUG(valloc_base);
641ae115bc7Smrj 	PRM_DEBUG(valloc_sz);
642ae115bc7Smrj 	valloc_align = mmu.level_size[mmu.max_page_level > 0];
643ae115bc7Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
6447c478bd9Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
6457c478bd9Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
6467c478bd9Sstevel@tonic-gate 	bzero(mem, valloc_sz);
6477c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
6487c478bd9Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
6497c478bd9Sstevel@tonic-gate 		mem += allocations[i].al_size;
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate  * Our world looks like this at startup time.
6557c478bd9Sstevel@tonic-gate  *
6567c478bd9Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
6577c478bd9Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
6587c478bd9Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
6597c478bd9Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
6607c478bd9Sstevel@tonic-gate  *
6617c478bd9Sstevel@tonic-gate  * On the text page:
6627c478bd9Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
6637c478bd9Sstevel@tonic-gate  *
6647c478bd9Sstevel@tonic-gate  * On the data page:
665ae115bc7Smrj  * unix/genunix/krtld/module data loads.
666ae115bc7Smrj  *
6677c478bd9Sstevel@tonic-gate  * Machine-dependent startup code
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate void
6707c478bd9Sstevel@tonic-gate startup(void)
6717c478bd9Sstevel@tonic-gate {
672843e1988Sjohnlev #if !defined(__xpv)
67375bcd456Sjg 	extern void startup_bios_disk(void);
67475bcd456Sjg 	extern void startup_pci_bios(void);
67519397407SSherry Moore 	extern int post_fastreboot;
676843e1988Sjohnlev #endif
677a563a037Sbholler 	extern cpuset_t cpu_ready_set;
678a563a037Sbholler 
679843e1988Sjohnlev 	/*
680843e1988Sjohnlev 	 * Make sure that nobody tries to use sekpm until we have
681843e1988Sjohnlev 	 * initialized it properly.
682843e1988Sjohnlev 	 */
6837c478bd9Sstevel@tonic-gate #if defined(__amd64)
68435b1ab99Sjosephb 	kpm_desired = 1;
6857c478bd9Sstevel@tonic-gate #endif
6867c478bd9Sstevel@tonic-gate 	kpm_enable = 0;
687a563a037Sbholler 	CPUSET_ONLY(cpu_ready_set, 0);	/* cpu 0 is boot cpu */
6887c478bd9Sstevel@tonic-gate 
689843e1988Sjohnlev #if defined(__xpv)	/* XXPV fix me! */
690843e1988Sjohnlev 	{
691843e1988Sjohnlev 		extern int segvn_use_regions;
692843e1988Sjohnlev 		segvn_use_regions = 0;
693843e1988Sjohnlev 	}
694843e1988Sjohnlev #endif
6957c478bd9Sstevel@tonic-gate 	progressbar_init();
6967c478bd9Sstevel@tonic-gate 	startup_init();
697ab4a9bebSjohnlev #if defined(__xpv)
698ab4a9bebSjohnlev 	startup_xen_version();
699ab4a9bebSjohnlev #endif
7007c478bd9Sstevel@tonic-gate 	startup_memlist();
701ae115bc7Smrj 	startup_kmem();
70235b1ab99Sjosephb 	startup_vm();
703843e1988Sjohnlev #if !defined(__xpv)
70419397407SSherry Moore 	if (!post_fastreboot)
70519397407SSherry Moore 		startup_pci_bios();
706e4b86885SCheng Sean Ye #endif
707e4b86885SCheng Sean Ye #if defined(__xpv)
708e4b86885SCheng Sean Ye 	startup_xen_mca();
709843e1988Sjohnlev #endif
7107c478bd9Sstevel@tonic-gate 	startup_modules();
711843e1988Sjohnlev #if !defined(__xpv)
71219397407SSherry Moore 	if (!post_fastreboot)
71319397407SSherry Moore 		startup_bios_disk();
714843e1988Sjohnlev #endif
7157c478bd9Sstevel@tonic-gate 	startup_end();
7167c478bd9Sstevel@tonic-gate 	progressbar_start();
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate static void
7207c478bd9Sstevel@tonic-gate startup_init()
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	/*
7257c478bd9Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
7267c478bd9Sstevel@tonic-gate 	 */
7277c478bd9Sstevel@tonic-gate 	cpuid_pass2(CPU);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/*
7327c478bd9Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
7337c478bd9Sstevel@tonic-gate 	 */
7347c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
7357c478bd9Sstevel@tonic-gate 		++prom_debug;
7367c478bd9Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	/*
7407c478bd9Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
7417c478bd9Sstevel@tonic-gate 	 */
7427c478bd9Sstevel@tonic-gate 	get_system_configuration();
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/*
7457c478bd9Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
7467c478bd9Sstevel@tonic-gate 	 */
7477c478bd9Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
7487c478bd9Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
7497c478bd9Sstevel@tonic-gate 		    CPU->cpu_brandstr);
7507c478bd9Sstevel@tonic-gate 		halt("This processor is not supported by this release "
7517c478bd9Sstevel@tonic-gate 		    "of Solaris.");
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate /*
7587c478bd9Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
7597c478bd9Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
7607c478bd9Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
7617c478bd9Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
7627c478bd9Sstevel@tonic-gate  */
7637c478bd9Sstevel@tonic-gate static void
7647c478bd9Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	uintptr_t va;
7677c478bd9Sstevel@tonic-gate 	uintptr_t next_va;
7687c478bd9Sstevel@tonic-gate 	pfn_t pfn;
7697c478bd9Sstevel@tonic-gate 	uint64_t pfn_addr;
7707c478bd9Sstevel@tonic-gate 	uint64_t pfn_eaddr;
7717c478bd9Sstevel@tonic-gate 	uint_t prot;
7727c478bd9Sstevel@tonic-gate 	size_t len;
7737c478bd9Sstevel@tonic-gate 	uint_t change;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (prom_debug)
7767c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
7777c478bd9Sstevel@tonic-gate 		    *addr, *size);
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	/*
7807c478bd9Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
7817c478bd9Sstevel@tonic-gate 	 */
7827c478bd9Sstevel@tonic-gate 	if (*addr == 0) {
7837c478bd9Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
7847c478bd9Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	/*
788ae115bc7Smrj 	 * First we trim from the front of the range. Since kbm_probe()
7897c478bd9Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
7907c478bd9Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
7917c478bd9Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
7927c478bd9Sstevel@tonic-gate 	 * but w < v.
7937c478bd9Sstevel@tonic-gate 	 */
7947c478bd9Sstevel@tonic-gate 	do {
7957c478bd9Sstevel@tonic-gate 		change = 0;
7967c478bd9Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
797ae115bc7Smrj 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7987c478bd9Sstevel@tonic-gate 		    va = next_va) {
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 			next_va = va + len;
801ae115bc7Smrj 			pfn_addr = pfn_to_pa(pfn);
8027c478bd9Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
8057c478bd9Sstevel@tonic-gate 				change = 1;
8067c478bd9Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
8077c478bd9Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
8087c478bd9Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
8097c478bd9Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
8107c478bd9Sstevel@tonic-gate 				}
8117c478bd9Sstevel@tonic-gate 			}
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		if (change && prom_debug)
8147c478bd9Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
8157c478bd9Sstevel@tonic-gate 			    *addr, *size);
8167c478bd9Sstevel@tonic-gate 	} while (change);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	/*
8197c478bd9Sstevel@tonic-gate 	 * Trim pages from the end of the range.
8207c478bd9Sstevel@tonic-gate 	 */
8217c478bd9Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
822ae115bc7Smrj 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
8237c478bd9Sstevel@tonic-gate 	    va = next_va) {
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		next_va = va + len;
826ae115bc7Smrj 		pfn_addr = pfn_to_pa(pfn);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
8297c478bd9Sstevel@tonic-gate 			*size = pfn_addr - *addr;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (prom_debug)
8337c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
8347c478bd9Sstevel@tonic-gate 		    *addr, *size);
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate static void
8387c478bd9Sstevel@tonic-gate kpm_init()
8397c478bd9Sstevel@tonic-gate {
8407c478bd9Sstevel@tonic-gate 	struct segkpm_crargs b;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
8447c478bd9Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
8457c478bd9Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
8467c478bd9Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
8477c478bd9Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
8487c478bd9Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
8497c478bd9Sstevel@tonic-gate 	 * pagesize is used.
8507c478bd9Sstevel@tonic-gate 	 */
8517c478bd9Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
8527c478bd9Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
8537c478bd9Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
8547c478bd9Sstevel@tonic-gate 	kpmp2pshft = 0;
8557c478bd9Sstevel@tonic-gate 	kpmpnpgs = 1;
8567c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
8597c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
8627c478bd9Sstevel@tonic-gate 		panic("cannot attach segkpm");
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
8657c478bd9Sstevel@tonic-gate 	b.nvcolors = 1;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
8687c478bd9Sstevel@tonic-gate 		panic("segkpm_create segkpm");
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
871ae115bc7Smrj }
8727c478bd9Sstevel@tonic-gate 
873ae115bc7Smrj /*
874ae115bc7Smrj  * The debug info page provides enough information to allow external
875ae115bc7Smrj  * inspectors (e.g. when running under a hypervisor) to bootstrap
876ae115bc7Smrj  * themselves into allowing full-blown kernel debugging.
877ae115bc7Smrj  */
878ae115bc7Smrj static void
879ae115bc7Smrj init_debug_info(void)
880ae115bc7Smrj {
881ae115bc7Smrj 	caddr_t mem;
882ae115bc7Smrj 	debug_info_t *di;
883ae115bc7Smrj 
884ae115bc7Smrj #ifndef __lint
885ae115bc7Smrj 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
886ae115bc7Smrj #endif
887ae115bc7Smrj 
888ae115bc7Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
889ae115bc7Smrj 	    MMU_PAGESIZE);
890ae115bc7Smrj 
891ae115bc7Smrj 	if (mem != (caddr_t)DEBUG_INFO_VA)
892ae115bc7Smrj 		panic("BOP_ALLOC() failed");
893ae115bc7Smrj 	bzero(mem, MMU_PAGESIZE);
894ae115bc7Smrj 
895ae115bc7Smrj 	di = (debug_info_t *)mem;
896ae115bc7Smrj 
897ae115bc7Smrj 	di->di_magic = DEBUG_INFO_MAGIC;
898ae115bc7Smrj 	di->di_version = DEBUG_INFO_VERSION;
899843e1988Sjohnlev 	di->di_modules = (uintptr_t)&modules;
900843e1988Sjohnlev 	di->di_s_text = (uintptr_t)s_text;
901843e1988Sjohnlev 	di->di_e_text = (uintptr_t)e_text;
902843e1988Sjohnlev 	di->di_s_data = (uintptr_t)s_data;
903843e1988Sjohnlev 	di->di_e_data = (uintptr_t)e_data;
904843e1988Sjohnlev 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
905843e1988Sjohnlev 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
909ae115bc7Smrj  * Build the memlists and other kernel essential memory system data structures.
910ae115bc7Smrj  * This is everything at valloc_base.
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate static void
9137c478bd9Sstevel@tonic-gate startup_memlist(void)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	size_t memlist_sz;
9167c478bd9Sstevel@tonic-gate 	size_t memseg_sz;
9177c478bd9Sstevel@tonic-gate 	size_t pagehash_sz;
9187c478bd9Sstevel@tonic-gate 	size_t pp_sz;
9197c478bd9Sstevel@tonic-gate 	uintptr_t va;
9207c478bd9Sstevel@tonic-gate 	size_t len;
9217c478bd9Sstevel@tonic-gate 	uint_t prot;
9227c478bd9Sstevel@tonic-gate 	pfn_t pfn;
9237c478bd9Sstevel@tonic-gate 	int memblocks;
9247c478bd9Sstevel@tonic-gate 	caddr_t pagecolor_mem;
9257c478bd9Sstevel@tonic-gate 	size_t pagecolor_memsz;
9267c478bd9Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
9277c478bd9Sstevel@tonic-gate 	size_t page_ctrs_size;
928d7d93655Sblakej 	size_t pse_table_alloc_size;
9297c478bd9Sstevel@tonic-gate 	struct memlist *current;
9307c478bd9Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
9337c478bd9Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
9347c478bd9Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	/*
9397c478bd9Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
9407c478bd9Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
9417c478bd9Sstevel@tonic-gate 	 */
942ae115bc7Smrj 	len = kbm_nucleus_size;
943ae115bc7Smrj 	ASSERT(len > MMU_PAGESIZE);
9447c478bd9Sstevel@tonic-gate 
945ae115bc7Smrj 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
946ae115bc7Smrj 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
947ae115bc7Smrj 	if (e_moddata - moddata > MODDATA)
948ae115bc7Smrj 		e_moddata = moddata + MODDATA;
9497c478bd9Sstevel@tonic-gate 
950ae115bc7Smrj 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
951ae115bc7Smrj 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
952ae115bc7Smrj 	if (e_modtext - modtext > MODTEXT)
953ae115bc7Smrj 		e_modtext = modtext + MODTEXT;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	econtig = e_moddata;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9587c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
9597c478bd9Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9607c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
9617c478bd9Sstevel@tonic-gate 	PRM_DEBUG(econtig);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	/*
964ae115bc7Smrj 	 * Examine the boot loader physical memory map to find out:
9657c478bd9Sstevel@tonic-gate 	 * - total memory in system - physinstalled
9667c478bd9Sstevel@tonic-gate 	 * - the max physical address - physmax
967ae115bc7Smrj 	 * - the number of discontiguous segments of memory.
9687c478bd9Sstevel@tonic-gate 	 */
9697c478bd9Sstevel@tonic-gate 	if (prom_debug)
970ae115bc7Smrj 		print_memlist("boot physinstalled",
9717c478bd9Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
9727c478bd9Sstevel@tonic-gate 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
9737c478bd9Sstevel@tonic-gate 	    &physinstalled, &memblocks);
9747c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9757c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9767c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	/*
9797c478bd9Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
9807c478bd9Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
9817c478bd9Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
9827c478bd9Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
9837c478bd9Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
9847c478bd9Sstevel@tonic-gate 	 */
9857c478bd9Sstevel@tonic-gate 	mmu_init();
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate #ifdef	__i386
9887c478bd9Sstevel@tonic-gate 	/*
9897c478bd9Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
9907c478bd9Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
9917c478bd9Sstevel@tonic-gate 	 */
9927c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
9937c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
9947c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
9957c478bd9Sstevel@tonic-gate 			physmax = PFN_64G - 1;
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 	} else {
9987c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
9997c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
10007c478bd9Sstevel@tonic-gate 			physmax = PFN_4G - 1;
10017c478bd9Sstevel@tonic-gate 		}
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate #endif
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
10087c478bd9Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
10097c478bd9Sstevel@tonic-gate 		char value[8];
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 		if (len < 8)
10127c478bd9Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
10137c478bd9Sstevel@tonic-gate 		else
10147c478bd9Sstevel@tonic-gate 			(void) strcpy(value, "");
10157c478bd9Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
10167c478bd9Sstevel@tonic-gate 			mmu.pt_nx = 0;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	/*
10217c478bd9Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
10227c478bd9Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
10237c478bd9Sstevel@tonic-gate 	 *
10247c478bd9Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
10257c478bd9Sstevel@tonic-gate 	 */
10267c478bd9Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
10277c478bd9Sstevel@tonic-gate 	obp_pages = 0;
10287c478bd9Sstevel@tonic-gate 	va = KERNEL_TEXT;
1029ae115bc7Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
10307c478bd9Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
10317c478bd9Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
10327c478bd9Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
10337c478bd9Sstevel@tonic-gate 		va += len;
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
10367c478bd9Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	/*
103935b1ab99Sjosephb 	 * If physmem is patched to be non-zero, use it instead of the computed
104035b1ab99Sjosephb 	 * value unless it is larger than the actual amount of memory on hand.
10417c478bd9Sstevel@tonic-gate 	 */
10424944b02eSkchow 	if (physmem == 0 || physmem > npages) {
10437c478bd9Sstevel@tonic-gate 		physmem = npages;
10444944b02eSkchow 	} else if (physmem < npages) {
1045f53ad214Skchow 		orig_npages = npages;
10467c478bd9Sstevel@tonic-gate 		npages = physmem;
10474944b02eSkchow 	}
10487c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmem);
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	/*
10517c478bd9Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
10527c478bd9Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
10537c478bd9Sstevel@tonic-gate 	 * include:
10547c478bd9Sstevel@tonic-gate 	 *	memsegs
10557c478bd9Sstevel@tonic-gate 	 *	memlists
10567c478bd9Sstevel@tonic-gate 	 *	page hash table
10577c478bd9Sstevel@tonic-gate 	 *	page_t's
10587c478bd9Sstevel@tonic-gate 	 *	page coloring data structs
10597c478bd9Sstevel@tonic-gate 	 */
10607c478bd9Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
10617c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
10627c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/*
1065ae115bc7Smrj 	 * Reserve space for memlists. There's no real good way to know exactly
1066ae115bc7Smrj 	 * how much room we'll need, but this should be a good upper bound.
10677c478bd9Sstevel@tonic-gate 	 */
10687c478bd9Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
10697c478bd9Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
10707c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
10717c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	/*
10747c478bd9Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
10757c478bd9Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
10767c478bd9Sstevel@tonic-gate 	 */
10777c478bd9Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
10787c478bd9Sstevel@tonic-gate 	page_hashsz = 1 << highbit(page_hashsz);
10797c478bd9Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
10807c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
10817c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/*
1084ae115bc7Smrj 	 * Set aside room for the page structures themselves.
10857c478bd9Sstevel@tonic-gate 	 */
1086ae115bc7Smrj 	PRM_DEBUG(npages);
1087ae115bc7Smrj 	pp_sz = sizeof (struct page) * npages;
10887c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
10897c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	/*
10927c478bd9Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
10937c478bd9Sstevel@tonic-gate 	 */
10947c478bd9Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
10957c478bd9Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
10967c478bd9Sstevel@tonic-gate 	pagecolor_memsz =
10977c478bd9Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
10987c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
10997c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
11027c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
11037c478bd9Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
11047c478bd9Sstevel@tonic-gate 
1105d7d93655Sblakej 	/*
1106d7d93655Sblakej 	 * Allocate the array that protects pp->p_selock.
1107d7d93655Sblakej 	 */
1108d7d93655Sblakej 	pse_shift = size_pse_array(physmem, max_ncpus);
1109d7d93655Sblakej 	pse_table_size = 1 << pse_shift;
1110d7d93655Sblakej 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
1111d7d93655Sblakej 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
1112d7d93655Sblakej 
1113ae115bc7Smrj #if defined(__amd64)
11147c478bd9Sstevel@tonic-gate 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
1115ae115bc7Smrj 	valloc_base = VALLOC_BASE;
111635b1ab99Sjosephb 
111735b1ab99Sjosephb 	/*
111835b1ab99Sjosephb 	 * The default values of VALLOC_BASE and SEGKPM_BASE should work
111935b1ab99Sjosephb 	 * for values of physmax up to 1 Terabyte. They need adjusting when
112035b1ab99Sjosephb 	 * memory is at addresses above 1 TB.
112135b1ab99Sjosephb 	 */
112235b1ab99Sjosephb 	if (physmax + 1 > mmu_btop(TERABYTE)) {
112335b1ab99Sjosephb 		uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
112435b1ab99Sjosephb 
112535b1ab99Sjosephb 		/* Round to largest possible pagesize for now */
112635b1ab99Sjosephb 		kpm_resv_amount = P2ROUNDUP(kpm_resv_amount, ONE_GIG);
112735b1ab99Sjosephb 
112835b1ab99Sjosephb 		segkpm_base = -(2 * kpm_resv_amount); /* down from top VA */
112935b1ab99Sjosephb 
113035b1ab99Sjosephb 		/* make sure we leave some space for user apps above hole */
113135b1ab99Sjosephb 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
113235b1ab99Sjosephb 		if (segkpm_base > SEGKPM_BASE)
113335b1ab99Sjosephb 			segkpm_base = SEGKPM_BASE;
113435b1ab99Sjosephb 		PRM_DEBUG(segkpm_base);
113535b1ab99Sjosephb 
113635b1ab99Sjosephb 		valloc_base = segkpm_base + kpm_resv_amount;
113735b1ab99Sjosephb 		PRM_DEBUG(valloc_base);
113835b1ab99Sjosephb 	}
1139ae115bc7Smrj #else	/* __i386 */
1140ae115bc7Smrj 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
1141ae115bc7Smrj 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
11427c478bd9Sstevel@tonic-gate 	PRM_DEBUG(valloc_base);
114335b1ab99Sjosephb #endif	/* __i386 */
1144ae115bc7Smrj 
1145ae115bc7Smrj 	/*
1146ae115bc7Smrj 	 * do all the initial allocations
1147ae115bc7Smrj 	 */
1148ae115bc7Smrj 	perform_allocations();
1149ae115bc7Smrj 
1150ae115bc7Smrj 	/*
1151ae115bc7Smrj 	 * Build phys_install and phys_avail in kernel memspace.
1152ae115bc7Smrj 	 * - phys_install should be all memory in the system.
1153ae115bc7Smrj 	 * - phys_avail is phys_install minus any memory mapped before this
1154ae115bc7Smrj 	 *    point above KERNEL_TEXT.
1155ae115bc7Smrj 	 */
1156ae115bc7Smrj 	current = phys_install = memlist;
1157ae115bc7Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
1158ae115bc7Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1159ae115bc7Smrj 		panic("physinstalled was too big!");
1160ae115bc7Smrj 	if (prom_debug)
1161ae115bc7Smrj 		print_memlist("phys_install", phys_install);
1162ae115bc7Smrj 
1163ae115bc7Smrj 	phys_avail = current;
1164ae115bc7Smrj 	PRM_POINT("Building phys_avail:\n");
1165ae115bc7Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
1166ae115bc7Smrj 	    avail_filter);
1167ae115bc7Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1168ae115bc7Smrj 		panic("physavail was too big!");
1169ae115bc7Smrj 	if (prom_debug)
1170ae115bc7Smrj 		print_memlist("phys_avail", phys_avail);
1171ae115bc7Smrj 
1172ae115bc7Smrj 	/*
1173ae115bc7Smrj 	 * setup page coloring
1174ae115bc7Smrj 	 */
1175ae115bc7Smrj 	page_coloring_setup(pagecolor_mem);
1176ae115bc7Smrj 	page_lock_init();	/* currently a no-op */
1177ae115bc7Smrj 
1178ae115bc7Smrj 	/*
1179ae115bc7Smrj 	 * free page list counters
1180ae115bc7Smrj 	 */
1181ae115bc7Smrj 	(void) page_ctrs_alloc(page_ctrs_mem);
1182ae115bc7Smrj 
118306fb6a36Sdv 	/*
118406fb6a36Sdv 	 * Size the pcf array based on the number of cpus in the box at
118506fb6a36Sdv 	 * boot time.
118606fb6a36Sdv 	 */
118706fb6a36Sdv 
118806fb6a36Sdv 	pcf_init();
118906fb6a36Sdv 
1190ae115bc7Smrj 	/*
1191ae115bc7Smrj 	 * Initialize the page structures from the memory lists.
1192ae115bc7Smrj 	 */
1193ae115bc7Smrj 	availrmem_initial = availrmem = freemem = 0;
1194ae115bc7Smrj 	PRM_POINT("Calling kphysm_init()...");
1195ae115bc7Smrj 	npages = kphysm_init(pp_base, npages);
1196ae115bc7Smrj 	PRM_POINT("kphysm_init() done");
1197ae115bc7Smrj 	PRM_DEBUG(npages);
1198ae115bc7Smrj 
1199ae115bc7Smrj 	init_debug_info();
1200ae115bc7Smrj 
1201ae115bc7Smrj 	/*
1202ae115bc7Smrj 	 * Now that page_t's have been initialized, remove all the
1203ae115bc7Smrj 	 * initial allocation pages from the kernel free page lists.
1204ae115bc7Smrj 	 */
1205ae115bc7Smrj 	boot_mapin((caddr_t)valloc_base, valloc_sz);
12060cfdb603Sjosephb 	boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
1207ae115bc7Smrj 	PRM_POINT("startup_memlist() done");
1208ae115bc7Smrj 
1209ae115bc7Smrj 	PRM_DEBUG(valloc_sz);
12102cb27123Saguzovsk 
1211567d55e1Saguzovsk #if defined(__amd64)
1212567d55e1Saguzovsk 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
1213567d55e1Saguzovsk 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
1214567d55e1Saguzovsk 		extern size_t textrepl_size_thresh;
12152cb27123Saguzovsk 		textrepl_size_thresh = (16 << 20) - 1;
12162cb27123Saguzovsk 	}
1217567d55e1Saguzovsk #endif
1218ae115bc7Smrj }
1219ae115bc7Smrj 
1220ae115bc7Smrj /*
1221ae115bc7Smrj  * Layout the kernel's part of address space and initialize kmem allocator.
1222ae115bc7Smrj  */
1223ae115bc7Smrj static void
1224ae115bc7Smrj startup_kmem(void)
1225ae115bc7Smrj {
1226932dc8e5Sdp 	extern void page_set_colorequiv_arr(void);
1227932dc8e5Sdp 
1228ae115bc7Smrj 	PRM_POINT("startup_kmem() starting...");
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate #if defined(__amd64)
12317c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
12327c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
12337c478bd9Sstevel@tonic-gate 		    "systems.");
123435b1ab99Sjosephb 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
12357c478bd9Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
1236ae115bc7Smrj 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
12377c478bd9Sstevel@tonic-gate #else	/* __i386 */
12387c478bd9Sstevel@tonic-gate 	/*
12397c478bd9Sstevel@tonic-gate 	 * We configure kernelbase based on:
12407c478bd9Sstevel@tonic-gate 	 *
12417c478bd9Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
12427c478bd9Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
12437c478bd9Sstevel@tonic-gate 	 *
12447c478bd9Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
12457c478bd9Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
12467c478bd9Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
12477c478bd9Sstevel@tonic-gate 	 *
12487c478bd9Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
12497c478bd9Sstevel@tonic-gate 	 */
12507c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase) {
12517c478bd9Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
12527c478bd9Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
12537c478bd9Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
12547c478bd9Sstevel@tonic-gate 	} else {
12557c478bd9Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
12567c478bd9Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
1259ae115bc7Smrj 	core_base = valloc_base;
12607c478bd9Sstevel@tonic-gate 	core_size = 0;
1261ae115bc7Smrj #endif	/* __i386 */
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_base);
12647c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_size);
1265ae115bc7Smrj 	PRM_DEBUG(kernelbase);
12667c478bd9Sstevel@tonic-gate 
126735b1ab99Sjosephb #if defined(__i386)
12687c478bd9Sstevel@tonic-gate 	segkp_fromheap = 1;
1269ae115bc7Smrj #endif	/* __i386 */
127035b1ab99Sjosephb 
12717c478bd9Sstevel@tonic-gate 	ekernelheap = (char *)core_base;
1272ae115bc7Smrj 	PRM_DEBUG(ekernelheap);
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	/*
12757c478bd9Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
12767c478bd9Sstevel@tonic-gate 	 * update variables that were initialized with a value of
12777c478bd9Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
12787c478bd9Sstevel@tonic-gate 	 *
12797c478bd9Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
12807c478bd9Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
12817c478bd9Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
12827c478bd9Sstevel@tonic-gate 	 *	just be declared as variables there?
12837c478bd9Sstevel@tonic-gate 	 */
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
12867c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
1287ccbaea4fSjosephb #if defined(__amd64)
1288ccbaea4fSjosephb 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
1289ccbaea4fSjosephb #else
12907c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
12917c478bd9Sstevel@tonic-gate #endif
12927c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
12937c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
12947c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
12957c478bd9Sstevel@tonic-gate 
129635b1ab99Sjosephb 	layout_kernel_va();
129735b1ab99Sjosephb 
129835b1ab99Sjosephb #if defined(__i386)
129935b1ab99Sjosephb 	/*
130035b1ab99Sjosephb 	 * If segmap is too large we can push the bottom of the kernel heap
130135b1ab99Sjosephb 	 * higher than the base.  Or worse, it could exceed the top of the
130235b1ab99Sjosephb 	 * VA space entirely, causing it to wrap around.
130335b1ab99Sjosephb 	 */
130435b1ab99Sjosephb 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
130535b1ab99Sjosephb 		panic("too little address space available for kernelheap,"
130635b1ab99Sjosephb 		    " use eeprom for lower kernelbase or smaller segmapsize");
130735b1ab99Sjosephb #endif	/* __i386 */
130835b1ab99Sjosephb 
13097c478bd9Sstevel@tonic-gate 	/*
13107c478bd9Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
13117c478bd9Sstevel@tonic-gate 	 */
131235b1ab99Sjosephb 	kernelheap_init(kernelheap, ekernelheap,
131335b1ab99Sjosephb 	    kernelheap + MMU_PAGESIZE,
1314ae115bc7Smrj 	    (void *)core_base, (void *)(core_base + core_size));
13157c478bd9Sstevel@tonic-gate 
1316843e1988Sjohnlev #if defined(__xpv)
1317843e1988Sjohnlev 	/*
1318843e1988Sjohnlev 	 * Link pending events struct into cpu struct
1319843e1988Sjohnlev 	 */
1320843e1988Sjohnlev 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
1321843e1988Sjohnlev #endif
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	kmem_init();
13267c478bd9Sstevel@tonic-gate 
1327932dc8e5Sdp 	/*
1328932dc8e5Sdp 	 * Factor in colorequiv to check additional 'equivalent' bins
1329932dc8e5Sdp 	 */
1330932dc8e5Sdp 	page_set_colorequiv_arr();
1331932dc8e5Sdp 
13327c478bd9Sstevel@tonic-gate 	/*
13337c478bd9Sstevel@tonic-gate 	 * print this out early so that we know what's going on
13347c478bd9Sstevel@tonic-gate 	 */
13357c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	/*
13387c478bd9Sstevel@tonic-gate 	 * Initialize bp_mapin().
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
13417c478bd9Sstevel@tonic-gate 
1342f53ad214Skchow 	/*
1343f53ad214Skchow 	 * orig_npages is non-zero if physmem has been configured for less
1344f53ad214Skchow 	 * than the available memory.
1345f53ad214Skchow 	 */
1346f53ad214Skchow 	if (orig_npages) {
134735b1ab99Sjosephb 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
134835b1ab99Sjosephb 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
134935b1ab99Sjosephb 		    npages, orig_npages);
1350f53ad214Skchow 	}
13517c478bd9Sstevel@tonic-gate #if defined(__i386)
13527c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
13537c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
13547c478bd9Sstevel@tonic-gate 		    "System using 0x%lx",
13557c478bd9Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
13567c478bd9Sstevel@tonic-gate #endif
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
13597c478bd9Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
13607c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
13617c478bd9Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
13627c478bd9Sstevel@tonic-gate 	}
13637c478bd9Sstevel@tonic-gate #endif
13647c478bd9Sstevel@tonic-gate 
1365843e1988Sjohnlev #ifdef __xpv
1366843e1988Sjohnlev 	/*
1367843e1988Sjohnlev 	 * Some of the xen start information has to be relocated up
1368843e1988Sjohnlev 	 * into the kernel's permanent address space.
1369843e1988Sjohnlev 	 */
1370843e1988Sjohnlev 	PRM_POINT("calling xen_relocate_start_info()");
1371843e1988Sjohnlev 	xen_relocate_start_info();
1372843e1988Sjohnlev 	PRM_POINT("xen_relocate_start_info() done");
1373843e1988Sjohnlev 
1374843e1988Sjohnlev 	/*
1375843e1988Sjohnlev 	 * (Update the vcpu pointer in our cpu structure to point into
1376843e1988Sjohnlev 	 * the relocated shared info.)
1377843e1988Sjohnlev 	 */
1378843e1988Sjohnlev 	CPU->cpu_m.mcpu_vcpu_info =
1379843e1988Sjohnlev 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
1380843e1988Sjohnlev #endif
1381843e1988Sjohnlev 
1382ae115bc7Smrj 	PRM_POINT("startup_kmem() done");
13837c478bd9Sstevel@tonic-gate }
13847c478bd9Sstevel@tonic-gate 
1385551bc2a6Smrj #ifndef __xpv
1386551bc2a6Smrj /*
1387551bc2a6Smrj  * If we have detected that we are running in an HVM environment, we need
1388551bc2a6Smrj  * to prepend the PV driver directory to the module search path.
1389551bc2a6Smrj  */
1390551bc2a6Smrj #define	HVM_MOD_DIR "/platform/i86hvm/kernel"
1391551bc2a6Smrj static void
1392551bc2a6Smrj update_default_path()
1393551bc2a6Smrj {
1394551bc2a6Smrj 	char *current, *newpath;
1395551bc2a6Smrj 	int newlen;
1396551bc2a6Smrj 
1397551bc2a6Smrj 	/*
1398551bc2a6Smrj 	 * We are about to resync with krtld.  krtld will reset its
1399551bc2a6Smrj 	 * internal module search path iff Solaris has set default_path.
1400551bc2a6Smrj 	 * We want to be sure we're prepending this new directory to the
1401551bc2a6Smrj 	 * right search path.
1402551bc2a6Smrj 	 */
1403551bc2a6Smrj 	current = (default_path == NULL) ? kobj_module_path : default_path;
1404551bc2a6Smrj 
1405551bc2a6Smrj 	newlen = strlen(HVM_MOD_DIR) + strlen(current) + 1;
1406551bc2a6Smrj 	newpath = kmem_alloc(newlen, KM_SLEEP);
1407551bc2a6Smrj 	(void) strcpy(newpath, HVM_MOD_DIR);
1408551bc2a6Smrj 	(void) strcat(newpath, " ");
1409551bc2a6Smrj 	(void) strcat(newpath, current);
1410551bc2a6Smrj 
1411551bc2a6Smrj 	default_path = newpath;
1412551bc2a6Smrj }
1413551bc2a6Smrj #endif
1414551bc2a6Smrj 
14157c478bd9Sstevel@tonic-gate static void
14167c478bd9Sstevel@tonic-gate startup_modules(void)
14177c478bd9Sstevel@tonic-gate {
1418ed5289f9SKen Erickson 	int cnt;
14197c478bd9Sstevel@tonic-gate 	extern void prom_setup(void);
1420ed5289f9SKen Erickson 	int32_t v, h;
1421ed5289f9SKen Erickson 	char d[11];
1422ed5289f9SKen Erickson 	char *cp;
1423e4b86885SCheng Sean Ye 	cmi_hdl_t hdl;
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
1426843e1988Sjohnlev 
1427843e1988Sjohnlev #ifndef __xpv
14287c478bd9Sstevel@tonic-gate 	/*
14297c478bd9Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
14307c478bd9Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
14317c478bd9Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
14327c478bd9Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
14337c478bd9Sstevel@tonic-gate 	 */
14347c478bd9Sstevel@tonic-gate 	microfind();
1435551bc2a6Smrj 
1436551bc2a6Smrj 	if (xpv_is_hvm)
1437551bc2a6Smrj 		update_default_path();
1438843e1988Sjohnlev #endif
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	/*
14417c478bd9Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
14427c478bd9Sstevel@tonic-gate 	 */
1443ae115bc7Smrj 	sgmtl(process_rtc_config_file());
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	/*
14467c478bd9Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
14477c478bd9Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
14487c478bd9Sstevel@tonic-gate 	 */
14497c478bd9Sstevel@tonic-gate 	param_calc(0);
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	mod_setup();
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/*
14547c478bd9Sstevel@tonic-gate 	 * Initialize system parameters.
14557c478bd9Sstevel@tonic-gate 	 */
14567c478bd9Sstevel@tonic-gate 	param_init();
14577c478bd9Sstevel@tonic-gate 
14589acbbeafSnn 	/*
14599acbbeafSnn 	 * Initialize the default brands
14609acbbeafSnn 	 */
14619acbbeafSnn 	brand_init();
14629acbbeafSnn 
14637c478bd9Sstevel@tonic-gate 	/*
14647c478bd9Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
14657c478bd9Sstevel@tonic-gate 	 */
14667c478bd9Sstevel@tonic-gate 	maxmem = physmem;
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/*
14697c478bd9Sstevel@tonic-gate 	 * Initialize segment management stuff.
14707c478bd9Sstevel@tonic-gate 	 */
14717c478bd9Sstevel@tonic-gate 	seg_init();
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
14747c478bd9Sstevel@tonic-gate 		halt("Can't load specfs");
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
14777c478bd9Sstevel@tonic-gate 		halt("Can't load devfs");
14787c478bd9Sstevel@tonic-gate 
1479facf4a8dSllai 	if (modload("fs", "dev") == -1)
1480facf4a8dSllai 		halt("Can't load dev");
1481facf4a8dSllai 
148245916cd2Sjpk 	(void) modloadonly("sys", "lbl_edition");
148345916cd2Sjpk 
14847c478bd9Sstevel@tonic-gate 	dispinit();
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	/*
14877c478bd9Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
14887c478bd9Sstevel@tonic-gate 	 */
1489ed5289f9SKen Erickson 	if ((h = set_soft_hostid()) == -1)
1490ed5289f9SKen Erickson 		cmn_err(CE_WARN, "Unable to set hostid");
1491ed5289f9SKen Erickson 	else {
1492ed5289f9SKen Erickson 		for (v = h, cnt = 0; cnt < 10; cnt++) {
1493ed5289f9SKen Erickson 			d[cnt] = v % 10;
1494ed5289f9SKen Erickson 			v /= 10;
1495ed5289f9SKen Erickson 			if (v == 0)
1496ed5289f9SKen Erickson 				break;
1497ed5289f9SKen Erickson 		}
1498ed5289f9SKen Erickson 		for (cp = hw_serial; cnt >= 0; cnt--)
1499ed5289f9SKen Erickson 			*cp++ = d[cnt] + '0';
1500ed5289f9SKen Erickson 		*cp = 0;
1501ed5289f9SKen Erickson 	}
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	/* Read cluster configuration data. */
15047c478bd9Sstevel@tonic-gate 	clconf_init();
15057c478bd9Sstevel@tonic-gate 
1506843e1988Sjohnlev #if defined(__xpv)
1507843e1988Sjohnlev 	ec_init();
1508843e1988Sjohnlev 	gnttab_init();
1509843e1988Sjohnlev 	(void) xs_early_init();
1510843e1988Sjohnlev #endif /* __xpv */
1511843e1988Sjohnlev 
15127c478bd9Sstevel@tonic-gate 	/*
15137c478bd9Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
15147c478bd9Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
15157c478bd9Sstevel@tonic-gate 	 */
15167c478bd9Sstevel@tonic-gate 	setup_ddi();
15177aec1d6eScindi 
1518e4b86885SCheng Sean Ye 	/*
1519e4b86885SCheng Sean Ye 	 * Set up the CPU module subsystem for the boot cpu in the native
1520e4b86885SCheng Sean Ye 	 * case, and all physical cpu resource in the xpv dom0 case.
1521e4b86885SCheng Sean Ye 	 * Modifies the device tree, so this must be done after
1522e4b86885SCheng Sean Ye 	 * setup_ddi().
1523e4b86885SCheng Sean Ye 	 */
1524e4b86885SCheng Sean Ye #ifdef __xpv
1525e4b86885SCheng Sean Ye 	/*
1526e4b86885SCheng Sean Ye 	 * If paravirtualized and on dom0 then we initialize all physical
1527e4b86885SCheng Sean Ye 	 * cpu handles now;  if paravirtualized on a domU then do not
1528e4b86885SCheng Sean Ye 	 * initialize.
1529e4b86885SCheng Sean Ye 	 */
1530e4b86885SCheng Sean Ye 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1531e4b86885SCheng Sean Ye 		xen_mc_lcpu_cookie_t cpi;
1532e4b86885SCheng Sean Ye 
1533e4b86885SCheng Sean Ye 		for (cpi = xen_physcpu_next(NULL); cpi != NULL;
1534e4b86885SCheng Sean Ye 		    cpi = xen_physcpu_next(cpi)) {
1535e4b86885SCheng Sean Ye 			if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
1536e4b86885SCheng Sean Ye 			    xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
1537e4b86885SCheng Sean Ye 			    xen_physcpu_strandid(cpi))) != NULL &&
1538e4b86885SCheng Sean Ye 			    (x86_feature & X86_MCA))
153920c794b3Sgavinm 				cmi_mca_init(hdl);
154020c794b3Sgavinm 		}
154120c794b3Sgavinm 	}
1542e4b86885SCheng Sean Ye #else
1543e4b86885SCheng Sean Ye 	/*
1544e4b86885SCheng Sean Ye 	 * Initialize a handle for the boot cpu - others will initialize
1545e4b86885SCheng Sean Ye 	 * as they startup.  Do not do this if we know we are in an HVM domU.
1546e4b86885SCheng Sean Ye 	 */
1547e4b86885SCheng Sean Ye 	if (!xpv_is_hvm &&
1548e4b86885SCheng Sean Ye 	    (hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
1549e4b86885SCheng Sean Ye 	    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL &&
1550e4b86885SCheng Sean Ye 	    (x86_feature & X86_MCA))
1551e4b86885SCheng Sean Ye 			cmi_mca_init(hdl);
155220c794b3Sgavinm #endif	/* __xpv */
15537aec1d6eScindi 
15547c478bd9Sstevel@tonic-gate 	/*
15557c478bd9Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
15567c478bd9Sstevel@tonic-gate 	 */
1557ae115bc7Smrj 	PRM_POINT("startup_modules: calling prom_setup...");
15587c478bd9Sstevel@tonic-gate 	prom_setup();
1559ae115bc7Smrj 	PRM_POINT("startup_modules: done");
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	/*
15627c478bd9Sstevel@tonic-gate 	 * Load all platform specific modules
15637c478bd9Sstevel@tonic-gate 	 */
1564ae115bc7Smrj 	PRM_POINT("startup_modules: calling psm_modload...");
15657c478bd9Sstevel@tonic-gate 	psm_modload();
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
15687c478bd9Sstevel@tonic-gate }
15697c478bd9Sstevel@tonic-gate 
1570ae115bc7Smrj /*
1571ae115bc7Smrj  * claim a "setaside" boot page for use in the kernel
1572ae115bc7Smrj  */
1573ae115bc7Smrj page_t *
1574ae115bc7Smrj boot_claim_page(pfn_t pfn)
15757c478bd9Sstevel@tonic-gate {
1576ae115bc7Smrj 	page_t *pp;
15777c478bd9Sstevel@tonic-gate 
1578ae115bc7Smrj 	pp = page_numtopp_nolock(pfn);
1579ae115bc7Smrj 	ASSERT(pp != NULL);
15807c478bd9Sstevel@tonic-gate 
1581ae115bc7Smrj 	if (PP_ISBOOTPAGES(pp)) {
1582ae115bc7Smrj 		if (pp->p_next != NULL)
1583ae115bc7Smrj 			pp->p_next->p_prev = pp->p_prev;
1584ae115bc7Smrj 		if (pp->p_prev == NULL)
1585ae115bc7Smrj 			bootpages = pp->p_next;
1586ae115bc7Smrj 		else
1587ae115bc7Smrj 			pp->p_prev->p_next = pp->p_next;
1588ae115bc7Smrj 	} else {
1589ae115bc7Smrj 		/*
1590ae115bc7Smrj 		 * htable_attach() expects a base pagesize page
1591ae115bc7Smrj 		 */
1592ae115bc7Smrj 		if (pp->p_szc != 0)
1593ae115bc7Smrj 			page_boot_demote(pp);
1594ae115bc7Smrj 		pp = page_numtopp(pfn, SE_EXCL);
1595ae115bc7Smrj 	}
1596ae115bc7Smrj 	return (pp);
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate /*
16007c478bd9Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
16017c478bd9Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
16027c478bd9Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
16037c478bd9Sstevel@tonic-gate  */
16047c478bd9Sstevel@tonic-gate static void
16057c478bd9Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate 	uintptr_t va = low;
16087c478bd9Sstevel@tonic-gate 	size_t len;
16097c478bd9Sstevel@tonic-gate 	uint_t prot;
16107c478bd9Sstevel@tonic-gate 	pfn_t pfn;
16117c478bd9Sstevel@tonic-gate 	page_t *pp;
16127c478bd9Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
16137c478bd9Sstevel@tonic-gate 
1614ae115bc7Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
16157c478bd9Sstevel@tonic-gate 		if (va + len >= high)
16167c478bd9Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
16177c478bd9Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 		while (len > 0) {
16207c478bd9Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
16217c478bd9Sstevel@tonic-gate 			if (pp != NULL) {
16227c478bd9Sstevel@tonic-gate 				if (setaside == 0)
16237c478bd9Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
16247c478bd9Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
16257c478bd9Sstevel@tonic-gate 					    (void *)va, pfn);
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 				pp->p_next = bootpages;
1628ae115bc7Smrj 				pp->p_prev = NULL;
1629ae115bc7Smrj 				PP_SETBOOTPAGES(pp);
1630ae115bc7Smrj 				if (bootpages != NULL) {
1631ae115bc7Smrj 					bootpages->p_prev = pp;
1632ae115bc7Smrj 				}
16337c478bd9Sstevel@tonic-gate 				bootpages = pp;
16347c478bd9Sstevel@tonic-gate 				++boot_protect_cnt;
16357c478bd9Sstevel@tonic-gate 			}
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 			++pfn;
16387c478bd9Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
16397c478bd9Sstevel@tonic-gate 			va += MMU_PAGESIZE;
16407c478bd9Sstevel@tonic-gate 		}
16417c478bd9Sstevel@tonic-gate 	}
16427c478bd9Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
16437c478bd9Sstevel@tonic-gate }
16447c478bd9Sstevel@tonic-gate 
1645ae115bc7Smrj /*
164635b1ab99Sjosephb  *
1647ae115bc7Smrj  */
16487c478bd9Sstevel@tonic-gate static void
164935b1ab99Sjosephb layout_kernel_va(void)
16507c478bd9Sstevel@tonic-gate {
165135b1ab99Sjosephb 	PRM_POINT("layout_kernel_va() starting...");
16527c478bd9Sstevel@tonic-gate 	/*
1653ae115bc7Smrj 	 * Establish the final size of the kernel's heap, size of segmap,
1654ae115bc7Smrj 	 * segkp, etc.
16557c478bd9Sstevel@tonic-gate 	 */
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate #if defined(__amd64)
16587c478bd9Sstevel@tonic-gate 
165935b1ab99Sjosephb 	kpm_vbase = (caddr_t)segkpm_base;
166035b1ab99Sjosephb 	kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
166135b1ab99Sjosephb 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
166235b1ab99Sjosephb 		panic("not enough room for kpm!");
1663ae115bc7Smrj 	PRM_DEBUG(kpm_size);
1664ae115bc7Smrj 	PRM_DEBUG(kpm_vbase);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	/*
1667ae115bc7Smrj 	 * By default we create a seg_kp in 64 bit kernels, it's a little
1668ae115bc7Smrj 	 * faster to access than embedding it in the heap.
16697c478bd9Sstevel@tonic-gate 	 */
1670ae115bc7Smrj 	segkp_base = (caddr_t)valloc_base + valloc_sz;
16717c478bd9Sstevel@tonic-gate 	if (!segkp_fromheap) {
16727c478bd9Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 		/*
1675ae115bc7Smrj 		 * determine size of segkp
16767c478bd9Sstevel@tonic-gate 		 */
16777c478bd9Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
16787c478bd9Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
16797c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
16807c478bd9Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
16817c478bd9Sstevel@tonic-gate 			    mmu_btop(sz));
16827c478bd9Sstevel@tonic-gate 		}
16837c478bd9Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
16867c478bd9Sstevel@tonic-gate 	}
1687ae115bc7Smrj 	PRM_DEBUG(segkp_base);
1688ae115bc7Smrj 	PRM_DEBUG(segkpsize);
16897c478bd9Sstevel@tonic-gate 
169035b1ab99Sjosephb 	/*
169135b1ab99Sjosephb 	 * segzio is used for ZFS cached data. It uses a distinct VA
169235b1ab99Sjosephb 	 * segment (from kernel heap) so that we can easily tell not to
169335b1ab99Sjosephb 	 * include it in kernel crash dumps on 64 bit kernels. The trick is
169435b1ab99Sjosephb 	 * to give it lots of VA, but not constrain the kernel heap.
169535b1ab99Sjosephb 	 * We scale the size of segzio linearly with physmem up to
169635b1ab99Sjosephb 	 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem.
169735b1ab99Sjosephb 	 */
1698ae115bc7Smrj 	segzio_base = segkp_base + mmu_ptob(segkpsize);
1699ae115bc7Smrj 	if (segzio_fromheap) {
1700ae115bc7Smrj 		segziosize = 0;
1701ae115bc7Smrj 	} else {
170235b1ab99Sjosephb 		size_t physmem_size = mmu_ptob(physmem);
170335b1ab99Sjosephb 		size_t size = (segziosize == 0) ?
170435b1ab99Sjosephb 		    physmem_size : mmu_ptob(segziosize);
1705ad23a2dbSjohansen 
170635b1ab99Sjosephb 		if (size < SEGZIOMINSIZE)
1707ad23a2dbSjohansen 			size = SEGZIOMINSIZE;
170835b1ab99Sjosephb 		if (size > SEGZIOMAXSIZE) {
1709a778305aSjohansen 			size = SEGZIOMAXSIZE;
171035b1ab99Sjosephb 			if (physmem_size > size)
171135b1ab99Sjosephb 				size += (physmem_size - size) / 2;
1712ad23a2dbSjohansen 		}
1713ad23a2dbSjohansen 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
1714ad23a2dbSjohansen 	}
1715ae115bc7Smrj 	PRM_DEBUG(segziosize);
1716ae115bc7Smrj 	PRM_DEBUG(segzio_base);
1717ad23a2dbSjohansen 
17187c478bd9Sstevel@tonic-gate 	/*
1719ae115bc7Smrj 	 * Put the range of VA for device mappings next, kmdb knows to not
1720ae115bc7Smrj 	 * grep in this range of addresses.
17217c478bd9Sstevel@tonic-gate 	 */
1722ae115bc7Smrj 	toxic_addr =
1723ae115bc7Smrj 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
17247c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
1725ae115bc7Smrj 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
1726ae115bc7Smrj #else /* __i386 */
1727ae115bc7Smrj 	segmap_start = ROUND_UP_LPAGE(kernelbase);
1728ae115bc7Smrj #endif /* __i386 */
1729ae115bc7Smrj 	PRM_DEBUG(segmap_start);
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	/*
173252eef812SAmrita Sadhukhan 	 * Users can change segmapsize through eeprom. If the variable
173352eef812SAmrita Sadhukhan 	 * is tuned through eeprom, there is no upper bound on the
1734ed5289f9SKen Erickson 	 * size of segmap.
17357c478bd9Sstevel@tonic-gate 	 */
17367c478bd9Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate #if defined(__i386)
17397c478bd9Sstevel@tonic-gate 	/*
17407c478bd9Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
17417c478bd9Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
1742ae115bc7Smrj 	 * small red zone just below the start of segmap.
17437c478bd9Sstevel@tonic-gate 	 */
1744ae115bc7Smrj 	segmap_start += KERNEL_REDZONE_SIZE;
17457c478bd9Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
17467c478bd9Sstevel@tonic-gate #endif
17477c478bd9Sstevel@tonic-gate 
1748ae115bc7Smrj 	PRM_DEBUG(segmap_start);
17497c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segmapsize);
175035b1ab99Sjosephb 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
175135b1ab99Sjosephb 	PRM_DEBUG(kernelheap);
175235b1ab99Sjosephb 	PRM_POINT("layout_kernel_va() done...");
175335b1ab99Sjosephb }
175435b1ab99Sjosephb 
175535b1ab99Sjosephb /*
175635b1ab99Sjosephb  * Finish initializing the VM system, now that we are no longer
175735b1ab99Sjosephb  * relying on the boot time memory allocators.
175835b1ab99Sjosephb  */
175935b1ab99Sjosephb static void
176035b1ab99Sjosephb startup_vm(void)
176135b1ab99Sjosephb {
176235b1ab99Sjosephb 	struct segmap_crargs a;
176335b1ab99Sjosephb 
176435b1ab99Sjosephb 	extern int use_brk_lpg, use_stk_lpg;
176535b1ab99Sjosephb 
176635b1ab99Sjosephb 	PRM_POINT("startup_vm() starting...");
176735b1ab99Sjosephb 
176835b1ab99Sjosephb 	/*
176935b1ab99Sjosephb 	 * Initialize the hat layer.
177035b1ab99Sjosephb 	 */
177135b1ab99Sjosephb 	hat_init();
17727c478bd9Sstevel@tonic-gate 
1773ae115bc7Smrj 	/*
1774ae115bc7Smrj 	 * Do final allocations of HAT data structures that need to
1775ae115bc7Smrj 	 * be allocated before quiescing the boot loader.
1776ae115bc7Smrj 	 */
1777ae115bc7Smrj 	PRM_POINT("Calling hat_kern_alloc()...");
1778ae115bc7Smrj 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
1779ae115bc7Smrj 	PRM_POINT("hat_kern_alloc() done");
1780ae115bc7Smrj 
1781843e1988Sjohnlev #ifndef __xpv
1782ae115bc7Smrj 	/*
17831d03c31eSjohnlev 	 * Setup Page Attribute Table
1784ae115bc7Smrj 	 */
17851d03c31eSjohnlev 	pat_sync();
1786843e1988Sjohnlev #endif
1787ae115bc7Smrj 
1788ae115bc7Smrj 	/*
1789ae115bc7Smrj 	 * The next two loops are done in distinct steps in order
1790ae115bc7Smrj 	 * to be sure that any page that is doubly mapped (both above
1791ae115bc7Smrj 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
1792ae115bc7Smrj 	 * Note this may never happen, but it might someday.
1793ae115bc7Smrj 	 */
1794ae115bc7Smrj 	bootpages = NULL;
1795ae115bc7Smrj 	PRM_POINT("Protecting boot pages");
1796ae115bc7Smrj 
1797ae115bc7Smrj 	/*
1798ae115bc7Smrj 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
1799ae115bc7Smrj 	 * page_t's. This can only happen if something weird allocated
1800ae115bc7Smrj 	 * in this range (like kadb/kmdb).
1801ae115bc7Smrj 	 */
1802ae115bc7Smrj 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
1803ae115bc7Smrj 
1804ae115bc7Smrj 	/*
1805ae115bc7Smrj 	 * Before we can take over memory allocation/mapping from the boot
1806ae115bc7Smrj 	 * loader we must remove from our free page lists any boot allocated
1807ae115bc7Smrj 	 * pages that stay mapped until release_bootstrap().
1808ae115bc7Smrj 	 */
1809ae115bc7Smrj 	protect_boot_range(0, kernelbase, 1);
1810ae115bc7Smrj 
1811843e1988Sjohnlev 
1812ae115bc7Smrj 	/*
1813ae115bc7Smrj 	 * Switch to running on regular HAT (not boot_mmu)
1814ae115bc7Smrj 	 */
1815ae115bc7Smrj 	PRM_POINT("Calling hat_kern_setup()...");
1816ae115bc7Smrj 	hat_kern_setup();
1817ae115bc7Smrj 
1818ae115bc7Smrj 	/*
1819ae115bc7Smrj 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
1820ae115bc7Smrj 	 */
1821ae115bc7Smrj 	bop_no_more_mem();
1822ae115bc7Smrj 
1823ae115bc7Smrj 	PRM_POINT("hat_kern_setup() done");
1824ae115bc7Smrj 
1825ae115bc7Smrj 	hat_cpu_online(CPU);
1826ae115bc7Smrj 
18277c478bd9Sstevel@tonic-gate 	/*
18287c478bd9Sstevel@tonic-gate 	 * Initialize VM system
18297c478bd9Sstevel@tonic-gate 	 */
18307c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
18317c478bd9Sstevel@tonic-gate 	kvm_init();
18327c478bd9Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	/*
18357c478bd9Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
18367c478bd9Sstevel@tonic-gate 	 */
18377c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
18387c478bd9Sstevel@tonic-gate 		kdi_dvec_vmready();
18397c478bd9Sstevel@tonic-gate 
1840843e1988Sjohnlev #if defined(__xpv)
1841843e1988Sjohnlev 	/*
1842843e1988Sjohnlev 	 * Populate the I/O pool on domain 0
1843843e1988Sjohnlev 	 */
1844843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1845843e1988Sjohnlev 		extern long populate_io_pool(void);
1846843e1988Sjohnlev 		long init_io_pool_cnt;
1847843e1988Sjohnlev 
1848843e1988Sjohnlev 		PRM_POINT("Populating reserve I/O page pool");
1849843e1988Sjohnlev 		init_io_pool_cnt = populate_io_pool();
1850843e1988Sjohnlev 		PRM_DEBUG(init_io_pool_cnt);
1851843e1988Sjohnlev 	}
1852843e1988Sjohnlev #endif
18537c478bd9Sstevel@tonic-gate 	/*
18547c478bd9Sstevel@tonic-gate 	 * Mangle the brand string etc.
18557c478bd9Sstevel@tonic-gate 	 */
18567c478bd9Sstevel@tonic-gate 	cpuid_pass3(CPU);
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate #if defined(__amd64)
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/*
18617c478bd9Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
18627c478bd9Sstevel@tonic-gate 	 */
18637c478bd9Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
18647c478bd9Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate #else	/* __i386 */
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	/*
18697c478bd9Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
18707c478bd9Sstevel@tonic-gate 	 */
1871ae115bc7Smrj 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
18727c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
18737c478bd9Sstevel@tonic-gate 	toxic_bit_map =
18747c478bd9Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
18757c478bd9Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
18767c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate #endif	/* __i386 */
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	/*
18827c478bd9Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
18837c478bd9Sstevel@tonic-gate 	 * it, tell the debugger.
18847c478bd9Sstevel@tonic-gate 	 */
18857c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
18867c478bd9Sstevel@tonic-gate 		kdi_dvec_memavail();
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	/*
18897c478bd9Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
18907c478bd9Sstevel@tonic-gate 	 * to work around a pentium bug.
18917c478bd9Sstevel@tonic-gate 	 */
1892843e1988Sjohnlev #if !defined(__amd64) && !defined(__xpv)
18937c478bd9Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
1894ae115bc7Smrj 		desctbr_t idtr;
18957c478bd9Sstevel@tonic-gate 		gate_desc_t *newidt;
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
18987c478bd9Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
18997c478bd9Sstevel@tonic-gate 
19000cfdb603Sjosephb 		bcopy(idt0, newidt, NIDT * sizeof (*idt0));
19017c478bd9Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
1902843e1988Sjohnlev 		    KCS_SEL, SDT_SYSIGT, TRP_KPL);
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
19050cfdb603Sjosephb 		    PROT_READ | PROT_EXEC);
19067c478bd9Sstevel@tonic-gate 
19070cfdb603Sjosephb 		CPU->cpu_idt = newidt;
19080cfdb603Sjosephb 		idtr.dtr_base = (uintptr_t)CPU->cpu_idt;
19090cfdb603Sjosephb 		idtr.dtr_limit = (NIDT * sizeof (*idt0)) - 1;
1910ae115bc7Smrj 		wr_idtr(&idtr);
19117c478bd9Sstevel@tonic-gate 	}
19127c478bd9Sstevel@tonic-gate #endif	/* !__amd64 */
19137c478bd9Sstevel@tonic-gate 
1914843e1988Sjohnlev #if !defined(__xpv)
19157c478bd9Sstevel@tonic-gate 	/*
19167c478bd9Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
19177c478bd9Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
19187c478bd9Sstevel@tonic-gate 	 */
19197c478bd9Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
19207c478bd9Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
19217c478bd9Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
1922843e1988Sjohnlev #endif
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
19257c478bd9Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
19267c478bd9Sstevel@tonic-gate 
192783f9b804Skchow 	/*
192883f9b804Skchow 	 * disable automatic large pages for small memory systems or
192983f9b804Skchow 	 * when the disable flag is set.
193002bc52beSkchow 	 *
193102bc52beSkchow 	 * Do not yet consider page sizes larger than 2m/4m.
193283f9b804Skchow 	 */
1933ec25b48fSsusans 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
1934ec25b48fSsusans 		max_uheap_lpsize = LEVEL_SIZE(1);
1935ec25b48fSsusans 		max_ustack_lpsize = LEVEL_SIZE(1);
1936ec25b48fSsusans 		max_privmap_lpsize = LEVEL_SIZE(1);
1937ec25b48fSsusans 		max_uidata_lpsize = LEVEL_SIZE(1);
1938ec25b48fSsusans 		max_utext_lpsize = LEVEL_SIZE(1);
1939ec25b48fSsusans 		max_shm_lpsize = LEVEL_SIZE(1);
1940ec25b48fSsusans 	}
1941ec25b48fSsusans 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
1942ec25b48fSsusans 	    auto_lpg_disable) {
1943beb1bda0Sdavemq 		use_brk_lpg = 0;
1944beb1bda0Sdavemq 		use_stk_lpg = 0;
1945ec25b48fSsusans 	}
194602bc52beSkchow 	mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
1947beb1bda0Sdavemq 
19487c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
19497c478bd9Sstevel@tonic-gate 	hat_init_finish();
19507c478bd9Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	/*
19537c478bd9Sstevel@tonic-gate 	 * Initialize the segkp segment type.
19547c478bd9Sstevel@tonic-gate 	 */
19557c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
1956ae115bc7Smrj 	PRM_POINT("Attaching segkp");
1957ae115bc7Smrj 	if (segkp_fromheap) {
19587c478bd9Sstevel@tonic-gate 		segkp->s_as = &kas;
1959ae115bc7Smrj 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
1960ae115bc7Smrj 	    segkp) < 0) {
1961ae115bc7Smrj 		panic("startup: cannot attach segkp");
1962ae115bc7Smrj 		/*NOTREACHED*/
19637c478bd9Sstevel@tonic-gate 	}
1964ae115bc7Smrj 	PRM_POINT("Doing segkp_create()");
19657c478bd9Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
19667c478bd9Sstevel@tonic-gate 		panic("startup: segkp_create failed");
19677c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segkp);
19707c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	/*
19737c478bd9Sstevel@tonic-gate 	 * kpm segment
19747c478bd9Sstevel@tonic-gate 	 */
19757c478bd9Sstevel@tonic-gate 	segmap_kpm = 0;
19767c478bd9Sstevel@tonic-gate 	if (kpm_desired) {
19777c478bd9Sstevel@tonic-gate 		kpm_init();
19787c478bd9Sstevel@tonic-gate 		kpm_enable = 1;
1979a5652762Spraks 		vpm_enable = 1;
19807c478bd9Sstevel@tonic-gate 	}
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	/*
19837c478bd9Sstevel@tonic-gate 	 * Now create segmap segment.
19847c478bd9Sstevel@tonic-gate 	 */
19857c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
1986ae115bc7Smrj 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
1987ae115bc7Smrj 		panic("cannot attach segmap");
19887c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
19897c478bd9Sstevel@tonic-gate 	}
1990ae115bc7Smrj 	PRM_DEBUG(segmap);
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
19937c478bd9Sstevel@tonic-gate 	a.shmsize = 0;
19947c478bd9Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
19957c478bd9Sstevel@tonic-gate 
1996ae115bc7Smrj 	if (segmap_create(segmap, (caddr_t)&a) != 0)
1997ae115bc7Smrj 		panic("segmap_create segmap");
19987c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	segdev_init();
2003843e1988Sjohnlev #if defined(__xpv)
2004843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2005843e1988Sjohnlev #endif
2006843e1988Sjohnlev 		pmem_init();
2007ae115bc7Smrj 
20087c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
20097c478bd9Sstevel@tonic-gate }
20107c478bd9Sstevel@tonic-gate 
2011ae115bc7Smrj /*
2012ae115bc7Smrj  * Load a tod module for the non-standard tod part found on this system.
2013ae115bc7Smrj  */
2014ae115bc7Smrj static void
2015ae115bc7Smrj load_tod_module(char *todmod)
2016ae115bc7Smrj {
2017ae115bc7Smrj 	if (modload("tod", todmod) == -1)
2018ae115bc7Smrj 		halt("Can't load TOD module");
2019ae115bc7Smrj }
2020ae115bc7Smrj 
2021*94f1124eSVikram Hegde #if !defined(__xpv)
2022*94f1124eSVikram Hegde static void
2023*94f1124eSVikram Hegde get_amd_iommu_boot_props(void)
2024*94f1124eSVikram Hegde {
2025*94f1124eSVikram Hegde 	int len;
2026*94f1124eSVikram Hegde 	char *disable;
2027*94f1124eSVikram Hegde 
2028*94f1124eSVikram Hegde 	if ((len = do_bsys_getproplen(NULL, "amd-iommu")) > 0) {
2029*94f1124eSVikram Hegde 		disable = kmem_zalloc(len, KM_SLEEP);
2030*94f1124eSVikram Hegde 		(void) do_bsys_getprop(NULL, "amd-iommu", disable);
2031*94f1124eSVikram Hegde 		if (strcmp(disable, "no") == 0) {
2032*94f1124eSVikram Hegde 			startup_amd_iommu_disable = 1;
2033*94f1124eSVikram Hegde 		}
2034*94f1124eSVikram Hegde 		kmem_free(disable, len);
2035*94f1124eSVikram Hegde 	}
2036*94f1124eSVikram Hegde 
2037*94f1124eSVikram Hegde 	if ((len = do_bsys_getproplen(NULL, "amd-iommu-disable-list")) > 0) {
2038*94f1124eSVikram Hegde 		startup_amd_iommu_disable_list = kmem_zalloc(len, KM_SLEEP);
2039*94f1124eSVikram Hegde 		(void) do_bsys_getprop(NULL, "amd-iommu-disable-list",
2040*94f1124eSVikram Hegde 		    startup_amd_iommu_disable_list);
2041*94f1124eSVikram Hegde 	}
2042*94f1124eSVikram Hegde }
2043*94f1124eSVikram Hegde #endif
2044*94f1124eSVikram Hegde 
20457c478bd9Sstevel@tonic-gate static void
20467c478bd9Sstevel@tonic-gate startup_end(void)
20477c478bd9Sstevel@tonic-gate {
2048dd4eeefdSeota 	int i;
20497c478bd9Sstevel@tonic-gate 	extern void setx86isalist(void);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	/*
20547c478bd9Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
20557c478bd9Sstevel@tonic-gate 	 * initialization has been done but before the clock
20567c478bd9Sstevel@tonic-gate 	 * and other devices get started.
20577c478bd9Sstevel@tonic-gate 	 */
20587c478bd9Sstevel@tonic-gate 	kern_setup1();
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	/*
20617c478bd9Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
20627c478bd9Sstevel@tonic-gate 	 */
20637c478bd9Sstevel@tonic-gate 	kcpc_hw_init(CPU);
20647c478bd9Sstevel@tonic-gate 
2065ee88d2b9Skchow #if defined(OPTERON_WORKAROUND_6323525)
2066ee88d2b9Skchow 	if (opteron_workaround_6323525)
2067ee88d2b9Skchow 		patch_workaround_6323525();
2068ee88d2b9Skchow #endif
2069ae115bc7Smrj 	/*
2070ae115bc7Smrj 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
2071ae115bc7Smrj 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
2072ae115bc7Smrj 	 */
2073ae115bc7Smrj 	if (tod_module_name != NULL) {
2074ae115bc7Smrj 		PRM_POINT("load_tod_module()");
2075ae115bc7Smrj 		load_tod_module(tod_module_name);
2076ae115bc7Smrj 	}
2077ae115bc7Smrj 
2078843e1988Sjohnlev #if defined(__xpv)
2079843e1988Sjohnlev 	/*
2080843e1988Sjohnlev 	 * Forceload interposing TOD module for the hypervisor.
2081843e1988Sjohnlev 	 */
2082843e1988Sjohnlev 	PRM_POINT("load_tod_module()");
2083843e1988Sjohnlev 	load_tod_module("xpvtod");
2084843e1988Sjohnlev #endif
2085843e1988Sjohnlev 
20867c478bd9Sstevel@tonic-gate 	/*
20877c478bd9Sstevel@tonic-gate 	 * Configure the system.
20887c478bd9Sstevel@tonic-gate 	 */
20897c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
20907c478bd9Sstevel@tonic-gate 	configure();		/* set up devices */
20917c478bd9Sstevel@tonic-gate 	PRM_POINT("configure() done");
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	/*
20947c478bd9Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
20957c478bd9Sstevel@tonic-gate 	 * support.
20967c478bd9Sstevel@tonic-gate 	 */
20977c478bd9Sstevel@tonic-gate 	setx86isalist();
2098100b72f4Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
20997c478bd9Sstevel@tonic-gate 	psm_install();
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	/*
21027c478bd9Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
21037c478bd9Sstevel@tonic-gate 	 * we're still using bootsvcs.
21047c478bd9Sstevel@tonic-gate 	 */
2105ae115bc7Smrj 	PRM_POINT("NULLing out bootops");
2106ae115bc7Smrj 	*bootopsp = (struct bootops *)NULL;
21077c478bd9Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
21087c478bd9Sstevel@tonic-gate 
2109843e1988Sjohnlev #if defined(__xpv)
2110843e1988Sjohnlev 	ec_init_debug_irq();
2111843e1988Sjohnlev 	xs_domu_init();
2112843e1988Sjohnlev #endif
21137c478bd9Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
21147c478bd9Sstevel@tonic-gate 	(*picinitf)();
21157c478bd9Sstevel@tonic-gate 	sti();
2116843e1988Sjohnlev #if defined(__xpv)
2117843e1988Sjohnlev 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
2118843e1988Sjohnlev 	xen_late_startup();
2119843e1988Sjohnlev #endif
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
212235b1ab99Sjosephb 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
21237c478bd9Sstevel@tonic-gate 
2124dd4eeefdSeota 	/*
2125dd4eeefdSeota 	 * Register these software interrupts for ddi timer.
2126dd4eeefdSeota 	 * Software interrupts up to the level 10 are supported.
2127dd4eeefdSeota 	 */
2128dd4eeefdSeota 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
2129dd4eeefdSeota 		char name[sizeof ("timer_softintr") + 2];
2130dd4eeefdSeota 		(void) sprintf(name, "timer_softintr%02d", i);
2131dd4eeefdSeota 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
2132dd4eeefdSeota 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
2133dd4eeefdSeota 	}
2134dd4eeefdSeota 
21357125fcbdSVikram Hegde #if !defined(__xpv)
2136*94f1124eSVikram Hegde 	get_amd_iommu_boot_props();
2137*94f1124eSVikram Hegde 
21387125fcbdSVikram Hegde 	if (modload("drv", "amd_iommu") < 0) {
21397125fcbdSVikram Hegde 		PRM_POINT("No AMD IOMMU present\n");
2140*94f1124eSVikram Hegde 	} else if (ddi_hold_installed_driver(ddi_name_to_major(
2141*94f1124eSVikram Hegde 	    "amd_iommu")) == NULL) {
21427125fcbdSVikram Hegde 		prom_printf("ERROR: failed to attach AMD IOMMU\n");
21437125fcbdSVikram Hegde 	}
21447125fcbdSVikram Hegde #endif
21457125fcbdSVikram Hegde 
21467c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
21477c478bd9Sstevel@tonic-gate }
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate extern char hw_serial[];
215096d008c2SKen Erickson /*
215196d008c2SKen Erickson  * Don't remove the following 2 variables.  They are necessary
215296d008c2SKen Erickson  * for reading the hostid from the legacy file (/kernel/misc/sysinit).
215396d008c2SKen Erickson  */
21547c478bd9Sstevel@tonic-gate char *_hs1107 = hw_serial;
21557c478bd9Sstevel@tonic-gate ulong_t  _bdhs34;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate void
21587c478bd9Sstevel@tonic-gate post_startup(void)
21597c478bd9Sstevel@tonic-gate {
21607c478bd9Sstevel@tonic-gate 	/*
21617c478bd9Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
21627c478bd9Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
21637c478bd9Sstevel@tonic-gate 	 * instruction set extensions.
21647c478bd9Sstevel@tonic-gate 	 */
21657c478bd9Sstevel@tonic-gate 	bind_hwcap();
21667c478bd9Sstevel@tonic-gate 
2167843e1988Sjohnlev #ifdef __xpv
2168843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2169843e1988Sjohnlev #endif
2170843e1988Sjohnlev 	{
2171843e1988Sjohnlev 		/*
2172843e1988Sjohnlev 		 * Load the System Management BIOS into the global ksmbios
2173843e1988Sjohnlev 		 * handle, if an SMBIOS is present on this system.
2174843e1988Sjohnlev 		 */
2175843e1988Sjohnlev 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
21767c478bd9Sstevel@tonic-gate 
2177843e1988Sjohnlev #if defined(__xpv)
2178843e1988Sjohnlev 		xpv_panic_init();
2179843e1988Sjohnlev #else
2180843e1988Sjohnlev 		/*
2181843e1988Sjohnlev 		 * Startup the memory scrubber.
2182843e1988Sjohnlev 		 * XXPV	This should be running somewhere ..
2183843e1988Sjohnlev 		 */
2184e4b86885SCheng Sean Ye 		if (!xpv_is_hvm)
2185e4b86885SCheng Sean Ye 			memscrub_init();
2186843e1988Sjohnlev #endif
2187843e1988Sjohnlev 	}
21887c478bd9Sstevel@tonic-gate 
21897aec1d6eScindi 	/*
21907aec1d6eScindi 	 * Complete CPU module initialization
21917aec1d6eScindi 	 */
219220c794b3Sgavinm 	cmi_post_startup();
21937aec1d6eScindi 
21947c478bd9Sstevel@tonic-gate 	/*
219584ab085aSmws 	 * Perform forceloading tasks for /etc/system.
21967c478bd9Sstevel@tonic-gate 	 */
219784ab085aSmws 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	/*
22007c478bd9Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
22017c478bd9Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
22027c478bd9Sstevel@tonic-gate 	 */
22037c478bd9Sstevel@tonic-gate 	(void) modload("fs", "procfs");
22047c478bd9Sstevel@tonic-gate 
2205c35aa225Smarx 	(void) i_ddi_attach_hw_nodes("pit_beep");
2206c35aa225Smarx 
22077c478bd9Sstevel@tonic-gate #if defined(__i386)
22087c478bd9Sstevel@tonic-gate 	/*
22097c478bd9Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
22107c478bd9Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
22117c478bd9Sstevel@tonic-gate 	 */
22127c478bd9Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
22137c478bd9Sstevel@tonic-gate 		halt("No working FP hardware found");
22147c478bd9Sstevel@tonic-gate #endif
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	maxmem = freemem;
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
22197c478bd9Sstevel@tonic-gate }
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate static int
222219397407SSherry Moore pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
22237c478bd9Sstevel@tonic-gate {
222419397407SSherry Moore 	return ((pp->p_pagenum >= btop(low_addr)) &&
222519397407SSherry Moore 	    (pp->p_pagenum < btopr(high_addr)));
22267c478bd9Sstevel@tonic-gate }
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate void
22297c478bd9Sstevel@tonic-gate release_bootstrap(void)
22307c478bd9Sstevel@tonic-gate {
22317c478bd9Sstevel@tonic-gate 	int root_is_ramdisk;
22327c478bd9Sstevel@tonic-gate 	page_t *pp;
22337c478bd9Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
22347c478bd9Sstevel@tonic-gate 	extern dev_t rootdev;
223555d507a9SSeth Goldberg #if !defined(__xpv)
223655d507a9SSeth Goldberg 	pfn_t	pfn;
223755d507a9SSeth Goldberg #endif
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
22407c478bd9Sstevel@tonic-gate 	kobj_boot_unmountroot();
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	/*
22437c478bd9Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
22447c478bd9Sstevel@tonic-gate 	 */
22457c478bd9Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
224619397407SSherry Moore 
2247ae115bc7Smrj 	clear_boot_mappings(0, _userlimit);
224819397407SSherry Moore 
2249ae115bc7Smrj 	postbootkernelbase = kernelbase;
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	/*
22527c478bd9Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
22537c478bd9Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
22547c478bd9Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
22557c478bd9Sstevel@tonic-gate 	 */
22567c478bd9Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
22577c478bd9Sstevel@tonic-gate 	if (!root_is_ramdisk) {
22587c478bd9Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
22597c478bd9Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
22607c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
22617c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
22627c478bd9Sstevel@tonic-gate 	}
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
22657c478bd9Sstevel@tonic-gate 	while (bootpages) {
226619397407SSherry Moore 		extern uint64_t ramdisk_start, ramdisk_end;
22677c478bd9Sstevel@tonic-gate 		pp = bootpages;
22687c478bd9Sstevel@tonic-gate 		bootpages = pp->p_next;
226919397407SSherry Moore 
227019397407SSherry Moore 
227119397407SSherry Moore 		/* Keep pages for the lower 64K */
227219397407SSherry Moore 		if (pp_in_range(pp, 0, 0x40000)) {
227319397407SSherry Moore 			pp->p_next = lower_pages;
227419397407SSherry Moore 			lower_pages = pp;
227519397407SSherry Moore 			lower_pages_count++;
227619397407SSherry Moore 			continue;
227719397407SSherry Moore 		}
227819397407SSherry Moore 
227919397407SSherry Moore 
228019397407SSherry Moore 		if (root_is_ramdisk && pp_in_range(pp, ramdisk_start,
228119397407SSherry Moore 		    ramdisk_end)) {
22827c478bd9Sstevel@tonic-gate 			pp->p_next = rd_pages;
22837c478bd9Sstevel@tonic-gate 			rd_pages = pp;
22847c478bd9Sstevel@tonic-gate 			continue;
22857c478bd9Sstevel@tonic-gate 		}
22867c478bd9Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
2287ae115bc7Smrj 		pp->p_prev = (struct page *)0;
2288ae115bc7Smrj 		PP_CLRBOOTPAGES(pp);
22897c478bd9Sstevel@tonic-gate 		page_free(pp, 1);
22907c478bd9Sstevel@tonic-gate 	}
2291ae115bc7Smrj 	PRM_POINT("Boot pages released");
22927c478bd9Sstevel@tonic-gate 
2293843e1988Sjohnlev #if !defined(__xpv)
2294843e1988Sjohnlev /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
22957c478bd9Sstevel@tonic-gate 	/*
229655d507a9SSeth Goldberg 	 * Find 1 page below 1 MB so that other processors can boot up or
229755d507a9SSeth Goldberg 	 * so that any processor can resume.
22987c478bd9Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
22997c478bd9Sstevel@tonic-gate 	 * We should have just free'd one up.
23007c478bd9Sstevel@tonic-gate 	 */
230119397407SSherry Moore 
230219397407SSherry Moore 	/*
230319397407SSherry Moore 	 * 0x10 pages is 64K.  Leave the bottom 64K alone
230419397407SSherry Moore 	 * for BIOS.
230519397407SSherry Moore 	 */
230619397407SSherry Moore 	for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
230755d507a9SSeth Goldberg 		if (page_numtopp_alloc(pfn) == NULL)
230855d507a9SSeth Goldberg 			continue;
230955d507a9SSeth Goldberg 		rm_platter_va = i86devmap(pfn, 1,
231055d507a9SSeth Goldberg 		    PROT_READ | PROT_WRITE | PROT_EXEC);
231155d507a9SSeth Goldberg 		rm_platter_pa = ptob(pfn);
231255d507a9SSeth Goldberg 		hat_devload(kas.a_hat,
231355d507a9SSeth Goldberg 		    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
231455d507a9SSeth Goldberg 		    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
231555d507a9SSeth Goldberg 		    HAT_LOAD_NOCONSIST);
231655d507a9SSeth Goldberg 		break;
23177c478bd9Sstevel@tonic-gate 	}
231855d507a9SSeth Goldberg 	if (pfn == btop(1*1024*1024) && use_mp)
231955d507a9SSeth Goldberg 		panic("No page below 1M available for starting "
232055d507a9SSeth Goldberg 		    "other processors or for resuming from system-suspend");
2321843e1988Sjohnlev #endif	/* !__xpv */
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate /*
23257c478bd9Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
23267c478bd9Sstevel@tonic-gate  */
23277c478bd9Sstevel@tonic-gate void
23287c478bd9Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
23297c478bd9Sstevel@tonic-gate {
23307c478bd9Sstevel@tonic-gate 	pp->p_pagenum = pnum;
23317c478bd9Sstevel@tonic-gate 	pp->p_mapping = NULL;
23327c478bd9Sstevel@tonic-gate 	pp->p_embed = 0;
23337c478bd9Sstevel@tonic-gate 	pp->p_share = 0;
23347c478bd9Sstevel@tonic-gate 	pp->p_mlentry = 0;
23357c478bd9Sstevel@tonic-gate }
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate /*
23387c478bd9Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
23397c478bd9Sstevel@tonic-gate  */
23407c478bd9Sstevel@tonic-gate static pgcnt_t
23417c478bd9Sstevel@tonic-gate kphysm_init(
2342ae115bc7Smrj 	page_t *pp,
23437c478bd9Sstevel@tonic-gate 	pgcnt_t npages)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	struct memlist	*pmem;
23467c478bd9Sstevel@tonic-gate 	struct memseg	*cur_memseg;
23477c478bd9Sstevel@tonic-gate 	pfn_t		base_pfn;
23487c478bd9Sstevel@tonic-gate 	pgcnt_t		num;
23497c478bd9Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
23507c478bd9Sstevel@tonic-gate 	uint64_t	addr;
23517c478bd9Sstevel@tonic-gate 	uint64_t	size;
23527c478bd9Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
23557c478bd9Sstevel@tonic-gate 
2356ae115bc7Smrj 	cur_memseg = memseg_base;
23577c478bd9Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
23587c478bd9Sstevel@tonic-gate 		/*
23597c478bd9Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
23607c478bd9Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
23617c478bd9Sstevel@tonic-gate 		 */
23627c478bd9Sstevel@tonic-gate 		addr = pmem->address;
23637c478bd9Sstevel@tonic-gate 		size = pmem->size;
23647c478bd9Sstevel@tonic-gate 		if (btop(addr) > physmax)
23657c478bd9Sstevel@tonic-gate 			continue;
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 		/*
23687c478bd9Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
23697c478bd9Sstevel@tonic-gate 		 */
23707c478bd9Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
23717c478bd9Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
23727c478bd9Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
23737c478bd9Sstevel@tonic-gate 			size -= addr - pmem->address;
23747c478bd9Sstevel@tonic-gate 		}
23757c478bd9Sstevel@tonic-gate 
23766bb54764Skchow 		/* only process pages below or equal to physmax */
23776bb54764Skchow 		if ((btop(addr + size) - 1) > physmax)
23786bb54764Skchow 			size = ptob(physmax - btop(addr) + 1);
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 		num = btop(size);
23817c478bd9Sstevel@tonic-gate 		if (num == 0)
23827c478bd9Sstevel@tonic-gate 			continue;
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 		if (num > npages)
23857c478bd9Sstevel@tonic-gate 			num = npages;
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 		npages -= num;
23887c478bd9Sstevel@tonic-gate 		pages_done += num;
23897c478bd9Sstevel@tonic-gate 		base_pfn = btop(addr);
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 		if (prom_debug)
23927c478bd9Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
23937c478bd9Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
23947c478bd9Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 		/*
2397ae115bc7Smrj 		 * Ignore pages below ddiphysmin to simplify ddi memory
23987c478bd9Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
23997c478bd9Sstevel@tonic-gate 		 */
24007c478bd9Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
2401ae115bc7Smrj 			if (base_pfn + num <= ddiphysmin)
24027c478bd9Sstevel@tonic-gate 				continue;
24037c478bd9Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
24047c478bd9Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
24057c478bd9Sstevel@tonic-gate 			base_pfn = ddiphysmin;
24067c478bd9Sstevel@tonic-gate 		}
2407ae115bc7Smrj 
24087c478bd9Sstevel@tonic-gate 		/*
24097c478bd9Sstevel@tonic-gate 		 * Build the memsegs entry
24107c478bd9Sstevel@tonic-gate 		 */
24117c478bd9Sstevel@tonic-gate 		cur_memseg->pages = pp;
24127c478bd9Sstevel@tonic-gate 		cur_memseg->epages = pp + num;
24137c478bd9Sstevel@tonic-gate 		cur_memseg->pages_base = base_pfn;
24147c478bd9Sstevel@tonic-gate 		cur_memseg->pages_end = base_pfn + num;
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 		/*
2417ae115bc7Smrj 		 * Insert into memseg list in decreasing pfn range order.
24187c478bd9Sstevel@tonic-gate 		 * Low memory is typically more fragmented such that this
24197c478bd9Sstevel@tonic-gate 		 * ordering keeps the larger ranges at the front of the list
24207c478bd9Sstevel@tonic-gate 		 * for code that searches memseg.
2421ae115bc7Smrj 		 * This ASSERTS that the memsegs coming in from boot are in
2422ae115bc7Smrj 		 * increasing physical address order and not contiguous.
24237c478bd9Sstevel@tonic-gate 		 */
2424ae115bc7Smrj 		if (memsegs != NULL) {
2425ae115bc7Smrj 			ASSERT(cur_memseg->pages_base >= memsegs->pages_end);
2426ae115bc7Smrj 			cur_memseg->next = memsegs;
24277c478bd9Sstevel@tonic-gate 		}
2428ae115bc7Smrj 		memsegs = cur_memseg;
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 		/*
24317c478bd9Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
24327c478bd9Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
24337c478bd9Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
24347c478bd9Sstevel@tonic-gate 		 * it initializes them.
24357c478bd9Sstevel@tonic-gate 		 */
24367c478bd9Sstevel@tonic-gate 		add_physmem(pp, num, base_pfn);
24377c478bd9Sstevel@tonic-gate 		cur_memseg++;
24387c478bd9Sstevel@tonic-gate 		availrmem_initial += num;
24397c478bd9Sstevel@tonic-gate 		availrmem += num;
24407c478bd9Sstevel@tonic-gate 
2441ae115bc7Smrj 		pp += num;
24427c478bd9Sstevel@tonic-gate 	}
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
24457c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
24467c478bd9Sstevel@tonic-gate 	PRM_DEBUG(freemem);
24477c478bd9Sstevel@tonic-gate 	build_pfn_hash();
24487c478bd9Sstevel@tonic-gate 	return (pages_done);
24497c478bd9Sstevel@tonic-gate }
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate /*
24527c478bd9Sstevel@tonic-gate  * Kernel VM initialization.
24537c478bd9Sstevel@tonic-gate  */
24547c478bd9Sstevel@tonic-gate static void
24557c478bd9Sstevel@tonic-gate kvm_init(void)
24567c478bd9Sstevel@tonic-gate {
24577c478bd9Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	/*
24607c478bd9Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
24617c478bd9Sstevel@tonic-gate 	 */
24627c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
24637c478bd9Sstevel@tonic-gate 	as_avlinit(&kas);
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
24667c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
24697c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
24707c478bd9Sstevel@tonic-gate 
247135b1ab99Sjosephb 	(void) seg_attach(&kas, kernelheap,
247235b1ab99Sjosephb 	    ekernelheap - kernelheap, &kvseg);
24737c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
24747c478bd9Sstevel@tonic-gate 
2475ae115bc7Smrj 	if (core_size > 0) {
2476ae115bc7Smrj 		PRM_POINT("attaching kvseg_core");
2477ae115bc7Smrj 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
2478ae115bc7Smrj 		    &kvseg_core);
2479ae115bc7Smrj 		(void) segkmem_create(&kvseg_core);
2480ae115bc7Smrj 	}
2481ad23a2dbSjohansen 
2482ae115bc7Smrj 	if (segziosize > 0) {
2483ae115bc7Smrj 		PRM_POINT("attaching segzio");
2484ad23a2dbSjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2485ad23a2dbSjohansen 		    &kzioseg);
2486ad23a2dbSjohansen 		(void) segkmem_zio_create(&kzioseg);
2487ad23a2dbSjohansen 
2488ad23a2dbSjohansen 		/* create zio area covering new segment */
2489ad23a2dbSjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2490ad23a2dbSjohansen 	}
24917c478bd9Sstevel@tonic-gate 
2492ae115bc7Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
24937c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 	/*
24987c478bd9Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
24997c478bd9Sstevel@tonic-gate 	 */
2500ae115bc7Smrj 	PRM_POINT("protecting redzone");
25017c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 	/*
25047c478bd9Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
25057c478bd9Sstevel@tonic-gate 	 */
25067c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
25077c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 	/*
25107c478bd9Sstevel@tonic-gate 	 * Make data writable until end.
25117c478bd9Sstevel@tonic-gate 	 */
25127c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
25137c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
25147c478bd9Sstevel@tonic-gate }
25157c478bd9Sstevel@tonic-gate 
2516843e1988Sjohnlev #ifndef __xpv
25177c478bd9Sstevel@tonic-gate /*
25181d03c31eSjohnlev  * Solaris adds an entry for Write Combining caching to the PAT
25197c478bd9Sstevel@tonic-gate  */
25201d03c31eSjohnlev static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate void
25231d03c31eSjohnlev pat_sync(void)
25247c478bd9Sstevel@tonic-gate {
25251d03c31eSjohnlev 	ulong_t	cr0, cr0_orig, cr4;
25267c478bd9Sstevel@tonic-gate 
25271d03c31eSjohnlev 	if (!(x86_feature & X86_PAT))
25287c478bd9Sstevel@tonic-gate 		return;
25291d03c31eSjohnlev 	cr0_orig = cr0 = getcr0();
25301d03c31eSjohnlev 	cr4 = getcr4();
25317c478bd9Sstevel@tonic-gate 
25321d03c31eSjohnlev 	/* disable caching and flush all caches and TLBs */
25331d03c31eSjohnlev 	cr0 |= CR0_CD;
25341d03c31eSjohnlev 	cr0 &= ~CR0_NW;
25351d03c31eSjohnlev 	setcr0(cr0);
25361d03c31eSjohnlev 	invalidate_cache();
25371d03c31eSjohnlev 	if (cr4 & CR4_PGE) {
25381d03c31eSjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
25391d03c31eSjohnlev 		setcr4(cr4);
25401d03c31eSjohnlev 	} else {
25411d03c31eSjohnlev 		reload_cr3();
25427c478bd9Sstevel@tonic-gate 	}
25437c478bd9Sstevel@tonic-gate 
25441d03c31eSjohnlev 	/* add our entry to the PAT */
25451d03c31eSjohnlev 	wrmsr(REG_PAT, pat_attr_reg);
25467c478bd9Sstevel@tonic-gate 
25471d03c31eSjohnlev 	/* flush TLBs and cache again, then reenable cr0 caching */
25481d03c31eSjohnlev 	if (cr4 & CR4_PGE) {
25491d03c31eSjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
25501d03c31eSjohnlev 		setcr4(cr4);
25511d03c31eSjohnlev 	} else {
25521d03c31eSjohnlev 		reload_cr3();
25537c478bd9Sstevel@tonic-gate 	}
25547c478bd9Sstevel@tonic-gate 	invalidate_cache();
25557c478bd9Sstevel@tonic-gate 	setcr0(cr0_orig);
25567c478bd9Sstevel@tonic-gate }
25577c478bd9Sstevel@tonic-gate 
25581d03c31eSjohnlev #endif /* !__xpv */
25597c478bd9Sstevel@tonic-gate 
2560ed5289f9SKen Erickson #if defined(_SOFT_HOSTID)
2561ed5289f9SKen Erickson /*
2562ed5289f9SKen Erickson  * On platforms that do not have a hardware serial number, attempt
2563ed5289f9SKen Erickson  * to set one based on the contents of /etc/hostid.  If this file does
2564ed5289f9SKen Erickson  * not exist, assume that we are to generate a new hostid and set
2565ed5289f9SKen Erickson  * it in the kernel, for subsequent saving by a userland process
2566ed5289f9SKen Erickson  * once the system is up and the root filesystem is mounted r/w.
2567ed5289f9SKen Erickson  *
256896d008c2SKen Erickson  * In order to gracefully support upgrade on OpenSolaris, if
256996d008c2SKen Erickson  * /etc/hostid does not exist, we will attempt to get a serial number
257096d008c2SKen Erickson  * using the legacy method (/kernel/misc/sysinit).
257196d008c2SKen Erickson  *
2572ed5289f9SKen Erickson  * In an attempt to make the hostid less prone to abuse
2573ed5289f9SKen Erickson  * (for license circumvention, etc), we store it in /etc/hostid
2574ed5289f9SKen Erickson  * in rot47 format.
2575ed5289f9SKen Erickson  */
2576ed5289f9SKen Erickson extern volatile unsigned long tenmicrodata;
257796d008c2SKen Erickson static int atoi(char *);
2578ed5289f9SKen Erickson 
2579ed5289f9SKen Erickson static int32_t
2580ed5289f9SKen Erickson set_soft_hostid(void)
2581ed5289f9SKen Erickson {
2582ed5289f9SKen Erickson 	struct _buf *file;
2583ed5289f9SKen Erickson 	char tokbuf[MAXNAMELEN];
2584ed5289f9SKen Erickson 	token_t token;
2585ed5289f9SKen Erickson 	int done = 0;
2586ed5289f9SKen Erickson 	u_longlong_t tmp;
258796d008c2SKen Erickson 	int i;
2588ed5289f9SKen Erickson 	int32_t hostid = -1;
2589ed5289f9SKen Erickson 	unsigned char *c;
2590ed5289f9SKen Erickson 	hrtime_t tsc;
2591ed5289f9SKen Erickson 
2592ed5289f9SKen Erickson 	/*
2593ed5289f9SKen Erickson 	 * If /etc/hostid file not found, we'd like to get a pseudo
2594ed5289f9SKen Erickson 	 * random number to use at the hostid.  A nice way to do this
2595ed5289f9SKen Erickson 	 * is to read the real time clock.  To remain xen-compatible,
2596ed5289f9SKen Erickson 	 * we can't poke the real hardware, so we use tsc_read() to
2597ed5289f9SKen Erickson 	 * read the real time clock.  However, there is an ominous
2598ed5289f9SKen Erickson 	 * warning in tsc_read that says it can return zero, so we
2599ed5289f9SKen Erickson 	 * deal with that possibility by falling back to using the
2600ed5289f9SKen Erickson 	 * (hopefully random enough) value in tenmicrodata.
2601ed5289f9SKen Erickson 	 */
2602ed5289f9SKen Erickson 
2603ed5289f9SKen Erickson 	if ((file = kobj_open_file(hostid_file)) == (struct _buf *)-1) {
260496d008c2SKen Erickson 		/*
260596d008c2SKen Erickson 		 * hostid file not found - try to load sysinit module
260696d008c2SKen Erickson 		 * and see if it has a nonzero hostid value...use that
260796d008c2SKen Erickson 		 * instead of generating a new hostid here if so.
260896d008c2SKen Erickson 		 */
260996d008c2SKen Erickson 		if ((i = modload("misc", "sysinit")) != -1) {
261096d008c2SKen Erickson 			if (strlen(hw_serial) > 0)
261196d008c2SKen Erickson 				hostid = (int32_t)atoi(hw_serial);
261296d008c2SKen Erickson 			(void) modunload(i);
261396d008c2SKen Erickson 		}
261496d008c2SKen Erickson 		if (hostid == -1) {
261596d008c2SKen Erickson 			tsc = tsc_read();
261696d008c2SKen Erickson 			if (tsc == 0)	/* tsc_read can return zero sometimes */
261796d008c2SKen Erickson 				hostid = (int32_t)tenmicrodata & 0x0CFFFFF;
261896d008c2SKen Erickson 			else
261996d008c2SKen Erickson 				hostid = (int32_t)tsc & 0x0CFFFFF;
262096d008c2SKen Erickson 		}
2621ed5289f9SKen Erickson 	} else {
2622ed5289f9SKen Erickson 		/* hostid file found */
2623ed5289f9SKen Erickson 		while (!done) {
2624ed5289f9SKen Erickson 			token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2625ed5289f9SKen Erickson 
2626ed5289f9SKen Erickson 			switch (token) {
2627ed5289f9SKen Erickson 			case POUND:
2628ed5289f9SKen Erickson 				/*
2629ed5289f9SKen Erickson 				 * skip comments
2630ed5289f9SKen Erickson 				 */
2631ed5289f9SKen Erickson 				kobj_find_eol(file);
2632ed5289f9SKen Erickson 				break;
2633ed5289f9SKen Erickson 			case STRING:
2634ed5289f9SKen Erickson 				/*
2635ed5289f9SKen Erickson 				 * un-rot47 - obviously this
2636ed5289f9SKen Erickson 				 * nonsense is ascii-specific
2637ed5289f9SKen Erickson 				 */
2638ed5289f9SKen Erickson 				for (c = (unsigned char *)tokbuf;
2639ed5289f9SKen Erickson 				    *c != '\0'; c++) {
2640ed5289f9SKen Erickson 					*c += 47;
2641ed5289f9SKen Erickson 					if (*c > '~')
2642ed5289f9SKen Erickson 						*c -= 94;
2643ed5289f9SKen Erickson 					else if (*c < '!')
2644ed5289f9SKen Erickson 						*c += 94;
2645ed5289f9SKen Erickson 				}
2646ed5289f9SKen Erickson 				/*
2647ed5289f9SKen Erickson 				 * now we should have a real number
2648ed5289f9SKen Erickson 				 */
2649ed5289f9SKen Erickson 
2650ed5289f9SKen Erickson 				if (kobj_getvalue(tokbuf, &tmp) != 0)
2651ed5289f9SKen Erickson 					kobj_file_err(CE_WARN, file,
2652ed5289f9SKen Erickson 					    "Bad value %s for hostid",
2653ed5289f9SKen Erickson 					    tokbuf);
2654ed5289f9SKen Erickson 				else
2655ed5289f9SKen Erickson 					hostid = (int32_t)tmp;
2656ed5289f9SKen Erickson 
2657ed5289f9SKen Erickson 				break;
2658ed5289f9SKen Erickson 			case EOF:
2659ed5289f9SKen Erickson 				done = 1;
2660ed5289f9SKen Erickson 				/* FALLTHROUGH */
2661ed5289f9SKen Erickson 			case NEWLINE:
2662ed5289f9SKen Erickson 				kobj_newline(file);
2663ed5289f9SKen Erickson 				break;
2664ed5289f9SKen Erickson 			default:
2665ed5289f9SKen Erickson 				break;
2666ed5289f9SKen Erickson 
2667ed5289f9SKen Erickson 			}
2668ed5289f9SKen Erickson 		}
2669ed5289f9SKen Erickson 		if (hostid == -1) /* didn't find a hostid string */
2670ed5289f9SKen Erickson 			kobj_file_err(CE_WARN, file,
2671ed5289f9SKen Erickson 			    "hostid missing or corrupt");
2672ed5289f9SKen Erickson 
2673ed5289f9SKen Erickson 		kobj_close_file(file);
2674ed5289f9SKen Erickson 	}
2675ed5289f9SKen Erickson 	/*
2676ed5289f9SKen Erickson 	 * hostid is now the value read from /etc/hostid, or the
2677ed5289f9SKen Erickson 	 * new hostid we generated in this routine or -1 if not set.
2678ed5289f9SKen Erickson 	 */
2679ed5289f9SKen Erickson 	return (hostid);
2680ed5289f9SKen Erickson }
268196d008c2SKen Erickson 
268296d008c2SKen Erickson static int
268396d008c2SKen Erickson atoi(char *p)
268496d008c2SKen Erickson {
268596d008c2SKen Erickson 	int i = 0;
268696d008c2SKen Erickson 
268796d008c2SKen Erickson 	while (*p != '\0')
268896d008c2SKen Erickson 		i = 10 * i + (*p++ - '0');
268996d008c2SKen Erickson 
269096d008c2SKen Erickson 	return (i);
269196d008c2SKen Erickson }
269296d008c2SKen Erickson 
2693ed5289f9SKen Erickson #endif /* _SOFT_HOSTID */
2694ed5289f9SKen Erickson 
26957c478bd9Sstevel@tonic-gate void
269645916cd2Sjpk get_system_configuration(void)
26977c478bd9Sstevel@tonic-gate {
26987c478bd9Sstevel@tonic-gate 	char	prop[32];
26997c478bd9Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
27007c478bd9Sstevel@tonic-gate 
270135b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
270235b1ab99Sjosephb 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
270335b1ab99Sjosephb 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
270435b1ab99Sjosephb 	    nodes_ll > MAXNODES ||
270535b1ab99Sjosephb 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
270635b1ab99Sjosephb 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
270735b1ab99Sjosephb 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
27087c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
27097c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
27107c478bd9Sstevel@tonic-gate 	} else {
27117c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
27127c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
27137c478bd9Sstevel@tonic-gate 	}
271435b1ab99Sjosephb 
271535b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
271635b1ab99Sjosephb 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
271735b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
271835b1ab99Sjosephb 		eprom_kernelbase = NULL;
27197c478bd9Sstevel@tonic-gate 	else
272035b1ab99Sjosephb 		eprom_kernelbase = (uintptr_t)lvalue;
27217c478bd9Sstevel@tonic-gate 
272235b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
272335b1ab99Sjosephb 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
272435b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
27257c478bd9Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
272635b1ab99Sjosephb 	else
27277c478bd9Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
27287c478bd9Sstevel@tonic-gate 
272935b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
273035b1ab99Sjosephb 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
273135b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
27327c478bd9Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
273335b1ab99Sjosephb 	else
27347c478bd9Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
27354944b02eSkchow 
2736aac11643Sjosephb 	/* physmem used to be here, but moved much earlier to fakebop.c */
27377c478bd9Sstevel@tonic-gate }
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate /*
27407c478bd9Sstevel@tonic-gate  * Add to a memory list.
27417c478bd9Sstevel@tonic-gate  * start = start of new memory segment
27427c478bd9Sstevel@tonic-gate  * len = length of new memory segment in bytes
27437c478bd9Sstevel@tonic-gate  * new = pointer to a new struct memlist
27447c478bd9Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
27457c478bd9Sstevel@tonic-gate  */
2746ae115bc7Smrj void
27477c478bd9Sstevel@tonic-gate memlist_add(
27487c478bd9Sstevel@tonic-gate 	uint64_t start,
27497c478bd9Sstevel@tonic-gate 	uint64_t len,
27507c478bd9Sstevel@tonic-gate 	struct memlist *new,
27517c478bd9Sstevel@tonic-gate 	struct memlist **memlistp)
27527c478bd9Sstevel@tonic-gate {
27537c478bd9Sstevel@tonic-gate 	struct memlist *cur;
27547c478bd9Sstevel@tonic-gate 	uint64_t end = start + len;
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	new->address = start;
27577c478bd9Sstevel@tonic-gate 	new->size = len;
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 	cur = *memlistp;
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 	while (cur) {
27627c478bd9Sstevel@tonic-gate 		if (cur->address >= end) {
27637c478bd9Sstevel@tonic-gate 			new->next = cur;
27647c478bd9Sstevel@tonic-gate 			*memlistp = new;
27657c478bd9Sstevel@tonic-gate 			new->prev = cur->prev;
27667c478bd9Sstevel@tonic-gate 			cur->prev = new;
27677c478bd9Sstevel@tonic-gate 			return;
27687c478bd9Sstevel@tonic-gate 		}
27697c478bd9Sstevel@tonic-gate 		ASSERT(cur->address + cur->size <= start);
27707c478bd9Sstevel@tonic-gate 		if (cur->next == NULL) {
27717c478bd9Sstevel@tonic-gate 			cur->next = new;
27727c478bd9Sstevel@tonic-gate 			new->prev = cur;
27737c478bd9Sstevel@tonic-gate 			new->next = NULL;
27747c478bd9Sstevel@tonic-gate 			return;
27757c478bd9Sstevel@tonic-gate 		}
27767c478bd9Sstevel@tonic-gate 		memlistp = &cur->next;
27777c478bd9Sstevel@tonic-gate 		cur = cur->next;
27787c478bd9Sstevel@tonic-gate 	}
27797c478bd9Sstevel@tonic-gate }
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate void
27827c478bd9Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
27837c478bd9Sstevel@tonic-gate {
27847c478bd9Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
27857c478bd9Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
27887c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
27897c478bd9Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
27907c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
27917c478bd9Sstevel@tonic-gate }
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate caddr_t
27947c478bd9Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
27957c478bd9Sstevel@tonic-gate {
27967c478bd9Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
27977c478bd9Sstevel@tonic-gate }
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28007c478bd9Sstevel@tonic-gate caddr_t
28017c478bd9Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
28027c478bd9Sstevel@tonic-gate {
28037c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
28047c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
28057c478bd9Sstevel@tonic-gate 	return (0);
28067c478bd9Sstevel@tonic-gate }
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28097c478bd9Sstevel@tonic-gate void
28107c478bd9Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
28117c478bd9Sstevel@tonic-gate {
28127c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
28137c478bd9Sstevel@tonic-gate }
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate /*
28167c478bd9Sstevel@tonic-gate  * This is called just after configure() in startup().
28177c478bd9Sstevel@tonic-gate  *
28187c478bd9Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
28197c478bd9Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
28207c478bd9Sstevel@tonic-gate  * given that various parts of the instruction set may appear
28217c478bd9Sstevel@tonic-gate  * and disappear between different implementations.
28227c478bd9Sstevel@tonic-gate  *
28237c478bd9Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
28247c478bd9Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
28257c478bd9Sstevel@tonic-gate  * more flexibility.
28267c478bd9Sstevel@tonic-gate  *
28277c478bd9Sstevel@tonic-gate  * So, we just leave this alone.
28287c478bd9Sstevel@tonic-gate  */
28297c478bd9Sstevel@tonic-gate void
28307c478bd9Sstevel@tonic-gate setx86isalist(void)
28317c478bd9Sstevel@tonic-gate {
28327c478bd9Sstevel@tonic-gate 	char *tp;
28337c478bd9Sstevel@tonic-gate 	size_t len;
28347c478bd9Sstevel@tonic-gate 	extern char *isa_list;
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate #define	TBUFSIZE	1024
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
28397c478bd9Sstevel@tonic-gate 	*tp = '\0';
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate #if defined(__amd64)
28427c478bd9Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
28437c478bd9Sstevel@tonic-gate #endif
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 	switch (x86_vendor) {
28467c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
28477c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
28487c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
28497c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
28507c478bd9Sstevel@tonic-gate 			/*
28517c478bd9Sstevel@tonic-gate 			 * Pentium Pro or later
28527c478bd9Sstevel@tonic-gate 			 */
28537c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
28547c478bd9Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
28557c478bd9Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
28567c478bd9Sstevel@tonic-gate 		}
28577c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
28587c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
28597c478bd9Sstevel@tonic-gate 		/*
28607c478bd9Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
28617c478bd9Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
28627c478bd9Sstevel@tonic-gate 		 */
28637c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
28647c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
28657c478bd9Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
28667c478bd9Sstevel@tonic-gate 			    "+mmx pentium " : " ");
28677c478bd9Sstevel@tonic-gate 		}
28687c478bd9Sstevel@tonic-gate 		break;
28697c478bd9Sstevel@tonic-gate 	default:
28707c478bd9Sstevel@tonic-gate 		break;
28717c478bd9Sstevel@tonic-gate 	}
28727c478bd9Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
28737c478bd9Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
28747c478bd9Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
28757c478bd9Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate #undef TBUFSIZE
28787c478bd9Sstevel@tonic-gate }
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate #ifdef __amd64
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate void *
28847c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
28857c478bd9Sstevel@tonic-gate {
28867c478bd9Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
28877c478bd9Sstevel@tonic-gate }
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate void
28907c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
28917c478bd9Sstevel@tonic-gate {
28927c478bd9Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
28937c478bd9Sstevel@tonic-gate }
28947c478bd9Sstevel@tonic-gate 
2895ae115bc7Smrj #else /* __i386 */
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate void *
28987c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
28997c478bd9Sstevel@tonic-gate {
29007c478bd9Sstevel@tonic-gate 	caddr_t	vaddr;
29017c478bd9Sstevel@tonic-gate 	uintptr_t v;
29027c478bd9Sstevel@tonic-gate 	size_t	start;
29037c478bd9Sstevel@tonic-gate 	size_t	end;
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
29067c478bd9Sstevel@tonic-gate 	if (vaddr == NULL)
29077c478bd9Sstevel@tonic-gate 		return (NULL);
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
29107c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
2911ae115bc7Smrj 	ASSERT(v + size <= valloc_base);
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
29147c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
29157c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
29167c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	while (start <= end) {
29197c478bd9Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
29207c478bd9Sstevel@tonic-gate 		++start;
29217c478bd9Sstevel@tonic-gate 	}
29227c478bd9Sstevel@tonic-gate 	return (vaddr);
29237c478bd9Sstevel@tonic-gate }
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate void
29267c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
29277c478bd9Sstevel@tonic-gate {
29287c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
29297c478bd9Sstevel@tonic-gate 	size_t	start;
29307c478bd9Sstevel@tonic-gate 	size_t	end;
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
2933ae115bc7Smrj 	ASSERT(v + size <= valloc_base);
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
29367c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
29377c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
29387c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	while (start <= end) {
29417c478bd9Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
29427c478bd9Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
29437c478bd9Sstevel@tonic-gate 		++start;
29447c478bd9Sstevel@tonic-gate 	}
29457c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
29467c478bd9Sstevel@tonic-gate }
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate /*
29497c478bd9Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
29507c478bd9Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
29517c478bd9Sstevel@tonic-gate  */
29527c478bd9Sstevel@tonic-gate void *
29537c478bd9Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
29547c478bd9Sstevel@tonic-gate {
29557c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
29567c478bd9Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
29577c478bd9Sstevel@tonic-gate 	size_t start;
29587c478bd9Sstevel@tonic-gate 	size_t end;
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 	/*
29617c478bd9Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
29627c478bd9Sstevel@tonic-gate 	 */
29637c478bd9Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
29647c478bd9Sstevel@tonic-gate 		return (NULL);
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 	/*
29677c478bd9Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
29687c478bd9Sstevel@tonic-gate 	 */
2969ae115bc7Smrj 	if (v >= valloc_base || eaddr < kernelbase)
29707c478bd9Sstevel@tonic-gate 		return (NULL);
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 	/*
29737c478bd9Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
29747c478bd9Sstevel@tonic-gate 	 */
29757c478bd9Sstevel@tonic-gate 	if (v < kernelbase)
29767c478bd9Sstevel@tonic-gate 		v = kernelbase;
29777c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
29787c478bd9Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
29797c478bd9Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
29807c478bd9Sstevel@tonic-gate 		end = toxic_bit_map_len;
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
29837c478bd9Sstevel@tonic-gate 		return (NULL);
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 	v = kernelbase + ptob(start);
29867c478bd9Sstevel@tonic-gate 	if (len != NULL)
29877c478bd9Sstevel@tonic-gate 		*len = ptob(end - start);
29887c478bd9Sstevel@tonic-gate 	return ((void *)v);
29897c478bd9Sstevel@tonic-gate }
29907c478bd9Sstevel@tonic-gate 
2991ae115bc7Smrj #endif	/* __i386 */
2992