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