xref: /titanic_50/usr/src/uts/i86pc/os/startup.c (revision 33a5e6b27fde6c2d7434c79c15232c335e18950e)
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 
127 extern void debug_enter(char *);
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 	extern void memscrub_init(void);
1874 
1875 	/*
1876 	 * Set the system wide, processor-specific flags to be passed
1877 	 * to userland via the aux vector for performance hints and
1878 	 * instruction set extensions.
1879 	 */
1880 	bind_hwcap();
1881 
1882 	/*
1883 	 * Startup memory scrubber.
1884 	 */
1885 	(void) memscrub_init();
1886 
1887 	/*
1888 	 * Perform forceloading tasks for /etc/system.
1889 	 */
1890 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
1891 
1892 	/*
1893 	 * complete mmu initialization, now that kernel and critical
1894 	 * modules have been loaded.
1895 	 */
1896 	(void) post_startup_mmu_initialization();
1897 
1898 	/*
1899 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
1900 	 * ON4.0: This must be fixed or restated in /etc/systems.
1901 	 */
1902 	(void) modload("fs", "procfs");
1903 
1904 #if defined(__i386)
1905 	/*
1906 	 * Check for required functional Floating Point hardware,
1907 	 * unless FP hardware explicitly disabled.
1908 	 */
1909 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
1910 		halt("No working FP hardware found");
1911 #endif
1912 
1913 	maxmem = freemem;
1914 
1915 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
1916 
1917 	/*
1918 	 * Perform the formal initialization of the boot chip,
1919 	 * and associate the boot cpu with it.
1920 	 * This must be done after the cpu node for CPU has been
1921 	 * added to the device tree, when the necessary probing to
1922 	 * know the chip type and chip "id" is performed.
1923 	 */
1924 	chip_cpu_init(CPU);
1925 	chip_cpu_assign(CPU);
1926 }
1927 
1928 static int
1929 pp_in_ramdisk(page_t *pp)
1930 {
1931 	extern uint64_t ramdisk_start, ramdisk_end;
1932 
1933 	return ((pp->p_pagenum >= btop(ramdisk_start)) &&
1934 	    (pp->p_pagenum < btopr(ramdisk_end)));
1935 }
1936 
1937 void
1938 release_bootstrap(void)
1939 {
1940 	int root_is_ramdisk;
1941 	pfn_t pfn;
1942 	page_t *pp;
1943 	extern void kobj_boot_unmountroot(void);
1944 	extern dev_t rootdev;
1945 
1946 	/* unmount boot ramdisk and release kmem usage */
1947 	kobj_boot_unmountroot();
1948 
1949 	/*
1950 	 * We're finished using the boot loader so free its pages.
1951 	 */
1952 	PRM_POINT("Unmapping lower boot pages");
1953 	clear_boot_mappings(0, kernelbase);
1954 #if defined(__amd64)
1955 	PRM_POINT("Unmapping upper boot pages");
1956 	clear_boot_mappings(BOOT_DOUBLEMAP_BASE,
1957 	    BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE);
1958 #endif
1959 
1960 	/*
1961 	 * If root isn't on ramdisk, destroy the hardcoded
1962 	 * ramdisk node now and release the memory. Else,
1963 	 * ramdisk memory is kept in rd_pages.
1964 	 */
1965 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
1966 	if (!root_is_ramdisk) {
1967 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
1968 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
1969 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
1970 		(void) ddi_remove_child(dip, 0);
1971 	}
1972 
1973 	PRM_POINT("Releasing boot pages");
1974 	while (bootpages) {
1975 		pp = bootpages;
1976 		bootpages = pp->p_next;
1977 		if (root_is_ramdisk && pp_in_ramdisk(pp)) {
1978 			pp->p_next = rd_pages;
1979 			rd_pages = pp;
1980 			continue;
1981 		}
1982 		pp->p_next = (struct page *)0;
1983 		page_free(pp, 1);
1984 	}
1985 
1986 	/*
1987 	 * Find 1 page below 1 MB so that other processors can boot up.
1988 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
1989 	 * We should have just free'd one up.
1990 	 */
1991 	if (use_mp) {
1992 		for (pfn = 1; pfn < btop(1*1024*1024); pfn++) {
1993 			if (page_numtopp_alloc(pfn) == NULL)
1994 				continue;
1995 			rm_platter_va = i86devmap(pfn, 1,
1996 			    PROT_READ | PROT_WRITE | PROT_EXEC);
1997 			rm_platter_pa = ptob(pfn);
1998 			hat_devload(kas.a_hat,
1999 			    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
2000 			    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
2001 			    HAT_LOAD_NOCONSIST);
2002 			break;
2003 		}
2004 		if (pfn == btop(1*1024*1024))
2005 			panic("No page available for starting "
2006 			    "other processors");
2007 	}
2008 
2009 #if defined(__amd64)
2010 	PRM_POINT("Returning boot's VA space to kernel heap");
2011 	if (kmem_setaside != NULL)
2012 		vmem_free(heap_arena, kmem_setaside, BOOT_DOUBLEMAP_SIZE);
2013 #endif
2014 }
2015 
2016 /*
2017  * Initialize the platform-specific parts of a page_t.
2018  */
2019 void
2020 add_physmem_cb(page_t *pp, pfn_t pnum)
2021 {
2022 	pp->p_pagenum = pnum;
2023 	pp->p_mapping = NULL;
2024 	pp->p_embed = 0;
2025 	pp->p_share = 0;
2026 	pp->p_mlentry = 0;
2027 }
2028 
2029 /*
2030  * kphysm_init() initializes physical memory.
2031  */
2032 static pgcnt_t
2033 kphysm_init(
2034 	page_t *inpp,
2035 	struct memseg *memsegp,
2036 	pgcnt_t start,
2037 	pgcnt_t npages)
2038 {
2039 	struct memlist	*pmem;
2040 	struct memseg	*cur_memseg;
2041 	struct memseg	**memsegpp;
2042 	pfn_t		base_pfn;
2043 	pgcnt_t		num;
2044 	pgcnt_t		total_skipped = 0;
2045 	pgcnt_t		skipping = 0;
2046 	pgcnt_t		pages_done = 0;
2047 	pgcnt_t		largepgcnt;
2048 	uint64_t	addr;
2049 	uint64_t	size;
2050 	page_t		*pp = inpp;
2051 	int		dobreak = 0;
2052 	extern pfn_t	ddiphysmin;
2053 
2054 	ASSERT(page_hash != NULL && page_hashsz != 0);
2055 
2056 	for (cur_memseg = memsegp; cur_memseg->pages != NULL; cur_memseg++);
2057 	ASSERT(cur_memseg == memsegp || start > 0);
2058 
2059 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
2060 		/*
2061 		 * In a 32 bit kernel can't use higher memory if we're
2062 		 * not booting in PAE mode. This check takes care of that.
2063 		 */
2064 		addr = pmem->address;
2065 		size = pmem->size;
2066 		if (btop(addr) > physmax)
2067 			continue;
2068 
2069 		/*
2070 		 * align addr and size - they may not be at page boundaries
2071 		 */
2072 		if ((addr & MMU_PAGEOFFSET) != 0) {
2073 			addr += MMU_PAGEOFFSET;
2074 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
2075 			size -= addr - pmem->address;
2076 		}
2077 
2078 		/* only process pages below physmax */
2079 		if (btop(addr + size) > physmax)
2080 			size = ptob(physmax - btop(addr));
2081 
2082 		num = btop(size);
2083 		if (num == 0)
2084 			continue;
2085 
2086 		if (total_skipped < start) {
2087 			if (start - total_skipped > num) {
2088 				total_skipped += num;
2089 				continue;
2090 			}
2091 			skipping = start - total_skipped;
2092 			num -= skipping;
2093 			addr += (MMU_PAGESIZE * skipping);
2094 			total_skipped = start;
2095 		}
2096 		if (num == 0)
2097 			continue;
2098 
2099 		if (num > npages)
2100 			num = npages;
2101 
2102 		npages -= num;
2103 		pages_done += num;
2104 		base_pfn = btop(addr);
2105 
2106 		/*
2107 		 * If the caller didn't provide space for the page
2108 		 * structures, carve them out of the memseg they will
2109 		 * represent.
2110 		 */
2111 		if (pp == NULL) {
2112 			pgcnt_t pp_pgs;
2113 
2114 			if (num <= 1)
2115 				continue;
2116 
2117 			/*
2118 			 * Compute how many of the pages we need to use for
2119 			 * page_ts
2120 			 */
2121 			pp_pgs = (num * sizeof (page_t)) / MMU_PAGESIZE + 1;
2122 			while (mmu_ptob(pp_pgs - 1) / sizeof (page_t) >=
2123 			    num - pp_pgs + 1)
2124 				--pp_pgs;
2125 			PRM_DEBUG(pp_pgs);
2126 
2127 			pp = vmem_alloc(heap_arena, mmu_ptob(pp_pgs),
2128 			    VM_NOSLEEP);
2129 			if (pp == NULL) {
2130 				cmn_err(CE_WARN, "Unable to add %ld pages to "
2131 				    "the system.", num);
2132 				continue;
2133 			}
2134 
2135 			hat_devload(kas.a_hat, (void *)pp, mmu_ptob(pp_pgs),
2136 			    base_pfn, PROT_READ | PROT_WRITE | HAT_UNORDERED_OK,
2137 			    HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
2138 			bzero(pp, mmu_ptob(pp_pgs));
2139 			num -= pp_pgs;
2140 			base_pfn += pp_pgs;
2141 		}
2142 
2143 		if (prom_debug)
2144 			prom_printf("MEMSEG addr=0x%" PRIx64
2145 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
2146 			    addr, num, base_pfn, base_pfn + num);
2147 
2148 		/*
2149 		 * drop pages below ddiphysmin to simplify ddi memory
2150 		 * allocation with non-zero addr_lo requests.
2151 		 */
2152 		if (base_pfn < ddiphysmin) {
2153 			if (base_pfn + num <= ddiphysmin) {
2154 				/* drop entire range below ddiphysmin */
2155 				continue;
2156 			}
2157 			/* adjust range to ddiphysmin */
2158 			pp += (ddiphysmin - base_pfn);
2159 			num -= (ddiphysmin - base_pfn);
2160 			base_pfn = ddiphysmin;
2161 		}
2162 		/*
2163 		 * Build the memsegs entry
2164 		 */
2165 		cur_memseg->pages = pp;
2166 		cur_memseg->epages = pp + num;
2167 		cur_memseg->pages_base = base_pfn;
2168 		cur_memseg->pages_end = base_pfn + num;
2169 
2170 		/*
2171 		 * insert in memseg list in decreasing pfn range order.
2172 		 * Low memory is typically more fragmented such that this
2173 		 * ordering keeps the larger ranges at the front of the list
2174 		 * for code that searches memseg.
2175 		 */
2176 		memsegpp = &memsegs;
2177 		for (;;) {
2178 			if (*memsegpp == NULL) {
2179 				/* empty memsegs */
2180 				memsegs = cur_memseg;
2181 				break;
2182 			}
2183 			/* check for continuity with start of memsegpp */
2184 			if (cur_memseg->pages_end == (*memsegpp)->pages_base) {
2185 				if (cur_memseg->epages == (*memsegpp)->pages) {
2186 					/*
2187 					 * contiguous pfn and page_t's. Merge
2188 					 * cur_memseg into *memsegpp. Drop
2189 					 * cur_memseg
2190 					 */
2191 					(*memsegpp)->pages_base =
2192 					    cur_memseg->pages_base;
2193 					(*memsegpp)->pages =
2194 					    cur_memseg->pages;
2195 					/*
2196 					 * check if contiguous with the end of
2197 					 * the next memseg.
2198 					 */
2199 					if ((*memsegpp)->next &&
2200 					    ((*memsegpp)->pages_base ==
2201 					    (*memsegpp)->next->pages_end)) {
2202 						cur_memseg = *memsegpp;
2203 						memsegpp = &((*memsegpp)->next);
2204 						dobreak = 1;
2205 					} else {
2206 						break;
2207 					}
2208 				} else {
2209 					/*
2210 					 * contiguous pfn but not page_t's.
2211 					 * drop last pfn/page_t in cur_memseg
2212 					 * to prevent creation of large pages
2213 					 * with noncontiguous page_t's if not
2214 					 * aligned to largest page boundary.
2215 					 */
2216 					largepgcnt = page_get_pagecnt(
2217 					    page_num_pagesizes() - 1);
2218 
2219 					if (cur_memseg->pages_end &
2220 					    (largepgcnt - 1)) {
2221 						num--;
2222 						cur_memseg->epages--;
2223 						cur_memseg->pages_end--;
2224 					}
2225 				}
2226 			}
2227 
2228 			/* check for continuity with end of memsegpp */
2229 			if (cur_memseg->pages_base == (*memsegpp)->pages_end) {
2230 				if (cur_memseg->pages == (*memsegpp)->epages) {
2231 					/*
2232 					 * contiguous pfn and page_t's. Merge
2233 					 * cur_memseg into *memsegpp. Drop
2234 					 * cur_memseg.
2235 					 */
2236 					if (dobreak) {
2237 						/* merge previously done */
2238 						cur_memseg->pages =
2239 						    (*memsegpp)->pages;
2240 						cur_memseg->pages_base =
2241 						    (*memsegpp)->pages_base;
2242 						cur_memseg->next =
2243 						    (*memsegpp)->next;
2244 					} else {
2245 						(*memsegpp)->pages_end =
2246 						    cur_memseg->pages_end;
2247 						(*memsegpp)->epages =
2248 						    cur_memseg->epages;
2249 					}
2250 					break;
2251 				}
2252 				/*
2253 				 * contiguous pfn but not page_t's.
2254 				 * drop first pfn/page_t in cur_memseg
2255 				 * to prevent creation of large pages
2256 				 * with noncontiguous page_t's if not
2257 				 * aligned to largest page boundary.
2258 				 */
2259 				largepgcnt = page_get_pagecnt(
2260 				    page_num_pagesizes() - 1);
2261 				if (base_pfn & (largepgcnt - 1)) {
2262 					num--;
2263 					base_pfn++;
2264 					cur_memseg->pages++;
2265 					cur_memseg->pages_base++;
2266 					pp = cur_memseg->pages;
2267 				}
2268 				if (dobreak)
2269 					break;
2270 			}
2271 
2272 			if (cur_memseg->pages_base >=
2273 			    (*memsegpp)->pages_end) {
2274 				cur_memseg->next = *memsegpp;
2275 				*memsegpp = cur_memseg;
2276 				break;
2277 			}
2278 			if ((*memsegpp)->next == NULL) {
2279 				cur_memseg->next = NULL;
2280 				(*memsegpp)->next = cur_memseg;
2281 				break;
2282 			}
2283 			memsegpp = &((*memsegpp)->next);
2284 			ASSERT(*memsegpp != NULL);
2285 		}
2286 
2287 		/*
2288 		 * add_physmem() initializes the PSM part of the page
2289 		 * struct by calling the PSM back with add_physmem_cb().
2290 		 * In addition it coalesces pages into larger pages as
2291 		 * it initializes them.
2292 		 */
2293 		add_physmem(pp, num, base_pfn);
2294 		cur_memseg++;
2295 		availrmem_initial += num;
2296 		availrmem += num;
2297 
2298 		/*
2299 		 * If the caller provided the page frames to us, then
2300 		 * advance in that list.  Otherwise, prepare to allocate
2301 		 * our own page frames for the next memseg.
2302 		 */
2303 		pp = (inpp == NULL) ? NULL : pp + num;
2304 	}
2305 
2306 	PRM_DEBUG(availrmem_initial);
2307 	PRM_DEBUG(availrmem);
2308 	PRM_DEBUG(freemem);
2309 	build_pfn_hash();
2310 	return (pages_done);
2311 }
2312 
2313 /*
2314  * Kernel VM initialization.
2315  */
2316 static void
2317 kvm_init(void)
2318 {
2319 #ifdef DEBUG
2320 	extern void _start();
2321 
2322 	ASSERT((caddr_t)_start == s_text);
2323 #endif
2324 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
2325 
2326 	/*
2327 	 * Put the kernel segments in kernel address space.
2328 	 */
2329 	rw_enter(&kas.a_lock, RW_WRITER);
2330 	as_avlinit(&kas);
2331 
2332 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
2333 	(void) segkmem_create(&ktextseg);
2334 
2335 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
2336 	(void) segkmem_create(&kvalloc);
2337 
2338 	/*
2339 	 * We're about to map out /boot.  This is the beginning of the
2340 	 * system resource management transition. We can no longer
2341 	 * call into /boot for I/O or memory allocations.
2342 	 *
2343 	 * XX64 - Is this still correct with kernelheap_extend() being called
2344 	 * later than this????
2345 	 */
2346 	(void) seg_attach(&kas, final_kernelheap,
2347 	    ekernelheap - final_kernelheap, &kvseg);
2348 	(void) segkmem_create(&kvseg);
2349 
2350 #if defined(__amd64)
2351 	(void) seg_attach(&kas, (caddr_t)core_base, core_size, &kvseg_core);
2352 	(void) segkmem_create(&kvseg_core);
2353 #endif
2354 
2355 	(void) seg_attach(&kas, (caddr_t)SEGDEBUGBASE, (size_t)SEGDEBUGSIZE,
2356 	    &kdebugseg);
2357 	(void) segkmem_create(&kdebugseg);
2358 
2359 	rw_exit(&kas.a_lock);
2360 
2361 	/*
2362 	 * Ensure that the red zone at kernelbase is never accessible.
2363 	 */
2364 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
2365 
2366 	/*
2367 	 * Make the text writable so that it can be hot patched by DTrace.
2368 	 */
2369 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
2370 	    PROT_READ | PROT_WRITE | PROT_EXEC);
2371 
2372 	/*
2373 	 * Make data writable until end.
2374 	 */
2375 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
2376 	    PROT_READ | PROT_WRITE | PROT_EXEC);
2377 }
2378 
2379 /*
2380  * These are MTTR registers supported by P6
2381  */
2382 static struct	mtrrvar	mtrrphys_arr[MAX_MTRRVAR];
2383 static uint64_t mtrr64k, mtrr16k1, mtrr16k2;
2384 static uint64_t mtrr4k1, mtrr4k2, mtrr4k3;
2385 static uint64_t mtrr4k4, mtrr4k5, mtrr4k6;
2386 static uint64_t mtrr4k7, mtrr4k8, mtrrcap;
2387 uint64_t mtrrdef, pat_attr_reg;
2388 
2389 /*
2390  * Disable reprogramming of MTRRs by default.
2391  */
2392 int	enable_relaxed_mtrr = 0;
2393 
2394 /*
2395  * These must serve for Pentium, Pentium Pro (P6/Pentium II/Pentium III)
2396  * and Pentium 4, and yes, they are named 0, 1, 2, 4, 3 in ascending
2397  * address order (starting from 0x400).  The Pentium 4 only implements
2398  * 4 sets, and while they are named 0-3 in the doc, the corresponding
2399  * names for P6 are 0,1,2,4.  So define these arrays in address order
2400  * so that they work for both pre-Pentium4 and Pentium 4 processors.
2401  */
2402 
2403 static uint_t	mci_ctl[] = {REG_MC0_CTL, REG_MC1_CTL, REG_MC2_CTL,
2404 		    REG_MC4_CTL, REG_MC3_CTL};
2405 static uint_t	mci_status[] = {REG_MC0_STATUS, REG_MC1_STATUS, REG_MC2_STATUS,
2406 		    REG_MC4_STATUS, REG_MC3_STATUS};
2407 static uint_t	mci_addr[] = {REG_MC0_ADDR, REG_MC1_ADDR, REG_MC2_ADDR,
2408 		    REG_MC4_ADDR, REG_MC3_ADDR};
2409 static int	mca_cnt;
2410 
2411 
2412 void
2413 setup_mca()
2414 {
2415 	int 		i;
2416 	uint64_t	allzeros;
2417 	uint64_t	allones;
2418 	uint64_t	mca_cap;
2419 
2420 	if (!(x86_feature & X86_MCA))
2421 		return;
2422 	(void) rdmsr(REG_MCG_CAP, &mca_cap);
2423 	allones = 0xffffffffffffffffULL;
2424 	if (mca_cap & MCG_CAP_CTL_P)
2425 		(void) wrmsr(REG_MCG_CTL, &allones);
2426 	mca_cnt = mca_cap & MCG_CAP_COUNT_MASK;
2427 	if (mca_cnt > P6_MCG_CAP_COUNT)
2428 		mca_cnt = P6_MCG_CAP_COUNT;
2429 	for (i = 1; i < mca_cnt; i++)
2430 		(void) wrmsr(mci_ctl[i], &allones);
2431 	allzeros = 0;
2432 	for (i = 0; i < mca_cnt; i++)
2433 		(void) wrmsr(mci_status[i], &allzeros);
2434 	setcr4(getcr4() | CR4_MCE);
2435 
2436 }
2437 
2438 int
2439 mca_exception(struct regs *rp)
2440 {
2441 	uint64_t	status, addr;
2442 	uint64_t	allzeros;
2443 	uint64_t	buf;
2444 	int		i, ret = 1, errcode, mserrcode;
2445 
2446 	allzeros = 0;
2447 	(void) rdmsr(REG_MCG_STATUS, &buf);
2448 	status = buf;
2449 	if (status & MCG_STATUS_RIPV)
2450 		ret = 0;
2451 	if (status & MCG_STATUS_EIPV)
2452 		cmn_err(CE_WARN, "MCE at 0x%lx", rp->r_pc);
2453 	(void) wrmsr(REG_MCG_STATUS, &allzeros);
2454 	for (i = 0; i < mca_cnt; i++) {
2455 		(void) rdmsr(mci_status[i], &buf);
2456 		status = buf;
2457 		/*
2458 		 * If status register not valid skip this bank
2459 		 */
2460 		if (!(status & MCI_STATUS_VAL))
2461 			continue;
2462 		errcode = status & MCI_STATUS_ERRCODE;
2463 		mserrcode = (status  >> MSERRCODE_SHFT) & MCI_STATUS_ERRCODE;
2464 		if (status & MCI_STATUS_ADDRV) {
2465 			/*
2466 			 * If mci_addr contains the address where
2467 			 * error occurred, display the address
2468 			 */
2469 			(void) rdmsr(mci_addr[i], &buf);
2470 			addr = buf;
2471 			cmn_err(CE_WARN, "MCE: Bank %d: error code 0x%x:"\
2472 			    "addr = 0x%" PRIx64 ", model errcode = 0x%x", i,
2473 			    errcode, addr, mserrcode);
2474 		} else {
2475 			cmn_err(CE_WARN,
2476 			    "MCE: Bank %d: error code 0x%x, mserrcode = 0x%x",
2477 			    i, errcode, mserrcode);
2478 		}
2479 		(void) wrmsr(mci_status[i], &allzeros);
2480 	}
2481 	return (ret);
2482 }
2483 
2484 void
2485 setup_mtrr()
2486 {
2487 	int i, ecx;
2488 	int vcnt;
2489 	struct	mtrrvar	*mtrrphys;
2490 
2491 	if (!(x86_feature & X86_MTRR))
2492 		return;
2493 
2494 	(void) rdmsr(REG_MTRRCAP, &mtrrcap);
2495 	(void) rdmsr(REG_MTRRDEF, &mtrrdef);
2496 	if (mtrrcap & MTRRCAP_FIX) {
2497 		(void) rdmsr(REG_MTRR64K, &mtrr64k);
2498 		(void) rdmsr(REG_MTRR16K1, &mtrr16k1);
2499 		(void) rdmsr(REG_MTRR16K2, &mtrr16k2);
2500 		(void) rdmsr(REG_MTRR4K1, &mtrr4k1);
2501 		(void) rdmsr(REG_MTRR4K2, &mtrr4k2);
2502 		(void) rdmsr(REG_MTRR4K3, &mtrr4k3);
2503 		(void) rdmsr(REG_MTRR4K4, &mtrr4k4);
2504 		(void) rdmsr(REG_MTRR4K5, &mtrr4k5);
2505 		(void) rdmsr(REG_MTRR4K6, &mtrr4k6);
2506 		(void) rdmsr(REG_MTRR4K7, &mtrr4k7);
2507 		(void) rdmsr(REG_MTRR4K8, &mtrr4k8);
2508 	}
2509 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
2510 		vcnt = MAX_MTRRVAR;
2511 
2512 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
2513 		i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
2514 		(void) rdmsr(ecx, &mtrrphys->mtrrphys_base);
2515 		(void) rdmsr(ecx + 1, &mtrrphys->mtrrphys_mask);
2516 		if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
2517 			mtrrphys->mtrrphys_mask &= ~MTRRPHYSMASK_V;
2518 		}
2519 	}
2520 	if (x86_feature & X86_PAT) {
2521 		if (enable_relaxed_mtrr)
2522 			mtrrdef = MTRR_TYPE_WB|MTRRDEF_FE|MTRRDEF_E;
2523 		pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
2524 	}
2525 
2526 	mtrr_sync();
2527 }
2528 
2529 /*
2530  * Sync current cpu mtrr with the incore copy of mtrr.
2531  * This function has to be invoked with interrupts disabled
2532  * Currently we do not capture other cpu's. This is invoked on cpu0
2533  * just after reading /etc/system.
2534  * On other cpu's its invoked from mp_startup().
2535  */
2536 void
2537 mtrr_sync()
2538 {
2539 	uint64_t my_mtrrdef;
2540 	uint_t	crvalue, cr0_orig;
2541 	int	vcnt, i, ecx;
2542 	struct	mtrrvar	*mtrrphys;
2543 
2544 	cr0_orig = crvalue = getcr0();
2545 	crvalue |= CR0_CD;
2546 	crvalue &= ~CR0_NW;
2547 	setcr0(crvalue);
2548 	invalidate_cache();
2549 	setcr3(getcr3());
2550 
2551 	if (x86_feature & X86_PAT) {
2552 		(void) wrmsr(REG_MTRRPAT, &pat_attr_reg);
2553 	}
2554 	(void) rdmsr(REG_MTRRDEF, &my_mtrrdef);
2555 	my_mtrrdef &= ~MTRRDEF_E;
2556 	(void) wrmsr(REG_MTRRDEF, &my_mtrrdef);
2557 	if (mtrrcap & MTRRCAP_FIX) {
2558 		(void) wrmsr(REG_MTRR64K, &mtrr64k);
2559 		(void) wrmsr(REG_MTRR16K1, &mtrr16k1);
2560 		(void) wrmsr(REG_MTRR16K2, &mtrr16k2);
2561 		(void) wrmsr(REG_MTRR4K1, &mtrr4k1);
2562 		(void) wrmsr(REG_MTRR4K2, &mtrr4k2);
2563 		(void) wrmsr(REG_MTRR4K3, &mtrr4k3);
2564 		(void) wrmsr(REG_MTRR4K4, &mtrr4k4);
2565 		(void) wrmsr(REG_MTRR4K5, &mtrr4k5);
2566 		(void) wrmsr(REG_MTRR4K6, &mtrr4k6);
2567 		(void) wrmsr(REG_MTRR4K7, &mtrr4k7);
2568 		(void) wrmsr(REG_MTRR4K8, &mtrr4k8);
2569 	}
2570 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
2571 		vcnt = MAX_MTRRVAR;
2572 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
2573 		i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
2574 		(void) wrmsr(ecx, &mtrrphys->mtrrphys_base);
2575 		(void) wrmsr(ecx + 1, &mtrrphys->mtrrphys_mask);
2576 	}
2577 	(void) wrmsr(REG_MTRRDEF, &mtrrdef);
2578 	setcr3(getcr3());
2579 	invalidate_cache();
2580 	setcr0(cr0_orig);
2581 }
2582 
2583 /*
2584  * resync mtrr so that BIOS is happy. Called from mdboot
2585  */
2586 void
2587 mtrr_resync()
2588 {
2589 	if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
2590 		/*
2591 		 * We could have changed the default mtrr definition.
2592 		 * Put it back to uncached which is what it is at power on
2593 		 */
2594 		mtrrdef = MTRR_TYPE_UC|MTRRDEF_FE|MTRRDEF_E;
2595 		mtrr_sync();
2596 	}
2597 }
2598 
2599 void
2600 get_system_configuration()
2601 {
2602 	char	prop[32];
2603 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
2604 
2605 	if (((BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop)) ||
2606 		(BOP_GETPROP(bootops, "nodes", prop) < 0) 	||
2607 		(kobj_getvalue(prop, &nodes_ll) == -1) ||
2608 		(nodes_ll > MAXNODES))			   ||
2609 	    ((BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop)) ||
2610 		(BOP_GETPROP(bootops, "cpus_pernode", prop) < 0) ||
2611 		(kobj_getvalue(prop, &cpus_pernode_ll) == -1))) {
2612 
2613 		system_hardware.hd_nodes = 1;
2614 		system_hardware.hd_cpus_per_node = 0;
2615 	} else {
2616 		system_hardware.hd_nodes = (int)nodes_ll;
2617 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
2618 	}
2619 	if ((BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop)) ||
2620 		(BOP_GETPROP(bootops, "kernelbase", prop) < 0) 	||
2621 		(kobj_getvalue(prop, &lvalue) == -1))
2622 			eprom_kernelbase = NULL;
2623 	else
2624 			eprom_kernelbase = (uintptr_t)lvalue;
2625 
2626 	if ((BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop)) ||
2627 	    (BOP_GETPROP(bootops, "segmapsize", prop) < 0) ||
2628 	    (kobj_getvalue(prop, &lvalue) == -1)) {
2629 		segmapsize = SEGMAPDEFAULT;
2630 	} else {
2631 		segmapsize = (uintptr_t)lvalue;
2632 	}
2633 
2634 	if ((BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop)) ||
2635 	    (BOP_GETPROP(bootops, "segmapfreelists", prop) < 0) ||
2636 	    (kobj_getvalue(prop, &lvalue) == -1)) {
2637 		segmapfreelists = 0;	/* use segmap driver default */
2638 	} else {
2639 		segmapfreelists = (int)lvalue;
2640 	}
2641 }
2642 
2643 /*
2644  * Add to a memory list.
2645  * start = start of new memory segment
2646  * len = length of new memory segment in bytes
2647  * new = pointer to a new struct memlist
2648  * memlistp = memory list to which to add segment.
2649  */
2650 static void
2651 memlist_add(
2652 	uint64_t start,
2653 	uint64_t len,
2654 	struct memlist *new,
2655 	struct memlist **memlistp)
2656 {
2657 	struct memlist *cur;
2658 	uint64_t end = start + len;
2659 
2660 	new->address = start;
2661 	new->size = len;
2662 
2663 	cur = *memlistp;
2664 
2665 	while (cur) {
2666 		if (cur->address >= end) {
2667 			new->next = cur;
2668 			*memlistp = new;
2669 			new->prev = cur->prev;
2670 			cur->prev = new;
2671 			return;
2672 		}
2673 		ASSERT(cur->address + cur->size <= start);
2674 		if (cur->next == NULL) {
2675 			cur->next = new;
2676 			new->prev = cur;
2677 			new->next = NULL;
2678 			return;
2679 		}
2680 		memlistp = &cur->next;
2681 		cur = cur->next;
2682 	}
2683 }
2684 
2685 void
2686 kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
2687 {
2688 	size_t tsize = e_modtext - modtext;
2689 	size_t dsize = e_moddata - moddata;
2690 
2691 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
2692 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
2693 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
2694 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
2695 }
2696 
2697 caddr_t
2698 kobj_text_alloc(vmem_t *arena, size_t size)
2699 {
2700 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
2701 }
2702 
2703 /*ARGSUSED*/
2704 caddr_t
2705 kobj_texthole_alloc(caddr_t addr, size_t size)
2706 {
2707 	panic("unexpected call to kobj_texthole_alloc()");
2708 	/*NOTREACHED*/
2709 	return (0);
2710 }
2711 
2712 /*ARGSUSED*/
2713 void
2714 kobj_texthole_free(caddr_t addr, size_t size)
2715 {
2716 	panic("unexpected call to kobj_texthole_free()");
2717 }
2718 
2719 /*
2720  * This is called just after configure() in startup().
2721  *
2722  * The ISALIST concept is a bit hopeless on Intel, because
2723  * there's no guarantee of an ever-more-capable processor
2724  * given that various parts of the instruction set may appear
2725  * and disappear between different implementations.
2726  *
2727  * While it would be possible to correct it and even enhance
2728  * it somewhat, the explicit hardware capability bitmask allows
2729  * more flexibility.
2730  *
2731  * So, we just leave this alone.
2732  */
2733 void
2734 setx86isalist(void)
2735 {
2736 	char *tp;
2737 	size_t len;
2738 	extern char *isa_list;
2739 
2740 #define	TBUFSIZE	1024
2741 
2742 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
2743 	*tp = '\0';
2744 
2745 #if defined(__amd64)
2746 	(void) strcpy(tp, "amd64 ");
2747 #endif
2748 
2749 	switch (x86_vendor) {
2750 	case X86_VENDOR_Intel:
2751 	case X86_VENDOR_AMD:
2752 	case X86_VENDOR_TM:
2753 		if (x86_feature & X86_CMOV) {
2754 			/*
2755 			 * Pentium Pro or later
2756 			 */
2757 			(void) strcat(tp, "pentium_pro");
2758 			(void) strcat(tp, x86_feature & X86_MMX ?
2759 			    "+mmx pentium_pro " : " ");
2760 		}
2761 		/*FALLTHROUGH*/
2762 	case X86_VENDOR_Cyrix:
2763 		/*
2764 		 * The Cyrix 6x86 does not have any Pentium features
2765 		 * accessible while not at privilege level 0.
2766 		 */
2767 		if (x86_feature & X86_CPUID) {
2768 			(void) strcat(tp, "pentium");
2769 			(void) strcat(tp, x86_feature & X86_MMX ?
2770 			    "+mmx pentium " : " ");
2771 		}
2772 		break;
2773 	default:
2774 		break;
2775 	}
2776 	(void) strcat(tp, "i486 i386 i86");
2777 	len = strlen(tp) + 1;   /* account for NULL at end of string */
2778 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
2779 	kmem_free(tp, TBUFSIZE);
2780 
2781 #undef TBUFSIZE
2782 }
2783 
2784 
2785 #ifdef __amd64
2786 
2787 void *
2788 device_arena_alloc(size_t size, int vm_flag)
2789 {
2790 	return (vmem_alloc(device_arena, size, vm_flag));
2791 }
2792 
2793 void
2794 device_arena_free(void *vaddr, size_t size)
2795 {
2796 	vmem_free(device_arena, vaddr, size);
2797 }
2798 
2799 #else
2800 
2801 void *
2802 device_arena_alloc(size_t size, int vm_flag)
2803 {
2804 	caddr_t	vaddr;
2805 	uintptr_t v;
2806 	size_t	start;
2807 	size_t	end;
2808 
2809 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
2810 	if (vaddr == NULL)
2811 		return (NULL);
2812 
2813 	v = (uintptr_t)vaddr;
2814 	ASSERT(v >= kernelbase);
2815 	ASSERT(v + size <= ptable_va);
2816 
2817 	start = btop(v - kernelbase);
2818 	end = btop(v + size - 1 - kernelbase);
2819 	ASSERT(start < toxic_bit_map_len);
2820 	ASSERT(end < toxic_bit_map_len);
2821 
2822 	while (start <= end) {
2823 		BT_ATOMIC_SET(toxic_bit_map, start);
2824 		++start;
2825 	}
2826 	return (vaddr);
2827 }
2828 
2829 void
2830 device_arena_free(void *vaddr, size_t size)
2831 {
2832 	uintptr_t v = (uintptr_t)vaddr;
2833 	size_t	start;
2834 	size_t	end;
2835 
2836 	ASSERT(v >= kernelbase);
2837 	ASSERT(v + size <= ptable_va);
2838 
2839 	start = btop(v - kernelbase);
2840 	end = btop(v + size - 1 - kernelbase);
2841 	ASSERT(start < toxic_bit_map_len);
2842 	ASSERT(end < toxic_bit_map_len);
2843 
2844 	while (start <= end) {
2845 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
2846 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
2847 		++start;
2848 	}
2849 	vmem_free(heap_arena, vaddr, size);
2850 }
2851 
2852 /*
2853  * returns 1st address in range that is in device arena, or NULL
2854  * if len is not NULL it returns the length of the toxic range
2855  */
2856 void *
2857 device_arena_contains(void *vaddr, size_t size, size_t *len)
2858 {
2859 	uintptr_t v = (uintptr_t)vaddr;
2860 	uintptr_t eaddr = v + size;
2861 	size_t start;
2862 	size_t end;
2863 
2864 	/*
2865 	 * if called very early by kmdb, just return NULL
2866 	 */
2867 	if (toxic_bit_map == NULL)
2868 		return (NULL);
2869 
2870 	/*
2871 	 * First check if we're completely outside the bitmap range.
2872 	 */
2873 	if (v >= ptable_va || eaddr < kernelbase)
2874 		return (NULL);
2875 
2876 	/*
2877 	 * Trim ends of search to look at only what the bitmap covers.
2878 	 */
2879 	if (v < kernelbase)
2880 		v = kernelbase;
2881 	start = btop(v - kernelbase);
2882 	end = btop(eaddr - kernelbase);
2883 	if (end >= toxic_bit_map_len)
2884 		end = toxic_bit_map_len;
2885 
2886 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
2887 		return (NULL);
2888 
2889 	v = kernelbase + ptob(start);
2890 	if (len != NULL)
2891 		*len = ptob(end - start);
2892 	return ((void *)v);
2893 }
2894 
2895 #endif
2896