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