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