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