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