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