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