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