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