xref: /titanic_52/usr/src/uts/i86pc/os/startup.c (revision 1bf5e2ae883107708d4e1651562a348519be0e6a)
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 #if defined(__xpv)
684 	startup_xen_mca();
685 #endif
686 	startup_modules();
687 #if !defined(__xpv)
688 	startup_bios_disk();
689 #endif
690 	startup_end();
691 	progressbar_start();
692 }
693 
694 static void
695 startup_init()
696 {
697 	PRM_POINT("startup_init() starting...");
698 
699 	/*
700 	 * Complete the extraction of cpuid data
701 	 */
702 	cpuid_pass2(CPU);
703 
704 	(void) check_boot_version(BOP_GETVERSION(bootops));
705 
706 	/*
707 	 * Check for prom_debug in boot environment
708 	 */
709 	if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
710 		++prom_debug;
711 		PRM_POINT("prom_debug found in boot enviroment");
712 	}
713 
714 	/*
715 	 * Collect node, cpu and memory configuration information.
716 	 */
717 	get_system_configuration();
718 
719 	/*
720 	 * Halt if this is an unsupported processor.
721 	 */
722 	if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
723 		printf("\n486 processor (\"%s\") detected.\n",
724 		    CPU->cpu_brandstr);
725 		halt("This processor is not supported by this release "
726 		    "of Solaris.");
727 	}
728 
729 	PRM_POINT("startup_init() done");
730 }
731 
732 /*
733  * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
734  * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
735  * also filters out physical page zero.  There is some reliance on the
736  * boot loader allocating only a few contiguous physical memory chunks.
737  */
738 static void
739 avail_filter(uint64_t *addr, uint64_t *size)
740 {
741 	uintptr_t va;
742 	uintptr_t next_va;
743 	pfn_t pfn;
744 	uint64_t pfn_addr;
745 	uint64_t pfn_eaddr;
746 	uint_t prot;
747 	size_t len;
748 	uint_t change;
749 
750 	if (prom_debug)
751 		prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
752 		    *addr, *size);
753 
754 	/*
755 	 * page zero is required for BIOS.. never make it available
756 	 */
757 	if (*addr == 0) {
758 		*addr += MMU_PAGESIZE;
759 		*size -= MMU_PAGESIZE;
760 	}
761 
762 	/*
763 	 * First we trim from the front of the range. Since kbm_probe()
764 	 * walks ranges in virtual order, but addr/size are physical, we need
765 	 * to the list until no changes are seen.  This deals with the case
766 	 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
767 	 * but w < v.
768 	 */
769 	do {
770 		change = 0;
771 		for (va = KERNEL_TEXT;
772 		    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
773 		    va = next_va) {
774 
775 			next_va = va + len;
776 			pfn_addr = pfn_to_pa(pfn);
777 			pfn_eaddr = pfn_addr + len;
778 
779 			if (pfn_addr <= *addr && pfn_eaddr > *addr) {
780 				change = 1;
781 				while (*size > 0 && len > 0) {
782 					*addr += MMU_PAGESIZE;
783 					*size -= MMU_PAGESIZE;
784 					len -= MMU_PAGESIZE;
785 				}
786 			}
787 		}
788 		if (change && prom_debug)
789 			prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
790 			    *addr, *size);
791 	} while (change);
792 
793 	/*
794 	 * Trim pages from the end of the range.
795 	 */
796 	for (va = KERNEL_TEXT;
797 	    *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
798 	    va = next_va) {
799 
800 		next_va = va + len;
801 		pfn_addr = pfn_to_pa(pfn);
802 
803 		if (pfn_addr >= *addr && pfn_addr < *addr + *size)
804 			*size = pfn_addr - *addr;
805 	}
806 
807 	if (prom_debug)
808 		prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
809 		    *addr, *size);
810 }
811 
812 static void
813 kpm_init()
814 {
815 	struct segkpm_crargs b;
816 
817 	/*
818 	 * These variables were all designed for sfmmu in which segkpm is
819 	 * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
820 	 * might use 2+ page sizes on a single machine, so none of these
821 	 * variables have a single correct value.  They are set up as if we
822 	 * always use a 4KB pagesize, which should do no harm.  In the long
823 	 * run, we should get rid of KPM's assumption that only a single
824 	 * pagesize is used.
825 	 */
826 	kpm_pgshft = MMU_PAGESHIFT;
827 	kpm_pgsz =  MMU_PAGESIZE;
828 	kpm_pgoff = MMU_PAGEOFFSET;
829 	kpmp2pshft = 0;
830 	kpmpnpgs = 1;
831 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
832 
833 	PRM_POINT("about to create segkpm");
834 	rw_enter(&kas.a_lock, RW_WRITER);
835 
836 	if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
837 		panic("cannot attach segkpm");
838 
839 	b.prot = PROT_READ | PROT_WRITE;
840 	b.nvcolors = 1;
841 
842 	if (segkpm_create(segkpm, (caddr_t)&b) != 0)
843 		panic("segkpm_create segkpm");
844 
845 	rw_exit(&kas.a_lock);
846 }
847 
848 /*
849  * The debug info page provides enough information to allow external
850  * inspectors (e.g. when running under a hypervisor) to bootstrap
851  * themselves into allowing full-blown kernel debugging.
852  */
853 static void
854 init_debug_info(void)
855 {
856 	caddr_t mem;
857 	debug_info_t *di;
858 
859 #ifndef __lint
860 	ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
861 #endif
862 
863 	mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
864 	    MMU_PAGESIZE);
865 
866 	if (mem != (caddr_t)DEBUG_INFO_VA)
867 		panic("BOP_ALLOC() failed");
868 	bzero(mem, MMU_PAGESIZE);
869 
870 	di = (debug_info_t *)mem;
871 
872 	di->di_magic = DEBUG_INFO_MAGIC;
873 	di->di_version = DEBUG_INFO_VERSION;
874 	di->di_modules = (uintptr_t)&modules;
875 	di->di_s_text = (uintptr_t)s_text;
876 	di->di_e_text = (uintptr_t)e_text;
877 	di->di_s_data = (uintptr_t)s_data;
878 	di->di_e_data = (uintptr_t)e_data;
879 	di->di_hat_htable_off = offsetof(hat_t, hat_htable);
880 	di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
881 }
882 
883 /*
884  * Build the memlists and other kernel essential memory system data structures.
885  * This is everything at valloc_base.
886  */
887 static void
888 startup_memlist(void)
889 {
890 	size_t memlist_sz;
891 	size_t memseg_sz;
892 	size_t pagehash_sz;
893 	size_t pp_sz;
894 	uintptr_t va;
895 	size_t len;
896 	uint_t prot;
897 	pfn_t pfn;
898 	int memblocks;
899 	caddr_t pagecolor_mem;
900 	size_t pagecolor_memsz;
901 	caddr_t page_ctrs_mem;
902 	size_t page_ctrs_size;
903 	size_t pse_table_alloc_size;
904 	struct memlist *current;
905 	extern void startup_build_mem_nodes(struct memlist *);
906 
907 	/* XX64 fix these - they should be in include files */
908 	extern size_t page_coloring_init(uint_t, int, int);
909 	extern void page_coloring_setup(caddr_t);
910 
911 	PRM_POINT("startup_memlist() starting...");
912 
913 	/*
914 	 * Use leftover large page nucleus text/data space for loadable modules.
915 	 * Use at most MODTEXT/MODDATA.
916 	 */
917 	len = kbm_nucleus_size;
918 	ASSERT(len > MMU_PAGESIZE);
919 
920 	moddata = (caddr_t)ROUND_UP_PAGE(e_data);
921 	e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
922 	if (e_moddata - moddata > MODDATA)
923 		e_moddata = moddata + MODDATA;
924 
925 	modtext = (caddr_t)ROUND_UP_PAGE(e_text);
926 	e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
927 	if (e_modtext - modtext > MODTEXT)
928 		e_modtext = modtext + MODTEXT;
929 
930 	econtig = e_moddata;
931 
932 	PRM_DEBUG(modtext);
933 	PRM_DEBUG(e_modtext);
934 	PRM_DEBUG(moddata);
935 	PRM_DEBUG(e_moddata);
936 	PRM_DEBUG(econtig);
937 
938 	/*
939 	 * Examine the boot loader physical memory map to find out:
940 	 * - total memory in system - physinstalled
941 	 * - the max physical address - physmax
942 	 * - the number of discontiguous segments of memory.
943 	 */
944 	if (prom_debug)
945 		print_memlist("boot physinstalled",
946 		    bootops->boot_mem->physinstalled);
947 	installed_top_size(bootops->boot_mem->physinstalled, &physmax,
948 	    &physinstalled, &memblocks);
949 	PRM_DEBUG(physmax);
950 	PRM_DEBUG(physinstalled);
951 	PRM_DEBUG(memblocks);
952 
953 	/*
954 	 * Initialize hat's mmu parameters.
955 	 * Check for enforce-prot-exec in boot environment. It's used to
956 	 * enable/disable support for the page table entry NX bit.
957 	 * The default is to enforce PROT_EXEC on processors that support NX.
958 	 * Boot seems to round up the "len", but 8 seems to be big enough.
959 	 */
960 	mmu_init();
961 
962 #ifdef	__i386
963 	/*
964 	 * physmax is lowered if there is more memory than can be
965 	 * physically addressed in 32 bit (PAE/non-PAE) modes.
966 	 */
967 	if (mmu.pae_hat) {
968 		if (PFN_ABOVE64G(physmax)) {
969 			physinstalled -= (physmax - (PFN_64G - 1));
970 			physmax = PFN_64G - 1;
971 		}
972 	} else {
973 		if (PFN_ABOVE4G(physmax)) {
974 			physinstalled -= (physmax - (PFN_4G - 1));
975 			physmax = PFN_4G - 1;
976 		}
977 	}
978 #endif
979 
980 	startup_build_mem_nodes(bootops->boot_mem->physinstalled);
981 
982 	if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
983 		int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
984 		char value[8];
985 
986 		if (len < 8)
987 			(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
988 		else
989 			(void) strcpy(value, "");
990 		if (strcmp(value, "off") == 0)
991 			mmu.pt_nx = 0;
992 	}
993 	PRM_DEBUG(mmu.pt_nx);
994 
995 	/*
996 	 * We will need page_t's for every page in the system, except for
997 	 * memory mapped at or above above the start of the kernel text segment.
998 	 *
999 	 * pages above e_modtext are attributed to kernel debugger (obp_pages)
1000 	 */
1001 	npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
1002 	obp_pages = 0;
1003 	va = KERNEL_TEXT;
1004 	while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
1005 		npages -= len >> MMU_PAGESHIFT;
1006 		if (va >= (uintptr_t)e_moddata)
1007 			obp_pages += len >> MMU_PAGESHIFT;
1008 		va += len;
1009 	}
1010 	PRM_DEBUG(npages);
1011 	PRM_DEBUG(obp_pages);
1012 
1013 	/*
1014 	 * If physmem is patched to be non-zero, use it instead of the computed
1015 	 * value unless it is larger than the actual amount of memory on hand.
1016 	 */
1017 	if (physmem == 0 || physmem > npages) {
1018 		physmem = npages;
1019 	} else if (physmem < npages) {
1020 		orig_npages = npages;
1021 		npages = physmem;
1022 	}
1023 	PRM_DEBUG(physmem);
1024 
1025 	/*
1026 	 * We now compute the sizes of all the  initial allocations for
1027 	 * structures the kernel needs in order do kmem_alloc(). These
1028 	 * include:
1029 	 *	memsegs
1030 	 *	memlists
1031 	 *	page hash table
1032 	 *	page_t's
1033 	 *	page coloring data structs
1034 	 */
1035 	memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
1036 	ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
1037 	PRM_DEBUG(memseg_sz);
1038 
1039 	/*
1040 	 * Reserve space for memlists. There's no real good way to know exactly
1041 	 * how much room we'll need, but this should be a good upper bound.
1042 	 */
1043 	memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
1044 	    (memblocks + POSS_NEW_FRAGMENTS));
1045 	ADD_TO_ALLOCATIONS(memlist, memlist_sz);
1046 	PRM_DEBUG(memlist_sz);
1047 
1048 	/*
1049 	 * The page structure hash table size is a power of 2
1050 	 * such that the average hash chain length is PAGE_HASHAVELEN.
1051 	 */
1052 	page_hashsz = npages / PAGE_HASHAVELEN;
1053 	page_hashsz = 1 << highbit(page_hashsz);
1054 	pagehash_sz = sizeof (struct page *) * page_hashsz;
1055 	ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
1056 	PRM_DEBUG(pagehash_sz);
1057 
1058 	/*
1059 	 * Set aside room for the page structures themselves.
1060 	 */
1061 	PRM_DEBUG(npages);
1062 	pp_sz = sizeof (struct page) * npages;
1063 	ADD_TO_ALLOCATIONS(pp_base, pp_sz);
1064 	PRM_DEBUG(pp_sz);
1065 
1066 	/*
1067 	 * determine l2 cache info and memory size for page coloring
1068 	 */
1069 	(void) getl2cacheinfo(CPU,
1070 	    &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
1071 	pagecolor_memsz =
1072 	    page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
1073 	ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
1074 	PRM_DEBUG(pagecolor_memsz);
1075 
1076 	page_ctrs_size = page_ctrs_sz();
1077 	ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
1078 	PRM_DEBUG(page_ctrs_size);
1079 
1080 	/*
1081 	 * Allocate the array that protects pp->p_selock.
1082 	 */
1083 	pse_shift = size_pse_array(physmem, max_ncpus);
1084 	pse_table_size = 1 << pse_shift;
1085 	pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
1086 	ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
1087 
1088 #if defined(__amd64)
1089 	valloc_sz = ROUND_UP_LPAGE(valloc_sz);
1090 	valloc_base = VALLOC_BASE;
1091 
1092 	/*
1093 	 * The default values of VALLOC_BASE and SEGKPM_BASE should work
1094 	 * for values of physmax up to 1 Terabyte. They need adjusting when
1095 	 * memory is at addresses above 1 TB.
1096 	 */
1097 	if (physmax + 1 > mmu_btop(TERABYTE)) {
1098 		uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
1099 
1100 		/* Round to largest possible pagesize for now */
1101 		kpm_resv_amount = P2ROUNDUP(kpm_resv_amount, ONE_GIG);
1102 
1103 		segkpm_base = -(2 * kpm_resv_amount); /* down from top VA */
1104 
1105 		/* make sure we leave some space for user apps above hole */
1106 		segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
1107 		if (segkpm_base > SEGKPM_BASE)
1108 			segkpm_base = SEGKPM_BASE;
1109 		PRM_DEBUG(segkpm_base);
1110 
1111 		valloc_base = segkpm_base + kpm_resv_amount;
1112 		PRM_DEBUG(valloc_base);
1113 	}
1114 #else	/* __i386 */
1115 	valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
1116 	valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
1117 	PRM_DEBUG(valloc_base);
1118 #endif	/* __i386 */
1119 
1120 	/*
1121 	 * do all the initial allocations
1122 	 */
1123 	perform_allocations();
1124 
1125 	/*
1126 	 * Build phys_install and phys_avail in kernel memspace.
1127 	 * - phys_install should be all memory in the system.
1128 	 * - phys_avail is phys_install minus any memory mapped before this
1129 	 *    point above KERNEL_TEXT.
1130 	 */
1131 	current = phys_install = memlist;
1132 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current, NULL);
1133 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1134 		panic("physinstalled was too big!");
1135 	if (prom_debug)
1136 		print_memlist("phys_install", phys_install);
1137 
1138 	phys_avail = current;
1139 	PRM_POINT("Building phys_avail:\n");
1140 	copy_memlist_filter(bootops->boot_mem->physinstalled, &current,
1141 	    avail_filter);
1142 	if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1143 		panic("physavail was too big!");
1144 	if (prom_debug)
1145 		print_memlist("phys_avail", phys_avail);
1146 
1147 	/*
1148 	 * setup page coloring
1149 	 */
1150 	page_coloring_setup(pagecolor_mem);
1151 	page_lock_init();	/* currently a no-op */
1152 
1153 	/*
1154 	 * free page list counters
1155 	 */
1156 	(void) page_ctrs_alloc(page_ctrs_mem);
1157 
1158 	/*
1159 	 * Size the pcf array based on the number of cpus in the box at
1160 	 * boot time.
1161 	 */
1162 
1163 	pcf_init();
1164 
1165 	/*
1166 	 * Initialize the page structures from the memory lists.
1167 	 */
1168 	availrmem_initial = availrmem = freemem = 0;
1169 	PRM_POINT("Calling kphysm_init()...");
1170 	npages = kphysm_init(pp_base, npages);
1171 	PRM_POINT("kphysm_init() done");
1172 	PRM_DEBUG(npages);
1173 
1174 	init_debug_info();
1175 
1176 	/*
1177 	 * Now that page_t's have been initialized, remove all the
1178 	 * initial allocation pages from the kernel free page lists.
1179 	 */
1180 	boot_mapin((caddr_t)valloc_base, valloc_sz);
1181 	boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
1182 	PRM_POINT("startup_memlist() done");
1183 
1184 	PRM_DEBUG(valloc_sz);
1185 
1186 #if defined(__amd64)
1187 	if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
1188 	    textrepl_min_gb && l2cache_sz <= 2 << 20) {
1189 		extern size_t textrepl_size_thresh;
1190 		textrepl_size_thresh = (16 << 20) - 1;
1191 	}
1192 #endif
1193 }
1194 
1195 /*
1196  * Layout the kernel's part of address space and initialize kmem allocator.
1197  */
1198 static void
1199 startup_kmem(void)
1200 {
1201 	extern void page_set_colorequiv_arr(void);
1202 
1203 	PRM_POINT("startup_kmem() starting...");
1204 
1205 #if defined(__amd64)
1206 	if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
1207 		cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
1208 		    "systems.");
1209 	kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
1210 	core_base = (uintptr_t)COREHEAP_BASE;
1211 	core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
1212 #else	/* __i386 */
1213 	/*
1214 	 * We configure kernelbase based on:
1215 	 *
1216 	 * 1. user specified kernelbase via eeprom command. Value cannot exceed
1217 	 *    KERNELBASE_MAX. we large page align eprom_kernelbase
1218 	 *
1219 	 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
1220 	 *    On large memory systems we must lower kernelbase to allow
1221 	 *    enough room for page_t's for all of memory.
1222 	 *
1223 	 * The value set here, might be changed a little later.
1224 	 */
1225 	if (eprom_kernelbase) {
1226 		kernelbase = eprom_kernelbase & mmu.level_mask[1];
1227 		if (kernelbase > KERNELBASE_MAX)
1228 			kernelbase = KERNELBASE_MAX;
1229 	} else {
1230 		kernelbase = (uintptr_t)KERNELBASE;
1231 		kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
1232 	}
1233 	ASSERT((kernelbase & mmu.level_offset[1]) == 0);
1234 	core_base = valloc_base;
1235 	core_size = 0;
1236 #endif	/* __i386 */
1237 
1238 	PRM_DEBUG(core_base);
1239 	PRM_DEBUG(core_size);
1240 	PRM_DEBUG(kernelbase);
1241 
1242 #if defined(__i386)
1243 	segkp_fromheap = 1;
1244 #endif	/* __i386 */
1245 
1246 	ekernelheap = (char *)core_base;
1247 	PRM_DEBUG(ekernelheap);
1248 
1249 	/*
1250 	 * Now that we know the real value of kernelbase,
1251 	 * update variables that were initialized with a value of
1252 	 * KERNELBASE (in common/conf/param.c).
1253 	 *
1254 	 * XXX	The problem with this sort of hackery is that the
1255 	 *	compiler just may feel like putting the const declarations
1256 	 *	(in param.c) into the .text section.  Perhaps they should
1257 	 *	just be declared as variables there?
1258 	 */
1259 
1260 	*(uintptr_t *)&_kernelbase = kernelbase;
1261 	*(uintptr_t *)&_userlimit = kernelbase;
1262 #if defined(__amd64)
1263 	*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
1264 #else
1265 	*(uintptr_t *)&_userlimit32 = _userlimit;
1266 #endif
1267 	PRM_DEBUG(_kernelbase);
1268 	PRM_DEBUG(_userlimit);
1269 	PRM_DEBUG(_userlimit32);
1270 
1271 	layout_kernel_va();
1272 
1273 #if defined(__i386)
1274 	/*
1275 	 * If segmap is too large we can push the bottom of the kernel heap
1276 	 * higher than the base.  Or worse, it could exceed the top of the
1277 	 * VA space entirely, causing it to wrap around.
1278 	 */
1279 	if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
1280 		panic("too little address space available for kernelheap,"
1281 		    " use eeprom for lower kernelbase or smaller segmapsize");
1282 #endif	/* __i386 */
1283 
1284 	/*
1285 	 * Initialize the kernel heap. Note 3rd argument must be > 1st.
1286 	 */
1287 	kernelheap_init(kernelheap, ekernelheap,
1288 	    kernelheap + MMU_PAGESIZE,
1289 	    (void *)core_base, (void *)(core_base + core_size));
1290 
1291 #if defined(__xpv)
1292 	/*
1293 	 * Link pending events struct into cpu struct
1294 	 */
1295 	CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
1296 #endif
1297 	/*
1298 	 * Initialize kernel memory allocator.
1299 	 */
1300 	kmem_init();
1301 
1302 	/*
1303 	 * Factor in colorequiv to check additional 'equivalent' bins
1304 	 */
1305 	page_set_colorequiv_arr();
1306 
1307 	/*
1308 	 * print this out early so that we know what's going on
1309 	 */
1310 	cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE);
1311 
1312 	/*
1313 	 * Initialize bp_mapin().
1314 	 */
1315 	bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
1316 
1317 	/*
1318 	 * orig_npages is non-zero if physmem has been configured for less
1319 	 * than the available memory.
1320 	 */
1321 	if (orig_npages) {
1322 		cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
1323 		    (npages == PHYSMEM ? "Due to virtual address space " : ""),
1324 		    npages, orig_npages);
1325 	}
1326 #if defined(__i386)
1327 	if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
1328 		cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
1329 		    "System using 0x%lx",
1330 		    (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
1331 #endif
1332 
1333 #ifdef	KERNELBASE_ABI_MIN
1334 	if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
1335 		cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
1336 		    "i386 ABI compliant.", (uintptr_t)kernelbase);
1337 	}
1338 #endif
1339 
1340 #ifdef __xpv
1341 	/*
1342 	 * Some of the xen start information has to be relocated up
1343 	 * into the kernel's permanent address space.
1344 	 */
1345 	PRM_POINT("calling xen_relocate_start_info()");
1346 	xen_relocate_start_info();
1347 	PRM_POINT("xen_relocate_start_info() done");
1348 
1349 	/*
1350 	 * (Update the vcpu pointer in our cpu structure to point into
1351 	 * the relocated shared info.)
1352 	 */
1353 	CPU->cpu_m.mcpu_vcpu_info =
1354 	    &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
1355 #endif
1356 
1357 	PRM_POINT("startup_kmem() done");
1358 }
1359 
1360 #ifndef __xpv
1361 /*
1362  * If we have detected that we are running in an HVM environment, we need
1363  * to prepend the PV driver directory to the module search path.
1364  */
1365 #define	HVM_MOD_DIR "/platform/i86hvm/kernel"
1366 static void
1367 update_default_path()
1368 {
1369 	char *current, *newpath;
1370 	int newlen;
1371 
1372 	/*
1373 	 * We are about to resync with krtld.  krtld will reset its
1374 	 * internal module search path iff Solaris has set default_path.
1375 	 * We want to be sure we're prepending this new directory to the
1376 	 * right search path.
1377 	 */
1378 	current = (default_path == NULL) ? kobj_module_path : default_path;
1379 
1380 	newlen = strlen(HVM_MOD_DIR) + strlen(current) + 1;
1381 	newpath = kmem_alloc(newlen, KM_SLEEP);
1382 	(void) strcpy(newpath, HVM_MOD_DIR);
1383 	(void) strcat(newpath, " ");
1384 	(void) strcat(newpath, current);
1385 
1386 	default_path = newpath;
1387 }
1388 #endif
1389 
1390 static void
1391 startup_modules(void)
1392 {
1393 	unsigned int i;
1394 	extern void prom_setup(void);
1395 	cmi_hdl_t hdl;
1396 
1397 	PRM_POINT("startup_modules() starting...");
1398 
1399 #ifndef __xpv
1400 	/*
1401 	 * Initialize ten-micro second timer so that drivers will
1402 	 * not get short changed in their init phase. This was
1403 	 * not getting called until clkinit which, on fast cpu's
1404 	 * caused the drv_usecwait to be way too short.
1405 	 */
1406 	microfind();
1407 
1408 	if (xpv_is_hvm)
1409 		update_default_path();
1410 #endif
1411 
1412 	/*
1413 	 * Read the GMT lag from /etc/rtc_config.
1414 	 */
1415 	sgmtl(process_rtc_config_file());
1416 
1417 	/*
1418 	 * Calculate default settings of system parameters based upon
1419 	 * maxusers, yet allow to be overridden via the /etc/system file.
1420 	 */
1421 	param_calc(0);
1422 
1423 	mod_setup();
1424 
1425 	/*
1426 	 * Initialize system parameters.
1427 	 */
1428 	param_init();
1429 
1430 	/*
1431 	 * Initialize the default brands
1432 	 */
1433 	brand_init();
1434 
1435 	/*
1436 	 * maxmem is the amount of physical memory we're playing with.
1437 	 */
1438 	maxmem = physmem;
1439 
1440 	/*
1441 	 * Initialize segment management stuff.
1442 	 */
1443 	seg_init();
1444 
1445 	if (modload("fs", "specfs") == -1)
1446 		halt("Can't load specfs");
1447 
1448 	if (modload("fs", "devfs") == -1)
1449 		halt("Can't load devfs");
1450 
1451 	if (modload("fs", "dev") == -1)
1452 		halt("Can't load dev");
1453 
1454 	(void) modloadonly("sys", "lbl_edition");
1455 
1456 	dispinit();
1457 
1458 	/*
1459 	 * This is needed here to initialize hw_serial[] for cluster booting.
1460 	 */
1461 	if ((i = modload("misc", "sysinit")) != (unsigned int)-1)
1462 		(void) modunload(i);
1463 	else
1464 		cmn_err(CE_CONT, "sysinit load failed");
1465 
1466 	/* Read cluster configuration data. */
1467 	clconf_init();
1468 
1469 #if defined(__xpv)
1470 	ec_init();
1471 	gnttab_init();
1472 	(void) xs_early_init();
1473 #endif /* __xpv */
1474 
1475 	/*
1476 	 * Create a kernel device tree. First, create rootnex and
1477 	 * then invoke bus specific code to probe devices.
1478 	 */
1479 	setup_ddi();
1480 
1481 	/*
1482 	 * Set up the CPU module subsystem for the boot cpu in the native
1483 	 * case, and all physical cpu resource in the xpv dom0 case.
1484 	 * Modifies the device tree, so this must be done after
1485 	 * setup_ddi().
1486 	 */
1487 #ifdef __xpv
1488 	/*
1489 	 * If paravirtualized and on dom0 then we initialize all physical
1490 	 * cpu handles now;  if paravirtualized on a domU then do not
1491 	 * initialize.
1492 	 */
1493 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1494 		xen_mc_lcpu_cookie_t cpi;
1495 
1496 		for (cpi = xen_physcpu_next(NULL); cpi != NULL;
1497 		    cpi = xen_physcpu_next(cpi)) {
1498 			if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
1499 			    xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
1500 			    xen_physcpu_strandid(cpi))) != NULL &&
1501 			    (x86_feature & X86_MCA))
1502 				cmi_mca_init(hdl);
1503 		}
1504 	}
1505 #else
1506 	/*
1507 	 * Initialize a handle for the boot cpu - others will initialize
1508 	 * as they startup.  Do not do this if we know we are in an HVM domU.
1509 	 */
1510 	if (!xpv_is_hvm &&
1511 	    (hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
1512 	    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL &&
1513 	    (x86_feature & X86_MCA))
1514 			cmi_mca_init(hdl);
1515 #endif	/* __xpv */
1516 
1517 	/*
1518 	 * Fake a prom tree such that /dev/openprom continues to work
1519 	 */
1520 	PRM_POINT("startup_modules: calling prom_setup...");
1521 	prom_setup();
1522 	PRM_POINT("startup_modules: done");
1523 
1524 	/*
1525 	 * Load all platform specific modules
1526 	 */
1527 	PRM_POINT("startup_modules: calling psm_modload...");
1528 	psm_modload();
1529 
1530 	PRM_POINT("startup_modules() done");
1531 }
1532 
1533 /*
1534  * claim a "setaside" boot page for use in the kernel
1535  */
1536 page_t *
1537 boot_claim_page(pfn_t pfn)
1538 {
1539 	page_t *pp;
1540 
1541 	pp = page_numtopp_nolock(pfn);
1542 	ASSERT(pp != NULL);
1543 
1544 	if (PP_ISBOOTPAGES(pp)) {
1545 		if (pp->p_next != NULL)
1546 			pp->p_next->p_prev = pp->p_prev;
1547 		if (pp->p_prev == NULL)
1548 			bootpages = pp->p_next;
1549 		else
1550 			pp->p_prev->p_next = pp->p_next;
1551 	} else {
1552 		/*
1553 		 * htable_attach() expects a base pagesize page
1554 		 */
1555 		if (pp->p_szc != 0)
1556 			page_boot_demote(pp);
1557 		pp = page_numtopp(pfn, SE_EXCL);
1558 	}
1559 	return (pp);
1560 }
1561 
1562 /*
1563  * Walk through the pagetables looking for pages mapped in by boot.  If the
1564  * setaside flag is set the pages are expected to be returned to the
1565  * kernel later in boot, so we add them to the bootpages list.
1566  */
1567 static void
1568 protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
1569 {
1570 	uintptr_t va = low;
1571 	size_t len;
1572 	uint_t prot;
1573 	pfn_t pfn;
1574 	page_t *pp;
1575 	pgcnt_t boot_protect_cnt = 0;
1576 
1577 	while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
1578 		if (va + len >= high)
1579 			panic("0x%lx byte mapping at 0x%p exceeds boot's "
1580 			    "legal range.", len, (void *)va);
1581 
1582 		while (len > 0) {
1583 			pp = page_numtopp_alloc(pfn);
1584 			if (pp != NULL) {
1585 				if (setaside == 0)
1586 					panic("Unexpected mapping by boot.  "
1587 					    "addr=%p pfn=%lx\n",
1588 					    (void *)va, pfn);
1589 
1590 				pp->p_next = bootpages;
1591 				pp->p_prev = NULL;
1592 				PP_SETBOOTPAGES(pp);
1593 				if (bootpages != NULL) {
1594 					bootpages->p_prev = pp;
1595 				}
1596 				bootpages = pp;
1597 				++boot_protect_cnt;
1598 			}
1599 
1600 			++pfn;
1601 			len -= MMU_PAGESIZE;
1602 			va += MMU_PAGESIZE;
1603 		}
1604 	}
1605 	PRM_DEBUG(boot_protect_cnt);
1606 }
1607 
1608 /*
1609  *
1610  */
1611 static void
1612 layout_kernel_va(void)
1613 {
1614 	PRM_POINT("layout_kernel_va() starting...");
1615 	/*
1616 	 * Establish the final size of the kernel's heap, size of segmap,
1617 	 * segkp, etc.
1618 	 */
1619 
1620 #if defined(__amd64)
1621 
1622 	kpm_vbase = (caddr_t)segkpm_base;
1623 	kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
1624 	if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
1625 		panic("not enough room for kpm!");
1626 	PRM_DEBUG(kpm_size);
1627 	PRM_DEBUG(kpm_vbase);
1628 
1629 	/*
1630 	 * By default we create a seg_kp in 64 bit kernels, it's a little
1631 	 * faster to access than embedding it in the heap.
1632 	 */
1633 	segkp_base = (caddr_t)valloc_base + valloc_sz;
1634 	if (!segkp_fromheap) {
1635 		size_t sz = mmu_ptob(segkpsize);
1636 
1637 		/*
1638 		 * determine size of segkp
1639 		 */
1640 		if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
1641 			sz = SEGKPDEFSIZE;
1642 			cmn_err(CE_WARN, "!Illegal value for segkpsize. "
1643 			    "segkpsize has been reset to %ld pages",
1644 			    mmu_btop(sz));
1645 		}
1646 		sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
1647 
1648 		segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
1649 	}
1650 	PRM_DEBUG(segkp_base);
1651 	PRM_DEBUG(segkpsize);
1652 
1653 	/*
1654 	 * segzio is used for ZFS cached data. It uses a distinct VA
1655 	 * segment (from kernel heap) so that we can easily tell not to
1656 	 * include it in kernel crash dumps on 64 bit kernels. The trick is
1657 	 * to give it lots of VA, but not constrain the kernel heap.
1658 	 * We scale the size of segzio linearly with physmem up to
1659 	 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem.
1660 	 */
1661 	segzio_base = segkp_base + mmu_ptob(segkpsize);
1662 	if (segzio_fromheap) {
1663 		segziosize = 0;
1664 	} else {
1665 		size_t physmem_size = mmu_ptob(physmem);
1666 		size_t size = (segziosize == 0) ?
1667 		    physmem_size : mmu_ptob(segziosize);
1668 
1669 		if (size < SEGZIOMINSIZE)
1670 			size = SEGZIOMINSIZE;
1671 		if (size > SEGZIOMAXSIZE) {
1672 			size = SEGZIOMAXSIZE;
1673 			if (physmem_size > size)
1674 				size += (physmem_size - size) / 2;
1675 		}
1676 		segziosize = mmu_btop(ROUND_UP_LPAGE(size));
1677 	}
1678 	PRM_DEBUG(segziosize);
1679 	PRM_DEBUG(segzio_base);
1680 
1681 	/*
1682 	 * Put the range of VA for device mappings next, kmdb knows to not
1683 	 * grep in this range of addresses.
1684 	 */
1685 	toxic_addr =
1686 	    ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
1687 	PRM_DEBUG(toxic_addr);
1688 	segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
1689 #else /* __i386 */
1690 	segmap_start = ROUND_UP_LPAGE(kernelbase);
1691 #endif /* __i386 */
1692 	PRM_DEBUG(segmap_start);
1693 
1694 	/*
1695 	 * Users can change segmapsize through eeprom or /etc/system.
1696 	 * If the variable is tuned through eeprom, there is no upper
1697 	 * bound on the size of segmap.  If it is tuned through
1698 	 * /etc/system on 32-bit systems, it must be no larger than we
1699 	 * planned for in startup_memlist().
1700 	 */
1701 	segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
1702 
1703 #if defined(__i386)
1704 	/*
1705 	 * 32-bit systems don't have segkpm or segkp, so segmap appears at
1706 	 * the bottom of the kernel's address range.  Set aside space for a
1707 	 * small red zone just below the start of segmap.
1708 	 */
1709 	segmap_start += KERNEL_REDZONE_SIZE;
1710 	segmapsize -= KERNEL_REDZONE_SIZE;
1711 #endif
1712 
1713 	PRM_DEBUG(segmap_start);
1714 	PRM_DEBUG(segmapsize);
1715 	kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
1716 	PRM_DEBUG(kernelheap);
1717 	PRM_POINT("layout_kernel_va() done...");
1718 }
1719 
1720 /*
1721  * Finish initializing the VM system, now that we are no longer
1722  * relying on the boot time memory allocators.
1723  */
1724 static void
1725 startup_vm(void)
1726 {
1727 	struct segmap_crargs a;
1728 
1729 	extern int use_brk_lpg, use_stk_lpg;
1730 
1731 	PRM_POINT("startup_vm() starting...");
1732 
1733 	/*
1734 	 * Initialize the hat layer.
1735 	 */
1736 	hat_init();
1737 
1738 	/*
1739 	 * Do final allocations of HAT data structures that need to
1740 	 * be allocated before quiescing the boot loader.
1741 	 */
1742 	PRM_POINT("Calling hat_kern_alloc()...");
1743 	hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
1744 	PRM_POINT("hat_kern_alloc() done");
1745 
1746 #ifndef __xpv
1747 	/*
1748 	 * Setup Page Attribute Table
1749 	 */
1750 	pat_sync();
1751 #endif
1752 
1753 	/*
1754 	 * The next two loops are done in distinct steps in order
1755 	 * to be sure that any page that is doubly mapped (both above
1756 	 * KERNEL_TEXT and below kernelbase) is dealt with correctly.
1757 	 * Note this may never happen, but it might someday.
1758 	 */
1759 	bootpages = NULL;
1760 	PRM_POINT("Protecting boot pages");
1761 
1762 	/*
1763 	 * Protect any pages mapped above KERNEL_TEXT that somehow have
1764 	 * page_t's. This can only happen if something weird allocated
1765 	 * in this range (like kadb/kmdb).
1766 	 */
1767 	protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
1768 
1769 	/*
1770 	 * Before we can take over memory allocation/mapping from the boot
1771 	 * loader we must remove from our free page lists any boot allocated
1772 	 * pages that stay mapped until release_bootstrap().
1773 	 */
1774 	protect_boot_range(0, kernelbase, 1);
1775 
1776 
1777 	/*
1778 	 * Switch to running on regular HAT (not boot_mmu)
1779 	 */
1780 	PRM_POINT("Calling hat_kern_setup()...");
1781 	hat_kern_setup();
1782 
1783 	/*
1784 	 * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
1785 	 */
1786 	bop_no_more_mem();
1787 
1788 	PRM_POINT("hat_kern_setup() done");
1789 
1790 	hat_cpu_online(CPU);
1791 
1792 	/*
1793 	 * Initialize VM system
1794 	 */
1795 	PRM_POINT("Calling kvm_init()...");
1796 	kvm_init();
1797 	PRM_POINT("kvm_init() done");
1798 
1799 	/*
1800 	 * Tell kmdb that the VM system is now working
1801 	 */
1802 	if (boothowto & RB_DEBUG)
1803 		kdi_dvec_vmready();
1804 
1805 #if defined(__xpv)
1806 	/*
1807 	 * Populate the I/O pool on domain 0
1808 	 */
1809 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1810 		extern long populate_io_pool(void);
1811 		long init_io_pool_cnt;
1812 
1813 		PRM_POINT("Populating reserve I/O page pool");
1814 		init_io_pool_cnt = populate_io_pool();
1815 		PRM_DEBUG(init_io_pool_cnt);
1816 	}
1817 #endif
1818 	/*
1819 	 * Mangle the brand string etc.
1820 	 */
1821 	cpuid_pass3(CPU);
1822 
1823 #if defined(__amd64)
1824 
1825 	/*
1826 	 * Create the device arena for toxic (to dtrace/kmdb) mappings.
1827 	 */
1828 	device_arena = vmem_create("device", (void *)toxic_addr,
1829 	    toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
1830 
1831 #else	/* __i386 */
1832 
1833 	/*
1834 	 * allocate the bit map that tracks toxic pages
1835 	 */
1836 	toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
1837 	PRM_DEBUG(toxic_bit_map_len);
1838 	toxic_bit_map =
1839 	    kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
1840 	ASSERT(toxic_bit_map != NULL);
1841 	PRM_DEBUG(toxic_bit_map);
1842 
1843 #endif	/* __i386 */
1844 
1845 
1846 	/*
1847 	 * Now that we've got more VA, as well as the ability to allocate from
1848 	 * it, tell the debugger.
1849 	 */
1850 	if (boothowto & RB_DEBUG)
1851 		kdi_dvec_memavail();
1852 
1853 	/*
1854 	 * The following code installs a special page fault handler (#pf)
1855 	 * to work around a pentium bug.
1856 	 */
1857 #if !defined(__amd64) && !defined(__xpv)
1858 	if (x86_type == X86_TYPE_P5) {
1859 		desctbr_t idtr;
1860 		gate_desc_t *newidt;
1861 
1862 		if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL)
1863 			panic("failed to install pentium_pftrap");
1864 
1865 		bcopy(idt0, newidt, NIDT * sizeof (*idt0));
1866 		set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap,
1867 		    KCS_SEL, SDT_SYSIGT, TRP_KPL);
1868 
1869 		(void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE,
1870 		    PROT_READ | PROT_EXEC);
1871 
1872 		CPU->cpu_idt = newidt;
1873 		idtr.dtr_base = (uintptr_t)CPU->cpu_idt;
1874 		idtr.dtr_limit = (NIDT * sizeof (*idt0)) - 1;
1875 		wr_idtr(&idtr);
1876 	}
1877 #endif	/* !__amd64 */
1878 
1879 #if !defined(__xpv)
1880 	/*
1881 	 * Map page pfn=0 for drivers, such as kd, that need to pick up
1882 	 * parameters left there by controllers/BIOS.
1883 	 */
1884 	PRM_POINT("setup up p0_va");
1885 	p0_va = i86devmap(0, 1, PROT_READ);
1886 	PRM_DEBUG(p0_va);
1887 #endif
1888 
1889 	cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
1890 	    physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
1891 
1892 	/*
1893 	 * disable automatic large pages for small memory systems or
1894 	 * when the disable flag is set.
1895 	 *
1896 	 * Do not yet consider page sizes larger than 2m/4m.
1897 	 */
1898 	if (!auto_lpg_disable && mmu.max_page_level > 0) {
1899 		max_uheap_lpsize = LEVEL_SIZE(1);
1900 		max_ustack_lpsize = LEVEL_SIZE(1);
1901 		max_privmap_lpsize = LEVEL_SIZE(1);
1902 		max_uidata_lpsize = LEVEL_SIZE(1);
1903 		max_utext_lpsize = LEVEL_SIZE(1);
1904 		max_shm_lpsize = LEVEL_SIZE(1);
1905 	}
1906 	if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
1907 	    auto_lpg_disable) {
1908 		use_brk_lpg = 0;
1909 		use_stk_lpg = 0;
1910 	}
1911 	mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
1912 
1913 	PRM_POINT("Calling hat_init_finish()...");
1914 	hat_init_finish();
1915 	PRM_POINT("hat_init_finish() done");
1916 
1917 	/*
1918 	 * Initialize the segkp segment type.
1919 	 */
1920 	rw_enter(&kas.a_lock, RW_WRITER);
1921 	PRM_POINT("Attaching segkp");
1922 	if (segkp_fromheap) {
1923 		segkp->s_as = &kas;
1924 	} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
1925 	    segkp) < 0) {
1926 		panic("startup: cannot attach segkp");
1927 		/*NOTREACHED*/
1928 	}
1929 	PRM_POINT("Doing segkp_create()");
1930 	if (segkp_create(segkp) != 0) {
1931 		panic("startup: segkp_create failed");
1932 		/*NOTREACHED*/
1933 	}
1934 	PRM_DEBUG(segkp);
1935 	rw_exit(&kas.a_lock);
1936 
1937 	/*
1938 	 * kpm segment
1939 	 */
1940 	segmap_kpm = 0;
1941 	if (kpm_desired) {
1942 		kpm_init();
1943 		kpm_enable = 1;
1944 		vpm_enable = 1;
1945 	}
1946 
1947 	/*
1948 	 * Now create segmap segment.
1949 	 */
1950 	rw_enter(&kas.a_lock, RW_WRITER);
1951 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
1952 		panic("cannot attach segmap");
1953 		/*NOTREACHED*/
1954 	}
1955 	PRM_DEBUG(segmap);
1956 
1957 	a.prot = PROT_READ | PROT_WRITE;
1958 	a.shmsize = 0;
1959 	a.nfreelist = segmapfreelists;
1960 
1961 	if (segmap_create(segmap, (caddr_t)&a) != 0)
1962 		panic("segmap_create segmap");
1963 	rw_exit(&kas.a_lock);
1964 
1965 	setup_vaddr_for_ppcopy(CPU);
1966 
1967 	segdev_init();
1968 #if defined(__xpv)
1969 	if (DOMAIN_IS_INITDOMAIN(xen_info))
1970 #endif
1971 		pmem_init();
1972 
1973 	PRM_POINT("startup_vm() done");
1974 }
1975 
1976 /*
1977  * Load a tod module for the non-standard tod part found on this system.
1978  */
1979 static void
1980 load_tod_module(char *todmod)
1981 {
1982 	if (modload("tod", todmod) == -1)
1983 		halt("Can't load TOD module");
1984 }
1985 
1986 static void
1987 startup_end(void)
1988 {
1989 	int i;
1990 	extern void setx86isalist(void);
1991 
1992 	PRM_POINT("startup_end() starting...");
1993 
1994 	/*
1995 	 * Perform tasks that get done after most of the VM
1996 	 * initialization has been done but before the clock
1997 	 * and other devices get started.
1998 	 */
1999 	kern_setup1();
2000 
2001 	/*
2002 	 * Perform CPC initialization for this CPU.
2003 	 */
2004 	kcpc_hw_init(CPU);
2005 
2006 #if defined(OPTERON_WORKAROUND_6323525)
2007 	if (opteron_workaround_6323525)
2008 		patch_workaround_6323525();
2009 #endif
2010 	/*
2011 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
2012 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
2013 	 */
2014 	if (tod_module_name != NULL) {
2015 		PRM_POINT("load_tod_module()");
2016 		load_tod_module(tod_module_name);
2017 	}
2018 
2019 #if defined(__xpv)
2020 	/*
2021 	 * Forceload interposing TOD module for the hypervisor.
2022 	 */
2023 	PRM_POINT("load_tod_module()");
2024 	load_tod_module("xpvtod");
2025 #endif
2026 
2027 	/*
2028 	 * Configure the system.
2029 	 */
2030 	PRM_POINT("Calling configure()...");
2031 	configure();		/* set up devices */
2032 	PRM_POINT("configure() done");
2033 
2034 	/*
2035 	 * Set the isa_list string to the defined instruction sets we
2036 	 * support.
2037 	 */
2038 	setx86isalist();
2039 	cpu_intr_alloc(CPU, NINTR_THREADS);
2040 	psm_install();
2041 
2042 	/*
2043 	 * We're done with bootops.  We don't unmap the bootstrap yet because
2044 	 * we're still using bootsvcs.
2045 	 */
2046 	PRM_POINT("NULLing out bootops");
2047 	*bootopsp = (struct bootops *)NULL;
2048 	bootops = (struct bootops *)NULL;
2049 
2050 #if defined(__xpv)
2051 	ec_init_debug_irq();
2052 	xs_domu_init();
2053 #endif
2054 	PRM_POINT("Enabling interrupts");
2055 	(*picinitf)();
2056 	sti();
2057 #if defined(__xpv)
2058 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
2059 	xen_late_startup();
2060 #endif
2061 
2062 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
2063 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
2064 
2065 	/*
2066 	 * Register these software interrupts for ddi timer.
2067 	 * Software interrupts up to the level 10 are supported.
2068 	 */
2069 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
2070 		char name[sizeof ("timer_softintr") + 2];
2071 		(void) sprintf(name, "timer_softintr%02d", i);
2072 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
2073 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
2074 	}
2075 
2076 	PRM_POINT("startup_end() done");
2077 }
2078 
2079 extern char hw_serial[];
2080 char *_hs1107 = hw_serial;
2081 ulong_t  _bdhs34;
2082 
2083 void
2084 post_startup(void)
2085 {
2086 	/*
2087 	 * Set the system wide, processor-specific flags to be passed
2088 	 * to userland via the aux vector for performance hints and
2089 	 * instruction set extensions.
2090 	 */
2091 	bind_hwcap();
2092 
2093 #ifdef __xpv
2094 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2095 #endif
2096 	{
2097 		/*
2098 		 * Load the System Management BIOS into the global ksmbios
2099 		 * handle, if an SMBIOS is present on this system.
2100 		 */
2101 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
2102 
2103 #if defined(__xpv)
2104 		xpv_panic_init();
2105 #else
2106 		/*
2107 		 * Startup the memory scrubber.
2108 		 * XXPV	This should be running somewhere ..
2109 		 */
2110 		if (!xpv_is_hvm)
2111 			memscrub_init();
2112 #endif
2113 	}
2114 
2115 	/*
2116 	 * Complete CPU module initialization
2117 	 */
2118 	cmi_post_startup();
2119 
2120 	/*
2121 	 * Perform forceloading tasks for /etc/system.
2122 	 */
2123 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
2124 
2125 	/*
2126 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
2127 	 * ON4.0: This must be fixed or restated in /etc/systems.
2128 	 */
2129 	(void) modload("fs", "procfs");
2130 
2131 	(void) i_ddi_attach_hw_nodes("pit_beep");
2132 
2133 #if defined(__i386)
2134 	/*
2135 	 * Check for required functional Floating Point hardware,
2136 	 * unless FP hardware explicitly disabled.
2137 	 */
2138 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
2139 		halt("No working FP hardware found");
2140 #endif
2141 
2142 	maxmem = freemem;
2143 
2144 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
2145 }
2146 
2147 static int
2148 pp_in_ramdisk(page_t *pp)
2149 {
2150 	extern uint64_t ramdisk_start, ramdisk_end;
2151 
2152 	return ((pp->p_pagenum >= btop(ramdisk_start)) &&
2153 	    (pp->p_pagenum < btopr(ramdisk_end)));
2154 }
2155 
2156 void
2157 release_bootstrap(void)
2158 {
2159 	int root_is_ramdisk;
2160 	page_t *pp;
2161 	extern void kobj_boot_unmountroot(void);
2162 	extern dev_t rootdev;
2163 #if !defined(__xpv)
2164 	pfn_t	pfn;
2165 #endif
2166 
2167 	/* unmount boot ramdisk and release kmem usage */
2168 	kobj_boot_unmountroot();
2169 
2170 	/*
2171 	 * We're finished using the boot loader so free its pages.
2172 	 */
2173 	PRM_POINT("Unmapping lower boot pages");
2174 	clear_boot_mappings(0, _userlimit);
2175 	postbootkernelbase = kernelbase;
2176 
2177 	/*
2178 	 * If root isn't on ramdisk, destroy the hardcoded
2179 	 * ramdisk node now and release the memory. Else,
2180 	 * ramdisk memory is kept in rd_pages.
2181 	 */
2182 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
2183 	if (!root_is_ramdisk) {
2184 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
2185 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
2186 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
2187 		(void) ddi_remove_child(dip, 0);
2188 	}
2189 
2190 	PRM_POINT("Releasing boot pages");
2191 	while (bootpages) {
2192 		pp = bootpages;
2193 		bootpages = pp->p_next;
2194 		if (root_is_ramdisk && pp_in_ramdisk(pp)) {
2195 			pp->p_next = rd_pages;
2196 			rd_pages = pp;
2197 			continue;
2198 		}
2199 		pp->p_next = (struct page *)0;
2200 		pp->p_prev = (struct page *)0;
2201 		PP_CLRBOOTPAGES(pp);
2202 		page_free(pp, 1);
2203 	}
2204 	PRM_POINT("Boot pages released");
2205 
2206 #if !defined(__xpv)
2207 /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
2208 	/*
2209 	 * Find 1 page below 1 MB so that other processors can boot up or
2210 	 * so that any processor can resume.
2211 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
2212 	 * We should have just free'd one up.
2213 	 */
2214 	for (pfn = 1; pfn < btop(1*1024*1024); pfn++) {
2215 		if (page_numtopp_alloc(pfn) == NULL)
2216 			continue;
2217 		rm_platter_va = i86devmap(pfn, 1,
2218 		    PROT_READ | PROT_WRITE | PROT_EXEC);
2219 		rm_platter_pa = ptob(pfn);
2220 		hat_devload(kas.a_hat,
2221 		    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
2222 		    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
2223 		    HAT_LOAD_NOCONSIST);
2224 		break;
2225 	}
2226 	if (pfn == btop(1*1024*1024) && use_mp)
2227 		panic("No page below 1M available for starting "
2228 		    "other processors or for resuming from system-suspend");
2229 #endif	/* !__xpv */
2230 }
2231 
2232 /*
2233  * Initialize the platform-specific parts of a page_t.
2234  */
2235 void
2236 add_physmem_cb(page_t *pp, pfn_t pnum)
2237 {
2238 	pp->p_pagenum = pnum;
2239 	pp->p_mapping = NULL;
2240 	pp->p_embed = 0;
2241 	pp->p_share = 0;
2242 	pp->p_mlentry = 0;
2243 }
2244 
2245 /*
2246  * kphysm_init() initializes physical memory.
2247  */
2248 static pgcnt_t
2249 kphysm_init(
2250 	page_t *pp,
2251 	pgcnt_t npages)
2252 {
2253 	struct memlist	*pmem;
2254 	struct memseg	*cur_memseg;
2255 	pfn_t		base_pfn;
2256 	pgcnt_t		num;
2257 	pgcnt_t		pages_done = 0;
2258 	uint64_t	addr;
2259 	uint64_t	size;
2260 	extern pfn_t	ddiphysmin;
2261 
2262 	ASSERT(page_hash != NULL && page_hashsz != 0);
2263 
2264 	cur_memseg = memseg_base;
2265 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
2266 		/*
2267 		 * In a 32 bit kernel can't use higher memory if we're
2268 		 * not booting in PAE mode. This check takes care of that.
2269 		 */
2270 		addr = pmem->address;
2271 		size = pmem->size;
2272 		if (btop(addr) > physmax)
2273 			continue;
2274 
2275 		/*
2276 		 * align addr and size - they may not be at page boundaries
2277 		 */
2278 		if ((addr & MMU_PAGEOFFSET) != 0) {
2279 			addr += MMU_PAGEOFFSET;
2280 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
2281 			size -= addr - pmem->address;
2282 		}
2283 
2284 		/* only process pages below or equal to physmax */
2285 		if ((btop(addr + size) - 1) > physmax)
2286 			size = ptob(physmax - btop(addr) + 1);
2287 
2288 		num = btop(size);
2289 		if (num == 0)
2290 			continue;
2291 
2292 		if (num > npages)
2293 			num = npages;
2294 
2295 		npages -= num;
2296 		pages_done += num;
2297 		base_pfn = btop(addr);
2298 
2299 		if (prom_debug)
2300 			prom_printf("MEMSEG addr=0x%" PRIx64
2301 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
2302 			    addr, num, base_pfn, base_pfn + num);
2303 
2304 		/*
2305 		 * Ignore pages below ddiphysmin to simplify ddi memory
2306 		 * allocation with non-zero addr_lo requests.
2307 		 */
2308 		if (base_pfn < ddiphysmin) {
2309 			if (base_pfn + num <= ddiphysmin)
2310 				continue;
2311 			pp += (ddiphysmin - base_pfn);
2312 			num -= (ddiphysmin - base_pfn);
2313 			base_pfn = ddiphysmin;
2314 		}
2315 
2316 		/*
2317 		 * Build the memsegs entry
2318 		 */
2319 		cur_memseg->pages = pp;
2320 		cur_memseg->epages = pp + num;
2321 		cur_memseg->pages_base = base_pfn;
2322 		cur_memseg->pages_end = base_pfn + num;
2323 
2324 		/*
2325 		 * Insert into memseg list in decreasing pfn range order.
2326 		 * Low memory is typically more fragmented such that this
2327 		 * ordering keeps the larger ranges at the front of the list
2328 		 * for code that searches memseg.
2329 		 * This ASSERTS that the memsegs coming in from boot are in
2330 		 * increasing physical address order and not contiguous.
2331 		 */
2332 		if (memsegs != NULL) {
2333 			ASSERT(cur_memseg->pages_base >= memsegs->pages_end);
2334 			cur_memseg->next = memsegs;
2335 		}
2336 		memsegs = cur_memseg;
2337 
2338 		/*
2339 		 * add_physmem() initializes the PSM part of the page
2340 		 * struct by calling the PSM back with add_physmem_cb().
2341 		 * In addition it coalesces pages into larger pages as
2342 		 * it initializes them.
2343 		 */
2344 		add_physmem(pp, num, base_pfn);
2345 		cur_memseg++;
2346 		availrmem_initial += num;
2347 		availrmem += num;
2348 
2349 		pp += num;
2350 	}
2351 
2352 	PRM_DEBUG(availrmem_initial);
2353 	PRM_DEBUG(availrmem);
2354 	PRM_DEBUG(freemem);
2355 	build_pfn_hash();
2356 	return (pages_done);
2357 }
2358 
2359 /*
2360  * Kernel VM initialization.
2361  */
2362 static void
2363 kvm_init(void)
2364 {
2365 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
2366 
2367 	/*
2368 	 * Put the kernel segments in kernel address space.
2369 	 */
2370 	rw_enter(&kas.a_lock, RW_WRITER);
2371 	as_avlinit(&kas);
2372 
2373 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
2374 	(void) segkmem_create(&ktextseg);
2375 
2376 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
2377 	(void) segkmem_create(&kvalloc);
2378 
2379 	(void) seg_attach(&kas, kernelheap,
2380 	    ekernelheap - kernelheap, &kvseg);
2381 	(void) segkmem_create(&kvseg);
2382 
2383 	if (core_size > 0) {
2384 		PRM_POINT("attaching kvseg_core");
2385 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
2386 		    &kvseg_core);
2387 		(void) segkmem_create(&kvseg_core);
2388 	}
2389 
2390 	if (segziosize > 0) {
2391 		PRM_POINT("attaching segzio");
2392 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2393 		    &kzioseg);
2394 		(void) segkmem_zio_create(&kzioseg);
2395 
2396 		/* create zio area covering new segment */
2397 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2398 	}
2399 
2400 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
2401 	(void) segkmem_create(&kdebugseg);
2402 
2403 	rw_exit(&kas.a_lock);
2404 
2405 	/*
2406 	 * Ensure that the red zone at kernelbase is never accessible.
2407 	 */
2408 	PRM_POINT("protecting redzone");
2409 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
2410 
2411 	/*
2412 	 * Make the text writable so that it can be hot patched by DTrace.
2413 	 */
2414 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
2415 	    PROT_READ | PROT_WRITE | PROT_EXEC);
2416 
2417 	/*
2418 	 * Make data writable until end.
2419 	 */
2420 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
2421 	    PROT_READ | PROT_WRITE | PROT_EXEC);
2422 }
2423 
2424 #ifndef __xpv
2425 /*
2426  * Solaris adds an entry for Write Combining caching to the PAT
2427  */
2428 static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
2429 
2430 void
2431 pat_sync(void)
2432 {
2433 	ulong_t	cr0, cr0_orig, cr4;
2434 
2435 	if (!(x86_feature & X86_PAT))
2436 		return;
2437 	cr0_orig = cr0 = getcr0();
2438 	cr4 = getcr4();
2439 
2440 	/* disable caching and flush all caches and TLBs */
2441 	cr0 |= CR0_CD;
2442 	cr0 &= ~CR0_NW;
2443 	setcr0(cr0);
2444 	invalidate_cache();
2445 	if (cr4 & CR4_PGE) {
2446 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
2447 		setcr4(cr4);
2448 	} else {
2449 		reload_cr3();
2450 	}
2451 
2452 	/* add our entry to the PAT */
2453 	wrmsr(REG_PAT, pat_attr_reg);
2454 
2455 	/* flush TLBs and cache again, then reenable cr0 caching */
2456 	if (cr4 & CR4_PGE) {
2457 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
2458 		setcr4(cr4);
2459 	} else {
2460 		reload_cr3();
2461 	}
2462 	invalidate_cache();
2463 	setcr0(cr0_orig);
2464 }
2465 
2466 #endif /* !__xpv */
2467 
2468 void
2469 get_system_configuration(void)
2470 {
2471 	char	prop[32];
2472 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
2473 
2474 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
2475 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
2476 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
2477 	    nodes_ll > MAXNODES ||
2478 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
2479 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
2480 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
2481 		system_hardware.hd_nodes = 1;
2482 		system_hardware.hd_cpus_per_node = 0;
2483 	} else {
2484 		system_hardware.hd_nodes = (int)nodes_ll;
2485 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
2486 	}
2487 
2488 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
2489 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
2490 	    kobj_getvalue(prop, &lvalue) == -1)
2491 		eprom_kernelbase = NULL;
2492 	else
2493 		eprom_kernelbase = (uintptr_t)lvalue;
2494 
2495 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
2496 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
2497 	    kobj_getvalue(prop, &lvalue) == -1)
2498 		segmapsize = SEGMAPDEFAULT;
2499 	else
2500 		segmapsize = (uintptr_t)lvalue;
2501 
2502 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
2503 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
2504 	    kobj_getvalue(prop, &lvalue) == -1)
2505 		segmapfreelists = 0;	/* use segmap driver default */
2506 	else
2507 		segmapfreelists = (int)lvalue;
2508 
2509 	/* physmem used to be here, but moved much earlier to fakebop.c */
2510 }
2511 
2512 /*
2513  * Add to a memory list.
2514  * start = start of new memory segment
2515  * len = length of new memory segment in bytes
2516  * new = pointer to a new struct memlist
2517  * memlistp = memory list to which to add segment.
2518  */
2519 void
2520 memlist_add(
2521 	uint64_t start,
2522 	uint64_t len,
2523 	struct memlist *new,
2524 	struct memlist **memlistp)
2525 {
2526 	struct memlist *cur;
2527 	uint64_t end = start + len;
2528 
2529 	new->address = start;
2530 	new->size = len;
2531 
2532 	cur = *memlistp;
2533 
2534 	while (cur) {
2535 		if (cur->address >= end) {
2536 			new->next = cur;
2537 			*memlistp = new;
2538 			new->prev = cur->prev;
2539 			cur->prev = new;
2540 			return;
2541 		}
2542 		ASSERT(cur->address + cur->size <= start);
2543 		if (cur->next == NULL) {
2544 			cur->next = new;
2545 			new->prev = cur;
2546 			new->next = NULL;
2547 			return;
2548 		}
2549 		memlistp = &cur->next;
2550 		cur = cur->next;
2551 	}
2552 }
2553 
2554 void
2555 kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
2556 {
2557 	size_t tsize = e_modtext - modtext;
2558 	size_t dsize = e_moddata - moddata;
2559 
2560 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
2561 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
2562 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
2563 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
2564 }
2565 
2566 caddr_t
2567 kobj_text_alloc(vmem_t *arena, size_t size)
2568 {
2569 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
2570 }
2571 
2572 /*ARGSUSED*/
2573 caddr_t
2574 kobj_texthole_alloc(caddr_t addr, size_t size)
2575 {
2576 	panic("unexpected call to kobj_texthole_alloc()");
2577 	/*NOTREACHED*/
2578 	return (0);
2579 }
2580 
2581 /*ARGSUSED*/
2582 void
2583 kobj_texthole_free(caddr_t addr, size_t size)
2584 {
2585 	panic("unexpected call to kobj_texthole_free()");
2586 }
2587 
2588 /*
2589  * This is called just after configure() in startup().
2590  *
2591  * The ISALIST concept is a bit hopeless on Intel, because
2592  * there's no guarantee of an ever-more-capable processor
2593  * given that various parts of the instruction set may appear
2594  * and disappear between different implementations.
2595  *
2596  * While it would be possible to correct it and even enhance
2597  * it somewhat, the explicit hardware capability bitmask allows
2598  * more flexibility.
2599  *
2600  * So, we just leave this alone.
2601  */
2602 void
2603 setx86isalist(void)
2604 {
2605 	char *tp;
2606 	size_t len;
2607 	extern char *isa_list;
2608 
2609 #define	TBUFSIZE	1024
2610 
2611 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
2612 	*tp = '\0';
2613 
2614 #if defined(__amd64)
2615 	(void) strcpy(tp, "amd64 ");
2616 #endif
2617 
2618 	switch (x86_vendor) {
2619 	case X86_VENDOR_Intel:
2620 	case X86_VENDOR_AMD:
2621 	case X86_VENDOR_TM:
2622 		if (x86_feature & X86_CMOV) {
2623 			/*
2624 			 * Pentium Pro or later
2625 			 */
2626 			(void) strcat(tp, "pentium_pro");
2627 			(void) strcat(tp, x86_feature & X86_MMX ?
2628 			    "+mmx pentium_pro " : " ");
2629 		}
2630 		/*FALLTHROUGH*/
2631 	case X86_VENDOR_Cyrix:
2632 		/*
2633 		 * The Cyrix 6x86 does not have any Pentium features
2634 		 * accessible while not at privilege level 0.
2635 		 */
2636 		if (x86_feature & X86_CPUID) {
2637 			(void) strcat(tp, "pentium");
2638 			(void) strcat(tp, x86_feature & X86_MMX ?
2639 			    "+mmx pentium " : " ");
2640 		}
2641 		break;
2642 	default:
2643 		break;
2644 	}
2645 	(void) strcat(tp, "i486 i386 i86");
2646 	len = strlen(tp) + 1;   /* account for NULL at end of string */
2647 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
2648 	kmem_free(tp, TBUFSIZE);
2649 
2650 #undef TBUFSIZE
2651 }
2652 
2653 
2654 #ifdef __amd64
2655 
2656 void *
2657 device_arena_alloc(size_t size, int vm_flag)
2658 {
2659 	return (vmem_alloc(device_arena, size, vm_flag));
2660 }
2661 
2662 void
2663 device_arena_free(void *vaddr, size_t size)
2664 {
2665 	vmem_free(device_arena, vaddr, size);
2666 }
2667 
2668 #else /* __i386 */
2669 
2670 void *
2671 device_arena_alloc(size_t size, int vm_flag)
2672 {
2673 	caddr_t	vaddr;
2674 	uintptr_t v;
2675 	size_t	start;
2676 	size_t	end;
2677 
2678 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
2679 	if (vaddr == NULL)
2680 		return (NULL);
2681 
2682 	v = (uintptr_t)vaddr;
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 		BT_ATOMIC_SET(toxic_bit_map, start);
2693 		++start;
2694 	}
2695 	return (vaddr);
2696 }
2697 
2698 void
2699 device_arena_free(void *vaddr, size_t size)
2700 {
2701 	uintptr_t v = (uintptr_t)vaddr;
2702 	size_t	start;
2703 	size_t	end;
2704 
2705 	ASSERT(v >= kernelbase);
2706 	ASSERT(v + size <= valloc_base);
2707 
2708 	start = btop(v - kernelbase);
2709 	end = btop(v + size - 1 - kernelbase);
2710 	ASSERT(start < toxic_bit_map_len);
2711 	ASSERT(end < toxic_bit_map_len);
2712 
2713 	while (start <= end) {
2714 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
2715 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
2716 		++start;
2717 	}
2718 	vmem_free(heap_arena, vaddr, size);
2719 }
2720 
2721 /*
2722  * returns 1st address in range that is in device arena, or NULL
2723  * if len is not NULL it returns the length of the toxic range
2724  */
2725 void *
2726 device_arena_contains(void *vaddr, size_t size, size_t *len)
2727 {
2728 	uintptr_t v = (uintptr_t)vaddr;
2729 	uintptr_t eaddr = v + size;
2730 	size_t start;
2731 	size_t end;
2732 
2733 	/*
2734 	 * if called very early by kmdb, just return NULL
2735 	 */
2736 	if (toxic_bit_map == NULL)
2737 		return (NULL);
2738 
2739 	/*
2740 	 * First check if we're completely outside the bitmap range.
2741 	 */
2742 	if (v >= valloc_base || eaddr < kernelbase)
2743 		return (NULL);
2744 
2745 	/*
2746 	 * Trim ends of search to look at only what the bitmap covers.
2747 	 */
2748 	if (v < kernelbase)
2749 		v = kernelbase;
2750 	start = btop(v - kernelbase);
2751 	end = btop(eaddr - kernelbase);
2752 	if (end >= toxic_bit_map_len)
2753 		end = toxic_bit_map_len;
2754 
2755 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
2756 		return (NULL);
2757 
2758 	v = kernelbase + ptob(start);
2759 	if (len != NULL)
2760 		*len = ptob(end - start);
2761 	return ((void *)v);
2762 }
2763 
2764 #endif	/* __i386 */
2765