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