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