xref: /titanic_50/usr/src/uts/i86pc/os/startup.c (revision ab4a9beb2e4d596be0b3288c7d92919e27781b57)
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 /*
22fb2f18f8Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/signal.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/user.h>
357c478bd9Sstevel@tonic-gate #include <sys/mman.h>
367c478bd9Sstevel@tonic-gate #include <sys/vm.h>
377c478bd9Sstevel@tonic-gate #include <sys/conf.h>
387c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
397c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
407c478bd9Sstevel@tonic-gate #include <sys/disp.h>
417c478bd9Sstevel@tonic-gate #include <sys/class.h>
427c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <sys/proc.h>
477c478bd9Sstevel@tonic-gate #include <sys/buf.h>
487c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
49ae115bc7Smrj #include <sys/mem.h>
507c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/cred.h>
557c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
567c478bd9Sstevel@tonic-gate #include <sys/file.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
617c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
627c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
637c478bd9Sstevel@tonic-gate #include <sys/debug.h>
647c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
677c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
687c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
697c478bd9Sstevel@tonic-gate #include <sys/promif.h>
70843e1988Sjohnlev #include <sys/modctl.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
737c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
747c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
757c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
767c478bd9Sstevel@tonic-gate #include <sys/psw.h>
777c478bd9Sstevel@tonic-gate #include <sys/regset.h>
787c478bd9Sstevel@tonic-gate #include <sys/clock.h>
797c478bd9Sstevel@tonic-gate #include <sys/pte.h>
807c478bd9Sstevel@tonic-gate #include <sys/tss.h>
817c478bd9Sstevel@tonic-gate #include <sys/stack.h>
827c478bd9Sstevel@tonic-gate #include <sys/trap.h>
837c478bd9Sstevel@tonic-gate #include <sys/fp.h>
841d03c31eSjohnlev #include <vm/kboot_mmu.h>
857c478bd9Sstevel@tonic-gate #include <vm/anon.h>
867c478bd9Sstevel@tonic-gate #include <vm/as.h>
877c478bd9Sstevel@tonic-gate #include <vm/page.h>
887c478bd9Sstevel@tonic-gate #include <vm/seg.h>
897c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
907c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
917c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
927c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
937c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
947c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
957c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
967c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
977c478bd9Sstevel@tonic-gate #include <sys/thread.h>
987c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
997c478bd9Sstevel@tonic-gate #include <sys/vm_machparam.h>
1007c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
1017c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
1027c478bd9Sstevel@tonic-gate #include <vm/hat.h>
1037c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
1047c478bd9Sstevel@tonic-gate #include <sys/pmem.h>
1057c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1067c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
1077c478bd9Sstevel@tonic-gate #include <sys/segments.h>
1087c478bd9Sstevel@tonic-gate #include <sys/clconf.h>
1097c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
1107c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
1117c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
1127c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
1137aec1d6eScindi #include <sys/cpu_module.h>
11484ab085aSmws #include <sys/smbios.h>
115ae115bc7Smrj #include <sys/debug_info.h>
1161d03c31eSjohnlev #include <sys/bootinfo.h>
117dd4eeefdSeota #include <sys/ddi_timer.h>
118ae115bc7Smrj 
119843e1988Sjohnlev #ifdef __xpv
1201d03c31eSjohnlev 
121843e1988Sjohnlev #include <sys/hypervisor.h>
122843e1988Sjohnlev #include <sys/xen_mmu.h>
123843e1988Sjohnlev #include <sys/evtchn_impl.h>
124843e1988Sjohnlev #include <sys/gnttab.h>
125843e1988Sjohnlev #include <sys/xpv_panic.h>
126843e1988Sjohnlev #include <xen/sys/xenbus_comms.h>
127843e1988Sjohnlev #include <xen/public/physdev.h>
128ae115bc7Smrj 
1291d03c31eSjohnlev extern void xen_late_startup(void);
1301d03c31eSjohnlev 
1311d03c31eSjohnlev struct xen_evt_data cpu0_evt_data;
1321d03c31eSjohnlev 
1331d03c31eSjohnlev #endif /* __xpv */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate extern void progressbar_init(void);
1367c478bd9Sstevel@tonic-gate extern void progressbar_start(void);
1379acbbeafSnn35248 extern void brand_init(void);
1387c478bd9Sstevel@tonic-gate 
139d7d93655Sblakej extern int size_pse_array(pgcnt_t, int);
140d7d93655Sblakej 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * XXX make declaration below "static" when drivers no longer use this
1437c478bd9Sstevel@tonic-gate  * interface.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * segkp
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate extern int segkp_fromheap;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static void kvm_init(void);
1537c478bd9Sstevel@tonic-gate static void startup_init(void);
1547c478bd9Sstevel@tonic-gate static void startup_memlist(void);
155ae115bc7Smrj static void startup_kmem(void);
1567c478bd9Sstevel@tonic-gate static void startup_modules(void);
1577c478bd9Sstevel@tonic-gate static void startup_vm(void);
1587c478bd9Sstevel@tonic-gate static void startup_end(void);
15935b1ab99Sjosephb static void layout_kernel_va(void);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1637c478bd9Sstevel@tonic-gate  */
164f53ad214Skchow #ifdef __i386
16535b1ab99Sjosephb 
166f53ad214Skchow /*
167f53ad214Skchow  * Due to virtual address space limitations running in 32 bit mode, restrict
16835b1ab99Sjosephb  * the amount of physical memory configured to a max of PHYSMEM pages (16g).
169f53ad214Skchow  *
170f53ad214Skchow  * If the physical max memory size of 64g were allowed to be configured, the
171f53ad214Skchow  * size of user virtual address space will be less than 1g. A limited user
172f53ad214Skchow  * address space greatly reduces the range of applications that can run.
173f53ad214Skchow  *
17435b1ab99Sjosephb  * If more physical memory than PHYSMEM is required, users should preferably
17535b1ab99Sjosephb  * run in 64 bit mode which has far looser virtual address space limitations.
176f53ad214Skchow  *
177f53ad214Skchow  * If 64 bit mode is not available (as in IA32) and/or more physical memory
17835b1ab99Sjosephb  * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
179f53ad214Skchow  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
180f53ad214Skchow  * should also be carefully tuned to balance out the need of the user
181f53ad214Skchow  * application while minimizing the risk of kernel heap exhaustion due to
182f53ad214Skchow  * kernelbase being set too high.
183f53ad214Skchow  */
18435b1ab99Sjosephb #define	PHYSMEM	0x400000
185f53ad214Skchow 
18635b1ab99Sjosephb #else /* __amd64 */
18735b1ab99Sjosephb 
18835b1ab99Sjosephb /*
18935b1ab99Sjosephb  * For now we can handle memory with physical addresses up to about
19035b1ab99Sjosephb  * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
19135b1ab99Sjosephb  * half the VA space for seg_kpm. When systems get bigger than 64TB this
19235b1ab99Sjosephb  * code will need revisiting. There is an implicit assumption that there
19335b1ab99Sjosephb  * are no *huge* holes in the physical address space too.
19435b1ab99Sjosephb  */
19535b1ab99Sjosephb #define	TERABYTE		(1ul << 40)
19635b1ab99Sjosephb #define	PHYSMEM_MAX64		mmu_btop(64 * TERABYTE)
19735b1ab99Sjosephb #define	PHYSMEM			PHYSMEM_MAX64
19835b1ab99Sjosephb #define	AMD64_VA_HOLE_END	0xFFFF800000000000ul
19935b1ab99Sjosephb 
20035b1ab99Sjosephb #endif /* __amd64 */
20135b1ab99Sjosephb 
20235b1ab99Sjosephb pgcnt_t physmem = PHYSMEM;
2037c478bd9Sstevel@tonic-gate pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate char *kobj_file_buf;
2067c478bd9Sstevel@tonic-gate int kobj_file_bufsize;	/* set in /etc/system */
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /* Global variables for MP support. Used in mp_startup */
2097c478bd9Sstevel@tonic-gate caddr_t	rm_platter_va;
2107c478bd9Sstevel@tonic-gate uint32_t rm_platter_pa;
2117c478bd9Sstevel@tonic-gate 
21283f9b804Skchow int	auto_lpg_disable = 1;
21383f9b804Skchow 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * Some CPUs have holes in the middle of the 64-bit virtual address range.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate uintptr_t hole_start, hole_end;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * kpm mapping window
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate caddr_t kpm_vbase;
2237c478bd9Sstevel@tonic-gate size_t  kpm_size;
22435b1ab99Sjosephb static int kpm_desired;
22535b1ab99Sjosephb #ifdef __amd64
22635b1ab99Sjosephb static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
22735b1ab99Sjosephb #endif
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Configuration parameters set at boot time.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate caddr_t econtig;		/* end of first block of contiguous kernel */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate struct bootops		*bootops = 0;	/* passed in from boot */
2367c478bd9Sstevel@tonic-gate struct bootops		**bootopsp;
2377c478bd9Sstevel@tonic-gate struct boot_syscalls	*sysp;		/* passed in from boot */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate char bootblock_fstype[16];
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
244ad23a2dbSjohansen  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
245ad23a2dbSjohansen  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
246ad23a2dbSjohansen  * zio buffers from their own segment, otherwise they are allocated from the
247ad23a2dbSjohansen  * heap.  The optimization of allocating zio buffers from their own segment is
248ad23a2dbSjohansen  * only valid on 64-bit kernels.
249ad23a2dbSjohansen  */
250ad23a2dbSjohansen #if defined(__amd64)
251ad23a2dbSjohansen int segzio_fromheap = 0;
252ad23a2dbSjohansen #else
253ad23a2dbSjohansen int segzio_fromheap = 1;
254ad23a2dbSjohansen #endif
255ad23a2dbSjohansen 
256ad23a2dbSjohansen /*
2577c478bd9Sstevel@tonic-gate  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
2587c478bd9Sstevel@tonic-gate  * depends on number of BOP_ALLOC calls made and requested size, memory size
2597c478bd9Sstevel@tonic-gate  * combination and whether boot.bin memory needs to be freed.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate #define	POSS_NEW_FRAGMENTS	12
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * VM data structures
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
2677c478bd9Sstevel@tonic-gate struct page *pp_base;		/* Base of initial system page struct array */
2687c478bd9Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
269d7d93655Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
270d7d93655Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
271d7d93655Sblakej int pse_shift;			/* log2(pse_table_size) */
2727c478bd9Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
2737c478bd9Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
2747c478bd9Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
2757c478bd9Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
2767c478bd9Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
279ae115bc7Smrj static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
280ae115bc7Smrj 
2817c478bd9Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate #if defined(__amd64)
2847c478bd9Sstevel@tonic-gate struct seg kvseg_core;		/* Segment used for the core heap */
2857c478bd9Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
2867c478bd9Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
2877c478bd9Sstevel@tonic-gate #else
2887c478bd9Sstevel@tonic-gate struct seg *segkpm = NULL;	/* Unused on IA32 */
2897c478bd9Sstevel@tonic-gate #endif
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate caddr_t segkp_base;		/* Base address of segkp */
292ad23a2dbSjohansen caddr_t segzio_base;		/* Base address of segzio */
2937c478bd9Sstevel@tonic-gate #if defined(__amd64)
2947c478bd9Sstevel@tonic-gate pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
2957c478bd9Sstevel@tonic-gate #else
2967c478bd9Sstevel@tonic-gate pgcnt_t segkpsize = 0;
2977c478bd9Sstevel@tonic-gate #endif
298ad23a2dbSjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
2997c478bd9Sstevel@tonic-gate 
300ae115bc7Smrj /*
301ae115bc7Smrj  * VA range available to the debugger
302ae115bc7Smrj  */
303ae115bc7Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
304ae115bc7Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
305ae115bc7Smrj 
3067c478bd9Sstevel@tonic-gate struct memseg *memseg_base;
3077c478bd9Sstevel@tonic-gate struct vnode unused_pages_vp;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate #define	FOURGB	0x100000000LL
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate struct memlist *memlist;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate caddr_t s_text;		/* start of kernel text segment */
3147c478bd9Sstevel@tonic-gate caddr_t e_text;		/* end of kernel text segment */
3157c478bd9Sstevel@tonic-gate caddr_t s_data;		/* start of kernel data segment */
3167c478bd9Sstevel@tonic-gate caddr_t e_data;		/* end of kernel data segment */
3177c478bd9Sstevel@tonic-gate caddr_t modtext;	/* start of loadable module text reserved */
3187c478bd9Sstevel@tonic-gate caddr_t e_modtext;	/* end of loadable module text reserved */
3197c478bd9Sstevel@tonic-gate caddr_t moddata;	/* start of loadable module data reserved */
3207c478bd9Sstevel@tonic-gate caddr_t e_moddata;	/* end of loadable module data reserved */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
3237c478bd9Sstevel@tonic-gate struct memlist *phys_avail;	/* Total available physical memory */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * kphysm_init returns the number of pages that were processed
3277c478bd9Sstevel@tonic-gate  */
328ae115bc7Smrj static pgcnt_t kphysm_init(page_t *, pgcnt_t);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate #define	IO_PROP_SIZE	64	/* device property size */
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * a couple useful roundup macros
3347c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate #define	ROUND_UP_PAGE(x)	\
3367c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
3377c478bd9Sstevel@tonic-gate #define	ROUND_UP_LPAGE(x)	\
3387c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
3397c478bd9Sstevel@tonic-gate #define	ROUND_UP_4MEG(x)	\
340ae115bc7Smrj 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
3417c478bd9Sstevel@tonic-gate #define	ROUND_UP_TOPLEVEL(x)	\
3427c478bd9Sstevel@tonic-gate 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  *	32-bit Kernel's Virtual memory layout.
3467c478bd9Sstevel@tonic-gate  *		+-----------------------+
347ae115bc7Smrj  *		|			|
3487c478bd9Sstevel@tonic-gate  * 0xFFC00000  -|-----------------------|- ARGSBASE
3497c478bd9Sstevel@tonic-gate  *		|	debugger	|
3507c478bd9Sstevel@tonic-gate  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
3517c478bd9Sstevel@tonic-gate  *		|      Kernel Data	|
3527c478bd9Sstevel@tonic-gate  * 0xFEC00000  -|-----------------------|
3537c478bd9Sstevel@tonic-gate  *              |      Kernel Text	|
354843e1988Sjohnlev  * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
355ae115bc7Smrj  *		|---       GDT       ---|- GDT page (GDT_VA)
356ae115bc7Smrj  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
357ae115bc7Smrj  *		|			|
358ae115bc7Smrj  * 		|   page_t structures	|
3597c478bd9Sstevel@tonic-gate  * 		|   memsegs, memlists, 	|
3607c478bd9Sstevel@tonic-gate  * 		|   page hash, etc.	|
361ae115bc7Smrj  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
362ae115bc7Smrj  *		|			|  (segkp is just an arena in the heap)
3637c478bd9Sstevel@tonic-gate  *		|			|
3647c478bd9Sstevel@tonic-gate  *		|	kvseg		|
3657c478bd9Sstevel@tonic-gate  *		|			|
3667c478bd9Sstevel@tonic-gate  *		|			|
3677c478bd9Sstevel@tonic-gate  * ---         -|-----------------------|- kernelheap (floating)
3687c478bd9Sstevel@tonic-gate  * 		|        Segkmap	|
369ae115bc7Smrj  * 0xC3002000  -|-----------------------|- segmap_start (floating)
3707c478bd9Sstevel@tonic-gate  *		|	Red Zone	|
3717c478bd9Sstevel@tonic-gate  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
3727c478bd9Sstevel@tonic-gate  *		|			|			||
3737c478bd9Sstevel@tonic-gate  *		|     Shared objects	|			\/
3747c478bd9Sstevel@tonic-gate  *		|			|
3757c478bd9Sstevel@tonic-gate  *		:			:
3767c478bd9Sstevel@tonic-gate  *		|	user data	|
3777c478bd9Sstevel@tonic-gate  *		|-----------------------|
3787c478bd9Sstevel@tonic-gate  *		|	user text	|
3797c478bd9Sstevel@tonic-gate  * 0x08048000  -|-----------------------|
3807c478bd9Sstevel@tonic-gate  *		|	user stack	|
3817c478bd9Sstevel@tonic-gate  *		:			:
3827c478bd9Sstevel@tonic-gate  *		|	invalid		|
3837c478bd9Sstevel@tonic-gate  * 0x00000000	+-----------------------+
3847c478bd9Sstevel@tonic-gate  *
3857c478bd9Sstevel@tonic-gate  *
3867c478bd9Sstevel@tonic-gate  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
3877c478bd9Sstevel@tonic-gate  *			+-----------------------+
388ae115bc7Smrj  *			|			|
3897c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
3907c478bd9Sstevel@tonic-gate  *			|	debugger (?)	|
3917c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
3927c478bd9Sstevel@tonic-gate  *			|      unused    	|
3937c478bd9Sstevel@tonic-gate  *			+-----------------------+
3947c478bd9Sstevel@tonic-gate  *			|      Kernel Data	|
3957c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FBC00000  |-----------------------|
3967c478bd9Sstevel@tonic-gate  *			|      Kernel Text	|
3977c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
398ae115bc7Smrj  *			|---       GDT       ---|- GDT page (GDT_VA)
399ae115bc7Smrj  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
400ae115bc7Smrj  *			|			|
4017c478bd9Sstevel@tonic-gate  * 			|      Core heap	| (used for loadable modules)
4027c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
4037c478bd9Sstevel@tonic-gate  *			|	 Kernel		|
4047c478bd9Sstevel@tonic-gate  *			|	  heap		|
4057c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
406ae115bc7Smrj  *			|	 segmap		|
407ae115bc7Smrj  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
4087c478bd9Sstevel@tonic-gate  *			|    device mappings	|
4097c478bd9Sstevel@tonic-gate  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
410ad23a2dbSjohansen  *			|	  segzio	|
411ad23a2dbSjohansen  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
4127c478bd9Sstevel@tonic-gate  *			|	  segkp		|
413ae115bc7Smrj  * ---                  |-----------------------|- segkp_base (floating)
414ae115bc7Smrj  * 			|   page_t structures	|  valloc_base + valloc_sz
415ae115bc7Smrj  * 			|   memsegs, memlists, 	|
416ae115bc7Smrj  * 			|   page hash, etc.	|
41735b1ab99Sjosephb  * 0xFFFFFF00.00000000  |-----------------------|- valloc_base (lower if > 1TB)
4187c478bd9Sstevel@tonic-gate  *			|	 segkpm		|
4197c478bd9Sstevel@tonic-gate  * 0xFFFFFE00.00000000  |-----------------------|
4207c478bd9Sstevel@tonic-gate  *			|	Red Zone	|
42135b1ab99Sjosephb  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE (lower if > 1TB)
4227c478bd9Sstevel@tonic-gate  *			|     User stack	|- User space memory
4237c478bd9Sstevel@tonic-gate  * 			|			|
4247c478bd9Sstevel@tonic-gate  * 			| shared objects, etc	|	(grows downwards)
4257c478bd9Sstevel@tonic-gate  *			:			:
4267c478bd9Sstevel@tonic-gate  * 			|			|
4277c478bd9Sstevel@tonic-gate  * 0xFFFF8000.00000000  |-----------------------|
4287c478bd9Sstevel@tonic-gate  * 			|			|
4297c478bd9Sstevel@tonic-gate  * 			| VA Hole / unused	|
4307c478bd9Sstevel@tonic-gate  * 			|			|
4317c478bd9Sstevel@tonic-gate  * 0x00008000.00000000  |-----------------------|
4327c478bd9Sstevel@tonic-gate  *			|			|
4337c478bd9Sstevel@tonic-gate  *			|			|
4347c478bd9Sstevel@tonic-gate  *			:			:
4357c478bd9Sstevel@tonic-gate  *			|	user heap	|	(grows upwards)
4367c478bd9Sstevel@tonic-gate  *			|			|
4377c478bd9Sstevel@tonic-gate  *			|	user data	|
4387c478bd9Sstevel@tonic-gate  *			|-----------------------|
4397c478bd9Sstevel@tonic-gate  *			|	user text	|
4407c478bd9Sstevel@tonic-gate  * 0x00000000.04000000  |-----------------------|
4417c478bd9Sstevel@tonic-gate  *			|	invalid		|
4427c478bd9Sstevel@tonic-gate  * 0x00000000.00000000	+-----------------------+
4437c478bd9Sstevel@tonic-gate  *
4447c478bd9Sstevel@tonic-gate  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
4457c478bd9Sstevel@tonic-gate  * kernel, except that userlimit is raised to 0xfe000000
4467c478bd9Sstevel@tonic-gate  *
4477c478bd9Sstevel@tonic-gate  * Floating values:
4487c478bd9Sstevel@tonic-gate  *
4497c478bd9Sstevel@tonic-gate  * valloc_base: start of the kernel's memory management/tracking data
450ae115bc7Smrj  * structures.  This region contains page_t structures for
451ae115bc7Smrj  * physical memory, memsegs, memlists, and the page hash.
4527c478bd9Sstevel@tonic-gate  *
4537c478bd9Sstevel@tonic-gate  * core_base: start of the kernel's "core" heap area on 64-bit systems.
4547c478bd9Sstevel@tonic-gate  * This area is intended to be used for global data as well as for module
4557c478bd9Sstevel@tonic-gate  * text/data that does not fit into the nucleus pages.  The core heap is
4567c478bd9Sstevel@tonic-gate  * restricted to a 2GB range, allowing every address within it to be
4577c478bd9Sstevel@tonic-gate  * accessed using rip-relative addressing
4587c478bd9Sstevel@tonic-gate  *
4597c478bd9Sstevel@tonic-gate  * ekernelheap: end of kernelheap and start of segmap.
4607c478bd9Sstevel@tonic-gate  *
4617c478bd9Sstevel@tonic-gate  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
4627c478bd9Sstevel@tonic-gate  * above a red zone that separates the user's address space from the
4637c478bd9Sstevel@tonic-gate  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
4647c478bd9Sstevel@tonic-gate  *
465ae115bc7Smrj  * segmap_start: start of segmap. The length of segmap can be modified
4667c478bd9Sstevel@tonic-gate  * by changing segmapsize in /etc/system (preferred) or eeprom (deprecated).
4677c478bd9Sstevel@tonic-gate  * The default length is 16MB on 32-bit systems and 64MB on 64-bit systems.
4687c478bd9Sstevel@tonic-gate  *
4697c478bd9Sstevel@tonic-gate  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
4707c478bd9Sstevel@tonic-gate  * decreased by 2X the size required for page_t.  This allows the kernel
4717c478bd9Sstevel@tonic-gate  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
4727c478bd9Sstevel@tonic-gate  * bytes, the following shows the values of kernelbase and kernel heap
4737c478bd9Sstevel@tonic-gate  * sizes for different memory configurations (assuming default segmap and
4747c478bd9Sstevel@tonic-gate  * segkp sizes).
4757c478bd9Sstevel@tonic-gate  *
4767c478bd9Sstevel@tonic-gate  *	mem	size for	kernelbase	kernel heap
4777c478bd9Sstevel@tonic-gate  *	size	page_t's			size
4787c478bd9Sstevel@tonic-gate  *	----	---------	----------	-----------
4797c478bd9Sstevel@tonic-gate  *	1gb	0x01400000	0xd1800000	684MB
4807c478bd9Sstevel@tonic-gate  *	2gb	0x02800000	0xcf000000	704MB
4817c478bd9Sstevel@tonic-gate  *	4gb	0x05000000	0xca000000	744MB
4827c478bd9Sstevel@tonic-gate  *	6gb	0x07800000	0xc5000000	784MB
4837c478bd9Sstevel@tonic-gate  *	8gb	0x0a000000	0xc0000000	824MB
4847c478bd9Sstevel@tonic-gate  *	16gb	0x14000000	0xac000000	984MB
4857c478bd9Sstevel@tonic-gate  *	32gb	0x28000000	0x84000000	1304MB
4867c478bd9Sstevel@tonic-gate  *	64gb	0x50000000	0x34000000	1944MB (*)
4877c478bd9Sstevel@tonic-gate  *
4887c478bd9Sstevel@tonic-gate  * kernelbase is less than the abi minimum of 0xc0000000 for memory
4897c478bd9Sstevel@tonic-gate  * configurations above 8gb.
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  * (*) support for memory configurations above 32gb will require manual tuning
4927c478bd9Sstevel@tonic-gate  * of kernelbase to balance out the need of user applications.
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /* real-time-clock initialization parameters */
496ae115bc7Smrj extern time_t process_rtc_config_file(void);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate uintptr_t	kernelbase;
499ae115bc7Smrj uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
5007c478bd9Sstevel@tonic-gate uintptr_t	eprom_kernelbase;
5017c478bd9Sstevel@tonic-gate size_t		segmapsize;
502ae115bc7Smrj uintptr_t	segmap_start;
5037c478bd9Sstevel@tonic-gate int		segmapfreelists;
5047c478bd9Sstevel@tonic-gate pgcnt_t		npages;
505ae115bc7Smrj pgcnt_t		orig_npages;
5067c478bd9Sstevel@tonic-gate size_t		core_size;		/* size of "core" heap */
5077c478bd9Sstevel@tonic-gate uintptr_t	core_base;		/* base address of "core" heap */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  * List of bootstrap pages. We mark these as allocated in startup.
5117c478bd9Sstevel@tonic-gate  * release_bootstrap() will free them when we're completely done with
5127c478bd9Sstevel@tonic-gate  * the bootstrap.
5137c478bd9Sstevel@tonic-gate  */
514ae115bc7Smrj static page_t *bootpages;
515ae115bc7Smrj 
516ae115bc7Smrj /*
517ae115bc7Smrj  * boot time pages that have a vnode from the ramdisk will keep that forever.
518ae115bc7Smrj  */
519ae115bc7Smrj static page_t *rd_pages;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate struct system_hardware system_hardware;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
5257c478bd9Sstevel@tonic-gate  */
5267c478bd9Sstevel@tonic-gate static void
527ae115bc7Smrj print_memlist(char *title, struct memlist *mp)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	prom_printf("MEMLIST: %s:\n", title);
5307c478bd9Sstevel@tonic-gate 	while (mp != NULL)  {
5317c478bd9Sstevel@tonic-gate 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
5327c478bd9Sstevel@tonic-gate 		    mp->address, mp->size);
5337c478bd9Sstevel@tonic-gate 		mp = mp->next;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate /*
5387c478bd9Sstevel@tonic-gate  * XX64 need a comment here.. are these just default values, surely
5397c478bd9Sstevel@tonic-gate  * we read the "cpuid" type information to figure this out.
5407c478bd9Sstevel@tonic-gate  */
5417c478bd9Sstevel@tonic-gate int	l2cache_sz = 0x80000;
5427c478bd9Sstevel@tonic-gate int	l2cache_linesz = 0x40;
5437c478bd9Sstevel@tonic-gate int	l2cache_assoc = 1;
5447c478bd9Sstevel@tonic-gate 
5452cb27123Saguzovsk static size_t	textrepl_min_gb = 10;
5462cb27123Saguzovsk 
5477c478bd9Sstevel@tonic-gate /*
5487c478bd9Sstevel@tonic-gate  * on 64 bit we use a predifined VA range for mapping devices in the kernel
5497c478bd9Sstevel@tonic-gate  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
5507c478bd9Sstevel@tonic-gate  */
5517c478bd9Sstevel@tonic-gate #ifdef __amd64
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate vmem_t		*device_arena;
5547c478bd9Sstevel@tonic-gate uintptr_t	toxic_addr = (uintptr_t)NULL;
555ae115bc7Smrj size_t		toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate #else	/* __i386 */
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
5607c478bd9Sstevel@tonic-gate size_t		toxic_bit_map_len = 0;	/* in bits */
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate #endif	/* __i386 */
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Simple boot time debug facilities
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate static char *prm_dbg_str[] = {
5687c478bd9Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%x\n",
5697c478bd9Sstevel@tonic-gate 	"%s:%d: '%s' is 0x%llx\n"
5707c478bd9Sstevel@tonic-gate };
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate int prom_debug;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate #define	PRM_DEBUG(q)	if (prom_debug) 	\
5757c478bd9Sstevel@tonic-gate 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
5767c478bd9Sstevel@tonic-gate #define	PRM_POINT(q)	if (prom_debug) 	\
5777c478bd9Sstevel@tonic-gate 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * This structure is used to keep track of the intial allocations
5817c478bd9Sstevel@tonic-gate  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
5827c478bd9Sstevel@tonic-gate  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate #define	NUM_ALLOCATIONS 7
5857c478bd9Sstevel@tonic-gate int num_allocations = 0;
5867c478bd9Sstevel@tonic-gate struct {
5877c478bd9Sstevel@tonic-gate 	void **al_ptr;
5887c478bd9Sstevel@tonic-gate 	size_t al_size;
5897c478bd9Sstevel@tonic-gate } allocations[NUM_ALLOCATIONS];
5907c478bd9Sstevel@tonic-gate size_t valloc_sz = 0;
5917c478bd9Sstevel@tonic-gate uintptr_t valloc_base;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
5947c478bd9Sstevel@tonic-gate 		size = ROUND_UP_PAGE(size);		 		\
5957c478bd9Sstevel@tonic-gate 		if (num_allocations == NUM_ALLOCATIONS)			\
5967c478bd9Sstevel@tonic-gate 			panic("too many ADD_TO_ALLOCATIONS()");		\
5977c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
5987c478bd9Sstevel@tonic-gate 		allocations[num_allocations].al_size = size;		\
5997c478bd9Sstevel@tonic-gate 		valloc_sz += size;					\
6007c478bd9Sstevel@tonic-gate 		++num_allocations;				 	\
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
603ae115bc7Smrj /*
604ae115bc7Smrj  * Allocate all the initial memory needed by the page allocator.
605ae115bc7Smrj  */
6067c478bd9Sstevel@tonic-gate static void
6077c478bd9Sstevel@tonic-gate perform_allocations(void)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	caddr_t mem;
6107c478bd9Sstevel@tonic-gate 	int i;
611ae115bc7Smrj 	int valloc_align;
6127c478bd9Sstevel@tonic-gate 
613ae115bc7Smrj 	PRM_DEBUG(valloc_base);
614ae115bc7Smrj 	PRM_DEBUG(valloc_sz);
615ae115bc7Smrj 	valloc_align = mmu.level_size[mmu.max_page_level > 0];
616ae115bc7Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
6177c478bd9Sstevel@tonic-gate 	if (mem != (caddr_t)valloc_base)
6187c478bd9Sstevel@tonic-gate 		panic("BOP_ALLOC() failed");
6197c478bd9Sstevel@tonic-gate 	bzero(mem, valloc_sz);
6207c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_allocations; ++i) {
6217c478bd9Sstevel@tonic-gate 		*allocations[i].al_ptr = (void *)mem;
6227c478bd9Sstevel@tonic-gate 		mem += allocations[i].al_size;
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate  * Our world looks like this at startup time.
6287c478bd9Sstevel@tonic-gate  *
6297c478bd9Sstevel@tonic-gate  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
6307c478bd9Sstevel@tonic-gate  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
6317c478bd9Sstevel@tonic-gate  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
6327c478bd9Sstevel@tonic-gate  * addresses are fixed in the binary at link time.
6337c478bd9Sstevel@tonic-gate  *
6347c478bd9Sstevel@tonic-gate  * On the text page:
6357c478bd9Sstevel@tonic-gate  * unix/genunix/krtld/module text loads.
6367c478bd9Sstevel@tonic-gate  *
6377c478bd9Sstevel@tonic-gate  * On the data page:
638ae115bc7Smrj  * unix/genunix/krtld/module data loads.
639ae115bc7Smrj  *
6407c478bd9Sstevel@tonic-gate  * Machine-dependent startup code
6417c478bd9Sstevel@tonic-gate  */
6427c478bd9Sstevel@tonic-gate void
6437c478bd9Sstevel@tonic-gate startup(void)
6447c478bd9Sstevel@tonic-gate {
645843e1988Sjohnlev #if !defined(__xpv)
64675bcd456Sjg 	extern void startup_bios_disk(void);
64775bcd456Sjg 	extern void startup_pci_bios(void);
648843e1988Sjohnlev #endif
649843e1988Sjohnlev 	/*
650843e1988Sjohnlev 	 * Make sure that nobody tries to use sekpm until we have
651843e1988Sjohnlev 	 * initialized it properly.
652843e1988Sjohnlev 	 */
6537c478bd9Sstevel@tonic-gate #if defined(__amd64)
65435b1ab99Sjosephb 	kpm_desired = 1;
6557c478bd9Sstevel@tonic-gate #endif
6567c478bd9Sstevel@tonic-gate 	kpm_enable = 0;
6577c478bd9Sstevel@tonic-gate 
658843e1988Sjohnlev #if defined(__xpv)	/* XXPV fix me! */
659843e1988Sjohnlev 	{
660843e1988Sjohnlev 		extern int segvn_use_regions;
661843e1988Sjohnlev 		segvn_use_regions = 0;
662843e1988Sjohnlev 	}
663843e1988Sjohnlev #endif
6647c478bd9Sstevel@tonic-gate 	progressbar_init();
6657c478bd9Sstevel@tonic-gate 	startup_init();
666*ab4a9bebSjohnlev #if defined(__xpv)
667*ab4a9bebSjohnlev 	startup_xen_version();
668*ab4a9bebSjohnlev #endif
6697c478bd9Sstevel@tonic-gate 	startup_memlist();
670ae115bc7Smrj 	startup_kmem();
67135b1ab99Sjosephb 	startup_vm();
672843e1988Sjohnlev #if !defined(__xpv)
67375bcd456Sjg 	startup_pci_bios();
674843e1988Sjohnlev #endif
6757c478bd9Sstevel@tonic-gate 	startup_modules();
676843e1988Sjohnlev #if !defined(__xpv)
6777c478bd9Sstevel@tonic-gate 	startup_bios_disk();
678843e1988Sjohnlev #endif
6797c478bd9Sstevel@tonic-gate 	startup_end();
6807c478bd9Sstevel@tonic-gate 	progressbar_start();
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate static void
6847c478bd9Sstevel@tonic-gate startup_init()
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() starting...");
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/*
6897c478bd9Sstevel@tonic-gate 	 * Complete the extraction of cpuid data
6907c478bd9Sstevel@tonic-gate 	 */
6917c478bd9Sstevel@tonic-gate 	cpuid_pass2(CPU);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/*
6967c478bd9Sstevel@tonic-gate 	 * Check for prom_debug in boot environment
6977c478bd9Sstevel@tonic-gate 	 */
6987c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
6997c478bd9Sstevel@tonic-gate 		++prom_debug;
7007c478bd9Sstevel@tonic-gate 		PRM_POINT("prom_debug found in boot enviroment");
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * Collect node, cpu and memory configuration information.
7057c478bd9Sstevel@tonic-gate 	 */
7067c478bd9Sstevel@tonic-gate 	get_system_configuration();
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	/*
7097c478bd9Sstevel@tonic-gate 	 * Halt if this is an unsupported processor.
7107c478bd9Sstevel@tonic-gate 	 */
7117c478bd9Sstevel@tonic-gate 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
7127c478bd9Sstevel@tonic-gate 		printf("\n486 processor (\"%s\") detected.\n",
7137c478bd9Sstevel@tonic-gate 		    CPU->cpu_brandstr);
7147c478bd9Sstevel@tonic-gate 		halt("This processor is not supported by this release "
7157c478bd9Sstevel@tonic-gate 		    "of Solaris.");
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_init() done");
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate /*
7227c478bd9Sstevel@tonic-gate  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
7237c478bd9Sstevel@tonic-gate  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
7247c478bd9Sstevel@tonic-gate  * also filters out physical page zero.  There is some reliance on the
7257c478bd9Sstevel@tonic-gate  * boot loader allocating only a few contiguous physical memory chunks.
7267c478bd9Sstevel@tonic-gate  */
7277c478bd9Sstevel@tonic-gate static void
7287c478bd9Sstevel@tonic-gate avail_filter(uint64_t *addr, uint64_t *size)
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	uintptr_t va;
7317c478bd9Sstevel@tonic-gate 	uintptr_t next_va;
7327c478bd9Sstevel@tonic-gate 	pfn_t pfn;
7337c478bd9Sstevel@tonic-gate 	uint64_t pfn_addr;
7347c478bd9Sstevel@tonic-gate 	uint64_t pfn_eaddr;
7357c478bd9Sstevel@tonic-gate 	uint_t prot;
7367c478bd9Sstevel@tonic-gate 	size_t len;
7377c478bd9Sstevel@tonic-gate 	uint_t change;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (prom_debug)
7407c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
7417c478bd9Sstevel@tonic-gate 		    *addr, *size);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	/*
7447c478bd9Sstevel@tonic-gate 	 * page zero is required for BIOS.. never make it available
7457c478bd9Sstevel@tonic-gate 	 */
7467c478bd9Sstevel@tonic-gate 	if (*addr == 0) {
7477c478bd9Sstevel@tonic-gate 		*addr += MMU_PAGESIZE;
7487c478bd9Sstevel@tonic-gate 		*size -= MMU_PAGESIZE;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/*
752ae115bc7Smrj 	 * First we trim from the front of the range. Since kbm_probe()
7537c478bd9Sstevel@tonic-gate 	 * walks ranges in virtual order, but addr/size are physical, we need
7547c478bd9Sstevel@tonic-gate 	 * to the list until no changes are seen.  This deals with the case
7557c478bd9Sstevel@tonic-gate 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
7567c478bd9Sstevel@tonic-gate 	 * but w < v.
7577c478bd9Sstevel@tonic-gate 	 */
7587c478bd9Sstevel@tonic-gate 	do {
7597c478bd9Sstevel@tonic-gate 		change = 0;
7607c478bd9Sstevel@tonic-gate 		for (va = KERNEL_TEXT;
761ae115bc7Smrj 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7627c478bd9Sstevel@tonic-gate 		    va = next_va) {
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 			next_va = va + len;
765ae115bc7Smrj 			pfn_addr = pfn_to_pa(pfn);
7667c478bd9Sstevel@tonic-gate 			pfn_eaddr = pfn_addr + len;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
7697c478bd9Sstevel@tonic-gate 				change = 1;
7707c478bd9Sstevel@tonic-gate 				while (*size > 0 && len > 0) {
7717c478bd9Sstevel@tonic-gate 					*addr += MMU_PAGESIZE;
7727c478bd9Sstevel@tonic-gate 					*size -= MMU_PAGESIZE;
7737c478bd9Sstevel@tonic-gate 					len -= MMU_PAGESIZE;
7747c478bd9Sstevel@tonic-gate 				}
7757c478bd9Sstevel@tonic-gate 			}
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 		if (change && prom_debug)
7787c478bd9Sstevel@tonic-gate 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
7797c478bd9Sstevel@tonic-gate 			    *addr, *size);
7807c478bd9Sstevel@tonic-gate 	} while (change);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	/*
7837c478bd9Sstevel@tonic-gate 	 * Trim pages from the end of the range.
7847c478bd9Sstevel@tonic-gate 	 */
7857c478bd9Sstevel@tonic-gate 	for (va = KERNEL_TEXT;
786ae115bc7Smrj 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
7877c478bd9Sstevel@tonic-gate 	    va = next_va) {
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		next_va = va + len;
790ae115bc7Smrj 		pfn_addr = pfn_to_pa(pfn);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
7937c478bd9Sstevel@tonic-gate 			*size = pfn_addr - *addr;
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (prom_debug)
7977c478bd9Sstevel@tonic-gate 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
7987c478bd9Sstevel@tonic-gate 		    *addr, *size);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate static void
8027c478bd9Sstevel@tonic-gate kpm_init()
8037c478bd9Sstevel@tonic-gate {
8047c478bd9Sstevel@tonic-gate 	struct segkpm_crargs b;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/*
8077c478bd9Sstevel@tonic-gate 	 * These variables were all designed for sfmmu in which segkpm is
8087c478bd9Sstevel@tonic-gate 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
8097c478bd9Sstevel@tonic-gate 	 * might use 2+ page sizes on a single machine, so none of these
8107c478bd9Sstevel@tonic-gate 	 * variables have a single correct value.  They are set up as if we
8117c478bd9Sstevel@tonic-gate 	 * always use a 4KB pagesize, which should do no harm.  In the long
8127c478bd9Sstevel@tonic-gate 	 * run, we should get rid of KPM's assumption that only a single
8137c478bd9Sstevel@tonic-gate 	 * pagesize is used.
8147c478bd9Sstevel@tonic-gate 	 */
8157c478bd9Sstevel@tonic-gate 	kpm_pgshft = MMU_PAGESHIFT;
8167c478bd9Sstevel@tonic-gate 	kpm_pgsz =  MMU_PAGESIZE;
8177c478bd9Sstevel@tonic-gate 	kpm_pgoff = MMU_PAGEOFFSET;
8187c478bd9Sstevel@tonic-gate 	kpmp2pshft = 0;
8197c478bd9Sstevel@tonic-gate 	kpmpnpgs = 1;
8207c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	PRM_POINT("about to create segkpm");
8237c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
8267c478bd9Sstevel@tonic-gate 		panic("cannot attach segkpm");
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	b.prot = PROT_READ | PROT_WRITE;
8297c478bd9Sstevel@tonic-gate 	b.nvcolors = 1;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
8327c478bd9Sstevel@tonic-gate 		panic("segkpm_create segkpm");
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
835ae115bc7Smrj }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate /*
838ae115bc7Smrj  * The debug info page provides enough information to allow external
839ae115bc7Smrj  * inspectors (e.g. when running under a hypervisor) to bootstrap
840ae115bc7Smrj  * themselves into allowing full-blown kernel debugging.
8417c478bd9Sstevel@tonic-gate  */
842ae115bc7Smrj static void
843ae115bc7Smrj init_debug_info(void)
844ae115bc7Smrj {
845ae115bc7Smrj 	caddr_t mem;
846ae115bc7Smrj 	debug_info_t *di;
847ae115bc7Smrj 
848ae115bc7Smrj #ifndef __lint
849ae115bc7Smrj 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
850ae115bc7Smrj #endif
851ae115bc7Smrj 
852ae115bc7Smrj 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
853ae115bc7Smrj 	    MMU_PAGESIZE);
854ae115bc7Smrj 
855ae115bc7Smrj 	if (mem != (caddr_t)DEBUG_INFO_VA)
856ae115bc7Smrj 		panic("BOP_ALLOC() failed");
857ae115bc7Smrj 	bzero(mem, MMU_PAGESIZE);
858ae115bc7Smrj 
859ae115bc7Smrj 	di = (debug_info_t *)mem;
860ae115bc7Smrj 
861ae115bc7Smrj 	di->di_magic = DEBUG_INFO_MAGIC;
862ae115bc7Smrj 	di->di_version = DEBUG_INFO_VERSION;
863843e1988Sjohnlev 	di->di_modules = (uintptr_t)&modules;
864843e1988Sjohnlev 	di->di_s_text = (uintptr_t)s_text;
865843e1988Sjohnlev 	di->di_e_text = (uintptr_t)e_text;
866843e1988Sjohnlev 	di->di_s_data = (uintptr_t)s_data;
867843e1988Sjohnlev 	di->di_e_data = (uintptr_t)e_data;
868843e1988Sjohnlev 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
869843e1988Sjohnlev 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate /*
873ae115bc7Smrj  * Build the memlists and other kernel essential memory system data structures.
874ae115bc7Smrj  * This is everything at valloc_base.
8757c478bd9Sstevel@tonic-gate  */
8767c478bd9Sstevel@tonic-gate static void
8777c478bd9Sstevel@tonic-gate startup_memlist(void)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	size_t memlist_sz;
8807c478bd9Sstevel@tonic-gate 	size_t memseg_sz;
8817c478bd9Sstevel@tonic-gate 	size_t pagehash_sz;
8827c478bd9Sstevel@tonic-gate 	size_t pp_sz;
8837c478bd9Sstevel@tonic-gate 	uintptr_t va;
8847c478bd9Sstevel@tonic-gate 	size_t len;
8857c478bd9Sstevel@tonic-gate 	uint_t prot;
8867c478bd9Sstevel@tonic-gate 	pfn_t pfn;
8877c478bd9Sstevel@tonic-gate 	int memblocks;
8887c478bd9Sstevel@tonic-gate 	caddr_t pagecolor_mem;
8897c478bd9Sstevel@tonic-gate 	size_t pagecolor_memsz;
8907c478bd9Sstevel@tonic-gate 	caddr_t page_ctrs_mem;
8917c478bd9Sstevel@tonic-gate 	size_t page_ctrs_size;
892d7d93655Sblakej 	size_t pse_table_alloc_size;
8937c478bd9Sstevel@tonic-gate 	struct memlist *current;
8947c478bd9Sstevel@tonic-gate 	extern void startup_build_mem_nodes(struct memlist *);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	/* XX64 fix these - they should be in include files */
8977c478bd9Sstevel@tonic-gate 	extern size_t page_coloring_init(uint_t, int, int);
8987c478bd9Sstevel@tonic-gate 	extern void page_coloring_setup(caddr_t);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_memlist() starting...");
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	/*
9037c478bd9Sstevel@tonic-gate 	 * Use leftover large page nucleus text/data space for loadable modules.
9047c478bd9Sstevel@tonic-gate 	 * Use at most MODTEXT/MODDATA.
9057c478bd9Sstevel@tonic-gate 	 */
906ae115bc7Smrj 	len = kbm_nucleus_size;
907ae115bc7Smrj 	ASSERT(len > MMU_PAGESIZE);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
910ae115bc7Smrj 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
9117c478bd9Sstevel@tonic-gate 	if (e_moddata - moddata > MODDATA)
9127c478bd9Sstevel@tonic-gate 		e_moddata = moddata + MODDATA;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
915ae115bc7Smrj 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
9167c478bd9Sstevel@tonic-gate 	if (e_modtext - modtext > MODTEXT)
9177c478bd9Sstevel@tonic-gate 		e_modtext = modtext + MODTEXT;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	econtig = e_moddata;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9227c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_modtext);
9237c478bd9Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9247c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_moddata);
9257c478bd9Sstevel@tonic-gate 	PRM_DEBUG(econtig);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/*
928ae115bc7Smrj 	 * Examine the boot loader physical memory map to find out:
9297c478bd9Sstevel@tonic-gate 	 * - total memory in system - physinstalled
9307c478bd9Sstevel@tonic-gate 	 * - the max physical address - physmax
931ae115bc7Smrj 	 * - the number of discontiguous segments of memory.
9327c478bd9Sstevel@tonic-gate 	 */
9337c478bd9Sstevel@tonic-gate 	if (prom_debug)
934ae115bc7Smrj 		print_memlist("boot physinstalled",
9357c478bd9Sstevel@tonic-gate 		    bootops->boot_mem->physinstalled);
9367c478bd9Sstevel@tonic-gate 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
9377c478bd9Sstevel@tonic-gate 	    &physinstalled, &memblocks);
9387c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9397c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9407c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memblocks);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	/*
9437c478bd9Sstevel@tonic-gate 	 * Initialize hat's mmu parameters.
9447c478bd9Sstevel@tonic-gate 	 * Check for enforce-prot-exec in boot environment. It's used to
9457c478bd9Sstevel@tonic-gate 	 * enable/disable support for the page table entry NX bit.
9467c478bd9Sstevel@tonic-gate 	 * The default is to enforce PROT_EXEC on processors that support NX.
9477c478bd9Sstevel@tonic-gate 	 * Boot seems to round up the "len", but 8 seems to be big enough.
9487c478bd9Sstevel@tonic-gate 	 */
9497c478bd9Sstevel@tonic-gate 	mmu_init();
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate #ifdef	__i386
9527c478bd9Sstevel@tonic-gate 	/*
9537c478bd9Sstevel@tonic-gate 	 * physmax is lowered if there is more memory than can be
9547c478bd9Sstevel@tonic-gate 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
9557c478bd9Sstevel@tonic-gate 	 */
9567c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
9577c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE64G(physmax)) {
9587c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_64G - 1));
9597c478bd9Sstevel@tonic-gate 			physmax = PFN_64G - 1;
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 	} else {
9627c478bd9Sstevel@tonic-gate 		if (PFN_ABOVE4G(physmax)) {
9637c478bd9Sstevel@tonic-gate 			physinstalled -= (physmax - (PFN_4G - 1));
9647c478bd9Sstevel@tonic-gate 			physmax = PFN_4G - 1;
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate #endif
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
9727c478bd9Sstevel@tonic-gate 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
9737c478bd9Sstevel@tonic-gate 		char value[8];
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 		if (len < 8)
9767c478bd9Sstevel@tonic-gate 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
9777c478bd9Sstevel@tonic-gate 		else
9787c478bd9Sstevel@tonic-gate 			(void) strcpy(value, "");
9797c478bd9Sstevel@tonic-gate 		if (strcmp(value, "off") == 0)
9807c478bd9Sstevel@tonic-gate 			mmu.pt_nx = 0;
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 	PRM_DEBUG(mmu.pt_nx);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * We will need page_t's for every page in the system, except for
9867c478bd9Sstevel@tonic-gate 	 * memory mapped at or above above the start of the kernel text segment.
9877c478bd9Sstevel@tonic-gate 	 *
9887c478bd9Sstevel@tonic-gate 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
9897c478bd9Sstevel@tonic-gate 	 */
9907c478bd9Sstevel@tonic-gate 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
9917c478bd9Sstevel@tonic-gate 	obp_pages = 0;
9927c478bd9Sstevel@tonic-gate 	va = KERNEL_TEXT;
993ae115bc7Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
9947c478bd9Sstevel@tonic-gate 		npages -= len >> MMU_PAGESHIFT;
9957c478bd9Sstevel@tonic-gate 		if (va >= (uintptr_t)e_moddata)
9967c478bd9Sstevel@tonic-gate 			obp_pages += len >> MMU_PAGESHIFT;
9977c478bd9Sstevel@tonic-gate 		va += len;
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
10007c478bd9Sstevel@tonic-gate 	PRM_DEBUG(obp_pages);
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	/*
100335b1ab99Sjosephb 	 * If physmem is patched to be non-zero, use it instead of the computed
100435b1ab99Sjosephb 	 * value unless it is larger than the actual amount of memory on hand.
10057c478bd9Sstevel@tonic-gate 	 */
10064944b02eSkchow 	if (physmem == 0 || physmem > npages) {
10077c478bd9Sstevel@tonic-gate 		physmem = npages;
10084944b02eSkchow 	} else if (physmem < npages) {
1009f53ad214Skchow 		orig_npages = npages;
10107c478bd9Sstevel@tonic-gate 		npages = physmem;
10114944b02eSkchow 	}
10127c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmem);
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	/*
10157c478bd9Sstevel@tonic-gate 	 * We now compute the sizes of all the  initial allocations for
10167c478bd9Sstevel@tonic-gate 	 * structures the kernel needs in order do kmem_alloc(). These
10177c478bd9Sstevel@tonic-gate 	 * include:
10187c478bd9Sstevel@tonic-gate 	 *	memsegs
10197c478bd9Sstevel@tonic-gate 	 *	memlists
10207c478bd9Sstevel@tonic-gate 	 *	page hash table
10217c478bd9Sstevel@tonic-gate 	 *	page_t's
10227c478bd9Sstevel@tonic-gate 	 *	page coloring data structs
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
10257c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
10267c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memseg_sz);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/*
1029ae115bc7Smrj 	 * Reserve space for memlists. There's no real good way to know exactly
1030ae115bc7Smrj 	 * how much room we'll need, but this should be a good upper bound.
10317c478bd9Sstevel@tonic-gate 	 */
10327c478bd9Sstevel@tonic-gate 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
10337c478bd9Sstevel@tonic-gate 	    (memblocks + POSS_NEW_FRAGMENTS));
10347c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
10357c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	/*
10387c478bd9Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
10397c478bd9Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
10427c478bd9Sstevel@tonic-gate 	page_hashsz = 1 << highbit(page_hashsz);
10437c478bd9Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
10447c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
10457c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagehash_sz);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	/*
1048ae115bc7Smrj 	 * Set aside room for the page structures themselves.
10497c478bd9Sstevel@tonic-gate 	 */
1050ae115bc7Smrj 	PRM_DEBUG(npages);
1051ae115bc7Smrj 	pp_sz = sizeof (struct page) * npages;
10527c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
10537c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/*
10567c478bd9Sstevel@tonic-gate 	 * determine l2 cache info and memory size for page coloring
10577c478bd9Sstevel@tonic-gate 	 */
10587c478bd9Sstevel@tonic-gate 	(void) getl2cacheinfo(CPU,
10597c478bd9Sstevel@tonic-gate 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
10607c478bd9Sstevel@tonic-gate 	pagecolor_memsz =
10617c478bd9Sstevel@tonic-gate 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
10627c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
10637c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pagecolor_memsz);
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	page_ctrs_size = page_ctrs_sz();
10667c478bd9Sstevel@tonic-gate 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
10677c478bd9Sstevel@tonic-gate 	PRM_DEBUG(page_ctrs_size);
10687c478bd9Sstevel@tonic-gate 
1069d7d93655Sblakej 	/*
1070d7d93655Sblakej 	 * Allocate the array that protects pp->p_selock.
1071d7d93655Sblakej 	 */
1072d7d93655Sblakej 	pse_shift = size_pse_array(physmem, max_ncpus);
1073d7d93655Sblakej 	pse_table_size = 1 << pse_shift;
1074d7d93655Sblakej 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
1075d7d93655Sblakej 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
1076d7d93655Sblakej 
1077ae115bc7Smrj #if defined(__amd64)
10787c478bd9Sstevel@tonic-gate 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
1079ae115bc7Smrj 	valloc_base = VALLOC_BASE;
108035b1ab99Sjosephb 
108135b1ab99Sjosephb 	/*
108235b1ab99Sjosephb 	 * The default values of VALLOC_BASE and SEGKPM_BASE should work
108335b1ab99Sjosephb 	 * for values of physmax up to 1 Terabyte. They need adjusting when
108435b1ab99Sjosephb 	 * memory is at addresses above 1 TB.
108535b1ab99Sjosephb 	 */
108635b1ab99Sjosephb 	if (physmax + 1 > mmu_btop(TERABYTE)) {
108735b1ab99Sjosephb 		uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
108835b1ab99Sjosephb 
108935b1ab99Sjosephb 		/* Round to largest possible pagesize for now */
109035b1ab99Sjosephb 		kpm_resv_amount = P2ROUNDUP(kpm_resv_amount, ONE_GIG);
109135b1ab99Sjosephb 
109235b1ab99Sjosephb 		segkpm_base = -(2 * kpm_resv_amount); /* down from top VA */
109335b1ab99Sjosephb 
109435b1ab99Sjosephb 		/* make sure we leave some space for user apps above hole */
109535b1ab99Sjosephb 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
109635b1ab99Sjosephb 		if (segkpm_base > SEGKPM_BASE)
109735b1ab99Sjosephb 			segkpm_base = SEGKPM_BASE;
109835b1ab99Sjosephb 		PRM_DEBUG(segkpm_base);
109935b1ab99Sjosephb 
110035b1ab99Sjosephb 		valloc_base = segkpm_base + kpm_resv_amount;
110135b1ab99Sjosephb 		PRM_DEBUG(valloc_base);
110235b1ab99Sjosephb 	}
1103ae115bc7Smrj #else	/* __i386 */
1104ae115bc7Smrj 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
1105ae115bc7Smrj 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
11067c478bd9Sstevel@tonic-gate 	PRM_DEBUG(valloc_base);
110735b1ab99Sjosephb #endif	/* __i386 */
1108ae115bc7Smrj 
1109ae115bc7Smrj 	/*
1110ae115bc7Smrj 	 * do all the initial allocations
1111ae115bc7Smrj 	 */
1112ae115bc7Smrj 	perform_allocations();
1113ae115bc7Smrj 
1114ae115bc7Smrj 	/*
1115ae115bc7Smrj 	 * Build phys_install and phys_avail in kernel memspace.
1116ae115bc7Smrj 	 * - phys_install should be all memory in the system.
1117ae115bc7Smrj 	 * - phys_avail is phys_install minus any memory mapped before this
1118ae115bc7Smrj 	 *    point above KERNEL_TEXT.
1119ae115bc7Smrj 	 */
1120ae115bc7Smrj 	current = phys_install = memlist;
1121ae115bc7Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
1122ae115bc7Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1123ae115bc7Smrj 		panic("physinstalled was too big!");
1124ae115bc7Smrj 	if (prom_debug)
1125ae115bc7Smrj 		print_memlist("phys_install", phys_install);
1126ae115bc7Smrj 
1127ae115bc7Smrj 	phys_avail = current;
1128ae115bc7Smrj 	PRM_POINT("Building phys_avail:\n");
1129ae115bc7Smrj 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
1130ae115bc7Smrj 	    avail_filter);
1131ae115bc7Smrj 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1132ae115bc7Smrj 		panic("physavail was too big!");
1133ae115bc7Smrj 	if (prom_debug)
1134ae115bc7Smrj 		print_memlist("phys_avail", phys_avail);
1135ae115bc7Smrj 
1136ae115bc7Smrj 	/*
1137ae115bc7Smrj 	 * setup page coloring
1138ae115bc7Smrj 	 */
1139ae115bc7Smrj 	page_coloring_setup(pagecolor_mem);
1140ae115bc7Smrj 	page_lock_init();	/* currently a no-op */
1141ae115bc7Smrj 
1142ae115bc7Smrj 	/*
1143ae115bc7Smrj 	 * free page list counters
1144ae115bc7Smrj 	 */
1145ae115bc7Smrj 	(void) page_ctrs_alloc(page_ctrs_mem);
1146ae115bc7Smrj 
1147ae115bc7Smrj 	/*
1148ae115bc7Smrj 	 * Initialize the page structures from the memory lists.
1149ae115bc7Smrj 	 */
1150ae115bc7Smrj 	availrmem_initial = availrmem = freemem = 0;
1151ae115bc7Smrj 	PRM_POINT("Calling kphysm_init()...");
1152ae115bc7Smrj 	npages = kphysm_init(pp_base, npages);
1153ae115bc7Smrj 	PRM_POINT("kphysm_init() done");
1154ae115bc7Smrj 	PRM_DEBUG(npages);
1155ae115bc7Smrj 
1156ae115bc7Smrj 	init_debug_info();
1157ae115bc7Smrj 
1158ae115bc7Smrj 	/*
1159ae115bc7Smrj 	 * Now that page_t's have been initialized, remove all the
1160ae115bc7Smrj 	 * initial allocation pages from the kernel free page lists.
1161ae115bc7Smrj 	 */
1162ae115bc7Smrj 	boot_mapin((caddr_t)valloc_base, valloc_sz);
1163ae115bc7Smrj 	boot_mapin((caddr_t)GDT_VA, MMU_PAGESIZE);
1164ae115bc7Smrj 	boot_mapin((caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE);
1165ae115bc7Smrj 	PRM_POINT("startup_memlist() done");
1166ae115bc7Smrj 
1167ae115bc7Smrj 	PRM_DEBUG(valloc_sz);
11682cb27123Saguzovsk 
1169567d55e1Saguzovsk #if defined(__amd64)
1170567d55e1Saguzovsk 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
1171567d55e1Saguzovsk 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
1172567d55e1Saguzovsk 		extern size_t textrepl_size_thresh;
11732cb27123Saguzovsk 		textrepl_size_thresh = (16 << 20) - 1;
11742cb27123Saguzovsk 	}
1175567d55e1Saguzovsk #endif
1176ae115bc7Smrj }
1177ae115bc7Smrj 
1178ae115bc7Smrj /*
1179ae115bc7Smrj  * Layout the kernel's part of address space and initialize kmem allocator.
1180ae115bc7Smrj  */
1181ae115bc7Smrj static void
1182ae115bc7Smrj startup_kmem(void)
1183ae115bc7Smrj {
1184932dc8e5Sdp78419 	extern void page_set_colorequiv_arr(void);
1185932dc8e5Sdp78419 
1186ae115bc7Smrj 	PRM_POINT("startup_kmem() starting...");
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate #if defined(__amd64)
11897c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
11907c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
11917c478bd9Sstevel@tonic-gate 		    "systems.");
119235b1ab99Sjosephb 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
11937c478bd9Sstevel@tonic-gate 	core_base = (uintptr_t)COREHEAP_BASE;
1194ae115bc7Smrj 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
11957c478bd9Sstevel@tonic-gate #else	/* __i386 */
11967c478bd9Sstevel@tonic-gate 	/*
11977c478bd9Sstevel@tonic-gate 	 * We configure kernelbase based on:
11987c478bd9Sstevel@tonic-gate 	 *
11997c478bd9Sstevel@tonic-gate 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
12007c478bd9Sstevel@tonic-gate 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
12017c478bd9Sstevel@tonic-gate 	 *
12027c478bd9Sstevel@tonic-gate 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
12037c478bd9Sstevel@tonic-gate 	 *    On large memory systems we must lower kernelbase to allow
12047c478bd9Sstevel@tonic-gate 	 *    enough room for page_t's for all of memory.
12057c478bd9Sstevel@tonic-gate 	 *
12067c478bd9Sstevel@tonic-gate 	 * The value set here, might be changed a little later.
12077c478bd9Sstevel@tonic-gate 	 */
12087c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase) {
12097c478bd9Sstevel@tonic-gate 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
12107c478bd9Sstevel@tonic-gate 		if (kernelbase > KERNELBASE_MAX)
12117c478bd9Sstevel@tonic-gate 			kernelbase = KERNELBASE_MAX;
12127c478bd9Sstevel@tonic-gate 	} else {
12137c478bd9Sstevel@tonic-gate 		kernelbase = (uintptr_t)KERNELBASE;
12147c478bd9Sstevel@tonic-gate 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
1217ae115bc7Smrj 	core_base = valloc_base;
12187c478bd9Sstevel@tonic-gate 	core_size = 0;
1219ae115bc7Smrj #endif	/* __i386 */
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_base);
12227c478bd9Sstevel@tonic-gate 	PRM_DEBUG(core_size);
1223ae115bc7Smrj 	PRM_DEBUG(kernelbase);
12247c478bd9Sstevel@tonic-gate 
122535b1ab99Sjosephb #if defined(__i386)
12267c478bd9Sstevel@tonic-gate 	segkp_fromheap = 1;
1227ae115bc7Smrj #endif	/* __i386 */
122835b1ab99Sjosephb 
12297c478bd9Sstevel@tonic-gate 	ekernelheap = (char *)core_base;
1230ae115bc7Smrj 	PRM_DEBUG(ekernelheap);
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	/*
12337c478bd9Sstevel@tonic-gate 	 * Now that we know the real value of kernelbase,
12347c478bd9Sstevel@tonic-gate 	 * update variables that were initialized with a value of
12357c478bd9Sstevel@tonic-gate 	 * KERNELBASE (in common/conf/param.c).
12367c478bd9Sstevel@tonic-gate 	 *
12377c478bd9Sstevel@tonic-gate 	 * XXX	The problem with this sort of hackery is that the
12387c478bd9Sstevel@tonic-gate 	 *	compiler just may feel like putting the const declarations
12397c478bd9Sstevel@tonic-gate 	 *	(in param.c) into the .text section.  Perhaps they should
12407c478bd9Sstevel@tonic-gate 	 *	just be declared as variables there?
12417c478bd9Sstevel@tonic-gate 	 */
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_kernelbase = kernelbase;
12447c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit = kernelbase;
1245ccbaea4fSjosephb #if defined(__amd64)
1246ccbaea4fSjosephb 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
1247ccbaea4fSjosephb #else
12487c478bd9Sstevel@tonic-gate 	*(uintptr_t *)&_userlimit32 = _userlimit;
12497c478bd9Sstevel@tonic-gate #endif
12507c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_kernelbase);
12517c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit);
12527c478bd9Sstevel@tonic-gate 	PRM_DEBUG(_userlimit32);
12537c478bd9Sstevel@tonic-gate 
125435b1ab99Sjosephb 	layout_kernel_va();
125535b1ab99Sjosephb 
125635b1ab99Sjosephb #if defined(__i386)
125735b1ab99Sjosephb 	/*
125835b1ab99Sjosephb 	 * If segmap is too large we can push the bottom of the kernel heap
125935b1ab99Sjosephb 	 * higher than the base.  Or worse, it could exceed the top of the
126035b1ab99Sjosephb 	 * VA space entirely, causing it to wrap around.
126135b1ab99Sjosephb 	 */
126235b1ab99Sjosephb 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
126335b1ab99Sjosephb 		panic("too little address space available for kernelheap,"
126435b1ab99Sjosephb 		    " use eeprom for lower kernelbase or smaller segmapsize");
126535b1ab99Sjosephb #endif	/* __i386 */
126635b1ab99Sjosephb 
12677c478bd9Sstevel@tonic-gate 	/*
12687c478bd9Sstevel@tonic-gate 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
12697c478bd9Sstevel@tonic-gate 	 */
127035b1ab99Sjosephb 	kernelheap_init(kernelheap, ekernelheap,
127135b1ab99Sjosephb 	    kernelheap + MMU_PAGESIZE,
1272ae115bc7Smrj 	    (void *)core_base, (void *)(core_base + core_size));
12737c478bd9Sstevel@tonic-gate 
1274843e1988Sjohnlev #if defined(__xpv)
1275843e1988Sjohnlev 	/*
1276843e1988Sjohnlev 	 * Link pending events struct into cpu struct
1277843e1988Sjohnlev 	 */
1278843e1988Sjohnlev 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
1279843e1988Sjohnlev #endif
12807c478bd9Sstevel@tonic-gate 	/*
12817c478bd9Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
12827c478bd9Sstevel@tonic-gate 	 */
12837c478bd9Sstevel@tonic-gate 	kmem_init();
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	/*
1286932dc8e5Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
1287932dc8e5Sdp78419 	 */
1288932dc8e5Sdp78419 	page_set_colorequiv_arr();
1289932dc8e5Sdp78419 
1290932dc8e5Sdp78419 	/*
12917c478bd9Sstevel@tonic-gate 	 * print this out early so that we know what's going on
12927c478bd9Sstevel@tonic-gate 	 */
12937c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE);
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	/*
12967c478bd9Sstevel@tonic-gate 	 * Initialize bp_mapin().
12977c478bd9Sstevel@tonic-gate 	 */
12987c478bd9Sstevel@tonic-gate 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
12997c478bd9Sstevel@tonic-gate 
1300f53ad214Skchow 	/*
1301f53ad214Skchow 	 * orig_npages is non-zero if physmem has been configured for less
1302f53ad214Skchow 	 * than the available memory.
1303f53ad214Skchow 	 */
1304f53ad214Skchow 	if (orig_npages) {
130535b1ab99Sjosephb 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
130635b1ab99Sjosephb 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
1307f53ad214Skchow 		    npages, orig_npages);
1308f53ad214Skchow 	}
13097c478bd9Sstevel@tonic-gate #if defined(__i386)
13107c478bd9Sstevel@tonic-gate 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
13117c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
13127c478bd9Sstevel@tonic-gate 		    "System using 0x%lx",
13137c478bd9Sstevel@tonic-gate 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
13147c478bd9Sstevel@tonic-gate #endif
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate #ifdef	KERNELBASE_ABI_MIN
13177c478bd9Sstevel@tonic-gate 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
13187c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
13197c478bd9Sstevel@tonic-gate 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate #endif
13227c478bd9Sstevel@tonic-gate 
1323843e1988Sjohnlev #ifdef __xpv
1324843e1988Sjohnlev 	/*
1325843e1988Sjohnlev 	 * Some of the xen start information has to be relocated up
1326843e1988Sjohnlev 	 * into the kernel's permanent address space.
1327843e1988Sjohnlev 	 */
1328843e1988Sjohnlev 	PRM_POINT("calling xen_relocate_start_info()");
1329843e1988Sjohnlev 	xen_relocate_start_info();
1330843e1988Sjohnlev 	PRM_POINT("xen_relocate_start_info() done");
1331843e1988Sjohnlev 
1332843e1988Sjohnlev 	/*
1333843e1988Sjohnlev 	 * (Update the vcpu pointer in our cpu structure to point into
1334843e1988Sjohnlev 	 * the relocated shared info.)
1335843e1988Sjohnlev 	 */
1336843e1988Sjohnlev 	CPU->cpu_m.mcpu_vcpu_info =
1337843e1988Sjohnlev 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
1338843e1988Sjohnlev #endif
1339843e1988Sjohnlev 
1340ae115bc7Smrj 	PRM_POINT("startup_kmem() done");
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate static void
13447c478bd9Sstevel@tonic-gate startup_modules(void)
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate 	unsigned int i;
13477c478bd9Sstevel@tonic-gate 	extern void prom_setup(void);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() starting...");
1350843e1988Sjohnlev 
1351843e1988Sjohnlev #ifndef __xpv
13527c478bd9Sstevel@tonic-gate 	/*
13537c478bd9Sstevel@tonic-gate 	 * Initialize ten-micro second timer so that drivers will
13547c478bd9Sstevel@tonic-gate 	 * not get short changed in their init phase. This was
13557c478bd9Sstevel@tonic-gate 	 * not getting called until clkinit which, on fast cpu's
13567c478bd9Sstevel@tonic-gate 	 * caused the drv_usecwait to be way too short.
13577c478bd9Sstevel@tonic-gate 	 */
13587c478bd9Sstevel@tonic-gate 	microfind();
1359843e1988Sjohnlev #endif
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	/*
13627c478bd9Sstevel@tonic-gate 	 * Read the GMT lag from /etc/rtc_config.
13637c478bd9Sstevel@tonic-gate 	 */
1364ae115bc7Smrj 	sgmtl(process_rtc_config_file());
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	/*
13677c478bd9Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
13687c478bd9Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
13697c478bd9Sstevel@tonic-gate 	 */
13707c478bd9Sstevel@tonic-gate 	param_calc(0);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	mod_setup();
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/*
13757c478bd9Sstevel@tonic-gate 	 * Initialize system parameters.
13767c478bd9Sstevel@tonic-gate 	 */
13777c478bd9Sstevel@tonic-gate 	param_init();
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	/*
13809acbbeafSnn35248 	 * Initialize the default brands
13819acbbeafSnn35248 	 */
13829acbbeafSnn35248 	brand_init();
13839acbbeafSnn35248 
13849acbbeafSnn35248 	/*
13857c478bd9Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
13867c478bd9Sstevel@tonic-gate 	 */
13877c478bd9Sstevel@tonic-gate 	maxmem = physmem;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * Initialize segment management stuff.
13917c478bd9Sstevel@tonic-gate 	 */
13927c478bd9Sstevel@tonic-gate 	seg_init();
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	if (modload("fs", "specfs") == -1)
13957c478bd9Sstevel@tonic-gate 		halt("Can't load specfs");
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	if (modload("fs", "devfs") == -1)
13987c478bd9Sstevel@tonic-gate 		halt("Can't load devfs");
13997c478bd9Sstevel@tonic-gate 
1400facf4a8dSllai1 	if (modload("fs", "dev") == -1)
1401facf4a8dSllai1 		halt("Can't load dev");
1402facf4a8dSllai1 
140345916cd2Sjpk 	(void) modloadonly("sys", "lbl_edition");
140445916cd2Sjpk 
14057c478bd9Sstevel@tonic-gate 	dispinit();
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	/*
14087c478bd9Sstevel@tonic-gate 	 * This is needed here to initialize hw_serial[] for cluster booting.
14097c478bd9Sstevel@tonic-gate 	 */
14107c478bd9Sstevel@tonic-gate 	if ((i = modload("misc", "sysinit")) != (unsigned int)-1)
14117c478bd9Sstevel@tonic-gate 		(void) modunload(i);
14127c478bd9Sstevel@tonic-gate 	else
14137c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "sysinit load failed");
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	/* Read cluster configuration data. */
14167c478bd9Sstevel@tonic-gate 	clconf_init();
14177c478bd9Sstevel@tonic-gate 
1418843e1988Sjohnlev #if defined(__xpv)
1419843e1988Sjohnlev 	ec_init();
1420843e1988Sjohnlev 	gnttab_init();
1421843e1988Sjohnlev 	(void) xs_early_init();
1422843e1988Sjohnlev #endif /* __xpv */
1423843e1988Sjohnlev 
14247c478bd9Sstevel@tonic-gate 	/*
14257c478bd9Sstevel@tonic-gate 	 * Create a kernel device tree. First, create rootnex and
14267c478bd9Sstevel@tonic-gate 	 * then invoke bus specific code to probe devices.
14277c478bd9Sstevel@tonic-gate 	 */
14287c478bd9Sstevel@tonic-gate 	setup_ddi();
14297aec1d6eScindi 
143020c794b3Sgavinm #ifndef __xpv
143120c794b3Sgavinm 	{
14327aec1d6eScindi 		/*
143320c794b3Sgavinm 		 * Set up the CPU module subsystem.  Modifies the device tree,
143420c794b3Sgavinm 		 * so it must be done after setup_ddi().
14357aec1d6eScindi 		 */
14367aec1d6eScindi 
143720c794b3Sgavinm 		cmi_hdl_t hdl;
143820c794b3Sgavinm 
143920c794b3Sgavinm 		if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
144020c794b3Sgavinm 		    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
14417aec1d6eScindi 			if (x86_feature & X86_MCA)
144220c794b3Sgavinm 				cmi_mca_init(hdl);
144320c794b3Sgavinm 		}
144420c794b3Sgavinm 	}
144520c794b3Sgavinm #endif	/* __xpv */
14467aec1d6eScindi 
14477c478bd9Sstevel@tonic-gate 	/*
14487c478bd9Sstevel@tonic-gate 	 * Fake a prom tree such that /dev/openprom continues to work
14497c478bd9Sstevel@tonic-gate 	 */
1450ae115bc7Smrj 	PRM_POINT("startup_modules: calling prom_setup...");
14517c478bd9Sstevel@tonic-gate 	prom_setup();
1452ae115bc7Smrj 	PRM_POINT("startup_modules: done");
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	/*
14557c478bd9Sstevel@tonic-gate 	 * Load all platform specific modules
14567c478bd9Sstevel@tonic-gate 	 */
1457ae115bc7Smrj 	PRM_POINT("startup_modules: calling psm_modload...");
14587c478bd9Sstevel@tonic-gate 	psm_modload();
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_modules() done");
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate 
1463ae115bc7Smrj /*
1464ae115bc7Smrj  * claim a "setaside" boot page for use in the kernel
1465ae115bc7Smrj  */
1466ae115bc7Smrj page_t *
1467ae115bc7Smrj boot_claim_page(pfn_t pfn)
14687c478bd9Sstevel@tonic-gate {
1469ae115bc7Smrj 	page_t *pp;
14707c478bd9Sstevel@tonic-gate 
1471ae115bc7Smrj 	pp = page_numtopp_nolock(pfn);
1472ae115bc7Smrj 	ASSERT(pp != NULL);
14737c478bd9Sstevel@tonic-gate 
1474ae115bc7Smrj 	if (PP_ISBOOTPAGES(pp)) {
1475ae115bc7Smrj 		if (pp->p_next != NULL)
1476ae115bc7Smrj 			pp->p_next->p_prev = pp->p_prev;
1477ae115bc7Smrj 		if (pp->p_prev == NULL)
1478ae115bc7Smrj 			bootpages = pp->p_next;
1479ae115bc7Smrj 		else
1480ae115bc7Smrj 			pp->p_prev->p_next = pp->p_next;
1481ae115bc7Smrj 	} else {
14827c478bd9Sstevel@tonic-gate 		/*
1483ae115bc7Smrj 		 * htable_attach() expects a base pagesize page
14847c478bd9Sstevel@tonic-gate 		 */
1485ae115bc7Smrj 		if (pp->p_szc != 0)
1486ae115bc7Smrj 			page_boot_demote(pp);
1487ae115bc7Smrj 		pp = page_numtopp(pfn, SE_EXCL);
1488ae115bc7Smrj 	}
1489ae115bc7Smrj 	return (pp);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /*
14937c478bd9Sstevel@tonic-gate  * Walk through the pagetables looking for pages mapped in by boot.  If the
14947c478bd9Sstevel@tonic-gate  * setaside flag is set the pages are expected to be returned to the
14957c478bd9Sstevel@tonic-gate  * kernel later in boot, so we add them to the bootpages list.
14967c478bd9Sstevel@tonic-gate  */
14977c478bd9Sstevel@tonic-gate static void
14987c478bd9Sstevel@tonic-gate protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
14997c478bd9Sstevel@tonic-gate {
15007c478bd9Sstevel@tonic-gate 	uintptr_t va = low;
15017c478bd9Sstevel@tonic-gate 	size_t len;
15027c478bd9Sstevel@tonic-gate 	uint_t prot;
15037c478bd9Sstevel@tonic-gate 	pfn_t pfn;
15047c478bd9Sstevel@tonic-gate 	page_t *pp;
15057c478bd9Sstevel@tonic-gate 	pgcnt_t boot_protect_cnt = 0;
15067c478bd9Sstevel@tonic-gate 
1507ae115bc7Smrj 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
15087c478bd9Sstevel@tonic-gate 		if (va + len >= high)
15097c478bd9Sstevel@tonic-gate 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
15107c478bd9Sstevel@tonic-gate 			    "legal range.", len, (void *)va);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 		while (len > 0) {
15137c478bd9Sstevel@tonic-gate 			pp = page_numtopp_alloc(pfn);
15147c478bd9Sstevel@tonic-gate 			if (pp != NULL) {
15157c478bd9Sstevel@tonic-gate 				if (setaside == 0)
15167c478bd9Sstevel@tonic-gate 					panic("Unexpected mapping by boot.  "
15177c478bd9Sstevel@tonic-gate 					    "addr=%p pfn=%lx\n",
15187c478bd9Sstevel@tonic-gate 					    (void *)va, pfn);
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 				pp->p_next = bootpages;
1521ae115bc7Smrj 				pp->p_prev = NULL;
1522ae115bc7Smrj 				PP_SETBOOTPAGES(pp);
1523ae115bc7Smrj 				if (bootpages != NULL) {
1524ae115bc7Smrj 					bootpages->p_prev = pp;
1525ae115bc7Smrj 				}
15267c478bd9Sstevel@tonic-gate 				bootpages = pp;
15277c478bd9Sstevel@tonic-gate 				++boot_protect_cnt;
15287c478bd9Sstevel@tonic-gate 			}
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 			++pfn;
15317c478bd9Sstevel@tonic-gate 			len -= MMU_PAGESIZE;
15327c478bd9Sstevel@tonic-gate 			va += MMU_PAGESIZE;
15337c478bd9Sstevel@tonic-gate 		}
15347c478bd9Sstevel@tonic-gate 	}
15357c478bd9Sstevel@tonic-gate 	PRM_DEBUG(boot_protect_cnt);
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate 
1538ae115bc7Smrj /*
153935b1ab99Sjosephb  *
1540ae115bc7Smrj  */
15417c478bd9Sstevel@tonic-gate static void
154235b1ab99Sjosephb layout_kernel_va(void)
15437c478bd9Sstevel@tonic-gate {
154435b1ab99Sjosephb 	PRM_POINT("layout_kernel_va() starting...");
15457c478bd9Sstevel@tonic-gate 	/*
1546ae115bc7Smrj 	 * Establish the final size of the kernel's heap, size of segmap,
1547ae115bc7Smrj 	 * segkp, etc.
15487c478bd9Sstevel@tonic-gate 	 */
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate #if defined(__amd64)
15517c478bd9Sstevel@tonic-gate 
155235b1ab99Sjosephb 	kpm_vbase = (caddr_t)segkpm_base;
1553ae115bc7Smrj 	kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
155435b1ab99Sjosephb 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
155535b1ab99Sjosephb 		panic("not enough room for kpm!");
1556ae115bc7Smrj 	PRM_DEBUG(kpm_size);
1557ae115bc7Smrj 	PRM_DEBUG(kpm_vbase);
1558ae115bc7Smrj 
1559ae115bc7Smrj 	/*
1560ae115bc7Smrj 	 * By default we create a seg_kp in 64 bit kernels, it's a little
1561ae115bc7Smrj 	 * faster to access than embedding it in the heap.
1562ae115bc7Smrj 	 */
1563ae115bc7Smrj 	segkp_base = (caddr_t)valloc_base + valloc_sz;
15647c478bd9Sstevel@tonic-gate 	if (!segkp_fromheap) {
15657c478bd9Sstevel@tonic-gate 		size_t sz = mmu_ptob(segkpsize);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		/*
1568ae115bc7Smrj 		 * determine size of segkp
15697c478bd9Sstevel@tonic-gate 		 */
15707c478bd9Sstevel@tonic-gate 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
15717c478bd9Sstevel@tonic-gate 			sz = SEGKPDEFSIZE;
15727c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
15737c478bd9Sstevel@tonic-gate 			    "segkpsize has been reset to %ld pages",
15747c478bd9Sstevel@tonic-gate 			    mmu_btop(sz));
15757c478bd9Sstevel@tonic-gate 		}
15767c478bd9Sstevel@tonic-gate 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
15797c478bd9Sstevel@tonic-gate 	}
1580ae115bc7Smrj 	PRM_DEBUG(segkp_base);
1581ae115bc7Smrj 	PRM_DEBUG(segkpsize);
15827c478bd9Sstevel@tonic-gate 
158335b1ab99Sjosephb 	/*
158435b1ab99Sjosephb 	 * segzio is used for ZFS cached data. It uses a distinct VA
158535b1ab99Sjosephb 	 * segment (from kernel heap) so that we can easily tell not to
158635b1ab99Sjosephb 	 * include it in kernel crash dumps on 64 bit kernels. The trick is
158735b1ab99Sjosephb 	 * to give it lots of VA, but not constrain the kernel heap.
158835b1ab99Sjosephb 	 * We scale the size of segzio linearly with physmem up to
158935b1ab99Sjosephb 	 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem.
159035b1ab99Sjosephb 	 */
1591ae115bc7Smrj 	segzio_base = segkp_base + mmu_ptob(segkpsize);
1592ae115bc7Smrj 	if (segzio_fromheap) {
1593ae115bc7Smrj 		segziosize = 0;
1594ae115bc7Smrj 	} else {
159535b1ab99Sjosephb 		size_t physmem_size = mmu_ptob(physmem);
159635b1ab99Sjosephb 		size_t size = (segziosize == 0) ?
159735b1ab99Sjosephb 		    physmem_size : mmu_ptob(segziosize);
1598ad23a2dbSjohansen 
159935b1ab99Sjosephb 		if (size < SEGZIOMINSIZE)
1600ad23a2dbSjohansen 			size = SEGZIOMINSIZE;
160135b1ab99Sjosephb 		if (size > SEGZIOMAXSIZE) {
1602a778305aSjohansen 			size = SEGZIOMAXSIZE;
160335b1ab99Sjosephb 			if (physmem_size > size)
160435b1ab99Sjosephb 				size += (physmem_size - size) / 2;
1605ad23a2dbSjohansen 		}
1606ad23a2dbSjohansen 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
1607ae115bc7Smrj 	}
1608ad23a2dbSjohansen 	PRM_DEBUG(segziosize);
1609ad23a2dbSjohansen 	PRM_DEBUG(segzio_base);
1610ad23a2dbSjohansen 
16117c478bd9Sstevel@tonic-gate 	/*
1612ae115bc7Smrj 	 * Put the range of VA for device mappings next, kmdb knows to not
1613ae115bc7Smrj 	 * grep in this range of addresses.
16147c478bd9Sstevel@tonic-gate 	 */
1615ae115bc7Smrj 	toxic_addr =
1616ae115bc7Smrj 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
16177c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_addr);
1618ae115bc7Smrj 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
1619ae115bc7Smrj #else /* __i386 */
1620ae115bc7Smrj 	segmap_start = ROUND_UP_LPAGE(kernelbase);
1621ae115bc7Smrj #endif /* __i386 */
1622ae115bc7Smrj 	PRM_DEBUG(segmap_start);
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/*
16257c478bd9Sstevel@tonic-gate 	 * Users can change segmapsize through eeprom or /etc/system.
16267c478bd9Sstevel@tonic-gate 	 * If the variable is tuned through eeprom, there is no upper
16277c478bd9Sstevel@tonic-gate 	 * bound on the size of segmap.  If it is tuned through
16287c478bd9Sstevel@tonic-gate 	 * /etc/system on 32-bit systems, it must be no larger than we
16297c478bd9Sstevel@tonic-gate 	 * planned for in startup_memlist().
16307c478bd9Sstevel@tonic-gate 	 */
16317c478bd9Sstevel@tonic-gate 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate #if defined(__i386)
16347c478bd9Sstevel@tonic-gate 	/*
16357c478bd9Sstevel@tonic-gate 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
16367c478bd9Sstevel@tonic-gate 	 * the bottom of the kernel's address range.  Set aside space for a
1637ae115bc7Smrj 	 * small red zone just below the start of segmap.
16387c478bd9Sstevel@tonic-gate 	 */
1639ae115bc7Smrj 	segmap_start += KERNEL_REDZONE_SIZE;
16407c478bd9Sstevel@tonic-gate 	segmapsize -= KERNEL_REDZONE_SIZE;
16417c478bd9Sstevel@tonic-gate #endif
16427c478bd9Sstevel@tonic-gate 
1643ae115bc7Smrj 	PRM_DEBUG(segmap_start);
16447c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segmapsize);
164535b1ab99Sjosephb 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
164635b1ab99Sjosephb 	PRM_DEBUG(kernelheap);
164735b1ab99Sjosephb 	PRM_POINT("layout_kernel_va() done...");
164835b1ab99Sjosephb }
164935b1ab99Sjosephb 
165035b1ab99Sjosephb /*
165135b1ab99Sjosephb  * Finish initializing the VM system, now that we are no longer
165235b1ab99Sjosephb  * relying on the boot time memory allocators.
165335b1ab99Sjosephb  */
165435b1ab99Sjosephb static void
165535b1ab99Sjosephb startup_vm(void)
165635b1ab99Sjosephb {
165735b1ab99Sjosephb 	struct segmap_crargs a;
165835b1ab99Sjosephb 
165935b1ab99Sjosephb 	extern int use_brk_lpg, use_stk_lpg;
166035b1ab99Sjosephb 
166135b1ab99Sjosephb 	PRM_POINT("startup_vm() starting...");
166235b1ab99Sjosephb 
166335b1ab99Sjosephb 	/*
166435b1ab99Sjosephb 	 * Initialize the hat layer.
166535b1ab99Sjosephb 	 */
166635b1ab99Sjosephb 	hat_init();
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	/*
1669ae115bc7Smrj 	 * Do final allocations of HAT data structures that need to
1670ae115bc7Smrj 	 * be allocated before quiescing the boot loader.
1671ae115bc7Smrj 	 */
1672ae115bc7Smrj 	PRM_POINT("Calling hat_kern_alloc()...");
1673ae115bc7Smrj 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
1674ae115bc7Smrj 	PRM_POINT("hat_kern_alloc() done");
1675ae115bc7Smrj 
1676843e1988Sjohnlev #ifndef __xpv
1677ae115bc7Smrj 	/*
16781d03c31eSjohnlev 	 * Setup Page Attribute Table
1679ae115bc7Smrj 	 */
16801d03c31eSjohnlev 	pat_sync();
1681843e1988Sjohnlev #endif
1682ae115bc7Smrj 
1683ae115bc7Smrj 	/*
1684ae115bc7Smrj 	 * The next two loops are done in distinct steps in order
1685ae115bc7Smrj 	 * to be sure that any page that is doubly mapped (both above
1686ae115bc7Smrj 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
1687ae115bc7Smrj 	 * Note this may never happen, but it might someday.
1688ae115bc7Smrj 	 */
1689ae115bc7Smrj 	bootpages = NULL;
1690ae115bc7Smrj 	PRM_POINT("Protecting boot pages");
1691ae115bc7Smrj 
1692ae115bc7Smrj 	/*
1693ae115bc7Smrj 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
1694ae115bc7Smrj 	 * page_t's. This can only happen if something weird allocated
1695ae115bc7Smrj 	 * in this range (like kadb/kmdb).
1696ae115bc7Smrj 	 */
1697ae115bc7Smrj 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
1698ae115bc7Smrj 
1699ae115bc7Smrj 	/*
1700ae115bc7Smrj 	 * Before we can take over memory allocation/mapping from the boot
1701ae115bc7Smrj 	 * loader we must remove from our free page lists any boot allocated
1702ae115bc7Smrj 	 * pages that stay mapped until release_bootstrap().
1703ae115bc7Smrj 	 */
1704ae115bc7Smrj 	protect_boot_range(0, kernelbase, 1);
1705ae115bc7Smrj 
1706843e1988Sjohnlev 
1707ae115bc7Smrj 	/*
1708ae115bc7Smrj 	 * Switch to running on regular HAT (not boot_mmu)
1709ae115bc7Smrj 	 */
1710ae115bc7Smrj 	PRM_POINT("Calling hat_kern_setup()...");
1711ae115bc7Smrj 	hat_kern_setup();
1712ae115bc7Smrj 
1713ae115bc7Smrj 	/*
1714ae115bc7Smrj 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
1715ae115bc7Smrj 	 */
1716ae115bc7Smrj 	bop_no_more_mem();
1717ae115bc7Smrj 
1718ae115bc7Smrj 	PRM_POINT("hat_kern_setup() done");
1719ae115bc7Smrj 
1720ae115bc7Smrj 	hat_cpu_online(CPU);
1721ae115bc7Smrj 
1722ae115bc7Smrj 	/*
17237c478bd9Sstevel@tonic-gate 	 * Initialize VM system
17247c478bd9Sstevel@tonic-gate 	 */
17257c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling kvm_init()...");
17267c478bd9Sstevel@tonic-gate 	kvm_init();
17277c478bd9Sstevel@tonic-gate 	PRM_POINT("kvm_init() done");
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	/*
17307c478bd9Sstevel@tonic-gate 	 * Tell kmdb that the VM system is now working
17317c478bd9Sstevel@tonic-gate 	 */
17327c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
17337c478bd9Sstevel@tonic-gate 		kdi_dvec_vmready();
17347c478bd9Sstevel@tonic-gate 
1735843e1988Sjohnlev #if defined(__xpv)
1736843e1988Sjohnlev 	/*
1737843e1988Sjohnlev 	 * Populate the I/O pool on domain 0
1738843e1988Sjohnlev 	 */
1739843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1740843e1988Sjohnlev 		extern long populate_io_pool(void);
1741843e1988Sjohnlev 		long init_io_pool_cnt;
1742843e1988Sjohnlev 
1743843e1988Sjohnlev 		PRM_POINT("Populating reserve I/O page pool");
1744843e1988Sjohnlev 		init_io_pool_cnt = populate_io_pool();
1745843e1988Sjohnlev 		PRM_DEBUG(init_io_pool_cnt);
1746843e1988Sjohnlev 	}
1747843e1988Sjohnlev #endif
17487c478bd9Sstevel@tonic-gate 	/*
17497c478bd9Sstevel@tonic-gate 	 * Mangle the brand string etc.
17507c478bd9Sstevel@tonic-gate 	 */
17517c478bd9Sstevel@tonic-gate 	cpuid_pass3(CPU);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate #if defined(__amd64)
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	/*
17567c478bd9Sstevel@tonic-gate 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
17577c478bd9Sstevel@tonic-gate 	 */
17587c478bd9Sstevel@tonic-gate 	device_arena = vmem_create("device", (void *)toxic_addr,
17597c478bd9Sstevel@tonic-gate 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate #else	/* __i386 */
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	/*
17647c478bd9Sstevel@tonic-gate 	 * allocate the bit map that tracks toxic pages
17657c478bd9Sstevel@tonic-gate 	 */
1766ae115bc7Smrj 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
17677c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map_len);
17687c478bd9Sstevel@tonic-gate 	toxic_bit_map =
17697c478bd9Sstevel@tonic-gate 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
17707c478bd9Sstevel@tonic-gate 	ASSERT(toxic_bit_map != NULL);
17717c478bd9Sstevel@tonic-gate 	PRM_DEBUG(toxic_bit_map);
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate #endif	/* __i386 */
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	/*
17777c478bd9Sstevel@tonic-gate 	 * Now that we've got more VA, as well as the ability to allocate from
17787c478bd9Sstevel@tonic-gate 	 * it, tell the debugger.
17797c478bd9Sstevel@tonic-gate 	 */
17807c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
17817c478bd9Sstevel@tonic-gate 		kdi_dvec_memavail();
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	/*
17847c478bd9Sstevel@tonic-gate 	 * The following code installs a special page fault handler (#pf)
17857c478bd9Sstevel@tonic-gate 	 * to work around a pentium bug.
17867c478bd9Sstevel@tonic-gate 	 */
1787843e1988Sjohnlev #if !defined(__amd64) && !defined(__xpv)
17887c478bd9Sstevel@tonic-gate 	if (x86_type == X86_TYPE_P5) {
1789ae115bc7Smrj 		desctbr_t idtr;
17907c478bd9Sstevel@tonic-gate 		gate_desc_t *newidt;
1791ae115bc7Smrj 		struct machcpu *mcpu = &CPU->cpu_m;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
17947c478bd9Sstevel@tonic-gate 			panic("failed to install pentium_pftrap");
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 		bcopy(idt0, newidt, sizeof (idt0));
17977c478bd9Sstevel@tonic-gate 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
1798843e1988Sjohnlev 		    KCS_SEL, SDT_SYSIGT, TRP_KPL);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
18017c478bd9Sstevel@tonic-gate 		    PROT_READ|PROT_EXEC);
18027c478bd9Sstevel@tonic-gate 
1803ae115bc7Smrj 		mcpu->mcpu_idt = newidt;
1804ae115bc7Smrj 		idtr.dtr_base = (uintptr_t)mcpu->mcpu_idt;
1805ae115bc7Smrj 		idtr.dtr_limit = sizeof (idt0) - 1;
1806ae115bc7Smrj 		wr_idtr(&idtr);
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate #endif	/* !__amd64 */
18097c478bd9Sstevel@tonic-gate 
1810843e1988Sjohnlev #if !defined(__xpv)
18117c478bd9Sstevel@tonic-gate 	/*
18127c478bd9Sstevel@tonic-gate 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
18137c478bd9Sstevel@tonic-gate 	 * parameters left there by controllers/BIOS.
18147c478bd9Sstevel@tonic-gate 	 */
18157c478bd9Sstevel@tonic-gate 	PRM_POINT("setup up p0_va");
18167c478bd9Sstevel@tonic-gate 	p0_va = i86devmap(0, 1, PROT_READ);
18177c478bd9Sstevel@tonic-gate 	PRM_DEBUG(p0_va);
1818843e1988Sjohnlev #endif
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
18217c478bd9Sstevel@tonic-gate 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
18227c478bd9Sstevel@tonic-gate 
182383f9b804Skchow 	/*
182483f9b804Skchow 	 * disable automatic large pages for small memory systems or
182583f9b804Skchow 	 * when the disable flag is set.
182683f9b804Skchow 	 */
1827ec25b48fSsusans 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
1828ec25b48fSsusans 		max_uheap_lpsize = LEVEL_SIZE(1);
1829ec25b48fSsusans 		max_ustack_lpsize = LEVEL_SIZE(1);
1830ec25b48fSsusans 		max_privmap_lpsize = LEVEL_SIZE(1);
1831ec25b48fSsusans 		max_uidata_lpsize = LEVEL_SIZE(1);
1832ec25b48fSsusans 		max_utext_lpsize = LEVEL_SIZE(1);
1833ec25b48fSsusans 		max_shm_lpsize = LEVEL_SIZE(1);
1834ec25b48fSsusans 	}
1835ec25b48fSsusans 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
1836ec25b48fSsusans 	    auto_lpg_disable) {
1837beb1bda0Sdavemq 		use_brk_lpg = 0;
1838beb1bda0Sdavemq 		use_stk_lpg = 0;
1839ec25b48fSsusans 	}
1840ec25b48fSsusans 	if (mmu.max_page_level > 0) {
1841ec25b48fSsusans 		mcntl0_lpsize = LEVEL_SIZE(1);
1842beb1bda0Sdavemq 	}
1843beb1bda0Sdavemq 
18447c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling hat_init_finish()...");
18457c478bd9Sstevel@tonic-gate 	hat_init_finish();
18467c478bd9Sstevel@tonic-gate 	PRM_POINT("hat_init_finish() done");
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	/*
18497c478bd9Sstevel@tonic-gate 	 * Initialize the segkp segment type.
18507c478bd9Sstevel@tonic-gate 	 */
18517c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
1852ae115bc7Smrj 	PRM_POINT("Attaching segkp");
1853ae115bc7Smrj 	if (segkp_fromheap) {
1854ae115bc7Smrj 		segkp->s_as = &kas;
1855ae115bc7Smrj 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
18567c478bd9Sstevel@tonic-gate 	    segkp) < 0) {
18577c478bd9Sstevel@tonic-gate 		panic("startup: cannot attach segkp");
18587c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
18597c478bd9Sstevel@tonic-gate 	}
1860ae115bc7Smrj 	PRM_POINT("Doing segkp_create()");
18617c478bd9Sstevel@tonic-gate 	if (segkp_create(segkp) != 0) {
18627c478bd9Sstevel@tonic-gate 		panic("startup: segkp_create failed");
18637c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
18647c478bd9Sstevel@tonic-gate 	}
18657c478bd9Sstevel@tonic-gate 	PRM_DEBUG(segkp);
18667c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	/*
18697c478bd9Sstevel@tonic-gate 	 * kpm segment
18707c478bd9Sstevel@tonic-gate 	 */
18717c478bd9Sstevel@tonic-gate 	segmap_kpm = 0;
18727c478bd9Sstevel@tonic-gate 	if (kpm_desired) {
18737c478bd9Sstevel@tonic-gate 		kpm_init();
18747c478bd9Sstevel@tonic-gate 		kpm_enable = 1;
1875a5652762Spraks 		vpm_enable = 1;
18767c478bd9Sstevel@tonic-gate 	}
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	/*
18797c478bd9Sstevel@tonic-gate 	 * Now create segmap segment.
18807c478bd9Sstevel@tonic-gate 	 */
18817c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
1882ae115bc7Smrj 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
1883ae115bc7Smrj 		panic("cannot attach segmap");
18847c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
18857c478bd9Sstevel@tonic-gate 	}
1886ae115bc7Smrj 	PRM_DEBUG(segmap);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
18897c478bd9Sstevel@tonic-gate 	a.shmsize = 0;
18907c478bd9Sstevel@tonic-gate 	a.nfreelist = segmapfreelists;
18917c478bd9Sstevel@tonic-gate 
1892ae115bc7Smrj 	if (segmap_create(segmap, (caddr_t)&a) != 0)
1893ae115bc7Smrj 		panic("segmap_create segmap");
18947c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(CPU);
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	segdev_init();
1899843e1988Sjohnlev #if defined(__xpv)
1900843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
1901843e1988Sjohnlev #endif
19027c478bd9Sstevel@tonic-gate 		pmem_init();
1903ae115bc7Smrj 
19047c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_vm() done");
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
1907ae115bc7Smrj /*
1908ae115bc7Smrj  * Load a tod module for the non-standard tod part found on this system.
1909ae115bc7Smrj  */
1910ae115bc7Smrj static void
1911ae115bc7Smrj load_tod_module(char *todmod)
1912ae115bc7Smrj {
1913ae115bc7Smrj 	if (modload("tod", todmod) == -1)
1914ae115bc7Smrj 		halt("Can't load TOD module");
1915ae115bc7Smrj }
1916ae115bc7Smrj 
19177c478bd9Sstevel@tonic-gate static void
19187c478bd9Sstevel@tonic-gate startup_end(void)
19197c478bd9Sstevel@tonic-gate {
1920dd4eeefdSeota 	int i;
19217c478bd9Sstevel@tonic-gate 	extern void setx86isalist(void);
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() starting...");
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	/*
19267c478bd9Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
19277c478bd9Sstevel@tonic-gate 	 * initialization has been done but before the clock
19287c478bd9Sstevel@tonic-gate 	 * and other devices get started.
19297c478bd9Sstevel@tonic-gate 	 */
19307c478bd9Sstevel@tonic-gate 	kern_setup1();
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	/*
19337c478bd9Sstevel@tonic-gate 	 * Perform CPC initialization for this CPU.
19347c478bd9Sstevel@tonic-gate 	 */
19357c478bd9Sstevel@tonic-gate 	kcpc_hw_init(CPU);
19367c478bd9Sstevel@tonic-gate 
1937ee88d2b9Skchow #if defined(OPTERON_WORKAROUND_6323525)
1938ee88d2b9Skchow 	if (opteron_workaround_6323525)
1939ee88d2b9Skchow 		patch_workaround_6323525();
1940ee88d2b9Skchow #endif
19417c478bd9Sstevel@tonic-gate 	/*
1942ae115bc7Smrj 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
1943ae115bc7Smrj 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
1944ae115bc7Smrj 	 */
1945ae115bc7Smrj 	if (tod_module_name != NULL) {
1946ae115bc7Smrj 		PRM_POINT("load_tod_module()");
1947ae115bc7Smrj 		load_tod_module(tod_module_name);
1948ae115bc7Smrj 	}
1949ae115bc7Smrj 
1950843e1988Sjohnlev #if defined(__xpv)
1951843e1988Sjohnlev 	/*
1952843e1988Sjohnlev 	 * Forceload interposing TOD module for the hypervisor.
1953843e1988Sjohnlev 	 */
1954843e1988Sjohnlev 	PRM_POINT("load_tod_module()");
1955843e1988Sjohnlev 	load_tod_module("xpvtod");
1956843e1988Sjohnlev #endif
1957843e1988Sjohnlev 
1958ae115bc7Smrj 	/*
19597c478bd9Sstevel@tonic-gate 	 * Configure the system.
19607c478bd9Sstevel@tonic-gate 	 */
19617c478bd9Sstevel@tonic-gate 	PRM_POINT("Calling configure()...");
19627c478bd9Sstevel@tonic-gate 	configure();		/* set up devices */
19637c478bd9Sstevel@tonic-gate 	PRM_POINT("configure() done");
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	/*
19667c478bd9Sstevel@tonic-gate 	 * Set the isa_list string to the defined instruction sets we
19677c478bd9Sstevel@tonic-gate 	 * support.
19687c478bd9Sstevel@tonic-gate 	 */
19697c478bd9Sstevel@tonic-gate 	setx86isalist();
1970100b72f4Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
19717c478bd9Sstevel@tonic-gate 	psm_install();
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	/*
19747c478bd9Sstevel@tonic-gate 	 * We're done with bootops.  We don't unmap the bootstrap yet because
19757c478bd9Sstevel@tonic-gate 	 * we're still using bootsvcs.
19767c478bd9Sstevel@tonic-gate 	 */
1977ae115bc7Smrj 	PRM_POINT("NULLing out bootops");
1978ae115bc7Smrj 	*bootopsp = (struct bootops *)NULL;
19797c478bd9Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
19807c478bd9Sstevel@tonic-gate 
1981843e1988Sjohnlev #if defined(__xpv)
1982843e1988Sjohnlev 	ec_init_debug_irq();
1983843e1988Sjohnlev 	xs_domu_init();
1984843e1988Sjohnlev #endif
19857c478bd9Sstevel@tonic-gate 	PRM_POINT("Enabling interrupts");
19867c478bd9Sstevel@tonic-gate 	(*picinitf)();
19877c478bd9Sstevel@tonic-gate 	sti();
1988843e1988Sjohnlev #if defined(__xpv)
1989843e1988Sjohnlev 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
1990843e1988Sjohnlev 	xen_late_startup();
1991843e1988Sjohnlev #endif
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
19947c478bd9Sstevel@tonic-gate 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
19957c478bd9Sstevel@tonic-gate 
1996dd4eeefdSeota 	/*
1997dd4eeefdSeota 	 * Register these software interrupts for ddi timer.
1998dd4eeefdSeota 	 * Software interrupts up to the level 10 are supported.
1999dd4eeefdSeota 	 */
2000dd4eeefdSeota 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
2001dd4eeefdSeota 		char name[sizeof ("timer_softintr") + 2];
2002dd4eeefdSeota 		(void) sprintf(name, "timer_softintr%02d", i);
2003dd4eeefdSeota 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
2004dd4eeefdSeota 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
2005dd4eeefdSeota 	}
2006dd4eeefdSeota 
20077c478bd9Sstevel@tonic-gate 	PRM_POINT("startup_end() done");
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate extern char hw_serial[];
20117c478bd9Sstevel@tonic-gate char *_hs1107 = hw_serial;
20127c478bd9Sstevel@tonic-gate ulong_t  _bdhs34;
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate void
20157c478bd9Sstevel@tonic-gate post_startup(void)
20167c478bd9Sstevel@tonic-gate {
20177c478bd9Sstevel@tonic-gate 	/*
20187c478bd9Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
20197c478bd9Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
20207c478bd9Sstevel@tonic-gate 	 * instruction set extensions.
20217c478bd9Sstevel@tonic-gate 	 */
20227c478bd9Sstevel@tonic-gate 	bind_hwcap();
20237c478bd9Sstevel@tonic-gate 
2024843e1988Sjohnlev #ifdef __xpv
2025843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2026843e1988Sjohnlev #endif
2027843e1988Sjohnlev 	{
20287c478bd9Sstevel@tonic-gate 		/*
2029ae115bc7Smrj 		 * Load the System Management BIOS into the global ksmbios
2030ae115bc7Smrj 		 * handle, if an SMBIOS is present on this system.
203184ab085aSmws 		 */
203284ab085aSmws 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
203384ab085aSmws 
2034843e1988Sjohnlev #if defined(__xpv)
2035843e1988Sjohnlev 		xpv_panic_init();
2036843e1988Sjohnlev #else
203784ab085aSmws 		/*
2038ae115bc7Smrj 		 * Startup the memory scrubber.
2039843e1988Sjohnlev 		 * XXPV	This should be running somewhere ..
20407c478bd9Sstevel@tonic-gate 		 */
204184ab085aSmws 		memscrub_init();
2042843e1988Sjohnlev #endif
2043843e1988Sjohnlev 	}
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	/*
20467aec1d6eScindi 	 * Complete CPU module initialization
20477aec1d6eScindi 	 */
204820c794b3Sgavinm 	cmi_post_startup();
20497aec1d6eScindi 
20507aec1d6eScindi 	/*
20517c478bd9Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
20527c478bd9Sstevel@tonic-gate 	 */
20537c478bd9Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	/*
20567c478bd9Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
20577c478bd9Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
20587c478bd9Sstevel@tonic-gate 	 */
20597c478bd9Sstevel@tonic-gate 	(void) modload("fs", "procfs");
20607c478bd9Sstevel@tonic-gate 
2061c35aa225Smarx 	(void) i_ddi_attach_hw_nodes("pit_beep");
2062c35aa225Smarx 
20637c478bd9Sstevel@tonic-gate #if defined(__i386)
20647c478bd9Sstevel@tonic-gate 	/*
20657c478bd9Sstevel@tonic-gate 	 * Check for required functional Floating Point hardware,
20667c478bd9Sstevel@tonic-gate 	 * unless FP hardware explicitly disabled.
20677c478bd9Sstevel@tonic-gate 	 */
20687c478bd9Sstevel@tonic-gate 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
20697c478bd9Sstevel@tonic-gate 		halt("No working FP hardware found");
20707c478bd9Sstevel@tonic-gate #endif
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	maxmem = freemem;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
20757c478bd9Sstevel@tonic-gate }
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate static int
20787c478bd9Sstevel@tonic-gate pp_in_ramdisk(page_t *pp)
20797c478bd9Sstevel@tonic-gate {
20807c478bd9Sstevel@tonic-gate 	extern uint64_t ramdisk_start, ramdisk_end;
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	return ((pp->p_pagenum >= btop(ramdisk_start)) &&
20837c478bd9Sstevel@tonic-gate 	    (pp->p_pagenum < btopr(ramdisk_end)));
20847c478bd9Sstevel@tonic-gate }
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate void
20877c478bd9Sstevel@tonic-gate release_bootstrap(void)
20887c478bd9Sstevel@tonic-gate {
20897c478bd9Sstevel@tonic-gate 	int root_is_ramdisk;
20907c478bd9Sstevel@tonic-gate 	page_t *pp;
20917c478bd9Sstevel@tonic-gate 	extern void kobj_boot_unmountroot(void);
20927c478bd9Sstevel@tonic-gate 	extern dev_t rootdev;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	/* unmount boot ramdisk and release kmem usage */
20957c478bd9Sstevel@tonic-gate 	kobj_boot_unmountroot();
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	/*
20987c478bd9Sstevel@tonic-gate 	 * We're finished using the boot loader so free its pages.
20997c478bd9Sstevel@tonic-gate 	 */
21007c478bd9Sstevel@tonic-gate 	PRM_POINT("Unmapping lower boot pages");
2101ae115bc7Smrj 	clear_boot_mappings(0, _userlimit);
2102ae115bc7Smrj 	postbootkernelbase = kernelbase;
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	/*
21057c478bd9Sstevel@tonic-gate 	 * If root isn't on ramdisk, destroy the hardcoded
21067c478bd9Sstevel@tonic-gate 	 * ramdisk node now and release the memory. Else,
21077c478bd9Sstevel@tonic-gate 	 * ramdisk memory is kept in rd_pages.
21087c478bd9Sstevel@tonic-gate 	 */
21097c478bd9Sstevel@tonic-gate 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
21107c478bd9Sstevel@tonic-gate 	if (!root_is_ramdisk) {
21117c478bd9Sstevel@tonic-gate 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
21127c478bd9Sstevel@tonic-gate 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
21137c478bd9Sstevel@tonic-gate 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
21147c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(dip, 0);
21157c478bd9Sstevel@tonic-gate 	}
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	PRM_POINT("Releasing boot pages");
21187c478bd9Sstevel@tonic-gate 	while (bootpages) {
21197c478bd9Sstevel@tonic-gate 		pp = bootpages;
21207c478bd9Sstevel@tonic-gate 		bootpages = pp->p_next;
21217c478bd9Sstevel@tonic-gate 		if (root_is_ramdisk && pp_in_ramdisk(pp)) {
21227c478bd9Sstevel@tonic-gate 			pp->p_next = rd_pages;
21237c478bd9Sstevel@tonic-gate 			rd_pages = pp;
21247c478bd9Sstevel@tonic-gate 			continue;
21257c478bd9Sstevel@tonic-gate 		}
21267c478bd9Sstevel@tonic-gate 		pp->p_next = (struct page *)0;
2127ae115bc7Smrj 		pp->p_prev = (struct page *)0;
2128ae115bc7Smrj 		PP_CLRBOOTPAGES(pp);
21297c478bd9Sstevel@tonic-gate 		page_free(pp, 1);
21307c478bd9Sstevel@tonic-gate 	}
2131ae115bc7Smrj 	PRM_POINT("Boot pages released");
21327c478bd9Sstevel@tonic-gate 
2133843e1988Sjohnlev #if !defined(__xpv)
2134843e1988Sjohnlev /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
21357c478bd9Sstevel@tonic-gate 	/*
21367c478bd9Sstevel@tonic-gate 	 * Find 1 page below 1 MB so that other processors can boot up.
21377c478bd9Sstevel@tonic-gate 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
21387c478bd9Sstevel@tonic-gate 	 * We should have just free'd one up.
21397c478bd9Sstevel@tonic-gate 	 */
21407c478bd9Sstevel@tonic-gate 	if (use_mp) {
2141ae115bc7Smrj 		pfn_t pfn;
2142ae115bc7Smrj 
21437c478bd9Sstevel@tonic-gate 		for (pfn = 1; pfn < btop(1*1024*1024); pfn++) {
21447c478bd9Sstevel@tonic-gate 			if (page_numtopp_alloc(pfn) == NULL)
21457c478bd9Sstevel@tonic-gate 				continue;
21467c478bd9Sstevel@tonic-gate 			rm_platter_va = i86devmap(pfn, 1,
21477c478bd9Sstevel@tonic-gate 			    PROT_READ | PROT_WRITE | PROT_EXEC);
21487c478bd9Sstevel@tonic-gate 			rm_platter_pa = ptob(pfn);
21497c478bd9Sstevel@tonic-gate 			hat_devload(kas.a_hat,
21507c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
21517c478bd9Sstevel@tonic-gate 			    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
21527c478bd9Sstevel@tonic-gate 			    HAT_LOAD_NOCONSIST);
21537c478bd9Sstevel@tonic-gate 			break;
21547c478bd9Sstevel@tonic-gate 		}
21557c478bd9Sstevel@tonic-gate 		if (pfn == btop(1*1024*1024))
21567c478bd9Sstevel@tonic-gate 			panic("No page available for starting "
21577c478bd9Sstevel@tonic-gate 			    "other processors");
21587c478bd9Sstevel@tonic-gate 	}
2159843e1988Sjohnlev #endif	/* !__xpv */
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate /*
21637c478bd9Sstevel@tonic-gate  * Initialize the platform-specific parts of a page_t.
21647c478bd9Sstevel@tonic-gate  */
21657c478bd9Sstevel@tonic-gate void
21667c478bd9Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
21677c478bd9Sstevel@tonic-gate {
21687c478bd9Sstevel@tonic-gate 	pp->p_pagenum = pnum;
21697c478bd9Sstevel@tonic-gate 	pp->p_mapping = NULL;
21707c478bd9Sstevel@tonic-gate 	pp->p_embed = 0;
21717c478bd9Sstevel@tonic-gate 	pp->p_share = 0;
21727c478bd9Sstevel@tonic-gate 	pp->p_mlentry = 0;
21737c478bd9Sstevel@tonic-gate }
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate /*
21767c478bd9Sstevel@tonic-gate  * kphysm_init() initializes physical memory.
21777c478bd9Sstevel@tonic-gate  */
21787c478bd9Sstevel@tonic-gate static pgcnt_t
21797c478bd9Sstevel@tonic-gate kphysm_init(
2180ae115bc7Smrj 	page_t *pp,
21817c478bd9Sstevel@tonic-gate 	pgcnt_t npages)
21827c478bd9Sstevel@tonic-gate {
21837c478bd9Sstevel@tonic-gate 	struct memlist	*pmem;
21847c478bd9Sstevel@tonic-gate 	struct memseg	*cur_memseg;
21857c478bd9Sstevel@tonic-gate 	pfn_t		base_pfn;
21867c478bd9Sstevel@tonic-gate 	pgcnt_t		num;
21877c478bd9Sstevel@tonic-gate 	pgcnt_t		pages_done = 0;
21887c478bd9Sstevel@tonic-gate 	uint64_t	addr;
21897c478bd9Sstevel@tonic-gate 	uint64_t	size;
21907c478bd9Sstevel@tonic-gate 	extern pfn_t	ddiphysmin;
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
21937c478bd9Sstevel@tonic-gate 
2194ae115bc7Smrj 	cur_memseg = memseg_base;
21957c478bd9Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
21967c478bd9Sstevel@tonic-gate 		/*
21977c478bd9Sstevel@tonic-gate 		 * In a 32 bit kernel can't use higher memory if we're
21987c478bd9Sstevel@tonic-gate 		 * not booting in PAE mode. This check takes care of that.
21997c478bd9Sstevel@tonic-gate 		 */
22007c478bd9Sstevel@tonic-gate 		addr = pmem->address;
22017c478bd9Sstevel@tonic-gate 		size = pmem->size;
22027c478bd9Sstevel@tonic-gate 		if (btop(addr) > physmax)
22037c478bd9Sstevel@tonic-gate 			continue;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 		/*
22067c478bd9Sstevel@tonic-gate 		 * align addr and size - they may not be at page boundaries
22077c478bd9Sstevel@tonic-gate 		 */
22087c478bd9Sstevel@tonic-gate 		if ((addr & MMU_PAGEOFFSET) != 0) {
22097c478bd9Sstevel@tonic-gate 			addr += MMU_PAGEOFFSET;
22107c478bd9Sstevel@tonic-gate 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
22117c478bd9Sstevel@tonic-gate 			size -= addr - pmem->address;
22127c478bd9Sstevel@tonic-gate 		}
22137c478bd9Sstevel@tonic-gate 
22146bb54764Skchow 		/* only process pages below or equal to physmax */
22156bb54764Skchow 		if ((btop(addr + size) - 1) > physmax)
22166bb54764Skchow 			size = ptob(physmax - btop(addr) + 1);
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 		num = btop(size);
22197c478bd9Sstevel@tonic-gate 		if (num == 0)
22207c478bd9Sstevel@tonic-gate 			continue;
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 		if (num > npages)
22237c478bd9Sstevel@tonic-gate 			num = npages;
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 		npages -= num;
22267c478bd9Sstevel@tonic-gate 		pages_done += num;
22277c478bd9Sstevel@tonic-gate 		base_pfn = btop(addr);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 		if (prom_debug)
22307c478bd9Sstevel@tonic-gate 			prom_printf("MEMSEG addr=0x%" PRIx64
22317c478bd9Sstevel@tonic-gate 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
22327c478bd9Sstevel@tonic-gate 			    addr, num, base_pfn, base_pfn + num);
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 		/*
2235ae115bc7Smrj 		 * Ignore pages below ddiphysmin to simplify ddi memory
22367c478bd9Sstevel@tonic-gate 		 * allocation with non-zero addr_lo requests.
22377c478bd9Sstevel@tonic-gate 		 */
22387c478bd9Sstevel@tonic-gate 		if (base_pfn < ddiphysmin) {
2239ae115bc7Smrj 			if (base_pfn + num <= ddiphysmin)
22407c478bd9Sstevel@tonic-gate 				continue;
22417c478bd9Sstevel@tonic-gate 			pp += (ddiphysmin - base_pfn);
22427c478bd9Sstevel@tonic-gate 			num -= (ddiphysmin - base_pfn);
22437c478bd9Sstevel@tonic-gate 			base_pfn = ddiphysmin;
22447c478bd9Sstevel@tonic-gate 		}
2245ae115bc7Smrj 
22467c478bd9Sstevel@tonic-gate 		/*
22477c478bd9Sstevel@tonic-gate 		 * Build the memsegs entry
22487c478bd9Sstevel@tonic-gate 		 */
22497c478bd9Sstevel@tonic-gate 		cur_memseg->pages = pp;
22507c478bd9Sstevel@tonic-gate 		cur_memseg->epages = pp + num;
22517c478bd9Sstevel@tonic-gate 		cur_memseg->pages_base = base_pfn;
22527c478bd9Sstevel@tonic-gate 		cur_memseg->pages_end = base_pfn + num;
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 		/*
2255ae115bc7Smrj 		 * Insert into memseg list in decreasing pfn range order.
22567c478bd9Sstevel@tonic-gate 		 * Low memory is typically more fragmented such that this
22577c478bd9Sstevel@tonic-gate 		 * ordering keeps the larger ranges at the front of the list
22587c478bd9Sstevel@tonic-gate 		 * for code that searches memseg.
2259ae115bc7Smrj 		 * This ASSERTS that the memsegs coming in from boot are in
2260ae115bc7Smrj 		 * increasing physical address order and not contiguous.
22617c478bd9Sstevel@tonic-gate 		 */
2262ae115bc7Smrj 		if (memsegs != NULL) {
2263ae115bc7Smrj 			ASSERT(cur_memseg->pages_base >= memsegs->pages_end);
2264ae115bc7Smrj 			cur_memseg->next = memsegs;
2265ae115bc7Smrj 		}
22667c478bd9Sstevel@tonic-gate 		memsegs = cur_memseg;
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 		/*
22697c478bd9Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
22707c478bd9Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
22717c478bd9Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
22727c478bd9Sstevel@tonic-gate 		 * it initializes them.
22737c478bd9Sstevel@tonic-gate 		 */
22747c478bd9Sstevel@tonic-gate 		add_physmem(pp, num, base_pfn);
22757c478bd9Sstevel@tonic-gate 		cur_memseg++;
22767c478bd9Sstevel@tonic-gate 		availrmem_initial += num;
22777c478bd9Sstevel@tonic-gate 		availrmem += num;
22787c478bd9Sstevel@tonic-gate 
2279ae115bc7Smrj 		pp += num;
22807c478bd9Sstevel@tonic-gate 	}
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem_initial);
22837c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
22847c478bd9Sstevel@tonic-gate 	PRM_DEBUG(freemem);
22857c478bd9Sstevel@tonic-gate 	build_pfn_hash();
22867c478bd9Sstevel@tonic-gate 	return (pages_done);
22877c478bd9Sstevel@tonic-gate }
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate /*
22907c478bd9Sstevel@tonic-gate  * Kernel VM initialization.
22917c478bd9Sstevel@tonic-gate  */
22927c478bd9Sstevel@tonic-gate static void
22937c478bd9Sstevel@tonic-gate kvm_init(void)
22947c478bd9Sstevel@tonic-gate {
22957c478bd9Sstevel@tonic-gate 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	/*
22987c478bd9Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
22997c478bd9Sstevel@tonic-gate 	 */
23007c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
23017c478bd9Sstevel@tonic-gate 	as_avlinit(&kas);
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
23047c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
23077c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
23087c478bd9Sstevel@tonic-gate 
230935b1ab99Sjosephb 	(void) seg_attach(&kas, kernelheap,
231035b1ab99Sjosephb 	    ekernelheap - kernelheap, &kvseg);
23117c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
23127c478bd9Sstevel@tonic-gate 
2313ae115bc7Smrj 	if (core_size > 0) {
2314ae115bc7Smrj 		PRM_POINT("attaching kvseg_core");
2315ae115bc7Smrj 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
2316ae115bc7Smrj 		    &kvseg_core);
23177c478bd9Sstevel@tonic-gate 		(void) segkmem_create(&kvseg_core);
2318ae115bc7Smrj 	}
2319ad23a2dbSjohansen 
2320ae115bc7Smrj 	if (segziosize > 0) {
2321ae115bc7Smrj 		PRM_POINT("attaching segzio");
2322ad23a2dbSjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2323ad23a2dbSjohansen 		    &kzioseg);
2324ad23a2dbSjohansen 		(void) segkmem_zio_create(&kzioseg);
2325ad23a2dbSjohansen 
2326ad23a2dbSjohansen 		/* create zio area covering new segment */
2327ad23a2dbSjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2328ad23a2dbSjohansen 	}
23297c478bd9Sstevel@tonic-gate 
2330ae115bc7Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
23317c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	/*
23367c478bd9Sstevel@tonic-gate 	 * Ensure that the red zone at kernelbase is never accessible.
23377c478bd9Sstevel@tonic-gate 	 */
2338ae115bc7Smrj 	PRM_POINT("protecting redzone");
23397c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 	/*
23427c478bd9Sstevel@tonic-gate 	 * Make the text writable so that it can be hot patched by DTrace.
23437c478bd9Sstevel@tonic-gate 	 */
23447c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
23457c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	/*
23487c478bd9Sstevel@tonic-gate 	 * Make data writable until end.
23497c478bd9Sstevel@tonic-gate 	 */
23507c478bd9Sstevel@tonic-gate 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
23517c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC);
23527c478bd9Sstevel@tonic-gate }
23537c478bd9Sstevel@tonic-gate 
2354843e1988Sjohnlev #ifndef __xpv
23557c478bd9Sstevel@tonic-gate /*
23561d03c31eSjohnlev  * Solaris adds an entry for Write Combining caching to the PAT
23577c478bd9Sstevel@tonic-gate  */
23581d03c31eSjohnlev static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate void
23611d03c31eSjohnlev pat_sync(void)
23627c478bd9Sstevel@tonic-gate {
23631d03c31eSjohnlev 	ulong_t	cr0, cr0_orig, cr4;
23647c478bd9Sstevel@tonic-gate 
23651d03c31eSjohnlev 	if (!(x86_feature & X86_PAT))
23667c478bd9Sstevel@tonic-gate 		return;
23671d03c31eSjohnlev 	cr0_orig = cr0 = getcr0();
23681d03c31eSjohnlev 	cr4 = getcr4();
23697c478bd9Sstevel@tonic-gate 
23701d03c31eSjohnlev 	/* disable caching and flush all caches and TLBs */
23711d03c31eSjohnlev 	cr0 |= CR0_CD;
23721d03c31eSjohnlev 	cr0 &= ~CR0_NW;
23731d03c31eSjohnlev 	setcr0(cr0);
23747c478bd9Sstevel@tonic-gate 	invalidate_cache();
23751d03c31eSjohnlev 	if (cr4 & CR4_PGE) {
23761d03c31eSjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
23771d03c31eSjohnlev 		setcr4(cr4);
23781d03c31eSjohnlev 	} else {
2379ae115bc7Smrj 		reload_cr3();
23807c478bd9Sstevel@tonic-gate 	}
2381ae115bc7Smrj 
23821d03c31eSjohnlev 	/* add our entry to the PAT */
23831d03c31eSjohnlev 	wrmsr(REG_PAT, pat_attr_reg);
23841d03c31eSjohnlev 
23851d03c31eSjohnlev 	/* flush TLBs and cache again, then reenable cr0 caching */
23861d03c31eSjohnlev 	if (cr4 & CR4_PGE) {
23871d03c31eSjohnlev 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
23881d03c31eSjohnlev 		setcr4(cr4);
23891d03c31eSjohnlev 	} else {
2390ae115bc7Smrj 		reload_cr3();
23911d03c31eSjohnlev 	}
23927c478bd9Sstevel@tonic-gate 	invalidate_cache();
23937c478bd9Sstevel@tonic-gate 	setcr0(cr0_orig);
23947c478bd9Sstevel@tonic-gate }
23957c478bd9Sstevel@tonic-gate 
23961d03c31eSjohnlev #endif /* !__xpv */
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate void
239945916cd2Sjpk get_system_configuration(void)
24007c478bd9Sstevel@tonic-gate {
24017c478bd9Sstevel@tonic-gate 	char	prop[32];
24027c478bd9Sstevel@tonic-gate 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
24037c478bd9Sstevel@tonic-gate 
240435b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
240535b1ab99Sjosephb 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
240635b1ab99Sjosephb 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
240735b1ab99Sjosephb 	    nodes_ll > MAXNODES ||
240835b1ab99Sjosephb 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
240935b1ab99Sjosephb 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
241035b1ab99Sjosephb 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
24117c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = 1;
24127c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = 0;
24137c478bd9Sstevel@tonic-gate 	} else {
24147c478bd9Sstevel@tonic-gate 		system_hardware.hd_nodes = (int)nodes_ll;
24157c478bd9Sstevel@tonic-gate 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
24167c478bd9Sstevel@tonic-gate 	}
241735b1ab99Sjosephb 
241835b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
241935b1ab99Sjosephb 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
242035b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
24217c478bd9Sstevel@tonic-gate 		eprom_kernelbase = NULL;
24227c478bd9Sstevel@tonic-gate 	else
24237c478bd9Sstevel@tonic-gate 		eprom_kernelbase = (uintptr_t)lvalue;
24247c478bd9Sstevel@tonic-gate 
242535b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
242635b1ab99Sjosephb 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
242735b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
24287c478bd9Sstevel@tonic-gate 		segmapsize = SEGMAPDEFAULT;
242935b1ab99Sjosephb 	else
24307c478bd9Sstevel@tonic-gate 		segmapsize = (uintptr_t)lvalue;
24317c478bd9Sstevel@tonic-gate 
243235b1ab99Sjosephb 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
243335b1ab99Sjosephb 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
243435b1ab99Sjosephb 	    kobj_getvalue(prop, &lvalue) == -1)
24357c478bd9Sstevel@tonic-gate 		segmapfreelists = 0;	/* use segmap driver default */
243635b1ab99Sjosephb 	else
24377c478bd9Sstevel@tonic-gate 		segmapfreelists = (int)lvalue;
24384944b02eSkchow 
2439aac11643Sjosephb 	/* physmem used to be here, but moved much earlier to fakebop.c */
24407c478bd9Sstevel@tonic-gate }
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate /*
24437c478bd9Sstevel@tonic-gate  * Add to a memory list.
24447c478bd9Sstevel@tonic-gate  * start = start of new memory segment
24457c478bd9Sstevel@tonic-gate  * len = length of new memory segment in bytes
24467c478bd9Sstevel@tonic-gate  * new = pointer to a new struct memlist
24477c478bd9Sstevel@tonic-gate  * memlistp = memory list to which to add segment.
24487c478bd9Sstevel@tonic-gate  */
2449ae115bc7Smrj void
24507c478bd9Sstevel@tonic-gate memlist_add(
24517c478bd9Sstevel@tonic-gate 	uint64_t start,
24527c478bd9Sstevel@tonic-gate 	uint64_t len,
24537c478bd9Sstevel@tonic-gate 	struct memlist *new,
24547c478bd9Sstevel@tonic-gate 	struct memlist **memlistp)
24557c478bd9Sstevel@tonic-gate {
24567c478bd9Sstevel@tonic-gate 	struct memlist *cur;
24577c478bd9Sstevel@tonic-gate 	uint64_t end = start + len;
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	new->address = start;
24607c478bd9Sstevel@tonic-gate 	new->size = len;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	cur = *memlistp;
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	while (cur) {
24657c478bd9Sstevel@tonic-gate 		if (cur->address >= end) {
24667c478bd9Sstevel@tonic-gate 			new->next = cur;
24677c478bd9Sstevel@tonic-gate 			*memlistp = new;
24687c478bd9Sstevel@tonic-gate 			new->prev = cur->prev;
24697c478bd9Sstevel@tonic-gate 			cur->prev = new;
24707c478bd9Sstevel@tonic-gate 			return;
24717c478bd9Sstevel@tonic-gate 		}
24727c478bd9Sstevel@tonic-gate 		ASSERT(cur->address + cur->size <= start);
24737c478bd9Sstevel@tonic-gate 		if (cur->next == NULL) {
24747c478bd9Sstevel@tonic-gate 			cur->next = new;
24757c478bd9Sstevel@tonic-gate 			new->prev = cur;
24767c478bd9Sstevel@tonic-gate 			new->next = NULL;
24777c478bd9Sstevel@tonic-gate 			return;
24787c478bd9Sstevel@tonic-gate 		}
24797c478bd9Sstevel@tonic-gate 		memlistp = &cur->next;
24807c478bd9Sstevel@tonic-gate 		cur = cur->next;
24817c478bd9Sstevel@tonic-gate 	}
24827c478bd9Sstevel@tonic-gate }
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate void
24857c478bd9Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
24867c478bd9Sstevel@tonic-gate {
24877c478bd9Sstevel@tonic-gate 	size_t tsize = e_modtext - modtext;
24887c478bd9Sstevel@tonic-gate 	size_t dsize = e_moddata - moddata;
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
24917c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
24927c478bd9Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
24937c478bd9Sstevel@tonic-gate 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
24947c478bd9Sstevel@tonic-gate }
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate caddr_t
24977c478bd9Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
24987c478bd9Sstevel@tonic-gate {
24997c478bd9Sstevel@tonic-gate 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
25007c478bd9Sstevel@tonic-gate }
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
25037c478bd9Sstevel@tonic-gate caddr_t
25047c478bd9Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
25057c478bd9Sstevel@tonic-gate {
25067c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_alloc()");
25077c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
25087c478bd9Sstevel@tonic-gate 	return (0);
25097c478bd9Sstevel@tonic-gate }
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate /*ARGSUSED*/
25127c478bd9Sstevel@tonic-gate void
25137c478bd9Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
25147c478bd9Sstevel@tonic-gate {
25157c478bd9Sstevel@tonic-gate 	panic("unexpected call to kobj_texthole_free()");
25167c478bd9Sstevel@tonic-gate }
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate /*
25197c478bd9Sstevel@tonic-gate  * This is called just after configure() in startup().
25207c478bd9Sstevel@tonic-gate  *
25217c478bd9Sstevel@tonic-gate  * The ISALIST concept is a bit hopeless on Intel, because
25227c478bd9Sstevel@tonic-gate  * there's no guarantee of an ever-more-capable processor
25237c478bd9Sstevel@tonic-gate  * given that various parts of the instruction set may appear
25247c478bd9Sstevel@tonic-gate  * and disappear between different implementations.
25257c478bd9Sstevel@tonic-gate  *
25267c478bd9Sstevel@tonic-gate  * While it would be possible to correct it and even enhance
25277c478bd9Sstevel@tonic-gate  * it somewhat, the explicit hardware capability bitmask allows
25287c478bd9Sstevel@tonic-gate  * more flexibility.
25297c478bd9Sstevel@tonic-gate  *
25307c478bd9Sstevel@tonic-gate  * So, we just leave this alone.
25317c478bd9Sstevel@tonic-gate  */
25327c478bd9Sstevel@tonic-gate void
25337c478bd9Sstevel@tonic-gate setx86isalist(void)
25347c478bd9Sstevel@tonic-gate {
25357c478bd9Sstevel@tonic-gate 	char *tp;
25367c478bd9Sstevel@tonic-gate 	size_t len;
25377c478bd9Sstevel@tonic-gate 	extern char *isa_list;
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate #define	TBUFSIZE	1024
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
25427c478bd9Sstevel@tonic-gate 	*tp = '\0';
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate #if defined(__amd64)
25457c478bd9Sstevel@tonic-gate 	(void) strcpy(tp, "amd64 ");
25467c478bd9Sstevel@tonic-gate #endif
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	switch (x86_vendor) {
25497c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Intel:
25507c478bd9Sstevel@tonic-gate 	case X86_VENDOR_AMD:
25517c478bd9Sstevel@tonic-gate 	case X86_VENDOR_TM:
25527c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_CMOV) {
25537c478bd9Sstevel@tonic-gate 			/*
25547c478bd9Sstevel@tonic-gate 			 * Pentium Pro or later
25557c478bd9Sstevel@tonic-gate 			 */
25567c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium_pro");
25577c478bd9Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
25587c478bd9Sstevel@tonic-gate 			    "+mmx pentium_pro " : " ");
25597c478bd9Sstevel@tonic-gate 		}
25607c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
25617c478bd9Sstevel@tonic-gate 	case X86_VENDOR_Cyrix:
25627c478bd9Sstevel@tonic-gate 		/*
25637c478bd9Sstevel@tonic-gate 		 * The Cyrix 6x86 does not have any Pentium features
25647c478bd9Sstevel@tonic-gate 		 * accessible while not at privilege level 0.
25657c478bd9Sstevel@tonic-gate 		 */
25667c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_CPUID) {
25677c478bd9Sstevel@tonic-gate 			(void) strcat(tp, "pentium");
25687c478bd9Sstevel@tonic-gate 			(void) strcat(tp, x86_feature & X86_MMX ?
25697c478bd9Sstevel@tonic-gate 			    "+mmx pentium " : " ");
25707c478bd9Sstevel@tonic-gate 		}
25717c478bd9Sstevel@tonic-gate 		break;
25727c478bd9Sstevel@tonic-gate 	default:
25737c478bd9Sstevel@tonic-gate 		break;
25747c478bd9Sstevel@tonic-gate 	}
25757c478bd9Sstevel@tonic-gate 	(void) strcat(tp, "i486 i386 i86");
25767c478bd9Sstevel@tonic-gate 	len = strlen(tp) + 1;   /* account for NULL at end of string */
25777c478bd9Sstevel@tonic-gate 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
25787c478bd9Sstevel@tonic-gate 	kmem_free(tp, TBUFSIZE);
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate #undef TBUFSIZE
25817c478bd9Sstevel@tonic-gate }
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate #ifdef __amd64
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate void *
25877c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
25887c478bd9Sstevel@tonic-gate {
25897c478bd9Sstevel@tonic-gate 	return (vmem_alloc(device_arena, size, vm_flag));
25907c478bd9Sstevel@tonic-gate }
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate void
25937c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
25947c478bd9Sstevel@tonic-gate {
25957c478bd9Sstevel@tonic-gate 	vmem_free(device_arena, vaddr, size);
25967c478bd9Sstevel@tonic-gate }
25977c478bd9Sstevel@tonic-gate 
2598ae115bc7Smrj #else /* __i386 */
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate void *
26017c478bd9Sstevel@tonic-gate device_arena_alloc(size_t size, int vm_flag)
26027c478bd9Sstevel@tonic-gate {
26037c478bd9Sstevel@tonic-gate 	caddr_t	vaddr;
26047c478bd9Sstevel@tonic-gate 	uintptr_t v;
26057c478bd9Sstevel@tonic-gate 	size_t	start;
26067c478bd9Sstevel@tonic-gate 	size_t	end;
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
26097c478bd9Sstevel@tonic-gate 	if (vaddr == NULL)
26107c478bd9Sstevel@tonic-gate 		return (NULL);
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	v = (uintptr_t)vaddr;
26137c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
2614ae115bc7Smrj 	ASSERT(v + size <= valloc_base);
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
26177c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
26187c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
26197c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	while (start <= end) {
26227c478bd9Sstevel@tonic-gate 		BT_ATOMIC_SET(toxic_bit_map, start);
26237c478bd9Sstevel@tonic-gate 		++start;
26247c478bd9Sstevel@tonic-gate 	}
26257c478bd9Sstevel@tonic-gate 	return (vaddr);
26267c478bd9Sstevel@tonic-gate }
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate void
26297c478bd9Sstevel@tonic-gate device_arena_free(void *vaddr, size_t size)
26307c478bd9Sstevel@tonic-gate {
26317c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
26327c478bd9Sstevel@tonic-gate 	size_t	start;
26337c478bd9Sstevel@tonic-gate 	size_t	end;
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	ASSERT(v >= kernelbase);
2636ae115bc7Smrj 	ASSERT(v + size <= valloc_base);
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
26397c478bd9Sstevel@tonic-gate 	end = btop(v + size - 1 - kernelbase);
26407c478bd9Sstevel@tonic-gate 	ASSERT(start < toxic_bit_map_len);
26417c478bd9Sstevel@tonic-gate 	ASSERT(end < toxic_bit_map_len);
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	while (start <= end) {
26447c478bd9Sstevel@tonic-gate 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
26457c478bd9Sstevel@tonic-gate 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
26467c478bd9Sstevel@tonic-gate 		++start;
26477c478bd9Sstevel@tonic-gate 	}
26487c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, size);
26497c478bd9Sstevel@tonic-gate }
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate /*
26527c478bd9Sstevel@tonic-gate  * returns 1st address in range that is in device arena, or NULL
26537c478bd9Sstevel@tonic-gate  * if len is not NULL it returns the length of the toxic range
26547c478bd9Sstevel@tonic-gate  */
26557c478bd9Sstevel@tonic-gate void *
26567c478bd9Sstevel@tonic-gate device_arena_contains(void *vaddr, size_t size, size_t *len)
26577c478bd9Sstevel@tonic-gate {
26587c478bd9Sstevel@tonic-gate 	uintptr_t v = (uintptr_t)vaddr;
26597c478bd9Sstevel@tonic-gate 	uintptr_t eaddr = v + size;
26607c478bd9Sstevel@tonic-gate 	size_t start;
26617c478bd9Sstevel@tonic-gate 	size_t end;
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	/*
26647c478bd9Sstevel@tonic-gate 	 * if called very early by kmdb, just return NULL
26657c478bd9Sstevel@tonic-gate 	 */
26667c478bd9Sstevel@tonic-gate 	if (toxic_bit_map == NULL)
26677c478bd9Sstevel@tonic-gate 		return (NULL);
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 	/*
26707c478bd9Sstevel@tonic-gate 	 * First check if we're completely outside the bitmap range.
26717c478bd9Sstevel@tonic-gate 	 */
2672ae115bc7Smrj 	if (v >= valloc_base || eaddr < kernelbase)
26737c478bd9Sstevel@tonic-gate 		return (NULL);
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate 	/*
26767c478bd9Sstevel@tonic-gate 	 * Trim ends of search to look at only what the bitmap covers.
26777c478bd9Sstevel@tonic-gate 	 */
26787c478bd9Sstevel@tonic-gate 	if (v < kernelbase)
26797c478bd9Sstevel@tonic-gate 		v = kernelbase;
26807c478bd9Sstevel@tonic-gate 	start = btop(v - kernelbase);
26817c478bd9Sstevel@tonic-gate 	end = btop(eaddr - kernelbase);
26827c478bd9Sstevel@tonic-gate 	if (end >= toxic_bit_map_len)
26837c478bd9Sstevel@tonic-gate 		end = toxic_bit_map_len;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
26867c478bd9Sstevel@tonic-gate 		return (NULL);
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 	v = kernelbase + ptob(start);
26897c478bd9Sstevel@tonic-gate 	if (len != NULL)
26907c478bd9Sstevel@tonic-gate 		*len = ptob(end - start);
26917c478bd9Sstevel@tonic-gate 	return ((void *)v);
26927c478bd9Sstevel@tonic-gate }
26937c478bd9Sstevel@tonic-gate 
2694ae115bc7Smrj #endif	/* __i386 */
2695