xref: /illumos-gate/usr/src/uts/i86pc/os/startup.c (revision fdcca78f421670bb8f04d68bc3f2762a4b5342be)
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  */
21d6694327SYuri Pankov 
227c478bd9Sstevel@tonic-gate /*
230db3240dSStephen Hanson  * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
249e3e4df2SGarrett D'Amore  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
25d6694327SYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
2674ecdb51SJohn Levon  * Copyright (c) 2018 Joyent, Inc.
27af868f46SMatthew Ahrens  * Copyright (c) 2015 by Delphix. All rights reserved.
28*fdcca78fSJoshua M. Clulow  * Copyright 2020 Oxide Computer Company
297c478bd9Sstevel@tonic-gate  */
30a3114836SGerry Liu /*
31a3114836SGerry Liu  * Copyright (c) 2010, Intel Corporation.
32a3114836SGerry Liu  * All rights reserved.
33a3114836SGerry Liu  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
377c478bd9Sstevel@tonic-gate #include <sys/param.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/signal.h>
407c478bd9Sstevel@tonic-gate #include <sys/systm.h>
417c478bd9Sstevel@tonic-gate #include <sys/user.h>
427c478bd9Sstevel@tonic-gate #include <sys/mman.h>
437c478bd9Sstevel@tonic-gate #include <sys/vm.h>
447c478bd9Sstevel@tonic-gate #include <sys/conf.h>
457c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
467c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
477c478bd9Sstevel@tonic-gate #include <sys/disp.h>
487c478bd9Sstevel@tonic-gate #include <sys/class.h>
497c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <sys/proc.h>
547c478bd9Sstevel@tonic-gate #include <sys/buf.h>
557c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
56ae115bc7Smrj #include <sys/mem.h>
577c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <sys/cred.h>
627c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
637c478bd9Sstevel@tonic-gate #include <sys/file.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
687c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
697c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
707c478bd9Sstevel@tonic-gate #include <sys/debug.h>
717c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
747c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
75a3114836SGerry Liu #include <sys/memlist_plat.h>
767c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
777c478bd9Sstevel@tonic-gate #include <sys/promif.h>
78*fdcca78fSJoshua M. Clulow #include <sys/prom_debug.h>
79843e1988Sjohnlev #include <sys/modctl.h>
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
827c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
837c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
847c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
857c478bd9Sstevel@tonic-gate #include <sys/psw.h>
867c478bd9Sstevel@tonic-gate #include <sys/regset.h>
877c478bd9Sstevel@tonic-gate #include <sys/clock.h>
887c478bd9Sstevel@tonic-gate #include <sys/pte.h>
897c478bd9Sstevel@tonic-gate #include <sys/tss.h>
907c478bd9Sstevel@tonic-gate #include <sys/stack.h>
917c478bd9Sstevel@tonic-gate #include <sys/trap.h>
927c478bd9Sstevel@tonic-gate #include <sys/fp.h>
931d03c31eSjohnlev #include <vm/kboot_mmu.h>
947c478bd9Sstevel@tonic-gate #include <vm/anon.h>
957c478bd9Sstevel@tonic-gate #include <vm/as.h>
967c478bd9Sstevel@tonic-gate #include <vm/page.h>
977c478bd9Sstevel@tonic-gate #include <vm/seg.h>
987c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
997c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
1007c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
1017c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
1027c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
1037c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
1047c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
1057c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
1067c478bd9Sstevel@tonic-gate #include <sys/thread.h>
1077c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
1087c478bd9Sstevel@tonic-gate #include <sys/vm_machparam.h>
1097c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
1107c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
1117c478bd9Sstevel@tonic-gate #include <vm/hat.h>
1127c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
1137c478bd9Sstevel@tonic-gate #include <sys/pmem.h>
1147c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1157c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
116a563a037Sbholler #include <sys/cpuvar.h>
1177c478bd9Sstevel@tonic-gate #include <sys/segments.h>
1187c478bd9Sstevel@tonic-gate #include <sys/clconf.h>
1197c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
1207c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
1217c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
1227aec1d6eScindi #include <sys/cpu_module.h>
12384ab085aSmws #include <sys/smbios.h>
124ae115bc7Smrj #include <sys/debug_info.h>
1251d03c31eSjohnlev #include <sys/bootinfo.h>
126a288e5a9SJoshua M. Clulow #include <sys/ddi_periodic.h>
1275679c89fSjv #include <sys/systeminfo.h>
12819397407SSherry Moore #include <sys/multiboot.h>
1290181461bSKeith M Wesolowski #include <sys/ramdisk.h>
130ae115bc7Smrj 
131a3114836SGerry Liu #ifdef	__xpv
1321d03c31eSjohnlev 
133843e1988Sjohnlev #include <sys/hypervisor.h>
134843e1988Sjohnlev #include <sys/xen_mmu.h>
135843e1988Sjohnlev #include <sys/evtchn_impl.h>
136843e1988Sjohnlev #include <sys/gnttab.h>
137843e1988Sjohnlev #include <sys/xpv_panic.h>
138843e1988Sjohnlev #include <xen/sys/xenbus_comms.h>
139843e1988Sjohnlev #include <xen/public/physdev.h>
1401d03c31eSjohnlev 
141843e1988Sjohnlev extern void xen_late_startup(void);
142ae115bc7Smrj 
1431d03c31eSjohnlev struct xen_evt_data cpu0_evt_data;
1441d03c31eSjohnlev 
145a3114836SGerry Liu #else	/* __xpv */
146a3114836SGerry Liu #include <sys/memlist_impl.h>
147a3114836SGerry Liu 
148a3114836SGerry Liu extern void mem_config_init(void);
1491d03c31eSjohnlev #endif /* __xpv */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate extern void progressbar_init(void);
1529acbbeafSnn extern void brand_init(void);
15306fb6a36Sdv extern void pcf_init(void);
1540e751525SEric Saxe extern void pg_init(void);
1556a59053bSAlex Wilson extern void ssp_init(void);
1567c478bd9Sstevel@tonic-gate 
157d7d93655Sblakej extern int size_pse_array(pgcnt_t, int);
158d7d93655Sblakej 
159ed5289f9SKen Erickson #if defined(_SOFT_HOSTID)
160ed5289f9SKen Erickson 
161ed5289f9SKen Erickson #include <sys/rtc.h>
162ed5289f9SKen Erickson 
163ed5289f9SKen Erickson static int32_t set_soft_hostid(void);
164ed5289f9SKen Erickson static char hostid_file[] = "/etc/hostid";
165ed5289f9SKen Erickson 
166ed5289f9SKen Erickson #endif
167ed5289f9SKen Erickson 
16894f1124eSVikram Hegde void *gfx_devinfo_list;
169ed5289f9SKen Erickson 
1703a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
1713a634bfcSVikram Hegde extern void immu_startup(void);
1723a634bfcSVikram Hegde #endif
1733a634bfcSVikram Hegde 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1767c478bd9Sstevel@tonic-gate  * interface.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * segkp
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate extern int segkp_fromheap;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate static void kvm_init(void);
1867c478bd9Sstevel@tonic-gate static void startup_init(void);
1877c478bd9Sstevel@tonic-gate static void startup_memlist(void);
188ae115bc7Smrj static void startup_kmem(void);
1897c478bd9Sstevel@tonic-gate static void startup_modules(void);
1907c478bd9Sstevel@tonic-gate static void startup_vm(void);
1917c478bd9Sstevel@tonic-gate static void startup_end(void);
19235b1ab99Sjosephb static void layout_kernel_va(void);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1967c478bd9Sstevel@tonic-gate  */
197f53ad214Skchow #ifdef __i386
19835b1ab99Sjosephb 
199f53ad214Skchow /*
200f53ad214Skchow  * Due to virtual address space limitations running in 32 bit mode, restrict
20135b1ab99Sjosephb  * the amount of physical memory configured to a max of PHYSMEM pages (16g).
202f53ad214Skchow  *
203f53ad214Skchow  * If the physical max memory size of 64g were allowed to be configured, the
204f53ad214Skchow  * size of user virtual address space will be less than 1g. A limited user
205f53ad214Skchow  * address space greatly reduces the range of applications that can run.
206f53ad214Skchow  *
20735b1ab99Sjosephb  * If more physical memory than PHYSMEM is required, users should preferably
20835b1ab99Sjosephb  * run in 64 bit mode which has far looser virtual address space limitations.
209f53ad214Skchow  *
210f53ad214Skchow  * If 64 bit mode is not available (as in IA32) and/or more physical memory
21135b1ab99Sjosephb  * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
212f53ad214Skchow  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
213f53ad214Skchow  * should also be carefully tuned to balance out the need of the user
214f53ad214Skchow  * application while minimizing the risk of kernel heap exhaustion due to
215f53ad214Skchow  * kernelbase being set too high.
216f53ad214Skchow  */
21735b1ab99Sjosephb #define	PHYSMEM	0x400000
218f53ad214Skchow 
21935b1ab99Sjosephb #else /* __amd64 */
22035b1ab99Sjosephb 
22135b1ab99Sjosephb /*
22235b1ab99Sjosephb  * For now we can handle memory with physical addresses up to about
22335b1ab99Sjosephb  * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
22435b1ab99Sjosephb  * half the VA space for seg_kpm. When systems get bigger than 64TB this
22535b1ab99Sjosephb  * code will need revisiting. There is an implicit assumption that there
22635b1ab99Sjosephb  * are no *huge* holes in the physical address space too.
22735b1ab99Sjosephb  */
22835b1ab99Sjosephb #define	TERABYTE		(1ul << 40)
22935b1ab99Sjosephb #define	PHYSMEM_MAX64		mmu_btop(64 * TERABYTE)
23035b1ab99Sjosephb #define	PHYSMEM			PHYSMEM_MAX64
23135b1ab99Sjosephb #define	AMD64_VA_HOLE_END	0xFFFF800000000000ul
23235b1ab99Sjosephb 
23335b1ab99Sjosephb #endif /* __amd64 */
23435b1ab99Sjosephb 
23535b1ab99Sjosephb pgcnt_t physmem = PHYSMEM;
2367c478bd9Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate char *kobj_file_buf;
2397c478bd9Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
24219397407SSherry Moore caddr_t	rm_platter_va = 0;
2437c478bd9Sstevel@tonic-gate uint32_t rm_platter_pa;
2447c478bd9Sstevel@tonic-gate 
24583f9b804Skchow int	auto_lpg_disable = 1;
24683f9b804Skchow 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate uintptr_t hole_start, hole_end;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * kpm mapping window
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate caddr_t kpm_vbase;
2567c478bd9Sstevel@tonic-gate size_t  kpm_size;
25735b1ab99Sjosephb static int kpm_desired;
25835b1ab99Sjosephb #ifdef __amd64
25935b1ab99Sjosephb static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
26035b1ab99Sjosephb #endif
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * Configuration parameters set at boot time.
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
2697c478bd9Sstevel@tonic-gate struct bootops		**bootopsp;
2707c478bd9Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate char bootblock_fstype[16];
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
27519397407SSherry Moore char kern_bootfile[OBP_MAXPATHLEN];
2767c478bd9Sstevel@tonic-gate 
277ad23a2dbSjohansen /*
278ad23a2dbSjohansen  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
279ad23a2dbSjohansen  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
280ad23a2dbSjohansen  * zio buffers from their own segment, otherwise they are allocated from the
281ad23a2dbSjohansen  * heap.  The optimization of allocating zio buffers from their own segment is
282ad23a2dbSjohansen  * only valid on 64-bit kernels.
283ad23a2dbSjohansen  */
284ad23a2dbSjohansen #if defined(__amd64)
285ad23a2dbSjohansen int segzio_fromheap = 0;
286ad23a2dbSjohansen #else
287ad23a2dbSjohansen int segzio_fromheap = 1;
288ad23a2dbSjohansen #endif
289ad23a2dbSjohansen 
2903ce2fcdcSRobert Mustacchi /*
2913ce2fcdcSRobert Mustacchi  * Give folks an escape hatch for disabling SMAP via kmdb. Doesn't work
2923ce2fcdcSRobert Mustacchi  * post-boot.
2933ce2fcdcSRobert Mustacchi  */
2943ce2fcdcSRobert Mustacchi int disable_smap = 0;
2953ce2fcdcSRobert Mustacchi 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
2987c478bd9Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2997c478bd9Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * VM data structures
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
307cb15d5d9SPeter Rival unsigned int page_hashsz_shift;	/* log2(page_hashsz) */
3087c478bd9Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
3097c478bd9Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
310d7d93655Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
311d7d93655Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
312d7d93655Sblakej int pse_shift;			/* log2(pse_table_size) */
3137c478bd9Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
3147c478bd9Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
3157c478bd9Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
3167c478bd9Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
3177c478bd9Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
320ae115bc7Smrj static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
321ae115bc7Smrj 
3227c478bd9Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
3257c478bd9Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
3267c478bd9Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
329ad23a2dbSjohansen caddr_t segzio_base;		/* Base address of segzio */
3307c478bd9Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
33104909c8cSJohn Levon caddr_t segkvmm_base;
33204909c8cSJohn Levon pgcnt_t segkvmmsize;
33304909c8cSJohn Levon pgcnt_t segziosize;
3347c478bd9Sstevel@tonic-gate 
335a3114836SGerry Liu /*
336a3114836SGerry Liu  * A static DR page_t VA map is reserved that can map the page structures
337a3114836SGerry Liu  * for a domain's entire RA space. The pages that back this space are
338a3114836SGerry Liu  * dynamically allocated and need not be physically contiguous.  The DR
339a3114836SGerry Liu  * map size is derived from KPM size.
340a3114836SGerry Liu  * This mechanism isn't used by x86 yet, so just stubs here.
341a3114836SGerry Liu  */
342a3114836SGerry Liu int ppvm_enable = 0;		/* Static virtual map for page structs */
343a3114836SGerry Liu page_t *ppvm_base = NULL;	/* Base of page struct map */
344a3114836SGerry Liu pgcnt_t ppvm_size = 0;		/* Size of page struct map */
345a3114836SGerry Liu 
346ae115bc7Smrj /*
347ae115bc7Smrj  * VA range available to the debugger
348ae115bc7Smrj  */
349ae115bc7Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
350ae115bc7Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
351ae115bc7Smrj 
3527c478bd9Sstevel@tonic-gate struct memseg *memseg_base;
3537c478bd9Sstevel@tonic-gate struct vnode unused_pages_vp;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate #define	FOURGB	0x100000000LL
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate struct memlist *memlist;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
3607c478bd9Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
3617c478bd9Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
3627c478bd9Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
3637c478bd9Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
3647c478bd9Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
3657c478bd9Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
3667c478bd9Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
3697c478bd9Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
3701de082f7SVikram Hegde struct memlist *bios_rsvd;	/* Bios reserved memory */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
3747c478bd9Sstevel@tonic-gate  */
375ae115bc7Smrj static pgcnt_t kphysm_init(page_t *, pgcnt_t);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate  * a couple useful roundup macros
3817c478bd9Sstevel@tonic-gate  */
3827c478bd9Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
3837c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
3847c478bd9Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
3857c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
3867c478bd9Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
387ae115bc7Smrj 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
3887c478bd9Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
3897c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
3937c478bd9Sstevel@tonic-gate  *		+-----------------------+
394ae115bc7Smrj  *		|			|
3957c478bd9Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
3967c478bd9Sstevel@tonic-gate  *		|	debugger	|
3977c478bd9Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
3987c478bd9Sstevel@tonic-gate  *		|      Kernel Data	|
3997c478bd9Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
4007c478bd9Sstevel@tonic-gate  *              |      Kernel Text	|
401843e1988Sjohnlev  * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
402ae115bc7Smrj  *		|---       GDT       ---|- GDT page (GDT_VA)
403ae115bc7Smrj  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
404ae115bc7Smrj  *		|			|
405af868f46SMatthew Ahrens  *		|   page_t structures	|
406af868f46SMatthew Ahrens  *		|   memsegs, memlists,	|
407af868f46SMatthew Ahrens  *		|   page hash, etc.	|
408ae115bc7Smrj  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
409ae115bc7Smrj  *		|			|  (segkp is just an arena in the heap)
4107c478bd9Sstevel@tonic-gate  *		|			|
4117c478bd9Sstevel@tonic-gate  *		|	kvseg		|
4127c478bd9Sstevel@tonic-gate  *		|			|
4137c478bd9Sstevel@tonic-gate  *		|			|
4147c478bd9Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
415af868f46SMatthew Ahrens  *		|        Segkmap	|
416ae115bc7Smrj  * 0xC3002000  -|-----------------------|- segmap_start (floating)
4177c478bd9Sstevel@tonic-gate  *		|	Red Zone	|
4187c478bd9Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
4197c478bd9Sstevel@tonic-gate  *		|			|			||
4207c478bd9Sstevel@tonic-gate  *		|     Shared objects	|			\/
4217c478bd9Sstevel@tonic-gate  *		|			|
4227c478bd9Sstevel@tonic-gate  *		:			:
4237c478bd9Sstevel@tonic-gate  *		|	user data	|
4247c478bd9Sstevel@tonic-gate  *		|-----------------------|
4257c478bd9Sstevel@tonic-gate  *		|	user text	|
4267c478bd9Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
4277c478bd9Sstevel@tonic-gate  *		|	user stack	|
4287c478bd9Sstevel@tonic-gate  *		:			:
4297c478bd9Sstevel@tonic-gate  *		|	invalid		|
4307c478bd9Sstevel@tonic-gate  * 0x00000000	+-----------------------+
4317c478bd9Sstevel@tonic-gate  *
4327c478bd9Sstevel@tonic-gate  *
4337c478bd9Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
4347c478bd9Sstevel@tonic-gate  *			+-----------------------+
435ae115bc7Smrj  *			|			|
4367c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
4377c478bd9Sstevel@tonic-gate  *			|	debugger (?)	|
4387c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
439af868f46SMatthew Ahrens  *			|      unused		|
4407c478bd9Sstevel@tonic-gate  *			+-----------------------+
4417c478bd9Sstevel@tonic-gate  *			|      Kernel Data	|
4427c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
4437c478bd9Sstevel@tonic-gate  *			|      Kernel Text	|
4447c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
445ae115bc7Smrj  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
44674ecdb51SJohn Levon  *			|---       GDT       ---|- GDT page (GDT_VA)
44774ecdb51SJohn Levon  *			|---       IDT       ---|- IDT page (IDT_VA)
44874ecdb51SJohn Levon  *			|---       LDT       ---|- LDT pages (LDT_VA)
449ae115bc7Smrj  *			|			|
450af868f46SMatthew Ahrens  *			|      Core heap	| (used for loadable modules)
4517c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
4527c478bd9Sstevel@tonic-gate  *			|	 Kernel		|
4537c478bd9Sstevel@tonic-gate  *			|	  heap		|
45404909c8cSJohn Levon  *			|			|
45504909c8cSJohn Levon  *			|			|
4567c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
457ae115bc7Smrj  *			|	 segmap		|
458ae115bc7Smrj  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
4597c478bd9Sstevel@tonic-gate  *			|    device mappings	|
4607c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
46104909c8cSJohn Levon  *			|	 segzio		|
462ad23a2dbSjohansen  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
46304909c8cSJohn Levon  *			|        segkvmm	|
46404909c8cSJohn Levon  *			|			|
46504909c8cSJohn Levon  *			|			|
46604909c8cSJohn Levon  *			|			|
46704909c8cSJohn Levon  * 0xFFFFFXXX.XXX00000  |-----------------------|- segkvmm_base (floating)
46804909c8cSJohn Levon  *			|	 segkp		|
469*fdcca78fSJoshua M. Clulow  *			|-----------------------|- segkp_base (floating)
470af868f46SMatthew Ahrens  *			|   page_t structures	|  valloc_base + valloc_sz
471af868f46SMatthew Ahrens  *			|   memsegs, memlists,	|
472af868f46SMatthew Ahrens  *			|   page hash, etc.	|
47304909c8cSJohn Levon  * 0xFFFFFE00.00000000  |-----------------------|- valloc_base (lower if >256GB)
4747c478bd9Sstevel@tonic-gate  *			|	 segkpm		|
47504909c8cSJohn Levon  *			|			|
47604909c8cSJohn Levon  * 0xFFFFFD00.00000000  |-----------------------|- SEGKPM_BASE (lower if >256GB)
4777c478bd9Sstevel@tonic-gate  *			|	Red Zone	|
47804909c8cSJohn Levon  * 0xFFFFFC80.00000000  |-----------------------|- KERNELBASE (lower if >256GB)
47904909c8cSJohn Levon  * 0xFFFFFC7F.FFE00000  |-----------------------|- USERLIMIT (lower if >256GB)
4807c478bd9Sstevel@tonic-gate  *			|     User stack	|- User space memory
481af868f46SMatthew Ahrens  *			|			|
482af868f46SMatthew Ahrens  *			| shared objects, etc	|	(grows downwards)
4837c478bd9Sstevel@tonic-gate  *			:			:
484af868f46SMatthew Ahrens  *			|			|
4857c478bd9Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
486af868f46SMatthew Ahrens  *			|			|
487af868f46SMatthew Ahrens  *			| VA Hole / unused	|
488af868f46SMatthew Ahrens  *			|			|
4897c478bd9Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
4907c478bd9Sstevel@tonic-gate  *			|			|
4917c478bd9Sstevel@tonic-gate  *			|			|
4927c478bd9Sstevel@tonic-gate  *			:			:
4937c478bd9Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
4947c478bd9Sstevel@tonic-gate  *			|			|
4957c478bd9Sstevel@tonic-gate  *			|	user data	|
4967c478bd9Sstevel@tonic-gate  *			|-----------------------|
4977c478bd9Sstevel@tonic-gate  *			|	user text	|
4987c478bd9Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
4997c478bd9Sstevel@tonic-gate  *			|	invalid		|
5007c478bd9Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
5017c478bd9Sstevel@tonic-gate  *
5027c478bd9Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
5037c478bd9Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
5047c478bd9Sstevel@tonic-gate  *
5057c478bd9Sstevel@tonic-gate  * Floating values:
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
508ae115bc7Smrj  * structures.  This region contains page_t structures for
509ae115bc7Smrj  * physical memory, memsegs, memlists, and the page hash.
5107c478bd9Sstevel@tonic-gate  *
5117c478bd9Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
5127c478bd9Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
5137c478bd9Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
5147c478bd9Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
5157c478bd9Sstevel@tonic-gate  * accessed using rip-relative addressing
5167c478bd9Sstevel@tonic-gate  *
5177c478bd9Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
5187c478bd9Sstevel@tonic-gate  *
5197c478bd9Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
5207c478bd9Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
5217c478bd9Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
5227c478bd9Sstevel@tonic-gate  *
523ae115bc7Smrj  * segmap_start: start of segmap. The length of segmap can be modified
52452eef812SAmrita Sadhukhan  * through eeprom. The default length is 16MB on 32-bit systems and 64MB
52552eef812SAmrita Sadhukhan  * on 64-bit systems.
5267c478bd9Sstevel@tonic-gate  *
5277c478bd9Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
5287c478bd9Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
5297c478bd9Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
5307c478bd9Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
5317c478bd9Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
5327c478bd9Sstevel@tonic-gate  * segkp sizes).
5337c478bd9Sstevel@tonic-gate  *
5347c478bd9Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
5357c478bd9Sstevel@tonic-gate  *	size	page_t's			size
5367c478bd9Sstevel@tonic-gate  *	----	---------	----------	-----------
5377c478bd9Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
5387c478bd9Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
5397c478bd9Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
5407c478bd9Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
5417c478bd9Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
5427c478bd9Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
5437c478bd9Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
5447c478bd9Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
5457c478bd9Sstevel@tonic-gate  *
5467c478bd9Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
5477c478bd9Sstevel@tonic-gate  * configurations above 8gb.
5487c478bd9Sstevel@tonic-gate  *
5497c478bd9Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
5507c478bd9Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
5517c478bd9Sstevel@tonic-gate  */
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /* real-time-clock initialization parameters */
554ae115bc7Smrj extern time_t process_rtc_config_file(void);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate uintptr_t	kernelbase;
557ae115bc7Smrj uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
5587c478bd9Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
5597c478bd9Sstevel@tonic-gate size_t		segmapsize;
560ae115bc7Smrj uintptr_t	segmap_start;
5617c478bd9Sstevel@tonic-gate int		segmapfreelists;
5627c478bd9Sstevel@tonic-gate pgcnt_t		npages;
563ae115bc7Smrj pgcnt_t		orig_npages;
5647c478bd9Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
5657c478bd9Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
5697c478bd9Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
5707c478bd9Sstevel@tonic-gate  * the bootstrap.
5717c478bd9Sstevel@tonic-gate  */
572ae115bc7Smrj static page_t *bootpages;
573ae115bc7Smrj 
574ae115bc7Smrj /*
575ae115bc7Smrj  * boot time pages that have a vnode from the ramdisk will keep that forever.
576ae115bc7Smrj  */
577ae115bc7Smrj static page_t *rd_pages;
5787c478bd9Sstevel@tonic-gate 
57919397407SSherry Moore /*
58019397407SSherry Moore  * Lower 64K
58119397407SSherry Moore  */
58219397407SSherry Moore static page_t *lower_pages = NULL;
58319397407SSherry Moore static int lower_pages_count = 0;
58419397407SSherry Moore 
5857c478bd9Sstevel@tonic-gate struct system_hardware system_hardware;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
5897c478bd9Sstevel@tonic-gate  */
5907c478bd9Sstevel@tonic-gate static void
591ae115bc7Smrj print_memlist(char *title, struct memlist *mp)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
5947c478bd9Sstevel@tonic-gate 	while (mp != NULL)  {
5957c478bd9Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
59656f33205SJonathan Adams 		    mp->ml_address, mp->ml_size);
59756f33205SJonathan Adams 		mp = mp->ml_next;
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
6037c478bd9Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
6047c478bd9Sstevel@tonic-gate  */
6057c478bd9Sstevel@tonic-gate int	l2cache_sz = 0x80000;
6067c478bd9Sstevel@tonic-gate int	l2cache_linesz = 0x40;
6077c478bd9Sstevel@tonic-gate int	l2cache_assoc = 1;
6087c478bd9Sstevel@tonic-gate 
6092cb27123Saguzovsk static size_t	textrepl_min_gb = 10;
6102cb27123Saguzovsk 
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
6137c478bd9Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
6147c478bd9Sstevel@tonic-gate  */
6157c478bd9Sstevel@tonic-gate #ifdef __amd64
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate vmem_t		*device_arena;
6187c478bd9Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
619ae115bc7Smrj size_t		toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate #else	/* __i386 */
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
6247c478bd9Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate #endif	/* __i386 */
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate int prom_debug;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
6327c478bd9Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
6337c478bd9Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
6347c478bd9Sstevel@tonic-gate  */
6351de082f7SVikram Hegde #define	NUM_ALLOCATIONS 8
6367c478bd9Sstevel@tonic-gate int num_allocations = 0;
6377c478bd9Sstevel@tonic-gate struct {
6387c478bd9Sstevel@tonic-gate 	void **al_ptr;
6397c478bd9Sstevel@tonic-gate 	size_t al_size;
6407c478bd9Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
6417c478bd9Sstevel@tonic-gate size_t valloc_sz = 0;
6427c478bd9Sstevel@tonic-gate uintptr_t valloc_base;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
6454da99751SToomas Soome 		size = ROUND_UP_PAGE(size);				\
6467c478bd9Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
6477c478bd9Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
6487c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
6497c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
6507c478bd9Sstevel@tonic-gate 		valloc_sz += size;					\
6514da99751SToomas Soome 		++num_allocations;					\
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
654ae115bc7Smrj /*
655ae115bc7Smrj  * Allocate all the initial memory needed by the page allocator.
656ae115bc7Smrj  */
6577c478bd9Sstevel@tonic-gate static void
6587c478bd9Sstevel@tonic-gate perform_allocations(void)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	caddr_t mem;
6617c478bd9Sstevel@tonic-gate 	int i;
662ae115bc7Smrj 	int valloc_align;
6637c478bd9Sstevel@tonic-gate 
664ae115bc7Smrj 	PRM_DEBUG(valloc_base);
665ae115bc7Smrj 	PRM_DEBUG(valloc_sz);
666ae115bc7Smrj 	valloc_align = mmu.level_size[mmu.max_page_level > 0];
667ae115bc7Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
6687c478bd9Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
6697c478bd9Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
6707c478bd9Sstevel@tonic-gate 	bzero(mem, valloc_sz);
6717c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
6727c478bd9Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
6737c478bd9Sstevel@tonic-gate 		mem += allocations[i].al_size;
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6773ce2fcdcSRobert Mustacchi /*
6783ce2fcdcSRobert Mustacchi  * Set up and enable SMAP now before we start other CPUs, but after the kernel's
6793ce2fcdcSRobert Mustacchi  * VM has been set up so we can use hot_patch_kernel_text().
6803ce2fcdcSRobert Mustacchi  *
6813ce2fcdcSRobert Mustacchi  * We can only patch 1, 2, or 4 bytes, but not three bytes. So instead, we
6823ce2fcdcSRobert Mustacchi  * replace the four byte word at the patch point. See uts/intel/ia32/ml/copy.s
6833ce2fcdcSRobert Mustacchi  * for more information on what's going on here.
6843ce2fcdcSRobert Mustacchi  */
6853ce2fcdcSRobert Mustacchi static void
6863ce2fcdcSRobert Mustacchi startup_smap(void)
6873ce2fcdcSRobert Mustacchi {
6883ce2fcdcSRobert Mustacchi 	int i;
6893ce2fcdcSRobert Mustacchi 	uint32_t inst;
6903ce2fcdcSRobert Mustacchi 	uint8_t *instp;
6913ce2fcdcSRobert Mustacchi 	char sym[128];
692425251fdSSam Gwydir 	struct modctl *modp;
6933ce2fcdcSRobert Mustacchi 
6943ce2fcdcSRobert Mustacchi 	extern int _smap_enable_patch_count;
6953ce2fcdcSRobert Mustacchi 	extern int _smap_disable_patch_count;
6963ce2fcdcSRobert Mustacchi 
6973ce2fcdcSRobert Mustacchi 	if (disable_smap != 0)
6983ce2fcdcSRobert Mustacchi 		remove_x86_feature(x86_featureset, X86FSET_SMAP);
6993ce2fcdcSRobert Mustacchi 
7003ce2fcdcSRobert Mustacchi 	if (is_x86_feature(x86_featureset, X86FSET_SMAP) == B_FALSE)
7013ce2fcdcSRobert Mustacchi 		return;
7023ce2fcdcSRobert Mustacchi 
7033ce2fcdcSRobert Mustacchi 	for (i = 0; i < _smap_enable_patch_count; i++) {
7043ce2fcdcSRobert Mustacchi 		int sizep;
7053ce2fcdcSRobert Mustacchi 
7063ce2fcdcSRobert Mustacchi 		VERIFY3U(i, <, _smap_enable_patch_count);
7073ce2fcdcSRobert Mustacchi 		VERIFY(snprintf(sym, sizeof (sym), "_smap_enable_patch_%d", i) <
7083ce2fcdcSRobert Mustacchi 		    sizeof (sym));
7093ce2fcdcSRobert Mustacchi 		instp = (uint8_t *)(void *)kobj_getelfsym(sym, NULL, &sizep);
7103ce2fcdcSRobert Mustacchi 		VERIFY(instp != 0);
7113ce2fcdcSRobert Mustacchi 		inst = (instp[3] << 24) | (SMAP_CLAC_INSTR & 0x00ffffff);
7123ce2fcdcSRobert Mustacchi 		hot_patch_kernel_text((caddr_t)instp, inst, 4);
7133ce2fcdcSRobert Mustacchi 	}
7143ce2fcdcSRobert Mustacchi 
7153ce2fcdcSRobert Mustacchi 	for (i = 0; i < _smap_disable_patch_count; i++) {
7163ce2fcdcSRobert Mustacchi 		int sizep;
7173ce2fcdcSRobert Mustacchi 
7183ce2fcdcSRobert Mustacchi 		VERIFY(snprintf(sym, sizeof (sym), "_smap_disable_patch_%d",
7193ce2fcdcSRobert Mustacchi 		    i) < sizeof (sym));
7203ce2fcdcSRobert Mustacchi 		instp = (uint8_t *)(void *)kobj_getelfsym(sym, NULL, &sizep);
7213ce2fcdcSRobert Mustacchi 		VERIFY(instp != 0);
7223ce2fcdcSRobert Mustacchi 		inst = (instp[3] << 24) | (SMAP_STAC_INSTR & 0x00ffffff);
7233ce2fcdcSRobert Mustacchi 		hot_patch_kernel_text((caddr_t)instp, inst, 4);
7243ce2fcdcSRobert Mustacchi 	}
7253ce2fcdcSRobert Mustacchi 
726425251fdSSam Gwydir 	/*
727425251fdSSam Gwydir 	 * Hotinline calls to smap_enable and smap_disable within
728425251fdSSam Gwydir 	 * unix module. Hotinlines in other modules are done on
729425251fdSSam Gwydir 	 * mod_load().
730425251fdSSam Gwydir 	 */
731425251fdSSam Gwydir 	modp = mod_hold_by_name("unix");
732425251fdSSam Gwydir 	do_hotinlines(modp->mod_mp);
733425251fdSSam Gwydir 	mod_release_mod(modp);
734425251fdSSam Gwydir 
7353ce2fcdcSRobert Mustacchi 	setcr4(getcr4() | CR4_SMAP);
7363ce2fcdcSRobert Mustacchi 	smap_enable();
7373ce2fcdcSRobert Mustacchi }
7383ce2fcdcSRobert Mustacchi 
7397c478bd9Sstevel@tonic-gate /*
7407c478bd9Sstevel@tonic-gate  * Our world looks like this at startup time.
7417c478bd9Sstevel@tonic-gate  *
7427c478bd9Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
7437c478bd9Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
7447c478bd9Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
7457c478bd9Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
7467c478bd9Sstevel@tonic-gate  *
7477c478bd9Sstevel@tonic-gate  * On the text page:
7487c478bd9Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
7497c478bd9Sstevel@tonic-gate  *
7507c478bd9Sstevel@tonic-gate  * On the data page:
751ae115bc7Smrj  * unix/genunix/krtld/module data loads.
752ae115bc7Smrj  *
7537c478bd9Sstevel@tonic-gate  * Machine-dependent startup code
7547c478bd9Sstevel@tonic-gate  */
7557c478bd9Sstevel@tonic-gate void
7567c478bd9Sstevel@tonic-gate startup(void)
7577c478bd9Sstevel@tonic-gate {
758843e1988Sjohnlev #if !defined(__xpv)
75975bcd456Sjg 	extern void startup_pci_bios(void);
760843e1988Sjohnlev #endif
761a563a037Sbholler 	extern cpuset_t cpu_ready_set;
762a563a037Sbholler 
763843e1988Sjohnlev 	/*
764843e1988Sjohnlev 	 * Make sure that nobody tries to use sekpm until we have
765843e1988Sjohnlev 	 * initialized it properly.
766843e1988Sjohnlev 	 */
7677c478bd9Sstevel@tonic-gate #if defined(__amd64)
76835b1ab99Sjosephb 	kpm_desired = 1;
7697c478bd9Sstevel@tonic-gate #endif
7707c478bd9Sstevel@tonic-gate 	kpm_enable = 0;
771a563a037Sbholler 	CPUSET_ONLY(cpu_ready_set, 0);	/* cpu 0 is boot cpu */
7727c478bd9Sstevel@tonic-gate 
773843e1988Sjohnlev #if defined(__xpv)	/* XXPV fix me! */
774843e1988Sjohnlev 	{
775843e1988Sjohnlev 		extern int segvn_use_regions;
776843e1988Sjohnlev 		segvn_use_regions = 0;
777843e1988Sjohnlev 	}
778843e1988Sjohnlev #endif
7796a59053bSAlex Wilson 	ssp_init();
7807c478bd9Sstevel@tonic-gate 	progressbar_init();
7817c478bd9Sstevel@tonic-gate 	startup_init();
782ab4a9bebSjohnlev #if defined(__xpv)
783ab4a9bebSjohnlev 	startup_xen_version();
784ab4a9bebSjohnlev #endif
7857c478bd9Sstevel@tonic-gate 	startup_memlist();
786ae115bc7Smrj 	startup_kmem();
78735b1ab99Sjosephb 	startup_vm();
788843e1988Sjohnlev #if !defined(__xpv)
7890db3240dSStephen Hanson 	/*
7900db3240dSStephen Hanson 	 * Note we need to do this even on fast reboot in order to access
7910db3240dSStephen Hanson 	 * the irq routing table (used for pci labels).
7920db3240dSStephen Hanson 	 */
7930db3240dSStephen Hanson 	startup_pci_bios();
7943ce2fcdcSRobert Mustacchi 	startup_smap();
795e4b86885SCheng Sean Ye #endif
796e4b86885SCheng Sean Ye #if defined(__xpv)
797e4b86885SCheng Sean Ye 	startup_xen_mca();
798843e1988Sjohnlev #endif
7997c478bd9Sstevel@tonic-gate 	startup_modules();
8008770118eSlipeng sang - Sun Microsystems - Beijing China 
8017c478bd9Sstevel@tonic-gate 	startup_end();
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate static void
8057c478bd9Sstevel@tonic-gate startup_init()
8067c478bd9Sstevel@tonic-gate {
8077c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	/*
8107c478bd9Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
8117c478bd9Sstevel@tonic-gate 	 */
8127c478bd9Sstevel@tonic-gate 	cpuid_pass2(CPU);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	/*
8177c478bd9Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
8187c478bd9Sstevel@tonic-gate 	 */
8197c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
8207c478bd9Sstevel@tonic-gate 		++prom_debug;
8217c478bd9Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/*
8257c478bd9Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
8267c478bd9Sstevel@tonic-gate 	 */
8277c478bd9Sstevel@tonic-gate 	get_system_configuration();
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	/*
8307c478bd9Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
8317c478bd9Sstevel@tonic-gate 	 */
8327c478bd9Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
8337c478bd9Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
8347c478bd9Sstevel@tonic-gate 		    CPU->cpu_brandstr);
8357c478bd9Sstevel@tonic-gate 		halt("This processor is not supported by this release "
8367c478bd9Sstevel@tonic-gate 		    "of Solaris.");
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
8447c478bd9Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
8457c478bd9Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
8467c478bd9Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
8477c478bd9Sstevel@tonic-gate  */
8487c478bd9Sstevel@tonic-gate static void
8497c478bd9Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	uintptr_t va;
8527c478bd9Sstevel@tonic-gate 	uintptr_t next_va;
8537c478bd9Sstevel@tonic-gate 	pfn_t pfn;
8547c478bd9Sstevel@tonic-gate 	uint64_t pfn_addr;
8557c478bd9Sstevel@tonic-gate 	uint64_t pfn_eaddr;
8567c478bd9Sstevel@tonic-gate 	uint_t prot;
8577c478bd9Sstevel@tonic-gate 	size_t len;
8587c478bd9Sstevel@tonic-gate 	uint_t change;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (prom_debug)
8617c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
8627c478bd9Sstevel@tonic-gate 		    *addr, *size);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/*
8657c478bd9Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	if (*addr == 0) {
8687c478bd9Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
8697c478bd9Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/*
873ae115bc7Smrj 	 * First we trim from the front of the range. Since kbm_probe()
8747c478bd9Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
8757c478bd9Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
8767c478bd9Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
8777c478bd9Sstevel@tonic-gate 	 * but w < v.
8787c478bd9Sstevel@tonic-gate 	 */
8797c478bd9Sstevel@tonic-gate 	do {
8807c478bd9Sstevel@tonic-gate 		change = 0;
8817c478bd9Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
882ae115bc7Smrj 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
8837c478bd9Sstevel@tonic-gate 		    va = next_va) {
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 			next_va = va + len;
886ae115bc7Smrj 			pfn_addr = pfn_to_pa(pfn);
8877c478bd9Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
8907c478bd9Sstevel@tonic-gate 				change = 1;
8917c478bd9Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
8927c478bd9Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
8937c478bd9Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
8947c478bd9Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
8957c478bd9Sstevel@tonic-gate 				}
8967c478bd9Sstevel@tonic-gate 			}
8977c478bd9Sstevel@tonic-gate 		}
8987c478bd9Sstevel@tonic-gate 		if (change && prom_debug)
8997c478bd9Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
9007c478bd9Sstevel@tonic-gate 			    *addr, *size);
9017c478bd9Sstevel@tonic-gate 	} while (change);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	/*
9047c478bd9Sstevel@tonic-gate 	 * Trim pages from the end of the range.
9057c478bd9Sstevel@tonic-gate 	 */
9067c478bd9Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
907ae115bc7Smrj 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
9087c478bd9Sstevel@tonic-gate 	    va = next_va) {
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 		next_va = va + len;
911ae115bc7Smrj 		pfn_addr = pfn_to_pa(pfn);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
9147c478bd9Sstevel@tonic-gate 			*size = pfn_addr - *addr;
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (prom_debug)
9187c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
9197c478bd9Sstevel@tonic-gate 		    *addr, *size);
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate static void
9237c478bd9Sstevel@tonic-gate kpm_init()
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate 	struct segkpm_crargs b;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/*
9287c478bd9Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
9297c478bd9Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
9307c478bd9Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
9317c478bd9Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
9327c478bd9Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
9337c478bd9Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
9347c478bd9Sstevel@tonic-gate 	 * pagesize is used.
9357c478bd9Sstevel@tonic-gate 	 */
9367c478bd9Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
9377c478bd9Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
9387c478bd9Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
9397c478bd9Sstevel@tonic-gate 	kpmp2pshft = 0;
9407c478bd9Sstevel@tonic-gate 	kpmpnpgs = 1;
9417c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
9447c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
9477c478bd9Sstevel@tonic-gate 		panic("cannot attach segkpm");
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
9507c478bd9Sstevel@tonic-gate 	b.nvcolors = 1;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
9537c478bd9Sstevel@tonic-gate 		panic("segkpm_create segkpm");
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
95674ecdb51SJohn Levon 
95774ecdb51SJohn Levon 	kpm_enable = 1;
95874ecdb51SJohn Levon 
95974ecdb51SJohn Levon 	/*
96074ecdb51SJohn Levon 	 * As the KPM was disabled while setting up the system, go back and fix
96174ecdb51SJohn Levon 	 * CPU zero's access to its user page table. This is a bit gross, but
96274ecdb51SJohn Levon 	 * we have a chicken and egg problem otherwise.
96374ecdb51SJohn Levon 	 */
96474ecdb51SJohn Levon 	ASSERT(CPU->cpu_hat_info->hci_user_l3ptes == NULL);
96574ecdb51SJohn Levon 	CPU->cpu_hat_info->hci_user_l3ptes =
96674ecdb51SJohn Levon 	    (x86pte_t *)hat_kpm_mapin_pfn(CPU->cpu_hat_info->hci_user_l3pfn);
967ae115bc7Smrj }
9687c478bd9Sstevel@tonic-gate 
969ae115bc7Smrj /*
970ae115bc7Smrj  * The debug info page provides enough information to allow external
971ae115bc7Smrj  * inspectors (e.g. when running under a hypervisor) to bootstrap
972ae115bc7Smrj  * themselves into allowing full-blown kernel debugging.
973ae115bc7Smrj  */
974ae115bc7Smrj static void
975ae115bc7Smrj init_debug_info(void)
976ae115bc7Smrj {
977ae115bc7Smrj 	caddr_t mem;
978ae115bc7Smrj 	debug_info_t *di;
979ae115bc7Smrj 
980ae115bc7Smrj #ifndef __lint
981ae115bc7Smrj 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
982ae115bc7Smrj #endif
983ae115bc7Smrj 
984ae115bc7Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
985ae115bc7Smrj 	    MMU_PAGESIZE);
986ae115bc7Smrj 
987ae115bc7Smrj 	if (mem != (caddr_t)DEBUG_INFO_VA)
988ae115bc7Smrj 		panic("BOP_ALLOC() failed");
989ae115bc7Smrj 	bzero(mem, MMU_PAGESIZE);
990ae115bc7Smrj 
991ae115bc7Smrj 	di = (debug_info_t *)mem;
992ae115bc7Smrj 
993ae115bc7Smrj 	di->di_magic = DEBUG_INFO_MAGIC;
994ae115bc7Smrj 	di->di_version = DEBUG_INFO_VERSION;
995843e1988Sjohnlev 	di->di_modules = (uintptr_t)&modules;
996843e1988Sjohnlev 	di->di_s_text = (uintptr_t)s_text;
997843e1988Sjohnlev 	di->di_e_text = (uintptr_t)e_text;
998843e1988Sjohnlev 	di->di_s_data = (uintptr_t)s_data;
999843e1988Sjohnlev 	di->di_e_data = (uintptr_t)e_data;
1000843e1988Sjohnlev 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
1001843e1988Sjohnlev 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate /*
1005ae115bc7Smrj  * Build the memlists and other kernel essential memory system data structures.
1006ae115bc7Smrj  * This is everything at valloc_base.
10077c478bd9Sstevel@tonic-gate  */
10087c478bd9Sstevel@tonic-gate static void
10097c478bd9Sstevel@tonic-gate startup_memlist(void)
10107c478bd9Sstevel@tonic-gate {
10117c478bd9Sstevel@tonic-gate 	size_t memlist_sz;
10127c478bd9Sstevel@tonic-gate 	size_t memseg_sz;
10137c478bd9Sstevel@tonic-gate 	size_t pagehash_sz;
10147c478bd9Sstevel@tonic-gate 	size_t pp_sz;
10157c478bd9Sstevel@tonic-gate 	uintptr_t va;
10167c478bd9Sstevel@tonic-gate 	size_t len;
10177c478bd9Sstevel@tonic-gate 	uint_t prot;
10187c478bd9Sstevel@tonic-gate 	pfn_t pfn;
10197c478bd9Sstevel@tonic-gate 	int memblocks;
10201de082f7SVikram Hegde 	pfn_t rsvd_high_pfn;
10211de082f7SVikram Hegde 	pgcnt_t rsvd_pgcnt;
10221de082f7SVikram Hegde 	size_t rsvdmemlist_sz;
10231de082f7SVikram Hegde 	int rsvdmemblocks;
10247c478bd9Sstevel@tonic-gate 	caddr_t pagecolor_mem;
10257c478bd9Sstevel@tonic-gate 	size_t pagecolor_memsz;
10267c478bd9Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
10277c478bd9Sstevel@tonic-gate 	size_t page_ctrs_size;
1028d7d93655Sblakej 	size_t pse_table_alloc_size;
10297c478bd9Sstevel@tonic-gate 	struct memlist *current;
10307c478bd9Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
10337c478bd9Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
10347c478bd9Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	/*
10397c478bd9Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
10407c478bd9Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
10417c478bd9Sstevel@tonic-gate 	 */
1042ae115bc7Smrj 	len = kbm_nucleus_size;
1043ae115bc7Smrj 	ASSERT(len > MMU_PAGESIZE);
10447c478bd9Sstevel@tonic-gate 
1045ae115bc7Smrj 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
1046ae115bc7Smrj 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
1047ae115bc7Smrj 	if (e_moddata - moddata > MODDATA)
1048ae115bc7Smrj 		e_moddata = moddata + MODDATA;
10497c478bd9Sstevel@tonic-gate 
1050ae115bc7Smrj 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
1051ae115bc7Smrj 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
1052ae115bc7Smrj 	if (e_modtext - modtext > MODTEXT)
1053ae115bc7Smrj 		e_modtext = modtext + MODTEXT;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	econtig = e_moddata;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	PRM_DEBUG(modtext);
10587c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
10597c478bd9Sstevel@tonic-gate 	PRM_DEBUG(moddata);
10607c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
10617c478bd9Sstevel@tonic-gate 	PRM_DEBUG(econtig);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/*
1064ae115bc7Smrj 	 * Examine the boot loader physical memory map to find out:
10657c478bd9Sstevel@tonic-gate 	 * - total memory in system - physinstalled
10667c478bd9Sstevel@tonic-gate 	 * - the max physical address - physmax
1067ae115bc7Smrj 	 * - the number of discontiguous segments of memory.
10687c478bd9Sstevel@tonic-gate 	 */
10697c478bd9Sstevel@tonic-gate 	if (prom_debug)
1070ae115bc7Smrj 		print_memlist("boot physinstalled",
10717c478bd9Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
1072a3114836SGerry Liu 	installed_top_size_ex(bootops->boot_mem->physinstalled, &physmax,
10737c478bd9Sstevel@tonic-gate 	    &physinstalled, &memblocks);
10747c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmax);
10757c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
10767c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
10777c478bd9Sstevel@tonic-gate 
1078a3114836SGerry Liu 	/*
107904909c8cSJohn Levon 	 * We no longer support any form of memory DR.
1080a3114836SGerry Liu 	 */
108104909c8cSJohn Levon 	plat_dr_physmax = 0;
1082a3114836SGerry Liu 
10831de082f7SVikram Hegde 	/*
10841de082f7SVikram Hegde 	 * Examine the bios reserved memory to find out:
10851de082f7SVikram Hegde 	 * - the number of discontiguous segments of memory.
10861de082f7SVikram Hegde 	 */
10871de082f7SVikram Hegde 	if (prom_debug)
10881de082f7SVikram Hegde 		print_memlist("boot reserved mem",
10891de082f7SVikram Hegde 		    bootops->boot_mem->rsvdmem);
1090a3114836SGerry Liu 	installed_top_size_ex(bootops->boot_mem->rsvdmem, &rsvd_high_pfn,
10911de082f7SVikram Hegde 	    &rsvd_pgcnt, &rsvdmemblocks);
10921de082f7SVikram Hegde 	PRM_DEBUG(rsvd_high_pfn);
10931de082f7SVikram Hegde 	PRM_DEBUG(rsvd_pgcnt);
10941de082f7SVikram Hegde 	PRM_DEBUG(rsvdmemblocks);
10951de082f7SVikram Hegde 
10967c478bd9Sstevel@tonic-gate 	/*
10977c478bd9Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
10987c478bd9Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
10997c478bd9Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
11007c478bd9Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
11017c478bd9Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
11027c478bd9Sstevel@tonic-gate 	 */
11037c478bd9Sstevel@tonic-gate 	mmu_init();
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate #ifdef	__i386
11067c478bd9Sstevel@tonic-gate 	/*
11077c478bd9Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
11087c478bd9Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
11097c478bd9Sstevel@tonic-gate 	 */
11107c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
11117c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
11127c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
11137c478bd9Sstevel@tonic-gate 			physmax = PFN_64G - 1;
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 	} else {
11167c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
11177c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
11187c478bd9Sstevel@tonic-gate 			physmax = PFN_4G - 1;
11197c478bd9Sstevel@tonic-gate 		}
11207c478bd9Sstevel@tonic-gate 	}
11217c478bd9Sstevel@tonic-gate #endif
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
11267c478bd9Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
11277c478bd9Sstevel@tonic-gate 		char value[8];
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 		if (len < 8)
11307c478bd9Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
11317c478bd9Sstevel@tonic-gate 		else
11327c478bd9Sstevel@tonic-gate 			(void) strcpy(value, "");
11337c478bd9Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
11347c478bd9Sstevel@tonic-gate 			mmu.pt_nx = 0;
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	/*
11397c478bd9Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
11407c478bd9Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
11417c478bd9Sstevel@tonic-gate 	 *
11427c478bd9Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
11437c478bd9Sstevel@tonic-gate 	 */
11447c478bd9Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
11457c478bd9Sstevel@tonic-gate 	obp_pages = 0;
11467c478bd9Sstevel@tonic-gate 	va = KERNEL_TEXT;
1147ae115bc7Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
11487c478bd9Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
11497c478bd9Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
11507c478bd9Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
11517c478bd9Sstevel@tonic-gate 		va += len;
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
11547c478bd9Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	/*
115735b1ab99Sjosephb 	 * If physmem is patched to be non-zero, use it instead of the computed
115835b1ab99Sjosephb 	 * value unless it is larger than the actual amount of memory on hand.
11597c478bd9Sstevel@tonic-gate 	 */
11604944b02eSkchow 	if (physmem == 0 || physmem > npages) {
11617c478bd9Sstevel@tonic-gate 		physmem = npages;
11624944b02eSkchow 	} else if (physmem < npages) {
1163f53ad214Skchow 		orig_npages = npages;
11647c478bd9Sstevel@tonic-gate 		npages = physmem;
11654944b02eSkchow 	}
11667c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmem);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	/*
11697c478bd9Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
11707c478bd9Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
11717c478bd9Sstevel@tonic-gate 	 * include:
11727c478bd9Sstevel@tonic-gate 	 *	memsegs
11737c478bd9Sstevel@tonic-gate 	 *	memlists
11747c478bd9Sstevel@tonic-gate 	 *	page hash table
11757c478bd9Sstevel@tonic-gate 	 *	page_t's
11767c478bd9Sstevel@tonic-gate 	 *	page coloring data structs
11777c478bd9Sstevel@tonic-gate 	 */
11787c478bd9Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
11797c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
11807c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	/*
1183ae115bc7Smrj 	 * Reserve space for memlists. There's no real good way to know exactly
1184ae115bc7Smrj 	 * how much room we'll need, but this should be a good upper bound.
11857c478bd9Sstevel@tonic-gate 	 */
11867c478bd9Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
11877c478bd9Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
11887c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
11897c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
11907c478bd9Sstevel@tonic-gate 
11911de082f7SVikram Hegde 	/*
11921de082f7SVikram Hegde 	 * Reserve space for bios reserved memlists.
11931de082f7SVikram Hegde 	 */
11941de082f7SVikram Hegde 	rsvdmemlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
11951de082f7SVikram Hegde 	    (rsvdmemblocks + POSS_NEW_FRAGMENTS));
11961de082f7SVikram Hegde 	ADD_TO_ALLOCATIONS(bios_rsvd, rsvdmemlist_sz);
11971de082f7SVikram Hegde 	PRM_DEBUG(rsvdmemlist_sz);
11981de082f7SVikram Hegde 
1199cb15d5d9SPeter Rival 	/* LINTED */
1200cb15d5d9SPeter Rival 	ASSERT(P2SAMEHIGHBIT((1 << PP_SHIFT), sizeof (struct page)));
12017c478bd9Sstevel@tonic-gate 	/*
12027c478bd9Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
12037c478bd9Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
12047c478bd9Sstevel@tonic-gate 	 */
12057c478bd9Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
1206cb15d5d9SPeter Rival 	page_hashsz_shift = highbit(page_hashsz);
1207cb15d5d9SPeter Rival 	page_hashsz = 1 << page_hashsz_shift;
12087c478bd9Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
12097c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
12107c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	/*
1213ae115bc7Smrj 	 * Set aside room for the page structures themselves.
12147c478bd9Sstevel@tonic-gate 	 */
1215ae115bc7Smrj 	PRM_DEBUG(npages);
1216ae115bc7Smrj 	pp_sz = sizeof (struct page) * npages;
12177c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
12187c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	/*
12217c478bd9Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
12227c478bd9Sstevel@tonic-gate 	 */
12237c478bd9Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
12247c478bd9Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
12257c478bd9Sstevel@tonic-gate 	pagecolor_memsz =
12267c478bd9Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
12277c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
12287c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
12317c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
12327c478bd9Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
12337c478bd9Sstevel@tonic-gate 
1234d7d93655Sblakej 	/*
1235d7d93655Sblakej 	 * Allocate the array that protects pp->p_selock.
1236d7d93655Sblakej 	 */
1237d7d93655Sblakej 	pse_shift = size_pse_array(physmem, max_ncpus);
1238d7d93655Sblakej 	pse_table_size = 1 << pse_shift;
1239d7d93655Sblakej 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
1240d7d93655Sblakej 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
1241d7d93655Sblakej 
12427c478bd9Sstevel@tonic-gate 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
1243ae115bc7Smrj 	valloc_base = VALLOC_BASE;
124435b1ab99Sjosephb 
124535b1ab99Sjosephb 	/*
124604909c8cSJohn Levon 	 * The signicant memory-sized regions are roughly sized as follows in
124704909c8cSJohn Levon 	 * the default layout with max physmem:
124804909c8cSJohn Levon 	 *  segkpm: 1x physmem allocated (but 1Tb room, below VALLOC_BASE)
124904909c8cSJohn Levon 	 *  segzio: 1.5x physmem
125004909c8cSJohn Levon 	 *  segkvmm: 4x physmem
125104909c8cSJohn Levon 	 *  heap: whatever's left up to COREHEAP_BASE, at least 1.5x physmem
125204909c8cSJohn Levon 	 *
125304909c8cSJohn Levon 	 * The idea is that we leave enough room to avoid fragmentation issues,
125404909c8cSJohn Levon 	 * so we would like the VA arenas to have some extra.
1255af868f46SMatthew Ahrens 	 *
125604909c8cSJohn Levon 	 * Ignoring the loose change of segkp, valloc, and such, this means that
125704909c8cSJohn Levon 	 * as COREHEAP_BASE-VALLOC_BASE=2Tb, we can accommodate a physmem up to
125804909c8cSJohn Levon 	 * about (2Tb / 7.0), rounded down to 256Gb in the check below.
1259af868f46SMatthew Ahrens 	 *
126004909c8cSJohn Levon 	 * Note that KPM lives below VALLOC_BASE, but we want to include it in
126104909c8cSJohn Levon 	 * adjustments, hence the 8 below.
126204909c8cSJohn Levon 	 *
126304909c8cSJohn Levon 	 * Beyond 256Gb, we push segkpm_base (and hence kernelbase and
126404909c8cSJohn Levon 	 * _userlimit) down to accommodate the VA requirements above.
126535b1ab99Sjosephb 	 */
126604909c8cSJohn Levon 	if (physmax + 1 > mmu_btop(TERABYTE / 4)) {
126704909c8cSJohn Levon 		uint64_t physmem_bytes = mmu_ptob(physmax + 1);
126804909c8cSJohn Levon 		uint64_t adjustment = 8 * (physmem_bytes - (TERABYTE / 4));
126935b1ab99Sjosephb 
127004909c8cSJohn Levon 		PRM_DEBUG(adjustment);
1271a3114836SGerry Liu 
1272af868f46SMatthew Ahrens 		/*
127304909c8cSJohn Levon 		 * segkpm_base is always aligned on a L3 PTE boundary.
1274af868f46SMatthew Ahrens 		 */
127504909c8cSJohn Levon 		segkpm_base -= P2ROUNDUP(adjustment, KERNEL_REDZONE_SIZE);
127635b1ab99Sjosephb 
127704909c8cSJohn Levon 		/*
127804909c8cSJohn Levon 		 * But make sure we leave some space for user apps above hole.
127904909c8cSJohn Levon 		 */
128035b1ab99Sjosephb 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
128135b1ab99Sjosephb 
128204909c8cSJohn Levon 		ASSERT(segkpm_base <= SEGKPM_BASE);
128304909c8cSJohn Levon 
128404909c8cSJohn Levon 		valloc_base = segkpm_base + P2ROUNDUP(physmem_bytes, ONE_GIG);
12856c8c89eaSKit Chow 		if (valloc_base < segkpm_base)
12866c8c89eaSKit Chow 			panic("not enough kernel VA to support memory size");
128735b1ab99Sjosephb 	}
128804909c8cSJohn Levon 
128904909c8cSJohn Levon 	PRM_DEBUG(segkpm_base);
12907c478bd9Sstevel@tonic-gate 	PRM_DEBUG(valloc_base);
1291ae115bc7Smrj 
1292ae115bc7Smrj 	/*
1293ae115bc7Smrj 	 * do all the initial allocations
1294ae115bc7Smrj 	 */
1295ae115bc7Smrj 	perform_allocations();
1296ae115bc7Smrj 
1297ae115bc7Smrj 	/*
1298ae115bc7Smrj 	 * Build phys_install and phys_avail in kernel memspace.
1299ae115bc7Smrj 	 * - phys_install should be all memory in the system.
1300ae115bc7Smrj 	 * - phys_avail is phys_install minus any memory mapped before this
1301ae115bc7Smrj 	 *    point above KERNEL_TEXT.
1302ae115bc7Smrj 	 */
1303ae115bc7Smrj 	current = phys_install = memlist;
1304ae115bc7Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
1305ae115bc7Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1306ae115bc7Smrj 		panic("physinstalled was too big!");
1307ae115bc7Smrj 	if (prom_debug)
1308ae115bc7Smrj 		print_memlist("phys_install", phys_install);
1309ae115bc7Smrj 
1310ae115bc7Smrj 	phys_avail = current;
1311ae115bc7Smrj 	PRM_POINT("Building phys_avail:\n");
1312ae115bc7Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
1313ae115bc7Smrj 	    avail_filter);
1314ae115bc7Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1315ae115bc7Smrj 		panic("physavail was too big!");
1316ae115bc7Smrj 	if (prom_debug)
1317ae115bc7Smrj 		print_memlist("phys_avail", phys_avail);
1318a3114836SGerry Liu #ifndef	__xpv
1319a3114836SGerry Liu 	/*
1320a3114836SGerry Liu 	 * Free unused memlist items, which may be used by memory DR driver
1321a3114836SGerry Liu 	 * at runtime.
1322a3114836SGerry Liu 	 */
1323a3114836SGerry Liu 	if ((caddr_t)current < (caddr_t)memlist + memlist_sz) {
1324a3114836SGerry Liu 		memlist_free_block((caddr_t)current,
1325a3114836SGerry Liu 		    (caddr_t)memlist + memlist_sz - (caddr_t)current);
1326a3114836SGerry Liu 	}
1327a3114836SGerry Liu #endif
1328ae115bc7Smrj 
13291de082f7SVikram Hegde 	/*
13301de082f7SVikram Hegde 	 * Build bios reserved memspace
13311de082f7SVikram Hegde 	 */
13321de082f7SVikram Hegde 	current = bios_rsvd;
13331de082f7SVikram Hegde 	copy_memlist_filter(bootops->boot_mem->rsvdmem, &current, NULL);
13341de082f7SVikram Hegde 	if ((caddr_t)current > (caddr_t)bios_rsvd + rsvdmemlist_sz)
13351de082f7SVikram Hegde 		panic("bios_rsvd was too big!");
13361de082f7SVikram Hegde 	if (prom_debug)
13371de082f7SVikram Hegde 		print_memlist("bios_rsvd", bios_rsvd);
1338a3114836SGerry Liu #ifndef	__xpv
1339a3114836SGerry Liu 	/*
1340a3114836SGerry Liu 	 * Free unused memlist items, which may be used by memory DR driver
1341a3114836SGerry Liu 	 * at runtime.
1342a3114836SGerry Liu 	 */
1343a3114836SGerry Liu 	if ((caddr_t)current < (caddr_t)bios_rsvd + rsvdmemlist_sz) {
1344a3114836SGerry Liu 		memlist_free_block((caddr_t)current,
1345a3114836SGerry Liu 		    (caddr_t)bios_rsvd + rsvdmemlist_sz - (caddr_t)current);
1346a3114836SGerry Liu 	}
1347a3114836SGerry Liu #endif
13481de082f7SVikram Hegde 
1349ae115bc7Smrj 	/*
1350ae115bc7Smrj 	 * setup page coloring
1351ae115bc7Smrj 	 */
1352ae115bc7Smrj 	page_coloring_setup(pagecolor_mem);
1353ae115bc7Smrj 	page_lock_init();	/* currently a no-op */
1354ae115bc7Smrj 
1355ae115bc7Smrj 	/*
1356ae115bc7Smrj 	 * free page list counters
1357ae115bc7Smrj 	 */
1358ae115bc7Smrj 	(void) page_ctrs_alloc(page_ctrs_mem);
1359ae115bc7Smrj 
136006fb6a36Sdv 	/*
136106fb6a36Sdv 	 * Size the pcf array based on the number of cpus in the box at
136206fb6a36Sdv 	 * boot time.
136306fb6a36Sdv 	 */
136406fb6a36Sdv 
136506fb6a36Sdv 	pcf_init();
136606fb6a36Sdv 
1367ae115bc7Smrj 	/*
1368ae115bc7Smrj 	 * Initialize the page structures from the memory lists.
1369ae115bc7Smrj 	 */
1370ae115bc7Smrj 	availrmem_initial = availrmem = freemem = 0;
1371ae115bc7Smrj 	PRM_POINT("Calling kphysm_init()...");
1372ae115bc7Smrj 	npages = kphysm_init(pp_base, npages);
1373ae115bc7Smrj 	PRM_POINT("kphysm_init() done");
1374ae115bc7Smrj 	PRM_DEBUG(npages);
1375ae115bc7Smrj 
1376ae115bc7Smrj 	init_debug_info();
1377ae115bc7Smrj 
1378ae115bc7Smrj 	/*
1379ae115bc7Smrj 	 * Now that page_t's have been initialized, remove all the
1380ae115bc7Smrj 	 * initial allocation pages from the kernel free page lists.
1381ae115bc7Smrj 	 */
1382ae115bc7Smrj 	boot_mapin((caddr_t)valloc_base, valloc_sz);
13830cfdb603Sjosephb 	boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
1384ae115bc7Smrj 	PRM_POINT("startup_memlist() done");
1385ae115bc7Smrj 
1386ae115bc7Smrj 	PRM_DEBUG(valloc_sz);
13872cb27123Saguzovsk 
1388567d55e1Saguzovsk #if defined(__amd64)
1389567d55e1Saguzovsk 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
1390567d55e1Saguzovsk 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
1391567d55e1Saguzovsk 		extern size_t textrepl_size_thresh;
13922cb27123Saguzovsk 		textrepl_size_thresh = (16 << 20) - 1;
13932cb27123Saguzovsk 	}
1394567d55e1Saguzovsk #endif
1395ae115bc7Smrj }
1396ae115bc7Smrj 
1397ae115bc7Smrj /*
1398ae115bc7Smrj  * Layout the kernel's part of address space and initialize kmem allocator.
1399ae115bc7Smrj  */
1400ae115bc7Smrj static void
1401ae115bc7Smrj startup_kmem(void)
1402ae115bc7Smrj {
1403932dc8e5Sdp 	extern void page_set_colorequiv_arr(void);
140474ecdb51SJohn Levon #if !defined(__xpv)
140574ecdb51SJohn Levon 	extern uint64_t kpti_kbase;
140674ecdb51SJohn Levon #endif
1407932dc8e5Sdp 
1408ae115bc7Smrj 	PRM_POINT("startup_kmem() starting...");
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate #if defined(__amd64)
14117c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
14127c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
14137c478bd9Sstevel@tonic-gate 		    "systems.");
141435b1ab99Sjosephb 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
14157c478bd9Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
1416ae115bc7Smrj 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
14177c478bd9Sstevel@tonic-gate #else	/* __i386 */
14187c478bd9Sstevel@tonic-gate 	/*
14197c478bd9Sstevel@tonic-gate 	 * We configure kernelbase based on:
14207c478bd9Sstevel@tonic-gate 	 *
14217c478bd9Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
14227c478bd9Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
14237c478bd9Sstevel@tonic-gate 	 *
14247c478bd9Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
14257c478bd9Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
14267c478bd9Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
14277c478bd9Sstevel@tonic-gate 	 *
14287c478bd9Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
14297c478bd9Sstevel@tonic-gate 	 */
14307c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase) {
14317c478bd9Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
14327c478bd9Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
14337c478bd9Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
14347c478bd9Sstevel@tonic-gate 	} else {
14357c478bd9Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
14367c478bd9Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
1439ae115bc7Smrj 	core_base = valloc_base;
14407c478bd9Sstevel@tonic-gate 	core_size = 0;
1441ae115bc7Smrj #endif	/* __i386 */
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_base);
14447c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_size);
1445ae115bc7Smrj 	PRM_DEBUG(kernelbase);
14467c478bd9Sstevel@tonic-gate 
144735b1ab99Sjosephb #if defined(__i386)
14487c478bd9Sstevel@tonic-gate 	segkp_fromheap = 1;
1449ae115bc7Smrj #endif	/* __i386 */
145035b1ab99Sjosephb 
14517c478bd9Sstevel@tonic-gate 	ekernelheap = (char *)core_base;
1452ae115bc7Smrj 	PRM_DEBUG(ekernelheap);
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	/*
14557c478bd9Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
14567c478bd9Sstevel@tonic-gate 	 * update variables that were initialized with a value of
14577c478bd9Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
14587c478bd9Sstevel@tonic-gate 	 *
14597c478bd9Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
14607c478bd9Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
14617c478bd9Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
14627c478bd9Sstevel@tonic-gate 	 *	just be declared as variables there?
14637c478bd9Sstevel@tonic-gate 	 */
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
14667c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
1467ccbaea4fSjosephb #if defined(__amd64)
1468ccbaea4fSjosephb 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
146974ecdb51SJohn Levon #if !defined(__xpv)
147074ecdb51SJohn Levon 	kpti_kbase = kernelbase;
147174ecdb51SJohn Levon #endif
1472ccbaea4fSjosephb #else
14737c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
14747c478bd9Sstevel@tonic-gate #endif
14757c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
14767c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
14777c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
14787c478bd9Sstevel@tonic-gate 
147974ecdb51SJohn Levon 	/* We have to re-do this now that we've modified _userlimit. */
148074ecdb51SJohn Levon 	mmu_calc_user_slots();
148174ecdb51SJohn Levon 
148235b1ab99Sjosephb 	layout_kernel_va();
148335b1ab99Sjosephb 
148435b1ab99Sjosephb #if defined(__i386)
148535b1ab99Sjosephb 	/*
148635b1ab99Sjosephb 	 * If segmap is too large we can push the bottom of the kernel heap
148735b1ab99Sjosephb 	 * higher than the base.  Or worse, it could exceed the top of the
148835b1ab99Sjosephb 	 * VA space entirely, causing it to wrap around.
148935b1ab99Sjosephb 	 */
149035b1ab99Sjosephb 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
149135b1ab99Sjosephb 		panic("too little address space available for kernelheap,"
149235b1ab99Sjosephb 		    " use eeprom for lower kernelbase or smaller segmapsize");
149335b1ab99Sjosephb #endif	/* __i386 */
149435b1ab99Sjosephb 
14957c478bd9Sstevel@tonic-gate 	/*
14967c478bd9Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
14977c478bd9Sstevel@tonic-gate 	 */
149835b1ab99Sjosephb 	kernelheap_init(kernelheap, ekernelheap,
149935b1ab99Sjosephb 	    kernelheap + MMU_PAGESIZE,
1500ae115bc7Smrj 	    (void *)core_base, (void *)(core_base + core_size));
15017c478bd9Sstevel@tonic-gate 
1502843e1988Sjohnlev #if defined(__xpv)
1503843e1988Sjohnlev 	/*
1504843e1988Sjohnlev 	 * Link pending events struct into cpu struct
1505843e1988Sjohnlev 	 */
1506843e1988Sjohnlev 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
1507843e1988Sjohnlev #endif
15087c478bd9Sstevel@tonic-gate 	/*
15097c478bd9Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
15107c478bd9Sstevel@tonic-gate 	 */
15117c478bd9Sstevel@tonic-gate 	kmem_init();
15127c478bd9Sstevel@tonic-gate 
1513932dc8e5Sdp 	/*
1514932dc8e5Sdp 	 * Factor in colorequiv to check additional 'equivalent' bins
1515932dc8e5Sdp 	 */
1516932dc8e5Sdp 	page_set_colorequiv_arr();
1517932dc8e5Sdp 
15187c478bd9Sstevel@tonic-gate 	/*
15197c478bd9Sstevel@tonic-gate 	 * print this out early so that we know what's going on
15207c478bd9Sstevel@tonic-gate 	 */
15217417cfdeSKuriakose Kuruvilla 	print_x86_featureset(x86_featureset);
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	/*
15247c478bd9Sstevel@tonic-gate 	 * Initialize bp_mapin().
15257c478bd9Sstevel@tonic-gate 	 */
15267c478bd9Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
15277c478bd9Sstevel@tonic-gate 
1528f53ad214Skchow 	/*
1529f53ad214Skchow 	 * orig_npages is non-zero if physmem has been configured for less
1530f53ad214Skchow 	 * than the available memory.
1531f53ad214Skchow 	 */
1532f53ad214Skchow 	if (orig_npages) {
153335b1ab99Sjosephb 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
153435b1ab99Sjosephb 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
153535b1ab99Sjosephb 		    npages, orig_npages);
1536f53ad214Skchow 	}
15377c478bd9Sstevel@tonic-gate #if defined(__i386)
15387c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
15397c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
15407c478bd9Sstevel@tonic-gate 		    "System using 0x%lx",
15417c478bd9Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
15427c478bd9Sstevel@tonic-gate #endif
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
15457c478bd9Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
15467c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
15477c478bd9Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
15487c478bd9Sstevel@tonic-gate 	}
15497c478bd9Sstevel@tonic-gate #endif
15507c478bd9Sstevel@tonic-gate 
1551a3114836SGerry Liu #ifndef __xpv
1552a3114836SGerry Liu 	if (plat_dr_support_memory()) {
1553a3114836SGerry Liu 		mem_config_init();
1554a3114836SGerry Liu 	}
1555a3114836SGerry Liu #else	/* __xpv */
1556843e1988Sjohnlev 	/*
1557843e1988Sjohnlev 	 * Some of the xen start information has to be relocated up
1558843e1988Sjohnlev 	 * into the kernel's permanent address space.
1559843e1988Sjohnlev 	 */
1560843e1988Sjohnlev 	PRM_POINT("calling xen_relocate_start_info()");
1561843e1988Sjohnlev 	xen_relocate_start_info();
1562843e1988Sjohnlev 	PRM_POINT("xen_relocate_start_info() done");
1563843e1988Sjohnlev 
1564843e1988Sjohnlev 	/*
1565843e1988Sjohnlev 	 * (Update the vcpu pointer in our cpu structure to point into
1566843e1988Sjohnlev 	 * the relocated shared info.)
1567843e1988Sjohnlev 	 */
1568843e1988Sjohnlev 	CPU->cpu_m.mcpu_vcpu_info =
1569843e1988Sjohnlev 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
1570a3114836SGerry Liu #endif	/* __xpv */
1571843e1988Sjohnlev 
1572ae115bc7Smrj 	PRM_POINT("startup_kmem() done");
15737c478bd9Sstevel@tonic-gate }
15747c478bd9Sstevel@tonic-gate 
1575551bc2a6Smrj #ifndef __xpv
1576551bc2a6Smrj /*
1577551bc2a6Smrj  * If we have detected that we are running in an HVM environment, we need
1578551bc2a6Smrj  * to prepend the PV driver directory to the module search path.
1579551bc2a6Smrj  */
1580551bc2a6Smrj #define	HVM_MOD_DIR "/platform/i86hvm/kernel"
1581551bc2a6Smrj static void
1582551bc2a6Smrj update_default_path()
1583551bc2a6Smrj {
1584551bc2a6Smrj 	char *current, *newpath;
1585551bc2a6Smrj 	int newlen;
1586551bc2a6Smrj 
1587551bc2a6Smrj 	/*
1588551bc2a6Smrj 	 * We are about to resync with krtld.  krtld will reset its
1589551bc2a6Smrj 	 * internal module search path iff Solaris has set default_path.
1590551bc2a6Smrj 	 * We want to be sure we're prepending this new directory to the
1591551bc2a6Smrj 	 * right search path.
1592551bc2a6Smrj 	 */
1593551bc2a6Smrj 	current = (default_path == NULL) ? kobj_module_path : default_path;
1594551bc2a6Smrj 
1595b6611e3bSStuart Maybee 	newlen = strlen(HVM_MOD_DIR) + strlen(current) + 2;
1596551bc2a6Smrj 	newpath = kmem_alloc(newlen, KM_SLEEP);
1597551bc2a6Smrj 	(void) strcpy(newpath, HVM_MOD_DIR);
1598551bc2a6Smrj 	(void) strcat(newpath, " ");
1599551bc2a6Smrj 	(void) strcat(newpath, current);
1600551bc2a6Smrj 
1601551bc2a6Smrj 	default_path = newpath;
1602551bc2a6Smrj }
1603551bc2a6Smrj #endif
1604551bc2a6Smrj 
16057c478bd9Sstevel@tonic-gate static void
16067c478bd9Sstevel@tonic-gate startup_modules(void)
16077c478bd9Sstevel@tonic-gate {
1608ed5289f9SKen Erickson 	int cnt;
16097c478bd9Sstevel@tonic-gate 	extern void prom_setup(void);
1610ed5289f9SKen Erickson 	int32_t v, h;
1611ed5289f9SKen Erickson 	char d[11];
1612ed5289f9SKen Erickson 	char *cp;
1613e4b86885SCheng Sean Ye 	cmi_hdl_t hdl;
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
1616843e1988Sjohnlev 
1617843e1988Sjohnlev #ifndef __xpv
16187c478bd9Sstevel@tonic-gate 	/*
16197c478bd9Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
16207c478bd9Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
16217c478bd9Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
16227c478bd9Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
16237c478bd9Sstevel@tonic-gate 	 */
16247c478bd9Sstevel@tonic-gate 	microfind();
1625551bc2a6Smrj 
162679ec9da8SYuri Pankov 	if ((get_hwenv() & HW_XEN_HVM) != 0)
1627551bc2a6Smrj 		update_default_path();
1628843e1988Sjohnlev #endif
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	/*
16317c478bd9Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
16327c478bd9Sstevel@tonic-gate 	 */
1633ae115bc7Smrj 	sgmtl(process_rtc_config_file());
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	/*
16367c478bd9Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
16377c478bd9Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
16387c478bd9Sstevel@tonic-gate 	 */
16397c478bd9Sstevel@tonic-gate 	param_calc(0);
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	mod_setup();
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	/*
16447c478bd9Sstevel@tonic-gate 	 * Initialize system parameters.
16457c478bd9Sstevel@tonic-gate 	 */
16467c478bd9Sstevel@tonic-gate 	param_init();
16477c478bd9Sstevel@tonic-gate 
16489acbbeafSnn 	/*
16499acbbeafSnn 	 * Initialize the default brands
16509acbbeafSnn 	 */
16519acbbeafSnn 	brand_init();
16529acbbeafSnn 
16537c478bd9Sstevel@tonic-gate 	/*
16547c478bd9Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
16557c478bd9Sstevel@tonic-gate 	 */
16567c478bd9Sstevel@tonic-gate 	maxmem = physmem;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	/*
16597c478bd9Sstevel@tonic-gate 	 * Initialize segment management stuff.
16607c478bd9Sstevel@tonic-gate 	 */
16617c478bd9Sstevel@tonic-gate 	seg_init();
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
16647c478bd9Sstevel@tonic-gate 		halt("Can't load specfs");
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
16677c478bd9Sstevel@tonic-gate 		halt("Can't load devfs");
16687c478bd9Sstevel@tonic-gate 
1669facf4a8dSllai 	if (modload("fs", "dev") == -1)
1670facf4a8dSllai 		halt("Can't load dev");
1671facf4a8dSllai 
167235a5a358SJonathan Adams 	if (modload("fs", "procfs") == -1)
167335a5a358SJonathan Adams 		halt("Can't load procfs");
167435a5a358SJonathan Adams 
167545916cd2Sjpk 	(void) modloadonly("sys", "lbl_edition");
167645916cd2Sjpk 
16777c478bd9Sstevel@tonic-gate 	dispinit();
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	/* Read cluster configuration data. */
16807c478bd9Sstevel@tonic-gate 	clconf_init();
16817c478bd9Sstevel@tonic-gate 
1682843e1988Sjohnlev #if defined(__xpv)
1683349b53ddSStuart Maybee 	(void) ec_init();
1684843e1988Sjohnlev 	gnttab_init();
1685843e1988Sjohnlev 	(void) xs_early_init();
1686843e1988Sjohnlev #endif /* __xpv */
1687843e1988Sjohnlev 
16887c478bd9Sstevel@tonic-gate 	/*
16897c478bd9Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
16907c478bd9Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
16917c478bd9Sstevel@tonic-gate 	 */
16927c478bd9Sstevel@tonic-gate 	setup_ddi();
16937aec1d6eScindi 
1694074bb90dSTom Pothier #ifdef __xpv
1695074bb90dSTom Pothier 	if (DOMAIN_IS_INITDOMAIN(xen_info))
1696074bb90dSTom Pothier #endif
1697074bb90dSTom Pothier 	{
1698e41d648bSToomas Soome 		id_t smid;
1699e41d648bSToomas Soome 		smbios_system_t smsys;
1700e41d648bSToomas Soome 		smbios_info_t sminfo;
1701e41d648bSToomas Soome 		char *mfg;
1702074bb90dSTom Pothier 		/*
1703074bb90dSTom Pothier 		 * Load the System Management BIOS into the global ksmbios
1704074bb90dSTom Pothier 		 * handle, if an SMBIOS is present on this system.
1705e41d648bSToomas Soome 		 * Also set "si-hw-provider" property, if not already set.
1706074bb90dSTom Pothier 		 */
1707074bb90dSTom Pothier 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
1708e41d648bSToomas Soome 		if (ksmbios != NULL &&
1709e41d648bSToomas Soome 		    ((smid = smbios_info_system(ksmbios, &smsys)) != SMB_ERR) &&
1710e41d648bSToomas Soome 		    (smbios_info_common(ksmbios, smid, &sminfo)) != SMB_ERR) {
1711e41d648bSToomas Soome 			mfg = (char *)sminfo.smbi_manufacturer;
1712e41d648bSToomas Soome 			if (BOP_GETPROPLEN(bootops, "si-hw-provider") < 0) {
1713e41d648bSToomas Soome 				extern char hw_provider[];
1714e41d648bSToomas Soome 				int i;
1715e41d648bSToomas Soome 				for (i = 0; i < SYS_NMLN; i++) {
1716e41d648bSToomas Soome 					if (isprint(mfg[i]))
1717e41d648bSToomas Soome 						hw_provider[i] = mfg[i];
1718e41d648bSToomas Soome 					else {
1719e41d648bSToomas Soome 						hw_provider[i] = '\0';
1720e41d648bSToomas Soome 						break;
1721e41d648bSToomas Soome 					}
1722e41d648bSToomas Soome 				}
1723e41d648bSToomas Soome 				hw_provider[SYS_NMLN - 1] = '\0';
1724e41d648bSToomas Soome 			}
1725e41d648bSToomas Soome 		}
1726074bb90dSTom Pothier 	}
1727074bb90dSTom Pothier 
1728074bb90dSTom Pothier 
17299e3e4df2SGarrett D'Amore 	/*
17309e3e4df2SGarrett D'Amore 	 * Originally clconf_init() apparently needed the hostid.  But
17319e3e4df2SGarrett D'Amore 	 * this no longer appears to be true - it uses its own nodeid.
17329e3e4df2SGarrett D'Amore 	 * By placing the hostid logic here, we are able to make use of
17339e3e4df2SGarrett D'Amore 	 * the SMBIOS UUID.
17349e3e4df2SGarrett D'Amore 	 */
17359e3e4df2SGarrett D'Amore 	if ((h = set_soft_hostid()) == HW_INVALID_HOSTID) {
17369e3e4df2SGarrett D'Amore 		cmn_err(CE_WARN, "Unable to set hostid");
17379e3e4df2SGarrett D'Amore 	} else {
17389e3e4df2SGarrett D'Amore 		for (v = h, cnt = 0; cnt < 10; cnt++) {
17399e3e4df2SGarrett D'Amore 			d[cnt] = (char)(v % 10);
17409e3e4df2SGarrett D'Amore 			v /= 10;
17419e3e4df2SGarrett D'Amore 			if (v == 0)
17429e3e4df2SGarrett D'Amore 				break;
17439e3e4df2SGarrett D'Amore 		}
17449e3e4df2SGarrett D'Amore 		for (cp = hw_serial; cnt >= 0; cnt--)
17459e3e4df2SGarrett D'Amore 			*cp++ = d[cnt] + '0';
17469e3e4df2SGarrett D'Amore 		*cp = 0;
17479e3e4df2SGarrett D'Amore 	}
17489e3e4df2SGarrett D'Amore 
1749e4b86885SCheng Sean Ye 	/*
1750e4b86885SCheng Sean Ye 	 * Set up the CPU module subsystem for the boot cpu in the native
1751e4b86885SCheng Sean Ye 	 * case, and all physical cpu resource in the xpv dom0 case.
1752e4b86885SCheng Sean Ye 	 * Modifies the device tree, so this must be done after
1753e4b86885SCheng Sean Ye 	 * setup_ddi().
1754e4b86885SCheng Sean Ye 	 */
1755e4b86885SCheng Sean Ye #ifdef __xpv
1756e4b86885SCheng Sean Ye 	/*
1757e4b86885SCheng Sean Ye 	 * If paravirtualized and on dom0 then we initialize all physical
1758e4b86885SCheng Sean Ye 	 * cpu handles now;  if paravirtualized on a domU then do not
1759e4b86885SCheng Sean Ye 	 * initialize.
1760e4b86885SCheng Sean Ye 	 */
1761e4b86885SCheng Sean Ye 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1762e4b86885SCheng Sean Ye 		xen_mc_lcpu_cookie_t cpi;
1763e4b86885SCheng Sean Ye 
1764e4b86885SCheng Sean Ye 		for (cpi = xen_physcpu_next(NULL); cpi != NULL;
1765e4b86885SCheng Sean Ye 		    cpi = xen_physcpu_next(cpi)) {
1766e4b86885SCheng Sean Ye 			if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
1767e4b86885SCheng Sean Ye 			    xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
1768e4b86885SCheng Sean Ye 			    xen_physcpu_strandid(cpi))) != NULL &&
17697417cfdeSKuriakose Kuruvilla 			    is_x86_feature(x86_featureset, X86FSET_MCA))
177020c794b3Sgavinm 				cmi_mca_init(hdl);
177120c794b3Sgavinm 		}
177220c794b3Sgavinm 	}
1773e4b86885SCheng Sean Ye #else
1774e4b86885SCheng Sean Ye 	/*
1775e4b86885SCheng Sean Ye 	 * Initialize a handle for the boot cpu - others will initialize
1776d6694327SYuri Pankov 	 * as they startup.
1777e4b86885SCheng Sean Ye 	 */
1778d6694327SYuri Pankov 	if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
1779d6694327SYuri Pankov 	    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
1780d6694327SYuri Pankov 		if (is_x86_feature(x86_featureset, X86FSET_MCA))
1781e4b86885SCheng Sean Ye 			cmi_mca_init(hdl);
1782d6694327SYuri Pankov 		CPU->cpu_m.mcpu_cmi_hdl = hdl;
1783a3114836SGerry Liu 	}
178420c794b3Sgavinm #endif	/* __xpv */
17857aec1d6eScindi 
17867c478bd9Sstevel@tonic-gate 	/*
17877c478bd9Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
17887c478bd9Sstevel@tonic-gate 	 */
1789ae115bc7Smrj 	PRM_POINT("startup_modules: calling prom_setup...");
17907c478bd9Sstevel@tonic-gate 	prom_setup();
1791ae115bc7Smrj 	PRM_POINT("startup_modules: done");
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	/*
17947c478bd9Sstevel@tonic-gate 	 * Load all platform specific modules
17957c478bd9Sstevel@tonic-gate 	 */
1796ae115bc7Smrj 	PRM_POINT("startup_modules: calling psm_modload...");
17977c478bd9Sstevel@tonic-gate 	psm_modload();
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
18007c478bd9Sstevel@tonic-gate }
18017c478bd9Sstevel@tonic-gate 
1802ae115bc7Smrj /*
1803ae115bc7Smrj  * claim a "setaside" boot page for use in the kernel
1804ae115bc7Smrj  */
1805ae115bc7Smrj page_t *
1806ae115bc7Smrj boot_claim_page(pfn_t pfn)
18077c478bd9Sstevel@tonic-gate {
1808ae115bc7Smrj 	page_t *pp;
18097c478bd9Sstevel@tonic-gate 
1810ae115bc7Smrj 	pp = page_numtopp_nolock(pfn);
1811ae115bc7Smrj 	ASSERT(pp != NULL);
18127c478bd9Sstevel@tonic-gate 
1813ae115bc7Smrj 	if (PP_ISBOOTPAGES(pp)) {
1814ae115bc7Smrj 		if (pp->p_next != NULL)
1815ae115bc7Smrj 			pp->p_next->p_prev = pp->p_prev;
1816ae115bc7Smrj 		if (pp->p_prev == NULL)
1817ae115bc7Smrj 			bootpages = pp->p_next;
1818ae115bc7Smrj 		else
1819ae115bc7Smrj 			pp->p_prev->p_next = pp->p_next;
1820ae115bc7Smrj 	} else {
1821ae115bc7Smrj 		/*
1822ae115bc7Smrj 		 * htable_attach() expects a base pagesize page
1823ae115bc7Smrj 		 */
1824ae115bc7Smrj 		if (pp->p_szc != 0)
1825ae115bc7Smrj 			page_boot_demote(pp);
1826ae115bc7Smrj 		pp = page_numtopp(pfn, SE_EXCL);
1827ae115bc7Smrj 	}
1828ae115bc7Smrj 	return (pp);
18297c478bd9Sstevel@tonic-gate }
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate /*
18327c478bd9Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
18337c478bd9Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
18347c478bd9Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
18357c478bd9Sstevel@tonic-gate  */
18367c478bd9Sstevel@tonic-gate static void
18377c478bd9Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
18387c478bd9Sstevel@tonic-gate {
18397c478bd9Sstevel@tonic-gate 	uintptr_t va = low;
18407c478bd9Sstevel@tonic-gate 	size_t len;
18417c478bd9Sstevel@tonic-gate 	uint_t prot;
18427c478bd9Sstevel@tonic-gate 	pfn_t pfn;
18437c478bd9Sstevel@tonic-gate 	page_t *pp;
18447c478bd9Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
18457c478bd9Sstevel@tonic-gate 
1846ae115bc7Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
18477c478bd9Sstevel@tonic-gate 		if (va + len >= high)
18487c478bd9Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
18497c478bd9Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 		while (len > 0) {
18527c478bd9Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
18537c478bd9Sstevel@tonic-gate 			if (pp != NULL) {
18547c478bd9Sstevel@tonic-gate 				if (setaside == 0)
18557c478bd9Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
18567c478bd9Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
18577c478bd9Sstevel@tonic-gate 					    (void *)va, pfn);
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 				pp->p_next = bootpages;
1860ae115bc7Smrj 				pp->p_prev = NULL;
1861ae115bc7Smrj 				PP_SETBOOTPAGES(pp);
1862ae115bc7Smrj 				if (bootpages != NULL) {
1863ae115bc7Smrj 					bootpages->p_prev = pp;
1864ae115bc7Smrj 				}
18657c478bd9Sstevel@tonic-gate 				bootpages = pp;
18667c478bd9Sstevel@tonic-gate 				++boot_protect_cnt;
18677c478bd9Sstevel@tonic-gate 			}
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 			++pfn;
18707c478bd9Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
18717c478bd9Sstevel@tonic-gate 			va += MMU_PAGESIZE;
18727c478bd9Sstevel@tonic-gate 		}
18737c478bd9Sstevel@tonic-gate 	}
18747c478bd9Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
18757c478bd9Sstevel@tonic-gate }
18767c478bd9Sstevel@tonic-gate 
1877ae115bc7Smrj /*
187804909c8cSJohn Levon  * Establish the final size of the kernel's heap, size of segmap, segkp, etc.
1879ae115bc7Smrj  */
18807c478bd9Sstevel@tonic-gate static void
188135b1ab99Sjosephb layout_kernel_va(void)
18827c478bd9Sstevel@tonic-gate {
188304909c8cSJohn Levon 	const size_t physmem_size = mmu_ptob(physmem);
188404909c8cSJohn Levon 	size_t size;
18857c478bd9Sstevel@tonic-gate 
188604909c8cSJohn Levon 	PRM_POINT("layout_kernel_va() starting...");
18877c478bd9Sstevel@tonic-gate 
188835b1ab99Sjosephb 	kpm_vbase = (caddr_t)segkpm_base;
188904909c8cSJohn Levon 	kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
189035b1ab99Sjosephb 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
189135b1ab99Sjosephb 		panic("not enough room for kpm!");
1892ae115bc7Smrj 	PRM_DEBUG(kpm_size);
1893ae115bc7Smrj 	PRM_DEBUG(kpm_vbase);
18947c478bd9Sstevel@tonic-gate 
1895ae115bc7Smrj 	segkp_base = (caddr_t)valloc_base + valloc_sz;
18967c478bd9Sstevel@tonic-gate 	if (!segkp_fromheap) {
189704909c8cSJohn Levon 		size = mmu_ptob(segkpsize);
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 		/*
1900ae115bc7Smrj 		 * determine size of segkp
19017c478bd9Sstevel@tonic-gate 		 */
190204909c8cSJohn Levon 		if (size < SEGKPMINSIZE || size > SEGKPMAXSIZE) {
190304909c8cSJohn Levon 			size = SEGKPDEFSIZE;
19047c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
19057c478bd9Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
190604909c8cSJohn Levon 			    mmu_btop(size));
19077c478bd9Sstevel@tonic-gate 		}
190804909c8cSJohn Levon 		size = MIN(size, MAX(SEGKPMINSIZE, physmem_size));
19097c478bd9Sstevel@tonic-gate 
191004909c8cSJohn Levon 		segkpsize = mmu_btop(ROUND_UP_LPAGE(size));
19117c478bd9Sstevel@tonic-gate 	}
1912ae115bc7Smrj 	PRM_DEBUG(segkp_base);
1913ae115bc7Smrj 	PRM_DEBUG(segkpsize);
19147c478bd9Sstevel@tonic-gate 
191535b1ab99Sjosephb 	/*
191604909c8cSJohn Levon 	 * segkvmm: backing for vmm guest memory. Like segzio, we have a
191704909c8cSJohn Levon 	 * separate segment for two reasons: it makes it easy to skip our pages
191804909c8cSJohn Levon 	 * on kernel crash dumps, and it helps avoid fragmentation.  With this
191904909c8cSJohn Levon 	 * segment, we're expecting significantly-sized allocations only; we'll
192004909c8cSJohn Levon 	 * default to 4x the size of physmem.
192135b1ab99Sjosephb 	 */
192204909c8cSJohn Levon 	segkvmm_base = segkp_base + mmu_ptob(segkpsize);
192304909c8cSJohn Levon 	size = segkvmmsize != 0 ? mmu_ptob(segkvmmsize) : (physmem_size * 4);
192404909c8cSJohn Levon 
192504909c8cSJohn Levon 	size = MAX(size, SEGVMMMINSIZE);
192604909c8cSJohn Levon 	segkvmmsize = mmu_btop(ROUND_UP_LPAGE(size));
192704909c8cSJohn Levon 
192804909c8cSJohn Levon 	PRM_DEBUG(segkvmmsize);
192904909c8cSJohn Levon 	PRM_DEBUG(segkvmm_base);
193004909c8cSJohn Levon 
193104909c8cSJohn Levon 	/*
193204909c8cSJohn Levon 	 * segzio is used for ZFS cached data.  For segzio, we use 1.5x physmem.
193304909c8cSJohn Levon 	 */
193404909c8cSJohn Levon 	segzio_base = segkvmm_base + mmu_ptob(segkvmmsize);
1935ae115bc7Smrj 	if (segzio_fromheap) {
1936ae115bc7Smrj 		segziosize = 0;
1937ae115bc7Smrj 	} else {
193804909c8cSJohn Levon 		size = (segziosize != 0) ? mmu_ptob(segziosize) :
193904909c8cSJohn Levon 		    (physmem_size * 3) / 2;
1940ad23a2dbSjohansen 
194104909c8cSJohn Levon 		size = MAX(size, SEGZIOMINSIZE);
1942ad23a2dbSjohansen 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
1943ad23a2dbSjohansen 	}
1944ae115bc7Smrj 	PRM_DEBUG(segziosize);
1945ae115bc7Smrj 	PRM_DEBUG(segzio_base);
1946ad23a2dbSjohansen 
19477c478bd9Sstevel@tonic-gate 	/*
1948ae115bc7Smrj 	 * Put the range of VA for device mappings next, kmdb knows to not
1949ae115bc7Smrj 	 * grep in this range of addresses.
19507c478bd9Sstevel@tonic-gate 	 */
1951ae115bc7Smrj 	toxic_addr =
1952ae115bc7Smrj 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
19537c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
1954ae115bc7Smrj 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	/*
195752eef812SAmrita Sadhukhan 	 * Users can change segmapsize through eeprom. If the variable
195852eef812SAmrita Sadhukhan 	 * is tuned through eeprom, there is no upper bound on the
1959ed5289f9SKen Erickson 	 * size of segmap.
19607c478bd9Sstevel@tonic-gate 	 */
19617c478bd9Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
19627c478bd9Sstevel@tonic-gate 
1963ae115bc7Smrj 	PRM_DEBUG(segmap_start);
19647c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segmapsize);
196535b1ab99Sjosephb 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
196635b1ab99Sjosephb 	PRM_DEBUG(kernelheap);
196735b1ab99Sjosephb 	PRM_POINT("layout_kernel_va() done...");
196835b1ab99Sjosephb }
196935b1ab99Sjosephb 
197035b1ab99Sjosephb /*
197135b1ab99Sjosephb  * Finish initializing the VM system, now that we are no longer
197235b1ab99Sjosephb  * relying on the boot time memory allocators.
197335b1ab99Sjosephb  */
197435b1ab99Sjosephb static void
197535b1ab99Sjosephb startup_vm(void)
197635b1ab99Sjosephb {
197735b1ab99Sjosephb 	struct segmap_crargs a;
197835b1ab99Sjosephb 
197935b1ab99Sjosephb 	extern int use_brk_lpg, use_stk_lpg;
198035b1ab99Sjosephb 
198135b1ab99Sjosephb 	PRM_POINT("startup_vm() starting...");
198235b1ab99Sjosephb 
198335b1ab99Sjosephb 	/*
198435b1ab99Sjosephb 	 * Initialize the hat layer.
198535b1ab99Sjosephb 	 */
198635b1ab99Sjosephb 	hat_init();
19877c478bd9Sstevel@tonic-gate 
1988ae115bc7Smrj 	/*
1989ae115bc7Smrj 	 * Do final allocations of HAT data structures that need to
1990ae115bc7Smrj 	 * be allocated before quiescing the boot loader.
1991ae115bc7Smrj 	 */
1992ae115bc7Smrj 	PRM_POINT("Calling hat_kern_alloc()...");
1993ae115bc7Smrj 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
1994ae115bc7Smrj 	PRM_POINT("hat_kern_alloc() done");
1995ae115bc7Smrj 
1996843e1988Sjohnlev #ifndef __xpv
1997ae115bc7Smrj 	/*
19981d03c31eSjohnlev 	 * Setup Page Attribute Table
1999ae115bc7Smrj 	 */
20001d03c31eSjohnlev 	pat_sync();
2001843e1988Sjohnlev #endif
2002ae115bc7Smrj 
2003ae115bc7Smrj 	/*
2004ae115bc7Smrj 	 * The next two loops are done in distinct steps in order
2005ae115bc7Smrj 	 * to be sure that any page that is doubly mapped (both above
2006ae115bc7Smrj 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
2007ae115bc7Smrj 	 * Note this may never happen, but it might someday.
2008ae115bc7Smrj 	 */
2009ae115bc7Smrj 	bootpages = NULL;
2010ae115bc7Smrj 	PRM_POINT("Protecting boot pages");
2011ae115bc7Smrj 
2012ae115bc7Smrj 	/*
2013ae115bc7Smrj 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
2014ae115bc7Smrj 	 * page_t's. This can only happen if something weird allocated
2015ae115bc7Smrj 	 * in this range (like kadb/kmdb).
2016ae115bc7Smrj 	 */
2017ae115bc7Smrj 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
2018ae115bc7Smrj 
2019ae115bc7Smrj 	/*
2020ae115bc7Smrj 	 * Before we can take over memory allocation/mapping from the boot
2021ae115bc7Smrj 	 * loader we must remove from our free page lists any boot allocated
2022ae115bc7Smrj 	 * pages that stay mapped until release_bootstrap().
2023ae115bc7Smrj 	 */
2024ae115bc7Smrj 	protect_boot_range(0, kernelbase, 1);
2025ae115bc7Smrj 
2026843e1988Sjohnlev 
2027ae115bc7Smrj 	/*
2028ae115bc7Smrj 	 * Switch to running on regular HAT (not boot_mmu)
2029ae115bc7Smrj 	 */
2030ae115bc7Smrj 	PRM_POINT("Calling hat_kern_setup()...");
2031ae115bc7Smrj 	hat_kern_setup();
2032ae115bc7Smrj 
2033ae115bc7Smrj 	/*
2034ae115bc7Smrj 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
2035ae115bc7Smrj 	 */
2036ae115bc7Smrj 	bop_no_more_mem();
2037ae115bc7Smrj 
2038ae115bc7Smrj 	PRM_POINT("hat_kern_setup() done");
2039ae115bc7Smrj 
2040ae115bc7Smrj 	hat_cpu_online(CPU);
2041ae115bc7Smrj 
20427c478bd9Sstevel@tonic-gate 	/*
20437c478bd9Sstevel@tonic-gate 	 * Initialize VM system
20447c478bd9Sstevel@tonic-gate 	 */
20457c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
20467c478bd9Sstevel@tonic-gate 	kvm_init();
20477c478bd9Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	/*
20507c478bd9Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
20517c478bd9Sstevel@tonic-gate 	 */
20527c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
20537c478bd9Sstevel@tonic-gate 		kdi_dvec_vmready();
20547c478bd9Sstevel@tonic-gate 
2055843e1988Sjohnlev #if defined(__xpv)
2056843e1988Sjohnlev 	/*
2057843e1988Sjohnlev 	 * Populate the I/O pool on domain 0
2058843e1988Sjohnlev 	 */
2059843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
2060843e1988Sjohnlev 		extern long populate_io_pool(void);
2061843e1988Sjohnlev 		long init_io_pool_cnt;
2062843e1988Sjohnlev 
2063843e1988Sjohnlev 		PRM_POINT("Populating reserve I/O page pool");
2064843e1988Sjohnlev 		init_io_pool_cnt = populate_io_pool();
2065843e1988Sjohnlev 		PRM_DEBUG(init_io_pool_cnt);
2066843e1988Sjohnlev 	}
2067843e1988Sjohnlev #endif
20687c478bd9Sstevel@tonic-gate 	/*
20697c478bd9Sstevel@tonic-gate 	 * Mangle the brand string etc.
20707c478bd9Sstevel@tonic-gate 	 */
20717c478bd9Sstevel@tonic-gate 	cpuid_pass3(CPU);
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate #if defined(__amd64)
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/*
20767c478bd9Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
20777c478bd9Sstevel@tonic-gate 	 */
20787c478bd9Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
20797c478bd9Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate #else	/* __i386 */
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	/*
20847c478bd9Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
20857c478bd9Sstevel@tonic-gate 	 */
2086ae115bc7Smrj 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
20877c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
20887c478bd9Sstevel@tonic-gate 	toxic_bit_map =
20897c478bd9Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
20907c478bd9Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
20917c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate #endif	/* __i386 */
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	/*
20977c478bd9Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
20987c478bd9Sstevel@tonic-gate 	 * it, tell the debugger.
20997c478bd9Sstevel@tonic-gate 	 */
21007c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
21017c478bd9Sstevel@tonic-gate 		kdi_dvec_memavail();
21027c478bd9Sstevel@tonic-gate 
2103843e1988Sjohnlev #if !defined(__xpv)
21047c478bd9Sstevel@tonic-gate 	/*
21057c478bd9Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
21067c478bd9Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
21077c478bd9Sstevel@tonic-gate 	 */
21087c478bd9Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
21097c478bd9Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
21107c478bd9Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
2111843e1988Sjohnlev #endif
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
21147c478bd9Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
21157c478bd9Sstevel@tonic-gate 
211683f9b804Skchow 	/*
211783f9b804Skchow 	 * disable automatic large pages for small memory systems or
211883f9b804Skchow 	 * when the disable flag is set.
211902bc52beSkchow 	 *
212002bc52beSkchow 	 * Do not yet consider page sizes larger than 2m/4m.
212183f9b804Skchow 	 */
2122ec25b48fSsusans 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
2123ec25b48fSsusans 		max_uheap_lpsize = LEVEL_SIZE(1);
2124ec25b48fSsusans 		max_ustack_lpsize = LEVEL_SIZE(1);
2125ec25b48fSsusans 		max_privmap_lpsize = LEVEL_SIZE(1);
2126ec25b48fSsusans 		max_uidata_lpsize = LEVEL_SIZE(1);
2127ec25b48fSsusans 		max_utext_lpsize = LEVEL_SIZE(1);
2128ec25b48fSsusans 		max_shm_lpsize = LEVEL_SIZE(1);
2129ec25b48fSsusans 	}
2130ec25b48fSsusans 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
2131ec25b48fSsusans 	    auto_lpg_disable) {
2132beb1bda0Sdavemq 		use_brk_lpg = 0;
2133beb1bda0Sdavemq 		use_stk_lpg = 0;
2134ec25b48fSsusans 	}
213502bc52beSkchow 	mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
2136beb1bda0Sdavemq 
21377c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
21387c478bd9Sstevel@tonic-gate 	hat_init_finish();
21397c478bd9Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	/*
21427c478bd9Sstevel@tonic-gate 	 * Initialize the segkp segment type.
21437c478bd9Sstevel@tonic-gate 	 */
21447c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
2145ae115bc7Smrj 	PRM_POINT("Attaching segkp");
2146ae115bc7Smrj 	if (segkp_fromheap) {
21477c478bd9Sstevel@tonic-gate 		segkp->s_as = &kas;
2148ae115bc7Smrj 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
2149ae115bc7Smrj 	    segkp) < 0) {
2150ae115bc7Smrj 		panic("startup: cannot attach segkp");
2151ae115bc7Smrj 		/*NOTREACHED*/
21527c478bd9Sstevel@tonic-gate 	}
2153ae115bc7Smrj 	PRM_POINT("Doing segkp_create()");
21547c478bd9Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
21557c478bd9Sstevel@tonic-gate 		panic("startup: segkp_create failed");
21567c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
21577c478bd9Sstevel@tonic-gate 	}
21587c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segkp);
21597c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	/*
21627c478bd9Sstevel@tonic-gate 	 * kpm segment
21637c478bd9Sstevel@tonic-gate 	 */
21647c478bd9Sstevel@tonic-gate 	segmap_kpm = 0;
216574ecdb51SJohn Levon 	if (kpm_desired)
21667c478bd9Sstevel@tonic-gate 		kpm_init();
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	/*
21697c478bd9Sstevel@tonic-gate 	 * Now create segmap segment.
21707c478bd9Sstevel@tonic-gate 	 */
21717c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
2172ae115bc7Smrj 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
2173ae115bc7Smrj 		panic("cannot attach segmap");
21747c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
21757c478bd9Sstevel@tonic-gate 	}
2176ae115bc7Smrj 	PRM_DEBUG(segmap);
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
21797c478bd9Sstevel@tonic-gate 	a.shmsize = 0;
21807c478bd9Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
21817c478bd9Sstevel@tonic-gate 
2182ae115bc7Smrj 	if (segmap_create(segmap, (caddr_t)&a) != 0)
2183ae115bc7Smrj 		panic("segmap_create segmap");
21847c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	segdev_init();
2189843e1988Sjohnlev #if defined(__xpv)
2190843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2191843e1988Sjohnlev #endif
2192843e1988Sjohnlev 		pmem_init();
2193ae115bc7Smrj 
21947c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
21957c478bd9Sstevel@tonic-gate }
21967c478bd9Sstevel@tonic-gate 
2197ae115bc7Smrj /*
2198ae115bc7Smrj  * Load a tod module for the non-standard tod part found on this system.
2199ae115bc7Smrj  */
2200ae115bc7Smrj static void
2201ae115bc7Smrj load_tod_module(char *todmod)
2202ae115bc7Smrj {
2203ae115bc7Smrj 	if (modload("tod", todmod) == -1)
2204ae115bc7Smrj 		halt("Can't load TOD module");
2205ae115bc7Smrj }
2206ae115bc7Smrj 
22077c478bd9Sstevel@tonic-gate static void
22087c478bd9Sstevel@tonic-gate startup_end(void)
22097c478bd9Sstevel@tonic-gate {
2210dd4eeefdSeota 	int i;
22117c478bd9Sstevel@tonic-gate 	extern void setx86isalist(void);
2212fb2caebeSRandy Fishel 	extern void cpu_event_init(void);
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	/*
22177c478bd9Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
22187c478bd9Sstevel@tonic-gate 	 * initialization has been done but before the clock
22197c478bd9Sstevel@tonic-gate 	 * and other devices get started.
22207c478bd9Sstevel@tonic-gate 	 */
22217c478bd9Sstevel@tonic-gate 	kern_setup1();
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 	/*
22247c478bd9Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
22257c478bd9Sstevel@tonic-gate 	 */
22267c478bd9Sstevel@tonic-gate 	kcpc_hw_init(CPU);
22277c478bd9Sstevel@tonic-gate 
2228fb2caebeSRandy Fishel 	/*
2229fb2caebeSRandy Fishel 	 * Initialize cpu event framework.
2230fb2caebeSRandy Fishel 	 */
2231fb2caebeSRandy Fishel 	cpu_event_init();
2232fb2caebeSRandy Fishel 
2233ee88d2b9Skchow #if defined(OPTERON_WORKAROUND_6323525)
2234ee88d2b9Skchow 	if (opteron_workaround_6323525)
2235ee88d2b9Skchow 		patch_workaround_6323525();
2236ee88d2b9Skchow #endif
2237ae115bc7Smrj 	/*
2238ae115bc7Smrj 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
2239ae115bc7Smrj 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
2240ae115bc7Smrj 	 */
2241ae115bc7Smrj 	if (tod_module_name != NULL) {
2242ae115bc7Smrj 		PRM_POINT("load_tod_module()");
2243ae115bc7Smrj 		load_tod_module(tod_module_name);
2244ae115bc7Smrj 	}
2245ae115bc7Smrj 
2246843e1988Sjohnlev #if defined(__xpv)
2247843e1988Sjohnlev 	/*
2248843e1988Sjohnlev 	 * Forceload interposing TOD module for the hypervisor.
2249843e1988Sjohnlev 	 */
2250843e1988Sjohnlev 	PRM_POINT("load_tod_module()");
2251843e1988Sjohnlev 	load_tod_module("xpvtod");
2252843e1988Sjohnlev #endif
2253843e1988Sjohnlev 
22547c478bd9Sstevel@tonic-gate 	/*
22557c478bd9Sstevel@tonic-gate 	 * Configure the system.
22567c478bd9Sstevel@tonic-gate 	 */
22577c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
22587c478bd9Sstevel@tonic-gate 	configure();		/* set up devices */
22597c478bd9Sstevel@tonic-gate 	PRM_POINT("configure() done");
22607c478bd9Sstevel@tonic-gate 
22617af88ac7SKuriakose Kuruvilla 	/*
22627af88ac7SKuriakose Kuruvilla 	 * We can now setup for XSAVE because fpu_probe is done in configure().
22637af88ac7SKuriakose Kuruvilla 	 */
22647af88ac7SKuriakose Kuruvilla 	if (fp_save_mech == FP_XSAVE) {
2265*fdcca78fSJoshua M. Clulow 		PRM_POINT("xsave_setup_msr()");
22667af88ac7SKuriakose Kuruvilla 		xsave_setup_msr(CPU);
22677af88ac7SKuriakose Kuruvilla 	}
22687af88ac7SKuriakose Kuruvilla 
22697c478bd9Sstevel@tonic-gate 	/*
22707c478bd9Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
22717c478bd9Sstevel@tonic-gate 	 * support.
22727c478bd9Sstevel@tonic-gate 	 */
22737c478bd9Sstevel@tonic-gate 	setx86isalist();
2274*fdcca78fSJoshua M. Clulow 	PRM_POINT("cpu_intr_alloc()");
2275100b72f4Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
2276*fdcca78fSJoshua M. Clulow 	PRM_POINT("psm_install()");
22777c478bd9Sstevel@tonic-gate 	psm_install();
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	/*
22807c478bd9Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
22817c478bd9Sstevel@tonic-gate 	 * we're still using bootsvcs.
22827c478bd9Sstevel@tonic-gate 	 */
2283ae115bc7Smrj 	PRM_POINT("NULLing out bootops");
2284ae115bc7Smrj 	*bootopsp = (struct bootops *)NULL;
22857c478bd9Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
22867c478bd9Sstevel@tonic-gate 
2287843e1988Sjohnlev #if defined(__xpv)
2288843e1988Sjohnlev 	ec_init_debug_irq();
2289843e1988Sjohnlev 	xs_domu_init();
2290843e1988Sjohnlev #endif
22911de082f7SVikram Hegde 
229274ecdb51SJohn Levon #if !defined(__xpv)
22933a634bfcSVikram Hegde 	/*
22943a634bfcSVikram Hegde 	 * Intel IOMMU has been setup/initialized in ddi_impl.c
22953a634bfcSVikram Hegde 	 * Start it up now.
22963a634bfcSVikram Hegde 	 */
22973a634bfcSVikram Hegde 	immu_startup();
229874ecdb51SJohn Levon 
229974ecdb51SJohn Levon 	/*
230074ecdb51SJohn Levon 	 * Now that we're no longer going to drop into real mode for a BIOS call
230174ecdb51SJohn Levon 	 * via bootops, we can enable PCID (which requires CR0.PG).
230274ecdb51SJohn Levon 	 */
230374ecdb51SJohn Levon 	enable_pcid();
23041de082f7SVikram Hegde #endif
23053a634bfcSVikram Hegde 
23067c478bd9Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
23077c478bd9Sstevel@tonic-gate 	(*picinitf)();
23087c478bd9Sstevel@tonic-gate 	sti();
2309843e1988Sjohnlev #if defined(__xpv)
2310843e1988Sjohnlev 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
2311843e1988Sjohnlev 	xen_late_startup();
2312843e1988Sjohnlev #endif
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
231535b1ab99Sjosephb 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
23167c478bd9Sstevel@tonic-gate 
2317dd4eeefdSeota 	/*
2318a288e5a9SJoshua M. Clulow 	 * Register software interrupt handlers for ddi_periodic_add(9F).
2319dd4eeefdSeota 	 * Software interrupts up to the level 10 are supported.
2320dd4eeefdSeota 	 */
2321dd4eeefdSeota 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
2322dd4eeefdSeota 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
2323027bcc9fSToomas Soome 		    (avfunc)(uintptr_t)ddi_periodic_softintr, "ddi_periodic",
2324a288e5a9SJoshua M. Clulow 		    (caddr_t)(uintptr_t)i, NULL);
2325dd4eeefdSeota 	}
2326dd4eeefdSeota 
23277125fcbdSVikram Hegde #if !defined(__xpv)
23287125fcbdSVikram Hegde 	if (modload("drv", "amd_iommu") < 0) {
23297125fcbdSVikram Hegde 		PRM_POINT("No AMD IOMMU present\n");
233094f1124eSVikram Hegde 	} else if (ddi_hold_installed_driver(ddi_name_to_major(
233194f1124eSVikram Hegde 	    "amd_iommu")) == NULL) {
23327125fcbdSVikram Hegde 		prom_printf("ERROR: failed to attach AMD IOMMU\n");
23337125fcbdSVikram Hegde 	}
23347125fcbdSVikram Hegde #endif
2335e774b42bSBill Holler 	post_startup_cpu_fixups();
23367125fcbdSVikram Hegde 
23377c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
23387c478bd9Sstevel@tonic-gate }
23397c478bd9Sstevel@tonic-gate 
234096d008c2SKen Erickson /*
234196d008c2SKen Erickson  * Don't remove the following 2 variables.  They are necessary
234296d008c2SKen Erickson  * for reading the hostid from the legacy file (/kernel/misc/sysinit).
234396d008c2SKen Erickson  */
23447c478bd9Sstevel@tonic-gate char *_hs1107 = hw_serial;
23457c478bd9Sstevel@tonic-gate ulong_t  _bdhs34;
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate void
23487c478bd9Sstevel@tonic-gate post_startup(void)
23497c478bd9Sstevel@tonic-gate {
23500e751525SEric Saxe 	extern void cpupm_init(cpu_t *);
2351fb2caebeSRandy Fishel 	extern void cpu_event_init_cpu(cpu_t *);
23520e751525SEric Saxe 
23537c478bd9Sstevel@tonic-gate 	/*
23547c478bd9Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
23557c478bd9Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
23567c478bd9Sstevel@tonic-gate 	 * instruction set extensions.
23577c478bd9Sstevel@tonic-gate 	 */
23587c478bd9Sstevel@tonic-gate 	bind_hwcap();
23597c478bd9Sstevel@tonic-gate 
2360843e1988Sjohnlev #ifdef __xpv
2361843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2362843e1988Sjohnlev #endif
2363843e1988Sjohnlev 	{
2364843e1988Sjohnlev #if defined(__xpv)
2365843e1988Sjohnlev 		xpv_panic_init();
2366843e1988Sjohnlev #else
2367843e1988Sjohnlev 		/*
2368843e1988Sjohnlev 		 * Startup the memory scrubber.
2369843e1988Sjohnlev 		 * XXPV	This should be running somewhere ..
2370843e1988Sjohnlev 		 */
237179ec9da8SYuri Pankov 		if ((get_hwenv() & HW_VIRTUAL) == 0)
2372e4b86885SCheng Sean Ye 			memscrub_init();
2373843e1988Sjohnlev #endif
2374843e1988Sjohnlev 	}
23757c478bd9Sstevel@tonic-gate 
23767aec1d6eScindi 	/*
23777aec1d6eScindi 	 * Complete CPU module initialization
23787aec1d6eScindi 	 */
237920c794b3Sgavinm 	cmi_post_startup();
23807aec1d6eScindi 
23817c478bd9Sstevel@tonic-gate 	/*
238284ab085aSmws 	 * Perform forceloading tasks for /etc/system.
23837c478bd9Sstevel@tonic-gate 	 */
238484ab085aSmws 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	/*
23877c478bd9Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
23887c478bd9Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
23897c478bd9Sstevel@tonic-gate 	 */
23907c478bd9Sstevel@tonic-gate 	(void) modload("fs", "procfs");
23917c478bd9Sstevel@tonic-gate 
2392c35aa225Smarx 	(void) i_ddi_attach_hw_nodes("pit_beep");
2393c35aa225Smarx 
23947c478bd9Sstevel@tonic-gate #if defined(__i386)
23957c478bd9Sstevel@tonic-gate 	/*
23967c478bd9Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
23977c478bd9Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
23987c478bd9Sstevel@tonic-gate 	 */
23997c478bd9Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
24007c478bd9Sstevel@tonic-gate 		halt("No working FP hardware found");
24017c478bd9Sstevel@tonic-gate #endif
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	maxmem = freemem;
24047c478bd9Sstevel@tonic-gate 
2405fb2caebeSRandy Fishel 	cpu_event_init_cpu(CPU);
24060e751525SEric Saxe 	cpupm_init(CPU);
2407fa96bd91SMichael Corcoran 	(void) mach_cpu_create_device_node(CPU, NULL);
24080e751525SEric Saxe 
24090e751525SEric Saxe 	pg_init();
24107c478bd9Sstevel@tonic-gate }
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate static int
241319397407SSherry Moore pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
24147c478bd9Sstevel@tonic-gate {
241519397407SSherry Moore 	return ((pp->p_pagenum >= btop(low_addr)) &&
241619397407SSherry Moore 	    (pp->p_pagenum < btopr(high_addr)));
24177c478bd9Sstevel@tonic-gate }
24187c478bd9Sstevel@tonic-gate 
24190181461bSKeith M Wesolowski static int
24200181461bSKeith M Wesolowski pp_in_module(page_t *pp, const rd_existing_t *modranges)
24210181461bSKeith M Wesolowski {
24220181461bSKeith M Wesolowski 	uint_t i;
24230181461bSKeith M Wesolowski 
24240181461bSKeith M Wesolowski 	for (i = 0; modranges[i].phys != 0; i++) {
24250181461bSKeith M Wesolowski 		if (pp_in_range(pp, modranges[i].phys,
24260181461bSKeith M Wesolowski 		    modranges[i].phys + modranges[i].size))
24270181461bSKeith M Wesolowski 			return (1);
24280181461bSKeith M Wesolowski 	}
24290181461bSKeith M Wesolowski 
24300181461bSKeith M Wesolowski 	return (0);
24310181461bSKeith M Wesolowski }
24320181461bSKeith M Wesolowski 
24337c478bd9Sstevel@tonic-gate void
24347c478bd9Sstevel@tonic-gate release_bootstrap(void)
24357c478bd9Sstevel@tonic-gate {
24367c478bd9Sstevel@tonic-gate 	int root_is_ramdisk;
24377c478bd9Sstevel@tonic-gate 	page_t *pp;
24387c478bd9Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
24397c478bd9Sstevel@tonic-gate 	extern dev_t rootdev;
24400181461bSKeith M Wesolowski 	uint_t i;
24410181461bSKeith M Wesolowski 	char propname[32];
24420181461bSKeith M Wesolowski 	rd_existing_t *modranges;
244355d507a9SSeth Goldberg #if !defined(__xpv)
244455d507a9SSeth Goldberg 	pfn_t	pfn;
244555d507a9SSeth Goldberg #endif
24467c478bd9Sstevel@tonic-gate 
24470181461bSKeith M Wesolowski 	/*
24480181461bSKeith M Wesolowski 	 * Save the bootfs module ranges so that we can reserve them below
24490181461bSKeith M Wesolowski 	 * for the real bootfs.
24500181461bSKeith M Wesolowski 	 */
24510181461bSKeith M Wesolowski 	modranges = kmem_alloc(sizeof (rd_existing_t) * MAX_BOOT_MODULES,
24520181461bSKeith M Wesolowski 	    KM_SLEEP);
24530181461bSKeith M Wesolowski 	for (i = 0; ; i++) {
24540181461bSKeith M Wesolowski 		uint64_t start, size;
24550181461bSKeith M Wesolowski 
24560181461bSKeith M Wesolowski 		modranges[i].phys = 0;
24570181461bSKeith M Wesolowski 
24580181461bSKeith M Wesolowski 		(void) snprintf(propname, sizeof (propname),
24590181461bSKeith M Wesolowski 		    "module-addr-%u", i);
24600181461bSKeith M Wesolowski 		if (do_bsys_getproplen(NULL, propname) <= 0)
24610181461bSKeith M Wesolowski 			break;
24620181461bSKeith M Wesolowski 		(void) do_bsys_getprop(NULL, propname, &start);
24630181461bSKeith M Wesolowski 
24640181461bSKeith M Wesolowski 		(void) snprintf(propname, sizeof (propname),
24650181461bSKeith M Wesolowski 		    "module-size-%u", i);
24660181461bSKeith M Wesolowski 		if (do_bsys_getproplen(NULL, propname) <= 0)
24670181461bSKeith M Wesolowski 			break;
24680181461bSKeith M Wesolowski 		(void) do_bsys_getprop(NULL, propname, &size);
24690181461bSKeith M Wesolowski 
24700181461bSKeith M Wesolowski 		modranges[i].phys = start;
24710181461bSKeith M Wesolowski 		modranges[i].size = size;
24720181461bSKeith M Wesolowski 	}
24730181461bSKeith M Wesolowski 
24747c478bd9Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
24757c478bd9Sstevel@tonic-gate 	kobj_boot_unmountroot();
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	/*
24787c478bd9Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
24797c478bd9Sstevel@tonic-gate 	 */
24807c478bd9Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
248119397407SSherry Moore 
2482ae115bc7Smrj 	clear_boot_mappings(0, _userlimit);
248319397407SSherry Moore 
2484ae115bc7Smrj 	postbootkernelbase = kernelbase;
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	/*
24877c478bd9Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
24887c478bd9Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
24897c478bd9Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
24907c478bd9Sstevel@tonic-gate 	 */
24917c478bd9Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
24927c478bd9Sstevel@tonic-gate 	if (!root_is_ramdisk) {
24937c478bd9Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
24947c478bd9Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
24957c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
24967c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
24977c478bd9Sstevel@tonic-gate 	}
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
25007c478bd9Sstevel@tonic-gate 	while (bootpages) {
250119397407SSherry Moore 		extern uint64_t ramdisk_start, ramdisk_end;
25027c478bd9Sstevel@tonic-gate 		pp = bootpages;
25037c478bd9Sstevel@tonic-gate 		bootpages = pp->p_next;
250419397407SSherry Moore 
250519397407SSherry Moore 
250619397407SSherry Moore 		/* Keep pages for the lower 64K */
250719397407SSherry Moore 		if (pp_in_range(pp, 0, 0x40000)) {
250819397407SSherry Moore 			pp->p_next = lower_pages;
250919397407SSherry Moore 			lower_pages = pp;
251019397407SSherry Moore 			lower_pages_count++;
251119397407SSherry Moore 			continue;
251219397407SSherry Moore 		}
251319397407SSherry Moore 
251419397407SSherry Moore 		if (root_is_ramdisk && pp_in_range(pp, ramdisk_start,
25150181461bSKeith M Wesolowski 		    ramdisk_end) || pp_in_module(pp, modranges)) {
25167c478bd9Sstevel@tonic-gate 			pp->p_next = rd_pages;
25177c478bd9Sstevel@tonic-gate 			rd_pages = pp;
25187c478bd9Sstevel@tonic-gate 			continue;
25197c478bd9Sstevel@tonic-gate 		}
25207c478bd9Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
2521ae115bc7Smrj 		pp->p_prev = (struct page *)0;
2522ae115bc7Smrj 		PP_CLRBOOTPAGES(pp);
25237c478bd9Sstevel@tonic-gate 		page_free(pp, 1);
25247c478bd9Sstevel@tonic-gate 	}
2525ae115bc7Smrj 	PRM_POINT("Boot pages released");
25267c478bd9Sstevel@tonic-gate 
25270181461bSKeith M Wesolowski 	kmem_free(modranges, sizeof (rd_existing_t) * 99);
25280181461bSKeith M Wesolowski 
2529843e1988Sjohnlev #if !defined(__xpv)
2530843e1988Sjohnlev /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
25317c478bd9Sstevel@tonic-gate 	/*
253255d507a9SSeth Goldberg 	 * Find 1 page below 1 MB so that other processors can boot up or
253355d507a9SSeth Goldberg 	 * so that any processor can resume.
25347c478bd9Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
25357c478bd9Sstevel@tonic-gate 	 * We should have just free'd one up.
25367c478bd9Sstevel@tonic-gate 	 */
253719397407SSherry Moore 
253819397407SSherry Moore 	/*
253919397407SSherry Moore 	 * 0x10 pages is 64K.  Leave the bottom 64K alone
254019397407SSherry Moore 	 * for BIOS.
254119397407SSherry Moore 	 */
254219397407SSherry Moore 	for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
254355d507a9SSeth Goldberg 		if (page_numtopp_alloc(pfn) == NULL)
254455d507a9SSeth Goldberg 			continue;
254555d507a9SSeth Goldberg 		rm_platter_va = i86devmap(pfn, 1,
254655d507a9SSeth Goldberg 		    PROT_READ | PROT_WRITE | PROT_EXEC);
254755d507a9SSeth Goldberg 		rm_platter_pa = ptob(pfn);
254855d507a9SSeth Goldberg 		break;
25497c478bd9Sstevel@tonic-gate 	}
255055d507a9SSeth Goldberg 	if (pfn == btop(1*1024*1024) && use_mp)
255155d507a9SSeth Goldberg 		panic("No page below 1M available for starting "
255255d507a9SSeth Goldberg 		    "other processors or for resuming from system-suspend");
2553843e1988Sjohnlev #endif	/* !__xpv */
25547c478bd9Sstevel@tonic-gate }
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate /*
25577c478bd9Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
25587c478bd9Sstevel@tonic-gate  */
25597c478bd9Sstevel@tonic-gate void
25607c478bd9Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
25617c478bd9Sstevel@tonic-gate {
25627c478bd9Sstevel@tonic-gate 	pp->p_pagenum = pnum;
25637c478bd9Sstevel@tonic-gate 	pp->p_mapping = NULL;
25647c478bd9Sstevel@tonic-gate 	pp->p_embed = 0;
25657c478bd9Sstevel@tonic-gate 	pp->p_share = 0;
25667c478bd9Sstevel@tonic-gate 	pp->p_mlentry = 0;
25677c478bd9Sstevel@tonic-gate }
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate /*
25707c478bd9Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
25717c478bd9Sstevel@tonic-gate  */
25727c478bd9Sstevel@tonic-gate static pgcnt_t
2573584b574aSToomas Soome kphysm_init(page_t *pp, pgcnt_t npages)
25747c478bd9Sstevel@tonic-gate {
25757c478bd9Sstevel@tonic-gate 	struct memlist	*pmem;
25767c478bd9Sstevel@tonic-gate 	struct memseg	*cur_memseg;
25777c478bd9Sstevel@tonic-gate 	pfn_t		base_pfn;
257818968004SKit Chow 	pfn_t		end_pfn;
25797c478bd9Sstevel@tonic-gate 	pgcnt_t		num;
25807c478bd9Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
25817c478bd9Sstevel@tonic-gate 	uint64_t	addr;
25827c478bd9Sstevel@tonic-gate 	uint64_t	size;
25837c478bd9Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
258418968004SKit Chow 	extern int	mnode_xwa;
258518968004SKit Chow 	int		ms = 0, me = 0;
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
25887c478bd9Sstevel@tonic-gate 
2589ae115bc7Smrj 	cur_memseg = memseg_base;
259056f33205SJonathan Adams 	for (pmem = phys_avail; pmem && npages; pmem = pmem->ml_next) {
25917c478bd9Sstevel@tonic-gate 		/*
25927c478bd9Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
25937c478bd9Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
25947c478bd9Sstevel@tonic-gate 		 */
259556f33205SJonathan Adams 		addr = pmem->ml_address;
259656f33205SJonathan Adams 		size = pmem->ml_size;
25977c478bd9Sstevel@tonic-gate 		if (btop(addr) > physmax)
25987c478bd9Sstevel@tonic-gate 			continue;
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 		/*
26017c478bd9Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
26027c478bd9Sstevel@tonic-gate 		 */
26037c478bd9Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
26047c478bd9Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
26057c478bd9Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
260656f33205SJonathan Adams 			size -= addr - pmem->ml_address;
26077c478bd9Sstevel@tonic-gate 		}
26087c478bd9Sstevel@tonic-gate 
26096bb54764Skchow 		/* only process pages below or equal to physmax */
26106bb54764Skchow 		if ((btop(addr + size) - 1) > physmax)
26116bb54764Skchow 			size = ptob(physmax - btop(addr) + 1);
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 		num = btop(size);
26147c478bd9Sstevel@tonic-gate 		if (num == 0)
26157c478bd9Sstevel@tonic-gate 			continue;
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate 		if (num > npages)
26187c478bd9Sstevel@tonic-gate 			num = npages;
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 		npages -= num;
26217c478bd9Sstevel@tonic-gate 		pages_done += num;
26227c478bd9Sstevel@tonic-gate 		base_pfn = btop(addr);
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 		if (prom_debug)
26257c478bd9Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
26267c478bd9Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
26277c478bd9Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 		/*
2630ae115bc7Smrj 		 * Ignore pages below ddiphysmin to simplify ddi memory
26317c478bd9Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
26327c478bd9Sstevel@tonic-gate 		 */
26337c478bd9Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
2634ae115bc7Smrj 			if (base_pfn + num <= ddiphysmin)
26357c478bd9Sstevel@tonic-gate 				continue;
26367c478bd9Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
26377c478bd9Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
26387c478bd9Sstevel@tonic-gate 			base_pfn = ddiphysmin;
26397c478bd9Sstevel@tonic-gate 		}
2640ae115bc7Smrj 
26417c478bd9Sstevel@tonic-gate 		/*
264218968004SKit Chow 		 * mnode_xwa is greater than 1 when large pages regions can
264318968004SKit Chow 		 * cross memory node boundaries. To prevent the formation
264418968004SKit Chow 		 * of these large pages, configure the memsegs based on the
264518968004SKit Chow 		 * memory node ranges which had been made non-contiguous.
26467c478bd9Sstevel@tonic-gate 		 */
2647584b574aSToomas Soome 		end_pfn = base_pfn + num - 1;
264818968004SKit Chow 		if (mnode_xwa > 1) {
264918968004SKit Chow 			ms = PFN_2_MEM_NODE(base_pfn);
265018968004SKit Chow 			me = PFN_2_MEM_NODE(end_pfn);
265118968004SKit Chow 
265218968004SKit Chow 			if (ms != me) {
265318968004SKit Chow 				/*
265418968004SKit Chow 				 * current range spans more than 1 memory node.
265518968004SKit Chow 				 * Set num to only the pfn range in the start
265618968004SKit Chow 				 * memory node.
265718968004SKit Chow 				 */
265818968004SKit Chow 				num = mem_node_config[ms].physmax - base_pfn
265918968004SKit Chow 				    + 1;
266018968004SKit Chow 				ASSERT(end_pfn > mem_node_config[ms].physmax);
266118968004SKit Chow 			}
26627c478bd9Sstevel@tonic-gate 		}
26637c478bd9Sstevel@tonic-gate 
266418968004SKit Chow 		for (;;) {
266518968004SKit Chow 			/*
266618968004SKit Chow 			 * Build the memsegs entry
266718968004SKit Chow 			 */
266818968004SKit Chow 			cur_memseg->pages = pp;
266918968004SKit Chow 			cur_memseg->epages = pp + num;
267018968004SKit Chow 			cur_memseg->pages_base = base_pfn;
267118968004SKit Chow 			cur_memseg->pages_end = base_pfn + num;
267218968004SKit Chow 
267318968004SKit Chow 			/*
267418968004SKit Chow 			 * Insert into memseg list in decreasing pfn range
267518968004SKit Chow 			 * order. Low memory is typically more fragmented such
267618968004SKit Chow 			 * that this ordering keeps the larger ranges at the
267718968004SKit Chow 			 * front of the list for code that searches memseg.
267818968004SKit Chow 			 * This ASSERTS that the memsegs coming in from boot
267918968004SKit Chow 			 * are in increasing physical address order and not
268018968004SKit Chow 			 * contiguous.
268118968004SKit Chow 			 */
268218968004SKit Chow 			if (memsegs != NULL) {
268318968004SKit Chow 				ASSERT(cur_memseg->pages_base >=
268418968004SKit Chow 				    memsegs->pages_end);
268518968004SKit Chow 				cur_memseg->next = memsegs;
268618968004SKit Chow 			}
268718968004SKit Chow 			memsegs = cur_memseg;
268818968004SKit Chow 
268918968004SKit Chow 			/*
269018968004SKit Chow 			 * add_physmem() initializes the PSM part of the page
269118968004SKit Chow 			 * struct by calling the PSM back with add_physmem_cb().
269218968004SKit Chow 			 * In addition it coalesces pages into larger pages as
269318968004SKit Chow 			 * it initializes them.
269418968004SKit Chow 			 */
269518968004SKit Chow 			add_physmem(pp, num, base_pfn);
269618968004SKit Chow 			cur_memseg++;
269718968004SKit Chow 			availrmem_initial += num;
269818968004SKit Chow 			availrmem += num;
269918968004SKit Chow 
270018968004SKit Chow 			pp += num;
270118968004SKit Chow 			if (ms >= me)
270218968004SKit Chow 				break;
27037c478bd9Sstevel@tonic-gate 
270418968004SKit Chow 			/* process next memory node range */
270518968004SKit Chow 			ms++;
270618968004SKit Chow 			base_pfn = mem_node_config[ms].physbase;
2707584b574aSToomas Soome 
2708584b574aSToomas Soome 			if (mnode_xwa > 1) {
2709584b574aSToomas Soome 				num = MIN(mem_node_config[ms].physmax,
2710584b574aSToomas Soome 				    end_pfn) - base_pfn + 1;
2711584b574aSToomas Soome 			} else {
2712584b574aSToomas Soome 				num = mem_node_config[ms].physmax -
2713584b574aSToomas Soome 				    base_pfn + 1;
2714584b574aSToomas Soome 			}
271518968004SKit Chow 		}
27167c478bd9Sstevel@tonic-gate 	}
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
27197c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
27207c478bd9Sstevel@tonic-gate 	PRM_DEBUG(freemem);
27217c478bd9Sstevel@tonic-gate 	build_pfn_hash();
27227c478bd9Sstevel@tonic-gate 	return (pages_done);
27237c478bd9Sstevel@tonic-gate }
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate /*
27267c478bd9Sstevel@tonic-gate  * Kernel VM initialization.
27277c478bd9Sstevel@tonic-gate  */
27287c478bd9Sstevel@tonic-gate static void
27297c478bd9Sstevel@tonic-gate kvm_init(void)
27307c478bd9Sstevel@tonic-gate {
27317c478bd9Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 	/*
27347c478bd9Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
27357c478bd9Sstevel@tonic-gate 	 */
27367c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
27377c478bd9Sstevel@tonic-gate 	as_avlinit(&kas);
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
27407c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
27437c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
27447c478bd9Sstevel@tonic-gate 
274535b1ab99Sjosephb 	(void) seg_attach(&kas, kernelheap,
274635b1ab99Sjosephb 	    ekernelheap - kernelheap, &kvseg);
27477c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
27487c478bd9Sstevel@tonic-gate 
2749ae115bc7Smrj 	if (core_size > 0) {
2750ae115bc7Smrj 		PRM_POINT("attaching kvseg_core");
2751ae115bc7Smrj 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
2752ae115bc7Smrj 		    &kvseg_core);
2753ae115bc7Smrj 		(void) segkmem_create(&kvseg_core);
2754ae115bc7Smrj 	}
2755ad23a2dbSjohansen 
275604909c8cSJohn Levon 	PRM_POINT("attaching segkvmm");
275704909c8cSJohn Levon 	(void) seg_attach(&kas, segkvmm_base, mmu_ptob(segkvmmsize), &kvmmseg);
275804909c8cSJohn Levon 	(void) segkmem_create(&kvmmseg);
275904909c8cSJohn Levon 	segkmem_kvmm_init(segkvmm_base, mmu_ptob(segkvmmsize));
276004909c8cSJohn Levon 
2761ae115bc7Smrj 	if (segziosize > 0) {
2762ae115bc7Smrj 		PRM_POINT("attaching segzio");
2763ad23a2dbSjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2764ad23a2dbSjohansen 		    &kzioseg);
276504909c8cSJohn Levon 		(void) segkmem_create(&kzioseg);
2766ad23a2dbSjohansen 
2767ad23a2dbSjohansen 		/* create zio area covering new segment */
2768ad23a2dbSjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2769ad23a2dbSjohansen 	}
27707c478bd9Sstevel@tonic-gate 
2771ae115bc7Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
27727c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 	/*
27777c478bd9Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
27787c478bd9Sstevel@tonic-gate 	 */
2779ae115bc7Smrj 	PRM_POINT("protecting redzone");
27807c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 	/*
27837c478bd9Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
27847c478bd9Sstevel@tonic-gate 	 */
27857c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
27867c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
27877c478bd9Sstevel@tonic-gate 
27887c478bd9Sstevel@tonic-gate 	/*
27897c478bd9Sstevel@tonic-gate 	 * Make data writable until end.
27907c478bd9Sstevel@tonic-gate 	 */
27917c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
27927c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
27937c478bd9Sstevel@tonic-gate }
27947c478bd9Sstevel@tonic-gate 
2795843e1988Sjohnlev #ifndef __xpv
27967c478bd9Sstevel@tonic-gate /*
27971d03c31eSjohnlev  * Solaris adds an entry for Write Combining caching to the PAT
27987c478bd9Sstevel@tonic-gate  */
27991d03c31eSjohnlev static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate void
28021d03c31eSjohnlev pat_sync(void)
28037c478bd9Sstevel@tonic-gate {
28041d03c31eSjohnlev 	ulong_t	cr0, cr0_orig, cr4;
28057c478bd9Sstevel@tonic-gate 
28067417cfdeSKuriakose Kuruvilla 	if (!is_x86_feature(x86_featureset, X86FSET_PAT))
28077c478bd9Sstevel@tonic-gate 		return;
28081d03c31eSjohnlev 	cr0_orig = cr0 = getcr0();
28091d03c31eSjohnlev 	cr4 = getcr4();
28107c478bd9Sstevel@tonic-gate 
28111d03c31eSjohnlev 	/* disable caching and flush all caches and TLBs */
28121d03c31eSjohnlev 	cr0 |= CR0_CD;
28131d03c31eSjohnlev 	cr0 &= ~CR0_NW;
28141d03c31eSjohnlev 	setcr0(cr0);
28151d03c31eSjohnlev 	invalidate_cache();
28161d03c31eSjohnlev 	if (cr4 & CR4_PGE) {
28171d03c31eSjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
28181d03c31eSjohnlev 		setcr4(cr4);
28191d03c31eSjohnlev 	} else {
28201d03c31eSjohnlev 		reload_cr3();
28217c478bd9Sstevel@tonic-gate 	}
28227c478bd9Sstevel@tonic-gate 
28231d03c31eSjohnlev 	/* add our entry to the PAT */
28241d03c31eSjohnlev 	wrmsr(REG_PAT, pat_attr_reg);
28257c478bd9Sstevel@tonic-gate 
28261d03c31eSjohnlev 	/* flush TLBs and cache again, then reenable cr0 caching */
28271d03c31eSjohnlev 	if (cr4 & CR4_PGE) {
28281d03c31eSjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
28291d03c31eSjohnlev 		setcr4(cr4);
28301d03c31eSjohnlev 	} else {
28311d03c31eSjohnlev 		reload_cr3();
28327c478bd9Sstevel@tonic-gate 	}
28337c478bd9Sstevel@tonic-gate 	invalidate_cache();
28347c478bd9Sstevel@tonic-gate 	setcr0(cr0_orig);
28357c478bd9Sstevel@tonic-gate }
28367c478bd9Sstevel@tonic-gate 
28371d03c31eSjohnlev #endif /* !__xpv */
28387c478bd9Sstevel@tonic-gate 
2839ed5289f9SKen Erickson #if defined(_SOFT_HOSTID)
2840ed5289f9SKen Erickson /*
2841ed5289f9SKen Erickson  * On platforms that do not have a hardware serial number, attempt
2842ed5289f9SKen Erickson  * to set one based on the contents of /etc/hostid.  If this file does
2843ed5289f9SKen Erickson  * not exist, assume that we are to generate a new hostid and set
2844ed5289f9SKen Erickson  * it in the kernel, for subsequent saving by a userland process
2845ed5289f9SKen Erickson  * once the system is up and the root filesystem is mounted r/w.
2846ed5289f9SKen Erickson  *
284796d008c2SKen Erickson  * In order to gracefully support upgrade on OpenSolaris, if
284896d008c2SKen Erickson  * /etc/hostid does not exist, we will attempt to get a serial number
284996d008c2SKen Erickson  * using the legacy method (/kernel/misc/sysinit).
285096d008c2SKen Erickson  *
28519e3e4df2SGarrett D'Amore  * If that isn't present, we attempt to use an SMBIOS UUID, which is
28529e3e4df2SGarrett D'Amore  * a hardware serial number.  Note that we don't automatically trust
28539e3e4df2SGarrett D'Amore  * all SMBIOS UUIDs (some older platforms are defective and ship duplicate
28549e3e4df2SGarrett D'Amore  * UUIDs in violation of the standard), we check against a blacklist.
28559e3e4df2SGarrett D'Amore  *
2856ed5289f9SKen Erickson  * In an attempt to make the hostid less prone to abuse
2857ed5289f9SKen Erickson  * (for license circumvention, etc), we store it in /etc/hostid
2858ed5289f9SKen Erickson  * in rot47 format.
2859ed5289f9SKen Erickson  */
2860ed5289f9SKen Erickson extern volatile unsigned long tenmicrodata;
286196d008c2SKen Erickson static int atoi(char *);
2862ed5289f9SKen Erickson 
28639e3e4df2SGarrett D'Amore /*
28649e3e4df2SGarrett D'Amore  * Set this to non-zero in /etc/system if you think your SMBIOS returns a
28659e3e4df2SGarrett D'Amore  * UUID that is not unique. (Also report it so that the smbios_uuid_blacklist
28669e3e4df2SGarrett D'Amore  * array can be updated.)
28679e3e4df2SGarrett D'Amore  */
28689e3e4df2SGarrett D'Amore int smbios_broken_uuid = 0;
28699e3e4df2SGarrett D'Amore 
28709e3e4df2SGarrett D'Amore /*
28719e3e4df2SGarrett D'Amore  * List of known bad UUIDs.  This is just the lower 32-bit values, since
28729e3e4df2SGarrett D'Amore  * that's what we use for the host id.  If your hostid falls here, you need
28739e3e4df2SGarrett D'Amore  * to contact your hardware OEM for a fix for your BIOS.
28749e3e4df2SGarrett D'Amore  */
28759e3e4df2SGarrett D'Amore static unsigned char
28769e3e4df2SGarrett D'Amore smbios_uuid_blacklist[][16] = {
28779e3e4df2SGarrett D'Amore 
28789e3e4df2SGarrett D'Amore 	{	/* Reported bad UUID (Google search) */
28799e3e4df2SGarrett D'Amore 		0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05,
28809e3e4df2SGarrett D'Amore 		0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09,
28819e3e4df2SGarrett D'Amore 	},
28829e3e4df2SGarrett D'Amore 	{	/* Known bad DELL UUID */
28839e3e4df2SGarrett D'Amore 		0x4C, 0x4C, 0x45, 0x44, 0x00, 0x00, 0x20, 0x10,
28849e3e4df2SGarrett D'Amore 		0x80, 0x20, 0x80, 0xC0, 0x4F, 0x20, 0x20, 0x20,
28859e3e4df2SGarrett D'Amore 	},
28869e3e4df2SGarrett D'Amore 	{	/* Uninitialized flash */
28879e3e4df2SGarrett D'Amore 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
28889e3e4df2SGarrett D'Amore 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
28899e3e4df2SGarrett D'Amore 	},
28909e3e4df2SGarrett D'Amore 	{	/* All zeros */
28919e3e4df2SGarrett D'Amore 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28929e3e4df2SGarrett D'Amore 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
28939e3e4df2SGarrett D'Amore 	},
28949e3e4df2SGarrett D'Amore };
28959e3e4df2SGarrett D'Amore 
28969e3e4df2SGarrett D'Amore static int32_t
28979e3e4df2SGarrett D'Amore uuid_to_hostid(const uint8_t *uuid)
28989e3e4df2SGarrett D'Amore {
28999e3e4df2SGarrett D'Amore 	/*
29009e3e4df2SGarrett D'Amore 	 * Although the UUIDs are 128-bits, they may not distribute entropy
29019e3e4df2SGarrett D'Amore 	 * evenly.  We would like to use SHA or MD5, but those are located
29029e3e4df2SGarrett D'Amore 	 * in loadable modules and not available this early in boot.  As we
29039e3e4df2SGarrett D'Amore 	 * don't need the values to be cryptographically strong, we just
29049e3e4df2SGarrett D'Amore 	 * generate 32-bit vaue by xor'ing the various sequences together,
29052df37efaSGarrett D'Amore 	 * which ensures that the entire UUID contributes to the hostid.
29069e3e4df2SGarrett D'Amore 	 */
29072df37efaSGarrett D'Amore 	uint32_t	id = 0;
29089e3e4df2SGarrett D'Amore 
29099e3e4df2SGarrett D'Amore 	/* first check against the blacklist */
29109e3e4df2SGarrett D'Amore 	for (int i = 0; i < (sizeof (smbios_uuid_blacklist) / 16); i++) {
29119e3e4df2SGarrett D'Amore 		if (bcmp(smbios_uuid_blacklist[0], uuid, 16) == 0) {
29129e3e4df2SGarrett D'Amore 			cmn_err(CE_CONT, "?Broken SMBIOS UUID. "
29139e3e4df2SGarrett D'Amore 			    "Contact BIOS manufacturer for repair.\n");
29149e3e4df2SGarrett D'Amore 			return ((int32_t)HW_INVALID_HOSTID);
29159e3e4df2SGarrett D'Amore 		}
29169e3e4df2SGarrett D'Amore 	}
29179e3e4df2SGarrett D'Amore 
29189e3e4df2SGarrett D'Amore 	for (int i = 0; i < 16; i++)
29199e3e4df2SGarrett D'Amore 		id ^= ((uuid[i]) << (8 * (i % sizeof (id))));
29209e3e4df2SGarrett D'Amore 
29212df37efaSGarrett D'Amore 	/* Make sure return value is positive */
29222df37efaSGarrett D'Amore 	return (id & 0x7fffffff);
29239e3e4df2SGarrett D'Amore }
29249e3e4df2SGarrett D'Amore 
2925ed5289f9SKen Erickson static int32_t
2926ed5289f9SKen Erickson set_soft_hostid(void)
2927ed5289f9SKen Erickson {
2928ed5289f9SKen Erickson 	struct _buf *file;
2929ed5289f9SKen Erickson 	char tokbuf[MAXNAMELEN];
2930ed5289f9SKen Erickson 	token_t token;
2931ed5289f9SKen Erickson 	int done = 0;
2932ed5289f9SKen Erickson 	u_longlong_t tmp;
293396d008c2SKen Erickson 	int i;
29345679c89fSjv 	int32_t hostid = (int32_t)HW_INVALID_HOSTID;
2935ed5289f9SKen Erickson 	unsigned char *c;
2936ed5289f9SKen Erickson 	hrtime_t tsc;
29379e3e4df2SGarrett D'Amore 	smbios_system_t smsys;
2938ed5289f9SKen Erickson 
2939ed5289f9SKen Erickson 	/*
2940ed5289f9SKen Erickson 	 * If /etc/hostid file not found, we'd like to get a pseudo
2941ed5289f9SKen Erickson 	 * random number to use at the hostid.  A nice way to do this
2942ed5289f9SKen Erickson 	 * is to read the real time clock.  To remain xen-compatible,
2943ed5289f9SKen Erickson 	 * we can't poke the real hardware, so we use tsc_read() to
2944ed5289f9SKen Erickson 	 * read the real time clock.  However, there is an ominous
2945ed5289f9SKen Erickson 	 * warning in tsc_read that says it can return zero, so we
2946ed5289f9SKen Erickson 	 * deal with that possibility by falling back to using the
2947ed5289f9SKen Erickson 	 * (hopefully random enough) value in tenmicrodata.
2948ed5289f9SKen Erickson 	 */
2949ed5289f9SKen Erickson 
2950ed5289f9SKen Erickson 	if ((file = kobj_open_file(hostid_file)) == (struct _buf *)-1) {
295196d008c2SKen Erickson 		/*
295296d008c2SKen Erickson 		 * hostid file not found - try to load sysinit module
295396d008c2SKen Erickson 		 * and see if it has a nonzero hostid value...use that
295496d008c2SKen Erickson 		 * instead of generating a new hostid here if so.
295596d008c2SKen Erickson 		 */
295696d008c2SKen Erickson 		if ((i = modload("misc", "sysinit")) != -1) {
295796d008c2SKen Erickson 			if (strlen(hw_serial) > 0)
295896d008c2SKen Erickson 				hostid = (int32_t)atoi(hw_serial);
295996d008c2SKen Erickson 			(void) modunload(i);
296096d008c2SKen Erickson 		}
29619e3e4df2SGarrett D'Amore 
29629e3e4df2SGarrett D'Amore 		/*
29639e3e4df2SGarrett D'Amore 		 * We try to use the SMBIOS UUID. But not if it is blacklisted
29649e3e4df2SGarrett D'Amore 		 * in /etc/system.
29659e3e4df2SGarrett D'Amore 		 */
29669e3e4df2SGarrett D'Amore 		if ((hostid == HW_INVALID_HOSTID) &&
29679e3e4df2SGarrett D'Amore 		    (smbios_broken_uuid == 0) &&
29689e3e4df2SGarrett D'Amore 		    (ksmbios != NULL) &&
29699e3e4df2SGarrett D'Amore 		    (smbios_info_system(ksmbios, &smsys) != SMB_ERR) &&
29709e3e4df2SGarrett D'Amore 		    (smsys.smbs_uuidlen >= 16)) {
29719e3e4df2SGarrett D'Amore 			hostid = uuid_to_hostid(smsys.smbs_uuid);
29729e3e4df2SGarrett D'Amore 		}
29739e3e4df2SGarrett D'Amore 
29749e3e4df2SGarrett D'Amore 		/*
29759e3e4df2SGarrett D'Amore 		 * Generate a "random" hostid using the clock.  These
29769e3e4df2SGarrett D'Amore 		 * hostids will change on each boot if the value is not
29779e3e4df2SGarrett D'Amore 		 * saved to a persistent /etc/hostid file.
29789e3e4df2SGarrett D'Amore 		 */
29795679c89fSjv 		if (hostid == HW_INVALID_HOSTID) {
298096d008c2SKen Erickson 			tsc = tsc_read();
298196d008c2SKen Erickson 			if (tsc == 0)	/* tsc_read can return zero sometimes */
298296d008c2SKen Erickson 				hostid = (int32_t)tenmicrodata & 0x0CFFFFF;
298396d008c2SKen Erickson 			else
298496d008c2SKen Erickson 				hostid = (int32_t)tsc & 0x0CFFFFF;
298596d008c2SKen Erickson 		}
2986ed5289f9SKen Erickson 	} else {
2987ed5289f9SKen Erickson 		/* hostid file found */
2988ed5289f9SKen Erickson 		while (!done) {
2989ed5289f9SKen Erickson 			token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2990ed5289f9SKen Erickson 
2991ed5289f9SKen Erickson 			switch (token) {
2992ed5289f9SKen Erickson 			case POUND:
2993ed5289f9SKen Erickson 				/*
2994ed5289f9SKen Erickson 				 * skip comments
2995ed5289f9SKen Erickson 				 */
2996ed5289f9SKen Erickson 				kobj_find_eol(file);
2997ed5289f9SKen Erickson 				break;
2998ed5289f9SKen Erickson 			case STRING:
2999ed5289f9SKen Erickson 				/*
3000ed5289f9SKen Erickson 				 * un-rot47 - obviously this
3001ed5289f9SKen Erickson 				 * nonsense is ascii-specific
3002ed5289f9SKen Erickson 				 */
3003ed5289f9SKen Erickson 				for (c = (unsigned char *)tokbuf;
3004ed5289f9SKen Erickson 				    *c != '\0'; c++) {
3005ed5289f9SKen Erickson 					*c += 47;
3006ed5289f9SKen Erickson 					if (*c > '~')
3007ed5289f9SKen Erickson 						*c -= 94;
3008ed5289f9SKen Erickson 					else if (*c < '!')
3009ed5289f9SKen Erickson 						*c += 94;
3010ed5289f9SKen Erickson 				}
3011ed5289f9SKen Erickson 				/*
3012ed5289f9SKen Erickson 				 * now we should have a real number
3013ed5289f9SKen Erickson 				 */
3014ed5289f9SKen Erickson 
3015ed5289f9SKen Erickson 				if (kobj_getvalue(tokbuf, &tmp) != 0)
3016ed5289f9SKen Erickson 					kobj_file_err(CE_WARN, file,
3017ed5289f9SKen Erickson 					    "Bad value %s for hostid",
3018ed5289f9SKen Erickson 					    tokbuf);
3019ed5289f9SKen Erickson 				else
3020ed5289f9SKen Erickson 					hostid = (int32_t)tmp;
3021ed5289f9SKen Erickson 
3022ed5289f9SKen Erickson 				break;
3023ed5289f9SKen Erickson 			case EOF:
3024ed5289f9SKen Erickson 				done = 1;
3025ed5289f9SKen Erickson 				/* FALLTHROUGH */
3026ed5289f9SKen Erickson 			case NEWLINE:
3027ed5289f9SKen Erickson 				kobj_newline(file);
3028ed5289f9SKen Erickson 				break;
3029ed5289f9SKen Erickson 			default:
3030ed5289f9SKen Erickson 				break;
3031ed5289f9SKen Erickson 
3032ed5289f9SKen Erickson 			}
3033ed5289f9SKen Erickson 		}
30345679c89fSjv 		if (hostid == HW_INVALID_HOSTID) /* didn't find a hostid */
3035ed5289f9SKen Erickson 			kobj_file_err(CE_WARN, file,
3036ed5289f9SKen Erickson 			    "hostid missing or corrupt");
3037ed5289f9SKen Erickson 
3038ed5289f9SKen Erickson 		kobj_close_file(file);
3039ed5289f9SKen Erickson 	}
3040ed5289f9SKen Erickson 	/*
3041ed5289f9SKen Erickson 	 * hostid is now the value read from /etc/hostid, or the
30425679c89fSjv 	 * new hostid we generated in this routine or HW_INVALID_HOSTID if not
30435679c89fSjv 	 * set.
3044ed5289f9SKen Erickson 	 */
3045ed5289f9SKen Erickson 	return (hostid);
3046ed5289f9SKen Erickson }
304796d008c2SKen Erickson 
304896d008c2SKen Erickson static int
304996d008c2SKen Erickson atoi(char *p)
305096d008c2SKen Erickson {
305196d008c2SKen Erickson 	int i = 0;
305296d008c2SKen Erickson 
305396d008c2SKen Erickson 	while (*p != '\0')
305496d008c2SKen Erickson 		i = 10 * i + (*p++ - '0');
305596d008c2SKen Erickson 
305696d008c2SKen Erickson 	return (i);
305796d008c2SKen Erickson }
305896d008c2SKen Erickson 
3059ed5289f9SKen Erickson #endif /* _SOFT_HOSTID */
3060ed5289f9SKen Erickson 
30617c478bd9Sstevel@tonic-gate void
306245916cd2Sjpk get_system_configuration(void)
30637c478bd9Sstevel@tonic-gate {
30647c478bd9Sstevel@tonic-gate 	char	prop[32];
30657c478bd9Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
30667c478bd9Sstevel@tonic-gate 
306735b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
306835b1ab99Sjosephb 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
306935b1ab99Sjosephb 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
307035b1ab99Sjosephb 	    nodes_ll > MAXNODES ||
307135b1ab99Sjosephb 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
307235b1ab99Sjosephb 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
307335b1ab99Sjosephb 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
30747c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
30757c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
30767c478bd9Sstevel@tonic-gate 	} else {
30777c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
30787c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
30797c478bd9Sstevel@tonic-gate 	}
308035b1ab99Sjosephb 
308135b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
308235b1ab99Sjosephb 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
308335b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
30844da99751SToomas Soome 		eprom_kernelbase = 0;
30857c478bd9Sstevel@tonic-gate 	else
308635b1ab99Sjosephb 		eprom_kernelbase = (uintptr_t)lvalue;
30877c478bd9Sstevel@tonic-gate 
308835b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
308935b1ab99Sjosephb 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
309035b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
30917c478bd9Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
309235b1ab99Sjosephb 	else
30937c478bd9Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
30947c478bd9Sstevel@tonic-gate 
309535b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
309635b1ab99Sjosephb 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
309735b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
30987c478bd9Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
309935b1ab99Sjosephb 	else
31007c478bd9Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
31014944b02eSkchow 
3102aac11643Sjosephb 	/* physmem used to be here, but moved much earlier to fakebop.c */
31037c478bd9Sstevel@tonic-gate }
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate /*
31067c478bd9Sstevel@tonic-gate  * Add to a memory list.
31077c478bd9Sstevel@tonic-gate  * start = start of new memory segment
31087c478bd9Sstevel@tonic-gate  * len = length of new memory segment in bytes
31097c478bd9Sstevel@tonic-gate  * new = pointer to a new struct memlist
31107c478bd9Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
31117c478bd9Sstevel@tonic-gate  */
3112ae115bc7Smrj void
31137c478bd9Sstevel@tonic-gate memlist_add(
31147c478bd9Sstevel@tonic-gate 	uint64_t start,
31157c478bd9Sstevel@tonic-gate 	uint64_t len,
31167c478bd9Sstevel@tonic-gate 	struct memlist *new,
31177c478bd9Sstevel@tonic-gate 	struct memlist **memlistp)
31187c478bd9Sstevel@tonic-gate {
31197c478bd9Sstevel@tonic-gate 	struct memlist *cur;
31207c478bd9Sstevel@tonic-gate 	uint64_t end = start + len;
31217c478bd9Sstevel@tonic-gate 
312256f33205SJonathan Adams 	new->ml_address = start;
312356f33205SJonathan Adams 	new->ml_size = len;
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	cur = *memlistp;
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	while (cur) {
312856f33205SJonathan Adams 		if (cur->ml_address >= end) {
312956f33205SJonathan Adams 			new->ml_next = cur;
31307c478bd9Sstevel@tonic-gate 			*memlistp = new;
313156f33205SJonathan Adams 			new->ml_prev = cur->ml_prev;
313256f33205SJonathan Adams 			cur->ml_prev = new;
31337c478bd9Sstevel@tonic-gate 			return;
31347c478bd9Sstevel@tonic-gate 		}
313556f33205SJonathan Adams 		ASSERT(cur->ml_address + cur->ml_size <= start);
313656f33205SJonathan Adams 		if (cur->ml_next == NULL) {
313756f33205SJonathan Adams 			cur->ml_next = new;
313856f33205SJonathan Adams 			new->ml_prev = cur;
313956f33205SJonathan Adams 			new->ml_next = NULL;
31407c478bd9Sstevel@tonic-gate 			return;
31417c478bd9Sstevel@tonic-gate 		}
314256f33205SJonathan Adams 		memlistp = &cur->ml_next;
314356f33205SJonathan Adams 		cur = cur->ml_next;
31447c478bd9Sstevel@tonic-gate 	}
31457c478bd9Sstevel@tonic-gate }
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate void
31487c478bd9Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
31497c478bd9Sstevel@tonic-gate {
31507c478bd9Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
31517c478bd9Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
31547c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
31557c478bd9Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
31567c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
31577c478bd9Sstevel@tonic-gate }
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate caddr_t
31607c478bd9Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
31617c478bd9Sstevel@tonic-gate {
31627c478bd9Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
31637c478bd9Sstevel@tonic-gate }
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31667c478bd9Sstevel@tonic-gate caddr_t
31677c478bd9Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
31687c478bd9Sstevel@tonic-gate {
31697c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
31707c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
31717c478bd9Sstevel@tonic-gate 	return (0);
31727c478bd9Sstevel@tonic-gate }
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31757c478bd9Sstevel@tonic-gate void
31767c478bd9Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
31777c478bd9Sstevel@tonic-gate {
31787c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
31797c478bd9Sstevel@tonic-gate }
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate /*
31827c478bd9Sstevel@tonic-gate  * This is called just after configure() in startup().
31837c478bd9Sstevel@tonic-gate  *
31847c478bd9Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
31857c478bd9Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
31867c478bd9Sstevel@tonic-gate  * given that various parts of the instruction set may appear
31877c478bd9Sstevel@tonic-gate  * and disappear between different implementations.
31887c478bd9Sstevel@tonic-gate  *
31897c478bd9Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
31907c478bd9Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
31917c478bd9Sstevel@tonic-gate  * more flexibility.
31927c478bd9Sstevel@tonic-gate  *
31937c478bd9Sstevel@tonic-gate  * So, we just leave this alone.
31947c478bd9Sstevel@tonic-gate  */
31957c478bd9Sstevel@tonic-gate void
31967c478bd9Sstevel@tonic-gate setx86isalist(void)
31977c478bd9Sstevel@tonic-gate {
31987c478bd9Sstevel@tonic-gate 	char *tp;
31997c478bd9Sstevel@tonic-gate 	size_t len;
32007c478bd9Sstevel@tonic-gate 	extern char *isa_list;
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate #define	TBUFSIZE	1024
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
32057c478bd9Sstevel@tonic-gate 	*tp = '\0';
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate #if defined(__amd64)
32087c478bd9Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
32097c478bd9Sstevel@tonic-gate #endif
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate 	switch (x86_vendor) {
32127c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
32137c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
32147c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
32157417cfdeSKuriakose Kuruvilla 		if (is_x86_feature(x86_featureset, X86FSET_CMOV)) {
32167c478bd9Sstevel@tonic-gate 			/*
32177c478bd9Sstevel@tonic-gate 			 * Pentium Pro or later
32187c478bd9Sstevel@tonic-gate 			 */
32197c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
32207417cfdeSKuriakose Kuruvilla 			(void) strcat(tp,
32217417cfdeSKuriakose Kuruvilla 			    is_x86_feature(x86_featureset, X86FSET_MMX) ?
32227c478bd9Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
32237c478bd9Sstevel@tonic-gate 		}
32247c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
32257c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
32267c478bd9Sstevel@tonic-gate 		/*
32277c478bd9Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
32287c478bd9Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
32297c478bd9Sstevel@tonic-gate 		 */
32307417cfdeSKuriakose Kuruvilla 		if (is_x86_feature(x86_featureset, X86FSET_CPUID)) {
32317c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
32327417cfdeSKuriakose Kuruvilla 			(void) strcat(tp,
32337417cfdeSKuriakose Kuruvilla 			    is_x86_feature(x86_featureset, X86FSET_MMX) ?
32347c478bd9Sstevel@tonic-gate 			    "+mmx pentium " : " ");
32357c478bd9Sstevel@tonic-gate 		}
32367c478bd9Sstevel@tonic-gate 		break;
32377c478bd9Sstevel@tonic-gate 	default:
32387c478bd9Sstevel@tonic-gate 		break;
32397c478bd9Sstevel@tonic-gate 	}
32407c478bd9Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
32417c478bd9Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
32427c478bd9Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
32437c478bd9Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate #undef TBUFSIZE
32467c478bd9Sstevel@tonic-gate }
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate #ifdef __amd64
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate void *
32527c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
32537c478bd9Sstevel@tonic-gate {
32547c478bd9Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
32557c478bd9Sstevel@tonic-gate }
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate void
32587c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
32597c478bd9Sstevel@tonic-gate {
32607c478bd9Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
32617c478bd9Sstevel@tonic-gate }
32627c478bd9Sstevel@tonic-gate 
3263ae115bc7Smrj #else /* __i386 */
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate void *
32667c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
32677c478bd9Sstevel@tonic-gate {
32687c478bd9Sstevel@tonic-gate 	caddr_t	vaddr;
32697c478bd9Sstevel@tonic-gate 	uintptr_t v;
32707c478bd9Sstevel@tonic-gate 	size_t	start;
32717c478bd9Sstevel@tonic-gate 	size_t	end;
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
32747c478bd9Sstevel@tonic-gate 	if (vaddr == NULL)
32757c478bd9Sstevel@tonic-gate 		return (NULL);
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
32787c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
3279ae115bc7Smrj 	ASSERT(v + size <= valloc_base);
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
32827c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
32837c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
32847c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 	while (start <= end) {
32877c478bd9Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
32887c478bd9Sstevel@tonic-gate 		++start;
32897c478bd9Sstevel@tonic-gate 	}
32907c478bd9Sstevel@tonic-gate 	return (vaddr);
32917c478bd9Sstevel@tonic-gate }
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate void
32947c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
32957c478bd9Sstevel@tonic-gate {
32967c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
32977c478bd9Sstevel@tonic-gate 	size_t	start;
32987c478bd9Sstevel@tonic-gate 	size_t	end;
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
3301ae115bc7Smrj 	ASSERT(v + size <= valloc_base);
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
33047c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
33057c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
33067c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
33077c478bd9Sstevel@tonic-gate 
33087c478bd9Sstevel@tonic-gate 	while (start <= end) {
33097c478bd9Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
33107c478bd9Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
33117c478bd9Sstevel@tonic-gate 		++start;
33127c478bd9Sstevel@tonic-gate 	}
33137c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
33147c478bd9Sstevel@tonic-gate }
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate /*
33177c478bd9Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
33187c478bd9Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
33197c478bd9Sstevel@tonic-gate  */
33207c478bd9Sstevel@tonic-gate void *
33217c478bd9Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
33227c478bd9Sstevel@tonic-gate {
33237c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
33247c478bd9Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
33257c478bd9Sstevel@tonic-gate 	size_t start;
33267c478bd9Sstevel@tonic-gate 	size_t end;
33277c478bd9Sstevel@tonic-gate 
33287c478bd9Sstevel@tonic-gate 	/*
33297c478bd9Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
33307c478bd9Sstevel@tonic-gate 	 */
33317c478bd9Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
33327c478bd9Sstevel@tonic-gate 		return (NULL);
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	/*
33357c478bd9Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
33367c478bd9Sstevel@tonic-gate 	 */
3337ae115bc7Smrj 	if (v >= valloc_base || eaddr < kernelbase)
33387c478bd9Sstevel@tonic-gate 		return (NULL);
33397c478bd9Sstevel@tonic-gate 
33407c478bd9Sstevel@tonic-gate 	/*
33417c478bd9Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
33427c478bd9Sstevel@tonic-gate 	 */
33437c478bd9Sstevel@tonic-gate 	if (v < kernelbase)
33447c478bd9Sstevel@tonic-gate 		v = kernelbase;
33457c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
33467c478bd9Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
33477c478bd9Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
33487c478bd9Sstevel@tonic-gate 		end = toxic_bit_map_len;
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
33517c478bd9Sstevel@tonic-gate 		return (NULL);
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 	v = kernelbase + ptob(start);
33547c478bd9Sstevel@tonic-gate 	if (len != NULL)
33557c478bd9Sstevel@tonic-gate 		*len = ptob(end - start);
33567c478bd9Sstevel@tonic-gate 	return ((void *)v);
33577c478bd9Sstevel@tonic-gate }
33587c478bd9Sstevel@tonic-gate 
3359ae115bc7Smrj #endif	/* __i386 */
3360