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