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