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