Home | History | Annotate | Download | only in os
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #include <sys/types.h>
     27 #include <sys/t_lock.h>
     28 #include <sys/param.h>
     29 #include <sys/sysmacros.h>
     30 #include <sys/signal.h>
     31 #include <sys/systm.h>
     32 #include <sys/user.h>
     33 #include <sys/mman.h>
     34 #include <sys/vm.h>
     35 #include <sys/conf.h>
     36 #include <sys/avintr.h>
     37 #include <sys/autoconf.h>
     38 #include <sys/disp.h>
     39 #include <sys/class.h>
     40 #include <sys/bitmap.h>
     41 
     42 #include <sys/privregs.h>
     43 
     44 #include <sys/proc.h>
     45 #include <sys/buf.h>
     46 #include <sys/kmem.h>
     47 #include <sys/mem.h>
     48 #include <sys/kstat.h>
     49 
     50 #include <sys/reboot.h>
     51 
     52 #include <sys/cred.h>
     53 #include <sys/vnode.h>
     54 #include <sys/file.h>
     55 
     56 #include <sys/procfs.h>
     57 
     58 #include <sys/vfs.h>
     59 #include <sys/cmn_err.h>
     60 #include <sys/utsname.h>
     61 #include <sys/debug.h>
     62 #include <sys/kdi.h>
     63 
     64 #include <sys/dumphdr.h>
     65 #include <sys/bootconf.h>
     66 #include <sys/varargs.h>
     67 #include <sys/promif.h>
     68 #include <sys/modctl.h>
     69 
     70 #include <sys/sunddi.h>
     71 #include <sys/sunndi.h>
     72 #include <sys/ndi_impldefs.h>
     73 #include <sys/ddidmareq.h>
     74 #include <sys/psw.h>
     75 #include <sys/regset.h>
     76 #include <sys/clock.h>
     77 #include <sys/pte.h>
     78 #include <sys/tss.h>
     79 #include <sys/stack.h>
     80 #include <sys/trap.h>
     81 #include <sys/fp.h>
     82 #include <vm/kboot_mmu.h>
     83 #include <vm/anon.h>
     84 #include <vm/as.h>
     85 #include <vm/page.h>
     86 #include <vm/seg.h>
     87 #include <vm/seg_dev.h>
     88 #include <vm/seg_kmem.h>
     89 #include <vm/seg_kpm.h>
     90 #include <vm/seg_map.h>
     91 #include <vm/seg_vn.h>
     92 #include <vm/seg_kp.h>
     93 #include <sys/memnode.h>
     94 #include <vm/vm_dep.h>
     95 #include <sys/thread.h>
     96 #include <sys/sysconf.h>
     97 #include <sys/vm_machparam.h>
     98 #include <sys/archsystm.h>
     99 #include <sys/machsystm.h>
    100 #include <vm/hat.h>
    101 #include <vm/hat_i86.h>
    102 #include <sys/pmem.h>
    103 #include <sys/smp_impldefs.h>
    104 #include <sys/x86_archext.h>
    105 #include <sys/cpuvar.h>
    106 #include <sys/segments.h>
    107 #include <sys/clconf.h>
    108 #include <sys/kobj.h>
    109 #include <sys/kobj_lex.h>
    110 #include <sys/cpc_impl.h>
    111 #include <sys/cpu_module.h>
    112 #include <sys/smbios.h>
    113 #include <sys/debug_info.h>
    114 #include <sys/bootinfo.h>
    115 #include <sys/ddi_timer.h>
    116 #include <sys/systeminfo.h>
    117 #include <sys/multiboot.h>
    118 
    119 #ifdef __xpv
    120 
    121 #include <sys/hypervisor.h>
    122 #include <sys/xen_mmu.h>
    123 #include <sys/evtchn_impl.h>
    124 #include <sys/gnttab.h>
    125 #include <sys/xpv_panic.h>
    126 #include <xen/sys/xenbus_comms.h>
    127 #include <xen/public/physdev.h>
    128 
    129 extern void xen_late_startup(void);
    130 
    131 struct xen_evt_data cpu0_evt_data;
    132 
    133 #endif /* __xpv */
    134 
    135 extern void progressbar_init(void);
    136 extern void progressbar_start(void);
    137 extern void brand_init(void);
    138 extern void pcf_init(void);
    139 extern void pg_init(void);
    140 
    141 extern int size_pse_array(pgcnt_t, int);
    142 
    143 #if defined(_SOFT_HOSTID)
    144 
    145 #include <sys/rtc.h>
    146 
    147 static int32_t set_soft_hostid(void);
    148 static char hostid_file[] = "/etc/hostid";
    149 
    150 #endif
    151 
    152 void *gfx_devinfo_list;
    153 
    154 /*
    155  * XXX make declaration below "static" when drivers no longer use this
    156  * interface.
    157  */
    158 extern caddr_t p0_va;	/* Virtual address for accessing physical page 0 */
    159 
    160 /*
    161  * segkp
    162  */
    163 extern int segkp_fromheap;
    164 
    165 static void kvm_init(void);
    166 static void startup_init(void);
    167 static void startup_memlist(void);
    168 static void startup_kmem(void);
    169 static void startup_modules(void);
    170 static void startup_vm(void);
    171 static void startup_end(void);
    172 static void layout_kernel_va(void);
    173 
    174 #if !defined(__xpv)
    175 void (*rootnex_iommu_init)(void) = NULL;
    176 #endif
    177 
    178 /*
    179  * Declare these as initialized data so we can patch them.
    180  */
    181 #ifdef __i386
    182 
    183 /*
    184  * Due to virtual address space limitations running in 32 bit mode, restrict
    185  * the amount of physical memory configured to a max of PHYSMEM pages (16g).
    186  *
    187  * If the physical max memory size of 64g were allowed to be configured, the
    188  * size of user virtual address space will be less than 1g. A limited user
    189  * address space greatly reduces the range of applications that can run.
    190  *
    191  * If more physical memory than PHYSMEM is required, users should preferably
    192  * run in 64 bit mode which has far looser virtual address space limitations.
    193  *
    194  * If 64 bit mode is not available (as in IA32) and/or more physical memory
    195  * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
    196  * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
    197  * should also be carefully tuned to balance out the need of the user
    198  * application while minimizing the risk of kernel heap exhaustion due to
    199  * kernelbase being set too high.
    200  */
    201 #define	PHYSMEM	0x400000
    202 
    203 #else /* __amd64 */
    204 
    205 /*
    206  * For now we can handle memory with physical addresses up to about
    207  * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
    208  * half the VA space for seg_kpm. When systems get bigger than 64TB this
    209  * code will need revisiting. There is an implicit assumption that there
    210  * are no *huge* holes in the physical address space too.
    211  */
    212 #define	TERABYTE		(1ul << 40)
    213 #define	PHYSMEM_MAX64		mmu_btop(64 * TERABYTE)
    214 #define	PHYSMEM			PHYSMEM_MAX64
    215 #define	AMD64_VA_HOLE_END	0xFFFF800000000000ul
    216 
    217 #endif /* __amd64 */
    218 
    219 pgcnt_t physmem = PHYSMEM;
    220 pgcnt_t obp_pages;	/* Memory used by PROM for its text and data */
    221 
    222 char *kobj_file_buf;
    223 int kobj_file_bufsize;	/* set in /etc/system */
    224 
    225 /* Global variables for MP support. Used in mp_startup */
    226 caddr_t	rm_platter_va = 0;
    227 uint32_t rm_platter_pa;
    228 
    229 int	auto_lpg_disable = 1;
    230 
    231 /*
    232  * Some CPUs have holes in the middle of the 64-bit virtual address range.
    233  */
    234 uintptr_t hole_start, hole_end;
    235 
    236 /*
    237  * kpm mapping window
    238  */
    239 caddr_t kpm_vbase;
    240 size_t  kpm_size;
    241 static int kpm_desired;
    242 #ifdef __amd64
    243 static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
    244 #endif
    245 
    246 /*
    247  * Configuration parameters set at boot time.
    248  */
    249 
    250 caddr_t econtig;		/* end of first block of contiguous kernel */
    251 
    252 struct bootops		*bootops = 0;	/* passed in from boot */
    253 struct bootops		**bootopsp;
    254 struct boot_syscalls	*sysp;		/* passed in from boot */
    255 
    256 char bootblock_fstype[16];
    257 
    258 char kern_bootargs[OBP_MAXPATHLEN];
    259 char kern_bootfile[OBP_MAXPATHLEN];
    260 
    261 /*
    262  * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
    263  * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
    264  * zio buffers from their own segment, otherwise they are allocated from the
    265  * heap.  The optimization of allocating zio buffers from their own segment is
    266  * only valid on 64-bit kernels.
    267  */
    268 #if defined(__amd64)
    269 int segzio_fromheap = 0;
    270 #else
    271 int segzio_fromheap = 1;
    272 #endif
    273 
    274 /*
    275  * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
    276  * depends on number of BOP_ALLOC calls made and requested size, memory size
    277  * combination and whether boot.bin memory needs to be freed.
    278  */
    279 #define	POSS_NEW_FRAGMENTS	12
    280 
    281 /*
    282  * VM data structures
    283  */
    284 long page_hashsz;		/* Size of page hash table (power of two) */
    285 struct page *pp_base;		/* Base of initial system page struct array */
    286 struct page **page_hash;	/* Page hash table */
    287 pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
    288 size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
    289 int pse_shift;			/* log2(pse_table_size) */
    290 struct seg ktextseg;		/* Segment used for kernel executable image */
    291 struct seg kvalloc;		/* Segment used for "valloc" mapping */
    292 struct seg kpseg;		/* Segment used for pageable kernel virt mem */
    293 struct seg kmapseg;		/* Segment used for generic kernel mappings */
    294 struct seg kdebugseg;		/* Segment used for the kernel debugger */
    295 
    296 struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
    297 static struct seg *segmap = &kmapseg;	/* easier to use name for in here */
    298 
    299 struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
    300 
    301 #if defined(__amd64)
    302 struct seg kvseg_core;		/* Segment used for the core heap */
    303 struct seg kpmseg;		/* Segment used for physical mapping */
    304 struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
    305 #else
    306 struct seg *segkpm = NULL;	/* Unused on IA32 */
    307 #endif
    308 
    309 caddr_t segkp_base;		/* Base address of segkp */
    310 caddr_t segzio_base;		/* Base address of segzio */
    311 #if defined(__amd64)
    312 pgcnt_t segkpsize = btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
    313 #else
    314 pgcnt_t segkpsize = 0;
    315 #endif
    316 pgcnt_t segziosize = 0;		/* size of zio segment in pages */
    317 
    318 /*
    319  * VA range available to the debugger
    320  */
    321 const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
    322 const size_t kdi_segdebugsize = SEGDEBUGSIZE;
    323 
    324 struct memseg *memseg_base;
    325 struct vnode unused_pages_vp;
    326 
    327 #define	FOURGB	0x100000000LL
    328 
    329 struct memlist *memlist;
    330 
    331 caddr_t s_text;		/* start of kernel text segment */
    332 caddr_t e_text;		/* end of kernel text segment */
    333 caddr_t s_data;		/* start of kernel data segment */
    334 caddr_t e_data;		/* end of kernel data segment */
    335 caddr_t modtext;	/* start of loadable module text reserved */
    336 caddr_t e_modtext;	/* end of loadable module text reserved */
    337 caddr_t moddata;	/* start of loadable module data reserved */
    338 caddr_t e_moddata;	/* end of loadable module data reserved */
    339 
    340 struct memlist *phys_install;	/* Total installed physical memory */
    341 struct memlist *phys_avail;	/* Total available physical memory */
    342 struct memlist *bios_rsvd;	/* Bios reserved memory */
    343 
    344 /*
    345  * kphysm_init returns the number of pages that were processed
    346  */
    347 static pgcnt_t kphysm_init(page_t *, pgcnt_t);
    348 
    349 #define	IO_PROP_SIZE	64	/* device property size */
    350 
    351 /*
    352  * a couple useful roundup macros
    353  */
    354 #define	ROUND_UP_PAGE(x)	\
    355 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
    356 #define	ROUND_UP_LPAGE(x)	\
    357 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
    358 #define	ROUND_UP_4MEG(x)	\
    359 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
    360 #define	ROUND_UP_TOPLEVEL(x)	\
    361 	((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
    362 
    363 /*
    364  *	32-bit Kernel's Virtual memory layout.
    365  *		+-----------------------+
    366  *		|			|
    367  * 0xFFC00000  -|-----------------------|- ARGSBASE
    368  *		|	debugger	|
    369  * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
    370  *		|      Kernel Data	|
    371  * 0xFEC00000  -|-----------------------|
    372  *              |      Kernel Text	|
    373  * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
    374  *		|---       GDT       ---|- GDT page (GDT_VA)
    375  *		|---    debug info   ---|- debug info (DEBUG_INFO_VA)
    376  *		|			|
    377  * 		|   page_t structures	|
    378  * 		|   memsegs, memlists, 	|
    379  * 		|   page hash, etc.	|
    380  * ---	       -|-----------------------|- ekernelheap, valloc_base (floating)
    381  *		|			|  (segkp is just an arena in the heap)
    382  *		|			|
    383  *		|	kvseg		|
    384  *		|			|
    385  *		|			|
    386  * ---         -|-----------------------|- kernelheap (floating)
    387  * 		|        Segkmap	|
    388  * 0xC3002000  -|-----------------------|- segmap_start (floating)
    389  *		|	Red Zone	|
    390  * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
    391  *		|			|			||
    392  *		|     Shared objects	|			\/
    393  *		|			|
    394  *		:			:
    395  *		|	user data	|
    396  *		|-----------------------|
    397  *		|	user text	|
    398  * 0x08048000  -|-----------------------|
    399  *		|	user stack	|
    400  *		:			:
    401  *		|	invalid		|
    402  * 0x00000000	+-----------------------+
    403  *
    404  *
    405  *		64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
    406  *			+-----------------------+
    407  *			|			|
    408  * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
    409  *			|	debugger (?)	|
    410  * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
    411  *			|      unused    	|
    412  *			+-----------------------+
    413  *			|      Kernel Data	|
    414  * 0xFFFFFFFF.FBC00000  |-----------------------|
    415  *			|      Kernel Text	|
    416  * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
    417  *			|---       GDT       ---|- GDT page (GDT_VA)
    418  *			|---    debug info   ---|- debug info (DEBUG_INFO_VA)
    419  *			|			|
    420  * 			|      Core heap	| (used for loadable modules)
    421  * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
    422  *			|	 Kernel		|
    423  *			|	  heap		|
    424  * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
    425  *			|	 segmap		|
    426  * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
    427  *			|    device mappings	|
    428  * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
    429  *			|	  segzio	|
    430  * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
    431  *			|	  segkp		|
    432  * ---                  |-----------------------|- segkp_base (floating)
    433  * 			|   page_t structures	|  valloc_base + valloc_sz
    434  * 			|   memsegs, memlists, 	|
    435  * 			|   page hash, etc.	|
    436  * 0xFFFFFF00.00000000  |-----------------------|- valloc_base (lower if > 1TB)
    437  *			|	 segkpm		|
    438  * 0xFFFFFE00.00000000  |-----------------------|
    439  *			|	Red Zone	|
    440  * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE (lower if > 1TB)
    441  *			|     User stack	|- User space memory
    442  * 			|			|
    443  * 			| shared objects, etc	|	(grows downwards)
    444  *			:			:
    445  * 			|			|
    446  * 0xFFFF8000.00000000  |-----------------------|
    447  * 			|			|
    448  * 			| VA Hole / unused	|
    449  * 			|			|
    450  * 0x00008000.00000000  |-----------------------|
    451  *			|			|
    452  *			|			|
    453  *			:			:
    454  *			|	user heap	|	(grows upwards)
    455  *			|			|
    456  *			|	user data	|
    457  *			|-----------------------|
    458  *			|	user text	|
    459  * 0x00000000.04000000  |-----------------------|
    460  *			|	invalid		|
    461  * 0x00000000.00000000	+-----------------------+
    462  *
    463  * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
    464  * kernel, except that userlimit is raised to 0xfe000000
    465  *
    466  * Floating values:
    467  *
    468  * valloc_base: start of the kernel's memory management/tracking data
    469  * structures.  This region contains page_t structures for
    470  * physical memory, memsegs, memlists, and the page hash.
    471  *
    472  * core_base: start of the kernel's "core" heap area on 64-bit systems.
    473  * This area is intended to be used for global data as well as for module
    474  * text/data that does not fit into the nucleus pages.  The core heap is
    475  * restricted to a 2GB range, allowing every address within it to be
    476  * accessed using rip-relative addressing
    477  *
    478  * ekernelheap: end of kernelheap and start of segmap.
    479  *
    480  * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
    481  * above a red zone that separates the user's address space from the
    482  * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
    483  *
    484  * segmap_start: start of segmap. The length of segmap can be modified
    485  * through eeprom. The default length is 16MB on 32-bit systems and 64MB
    486  * on 64-bit systems.
    487  *
    488  * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
    489  * decreased by 2X the size required for page_t.  This allows the kernel
    490  * heap to grow in size with physical memory.  With sizeof(page_t) == 80
    491  * bytes, the following shows the values of kernelbase and kernel heap
    492  * sizes for different memory configurations (assuming default segmap and
    493  * segkp sizes).
    494  *
    495  *	mem	size for	kernelbase	kernel heap
    496  *	size	page_t's			size
    497  *	----	---------	----------	-----------
    498  *	1gb	0x01400000	0xd1800000	684MB
    499  *	2gb	0x02800000	0xcf000000	704MB
    500  *	4gb	0x05000000	0xca000000	744MB
    501  *	6gb	0x07800000	0xc5000000	784MB
    502  *	8gb	0x0a000000	0xc0000000	824MB
    503  *	16gb	0x14000000	0xac000000	984MB
    504  *	32gb	0x28000000	0x84000000	1304MB
    505  *	64gb	0x50000000	0x34000000	1944MB (*)
    506  *
    507  * kernelbase is less than the abi minimum of 0xc0000000 for memory
    508  * configurations above 8gb.
    509  *
    510  * (*) support for memory configurations above 32gb will require manual tuning
    511  * of kernelbase to balance out the need of user applications.
    512  */
    513 
    514 /* real-time-clock initialization parameters */
    515 extern time_t process_rtc_config_file(void);
    516 
    517 uintptr_t	kernelbase;
    518 uintptr_t	postbootkernelbase;	/* not set till boot loader is gone */
    519 uintptr_t	eprom_kernelbase;
    520 size_t		segmapsize;
    521 uintptr_t	segmap_start;
    522 int		segmapfreelists;
    523 pgcnt_t		npages;
    524 pgcnt_t		orig_npages;
    525 size_t		core_size;		/* size of "core" heap */
    526 uintptr_t	core_base;		/* base address of "core" heap */
    527 
    528 /*
    529  * List of bootstrap pages. We mark these as allocated in startup.
    530  * release_bootstrap() will free them when we're completely done with
    531  * the bootstrap.
    532  */
    533 static page_t *bootpages;
    534 
    535 /*
    536  * boot time pages that have a vnode from the ramdisk will keep that forever.
    537  */
    538 static page_t *rd_pages;
    539 
    540 /*
    541  * Lower 64K
    542  */
    543 static page_t *lower_pages = NULL;
    544 static int lower_pages_count = 0;
    545 
    546 struct system_hardware system_hardware;
    547 
    548 /*
    549  * Enable some debugging messages concerning memory usage...
    550  */
    551 static void
    552 print_memlist(char *title, struct memlist *mp)
    553 {
    554 	prom_printf("MEMLIST: %s:\n", title);
    555 	while (mp != NULL)  {
    556 		prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
    557 		    mp->address, mp->size);
    558 		mp = mp->next;
    559 	}
    560 }
    561 
    562 /*
    563  * XX64 need a comment here.. are these just default values, surely
    564  * we read the "cpuid" type information to figure this out.
    565  */
    566 int	l2cache_sz = 0x80000;
    567 int	l2cache_linesz = 0x40;
    568 int	l2cache_assoc = 1;
    569 
    570 static size_t	textrepl_min_gb = 10;
    571 
    572 /*
    573  * on 64 bit we use a predifined VA range for mapping devices in the kernel
    574  * on 32 bit the mappings are intermixed in the heap, so we use a bit map
    575  */
    576 #ifdef __amd64
    577 
    578 vmem_t		*device_arena;
    579 uintptr_t	toxic_addr = (uintptr_t)NULL;
    580 size_t		toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
    581 
    582 #else	/* __i386 */
    583 
    584 ulong_t		*toxic_bit_map;	/* one bit for each 4k of VA in heap_arena */
    585 size_t		toxic_bit_map_len = 0;	/* in bits */
    586 
    587 #endif	/* __i386 */
    588 
    589 /*
    590  * Simple boot time debug facilities
    591  */
    592 static char *prm_dbg_str[] = {
    593 	"%s:%d: '%s' is 0x%x\n",
    594 	"%s:%d: '%s' is 0x%llx\n"
    595 };
    596 
    597 int prom_debug;
    598 
    599 #define	PRM_DEBUG(q)	if (prom_debug) 	\
    600 	prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
    601 #define	PRM_POINT(q)	if (prom_debug) 	\
    602 	prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
    603 
    604 /*
    605  * This structure is used to keep track of the intial allocations
    606  * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
    607  * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
    608  */
    609 #define	NUM_ALLOCATIONS 8
    610 int num_allocations = 0;
    611 struct {
    612 	void **al_ptr;
    613 	size_t al_size;
    614 } allocations[NUM_ALLOCATIONS];
    615 size_t valloc_sz = 0;
    616 uintptr_t valloc_base;
    617 
    618 #define	ADD_TO_ALLOCATIONS(ptr, size) {					\
    619 		size = ROUND_UP_PAGE(size);		 		\
    620 		if (num_allocations == NUM_ALLOCATIONS)			\
    621 			panic("too many ADD_TO_ALLOCATIONS()");		\
    622 		allocations[num_allocations].al_ptr = (void**)&ptr;	\
    623 		allocations[num_allocations].al_size = size;		\
    624 		valloc_sz += size;					\
    625 		++num_allocations;				 	\
    626 	}
    627 
    628 /*
    629  * Allocate all the initial memory needed by the page allocator.
    630  */
    631 static void
    632 perform_allocations(void)
    633 {
    634 	caddr_t mem;
    635 	int i;
    636 	int valloc_align;
    637 
    638 	PRM_DEBUG(valloc_base);
    639 	PRM_DEBUG(valloc_sz);
    640 	valloc_align = mmu.level_size[mmu.max_page_level > 0];
    641 	mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
    642 	if (mem != (caddr_t)valloc_base)
    643 		panic("BOP_ALLOC() failed");
    644 	bzero(mem, valloc_sz);
    645 	for (i = 0; i < num_allocations; ++i) {
    646 		*allocations[i].al_ptr = (void *)mem;
    647 		mem += allocations[i].al_size;
    648 	}
    649 }
    650 
    651 /*
    652  * Our world looks like this at startup time.
    653  *
    654  * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
    655  * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
    656  * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
    657  * addresses are fixed in the binary at link time.
    658  *
    659  * On the text page:
    660  * unix/genunix/krtld/module text loads.
    661  *
    662  * On the data page:
    663  * unix/genunix/krtld/module data loads.
    664  *
    665  * Machine-dependent startup code
    666  */
    667 void
    668 startup(void)
    669 {
    670 #if !defined(__xpv)
    671 	extern void startup_pci_bios(void);
    672 	extern int post_fastreboot;
    673 #endif
    674 	extern cpuset_t cpu_ready_set;
    675 
    676 	/*
    677 	 * Make sure that nobody tries to use sekpm until we have
    678 	 * initialized it properly.
    679 	 */
    680 #if defined(__amd64)
    681 	kpm_desired = 1;
    682 #endif
    683 	kpm_enable = 0;
    684 	CPUSET_ONLY(cpu_ready_set, 0);	/* cpu 0 is boot cpu */
    685 
    686 #if defined(__xpv)	/* XXPV fix me! */
    687 	{
    688 		extern int segvn_use_regions;
    689 		segvn_use_regions = 0;
    690 	}
    691 #endif
    692 	progressbar_init();
    693 	startup_init();
    694 #if defined(__xpv)
    695 	startup_xen_version();
    696 #endif
    697 	startup_memlist();
    698 	startup_kmem();
    699 	startup_vm();
    700 #if !defined(__xpv)
    701 	if (!post_fastreboot)
    702 		startup_pci_bios();
    703 #endif
    704 #if defined(__xpv)
    705 	startup_xen_mca();
    706 #endif
    707 	startup_modules();
    708 
    709 	startup_end();
    710 	progressbar_start();
    711 }
    712 
    713 static void
    714 startup_init()
    715 {
    716 	PRM_POINT("startup_init() starting...");
    717 
    718 	/*
    719 	 * Complete the extraction of cpuid data
    720 	 */
    721 	cpuid_pass2(CPU);
    722 
    723 	(void) check_boot_version(BOP_GETVERSION(bootops));
    724 
    725 	/*
    726 	 * Check for prom_debug in boot environment
    727 	 */
    728 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
    729 		++prom_debug;
    730 		PRM_POINT("prom_debug found in boot enviroment");
    731 	}
    732 
    733 	/*
    734 	 * Collect node, cpu and memory configuration information.
    735 	 */
    736 	get_system_configuration();
    737 
    738 	/*
    739 	 * Halt if this is an unsupported processor.
    740 	 */
    741 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
    742 		printf("\n486 processor (\"%s\") detected.\n",
    743 		    CPU->cpu_brandstr);
    744 		halt("This processor is not supported by this release "
    745 		    "of Solaris.");
    746 	}
    747 
    748 	PRM_POINT("startup_init() done");
    749 }
    750 
    751 /*
    752  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
    753  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
    754  * also filters out physical page zero.  There is some reliance on the
    755  * boot loader allocating only a few contiguous physical memory chunks.
    756  */
    757 static void
    758 avail_filter(uint64_t *addr, uint64_t *size)
    759 {
    760 	uintptr_t va;
    761 	uintptr_t next_va;
    762 	pfn_t pfn;
    763 	uint64_t pfn_addr;
    764 	uint64_t pfn_eaddr;
    765 	uint_t prot;
    766 	size_t len;
    767 	uint_t change;
    768 
    769 	if (prom_debug)
    770 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
    771 		    *addr, *size);
    772 
    773 	/*
    774 	 * page zero is required for BIOS.. never make it available
    775 	 */
    776 	if (*addr == 0) {
    777 		*addr += MMU_PAGESIZE;
    778 		*size -= MMU_PAGESIZE;
    779 	}
    780 
    781 	/*
    782 	 * First we trim from the front of the range. Since kbm_probe()
    783 	 * walks ranges in virtual order, but addr/size are physical, we need
    784 	 * to the list until no changes are seen.  This deals with the case
    785 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
    786 	 * but w < v.
    787 	 */
    788 	do {
    789 		change = 0;
    790 		for (va = KERNEL_TEXT;
    791 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
    792 		    va = next_va) {
    793 
    794 			next_va = va + len;
    795 			pfn_addr = pfn_to_pa(pfn);
    796 			pfn_eaddr = pfn_addr + len;
    797 
    798 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
    799 				change = 1;
    800 				while (*size > 0 && len > 0) {
    801 					*addr += MMU_PAGESIZE;
    802 					*size -= MMU_PAGESIZE;
    803 					len -= MMU_PAGESIZE;
    804 				}
    805 			}
    806 		}
    807 		if (change && prom_debug)
    808 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
    809 			    *addr, *size);
    810 	} while (change);
    811 
    812 	/*
    813 	 * Trim pages from the end of the range.
    814 	 */
    815 	for (va = KERNEL_TEXT;
    816 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
    817 	    va = next_va) {
    818 
    819 		next_va = va + len;
    820 		pfn_addr = pfn_to_pa(pfn);
    821 
    822 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
    823 			*size = pfn_addr - *addr;
    824 	}
    825 
    826 	if (prom_debug)
    827 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
    828 		    *addr, *size);
    829 }
    830 
    831 static void
    832 kpm_init()
    833 {
    834 	struct segkpm_crargs b;
    835 
    836 	/*
    837 	 * These variables were all designed for sfmmu in which segkpm is
    838 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
    839 	 * might use 2+ page sizes on a single machine, so none of these
    840 	 * variables have a single correct value.  They are set up as if we
    841 	 * always use a 4KB pagesize, which should do no harm.  In the long
    842 	 * run, we should get rid of KPM's assumption that only a single
    843 	 * pagesize is used.
    844 	 */
    845 	kpm_pgshft = MMU_PAGESHIFT;
    846 	kpm_pgsz =  MMU_PAGESIZE;
    847 	kpm_pgoff = MMU_PAGEOFFSET;
    848 	kpmp2pshft = 0;
    849 	kpmpnpgs = 1;
    850 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
    851 
    852 	PRM_POINT("about to create segkpm");
    853 	rw_enter(&kas.a_lock, RW_WRITER);
    854 
    855 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
    856 		panic("cannot attach segkpm");
    857 
    858 	b.prot = PROT_READ | PROT_WRITE;
    859 	b.nvcolors = 1;
    860 
    861 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
    862 		panic("segkpm_create segkpm");
    863 
    864 	rw_exit(&kas.a_lock);
    865 }
    866 
    867 /*
    868  * The debug info page provides enough information to allow external
    869  * inspectors (e.g. when running under a hypervisor) to bootstrap
    870  * themselves into allowing full-blown kernel debugging.
    871  */
    872 static void
    873 init_debug_info(void)
    874 {
    875 	caddr_t mem;
    876 	debug_info_t *di;
    877 
    878 #ifndef __lint
    879 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
    880 #endif
    881 
    882 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
    883 	    MMU_PAGESIZE);
    884 
    885 	if (mem != (caddr_t)DEBUG_INFO_VA)
    886 		panic("BOP_ALLOC() failed");
    887 	bzero(mem, MMU_PAGESIZE);
    888 
    889 	di = (debug_info_t *)mem;
    890 
    891 	di->di_magic = DEBUG_INFO_MAGIC;
    892 	di->di_version = DEBUG_INFO_VERSION;
    893 	di->di_modules = (uintptr_t)&modules;
    894 	di->di_s_text = (uintptr_t)s_text;
    895 	di->di_e_text = (uintptr_t)e_text;
    896 	di->di_s_data = (uintptr_t)s_data;
    897 	di->di_e_data = (uintptr_t)e_data;
    898 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
    899 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
    900 }
    901 
    902 /*
    903  * Build the memlists and other kernel essential memory system data structures.
    904  * This is everything at valloc_base.
    905  */
    906 static void
    907 startup_memlist(void)
    908 {
    909 	size_t memlist_sz;
    910 	size_t memseg_sz;
    911 	size_t pagehash_sz;
    912 	size_t pp_sz;
    913 	uintptr_t va;
    914 	size_t len;
    915 	uint_t prot;
    916 	pfn_t pfn;
    917 	int memblocks;
    918 	pfn_t rsvd_high_pfn;
    919 	pgcnt_t rsvd_pgcnt;
    920 	size_t rsvdmemlist_sz;
    921 	int rsvdmemblocks;
    922 	caddr_t pagecolor_mem;
    923 	size_t pagecolor_memsz;
    924 	caddr_t page_ctrs_mem;
    925 	size_t page_ctrs_size;
    926 	size_t pse_table_alloc_size;
    927 	struct memlist *current;
    928 	extern void startup_build_mem_nodes(struct memlist *);
    929 
    930 	/* XX64 fix these - they should be in include files */
    931 	extern size_t page_coloring_init(uint_t, int, int);
    932 	extern void page_coloring_setup(caddr_t);
    933 
    934 	PRM_POINT("startup_memlist() starting...");
    935 
    936 	/*
    937 	 * Use leftover large page nucleus text/data space for loadable modules.
    938 	 * Use at most MODTEXT/MODDATA.
    939 	 */
    940 	len = kbm_nucleus_size;
    941 	ASSERT(len > MMU_PAGESIZE);
    942 
    943 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
    944 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
    945 	if (e_moddata - moddata > MODDATA)
    946 		e_moddata = moddata + MODDATA;
    947 
    948 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
    949 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
    950 	if (e_modtext - modtext > MODTEXT)
    951 		e_modtext = modtext + MODTEXT;
    952 
    953 	econtig = e_moddata;
    954 
    955 	PRM_DEBUG(modtext);
    956 	PRM_DEBUG(e_modtext);
    957 	PRM_DEBUG(moddata);
    958 	PRM_DEBUG(e_moddata);
    959 	PRM_DEBUG(econtig);
    960 
    961 	/*
    962 	 * Examine the boot loader physical memory map to find out:
    963 	 * - total memory in system - physinstalled
    964 	 * - the max physical address - physmax
    965 	 * - the number of discontiguous segments of memory.
    966 	 */
    967 	if (prom_debug)
    968 		print_memlist("boot physinstalled",
    969 		    bootops->boot_mem->physinstalled);
    970 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
    971 	    &physinstalled, &memblocks);
    972 	PRM_DEBUG(physmax);
    973 	PRM_DEBUG(physinstalled);
    974 	PRM_DEBUG(memblocks);
    975 
    976 	/*
    977 	 * Examine the bios reserved memory to find out:
    978 	 * - the number of discontiguous segments of memory.
    979 	 */
    980 	if (prom_debug)
    981 		print_memlist("boot reserved mem",
    982 		    bootops->boot_mem->rsvdmem);
    983 	installed_top_size(bootops->boot_mem->rsvdmem, &rsvd_high_pfn,
    984 	    &rsvd_pgcnt, &rsvdmemblocks);
    985 	PRM_DEBUG(rsvd_high_pfn);
    986 	PRM_DEBUG(rsvd_pgcnt);
    987 	PRM_DEBUG(rsvdmemblocks);
    988 
    989 	/*
    990 	 * Initialize hat's mmu parameters.
    991 	 * Check for enforce-prot-exec in boot environment. It's used to
    992 	 * enable/disable support for the page table entry NX bit.
    993 	 * The default is to enforce PROT_EXEC on processors that support NX.
    994 	 * Boot seems to round up the "len", but 8 seems to be big enough.
    995 	 */
    996 	mmu_init();
    997 
    998 #ifdef	__i386
    999 	/*
   1000 	 * physmax is lowered if there is more memory than can be
   1001 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
   1002 	 */
   1003 	if (mmu.pae_hat) {
   1004 		if (PFN_ABOVE64G(physmax)) {
   1005 			physinstalled -= (physmax - (PFN_64G - 1));
   1006 			physmax = PFN_64G - 1;
   1007 		}
   1008 	} else {
   1009 		if (PFN_ABOVE4G(physmax)) {
   1010 			physinstalled -= (physmax - (PFN_4G - 1));
   1011 			physmax = PFN_4G - 1;
   1012 		}
   1013 	}
   1014 #endif
   1015 
   1016 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
   1017 
   1018 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
   1019 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
   1020 		char value[8];
   1021 
   1022 		if (len < 8)
   1023 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
   1024 		else
   1025 			(void) strcpy(value, "");
   1026 		if (strcmp(value, "off") == 0)
   1027 			mmu.pt_nx = 0;
   1028 	}
   1029 	PRM_DEBUG(mmu.pt_nx);
   1030 
   1031 	/*
   1032 	 * We will need page_t's for every page in the system, except for
   1033 	 * memory mapped at or above above the start of the kernel text segment.
   1034 	 *
   1035 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
   1036 	 */
   1037 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
   1038 	obp_pages = 0;
   1039 	va = KERNEL_TEXT;
   1040 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
   1041 		npages -= len >> MMU_PAGESHIFT;
   1042 		if (va >= (uintptr_t)e_moddata)
   1043 			obp_pages += len >> MMU_PAGESHIFT;
   1044 		va += len;
   1045 	}
   1046 	PRM_DEBUG(npages);
   1047 	PRM_DEBUG(obp_pages);
   1048 
   1049 	/*
   1050 	 * If physmem is patched to be non-zero, use it instead of the computed
   1051 	 * value unless it is larger than the actual amount of memory on hand.
   1052 	 */
   1053 	if (physmem == 0 || physmem > npages) {
   1054 		physmem = npages;
   1055 	} else if (physmem < npages) {
   1056 		orig_npages = npages;
   1057 		npages = physmem;
   1058 	}
   1059 	PRM_DEBUG(physmem);
   1060 
   1061 	/*
   1062 	 * We now compute the sizes of all the  initial allocations for
   1063 	 * structures the kernel needs in order do kmem_alloc(). These
   1064 	 * include:
   1065 	 *	memsegs
   1066 	 *	memlists
   1067 	 *	page hash table
   1068 	 *	page_t's
   1069 	 *	page coloring data structs
   1070 	 */
   1071 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
   1072 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
   1073 	PRM_DEBUG(memseg_sz);
   1074 
   1075 	/*
   1076 	 * Reserve space for memlists. There's no real good way to know exactly
   1077 	 * how much room we'll need, but this should be a good upper bound.
   1078 	 */
   1079 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
   1080 	    (memblocks + POSS_NEW_FRAGMENTS));
   1081 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
   1082 	PRM_DEBUG(memlist_sz);
   1083 
   1084 	/*
   1085 	 * Reserve space for bios reserved memlists.
   1086 	 */
   1087 	rsvdmemlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
   1088 	    (rsvdmemblocks + POSS_NEW_FRAGMENTS));
   1089 	ADD_TO_ALLOCATIONS(bios_rsvd, rsvdmemlist_sz);
   1090 	PRM_DEBUG(rsvdmemlist_sz);
   1091 
   1092 	/*
   1093 	 * The page structure hash table size is a power of 2
   1094 	 * such that the average hash chain length is PAGE_HASHAVELEN.
   1095 	 */
   1096 	page_hashsz = npages / PAGE_HASHAVELEN;
   1097 	page_hashsz = 1 << highbit(page_hashsz);
   1098 	pagehash_sz = sizeof (struct page *) * page_hashsz;
   1099 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
   1100 	PRM_DEBUG(pagehash_sz);
   1101 
   1102 	/*
   1103 	 * Set aside room for the page structures themselves.
   1104 	 */
   1105 	PRM_DEBUG(npages);
   1106 	pp_sz = sizeof (struct page) * npages;
   1107 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
   1108 	PRM_DEBUG(pp_sz);
   1109 
   1110 	/*
   1111 	 * determine l2 cache info and memory size for page coloring
   1112 	 */
   1113 	(void) getl2cacheinfo(CPU,
   1114 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
   1115 	pagecolor_memsz =
   1116 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
   1117 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
   1118 	PRM_DEBUG(pagecolor_memsz);
   1119 
   1120 	page_ctrs_size = page_ctrs_sz();
   1121 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
   1122 	PRM_DEBUG(page_ctrs_size);
   1123 
   1124 	/*
   1125 	 * Allocate the array that protects pp->p_selock.
   1126 	 */
   1127 	pse_shift = size_pse_array(physmem, max_ncpus);
   1128 	pse_table_size = 1 << pse_shift;
   1129 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
   1130 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
   1131 
   1132 #if defined(__amd64)
   1133 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
   1134 	valloc_base = VALLOC_BASE;
   1135 
   1136 	/*
   1137 	 * The default values of VALLOC_BASE and SEGKPM_BASE should work
   1138 	 * for values of physmax up to 1 Terabyte. They need adjusting when
   1139 	 * memory is at addresses above 1 TB.
   1140 	 */
   1141 	if (physmax + 1 > mmu_btop(TERABYTE)) {
   1142 		uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
   1143 
   1144 		/* Round to largest possible pagesize for now */
   1145 		kpm_resv_amount = P2ROUNDUP(kpm_resv_amount, ONE_GIG);
   1146 
   1147 		segkpm_base = -(2 * kpm_resv_amount); /* down from top VA */
   1148 
   1149 		/* make sure we leave some space for user apps above hole */
   1150 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
   1151 		if (segkpm_base > SEGKPM_BASE)
   1152 			segkpm_base = SEGKPM_BASE;
   1153 		PRM_DEBUG(segkpm_base);
   1154 
   1155 		valloc_base = segkpm_base + kpm_resv_amount;
   1156 		PRM_DEBUG(valloc_base);
   1157 	}
   1158 #else	/* __i386 */
   1159 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
   1160 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
   1161 	PRM_DEBUG(valloc_base);
   1162 #endif	/* __i386 */
   1163 
   1164 	/*
   1165 	 * do all the initial allocations
   1166 	 */
   1167 	perform_allocations();
   1168 
   1169 	/*
   1170 	 * Build phys_install and phys_avail in kernel memspace.
   1171 	 * - phys_install should be all memory in the system.
   1172 	 * - phys_avail is phys_install minus any memory mapped before this
   1173 	 *    point above KERNEL_TEXT.
   1174 	 */
   1175 	current = phys_install = memlist;
   1176 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
   1177 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
   1178 		panic("physinstalled was too big!");
   1179 	if (prom_debug)
   1180 		print_memlist("phys_install", phys_install);
   1181 
   1182 	phys_avail = current;
   1183 	PRM_POINT("Building phys_avail:\n");
   1184 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
   1185 	    avail_filter);
   1186 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
   1187 		panic("physavail was too big!");
   1188 	if (prom_debug)
   1189 		print_memlist("phys_avail", phys_avail);
   1190 
   1191 	/*
   1192 	 * Build bios reserved memspace
   1193 	 */
   1194 	current = bios_rsvd;
   1195 	copy_memlist_filter(bootops->boot_mem->rsvdmem, &current, NULL);
   1196 	if ((caddr_t)current > (caddr_t)bios_rsvd + rsvdmemlist_sz)
   1197 		panic("bios_rsvd was too big!");
   1198 	if (prom_debug)
   1199 		print_memlist("bios_rsvd", bios_rsvd);
   1200 
   1201 	/*
   1202 	 * setup page coloring
   1203 	 */
   1204 	page_coloring_setup(pagecolor_mem);
   1205 	page_lock_init();	/* currently a no-op */
   1206 
   1207 	/*
   1208 	 * free page list counters
   1209 	 */
   1210 	(void) page_ctrs_alloc(page_ctrs_mem);
   1211 
   1212 	/*
   1213 	 * Size the pcf array based on the number of cpus in the box at
   1214 	 * boot time.
   1215 	 */
   1216 
   1217 	pcf_init();
   1218 
   1219 	/*
   1220 	 * Initialize the page structures from the memory lists.
   1221 	 */
   1222 	availrmem_initial = availrmem = freemem = 0;
   1223 	PRM_POINT("Calling kphysm_init()...");
   1224 	npages = kphysm_init(pp_base, npages);
   1225 	PRM_POINT("kphysm_init() done");
   1226 	PRM_DEBUG(npages);
   1227 
   1228 	init_debug_info();
   1229 
   1230 	/*
   1231 	 * Now that page_t's have been initialized, remove all the
   1232 	 * initial allocation pages from the kernel free page lists.
   1233 	 */
   1234 	boot_mapin((caddr_t)valloc_base, valloc_sz);
   1235 	boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
   1236 	PRM_POINT("startup_memlist() done");
   1237 
   1238 	PRM_DEBUG(valloc_sz);
   1239 
   1240 #if defined(__amd64)
   1241 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
   1242 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
   1243 		extern size_t textrepl_size_thresh;
   1244 		textrepl_size_thresh = (16 << 20) - 1;
   1245 	}
   1246 #endif
   1247 }
   1248 
   1249 /*
   1250  * Layout the kernel's part of address space and initialize kmem allocator.
   1251  */
   1252 static void
   1253 startup_kmem(void)
   1254 {
   1255 	extern void page_set_colorequiv_arr(void);
   1256 	const char *fmt = "?features: %b\n";
   1257 
   1258 	PRM_POINT("startup_kmem() starting...");
   1259 
   1260 #if defined(__amd64)
   1261 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
   1262 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
   1263 		    "systems.");
   1264 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
   1265 	core_base = (uintptr_t)COREHEAP_BASE;
   1266 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
   1267 #else	/* __i386 */
   1268 	/*
   1269 	 * We configure kernelbase based on:
   1270 	 *
   1271 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
   1272 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
   1273 	 *
   1274 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
   1275 	 *    On large memory systems we must lower kernelbase to allow
   1276 	 *    enough room for page_t's for all of memory.
   1277 	 *
   1278 	 * The value set here, might be changed a little later.
   1279 	 */
   1280 	if (eprom_kernelbase) {
   1281 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
   1282 		if (kernelbase > KERNELBASE_MAX)
   1283 			kernelbase = KERNELBASE_MAX;
   1284 	} else {
   1285 		kernelbase = (uintptr_t)KERNELBASE;
   1286 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
   1287 	}
   1288 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
   1289 	core_base = valloc_base;
   1290 	core_size = 0;
   1291 #endif	/* __i386 */
   1292 
   1293 	PRM_DEBUG(core_base);
   1294 	PRM_DEBUG(core_size);
   1295 	PRM_DEBUG(kernelbase);
   1296 
   1297 #if defined(__i386)
   1298 	segkp_fromheap = 1;
   1299 #endif	/* __i386 */
   1300 
   1301 	ekernelheap = (char *)core_base;
   1302 	PRM_DEBUG(ekernelheap);
   1303 
   1304 	/*
   1305 	 * Now that we know the real value of kernelbase,
   1306 	 * update variables that were initialized with a value of
   1307 	 * KERNELBASE (in common/conf/param.c).
   1308 	 *
   1309 	 * XXX	The problem with this sort of hackery is that the
   1310 	 *	compiler just may feel like putting the const declarations
   1311 	 *	(in param.c) into the .text section.  Perhaps they should
   1312 	 *	just be declared as variables there?
   1313 	 */
   1314 
   1315 	*(uintptr_t *)&_kernelbase = kernelbase;
   1316 	*(uintptr_t *)&_userlimit = kernelbase;
   1317 #if defined(__amd64)
   1318 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
   1319 #else
   1320 	*(uintptr_t *)&_userlimit32 = _userlimit;
   1321 #endif
   1322 	PRM_DEBUG(_kernelbase);
   1323 	PRM_DEBUG(_userlimit);
   1324 	PRM_DEBUG(_userlimit32);
   1325 
   1326 	layout_kernel_va();
   1327 
   1328 #if defined(__i386)
   1329 	/*
   1330 	 * If segmap is too large we can push the bottom of the kernel heap
   1331 	 * higher than the base.  Or worse, it could exceed the top of the
   1332 	 * VA space entirely, causing it to wrap around.
   1333 	 */
   1334 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
   1335 		panic("too little address space available for kernelheap,"
   1336 		    " use eeprom for lower kernelbase or smaller segmapsize");
   1337 #endif	/* __i386 */
   1338 
   1339 	/*
   1340 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
   1341 	 */
   1342 	kernelheap_init(kernelheap, ekernelheap,
   1343 	    kernelheap + MMU_PAGESIZE,
   1344 	    (void *)core_base, (void *)(core_base + core_size));
   1345 
   1346 #if defined(__xpv)
   1347 	/*
   1348 	 * Link pending events struct into cpu struct
   1349 	 */
   1350 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
   1351 #endif
   1352 	/*
   1353 	 * Initialize kernel memory allocator.
   1354 	 */
   1355 	kmem_init();
   1356 
   1357 	/*
   1358 	 * Factor in colorequiv to check additional 'equivalent' bins
   1359 	 */
   1360 	page_set_colorequiv_arr();
   1361 
   1362 	/*
   1363 	 * print this out early so that we know what's going on
   1364 	 */
   1365 	cmn_err(CE_CONT, fmt, x86_feature, FMT_X86_FEATURE);
   1366 
   1367 	/*
   1368 	 * Initialize bp_mapin().
   1369 	 */
   1370 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
   1371 
   1372 	/*
   1373 	 * orig_npages is non-zero if physmem has been configured for less
   1374 	 * than the available memory.
   1375 	 */
   1376 	if (orig_npages) {
   1377 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
   1378 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
   1379 		    npages, orig_npages);
   1380 	}
   1381 #if defined(__i386)
   1382 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
   1383 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
   1384 		    "System using 0x%lx",
   1385 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
   1386 #endif
   1387 
   1388 #ifdef	KERNELBASE_ABI_MIN
   1389 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
   1390 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
   1391 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
   1392 	}
   1393 #endif
   1394 
   1395 #ifdef __xpv
   1396 	/*
   1397 	 * Some of the xen start information has to be relocated up
   1398 	 * into the kernel's permanent address space.
   1399 	 */
   1400 	PRM_POINT("calling xen_relocate_start_info()");
   1401 	xen_relocate_start_info();
   1402 	PRM_POINT("xen_relocate_start_info() done");
   1403 
   1404 	/*
   1405 	 * (Update the vcpu pointer in our cpu structure to point into
   1406 	 * the relocated shared info.)
   1407 	 */
   1408 	CPU->cpu_m.mcpu_vcpu_info =
   1409 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
   1410 #endif
   1411 
   1412 	PRM_POINT("startup_kmem() done");
   1413 }
   1414 
   1415 #ifndef __xpv
   1416 /*
   1417  * If we have detected that we are running in an HVM environment, we need
   1418  * to prepend the PV driver directory to the module search path.
   1419  */
   1420 #define	HVM_MOD_DIR "/platform/i86hvm/kernel"
   1421 static void
   1422 update_default_path()
   1423 {
   1424 	char *current, *newpath;
   1425 	int newlen;
   1426 
   1427 	/*
   1428 	 * We are about to resync with krtld.  krtld will reset its
   1429 	 * internal module search path iff Solaris has set default_path.
   1430 	 * We want to be sure we're prepending this new directory to the
   1431 	 * right search path.
   1432 	 */
   1433 	current = (default_path == NULL) ? kobj_module_path : default_path;
   1434 
   1435 	newlen = strlen(HVM_MOD_DIR) + strlen(current) + 1;
   1436 	newpath = kmem_alloc(newlen, KM_SLEEP);
   1437 	(void) strcpy(newpath, HVM_MOD_DIR);
   1438 	(void) strcat(newpath, " ");
   1439 	(void) strcat(newpath, current);
   1440 
   1441 	default_path = newpath;
   1442 }
   1443 #endif
   1444 
   1445 static void
   1446 startup_modules(void)
   1447 {
   1448 	int cnt;
   1449 	extern void prom_setup(void);
   1450 	int32_t v, h;
   1451 	char d[11];
   1452 	char *cp;
   1453 	cmi_hdl_t hdl;
   1454 
   1455 	PRM_POINT("startup_modules() starting...");
   1456 
   1457 #ifndef __xpv
   1458 	/*
   1459 	 * Initialize ten-micro second timer so that drivers will
   1460 	 * not get short changed in their init phase. This was
   1461 	 * not getting called until clkinit which, on fast cpu's
   1462 	 * caused the drv_usecwait to be way too short.
   1463 	 */
   1464 	microfind();
   1465 
   1466 	if (get_hwenv() == HW_XEN_HVM)
   1467 		update_default_path();
   1468 #endif
   1469 
   1470 	/*
   1471 	 * Read the GMT lag from /etc/rtc_config.
   1472 	 */
   1473 	sgmtl(process_rtc_config_file());
   1474 
   1475 	/*
   1476 	 * Calculate default settings of system parameters based upon
   1477 	 * maxusers, yet allow to be overridden via the /etc/system file.
   1478 	 */
   1479 	param_calc(0);
   1480 
   1481 	mod_setup();
   1482 
   1483 	/*
   1484 	 * Initialize system parameters.
   1485 	 */
   1486 	param_init();
   1487 
   1488 	/*
   1489 	 * Initialize the default brands
   1490 	 */
   1491 	brand_init();
   1492 
   1493 	/*
   1494 	 * maxmem is the amount of physical memory we're playing with.
   1495 	 */
   1496 	maxmem = physmem;
   1497 
   1498 	/*
   1499 	 * Initialize segment management stuff.
   1500 	 */
   1501 	seg_init();
   1502 
   1503 	if (modload("fs", "specfs") == -1)
   1504 		halt("Can't load specfs");
   1505 
   1506 	if (modload("fs", "devfs") == -1)
   1507 		halt("Can't load devfs");
   1508 
   1509 	if (modload("fs", "dev") == -1)
   1510 		halt("Can't load dev");
   1511 
   1512 	(void) modloadonly("sys", "lbl_edition");
   1513 
   1514 	dispinit();
   1515 
   1516 	/*
   1517 	 * This is needed here to initialize hw_serial[] for cluster booting.
   1518 	 */
   1519 	if ((h = set_soft_hostid()) == HW_INVALID_HOSTID) {
   1520 		cmn_err(CE_WARN, "Unable to set hostid");
   1521 	} else {
   1522 		for (v = h, cnt = 0; cnt < 10; cnt++) {
   1523 			d[cnt] = (char)(v % 10);
   1524 			v /= 10;
   1525 			if (v == 0)
   1526 				break;
   1527 		}
   1528 		for (cp = hw_serial; cnt >= 0; cnt--)
   1529 			*cp++ = d[cnt] + '0';
   1530 		*cp = 0;
   1531 	}
   1532 
   1533 	/* Read cluster configuration data. */
   1534 	clconf_init();
   1535 
   1536 #if defined(__xpv)
   1537 	(void) ec_init();
   1538 	gnttab_init();
   1539 	(void) xs_early_init();
   1540 #endif /* __xpv */
   1541 
   1542 	/*
   1543 	 * Create a kernel device tree. First, create rootnex and
   1544 	 * then invoke bus specific code to probe devices.
   1545 	 */
   1546 	setup_ddi();
   1547 
   1548 #ifdef __xpv
   1549 	if (DOMAIN_IS_INITDOMAIN(xen_info))
   1550 #endif
   1551 	{
   1552 		/*
   1553 		 * Load the System Management BIOS into the global ksmbios
   1554 		 * handle, if an SMBIOS is present on this system.
   1555 		 */
   1556 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
   1557 	}
   1558 
   1559 
   1560 	/*
   1561 	 * Set up the CPU module subsystem for the boot cpu in the native
   1562 	 * case, and all physical cpu resource in the xpv dom0 case.
   1563 	 * Modifies the device tree, so this must be done after
   1564 	 * setup_ddi().
   1565 	 */
   1566 #ifdef __xpv
   1567 	/*
   1568 	 * If paravirtualized and on dom0 then we initialize all physical
   1569 	 * cpu handles now;  if paravirtualized on a domU then do not
   1570 	 * initialize.
   1571 	 */
   1572 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
   1573 		xen_mc_lcpu_cookie_t cpi;
   1574 
   1575 		for (cpi = xen_physcpu_next(NULL); cpi != NULL;
   1576 		    cpi = xen_physcpu_next(cpi)) {
   1577 			if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
   1578 			    xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
   1579 			    xen_physcpu_strandid(cpi))) != NULL &&
   1580 			    (x86_feature & X86_MCA))
   1581 				cmi_mca_init(hdl);
   1582 		}
   1583 	}
   1584 #else
   1585 	/*
   1586 	 * Initialize a handle for the boot cpu - others will initialize
   1587 	 * as they startup.  Do not do this if we know we are in an HVM domU.
   1588 	 */
   1589 	if ((get_hwenv() != HW_XEN_HVM) &&
   1590 	    (hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
   1591 	    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL &&
   1592 	    (x86_feature & X86_MCA))
   1593 			cmi_mca_init(hdl);
   1594 #endif	/* __xpv */
   1595 
   1596 	/*
   1597 	 * Fake a prom tree such that /dev/openprom continues to work
   1598 	 */
   1599 	PRM_POINT("startup_modules: calling prom_setup...");
   1600 	prom_setup();
   1601 	PRM_POINT("startup_modules: done");
   1602 
   1603 	/*
   1604 	 * Load all platform specific modules
   1605 	 */
   1606 	PRM_POINT("startup_modules: calling psm_modload...");
   1607 	psm_modload();
   1608 
   1609 	PRM_POINT("startup_modules() done");
   1610 }
   1611 
   1612 /*
   1613  * claim a "setaside" boot page for use in the kernel
   1614  */
   1615 page_t *
   1616 boot_claim_page(pfn_t pfn)
   1617 {
   1618 	page_t *pp;
   1619 
   1620 	pp = page_numtopp_nolock(pfn);
   1621 	ASSERT(pp != NULL);
   1622 
   1623 	if (PP_ISBOOTPAGES(pp)) {
   1624 		if (pp->p_next != NULL)
   1625 			pp->p_next->p_prev = pp->p_prev;
   1626 		if (pp->p_prev == NULL)
   1627 			bootpages = pp->p_next;
   1628 		else
   1629 			pp->p_prev->p_next = pp->p_next;
   1630 	} else {
   1631 		/*
   1632 		 * htable_attach() expects a base pagesize page
   1633 		 */
   1634 		if (pp->p_szc != 0)
   1635 			page_boot_demote(pp);
   1636 		pp = page_numtopp(pfn, SE_EXCL);
   1637 	}
   1638 	return (pp);
   1639 }
   1640 
   1641 /*
   1642  * Walk through the pagetables looking for pages mapped in by boot.  If the
   1643  * setaside flag is set the pages are expected to be returned to the
   1644  * kernel later in boot, so we add them to the bootpages list.
   1645  */
   1646 static void
   1647 protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
   1648 {
   1649 	uintptr_t va = low;
   1650 	size_t len;
   1651 	uint_t prot;
   1652 	pfn_t pfn;
   1653 	page_t *pp;
   1654 	pgcnt_t boot_protect_cnt = 0;
   1655 
   1656 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
   1657 		if (va + len >= high)
   1658 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
   1659 			    "legal range.", len, (void *)va);
   1660 
   1661 		while (len > 0) {
   1662 			pp = page_numtopp_alloc(pfn);
   1663 			if (pp != NULL) {
   1664 				if (setaside == 0)
   1665 					panic("Unexpected mapping by boot.  "
   1666 					    "addr=%p pfn=%lx\n",
   1667 					    (void *)va, pfn);
   1668 
   1669 				pp->p_next = bootpages;
   1670 				pp->p_prev = NULL;
   1671 				PP_SETBOOTPAGES(pp);
   1672 				if (bootpages != NULL) {
   1673 					bootpages->p_prev = pp;
   1674 				}
   1675 				bootpages = pp;
   1676 				++boot_protect_cnt;
   1677 			}
   1678 
   1679 			++pfn;
   1680 			len -= MMU_PAGESIZE;
   1681 			va += MMU_PAGESIZE;
   1682 		}
   1683 	}
   1684 	PRM_DEBUG(boot_protect_cnt);
   1685 }
   1686 
   1687 /*
   1688  *
   1689  */
   1690 static void
   1691 layout_kernel_va(void)
   1692 {
   1693 	PRM_POINT("layout_kernel_va() starting...");
   1694 	/*
   1695 	 * Establish the final size of the kernel's heap, size of segmap,
   1696 	 * segkp, etc.
   1697 	 */
   1698 
   1699 #if defined(__amd64)
   1700 
   1701 	kpm_vbase = (caddr_t)segkpm_base;
   1702 	kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
   1703 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
   1704 		panic("not enough room for kpm!");
   1705 	PRM_DEBUG(kpm_size);
   1706 	PRM_DEBUG(kpm_vbase);
   1707 
   1708 	/*
   1709 	 * By default we create a seg_kp in 64 bit kernels, it's a little
   1710 	 * faster to access than embedding it in the heap.
   1711 	 */
   1712 	segkp_base = (caddr_t)valloc_base + valloc_sz;
   1713 	if (!segkp_fromheap) {
   1714 		size_t sz = mmu_ptob(segkpsize);
   1715 
   1716 		/*
   1717 		 * determine size of segkp
   1718 		 */
   1719 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
   1720 			sz = SEGKPDEFSIZE;
   1721 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
   1722 			    "segkpsize has been reset to %ld pages",
   1723 			    mmu_btop(sz));
   1724 		}
   1725 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
   1726 
   1727 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
   1728 	}
   1729 	PRM_DEBUG(segkp_base);
   1730 	PRM_DEBUG(segkpsize);
   1731 
   1732 	/*
   1733 	 * segzio is used for ZFS cached data. It uses a distinct VA
   1734 	 * segment (from kernel heap) so that we can easily tell not to
   1735 	 * include it in kernel crash dumps on 64 bit kernels. The trick is
   1736 	 * to give it lots of VA, but not constrain the kernel heap.
   1737 	 * We scale the size of segzio linearly with physmem up to
   1738 	 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem.
   1739 	 */
   1740 	segzio_base = segkp_base + mmu_ptob(segkpsize);
   1741 	if (segzio_fromheap) {
   1742 		segziosize = 0;
   1743 	} else {
   1744 		size_t physmem_size = mmu_ptob(physmem);
   1745 		size_t size = (segziosize == 0) ?
   1746 		    physmem_size : mmu_ptob(segziosize);
   1747 
   1748 		if (size < SEGZIOMINSIZE)
   1749 			size = SEGZIOMINSIZE;
   1750 		if (size > SEGZIOMAXSIZE) {
   1751 			size = SEGZIOMAXSIZE;
   1752 			if (physmem_size > size)
   1753 				size += (physmem_size - size) / 2;
   1754 		}
   1755 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
   1756 	}
   1757 	PRM_DEBUG(segziosize);
   1758 	PRM_DEBUG(segzio_base);
   1759 
   1760 	/*
   1761 	 * Put the range of VA for device mappings next, kmdb knows to not
   1762 	 * grep in this range of addresses.
   1763 	 */
   1764 	toxic_addr =
   1765 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
   1766 	PRM_DEBUG(toxic_addr);
   1767 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
   1768 #else /* __i386 */
   1769 	segmap_start = ROUND_UP_LPAGE(kernelbase);
   1770 #endif /* __i386 */
   1771 	PRM_DEBUG(segmap_start);
   1772 
   1773 	/*
   1774 	 * Users can change segmapsize through eeprom. If the variable
   1775 	 * is tuned through eeprom, there is no upper bound on the
   1776 	 * size of segmap.
   1777 	 */
   1778 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
   1779 
   1780 #if defined(__i386)
   1781 	/*
   1782 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
   1783 	 * the bottom of the kernel's address range.  Set aside space for a
   1784 	 * small red zone just below the start of segmap.
   1785 	 */
   1786 	segmap_start += KERNEL_REDZONE_SIZE;
   1787 	segmapsize -= KERNEL_REDZONE_SIZE;
   1788 #endif
   1789 
   1790 	PRM_DEBUG(segmap_start);
   1791 	PRM_DEBUG(segmapsize);
   1792 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
   1793 	PRM_DEBUG(kernelheap);
   1794 	PRM_POINT("layout_kernel_va() done...");
   1795 }
   1796 
   1797 /*
   1798  * Finish initializing the VM system, now that we are no longer
   1799  * relying on the boot time memory allocators.
   1800  */
   1801 static void
   1802 startup_vm(void)
   1803 {
   1804 	struct segmap_crargs a;
   1805 
   1806 	extern int use_brk_lpg, use_stk_lpg;
   1807 
   1808 	PRM_POINT("startup_vm() starting...");
   1809 
   1810 	/*
   1811 	 * Initialize the hat layer.
   1812 	 */
   1813 	hat_init();
   1814 
   1815 	/*
   1816 	 * Do final allocations of HAT data structures that need to
   1817 	 * be allocated before quiescing the boot loader.
   1818 	 */
   1819 	PRM_POINT("Calling hat_kern_alloc()...");
   1820 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
   1821 	PRM_POINT("hat_kern_alloc() done");
   1822 
   1823 #ifndef __xpv
   1824 	/*
   1825 	 * Setup Page Attribute Table
   1826 	 */
   1827 	pat_sync();
   1828 #endif
   1829 
   1830 	/*
   1831 	 * The next two loops are done in distinct steps in order
   1832 	 * to be sure that any page that is doubly mapped (both above
   1833 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
   1834 	 * Note this may never happen, but it might someday.
   1835 	 */
   1836 	bootpages = NULL;
   1837 	PRM_POINT("Protecting boot pages");
   1838 
   1839 	/*
   1840 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
   1841 	 * page_t's. This can only happen if something weird allocated
   1842 	 * in this range (like kadb/kmdb).
   1843 	 */
   1844 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
   1845 
   1846 	/*
   1847 	 * Before we can take over memory allocation/mapping from the boot
   1848 	 * loader we must remove from our free page lists any boot allocated
   1849 	 * pages that stay mapped until release_bootstrap().
   1850 	 */
   1851 	protect_boot_range(0, kernelbase, 1);
   1852 
   1853 
   1854 	/*
   1855 	 * Switch to running on regular HAT (not boot_mmu)
   1856 	 */
   1857 	PRM_POINT("Calling hat_kern_setup()...");
   1858 	hat_kern_setup();
   1859 
   1860 	/*
   1861 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
   1862 	 */
   1863 	bop_no_more_mem();
   1864 
   1865 	PRM_POINT("hat_kern_setup() done");
   1866 
   1867 	hat_cpu_online(CPU);
   1868 
   1869 	/*
   1870 	 * Initialize VM system
   1871 	 */
   1872 	PRM_POINT("Calling kvm_init()...");
   1873 	kvm_init();
   1874 	PRM_POINT("kvm_init() done");
   1875 
   1876 	/*
   1877 	 * Tell kmdb that the VM system is now working
   1878 	 */
   1879 	if (boothowto & RB_DEBUG)
   1880 		kdi_dvec_vmready();
   1881 
   1882 #if defined(__xpv)
   1883 	/*
   1884 	 * Populate the I/O pool on domain 0
   1885 	 */
   1886 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
   1887 		extern long populate_io_pool(void);
   1888 		long init_io_pool_cnt;
   1889 
   1890 		PRM_POINT("Populating reserve I/O page pool");
   1891 		init_io_pool_cnt = populate_io_pool();
   1892 		PRM_DEBUG(init_io_pool_cnt);
   1893 	}
   1894 #endif
   1895 	/*
   1896 	 * Mangle the brand string etc.
   1897 	 */
   1898 	cpuid_pass3(CPU);
   1899 
   1900 #if defined(__amd64)
   1901 
   1902 	/*
   1903 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
   1904 	 */
   1905 	device_arena = vmem_create("device", (void *)toxic_addr,
   1906 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
   1907 
   1908 #else	/* __i386 */
   1909 
   1910 	/*
   1911 	 * allocate the bit map that tracks toxic pages
   1912 	 */
   1913 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
   1914 	PRM_DEBUG(toxic_bit_map_len);
   1915 	toxic_bit_map =
   1916 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
   1917 	ASSERT(toxic_bit_map != NULL);
   1918 	PRM_DEBUG(toxic_bit_map);
   1919 
   1920 #endif	/* __i386 */
   1921 
   1922 
   1923 	/*
   1924 	 * Now that we've got more VA, as well as the ability to allocate from
   1925 	 * it, tell the debugger.
   1926 	 */
   1927 	if (boothowto & RB_DEBUG)
   1928 		kdi_dvec_memavail();
   1929 
   1930 	/*
   1931 	 * The following code installs a special page fault handler (#pf)
   1932 	 * to work around a pentium bug.
   1933 	 */
   1934 #if !defined(__amd64) && !defined(__xpv)
   1935 	if (x86_type == X86_TYPE_P5) {
   1936 		desctbr_t idtr;
   1937 		gate_desc_t *newidt;
   1938 
   1939 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
   1940 			panic("failed to install pentium_pftrap");
   1941 
   1942 		bcopy(idt0, newidt, NIDT * sizeof (*idt0));
   1943 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
   1944 		    KCS_SEL, SDT_SYSIGT, TRP_KPL, 0);
   1945 
   1946 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
   1947 		    PROT_READ | PROT_EXEC);
   1948 
   1949 		CPU->cpu_idt = newidt;
   1950 		idtr.dtr_base = (uintptr_t)CPU->cpu_idt;
   1951 		idtr.dtr_limit = (NIDT * sizeof (*idt0)) - 1;
   1952 		wr_idtr(&idtr);
   1953 	}
   1954 #endif	/* !__amd64 */
   1955 
   1956 #if !defined(__xpv)
   1957 	/*
   1958 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
   1959 	 * parameters left there by controllers/BIOS.
   1960 	 */
   1961 	PRM_POINT("setup up p0_va");
   1962 	p0_va = i86devmap(0, 1, PROT_READ);
   1963 	PRM_DEBUG(p0_va);
   1964 #endif
   1965 
   1966 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
   1967 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
   1968 
   1969 	/*
   1970 	 * disable automatic large pages for small memory systems or
   1971 	 * when the disable flag is set.
   1972 	 *
   1973 	 * Do not yet consider page sizes larger than 2m/4m.
   1974 	 */
   1975 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
   1976 		max_uheap_lpsize = LEVEL_SIZE(1);
   1977 		max_ustack_lpsize = LEVEL_SIZE(1);
   1978 		max_privmap_lpsize = LEVEL_SIZE(1);
   1979 		max_uidata_lpsize = LEVEL_SIZE(1);
   1980 		max_utext_lpsize = LEVEL_SIZE(1);
   1981 		max_shm_lpsize = LEVEL_SIZE(1);
   1982 	}
   1983 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
   1984 	    auto_lpg_disable) {
   1985 		use_brk_lpg = 0;
   1986 		use_stk_lpg = 0;
   1987 	}
   1988 	mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
   1989 
   1990 	PRM_POINT("Calling hat_init_finish()...");
   1991 	hat_init_finish();
   1992 	PRM_POINT("hat_init_finish() done");
   1993 
   1994 	/*
   1995 	 * Initialize the segkp segment type.
   1996 	 */
   1997 	rw_enter(&kas.a_lock, RW_WRITER);
   1998 	PRM_POINT("Attaching segkp");
   1999 	if (segkp_fromheap) {
   2000 		segkp->s_as = &kas;
   2001 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
   2002 	    segkp) < 0) {
   2003 		panic("startup: cannot attach segkp");
   2004 		/*NOTREACHED*/
   2005 	}
   2006 	PRM_POINT("Doing segkp_create()");
   2007 	if (segkp_create(segkp) != 0) {
   2008 		panic("startup: segkp_create failed");
   2009 		/*NOTREACHED*/
   2010 	}
   2011 	PRM_DEBUG(segkp);
   2012 	rw_exit(&kas.a_lock);
   2013 
   2014 	/*
   2015 	 * kpm segment
   2016 	 */
   2017 	segmap_kpm = 0;
   2018 	if (kpm_desired) {
   2019 		kpm_init();
   2020 		kpm_enable = 1;
   2021 	}
   2022 
   2023 	/*
   2024 	 * Now create segmap segment.
   2025 	 */
   2026 	rw_enter(&kas.a_lock, RW_WRITER);
   2027 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
   2028 		panic("cannot attach segmap");
   2029 		/*NOTREACHED*/
   2030 	}
   2031 	PRM_DEBUG(segmap);
   2032 
   2033 	a.prot = PROT_READ | PROT_WRITE;
   2034 	a.shmsize = 0;
   2035 	a.nfreelist = segmapfreelists;
   2036 
   2037 	if (segmap_create(segmap, (caddr_t)&a) != 0)
   2038 		panic("segmap_create segmap");
   2039 	rw_exit(&kas.a_lock);
   2040 
   2041 	setup_vaddr_for_ppcopy(CPU);
   2042 
   2043 	segdev_init();
   2044 #if defined(__xpv)
   2045 	if (DOMAIN_IS_INITDOMAIN(xen_info))
   2046 #endif
   2047 		pmem_init();
   2048 
   2049 	PRM_POINT("startup_vm() done");
   2050 }
   2051 
   2052 /*
   2053  * Load a tod module for the non-standard tod part found on this system.
   2054  */
   2055 static void
   2056 load_tod_module(char *todmod)
   2057 {
   2058 	if (modload("tod", todmod) == -1)
   2059 		halt("Can't load TOD module");
   2060 }
   2061 
   2062 static void
   2063 startup_end(void)
   2064 {
   2065 	int i;
   2066 	extern void setx86isalist(void);
   2067 	extern void cpu_event_init(void);
   2068 
   2069 	PRM_POINT("startup_end() starting...");
   2070 
   2071 	/*
   2072 	 * Perform tasks that get done after most of the VM
   2073 	 * initialization has been done but before the clock
   2074 	 * and other devices get started.
   2075 	 */
   2076 	kern_setup1();
   2077 
   2078 	/*
   2079 	 * Perform CPC initialization for this CPU.
   2080 	 */
   2081 	kcpc_hw_init(CPU);
   2082 
   2083 	/*
   2084 	 * Initialize cpu event framework.
   2085 	 */
   2086 	cpu_event_init();
   2087 
   2088 #if defined(OPTERON_WORKAROUND_6323525)
   2089 	if (opteron_workaround_6323525)
   2090 		patch_workaround_6323525();
   2091 #endif
   2092 	/*
   2093 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
   2094 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
   2095 	 */
   2096 	if (tod_module_name != NULL) {
   2097 		PRM_POINT("load_tod_module()");
   2098 		load_tod_module(tod_module_name);
   2099 	}
   2100 
   2101 #if defined(__xpv)
   2102 	/*
   2103 	 * Forceload interposing TOD module for the hypervisor.
   2104 	 */
   2105 	PRM_POINT("load_tod_module()");
   2106 	load_tod_module("xpvtod");
   2107 #endif
   2108 
   2109 	/*
   2110 	 * Configure the system.
   2111 	 */
   2112 	PRM_POINT("Calling configure()...");
   2113 	configure();		/* set up devices */
   2114 	PRM_POINT("configure() done");
   2115 
   2116 	/*
   2117 	 * Set the isa_list string to the defined instruction sets we
   2118 	 * support.
   2119 	 */
   2120 	setx86isalist();
   2121 	cpu_intr_alloc(CPU, NINTR_THREADS);
   2122 	psm_install();
   2123 
   2124 	/*
   2125 	 * We're done with bootops.  We don't unmap the bootstrap yet because
   2126 	 * we're still using bootsvcs.
   2127 	 */
   2128 	PRM_POINT("NULLing out bootops");
   2129 	*bootopsp = (struct bootops *)NULL;
   2130 	bootops = (struct bootops *)NULL;
   2131 
   2132 #if defined(__xpv)
   2133 	ec_init_debug_irq();
   2134 	xs_domu_init();
   2135 #endif
   2136 
   2137 #if !defined(__xpv)
   2138 	if (rootnex_iommu_init != NULL) {
   2139 		rootnex_iommu_init();
   2140 	}
   2141 #endif
   2142 	PRM_POINT("Enabling interrupts");
   2143 	(*picinitf)();
   2144 	sti();
   2145 #if defined(__xpv)
   2146 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
   2147 	xen_late_startup();
   2148 #endif
   2149 
   2150 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
   2151 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
   2152 
   2153 	/*
   2154 	 * Register these software interrupts for ddi timer.
   2155 	 * Software interrupts up to the level 10 are supported.
   2156 	 */
   2157 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
   2158 		char name[sizeof ("timer_softintr") + 2];
   2159 		(void) sprintf(name, "timer_softintr%02d", i);
   2160 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
   2161 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
   2162 	}
   2163 
   2164 #if !defined(__xpv)
   2165 	if (modload("drv", "amd_iommu") < 0) {
   2166 		PRM_POINT("No AMD IOMMU present\n");
   2167 	} else if (ddi_hold_installed_driver(ddi_name_to_major(
   2168 	    "amd_iommu")) == NULL) {
   2169 		prom_printf("ERROR: failed to attach AMD IOMMU\n");
   2170 	}
   2171 #endif
   2172 	post_startup_cpu_fixups();
   2173 
   2174 	PRM_POINT("startup_end() done");
   2175 }
   2176 
   2177 /*
   2178  * Don't remove the following 2 variables.  They are necessary
   2179  * for reading the hostid from the legacy file (/kernel/misc/sysinit).
   2180  */
   2181 char *_hs1107 = hw_serial;
   2182 ulong_t  _bdhs34;
   2183 
   2184 void
   2185 post_startup(void)
   2186 {
   2187 	extern void cpupm_init(cpu_t *);
   2188 	extern void cpu_event_init_cpu(cpu_t *);
   2189 
   2190 	/*
   2191 	 * Set the system wide, processor-specific flags to be passed
   2192 	 * to userland via the aux vector for performance hints and
   2193 	 * instruction set extensions.
   2194 	 */
   2195 	bind_hwcap();
   2196 
   2197 #ifdef __xpv
   2198 	if (DOMAIN_IS_INITDOMAIN(xen_info))
   2199 #endif
   2200 	{
   2201 #if defined(__xpv)
   2202 		xpv_panic_init();
   2203 #else
   2204 		/*
   2205 		 * Startup the memory scrubber.
   2206 		 * XXPV	This should be running somewhere ..
   2207 		 */
   2208 		if (get_hwenv() != HW_XEN_HVM)
   2209 			memscrub_init();
   2210 #endif
   2211 	}
   2212 
   2213 	/*
   2214 	 * Complete CPU module initialization
   2215 	 */
   2216 	cmi_post_startup();
   2217 
   2218 	/*
   2219 	 * Perform forceloading tasks for /etc/system.
   2220 	 */
   2221 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
   2222 
   2223 	/*
   2224 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
   2225 	 * ON4.0: This must be fixed or restated in /etc/systems.
   2226 	 */
   2227 	(void) modload("fs", "procfs");
   2228 
   2229 	(void) i_ddi_attach_hw_nodes("pit_beep");
   2230 
   2231 #if defined(__i386)
   2232 	/*
   2233 	 * Check for required functional Floating Point hardware,
   2234 	 * unless FP hardware explicitly disabled.
   2235 	 */
   2236 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
   2237 		halt("No working FP hardware found");
   2238 #endif
   2239 
   2240 	maxmem = freemem;
   2241 
   2242 	cpu_event_init_cpu(CPU);
   2243 	cpupm_init(CPU);
   2244 	(void) mach_cpu_create_device_node(CPU, NULL);
   2245 
   2246 	pg_init();
   2247 }
   2248 
   2249 static int
   2250 pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
   2251 {
   2252 	return ((pp->p_pagenum >= btop(low_addr)) &&
   2253 	    (pp->p_pagenum < btopr(high_addr)));
   2254 }
   2255 
   2256 void
   2257 release_bootstrap(void)
   2258 {
   2259 	int root_is_ramdisk;
   2260 	page_t *pp;
   2261 	extern void kobj_boot_unmountroot(void);
   2262 	extern dev_t rootdev;
   2263 #if !defined(__xpv)
   2264 	pfn_t	pfn;
   2265 #endif
   2266 
   2267 	/* unmount boot ramdisk and release kmem usage */
   2268 	kobj_boot_unmountroot();
   2269 
   2270 	/*
   2271 	 * We're finished using the boot loader so free its pages.
   2272 	 */
   2273 	PRM_POINT("Unmapping lower boot pages");
   2274 
   2275 	clear_boot_mappings(0, _userlimit);
   2276 
   2277 	postbootkernelbase = kernelbase;
   2278 
   2279 	/*
   2280 	 * If root isn't on ramdisk, destroy the hardcoded
   2281 	 * ramdisk node now and release the memory. Else,
   2282 	 * ramdisk memory is kept in rd_pages.
   2283 	 */
   2284 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
   2285 	if (!root_is_ramdisk) {
   2286 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
   2287 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
   2288 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
   2289 		(void) ddi_remove_child(dip, 0);
   2290 	}
   2291 
   2292 	PRM_POINT("Releasing boot pages");
   2293 	while (bootpages) {
   2294 		extern uint64_t ramdisk_start, ramdisk_end;
   2295 		pp = bootpages;
   2296 		bootpages = pp->p_next;
   2297 
   2298 
   2299 		/* Keep pages for the lower 64K */
   2300 		if (pp_in_range(pp, 0, 0x40000)) {
   2301 			pp->p_next = lower_pages;
   2302 			lower_pages = pp;
   2303 			lower_pages_count++;
   2304 			continue;
   2305 		}
   2306 
   2307 
   2308 		if (root_is_ramdisk && pp_in_range(pp, ramdisk_start,
   2309 		    ramdisk_end)) {
   2310 			pp->p_next = rd_pages;
   2311 			rd_pages = pp;
   2312 			continue;
   2313 		}
   2314 		pp->p_next = (struct page *)0;
   2315 		pp->p_prev = (struct page *)0;
   2316 		PP_CLRBOOTPAGES(pp);
   2317 		page_free(pp, 1);
   2318 	}
   2319 	PRM_POINT("Boot pages released");
   2320 
   2321 #if !defined(__xpv)
   2322 /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
   2323 	/*
   2324 	 * Find 1 page below 1 MB so that other processors can boot up or
   2325 	 * so that any processor can resume.
   2326 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
   2327 	 * We should have just free'd one up.
   2328 	 */
   2329 
   2330 	/*
   2331 	 * 0x10 pages is 64K.  Leave the bottom 64K alone
   2332 	 * for BIOS.
   2333 	 */
   2334 	for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
   2335 		if (page_numtopp_alloc(pfn) == NULL)
   2336 			continue;
   2337 		rm_platter_va = i86devmap(pfn, 1,
   2338 		    PROT_READ | PROT_WRITE | PROT_EXEC);
   2339 		rm_platter_pa = ptob(pfn);
   2340 		hat_devload(kas.a_hat,
   2341 		    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
   2342 		    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
   2343 		    HAT_LOAD_NOCONSIST);
   2344 		break;
   2345 	}
   2346 	if (pfn == btop(1*1024*1024) && use_mp)
   2347 		panic("No page below 1M available for starting "
   2348 		    "other processors or for resuming from system-suspend");
   2349 #endif	/* !__xpv */
   2350 }
   2351 
   2352 /*
   2353  * Initialize the platform-specific parts of a page_t.
   2354  */
   2355 void
   2356 add_physmem_cb(page_t *pp, pfn_t pnum)
   2357 {
   2358 	pp->p_pagenum = pnum;
   2359 	pp->p_mapping = NULL;
   2360 	pp->p_embed = 0;
   2361 	pp->p_share = 0;
   2362 	pp->p_mlentry = 0;
   2363 }
   2364 
   2365 /*
   2366  * kphysm_init() initializes physical memory.
   2367  */
   2368 static pgcnt_t
   2369 kphysm_init(
   2370 	page_t *pp,
   2371 	pgcnt_t npages)
   2372 {
   2373 	struct memlist	*pmem;
   2374 	struct memseg	*cur_memseg;
   2375 	pfn_t		base_pfn;
   2376 	pfn_t		end_pfn;
   2377 	pgcnt_t		num;
   2378 	pgcnt_t		pages_done = 0;
   2379 	uint64_t	addr;
   2380 	uint64_t	size;
   2381 	extern pfn_t	ddiphysmin;
   2382 	extern int	mnode_xwa;
   2383 	int		ms = 0, me = 0;
   2384 
   2385 	ASSERT(page_hash != NULL && page_hashsz != 0);
   2386 
   2387 	cur_memseg = memseg_base;
   2388 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
   2389 		/*
   2390 		 * In a 32 bit kernel can't use higher memory if we're
   2391 		 * not booting in PAE mode. This check takes care of that.
   2392 		 */
   2393 		addr = pmem->address;
   2394 		size = pmem->size;
   2395 		if (btop(addr) > physmax)
   2396 			continue;
   2397 
   2398 		/*
   2399 		 * align addr and size - they may not be at page boundaries
   2400 		 */
   2401 		if ((addr & MMU_PAGEOFFSET) != 0) {
   2402 			addr += MMU_PAGEOFFSET;
   2403 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
   2404 			size -= addr - pmem->address;
   2405 		}
   2406 
   2407 		/* only process pages below or equal to physmax */
   2408 		if ((btop(addr + size) - 1) > physmax)
   2409 			size = ptob(physmax - btop(addr) + 1);
   2410 
   2411 		num = btop(size);
   2412 		if (num == 0)
   2413 			continue;
   2414 
   2415 		if (num > npages)
   2416 			num = npages;
   2417 
   2418 		npages -= num;
   2419 		pages_done += num;
   2420 		base_pfn = btop(addr);
   2421 
   2422 		if (prom_debug)
   2423 			prom_printf("MEMSEG addr=0x%" PRIx64
   2424 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
   2425 			    addr, num, base_pfn, base_pfn + num);
   2426 
   2427 		/*
   2428 		 * Ignore pages below ddiphysmin to simplify ddi memory
   2429 		 * allocation with non-zero addr_lo requests.
   2430 		 */
   2431 		if (base_pfn < ddiphysmin) {
   2432 			if (base_pfn + num <= ddiphysmin)
   2433 				continue;
   2434 			pp += (ddiphysmin - base_pfn);
   2435 			num -= (ddiphysmin - base_pfn);
   2436 			base_pfn = ddiphysmin;
   2437 		}
   2438 
   2439 		/*
   2440 		 * mnode_xwa is greater than 1 when large pages regions can
   2441 		 * cross memory node boundaries. To prevent the formation
   2442 		 * of these large pages, configure the memsegs based on the
   2443 		 * memory node ranges which had been made non-contiguous.
   2444 		 */
   2445 		if (mnode_xwa > 1) {
   2446 
   2447 			end_pfn = base_pfn + num - 1;
   2448 			ms = PFN_2_MEM_NODE(base_pfn);
   2449 			me = PFN_2_MEM_NODE(end_pfn);
   2450 
   2451 			if (ms != me) {
   2452 				/*
   2453 				 * current range spans more than 1 memory node.
   2454 				 * Set num to only the pfn range in the start
   2455 				 * memory node.
   2456 				 */
   2457 				num = mem_node_config[ms].physmax - base_pfn
   2458 				    + 1;
   2459 				ASSERT(end_pfn > mem_node_config[ms].physmax);
   2460 			}
   2461 		}
   2462 
   2463 		for (;;) {
   2464 			/*
   2465 			 * Build the memsegs entry
   2466 			 */
   2467 			cur_memseg->pages = pp;
   2468 			cur_memseg->epages = pp + num;
   2469 			cur_memseg->pages_base = base_pfn;
   2470 			cur_memseg->pages_end = base_pfn + num;
   2471 
   2472 			/*
   2473 			 * Insert into memseg list in decreasing pfn range
   2474 			 * order. Low memory is typically more fragmented such
   2475 			 * that this ordering keeps the larger ranges at the
   2476 			 * front of the list for code that searches memseg.
   2477 			 * This ASSERTS that the memsegs coming in from boot
   2478 			 * are in increasing physical address order and not
   2479 			 * contiguous.
   2480 			 */
   2481 			if (memsegs != NULL) {
   2482 				ASSERT(cur_memseg->pages_base >=
   2483 				    memsegs->pages_end);
   2484 				cur_memseg->next = memsegs;
   2485 			}
   2486 			memsegs = cur_memseg;
   2487 
   2488 			/*
   2489 			 * add_physmem() initializes the PSM part of the page
   2490 			 * struct by calling the PSM back with add_physmem_cb().
   2491 			 * In addition it coalesces pages into larger pages as
   2492 			 * it initializes them.
   2493 			 */
   2494 			add_physmem(pp, num, base_pfn);
   2495 			cur_memseg++;
   2496 			availrmem_initial += num;
   2497 			availrmem += num;
   2498 
   2499 			pp += num;
   2500 			if (ms >= me)
   2501 				break;
   2502 
   2503 			/* process next memory node range */
   2504 			ms++;
   2505 			base_pfn = mem_node_config[ms].physbase;
   2506 			num = MIN(mem_node_config[ms].physmax,
   2507 			    end_pfn) - base_pfn + 1;
   2508 		}
   2509 	}
   2510 
   2511 	PRM_DEBUG(availrmem_initial);
   2512 	PRM_DEBUG(availrmem);
   2513 	PRM_DEBUG(freemem);
   2514 	build_pfn_hash();
   2515 	return (pages_done);
   2516 }
   2517 
   2518 /*
   2519  * Kernel VM initialization.
   2520  */
   2521 static void
   2522 kvm_init(void)
   2523 {
   2524 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
   2525 
   2526 	/*
   2527 	 * Put the kernel segments in kernel address space.
   2528 	 */
   2529 	rw_enter(&kas.a_lock, RW_WRITER);
   2530 	as_avlinit(&kas);
   2531 
   2532 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
   2533 	(void) segkmem_create(&ktextseg);
   2534 
   2535 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
   2536 	(void) segkmem_create(&kvalloc);
   2537 
   2538 	(void) seg_attach(&kas, kernelheap,
   2539 	    ekernelheap - kernelheap, &kvseg);
   2540 	(void) segkmem_create(&kvseg);
   2541 
   2542 	if (core_size > 0) {
   2543 		PRM_POINT("attaching kvseg_core");
   2544 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
   2545 		    &kvseg_core);
   2546 		(void) segkmem_create(&kvseg_core);
   2547 	}
   2548 
   2549 	if (segziosize > 0) {
   2550 		PRM_POINT("attaching segzio");
   2551 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
   2552 		    &kzioseg);
   2553 		(void) segkmem_zio_create(&kzioseg);
   2554 
   2555 		/* create zio area covering new segment */
   2556 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
   2557 	}
   2558 
   2559 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
   2560 	(void) segkmem_create(&kdebugseg);
   2561 
   2562 	rw_exit(&kas.a_lock);
   2563 
   2564 	/*
   2565 	 * Ensure that the red zone at kernelbase is never accessible.
   2566 	 */
   2567 	PRM_POINT("protecting redzone");
   2568 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
   2569 
   2570 	/*
   2571 	 * Make the text writable so that it can be hot patched by DTrace.
   2572 	 */
   2573 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
   2574 	    PROT_READ | PROT_WRITE | PROT_EXEC);
   2575 
   2576 	/*
   2577 	 * Make data writable until end.
   2578 	 */
   2579 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
   2580 	    PROT_READ | PROT_WRITE | PROT_EXEC);
   2581 }
   2582 
   2583 #ifndef __xpv
   2584 /*
   2585  * Solaris adds an entry for Write Combining caching to the PAT
   2586  */
   2587 static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
   2588 
   2589 void
   2590 pat_sync(void)
   2591 {
   2592 	ulong_t	cr0, cr0_orig, cr4;
   2593 
   2594 	if (!(x86_feature & X86_PAT))
   2595 		return;
   2596 	cr0_orig = cr0 = getcr0();
   2597 	cr4 = getcr4();
   2598 
   2599 	/* disable caching and flush all caches and TLBs */
   2600 	cr0 |= CR0_CD;
   2601 	cr0 &= ~CR0_NW;
   2602 	setcr0(cr0);
   2603 	invalidate_cache();
   2604 	if (cr4 & CR4_PGE) {
   2605 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
   2606 		setcr4(cr4);
   2607 	} else {
   2608 		reload_cr3();
   2609 	}
   2610 
   2611 	/* add our entry to the PAT */
   2612 	wrmsr(REG_PAT, pat_attr_reg);
   2613 
   2614 	/* flush TLBs and cache again, then reenable cr0 caching */
   2615 	if (cr4 & CR4_PGE) {
   2616 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
   2617 		setcr4(cr4);
   2618 	} else {
   2619 		reload_cr3();
   2620 	}
   2621 	invalidate_cache();
   2622 	setcr0(cr0_orig);
   2623 }
   2624 
   2625 #endif /* !__xpv */
   2626 
   2627 #if defined(_SOFT_HOSTID)
   2628 /*
   2629  * On platforms that do not have a hardware serial number, attempt
   2630  * to set one based on the contents of /etc/hostid.  If this file does
   2631  * not exist, assume that we are to generate a new hostid and set
   2632  * it in the kernel, for subsequent saving by a userland process
   2633  * once the system is up and the root filesystem is mounted r/w.
   2634  *
   2635  * In order to gracefully support upgrade on OpenSolaris, if
   2636  * /etc/hostid does not exist, we will attempt to get a serial number
   2637  * using the legacy method (/kernel/misc/sysinit).
   2638  *
   2639  * In an attempt to make the hostid less prone to abuse
   2640  * (for license circumvention, etc), we store it in /etc/hostid
   2641  * in rot47 format.
   2642  */
   2643 extern volatile unsigned long tenmicrodata;
   2644 static int atoi(char *);
   2645 
   2646 static int32_t
   2647 set_soft_hostid(void)
   2648 {
   2649 	struct _buf *file;
   2650 	char tokbuf[MAXNAMELEN];
   2651 	token_t token;
   2652 	int done = 0;
   2653 	u_longlong_t tmp;
   2654 	int i;
   2655 	int32_t hostid = (int32_t)HW_INVALID_HOSTID;
   2656 	unsigned char *c;
   2657 	hrtime_t tsc;
   2658 
   2659 	/*
   2660 	 * If /etc/hostid file not found, we'd like to get a pseudo
   2661 	 * random number to use at the hostid.  A nice way to do this
   2662 	 * is to read the real time clock.  To remain xen-compatible,
   2663 	 * we can't poke the real hardware, so we use tsc_read() to
   2664 	 * read the real time clock.  However, there is an ominous
   2665 	 * warning in tsc_read that says it can return zero, so we
   2666 	 * deal with that possibility by falling back to using the
   2667 	 * (hopefully random enough) value in tenmicrodata.
   2668 	 */
   2669 
   2670 	if ((file = kobj_open_file(hostid_file)) == (struct _buf *)-1) {
   2671 		/*
   2672 		 * hostid file not found - try to load sysinit module
   2673 		 * and see if it has a nonzero hostid value...use that
   2674 		 * instead of generating a new hostid here if so.
   2675 		 */
   2676 		if ((i = modload("misc", "sysinit")) != -1) {
   2677 			if (strlen(hw_serial) > 0)
   2678 				hostid = (int32_t)atoi(hw_serial);
   2679 			(void) modunload(i);
   2680 		}
   2681 		if (hostid == HW_INVALID_HOSTID) {
   2682 			tsc = tsc_read();
   2683 			if (tsc == 0)	/* tsc_read can return zero sometimes */
   2684 				hostid = (int32_t)tenmicrodata & 0x0CFFFFF;
   2685 			else
   2686 				hostid = (int32_t)tsc & 0x0CFFFFF;
   2687 		}
   2688 	} else {
   2689 		/* hostid file found */
   2690 		while (!done) {
   2691 			token = kobj_lex(file, tokbuf, sizeof (tokbuf));
   2692 
   2693 			switch (token) {
   2694 			case POUND:
   2695 				/*
   2696 				 * skip comments
   2697 				 */
   2698 				kobj_find_eol(file);
   2699 				break;
   2700 			case STRING:
   2701 				/*
   2702 				 * un-rot47 - obviously this
   2703 				 * nonsense is ascii-specific
   2704 				 */
   2705 				for (c = (unsigned char *)tokbuf;
   2706 				    *c != '\0'; c++) {
   2707 					*c += 47;
   2708 					if (*c > '~')
   2709 						*c -= 94;
   2710 					else if (*c < '!')
   2711 						*c += 94;
   2712 				}
   2713 				/*
   2714 				 * now we should have a real number
   2715 				 */
   2716 
   2717 				if (kobj_getvalue(tokbuf, &tmp) != 0)
   2718 					kobj_file_err(CE_WARN, file,
   2719 					    "Bad value %s for hostid",
   2720 					    tokbuf);
   2721 				else
   2722 					hostid = (int32_t)tmp;
   2723 
   2724 				break;
   2725 			case EOF:
   2726 				done = 1;
   2727 				/* FALLTHROUGH */
   2728 			case NEWLINE:
   2729 				kobj_newline(file);
   2730 				break;
   2731 			default:
   2732 				break;
   2733 
   2734 			}
   2735 		}
   2736 		if (hostid == HW_INVALID_HOSTID) /* didn't find a hostid */
   2737 			kobj_file_err(CE_WARN, file,
   2738 			    "hostid missing or corrupt");
   2739 
   2740 		kobj_close_file(file);
   2741 	}
   2742 	/*
   2743 	 * hostid is now the value read from /etc/hostid, or the
   2744 	 * new hostid we generated in this routine or HW_INVALID_HOSTID if not
   2745 	 * set.
   2746 	 */
   2747 	return (hostid);
   2748 }
   2749 
   2750 static int
   2751 atoi(char *p)
   2752 {
   2753 	int i = 0;
   2754 
   2755 	while (*p != '\0')
   2756 		i = 10 * i + (*p++ - '0');
   2757 
   2758 	return (i);
   2759 }
   2760 
   2761 #endif /* _SOFT_HOSTID */
   2762 
   2763 void
   2764 get_system_configuration(void)
   2765 {
   2766 	char	prop[32];
   2767 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
   2768 
   2769 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
   2770 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
   2771 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
   2772 	    nodes_ll > MAXNODES ||
   2773 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
   2774 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
   2775 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
   2776 		system_hardware.hd_nodes = 1;
   2777 		system_hardware.hd_cpus_per_node = 0;
   2778 	} else {
   2779 		system_hardware.hd_nodes = (int)nodes_ll;
   2780 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
   2781 	}
   2782 
   2783 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
   2784 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
   2785 	    kobj_getvalue(prop, &lvalue) == -1)
   2786 		eprom_kernelbase = NULL;
   2787 	else
   2788 		eprom_kernelbase = (uintptr_t)lvalue;
   2789 
   2790 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
   2791 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
   2792 	    kobj_getvalue(prop, &lvalue) == -1)
   2793 		segmapsize = SEGMAPDEFAULT;
   2794 	else
   2795 		segmapsize = (uintptr_t)lvalue;
   2796 
   2797 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
   2798 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
   2799 	    kobj_getvalue(prop, &lvalue) == -1)
   2800 		segmapfreelists = 0;	/* use segmap driver default */
   2801 	else
   2802 		segmapfreelists = (int)lvalue;
   2803 
   2804 	/* physmem used to be here, but moved much earlier to fakebop.c */
   2805 }
   2806 
   2807 /*
   2808  * Add to a memory list.
   2809  * start = start of new memory segment
   2810  * len = length of new memory segment in bytes
   2811  * new = pointer to a new struct memlist
   2812  * memlistp = memory list to which to add segment.
   2813  */
   2814 void
   2815 memlist_add(
   2816 	uint64_t start,
   2817 	uint64_t len,
   2818 	struct memlist *new,
   2819 	struct memlist **memlistp)
   2820 {
   2821 	struct memlist *cur;
   2822 	uint64_t end = start + len;
   2823 
   2824 	new->address = start;
   2825 	new->size = len;
   2826 
   2827 	cur = *memlistp;
   2828 
   2829 	while (cur) {
   2830 		if (cur->address >= end) {
   2831 			new->next = cur;
   2832 			*memlistp = new;
   2833 			new->prev = cur->prev;
   2834 			cur->prev = new;
   2835 			return;
   2836 		}
   2837 		ASSERT(cur->address + cur->size <= start);
   2838 		if (cur->next == NULL) {
   2839 			cur->next = new;
   2840 			new->prev = cur;
   2841 			new->next = NULL;
   2842 			return;
   2843 		}
   2844 		memlistp = &cur->next;
   2845 		cur = cur->next;
   2846 	}
   2847 }
   2848 
   2849 void
   2850 kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
   2851 {
   2852 	size_t tsize = e_modtext - modtext;
   2853 	size_t dsize = e_moddata - moddata;
   2854 
   2855 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
   2856 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
   2857 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
   2858 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
   2859 }
   2860 
   2861 caddr_t
   2862 kobj_text_alloc(vmem_t *arena, size_t size)
   2863 {
   2864 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
   2865 }
   2866 
   2867 /*ARGSUSED*/
   2868 caddr_t
   2869 kobj_texthole_alloc(caddr_t addr, size_t size)
   2870 {
   2871 	panic("unexpected call to kobj_texthole_alloc()");
   2872 	/*NOTREACHED*/
   2873 	return (0);
   2874 }
   2875 
   2876 /*ARGSUSED*/
   2877 void
   2878 kobj_texthole_free(caddr_t addr, size_t size)
   2879 {
   2880 	panic("unexpected call to kobj_texthole_free()");
   2881 }
   2882 
   2883 /*
   2884  * This is called just after configure() in startup().
   2885  *
   2886  * The ISALIST concept is a bit hopeless on Intel, because
   2887  * there's no guarantee of an ever-more-capable processor
   2888  * given that various parts of the instruction set may appear
   2889  * and disappear between different implementations.
   2890  *
   2891  * While it would be possible to correct it and even enhance
   2892  * it somewhat, the explicit hardware capability bitmask allows
   2893  * more flexibility.
   2894  *
   2895  * So, we just leave this alone.
   2896  */
   2897 void
   2898 setx86isalist(void)
   2899 {
   2900 	char *tp;
   2901 	size_t len;
   2902 	extern char *isa_list;
   2903 
   2904 #define	TBUFSIZE	1024
   2905 
   2906 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
   2907 	*tp = '\0';
   2908 
   2909 #if defined(__amd64)
   2910 	(void) strcpy(tp, "amd64 ");
   2911 #endif
   2912 
   2913 	switch (x86_vendor) {
   2914 	case X86_VENDOR_Intel:
   2915 	case X86_VENDOR_AMD:
   2916 	case X86_VENDOR_TM:
   2917 		if (x86_feature & X86_CMOV) {
   2918 			/*
   2919 			 * Pentium Pro or later
   2920 			 */
   2921 			(void) strcat(tp, "pentium_pro");
   2922 			(void) strcat(tp, x86_feature & X86_MMX ?
   2923 			    "+mmx pentium_pro " : " ");
   2924 		}
   2925 		/*FALLTHROUGH*/
   2926 	case X86_VENDOR_Cyrix:
   2927 		/*
   2928 		 * The Cyrix 6x86 does not have any Pentium features
   2929 		 * accessible while not at privilege level 0.
   2930 		 */
   2931 		if (x86_feature & X86_CPUID) {
   2932 			(void) strcat(tp, "pentium");
   2933 			(void) strcat(tp, x86_feature & X86_MMX ?
   2934 			    "+mmx pentium " : " ");
   2935 		}
   2936 		break;
   2937 	default:
   2938 		break;
   2939 	}
   2940 	(void) strcat(tp, "i486 i386 i86");
   2941 	len = strlen(tp) + 1;   /* account for NULL at end of string */
   2942 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
   2943 	kmem_free(tp, TBUFSIZE);
   2944 
   2945 #undef TBUFSIZE
   2946 }
   2947 
   2948 
   2949 #ifdef __amd64
   2950 
   2951 void *
   2952 device_arena_alloc(size_t size, int vm_flag)
   2953 {
   2954 	return (vmem_alloc(device_arena, size, vm_flag));
   2955 }
   2956 
   2957 void
   2958 device_arena_free(void *vaddr, size_t size)
   2959 {
   2960 	vmem_free(device_arena, vaddr, size);
   2961 }
   2962 
   2963 #else /* __i386 */
   2964 
   2965 void *
   2966 device_arena_alloc(size_t size, int vm_flag)
   2967 {
   2968 	caddr_t	vaddr;
   2969 	uintptr_t v;
   2970 	size_t	start;
   2971 	size_t	end;
   2972 
   2973 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
   2974 	if (vaddr == NULL)
   2975 		return (NULL);
   2976 
   2977 	v = (uintptr_t)vaddr;
   2978 	ASSERT(v >= kernelbase);
   2979 	ASSERT(v + size <= valloc_base);
   2980 
   2981 	start = btop(v - kernelbase);
   2982 	end = btop(v + size - 1 - kernelbase);
   2983 	ASSERT(start < toxic_bit_map_len);
   2984 	ASSERT(end < toxic_bit_map_len);
   2985 
   2986 	while (start <= end) {
   2987 		BT_ATOMIC_SET(toxic_bit_map, start);
   2988 		++start;
   2989 	}
   2990 	return (vaddr);
   2991 }
   2992 
   2993 void
   2994 device_arena_free(void *vaddr, size_t size)
   2995 {
   2996 	uintptr_t v = (uintptr_t)vaddr;
   2997 	size_t	start;
   2998 	size_t	end;
   2999 
   3000 	ASSERT(v >= kernelbase);
   3001 	ASSERT(v + size <= valloc_base);
   3002 
   3003 	start = btop(v - kernelbase);
   3004 	end = btop(v + size - 1 - kernelbase);
   3005 	ASSERT(start < toxic_bit_map_len);
   3006 	ASSERT(end < toxic_bit_map_len);
   3007 
   3008 	while (start <= end) {
   3009 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
   3010 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
   3011 		++start;
   3012 	}
   3013 	vmem_free(heap_arena, vaddr, size);
   3014 }
   3015 
   3016 /*
   3017  * returns 1st address in range that is in device arena, or NULL
   3018  * if len is not NULL it returns the length of the toxic range
   3019  */
   3020 void *
   3021 device_arena_contains(void *vaddr, size_t size, size_t *len)
   3022 {
   3023 	uintptr_t v = (uintptr_t)vaddr;
   3024 	uintptr_t eaddr = v + size;
   3025 	size_t start;
   3026 	size_t end;
   3027 
   3028 	/*
   3029 	 * if called very early by kmdb, just return NULL
   3030 	 */
   3031 	if (toxic_bit_map == NULL)
   3032 		return (NULL);
   3033 
   3034 	/*
   3035 	 * First check if we're completely outside the bitmap range.
   3036 	 */
   3037 	if (v >= valloc_base || eaddr < kernelbase)
   3038 		return (NULL);
   3039 
   3040 	/*
   3041 	 * Trim ends of search to look at only what the bitmap covers.
   3042 	 */
   3043 	if (v < kernelbase)
   3044 		v = kernelbase;
   3045 	start = btop(v - kernelbase);
   3046 	end = btop(eaddr - kernelbase);
   3047 	if (end >= toxic_bit_map_len)
   3048 		end = toxic_bit_map_len;
   3049 
   3050 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
   3051 		return (NULL);
   3052 
   3053 	v = kernelbase + ptob(start);
   3054 	if (len != NULL)
   3055 		*len = ptob(end - start);
   3056 	return ((void *)v);
   3057 }
   3058 
   3059 #endif	/* __i386 */
   3060