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