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