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