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