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