xref: /titanic_52/usr/src/uts/sun4/os/startup.c (revision 6b990117eca1cdf7ad1f4424209791ad38be9791)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/machsystm.h>
30 #include <sys/archsystm.h>
31 #include <sys/vm.h>
32 #include <sys/cpu.h>
33 #include <sys/atomic.h>
34 #include <sys/reboot.h>
35 #include <sys/kdi.h>
36 #include <sys/bootconf.h>
37 #include <sys/memlist_plat.h>
38 #include <sys/memlist_impl.h>
39 #include <sys/prom_plat.h>
40 #include <sys/prom_isa.h>
41 #include <sys/autoconf.h>
42 #include <sys/intreg.h>
43 #include <sys/ivintr.h>
44 #include <sys/fpu/fpusystm.h>
45 #include <sys/iommutsb.h>
46 #include <vm/vm_dep.h>
47 #include <vm/seg_dev.h>
48 #include <vm/seg_kmem.h>
49 #include <vm/seg_kpm.h>
50 #include <vm/seg_map.h>
51 #include <vm/seg_kp.h>
52 #include <sys/sysconf.h>
53 #include <vm/hat_sfmmu.h>
54 #include <sys/kobj.h>
55 #include <sys/sun4asi.h>
56 #include <sys/clconf.h>
57 #include <sys/platform_module.h>
58 #include <sys/panic.h>
59 #include <sys/cpu_sgnblk_defs.h>
60 #include <sys/clock.h>
61 #include <sys/cmn_err.h>
62 #include <sys/promif.h>
63 #include <sys/prom_debug.h>
64 #include <sys/traptrace.h>
65 #include <sys/memnode.h>
66 #include <sys/mem_cage.h>
67 #include <sys/mmu.h>
68 
69 extern void setup_trap_table(void);
70 extern int cpu_intrq_setup(struct cpu *);
71 extern void cpu_intrq_register(struct cpu *);
72 extern void contig_mem_init(void);
73 extern caddr_t contig_mem_prealloc(caddr_t, pgcnt_t);
74 extern void mach_dump_buffer_init(void);
75 extern void mach_descrip_init(void);
76 extern void mach_descrip_startup_fini(void);
77 extern void mach_memscrub(void);
78 extern void mach_fpras(void);
79 extern void mach_cpu_halt_idle(void);
80 extern void mach_hw_copy_limit(void);
81 extern void load_mach_drivers(void);
82 extern void load_tod_module(void);
83 #pragma weak load_tod_module
84 
85 extern int ndata_alloc_mmfsa(struct memlist *ndata);
86 #pragma weak ndata_alloc_mmfsa
87 
88 extern void cif_init(void);
89 #pragma weak cif_init
90 
91 extern void parse_idprom(void);
92 extern void add_vx_handler(char *, int, void (*)(cell_t *));
93 extern void mem_config_init(void);
94 extern void memseg_remap_init(void);
95 
96 extern void mach_kpm_init(void);
97 
98 /*
99  * External Data:
100  */
101 extern int vac_size;	/* cache size in bytes */
102 extern uint_t vac_mask;	/* VAC alignment consistency mask */
103 extern uint_t vac_colors;
104 
105 /*
106  * Global Data Definitions:
107  */
108 
109 /*
110  * XXX - Don't port this to new architectures
111  * A 3rd party volume manager driver (vxdm) depends on the symbol romp.
112  * 'romp' has no use with a prom with an IEEE 1275 client interface.
113  * The driver doesn't use the value, but it depends on the symbol.
114  */
115 void *romp;		/* veritas driver won't load without romp 4154976 */
116 /*
117  * Declare these as initialized data so we can patch them.
118  */
119 pgcnt_t physmem = 0;	/* memory size in pages, patch if you want less */
120 pgcnt_t segkpsize =
121     btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
122 uint_t segmap_percent = 12; /* Size of segmap segment */
123 
124 int use_cache = 1;		/* cache not reliable (605 bugs) with MP */
125 int vac_copyback = 1;
126 char *cache_mode = NULL;
127 int use_mix = 1;
128 int prom_debug = 0;
129 
130 struct bootops *bootops = 0;	/* passed in from boot in %o2 */
131 caddr_t boot_tba;		/* %tba at boot - used by kmdb */
132 uint_t	tba_taken_over = 0;
133 
134 caddr_t s_text;			/* start of kernel text segment */
135 caddr_t e_text;			/* end of kernel text segment */
136 caddr_t s_data;			/* start of kernel data segment */
137 caddr_t e_data;			/* end of kernel data segment */
138 
139 caddr_t modtext;		/* beginning of module text */
140 size_t	modtext_sz;		/* size of module text */
141 caddr_t moddata;		/* beginning of module data reserve */
142 caddr_t e_moddata;		/* end of module data reserve */
143 
144 /*
145  * End of first block of contiguous kernel in 32-bit virtual address space
146  */
147 caddr_t		econtig32;	/* end of first blk of contiguous kernel */
148 
149 caddr_t		ncbase;		/* beginning of non-cached segment */
150 caddr_t		ncend;		/* end of non-cached segment */
151 caddr_t		sdata;		/* beginning of data segment */
152 
153 caddr_t		extra_etva;	/* beginning of unused nucleus text */
154 pgcnt_t		extra_etpg;	/* number of pages of unused nucleus text */
155 
156 size_t	ndata_remain_sz;	/* bytes from end of data to 4MB boundary */
157 caddr_t	nalloc_base;		/* beginning of nucleus allocation */
158 caddr_t nalloc_end;		/* end of nucleus allocatable memory */
159 caddr_t valloc_base;		/* beginning of kvalloc segment	*/
160 
161 caddr_t kmem64_base;		/* base of kernel mem segment in 64-bit space */
162 caddr_t kmem64_end;		/* end of kernel mem segment in 64-bit space */
163 caddr_t kmem64_aligned_end;	/* end of large page, overmaps 64-bit space */
164 int	kmem64_alignsize;	/* page size for mem segment in 64-bit space */
165 int	kmem64_szc;		/* page size code */
166 uint64_t kmem64_pabase = (uint64_t)-1;	/* physical address of kmem64_base */
167 
168 uintptr_t shm_alignment;	/* VAC address consistency modulus */
169 struct memlist *phys_install;	/* Total installed physical memory */
170 struct memlist *phys_avail;	/* Available (unreserved) physical memory */
171 struct memlist *virt_avail;	/* Available (unmapped?) virtual memory */
172 struct memlist ndata;		/* memlist of nucleus allocatable memory */
173 int memexp_flag;		/* memory expansion card flag */
174 uint64_t ecache_flushaddr;	/* physical address used for flushing E$ */
175 pgcnt_t obp_pages;		/* Physical pages used by OBP */
176 
177 /*
178  * VM data structures
179  */
180 long page_hashsz;		/* Size of page hash table (power of two) */
181 struct page *pp_base;		/* Base of system page struct array */
182 size_t pp_sz;			/* Size in bytes of page struct array */
183 struct page **page_hash;	/* Page hash table */
184 struct seg ktextseg;		/* Segment used for kernel executable image */
185 struct seg kvalloc;		/* Segment used for "valloc" mapping */
186 struct seg kpseg;		/* Segment used for pageable kernel virt mem */
187 struct seg ktexthole;		/* Segment used for nucleus text hole */
188 struct seg kmapseg;		/* Segment used for generic kernel mappings */
189 struct seg kpmseg;		/* Segment used for physical mapping */
190 struct seg kdebugseg;		/* Segment used for the kernel debugger */
191 
192 uintptr_t kpm_pp_base;		/* Base of system kpm_page array */
193 size_t	kpm_pp_sz;		/* Size of system kpm_page array */
194 pgcnt_t	kpm_npages;		/* How many kpm pages are managed */
195 
196 struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
197 struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
198 struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
199 
200 int segzio_fromheap = 0;	/* zio allocations occur from heap */
201 caddr_t segzio_base;		/* Base address of segzio */
202 pgcnt_t segziosize = 0;		/* size of zio segment in pages */
203 
204 /*
205  * debugger pages (if allocated)
206  */
207 struct vnode kdebugvp;
208 
209 /*
210  * VA range available to the debugger
211  */
212 const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
213 const size_t kdi_segdebugsize = SEGDEBUGSIZE;
214 
215 /*
216  * Segment for relocated kernel structures in 64-bit large RAM kernels
217  */
218 struct seg kmem64;
219 
220 struct memseg *memseg_base;
221 size_t memseg_sz;		/* Used to translate a va to page */
222 struct vnode unused_pages_vp;
223 
224 /*
225  * VM data structures allocated early during boot.
226  */
227 size_t pagehash_sz;
228 uint64_t memlist_sz;
229 
230 char tbr_wr_addr_inited = 0;
231 
232 
233 /*
234  * Static Routines:
235  */
236 static void memlist_add(uint64_t, uint64_t, struct memlist **,
237 	struct memlist **);
238 static void kphysm_init(page_t *, struct memseg *, pgcnt_t, uintptr_t,
239 	pgcnt_t);
240 static void kvm_init(void);
241 
242 static void startup_init(void);
243 static void startup_memlist(void);
244 static void startup_modules(void);
245 static void startup_bop_gone(void);
246 static void startup_vm(void);
247 static void startup_end(void);
248 static void setup_cage_params(void);
249 static void startup_create_io_node(void);
250 
251 static pgcnt_t npages;
252 static struct memlist *memlist;
253 void *memlist_end;
254 
255 static pgcnt_t bop_alloc_pages;
256 static caddr_t hblk_base;
257 uint_t hblk_alloc_dynamic = 0;
258 uint_t hblk1_min = H1MIN;
259 
260 
261 /*
262  * Hooks for unsupported platforms and down-rev firmware
263  */
264 int iam_positron(void);
265 #pragma weak iam_positron
266 static void do_prom_version_check(void);
267 static void kpm_init(void);
268 static void kpm_npages_setup(int);
269 static void kpm_memseg_init(void);
270 
271 /*
272  * After receiving a thermal interrupt, this is the number of seconds
273  * to delay before shutting off the system, assuming
274  * shutdown fails.  Use /etc/system to change the delay if this isn't
275  * large enough.
276  */
277 int thermal_powerdown_delay = 1200;
278 
279 /*
280  * Used to hold off page relocations into the cage until OBP has completed
281  * its boot-time handoff of its resources to the kernel.
282  */
283 int page_relocate_ready = 0;
284 
285 /*
286  * Enable some debugging messages concerning memory usage...
287  */
288 #ifdef  DEBUGGING_MEM
289 static int debugging_mem;
290 static void
291 printmemlist(char *title, struct memlist *list)
292 {
293 	if (!debugging_mem)
294 		return;
295 
296 	printf("%s\n", title);
297 
298 	while (list) {
299 		prom_printf("\taddr = 0x%x %8x, size = 0x%x %8x\n",
300 		    (uint32_t)(list->address >> 32), (uint32_t)list->address,
301 		    (uint32_t)(list->size >> 32), (uint32_t)(list->size));
302 		list = list->next;
303 	}
304 }
305 
306 void
307 printmemseg(struct memseg *memseg)
308 {
309 	if (!debugging_mem)
310 		return;
311 
312 	printf("memseg\n");
313 
314 	while (memseg) {
315 		prom_printf("\tpage = 0x%p, epage = 0x%p, "
316 		    "pfn = 0x%x, epfn = 0x%x\n",
317 		    memseg->pages, memseg->epages,
318 		    memseg->pages_base, memseg->pages_end);
319 		memseg = memseg->next;
320 	}
321 }
322 
323 #define	debug_pause(str)	halt((str))
324 #define	MPRINTF(str)		if (debugging_mem) prom_printf((str))
325 #define	MPRINTF1(str, a)	if (debugging_mem) prom_printf((str), (a))
326 #define	MPRINTF2(str, a, b)	if (debugging_mem) prom_printf((str), (a), (b))
327 #define	MPRINTF3(str, a, b, c) \
328 	if (debugging_mem) prom_printf((str), (a), (b), (c))
329 #else	/* DEBUGGING_MEM */
330 #define	MPRINTF(str)
331 #define	MPRINTF1(str, a)
332 #define	MPRINTF2(str, a, b)
333 #define	MPRINTF3(str, a, b, c)
334 #endif	/* DEBUGGING_MEM */
335 
336 /* Simple message to indicate that the bootops pointer has been zeroed */
337 #ifdef DEBUG
338 static int bootops_gone_on = 0;
339 #define	BOOTOPS_GONE() \
340 	if (bootops_gone_on) \
341 		prom_printf("The bootops vec is zeroed now!\n");
342 #else
343 #define	BOOTOPS_GONE()
344 #endif /* DEBUG */
345 
346 /*
347  * Monitor pages may not be where this says they are.
348  * and the debugger may not be there either.
349  *
350  * Note that 'pages' here are *physical* pages, which are 8k on sun4u.
351  *
352  *                        Physical memory layout
353  *                     (not necessarily contiguous)
354  *                       (THIS IS SOMEWHAT WRONG)
355  *                       /-----------------------\
356  *                       |       monitor pages   |
357  *             availmem -|-----------------------|
358  *                       |                       |
359  *                       |       page pool       |
360  *                       |                       |
361  *                       |-----------------------|
362  *                       |   configured tables   |
363  *                       |       buffers         |
364  *            firstaddr -|-----------------------|
365  *                       |   hat data structures |
366  *                       |-----------------------|
367  *                       |    kernel data, bss   |
368  *                       |-----------------------|
369  *                       |    interrupt stack    |
370  *                       |-----------------------|
371  *                       |    kernel text (RO)   |
372  *                       |-----------------------|
373  *                       |    trap table (4k)    |
374  *                       |-----------------------|
375  *               page 1  |      panicbuf         |
376  *                       |-----------------------|
377  *               page 0  |       reclaimed       |
378  *                       |_______________________|
379  *
380  *
381  *
382  *                    Kernel's Virtual Memory Layout.
383  *                       /-----------------------\
384  * 0xFFFFFFFF.FFFFFFFF  -|                       |-
385  *                       |   OBP's virtual page  |
386  *                       |        tables         |
387  * 0xFFFFFFFC.00000000  -|-----------------------|-
388  *                       :                       :
389  *                       :                       :
390  *                      -|-----------------------|-
391  *                       |       segzio          | (base and size vary)
392  * 0xFFFFFE00.00000000  -|-----------------------|-
393  *                       |                       |  Ultrasparc I/II support
394  *                       |    segkpm segment     |  up to 2TB of physical
395  *                       | (64-bit kernel ONLY)  |  memory, VAC has 2 colors
396  *                       |                       |
397  * 0xFFFFFA00.00000000  -|-----------------------|- 2TB segkpm alignment
398  *                       :                       :
399  *                       :                       :
400  * 0xFFFFF810.00000000  -|-----------------------|- hole_end
401  *                       |                       |      ^
402  *                       |  UltraSPARC I/II call |      |
403  *                       | bug requires an extra |      |
404  *                       | 4 GB of space between |      |
405  *                       |   hole and used RAM   |	|
406  *                       |                       |      |
407  * 0xFFFFF800.00000000  -|-----------------------|-     |
408  *                       |                       |      |
409  *                       | Virtual Address Hole  |   UltraSPARC
410  *                       |  on UltraSPARC I/II   |  I/II * ONLY *
411  *                       |                       |      |
412  * 0x00000800.00000000  -|-----------------------|-     |
413  *                       |                       |      |
414  *                       |  UltraSPARC I/II call |      |
415  *                       | bug requires an extra |      |
416  *                       | 4 GB of space between |      |
417  *                       |   hole and used RAM   |      |
418  *                       |                       |      v
419  * 0x000007FF.00000000  -|-----------------------|- hole_start -----
420  *                       :                       :		   ^
421  *                       :                       :		   |
422  * 0x00000XXX.XXX00000  -|-----------------------|- kmem64_	   |
423  *                       | overmapped area       |   alignend_end  |
424  *                       | (kmem64_alignsize     |		   |
425  *                       |  boundary)            |		   |
426  * 0x00000XXX.XXXXXXXX  -|-----------------------|- kmem64_end	   |
427  *                       |                       |		   |
428  *                       |   64-bit kernel ONLY  |		   |
429  *                       |                       |		   |
430  *                       |    kmem64 segment     |		   |
431  *                       |                       |		   |
432  *                       | (Relocated extra HME  |	     Approximately
433  *                       |   block allocations,  |	    1 TB of virtual
434  *                       |   memnode freelists,  |	     address space
435  *                       |    HME hash buckets,  |		   |
436  *                       | mml_table, kpmp_table,|		   |
437  *                       |  page_t array and     |		   |
438  *                       |  hashblock pool to    |		   |
439  *                       |   avoid hard-coded    |		   |
440  *                       |     32-bit vaddr      |		   |
441  *                       |     limitations)      |		   |
442  *                       |                       |		   v
443  * 0x00000700.00000000  -|-----------------------|- SYSLIMIT (kmem64_base)
444  *                       |                       |
445  *                       |  segkmem segment      | (SYSLIMIT - SYSBASE = 4TB)
446  *                       |                       |
447  * 0x00000300.00000000  -|-----------------------|- SYSBASE
448  *                       :                       :
449  *                       :                       :
450  *                      -|-----------------------|-
451  *                       |                       |
452  *                       |  segmap segment       |   SEGMAPSIZE (1/8th physmem,
453  *                       |                       |               256G MAX)
454  * 0x000002a7.50000000  -|-----------------------|- SEGMAPBASE
455  *                       :                       :
456  *                       :                       :
457  *                      -|-----------------------|-
458  *                       |                       |
459  *                       |       segkp           |    SEGKPSIZE (2GB)
460  *                       |                       |
461  *                       |                       |
462  * 0x000002a1.00000000  -|-----------------------|- SEGKPBASE
463  *                       |                       |
464  * 0x000002a0.00000000  -|-----------------------|- MEMSCRUBBASE
465  *                       |                       |       (SEGKPBASE - 0x400000)
466  * 0x0000029F.FFE00000  -|-----------------------|- ARGSBASE
467  *                       |                       |       (MEMSCRUBBASE - NCARGS)
468  * 0x0000029F.FFD80000  -|-----------------------|- PPMAPBASE
469  *                       |                       |       (ARGSBASE - PPMAPSIZE)
470  * 0x0000029F.FFD00000  -|-----------------------|- PPMAP_FAST_BASE
471  *                       |                       |
472  * 0x0000029F.FF980000  -|-----------------------|- PIOMAPBASE
473  *                       |                       |
474  * 0x0000029F.FF580000  -|-----------------------|- NARG_BASE
475  *                       :                       :
476  *                       :                       :
477  * 0x00000000.FFFFFFFF  -|-----------------------|- OFW_END_ADDR
478  *                       |                       |
479  *                       |         OBP           |
480  *                       |                       |
481  * 0x00000000.F0000000  -|-----------------------|- OFW_START_ADDR
482  *                       |         kmdb          |
483  * 0x00000000.EDD00000  -|-----------------------|- SEGDEBUGBASE
484  *                       :                       :
485  *                       :                       :
486  * 0x00000000.7c000000  -|-----------------------|- SYSLIMIT32
487  *                       |                       |
488  *                       |  segkmem32 segment    | (SYSLIMIT32 - SYSBASE32 =
489  *                       |                       |    ~64MB)
490  * 0x00000000.78002000  -|-----------------------|
491  *                       |     panicbuf          |
492  * 0x00000000.78000000  -|-----------------------|- SYSBASE32
493  *                       :                       :
494  *                       :                       :
495  *                       |                       |
496  *                       |-----------------------|- econtig32
497  *                       |    vm structures      |
498  * 0x00000000.01C00000   |-----------------------|- nalloc_end
499  *                       |         TSBs          |
500  *                       |-----------------------|- end/nalloc_base
501  *                       |   kernel data & bss   |
502  * 0x00000000.01800000  -|-----------------------|
503  *                       :   nucleus text hole   :
504  * 0x00000000.01400000  -|-----------------------|
505  *                       :                       :
506  *                       |-----------------------|
507  *                       |      module text      |
508  *                       |-----------------------|- e_text/modtext
509  *                       |      kernel text      |
510  *                       |-----------------------|
511  *                       |    trap table (48k)   |
512  * 0x00000000.01000000  -|-----------------------|- KERNELBASE
513  *                       | reserved for trapstat |} TSTAT_TOTAL_SIZE
514  *                       |-----------------------|
515  *                       |                       |
516  *                       |        invalid        |
517  *                       |                       |
518  * 0x00000000.00000000  _|_______________________|
519  *
520  *
521  *
522  *                   32-bit User Virtual Memory Layout.
523  *                       /-----------------------\
524  *                       |                       |
525  *                       |        invalid        |
526  *                       |                       |
527  *          0xFFC00000  -|-----------------------|- USERLIMIT
528  *                       |       user stack      |
529  *                       :                       :
530  *                       :                       :
531  *                       :                       :
532  *                       |       user data       |
533  *                      -|-----------------------|-
534  *                       |       user text       |
535  *          0x00002000  -|-----------------------|-
536  *                       |       invalid         |
537  *          0x00000000  _|_______________________|
538  *
539  *
540  *
541  *                   64-bit User Virtual Memory Layout.
542  *                       /-----------------------\
543  *                       |                       |
544  *                       |        invalid        |
545  *                       |                       |
546  *  0xFFFFFFFF.80000000 -|-----------------------|- USERLIMIT
547  *                       |       user stack      |
548  *                       :                       :
549  *                       :                       :
550  *                       :                       :
551  *                       |       user data       |
552  *                      -|-----------------------|-
553  *                       |       user text       |
554  *  0x00000000.00100000 -|-----------------------|-
555  *                       |       invalid         |
556  *  0x00000000.00000000 _|_______________________|
557  */
558 
559 extern caddr_t ecache_init_scrub_flush_area(caddr_t alloc_base);
560 extern uint64_t ecache_flush_address(void);
561 
562 #pragma weak load_platform_modules
563 #pragma weak plat_startup_memlist
564 #pragma weak ecache_init_scrub_flush_area
565 #pragma weak ecache_flush_address
566 
567 
568 /*
569  * By default the DR Cage is enabled for maximum OS
570  * MPSS performance.  Users needing to disable the cage mechanism
571  * can set this variable to zero via /etc/system.
572  * Disabling the cage on systems supporting Dynamic Reconfiguration (DR)
573  * will result in loss of DR functionality.
574  * Platforms wishing to disable kernel Cage by default
575  * should do so in their set_platform_defaults() routine.
576  */
577 int	kernel_cage_enable = 1;
578 
579 static void
580 setup_cage_params(void)
581 {
582 	void (*func)(void);
583 
584 	func = (void (*)(void))kobj_getsymvalue("set_platform_cage_params", 0);
585 	if (func != NULL) {
586 		(*func)();
587 		return;
588 	}
589 
590 	if (kernel_cage_enable == 0) {
591 		return;
592 	}
593 	kcage_range_lock();
594 	if (kcage_range_init(phys_avail, 1) == 0) {
595 		kcage_init(total_pages / 256);
596 	}
597 	kcage_range_unlock();
598 
599 	if (kcage_on) {
600 		cmn_err(CE_NOTE, "!Kernel Cage is ENABLED");
601 	} else {
602 		cmn_err(CE_NOTE, "!Kernel Cage is DISABLED");
603 	}
604 
605 }
606 
607 /*
608  * Machine-dependent startup code
609  */
610 void
611 startup(void)
612 {
613 	startup_init();
614 	if (&startup_platform)
615 		startup_platform();
616 	startup_memlist();
617 	startup_modules();
618 	setup_cage_params();
619 	startup_bop_gone();
620 	startup_vm();
621 	startup_end();
622 }
623 
624 struct regs sync_reg_buf;
625 uint64_t sync_tt;
626 
627 void
628 sync_handler(void)
629 {
630 	struct  trap_info 	ti;
631 	int i;
632 
633 	/*
634 	 * Prevent trying to talk to the other CPUs since they are
635 	 * sitting in the prom and won't reply.
636 	 */
637 	for (i = 0; i < NCPU; i++) {
638 		if ((i != CPU->cpu_id) && CPU_XCALL_READY(i)) {
639 			cpu[i]->cpu_flags &= ~CPU_READY;
640 			cpu[i]->cpu_flags |= CPU_QUIESCED;
641 			CPUSET_DEL(cpu_ready_set, cpu[i]->cpu_id);
642 		}
643 	}
644 
645 	/*
646 	 * We've managed to get here without going through the
647 	 * normal panic code path. Try and save some useful
648 	 * information.
649 	 */
650 	if (!panicstr && (curthread->t_panic_trap == NULL)) {
651 		ti.trap_type = sync_tt;
652 		ti.trap_regs = &sync_reg_buf;
653 		ti.trap_addr = NULL;
654 		ti.trap_mmu_fsr = 0x0;
655 
656 		curthread->t_panic_trap = &ti;
657 	}
658 
659 	/*
660 	 * If we're re-entering the panic path, update the signature
661 	 * block so that the SC knows we're in the second part of panic.
662 	 */
663 	if (panicstr)
664 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DUMP, -1);
665 
666 	nopanicdebug = 1; /* do not perform debug_enter() prior to dump */
667 	panic("sync initiated");
668 }
669 
670 
671 static void
672 startup_init(void)
673 {
674 	/*
675 	 * We want to save the registers while we're still in OBP
676 	 * so that we know they haven't been fiddled with since.
677 	 * (In principle, OBP can't change them just because it
678 	 * makes a callback, but we'd rather not depend on that
679 	 * behavior.)
680 	 */
681 	char		sync_str[] =
682 		"warning @ warning off : sync "
683 		"%%tl-c %%tstate h# %p x! "
684 		"%%g1 h# %p x! %%g2 h# %p x! %%g3 h# %p x! "
685 		"%%g4 h# %p x! %%g5 h# %p x! %%g6 h# %p x! "
686 		"%%g7 h# %p x! %%o0 h# %p x! %%o1 h# %p x! "
687 		"%%o2 h# %p x! %%o3 h# %p x! %%o4 h# %p x! "
688 		"%%o5 h# %p x! %%o6 h# %p x! %%o7 h# %p x! "
689 		"%%tl-c %%tpc h# %p x! %%tl-c %%tnpc h# %p x! "
690 		"%%y h# %p l! %%tl-c %%tt h# %p x! "
691 		"sync ; warning !";
692 
693 	/*
694 	 * 20 == num of %p substrings
695 	 * 16 == max num of chars %p will expand to.
696 	 */
697 	char 		bp[sizeof (sync_str) + 16 * 20];
698 
699 	(void) check_boot_version(BOP_GETVERSION(bootops));
700 
701 	/*
702 	 * Initialize ptl1 stack for the 1st CPU.
703 	 */
704 	ptl1_init_cpu(&cpu0);
705 
706 	/*
707 	 * Initialize the address map for cache consistent mappings
708 	 * to random pages; must be done after vac_size is set.
709 	 */
710 	ppmapinit();
711 
712 	/*
713 	 * Initialize the PROM callback handler.
714 	 */
715 	init_vx_handler();
716 
717 	/*
718 	 * have prom call sync_callback() to handle the sync and
719 	 * save some useful information which will be stored in the
720 	 * core file later.
721 	 */
722 	(void) sprintf((char *)bp, sync_str,
723 		(void *)&sync_reg_buf.r_tstate, (void *)&sync_reg_buf.r_g1,
724 		(void *)&sync_reg_buf.r_g2, (void *)&sync_reg_buf.r_g3,
725 		(void *)&sync_reg_buf.r_g4, (void *)&sync_reg_buf.r_g5,
726 		(void *)&sync_reg_buf.r_g6, (void *)&sync_reg_buf.r_g7,
727 		(void *)&sync_reg_buf.r_o0, (void *)&sync_reg_buf.r_o1,
728 		(void *)&sync_reg_buf.r_o2, (void *)&sync_reg_buf.r_o3,
729 		(void *)&sync_reg_buf.r_o4, (void *)&sync_reg_buf.r_o5,
730 		(void *)&sync_reg_buf.r_o6, (void *)&sync_reg_buf.r_o7,
731 		(void *)&sync_reg_buf.r_pc, (void *)&sync_reg_buf.r_npc,
732 		(void *)&sync_reg_buf.r_y, (void *)&sync_tt);
733 	prom_interpret(bp, 0, 0, 0, 0, 0);
734 	add_vx_handler("sync", 1, (void (*)(cell_t *))sync_handler);
735 }
736 
737 static u_longlong_t *boot_physinstalled, *boot_physavail, *boot_virtavail;
738 static size_t boot_physinstalled_len, boot_physavail_len, boot_virtavail_len;
739 
740 #define	IVSIZE	((MAXIVNUM * sizeof (intr_vec_t *)) + \
741 		(MAX_RSVD_IV * sizeof (intr_vec_t)) + \
742 		(MAX_RSVD_IVX * sizeof (intr_vecx_t)))
743 
744 #if !defined(C_OBP)
745 /*
746  * Install a temporary tte handler in OBP for kmem64 area.
747  *
748  * We map kmem64 area with large pages before the trap table is taken
749  * over. Since OBP makes 8K mappings, it can create 8K tlb entries in
750  * the same area. Duplicate tlb entries with different page sizes
751  * cause unpredicatble behavior.  To avoid this, we don't create
752  * kmem64 mappings via BOP_ALLOC (ends up as prom_alloc() call to
753  * OBP).  Instead, we manage translations with a temporary va>tte-data
754  * handler (kmem64-tte).  This handler is replaced by unix-tte when
755  * the trap table is taken over.
756  *
757  * The temporary handler knows the physical address of the kmem64
758  * area. It uses the prom's pgmap@ Forth word for other addresses.
759  *
760  * We have to use BOP_ALLOC() method for C-OBP platforms because
761  * pgmap@ is not defined in C-OBP. C-OBP is only used on serengeti
762  * sun4u platforms. On sun4u we flush tlb after trap table is taken
763  * over if we use large pages for kernel heap and kmem64. Since sun4u
764  * prom (unlike sun4v) calls va>tte-data first for client address
765  * translation prom's ttes for kmem64 can't get into TLB even if we
766  * later switch to prom's trap table again. C-OBP uses 4M pages for
767  * client mappings when possible so on all platforms we get the
768  * benefit from large mappings for kmem64 area immediately during
769  * boot.
770  *
771  * pseudo code:
772  * if (context != 0) {
773  * 	return false
774  * } else if (miss_va in range[kmem64_base, kmem64_end)) {
775  *	tte = tte_template +
776  *		(((miss_va & pagemask) - kmem64_base));
777  *	return tte, true
778  * } else {
779  *	return pgmap@ result
780  * }
781  */
782 char kmem64_obp_str[] =
783 	"h# %lx constant kmem64_base "
784 	"h# %lx constant kmem64_end "
785 	"h# %lx constant kmem64_pagemask "
786 	"h# %lx constant kmem64_template "
787 
788 	": kmem64-tte ( addr cnum -- false | tte-data true ) "
789 	"    if                                       ( addr ) "
790 	"       drop false exit then                  ( false ) "
791 	"    dup  kmem64_base kmem64_end  within  if  ( addr ) "
792 	"	kmem64_pagemask and                   ( addr' ) "
793 	"	kmem64_base -                         ( addr' ) "
794 	"	kmem64_template +                     ( tte ) "
795 	"	true                                  ( tte true ) "
796 	"    else                                     ( addr ) "
797 	"	pgmap@                                ( tte ) "
798 	"       dup 0< if true else drop false then   ( tte true  |  false ) "
799 	"    then                                     ( tte true  |  false ) "
800 	"; "
801 
802 	"' kmem64-tte is va>tte-data "
803 ;
804 
805 void
806 install_kmem64_tte()
807 {
808 	char b[sizeof (kmem64_obp_str) + (4 * 16)];
809 	tte_t tte;
810 
811 	PRM_DEBUG(kmem64_pabase);
812 	PRM_DEBUG(kmem64_szc);
813 	sfmmu_memtte(&tte, kmem64_pabase >> MMU_PAGESHIFT,
814 	    PROC_DATA | HAT_NOSYNC, kmem64_szc);
815 	PRM_DEBUG(tte.ll);
816 	(void) sprintf(b, kmem64_obp_str,
817 	    kmem64_base, kmem64_end, TTE_PAGEMASK(kmem64_szc), tte.ll);
818 	ASSERT(strlen(b) < sizeof (b));
819 	prom_interpret(b, 0, 0, 0, 0, 0);
820 }
821 #endif	/* !C_OBP */
822 
823 /*
824  * As OBP takes up some RAM when the system boots, pages will already be "lost"
825  * to the system and reflected in npages by the time we see it.
826  *
827  * We only want to allocate kernel structures in the 64-bit virtual address
828  * space on systems with enough RAM to make the overhead of keeping track of
829  * an extra kernel memory segment worthwhile.
830  *
831  * Since OBP has already performed its memory allocations by this point, if we
832  * have more than MINMOVE_RAM_MB MB of RAM left free, go ahead and map
833  * memory in the 64-bit virtual address space; otherwise keep allocations
834  * contiguous with we've mapped so far in the 32-bit virtual address space.
835  */
836 #define	MINMOVE_RAM_MB	((size_t)1900)
837 #define	MB_TO_BYTES(mb)	((mb) * 1048576ul)
838 
839 pgcnt_t	tune_npages = (pgcnt_t)
840 	(MB_TO_BYTES(MINMOVE_RAM_MB)/ (size_t)MMU_PAGESIZE);
841 
842 #pragma weak page_set_colorequiv_arr_cpu
843 extern void page_set_colorequiv_arr_cpu(void);
844 
845 static void
846 startup_memlist(void)
847 {
848 	size_t alloc_sz;
849 	size_t ctrs_sz;
850 	caddr_t alloc_base;
851 	caddr_t ctrs_base, ctrs_end;
852 	caddr_t memspace;
853 	caddr_t va;
854 	int memblocks = 0;
855 	struct memlist *cur;
856 	size_t syslimit = (size_t)SYSLIMIT;
857 	size_t sysbase = (size_t)SYSBASE;
858 	int alloc_alignsize = ecache_alignsize;
859 	int i;
860 	extern void page_coloring_init(void);
861 	extern void page_set_colorequiv_arr(void);
862 
863 	/*
864 	 * Initialize enough of the system to allow kmem_alloc to work by
865 	 * calling boot to allocate its memory until the time that
866 	 * kvm_init is completed.  The page structs are allocated after
867 	 * rounding up end to the nearest page boundary; the memsegs are
868 	 * initialized and the space they use comes from the kernel heap.
869 	 * With appropriate initialization, they can be reallocated later
870 	 * to a size appropriate for the machine's configuration.
871 	 *
872 	 * At this point, memory is allocated for things that will never
873 	 * need to be freed, this used to be "valloced".  This allows a
874 	 * savings as the pages don't need page structures to describe
875 	 * them because them will not be managed by the vm system.
876 	 */
877 
878 	/*
879 	 * We're loaded by boot with the following configuration (as
880 	 * specified in the sun4u/conf/Mapfile):
881 	 *
882 	 * 	text:		4 MB chunk aligned on a 4MB boundary
883 	 * 	data & bss:	4 MB chunk aligned on a 4MB boundary
884 	 *
885 	 * These two chunks will eventually be mapped by 2 locked 4MB
886 	 * ttes and will represent the nucleus of the kernel.  This gives
887 	 * us some free space that is already allocated, some or all of
888 	 * which is made available to kernel module text.
889 	 *
890 	 * The free space in the data-bss chunk is used for nucleus
891 	 * allocatable data structures and we reserve it using the
892 	 * nalloc_base and nalloc_end variables.  This space is currently
893 	 * being used for hat data structures required for tlb miss
894 	 * handling operations.  We align nalloc_base to a l2 cache
895 	 * linesize because this is the line size the hardware uses to
896 	 * maintain cache coherency.
897 	 * 256K is carved out for module data.
898 	 */
899 
900 	nalloc_base = (caddr_t)roundup((uintptr_t)e_data, MMU_PAGESIZE);
901 	moddata = nalloc_base;
902 	e_moddata = nalloc_base + MODDATA;
903 	nalloc_base = e_moddata;
904 
905 	nalloc_end = (caddr_t)roundup((uintptr_t)nalloc_base, MMU_PAGESIZE4M);
906 	valloc_base = nalloc_base;
907 
908 	/*
909 	 * Calculate the start of the data segment.
910 	 */
911 	sdata = (caddr_t)((uintptr_t)e_data & MMU_PAGEMASK4M);
912 
913 	PRM_DEBUG(moddata);
914 	PRM_DEBUG(nalloc_base);
915 	PRM_DEBUG(nalloc_end);
916 	PRM_DEBUG(sdata);
917 
918 	/*
919 	 * Remember any slop after e_text so we can give it to the modules.
920 	 */
921 	PRM_DEBUG(e_text);
922 	modtext = (caddr_t)roundup((uintptr_t)e_text, MMU_PAGESIZE);
923 	if (((uintptr_t)e_text & MMU_PAGEMASK4M) != (uintptr_t)s_text)
924 		prom_panic("nucleus text overflow");
925 	modtext_sz = (caddr_t)roundup((uintptr_t)modtext, MMU_PAGESIZE4M) -
926 	    modtext;
927 	PRM_DEBUG(modtext);
928 	PRM_DEBUG(modtext_sz);
929 
930 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
931 	    &boot_physavail, &boot_physavail_len,
932 	    &boot_virtavail, &boot_virtavail_len);
933 	/*
934 	 * Remember what the physically available highest page is
935 	 * so that dumpsys works properly, and find out how much
936 	 * memory is installed.
937 	 */
938 	installed_top_size_memlist_array(boot_physinstalled,
939 	    boot_physinstalled_len, &physmax, &physinstalled);
940 	PRM_DEBUG(physinstalled);
941 	PRM_DEBUG(physmax);
942 
943 	/* Fill out memory nodes config structure */
944 	startup_build_mem_nodes(boot_physinstalled, boot_physinstalled_len);
945 
946 	/*
947 	 * Get the list of physically available memory to size
948 	 * the number of page structures needed.
949 	 */
950 	size_physavail(boot_physavail, boot_physavail_len, &npages, &memblocks);
951 	/*
952 	 * This first snap shot of npages can represent the pages used
953 	 * by OBP's text and data approximately. This is used in the
954 	 * the calculation of the kernel size
955 	 */
956 	obp_pages = physinstalled - npages;
957 
958 
959 	/*
960 	 * On small-memory systems (<MODTEXT_SM_SIZE MB, currently 256MB), the
961 	 * in-nucleus module text is capped to MODTEXT_SM_CAP bytes (currently
962 	 * 2MB) and any excess pages are put on physavail.  The assumption is
963 	 * that small-memory systems will need more pages more than they'll
964 	 * need efficiently-mapped module texts.
965 	 */
966 	if ((physinstalled < mmu_btop(MODTEXT_SM_SIZE << 20)) &&
967 	    modtext_sz > MODTEXT_SM_CAP) {
968 		extra_etpg = mmu_btop(modtext_sz - MODTEXT_SM_CAP);
969 		modtext_sz = MODTEXT_SM_CAP;
970 		extra_etva = modtext + modtext_sz;
971 	}
972 
973 	PRM_DEBUG(extra_etpg);
974 	PRM_DEBUG(modtext_sz);
975 	PRM_DEBUG(extra_etva);
976 
977 	/*
978 	 * Account for any pages after e_text and e_data.
979 	 */
980 	npages += extra_etpg;
981 	npages += mmu_btopr(nalloc_end - nalloc_base);
982 	PRM_DEBUG(npages);
983 
984 	/*
985 	 * npages is the maximum of available physical memory possible.
986 	 * (ie. it will never be more than this)
987 	 */
988 
989 	/*
990 	 * initialize the nucleus memory allocator.
991 	 */
992 	ndata_alloc_init(&ndata, (uintptr_t)nalloc_base, (uintptr_t)nalloc_end);
993 
994 	/*
995 	 * Allocate mmu fault status area from the nucleus data area.
996 	 */
997 	if ((&ndata_alloc_mmfsa != NULL) && (ndata_alloc_mmfsa(&ndata) != 0))
998 		cmn_err(CE_PANIC, "no more nucleus memory after mfsa alloc");
999 
1000 	/*
1001 	 * Allocate kernel TSBs from the nucleus data area.
1002 	 */
1003 	if (ndata_alloc_tsbs(&ndata, npages) != 0)
1004 		cmn_err(CE_PANIC, "no more nucleus memory after tsbs alloc");
1005 
1006 	/*
1007 	 * Allocate dmv dispatch table from the nucleus data area.
1008 	 */
1009 	if (ndata_alloc_dmv(&ndata) != 0)
1010 		cmn_err(CE_PANIC, "no more nucleus memory after dmv alloc");
1011 
1012 
1013 	page_coloring_init();
1014 
1015 	/*
1016 	 * Allocate page_freelists bin headers for memnode 0 from the
1017 	 * nucleus data area.
1018 	 */
1019 	if (ndata_alloc_page_freelists(&ndata, 0) != 0)
1020 		cmn_err(CE_PANIC,
1021 		    "no more nucleus memory after page free lists alloc");
1022 
1023 	if (kpm_enable) {
1024 		kpm_init();
1025 		/*
1026 		 * kpm page space -- Update kpm_npages and make the
1027 		 * same assumption about fragmenting as it is done
1028 		 * for memseg_sz.
1029 		 */
1030 		kpm_npages_setup(memblocks + 4);
1031 	}
1032 
1033 	/*
1034 	 * Allocate hat related structs from the nucleus data area.
1035 	 */
1036 	if (ndata_alloc_hat(&ndata, npages, kpm_npages) != 0)
1037 		cmn_err(CE_PANIC, "no more nucleus memory after hat alloc");
1038 
1039 	/*
1040 	 * We want to do the BOP_ALLOCs before the real allocation of page
1041 	 * structs in order to not have to allocate page structs for this
1042 	 * memory.  We need to calculate a virtual address because we want
1043 	 * the page structs to come before other allocations in virtual address
1044 	 * space.  This is so some (if not all) of page structs can actually
1045 	 * live in the nucleus.
1046 	 */
1047 
1048 	/*
1049 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
1050 	 *
1051 	 * There are comments all over the SFMMU code warning of dire
1052 	 * consequences if the TSBs are moved out of 32-bit space.  This
1053 	 * is largely because the asm code uses "sethi %hi(addr)"-type
1054 	 * instructions which will not provide the expected result if the
1055 	 * address is a 64-bit one.
1056 	 *
1057 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
1058 	 */
1059 	alloc_base = (caddr_t)roundup((uintptr_t)nalloc_end, MMU_PAGESIZE);
1060 	alloc_base = sfmmu_ktsb_alloc(alloc_base);
1061 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
1062 	PRM_DEBUG(alloc_base);
1063 
1064 	/*
1065 	 * Allocate IOMMU TSB array.  We do this here so that the physical
1066 	 * memory gets deducted from the PROM's physical memory list.
1067 	 */
1068 	alloc_base = iommu_tsb_init(alloc_base);
1069 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
1070 	    ecache_alignsize);
1071 	PRM_DEBUG(alloc_base);
1072 
1073 	/*
1074 	 * Platforms like Starcat and OPL need special structures assigned in
1075 	 * 32-bit virtual address space because their probing routines execute
1076 	 * FCode, and FCode can't handle 64-bit virtual addresses...
1077 	 */
1078 	if (&plat_startup_memlist) {
1079 		alloc_base = plat_startup_memlist(alloc_base);
1080 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
1081 		    ecache_alignsize);
1082 		PRM_DEBUG(alloc_base);
1083 	}
1084 
1085 	/*
1086 	 * Save off where the contiguous allocations to date have ended
1087 	 * in econtig32.
1088 	 */
1089 	econtig32 = alloc_base;
1090 	PRM_DEBUG(econtig32);
1091 
1092 	if (econtig32 > (caddr_t)KERNEL_LIMIT32)
1093 		cmn_err(CE_PANIC, "econtig32 too big");
1094 
1095 	/*
1096 	 * To avoid memory allocation collisions in the 32-bit virtual address
1097 	 * space, make allocations from this point forward in 64-bit virtual
1098 	 * address space starting at syslimit and working up.
1099 	 *
1100 	 * All this is needed because on large memory systems, the default
1101 	 * Solaris allocations will collide with SYSBASE32, which is hard
1102 	 * coded to be at the virtual address 0x78000000.  Therefore, on 64-bit
1103 	 * kernels, move the allocations to a location in the 64-bit virtual
1104 	 * address space space, allowing those structures to grow without
1105 	 * worry.
1106 	 *
1107 	 * On current CPUs we'll run out of physical memory address bits before
1108 	 * we need to worry about the allocations running into anything else in
1109 	 * VM or the virtual address holes on US-I and II, as there's currently
1110 	 * about 1 TB of addressable space before the US-I/II VA hole.
1111 	 */
1112 	kmem64_base = (caddr_t)syslimit;
1113 	PRM_DEBUG(kmem64_base);
1114 
1115 	/*
1116 	 * Allocate addresses, but not physical memory. None of these locations
1117 	 * can be touched until physical memory is allocated below.
1118 	 */
1119 	alloc_base = kmem64_base;
1120 
1121 	/*
1122 	 * If KHME and/or UHME hash buckets won't fit in the nucleus, allocate
1123 	 * them here.
1124 	 */
1125 	if (khme_hash == NULL || uhme_hash == NULL) {
1126 		/*
1127 		 * alloc_hme_buckets() will align alloc_base properly before
1128 		 * assigning the hash buckets, so we don't need to do it
1129 		 * before the call...
1130 		 */
1131 		alloc_base = alloc_hme_buckets(alloc_base, alloc_alignsize);
1132 
1133 		PRM_DEBUG(alloc_base);
1134 		PRM_DEBUG(khme_hash);
1135 		PRM_DEBUG(uhme_hash);
1136 	}
1137 
1138 	/*
1139 	 * Allow for an early allocation of physically contiguous memory.
1140 	 */
1141 	alloc_base = contig_mem_prealloc(alloc_base, npages);
1142 
1143 	/*
1144 	 * Allocate the remaining page freelists.  NUMA systems can
1145 	 * have lots of page freelists, one per node, which quickly
1146 	 * outgrow the amount of nucleus memory available.
1147 	 */
1148 	if (max_mem_nodes > 1) {
1149 		int mnode;
1150 
1151 		for (mnode = 1; mnode < max_mem_nodes; mnode++) {
1152 			alloc_base = alloc_page_freelists(mnode, alloc_base,
1153 				ecache_alignsize);
1154 		}
1155 		PRM_DEBUG(alloc_base);
1156 	}
1157 
1158 	if (!mml_table) {
1159 		size_t mmltable_sz;
1160 
1161 		/*
1162 		 * We need to allocate the mml_table here because there
1163 		 * was not enough space within the nucleus.
1164 		 */
1165 		mmltable_sz = sizeof (kmutex_t) * mml_table_sz;
1166 		alloc_sz = roundup(mmltable_sz, alloc_alignsize);
1167 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
1168 		    alloc_alignsize);
1169 		mml_table = (kmutex_t *)alloc_base;
1170 		alloc_base += alloc_sz;
1171 		PRM_DEBUG(mml_table);
1172 		PRM_DEBUG(alloc_base);
1173 	}
1174 
1175 	if (kpm_enable && !(kpmp_table || kpmp_stable)) {
1176 		size_t kpmptable_sz;
1177 		caddr_t table;
1178 
1179 		/*
1180 		 * We need to allocate either kpmp_table or kpmp_stable here
1181 		 * because there was not enough space within the nucleus.
1182 		 */
1183 		kpmptable_sz = (kpm_smallpages == 0) ?
1184 				sizeof (kpm_hlk_t) * kpmp_table_sz :
1185 				sizeof (kpm_shlk_t) * kpmp_stable_sz;
1186 
1187 		alloc_sz = roundup(kpmptable_sz, alloc_alignsize);
1188 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
1189 		    alloc_alignsize);
1190 
1191 		table = alloc_base;
1192 
1193 		if (kpm_smallpages == 0) {
1194 			kpmp_table = (kpm_hlk_t *)table;
1195 			PRM_DEBUG(kpmp_table);
1196 		} else {
1197 			kpmp_stable = (kpm_shlk_t *)table;
1198 			PRM_DEBUG(kpmp_stable);
1199 		}
1200 
1201 		alloc_base += alloc_sz;
1202 		PRM_DEBUG(alloc_base);
1203 	}
1204 
1205 	if (&ecache_init_scrub_flush_area) {
1206 		/*
1207 		 * Pass alloc_base directly, as the routine itself is
1208 		 * responsible for any special alignment requirements...
1209 		 */
1210 		alloc_base = ecache_init_scrub_flush_area(alloc_base);
1211 		PRM_DEBUG(alloc_base);
1212 	}
1213 
1214 	/*
1215 	 * Take the most current snapshot we can by calling mem-update.
1216 	 */
1217 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
1218 	    &boot_physavail, &boot_physavail_len,
1219 	    &boot_virtavail, &boot_virtavail_len);
1220 
1221 	/*
1222 	 * Reset npages and memblocks based on boot_physavail list.
1223 	 */
1224 	size_physavail(boot_physavail, boot_physavail_len, &npages, &memblocks);
1225 	PRM_DEBUG(npages);
1226 
1227 	/*
1228 	 * Account for extra memory after e_text.
1229 	 */
1230 	npages += extra_etpg;
1231 
1232 	/*
1233 	 * Calculate the largest free memory chunk in the nucleus data area.
1234 	 * We need to figure out if page structs can fit in there or not.
1235 	 * We also make sure enough page structs get created for any physical
1236 	 * memory we might be returning to the system.
1237 	 */
1238 	ndata_remain_sz = ndata_maxsize(&ndata);
1239 	PRM_DEBUG(ndata_remain_sz);
1240 
1241 	pp_sz = sizeof (struct page) * npages;
1242 
1243 	/*
1244 	 * Here's a nice bit of code based on somewhat recursive logic:
1245 	 *
1246 	 * If the page array would fit within the nucleus, we want to
1247 	 * add npages to cover any extra memory we may be returning back
1248 	 * to the system.
1249 	 *
1250 	 * HOWEVER, the page array is sized by calculating the size of
1251 	 * (struct page * npages), as are the pagehash table, ctrs and
1252 	 * memseg_list, so the very act of performing the calculation below may
1253 	 * in fact make the array large enough that it no longer fits in the
1254 	 * nucleus, meaning there would now be a much larger area of the
1255 	 * nucleus free that should really be added to npages, which would
1256 	 * make the page array that much larger, and so on.
1257 	 *
1258 	 * This also ignores the memory possibly used in the nucleus for the
1259 	 * the page hash, ctrs and memseg list and the fact that whether they
1260 	 * fit there or not varies with the npages calculation below, but we
1261 	 * don't even factor them into the equation at this point; perhaps we
1262 	 * should or perhaps we should just take the approach that the few
1263 	 * extra pages we could add via this calculation REALLY aren't worth
1264 	 * the hassle...
1265 	 */
1266 	if (ndata_remain_sz > pp_sz) {
1267 		size_t spare = ndata_spare(&ndata, pp_sz, ecache_alignsize);
1268 
1269 		npages += mmu_btop(spare);
1270 
1271 		pp_sz = npages * sizeof (struct page);
1272 
1273 		pp_base = ndata_alloc(&ndata, pp_sz, ecache_alignsize);
1274 	}
1275 
1276 	/*
1277 	 * If physmem is patched to be non-zero, use it instead of
1278 	 * the monitor value unless physmem is larger than the total
1279 	 * amount of memory on hand.
1280 	 */
1281 	if (physmem == 0 || physmem > npages)
1282 		physmem = npages;
1283 
1284 	/*
1285 	 * If pp_base is NULL that means the routines above have determined
1286 	 * the page array will not fit in the nucleus; we'll have to
1287 	 * BOP_ALLOC() ourselves some space for them.
1288 	 */
1289 	if (pp_base == NULL) {
1290 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
1291 		    alloc_alignsize);
1292 		alloc_sz = roundup(pp_sz, alloc_alignsize);
1293 
1294 		pp_base = (struct page *)alloc_base;
1295 
1296 		alloc_base += alloc_sz;
1297 	}
1298 
1299 	/*
1300 	 * The page structure hash table size is a power of 2
1301 	 * such that the average hash chain length is PAGE_HASHAVELEN.
1302 	 */
1303 	page_hashsz = npages / PAGE_HASHAVELEN;
1304 	page_hashsz = 1 << highbit((ulong_t)page_hashsz);
1305 	pagehash_sz = sizeof (struct page *) * page_hashsz;
1306 
1307 	/*
1308 	 * We want to TRY to fit the page structure hash table,
1309 	 * the page size free list counters, the memseg list and
1310 	 * and the kpm page space in the nucleus if possible.
1311 	 *
1312 	 * alloc_sz counts how much memory needs to be allocated by
1313 	 * BOP_ALLOC().
1314 	 */
1315 	page_hash = ndata_alloc(&ndata, pagehash_sz, ecache_alignsize);
1316 
1317 	alloc_sz = (page_hash == NULL ? pagehash_sz : 0);
1318 
1319 	/*
1320 	 * Size up per page size free list counters.
1321 	 */
1322 	ctrs_sz = page_ctrs_sz();
1323 	ctrs_base = ndata_alloc(&ndata, ctrs_sz, ecache_alignsize);
1324 
1325 	if (ctrs_base == NULL)
1326 		alloc_sz = roundup(alloc_sz, ecache_alignsize) + ctrs_sz;
1327 
1328 	/*
1329 	 * The memseg list is for the chunks of physical memory that
1330 	 * will be managed by the vm system.  The number calculated is
1331 	 * a guess as boot may fragment it more when memory allocations
1332 	 * are made before kphysm_init().  Currently, there are two
1333 	 * allocations before then, so we assume each causes fragmen-
1334 	 * tation, and add a couple more for good measure.
1335 	 */
1336 	memseg_sz = sizeof (struct memseg) * (memblocks + 4);
1337 	memseg_base = ndata_alloc(&ndata, memseg_sz, ecache_alignsize);
1338 
1339 	if (memseg_base == NULL)
1340 		alloc_sz = roundup(alloc_sz, ecache_alignsize) + memseg_sz;
1341 
1342 
1343 	if (kpm_enable) {
1344 		/*
1345 		 * kpm page space -- Update kpm_npages and make the
1346 		 * same assumption about fragmenting as it is done
1347 		 * for memseg_sz above.
1348 		 */
1349 		kpm_npages_setup(memblocks + 4);
1350 		kpm_pp_sz = (kpm_smallpages == 0) ?
1351 		    kpm_npages * sizeof (kpm_page_t):
1352 		    kpm_npages * sizeof (kpm_spage_t);
1353 
1354 		kpm_pp_base = (uintptr_t)ndata_alloc(&ndata, kpm_pp_sz,
1355 		    ecache_alignsize);
1356 
1357 		if (kpm_pp_base == NULL)
1358 			alloc_sz = roundup(alloc_sz, ecache_alignsize) +
1359 			    kpm_pp_sz;
1360 	}
1361 
1362 	if (alloc_sz > 0) {
1363 		uintptr_t bop_base;
1364 
1365 		/*
1366 		 * We need extra memory allocated through BOP_ALLOC.
1367 		 */
1368 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
1369 		    alloc_alignsize);
1370 
1371 		alloc_sz = roundup(alloc_sz, alloc_alignsize);
1372 
1373 		bop_base = (uintptr_t)alloc_base;
1374 
1375 		alloc_base += alloc_sz;
1376 
1377 		if (page_hash == NULL) {
1378 			page_hash = (struct page **)bop_base;
1379 			bop_base = roundup(bop_base + pagehash_sz,
1380 			    ecache_alignsize);
1381 		}
1382 
1383 		if (ctrs_base == NULL) {
1384 			ctrs_base = (caddr_t)bop_base;
1385 			bop_base = roundup(bop_base + ctrs_sz,
1386 			    ecache_alignsize);
1387 		}
1388 
1389 		if (memseg_base == NULL) {
1390 			memseg_base = (struct memseg *)bop_base;
1391 			bop_base = roundup(bop_base + memseg_sz,
1392 			    ecache_alignsize);
1393 		}
1394 
1395 		if (kpm_enable && kpm_pp_base == NULL) {
1396 			kpm_pp_base = (uintptr_t)bop_base;
1397 			bop_base = roundup(bop_base + kpm_pp_sz,
1398 			    ecache_alignsize);
1399 		}
1400 
1401 		ASSERT(bop_base <= (uintptr_t)alloc_base);
1402 	}
1403 
1404 	PRM_DEBUG(page_hash);
1405 	PRM_DEBUG(memseg_base);
1406 	PRM_DEBUG(kpm_pp_base);
1407 	PRM_DEBUG(kpm_pp_sz);
1408 	PRM_DEBUG(pp_base);
1409 	PRM_DEBUG(pp_sz);
1410 	PRM_DEBUG(alloc_base);
1411 
1412 #ifdef	TRAPTRACE
1413 	alloc_base = trap_trace_alloc(alloc_base);
1414 	PRM_DEBUG(alloc_base);
1415 #endif	/* TRAPTRACE */
1416 
1417 	/*
1418 	 * In theory it's possible that kmem64 chunk is 0 sized
1419 	 * (on very small machines). Check for that.
1420 	 */
1421 	if (alloc_base == kmem64_base) {
1422 		kmem64_base = NULL;
1423 		kmem64_end = NULL;
1424 		kmem64_aligned_end = NULL;
1425 		goto kmem64_alloced;
1426 	}
1427 
1428 	/*
1429 	 * Allocate kmem64 memory.
1430 	 * Round up to end of large page and overmap.
1431 	 * kmem64_end..kmem64_aligned_end is added to memory list for reuse
1432 	 */
1433 	kmem64_end = (caddr_t)roundup((uintptr_t)alloc_base,
1434 	    MMU_PAGESIZE);
1435 
1436 	/*
1437 	 * Make one large memory alloc after figuring out the 64-bit size. This
1438 	 * will enable use of the largest page size appropriate for the system
1439 	 * architecture.
1440 	 */
1441 	ASSERT(mmu_exported_pagesize_mask & (1 << TTE8K));
1442 	ASSERT(IS_P2ALIGNED(kmem64_base, TTEBYTES(max_bootlp_tteszc)));
1443 	for (i = max_bootlp_tteszc; i >= TTE8K; i--) {
1444 		size_t asize;
1445 #if !defined(C_OBP)
1446 		unsigned long long pa;
1447 #endif	/* !C_OBP */
1448 
1449 		if ((mmu_exported_pagesize_mask & (1 << i)) == 0)
1450 			continue;
1451 		kmem64_alignsize = TTEBYTES(i);
1452 		kmem64_szc = i;
1453 
1454 		/* limit page size for small memory */
1455 		if (mmu_btop(kmem64_alignsize) > (npages >> 2))
1456 			continue;
1457 
1458 		kmem64_aligned_end = (caddr_t)roundup((uintptr_t)kmem64_end,
1459 		    kmem64_alignsize);
1460 		asize = kmem64_aligned_end - kmem64_base;
1461 #if !defined(C_OBP)
1462 		if (prom_allocate_phys(asize, kmem64_alignsize, &pa) == 0) {
1463 			if (prom_claim_virt(asize, kmem64_base) !=
1464 			    (caddr_t)-1) {
1465 				kmem64_pabase = pa;
1466 				install_kmem64_tte();
1467 				break;
1468 			} else {
1469 				prom_free_phys(asize, pa);
1470 			}
1471 		}
1472 #else	/* !C_OBP */
1473 		if ((caddr_t)BOP_ALLOC(bootops, kmem64_base, asize,
1474 		    kmem64_alignsize) == kmem64_base) {
1475 			kmem64_pabase = va_to_pa(kmem64_base);
1476 			break;
1477 		}
1478 #endif	/* !C_OBP */
1479 		if (i == TTE8K) {
1480 			prom_panic("kmem64 allocation failure");
1481 		}
1482 	}
1483 
1484 	PRM_DEBUG(kmem64_base);
1485 	PRM_DEBUG(kmem64_end);
1486 	PRM_DEBUG(kmem64_aligned_end);
1487 	PRM_DEBUG(kmem64_alignsize);
1488 
1489 	/*
1490 	 * Now set pa using saved va from above.
1491 	 */
1492 	if (&ecache_init_scrub_flush_area) {
1493 		(void) ecache_init_scrub_flush_area(NULL);
1494 	}
1495 
1496 kmem64_alloced:
1497 
1498 	/*
1499 	 * Initialize per page size free list counters.
1500 	 */
1501 	ctrs_end = page_ctrs_alloc(ctrs_base);
1502 	ASSERT(ctrs_base + ctrs_sz >= ctrs_end);
1503 
1504 	/*
1505 	 * Allocate space for the interrupt vector table and also for the
1506 	 * reserved interrupt vector data structures.
1507 	 */
1508 	memspace = (caddr_t)BOP_ALLOC(bootops, (caddr_t)intr_vec_table,
1509 	    IVSIZE, MMU_PAGESIZE);
1510 	if (memspace != (caddr_t)intr_vec_table)
1511 		prom_panic("interrupt vector table allocation failure");
1512 
1513 	/*
1514 	 * The memory lists from boot are allocated from the heap arena
1515 	 * so that later they can be freed and/or reallocated.
1516 	 */
1517 	if (BOP_GETPROP(bootops, "extent", &memlist_sz) == -1)
1518 		prom_panic("could not retrieve property \"extent\"");
1519 
1520 	/*
1521 	 * Between now and when we finish copying in the memory lists,
1522 	 * allocations happen so the space gets fragmented and the
1523 	 * lists longer.  Leave enough space for lists twice as long
1524 	 * as what boot says it has now; roundup to a pagesize.
1525 	 * Also add space for the final phys-avail copy in the fixup
1526 	 * routine.
1527 	 */
1528 	va = (caddr_t)(sysbase + PAGESIZE + PANICBUFSIZE +
1529 	    roundup(IVSIZE, MMU_PAGESIZE));
1530 	memlist_sz *= 4;
1531 	memlist_sz = roundup(memlist_sz, MMU_PAGESIZE);
1532 	memspace = (caddr_t)BOP_ALLOC(bootops, va, memlist_sz, BO_NO_ALIGN);
1533 	if (memspace == NULL)
1534 		halt("Boot allocation failed.");
1535 
1536 	memlist = (struct memlist *)memspace;
1537 	memlist_end = (char *)memspace + memlist_sz;
1538 
1539 	PRM_DEBUG(memlist);
1540 	PRM_DEBUG(memlist_end);
1541 	PRM_DEBUG(sysbase);
1542 	PRM_DEBUG(syslimit);
1543 
1544 	kernelheap_init((void *)sysbase, (void *)syslimit,
1545 	    (caddr_t)sysbase + PAGESIZE, NULL, NULL);
1546 
1547 	/*
1548 	 * Take the most current snapshot we can by calling mem-update.
1549 	 */
1550 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
1551 	    &boot_physavail, &boot_physavail_len,
1552 	    &boot_virtavail, &boot_virtavail_len);
1553 
1554 	/*
1555 	 * Remove the space used by BOP_ALLOC from the kernel heap
1556 	 * plus the area actually used by the OBP (if any)
1557 	 * ignoring virtual addresses in virt_avail, above syslimit.
1558 	 */
1559 	virt_avail = memlist;
1560 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
1561 
1562 	for (cur = virt_avail; cur->next; cur = cur->next) {
1563 		uint64_t range_base, range_size;
1564 
1565 		if ((range_base = cur->address + cur->size) < (uint64_t)sysbase)
1566 			continue;
1567 		if (range_base >= (uint64_t)syslimit)
1568 			break;
1569 		/*
1570 		 * Limit the range to end at syslimit.
1571 		 */
1572 		range_size = MIN(cur->next->address,
1573 		    (uint64_t)syslimit) - range_base;
1574 		(void) vmem_xalloc(heap_arena, (size_t)range_size, PAGESIZE,
1575 		    0, 0, (void *)range_base, (void *)(range_base + range_size),
1576 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
1577 	}
1578 
1579 	phys_avail = memlist;
1580 	(void) copy_physavail(boot_physavail, boot_physavail_len,
1581 	    &memlist, 0, 0);
1582 
1583 	/*
1584 	 * Add any unused kmem64 memory from overmapped page
1585 	 * (Note: va_to_pa does not work for kmem64_end)
1586 	 */
1587 	if (kmem64_end < kmem64_aligned_end) {
1588 		uint64_t overlap_size = kmem64_aligned_end - kmem64_end;
1589 		uint64_t overlap_pa = kmem64_pabase +
1590 		    (kmem64_end - kmem64_base);
1591 
1592 		PRM_DEBUG(overlap_pa);
1593 		PRM_DEBUG(overlap_size);
1594 		memlist_add(overlap_pa, overlap_size, &memlist, &phys_avail);
1595 	}
1596 
1597 	/*
1598 	 * Add any extra memory after e_text to the phys_avail list, as long
1599 	 * as there's at least a page to add.
1600 	 */
1601 	if (extra_etpg)
1602 		memlist_add(va_to_pa(extra_etva), mmu_ptob(extra_etpg),
1603 		    &memlist, &phys_avail);
1604 
1605 	/*
1606 	 * Add any extra memory at the end of the ndata region if there's at
1607 	 * least a page to add.  There might be a few more pages available in
1608 	 * the middle of the ndata region, but for now they are ignored.
1609 	 */
1610 	nalloc_base = ndata_extra_base(&ndata, MMU_PAGESIZE, nalloc_end);
1611 	if (nalloc_base == NULL)
1612 		nalloc_base = nalloc_end;
1613 	ndata_remain_sz = nalloc_end - nalloc_base;
1614 
1615 	if (ndata_remain_sz >= MMU_PAGESIZE)
1616 		memlist_add(va_to_pa(nalloc_base),
1617 		    (uint64_t)ndata_remain_sz, &memlist, &phys_avail);
1618 
1619 	PRM_DEBUG(memlist);
1620 	PRM_DEBUG(memlist_sz);
1621 	PRM_DEBUG(memspace);
1622 
1623 	if ((caddr_t)memlist > (memspace + memlist_sz))
1624 		prom_panic("memlist overflow");
1625 
1626 	PRM_DEBUG(pp_base);
1627 	PRM_DEBUG(memseg_base);
1628 	PRM_DEBUG(npages);
1629 
1630 	/*
1631 	 * Initialize the page structures from the memory lists.
1632 	 */
1633 	kphysm_init(pp_base, memseg_base, npages, kpm_pp_base, kpm_npages);
1634 
1635 	availrmem_initial = availrmem = freemem;
1636 	PRM_DEBUG(availrmem);
1637 
1638 	/*
1639 	 * Some of the locks depend on page_hashsz being set!
1640 	 * kmem_init() depends on this; so, keep it here.
1641 	 */
1642 	page_lock_init();
1643 
1644 	/*
1645 	 * Initialize kernel memory allocator.
1646 	 */
1647 	kmem_init();
1648 
1649 	/*
1650 	 * Factor in colorequiv to check additional 'equivalent' bins
1651 	 */
1652 	if (&page_set_colorequiv_arr_cpu != NULL)
1653 		page_set_colorequiv_arr_cpu();
1654 	else
1655 		page_set_colorequiv_arr();
1656 
1657 	/*
1658 	 * Initialize bp_mapin().
1659 	 */
1660 	bp_init(shm_alignment, HAT_STRICTORDER);
1661 
1662 	/*
1663 	 * Reserve space for panicbuf, intr_vec_table and reserved interrupt
1664 	 * vector data structures from the 32-bit heap.
1665 	 */
1666 	(void) vmem_xalloc(heap32_arena, PANICBUFSIZE, PAGESIZE, 0, 0,
1667 	    panicbuf, panicbuf + PANICBUFSIZE,
1668 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
1669 
1670 	(void) vmem_xalloc(heap32_arena, IVSIZE, PAGESIZE, 0, 0,
1671 	    intr_vec_table, (caddr_t)intr_vec_table + IVSIZE,
1672 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
1673 
1674 	mem_config_init();
1675 }
1676 
1677 static void
1678 startup_modules(void)
1679 {
1680 	int proplen, nhblk1, nhblk8;
1681 	size_t  nhblksz;
1682 	pgcnt_t pages_per_hblk;
1683 	size_t hme8blk_sz, hme1blk_sz;
1684 
1685 	/*
1686 	 * Log any optional messages from the boot program
1687 	 */
1688 	proplen = (size_t)BOP_GETPROPLEN(bootops, "boot-message");
1689 	if (proplen > 0) {
1690 		char *msg;
1691 		size_t len = (size_t)proplen;
1692 
1693 		msg = kmem_zalloc(len, KM_SLEEP);
1694 		(void) BOP_GETPROP(bootops, "boot-message", msg);
1695 		cmn_err(CE_CONT, "?%s\n", msg);
1696 		kmem_free(msg, len);
1697 	}
1698 
1699 	/*
1700 	 * Let the platforms have a chance to change default
1701 	 * values before reading system file.
1702 	 */
1703 	if (&set_platform_defaults)
1704 		set_platform_defaults();
1705 
1706 	/*
1707 	 * Calculate default settings of system parameters based upon
1708 	 * maxusers, yet allow to be overridden via the /etc/system file.
1709 	 */
1710 	param_calc(0);
1711 
1712 	mod_setup();
1713 
1714 	/*
1715 	 * If this is a positron, complain and halt.
1716 	 */
1717 	if (&iam_positron && iam_positron()) {
1718 		cmn_err(CE_WARN, "This hardware platform is not supported"
1719 		    " by this release of Solaris.\n");
1720 #ifdef DEBUG
1721 		prom_enter_mon();	/* Type 'go' to resume */
1722 		cmn_err(CE_WARN, "Booting an unsupported platform.\n");
1723 		cmn_err(CE_WARN, "Booting with down-rev firmware.\n");
1724 
1725 #else /* DEBUG */
1726 		halt(0);
1727 #endif /* DEBUG */
1728 	}
1729 
1730 	/*
1731 	 * If we are running firmware that isn't 64-bit ready
1732 	 * then complain and halt.
1733 	 */
1734 	do_prom_version_check();
1735 
1736 	/*
1737 	 * Initialize system parameters
1738 	 */
1739 	param_init();
1740 
1741 	/*
1742 	 * maxmem is the amount of physical memory we're playing with.
1743 	 */
1744 	maxmem = physmem;
1745 
1746 	/* Set segkp limits. */
1747 	ncbase = kdi_segdebugbase;
1748 	ncend = kdi_segdebugbase;
1749 
1750 	/*
1751 	 * Initialize the hat layer.
1752 	 */
1753 	hat_init();
1754 
1755 	/*
1756 	 * Initialize segment management stuff.
1757 	 */
1758 	seg_init();
1759 
1760 	/*
1761 	 * Create the va>tte handler, so the prom can understand
1762 	 * kernel translations.  The handler is installed later, just
1763 	 * as we are about to take over the trap table from the prom.
1764 	 */
1765 	create_va_to_tte();
1766 
1767 	/*
1768 	 * Load the forthdebugger (optional)
1769 	 */
1770 	forthdebug_init();
1771 
1772 	/*
1773 	 * Create OBP node for console input callbacks
1774 	 * if it is needed.
1775 	 */
1776 	startup_create_io_node();
1777 
1778 	if (modloadonly("fs", "specfs") == -1)
1779 		halt("Can't load specfs");
1780 
1781 	if (modloadonly("fs", "devfs") == -1)
1782 		halt("Can't load devfs");
1783 
1784 	if (modloadonly("misc", "swapgeneric") == -1)
1785 		halt("Can't load swapgeneric");
1786 
1787 	(void) modloadonly("sys", "lbl_edition");
1788 
1789 	dispinit();
1790 
1791 	/*
1792 	 * Infer meanings to the members of the idprom buffer.
1793 	 */
1794 	parse_idprom();
1795 
1796 	/* Read cluster configuration data. */
1797 	clconf_init();
1798 
1799 	setup_ddi();
1800 
1801 	/*
1802 	 * Lets take this opportunity to load the root device.
1803 	 */
1804 	if (loadrootmodules() != 0)
1805 		debug_enter("Can't load the root filesystem");
1806 
1807 	/*
1808 	 * Load tod driver module for the tod part found on this system.
1809 	 * Recompute the cpu frequency/delays based on tod as tod part
1810 	 * tends to keep time more accurately.
1811 	 */
1812 	if (&load_tod_module)
1813 		load_tod_module();
1814 
1815 	/*
1816 	 * Allow platforms to load modules which might
1817 	 * be needed after bootops are gone.
1818 	 */
1819 	if (&load_platform_modules)
1820 		load_platform_modules();
1821 
1822 	setcpudelay();
1823 
1824 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
1825 	    &boot_physavail, &boot_physavail_len,
1826 	    &boot_virtavail, &boot_virtavail_len);
1827 
1828 	/*
1829 	 * Calculation and allocation of hmeblks needed to remap
1830 	 * the memory allocated by PROM till now.
1831 	 * Overestimate the number of hblk1 elements by assuming
1832 	 * worst case of TTE64K mappings.
1833 	 * sfmmu_hblk_alloc will panic if this calculation is wrong.
1834 	 */
1835 	bop_alloc_pages = btopr(kmem64_end - kmem64_base);
1836 	pages_per_hblk = btop(HMEBLK_SPAN(TTE64K));
1837 	bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
1838 	nhblk1 = bop_alloc_pages / pages_per_hblk + hblk1_min;
1839 
1840 	bop_alloc_pages = size_virtalloc(boot_virtavail, boot_virtavail_len);
1841 
1842 	/* sfmmu_init_nucleus_hblks expects properly aligned data structures */
1843 	hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t));
1844 	hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t));
1845 
1846 	bop_alloc_pages += btopr(nhblk1 * hme1blk_sz);
1847 
1848 	pages_per_hblk = btop(HMEBLK_SPAN(TTE8K));
1849 	nhblk8 = 0;
1850 	while (bop_alloc_pages > 1) {
1851 		bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
1852 		nhblk8 += bop_alloc_pages /= pages_per_hblk;
1853 		bop_alloc_pages *= hme8blk_sz;
1854 		bop_alloc_pages = btopr(bop_alloc_pages);
1855 	}
1856 	nhblk8 += 2;
1857 
1858 	/*
1859 	 * Since hblk8's can hold up to 64k of mappings aligned on a 64k
1860 	 * boundary, the number of hblk8's needed to map the entries in the
1861 	 * boot_virtavail list needs to be adjusted to take this into
1862 	 * consideration.  Thus, we need to add additional hblk8's since it
1863 	 * is possible that an hblk8 will not have all 8 slots used due to
1864 	 * alignment constraints.  Since there were boot_virtavail_len entries
1865 	 * in that list, we need to add that many hblk8's to the number
1866 	 * already calculated to make sure we don't underestimate.
1867 	 */
1868 	nhblk8 += boot_virtavail_len;
1869 	nhblksz = nhblk8 * hme8blk_sz + nhblk1 * hme1blk_sz;
1870 
1871 	/* Allocate in pagesize chunks */
1872 	nhblksz = roundup(nhblksz, MMU_PAGESIZE);
1873 	hblk_base = kmem_zalloc(nhblksz, KM_SLEEP);
1874 	sfmmu_init_nucleus_hblks(hblk_base, nhblksz, nhblk8, nhblk1);
1875 }
1876 
1877 static void
1878 startup_bop_gone(void)
1879 {
1880 	extern int bop_io_quiesced;
1881 
1882 	/*
1883 	 * Destroy the MD initialized at startup
1884 	 * The startup initializes the MD framework
1885 	 * using prom and BOP alloc free it now.
1886 	 */
1887 	mach_descrip_startup_fini();
1888 
1889 	/*
1890 	 * Call back into boot and release boots resources.
1891 	 */
1892 	BOP_QUIESCE_IO(bootops);
1893 	bop_io_quiesced = 1;
1894 
1895 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
1896 	    &boot_physavail, &boot_physavail_len,
1897 	    &boot_virtavail, &boot_virtavail_len);
1898 	/*
1899 	 * Copy physinstalled list into kernel space.
1900 	 */
1901 	phys_install = memlist;
1902 	copy_memlist(boot_physinstalled, boot_physinstalled_len, &memlist);
1903 
1904 	/*
1905 	 * setup physically contiguous area twice as large as the ecache.
1906 	 * this is used while doing displacement flush of ecaches
1907 	 */
1908 	if (&ecache_flush_address) {
1909 		ecache_flushaddr = ecache_flush_address();
1910 		if (ecache_flushaddr == (uint64_t)-1) {
1911 			cmn_err(CE_PANIC,
1912 			    "startup: no memory to set ecache_flushaddr");
1913 		}
1914 	}
1915 
1916 	/*
1917 	 * Virtual available next.
1918 	 */
1919 	ASSERT(virt_avail != NULL);
1920 	memlist_free_list(virt_avail);
1921 	virt_avail = memlist;
1922 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
1923 
1924 	/*
1925 	 * Last chance to ask our booter questions ..
1926 	 */
1927 }
1928 
1929 
1930 /*
1931  * startup_fixup_physavail - called from mach_sfmmu.c after the final
1932  * allocations have been performed.  We can't call it in startup_bop_gone
1933  * since later operations can cause obp to allocate more memory.
1934  */
1935 void
1936 startup_fixup_physavail(void)
1937 {
1938 	struct memlist *cur;
1939 	size_t kmem64_overmap_size = kmem64_aligned_end - kmem64_end;
1940 
1941 	PRM_DEBUG(kmem64_overmap_size);
1942 
1943 	/*
1944 	 * take the most current snapshot we can by calling mem-update
1945 	 */
1946 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
1947 	    &boot_physavail, &boot_physavail_len,
1948 	    &boot_virtavail, &boot_virtavail_len);
1949 
1950 	/*
1951 	 * Copy phys_avail list, again.
1952 	 * Both the kernel/boot and the prom have been allocating
1953 	 * from the original list we copied earlier.
1954 	 */
1955 	cur = memlist;
1956 	(void) copy_physavail(boot_physavail, boot_physavail_len,
1957 	    &memlist, 0, 0);
1958 
1959 	/*
1960 	 * Add any unused kmem64 memory from overmapped page
1961 	 * (Note: va_to_pa does not work for kmem64_end)
1962 	 */
1963 	if (kmem64_overmap_size) {
1964 		memlist_add(kmem64_pabase + (kmem64_end - kmem64_base),
1965 		    kmem64_overmap_size,
1966 		    &memlist, &cur);
1967 	}
1968 
1969 	/*
1970 	 * Add any extra memory after e_text we added to the phys_avail list
1971 	 * back to the old list.
1972 	 */
1973 	if (extra_etpg)
1974 		memlist_add(va_to_pa(extra_etva), mmu_ptob(extra_etpg),
1975 		    &memlist, &cur);
1976 	if (ndata_remain_sz >= MMU_PAGESIZE)
1977 		memlist_add(va_to_pa(nalloc_base),
1978 		    (uint64_t)ndata_remain_sz, &memlist, &cur);
1979 
1980 	/*
1981 	 * There isn't any bounds checking on the memlist area
1982 	 * so ensure it hasn't overgrown.
1983 	 */
1984 	if ((caddr_t)memlist > (caddr_t)memlist_end)
1985 		cmn_err(CE_PANIC, "startup: memlist size exceeded");
1986 
1987 	/*
1988 	 * The kernel removes the pages that were allocated for it from
1989 	 * the freelist, but we now have to find any -extra- pages that
1990 	 * the prom has allocated for it's own book-keeping, and remove
1991 	 * them from the freelist too. sigh.
1992 	 */
1993 	fix_prom_pages(phys_avail, cur);
1994 
1995 	ASSERT(phys_avail != NULL);
1996 	memlist_free_list(phys_avail);
1997 	phys_avail = cur;
1998 
1999 	/*
2000 	 * We're done with boot.  Just after this point in time, boot
2001 	 * gets unmapped, so we can no longer rely on its services.
2002 	 * Zero the bootops to indicate this fact.
2003 	 */
2004 	bootops = (struct bootops *)NULL;
2005 	BOOTOPS_GONE();
2006 }
2007 
2008 static void
2009 startup_vm(void)
2010 {
2011 	size_t	i;
2012 	struct segmap_crargs a;
2013 	struct segkpm_crargs b;
2014 
2015 	uint64_t avmem;
2016 	caddr_t va;
2017 	pgcnt_t	max_phys_segkp;
2018 	int	mnode;
2019 
2020 	extern int use_brk_lpg, use_stk_lpg;
2021 
2022 	/*
2023 	 * get prom's mappings, create hments for them and switch
2024 	 * to the kernel context.
2025 	 */
2026 	hat_kern_setup();
2027 
2028 	/*
2029 	 * Take over trap table
2030 	 */
2031 	setup_trap_table();
2032 
2033 	/*
2034 	 * Install the va>tte handler, so that the prom can handle
2035 	 * misses and understand the kernel table layout in case
2036 	 * we need call into the prom.
2037 	 */
2038 	install_va_to_tte();
2039 
2040 	/*
2041 	 * Set a flag to indicate that the tba has been taken over.
2042 	 */
2043 	tba_taken_over = 1;
2044 
2045 	/* initialize MMU primary context register */
2046 	mmu_init_kcontext();
2047 
2048 	/*
2049 	 * The boot cpu can now take interrupts, x-calls, x-traps
2050 	 */
2051 	CPUSET_ADD(cpu_ready_set, CPU->cpu_id);
2052 	CPU->cpu_flags |= (CPU_READY | CPU_ENABLE | CPU_EXISTS);
2053 
2054 	/*
2055 	 * Set a flag to tell write_scb_int() that it can access V_TBR_WR_ADDR.
2056 	 */
2057 	tbr_wr_addr_inited = 1;
2058 
2059 	/*
2060 	 * Initialize VM system, and map kernel address space.
2061 	 */
2062 	kvm_init();
2063 
2064 	/*
2065 	 * XXX4U: previously, we initialized and turned on
2066 	 * the caches at this point. But of course we have
2067 	 * nothing to do, as the prom has already done this
2068 	 * for us -- main memory must be E$able at all times.
2069 	 */
2070 
2071 	/*
2072 	 * If the following is true, someone has patched
2073 	 * phsymem to be less than the number of pages that
2074 	 * the system actually has.  Remove pages until system
2075 	 * memory is limited to the requested amount.  Since we
2076 	 * have allocated page structures for all pages, we
2077 	 * correct the amount of memory we want to remove
2078 	 * by the size of the memory used to hold page structures
2079 	 * for the non-used pages.
2080 	 */
2081 	if (physmem < npages) {
2082 		pgcnt_t diff, off;
2083 		struct page *pp;
2084 		struct seg kseg;
2085 
2086 		cmn_err(CE_WARN, "limiting physmem to %ld pages", physmem);
2087 
2088 		off = 0;
2089 		diff = npages - physmem;
2090 		diff -= mmu_btopr(diff * sizeof (struct page));
2091 		kseg.s_as = &kas;
2092 		while (diff--) {
2093 			pp = page_create_va(&unused_pages_vp, (offset_t)off,
2094 			    MMU_PAGESIZE, PG_WAIT | PG_EXCL,
2095 			    &kseg, (caddr_t)off);
2096 			if (pp == NULL)
2097 				cmn_err(CE_PANIC, "limited physmem too much!");
2098 			page_io_unlock(pp);
2099 			page_downgrade(pp);
2100 			availrmem--;
2101 			off += MMU_PAGESIZE;
2102 		}
2103 	}
2104 
2105 	/*
2106 	 * When printing memory, show the total as physmem less
2107 	 * that stolen by a debugger.
2108 	 */
2109 	cmn_err(CE_CONT, "?mem = %ldK (0x%lx000)\n",
2110 	    (ulong_t)(physinstalled) << (PAGESHIFT - 10),
2111 	    (ulong_t)(physinstalled) << (PAGESHIFT - 12));
2112 
2113 	avmem = (uint64_t)freemem << PAGESHIFT;
2114 	cmn_err(CE_CONT, "?avail mem = %lld\n", (unsigned long long)avmem);
2115 
2116 	/*
2117 	 * For small memory systems disable automatic large pages.
2118 	 */
2119 	if (physmem < privm_lpg_min_physmem) {
2120 		use_brk_lpg = 0;
2121 		use_stk_lpg = 0;
2122 	}
2123 
2124 	/*
2125 	 * Perform platform specific freelist processing
2126 	 */
2127 	if (&plat_freelist_process) {
2128 		for (mnode = 0; mnode < max_mem_nodes; mnode++)
2129 			if (mem_node_config[mnode].exists)
2130 				plat_freelist_process(mnode);
2131 	}
2132 
2133 	/*
2134 	 * Initialize the segkp segment type.  We position it
2135 	 * after the configured tables and buffers (whose end
2136 	 * is given by econtig) and before V_WKBASE_ADDR.
2137 	 * Also in this area is segkmap (size SEGMAPSIZE).
2138 	 */
2139 
2140 	/* XXX - cache alignment? */
2141 	va = (caddr_t)SEGKPBASE;
2142 	ASSERT(((uintptr_t)va & PAGEOFFSET) == 0);
2143 
2144 	max_phys_segkp = (physmem * 2);
2145 
2146 	if (segkpsize < btop(SEGKPMINSIZE) || segkpsize > btop(SEGKPMAXSIZE)) {
2147 		segkpsize = btop(SEGKPDEFSIZE);
2148 		cmn_err(CE_WARN, "Illegal value for segkpsize. "
2149 		    "segkpsize has been reset to %ld pages", segkpsize);
2150 	}
2151 
2152 	i = ptob(MIN(segkpsize, max_phys_segkp));
2153 
2154 	rw_enter(&kas.a_lock, RW_WRITER);
2155 	if (seg_attach(&kas, va, i, segkp) < 0)
2156 		cmn_err(CE_PANIC, "startup: cannot attach segkp");
2157 	if (segkp_create(segkp) != 0)
2158 		cmn_err(CE_PANIC, "startup: segkp_create failed");
2159 	rw_exit(&kas.a_lock);
2160 
2161 	/*
2162 	 * kpm segment
2163 	 */
2164 	segmap_kpm = kpm_enable &&
2165 		segmap_kpm && PAGESIZE == MAXBSIZE;
2166 
2167 	if (kpm_enable) {
2168 		rw_enter(&kas.a_lock, RW_WRITER);
2169 
2170 		/*
2171 		 * The segkpm virtual range range is larger than the
2172 		 * actual physical memory size and also covers gaps in
2173 		 * the physical address range for the following reasons:
2174 		 * . keep conversion between segkpm and physical addresses
2175 		 *   simple, cheap and unambiguous.
2176 		 * . avoid extension/shrink of the the segkpm in case of DR.
2177 		 * . avoid complexity for handling of virtual addressed
2178 		 *   caches, segkpm and the regular mapping scheme must be
2179 		 *   kept in sync wrt. the virtual color of mapped pages.
2180 		 * Any accesses to virtual segkpm ranges not backed by
2181 		 * physical memory will fall through the memseg pfn hash
2182 		 * and will be handled in segkpm_fault.
2183 		 * Additional kpm_size spaces needed for vac alias prevention.
2184 		 */
2185 		if (seg_attach(&kas, kpm_vbase, kpm_size * vac_colors,
2186 		    segkpm) < 0)
2187 			cmn_err(CE_PANIC, "cannot attach segkpm");
2188 
2189 		b.prot = PROT_READ | PROT_WRITE;
2190 		b.nvcolors = shm_alignment >> MMU_PAGESHIFT;
2191 
2192 		if (segkpm_create(segkpm, (caddr_t)&b) != 0)
2193 			panic("segkpm_create segkpm");
2194 
2195 		rw_exit(&kas.a_lock);
2196 
2197 		mach_kpm_init();
2198 	}
2199 
2200 	if (!segzio_fromheap) {
2201 		size_t size;
2202 		size_t physmem_b = mmu_ptob(physmem);
2203 
2204 		/* size is in bytes, segziosize is in pages */
2205 		if (segziosize == 0) {
2206 			size = physmem_b;
2207 		} else {
2208 			size = mmu_ptob(segziosize);
2209 		}
2210 
2211 		if (size < SEGZIOMINSIZE) {
2212 			size = SEGZIOMINSIZE;
2213 		} else if (size > SEGZIOMAXSIZE) {
2214 			size = SEGZIOMAXSIZE;
2215 			/*
2216 			 * On 64-bit x86, we only have 2TB of KVA.  This exists
2217 			 * for parity with x86.
2218 			 *
2219 			 * SEGZIOMAXSIZE is capped at 512gb so that segzio
2220 			 * doesn't consume all of KVA.  However, if we have a
2221 			 * system that has more thant 512gb of physical memory,
2222 			 * we can actually consume about half of the difference
2223 			 * between 512gb and the rest of the available physical
2224 			 * memory.
2225 			 */
2226 			if (physmem_b > SEGZIOMAXSIZE) {
2227 				size += (physmem_b - SEGZIOMAXSIZE) / 2;
2228 		}
2229 		}
2230 		segziosize = mmu_btop(roundup(size, MMU_PAGESIZE));
2231 		/* put the base of the ZIO segment after the kpm segment */
2232 		segzio_base = kpm_vbase + (kpm_size * vac_colors);
2233 		PRM_DEBUG(segziosize);
2234 		PRM_DEBUG(segzio_base);
2235 
2236 		/*
2237 		 * On some platforms, kvm_init is called after the kpm
2238 		 * sizes have been determined.  On SPARC, kvm_init is called
2239 		 * before, so we have to attach the kzioseg after kvm is
2240 		 * initialized, otherwise we'll try to allocate from the boot
2241 		 * area since the kernel heap hasn't yet been configured.
2242 		 */
2243 		rw_enter(&kas.a_lock, RW_WRITER);
2244 
2245 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2246 		    &kzioseg);
2247 		(void) segkmem_zio_create(&kzioseg);
2248 
2249 		/* create zio area covering new segment */
2250 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2251 
2252 		rw_exit(&kas.a_lock);
2253 	}
2254 
2255 
2256 	/*
2257 	 * Now create generic mapping segment.  This mapping
2258 	 * goes SEGMAPSIZE beyond SEGMAPBASE.  But if the total
2259 	 * virtual address is greater than the amount of free
2260 	 * memory that is available, then we trim back the
2261 	 * segment size to that amount
2262 	 */
2263 	va = (caddr_t)SEGMAPBASE;
2264 
2265 	/*
2266 	 * 1201049: segkmap base address must be MAXBSIZE aligned
2267 	 */
2268 	ASSERT(((uintptr_t)va & MAXBOFFSET) == 0);
2269 
2270 	/*
2271 	 * Set size of segmap to percentage of freemem at boot,
2272 	 * but stay within the allowable range
2273 	 * Note we take percentage  before converting from pages
2274 	 * to bytes to avoid an overflow on 32-bit kernels.
2275 	 */
2276 	i = mmu_ptob((freemem * segmap_percent) / 100);
2277 
2278 	if (i < MINMAPSIZE)
2279 		i = MINMAPSIZE;
2280 
2281 	if (i > MIN(SEGMAPSIZE, mmu_ptob(freemem)))
2282 		i = MIN(SEGMAPSIZE, mmu_ptob(freemem));
2283 
2284 	i &= MAXBMASK;	/* 1201049: segkmap size must be MAXBSIZE aligned */
2285 
2286 	rw_enter(&kas.a_lock, RW_WRITER);
2287 	if (seg_attach(&kas, va, i, segkmap) < 0)
2288 		cmn_err(CE_PANIC, "cannot attach segkmap");
2289 
2290 	a.prot = PROT_READ | PROT_WRITE;
2291 	a.shmsize = shm_alignment;
2292 	a.nfreelist = 0;	/* use segmap driver defaults */
2293 
2294 	if (segmap_create(segkmap, (caddr_t)&a) != 0)
2295 		panic("segmap_create segkmap");
2296 	rw_exit(&kas.a_lock);
2297 
2298 	segdev_init();
2299 }
2300 
2301 static void
2302 startup_end(void)
2303 {
2304 	if ((caddr_t)memlist > (caddr_t)memlist_end)
2305 		panic("memlist overflow 2");
2306 	memlist_free_block((caddr_t)memlist,
2307 	    ((caddr_t)memlist_end - (caddr_t)memlist));
2308 	memlist = NULL;
2309 
2310 	/* enable page_relocation since OBP is now done */
2311 	page_relocate_ready = 1;
2312 
2313 	/*
2314 	 * Perform tasks that get done after most of the VM
2315 	 * initialization has been done but before the clock
2316 	 * and other devices get started.
2317 	 */
2318 	kern_setup1();
2319 
2320 	/*
2321 	 * Intialize the VM arenas for allocating physically
2322 	 * contiguus memory chunk for interrupt queues snd
2323 	 * allocate/register boot cpu's queues, if any and
2324 	 * allocate dump buffer for sun4v systems to store
2325 	 * extra crash information during crash dump
2326 	 */
2327 	contig_mem_init();
2328 	mach_descrip_init();
2329 
2330 	if (cpu_intrq_setup(CPU)) {
2331 		cmn_err(CE_PANIC, "cpu%d: setup failed", CPU->cpu_id);
2332 	}
2333 	cpu_intrq_register(CPU);
2334 	mach_htraptrace_setup(CPU->cpu_id);
2335 	mach_htraptrace_configure(CPU->cpu_id);
2336 	mach_dump_buffer_init();
2337 
2338 	/*
2339 	 * Initialize interrupt related stuff
2340 	 */
2341 	cpu_intr_alloc(CPU, NINTR_THREADS);
2342 
2343 	(void) splzs();			/* allow hi clock ints but not zs */
2344 
2345 	/*
2346 	 * Initialize errors.
2347 	 */
2348 	error_init();
2349 
2350 	/*
2351 	 * Note that we may have already used kernel bcopy before this
2352 	 * point - but if you really care about this, adb the use_hw_*
2353 	 * variables to 0 before rebooting.
2354 	 */
2355 	mach_hw_copy_limit();
2356 
2357 	/*
2358 	 * Install the "real" preemption guards before DDI services
2359 	 * are available.
2360 	 */
2361 	(void) prom_set_preprom(kern_preprom);
2362 	(void) prom_set_postprom(kern_postprom);
2363 	CPU->cpu_m.mutex_ready = 1;
2364 
2365 	/*
2366 	 * Initialize segnf (kernel support for non-faulting loads).
2367 	 */
2368 	segnf_init();
2369 
2370 	/*
2371 	 * Configure the root devinfo node.
2372 	 */
2373 	configure();		/* set up devices */
2374 	mach_cpu_halt_idle();
2375 }
2376 
2377 
2378 void
2379 post_startup(void)
2380 {
2381 #ifdef	PTL1_PANIC_DEBUG
2382 	extern void init_ptl1_thread(void);
2383 #endif	/* PTL1_PANIC_DEBUG */
2384 	extern void abort_sequence_init(void);
2385 
2386 	/*
2387 	 * Set the system wide, processor-specific flags to be passed
2388 	 * to userland via the aux vector for performance hints and
2389 	 * instruction set extensions.
2390 	 */
2391 	bind_hwcap();
2392 
2393 	/*
2394 	 * Startup memory scrubber (if any)
2395 	 */
2396 	mach_memscrub();
2397 
2398 	/*
2399 	 * Allocate soft interrupt to handle abort sequence.
2400 	 */
2401 	abort_sequence_init();
2402 
2403 	/*
2404 	 * Configure the rest of the system.
2405 	 * Perform forceloading tasks for /etc/system.
2406 	 */
2407 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
2408 	/*
2409 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
2410 	 * ON4.0: This must be fixed or restated in /etc/systems.
2411 	 */
2412 	(void) modload("fs", "procfs");
2413 
2414 	/* load machine class specific drivers */
2415 	load_mach_drivers();
2416 
2417 	/* load platform specific drivers */
2418 	if (&load_platform_drivers)
2419 		load_platform_drivers();
2420 
2421 	/* load vis simulation module, if we are running w/fpu off */
2422 	if (!fpu_exists) {
2423 		if (modload("misc", "vis") == -1)
2424 			halt("Can't load vis");
2425 	}
2426 
2427 	mach_fpras();
2428 
2429 	maxmem = freemem;
2430 
2431 #ifdef	PTL1_PANIC_DEBUG
2432 	init_ptl1_thread();
2433 #endif	/* PTL1_PANIC_DEBUG */
2434 
2435 	if (&cif_init)
2436 		cif_init();
2437 }
2438 
2439 #ifdef	PTL1_PANIC_DEBUG
2440 int		ptl1_panic_test = 0;
2441 int		ptl1_panic_xc_one_test = 0;
2442 int		ptl1_panic_xc_all_test = 0;
2443 int		ptl1_panic_xt_one_test = 0;
2444 int		ptl1_panic_xt_all_test = 0;
2445 kthread_id_t	ptl1_thread_p = NULL;
2446 kcondvar_t	ptl1_cv;
2447 kmutex_t	ptl1_mutex;
2448 int		ptl1_recurse_count_threshold = 0x40;
2449 int		ptl1_recurse_trap_threshold = 0x3d;
2450 extern void	ptl1_recurse(int, int);
2451 extern void	ptl1_panic_xt(int, int);
2452 
2453 /*
2454  * Called once per second by timeout() to wake up
2455  * the ptl1_panic thread to see if it should cause
2456  * a trap to the ptl1_panic() code.
2457  */
2458 /* ARGSUSED */
2459 static void
2460 ptl1_wakeup(void *arg)
2461 {
2462 	mutex_enter(&ptl1_mutex);
2463 	cv_signal(&ptl1_cv);
2464 	mutex_exit(&ptl1_mutex);
2465 }
2466 
2467 /*
2468  * ptl1_panic cross call function:
2469  *     Needed because xc_one() and xc_some() can pass
2470  *	64 bit args but ptl1_recurse() expects ints.
2471  */
2472 static void
2473 ptl1_panic_xc(void)
2474 {
2475 	ptl1_recurse(ptl1_recurse_count_threshold,
2476 	    ptl1_recurse_trap_threshold);
2477 }
2478 
2479 /*
2480  * The ptl1 thread waits for a global flag to be set
2481  * and uses the recurse thresholds to set the stack depth
2482  * to cause a ptl1_panic() directly via a call to ptl1_recurse
2483  * or indirectly via the cross call and cross trap functions.
2484  *
2485  * This is useful testing stack overflows and normal
2486  * ptl1_panic() states with a know stack frame.
2487  *
2488  * ptl1_recurse() is an asm function in ptl1_panic.s that
2489  * sets the {In, Local, Out, and Global} registers to a
2490  * know state on the stack and just prior to causing a
2491  * test ptl1_panic trap.
2492  */
2493 static void
2494 ptl1_thread(void)
2495 {
2496 	mutex_enter(&ptl1_mutex);
2497 	while (ptl1_thread_p) {
2498 		cpuset_t	other_cpus;
2499 		int		cpu_id;
2500 		int		my_cpu_id;
2501 		int		target_cpu_id;
2502 		int		target_found;
2503 
2504 		if (ptl1_panic_test) {
2505 			ptl1_recurse(ptl1_recurse_count_threshold,
2506 			    ptl1_recurse_trap_threshold);
2507 		}
2508 
2509 		/*
2510 		 * Find potential targets for x-call and x-trap,
2511 		 * if any exist while preempt is disabled we
2512 		 * start a ptl1_panic if requested via a
2513 		 * globals.
2514 		 */
2515 		kpreempt_disable();
2516 		my_cpu_id = CPU->cpu_id;
2517 		other_cpus = cpu_ready_set;
2518 		CPUSET_DEL(other_cpus, CPU->cpu_id);
2519 		target_found = 0;
2520 		if (!CPUSET_ISNULL(other_cpus)) {
2521 			/*
2522 			 * Pick the first one
2523 			 */
2524 			for (cpu_id = 0; cpu_id < NCPU; cpu_id++) {
2525 				if (cpu_id == my_cpu_id)
2526 					continue;
2527 
2528 				if (CPU_XCALL_READY(cpu_id)) {
2529 					target_cpu_id = cpu_id;
2530 					target_found = 1;
2531 					break;
2532 				}
2533 			}
2534 			ASSERT(target_found);
2535 
2536 			if (ptl1_panic_xc_one_test) {
2537 				xc_one(target_cpu_id,
2538 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
2539 			}
2540 			if (ptl1_panic_xc_all_test) {
2541 				xc_some(other_cpus,
2542 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
2543 			}
2544 			if (ptl1_panic_xt_one_test) {
2545 				xt_one(target_cpu_id,
2546 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
2547 			}
2548 			if (ptl1_panic_xt_all_test) {
2549 				xt_some(other_cpus,
2550 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
2551 			}
2552 		}
2553 		kpreempt_enable();
2554 		(void) timeout(ptl1_wakeup, NULL, hz);
2555 		(void) cv_wait(&ptl1_cv, &ptl1_mutex);
2556 	}
2557 	mutex_exit(&ptl1_mutex);
2558 }
2559 
2560 /*
2561  * Called during early startup to create the ptl1_thread
2562  */
2563 void
2564 init_ptl1_thread(void)
2565 {
2566 	ptl1_thread_p = thread_create(NULL, 0, ptl1_thread, NULL, 0,
2567 	    &p0, TS_RUN, 0);
2568 }
2569 #endif	/* PTL1_PANIC_DEBUG */
2570 
2571 
2572 /*
2573  * Add to a memory list.
2574  * start = start of new memory segment
2575  * len = length of new memory segment in bytes
2576  * memlistp = pointer to array of available memory segment structures
2577  * curmemlistp = memory list to which to add segment.
2578  */
2579 static void
2580 memlist_add(uint64_t start, uint64_t len, struct memlist **memlistp,
2581 	struct memlist **curmemlistp)
2582 {
2583 	struct memlist *new;
2584 
2585 	new = *memlistp;
2586 	new->address = start;
2587 	new->size = len;
2588 	*memlistp = new + 1;
2589 
2590 	memlist_insert(new, curmemlistp);
2591 }
2592 
2593 /*
2594  * In the case of architectures that support dynamic addition of
2595  * memory at run-time there are two cases where memsegs need to
2596  * be initialized and added to the memseg list.
2597  * 1) memsegs that are constructed at startup.
2598  * 2) memsegs that are constructed at run-time on
2599  *    hot-plug capable architectures.
2600  * This code was originally part of the function kphysm_init().
2601  */
2602 
2603 static void
2604 memseg_list_add(struct memseg *memsegp)
2605 {
2606 	struct memseg **prev_memsegp;
2607 	pgcnt_t num;
2608 
2609 	/* insert in memseg list, decreasing number of pages order */
2610 
2611 	num = MSEG_NPAGES(memsegp);
2612 
2613 	for (prev_memsegp = &memsegs; *prev_memsegp;
2614 	    prev_memsegp = &((*prev_memsegp)->next)) {
2615 		if (num > MSEG_NPAGES(*prev_memsegp))
2616 			break;
2617 	}
2618 
2619 	memsegp->next = *prev_memsegp;
2620 	*prev_memsegp = memsegp;
2621 
2622 	if (kpm_enable) {
2623 		memsegp->nextpa = (memsegp->next) ?
2624 			va_to_pa(memsegp->next) : MSEG_NULLPTR_PA;
2625 
2626 		if (prev_memsegp != &memsegs) {
2627 			struct memseg *msp;
2628 			msp = (struct memseg *)((caddr_t)prev_memsegp -
2629 				offsetof(struct memseg, next));
2630 			msp->nextpa = va_to_pa(memsegp);
2631 		} else {
2632 			memsegspa = va_to_pa(memsegs);
2633 		}
2634 	}
2635 }
2636 
2637 /*
2638  * PSM add_physmem_cb(). US-II and newer processors have some
2639  * flavor of the prefetch capability implemented. We exploit
2640  * this capability for optimum performance.
2641  */
2642 #define	PREFETCH_BYTES	64
2643 
2644 void
2645 add_physmem_cb(page_t *pp, pfn_t pnum)
2646 {
2647 	extern void	 prefetch_page_w(void *);
2648 
2649 	pp->p_pagenum = pnum;
2650 
2651 	/*
2652 	 * Prefetch one more page_t into E$. To prevent future
2653 	 * mishaps with the sizeof(page_t) changing on us, we
2654 	 * catch this on debug kernels if we can't bring in the
2655 	 * entire hpage with 2 PREFETCH_BYTES reads. See
2656 	 * also, sun4u/cpu/cpu_module.c
2657 	 */
2658 	/*LINTED*/
2659 	ASSERT(sizeof (page_t) <= 2*PREFETCH_BYTES);
2660 	prefetch_page_w((char *)pp);
2661 }
2662 
2663 /*
2664  * kphysm_init() tackles the problem of initializing physical memory.
2665  * The old startup made some assumptions about the kernel living in
2666  * physically contiguous space which is no longer valid.
2667  */
2668 static void
2669 kphysm_init(page_t *pp, struct memseg *memsegp, pgcnt_t npages,
2670 	uintptr_t kpm_pp, pgcnt_t kpm_npages)
2671 {
2672 	struct memlist	*pmem;
2673 	struct memseg	*msp;
2674 	pfn_t		 base;
2675 	pgcnt_t		 num;
2676 	pfn_t		 lastseg_pages_end = 0;
2677 	pgcnt_t		 nelem_used = 0;
2678 
2679 	ASSERT(page_hash != NULL && page_hashsz != 0);
2680 
2681 	msp = memsegp;
2682 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
2683 
2684 		/*
2685 		 * Build the memsegs entry
2686 		 */
2687 		num = btop(pmem->size);
2688 		if (num > npages)
2689 			num = npages;
2690 		npages -= num;
2691 		base = btop(pmem->address);
2692 
2693 		msp->pages = pp;
2694 		msp->epages = pp + num;
2695 		msp->pages_base = base;
2696 		msp->pages_end = base + num;
2697 
2698 		if (kpm_enable) {
2699 			pfn_t pbase_a;
2700 			pfn_t pend_a;
2701 			pfn_t prev_pend_a;
2702 			pgcnt_t	nelem;
2703 
2704 			msp->pagespa = va_to_pa(pp);
2705 			msp->epagespa = va_to_pa(pp + num);
2706 			pbase_a = kpmptop(ptokpmp(base));
2707 			pend_a = kpmptop(ptokpmp(base + num - 1)) + kpmpnpgs;
2708 			nelem = ptokpmp(pend_a - pbase_a);
2709 			msp->kpm_nkpmpgs = nelem;
2710 			msp->kpm_pbase = pbase_a;
2711 			if (lastseg_pages_end) {
2712 				/*
2713 				 * Assume phys_avail is in ascending order
2714 				 * of physical addresses.
2715 				 */
2716 				ASSERT(base + num > lastseg_pages_end);
2717 				prev_pend_a = kpmptop(
2718 				    ptokpmp(lastseg_pages_end - 1)) + kpmpnpgs;
2719 
2720 				if (prev_pend_a > pbase_a) {
2721 					/*
2722 					 * Overlap, more than one memseg may
2723 					 * point to the same kpm_page range.
2724 					 */
2725 					if (kpm_smallpages == 0) {
2726 						msp->kpm_pages =
2727 						    (kpm_page_t *)kpm_pp - 1;
2728 						kpm_pp = (uintptr_t)
2729 							((kpm_page_t *)kpm_pp
2730 							+ nelem - 1);
2731 					} else {
2732 						msp->kpm_spages =
2733 						    (kpm_spage_t *)kpm_pp - 1;
2734 						kpm_pp = (uintptr_t)
2735 							((kpm_spage_t *)kpm_pp
2736 							+ nelem - 1);
2737 					}
2738 					nelem_used += nelem - 1;
2739 
2740 				} else {
2741 					if (kpm_smallpages == 0) {
2742 						msp->kpm_pages =
2743 						    (kpm_page_t *)kpm_pp;
2744 						kpm_pp = (uintptr_t)
2745 							((kpm_page_t *)kpm_pp
2746 							+ nelem);
2747 					} else {
2748 						msp->kpm_spages =
2749 						    (kpm_spage_t *)kpm_pp;
2750 						kpm_pp = (uintptr_t)
2751 							((kpm_spage_t *)
2752 							kpm_pp + nelem);
2753 					}
2754 					nelem_used += nelem;
2755 				}
2756 
2757 			} else {
2758 				if (kpm_smallpages == 0) {
2759 					msp->kpm_pages = (kpm_page_t *)kpm_pp;
2760 					kpm_pp = (uintptr_t)
2761 						((kpm_page_t *)kpm_pp + nelem);
2762 				} else {
2763 					msp->kpm_spages = (kpm_spage_t *)kpm_pp;
2764 					kpm_pp = (uintptr_t)
2765 						((kpm_spage_t *)kpm_pp + nelem);
2766 				}
2767 				nelem_used = nelem;
2768 			}
2769 
2770 			if (nelem_used > kpm_npages)
2771 				panic("kphysm_init: kpm_pp overflow\n");
2772 
2773 			msp->kpm_pagespa = va_to_pa(msp->kpm_pages);
2774 			lastseg_pages_end = msp->pages_end;
2775 		}
2776 
2777 		memseg_list_add(msp);
2778 
2779 		/*
2780 		 * add_physmem() initializes the PSM part of the page
2781 		 * struct by calling the PSM back with add_physmem_cb().
2782 		 * In addition it coalesces pages into larger pages as
2783 		 * it initializes them.
2784 		 */
2785 		add_physmem(pp, num, base);
2786 		pp += num;
2787 		msp++;
2788 	}
2789 
2790 	build_pfn_hash();
2791 }
2792 
2793 /*
2794  * Kernel VM initialization.
2795  * Assumptions about kernel address space ordering:
2796  *	(1) gap (user space)
2797  *	(2) kernel text
2798  *	(3) kernel data/bss
2799  *	(4) gap
2800  *	(5) kernel data structures
2801  *	(6) gap
2802  *	(7) debugger (optional)
2803  *	(8) monitor
2804  *	(9) gap (possibly null)
2805  *	(10) dvma
2806  *	(11) devices
2807  */
2808 static void
2809 kvm_init(void)
2810 {
2811 	/*
2812 	 * Put the kernel segments in kernel address space.
2813 	 */
2814 	rw_enter(&kas.a_lock, RW_WRITER);
2815 	as_avlinit(&kas);
2816 
2817 	(void) seg_attach(&kas, (caddr_t)KERNELBASE,
2818 	    (size_t)(e_moddata - KERNELBASE), &ktextseg);
2819 	(void) segkmem_create(&ktextseg);
2820 
2821 	(void) seg_attach(&kas, (caddr_t)(KERNELBASE + MMU_PAGESIZE4M),
2822 	    (size_t)(MMU_PAGESIZE4M), &ktexthole);
2823 	(void) segkmem_create(&ktexthole);
2824 
2825 	(void) seg_attach(&kas, (caddr_t)valloc_base,
2826 	    (size_t)(econtig32 - valloc_base), &kvalloc);
2827 	(void) segkmem_create(&kvalloc);
2828 
2829 	if (kmem64_base) {
2830 	    (void) seg_attach(&kas, (caddr_t)kmem64_base,
2831 		(size_t)(kmem64_end - kmem64_base), &kmem64);
2832 	    (void) segkmem_create(&kmem64);
2833 	}
2834 
2835 	/*
2836 	 * We're about to map out /boot.  This is the beginning of the
2837 	 * system resource management transition. We can no longer
2838 	 * call into /boot for I/O or memory allocations.
2839 	 */
2840 	(void) seg_attach(&kas, kernelheap, ekernelheap - kernelheap, &kvseg);
2841 	(void) segkmem_create(&kvseg);
2842 	hblk_alloc_dynamic = 1;
2843 
2844 	/*
2845 	 * we need to preallocate pages for DR operations before enabling large
2846 	 * page kernel heap because of memseg_remap_init() hat_unload() hack.
2847 	 */
2848 	memseg_remap_init();
2849 
2850 	/* at this point we are ready to use large page heap */
2851 	segkmem_heap_lp_init();
2852 
2853 	(void) seg_attach(&kas, (caddr_t)SYSBASE32, SYSLIMIT32 - SYSBASE32,
2854 	    &kvseg32);
2855 	(void) segkmem_create(&kvseg32);
2856 
2857 	/*
2858 	 * Create a segment for the debugger.
2859 	 */
2860 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
2861 	(void) segkmem_create(&kdebugseg);
2862 
2863 	rw_exit(&kas.a_lock);
2864 }
2865 
2866 char obp_tte_str[] =
2867 	"h# %x constant MMU_PAGESHIFT "
2868 	"h# %x constant TTE8K "
2869 	"h# %x constant SFHME_SIZE "
2870 	"h# %x constant SFHME_TTE "
2871 	"h# %x constant HMEBLK_TAG "
2872 	"h# %x constant HMEBLK_NEXT "
2873 	"h# %x constant HMEBLK_MISC "
2874 	"h# %x constant HMEBLK_HME1 "
2875 	"h# %x constant NHMENTS "
2876 	"h# %x constant HBLK_SZMASK "
2877 	"h# %x constant HBLK_RANGE_SHIFT "
2878 	"h# %x constant HMEBP_HBLK "
2879 	"h# %x constant HMEBUCKET_SIZE "
2880 	"h# %x constant HTAG_SFMMUPSZ "
2881 	"h# %x constant HTAG_REHASHSZ "
2882 	"h# %x constant mmu_hashcnt "
2883 	"h# %p constant uhme_hash "
2884 	"h# %p constant khme_hash "
2885 	"h# %x constant UHMEHASH_SZ "
2886 	"h# %x constant KHMEHASH_SZ "
2887 	"h# %p constant KCONTEXT "
2888 	"h# %p constant KHATID "
2889 	"h# %x constant ASI_MEM "
2890 
2891 	": PHYS-X@ ( phys -- data ) "
2892 	"   ASI_MEM spacex@ "
2893 	"; "
2894 
2895 	": PHYS-W@ ( phys -- data ) "
2896 	"   ASI_MEM spacew@ "
2897 	"; "
2898 
2899 	": PHYS-L@ ( phys -- data ) "
2900 	"   ASI_MEM spaceL@ "
2901 	"; "
2902 
2903 	": TTE_PAGE_SHIFT ( ttesz -- hmeshift ) "
2904 	"   3 * MMU_PAGESHIFT + "
2905 	"; "
2906 
2907 	": TTE_IS_VALID ( ttep -- flag ) "
2908 	"   PHYS-X@ 0< "
2909 	"; "
2910 
2911 	": HME_HASH_SHIFT ( ttesz -- hmeshift ) "
2912 	"   dup TTE8K =  if "
2913 	"      drop HBLK_RANGE_SHIFT "
2914 	"   else "
2915 	"      TTE_PAGE_SHIFT "
2916 	"   then "
2917 	"; "
2918 
2919 	": HME_HASH_BSPAGE ( addr hmeshift -- bspage ) "
2920 	"   tuck >> swap MMU_PAGESHIFT - << "
2921 	"; "
2922 
2923 	": HME_HASH_FUNCTION ( sfmmup addr hmeshift -- hmebp ) "
2924 	"   >> over xor swap                    ( hash sfmmup ) "
2925 	"   KHATID <>  if                       ( hash ) "
2926 	"      UHMEHASH_SZ and                  ( bucket ) "
2927 	"      HMEBUCKET_SIZE * uhme_hash +     ( hmebp ) "
2928 	"   else                                ( hash ) "
2929 	"      KHMEHASH_SZ and                  ( bucket ) "
2930 	"      HMEBUCKET_SIZE * khme_hash +     ( hmebp ) "
2931 	"   then                                ( hmebp ) "
2932 	"; "
2933 
2934 	": HME_HASH_TABLE_SEARCH "
2935 	"       ( sfmmup hmebp hblktag --  sfmmup null | sfmmup hmeblkp ) "
2936 	"   >r hmebp_hblk + phys-x@ begin ( sfmmup hmeblkp ) ( r: hblktag ) "
2937 	"      dup if   		( sfmmup hmeblkp ) ( r: hblktag ) "
2938 	"         dup hmeblk_tag + phys-x@ r@ = if ( sfmmup hmeblkp )	  "
2939 	"	     dup hmeblk_tag + 8 + phys-x@ 2 pick = if		  "
2940 	"		  true 	( sfmmup hmeblkp true ) ( r: hblktag )	  "
2941 	"	     else						  "
2942 	"	     	  hmeblk_next + phys-x@ false 			  "
2943 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
2944 	"	     then  						  "
2945 	"	  else							  "
2946 	"	     hmeblk_next + phys-x@ false 			  "
2947 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
2948 	"	  then 							  "
2949 	"      else							  "
2950 	"         true 							  "
2951 	"      then  							  "
2952 	"   until r> drop 						  "
2953 	"; "
2954 
2955 	": HME_HASH_TAG ( sfmmup rehash addr -- hblktag ) "
2956 	"   over HME_HASH_SHIFT HME_HASH_BSPAGE      ( sfmmup rehash bspage ) "
2957 	"   HTAG_REHASHSZ << or nip		     ( hblktag ) "
2958 	"; "
2959 
2960 	": HBLK_TO_TTEP ( hmeblkp addr -- ttep ) "
2961 	"   over HMEBLK_MISC + PHYS-L@ HBLK_SZMASK and  ( hmeblkp addr ttesz ) "
2962 	"   TTE8K =  if                            ( hmeblkp addr ) "
2963 	"      MMU_PAGESHIFT >> NHMENTS 1- and     ( hmeblkp hme-index ) "
2964 	"   else                                   ( hmeblkp addr ) "
2965 	"      drop 0                              ( hmeblkp 0 ) "
2966 	"   then                                   ( hmeblkp hme-index ) "
2967 	"   SFHME_SIZE * + HMEBLK_HME1 +           ( hmep ) "
2968 	"   SFHME_TTE +                            ( ttep ) "
2969 	"; "
2970 
2971 	": unix-tte ( addr cnum -- false | tte-data true ) "
2972 	"    KCONTEXT = if                   ( addr ) "
2973 	"	KHATID                       ( addr khatid ) "
2974 	"    else                            ( addr ) "
2975 	"       drop false exit              ( false ) "
2976 	"    then "
2977 	"      ( addr khatid ) "
2978 	"      mmu_hashcnt 1+ 1  do           ( addr sfmmup ) "
2979 	"         2dup swap i HME_HASH_SHIFT  "
2980 					"( addr sfmmup sfmmup addr hmeshift ) "
2981 	"         HME_HASH_FUNCTION           ( addr sfmmup hmebp ) "
2982 	"         over i 4 pick               "
2983 				"( addr sfmmup hmebp sfmmup rehash addr ) "
2984 	"         HME_HASH_TAG                ( addr sfmmup hmebp hblktag ) "
2985 	"         HME_HASH_TABLE_SEARCH       "
2986 					"( addr sfmmup { null | hmeblkp } ) "
2987 	"         ?dup  if                    ( addr sfmmup hmeblkp ) "
2988 	"            nip swap HBLK_TO_TTEP    ( ttep ) "
2989 	"            dup TTE_IS_VALID  if     ( valid-ttep ) "
2990 	"               PHYS-X@ true          ( tte-data true ) "
2991 	"            else                     ( invalid-tte ) "
2992 	"               drop false            ( false ) "
2993 	"            then                     ( false | tte-data true ) "
2994 	"            unloop exit              ( false | tte-data true ) "
2995 	"         then                        ( addr sfmmup ) "
2996 	"      loop                           ( addr sfmmup ) "
2997 	"      2drop false                    ( false ) "
2998 	"; "
2999 ;
3000 
3001 void
3002 create_va_to_tte(void)
3003 {
3004 	char *bp;
3005 	extern int khmehash_num, uhmehash_num;
3006 	extern struct hmehash_bucket *khme_hash, *uhme_hash;
3007 
3008 #define	OFFSET(type, field)	((uintptr_t)(&((type *)0)->field))
3009 
3010 	bp = (char *)kobj_zalloc(MMU_PAGESIZE, KM_SLEEP);
3011 
3012 	/*
3013 	 * Teach obp how to parse our sw ttes.
3014 	 */
3015 	(void) sprintf(bp, obp_tte_str,
3016 	    MMU_PAGESHIFT,
3017 	    TTE8K,
3018 	    sizeof (struct sf_hment),
3019 	    OFFSET(struct sf_hment, hme_tte),
3020 	    OFFSET(struct hme_blk, hblk_tag),
3021 	    OFFSET(struct hme_blk, hblk_nextpa),
3022 	    OFFSET(struct hme_blk, hblk_misc),
3023 	    OFFSET(struct hme_blk, hblk_hme),
3024 	    NHMENTS,
3025 	    HBLK_SZMASK,
3026 	    HBLK_RANGE_SHIFT,
3027 	    OFFSET(struct hmehash_bucket, hmeh_nextpa),
3028 	    sizeof (struct hmehash_bucket),
3029 	    HTAG_SFMMUPSZ,
3030 	    HTAG_REHASHSZ,
3031 	    mmu_hashcnt,
3032 	    (caddr_t)va_to_pa((caddr_t)uhme_hash),
3033 	    (caddr_t)va_to_pa((caddr_t)khme_hash),
3034 	    UHMEHASH_SZ,
3035 	    KHMEHASH_SZ,
3036 	    KCONTEXT,
3037 	    KHATID,
3038 	    ASI_MEM);
3039 	prom_interpret(bp, 0, 0, 0, 0, 0);
3040 
3041 	kobj_free(bp, MMU_PAGESIZE);
3042 }
3043 
3044 void
3045 install_va_to_tte(void)
3046 {
3047 	/*
3048 	 * advise prom that he can use unix-tte
3049 	 */
3050 	prom_interpret("' unix-tte is va>tte-data", 0, 0, 0, 0, 0);
3051 }
3052 
3053 /*
3054  * Here we add "device-type=console" for /os-io node, for currently
3055  * our kernel console output only supports displaying text and
3056  * performing cursor-positioning operations (through kernel framebuffer
3057  * driver) and it doesn't support other functionalities required for a
3058  * standard "display" device as specified in 1275 spec. The main missing
3059  * interface defined by the 1275 spec is "draw-logo".
3060  * also see the comments above prom_stdout_is_framebuffer().
3061  */
3062 static char *create_node =
3063 	"\" /\" find-device "
3064 	"new-device "
3065 	"\" os-io\" device-name "
3066 	"\" "OBP_DISPLAY_CONSOLE"\" device-type "
3067 	": cb-r/w  ( adr,len method$ -- #read/#written ) "
3068 	"   2>r swap 2 2r> ['] $callback  catch  if "
3069 	"      2drop 3drop 0 "
3070 	"   then "
3071 	"; "
3072 	": read ( adr,len -- #read ) "
3073 	"       \" read\" ['] cb-r/w catch  if  2drop 2drop -2 exit then "
3074 	"       ( retN ... ret1 N ) "
3075 	"       ?dup  if "
3076 	"               swap >r 1-  0  ?do  drop  loop  r> "
3077 	"       else "
3078 	"               -2 "
3079 	"       then "
3080 	";    "
3081 	": write ( adr,len -- #written ) "
3082 	"       \" write\" ['] cb-r/w catch  if  2drop 2drop 0 exit  then "
3083 	"       ( retN ... ret1 N ) "
3084 	"       ?dup  if "
3085 	"               swap >r 1-  0  ?do  drop  loop  r> "
3086 	"        else "
3087 	"               0 "
3088 	"       then "
3089 	"; "
3090 	": poll-tty ( -- ) ; "
3091 	": install-abort  ( -- )  ['] poll-tty d# 10 alarm ; "
3092 	": remove-abort ( -- )  ['] poll-tty 0 alarm ; "
3093 	": cb-give/take ( $method -- ) "
3094 	"       0 -rot ['] $callback catch  ?dup  if "
3095 	"               >r 2drop 2drop r> throw "
3096 	"       else "
3097 	"               0  ?do  drop  loop "
3098 	"       then "
3099 	"; "
3100 	": give ( -- )  \" exit-input\" cb-give/take ; "
3101 	": take ( -- )  \" enter-input\" cb-give/take ; "
3102 	": open ( -- ok? )  true ; "
3103 	": close ( -- ) ; "
3104 	"finish-device "
3105 	"device-end ";
3106 
3107 /*
3108  * Create the OBP input/output node (FCode serial driver).
3109  * It is needed for both USB console keyboard and for
3110  * the kernel terminal emulator.  It is too early to check for a
3111  * kernel console compatible framebuffer now, so we create this
3112  * so that we're ready if we need to enable kernel terminal emulation.
3113  *
3114  * When the USB software takes over the input device at the time
3115  * consconfig runs, OBP's stdin is redirected to this node.
3116  * Whenever the FORTH user interface is used after this switch,
3117  * the node will call back into the kernel for console input.
3118  * If a serial device such as ttya or a UART with a Type 5 keyboard
3119  * attached is used, OBP takes over the serial device when the system
3120  * goes to the debugger after the system is booted.  This sharing
3121  * of the relatively simple serial device is difficult but possible.
3122  * Sharing the USB host controller is impossible due its complexity.
3123  *
3124  * Similarly to USB keyboard input redirection, after consconfig_dacf
3125  * configures a kernel console framebuffer as the standard output
3126  * device, OBP's stdout is switched to to vector through the
3127  * /os-io node into the kernel terminal emulator.
3128  */
3129 static void
3130 startup_create_io_node(void)
3131 {
3132 	prom_interpret(create_node, 0, 0, 0, 0, 0);
3133 }
3134 
3135 
3136 static void
3137 do_prom_version_check(void)
3138 {
3139 	int i;
3140 	pnode_t node;
3141 	char buf[64];
3142 	static char drev[] = "Down-rev firmware detected%s\n"
3143 		"\tPlease upgrade to the following minimum version:\n"
3144 		"\t\t%s\n";
3145 
3146 	i = prom_version_check(buf, sizeof (buf), &node);
3147 
3148 	if (i == PROM_VER64_OK)
3149 		return;
3150 
3151 	if (i == PROM_VER64_UPGRADE) {
3152 		cmn_err(CE_WARN, drev, "", buf);
3153 
3154 #ifdef	DEBUG
3155 		prom_enter_mon();	/* Type 'go' to continue */
3156 		cmn_err(CE_WARN, "Booting with down-rev firmware\n");
3157 		return;
3158 #else
3159 		halt(0);
3160 #endif
3161 	}
3162 
3163 	/*
3164 	 * The other possibility is that this is a server running
3165 	 * good firmware, but down-rev firmware was detected on at
3166 	 * least one other cpu board. We just complain if we see
3167 	 * that.
3168 	 */
3169 	cmn_err(CE_WARN, drev, " on one or more CPU boards", buf);
3170 }
3171 
3172 static void
3173 kpm_init()
3174 {
3175 	kpm_pgshft = (kpm_smallpages == 0) ? MMU_PAGESHIFT4M : MMU_PAGESHIFT;
3176 	kpm_pgsz = 1ull << kpm_pgshft;
3177 	kpm_pgoff = kpm_pgsz - 1;
3178 	kpmp2pshft = kpm_pgshft - PAGESHIFT;
3179 	kpmpnpgs = 1 << kpmp2pshft;
3180 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
3181 }
3182 
3183 void
3184 kpm_npages_setup(int memblocks)
3185 {
3186 	/*
3187 	 * npages can be scattered in a maximum of 'memblocks'
3188 	 */
3189 	kpm_npages = ptokpmpr(npages) + memblocks;
3190 }
3191 
3192 /*
3193  * Must be defined in platform dependent code.
3194  */
3195 extern caddr_t modtext;
3196 extern size_t modtext_sz;
3197 extern caddr_t moddata;
3198 
3199 #define	HEAPTEXT_ARENA(addr)	\
3200 	((uintptr_t)(addr) < KERNELBASE + 2 * MMU_PAGESIZE4M ? 0 : \
3201 	(((uintptr_t)(addr) - HEAPTEXT_BASE) / \
3202 	(HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) + 1))
3203 
3204 #define	HEAPTEXT_OVERSIZED(addr)	\
3205 	((uintptr_t)(addr) >= HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE)
3206 
3207 vmem_t *texthole_source[HEAPTEXT_NARENAS];
3208 vmem_t *texthole_arena[HEAPTEXT_NARENAS];
3209 kmutex_t texthole_lock;
3210 
3211 char kern_bootargs[OBP_MAXPATHLEN];
3212 
3213 void
3214 kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
3215 {
3216 	uintptr_t addr, limit;
3217 
3218 	addr = HEAPTEXT_BASE;
3219 	limit = addr + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE;
3220 
3221 	/*
3222 	 * Before we initialize the text_arena, we want to punch holes in the
3223 	 * underlying heaptext_arena.  This guarantees that for any text
3224 	 * address we can find a text hole less than HEAPTEXT_MAPPED away.
3225 	 */
3226 	for (; addr + HEAPTEXT_UNMAPPED <= limit;
3227 	    addr += HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) {
3228 		(void) vmem_xalloc(heaptext_arena, HEAPTEXT_UNMAPPED, PAGESIZE,
3229 		    0, 0, (void *)addr, (void *)(addr + HEAPTEXT_UNMAPPED),
3230 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
3231 	}
3232 
3233 	/*
3234 	 * Allocate one page at the oversize to break up the text region
3235 	 * from the oversized region.
3236 	 */
3237 	(void) vmem_xalloc(heaptext_arena, PAGESIZE, PAGESIZE, 0, 0,
3238 	    (void *)limit, (void *)(limit + PAGESIZE),
3239 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
3240 
3241 	*text_arena = vmem_create("module_text", modtext_sz ? modtext : NULL,
3242 	    modtext_sz, sizeof (uintptr_t), segkmem_alloc, segkmem_free,
3243 	    heaptext_arena, 0, VM_SLEEP);
3244 	*data_arena = vmem_create("module_data", moddata, MODDATA, 1,
3245 	    segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
3246 }
3247 
3248 caddr_t
3249 kobj_text_alloc(vmem_t *arena, size_t size)
3250 {
3251 	caddr_t rval, better;
3252 
3253 	/*
3254 	 * First, try a sleeping allocation.
3255 	 */
3256 	rval = vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT);
3257 
3258 	if (size >= HEAPTEXT_MAPPED || !HEAPTEXT_OVERSIZED(rval))
3259 		return (rval);
3260 
3261 	/*
3262 	 * We didn't get the area that we wanted.  We're going to try to do an
3263 	 * allocation with explicit constraints.
3264 	 */
3265 	better = vmem_xalloc(arena, size, sizeof (uintptr_t), 0, 0, NULL,
3266 	    (void *)(HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE),
3267 	    VM_NOSLEEP | VM_BESTFIT);
3268 
3269 	if (better != NULL) {
3270 		/*
3271 		 * That worked.  Free our first attempt and return.
3272 		 */
3273 		vmem_free(arena, rval, size);
3274 		return (better);
3275 	}
3276 
3277 	/*
3278 	 * That didn't work; we'll have to return our first attempt.
3279 	 */
3280 	return (rval);
3281 }
3282 
3283 caddr_t
3284 kobj_texthole_alloc(caddr_t addr, size_t size)
3285 {
3286 	int arena = HEAPTEXT_ARENA(addr);
3287 	char c[30];
3288 	uintptr_t base;
3289 
3290 	if (HEAPTEXT_OVERSIZED(addr)) {
3291 		/*
3292 		 * If this is an oversized allocation, there is no text hole
3293 		 * available for it; return NULL.
3294 		 */
3295 		return (NULL);
3296 	}
3297 
3298 	mutex_enter(&texthole_lock);
3299 
3300 	if (texthole_arena[arena] == NULL) {
3301 		ASSERT(texthole_source[arena] == NULL);
3302 
3303 		if (arena == 0) {
3304 			texthole_source[0] = vmem_create("module_text_holesrc",
3305 			    (void *)(KERNELBASE + MMU_PAGESIZE4M),
3306 			    MMU_PAGESIZE4M, PAGESIZE, NULL, NULL, NULL,
3307 			    0, VM_SLEEP);
3308 		} else {
3309 			base = HEAPTEXT_BASE +
3310 			    (arena - 1) * (HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED);
3311 
3312 			(void) snprintf(c, sizeof (c),
3313 			    "heaptext_holesrc_%d", arena);
3314 
3315 			texthole_source[arena] = vmem_create(c, (void *)base,
3316 			    HEAPTEXT_UNMAPPED, PAGESIZE, NULL, NULL, NULL,
3317 			    0, VM_SLEEP);
3318 		}
3319 
3320 		(void) snprintf(c, sizeof (c), "heaptext_hole_%d", arena);
3321 
3322 		texthole_arena[arena] = vmem_create(c, NULL, 0,
3323 		    sizeof (uint32_t), segkmem_alloc_permanent, segkmem_free,
3324 		    texthole_source[arena], 0, VM_SLEEP);
3325 	}
3326 
3327 	mutex_exit(&texthole_lock);
3328 
3329 	ASSERT(texthole_arena[arena] != NULL);
3330 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
3331 	return (vmem_alloc(texthole_arena[arena], size,
3332 	    VM_BESTFIT | VM_NOSLEEP));
3333 }
3334 
3335 void
3336 kobj_texthole_free(caddr_t addr, size_t size)
3337 {
3338 	int arena = HEAPTEXT_ARENA(addr);
3339 
3340 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
3341 	ASSERT(texthole_arena[arena] != NULL);
3342 	vmem_free(texthole_arena[arena], addr, size);
3343 }
3344