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