xref: /titanic_41/usr/src/uts/i86pc/os/startup.c (revision b86efd96f8acd85ddaa930a2f0c1d664237e4aaf)
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 
1589 		/* size is in bytes, segziosize is in pages */
1590 		if (segziosize == 0) {
1591 			size = mmu_ptob(physmem * 2);
1592 		} else {
1593 			size = mmu_ptob(segziosize);
1594 		}
1595 
1596 		if (size < SEGZIOMINSIZE) {
1597 			size = SEGZIOMINSIZE;
1598 		} else if (size > mmu_ptob(physmem * 4)) {
1599 			size = mmu_ptob(physmem * 4);
1600 		}
1601 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
1602 		segzio_base = final_kernelheap;
1603 		PRM_DEBUG(segziosize);
1604 		PRM_DEBUG(segzio_base);
1605 		final_kernelheap = segzio_base + mmu_ptob(segziosize);
1606 		PRM_DEBUG(final_kernelheap);
1607 	}
1608 
1609 	/*
1610 	 * put the range of VA for device mappings next
1611 	 */
1612 	toxic_addr = (uintptr_t)final_kernelheap;
1613 	PRM_DEBUG(toxic_addr);
1614 	final_kernelheap = (char *)toxic_addr + toxic_size;
1615 #endif
1616 	PRM_DEBUG(final_kernelheap);
1617 	ASSERT(final_kernelheap < boot_kernelheap);
1618 
1619 	/*
1620 	 * Users can change segmapsize through eeprom or /etc/system.
1621 	 * If the variable is tuned through eeprom, there is no upper
1622 	 * bound on the size of segmap.  If it is tuned through
1623 	 * /etc/system on 32-bit systems, it must be no larger than we
1624 	 * planned for in startup_memlist().
1625 	 */
1626 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
1627 	segkmap_start = ROUND_UP_LPAGE((uintptr_t)final_kernelheap);
1628 
1629 #if defined(__i386)
1630 	if (segmapsize > segmap_reserved) {
1631 		cmn_err(CE_NOTE, "!segmapsize may not be set > 0x%lx in "
1632 		    "/etc/system.  Use eeprom.", (long)SEGMAPMAX);
1633 		segmapsize = segmap_reserved;
1634 	}
1635 	/*
1636 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
1637 	 * the bottom of the kernel's address range.  Set aside space for a
1638 	 * red zone just below the start of segmap.
1639 	 */
1640 	segkmap_start += KERNEL_REDZONE_SIZE;
1641 	segmapsize -= KERNEL_REDZONE_SIZE;
1642 #endif
1643 	final_kernelheap = (char *)(segkmap_start + segmapsize);
1644 
1645 	PRM_DEBUG(segkmap_start);
1646 	PRM_DEBUG(segmapsize);
1647 	PRM_DEBUG(final_kernelheap);
1648 
1649 	/*
1650 	 * Initialize VM system
1651 	 */
1652 	PRM_POINT("Calling kvm_init()...");
1653 	kvm_init();
1654 	PRM_POINT("kvm_init() done");
1655 
1656 	/*
1657 	 * Tell kmdb that the VM system is now working
1658 	 */
1659 	if (boothowto & RB_DEBUG)
1660 		kdi_dvec_vmready();
1661 
1662 	/*
1663 	 * Mangle the brand string etc.
1664 	 */
1665 	cpuid_pass3(CPU);
1666 
1667 	PRM_DEBUG(final_kernelheap);
1668 
1669 	/*
1670 	 * Now that we can use memory outside the top 4GB (on 64-bit
1671 	 * systems) and we know the size of segmap, we can set the final
1672 	 * size of the kernel's heap.  Note: on 64-bit systems we still
1673 	 * can't touch anything in the bottom half of the top 4GB range
1674 	 * because boot still has pages mapped there.
1675 	 */
1676 	if (final_kernelheap < boot_kernelheap) {
1677 		kernelheap_extend(final_kernelheap, boot_kernelheap);
1678 #if defined(__amd64)
1679 		kmem_setaside = vmem_xalloc(heap_arena, BOOT_DOUBLEMAP_SIZE,
1680 		    MMU_PAGESIZE, 0, 0, (void *)(BOOT_DOUBLEMAP_BASE),
1681 		    (void *)(BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE),
1682 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
1683 		PRM_DEBUG(kmem_setaside);
1684 		if (kmem_setaside == NULL)
1685 			panic("Could not protect boot's memory");
1686 #endif
1687 	}
1688 	/*
1689 	 * Now that the kernel heap may have grown significantly, we need
1690 	 * to make all the remaining page_t's available to back that memory.
1691 	 *
1692 	 * XX64 this should probably wait till after release boot-strap too.
1693 	 */
1694 	pages_left = npages - boot_npages;
1695 	if (pages_left > 0) {
1696 		PRM_DEBUG(pages_left);
1697 		(void) kphysm_init(NULL, memseg_base, boot_npages, pages_left);
1698 	}
1699 
1700 #if defined(__amd64)
1701 
1702 	/*
1703 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
1704 	 */
1705 	device_arena = vmem_create("device", (void *)toxic_addr,
1706 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
1707 
1708 #else	/* __i386 */
1709 
1710 	/*
1711 	 * allocate the bit map that tracks toxic pages
1712 	 */
1713 	toxic_bit_map_len = btop((ulong_t)(ptable_va - kernelbase));
1714 	PRM_DEBUG(toxic_bit_map_len);
1715 	toxic_bit_map =
1716 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
1717 	ASSERT(toxic_bit_map != NULL);
1718 	PRM_DEBUG(toxic_bit_map);
1719 
1720 #endif	/* __i386 */
1721 
1722 
1723 	/*
1724 	 * Now that we've got more VA, as well as the ability to allocate from
1725 	 * it, tell the debugger.
1726 	 */
1727 	if (boothowto & RB_DEBUG)
1728 		kdi_dvec_memavail();
1729 
1730 	/*
1731 	 * The following code installs a special page fault handler (#pf)
1732 	 * to work around a pentium bug.
1733 	 */
1734 #if !defined(__amd64)
1735 	if (x86_type == X86_TYPE_P5) {
1736 		gate_desc_t *newidt;
1737 		desctbr_t    newidt_r;
1738 
1739 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
1740 			panic("failed to install pentium_pftrap");
1741 
1742 		bcopy(idt0, newidt, sizeof (idt0));
1743 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
1744 		    KCS_SEL, 0, SDT_SYSIGT, SEL_KPL);
1745 
1746 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
1747 		    PROT_READ|PROT_EXEC);
1748 
1749 		newidt_r.dtr_limit = sizeof (idt0) - 1;
1750 		newidt_r.dtr_base = (uintptr_t)newidt;
1751 		CPU->cpu_idt = newidt;
1752 		wr_idtr(&newidt_r);
1753 	}
1754 #endif	/* !__amd64 */
1755 
1756 	/*
1757 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
1758 	 * parameters left there by controllers/BIOS.
1759 	 */
1760 	PRM_POINT("setup up p0_va");
1761 	p0_va = i86devmap(0, 1, PROT_READ);
1762 	PRM_DEBUG(p0_va);
1763 
1764 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
1765 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
1766 
1767 	/*
1768 	 * disable automatic large pages for small memory systems or
1769 	 * when the disable flag is set.
1770 	 */
1771 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
1772 		max_uheap_lpsize = LEVEL_SIZE(1);
1773 		max_ustack_lpsize = LEVEL_SIZE(1);
1774 		max_privmap_lpsize = LEVEL_SIZE(1);
1775 		max_uidata_lpsize = LEVEL_SIZE(1);
1776 		max_utext_lpsize = LEVEL_SIZE(1);
1777 		max_shm_lpsize = LEVEL_SIZE(1);
1778 	}
1779 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
1780 	    auto_lpg_disable) {
1781 		use_brk_lpg = 0;
1782 		use_stk_lpg = 0;
1783 	}
1784 	if (mmu.max_page_level > 0) {
1785 		mcntl0_lpsize = LEVEL_SIZE(1);
1786 	}
1787 
1788 	PRM_POINT("Calling hat_init_finish()...");
1789 	hat_init_finish();
1790 	PRM_POINT("hat_init_finish() done");
1791 
1792 	/*
1793 	 * Initialize the segkp segment type.
1794 	 */
1795 	rw_enter(&kas.a_lock, RW_WRITER);
1796 	if (!segkp_fromheap) {
1797 		if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
1798 		    segkp) < 0) {
1799 			panic("startup: cannot attach segkp");
1800 			/*NOTREACHED*/
1801 		}
1802 	} else {
1803 		/*
1804 		 * For 32 bit x86 systems, we will have segkp under the heap.
1805 		 * There will not be a segkp segment.  We do, however, need
1806 		 * to fill in the seg structure.
1807 		 */
1808 		segkp->s_as = &kas;
1809 	}
1810 	if (segkp_create(segkp) != 0) {
1811 		panic("startup: segkp_create failed");
1812 		/*NOTREACHED*/
1813 	}
1814 	PRM_DEBUG(segkp);
1815 	rw_exit(&kas.a_lock);
1816 
1817 	/*
1818 	 * kpm segment
1819 	 */
1820 	segmap_kpm = 0;
1821 	if (kpm_desired) {
1822 		kpm_init();
1823 		kpm_enable = 1;
1824 		vpm_enable = 1;
1825 	}
1826 
1827 	/*
1828 	 * Now create segmap segment.
1829 	 */
1830 	rw_enter(&kas.a_lock, RW_WRITER);
1831 	if (seg_attach(&kas, (caddr_t)segkmap_start, segmapsize, segkmap) < 0) {
1832 		panic("cannot attach segkmap");
1833 		/*NOTREACHED*/
1834 	}
1835 	PRM_DEBUG(segkmap);
1836 
1837 	/*
1838 	 * The 64 bit HAT permanently maps only segmap's page tables.
1839 	 * The 32 bit HAT maps the heap's page tables too.
1840 	 */
1841 #if defined(__amd64)
1842 	hat_kmap_init(segkmap_start, segmapsize);
1843 #else /* __i386 */
1844 	ASSERT(segkmap_start + segmapsize == (uintptr_t)final_kernelheap);
1845 	hat_kmap_init(segkmap_start, (uintptr_t)ekernelheap - segkmap_start);
1846 #endif /* __i386 */
1847 
1848 	a.prot = PROT_READ | PROT_WRITE;
1849 	a.shmsize = 0;
1850 	a.nfreelist = segmapfreelists;
1851 
1852 	if (segmap_create(segkmap, (caddr_t)&a) != 0)
1853 		panic("segmap_create segkmap");
1854 	rw_exit(&kas.a_lock);
1855 
1856 	setup_vaddr_for_ppcopy(CPU);
1857 
1858 	segdev_init();
1859 	pmem_init();
1860 	PRM_POINT("startup_vm() done");
1861 }
1862 
1863 static void
1864 startup_end(void)
1865 {
1866 	extern void setx86isalist(void);
1867 
1868 	PRM_POINT("startup_end() starting...");
1869 
1870 	/*
1871 	 * Perform tasks that get done after most of the VM
1872 	 * initialization has been done but before the clock
1873 	 * and other devices get started.
1874 	 */
1875 	kern_setup1();
1876 
1877 	/*
1878 	 * Perform CPC initialization for this CPU.
1879 	 */
1880 	kcpc_hw_init(CPU);
1881 
1882 #if defined(__amd64)
1883 	/*
1884 	 * Validate support for syscall/sysret
1885 	 * XX64 -- include SSE, SSE2, etc. here too?
1886 	 */
1887 	if ((x86_feature & X86_ASYSC) == 0) {
1888 		cmn_err(CE_WARN,
1889 		    "cpu%d does not support syscall/sysret", CPU->cpu_id);
1890 	}
1891 #endif
1892 
1893 #if defined(OPTERON_WORKAROUND_6323525)
1894 	if (opteron_workaround_6323525)
1895 		patch_workaround_6323525();
1896 #endif
1897 	/*
1898 	 * Configure the system.
1899 	 */
1900 	PRM_POINT("Calling configure()...");
1901 	configure();		/* set up devices */
1902 	PRM_POINT("configure() done");
1903 
1904 	/*
1905 	 * Set the isa_list string to the defined instruction sets we
1906 	 * support.
1907 	 */
1908 	setx86isalist();
1909 	cpu_intr_alloc(CPU, NINTR_THREADS);
1910 	psm_install();
1911 
1912 	/*
1913 	 * We're done with bootops.  We don't unmap the bootstrap yet because
1914 	 * we're still using bootsvcs.
1915 	 */
1916 	PRM_POINT("zeroing out bootops");
1917 	*bootopsp = (struct bootops *)0;
1918 	bootops = (struct bootops *)NULL;
1919 
1920 	PRM_POINT("Enabling interrupts");
1921 	(*picinitf)();
1922 	sti();
1923 
1924 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
1925 		"softlevel1", NULL, NULL); /* XXX to be moved later */
1926 
1927 	PRM_POINT("startup_end() done");
1928 }
1929 
1930 extern char hw_serial[];
1931 char *_hs1107 = hw_serial;
1932 ulong_t  _bdhs34;
1933 
1934 void
1935 post_startup(void)
1936 {
1937 	/*
1938 	 * Set the system wide, processor-specific flags to be passed
1939 	 * to userland via the aux vector for performance hints and
1940 	 * instruction set extensions.
1941 	 */
1942 	bind_hwcap();
1943 
1944 	/*
1945 	 * Load the System Management BIOS into the global ksmbios handle,
1946 	 * if an SMBIOS is present on this system.
1947 	 */
1948 	ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
1949 
1950 	/*
1951 	 * Startup memory scrubber.
1952 	 */
1953 	memscrub_init();
1954 
1955 	/*
1956 	 * Complete CPU module initialization
1957 	 */
1958 	cmi_post_init();
1959 
1960 	/*
1961 	 * Perform forceloading tasks for /etc/system.
1962 	 */
1963 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
1964 
1965 	/*
1966 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
1967 	 * ON4.0: This must be fixed or restated in /etc/systems.
1968 	 */
1969 	(void) modload("fs", "procfs");
1970 
1971 #if defined(__i386)
1972 	/*
1973 	 * Check for required functional Floating Point hardware,
1974 	 * unless FP hardware explicitly disabled.
1975 	 */
1976 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
1977 		halt("No working FP hardware found");
1978 #endif
1979 
1980 	maxmem = freemem;
1981 
1982 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
1983 
1984 	/*
1985 	 * Perform the formal initialization of the boot chip,
1986 	 * and associate the boot cpu with it.
1987 	 * This must be done after the cpu node for CPU has been
1988 	 * added to the device tree, when the necessary probing to
1989 	 * know the chip type and chip "id" is performed.
1990 	 */
1991 	chip_cpu_init(CPU);
1992 	chip_cpu_assign(CPU);
1993 }
1994 
1995 static int
1996 pp_in_ramdisk(page_t *pp)
1997 {
1998 	extern uint64_t ramdisk_start, ramdisk_end;
1999 
2000 	return ((pp->p_pagenum >= btop(ramdisk_start)) &&
2001 	    (pp->p_pagenum < btopr(ramdisk_end)));
2002 }
2003 
2004 void
2005 release_bootstrap(void)
2006 {
2007 	int root_is_ramdisk;
2008 	pfn_t pfn;
2009 	page_t *pp;
2010 	extern void kobj_boot_unmountroot(void);
2011 	extern dev_t rootdev;
2012 
2013 	/* unmount boot ramdisk and release kmem usage */
2014 	kobj_boot_unmountroot();
2015 
2016 	/*
2017 	 * We're finished using the boot loader so free its pages.
2018 	 */
2019 	PRM_POINT("Unmapping lower boot pages");
2020 	clear_boot_mappings(0, kernelbase);
2021 #if defined(__amd64)
2022 	PRM_POINT("Unmapping upper boot pages");
2023 	clear_boot_mappings(BOOT_DOUBLEMAP_BASE,
2024 	    BOOT_DOUBLEMAP_BASE + BOOT_DOUBLEMAP_SIZE);
2025 #endif
2026 
2027 	/*
2028 	 * If root isn't on ramdisk, destroy the hardcoded
2029 	 * ramdisk node now and release the memory. Else,
2030 	 * ramdisk memory is kept in rd_pages.
2031 	 */
2032 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
2033 	if (!root_is_ramdisk) {
2034 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
2035 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
2036 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
2037 		(void) ddi_remove_child(dip, 0);
2038 	}
2039 
2040 	PRM_POINT("Releasing boot pages");
2041 	while (bootpages) {
2042 		pp = bootpages;
2043 		bootpages = pp->p_next;
2044 		if (root_is_ramdisk && pp_in_ramdisk(pp)) {
2045 			pp->p_next = rd_pages;
2046 			rd_pages = pp;
2047 			continue;
2048 		}
2049 		pp->p_next = (struct page *)0;
2050 		page_free(pp, 1);
2051 	}
2052 
2053 	/*
2054 	 * Find 1 page below 1 MB so that other processors can boot up.
2055 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
2056 	 * We should have just free'd one up.
2057 	 */
2058 	if (use_mp) {
2059 		for (pfn = 1; pfn < btop(1*1024*1024); pfn++) {
2060 			if (page_numtopp_alloc(pfn) == NULL)
2061 				continue;
2062 			rm_platter_va = i86devmap(pfn, 1,
2063 			    PROT_READ | PROT_WRITE | PROT_EXEC);
2064 			rm_platter_pa = ptob(pfn);
2065 			hat_devload(kas.a_hat,
2066 			    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
2067 			    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
2068 			    HAT_LOAD_NOCONSIST);
2069 			break;
2070 		}
2071 		if (pfn == btop(1*1024*1024))
2072 			panic("No page available for starting "
2073 			    "other processors");
2074 	}
2075 
2076 #if defined(__amd64)
2077 	PRM_POINT("Returning boot's VA space to kernel heap");
2078 	if (kmem_setaside != NULL)
2079 		vmem_free(heap_arena, kmem_setaside, BOOT_DOUBLEMAP_SIZE);
2080 #endif
2081 }
2082 
2083 /*
2084  * Initialize the platform-specific parts of a page_t.
2085  */
2086 void
2087 add_physmem_cb(page_t *pp, pfn_t pnum)
2088 {
2089 	pp->p_pagenum = pnum;
2090 	pp->p_mapping = NULL;
2091 	pp->p_embed = 0;
2092 	pp->p_share = 0;
2093 	pp->p_mlentry = 0;
2094 }
2095 
2096 /*
2097  * kphysm_init() initializes physical memory.
2098  */
2099 static pgcnt_t
2100 kphysm_init(
2101 	page_t *inpp,
2102 	struct memseg *memsegp,
2103 	pgcnt_t start,
2104 	pgcnt_t npages)
2105 {
2106 	struct memlist	*pmem;
2107 	struct memseg	*cur_memseg;
2108 	struct memseg	**memsegpp;
2109 	pfn_t		base_pfn;
2110 	pgcnt_t		num;
2111 	pgcnt_t		total_skipped = 0;
2112 	pgcnt_t		skipping = 0;
2113 	pgcnt_t		pages_done = 0;
2114 	pgcnt_t		largepgcnt;
2115 	uint64_t	addr;
2116 	uint64_t	size;
2117 	page_t		*pp = inpp;
2118 	int		dobreak = 0;
2119 	extern pfn_t	ddiphysmin;
2120 
2121 	ASSERT(page_hash != NULL && page_hashsz != 0);
2122 
2123 	for (cur_memseg = memsegp; cur_memseg->pages != NULL; cur_memseg++);
2124 	ASSERT(cur_memseg == memsegp || start > 0);
2125 
2126 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
2127 		/*
2128 		 * In a 32 bit kernel can't use higher memory if we're
2129 		 * not booting in PAE mode. This check takes care of that.
2130 		 */
2131 		addr = pmem->address;
2132 		size = pmem->size;
2133 		if (btop(addr) > physmax)
2134 			continue;
2135 
2136 		/*
2137 		 * align addr and size - they may not be at page boundaries
2138 		 */
2139 		if ((addr & MMU_PAGEOFFSET) != 0) {
2140 			addr += MMU_PAGEOFFSET;
2141 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
2142 			size -= addr - pmem->address;
2143 		}
2144 
2145 		/* only process pages below or equal to physmax */
2146 		if ((btop(addr + size) - 1) > physmax)
2147 			size = ptob(physmax - btop(addr) + 1);
2148 
2149 		num = btop(size);
2150 		if (num == 0)
2151 			continue;
2152 
2153 		if (total_skipped < start) {
2154 			if (start - total_skipped > num) {
2155 				total_skipped += num;
2156 				continue;
2157 			}
2158 			skipping = start - total_skipped;
2159 			num -= skipping;
2160 			addr += (MMU_PAGESIZE * skipping);
2161 			total_skipped = start;
2162 		}
2163 		if (num == 0)
2164 			continue;
2165 
2166 		if (num > npages)
2167 			num = npages;
2168 
2169 		npages -= num;
2170 		pages_done += num;
2171 		base_pfn = btop(addr);
2172 
2173 		/*
2174 		 * If the caller didn't provide space for the page
2175 		 * structures, carve them out of the memseg they will
2176 		 * represent.
2177 		 */
2178 		if (pp == NULL) {
2179 			pgcnt_t pp_pgs;
2180 
2181 			if (num <= 1)
2182 				continue;
2183 
2184 			/*
2185 			 * Compute how many of the pages we need to use for
2186 			 * page_ts
2187 			 */
2188 			pp_pgs = (num * sizeof (page_t)) / MMU_PAGESIZE + 1;
2189 			while (mmu_ptob(pp_pgs - 1) / sizeof (page_t) >=
2190 			    num - pp_pgs + 1)
2191 				--pp_pgs;
2192 			PRM_DEBUG(pp_pgs);
2193 
2194 			pp = vmem_alloc(heap_arena, mmu_ptob(pp_pgs),
2195 			    VM_NOSLEEP);
2196 			if (pp == NULL) {
2197 				cmn_err(CE_WARN, "Unable to add %ld pages to "
2198 				    "the system.", num);
2199 				continue;
2200 			}
2201 
2202 			hat_devload(kas.a_hat, (void *)pp, mmu_ptob(pp_pgs),
2203 			    base_pfn, PROT_READ | PROT_WRITE | HAT_UNORDERED_OK,
2204 			    HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
2205 			bzero(pp, mmu_ptob(pp_pgs));
2206 			num -= pp_pgs;
2207 			base_pfn += pp_pgs;
2208 		}
2209 
2210 		if (prom_debug)
2211 			prom_printf("MEMSEG addr=0x%" PRIx64
2212 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
2213 			    addr, num, base_pfn, base_pfn + num);
2214 
2215 		/*
2216 		 * drop pages below ddiphysmin to simplify ddi memory
2217 		 * allocation with non-zero addr_lo requests.
2218 		 */
2219 		if (base_pfn < ddiphysmin) {
2220 			if (base_pfn + num <= ddiphysmin) {
2221 				/* drop entire range below ddiphysmin */
2222 				continue;
2223 			}
2224 			/* adjust range to ddiphysmin */
2225 			pp += (ddiphysmin - base_pfn);
2226 			num -= (ddiphysmin - base_pfn);
2227 			base_pfn = ddiphysmin;
2228 		}
2229 		/*
2230 		 * Build the memsegs entry
2231 		 */
2232 		cur_memseg->pages = pp;
2233 		cur_memseg->epages = pp + num;
2234 		cur_memseg->pages_base = base_pfn;
2235 		cur_memseg->pages_end = base_pfn + num;
2236 
2237 		/*
2238 		 * insert in memseg list in decreasing pfn range order.
2239 		 * Low memory is typically more fragmented such that this
2240 		 * ordering keeps the larger ranges at the front of the list
2241 		 * for code that searches memseg.
2242 		 */
2243 		memsegpp = &memsegs;
2244 		for (;;) {
2245 			if (*memsegpp == NULL) {
2246 				/* empty memsegs */
2247 				memsegs = cur_memseg;
2248 				break;
2249 			}
2250 			/* check for continuity with start of memsegpp */
2251 			if (cur_memseg->pages_end == (*memsegpp)->pages_base) {
2252 				if (cur_memseg->epages == (*memsegpp)->pages) {
2253 					/*
2254 					 * contiguous pfn and page_t's. Merge
2255 					 * cur_memseg into *memsegpp. Drop
2256 					 * cur_memseg
2257 					 */
2258 					(*memsegpp)->pages_base =
2259 					    cur_memseg->pages_base;
2260 					(*memsegpp)->pages =
2261 					    cur_memseg->pages;
2262 					/*
2263 					 * check if contiguous with the end of
2264 					 * the next memseg.
2265 					 */
2266 					if ((*memsegpp)->next &&
2267 					    ((*memsegpp)->pages_base ==
2268 					    (*memsegpp)->next->pages_end)) {
2269 						cur_memseg = *memsegpp;
2270 						memsegpp = &((*memsegpp)->next);
2271 						dobreak = 1;
2272 					} else {
2273 						break;
2274 					}
2275 				} else {
2276 					/*
2277 					 * contiguous pfn but not page_t's.
2278 					 * drop last pfn/page_t in cur_memseg
2279 					 * to prevent creation of large pages
2280 					 * with noncontiguous page_t's if not
2281 					 * aligned to largest page boundary.
2282 					 */
2283 					largepgcnt = page_get_pagecnt(
2284 					    page_num_pagesizes() - 1);
2285 
2286 					if (cur_memseg->pages_end &
2287 					    (largepgcnt - 1)) {
2288 						num--;
2289 						cur_memseg->epages--;
2290 						cur_memseg->pages_end--;
2291 					}
2292 				}
2293 			}
2294 
2295 			/* check for continuity with end of memsegpp */
2296 			if (cur_memseg->pages_base == (*memsegpp)->pages_end) {
2297 				if (cur_memseg->pages == (*memsegpp)->epages) {
2298 					/*
2299 					 * contiguous pfn and page_t's. Merge
2300 					 * cur_memseg into *memsegpp. Drop
2301 					 * cur_memseg.
2302 					 */
2303 					if (dobreak) {
2304 						/* merge previously done */
2305 						cur_memseg->pages =
2306 						    (*memsegpp)->pages;
2307 						cur_memseg->pages_base =
2308 						    (*memsegpp)->pages_base;
2309 						cur_memseg->next =
2310 						    (*memsegpp)->next;
2311 					} else {
2312 						(*memsegpp)->pages_end =
2313 						    cur_memseg->pages_end;
2314 						(*memsegpp)->epages =
2315 						    cur_memseg->epages;
2316 					}
2317 					break;
2318 				}
2319 				/*
2320 				 * contiguous pfn but not page_t's.
2321 				 * drop first pfn/page_t in cur_memseg
2322 				 * to prevent creation of large pages
2323 				 * with noncontiguous page_t's if not
2324 				 * aligned to largest page boundary.
2325 				 */
2326 				largepgcnt = page_get_pagecnt(
2327 				    page_num_pagesizes() - 1);
2328 				if (base_pfn & (largepgcnt - 1)) {
2329 					num--;
2330 					base_pfn++;
2331 					cur_memseg->pages++;
2332 					cur_memseg->pages_base++;
2333 					pp = cur_memseg->pages;
2334 				}
2335 				if (dobreak)
2336 					break;
2337 			}
2338 
2339 			if (cur_memseg->pages_base >=
2340 			    (*memsegpp)->pages_end) {
2341 				cur_memseg->next = *memsegpp;
2342 				*memsegpp = cur_memseg;
2343 				break;
2344 			}
2345 			if ((*memsegpp)->next == NULL) {
2346 				cur_memseg->next = NULL;
2347 				(*memsegpp)->next = cur_memseg;
2348 				break;
2349 			}
2350 			memsegpp = &((*memsegpp)->next);
2351 			ASSERT(*memsegpp != NULL);
2352 		}
2353 
2354 		/*
2355 		 * add_physmem() initializes the PSM part of the page
2356 		 * struct by calling the PSM back with add_physmem_cb().
2357 		 * In addition it coalesces pages into larger pages as
2358 		 * it initializes them.
2359 		 */
2360 		add_physmem(pp, num, base_pfn);
2361 		cur_memseg++;
2362 		availrmem_initial += num;
2363 		availrmem += num;
2364 
2365 		/*
2366 		 * If the caller provided the page frames to us, then
2367 		 * advance in that list.  Otherwise, prepare to allocate
2368 		 * our own page frames for the next memseg.
2369 		 */
2370 		pp = (inpp == NULL) ? NULL : pp + num;
2371 	}
2372 
2373 	PRM_DEBUG(availrmem_initial);
2374 	PRM_DEBUG(availrmem);
2375 	PRM_DEBUG(freemem);
2376 	build_pfn_hash();
2377 	return (pages_done);
2378 }
2379 
2380 /*
2381  * Kernel VM initialization.
2382  */
2383 static void
2384 kvm_init(void)
2385 {
2386 #ifdef DEBUG
2387 	extern void _start();
2388 
2389 	ASSERT((caddr_t)_start == s_text);
2390 #endif
2391 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
2392 
2393 	/*
2394 	 * Put the kernel segments in kernel address space.
2395 	 */
2396 	rw_enter(&kas.a_lock, RW_WRITER);
2397 	as_avlinit(&kas);
2398 
2399 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
2400 	(void) segkmem_create(&ktextseg);
2401 
2402 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
2403 	(void) segkmem_create(&kvalloc);
2404 
2405 	/*
2406 	 * We're about to map out /boot.  This is the beginning of the
2407 	 * system resource management transition. We can no longer
2408 	 * call into /boot for I/O or memory allocations.
2409 	 *
2410 	 * XX64 - Is this still correct with kernelheap_extend() being called
2411 	 * later than this????
2412 	 */
2413 	(void) seg_attach(&kas, final_kernelheap,
2414 	    ekernelheap - final_kernelheap, &kvseg);
2415 	(void) segkmem_create(&kvseg);
2416 
2417 #if defined(__amd64)
2418 	(void) seg_attach(&kas, (caddr_t)core_base, core_size, &kvseg_core);
2419 	(void) segkmem_create(&kvseg_core);
2420 
2421 	/* segzio optimization is only valid for 64-bit kernels */
2422 	if (!segzio_fromheap) {
2423 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2424 		    &kzioseg);
2425 		(void) segkmem_zio_create(&kzioseg);
2426 
2427 		/* create zio area covering new segment */
2428 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2429 	}
2430 #endif
2431 
2432 	(void) seg_attach(&kas, (caddr_t)SEGDEBUGBASE, (size_t)SEGDEBUGSIZE,
2433 	    &kdebugseg);
2434 	(void) segkmem_create(&kdebugseg);
2435 
2436 	rw_exit(&kas.a_lock);
2437 
2438 	/*
2439 	 * Ensure that the red zone at kernelbase is never accessible.
2440 	 */
2441 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
2442 
2443 	/*
2444 	 * Make the text writable so that it can be hot patched by DTrace.
2445 	 */
2446 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
2447 	    PROT_READ | PROT_WRITE | PROT_EXEC);
2448 
2449 	/*
2450 	 * Make data writable until end.
2451 	 */
2452 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
2453 	    PROT_READ | PROT_WRITE | PROT_EXEC);
2454 }
2455 
2456 /*
2457  * These are MTTR registers supported by P6
2458  */
2459 static struct	mtrrvar	mtrrphys_arr[MAX_MTRRVAR];
2460 static uint64_t mtrr64k, mtrr16k1, mtrr16k2;
2461 static uint64_t mtrr4k1, mtrr4k2, mtrr4k3;
2462 static uint64_t mtrr4k4, mtrr4k5, mtrr4k6;
2463 static uint64_t mtrr4k7, mtrr4k8, mtrrcap;
2464 uint64_t mtrrdef, pat_attr_reg;
2465 
2466 /*
2467  * Disable reprogramming of MTRRs by default.
2468  */
2469 int	enable_relaxed_mtrr = 0;
2470 
2471 void
2472 setup_mtrr(void)
2473 {
2474 	int i, ecx;
2475 	int vcnt;
2476 	struct	mtrrvar	*mtrrphys;
2477 
2478 	if (!(x86_feature & X86_MTRR))
2479 		return;
2480 
2481 	mtrrcap = rdmsr(REG_MTRRCAP);
2482 	mtrrdef = rdmsr(REG_MTRRDEF);
2483 	if (mtrrcap & MTRRCAP_FIX) {
2484 		mtrr64k = rdmsr(REG_MTRR64K);
2485 		mtrr16k1 = rdmsr(REG_MTRR16K1);
2486 		mtrr16k2 = rdmsr(REG_MTRR16K2);
2487 		mtrr4k1 = rdmsr(REG_MTRR4K1);
2488 		mtrr4k2 = rdmsr(REG_MTRR4K2);
2489 		mtrr4k3 = rdmsr(REG_MTRR4K3);
2490 		mtrr4k4 = rdmsr(REG_MTRR4K4);
2491 		mtrr4k5 = rdmsr(REG_MTRR4K5);
2492 		mtrr4k6 = rdmsr(REG_MTRR4K6);
2493 		mtrr4k7 = rdmsr(REG_MTRR4K7);
2494 		mtrr4k8 = rdmsr(REG_MTRR4K8);
2495 	}
2496 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
2497 		vcnt = MAX_MTRRVAR;
2498 
2499 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
2500 		i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
2501 		mtrrphys->mtrrphys_base = rdmsr(ecx);
2502 		mtrrphys->mtrrphys_mask = rdmsr(ecx + 1);
2503 		if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
2504 			mtrrphys->mtrrphys_mask &= ~MTRRPHYSMASK_V;
2505 		}
2506 	}
2507 	if (x86_feature & X86_PAT) {
2508 		if (enable_relaxed_mtrr)
2509 			mtrrdef = MTRR_TYPE_WB|MTRRDEF_FE|MTRRDEF_E;
2510 		pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
2511 	}
2512 
2513 	mtrr_sync();
2514 }
2515 
2516 /*
2517  * Sync current cpu mtrr with the incore copy of mtrr.
2518  * This function has to be invoked with interrupts disabled
2519  * Currently we do not capture other cpu's. This is invoked on cpu0
2520  * just after reading /etc/system.
2521  * On other cpu's its invoked from mp_startup().
2522  */
2523 void
2524 mtrr_sync(void)
2525 {
2526 	uint_t	crvalue, cr0_orig;
2527 	int	vcnt, i, ecx;
2528 	struct	mtrrvar	*mtrrphys;
2529 
2530 	cr0_orig = crvalue = getcr0();
2531 	crvalue |= CR0_CD;
2532 	crvalue &= ~CR0_NW;
2533 	setcr0(crvalue);
2534 	invalidate_cache();
2535 	setcr3(getcr3());
2536 
2537 	if (x86_feature & X86_PAT)
2538 		wrmsr(REG_MTRRPAT, pat_attr_reg);
2539 
2540 	wrmsr(REG_MTRRDEF, rdmsr(REG_MTRRDEF) &
2541 	    ~((uint64_t)(uintptr_t)MTRRDEF_E));
2542 
2543 	if (mtrrcap & MTRRCAP_FIX) {
2544 		wrmsr(REG_MTRR64K, mtrr64k);
2545 		wrmsr(REG_MTRR16K1, mtrr16k1);
2546 		wrmsr(REG_MTRR16K2, mtrr16k2);
2547 		wrmsr(REG_MTRR4K1, mtrr4k1);
2548 		wrmsr(REG_MTRR4K2, mtrr4k2);
2549 		wrmsr(REG_MTRR4K3, mtrr4k3);
2550 		wrmsr(REG_MTRR4K4, mtrr4k4);
2551 		wrmsr(REG_MTRR4K5, mtrr4k5);
2552 		wrmsr(REG_MTRR4K6, mtrr4k6);
2553 		wrmsr(REG_MTRR4K7, mtrr4k7);
2554 		wrmsr(REG_MTRR4K8, mtrr4k8);
2555 	}
2556 	if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR)
2557 		vcnt = MAX_MTRRVAR;
2558 	for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr;
2559 	    i <  vcnt - 1; i++, ecx += 2, mtrrphys++) {
2560 		wrmsr(ecx, mtrrphys->mtrrphys_base);
2561 		wrmsr(ecx + 1, mtrrphys->mtrrphys_mask);
2562 	}
2563 	wrmsr(REG_MTRRDEF, mtrrdef);
2564 	setcr3(getcr3());
2565 	invalidate_cache();
2566 	setcr0(cr0_orig);
2567 }
2568 
2569 /*
2570  * resync mtrr so that BIOS is happy. Called from mdboot
2571  */
2572 void
2573 mtrr_resync(void)
2574 {
2575 	if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) {
2576 		/*
2577 		 * We could have changed the default mtrr definition.
2578 		 * Put it back to uncached which is what it is at power on
2579 		 */
2580 		mtrrdef = MTRR_TYPE_UC|MTRRDEF_FE|MTRRDEF_E;
2581 		mtrr_sync();
2582 	}
2583 }
2584 
2585 void
2586 get_system_configuration(void)
2587 {
2588 	char	prop[32];
2589 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
2590 
2591 	if (((BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop)) ||
2592 		(BOP_GETPROP(bootops, "nodes", prop) < 0) 	||
2593 		(kobj_getvalue(prop, &nodes_ll) == -1) ||
2594 		(nodes_ll > MAXNODES))			   ||
2595 	    ((BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop)) ||
2596 		(BOP_GETPROP(bootops, "cpus_pernode", prop) < 0) ||
2597 		(kobj_getvalue(prop, &cpus_pernode_ll) == -1))) {
2598 
2599 		system_hardware.hd_nodes = 1;
2600 		system_hardware.hd_cpus_per_node = 0;
2601 	} else {
2602 		system_hardware.hd_nodes = (int)nodes_ll;
2603 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
2604 	}
2605 	if ((BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop)) ||
2606 		(BOP_GETPROP(bootops, "kernelbase", prop) < 0) 	||
2607 		(kobj_getvalue(prop, &lvalue) == -1))
2608 			eprom_kernelbase = NULL;
2609 	else
2610 			eprom_kernelbase = (uintptr_t)lvalue;
2611 
2612 	if ((BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop)) ||
2613 	    (BOP_GETPROP(bootops, "segmapsize", prop) < 0) ||
2614 	    (kobj_getvalue(prop, &lvalue) == -1)) {
2615 		segmapsize = SEGMAPDEFAULT;
2616 	} else {
2617 		segmapsize = (uintptr_t)lvalue;
2618 	}
2619 
2620 	if ((BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop)) ||
2621 	    (BOP_GETPROP(bootops, "segmapfreelists", prop) < 0) ||
2622 	    (kobj_getvalue(prop, &lvalue) == -1)) {
2623 		segmapfreelists = 0;	/* use segmap driver default */
2624 	} else {
2625 		segmapfreelists = (int)lvalue;
2626 	}
2627 
2628 	if ((BOP_GETPROPLEN(bootops, "physmem") <= sizeof (prop)) &&
2629 	    (BOP_GETPROP(bootops, "physmem", prop) >= 0) &&
2630 	    (kobj_getvalue(prop, &lvalue) != -1)) {
2631 		physmem = (uintptr_t)lvalue;
2632 	}
2633 }
2634 
2635 /*
2636  * Add to a memory list.
2637  * start = start of new memory segment
2638  * len = length of new memory segment in bytes
2639  * new = pointer to a new struct memlist
2640  * memlistp = memory list to which to add segment.
2641  */
2642 static void
2643 memlist_add(
2644 	uint64_t start,
2645 	uint64_t len,
2646 	struct memlist *new,
2647 	struct memlist **memlistp)
2648 {
2649 	struct memlist *cur;
2650 	uint64_t end = start + len;
2651 
2652 	new->address = start;
2653 	new->size = len;
2654 
2655 	cur = *memlistp;
2656 
2657 	while (cur) {
2658 		if (cur->address >= end) {
2659 			new->next = cur;
2660 			*memlistp = new;
2661 			new->prev = cur->prev;
2662 			cur->prev = new;
2663 			return;
2664 		}
2665 		ASSERT(cur->address + cur->size <= start);
2666 		if (cur->next == NULL) {
2667 			cur->next = new;
2668 			new->prev = cur;
2669 			new->next = NULL;
2670 			return;
2671 		}
2672 		memlistp = &cur->next;
2673 		cur = cur->next;
2674 	}
2675 }
2676 
2677 void
2678 kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
2679 {
2680 	size_t tsize = e_modtext - modtext;
2681 	size_t dsize = e_moddata - moddata;
2682 
2683 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
2684 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
2685 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
2686 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
2687 }
2688 
2689 caddr_t
2690 kobj_text_alloc(vmem_t *arena, size_t size)
2691 {
2692 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
2693 }
2694 
2695 /*ARGSUSED*/
2696 caddr_t
2697 kobj_texthole_alloc(caddr_t addr, size_t size)
2698 {
2699 	panic("unexpected call to kobj_texthole_alloc()");
2700 	/*NOTREACHED*/
2701 	return (0);
2702 }
2703 
2704 /*ARGSUSED*/
2705 void
2706 kobj_texthole_free(caddr_t addr, size_t size)
2707 {
2708 	panic("unexpected call to kobj_texthole_free()");
2709 }
2710 
2711 /*
2712  * This is called just after configure() in startup().
2713  *
2714  * The ISALIST concept is a bit hopeless on Intel, because
2715  * there's no guarantee of an ever-more-capable processor
2716  * given that various parts of the instruction set may appear
2717  * and disappear between different implementations.
2718  *
2719  * While it would be possible to correct it and even enhance
2720  * it somewhat, the explicit hardware capability bitmask allows
2721  * more flexibility.
2722  *
2723  * So, we just leave this alone.
2724  */
2725 void
2726 setx86isalist(void)
2727 {
2728 	char *tp;
2729 	size_t len;
2730 	extern char *isa_list;
2731 
2732 #define	TBUFSIZE	1024
2733 
2734 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
2735 	*tp = '\0';
2736 
2737 #if defined(__amd64)
2738 	(void) strcpy(tp, "amd64 ");
2739 #endif
2740 
2741 	switch (x86_vendor) {
2742 	case X86_VENDOR_Intel:
2743 	case X86_VENDOR_AMD:
2744 	case X86_VENDOR_TM:
2745 		if (x86_feature & X86_CMOV) {
2746 			/*
2747 			 * Pentium Pro or later
2748 			 */
2749 			(void) strcat(tp, "pentium_pro");
2750 			(void) strcat(tp, x86_feature & X86_MMX ?
2751 			    "+mmx pentium_pro " : " ");
2752 		}
2753 		/*FALLTHROUGH*/
2754 	case X86_VENDOR_Cyrix:
2755 		/*
2756 		 * The Cyrix 6x86 does not have any Pentium features
2757 		 * accessible while not at privilege level 0.
2758 		 */
2759 		if (x86_feature & X86_CPUID) {
2760 			(void) strcat(tp, "pentium");
2761 			(void) strcat(tp, x86_feature & X86_MMX ?
2762 			    "+mmx pentium " : " ");
2763 		}
2764 		break;
2765 	default:
2766 		break;
2767 	}
2768 	(void) strcat(tp, "i486 i386 i86");
2769 	len = strlen(tp) + 1;   /* account for NULL at end of string */
2770 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
2771 	kmem_free(tp, TBUFSIZE);
2772 
2773 #undef TBUFSIZE
2774 }
2775 
2776 
2777 #ifdef __amd64
2778 
2779 void *
2780 device_arena_alloc(size_t size, int vm_flag)
2781 {
2782 	return (vmem_alloc(device_arena, size, vm_flag));
2783 }
2784 
2785 void
2786 device_arena_free(void *vaddr, size_t size)
2787 {
2788 	vmem_free(device_arena, vaddr, size);
2789 }
2790 
2791 #else
2792 
2793 void *
2794 device_arena_alloc(size_t size, int vm_flag)
2795 {
2796 	caddr_t	vaddr;
2797 	uintptr_t v;
2798 	size_t	start;
2799 	size_t	end;
2800 
2801 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
2802 	if (vaddr == NULL)
2803 		return (NULL);
2804 
2805 	v = (uintptr_t)vaddr;
2806 	ASSERT(v >= kernelbase);
2807 	ASSERT(v + size <= ptable_va);
2808 
2809 	start = btop(v - kernelbase);
2810 	end = btop(v + size - 1 - kernelbase);
2811 	ASSERT(start < toxic_bit_map_len);
2812 	ASSERT(end < toxic_bit_map_len);
2813 
2814 	while (start <= end) {
2815 		BT_ATOMIC_SET(toxic_bit_map, start);
2816 		++start;
2817 	}
2818 	return (vaddr);
2819 }
2820 
2821 void
2822 device_arena_free(void *vaddr, size_t size)
2823 {
2824 	uintptr_t v = (uintptr_t)vaddr;
2825 	size_t	start;
2826 	size_t	end;
2827 
2828 	ASSERT(v >= kernelbase);
2829 	ASSERT(v + size <= ptable_va);
2830 
2831 	start = btop(v - kernelbase);
2832 	end = btop(v + size - 1 - kernelbase);
2833 	ASSERT(start < toxic_bit_map_len);
2834 	ASSERT(end < toxic_bit_map_len);
2835 
2836 	while (start <= end) {
2837 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
2838 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
2839 		++start;
2840 	}
2841 	vmem_free(heap_arena, vaddr, size);
2842 }
2843 
2844 /*
2845  * returns 1st address in range that is in device arena, or NULL
2846  * if len is not NULL it returns the length of the toxic range
2847  */
2848 void *
2849 device_arena_contains(void *vaddr, size_t size, size_t *len)
2850 {
2851 	uintptr_t v = (uintptr_t)vaddr;
2852 	uintptr_t eaddr = v + size;
2853 	size_t start;
2854 	size_t end;
2855 
2856 	/*
2857 	 * if called very early by kmdb, just return NULL
2858 	 */
2859 	if (toxic_bit_map == NULL)
2860 		return (NULL);
2861 
2862 	/*
2863 	 * First check if we're completely outside the bitmap range.
2864 	 */
2865 	if (v >= ptable_va || eaddr < kernelbase)
2866 		return (NULL);
2867 
2868 	/*
2869 	 * Trim ends of search to look at only what the bitmap covers.
2870 	 */
2871 	if (v < kernelbase)
2872 		v = kernelbase;
2873 	start = btop(v - kernelbase);
2874 	end = btop(eaddr - kernelbase);
2875 	if (end >= toxic_bit_map_len)
2876 		end = toxic_bit_map_len;
2877 
2878 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
2879 		return (NULL);
2880 
2881 	v = kernelbase + ptob(start);
2882 	if (len != NULL)
2883 		*len = ptob(end - start);
2884 	return ((void *)v);
2885 }
2886 
2887 #endif
2888