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