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