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