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