xref: /titanic_51/usr/src/uts/i86pc/os/startup.c (revision cb15d5d96b3b2730714c28bfe06cfe7421758b8c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5100b72f4Sandrei  * Common Development and Distribution License (the "License").
6100b72f4Sandrei  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220db3240dSStephen Hanson  * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
24a3114836SGerry Liu /*
25a3114836SGerry Liu  * Copyright (c) 2010, Intel Corporation.
26a3114836SGerry Liu  * All rights reserved.
27a3114836SGerry Liu  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/signal.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/user.h>
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate #include <sys/vm.h>
387c478bd9Sstevel@tonic-gate #include <sys/conf.h>
397c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
407c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
417c478bd9Sstevel@tonic-gate #include <sys/disp.h>
427c478bd9Sstevel@tonic-gate #include <sys/class.h>
437c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <sys/proc.h>
487c478bd9Sstevel@tonic-gate #include <sys/buf.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
50ae115bc7Smrj #include <sys/mem.h>
517c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <sys/cred.h>
567c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
577c478bd9Sstevel@tonic-gate #include <sys/file.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
627c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
637c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
647c478bd9Sstevel@tonic-gate #include <sys/debug.h>
657c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
687c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
69a3114836SGerry Liu #include <sys/memlist_plat.h>
707c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
717c478bd9Sstevel@tonic-gate #include <sys/promif.h>
72843e1988Sjohnlev #include <sys/modctl.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
757c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
767c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
777c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
787c478bd9Sstevel@tonic-gate #include <sys/psw.h>
797c478bd9Sstevel@tonic-gate #include <sys/regset.h>
807c478bd9Sstevel@tonic-gate #include <sys/clock.h>
817c478bd9Sstevel@tonic-gate #include <sys/pte.h>
827c478bd9Sstevel@tonic-gate #include <sys/tss.h>
837c478bd9Sstevel@tonic-gate #include <sys/stack.h>
847c478bd9Sstevel@tonic-gate #include <sys/trap.h>
857c478bd9Sstevel@tonic-gate #include <sys/fp.h>
861d03c31eSjohnlev #include <vm/kboot_mmu.h>
877c478bd9Sstevel@tonic-gate #include <vm/anon.h>
887c478bd9Sstevel@tonic-gate #include <vm/as.h>
897c478bd9Sstevel@tonic-gate #include <vm/page.h>
907c478bd9Sstevel@tonic-gate #include <vm/seg.h>
917c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
927c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
937c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
947c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
957c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
967c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
977c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
987c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
997c478bd9Sstevel@tonic-gate #include <sys/thread.h>
1007c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
1017c478bd9Sstevel@tonic-gate #include <sys/vm_machparam.h>
1027c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
1037c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
1047c478bd9Sstevel@tonic-gate #include <vm/hat.h>
1057c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
1067c478bd9Sstevel@tonic-gate #include <sys/pmem.h>
1077c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1087c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
109a563a037Sbholler #include <sys/cpuvar.h>
1107c478bd9Sstevel@tonic-gate #include <sys/segments.h>
1117c478bd9Sstevel@tonic-gate #include <sys/clconf.h>
1127c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
1137c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
1147c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
1157aec1d6eScindi #include <sys/cpu_module.h>
11684ab085aSmws #include <sys/smbios.h>
117ae115bc7Smrj #include <sys/debug_info.h>
1181d03c31eSjohnlev #include <sys/bootinfo.h>
119dd4eeefdSeota #include <sys/ddi_timer.h>
1205679c89fSjv227347 #include <sys/systeminfo.h>
12119397407SSherry Moore #include <sys/multiboot.h>
122ae115bc7Smrj 
123843e1988Sjohnlev #ifdef	__xpv
1241d03c31eSjohnlev 
125843e1988Sjohnlev #include <sys/hypervisor.h>
126843e1988Sjohnlev #include <sys/xen_mmu.h>
127843e1988Sjohnlev #include <sys/evtchn_impl.h>
128843e1988Sjohnlev #include <sys/gnttab.h>
129843e1988Sjohnlev #include <sys/xpv_panic.h>
130843e1988Sjohnlev #include <xen/sys/xenbus_comms.h>
131843e1988Sjohnlev #include <xen/public/physdev.h>
132ae115bc7Smrj 
1331d03c31eSjohnlev extern void xen_late_startup(void);
1341d03c31eSjohnlev 
1351d03c31eSjohnlev struct xen_evt_data cpu0_evt_data;
1361d03c31eSjohnlev 
137a3114836SGerry Liu #else	/* __xpv */
138a3114836SGerry Liu #include <sys/memlist_impl.h>
139a3114836SGerry Liu 
140a3114836SGerry Liu extern void mem_config_init(void);
1411d03c31eSjohnlev #endif /* __xpv */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate extern void progressbar_init(void);
1449acbbeafSnn35248 extern void brand_init(void);
14506fb6a36Sdv142724 extern void pcf_init(void);
1460e751525SEric Saxe extern void pg_init(void);
1477c478bd9Sstevel@tonic-gate 
148d7d93655Sblakej extern int size_pse_array(pgcnt_t, int);
149d7d93655Sblakej 
150ed5289f9SKen Erickson #if defined(_SOFT_HOSTID)
151ed5289f9SKen Erickson 
152ed5289f9SKen Erickson #include <sys/rtc.h>
153ed5289f9SKen Erickson 
154ed5289f9SKen Erickson static int32_t set_soft_hostid(void);
155ed5289f9SKen Erickson static char hostid_file[] = "/etc/hostid";
156ed5289f9SKen Erickson 
157ed5289f9SKen Erickson #endif
158ed5289f9SKen Erickson 
15994f1124eSVikram Hegde void *gfx_devinfo_list;
160ed5289f9SKen Erickson 
1613a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
1623a634bfcSVikram Hegde extern void immu_startup(void);
1633a634bfcSVikram Hegde #endif
1643a634bfcSVikram Hegde 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1677c478bd9Sstevel@tonic-gate  * interface.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * segkp
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate extern int segkp_fromheap;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static void kvm_init(void);
1777c478bd9Sstevel@tonic-gate static void startup_init(void);
1787c478bd9Sstevel@tonic-gate static void startup_memlist(void);
179ae115bc7Smrj static void startup_kmem(void);
1807c478bd9Sstevel@tonic-gate static void startup_modules(void);
1817c478bd9Sstevel@tonic-gate static void startup_vm(void);
1827c478bd9Sstevel@tonic-gate static void startup_end(void);
18335b1ab99Sjosephb static void layout_kernel_va(void);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1877c478bd9Sstevel@tonic-gate  */
188f53ad214Skchow #ifdef __i386
18935b1ab99Sjosephb 
190f53ad214Skchow /*
191f53ad214Skchow  * Due to virtual address space limitations running in 32 bit mode, restrict
19235b1ab99Sjosephb  * the amount of physical memory configured to a max of PHYSMEM pages (16g).
193f53ad214Skchow  *
194f53ad214Skchow  * If the physical max memory size of 64g were allowed to be configured, the
195f53ad214Skchow  * size of user virtual address space will be less than 1g. A limited user
196f53ad214Skchow  * address space greatly reduces the range of applications that can run.
197f53ad214Skchow  *
19835b1ab99Sjosephb  * If more physical memory than PHYSMEM is required, users should preferably
19935b1ab99Sjosephb  * run in 64 bit mode which has far looser virtual address space limitations.
200f53ad214Skchow  *
201f53ad214Skchow  * If 64 bit mode is not available (as in IA32) and/or more physical memory
20235b1ab99Sjosephb  * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
203f53ad214Skchow  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
204f53ad214Skchow  * should also be carefully tuned to balance out the need of the user
205f53ad214Skchow  * application while minimizing the risk of kernel heap exhaustion due to
206f53ad214Skchow  * kernelbase being set too high.
207f53ad214Skchow  */
20835b1ab99Sjosephb #define	PHYSMEM	0x400000
209f53ad214Skchow 
21035b1ab99Sjosephb #else /* __amd64 */
21135b1ab99Sjosephb 
21235b1ab99Sjosephb /*
21335b1ab99Sjosephb  * For now we can handle memory with physical addresses up to about
21435b1ab99Sjosephb  * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
21535b1ab99Sjosephb  * half the VA space for seg_kpm. When systems get bigger than 64TB this
21635b1ab99Sjosephb  * code will need revisiting. There is an implicit assumption that there
21735b1ab99Sjosephb  * are no *huge* holes in the physical address space too.
21835b1ab99Sjosephb  */
21935b1ab99Sjosephb #define	TERABYTE		(1ul << 40)
22035b1ab99Sjosephb #define	PHYSMEM_MAX64		mmu_btop(64 * TERABYTE)
22135b1ab99Sjosephb #define	PHYSMEM			PHYSMEM_MAX64
22235b1ab99Sjosephb #define	AMD64_VA_HOLE_END	0xFFFF800000000000ul
22335b1ab99Sjosephb 
22435b1ab99Sjosephb #endif /* __amd64 */
22535b1ab99Sjosephb 
22635b1ab99Sjosephb pgcnt_t physmem = PHYSMEM;
2277c478bd9Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate char *kobj_file_buf;
2307c478bd9Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
23319397407SSherry Moore caddr_t	rm_platter_va = 0;
2347c478bd9Sstevel@tonic-gate uint32_t rm_platter_pa;
2357c478bd9Sstevel@tonic-gate 
23683f9b804Skchow int	auto_lpg_disable = 1;
23783f9b804Skchow 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate uintptr_t hole_start, hole_end;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * kpm mapping window
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate caddr_t kpm_vbase;
2477c478bd9Sstevel@tonic-gate size_t  kpm_size;
24835b1ab99Sjosephb static int kpm_desired;
24935b1ab99Sjosephb #ifdef __amd64
25035b1ab99Sjosephb static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
25135b1ab99Sjosephb #endif
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * Configuration parameters set at boot time.
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
2607c478bd9Sstevel@tonic-gate struct bootops		**bootopsp;
2617c478bd9Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate char bootblock_fstype[16];
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
26619397407SSherry Moore char kern_bootfile[OBP_MAXPATHLEN];
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*
269ad23a2dbSjohansen  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
270ad23a2dbSjohansen  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
271ad23a2dbSjohansen  * zio buffers from their own segment, otherwise they are allocated from the
272ad23a2dbSjohansen  * heap.  The optimization of allocating zio buffers from their own segment is
273ad23a2dbSjohansen  * only valid on 64-bit kernels.
274ad23a2dbSjohansen  */
275ad23a2dbSjohansen #if defined(__amd64)
276ad23a2dbSjohansen int segzio_fromheap = 0;
277ad23a2dbSjohansen #else
278ad23a2dbSjohansen int segzio_fromheap = 1;
279ad23a2dbSjohansen #endif
280ad23a2dbSjohansen 
281ad23a2dbSjohansen /*
2827c478bd9Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
2837c478bd9Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2847c478bd9Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * VM data structures
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
292*cb15d5d9SPeter Rival unsigned int page_hashsz_shift;	/* log2(page_hashsz) */
2937c478bd9Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2947c478bd9Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
295d7d93655Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
296d7d93655Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
297d7d93655Sblakej int pse_shift;			/* log2(pse_table_size) */
2987c478bd9Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2997c478bd9Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
3007c478bd9Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
3017c478bd9Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
3027c478bd9Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
305ae115bc7Smrj static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
306ae115bc7Smrj 
3077c478bd9Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate #if defined(__amd64)
3107c478bd9Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
3117c478bd9Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
3127c478bd9Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
3137c478bd9Sstevel@tonic-gate #else
3147c478bd9Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
3157c478bd9Sstevel@tonic-gate #endif
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
318ad23a2dbSjohansen caddr_t segzio_base;		/* Base address of segzio */
3197c478bd9Sstevel@tonic-gate #if defined(__amd64)
3207c478bd9Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
3217c478bd9Sstevel@tonic-gate #else
3227c478bd9Sstevel@tonic-gate pgcnt_t segkpsize = 0;
3237c478bd9Sstevel@tonic-gate #endif
324ad23a2dbSjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
3257c478bd9Sstevel@tonic-gate 
326ae115bc7Smrj /*
327a3114836SGerry Liu  * A static DR page_t VA map is reserved that can map the page structures
328a3114836SGerry Liu  * for a domain's entire RA space. The pages that back this space are
329a3114836SGerry Liu  * dynamically allocated and need not be physically contiguous.  The DR
330a3114836SGerry Liu  * map size is derived from KPM size.
331a3114836SGerry Liu  * This mechanism isn't used by x86 yet, so just stubs here.
332a3114836SGerry Liu  */
333a3114836SGerry Liu int ppvm_enable = 0;		/* Static virtual map for page structs */
334a3114836SGerry Liu page_t *ppvm_base = NULL;	/* Base of page struct map */
335a3114836SGerry Liu pgcnt_t ppvm_size = 0;		/* Size of page struct map */
336a3114836SGerry Liu 
337a3114836SGerry Liu /*
338ae115bc7Smrj  * VA range available to the debugger
339ae115bc7Smrj  */
340ae115bc7Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
341ae115bc7Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
342ae115bc7Smrj 
3437c478bd9Sstevel@tonic-gate struct memseg *memseg_base;
3447c478bd9Sstevel@tonic-gate struct vnode unused_pages_vp;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate #define	FOURGB	0x100000000LL
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate struct memlist *memlist;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
3517c478bd9Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
3527c478bd9Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
3537c478bd9Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
3547c478bd9Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
3557c478bd9Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
3567c478bd9Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
3577c478bd9Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
3607c478bd9Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
3611de082f7SVikram Hegde struct memlist *bios_rsvd;	/* Bios reserved memory */
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
3657c478bd9Sstevel@tonic-gate  */
366ae115bc7Smrj static pgcnt_t kphysm_init(page_t *, pgcnt_t);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate  * a couple useful roundup macros
3727c478bd9Sstevel@tonic-gate  */
3737c478bd9Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
3747c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
3757c478bd9Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
3767c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
3777c478bd9Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
378ae115bc7Smrj 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
3797c478bd9Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
3807c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
3847c478bd9Sstevel@tonic-gate  *		+-----------------------+
385ae115bc7Smrj  *		|			|
3867c478bd9Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
3877c478bd9Sstevel@tonic-gate  *		|	debugger	|
3887c478bd9Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
3897c478bd9Sstevel@tonic-gate  *		|      Kernel Data	|
3907c478bd9Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
3917c478bd9Sstevel@tonic-gate  *              |      Kernel Text	|
392843e1988Sjohnlev  * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
393ae115bc7Smrj  *		|---       GDT       ---|- GDT page (GDT_VA)
394ae115bc7Smrj  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
395ae115bc7Smrj  *		|			|
396ae115bc7Smrj  * 		|   page_t structures	|
3977c478bd9Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
3987c478bd9Sstevel@tonic-gate  * 		|   page hash, etc.	|
399ae115bc7Smrj  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
400ae115bc7Smrj  *		|			|  (segkp is just an arena in the heap)
4017c478bd9Sstevel@tonic-gate  *		|			|
4027c478bd9Sstevel@tonic-gate  *		|	kvseg		|
4037c478bd9Sstevel@tonic-gate  *		|			|
4047c478bd9Sstevel@tonic-gate  *		|			|
4057c478bd9Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
4067c478bd9Sstevel@tonic-gate  * 		|        Segkmap	|
407ae115bc7Smrj  * 0xC3002000  -|-----------------------|- segmap_start (floating)
4087c478bd9Sstevel@tonic-gate  *		|	Red Zone	|
4097c478bd9Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
4107c478bd9Sstevel@tonic-gate  *		|			|			||
4117c478bd9Sstevel@tonic-gate  *		|     Shared objects	|			\/
4127c478bd9Sstevel@tonic-gate  *		|			|
4137c478bd9Sstevel@tonic-gate  *		:			:
4147c478bd9Sstevel@tonic-gate  *		|	user data	|
4157c478bd9Sstevel@tonic-gate  *		|-----------------------|
4167c478bd9Sstevel@tonic-gate  *		|	user text	|
4177c478bd9Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
4187c478bd9Sstevel@tonic-gate  *		|	user stack	|
4197c478bd9Sstevel@tonic-gate  *		:			:
4207c478bd9Sstevel@tonic-gate  *		|	invalid		|
4217c478bd9Sstevel@tonic-gate  * 0x00000000	+-----------------------+
4227c478bd9Sstevel@tonic-gate  *
4237c478bd9Sstevel@tonic-gate  *
4247c478bd9Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
4257c478bd9Sstevel@tonic-gate  *			+-----------------------+
426ae115bc7Smrj  *			|			|
4277c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
4287c478bd9Sstevel@tonic-gate  *			|	debugger (?)	|
4297c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
4307c478bd9Sstevel@tonic-gate  *			|      unused    	|
4317c478bd9Sstevel@tonic-gate  *			+-----------------------+
4327c478bd9Sstevel@tonic-gate  *			|      Kernel Data	|
4337c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
4347c478bd9Sstevel@tonic-gate  *			|      Kernel Text	|
4357c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
436ae115bc7Smrj  *			|---       GDT       ---|- GDT page (GDT_VA)
437ae115bc7Smrj  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
438ae115bc7Smrj  *			|			|
4397c478bd9Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
4407c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
4417c478bd9Sstevel@tonic-gate  *			|	 Kernel		|
4427c478bd9Sstevel@tonic-gate  *			|	  heap		|
4437c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
444ae115bc7Smrj  *			|	 segmap		|
445ae115bc7Smrj  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
4467c478bd9Sstevel@tonic-gate  *			|    device mappings	|
4477c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
448ad23a2dbSjohansen  *			|	  segzio	|
449ad23a2dbSjohansen  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
4507c478bd9Sstevel@tonic-gate  *			|	  segkp		|
451ae115bc7Smrj  * ---                  |-----------------------|- segkp_base (floating)
452ae115bc7Smrj  * 			|   page_t structures	|  valloc_base + valloc_sz
453ae115bc7Smrj  * 			|   memsegs, memlists, 	|
454ae115bc7Smrj  * 			|   page hash, etc.	|
45535b1ab99Sjosephb  * 0xFFFFFF00.00000000  |-----------------------|- valloc_base (lower if > 1TB)
4567c478bd9Sstevel@tonic-gate  *			|	 segkpm		|
4577c478bd9Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
4587c478bd9Sstevel@tonic-gate  *			|	Red Zone	|
45935b1ab99Sjosephb  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE (lower if > 1TB)
4607c478bd9Sstevel@tonic-gate  *			|     User stack	|- User space memory
4617c478bd9Sstevel@tonic-gate  * 			|			|
4627c478bd9Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
4637c478bd9Sstevel@tonic-gate  *			:			:
4647c478bd9Sstevel@tonic-gate  * 			|			|
4657c478bd9Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
4667c478bd9Sstevel@tonic-gate  * 			|			|
4677c478bd9Sstevel@tonic-gate  * 			| VA Hole / unused	|
4687c478bd9Sstevel@tonic-gate  * 			|			|
4697c478bd9Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
4707c478bd9Sstevel@tonic-gate  *			|			|
4717c478bd9Sstevel@tonic-gate  *			|			|
4727c478bd9Sstevel@tonic-gate  *			:			:
4737c478bd9Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
4747c478bd9Sstevel@tonic-gate  *			|			|
4757c478bd9Sstevel@tonic-gate  *			|	user data	|
4767c478bd9Sstevel@tonic-gate  *			|-----------------------|
4777c478bd9Sstevel@tonic-gate  *			|	user text	|
4787c478bd9Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
4797c478bd9Sstevel@tonic-gate  *			|	invalid		|
4807c478bd9Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
4817c478bd9Sstevel@tonic-gate  *
4827c478bd9Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
4837c478bd9Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
4847c478bd9Sstevel@tonic-gate  *
4857c478bd9Sstevel@tonic-gate  * Floating values:
4867c478bd9Sstevel@tonic-gate  *
4877c478bd9Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
488ae115bc7Smrj  * structures.  This region contains page_t structures for
489ae115bc7Smrj  * physical memory, memsegs, memlists, and the page hash.
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
4927c478bd9Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
4937c478bd9Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
4947c478bd9Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
4957c478bd9Sstevel@tonic-gate  * accessed using rip-relative addressing
4967c478bd9Sstevel@tonic-gate  *
4977c478bd9Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
4987c478bd9Sstevel@tonic-gate  *
4997c478bd9Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
5007c478bd9Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
5017c478bd9Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
5027c478bd9Sstevel@tonic-gate  *
503ae115bc7Smrj  * segmap_start: start of segmap. The length of segmap can be modified
50452eef812SAmrita Sadhukhan  * through eeprom. The default length is 16MB on 32-bit systems and 64MB
50552eef812SAmrita Sadhukhan  * on 64-bit systems.
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
5087c478bd9Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
5097c478bd9Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
5107c478bd9Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
5117c478bd9Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
5127c478bd9Sstevel@tonic-gate  * segkp sizes).
5137c478bd9Sstevel@tonic-gate  *
5147c478bd9Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
5157c478bd9Sstevel@tonic-gate  *	size	page_t's			size
5167c478bd9Sstevel@tonic-gate  *	----	---------	----------	-----------
5177c478bd9Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
5187c478bd9Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
5197c478bd9Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
5207c478bd9Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
5217c478bd9Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
5227c478bd9Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
5237c478bd9Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
5247c478bd9Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
5257c478bd9Sstevel@tonic-gate  *
5267c478bd9Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
5277c478bd9Sstevel@tonic-gate  * configurations above 8gb.
5287c478bd9Sstevel@tonic-gate  *
5297c478bd9Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
5307c478bd9Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
5317c478bd9Sstevel@tonic-gate  */
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate /* real-time-clock initialization parameters */
534ae115bc7Smrj extern time_t process_rtc_config_file(void);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate uintptr_t	kernelbase;
537ae115bc7Smrj uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
5387c478bd9Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
5397c478bd9Sstevel@tonic-gate size_t		segmapsize;
540ae115bc7Smrj uintptr_t	segmap_start;
5417c478bd9Sstevel@tonic-gate int		segmapfreelists;
5427c478bd9Sstevel@tonic-gate pgcnt_t		npages;
543ae115bc7Smrj pgcnt_t		orig_npages;
5447c478bd9Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
5457c478bd9Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate /*
5487c478bd9Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
5497c478bd9Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
5507c478bd9Sstevel@tonic-gate  * the bootstrap.
5517c478bd9Sstevel@tonic-gate  */
552ae115bc7Smrj static page_t *bootpages;
553ae115bc7Smrj 
554ae115bc7Smrj /*
555ae115bc7Smrj  * boot time pages that have a vnode from the ramdisk will keep that forever.
556ae115bc7Smrj  */
557ae115bc7Smrj static page_t *rd_pages;
5587c478bd9Sstevel@tonic-gate 
55919397407SSherry Moore /*
56019397407SSherry Moore  * Lower 64K
56119397407SSherry Moore  */
56219397407SSherry Moore static page_t *lower_pages = NULL;
56319397407SSherry Moore static int lower_pages_count = 0;
56419397407SSherry Moore 
5657c478bd9Sstevel@tonic-gate struct system_hardware system_hardware;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate static void
571ae115bc7Smrj print_memlist(char *title, struct memlist *mp)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
5747c478bd9Sstevel@tonic-gate 	while (mp != NULL)  {
5757c478bd9Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
57656f33205SJonathan Adams 		    mp->ml_address, mp->ml_size);
57756f33205SJonathan Adams 		mp = mp->ml_next;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /*
5827c478bd9Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
5837c478bd9Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
5847c478bd9Sstevel@tonic-gate  */
5857c478bd9Sstevel@tonic-gate int	l2cache_sz = 0x80000;
5867c478bd9Sstevel@tonic-gate int	l2cache_linesz = 0x40;
5877c478bd9Sstevel@tonic-gate int	l2cache_assoc = 1;
5887c478bd9Sstevel@tonic-gate 
5892cb27123Saguzovsk static size_t	textrepl_min_gb = 10;
5902cb27123Saguzovsk 
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
5937c478bd9Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate #ifdef __amd64
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate vmem_t		*device_arena;
5987c478bd9Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
599ae115bc7Smrj size_t		toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate #else	/* __i386 */
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
6047c478bd9Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate #endif	/* __i386 */
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate  * Simple boot time debug facilities
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate static char *prm_dbg_str[] = {
6127c478bd9Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%x\n",
6137c478bd9Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%llx\n"
6147c478bd9Sstevel@tonic-gate };
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate int prom_debug;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate #define	PRM_DEBUG(q)	if (prom_debug) 	\
6197c478bd9Sstevel@tonic-gate 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
6207c478bd9Sstevel@tonic-gate #define	PRM_POINT(q)	if (prom_debug) 	\
6217c478bd9Sstevel@tonic-gate 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
6257c478bd9Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
6267c478bd9Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
6277c478bd9Sstevel@tonic-gate  */
6281de082f7SVikram Hegde #define	NUM_ALLOCATIONS 8
6297c478bd9Sstevel@tonic-gate int num_allocations = 0;
6307c478bd9Sstevel@tonic-gate struct {
6317c478bd9Sstevel@tonic-gate 	void **al_ptr;
6327c478bd9Sstevel@tonic-gate 	size_t al_size;
6337c478bd9Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
6347c478bd9Sstevel@tonic-gate size_t valloc_sz = 0;
6357c478bd9Sstevel@tonic-gate uintptr_t valloc_base;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
6387c478bd9Sstevel@tonic-gate 		size = ROUND_UP_PAGE(size);		 		\
6397c478bd9Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
6407c478bd9Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
6417c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
6427c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
6437c478bd9Sstevel@tonic-gate 		valloc_sz += size;					\
6447c478bd9Sstevel@tonic-gate 		++num_allocations;				 	\
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
647ae115bc7Smrj /*
648ae115bc7Smrj  * Allocate all the initial memory needed by the page allocator.
649ae115bc7Smrj  */
6507c478bd9Sstevel@tonic-gate static void
6517c478bd9Sstevel@tonic-gate perform_allocations(void)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	caddr_t mem;
6547c478bd9Sstevel@tonic-gate 	int i;
655ae115bc7Smrj 	int valloc_align;
6567c478bd9Sstevel@tonic-gate 
657ae115bc7Smrj 	PRM_DEBUG(valloc_base);
658ae115bc7Smrj 	PRM_DEBUG(valloc_sz);
659ae115bc7Smrj 	valloc_align = mmu.level_size[mmu.max_page_level > 0];
660ae115bc7Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
6617c478bd9Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
6627c478bd9Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
6637c478bd9Sstevel@tonic-gate 	bzero(mem, valloc_sz);
6647c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
6657c478bd9Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
6667c478bd9Sstevel@tonic-gate 		mem += allocations[i].al_size;
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate /*
6717c478bd9Sstevel@tonic-gate  * Our world looks like this at startup time.
6727c478bd9Sstevel@tonic-gate  *
6737c478bd9Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
6747c478bd9Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
6757c478bd9Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
6767c478bd9Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
6777c478bd9Sstevel@tonic-gate  *
6787c478bd9Sstevel@tonic-gate  * On the text page:
6797c478bd9Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
6807c478bd9Sstevel@tonic-gate  *
6817c478bd9Sstevel@tonic-gate  * On the data page:
682ae115bc7Smrj  * unix/genunix/krtld/module data loads.
683ae115bc7Smrj  *
6847c478bd9Sstevel@tonic-gate  * Machine-dependent startup code
6857c478bd9Sstevel@tonic-gate  */
6867c478bd9Sstevel@tonic-gate void
6877c478bd9Sstevel@tonic-gate startup(void)
6887c478bd9Sstevel@tonic-gate {
689843e1988Sjohnlev #if !defined(__xpv)
69075bcd456Sjg 	extern void startup_pci_bios(void);
691843e1988Sjohnlev #endif
692a563a037Sbholler 	extern cpuset_t cpu_ready_set;
693a563a037Sbholler 
694843e1988Sjohnlev 	/*
695843e1988Sjohnlev 	 * Make sure that nobody tries to use sekpm until we have
696843e1988Sjohnlev 	 * initialized it properly.
697843e1988Sjohnlev 	 */
6987c478bd9Sstevel@tonic-gate #if defined(__amd64)
69935b1ab99Sjosephb 	kpm_desired = 1;
7007c478bd9Sstevel@tonic-gate #endif
7017c478bd9Sstevel@tonic-gate 	kpm_enable = 0;
702a563a037Sbholler 	CPUSET_ONLY(cpu_ready_set, 0);	/* cpu 0 is boot cpu */
7037c478bd9Sstevel@tonic-gate 
704843e1988Sjohnlev #if defined(__xpv)	/* XXPV fix me! */
705843e1988Sjohnlev 	{
706843e1988Sjohnlev 		extern int segvn_use_regions;
707843e1988Sjohnlev 		segvn_use_regions = 0;
708843e1988Sjohnlev 	}
709843e1988Sjohnlev #endif
7107c478bd9Sstevel@tonic-gate 	progressbar_init();
7117c478bd9Sstevel@tonic-gate 	startup_init();
712ab4a9bebSjohnlev #if defined(__xpv)
713ab4a9bebSjohnlev 	startup_xen_version();
714ab4a9bebSjohnlev #endif
7157c478bd9Sstevel@tonic-gate 	startup_memlist();
716ae115bc7Smrj 	startup_kmem();
71735b1ab99Sjosephb 	startup_vm();
718843e1988Sjohnlev #if !defined(__xpv)
7190db3240dSStephen Hanson 	/*
7200db3240dSStephen Hanson 	 * Note we need to do this even on fast reboot in order to access
7210db3240dSStephen Hanson 	 * the irq routing table (used for pci labels).
7220db3240dSStephen Hanson 	 */
72375bcd456Sjg 	startup_pci_bios();
724843e1988Sjohnlev #endif
725e4b86885SCheng Sean Ye #if defined(__xpv)
726e4b86885SCheng Sean Ye 	startup_xen_mca();
727e4b86885SCheng Sean Ye #endif
7287c478bd9Sstevel@tonic-gate 	startup_modules();
7298770118eSlipeng sang - Sun Microsystems - Beijing China 
7307c478bd9Sstevel@tonic-gate 	startup_end();
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate static void
7347c478bd9Sstevel@tonic-gate startup_init()
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	/*
7397c478bd9Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
7407c478bd9Sstevel@tonic-gate 	 */
7417c478bd9Sstevel@tonic-gate 	cpuid_pass2(CPU);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	/*
7467c478bd9Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
7477c478bd9Sstevel@tonic-gate 	 */
7487c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
7497c478bd9Sstevel@tonic-gate 		++prom_debug;
7507c478bd9Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	/*
7547c478bd9Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
7557c478bd9Sstevel@tonic-gate 	 */
7567c478bd9Sstevel@tonic-gate 	get_system_configuration();
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	/*
7597c478bd9Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
7607c478bd9Sstevel@tonic-gate 	 */
7617c478bd9Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
7627c478bd9Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
7637c478bd9Sstevel@tonic-gate 		    CPU->cpu_brandstr);
7647c478bd9Sstevel@tonic-gate 		halt("This processor is not supported by this release "
7657c478bd9Sstevel@tonic-gate 		    "of Solaris.");
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
7737c478bd9Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
7747c478bd9Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
7757c478bd9Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
7767c478bd9Sstevel@tonic-gate  */
7777c478bd9Sstevel@tonic-gate static void
7787c478bd9Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	uintptr_t va;
7817c478bd9Sstevel@tonic-gate 	uintptr_t next_va;
7827c478bd9Sstevel@tonic-gate 	pfn_t pfn;
7837c478bd9Sstevel@tonic-gate 	uint64_t pfn_addr;
7847c478bd9Sstevel@tonic-gate 	uint64_t pfn_eaddr;
7857c478bd9Sstevel@tonic-gate 	uint_t prot;
7867c478bd9Sstevel@tonic-gate 	size_t len;
7877c478bd9Sstevel@tonic-gate 	uint_t change;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (prom_debug)
7907c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
7917c478bd9Sstevel@tonic-gate 		    *addr, *size);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	/*
7947c478bd9Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
7957c478bd9Sstevel@tonic-gate 	 */
7967c478bd9Sstevel@tonic-gate 	if (*addr == 0) {
7977c478bd9Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
7987c478bd9Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	/*
802ae115bc7Smrj 	 * First we trim from the front of the range. Since kbm_probe()
8037c478bd9Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
8047c478bd9Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
8057c478bd9Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
8067c478bd9Sstevel@tonic-gate 	 * but w < v.
8077c478bd9Sstevel@tonic-gate 	 */
8087c478bd9Sstevel@tonic-gate 	do {
8097c478bd9Sstevel@tonic-gate 		change = 0;
8107c478bd9Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
811ae115bc7Smrj 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
8127c478bd9Sstevel@tonic-gate 		    va = next_va) {
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 			next_va = va + len;
815ae115bc7Smrj 			pfn_addr = pfn_to_pa(pfn);
8167c478bd9Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
8197c478bd9Sstevel@tonic-gate 				change = 1;
8207c478bd9Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
8217c478bd9Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
8227c478bd9Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
8237c478bd9Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
8247c478bd9Sstevel@tonic-gate 				}
8257c478bd9Sstevel@tonic-gate 			}
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 		if (change && prom_debug)
8287c478bd9Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
8297c478bd9Sstevel@tonic-gate 			    *addr, *size);
8307c478bd9Sstevel@tonic-gate 	} while (change);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/*
8337c478bd9Sstevel@tonic-gate 	 * Trim pages from the end of the range.
8347c478bd9Sstevel@tonic-gate 	 */
8357c478bd9Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
836ae115bc7Smrj 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
8377c478bd9Sstevel@tonic-gate 	    va = next_va) {
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		next_va = va + len;
840ae115bc7Smrj 		pfn_addr = pfn_to_pa(pfn);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
8437c478bd9Sstevel@tonic-gate 			*size = pfn_addr - *addr;
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	if (prom_debug)
8477c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
8487c478bd9Sstevel@tonic-gate 		    *addr, *size);
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate static void
8527c478bd9Sstevel@tonic-gate kpm_init()
8537c478bd9Sstevel@tonic-gate {
8547c478bd9Sstevel@tonic-gate 	struct segkpm_crargs b;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	/*
8577c478bd9Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
8587c478bd9Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
8597c478bd9Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
8607c478bd9Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
8617c478bd9Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
8627c478bd9Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
8637c478bd9Sstevel@tonic-gate 	 * pagesize is used.
8647c478bd9Sstevel@tonic-gate 	 */
8657c478bd9Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
8667c478bd9Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
8677c478bd9Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
8687c478bd9Sstevel@tonic-gate 	kpmp2pshft = 0;
8697c478bd9Sstevel@tonic-gate 	kpmpnpgs = 1;
8707c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
8737c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
8767c478bd9Sstevel@tonic-gate 		panic("cannot attach segkpm");
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
8797c478bd9Sstevel@tonic-gate 	b.nvcolors = 1;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
8827c478bd9Sstevel@tonic-gate 		panic("segkpm_create segkpm");
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
885ae115bc7Smrj }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate /*
888ae115bc7Smrj  * The debug info page provides enough information to allow external
889ae115bc7Smrj  * inspectors (e.g. when running under a hypervisor) to bootstrap
890ae115bc7Smrj  * themselves into allowing full-blown kernel debugging.
8917c478bd9Sstevel@tonic-gate  */
892ae115bc7Smrj static void
893ae115bc7Smrj init_debug_info(void)
894ae115bc7Smrj {
895ae115bc7Smrj 	caddr_t mem;
896ae115bc7Smrj 	debug_info_t *di;
897ae115bc7Smrj 
898ae115bc7Smrj #ifndef __lint
899ae115bc7Smrj 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
900ae115bc7Smrj #endif
901ae115bc7Smrj 
902ae115bc7Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
903ae115bc7Smrj 	    MMU_PAGESIZE);
904ae115bc7Smrj 
905ae115bc7Smrj 	if (mem != (caddr_t)DEBUG_INFO_VA)
906ae115bc7Smrj 		panic("BOP_ALLOC() failed");
907ae115bc7Smrj 	bzero(mem, MMU_PAGESIZE);
908ae115bc7Smrj 
909ae115bc7Smrj 	di = (debug_info_t *)mem;
910ae115bc7Smrj 
911ae115bc7Smrj 	di->di_magic = DEBUG_INFO_MAGIC;
912ae115bc7Smrj 	di->di_version = DEBUG_INFO_VERSION;
913843e1988Sjohnlev 	di->di_modules = (uintptr_t)&modules;
914843e1988Sjohnlev 	di->di_s_text = (uintptr_t)s_text;
915843e1988Sjohnlev 	di->di_e_text = (uintptr_t)e_text;
916843e1988Sjohnlev 	di->di_s_data = (uintptr_t)s_data;
917843e1988Sjohnlev 	di->di_e_data = (uintptr_t)e_data;
918843e1988Sjohnlev 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
919843e1988Sjohnlev 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate /*
923ae115bc7Smrj  * Build the memlists and other kernel essential memory system data structures.
924ae115bc7Smrj  * This is everything at valloc_base.
9257c478bd9Sstevel@tonic-gate  */
9267c478bd9Sstevel@tonic-gate static void
9277c478bd9Sstevel@tonic-gate startup_memlist(void)
9287c478bd9Sstevel@tonic-gate {
9297c478bd9Sstevel@tonic-gate 	size_t memlist_sz;
9307c478bd9Sstevel@tonic-gate 	size_t memseg_sz;
9317c478bd9Sstevel@tonic-gate 	size_t pagehash_sz;
9327c478bd9Sstevel@tonic-gate 	size_t pp_sz;
9337c478bd9Sstevel@tonic-gate 	uintptr_t va;
9347c478bd9Sstevel@tonic-gate 	size_t len;
9357c478bd9Sstevel@tonic-gate 	uint_t prot;
9367c478bd9Sstevel@tonic-gate 	pfn_t pfn;
9377c478bd9Sstevel@tonic-gate 	int memblocks;
9381de082f7SVikram Hegde 	pfn_t rsvd_high_pfn;
9391de082f7SVikram Hegde 	pgcnt_t rsvd_pgcnt;
9401de082f7SVikram Hegde 	size_t rsvdmemlist_sz;
9411de082f7SVikram Hegde 	int rsvdmemblocks;
9427c478bd9Sstevel@tonic-gate 	caddr_t pagecolor_mem;
9437c478bd9Sstevel@tonic-gate 	size_t pagecolor_memsz;
9447c478bd9Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
9457c478bd9Sstevel@tonic-gate 	size_t page_ctrs_size;
946d7d93655Sblakej 	size_t pse_table_alloc_size;
9477c478bd9Sstevel@tonic-gate 	struct memlist *current;
9487c478bd9Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
9517c478bd9Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
9527c478bd9Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	/*
9577c478bd9Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
9587c478bd9Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
9597c478bd9Sstevel@tonic-gate 	 */
960ae115bc7Smrj 	len = kbm_nucleus_size;
961ae115bc7Smrj 	ASSERT(len > MMU_PAGESIZE);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
964ae115bc7Smrj 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
9657c478bd9Sstevel@tonic-gate 	if (e_moddata - moddata > MODDATA)
9667c478bd9Sstevel@tonic-gate 		e_moddata = moddata + MODDATA;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
969ae115bc7Smrj 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
9707c478bd9Sstevel@tonic-gate 	if (e_modtext - modtext > MODTEXT)
9717c478bd9Sstevel@tonic-gate 		e_modtext = modtext + MODTEXT;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	econtig = e_moddata;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9767c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
9777c478bd9Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9787c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
9797c478bd9Sstevel@tonic-gate 	PRM_DEBUG(econtig);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	/*
982ae115bc7Smrj 	 * Examine the boot loader physical memory map to find out:
9837c478bd9Sstevel@tonic-gate 	 * - total memory in system - physinstalled
9847c478bd9Sstevel@tonic-gate 	 * - the max physical address - physmax
985ae115bc7Smrj 	 * - the number of discontiguous segments of memory.
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 	if (prom_debug)
988ae115bc7Smrj 		print_memlist("boot physinstalled",
9897c478bd9Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
990a3114836SGerry Liu 	installed_top_size_ex(bootops->boot_mem->physinstalled, &physmax,
9917c478bd9Sstevel@tonic-gate 	    &physinstalled, &memblocks);
9927c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9937c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9947c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	/*
997a3114836SGerry Liu 	 * Compute maximum physical address for memory DR operations.
998a3114836SGerry Liu 	 * Memory DR operations are unsupported on xpv or 32bit OSes.
999a3114836SGerry Liu 	 */
1000a3114836SGerry Liu #ifdef	__amd64
1001a3114836SGerry Liu 	if (plat_dr_support_memory()) {
1002a3114836SGerry Liu 		if (plat_dr_physmax == 0) {
1003a3114836SGerry Liu 			uint_t pabits = UINT_MAX;
1004a3114836SGerry Liu 
1005a3114836SGerry Liu 			cpuid_get_addrsize(CPU, &pabits, NULL);
1006a3114836SGerry Liu 			plat_dr_physmax = btop(1ULL << pabits);
1007a3114836SGerry Liu 		}
1008a3114836SGerry Liu 		if (plat_dr_physmax > PHYSMEM_MAX64)
1009a3114836SGerry Liu 			plat_dr_physmax = PHYSMEM_MAX64;
1010a3114836SGerry Liu 	} else
1011a3114836SGerry Liu #endif
1012a3114836SGerry Liu 		plat_dr_physmax = 0;
1013a3114836SGerry Liu 
1014a3114836SGerry Liu 	/*
10151de082f7SVikram Hegde 	 * Examine the bios reserved memory to find out:
10161de082f7SVikram Hegde 	 * - the number of discontiguous segments of memory.
10171de082f7SVikram Hegde 	 */
10181de082f7SVikram Hegde 	if (prom_debug)
10191de082f7SVikram Hegde 		print_memlist("boot reserved mem",
10201de082f7SVikram Hegde 		    bootops->boot_mem->rsvdmem);
1021a3114836SGerry Liu 	installed_top_size_ex(bootops->boot_mem->rsvdmem, &rsvd_high_pfn,
10221de082f7SVikram Hegde 	    &rsvd_pgcnt, &rsvdmemblocks);
10231de082f7SVikram Hegde 	PRM_DEBUG(rsvd_high_pfn);
10241de082f7SVikram Hegde 	PRM_DEBUG(rsvd_pgcnt);
10251de082f7SVikram Hegde 	PRM_DEBUG(rsvdmemblocks);
10261de082f7SVikram Hegde 
10271de082f7SVikram Hegde 	/*
10287c478bd9Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
10297c478bd9Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
10307c478bd9Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
10317c478bd9Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
10327c478bd9Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
10337c478bd9Sstevel@tonic-gate 	 */
10347c478bd9Sstevel@tonic-gate 	mmu_init();
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate #ifdef	__i386
10377c478bd9Sstevel@tonic-gate 	/*
10387c478bd9Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
10397c478bd9Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
10427c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
10437c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
10447c478bd9Sstevel@tonic-gate 			physmax = PFN_64G - 1;
10457c478bd9Sstevel@tonic-gate 		}
10467c478bd9Sstevel@tonic-gate 	} else {
10477c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
10487c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
10497c478bd9Sstevel@tonic-gate 			physmax = PFN_4G - 1;
10507c478bd9Sstevel@tonic-gate 		}
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate #endif
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
10577c478bd9Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
10587c478bd9Sstevel@tonic-gate 		char value[8];
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 		if (len < 8)
10617c478bd9Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
10627c478bd9Sstevel@tonic-gate 		else
10637c478bd9Sstevel@tonic-gate 			(void) strcpy(value, "");
10647c478bd9Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
10657c478bd9Sstevel@tonic-gate 			mmu.pt_nx = 0;
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	/*
10707c478bd9Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
10717c478bd9Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
10727c478bd9Sstevel@tonic-gate 	 *
10737c478bd9Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
10747c478bd9Sstevel@tonic-gate 	 */
10757c478bd9Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
10767c478bd9Sstevel@tonic-gate 	obp_pages = 0;
10777c478bd9Sstevel@tonic-gate 	va = KERNEL_TEXT;
1078ae115bc7Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
10797c478bd9Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
10807c478bd9Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
10817c478bd9Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
10827c478bd9Sstevel@tonic-gate 		va += len;
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
10857c478bd9Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	/*
108835b1ab99Sjosephb 	 * If physmem is patched to be non-zero, use it instead of the computed
108935b1ab99Sjosephb 	 * value unless it is larger than the actual amount of memory on hand.
10907c478bd9Sstevel@tonic-gate 	 */
10914944b02eSkchow 	if (physmem == 0 || physmem > npages) {
10927c478bd9Sstevel@tonic-gate 		physmem = npages;
10934944b02eSkchow 	} else if (physmem < npages) {
1094f53ad214Skchow 		orig_npages = npages;
10957c478bd9Sstevel@tonic-gate 		npages = physmem;
10964944b02eSkchow 	}
10977c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmem);
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	/*
11007c478bd9Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
11017c478bd9Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
11027c478bd9Sstevel@tonic-gate 	 * include:
11037c478bd9Sstevel@tonic-gate 	 *	memsegs
11047c478bd9Sstevel@tonic-gate 	 *	memlists
11057c478bd9Sstevel@tonic-gate 	 *	page hash table
11067c478bd9Sstevel@tonic-gate 	 *	page_t's
11077c478bd9Sstevel@tonic-gate 	 *	page coloring data structs
11087c478bd9Sstevel@tonic-gate 	 */
11097c478bd9Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
11107c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
11117c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/*
1114ae115bc7Smrj 	 * Reserve space for memlists. There's no real good way to know exactly
1115ae115bc7Smrj 	 * how much room we'll need, but this should be a good upper bound.
11167c478bd9Sstevel@tonic-gate 	 */
11177c478bd9Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
11187c478bd9Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
11197c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
11207c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/*
11231de082f7SVikram Hegde 	 * Reserve space for bios reserved memlists.
11241de082f7SVikram Hegde 	 */
11251de082f7SVikram Hegde 	rsvdmemlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
11261de082f7SVikram Hegde 	    (rsvdmemblocks + POSS_NEW_FRAGMENTS));
11271de082f7SVikram Hegde 	ADD_TO_ALLOCATIONS(bios_rsvd, rsvdmemlist_sz);
11281de082f7SVikram Hegde 	PRM_DEBUG(rsvdmemlist_sz);
11291de082f7SVikram Hegde 
1130*cb15d5d9SPeter Rival 	/* LINTED */
1131*cb15d5d9SPeter Rival 	ASSERT(P2SAMEHIGHBIT((1 << PP_SHIFT), sizeof (struct page)));
11321de082f7SVikram Hegde 	/*
11337c478bd9Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
11347c478bd9Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
11357c478bd9Sstevel@tonic-gate 	 */
11367c478bd9Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
1137*cb15d5d9SPeter Rival 	page_hashsz_shift = highbit(page_hashsz);
1138*cb15d5d9SPeter Rival 	page_hashsz = 1 << page_hashsz_shift;
11397c478bd9Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
11407c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
11417c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/*
1144ae115bc7Smrj 	 * Set aside room for the page structures themselves.
11457c478bd9Sstevel@tonic-gate 	 */
1146ae115bc7Smrj 	PRM_DEBUG(npages);
1147ae115bc7Smrj 	pp_sz = sizeof (struct page) * npages;
11487c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
11497c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	/*
11527c478bd9Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
11537c478bd9Sstevel@tonic-gate 	 */
11547c478bd9Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
11557c478bd9Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
11567c478bd9Sstevel@tonic-gate 	pagecolor_memsz =
11577c478bd9Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
11587c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
11597c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
11627c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
11637c478bd9Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
11647c478bd9Sstevel@tonic-gate 
1165d7d93655Sblakej 	/*
1166d7d93655Sblakej 	 * Allocate the array that protects pp->p_selock.
1167d7d93655Sblakej 	 */
1168d7d93655Sblakej 	pse_shift = size_pse_array(physmem, max_ncpus);
1169d7d93655Sblakej 	pse_table_size = 1 << pse_shift;
1170d7d93655Sblakej 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
1171d7d93655Sblakej 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
1172d7d93655Sblakej 
1173ae115bc7Smrj #if defined(__amd64)
11747c478bd9Sstevel@tonic-gate 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
1175ae115bc7Smrj 	valloc_base = VALLOC_BASE;
117635b1ab99Sjosephb 
117735b1ab99Sjosephb 	/*
117835b1ab99Sjosephb 	 * The default values of VALLOC_BASE and SEGKPM_BASE should work
117935b1ab99Sjosephb 	 * for values of physmax up to 1 Terabyte. They need adjusting when
11806c8c89eaSKit Chow 	 * memory is at addresses above 1 TB. When adjusted, segkpm_base must
11816c8c89eaSKit Chow 	 * be aligned on KERNEL_REDZONE_SIZE boundary (span of top level pte).
118235b1ab99Sjosephb 	 */
1183a3114836SGerry Liu 	if (physmax + 1 > mmu_btop(TERABYTE) ||
1184a3114836SGerry Liu 	    plat_dr_physmax > mmu_btop(TERABYTE)) {
118535b1ab99Sjosephb 		uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
118635b1ab99Sjosephb 
1187a3114836SGerry Liu 		if (kpm_resv_amount < mmu_ptob(plat_dr_physmax)) {
1188a3114836SGerry Liu 			kpm_resv_amount = mmu_ptob(plat_dr_physmax);
1189a3114836SGerry Liu 		}
1190a3114836SGerry Liu 
11916c8c89eaSKit Chow 		segkpm_base = -(P2ROUNDUP((2 * kpm_resv_amount),
11926c8c89eaSKit Chow 		    KERNEL_REDZONE_SIZE));	/* down from top VA */
119335b1ab99Sjosephb 
119435b1ab99Sjosephb 		/* make sure we leave some space for user apps above hole */
119535b1ab99Sjosephb 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
119635b1ab99Sjosephb 		if (segkpm_base > SEGKPM_BASE)
119735b1ab99Sjosephb 			segkpm_base = SEGKPM_BASE;
119835b1ab99Sjosephb 		PRM_DEBUG(segkpm_base);
119935b1ab99Sjosephb 
12006c8c89eaSKit Chow 		valloc_base = segkpm_base + P2ROUNDUP(kpm_resv_amount, ONE_GIG);
12016c8c89eaSKit Chow 		if (valloc_base < segkpm_base)
12026c8c89eaSKit Chow 			panic("not enough kernel VA to support memory size");
120335b1ab99Sjosephb 		PRM_DEBUG(valloc_base);
120435b1ab99Sjosephb 	}
1205ae115bc7Smrj #else	/* __i386 */
1206ae115bc7Smrj 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
1207ae115bc7Smrj 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
12087c478bd9Sstevel@tonic-gate 	PRM_DEBUG(valloc_base);
120935b1ab99Sjosephb #endif	/* __i386 */
1210ae115bc7Smrj 
1211ae115bc7Smrj 	/*
1212ae115bc7Smrj 	 * do all the initial allocations
1213ae115bc7Smrj 	 */
1214ae115bc7Smrj 	perform_allocations();
1215ae115bc7Smrj 
1216ae115bc7Smrj 	/*
1217ae115bc7Smrj 	 * Build phys_install and phys_avail in kernel memspace.
1218ae115bc7Smrj 	 * - phys_install should be all memory in the system.
1219ae115bc7Smrj 	 * - phys_avail is phys_install minus any memory mapped before this
1220ae115bc7Smrj 	 *    point above KERNEL_TEXT.
1221ae115bc7Smrj 	 */
1222ae115bc7Smrj 	current = phys_install = memlist;
1223ae115bc7Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
1224ae115bc7Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1225ae115bc7Smrj 		panic("physinstalled was too big!");
1226ae115bc7Smrj 	if (prom_debug)
1227ae115bc7Smrj 		print_memlist("phys_install", phys_install);
1228ae115bc7Smrj 
1229ae115bc7Smrj 	phys_avail = current;
1230ae115bc7Smrj 	PRM_POINT("Building phys_avail:\n");
1231ae115bc7Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
1232ae115bc7Smrj 	    avail_filter);
1233ae115bc7Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1234ae115bc7Smrj 		panic("physavail was too big!");
1235ae115bc7Smrj 	if (prom_debug)
1236ae115bc7Smrj 		print_memlist("phys_avail", phys_avail);
1237a3114836SGerry Liu #ifndef	__xpv
1238a3114836SGerry Liu 	/*
1239a3114836SGerry Liu 	 * Free unused memlist items, which may be used by memory DR driver
1240a3114836SGerry Liu 	 * at runtime.
1241a3114836SGerry Liu 	 */
1242a3114836SGerry Liu 	if ((caddr_t)current < (caddr_t)memlist + memlist_sz) {
1243a3114836SGerry Liu 		memlist_free_block((caddr_t)current,
1244a3114836SGerry Liu 		    (caddr_t)memlist + memlist_sz - (caddr_t)current);
1245a3114836SGerry Liu 	}
1246a3114836SGerry Liu #endif
1247ae115bc7Smrj 
1248ae115bc7Smrj 	/*
12491de082f7SVikram Hegde 	 * Build bios reserved memspace
12501de082f7SVikram Hegde 	 */
12511de082f7SVikram Hegde 	current = bios_rsvd;
12521de082f7SVikram Hegde 	copy_memlist_filter(bootops->boot_mem->rsvdmem, &current, NULL);
12531de082f7SVikram Hegde 	if ((caddr_t)current > (caddr_t)bios_rsvd + rsvdmemlist_sz)
12541de082f7SVikram Hegde 		panic("bios_rsvd was too big!");
12551de082f7SVikram Hegde 	if (prom_debug)
12561de082f7SVikram Hegde 		print_memlist("bios_rsvd", bios_rsvd);
1257a3114836SGerry Liu #ifndef	__xpv
1258a3114836SGerry Liu 	/*
1259a3114836SGerry Liu 	 * Free unused memlist items, which may be used by memory DR driver
1260a3114836SGerry Liu 	 * at runtime.
1261a3114836SGerry Liu 	 */
1262a3114836SGerry Liu 	if ((caddr_t)current < (caddr_t)bios_rsvd + rsvdmemlist_sz) {
1263a3114836SGerry Liu 		memlist_free_block((caddr_t)current,
1264a3114836SGerry Liu 		    (caddr_t)bios_rsvd + rsvdmemlist_sz - (caddr_t)current);
1265a3114836SGerry Liu 	}
1266a3114836SGerry Liu #endif
12671de082f7SVikram Hegde 
12681de082f7SVikram Hegde 	/*
1269ae115bc7Smrj 	 * setup page coloring
1270ae115bc7Smrj 	 */
1271ae115bc7Smrj 	page_coloring_setup(pagecolor_mem);
1272ae115bc7Smrj 	page_lock_init();	/* currently a no-op */
1273ae115bc7Smrj 
1274ae115bc7Smrj 	/*
1275ae115bc7Smrj 	 * free page list counters
1276ae115bc7Smrj 	 */
1277ae115bc7Smrj 	(void) page_ctrs_alloc(page_ctrs_mem);
1278ae115bc7Smrj 
1279ae115bc7Smrj 	/*
128006fb6a36Sdv142724 	 * Size the pcf array based on the number of cpus in the box at
128106fb6a36Sdv142724 	 * boot time.
128206fb6a36Sdv142724 	 */
128306fb6a36Sdv142724 
128406fb6a36Sdv142724 	pcf_init();
128506fb6a36Sdv142724 
128606fb6a36Sdv142724 	/*
1287ae115bc7Smrj 	 * Initialize the page structures from the memory lists.
1288ae115bc7Smrj 	 */
1289ae115bc7Smrj 	availrmem_initial = availrmem = freemem = 0;
1290ae115bc7Smrj 	PRM_POINT("Calling kphysm_init()...");
1291ae115bc7Smrj 	npages = kphysm_init(pp_base, npages);
1292ae115bc7Smrj 	PRM_POINT("kphysm_init() done");
1293ae115bc7Smrj 	PRM_DEBUG(npages);
1294ae115bc7Smrj 
1295ae115bc7Smrj 	init_debug_info();
1296ae115bc7Smrj 
1297ae115bc7Smrj 	/*
1298ae115bc7Smrj 	 * Now that page_t's have been initialized, remove all the
1299ae115bc7Smrj 	 * initial allocation pages from the kernel free page lists.
1300ae115bc7Smrj 	 */
1301ae115bc7Smrj 	boot_mapin((caddr_t)valloc_base, valloc_sz);
13020cfdb603Sjosephb 	boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
1303ae115bc7Smrj 	PRM_POINT("startup_memlist() done");
1304ae115bc7Smrj 
1305ae115bc7Smrj 	PRM_DEBUG(valloc_sz);
13062cb27123Saguzovsk 
1307567d55e1Saguzovsk #if defined(__amd64)
1308567d55e1Saguzovsk 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
1309567d55e1Saguzovsk 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
1310567d55e1Saguzovsk 		extern size_t textrepl_size_thresh;
13112cb27123Saguzovsk 		textrepl_size_thresh = (16 << 20) - 1;
13122cb27123Saguzovsk 	}
1313567d55e1Saguzovsk #endif
1314ae115bc7Smrj }
1315ae115bc7Smrj 
1316ae115bc7Smrj /*
1317ae115bc7Smrj  * Layout the kernel's part of address space and initialize kmem allocator.
1318ae115bc7Smrj  */
1319ae115bc7Smrj static void
1320ae115bc7Smrj startup_kmem(void)
1321ae115bc7Smrj {
1322932dc8e5Sdp78419 	extern void page_set_colorequiv_arr(void);
1323a50a8b93SKuriakose Kuruvilla 	const char *fmt = "?features: %b\n";
1324932dc8e5Sdp78419 
1325ae115bc7Smrj 	PRM_POINT("startup_kmem() starting...");
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate #if defined(__amd64)
13287c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
13297c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
13307c478bd9Sstevel@tonic-gate 		    "systems.");
133135b1ab99Sjosephb 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
13327c478bd9Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
1333ae115bc7Smrj 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
13347c478bd9Sstevel@tonic-gate #else	/* __i386 */
13357c478bd9Sstevel@tonic-gate 	/*
13367c478bd9Sstevel@tonic-gate 	 * We configure kernelbase based on:
13377c478bd9Sstevel@tonic-gate 	 *
13387c478bd9Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
13397c478bd9Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
13407c478bd9Sstevel@tonic-gate 	 *
13417c478bd9Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
13427c478bd9Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
13437c478bd9Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
13447c478bd9Sstevel@tonic-gate 	 *
13457c478bd9Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
13467c478bd9Sstevel@tonic-gate 	 */
13477c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase) {
13487c478bd9Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
13497c478bd9Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
13507c478bd9Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
13517c478bd9Sstevel@tonic-gate 	} else {
13527c478bd9Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
13537c478bd9Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
1356ae115bc7Smrj 	core_base = valloc_base;
13577c478bd9Sstevel@tonic-gate 	core_size = 0;
1358ae115bc7Smrj #endif	/* __i386 */
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_base);
13617c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_size);
1362ae115bc7Smrj 	PRM_DEBUG(kernelbase);
13637c478bd9Sstevel@tonic-gate 
136435b1ab99Sjosephb #if defined(__i386)
13657c478bd9Sstevel@tonic-gate 	segkp_fromheap = 1;
1366ae115bc7Smrj #endif	/* __i386 */
136735b1ab99Sjosephb 
13687c478bd9Sstevel@tonic-gate 	ekernelheap = (char *)core_base;
1369ae115bc7Smrj 	PRM_DEBUG(ekernelheap);
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	/*
13727c478bd9Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
13737c478bd9Sstevel@tonic-gate 	 * update variables that were initialized with a value of
13747c478bd9Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
13757c478bd9Sstevel@tonic-gate 	 *
13767c478bd9Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
13777c478bd9Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
13787c478bd9Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
13797c478bd9Sstevel@tonic-gate 	 *	just be declared as variables there?
13807c478bd9Sstevel@tonic-gate 	 */
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
13837c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
1384ccbaea4fSjosephb #if defined(__amd64)
1385ccbaea4fSjosephb 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
1386ccbaea4fSjosephb #else
13877c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
13887c478bd9Sstevel@tonic-gate #endif
13897c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
13907c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
13917c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
13927c478bd9Sstevel@tonic-gate 
139335b1ab99Sjosephb 	layout_kernel_va();
139435b1ab99Sjosephb 
139535b1ab99Sjosephb #if defined(__i386)
139635b1ab99Sjosephb 	/*
139735b1ab99Sjosephb 	 * If segmap is too large we can push the bottom of the kernel heap
139835b1ab99Sjosephb 	 * higher than the base.  Or worse, it could exceed the top of the
139935b1ab99Sjosephb 	 * VA space entirely, causing it to wrap around.
140035b1ab99Sjosephb 	 */
140135b1ab99Sjosephb 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
140235b1ab99Sjosephb 		panic("too little address space available for kernelheap,"
140335b1ab99Sjosephb 		    " use eeprom for lower kernelbase or smaller segmapsize");
140435b1ab99Sjosephb #endif	/* __i386 */
140535b1ab99Sjosephb 
14067c478bd9Sstevel@tonic-gate 	/*
14077c478bd9Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
14087c478bd9Sstevel@tonic-gate 	 */
140935b1ab99Sjosephb 	kernelheap_init(kernelheap, ekernelheap,
141035b1ab99Sjosephb 	    kernelheap + MMU_PAGESIZE,
1411ae115bc7Smrj 	    (void *)core_base, (void *)(core_base + core_size));
14127c478bd9Sstevel@tonic-gate 
1413843e1988Sjohnlev #if defined(__xpv)
1414843e1988Sjohnlev 	/*
1415843e1988Sjohnlev 	 * Link pending events struct into cpu struct
1416843e1988Sjohnlev 	 */
1417843e1988Sjohnlev 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
1418843e1988Sjohnlev #endif
14197c478bd9Sstevel@tonic-gate 	/*
14207c478bd9Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
14217c478bd9Sstevel@tonic-gate 	 */
14227c478bd9Sstevel@tonic-gate 	kmem_init();
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	/*
1425932dc8e5Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
1426932dc8e5Sdp78419 	 */
1427932dc8e5Sdp78419 	page_set_colorequiv_arr();
1428932dc8e5Sdp78419 
1429932dc8e5Sdp78419 	/*
14307c478bd9Sstevel@tonic-gate 	 * print this out early so that we know what's going on
14317c478bd9Sstevel@tonic-gate 	 */
1432a50a8b93SKuriakose Kuruvilla 	cmn_err(CE_CONT, fmt, x86_feature, FMT_X86_FEATURE);
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	/*
14357c478bd9Sstevel@tonic-gate 	 * Initialize bp_mapin().
14367c478bd9Sstevel@tonic-gate 	 */
14377c478bd9Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
14387c478bd9Sstevel@tonic-gate 
1439f53ad214Skchow 	/*
1440f53ad214Skchow 	 * orig_npages is non-zero if physmem has been configured for less
1441f53ad214Skchow 	 * than the available memory.
1442f53ad214Skchow 	 */
1443f53ad214Skchow 	if (orig_npages) {
144435b1ab99Sjosephb 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
144535b1ab99Sjosephb 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
1446f53ad214Skchow 		    npages, orig_npages);
1447f53ad214Skchow 	}
14487c478bd9Sstevel@tonic-gate #if defined(__i386)
14497c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
14507c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
14517c478bd9Sstevel@tonic-gate 		    "System using 0x%lx",
14527c478bd9Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
14537c478bd9Sstevel@tonic-gate #endif
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
14567c478bd9Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
14577c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
14587c478bd9Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate #endif
14617c478bd9Sstevel@tonic-gate 
1462a3114836SGerry Liu #ifndef __xpv
1463a3114836SGerry Liu 	if (plat_dr_support_memory()) {
1464a3114836SGerry Liu 		mem_config_init();
1465a3114836SGerry Liu 	}
1466a3114836SGerry Liu #else	/* __xpv */
1467843e1988Sjohnlev 	/*
1468843e1988Sjohnlev 	 * Some of the xen start information has to be relocated up
1469843e1988Sjohnlev 	 * into the kernel's permanent address space.
1470843e1988Sjohnlev 	 */
1471843e1988Sjohnlev 	PRM_POINT("calling xen_relocate_start_info()");
1472843e1988Sjohnlev 	xen_relocate_start_info();
1473843e1988Sjohnlev 	PRM_POINT("xen_relocate_start_info() done");
1474843e1988Sjohnlev 
1475843e1988Sjohnlev 	/*
1476843e1988Sjohnlev 	 * (Update the vcpu pointer in our cpu structure to point into
1477843e1988Sjohnlev 	 * the relocated shared info.)
1478843e1988Sjohnlev 	 */
1479843e1988Sjohnlev 	CPU->cpu_m.mcpu_vcpu_info =
1480843e1988Sjohnlev 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
1481a3114836SGerry Liu #endif	/* __xpv */
1482843e1988Sjohnlev 
1483ae115bc7Smrj 	PRM_POINT("startup_kmem() done");
14847c478bd9Sstevel@tonic-gate }
14857c478bd9Sstevel@tonic-gate 
1486551bc2a6Smrj #ifndef __xpv
1487551bc2a6Smrj /*
1488551bc2a6Smrj  * If we have detected that we are running in an HVM environment, we need
1489551bc2a6Smrj  * to prepend the PV driver directory to the module search path.
1490551bc2a6Smrj  */
1491551bc2a6Smrj #define	HVM_MOD_DIR "/platform/i86hvm/kernel"
1492551bc2a6Smrj static void
1493551bc2a6Smrj update_default_path()
1494551bc2a6Smrj {
1495551bc2a6Smrj 	char *current, *newpath;
1496551bc2a6Smrj 	int newlen;
1497551bc2a6Smrj 
1498551bc2a6Smrj 	/*
1499551bc2a6Smrj 	 * We are about to resync with krtld.  krtld will reset its
1500551bc2a6Smrj 	 * internal module search path iff Solaris has set default_path.
1501551bc2a6Smrj 	 * We want to be sure we're prepending this new directory to the
1502551bc2a6Smrj 	 * right search path.
1503551bc2a6Smrj 	 */
1504551bc2a6Smrj 	current = (default_path == NULL) ? kobj_module_path : default_path;
1505551bc2a6Smrj 
1506b6611e3bSStuart Maybee 	newlen = strlen(HVM_MOD_DIR) + strlen(current) + 2;
1507551bc2a6Smrj 	newpath = kmem_alloc(newlen, KM_SLEEP);
1508551bc2a6Smrj 	(void) strcpy(newpath, HVM_MOD_DIR);
1509551bc2a6Smrj 	(void) strcat(newpath, " ");
1510551bc2a6Smrj 	(void) strcat(newpath, current);
1511551bc2a6Smrj 
1512551bc2a6Smrj 	default_path = newpath;
1513551bc2a6Smrj }
1514551bc2a6Smrj #endif
1515551bc2a6Smrj 
15167c478bd9Sstevel@tonic-gate static void
15177c478bd9Sstevel@tonic-gate startup_modules(void)
15187c478bd9Sstevel@tonic-gate {
1519ed5289f9SKen Erickson 	int cnt;
15207c478bd9Sstevel@tonic-gate 	extern void prom_setup(void);
1521ed5289f9SKen Erickson 	int32_t v, h;
1522ed5289f9SKen Erickson 	char d[11];
1523ed5289f9SKen Erickson 	char *cp;
1524e4b86885SCheng Sean Ye 	cmi_hdl_t hdl;
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
1527843e1988Sjohnlev 
1528843e1988Sjohnlev #ifndef __xpv
15297c478bd9Sstevel@tonic-gate 	/*
15307c478bd9Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
15317c478bd9Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
15327c478bd9Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
15337c478bd9Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
15347c478bd9Sstevel@tonic-gate 	 */
15357c478bd9Sstevel@tonic-gate 	microfind();
1536551bc2a6Smrj 
1537b9bfdccdSStuart Maybee 	if (get_hwenv() == HW_XEN_HVM)
1538551bc2a6Smrj 		update_default_path();
1539843e1988Sjohnlev #endif
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	/*
15427c478bd9Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
15437c478bd9Sstevel@tonic-gate 	 */
1544ae115bc7Smrj 	sgmtl(process_rtc_config_file());
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	/*
15477c478bd9Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
15487c478bd9Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
15497c478bd9Sstevel@tonic-gate 	 */
15507c478bd9Sstevel@tonic-gate 	param_calc(0);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	mod_setup();
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	/*
15557c478bd9Sstevel@tonic-gate 	 * Initialize system parameters.
15567c478bd9Sstevel@tonic-gate 	 */
15577c478bd9Sstevel@tonic-gate 	param_init();
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	/*
15609acbbeafSnn35248 	 * Initialize the default brands
15619acbbeafSnn35248 	 */
15629acbbeafSnn35248 	brand_init();
15639acbbeafSnn35248 
15649acbbeafSnn35248 	/*
15657c478bd9Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
15667c478bd9Sstevel@tonic-gate 	 */
15677c478bd9Sstevel@tonic-gate 	maxmem = physmem;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	/*
15707c478bd9Sstevel@tonic-gate 	 * Initialize segment management stuff.
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	seg_init();
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
15757c478bd9Sstevel@tonic-gate 		halt("Can't load specfs");
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
15787c478bd9Sstevel@tonic-gate 		halt("Can't load devfs");
15797c478bd9Sstevel@tonic-gate 
1580facf4a8dSllai1 	if (modload("fs", "dev") == -1)
1581facf4a8dSllai1 		halt("Can't load dev");
1582facf4a8dSllai1 
158335a5a358SJonathan Adams 	if (modload("fs", "procfs") == -1)
158435a5a358SJonathan Adams 		halt("Can't load procfs");
158535a5a358SJonathan Adams 
158645916cd2Sjpk 	(void) modloadonly("sys", "lbl_edition");
158745916cd2Sjpk 
15887c478bd9Sstevel@tonic-gate 	dispinit();
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	/*
15917c478bd9Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
15927c478bd9Sstevel@tonic-gate 	 */
15935679c89fSjv227347 	if ((h = set_soft_hostid()) == HW_INVALID_HOSTID) {
1594ed5289f9SKen Erickson 		cmn_err(CE_WARN, "Unable to set hostid");
15955679c89fSjv227347 	} else {
1596ed5289f9SKen Erickson 		for (v = h, cnt = 0; cnt < 10; cnt++) {
15975679c89fSjv227347 			d[cnt] = (char)(v % 10);
1598ed5289f9SKen Erickson 			v /= 10;
1599ed5289f9SKen Erickson 			if (v == 0)
1600ed5289f9SKen Erickson 				break;
1601ed5289f9SKen Erickson 		}
1602ed5289f9SKen Erickson 		for (cp = hw_serial; cnt >= 0; cnt--)
1603ed5289f9SKen Erickson 			*cp++ = d[cnt] + '0';
1604ed5289f9SKen Erickson 		*cp = 0;
1605ed5289f9SKen Erickson 	}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	/* Read cluster configuration data. */
16087c478bd9Sstevel@tonic-gate 	clconf_init();
16097c478bd9Sstevel@tonic-gate 
1610843e1988Sjohnlev #if defined(__xpv)
1611349b53ddSStuart Maybee 	(void) ec_init();
1612843e1988Sjohnlev 	gnttab_init();
1613843e1988Sjohnlev 	(void) xs_early_init();
1614843e1988Sjohnlev #endif /* __xpv */
1615843e1988Sjohnlev 
16167c478bd9Sstevel@tonic-gate 	/*
16177c478bd9Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
16187c478bd9Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
16197c478bd9Sstevel@tonic-gate 	 */
16207c478bd9Sstevel@tonic-gate 	setup_ddi();
16217aec1d6eScindi 
1622074bb90dSTom Pothier #ifdef __xpv
1623074bb90dSTom Pothier 	if (DOMAIN_IS_INITDOMAIN(xen_info))
1624074bb90dSTom Pothier #endif
1625074bb90dSTom Pothier 	{
1626074bb90dSTom Pothier 		/*
1627074bb90dSTom Pothier 		 * Load the System Management BIOS into the global ksmbios
1628074bb90dSTom Pothier 		 * handle, if an SMBIOS is present on this system.
1629074bb90dSTom Pothier 		 */
1630074bb90dSTom Pothier 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
1631074bb90dSTom Pothier 	}
1632074bb90dSTom Pothier 
1633074bb90dSTom Pothier 
16347aec1d6eScindi 	/*
1635e4b86885SCheng Sean Ye 	 * Set up the CPU module subsystem for the boot cpu in the native
1636e4b86885SCheng Sean Ye 	 * case, and all physical cpu resource in the xpv dom0 case.
1637e4b86885SCheng Sean Ye 	 * Modifies the device tree, so this must be done after
1638e4b86885SCheng Sean Ye 	 * setup_ddi().
16397aec1d6eScindi 	 */
1640e4b86885SCheng Sean Ye #ifdef __xpv
1641e4b86885SCheng Sean Ye 	/*
1642e4b86885SCheng Sean Ye 	 * If paravirtualized and on dom0 then we initialize all physical
1643e4b86885SCheng Sean Ye 	 * cpu handles now;  if paravirtualized on a domU then do not
1644e4b86885SCheng Sean Ye 	 * initialize.
1645e4b86885SCheng Sean Ye 	 */
1646e4b86885SCheng Sean Ye 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1647e4b86885SCheng Sean Ye 		xen_mc_lcpu_cookie_t cpi;
16487aec1d6eScindi 
1649e4b86885SCheng Sean Ye 		for (cpi = xen_physcpu_next(NULL); cpi != NULL;
1650e4b86885SCheng Sean Ye 		    cpi = xen_physcpu_next(cpi)) {
1651e4b86885SCheng Sean Ye 			if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
1652e4b86885SCheng Sean Ye 			    xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
1653e4b86885SCheng Sean Ye 			    xen_physcpu_strandid(cpi))) != NULL &&
1654e4b86885SCheng Sean Ye 			    (x86_feature & X86_MCA))
165520c794b3Sgavinm 				cmi_mca_init(hdl);
165620c794b3Sgavinm 		}
165720c794b3Sgavinm 	}
1658e4b86885SCheng Sean Ye #else
1659e4b86885SCheng Sean Ye 	/*
1660e4b86885SCheng Sean Ye 	 * Initialize a handle for the boot cpu - others will initialize
1661e4b86885SCheng Sean Ye 	 * as they startup.  Do not do this if we know we are in an HVM domU.
1662e4b86885SCheng Sean Ye 	 */
1663b9bfdccdSStuart Maybee 	if ((get_hwenv() != HW_XEN_HVM) &&
1664e4b86885SCheng Sean Ye 	    (hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
1665e4b86885SCheng Sean Ye 	    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL &&
1666a3114836SGerry Liu 	    (x86_feature & X86_MCA)) {
1667e4b86885SCheng Sean Ye 			cmi_mca_init(hdl);
1668a3114836SGerry Liu 			CPU->cpu_m.mcpu_cmi_hdl = hdl;
1669a3114836SGerry Liu 	}
167020c794b3Sgavinm #endif	/* __xpv */
16717aec1d6eScindi 
16727c478bd9Sstevel@tonic-gate 	/*
16737c478bd9Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
16747c478bd9Sstevel@tonic-gate 	 */
1675ae115bc7Smrj 	PRM_POINT("startup_modules: calling prom_setup...");
16767c478bd9Sstevel@tonic-gate 	prom_setup();
1677ae115bc7Smrj 	PRM_POINT("startup_modules: done");
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	/*
16807c478bd9Sstevel@tonic-gate 	 * Load all platform specific modules
16817c478bd9Sstevel@tonic-gate 	 */
1682ae115bc7Smrj 	PRM_POINT("startup_modules: calling psm_modload...");
16837c478bd9Sstevel@tonic-gate 	psm_modload();
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
16867c478bd9Sstevel@tonic-gate }
16877c478bd9Sstevel@tonic-gate 
1688ae115bc7Smrj /*
1689ae115bc7Smrj  * claim a "setaside" boot page for use in the kernel
1690ae115bc7Smrj  */
1691ae115bc7Smrj page_t *
1692ae115bc7Smrj boot_claim_page(pfn_t pfn)
16937c478bd9Sstevel@tonic-gate {
1694ae115bc7Smrj 	page_t *pp;
16957c478bd9Sstevel@tonic-gate 
1696ae115bc7Smrj 	pp = page_numtopp_nolock(pfn);
1697ae115bc7Smrj 	ASSERT(pp != NULL);
16987c478bd9Sstevel@tonic-gate 
1699ae115bc7Smrj 	if (PP_ISBOOTPAGES(pp)) {
1700ae115bc7Smrj 		if (pp->p_next != NULL)
1701ae115bc7Smrj 			pp->p_next->p_prev = pp->p_prev;
1702ae115bc7Smrj 		if (pp->p_prev == NULL)
1703ae115bc7Smrj 			bootpages = pp->p_next;
1704ae115bc7Smrj 		else
1705ae115bc7Smrj 			pp->p_prev->p_next = pp->p_next;
1706ae115bc7Smrj 	} else {
17077c478bd9Sstevel@tonic-gate 		/*
1708ae115bc7Smrj 		 * htable_attach() expects a base pagesize page
17097c478bd9Sstevel@tonic-gate 		 */
1710ae115bc7Smrj 		if (pp->p_szc != 0)
1711ae115bc7Smrj 			page_boot_demote(pp);
1712ae115bc7Smrj 		pp = page_numtopp(pfn, SE_EXCL);
1713ae115bc7Smrj 	}
1714ae115bc7Smrj 	return (pp);
17157c478bd9Sstevel@tonic-gate }
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate /*
17187c478bd9Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
17197c478bd9Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
17207c478bd9Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
17217c478bd9Sstevel@tonic-gate  */
17227c478bd9Sstevel@tonic-gate static void
17237c478bd9Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
17247c478bd9Sstevel@tonic-gate {
17257c478bd9Sstevel@tonic-gate 	uintptr_t va = low;
17267c478bd9Sstevel@tonic-gate 	size_t len;
17277c478bd9Sstevel@tonic-gate 	uint_t prot;
17287c478bd9Sstevel@tonic-gate 	pfn_t pfn;
17297c478bd9Sstevel@tonic-gate 	page_t *pp;
17307c478bd9Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
17317c478bd9Sstevel@tonic-gate 
1732ae115bc7Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
17337c478bd9Sstevel@tonic-gate 		if (va + len >= high)
17347c478bd9Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
17357c478bd9Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 		while (len > 0) {
17387c478bd9Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
17397c478bd9Sstevel@tonic-gate 			if (pp != NULL) {
17407c478bd9Sstevel@tonic-gate 				if (setaside == 0)
17417c478bd9Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
17427c478bd9Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
17437c478bd9Sstevel@tonic-gate 					    (void *)va, pfn);
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 				pp->p_next = bootpages;
1746ae115bc7Smrj 				pp->p_prev = NULL;
1747ae115bc7Smrj 				PP_SETBOOTPAGES(pp);
1748ae115bc7Smrj 				if (bootpages != NULL) {
1749ae115bc7Smrj 					bootpages->p_prev = pp;
1750ae115bc7Smrj 				}
17517c478bd9Sstevel@tonic-gate 				bootpages = pp;
17527c478bd9Sstevel@tonic-gate 				++boot_protect_cnt;
17537c478bd9Sstevel@tonic-gate 			}
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 			++pfn;
17567c478bd9Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
17577c478bd9Sstevel@tonic-gate 			va += MMU_PAGESIZE;
17587c478bd9Sstevel@tonic-gate 		}
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
1763ae115bc7Smrj /*
176435b1ab99Sjosephb  *
1765ae115bc7Smrj  */
17667c478bd9Sstevel@tonic-gate static void
176735b1ab99Sjosephb layout_kernel_va(void)
17687c478bd9Sstevel@tonic-gate {
176935b1ab99Sjosephb 	PRM_POINT("layout_kernel_va() starting...");
17707c478bd9Sstevel@tonic-gate 	/*
1771ae115bc7Smrj 	 * Establish the final size of the kernel's heap, size of segmap,
1772ae115bc7Smrj 	 * segkp, etc.
17737c478bd9Sstevel@tonic-gate 	 */
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate #if defined(__amd64)
17767c478bd9Sstevel@tonic-gate 
177735b1ab99Sjosephb 	kpm_vbase = (caddr_t)segkpm_base;
1778a3114836SGerry Liu 	if (physmax + 1 < plat_dr_physmax) {
1779a3114836SGerry Liu 		kpm_size = ROUND_UP_LPAGE(mmu_ptob(plat_dr_physmax));
1780a3114836SGerry Liu 	} else {
1781ae115bc7Smrj 		kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
1782a3114836SGerry Liu 	}
178335b1ab99Sjosephb 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
178435b1ab99Sjosephb 		panic("not enough room for kpm!");
1785ae115bc7Smrj 	PRM_DEBUG(kpm_size);
1786ae115bc7Smrj 	PRM_DEBUG(kpm_vbase);
1787ae115bc7Smrj 
1788ae115bc7Smrj 	/*
1789ae115bc7Smrj 	 * By default we create a seg_kp in 64 bit kernels, it's a little
1790ae115bc7Smrj 	 * faster to access than embedding it in the heap.
1791ae115bc7Smrj 	 */
1792ae115bc7Smrj 	segkp_base = (caddr_t)valloc_base + valloc_sz;
17937c478bd9Sstevel@tonic-gate 	if (!segkp_fromheap) {
17947c478bd9Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 		/*
1797ae115bc7Smrj 		 * determine size of segkp
17987c478bd9Sstevel@tonic-gate 		 */
17997c478bd9Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
18007c478bd9Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
18017c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
18027c478bd9Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
18037c478bd9Sstevel@tonic-gate 			    mmu_btop(sz));
18047c478bd9Sstevel@tonic-gate 		}
18057c478bd9Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
18087c478bd9Sstevel@tonic-gate 	}
1809ae115bc7Smrj 	PRM_DEBUG(segkp_base);
1810ae115bc7Smrj 	PRM_DEBUG(segkpsize);
18117c478bd9Sstevel@tonic-gate 
181235b1ab99Sjosephb 	/*
181335b1ab99Sjosephb 	 * segzio is used for ZFS cached data. It uses a distinct VA
181435b1ab99Sjosephb 	 * segment (from kernel heap) so that we can easily tell not to
181535b1ab99Sjosephb 	 * include it in kernel crash dumps on 64 bit kernels. The trick is
181635b1ab99Sjosephb 	 * to give it lots of VA, but not constrain the kernel heap.
181735b1ab99Sjosephb 	 * We scale the size of segzio linearly with physmem up to
181835b1ab99Sjosephb 	 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem.
181935b1ab99Sjosephb 	 */
1820ae115bc7Smrj 	segzio_base = segkp_base + mmu_ptob(segkpsize);
1821ae115bc7Smrj 	if (segzio_fromheap) {
1822ae115bc7Smrj 		segziosize = 0;
1823ae115bc7Smrj 	} else {
182435b1ab99Sjosephb 		size_t physmem_size = mmu_ptob(physmem);
182535b1ab99Sjosephb 		size_t size = (segziosize == 0) ?
182635b1ab99Sjosephb 		    physmem_size : mmu_ptob(segziosize);
1827ad23a2dbSjohansen 
182835b1ab99Sjosephb 		if (size < SEGZIOMINSIZE)
1829ad23a2dbSjohansen 			size = SEGZIOMINSIZE;
183035b1ab99Sjosephb 		if (size > SEGZIOMAXSIZE) {
1831a778305aSjohansen 			size = SEGZIOMAXSIZE;
183235b1ab99Sjosephb 			if (physmem_size > size)
183335b1ab99Sjosephb 				size += (physmem_size - size) / 2;
1834ad23a2dbSjohansen 		}
1835ad23a2dbSjohansen 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
1836ae115bc7Smrj 	}
1837ad23a2dbSjohansen 	PRM_DEBUG(segziosize);
1838ad23a2dbSjohansen 	PRM_DEBUG(segzio_base);
1839ad23a2dbSjohansen 
18407c478bd9Sstevel@tonic-gate 	/*
1841ae115bc7Smrj 	 * Put the range of VA for device mappings next, kmdb knows to not
1842ae115bc7Smrj 	 * grep in this range of addresses.
18437c478bd9Sstevel@tonic-gate 	 */
1844ae115bc7Smrj 	toxic_addr =
1845ae115bc7Smrj 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
18467c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
1847ae115bc7Smrj 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
1848ae115bc7Smrj #else /* __i386 */
1849ae115bc7Smrj 	segmap_start = ROUND_UP_LPAGE(kernelbase);
1850ae115bc7Smrj #endif /* __i386 */
1851ae115bc7Smrj 	PRM_DEBUG(segmap_start);
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	/*
185452eef812SAmrita Sadhukhan 	 * Users can change segmapsize through eeprom. If the variable
185552eef812SAmrita Sadhukhan 	 * is tuned through eeprom, there is no upper bound on the
185652eef812SAmrita Sadhukhan 	 * size of segmap.
18577c478bd9Sstevel@tonic-gate 	 */
18587c478bd9Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate #if defined(__i386)
18617c478bd9Sstevel@tonic-gate 	/*
18627c478bd9Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
18637c478bd9Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
1864ae115bc7Smrj 	 * small red zone just below the start of segmap.
18657c478bd9Sstevel@tonic-gate 	 */
1866ae115bc7Smrj 	segmap_start += KERNEL_REDZONE_SIZE;
18677c478bd9Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
18687c478bd9Sstevel@tonic-gate #endif
18697c478bd9Sstevel@tonic-gate 
1870ae115bc7Smrj 	PRM_DEBUG(segmap_start);
18717c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segmapsize);
187235b1ab99Sjosephb 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
187335b1ab99Sjosephb 	PRM_DEBUG(kernelheap);
187435b1ab99Sjosephb 	PRM_POINT("layout_kernel_va() done...");
187535b1ab99Sjosephb }
187635b1ab99Sjosephb 
187735b1ab99Sjosephb /*
187835b1ab99Sjosephb  * Finish initializing the VM system, now that we are no longer
187935b1ab99Sjosephb  * relying on the boot time memory allocators.
188035b1ab99Sjosephb  */
188135b1ab99Sjosephb static void
188235b1ab99Sjosephb startup_vm(void)
188335b1ab99Sjosephb {
188435b1ab99Sjosephb 	struct segmap_crargs a;
188535b1ab99Sjosephb 
188635b1ab99Sjosephb 	extern int use_brk_lpg, use_stk_lpg;
188735b1ab99Sjosephb 
188835b1ab99Sjosephb 	PRM_POINT("startup_vm() starting...");
188935b1ab99Sjosephb 
189035b1ab99Sjosephb 	/*
189135b1ab99Sjosephb 	 * Initialize the hat layer.
189235b1ab99Sjosephb 	 */
189335b1ab99Sjosephb 	hat_init();
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	/*
1896ae115bc7Smrj 	 * Do final allocations of HAT data structures that need to
1897ae115bc7Smrj 	 * be allocated before quiescing the boot loader.
1898ae115bc7Smrj 	 */
1899ae115bc7Smrj 	PRM_POINT("Calling hat_kern_alloc()...");
1900ae115bc7Smrj 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
1901ae115bc7Smrj 	PRM_POINT("hat_kern_alloc() done");
1902ae115bc7Smrj 
1903843e1988Sjohnlev #ifndef __xpv
1904ae115bc7Smrj 	/*
19051d03c31eSjohnlev 	 * Setup Page Attribute Table
1906ae115bc7Smrj 	 */
19071d03c31eSjohnlev 	pat_sync();
1908843e1988Sjohnlev #endif
1909ae115bc7Smrj 
1910ae115bc7Smrj 	/*
1911ae115bc7Smrj 	 * The next two loops are done in distinct steps in order
1912ae115bc7Smrj 	 * to be sure that any page that is doubly mapped (both above
1913ae115bc7Smrj 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
1914ae115bc7Smrj 	 * Note this may never happen, but it might someday.
1915ae115bc7Smrj 	 */
1916ae115bc7Smrj 	bootpages = NULL;
1917ae115bc7Smrj 	PRM_POINT("Protecting boot pages");
1918ae115bc7Smrj 
1919ae115bc7Smrj 	/*
1920ae115bc7Smrj 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
1921ae115bc7Smrj 	 * page_t's. This can only happen if something weird allocated
1922ae115bc7Smrj 	 * in this range (like kadb/kmdb).
1923ae115bc7Smrj 	 */
1924ae115bc7Smrj 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
1925ae115bc7Smrj 
1926ae115bc7Smrj 	/*
1927ae115bc7Smrj 	 * Before we can take over memory allocation/mapping from the boot
1928ae115bc7Smrj 	 * loader we must remove from our free page lists any boot allocated
1929ae115bc7Smrj 	 * pages that stay mapped until release_bootstrap().
1930ae115bc7Smrj 	 */
1931ae115bc7Smrj 	protect_boot_range(0, kernelbase, 1);
1932ae115bc7Smrj 
1933843e1988Sjohnlev 
1934ae115bc7Smrj 	/*
1935ae115bc7Smrj 	 * Switch to running on regular HAT (not boot_mmu)
1936ae115bc7Smrj 	 */
1937ae115bc7Smrj 	PRM_POINT("Calling hat_kern_setup()...");
1938ae115bc7Smrj 	hat_kern_setup();
1939ae115bc7Smrj 
1940ae115bc7Smrj 	/*
1941ae115bc7Smrj 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
1942ae115bc7Smrj 	 */
1943ae115bc7Smrj 	bop_no_more_mem();
1944ae115bc7Smrj 
1945ae115bc7Smrj 	PRM_POINT("hat_kern_setup() done");
1946ae115bc7Smrj 
1947ae115bc7Smrj 	hat_cpu_online(CPU);
1948ae115bc7Smrj 
1949ae115bc7Smrj 	/*
19507c478bd9Sstevel@tonic-gate 	 * Initialize VM system
19517c478bd9Sstevel@tonic-gate 	 */
19527c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
19537c478bd9Sstevel@tonic-gate 	kvm_init();
19547c478bd9Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	/*
19577c478bd9Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
19587c478bd9Sstevel@tonic-gate 	 */
19597c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
19607c478bd9Sstevel@tonic-gate 		kdi_dvec_vmready();
19617c478bd9Sstevel@tonic-gate 
1962843e1988Sjohnlev #if defined(__xpv)
1963843e1988Sjohnlev 	/*
1964843e1988Sjohnlev 	 * Populate the I/O pool on domain 0
1965843e1988Sjohnlev 	 */
1966843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1967843e1988Sjohnlev 		extern long populate_io_pool(void);
1968843e1988Sjohnlev 		long init_io_pool_cnt;
1969843e1988Sjohnlev 
1970843e1988Sjohnlev 		PRM_POINT("Populating reserve I/O page pool");
1971843e1988Sjohnlev 		init_io_pool_cnt = populate_io_pool();
1972843e1988Sjohnlev 		PRM_DEBUG(init_io_pool_cnt);
1973843e1988Sjohnlev 	}
1974843e1988Sjohnlev #endif
19757c478bd9Sstevel@tonic-gate 	/*
19767c478bd9Sstevel@tonic-gate 	 * Mangle the brand string etc.
19777c478bd9Sstevel@tonic-gate 	 */
19787c478bd9Sstevel@tonic-gate 	cpuid_pass3(CPU);
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate #if defined(__amd64)
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	/*
19837c478bd9Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
19847c478bd9Sstevel@tonic-gate 	 */
19857c478bd9Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
19867c478bd9Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate #else	/* __i386 */
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	/*
19917c478bd9Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
19927c478bd9Sstevel@tonic-gate 	 */
1993ae115bc7Smrj 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
19947c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
19957c478bd9Sstevel@tonic-gate 	toxic_bit_map =
19967c478bd9Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
19977c478bd9Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
19987c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate #endif	/* __i386 */
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	/*
20047c478bd9Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
20057c478bd9Sstevel@tonic-gate 	 * it, tell the debugger.
20067c478bd9Sstevel@tonic-gate 	 */
20077c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
20087c478bd9Sstevel@tonic-gate 		kdi_dvec_memavail();
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	/*
20117c478bd9Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
20127c478bd9Sstevel@tonic-gate 	 * to work around a pentium bug.
20137c478bd9Sstevel@tonic-gate 	 */
2014843e1988Sjohnlev #if !defined(__amd64) && !defined(__xpv)
20157c478bd9Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
2016ae115bc7Smrj 		desctbr_t idtr;
20177c478bd9Sstevel@tonic-gate 		gate_desc_t *newidt;
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
20207c478bd9Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
20217c478bd9Sstevel@tonic-gate 
20220cfdb603Sjosephb 		bcopy(idt0, newidt, NIDT * sizeof (*idt0));
20237c478bd9Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
20249844da31SSeth Goldberg 		    KCS_SEL, SDT_SYSIGT, TRP_KPL, 0);
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
20277c478bd9Sstevel@tonic-gate 		    PROT_READ | PROT_EXEC);
20287c478bd9Sstevel@tonic-gate 
20290cfdb603Sjosephb 		CPU->cpu_idt = newidt;
20300cfdb603Sjosephb 		idtr.dtr_base = (uintptr_t)CPU->cpu_idt;
20310cfdb603Sjosephb 		idtr.dtr_limit = (NIDT * sizeof (*idt0)) - 1;
2032ae115bc7Smrj 		wr_idtr(&idtr);
20337c478bd9Sstevel@tonic-gate 	}
20347c478bd9Sstevel@tonic-gate #endif	/* !__amd64 */
20357c478bd9Sstevel@tonic-gate 
2036843e1988Sjohnlev #if !defined(__xpv)
20377c478bd9Sstevel@tonic-gate 	/*
20387c478bd9Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
20397c478bd9Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
20407c478bd9Sstevel@tonic-gate 	 */
20417c478bd9Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
20427c478bd9Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
20437c478bd9Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
2044843e1988Sjohnlev #endif
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
20477c478bd9Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
20487c478bd9Sstevel@tonic-gate 
204983f9b804Skchow 	/*
205083f9b804Skchow 	 * disable automatic large pages for small memory systems or
205183f9b804Skchow 	 * when the disable flag is set.
205202bc52beSkchow 	 *
205302bc52beSkchow 	 * Do not yet consider page sizes larger than 2m/4m.
205483f9b804Skchow 	 */
2055ec25b48fSsusans 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
2056ec25b48fSsusans 		max_uheap_lpsize = LEVEL_SIZE(1);
2057ec25b48fSsusans 		max_ustack_lpsize = LEVEL_SIZE(1);
2058ec25b48fSsusans 		max_privmap_lpsize = LEVEL_SIZE(1);
2059ec25b48fSsusans 		max_uidata_lpsize = LEVEL_SIZE(1);
2060ec25b48fSsusans 		max_utext_lpsize = LEVEL_SIZE(1);
2061ec25b48fSsusans 		max_shm_lpsize = LEVEL_SIZE(1);
2062ec25b48fSsusans 	}
2063ec25b48fSsusans 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
2064ec25b48fSsusans 	    auto_lpg_disable) {
2065beb1bda0Sdavemq 		use_brk_lpg = 0;
2066beb1bda0Sdavemq 		use_stk_lpg = 0;
2067ec25b48fSsusans 	}
206802bc52beSkchow 	mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
2069beb1bda0Sdavemq 
20707c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
20717c478bd9Sstevel@tonic-gate 	hat_init_finish();
20727c478bd9Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	/*
20757c478bd9Sstevel@tonic-gate 	 * Initialize the segkp segment type.
20767c478bd9Sstevel@tonic-gate 	 */
20777c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
2078ae115bc7Smrj 	PRM_POINT("Attaching segkp");
2079ae115bc7Smrj 	if (segkp_fromheap) {
2080ae115bc7Smrj 		segkp->s_as = &kas;
2081ae115bc7Smrj 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
20827c478bd9Sstevel@tonic-gate 	    segkp) < 0) {
20837c478bd9Sstevel@tonic-gate 		panic("startup: cannot attach segkp");
20847c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
20857c478bd9Sstevel@tonic-gate 	}
2086ae115bc7Smrj 	PRM_POINT("Doing segkp_create()");
20877c478bd9Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
20887c478bd9Sstevel@tonic-gate 		panic("startup: segkp_create failed");
20897c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
20907c478bd9Sstevel@tonic-gate 	}
20917c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segkp);
20927c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	/*
20957c478bd9Sstevel@tonic-gate 	 * kpm segment
20967c478bd9Sstevel@tonic-gate 	 */
20977c478bd9Sstevel@tonic-gate 	segmap_kpm = 0;
20987c478bd9Sstevel@tonic-gate 	if (kpm_desired) {
20997c478bd9Sstevel@tonic-gate 		kpm_init();
21007c478bd9Sstevel@tonic-gate 		kpm_enable = 1;
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	/*
21047c478bd9Sstevel@tonic-gate 	 * Now create segmap segment.
21057c478bd9Sstevel@tonic-gate 	 */
21067c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
2107ae115bc7Smrj 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
2108ae115bc7Smrj 		panic("cannot attach segmap");
21097c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
21107c478bd9Sstevel@tonic-gate 	}
2111ae115bc7Smrj 	PRM_DEBUG(segmap);
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
21147c478bd9Sstevel@tonic-gate 	a.shmsize = 0;
21157c478bd9Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
21167c478bd9Sstevel@tonic-gate 
2117ae115bc7Smrj 	if (segmap_create(segmap, (caddr_t)&a) != 0)
2118ae115bc7Smrj 		panic("segmap_create segmap");
21197c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	segdev_init();
2124843e1988Sjohnlev #if defined(__xpv)
2125843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2126843e1988Sjohnlev #endif
21277c478bd9Sstevel@tonic-gate 		pmem_init();
2128ae115bc7Smrj 
21297c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate 
2132ae115bc7Smrj /*
2133ae115bc7Smrj  * Load a tod module for the non-standard tod part found on this system.
2134ae115bc7Smrj  */
2135ae115bc7Smrj static void
2136ae115bc7Smrj load_tod_module(char *todmod)
2137ae115bc7Smrj {
2138ae115bc7Smrj 	if (modload("tod", todmod) == -1)
2139ae115bc7Smrj 		halt("Can't load TOD module");
2140ae115bc7Smrj }
2141ae115bc7Smrj 
21427c478bd9Sstevel@tonic-gate static void
21437c478bd9Sstevel@tonic-gate startup_end(void)
21447c478bd9Sstevel@tonic-gate {
2145dd4eeefdSeota 	int i;
21467c478bd9Sstevel@tonic-gate 	extern void setx86isalist(void);
2147fb2caebeSRandy Fishel 	extern void cpu_event_init(void);
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 	/*
21527c478bd9Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
21537c478bd9Sstevel@tonic-gate 	 * initialization has been done but before the clock
21547c478bd9Sstevel@tonic-gate 	 * and other devices get started.
21557c478bd9Sstevel@tonic-gate 	 */
21567c478bd9Sstevel@tonic-gate 	kern_setup1();
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	/*
21597c478bd9Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
21607c478bd9Sstevel@tonic-gate 	 */
21617c478bd9Sstevel@tonic-gate 	kcpc_hw_init(CPU);
21627c478bd9Sstevel@tonic-gate 
2163fb2caebeSRandy Fishel 	/*
2164fb2caebeSRandy Fishel 	 * Initialize cpu event framework.
2165fb2caebeSRandy Fishel 	 */
2166fb2caebeSRandy Fishel 	cpu_event_init();
2167fb2caebeSRandy Fishel 
2168ee88d2b9Skchow #if defined(OPTERON_WORKAROUND_6323525)
2169ee88d2b9Skchow 	if (opteron_workaround_6323525)
2170ee88d2b9Skchow 		patch_workaround_6323525();
2171ee88d2b9Skchow #endif
21727c478bd9Sstevel@tonic-gate 	/*
2173ae115bc7Smrj 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
2174ae115bc7Smrj 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
2175ae115bc7Smrj 	 */
2176ae115bc7Smrj 	if (tod_module_name != NULL) {
2177ae115bc7Smrj 		PRM_POINT("load_tod_module()");
2178ae115bc7Smrj 		load_tod_module(tod_module_name);
2179ae115bc7Smrj 	}
2180ae115bc7Smrj 
2181843e1988Sjohnlev #if defined(__xpv)
2182843e1988Sjohnlev 	/*
2183843e1988Sjohnlev 	 * Forceload interposing TOD module for the hypervisor.
2184843e1988Sjohnlev 	 */
2185843e1988Sjohnlev 	PRM_POINT("load_tod_module()");
2186843e1988Sjohnlev 	load_tod_module("xpvtod");
2187843e1988Sjohnlev #endif
2188843e1988Sjohnlev 
2189ae115bc7Smrj 	/*
21907c478bd9Sstevel@tonic-gate 	 * Configure the system.
21917c478bd9Sstevel@tonic-gate 	 */
21927c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
21937c478bd9Sstevel@tonic-gate 	configure();		/* set up devices */
21947c478bd9Sstevel@tonic-gate 	PRM_POINT("configure() done");
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	/*
21977c478bd9Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
21987c478bd9Sstevel@tonic-gate 	 * support.
21997c478bd9Sstevel@tonic-gate 	 */
22007c478bd9Sstevel@tonic-gate 	setx86isalist();
2201100b72f4Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
22027c478bd9Sstevel@tonic-gate 	psm_install();
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 	/*
22057c478bd9Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
22067c478bd9Sstevel@tonic-gate 	 * we're still using bootsvcs.
22077c478bd9Sstevel@tonic-gate 	 */
2208ae115bc7Smrj 	PRM_POINT("NULLing out bootops");
2209ae115bc7Smrj 	*bootopsp = (struct bootops *)NULL;
22107c478bd9Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
22117c478bd9Sstevel@tonic-gate 
2212843e1988Sjohnlev #if defined(__xpv)
2213843e1988Sjohnlev 	ec_init_debug_irq();
2214843e1988Sjohnlev 	xs_domu_init();
2215843e1988Sjohnlev #endif
22161de082f7SVikram Hegde 
22173a634bfcSVikram Hegde #if defined(__amd64) && !defined(__xpv)
22183a634bfcSVikram Hegde 	/*
22193a634bfcSVikram Hegde 	 * Intel IOMMU has been setup/initialized in ddi_impl.c
22203a634bfcSVikram Hegde 	 * Start it up now.
22213a634bfcSVikram Hegde 	 */
22223a634bfcSVikram Hegde 	immu_startup();
22231de082f7SVikram Hegde #endif
22243a634bfcSVikram Hegde 
22257c478bd9Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
22267c478bd9Sstevel@tonic-gate 	(*picinitf)();
22277c478bd9Sstevel@tonic-gate 	sti();
2228843e1988Sjohnlev #if defined(__xpv)
2229843e1988Sjohnlev 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
2230843e1988Sjohnlev 	xen_late_startup();
2231843e1988Sjohnlev #endif
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
22347c478bd9Sstevel@tonic-gate 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
22357c478bd9Sstevel@tonic-gate 
2236dd4eeefdSeota 	/*
2237dd4eeefdSeota 	 * Register these software interrupts for ddi timer.
2238dd4eeefdSeota 	 * Software interrupts up to the level 10 are supported.
2239dd4eeefdSeota 	 */
2240dd4eeefdSeota 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
2241dd4eeefdSeota 		char name[sizeof ("timer_softintr") + 2];
2242dd4eeefdSeota 		(void) sprintf(name, "timer_softintr%02d", i);
2243dd4eeefdSeota 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
2244dd4eeefdSeota 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
2245dd4eeefdSeota 	}
2246dd4eeefdSeota 
22477125fcbdSVikram Hegde #if !defined(__xpv)
22487125fcbdSVikram Hegde 	if (modload("drv", "amd_iommu") < 0) {
22497125fcbdSVikram Hegde 		PRM_POINT("No AMD IOMMU present\n");
225094f1124eSVikram Hegde 	} else if (ddi_hold_installed_driver(ddi_name_to_major(
225194f1124eSVikram Hegde 	    "amd_iommu")) == NULL) {
22527125fcbdSVikram Hegde 		prom_printf("ERROR: failed to attach AMD IOMMU\n");
22537125fcbdSVikram Hegde 	}
22547125fcbdSVikram Hegde #endif
2255e774b42bSBill Holler 	post_startup_cpu_fixups();
22567125fcbdSVikram Hegde 
22577c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
22587c478bd9Sstevel@tonic-gate }
22597c478bd9Sstevel@tonic-gate 
226096d008c2SKen Erickson /*
226196d008c2SKen Erickson  * Don't remove the following 2 variables.  They are necessary
226296d008c2SKen Erickson  * for reading the hostid from the legacy file (/kernel/misc/sysinit).
226396d008c2SKen Erickson  */
22647c478bd9Sstevel@tonic-gate char *_hs1107 = hw_serial;
22657c478bd9Sstevel@tonic-gate ulong_t  _bdhs34;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate void
22687c478bd9Sstevel@tonic-gate post_startup(void)
22697c478bd9Sstevel@tonic-gate {
22700e751525SEric Saxe 	extern void cpupm_init(cpu_t *);
2271fb2caebeSRandy Fishel 	extern void cpu_event_init_cpu(cpu_t *);
22720e751525SEric Saxe 
22737c478bd9Sstevel@tonic-gate 	/*
22747c478bd9Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
22757c478bd9Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
22767c478bd9Sstevel@tonic-gate 	 * instruction set extensions.
22777c478bd9Sstevel@tonic-gate 	 */
22787c478bd9Sstevel@tonic-gate 	bind_hwcap();
22797c478bd9Sstevel@tonic-gate 
2280843e1988Sjohnlev #ifdef __xpv
2281843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2282843e1988Sjohnlev #endif
2283843e1988Sjohnlev 	{
2284843e1988Sjohnlev #if defined(__xpv)
2285843e1988Sjohnlev 		xpv_panic_init();
2286843e1988Sjohnlev #else
228784ab085aSmws 		/*
2288ae115bc7Smrj 		 * Startup the memory scrubber.
2289843e1988Sjohnlev 		 * XXPV	This should be running somewhere ..
22907c478bd9Sstevel@tonic-gate 		 */
2291b9bfdccdSStuart Maybee 		if (get_hwenv() != HW_XEN_HVM)
229284ab085aSmws 			memscrub_init();
2293843e1988Sjohnlev #endif
2294843e1988Sjohnlev 	}
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	/*
22977aec1d6eScindi 	 * Complete CPU module initialization
22987aec1d6eScindi 	 */
229920c794b3Sgavinm 	cmi_post_startup();
23007aec1d6eScindi 
23017aec1d6eScindi 	/*
23027c478bd9Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
23037c478bd9Sstevel@tonic-gate 	 */
23047c478bd9Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	/*
23077c478bd9Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
23087c478bd9Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
23097c478bd9Sstevel@tonic-gate 	 */
23107c478bd9Sstevel@tonic-gate 	(void) modload("fs", "procfs");
23117c478bd9Sstevel@tonic-gate 
2312c35aa225Smarx 	(void) i_ddi_attach_hw_nodes("pit_beep");
2313c35aa225Smarx 
23147c478bd9Sstevel@tonic-gate #if defined(__i386)
23157c478bd9Sstevel@tonic-gate 	/*
23167c478bd9Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
23177c478bd9Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
23187c478bd9Sstevel@tonic-gate 	 */
23197c478bd9Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
23207c478bd9Sstevel@tonic-gate 		halt("No working FP hardware found");
23217c478bd9Sstevel@tonic-gate #endif
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 	maxmem = freemem;
23247c478bd9Sstevel@tonic-gate 
2325fb2caebeSRandy Fishel 	cpu_event_init_cpu(CPU);
23260e751525SEric Saxe 	cpupm_init(CPU);
2327fa96bd91SMichael Corcoran 	(void) mach_cpu_create_device_node(CPU, NULL);
23280e751525SEric Saxe 
23290e751525SEric Saxe 	pg_init();
23307c478bd9Sstevel@tonic-gate }
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate static int
233319397407SSherry Moore pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
23347c478bd9Sstevel@tonic-gate {
233519397407SSherry Moore 	return ((pp->p_pagenum >= btop(low_addr)) &&
233619397407SSherry Moore 	    (pp->p_pagenum < btopr(high_addr)));
23377c478bd9Sstevel@tonic-gate }
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate void
23407c478bd9Sstevel@tonic-gate release_bootstrap(void)
23417c478bd9Sstevel@tonic-gate {
23427c478bd9Sstevel@tonic-gate 	int root_is_ramdisk;
23437c478bd9Sstevel@tonic-gate 	page_t *pp;
23447c478bd9Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
23457c478bd9Sstevel@tonic-gate 	extern dev_t rootdev;
234655d507a9SSeth Goldberg #if !defined(__xpv)
234755d507a9SSeth Goldberg 	pfn_t	pfn;
234855d507a9SSeth Goldberg #endif
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
23517c478bd9Sstevel@tonic-gate 	kobj_boot_unmountroot();
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	/*
23547c478bd9Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
23557c478bd9Sstevel@tonic-gate 	 */
23567c478bd9Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
235719397407SSherry Moore 
2358ae115bc7Smrj 	clear_boot_mappings(0, _userlimit);
235919397407SSherry Moore 
2360ae115bc7Smrj 	postbootkernelbase = kernelbase;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	/*
23637c478bd9Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
23647c478bd9Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
23657c478bd9Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
23667c478bd9Sstevel@tonic-gate 	 */
23677c478bd9Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
23687c478bd9Sstevel@tonic-gate 	if (!root_is_ramdisk) {
23697c478bd9Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
23707c478bd9Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
23717c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
23727c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
23737c478bd9Sstevel@tonic-gate 	}
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
23767c478bd9Sstevel@tonic-gate 	while (bootpages) {
237719397407SSherry Moore 		extern uint64_t ramdisk_start, ramdisk_end;
23787c478bd9Sstevel@tonic-gate 		pp = bootpages;
23797c478bd9Sstevel@tonic-gate 		bootpages = pp->p_next;
238019397407SSherry Moore 
238119397407SSherry Moore 
238219397407SSherry Moore 		/* Keep pages for the lower 64K */
238319397407SSherry Moore 		if (pp_in_range(pp, 0, 0x40000)) {
238419397407SSherry Moore 			pp->p_next = lower_pages;
238519397407SSherry Moore 			lower_pages = pp;
238619397407SSherry Moore 			lower_pages_count++;
238719397407SSherry Moore 			continue;
238819397407SSherry Moore 		}
238919397407SSherry Moore 
239019397407SSherry Moore 
239119397407SSherry Moore 		if (root_is_ramdisk && pp_in_range(pp, ramdisk_start,
239219397407SSherry Moore 		    ramdisk_end)) {
23937c478bd9Sstevel@tonic-gate 			pp->p_next = rd_pages;
23947c478bd9Sstevel@tonic-gate 			rd_pages = pp;
23957c478bd9Sstevel@tonic-gate 			continue;
23967c478bd9Sstevel@tonic-gate 		}
23977c478bd9Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
2398ae115bc7Smrj 		pp->p_prev = (struct page *)0;
2399ae115bc7Smrj 		PP_CLRBOOTPAGES(pp);
24007c478bd9Sstevel@tonic-gate 		page_free(pp, 1);
24017c478bd9Sstevel@tonic-gate 	}
2402ae115bc7Smrj 	PRM_POINT("Boot pages released");
24037c478bd9Sstevel@tonic-gate 
2404843e1988Sjohnlev #if !defined(__xpv)
2405843e1988Sjohnlev /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
24067c478bd9Sstevel@tonic-gate 	/*
240755d507a9SSeth Goldberg 	 * Find 1 page below 1 MB so that other processors can boot up or
240855d507a9SSeth Goldberg 	 * so that any processor can resume.
24097c478bd9Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
24107c478bd9Sstevel@tonic-gate 	 * We should have just free'd one up.
24117c478bd9Sstevel@tonic-gate 	 */
241219397407SSherry Moore 
241319397407SSherry Moore 	/*
241419397407SSherry Moore 	 * 0x10 pages is 64K.  Leave the bottom 64K alone
241519397407SSherry Moore 	 * for BIOS.
241619397407SSherry Moore 	 */
241719397407SSherry Moore 	for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
24187c478bd9Sstevel@tonic-gate 		if (page_numtopp_alloc(pfn) == NULL)
24197c478bd9Sstevel@tonic-gate 			continue;
24207c478bd9Sstevel@tonic-gate 		rm_platter_va = i86devmap(pfn, 1,
24217c478bd9Sstevel@tonic-gate 		    PROT_READ | PROT_WRITE | PROT_EXEC);
24227c478bd9Sstevel@tonic-gate 		rm_platter_pa = ptob(pfn);
24237c478bd9Sstevel@tonic-gate 		break;
24247c478bd9Sstevel@tonic-gate 	}
242555d507a9SSeth Goldberg 	if (pfn == btop(1*1024*1024) && use_mp)
242655d507a9SSeth Goldberg 		panic("No page below 1M available for starting "
242755d507a9SSeth Goldberg 		    "other processors or for resuming from system-suspend");
2428843e1988Sjohnlev #endif	/* !__xpv */
24297c478bd9Sstevel@tonic-gate }
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate /*
24327c478bd9Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
24337c478bd9Sstevel@tonic-gate  */
24347c478bd9Sstevel@tonic-gate void
24357c478bd9Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
24367c478bd9Sstevel@tonic-gate {
24377c478bd9Sstevel@tonic-gate 	pp->p_pagenum = pnum;
24387c478bd9Sstevel@tonic-gate 	pp->p_mapping = NULL;
24397c478bd9Sstevel@tonic-gate 	pp->p_embed = 0;
24407c478bd9Sstevel@tonic-gate 	pp->p_share = 0;
24417c478bd9Sstevel@tonic-gate 	pp->p_mlentry = 0;
24427c478bd9Sstevel@tonic-gate }
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate /*
24457c478bd9Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
24467c478bd9Sstevel@tonic-gate  */
24477c478bd9Sstevel@tonic-gate static pgcnt_t
24487c478bd9Sstevel@tonic-gate kphysm_init(
2449ae115bc7Smrj 	page_t *pp,
24507c478bd9Sstevel@tonic-gate 	pgcnt_t npages)
24517c478bd9Sstevel@tonic-gate {
24527c478bd9Sstevel@tonic-gate 	struct memlist	*pmem;
24537c478bd9Sstevel@tonic-gate 	struct memseg	*cur_memseg;
24547c478bd9Sstevel@tonic-gate 	pfn_t		base_pfn;
245518968004SKit Chow 	pfn_t		end_pfn;
24567c478bd9Sstevel@tonic-gate 	pgcnt_t		num;
24577c478bd9Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
24587c478bd9Sstevel@tonic-gate 	uint64_t	addr;
24597c478bd9Sstevel@tonic-gate 	uint64_t	size;
24607c478bd9Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
246118968004SKit Chow 	extern int	mnode_xwa;
246218968004SKit Chow 	int		ms = 0, me = 0;
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
24657c478bd9Sstevel@tonic-gate 
2466ae115bc7Smrj 	cur_memseg = memseg_base;
246756f33205SJonathan Adams 	for (pmem = phys_avail; pmem && npages; pmem = pmem->ml_next) {
24687c478bd9Sstevel@tonic-gate 		/*
24697c478bd9Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
24707c478bd9Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
24717c478bd9Sstevel@tonic-gate 		 */
247256f33205SJonathan Adams 		addr = pmem->ml_address;
247356f33205SJonathan Adams 		size = pmem->ml_size;
24747c478bd9Sstevel@tonic-gate 		if (btop(addr) > physmax)
24757c478bd9Sstevel@tonic-gate 			continue;
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 		/*
24787c478bd9Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
24797c478bd9Sstevel@tonic-gate 		 */
24807c478bd9Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
24817c478bd9Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
24827c478bd9Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
248356f33205SJonathan Adams 			size -= addr - pmem->ml_address;
24847c478bd9Sstevel@tonic-gate 		}
24857c478bd9Sstevel@tonic-gate 
24866bb54764Skchow 		/* only process pages below or equal to physmax */
24876bb54764Skchow 		if ((btop(addr + size) - 1) > physmax)
24886bb54764Skchow 			size = ptob(physmax - btop(addr) + 1);
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 		num = btop(size);
24917c478bd9Sstevel@tonic-gate 		if (num == 0)
24927c478bd9Sstevel@tonic-gate 			continue;
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 		if (num > npages)
24957c478bd9Sstevel@tonic-gate 			num = npages;
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 		npages -= num;
24987c478bd9Sstevel@tonic-gate 		pages_done += num;
24997c478bd9Sstevel@tonic-gate 		base_pfn = btop(addr);
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 		if (prom_debug)
25027c478bd9Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
25037c478bd9Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
25047c478bd9Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate 		/*
2507ae115bc7Smrj 		 * Ignore pages below ddiphysmin to simplify ddi memory
25087c478bd9Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
25097c478bd9Sstevel@tonic-gate 		 */
25107c478bd9Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
2511ae115bc7Smrj 			if (base_pfn + num <= ddiphysmin)
25127c478bd9Sstevel@tonic-gate 				continue;
25137c478bd9Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
25147c478bd9Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
25157c478bd9Sstevel@tonic-gate 			base_pfn = ddiphysmin;
25167c478bd9Sstevel@tonic-gate 		}
2517ae115bc7Smrj 
25187c478bd9Sstevel@tonic-gate 		/*
251918968004SKit Chow 		 * mnode_xwa is greater than 1 when large pages regions can
252018968004SKit Chow 		 * cross memory node boundaries. To prevent the formation
252118968004SKit Chow 		 * of these large pages, configure the memsegs based on the
252218968004SKit Chow 		 * memory node ranges which had been made non-contiguous.
252318968004SKit Chow 		 */
252418968004SKit Chow 		if (mnode_xwa > 1) {
252518968004SKit Chow 
252618968004SKit Chow 			end_pfn = base_pfn + num - 1;
252718968004SKit Chow 			ms = PFN_2_MEM_NODE(base_pfn);
252818968004SKit Chow 			me = PFN_2_MEM_NODE(end_pfn);
252918968004SKit Chow 
253018968004SKit Chow 			if (ms != me) {
253118968004SKit Chow 				/*
253218968004SKit Chow 				 * current range spans more than 1 memory node.
253318968004SKit Chow 				 * Set num to only the pfn range in the start
253418968004SKit Chow 				 * memory node.
253518968004SKit Chow 				 */
253618968004SKit Chow 				num = mem_node_config[ms].physmax - base_pfn
253718968004SKit Chow 				    + 1;
253818968004SKit Chow 				ASSERT(end_pfn > mem_node_config[ms].physmax);
253918968004SKit Chow 			}
254018968004SKit Chow 		}
254118968004SKit Chow 
254218968004SKit Chow 		for (;;) {
254318968004SKit Chow 			/*
25447c478bd9Sstevel@tonic-gate 			 * Build the memsegs entry
25457c478bd9Sstevel@tonic-gate 			 */
25467c478bd9Sstevel@tonic-gate 			cur_memseg->pages = pp;
25477c478bd9Sstevel@tonic-gate 			cur_memseg->epages = pp + num;
25487c478bd9Sstevel@tonic-gate 			cur_memseg->pages_base = base_pfn;
25497c478bd9Sstevel@tonic-gate 			cur_memseg->pages_end = base_pfn + num;
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 			/*
255218968004SKit Chow 			 * Insert into memseg list in decreasing pfn range
255318968004SKit Chow 			 * order. Low memory is typically more fragmented such
255418968004SKit Chow 			 * that this ordering keeps the larger ranges at the
255518968004SKit Chow 			 * front of the list for code that searches memseg.
255618968004SKit Chow 			 * This ASSERTS that the memsegs coming in from boot
255718968004SKit Chow 			 * are in increasing physical address order and not
255818968004SKit Chow 			 * contiguous.
25597c478bd9Sstevel@tonic-gate 			 */
2560ae115bc7Smrj 			if (memsegs != NULL) {
256118968004SKit Chow 				ASSERT(cur_memseg->pages_base >=
256218968004SKit Chow 				    memsegs->pages_end);
2563ae115bc7Smrj 				cur_memseg->next = memsegs;
2564ae115bc7Smrj 			}
25657c478bd9Sstevel@tonic-gate 			memsegs = cur_memseg;
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 			/*
25687c478bd9Sstevel@tonic-gate 			 * add_physmem() initializes the PSM part of the page
25697c478bd9Sstevel@tonic-gate 			 * struct by calling the PSM back with add_physmem_cb().
25707c478bd9Sstevel@tonic-gate 			 * In addition it coalesces pages into larger pages as
25717c478bd9Sstevel@tonic-gate 			 * it initializes them.
25727c478bd9Sstevel@tonic-gate 			 */
25737c478bd9Sstevel@tonic-gate 			add_physmem(pp, num, base_pfn);
25747c478bd9Sstevel@tonic-gate 			cur_memseg++;
25757c478bd9Sstevel@tonic-gate 			availrmem_initial += num;
25767c478bd9Sstevel@tonic-gate 			availrmem += num;
25777c478bd9Sstevel@tonic-gate 
2578ae115bc7Smrj 			pp += num;
257918968004SKit Chow 			if (ms >= me)
258018968004SKit Chow 				break;
258118968004SKit Chow 
258218968004SKit Chow 			/* process next memory node range */
258318968004SKit Chow 			ms++;
258418968004SKit Chow 			base_pfn = mem_node_config[ms].physbase;
258518968004SKit Chow 			num = MIN(mem_node_config[ms].physmax,
258618968004SKit Chow 			    end_pfn) - base_pfn + 1;
258718968004SKit Chow 		}
25887c478bd9Sstevel@tonic-gate 	}
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
25917c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
25927c478bd9Sstevel@tonic-gate 	PRM_DEBUG(freemem);
25937c478bd9Sstevel@tonic-gate 	build_pfn_hash();
25947c478bd9Sstevel@tonic-gate 	return (pages_done);
25957c478bd9Sstevel@tonic-gate }
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate /*
25987c478bd9Sstevel@tonic-gate  * Kernel VM initialization.
25997c478bd9Sstevel@tonic-gate  */
26007c478bd9Sstevel@tonic-gate static void
26017c478bd9Sstevel@tonic-gate kvm_init(void)
26027c478bd9Sstevel@tonic-gate {
26037c478bd9Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	/*
26067c478bd9Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
26077c478bd9Sstevel@tonic-gate 	 */
26087c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
26097c478bd9Sstevel@tonic-gate 	as_avlinit(&kas);
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
26127c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
26157c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
26167c478bd9Sstevel@tonic-gate 
261735b1ab99Sjosephb 	(void) seg_attach(&kas, kernelheap,
261835b1ab99Sjosephb 	    ekernelheap - kernelheap, &kvseg);
26197c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
26207c478bd9Sstevel@tonic-gate 
2621ae115bc7Smrj 	if (core_size > 0) {
2622ae115bc7Smrj 		PRM_POINT("attaching kvseg_core");
2623ae115bc7Smrj 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
2624ae115bc7Smrj 		    &kvseg_core);
26257c478bd9Sstevel@tonic-gate 		(void) segkmem_create(&kvseg_core);
2626ae115bc7Smrj 	}
2627ad23a2dbSjohansen 
2628ae115bc7Smrj 	if (segziosize > 0) {
2629ae115bc7Smrj 		PRM_POINT("attaching segzio");
2630ad23a2dbSjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2631ad23a2dbSjohansen 		    &kzioseg);
2632ad23a2dbSjohansen 		(void) segkmem_zio_create(&kzioseg);
2633ad23a2dbSjohansen 
2634ad23a2dbSjohansen 		/* create zio area covering new segment */
2635ad23a2dbSjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2636ad23a2dbSjohansen 	}
26377c478bd9Sstevel@tonic-gate 
2638ae115bc7Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
26397c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	/*
26447c478bd9Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
26457c478bd9Sstevel@tonic-gate 	 */
2646ae115bc7Smrj 	PRM_POINT("protecting redzone");
26477c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	/*
26507c478bd9Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
26517c478bd9Sstevel@tonic-gate 	 */
26527c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
26537c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 	/*
26567c478bd9Sstevel@tonic-gate 	 * Make data writable until end.
26577c478bd9Sstevel@tonic-gate 	 */
26587c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
26597c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
26607c478bd9Sstevel@tonic-gate }
26617c478bd9Sstevel@tonic-gate 
2662843e1988Sjohnlev #ifndef __xpv
26637c478bd9Sstevel@tonic-gate /*
26641d03c31eSjohnlev  * Solaris adds an entry for Write Combining caching to the PAT
26657c478bd9Sstevel@tonic-gate  */
26661d03c31eSjohnlev static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate void
26691d03c31eSjohnlev pat_sync(void)
26707c478bd9Sstevel@tonic-gate {
26711d03c31eSjohnlev 	ulong_t	cr0, cr0_orig, cr4;
26727c478bd9Sstevel@tonic-gate 
26731d03c31eSjohnlev 	if (!(x86_feature & X86_PAT))
26747c478bd9Sstevel@tonic-gate 		return;
26751d03c31eSjohnlev 	cr0_orig = cr0 = getcr0();
26761d03c31eSjohnlev 	cr4 = getcr4();
26777c478bd9Sstevel@tonic-gate 
26781d03c31eSjohnlev 	/* disable caching and flush all caches and TLBs */
26791d03c31eSjohnlev 	cr0 |= CR0_CD;
26801d03c31eSjohnlev 	cr0 &= ~CR0_NW;
26811d03c31eSjohnlev 	setcr0(cr0);
26827c478bd9Sstevel@tonic-gate 	invalidate_cache();
26831d03c31eSjohnlev 	if (cr4 & CR4_PGE) {
26841d03c31eSjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
26851d03c31eSjohnlev 		setcr4(cr4);
26861d03c31eSjohnlev 	} else {
2687ae115bc7Smrj 		reload_cr3();
26887c478bd9Sstevel@tonic-gate 	}
2689ae115bc7Smrj 
26901d03c31eSjohnlev 	/* add our entry to the PAT */
26911d03c31eSjohnlev 	wrmsr(REG_PAT, pat_attr_reg);
26921d03c31eSjohnlev 
26931d03c31eSjohnlev 	/* flush TLBs and cache again, then reenable cr0 caching */
26941d03c31eSjohnlev 	if (cr4 & CR4_PGE) {
26951d03c31eSjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
26961d03c31eSjohnlev 		setcr4(cr4);
26971d03c31eSjohnlev 	} else {
2698ae115bc7Smrj 		reload_cr3();
26991d03c31eSjohnlev 	}
27007c478bd9Sstevel@tonic-gate 	invalidate_cache();
27017c478bd9Sstevel@tonic-gate 	setcr0(cr0_orig);
27027c478bd9Sstevel@tonic-gate }
27037c478bd9Sstevel@tonic-gate 
27041d03c31eSjohnlev #endif /* !__xpv */
27057c478bd9Sstevel@tonic-gate 
2706ed5289f9SKen Erickson #if defined(_SOFT_HOSTID)
2707ed5289f9SKen Erickson /*
2708ed5289f9SKen Erickson  * On platforms that do not have a hardware serial number, attempt
2709ed5289f9SKen Erickson  * to set one based on the contents of /etc/hostid.  If this file does
2710ed5289f9SKen Erickson  * not exist, assume that we are to generate a new hostid and set
2711ed5289f9SKen Erickson  * it in the kernel, for subsequent saving by a userland process
2712ed5289f9SKen Erickson  * once the system is up and the root filesystem is mounted r/w.
2713ed5289f9SKen Erickson  *
271496d008c2SKen Erickson  * In order to gracefully support upgrade on OpenSolaris, if
271596d008c2SKen Erickson  * /etc/hostid does not exist, we will attempt to get a serial number
271696d008c2SKen Erickson  * using the legacy method (/kernel/misc/sysinit).
271796d008c2SKen Erickson  *
2718ed5289f9SKen Erickson  * In an attempt to make the hostid less prone to abuse
2719ed5289f9SKen Erickson  * (for license circumvention, etc), we store it in /etc/hostid
2720ed5289f9SKen Erickson  * in rot47 format.
2721ed5289f9SKen Erickson  */
2722ed5289f9SKen Erickson extern volatile unsigned long tenmicrodata;
272396d008c2SKen Erickson static int atoi(char *);
2724ed5289f9SKen Erickson 
2725ed5289f9SKen Erickson static int32_t
2726ed5289f9SKen Erickson set_soft_hostid(void)
2727ed5289f9SKen Erickson {
2728ed5289f9SKen Erickson 	struct _buf *file;
2729ed5289f9SKen Erickson 	char tokbuf[MAXNAMELEN];
2730ed5289f9SKen Erickson 	token_t token;
2731ed5289f9SKen Erickson 	int done = 0;
2732ed5289f9SKen Erickson 	u_longlong_t tmp;
273396d008c2SKen Erickson 	int i;
27345679c89fSjv227347 	int32_t hostid = (int32_t)HW_INVALID_HOSTID;
2735ed5289f9SKen Erickson 	unsigned char *c;
2736ed5289f9SKen Erickson 	hrtime_t tsc;
2737ed5289f9SKen Erickson 
2738ed5289f9SKen Erickson 	/*
2739ed5289f9SKen Erickson 	 * If /etc/hostid file not found, we'd like to get a pseudo
2740ed5289f9SKen Erickson 	 * random number to use at the hostid.  A nice way to do this
2741ed5289f9SKen Erickson 	 * is to read the real time clock.  To remain xen-compatible,
2742ed5289f9SKen Erickson 	 * we can't poke the real hardware, so we use tsc_read() to
2743ed5289f9SKen Erickson 	 * read the real time clock.  However, there is an ominous
2744ed5289f9SKen Erickson 	 * warning in tsc_read that says it can return zero, so we
2745ed5289f9SKen Erickson 	 * deal with that possibility by falling back to using the
2746ed5289f9SKen Erickson 	 * (hopefully random enough) value in tenmicrodata.
2747ed5289f9SKen Erickson 	 */
2748ed5289f9SKen Erickson 
2749ed5289f9SKen Erickson 	if ((file = kobj_open_file(hostid_file)) == (struct _buf *)-1) {
275096d008c2SKen Erickson 		/*
275196d008c2SKen Erickson 		 * hostid file not found - try to load sysinit module
275296d008c2SKen Erickson 		 * and see if it has a nonzero hostid value...use that
275396d008c2SKen Erickson 		 * instead of generating a new hostid here if so.
275496d008c2SKen Erickson 		 */
275596d008c2SKen Erickson 		if ((i = modload("misc", "sysinit")) != -1) {
275696d008c2SKen Erickson 			if (strlen(hw_serial) > 0)
275796d008c2SKen Erickson 				hostid = (int32_t)atoi(hw_serial);
275896d008c2SKen Erickson 			(void) modunload(i);
275996d008c2SKen Erickson 		}
27605679c89fSjv227347 		if (hostid == HW_INVALID_HOSTID) {
2761ed5289f9SKen Erickson 			tsc = tsc_read();
2762ed5289f9SKen Erickson 			if (tsc == 0)	/* tsc_read can return zero sometimes */
2763ed5289f9SKen Erickson 				hostid = (int32_t)tenmicrodata & 0x0CFFFFF;
2764ed5289f9SKen Erickson 			else
2765ed5289f9SKen Erickson 				hostid = (int32_t)tsc & 0x0CFFFFF;
276696d008c2SKen Erickson 		}
2767ed5289f9SKen Erickson 	} else {
2768ed5289f9SKen Erickson 		/* hostid file found */
2769ed5289f9SKen Erickson 		while (!done) {
2770ed5289f9SKen Erickson 			token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2771ed5289f9SKen Erickson 
2772ed5289f9SKen Erickson 			switch (token) {
2773ed5289f9SKen Erickson 			case POUND:
2774ed5289f9SKen Erickson 				/*
2775ed5289f9SKen Erickson 				 * skip comments
2776ed5289f9SKen Erickson 				 */
2777ed5289f9SKen Erickson 				kobj_find_eol(file);
2778ed5289f9SKen Erickson 				break;
2779ed5289f9SKen Erickson 			case STRING:
2780ed5289f9SKen Erickson 				/*
2781ed5289f9SKen Erickson 				 * un-rot47 - obviously this
2782ed5289f9SKen Erickson 				 * nonsense is ascii-specific
2783ed5289f9SKen Erickson 				 */
2784ed5289f9SKen Erickson 				for (c = (unsigned char *)tokbuf;
2785ed5289f9SKen Erickson 				    *c != '\0'; c++) {
2786ed5289f9SKen Erickson 					*c += 47;
2787ed5289f9SKen Erickson 					if (*c > '~')
2788ed5289f9SKen Erickson 						*c -= 94;
2789ed5289f9SKen Erickson 					else if (*c < '!')
2790ed5289f9SKen Erickson 						*c += 94;
2791ed5289f9SKen Erickson 				}
2792ed5289f9SKen Erickson 				/*
2793ed5289f9SKen Erickson 				 * now we should have a real number
2794ed5289f9SKen Erickson 				 */
2795ed5289f9SKen Erickson 
2796ed5289f9SKen Erickson 				if (kobj_getvalue(tokbuf, &tmp) != 0)
2797ed5289f9SKen Erickson 					kobj_file_err(CE_WARN, file,
2798ed5289f9SKen Erickson 					    "Bad value %s for hostid",
2799ed5289f9SKen Erickson 					    tokbuf);
2800ed5289f9SKen Erickson 				else
2801ed5289f9SKen Erickson 					hostid = (int32_t)tmp;
2802ed5289f9SKen Erickson 
2803ed5289f9SKen Erickson 				break;
2804ed5289f9SKen Erickson 			case EOF:
2805ed5289f9SKen Erickson 				done = 1;
2806ed5289f9SKen Erickson 				/* FALLTHROUGH */
2807ed5289f9SKen Erickson 			case NEWLINE:
2808ed5289f9SKen Erickson 				kobj_newline(file);
2809ed5289f9SKen Erickson 				break;
2810ed5289f9SKen Erickson 			default:
2811ed5289f9SKen Erickson 				break;
2812ed5289f9SKen Erickson 
2813ed5289f9SKen Erickson 			}
2814ed5289f9SKen Erickson 		}
28155679c89fSjv227347 		if (hostid == HW_INVALID_HOSTID) /* didn't find a hostid */
2816ed5289f9SKen Erickson 			kobj_file_err(CE_WARN, file,
2817ed5289f9SKen Erickson 			    "hostid missing or corrupt");
2818ed5289f9SKen Erickson 
2819ed5289f9SKen Erickson 		kobj_close_file(file);
2820ed5289f9SKen Erickson 	}
2821ed5289f9SKen Erickson 	/*
2822ed5289f9SKen Erickson 	 * hostid is now the value read from /etc/hostid, or the
28235679c89fSjv227347 	 * new hostid we generated in this routine or HW_INVALID_HOSTID if not
28245679c89fSjv227347 	 * set.
2825ed5289f9SKen Erickson 	 */
2826ed5289f9SKen Erickson 	return (hostid);
2827ed5289f9SKen Erickson }
282896d008c2SKen Erickson 
282996d008c2SKen Erickson static int
283096d008c2SKen Erickson atoi(char *p)
283196d008c2SKen Erickson {
283296d008c2SKen Erickson 	int i = 0;
283396d008c2SKen Erickson 
283496d008c2SKen Erickson 	while (*p != '\0')
283596d008c2SKen Erickson 		i = 10 * i + (*p++ - '0');
283696d008c2SKen Erickson 
283796d008c2SKen Erickson 	return (i);
283896d008c2SKen Erickson }
283996d008c2SKen Erickson 
2840ed5289f9SKen Erickson #endif /* _SOFT_HOSTID */
2841ed5289f9SKen Erickson 
28427c478bd9Sstevel@tonic-gate void
284345916cd2Sjpk get_system_configuration(void)
28447c478bd9Sstevel@tonic-gate {
28457c478bd9Sstevel@tonic-gate 	char	prop[32];
28467c478bd9Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
28477c478bd9Sstevel@tonic-gate 
284835b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
284935b1ab99Sjosephb 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
285035b1ab99Sjosephb 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
285135b1ab99Sjosephb 	    nodes_ll > MAXNODES ||
285235b1ab99Sjosephb 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
285335b1ab99Sjosephb 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
285435b1ab99Sjosephb 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
28557c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
28567c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
28577c478bd9Sstevel@tonic-gate 	} else {
28587c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
28597c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
28607c478bd9Sstevel@tonic-gate 	}
286135b1ab99Sjosephb 
286235b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
286335b1ab99Sjosephb 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
286435b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
28657c478bd9Sstevel@tonic-gate 		eprom_kernelbase = NULL;
28667c478bd9Sstevel@tonic-gate 	else
28677c478bd9Sstevel@tonic-gate 		eprom_kernelbase = (uintptr_t)lvalue;
28687c478bd9Sstevel@tonic-gate 
286935b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
287035b1ab99Sjosephb 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
287135b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
28727c478bd9Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
287335b1ab99Sjosephb 	else
28747c478bd9Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
28757c478bd9Sstevel@tonic-gate 
287635b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
287735b1ab99Sjosephb 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
287835b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
28797c478bd9Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
288035b1ab99Sjosephb 	else
28817c478bd9Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
28824944b02eSkchow 
2883aac11643Sjosephb 	/* physmem used to be here, but moved much earlier to fakebop.c */
28847c478bd9Sstevel@tonic-gate }
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate /*
28877c478bd9Sstevel@tonic-gate  * Add to a memory list.
28887c478bd9Sstevel@tonic-gate  * start = start of new memory segment
28897c478bd9Sstevel@tonic-gate  * len = length of new memory segment in bytes
28907c478bd9Sstevel@tonic-gate  * new = pointer to a new struct memlist
28917c478bd9Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
28927c478bd9Sstevel@tonic-gate  */
2893ae115bc7Smrj void
28947c478bd9Sstevel@tonic-gate memlist_add(
28957c478bd9Sstevel@tonic-gate 	uint64_t start,
28967c478bd9Sstevel@tonic-gate 	uint64_t len,
28977c478bd9Sstevel@tonic-gate 	struct memlist *new,
28987c478bd9Sstevel@tonic-gate 	struct memlist **memlistp)
28997c478bd9Sstevel@tonic-gate {
29007c478bd9Sstevel@tonic-gate 	struct memlist *cur;
29017c478bd9Sstevel@tonic-gate 	uint64_t end = start + len;
29027c478bd9Sstevel@tonic-gate 
290356f33205SJonathan Adams 	new->ml_address = start;
290456f33205SJonathan Adams 	new->ml_size = len;
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	cur = *memlistp;
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	while (cur) {
290956f33205SJonathan Adams 		if (cur->ml_address >= end) {
291056f33205SJonathan Adams 			new->ml_next = cur;
29117c478bd9Sstevel@tonic-gate 			*memlistp = new;
291256f33205SJonathan Adams 			new->ml_prev = cur->ml_prev;
291356f33205SJonathan Adams 			cur->ml_prev = new;
29147c478bd9Sstevel@tonic-gate 			return;
29157c478bd9Sstevel@tonic-gate 		}
291656f33205SJonathan Adams 		ASSERT(cur->ml_address + cur->ml_size <= start);
291756f33205SJonathan Adams 		if (cur->ml_next == NULL) {
291856f33205SJonathan Adams 			cur->ml_next = new;
291956f33205SJonathan Adams 			new->ml_prev = cur;
292056f33205SJonathan Adams 			new->ml_next = NULL;
29217c478bd9Sstevel@tonic-gate 			return;
29227c478bd9Sstevel@tonic-gate 		}
292356f33205SJonathan Adams 		memlistp = &cur->ml_next;
292456f33205SJonathan Adams 		cur = cur->ml_next;
29257c478bd9Sstevel@tonic-gate 	}
29267c478bd9Sstevel@tonic-gate }
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate void
29297c478bd9Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
29307c478bd9Sstevel@tonic-gate {
29317c478bd9Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
29327c478bd9Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
29357c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
29367c478bd9Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
29377c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
29387c478bd9Sstevel@tonic-gate }
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate caddr_t
29417c478bd9Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
29427c478bd9Sstevel@tonic-gate {
29437c478bd9Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
29447c478bd9Sstevel@tonic-gate }
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29477c478bd9Sstevel@tonic-gate caddr_t
29487c478bd9Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
29497c478bd9Sstevel@tonic-gate {
29507c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
29517c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
29527c478bd9Sstevel@tonic-gate 	return (0);
29537c478bd9Sstevel@tonic-gate }
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29567c478bd9Sstevel@tonic-gate void
29577c478bd9Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
29587c478bd9Sstevel@tonic-gate {
29597c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
29607c478bd9Sstevel@tonic-gate }
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate /*
29637c478bd9Sstevel@tonic-gate  * This is called just after configure() in startup().
29647c478bd9Sstevel@tonic-gate  *
29657c478bd9Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
29667c478bd9Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
29677c478bd9Sstevel@tonic-gate  * given that various parts of the instruction set may appear
29687c478bd9Sstevel@tonic-gate  * and disappear between different implementations.
29697c478bd9Sstevel@tonic-gate  *
29707c478bd9Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
29717c478bd9Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
29727c478bd9Sstevel@tonic-gate  * more flexibility.
29737c478bd9Sstevel@tonic-gate  *
29747c478bd9Sstevel@tonic-gate  * So, we just leave this alone.
29757c478bd9Sstevel@tonic-gate  */
29767c478bd9Sstevel@tonic-gate void
29777c478bd9Sstevel@tonic-gate setx86isalist(void)
29787c478bd9Sstevel@tonic-gate {
29797c478bd9Sstevel@tonic-gate 	char *tp;
29807c478bd9Sstevel@tonic-gate 	size_t len;
29817c478bd9Sstevel@tonic-gate 	extern char *isa_list;
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate #define	TBUFSIZE	1024
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
29867c478bd9Sstevel@tonic-gate 	*tp = '\0';
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate #if defined(__amd64)
29897c478bd9Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
29907c478bd9Sstevel@tonic-gate #endif
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 	switch (x86_vendor) {
29937c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
29947c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
29957c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
29967c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
29977c478bd9Sstevel@tonic-gate 			/*
29987c478bd9Sstevel@tonic-gate 			 * Pentium Pro or later
29997c478bd9Sstevel@tonic-gate 			 */
30007c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
30017c478bd9Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
30027c478bd9Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
30037c478bd9Sstevel@tonic-gate 		}
30047c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
30057c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
30067c478bd9Sstevel@tonic-gate 		/*
30077c478bd9Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
30087c478bd9Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
30097c478bd9Sstevel@tonic-gate 		 */
30107c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
30117c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
30127c478bd9Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
30137c478bd9Sstevel@tonic-gate 			    "+mmx pentium " : " ");
30147c478bd9Sstevel@tonic-gate 		}
30157c478bd9Sstevel@tonic-gate 		break;
30167c478bd9Sstevel@tonic-gate 	default:
30177c478bd9Sstevel@tonic-gate 		break;
30187c478bd9Sstevel@tonic-gate 	}
30197c478bd9Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
30207c478bd9Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
30217c478bd9Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
30227c478bd9Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate #undef TBUFSIZE
30257c478bd9Sstevel@tonic-gate }
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate #ifdef __amd64
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate void *
30317c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
30327c478bd9Sstevel@tonic-gate {
30337c478bd9Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
30347c478bd9Sstevel@tonic-gate }
30357c478bd9Sstevel@tonic-gate 
30367c478bd9Sstevel@tonic-gate void
30377c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
30387c478bd9Sstevel@tonic-gate {
30397c478bd9Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
30407c478bd9Sstevel@tonic-gate }
30417c478bd9Sstevel@tonic-gate 
3042ae115bc7Smrj #else /* __i386 */
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate void *
30457c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
30467c478bd9Sstevel@tonic-gate {
30477c478bd9Sstevel@tonic-gate 	caddr_t	vaddr;
30487c478bd9Sstevel@tonic-gate 	uintptr_t v;
30497c478bd9Sstevel@tonic-gate 	size_t	start;
30507c478bd9Sstevel@tonic-gate 	size_t	end;
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
30537c478bd9Sstevel@tonic-gate 	if (vaddr == NULL)
30547c478bd9Sstevel@tonic-gate 		return (NULL);
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
30577c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
3058ae115bc7Smrj 	ASSERT(v + size <= valloc_base);
30597c478bd9Sstevel@tonic-gate 
30607c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
30617c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
30627c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
30637c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	while (start <= end) {
30667c478bd9Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
30677c478bd9Sstevel@tonic-gate 		++start;
30687c478bd9Sstevel@tonic-gate 	}
30697c478bd9Sstevel@tonic-gate 	return (vaddr);
30707c478bd9Sstevel@tonic-gate }
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate void
30737c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
30747c478bd9Sstevel@tonic-gate {
30757c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
30767c478bd9Sstevel@tonic-gate 	size_t	start;
30777c478bd9Sstevel@tonic-gate 	size_t	end;
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
3080ae115bc7Smrj 	ASSERT(v + size <= valloc_base);
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
30837c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
30847c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
30857c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	while (start <= end) {
30887c478bd9Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
30897c478bd9Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
30907c478bd9Sstevel@tonic-gate 		++start;
30917c478bd9Sstevel@tonic-gate 	}
30927c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
30937c478bd9Sstevel@tonic-gate }
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate /*
30967c478bd9Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
30977c478bd9Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
30987c478bd9Sstevel@tonic-gate  */
30997c478bd9Sstevel@tonic-gate void *
31007c478bd9Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
31017c478bd9Sstevel@tonic-gate {
31027c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
31037c478bd9Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
31047c478bd9Sstevel@tonic-gate 	size_t start;
31057c478bd9Sstevel@tonic-gate 	size_t end;
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 	/*
31087c478bd9Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
31097c478bd9Sstevel@tonic-gate 	 */
31107c478bd9Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
31117c478bd9Sstevel@tonic-gate 		return (NULL);
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	/*
31147c478bd9Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
31157c478bd9Sstevel@tonic-gate 	 */
3116ae115bc7Smrj 	if (v >= valloc_base || eaddr < kernelbase)
31177c478bd9Sstevel@tonic-gate 		return (NULL);
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 	/*
31207c478bd9Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
31217c478bd9Sstevel@tonic-gate 	 */
31227c478bd9Sstevel@tonic-gate 	if (v < kernelbase)
31237c478bd9Sstevel@tonic-gate 		v = kernelbase;
31247c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
31257c478bd9Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
31267c478bd9Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
31277c478bd9Sstevel@tonic-gate 		end = toxic_bit_map_len;
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
31307c478bd9Sstevel@tonic-gate 		return (NULL);
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 	v = kernelbase + ptob(start);
31337c478bd9Sstevel@tonic-gate 	if (len != NULL)
31347c478bd9Sstevel@tonic-gate 		*len = ptob(end - start);
31357c478bd9Sstevel@tonic-gate 	return ((void *)v);
31367c478bd9Sstevel@tonic-gate }
31377c478bd9Sstevel@tonic-gate 
3138ae115bc7Smrj #endif	/* __i386 */
3139