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