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