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