xref: /titanic_50/usr/src/uts/i86pc/os/startup.c (revision 63251bc7f1ca38259078c48e316fee4ed66d4e93)
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 		vpm_enable = 1;
1973 	}
1974 
1975 	/*
1976 	 * Now create segmap segment.
1977 	 */
1978 	rw_enter(&kas.a_lock, RW_WRITER);
1979 	if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
1980 		panic("cannot attach segmap");
1981 		/*NOTREACHED*/
1982 	}
1983 	PRM_DEBUG(segmap);
1984 
1985 	a.prot = PROT_READ | PROT_WRITE;
1986 	a.shmsize = 0;
1987 	a.nfreelist = segmapfreelists;
1988 
1989 	if (segmap_create(segmap, (caddr_t)&a) != 0)
1990 		panic("segmap_create segmap");
1991 	rw_exit(&kas.a_lock);
1992 
1993 	setup_vaddr_for_ppcopy(CPU);
1994 
1995 	segdev_init();
1996 #if defined(__xpv)
1997 	if (DOMAIN_IS_INITDOMAIN(xen_info))
1998 #endif
1999 		pmem_init();
2000 
2001 	PRM_POINT("startup_vm() done");
2002 }
2003 
2004 /*
2005  * Load a tod module for the non-standard tod part found on this system.
2006  */
2007 static void
2008 load_tod_module(char *todmod)
2009 {
2010 	if (modload("tod", todmod) == -1)
2011 		halt("Can't load TOD module");
2012 }
2013 
2014 static void
2015 startup_end(void)
2016 {
2017 	int i;
2018 	extern void setx86isalist(void);
2019 
2020 	PRM_POINT("startup_end() starting...");
2021 
2022 	/*
2023 	 * Perform tasks that get done after most of the VM
2024 	 * initialization has been done but before the clock
2025 	 * and other devices get started.
2026 	 */
2027 	kern_setup1();
2028 
2029 	/*
2030 	 * Perform CPC initialization for this CPU.
2031 	 */
2032 	kcpc_hw_init(CPU);
2033 
2034 #if defined(OPTERON_WORKAROUND_6323525)
2035 	if (opteron_workaround_6323525)
2036 		patch_workaround_6323525();
2037 #endif
2038 	/*
2039 	 * If needed, load TOD module now so that ddi_get_time(9F) etc. work
2040 	 * (For now, "needed" is defined as set tod_module_name in /etc/system)
2041 	 */
2042 	if (tod_module_name != NULL) {
2043 		PRM_POINT("load_tod_module()");
2044 		load_tod_module(tod_module_name);
2045 	}
2046 
2047 #if defined(__xpv)
2048 	/*
2049 	 * Forceload interposing TOD module for the hypervisor.
2050 	 */
2051 	PRM_POINT("load_tod_module()");
2052 	load_tod_module("xpvtod");
2053 #endif
2054 
2055 	/*
2056 	 * Configure the system.
2057 	 */
2058 	PRM_POINT("Calling configure()...");
2059 	configure();		/* set up devices */
2060 	PRM_POINT("configure() done");
2061 
2062 	/*
2063 	 * Set the isa_list string to the defined instruction sets we
2064 	 * support.
2065 	 */
2066 	setx86isalist();
2067 	cpu_intr_alloc(CPU, NINTR_THREADS);
2068 	psm_install();
2069 
2070 	/*
2071 	 * We're done with bootops.  We don't unmap the bootstrap yet because
2072 	 * we're still using bootsvcs.
2073 	 */
2074 	PRM_POINT("NULLing out bootops");
2075 	*bootopsp = (struct bootops *)NULL;
2076 	bootops = (struct bootops *)NULL;
2077 
2078 #if defined(__xpv)
2079 	ec_init_debug_irq();
2080 	xs_domu_init();
2081 #endif
2082 	PRM_POINT("Enabling interrupts");
2083 	(*picinitf)();
2084 	sti();
2085 #if defined(__xpv)
2086 	ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
2087 	xen_late_startup();
2088 #endif
2089 
2090 	(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
2091 	    "softlevel1", NULL, NULL); /* XXX to be moved later */
2092 
2093 	/*
2094 	 * Register these software interrupts for ddi timer.
2095 	 * Software interrupts up to the level 10 are supported.
2096 	 */
2097 	for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
2098 		char name[sizeof ("timer_softintr") + 2];
2099 		(void) sprintf(name, "timer_softintr%02d", i);
2100 		(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
2101 		    (avfunc)timer_softintr, name, (caddr_t)(uintptr_t)i, NULL);
2102 	}
2103 
2104 #if !defined(__xpv)
2105 	if (modload("drv", "amd_iommu") < 0) {
2106 		PRM_POINT("No AMD IOMMU present\n");
2107 	} else if (ddi_hold_installed_driver(ddi_name_to_major(
2108 	    "amd_iommu")) == NULL) {
2109 		prom_printf("ERROR: failed to attach AMD IOMMU\n");
2110 	}
2111 #endif
2112 	post_startup_cpu_fixups();
2113 
2114 	PRM_POINT("startup_end() done");
2115 }
2116 
2117 /*
2118  * Don't remove the following 2 variables.  They are necessary
2119  * for reading the hostid from the legacy file (/kernel/misc/sysinit).
2120  */
2121 char *_hs1107 = hw_serial;
2122 ulong_t  _bdhs34;
2123 
2124 void
2125 post_startup(void)
2126 {
2127 	extern void cpupm_init(cpu_t *);
2128 
2129 	/*
2130 	 * Set the system wide, processor-specific flags to be passed
2131 	 * to userland via the aux vector for performance hints and
2132 	 * instruction set extensions.
2133 	 */
2134 	bind_hwcap();
2135 
2136 #ifdef __xpv
2137 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2138 #endif
2139 	{
2140 		/*
2141 		 * Load the System Management BIOS into the global ksmbios
2142 		 * handle, if an SMBIOS is present on this system.
2143 		 */
2144 		ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
2145 
2146 #if defined(__xpv)
2147 		xpv_panic_init();
2148 #else
2149 		/*
2150 		 * Startup the memory scrubber.
2151 		 * XXPV	This should be running somewhere ..
2152 		 */
2153 		if (get_hwenv() != HW_XEN_HVM)
2154 			memscrub_init();
2155 #endif
2156 	}
2157 
2158 	/*
2159 	 * Complete CPU module initialization
2160 	 */
2161 	cmi_post_startup();
2162 
2163 	/*
2164 	 * Perform forceloading tasks for /etc/system.
2165 	 */
2166 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
2167 
2168 	/*
2169 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
2170 	 * ON4.0: This must be fixed or restated in /etc/systems.
2171 	 */
2172 	(void) modload("fs", "procfs");
2173 
2174 	(void) i_ddi_attach_hw_nodes("pit_beep");
2175 
2176 #if defined(__i386)
2177 	/*
2178 	 * Check for required functional Floating Point hardware,
2179 	 * unless FP hardware explicitly disabled.
2180 	 */
2181 	if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
2182 		halt("No working FP hardware found");
2183 #endif
2184 
2185 	maxmem = freemem;
2186 
2187 	cpupm_init(CPU);
2188 
2189 	add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi);
2190 
2191 	pg_init();
2192 }
2193 
2194 static int
2195 pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
2196 {
2197 	return ((pp->p_pagenum >= btop(low_addr)) &&
2198 	    (pp->p_pagenum < btopr(high_addr)));
2199 }
2200 
2201 void
2202 release_bootstrap(void)
2203 {
2204 	int root_is_ramdisk;
2205 	page_t *pp;
2206 	extern void kobj_boot_unmountroot(void);
2207 	extern dev_t rootdev;
2208 #if !defined(__xpv)
2209 	pfn_t	pfn;
2210 #endif
2211 
2212 	/* unmount boot ramdisk and release kmem usage */
2213 	kobj_boot_unmountroot();
2214 
2215 	/*
2216 	 * We're finished using the boot loader so free its pages.
2217 	 */
2218 	PRM_POINT("Unmapping lower boot pages");
2219 
2220 	clear_boot_mappings(0, _userlimit);
2221 
2222 	postbootkernelbase = kernelbase;
2223 
2224 	/*
2225 	 * If root isn't on ramdisk, destroy the hardcoded
2226 	 * ramdisk node now and release the memory. Else,
2227 	 * ramdisk memory is kept in rd_pages.
2228 	 */
2229 	root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
2230 	if (!root_is_ramdisk) {
2231 		dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
2232 		ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
2233 		ndi_rele_devi(dip);	/* held from ddi_find_devinfo */
2234 		(void) ddi_remove_child(dip, 0);
2235 	}
2236 
2237 	PRM_POINT("Releasing boot pages");
2238 	while (bootpages) {
2239 		extern uint64_t ramdisk_start, ramdisk_end;
2240 		pp = bootpages;
2241 		bootpages = pp->p_next;
2242 
2243 
2244 		/* Keep pages for the lower 64K */
2245 		if (pp_in_range(pp, 0, 0x40000)) {
2246 			pp->p_next = lower_pages;
2247 			lower_pages = pp;
2248 			lower_pages_count++;
2249 			continue;
2250 		}
2251 
2252 
2253 		if (root_is_ramdisk && pp_in_range(pp, ramdisk_start,
2254 		    ramdisk_end)) {
2255 			pp->p_next = rd_pages;
2256 			rd_pages = pp;
2257 			continue;
2258 		}
2259 		pp->p_next = (struct page *)0;
2260 		pp->p_prev = (struct page *)0;
2261 		PP_CLRBOOTPAGES(pp);
2262 		page_free(pp, 1);
2263 	}
2264 	PRM_POINT("Boot pages released");
2265 
2266 #if !defined(__xpv)
2267 /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
2268 	/*
2269 	 * Find 1 page below 1 MB so that other processors can boot up or
2270 	 * so that any processor can resume.
2271 	 * Make sure it has a kernel VA as well as a 1:1 mapping.
2272 	 * We should have just free'd one up.
2273 	 */
2274 
2275 	/*
2276 	 * 0x10 pages is 64K.  Leave the bottom 64K alone
2277 	 * for BIOS.
2278 	 */
2279 	for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
2280 		if (page_numtopp_alloc(pfn) == NULL)
2281 			continue;
2282 		rm_platter_va = i86devmap(pfn, 1,
2283 		    PROT_READ | PROT_WRITE | PROT_EXEC);
2284 		rm_platter_pa = ptob(pfn);
2285 		hat_devload(kas.a_hat,
2286 		    (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
2287 		    pfn, PROT_READ | PROT_WRITE | PROT_EXEC,
2288 		    HAT_LOAD_NOCONSIST);
2289 		break;
2290 	}
2291 	if (pfn == btop(1*1024*1024) && use_mp)
2292 		panic("No page below 1M available for starting "
2293 		    "other processors or for resuming from system-suspend");
2294 #endif	/* !__xpv */
2295 }
2296 
2297 /*
2298  * Initialize the platform-specific parts of a page_t.
2299  */
2300 void
2301 add_physmem_cb(page_t *pp, pfn_t pnum)
2302 {
2303 	pp->p_pagenum = pnum;
2304 	pp->p_mapping = NULL;
2305 	pp->p_embed = 0;
2306 	pp->p_share = 0;
2307 	pp->p_mlentry = 0;
2308 }
2309 
2310 /*
2311  * kphysm_init() initializes physical memory.
2312  */
2313 static pgcnt_t
2314 kphysm_init(
2315 	page_t *pp,
2316 	pgcnt_t npages)
2317 {
2318 	struct memlist	*pmem;
2319 	struct memseg	*cur_memseg;
2320 	pfn_t		base_pfn;
2321 	pgcnt_t		num;
2322 	pgcnt_t		pages_done = 0;
2323 	uint64_t	addr;
2324 	uint64_t	size;
2325 	extern pfn_t	ddiphysmin;
2326 
2327 	ASSERT(page_hash != NULL && page_hashsz != 0);
2328 
2329 	cur_memseg = memseg_base;
2330 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
2331 		/*
2332 		 * In a 32 bit kernel can't use higher memory if we're
2333 		 * not booting in PAE mode. This check takes care of that.
2334 		 */
2335 		addr = pmem->address;
2336 		size = pmem->size;
2337 		if (btop(addr) > physmax)
2338 			continue;
2339 
2340 		/*
2341 		 * align addr and size - they may not be at page boundaries
2342 		 */
2343 		if ((addr & MMU_PAGEOFFSET) != 0) {
2344 			addr += MMU_PAGEOFFSET;
2345 			addr &= ~(uint64_t)MMU_PAGEOFFSET;
2346 			size -= addr - pmem->address;
2347 		}
2348 
2349 		/* only process pages below or equal to physmax */
2350 		if ((btop(addr + size) - 1) > physmax)
2351 			size = ptob(physmax - btop(addr) + 1);
2352 
2353 		num = btop(size);
2354 		if (num == 0)
2355 			continue;
2356 
2357 		if (num > npages)
2358 			num = npages;
2359 
2360 		npages -= num;
2361 		pages_done += num;
2362 		base_pfn = btop(addr);
2363 
2364 		if (prom_debug)
2365 			prom_printf("MEMSEG addr=0x%" PRIx64
2366 			    " pgs=0x%lx pfn 0x%lx-0x%lx\n",
2367 			    addr, num, base_pfn, base_pfn + num);
2368 
2369 		/*
2370 		 * Ignore pages below ddiphysmin to simplify ddi memory
2371 		 * allocation with non-zero addr_lo requests.
2372 		 */
2373 		if (base_pfn < ddiphysmin) {
2374 			if (base_pfn + num <= ddiphysmin)
2375 				continue;
2376 			pp += (ddiphysmin - base_pfn);
2377 			num -= (ddiphysmin - base_pfn);
2378 			base_pfn = ddiphysmin;
2379 		}
2380 
2381 		/*
2382 		 * Build the memsegs entry
2383 		 */
2384 		cur_memseg->pages = pp;
2385 		cur_memseg->epages = pp + num;
2386 		cur_memseg->pages_base = base_pfn;
2387 		cur_memseg->pages_end = base_pfn + num;
2388 
2389 		/*
2390 		 * Insert into memseg list in decreasing pfn range order.
2391 		 * Low memory is typically more fragmented such that this
2392 		 * ordering keeps the larger ranges at the front of the list
2393 		 * for code that searches memseg.
2394 		 * This ASSERTS that the memsegs coming in from boot are in
2395 		 * increasing physical address order and not contiguous.
2396 		 */
2397 		if (memsegs != NULL) {
2398 			ASSERT(cur_memseg->pages_base >= memsegs->pages_end);
2399 			cur_memseg->next = memsegs;
2400 		}
2401 		memsegs = cur_memseg;
2402 
2403 		/*
2404 		 * add_physmem() initializes the PSM part of the page
2405 		 * struct by calling the PSM back with add_physmem_cb().
2406 		 * In addition it coalesces pages into larger pages as
2407 		 * it initializes them.
2408 		 */
2409 		add_physmem(pp, num, base_pfn);
2410 		cur_memseg++;
2411 		availrmem_initial += num;
2412 		availrmem += num;
2413 
2414 		pp += num;
2415 	}
2416 
2417 	PRM_DEBUG(availrmem_initial);
2418 	PRM_DEBUG(availrmem);
2419 	PRM_DEBUG(freemem);
2420 	build_pfn_hash();
2421 	return (pages_done);
2422 }
2423 
2424 /*
2425  * Kernel VM initialization.
2426  */
2427 static void
2428 kvm_init(void)
2429 {
2430 	ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
2431 
2432 	/*
2433 	 * Put the kernel segments in kernel address space.
2434 	 */
2435 	rw_enter(&kas.a_lock, RW_WRITER);
2436 	as_avlinit(&kas);
2437 
2438 	(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
2439 	(void) segkmem_create(&ktextseg);
2440 
2441 	(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
2442 	(void) segkmem_create(&kvalloc);
2443 
2444 	(void) seg_attach(&kas, kernelheap,
2445 	    ekernelheap - kernelheap, &kvseg);
2446 	(void) segkmem_create(&kvseg);
2447 
2448 	if (core_size > 0) {
2449 		PRM_POINT("attaching kvseg_core");
2450 		(void) seg_attach(&kas, (caddr_t)core_base, core_size,
2451 		    &kvseg_core);
2452 		(void) segkmem_create(&kvseg_core);
2453 	}
2454 
2455 	if (segziosize > 0) {
2456 		PRM_POINT("attaching segzio");
2457 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2458 		    &kzioseg);
2459 		(void) segkmem_zio_create(&kzioseg);
2460 
2461 		/* create zio area covering new segment */
2462 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2463 	}
2464 
2465 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
2466 	(void) segkmem_create(&kdebugseg);
2467 
2468 	rw_exit(&kas.a_lock);
2469 
2470 	/*
2471 	 * Ensure that the red zone at kernelbase is never accessible.
2472 	 */
2473 	PRM_POINT("protecting redzone");
2474 	(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
2475 
2476 	/*
2477 	 * Make the text writable so that it can be hot patched by DTrace.
2478 	 */
2479 	(void) as_setprot(&kas, s_text, e_modtext - s_text,
2480 	    PROT_READ | PROT_WRITE | PROT_EXEC);
2481 
2482 	/*
2483 	 * Make data writable until end.
2484 	 */
2485 	(void) as_setprot(&kas, s_data, e_moddata - s_data,
2486 	    PROT_READ | PROT_WRITE | PROT_EXEC);
2487 }
2488 
2489 #ifndef __xpv
2490 /*
2491  * Solaris adds an entry for Write Combining caching to the PAT
2492  */
2493 static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
2494 
2495 void
2496 pat_sync(void)
2497 {
2498 	ulong_t	cr0, cr0_orig, cr4;
2499 
2500 	if (!(x86_feature & X86_PAT))
2501 		return;
2502 	cr0_orig = cr0 = getcr0();
2503 	cr4 = getcr4();
2504 
2505 	/* disable caching and flush all caches and TLBs */
2506 	cr0 |= CR0_CD;
2507 	cr0 &= ~CR0_NW;
2508 	setcr0(cr0);
2509 	invalidate_cache();
2510 	if (cr4 & CR4_PGE) {
2511 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
2512 		setcr4(cr4);
2513 	} else {
2514 		reload_cr3();
2515 	}
2516 
2517 	/* add our entry to the PAT */
2518 	wrmsr(REG_PAT, pat_attr_reg);
2519 
2520 	/* flush TLBs and cache again, then reenable cr0 caching */
2521 	if (cr4 & CR4_PGE) {
2522 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
2523 		setcr4(cr4);
2524 	} else {
2525 		reload_cr3();
2526 	}
2527 	invalidate_cache();
2528 	setcr0(cr0_orig);
2529 }
2530 
2531 #endif /* !__xpv */
2532 
2533 #if defined(_SOFT_HOSTID)
2534 /*
2535  * On platforms that do not have a hardware serial number, attempt
2536  * to set one based on the contents of /etc/hostid.  If this file does
2537  * not exist, assume that we are to generate a new hostid and set
2538  * it in the kernel, for subsequent saving by a userland process
2539  * once the system is up and the root filesystem is mounted r/w.
2540  *
2541  * In order to gracefully support upgrade on OpenSolaris, if
2542  * /etc/hostid does not exist, we will attempt to get a serial number
2543  * using the legacy method (/kernel/misc/sysinit).
2544  *
2545  * In an attempt to make the hostid less prone to abuse
2546  * (for license circumvention, etc), we store it in /etc/hostid
2547  * in rot47 format.
2548  */
2549 extern volatile unsigned long tenmicrodata;
2550 static int atoi(char *);
2551 
2552 static int32_t
2553 set_soft_hostid(void)
2554 {
2555 	struct _buf *file;
2556 	char tokbuf[MAXNAMELEN];
2557 	token_t token;
2558 	int done = 0;
2559 	u_longlong_t tmp;
2560 	int i;
2561 	int32_t hostid = (int32_t)HW_INVALID_HOSTID;
2562 	unsigned char *c;
2563 	hrtime_t tsc;
2564 
2565 	/*
2566 	 * If /etc/hostid file not found, we'd like to get a pseudo
2567 	 * random number to use at the hostid.  A nice way to do this
2568 	 * is to read the real time clock.  To remain xen-compatible,
2569 	 * we can't poke the real hardware, so we use tsc_read() to
2570 	 * read the real time clock.  However, there is an ominous
2571 	 * warning in tsc_read that says it can return zero, so we
2572 	 * deal with that possibility by falling back to using the
2573 	 * (hopefully random enough) value in tenmicrodata.
2574 	 */
2575 
2576 	if ((file = kobj_open_file(hostid_file)) == (struct _buf *)-1) {
2577 		/*
2578 		 * hostid file not found - try to load sysinit module
2579 		 * and see if it has a nonzero hostid value...use that
2580 		 * instead of generating a new hostid here if so.
2581 		 */
2582 		if ((i = modload("misc", "sysinit")) != -1) {
2583 			if (strlen(hw_serial) > 0)
2584 				hostid = (int32_t)atoi(hw_serial);
2585 			(void) modunload(i);
2586 		}
2587 		if (hostid == HW_INVALID_HOSTID) {
2588 			tsc = tsc_read();
2589 			if (tsc == 0)	/* tsc_read can return zero sometimes */
2590 				hostid = (int32_t)tenmicrodata & 0x0CFFFFF;
2591 			else
2592 				hostid = (int32_t)tsc & 0x0CFFFFF;
2593 		}
2594 	} else {
2595 		/* hostid file found */
2596 		while (!done) {
2597 			token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2598 
2599 			switch (token) {
2600 			case POUND:
2601 				/*
2602 				 * skip comments
2603 				 */
2604 				kobj_find_eol(file);
2605 				break;
2606 			case STRING:
2607 				/*
2608 				 * un-rot47 - obviously this
2609 				 * nonsense is ascii-specific
2610 				 */
2611 				for (c = (unsigned char *)tokbuf;
2612 				    *c != '\0'; c++) {
2613 					*c += 47;
2614 					if (*c > '~')
2615 						*c -= 94;
2616 					else if (*c < '!')
2617 						*c += 94;
2618 				}
2619 				/*
2620 				 * now we should have a real number
2621 				 */
2622 
2623 				if (kobj_getvalue(tokbuf, &tmp) != 0)
2624 					kobj_file_err(CE_WARN, file,
2625 					    "Bad value %s for hostid",
2626 					    tokbuf);
2627 				else
2628 					hostid = (int32_t)tmp;
2629 
2630 				break;
2631 			case EOF:
2632 				done = 1;
2633 				/* FALLTHROUGH */
2634 			case NEWLINE:
2635 				kobj_newline(file);
2636 				break;
2637 			default:
2638 				break;
2639 
2640 			}
2641 		}
2642 		if (hostid == HW_INVALID_HOSTID) /* didn't find a hostid */
2643 			kobj_file_err(CE_WARN, file,
2644 			    "hostid missing or corrupt");
2645 
2646 		kobj_close_file(file);
2647 	}
2648 	/*
2649 	 * hostid is now the value read from /etc/hostid, or the
2650 	 * new hostid we generated in this routine or HW_INVALID_HOSTID if not
2651 	 * set.
2652 	 */
2653 	return (hostid);
2654 }
2655 
2656 static int
2657 atoi(char *p)
2658 {
2659 	int i = 0;
2660 
2661 	while (*p != '\0')
2662 		i = 10 * i + (*p++ - '0');
2663 
2664 	return (i);
2665 }
2666 
2667 #endif /* _SOFT_HOSTID */
2668 
2669 void
2670 get_system_configuration(void)
2671 {
2672 	char	prop[32];
2673 	u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
2674 
2675 	if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
2676 	    BOP_GETPROP(bootops, "nodes", prop) < 0 ||
2677 	    kobj_getvalue(prop, &nodes_ll) == -1 ||
2678 	    nodes_ll > MAXNODES ||
2679 	    BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
2680 	    BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
2681 	    kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
2682 		system_hardware.hd_nodes = 1;
2683 		system_hardware.hd_cpus_per_node = 0;
2684 	} else {
2685 		system_hardware.hd_nodes = (int)nodes_ll;
2686 		system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
2687 	}
2688 
2689 	if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
2690 	    BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
2691 	    kobj_getvalue(prop, &lvalue) == -1)
2692 		eprom_kernelbase = NULL;
2693 	else
2694 		eprom_kernelbase = (uintptr_t)lvalue;
2695 
2696 	if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
2697 	    BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
2698 	    kobj_getvalue(prop, &lvalue) == -1)
2699 		segmapsize = SEGMAPDEFAULT;
2700 	else
2701 		segmapsize = (uintptr_t)lvalue;
2702 
2703 	if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
2704 	    BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
2705 	    kobj_getvalue(prop, &lvalue) == -1)
2706 		segmapfreelists = 0;	/* use segmap driver default */
2707 	else
2708 		segmapfreelists = (int)lvalue;
2709 
2710 	/* physmem used to be here, but moved much earlier to fakebop.c */
2711 }
2712 
2713 /*
2714  * Add to a memory list.
2715  * start = start of new memory segment
2716  * len = length of new memory segment in bytes
2717  * new = pointer to a new struct memlist
2718  * memlistp = memory list to which to add segment.
2719  */
2720 void
2721 memlist_add(
2722 	uint64_t start,
2723 	uint64_t len,
2724 	struct memlist *new,
2725 	struct memlist **memlistp)
2726 {
2727 	struct memlist *cur;
2728 	uint64_t end = start + len;
2729 
2730 	new->address = start;
2731 	new->size = len;
2732 
2733 	cur = *memlistp;
2734 
2735 	while (cur) {
2736 		if (cur->address >= end) {
2737 			new->next = cur;
2738 			*memlistp = new;
2739 			new->prev = cur->prev;
2740 			cur->prev = new;
2741 			return;
2742 		}
2743 		ASSERT(cur->address + cur->size <= start);
2744 		if (cur->next == NULL) {
2745 			cur->next = new;
2746 			new->prev = cur;
2747 			new->next = NULL;
2748 			return;
2749 		}
2750 		memlistp = &cur->next;
2751 		cur = cur->next;
2752 	}
2753 }
2754 
2755 void
2756 kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
2757 {
2758 	size_t tsize = e_modtext - modtext;
2759 	size_t dsize = e_moddata - moddata;
2760 
2761 	*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
2762 	    1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
2763 	*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
2764 	    1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
2765 }
2766 
2767 caddr_t
2768 kobj_text_alloc(vmem_t *arena, size_t size)
2769 {
2770 	return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
2771 }
2772 
2773 /*ARGSUSED*/
2774 caddr_t
2775 kobj_texthole_alloc(caddr_t addr, size_t size)
2776 {
2777 	panic("unexpected call to kobj_texthole_alloc()");
2778 	/*NOTREACHED*/
2779 	return (0);
2780 }
2781 
2782 /*ARGSUSED*/
2783 void
2784 kobj_texthole_free(caddr_t addr, size_t size)
2785 {
2786 	panic("unexpected call to kobj_texthole_free()");
2787 }
2788 
2789 /*
2790  * This is called just after configure() in startup().
2791  *
2792  * The ISALIST concept is a bit hopeless on Intel, because
2793  * there's no guarantee of an ever-more-capable processor
2794  * given that various parts of the instruction set may appear
2795  * and disappear between different implementations.
2796  *
2797  * While it would be possible to correct it and even enhance
2798  * it somewhat, the explicit hardware capability bitmask allows
2799  * more flexibility.
2800  *
2801  * So, we just leave this alone.
2802  */
2803 void
2804 setx86isalist(void)
2805 {
2806 	char *tp;
2807 	size_t len;
2808 	extern char *isa_list;
2809 
2810 #define	TBUFSIZE	1024
2811 
2812 	tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
2813 	*tp = '\0';
2814 
2815 #if defined(__amd64)
2816 	(void) strcpy(tp, "amd64 ");
2817 #endif
2818 
2819 	switch (x86_vendor) {
2820 	case X86_VENDOR_Intel:
2821 	case X86_VENDOR_AMD:
2822 	case X86_VENDOR_TM:
2823 		if (x86_feature & X86_CMOV) {
2824 			/*
2825 			 * Pentium Pro or later
2826 			 */
2827 			(void) strcat(tp, "pentium_pro");
2828 			(void) strcat(tp, x86_feature & X86_MMX ?
2829 			    "+mmx pentium_pro " : " ");
2830 		}
2831 		/*FALLTHROUGH*/
2832 	case X86_VENDOR_Cyrix:
2833 		/*
2834 		 * The Cyrix 6x86 does not have any Pentium features
2835 		 * accessible while not at privilege level 0.
2836 		 */
2837 		if (x86_feature & X86_CPUID) {
2838 			(void) strcat(tp, "pentium");
2839 			(void) strcat(tp, x86_feature & X86_MMX ?
2840 			    "+mmx pentium " : " ");
2841 		}
2842 		break;
2843 	default:
2844 		break;
2845 	}
2846 	(void) strcat(tp, "i486 i386 i86");
2847 	len = strlen(tp) + 1;   /* account for NULL at end of string */
2848 	isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
2849 	kmem_free(tp, TBUFSIZE);
2850 
2851 #undef TBUFSIZE
2852 }
2853 
2854 
2855 #ifdef __amd64
2856 
2857 void *
2858 device_arena_alloc(size_t size, int vm_flag)
2859 {
2860 	return (vmem_alloc(device_arena, size, vm_flag));
2861 }
2862 
2863 void
2864 device_arena_free(void *vaddr, size_t size)
2865 {
2866 	vmem_free(device_arena, vaddr, size);
2867 }
2868 
2869 #else /* __i386 */
2870 
2871 void *
2872 device_arena_alloc(size_t size, int vm_flag)
2873 {
2874 	caddr_t	vaddr;
2875 	uintptr_t v;
2876 	size_t	start;
2877 	size_t	end;
2878 
2879 	vaddr = vmem_alloc(heap_arena, size, vm_flag);
2880 	if (vaddr == NULL)
2881 		return (NULL);
2882 
2883 	v = (uintptr_t)vaddr;
2884 	ASSERT(v >= kernelbase);
2885 	ASSERT(v + size <= valloc_base);
2886 
2887 	start = btop(v - kernelbase);
2888 	end = btop(v + size - 1 - kernelbase);
2889 	ASSERT(start < toxic_bit_map_len);
2890 	ASSERT(end < toxic_bit_map_len);
2891 
2892 	while (start <= end) {
2893 		BT_ATOMIC_SET(toxic_bit_map, start);
2894 		++start;
2895 	}
2896 	return (vaddr);
2897 }
2898 
2899 void
2900 device_arena_free(void *vaddr, size_t size)
2901 {
2902 	uintptr_t v = (uintptr_t)vaddr;
2903 	size_t	start;
2904 	size_t	end;
2905 
2906 	ASSERT(v >= kernelbase);
2907 	ASSERT(v + size <= valloc_base);
2908 
2909 	start = btop(v - kernelbase);
2910 	end = btop(v + size - 1 - kernelbase);
2911 	ASSERT(start < toxic_bit_map_len);
2912 	ASSERT(end < toxic_bit_map_len);
2913 
2914 	while (start <= end) {
2915 		ASSERT(BT_TEST(toxic_bit_map, start) != 0);
2916 		BT_ATOMIC_CLEAR(toxic_bit_map, start);
2917 		++start;
2918 	}
2919 	vmem_free(heap_arena, vaddr, size);
2920 }
2921 
2922 /*
2923  * returns 1st address in range that is in device arena, or NULL
2924  * if len is not NULL it returns the length of the toxic range
2925  */
2926 void *
2927 device_arena_contains(void *vaddr, size_t size, size_t *len)
2928 {
2929 	uintptr_t v = (uintptr_t)vaddr;
2930 	uintptr_t eaddr = v + size;
2931 	size_t start;
2932 	size_t end;
2933 
2934 	/*
2935 	 * if called very early by kmdb, just return NULL
2936 	 */
2937 	if (toxic_bit_map == NULL)
2938 		return (NULL);
2939 
2940 	/*
2941 	 * First check if we're completely outside the bitmap range.
2942 	 */
2943 	if (v >= valloc_base || eaddr < kernelbase)
2944 		return (NULL);
2945 
2946 	/*
2947 	 * Trim ends of search to look at only what the bitmap covers.
2948 	 */
2949 	if (v < kernelbase)
2950 		v = kernelbase;
2951 	start = btop(v - kernelbase);
2952 	end = btop(eaddr - kernelbase);
2953 	if (end >= toxic_bit_map_len)
2954 		end = toxic_bit_map_len;
2955 
2956 	if (bt_range(toxic_bit_map, &start, &end, end) == 0)
2957 		return (NULL);
2958 
2959 	v = kernelbase + ptob(start);
2960 	if (len != NULL)
2961 		*len = ptob(end - start);
2962 	return ((void *)v);
2963 }
2964 
2965 #endif	/* __i386 */
2966