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