xref: /onnv-gate/usr/src/uts/sun4/os/startup.c (revision 3764)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51253Slq150181  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211253Slq150181 
220Sstevel@tonic-gate /*
233446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/machsystm.h>
300Sstevel@tonic-gate #include <sys/archsystm.h>
310Sstevel@tonic-gate #include <sys/vm.h>
320Sstevel@tonic-gate #include <sys/cpu.h>
330Sstevel@tonic-gate #include <sys/atomic.h>
340Sstevel@tonic-gate #include <sys/reboot.h>
350Sstevel@tonic-gate #include <sys/kdi.h>
360Sstevel@tonic-gate #include <sys/bootconf.h>
370Sstevel@tonic-gate #include <sys/memlist_plat.h>
380Sstevel@tonic-gate #include <sys/memlist_impl.h>
390Sstevel@tonic-gate #include <sys/prom_plat.h>
400Sstevel@tonic-gate #include <sys/prom_isa.h>
410Sstevel@tonic-gate #include <sys/autoconf.h>
420Sstevel@tonic-gate #include <sys/intreg.h>
430Sstevel@tonic-gate #include <sys/ivintr.h>
440Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
450Sstevel@tonic-gate #include <sys/iommutsb.h>
460Sstevel@tonic-gate #include <vm/vm_dep.h>
470Sstevel@tonic-gate #include <vm/seg_dev.h>
480Sstevel@tonic-gate #include <vm/seg_kmem.h>
490Sstevel@tonic-gate #include <vm/seg_kpm.h>
500Sstevel@tonic-gate #include <vm/seg_map.h>
510Sstevel@tonic-gate #include <vm/seg_kp.h>
520Sstevel@tonic-gate #include <sys/sysconf.h>
530Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
540Sstevel@tonic-gate #include <sys/kobj.h>
550Sstevel@tonic-gate #include <sys/sun4asi.h>
560Sstevel@tonic-gate #include <sys/clconf.h>
570Sstevel@tonic-gate #include <sys/platform_module.h>
580Sstevel@tonic-gate #include <sys/panic.h>
590Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
600Sstevel@tonic-gate #include <sys/clock.h>
610Sstevel@tonic-gate #include <sys/cmn_err.h>
620Sstevel@tonic-gate #include <sys/promif.h>
630Sstevel@tonic-gate #include <sys/prom_debug.h>
640Sstevel@tonic-gate #include <sys/traptrace.h>
650Sstevel@tonic-gate #include <sys/memnode.h>
660Sstevel@tonic-gate #include <sys/mem_cage.h>
672241Shuah #include <sys/mmu.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate extern void setup_trap_table(void);
700Sstevel@tonic-gate extern void cpu_intrq_setup(struct cpu *);
710Sstevel@tonic-gate extern void cpu_intrq_register(struct cpu *);
720Sstevel@tonic-gate extern void contig_mem_init(void);
730Sstevel@tonic-gate extern void mach_dump_buffer_init(void);
740Sstevel@tonic-gate extern void mach_descrip_init(void);
751991Sheppo extern void mach_descrip_startup_fini(void);
760Sstevel@tonic-gate extern void mach_memscrub(void);
770Sstevel@tonic-gate extern void mach_fpras(void);
780Sstevel@tonic-gate extern void mach_cpu_halt_idle(void);
790Sstevel@tonic-gate extern void mach_hw_copy_limit(void);
801991Sheppo extern void load_mach_drivers(void);
810Sstevel@tonic-gate extern void load_tod_module(void);
820Sstevel@tonic-gate #pragma weak load_tod_module
830Sstevel@tonic-gate 
840Sstevel@tonic-gate extern int ndata_alloc_mmfsa(struct memlist *ndata);
850Sstevel@tonic-gate #pragma weak ndata_alloc_mmfsa
860Sstevel@tonic-gate 
871991Sheppo extern void cif_init(void);
881991Sheppo #pragma weak cif_init
891991Sheppo 
900Sstevel@tonic-gate extern void parse_idprom(void);
910Sstevel@tonic-gate extern void add_vx_handler(char *, int, void (*)(cell_t *));
920Sstevel@tonic-gate extern void mem_config_init(void);
930Sstevel@tonic-gate extern void memseg_remap_init(void);
940Sstevel@tonic-gate 
952296Sae112802 extern void mach_kpm_init(void);
962296Sae112802 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * External Data:
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate extern int vac_size;	/* cache size in bytes */
1010Sstevel@tonic-gate extern uint_t vac_mask;	/* VAC alignment consistency mask */
1020Sstevel@tonic-gate extern uint_t vac_colors;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * Global Data Definitions:
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * XXX - Don't port this to new architectures
1100Sstevel@tonic-gate  * A 3rd party volume manager driver (vxdm) depends on the symbol romp.
1110Sstevel@tonic-gate  * 'romp' has no use with a prom with an IEEE 1275 client interface.
1120Sstevel@tonic-gate  * The driver doesn't use the value, but it depends on the symbol.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate void *romp;		/* veritas driver won't load without romp 4154976 */
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate pgcnt_t physmem = 0;	/* memory size in pages, patch if you want less */
1190Sstevel@tonic-gate pgcnt_t segkpsize =
1200Sstevel@tonic-gate     btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
1210Sstevel@tonic-gate uint_t segmap_percent = 12; /* Size of segmap segment */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate int use_cache = 1;		/* cache not reliable (605 bugs) with MP */
1240Sstevel@tonic-gate int vac_copyback = 1;
1250Sstevel@tonic-gate char *cache_mode = NULL;
1260Sstevel@tonic-gate int use_mix = 1;
1270Sstevel@tonic-gate int prom_debug = 0;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate struct bootops *bootops = 0;	/* passed in from boot in %o2 */
1300Sstevel@tonic-gate caddr_t boot_tba;		/* %tba at boot - used by kmdb */
1310Sstevel@tonic-gate uint_t	tba_taken_over = 0;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate caddr_t s_text;			/* start of kernel text segment */
1340Sstevel@tonic-gate caddr_t e_text;			/* end of kernel text segment */
1350Sstevel@tonic-gate caddr_t s_data;			/* start of kernel data segment */
1360Sstevel@tonic-gate caddr_t e_data;			/* end of kernel data segment */
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate caddr_t modtext;		/* beginning of module text */
1390Sstevel@tonic-gate size_t	modtext_sz;		/* size of module text */
1400Sstevel@tonic-gate caddr_t moddata;		/* beginning of module data reserve */
1410Sstevel@tonic-gate caddr_t e_moddata;		/* end of module data reserve */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * End of first block of contiguous kernel in 32-bit virtual address space
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate caddr_t		econtig32;	/* end of first blk of contiguous kernel */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate caddr_t		ncbase;		/* beginning of non-cached segment */
1490Sstevel@tonic-gate caddr_t		ncend;		/* end of non-cached segment */
1500Sstevel@tonic-gate caddr_t		sdata;		/* beginning of data segment */
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate caddr_t		extra_etva;	/* beginning of unused nucleus text */
1530Sstevel@tonic-gate pgcnt_t		extra_etpg;	/* number of pages of unused nucleus text */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate size_t	ndata_remain_sz;	/* bytes from end of data to 4MB boundary */
1560Sstevel@tonic-gate caddr_t	nalloc_base;		/* beginning of nucleus allocation */
1570Sstevel@tonic-gate caddr_t nalloc_end;		/* end of nucleus allocatable memory */
1580Sstevel@tonic-gate caddr_t valloc_base;		/* beginning of kvalloc segment	*/
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate caddr_t kmem64_base;		/* base of kernel mem segment in 64-bit space */
1610Sstevel@tonic-gate caddr_t kmem64_end;		/* end of kernel mem segment in 64-bit space */
162*3764Sdp78419 caddr_t kmem64_aligned_end;	/* end of large page, overmaps 64-bit space */
163*3764Sdp78419 int	kmem64_alignsize;	/* page size for mem segment in 64-bit space */
164*3764Sdp78419 int	kmem64_szc;		/* page size code */
165*3764Sdp78419 uint64_t kmem64_pabase = (uint64_t)-1;	/* physical address of kmem64_base */
1660Sstevel@tonic-gate 
1672296Sae112802 uintptr_t shm_alignment;	/* VAC address consistency modulus */
1680Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
1690Sstevel@tonic-gate struct memlist *phys_avail;	/* Available (unreserved) physical memory */
1700Sstevel@tonic-gate struct memlist *virt_avail;	/* Available (unmapped?) virtual memory */
1710Sstevel@tonic-gate struct memlist ndata;		/* memlist of nucleus allocatable memory */
1720Sstevel@tonic-gate int memexp_flag;		/* memory expansion card flag */
1730Sstevel@tonic-gate uint64_t ecache_flushaddr;	/* physical address used for flushing E$ */
1740Sstevel@tonic-gate pgcnt_t obp_pages;		/* Physical pages used by OBP */
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate  * VM data structures
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
1800Sstevel@tonic-gate struct page *pp_base;		/* Base of system page struct array */
1810Sstevel@tonic-gate size_t pp_sz;			/* Size in bytes of page struct array */
1820Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
1830Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
1840Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
1850Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
1860Sstevel@tonic-gate struct seg ktexthole;		/* Segment used for nucleus text hole */
1870Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
1880Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
1890Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate uintptr_t kpm_pp_base;		/* Base of system kpm_page array */
1920Sstevel@tonic-gate size_t	kpm_pp_sz;		/* Size of system kpm_page array */
1930Sstevel@tonic-gate pgcnt_t	kpm_npages;		/* How many kpm pages are managed */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
1960Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
1970Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
1980Sstevel@tonic-gate 
1993290Sjohansen int segzio_fromheap = 0;	/* zio allocations occur from heap */
2003290Sjohansen caddr_t segzio_base;		/* Base address of segzio */
2013290Sjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
2023290Sjohansen 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * debugger pages (if allocated)
2050Sstevel@tonic-gate  */
2060Sstevel@tonic-gate struct vnode kdebugvp;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2093446Smrj  * VA range available to the debugger
2103446Smrj  */
2113446Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
2123446Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
2133446Smrj 
2143446Smrj /*
2150Sstevel@tonic-gate  * Segment for relocated kernel structures in 64-bit large RAM kernels
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate struct seg kmem64;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate struct memseg *memseg_base;
2200Sstevel@tonic-gate size_t memseg_sz;		/* Used to translate a va to page */
2210Sstevel@tonic-gate struct vnode unused_pages_vp;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  * VM data structures allocated early during boot.
2250Sstevel@tonic-gate  */
2260Sstevel@tonic-gate size_t pagehash_sz;
2270Sstevel@tonic-gate uint64_t memlist_sz;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate char tbr_wr_addr_inited = 0;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate  * Static Routines:
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate static void memlist_add(uint64_t, uint64_t, struct memlist **,
2360Sstevel@tonic-gate 	struct memlist **);
2370Sstevel@tonic-gate static void kphysm_init(page_t *, struct memseg *, pgcnt_t, uintptr_t,
2380Sstevel@tonic-gate 	pgcnt_t);
2390Sstevel@tonic-gate static void kvm_init(void);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static void startup_init(void);
2420Sstevel@tonic-gate static void startup_memlist(void);
2430Sstevel@tonic-gate static void startup_modules(void);
2440Sstevel@tonic-gate static void startup_bop_gone(void);
2450Sstevel@tonic-gate static void startup_vm(void);
2460Sstevel@tonic-gate static void startup_end(void);
2470Sstevel@tonic-gate static void setup_cage_params(void);
2481253Slq150181 static void startup_create_io_node(void);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate static pgcnt_t npages;
2510Sstevel@tonic-gate static struct memlist *memlist;
2520Sstevel@tonic-gate void *memlist_end;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate static pgcnt_t bop_alloc_pages;
2550Sstevel@tonic-gate static caddr_t hblk_base;
2560Sstevel@tonic-gate uint_t hblk_alloc_dynamic = 0;
2570Sstevel@tonic-gate uint_t hblk1_min = H1MIN;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate /*
2610Sstevel@tonic-gate  * Hooks for unsupported platforms and down-rev firmware
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate int iam_positron(void);
2640Sstevel@tonic-gate #pragma weak iam_positron
2650Sstevel@tonic-gate static void do_prom_version_check(void);
2660Sstevel@tonic-gate static void kpm_init(void);
2670Sstevel@tonic-gate static void kpm_npages_setup(int);
2680Sstevel@tonic-gate static void kpm_memseg_init(void);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  * After receiving a thermal interrupt, this is the number of seconds
2720Sstevel@tonic-gate  * to delay before shutting off the system, assuming
2730Sstevel@tonic-gate  * shutdown fails.  Use /etc/system to change the delay if this isn't
2740Sstevel@tonic-gate  * large enough.
2750Sstevel@tonic-gate  */
2760Sstevel@tonic-gate int thermal_powerdown_delay = 1200;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Used to hold off page relocations into the cage until OBP has completed
2800Sstevel@tonic-gate  * its boot-time handoff of its resources to the kernel.
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate int page_relocate_ready = 0;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate #ifdef  DEBUGGING_MEM
2880Sstevel@tonic-gate static int debugging_mem;
2890Sstevel@tonic-gate static void
2900Sstevel@tonic-gate printmemlist(char *title, struct memlist *list)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	if (!debugging_mem)
2930Sstevel@tonic-gate 		return;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	printf("%s\n", title);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	while (list) {
2980Sstevel@tonic-gate 		prom_printf("\taddr = 0x%x %8x, size = 0x%x %8x\n",
2990Sstevel@tonic-gate 		    (uint32_t)(list->address >> 32), (uint32_t)list->address,
3000Sstevel@tonic-gate 		    (uint32_t)(list->size >> 32), (uint32_t)(list->size));
3010Sstevel@tonic-gate 		list = list->next;
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate void
3060Sstevel@tonic-gate printmemseg(struct memseg *memseg)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	if (!debugging_mem)
3090Sstevel@tonic-gate 		return;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	printf("memseg\n");
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	while (memseg) {
3140Sstevel@tonic-gate 		prom_printf("\tpage = 0x%p, epage = 0x%p, "
3150Sstevel@tonic-gate 		    "pfn = 0x%x, epfn = 0x%x\n",
3160Sstevel@tonic-gate 		    memseg->pages, memseg->epages,
3170Sstevel@tonic-gate 		    memseg->pages_base, memseg->pages_end);
3180Sstevel@tonic-gate 		memseg = memseg->next;
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate #define	debug_pause(str)	halt((str))
3230Sstevel@tonic-gate #define	MPRINTF(str)		if (debugging_mem) prom_printf((str))
3240Sstevel@tonic-gate #define	MPRINTF1(str, a)	if (debugging_mem) prom_printf((str), (a))
3250Sstevel@tonic-gate #define	MPRINTF2(str, a, b)	if (debugging_mem) prom_printf((str), (a), (b))
3260Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c) \
3270Sstevel@tonic-gate 	if (debugging_mem) prom_printf((str), (a), (b), (c))
3280Sstevel@tonic-gate #else	/* DEBUGGING_MEM */
3290Sstevel@tonic-gate #define	MPRINTF(str)
3300Sstevel@tonic-gate #define	MPRINTF1(str, a)
3310Sstevel@tonic-gate #define	MPRINTF2(str, a, b)
3320Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c)
3330Sstevel@tonic-gate #endif	/* DEBUGGING_MEM */
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate /* Simple message to indicate that the bootops pointer has been zeroed */
3360Sstevel@tonic-gate #ifdef DEBUG
3370Sstevel@tonic-gate static int bootops_gone_on = 0;
3380Sstevel@tonic-gate #define	BOOTOPS_GONE() \
3390Sstevel@tonic-gate 	if (bootops_gone_on) \
3400Sstevel@tonic-gate 		prom_printf("The bootops vec is zeroed now!\n");
3410Sstevel@tonic-gate #else
3420Sstevel@tonic-gate #define	BOOTOPS_GONE()
3430Sstevel@tonic-gate #endif /* DEBUG */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * Monitor pages may not be where this says they are.
3470Sstevel@tonic-gate  * and the debugger may not be there either.
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  * Note that 'pages' here are *physical* pages, which are 8k on sun4u.
3500Sstevel@tonic-gate  *
3510Sstevel@tonic-gate  *                        Physical memory layout
3520Sstevel@tonic-gate  *                     (not necessarily contiguous)
3530Sstevel@tonic-gate  *                       (THIS IS SOMEWHAT WRONG)
3540Sstevel@tonic-gate  *                       /-----------------------\
3550Sstevel@tonic-gate  *                       |       monitor pages   |
3560Sstevel@tonic-gate  *             availmem -|-----------------------|
3570Sstevel@tonic-gate  *                       |                       |
3580Sstevel@tonic-gate  *                       |       page pool       |
3590Sstevel@tonic-gate  *                       |                       |
3600Sstevel@tonic-gate  *                       |-----------------------|
3610Sstevel@tonic-gate  *                       |   configured tables   |
3620Sstevel@tonic-gate  *                       |       buffers         |
3630Sstevel@tonic-gate  *            firstaddr -|-----------------------|
3640Sstevel@tonic-gate  *                       |   hat data structures |
3650Sstevel@tonic-gate  *                       |-----------------------|
3660Sstevel@tonic-gate  *                       |    kernel data, bss   |
3670Sstevel@tonic-gate  *                       |-----------------------|
3680Sstevel@tonic-gate  *                       |    interrupt stack    |
3690Sstevel@tonic-gate  *                       |-----------------------|
3700Sstevel@tonic-gate  *                       |    kernel text (RO)   |
3710Sstevel@tonic-gate  *                       |-----------------------|
3720Sstevel@tonic-gate  *                       |    trap table (4k)    |
3730Sstevel@tonic-gate  *                       |-----------------------|
3740Sstevel@tonic-gate  *               page 1  |      panicbuf         |
3750Sstevel@tonic-gate  *                       |-----------------------|
3760Sstevel@tonic-gate  *               page 0  |       reclaimed       |
3770Sstevel@tonic-gate  *                       |_______________________|
3780Sstevel@tonic-gate  *
3790Sstevel@tonic-gate  *
3800Sstevel@tonic-gate  *
3810Sstevel@tonic-gate  *                    Kernel's Virtual Memory Layout.
3820Sstevel@tonic-gate  *                       /-----------------------\
3830Sstevel@tonic-gate  * 0xFFFFFFFF.FFFFFFFF  -|                       |-
3840Sstevel@tonic-gate  *                       |   OBP's virtual page  |
3850Sstevel@tonic-gate  *                       |        tables         |
3860Sstevel@tonic-gate  * 0xFFFFFFFC.00000000  -|-----------------------|-
3870Sstevel@tonic-gate  *                       :                       :
3880Sstevel@tonic-gate  *                       :                       :
3893290Sjohansen  *                      -|-----------------------|-
3903290Sjohansen  *                       |       segzio          | (base and size vary)
3910Sstevel@tonic-gate  * 0xFFFFFE00.00000000  -|-----------------------|-
3920Sstevel@tonic-gate  *                       |                       |  Ultrasparc I/II support
3930Sstevel@tonic-gate  *                       |    segkpm segment     |  up to 2TB of physical
3940Sstevel@tonic-gate  *                       | (64-bit kernel ONLY)  |  memory, VAC has 2 colors
3950Sstevel@tonic-gate  *                       |                       |
3960Sstevel@tonic-gate  * 0xFFFFFA00.00000000  -|-----------------------|- 2TB segkpm alignment
3970Sstevel@tonic-gate  *                       :                       :
3980Sstevel@tonic-gate  *                       :                       :
3990Sstevel@tonic-gate  * 0xFFFFF810.00000000  -|-----------------------|- hole_end
4000Sstevel@tonic-gate  *                       |                       |      ^
4010Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
4020Sstevel@tonic-gate  *                       | bug requires an extra |      |
4030Sstevel@tonic-gate  *                       | 4 GB of space between |      |
4040Sstevel@tonic-gate  *                       |   hole and used RAM   |	|
4050Sstevel@tonic-gate  *                       |                       |      |
4060Sstevel@tonic-gate  * 0xFFFFF800.00000000  -|-----------------------|-     |
4070Sstevel@tonic-gate  *                       |                       |      |
4080Sstevel@tonic-gate  *                       | Virtual Address Hole  |   UltraSPARC
4090Sstevel@tonic-gate  *                       |  on UltraSPARC I/II   |  I/II * ONLY *
4100Sstevel@tonic-gate  *                       |                       |      |
4110Sstevel@tonic-gate  * 0x00000800.00000000  -|-----------------------|-     |
4120Sstevel@tonic-gate  *                       |                       |      |
4130Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
4140Sstevel@tonic-gate  *                       | bug requires an extra |      |
4150Sstevel@tonic-gate  *                       | 4 GB of space between |      |
4160Sstevel@tonic-gate  *                       |   hole and used RAM   |      |
4170Sstevel@tonic-gate  *                       |                       |      v
4180Sstevel@tonic-gate  * 0x000007FF.00000000  -|-----------------------|- hole_start -----
4190Sstevel@tonic-gate  *                       :                       :		   ^
4200Sstevel@tonic-gate  *                       :                       :		   |
421*3764Sdp78419  * 0x00000XXX.XXX00000  -|-----------------------|- kmem64_	   |
422*3764Sdp78419  *                       | overmapped area       |   alignend_end  |
423*3764Sdp78419  *                       | (kmem64_alignsize     |		   |
424*3764Sdp78419  *                       |  boundary)            |		   |
4250Sstevel@tonic-gate  * 0x00000XXX.XXXXXXXX  -|-----------------------|- kmem64_end	   |
4260Sstevel@tonic-gate  *                       |                       |		   |
4270Sstevel@tonic-gate  *                       |   64-bit kernel ONLY  |		   |
4280Sstevel@tonic-gate  *                       |                       |		   |
4290Sstevel@tonic-gate  *                       |    kmem64 segment     |		   |
4300Sstevel@tonic-gate  *                       |                       |		   |
4310Sstevel@tonic-gate  *                       | (Relocated extra HME  |	     Approximately
4320Sstevel@tonic-gate  *                       |   block allocations,  |	    1 TB of virtual
4330Sstevel@tonic-gate  *                       |   memnode freelists,  |	     address space
4340Sstevel@tonic-gate  *                       |    HME hash buckets,  |		   |
4350Sstevel@tonic-gate  *                       | mml_table, kpmp_table,|		   |
4360Sstevel@tonic-gate  *                       |  page_t array and     |		   |
4370Sstevel@tonic-gate  *                       |  hashblock pool to    |		   |
4380Sstevel@tonic-gate  *                       |   avoid hard-coded    |		   |
4390Sstevel@tonic-gate  *                       |     32-bit vaddr      |		   |
4400Sstevel@tonic-gate  *                       |     limitations)      |		   |
4410Sstevel@tonic-gate  *                       |                       |		   v
4420Sstevel@tonic-gate  * 0x00000700.00000000  -|-----------------------|- SYSLIMIT (kmem64_base)
4430Sstevel@tonic-gate  *                       |                       |
4440Sstevel@tonic-gate  *                       |  segkmem segment      | (SYSLIMIT - SYSBASE = 4TB)
4450Sstevel@tonic-gate  *                       |                       |
4460Sstevel@tonic-gate  * 0x00000300.00000000  -|-----------------------|- SYSBASE
4470Sstevel@tonic-gate  *                       :                       :
4480Sstevel@tonic-gate  *                       :                       :
4490Sstevel@tonic-gate  *                      -|-----------------------|-
4500Sstevel@tonic-gate  *                       |                       |
4510Sstevel@tonic-gate  *                       |  segmap segment       |   SEGMAPSIZE (1/8th physmem,
4520Sstevel@tonic-gate  *                       |                       |               256G MAX)
4530Sstevel@tonic-gate  * 0x000002a7.50000000  -|-----------------------|- SEGMAPBASE
4540Sstevel@tonic-gate  *                       :                       :
4550Sstevel@tonic-gate  *                       :                       :
4560Sstevel@tonic-gate  *                      -|-----------------------|-
4570Sstevel@tonic-gate  *                       |                       |
4580Sstevel@tonic-gate  *                       |       segkp           |    SEGKPSIZE (2GB)
4590Sstevel@tonic-gate  *                       |                       |
4600Sstevel@tonic-gate  *                       |                       |
4610Sstevel@tonic-gate  * 0x000002a1.00000000  -|-----------------------|- SEGKPBASE
4620Sstevel@tonic-gate  *                       |                       |
4630Sstevel@tonic-gate  * 0x000002a0.00000000  -|-----------------------|- MEMSCRUBBASE
4640Sstevel@tonic-gate  *                       |                       |       (SEGKPBASE - 0x400000)
4650Sstevel@tonic-gate  * 0x0000029F.FFE00000  -|-----------------------|- ARGSBASE
4660Sstevel@tonic-gate  *                       |                       |       (MEMSCRUBBASE - NCARGS)
4670Sstevel@tonic-gate  * 0x0000029F.FFD80000  -|-----------------------|- PPMAPBASE
4680Sstevel@tonic-gate  *                       |                       |       (ARGSBASE - PPMAPSIZE)
4690Sstevel@tonic-gate  * 0x0000029F.FFD00000  -|-----------------------|- PPMAP_FAST_BASE
4700Sstevel@tonic-gate  *                       |                       |
4710Sstevel@tonic-gate  * 0x0000029F.FF980000  -|-----------------------|- PIOMAPBASE
4720Sstevel@tonic-gate  *                       |                       |
4730Sstevel@tonic-gate  * 0x0000029F.FF580000  -|-----------------------|- NARG_BASE
4740Sstevel@tonic-gate  *                       :                       :
4750Sstevel@tonic-gate  *                       :                       :
4760Sstevel@tonic-gate  * 0x00000000.FFFFFFFF  -|-----------------------|- OFW_END_ADDR
4770Sstevel@tonic-gate  *                       |                       |
4780Sstevel@tonic-gate  *                       |         OBP           |
4790Sstevel@tonic-gate  *                       |                       |
4800Sstevel@tonic-gate  * 0x00000000.F0000000  -|-----------------------|- OFW_START_ADDR
4810Sstevel@tonic-gate  *                       |         kmdb          |
4820Sstevel@tonic-gate  * 0x00000000.EDD00000  -|-----------------------|- SEGDEBUGBASE
4830Sstevel@tonic-gate  *                       :                       :
4840Sstevel@tonic-gate  *                       :                       :
4850Sstevel@tonic-gate  * 0x00000000.7c000000  -|-----------------------|- SYSLIMIT32
4860Sstevel@tonic-gate  *                       |                       |
4870Sstevel@tonic-gate  *                       |  segkmem32 segment    | (SYSLIMIT32 - SYSBASE32 =
4880Sstevel@tonic-gate  *                       |                       |    ~64MB)
4890Sstevel@tonic-gate  * 0x00000000.78002000  -|-----------------------|
4900Sstevel@tonic-gate  *                       |     panicbuf          |
4910Sstevel@tonic-gate  * 0x00000000.78000000  -|-----------------------|- SYSBASE32
4920Sstevel@tonic-gate  *                       :                       :
4930Sstevel@tonic-gate  *                       :                       :
4940Sstevel@tonic-gate  *                       |                       |
4950Sstevel@tonic-gate  *                       |-----------------------|- econtig32
4960Sstevel@tonic-gate  *                       |    vm structures      |
4970Sstevel@tonic-gate  * 0x00000000.01C00000   |-----------------------|- nalloc_end
4980Sstevel@tonic-gate  *                       |         TSBs          |
4990Sstevel@tonic-gate  *                       |-----------------------|- end/nalloc_base
5000Sstevel@tonic-gate  *                       |   kernel data & bss   |
5010Sstevel@tonic-gate  * 0x00000000.01800000  -|-----------------------|
5020Sstevel@tonic-gate  *                       :   nucleus text hole   :
5030Sstevel@tonic-gate  * 0x00000000.01400000  -|-----------------------|
5040Sstevel@tonic-gate  *                       :                       :
5050Sstevel@tonic-gate  *                       |-----------------------|
5060Sstevel@tonic-gate  *                       |      module text      |
5070Sstevel@tonic-gate  *                       |-----------------------|- e_text/modtext
5080Sstevel@tonic-gate  *                       |      kernel text      |
5090Sstevel@tonic-gate  *                       |-----------------------|
5100Sstevel@tonic-gate  *                       |    trap table (48k)   |
5110Sstevel@tonic-gate  * 0x00000000.01000000  -|-----------------------|- KERNELBASE
5120Sstevel@tonic-gate  *                       | reserved for trapstat |} TSTAT_TOTAL_SIZE
5130Sstevel@tonic-gate  *                       |-----------------------|
5140Sstevel@tonic-gate  *                       |                       |
5150Sstevel@tonic-gate  *                       |        invalid        |
5160Sstevel@tonic-gate  *                       |                       |
5170Sstevel@tonic-gate  * 0x00000000.00000000  _|_______________________|
5180Sstevel@tonic-gate  *
5190Sstevel@tonic-gate  *
5200Sstevel@tonic-gate  *
5210Sstevel@tonic-gate  *                   32-bit User Virtual Memory Layout.
5220Sstevel@tonic-gate  *                       /-----------------------\
5230Sstevel@tonic-gate  *                       |                       |
5240Sstevel@tonic-gate  *                       |        invalid        |
5250Sstevel@tonic-gate  *                       |                       |
5260Sstevel@tonic-gate  *          0xFFC00000  -|-----------------------|- USERLIMIT
5270Sstevel@tonic-gate  *                       |       user stack      |
5280Sstevel@tonic-gate  *                       :                       :
5290Sstevel@tonic-gate  *                       :                       :
5300Sstevel@tonic-gate  *                       :                       :
5310Sstevel@tonic-gate  *                       |       user data       |
5320Sstevel@tonic-gate  *                      -|-----------------------|-
5330Sstevel@tonic-gate  *                       |       user text       |
5340Sstevel@tonic-gate  *          0x00002000  -|-----------------------|-
5350Sstevel@tonic-gate  *                       |       invalid         |
5360Sstevel@tonic-gate  *          0x00000000  _|_______________________|
5370Sstevel@tonic-gate  *
5380Sstevel@tonic-gate  *
5390Sstevel@tonic-gate  *
5400Sstevel@tonic-gate  *                   64-bit User Virtual Memory Layout.
5410Sstevel@tonic-gate  *                       /-----------------------\
5420Sstevel@tonic-gate  *                       |                       |
5430Sstevel@tonic-gate  *                       |        invalid        |
5440Sstevel@tonic-gate  *                       |                       |
5450Sstevel@tonic-gate  *  0xFFFFFFFF.80000000 -|-----------------------|- USERLIMIT
5460Sstevel@tonic-gate  *                       |       user stack      |
5470Sstevel@tonic-gate  *                       :                       :
5480Sstevel@tonic-gate  *                       :                       :
5490Sstevel@tonic-gate  *                       :                       :
5500Sstevel@tonic-gate  *                       |       user data       |
5510Sstevel@tonic-gate  *                      -|-----------------------|-
5520Sstevel@tonic-gate  *                       |       user text       |
5530Sstevel@tonic-gate  *  0x00000000.00100000 -|-----------------------|-
5540Sstevel@tonic-gate  *                       |       invalid         |
5550Sstevel@tonic-gate  *  0x00000000.00000000 _|_______________________|
5560Sstevel@tonic-gate  */
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate extern caddr_t ecache_init_scrub_flush_area(caddr_t alloc_base);
5590Sstevel@tonic-gate extern uint64_t ecache_flush_address(void);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate #pragma weak load_platform_modules
5621772Sjl139090 #pragma weak plat_startup_memlist
5630Sstevel@tonic-gate #pragma weak ecache_init_scrub_flush_area
5640Sstevel@tonic-gate #pragma weak ecache_flush_address
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate /*
5680Sstevel@tonic-gate  * By default the DR Cage is enabled for maximum OS
5690Sstevel@tonic-gate  * MPSS performance.  Users needing to disable the cage mechanism
5700Sstevel@tonic-gate  * can set this variable to zero via /etc/system.
5710Sstevel@tonic-gate  * Disabling the cage on systems supporting Dynamic Reconfiguration (DR)
5720Sstevel@tonic-gate  * will result in loss of DR functionality.
5730Sstevel@tonic-gate  * Platforms wishing to disable kernel Cage by default
5740Sstevel@tonic-gate  * should do so in their set_platform_defaults() routine.
5750Sstevel@tonic-gate  */
5760Sstevel@tonic-gate int	kernel_cage_enable = 1;
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate static void
5790Sstevel@tonic-gate setup_cage_params(void)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate 	void (*func)(void);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	func = (void (*)(void))kobj_getsymvalue("set_platform_cage_params", 0);
5840Sstevel@tonic-gate 	if (func != NULL) {
5850Sstevel@tonic-gate 		(*func)();
5860Sstevel@tonic-gate 		return;
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	if (kernel_cage_enable == 0) {
5900Sstevel@tonic-gate 		return;
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 	kcage_range_lock();
5930Sstevel@tonic-gate 	if (kcage_range_init(phys_avail, 1) == 0) {
5940Sstevel@tonic-gate 		kcage_init(total_pages / 256);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 	kcage_range_unlock();
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (kcage_on) {
5990Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is ENABLED");
6000Sstevel@tonic-gate 	} else {
6010Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is DISABLED");
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate  * Machine-dependent startup code
6080Sstevel@tonic-gate  */
6090Sstevel@tonic-gate void
6100Sstevel@tonic-gate startup(void)
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate 	startup_init();
6130Sstevel@tonic-gate 	if (&startup_platform)
6140Sstevel@tonic-gate 		startup_platform();
6150Sstevel@tonic-gate 	startup_memlist();
6160Sstevel@tonic-gate 	startup_modules();
6170Sstevel@tonic-gate 	setup_cage_params();
6180Sstevel@tonic-gate 	startup_bop_gone();
6190Sstevel@tonic-gate 	startup_vm();
6200Sstevel@tonic-gate 	startup_end();
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate struct regs sync_reg_buf;
6240Sstevel@tonic-gate uint64_t sync_tt;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate void
6270Sstevel@tonic-gate sync_handler(void)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	struct  trap_info 	ti;
6300Sstevel@tonic-gate 	int i;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	/*
6330Sstevel@tonic-gate 	 * Prevent trying to talk to the other CPUs since they are
6340Sstevel@tonic-gate 	 * sitting in the prom and won't reply.
6350Sstevel@tonic-gate 	 */
6360Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
6370Sstevel@tonic-gate 		if ((i != CPU->cpu_id) && CPU_XCALL_READY(i)) {
6380Sstevel@tonic-gate 			cpu[i]->cpu_flags &= ~CPU_READY;
6390Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
6400Sstevel@tonic-gate 			CPUSET_DEL(cpu_ready_set, cpu[i]->cpu_id);
6410Sstevel@tonic-gate 		}
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	/*
6450Sstevel@tonic-gate 	 * We've managed to get here without going through the
6460Sstevel@tonic-gate 	 * normal panic code path. Try and save some useful
6470Sstevel@tonic-gate 	 * information.
6480Sstevel@tonic-gate 	 */
6490Sstevel@tonic-gate 	if (!panicstr && (curthread->t_panic_trap == NULL)) {
6500Sstevel@tonic-gate 		ti.trap_type = sync_tt;
6510Sstevel@tonic-gate 		ti.trap_regs = &sync_reg_buf;
6520Sstevel@tonic-gate 		ti.trap_addr = NULL;
6530Sstevel@tonic-gate 		ti.trap_mmu_fsr = 0x0;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		curthread->t_panic_trap = &ti;
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/*
6590Sstevel@tonic-gate 	 * If we're re-entering the panic path, update the signature
6600Sstevel@tonic-gate 	 * block so that the SC knows we're in the second part of panic.
6610Sstevel@tonic-gate 	 */
6620Sstevel@tonic-gate 	if (panicstr)
6630Sstevel@tonic-gate 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DUMP, -1);
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	nopanicdebug = 1; /* do not perform debug_enter() prior to dump */
6660Sstevel@tonic-gate 	panic("sync initiated");
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate static void
6710Sstevel@tonic-gate startup_init(void)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * We want to save the registers while we're still in OBP
6750Sstevel@tonic-gate 	 * so that we know they haven't been fiddled with since.
6760Sstevel@tonic-gate 	 * (In principle, OBP can't change them just because it
6770Sstevel@tonic-gate 	 * makes a callback, but we'd rather not depend on that
6780Sstevel@tonic-gate 	 * behavior.)
6790Sstevel@tonic-gate 	 */
6800Sstevel@tonic-gate 	char		sync_str[] =
6810Sstevel@tonic-gate 		"warning @ warning off : sync "
6820Sstevel@tonic-gate 		"%%tl-c %%tstate h# %p x! "
6830Sstevel@tonic-gate 		"%%g1 h# %p x! %%g2 h# %p x! %%g3 h# %p x! "
6840Sstevel@tonic-gate 		"%%g4 h# %p x! %%g5 h# %p x! %%g6 h# %p x! "
6850Sstevel@tonic-gate 		"%%g7 h# %p x! %%o0 h# %p x! %%o1 h# %p x! "
6860Sstevel@tonic-gate 		"%%o2 h# %p x! %%o3 h# %p x! %%o4 h# %p x! "
6870Sstevel@tonic-gate 		"%%o5 h# %p x! %%o6 h# %p x! %%o7 h# %p x! "
6880Sstevel@tonic-gate 		"%%tl-c %%tpc h# %p x! %%tl-c %%tnpc h# %p x! "
6890Sstevel@tonic-gate 		"%%y h# %p l! %%tl-c %%tt h# %p x! "
6900Sstevel@tonic-gate 		"sync ; warning !";
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * 20 == num of %p substrings
6940Sstevel@tonic-gate 	 * 16 == max num of chars %p will expand to.
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	char 		bp[sizeof (sync_str) + 16 * 20];
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	/*
7010Sstevel@tonic-gate 	 * Initialize ptl1 stack for the 1st CPU.
7020Sstevel@tonic-gate 	 */
7030Sstevel@tonic-gate 	ptl1_init_cpu(&cpu0);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	/*
7060Sstevel@tonic-gate 	 * Initialize the address map for cache consistent mappings
7070Sstevel@tonic-gate 	 * to random pages; must be done after vac_size is set.
7080Sstevel@tonic-gate 	 */
7090Sstevel@tonic-gate 	ppmapinit();
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	/*
7120Sstevel@tonic-gate 	 * Initialize the PROM callback handler.
7130Sstevel@tonic-gate 	 */
7140Sstevel@tonic-gate 	init_vx_handler();
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	/*
7170Sstevel@tonic-gate 	 * have prom call sync_callback() to handle the sync and
7180Sstevel@tonic-gate 	 * save some useful information which will be stored in the
7190Sstevel@tonic-gate 	 * core file later.
7200Sstevel@tonic-gate 	 */
7210Sstevel@tonic-gate 	(void) sprintf((char *)bp, sync_str,
7220Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_tstate, (void *)&sync_reg_buf.r_g1,
7230Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_g2, (void *)&sync_reg_buf.r_g3,
7240Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_g4, (void *)&sync_reg_buf.r_g5,
7250Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_g6, (void *)&sync_reg_buf.r_g7,
7260Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o0, (void *)&sync_reg_buf.r_o1,
7270Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o2, (void *)&sync_reg_buf.r_o3,
7280Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o4, (void *)&sync_reg_buf.r_o5,
7290Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o6, (void *)&sync_reg_buf.r_o7,
7300Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_pc, (void *)&sync_reg_buf.r_npc,
7310Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_y, (void *)&sync_tt);
7320Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
7330Sstevel@tonic-gate 	add_vx_handler("sync", 1, (void (*)(cell_t *))sync_handler);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static u_longlong_t *boot_physinstalled, *boot_physavail, *boot_virtavail;
7370Sstevel@tonic-gate static size_t boot_physinstalled_len, boot_physavail_len, boot_virtavail_len;
7380Sstevel@tonic-gate 
7392973Sgovinda #define	IVSIZE	((MAXIVNUM * sizeof (intr_vec_t *)) + \
7402973Sgovinda 		(MAX_RSVD_IV * sizeof (intr_vec_t)) + \
7412973Sgovinda 		(MAX_RSVD_IVX * sizeof (intr_vecx_t)))
7420Sstevel@tonic-gate 
743*3764Sdp78419 #if !defined(C_OBP)
744*3764Sdp78419 /*
745*3764Sdp78419  * Install a temporary tte handler in OBP for kmem64 area.
746*3764Sdp78419  *
747*3764Sdp78419  * We map kmem64 area with large pages before the trap table is taken
748*3764Sdp78419  * over. Since OBP makes 8K mappings, it can create 8K tlb entries in
749*3764Sdp78419  * the same area. Duplicate tlb entries with different page sizes
750*3764Sdp78419  * cause unpredicatble behavior.  To avoid this, we don't create
751*3764Sdp78419  * kmem64 mappings via BOP_ALLOC (ends up as prom_alloc() call to
752*3764Sdp78419  * OBP).  Instead, we manage translations with a temporary va>tte-data
753*3764Sdp78419  * handler (kmem64-tte).  This handler is replaced by unix-tte when
754*3764Sdp78419  * the trap table is taken over.
755*3764Sdp78419  *
756*3764Sdp78419  * The temporary handler knows the physical address of the kmem64
757*3764Sdp78419  * area. It uses the prom's pgmap@ Forth word for other addresses.
758*3764Sdp78419  *
759*3764Sdp78419  * We have to use BOP_ALLOC() method for C-OBP platforms because
760*3764Sdp78419  * pgmap@ is not defined in C-OBP. C-OBP is only used on serengeti
761*3764Sdp78419  * sun4u platforms. On sun4u we flush tlb after trap table is taken
762*3764Sdp78419  * over if we use large pages for kernel heap and kmem64. Since sun4u
763*3764Sdp78419  * prom (unlike sun4v) calls va>tte-data first for client address
764*3764Sdp78419  * translation prom's ttes for kmem64 can't get into TLB even if we
765*3764Sdp78419  * later switch to prom's trap table again. C-OBP uses 4M pages for
766*3764Sdp78419  * client mappings when possible so on all platforms we get the
767*3764Sdp78419  * benefit from large mappings for kmem64 area immediately during
768*3764Sdp78419  * boot.
769*3764Sdp78419  *
770*3764Sdp78419  * pseudo code:
771*3764Sdp78419  * if (context != 0) {
772*3764Sdp78419  * 	return false
773*3764Sdp78419  * } else if (miss_va in range[kmem64_base, kmem64_end)) {
774*3764Sdp78419  *	tte = tte_template +
775*3764Sdp78419  *		(((miss_va & pagemask) - kmem64_base));
776*3764Sdp78419  *	return tte, true
777*3764Sdp78419  * } else {
778*3764Sdp78419  *	return pgmap@ result
779*3764Sdp78419  * }
780*3764Sdp78419  */
781*3764Sdp78419 char kmem64_obp_str[] =
782*3764Sdp78419 	"h# %lx constant kmem64_base "
783*3764Sdp78419 	"h# %lx constant kmem64_end "
784*3764Sdp78419 	"h# %lx constant kmem64_pagemask "
785*3764Sdp78419 	"h# %lx constant kmem64_template "
786*3764Sdp78419 
787*3764Sdp78419 	": kmem64-tte ( addr cnum -- false | tte-data true ) "
788*3764Sdp78419 	"    if                                       ( addr ) "
789*3764Sdp78419 	"       drop false exit then                  ( false ) "
790*3764Sdp78419 	"    dup  kmem64_base kmem64_end  within  if  ( addr ) "
791*3764Sdp78419 	"	kmem64_pagemask and                   ( addr' ) "
792*3764Sdp78419 	"	kmem64_base -                         ( addr' ) "
793*3764Sdp78419 	"	kmem64_template +                     ( tte ) "
794*3764Sdp78419 	"	true                                  ( tte true ) "
795*3764Sdp78419 	"    else                                     ( addr ) "
796*3764Sdp78419 	"	pgmap@                                ( tte ) "
797*3764Sdp78419 	"       dup 0< if true else drop false then   ( tte true  |  false ) "
798*3764Sdp78419 	"    then                                     ( tte true  |  false ) "
799*3764Sdp78419 	"; "
800*3764Sdp78419 
801*3764Sdp78419 	"' kmem64-tte is va>tte-data "
802*3764Sdp78419 ;
803*3764Sdp78419 
804*3764Sdp78419 void
805*3764Sdp78419 install_kmem64_tte()
806*3764Sdp78419 {
807*3764Sdp78419 	char b[sizeof (kmem64_obp_str) + (4 * 16)];
808*3764Sdp78419 	tte_t tte;
809*3764Sdp78419 
810*3764Sdp78419 	PRM_DEBUG(kmem64_pabase);
811*3764Sdp78419 	PRM_DEBUG(kmem64_szc);
812*3764Sdp78419 	sfmmu_memtte(&tte, kmem64_pabase >> MMU_PAGESHIFT,
813*3764Sdp78419 	    PROC_DATA | HAT_NOSYNC, kmem64_szc);
814*3764Sdp78419 	PRM_DEBUG(tte.ll);
815*3764Sdp78419 	(void) sprintf(b, kmem64_obp_str,
816*3764Sdp78419 	    kmem64_base, kmem64_end, TTE_PAGEMASK(kmem64_szc), tte.ll);
817*3764Sdp78419 	ASSERT(strlen(b) < sizeof (b));
818*3764Sdp78419 	prom_interpret(b, 0, 0, 0, 0, 0);
819*3764Sdp78419 }
820*3764Sdp78419 #endif	/* !C_OBP */
821*3764Sdp78419 
8220Sstevel@tonic-gate /*
8230Sstevel@tonic-gate  * As OBP takes up some RAM when the system boots, pages will already be "lost"
8240Sstevel@tonic-gate  * to the system and reflected in npages by the time we see it.
8250Sstevel@tonic-gate  *
8260Sstevel@tonic-gate  * We only want to allocate kernel structures in the 64-bit virtual address
8270Sstevel@tonic-gate  * space on systems with enough RAM to make the overhead of keeping track of
8280Sstevel@tonic-gate  * an extra kernel memory segment worthwhile.
8290Sstevel@tonic-gate  *
8300Sstevel@tonic-gate  * Since OBP has already performed its memory allocations by this point, if we
8310Sstevel@tonic-gate  * have more than MINMOVE_RAM_MB MB of RAM left free, go ahead and map
8320Sstevel@tonic-gate  * memory in the 64-bit virtual address space; otherwise keep allocations
8330Sstevel@tonic-gate  * contiguous with we've mapped so far in the 32-bit virtual address space.
8340Sstevel@tonic-gate  */
8350Sstevel@tonic-gate #define	MINMOVE_RAM_MB	((size_t)1900)
8360Sstevel@tonic-gate #define	MB_TO_BYTES(mb)	((mb) * 1048576ul)
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate pgcnt_t	tune_npages = (pgcnt_t)
8390Sstevel@tonic-gate 	(MB_TO_BYTES(MINMOVE_RAM_MB)/ (size_t)MMU_PAGESIZE);
8400Sstevel@tonic-gate 
8413733Sdp78419 #pragma weak page_set_colorequiv_arr_cpu
8423733Sdp78419 extern void page_set_colorequiv_arr_cpu(void);
8433733Sdp78419 
8440Sstevel@tonic-gate static void
8450Sstevel@tonic-gate startup_memlist(void)
8460Sstevel@tonic-gate {
8470Sstevel@tonic-gate 	size_t alloc_sz;
8480Sstevel@tonic-gate 	size_t ctrs_sz;
8490Sstevel@tonic-gate 	caddr_t alloc_base;
8500Sstevel@tonic-gate 	caddr_t ctrs_base, ctrs_end;
8510Sstevel@tonic-gate 	caddr_t memspace;
8520Sstevel@tonic-gate 	caddr_t va;
8530Sstevel@tonic-gate 	int memblocks = 0;
8540Sstevel@tonic-gate 	struct memlist *cur;
8550Sstevel@tonic-gate 	size_t syslimit = (size_t)SYSLIMIT;
8560Sstevel@tonic-gate 	size_t sysbase = (size_t)SYSBASE;
857*3764Sdp78419 	int alloc_alignsize = ecache_alignsize;
858*3764Sdp78419 	int i;
8590Sstevel@tonic-gate 	extern void page_coloring_init(void);
8603717Sdp78419 	extern void page_set_colorequiv_arr(void);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	/*
8630Sstevel@tonic-gate 	 * Initialize enough of the system to allow kmem_alloc to work by
8640Sstevel@tonic-gate 	 * calling boot to allocate its memory until the time that
8650Sstevel@tonic-gate 	 * kvm_init is completed.  The page structs are allocated after
8660Sstevel@tonic-gate 	 * rounding up end to the nearest page boundary; the memsegs are
8670Sstevel@tonic-gate 	 * initialized and the space they use comes from the kernel heap.
8680Sstevel@tonic-gate 	 * With appropriate initialization, they can be reallocated later
8690Sstevel@tonic-gate 	 * to a size appropriate for the machine's configuration.
8700Sstevel@tonic-gate 	 *
8710Sstevel@tonic-gate 	 * At this point, memory is allocated for things that will never
8720Sstevel@tonic-gate 	 * need to be freed, this used to be "valloced".  This allows a
8730Sstevel@tonic-gate 	 * savings as the pages don't need page structures to describe
8740Sstevel@tonic-gate 	 * them because them will not be managed by the vm system.
8750Sstevel@tonic-gate 	 */
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/*
8780Sstevel@tonic-gate 	 * We're loaded by boot with the following configuration (as
8790Sstevel@tonic-gate 	 * specified in the sun4u/conf/Mapfile):
8800Sstevel@tonic-gate 	 *
8810Sstevel@tonic-gate 	 * 	text:		4 MB chunk aligned on a 4MB boundary
8820Sstevel@tonic-gate 	 * 	data & bss:	4 MB chunk aligned on a 4MB boundary
8830Sstevel@tonic-gate 	 *
8840Sstevel@tonic-gate 	 * These two chunks will eventually be mapped by 2 locked 4MB
8850Sstevel@tonic-gate 	 * ttes and will represent the nucleus of the kernel.  This gives
8860Sstevel@tonic-gate 	 * us some free space that is already allocated, some or all of
8870Sstevel@tonic-gate 	 * which is made available to kernel module text.
8880Sstevel@tonic-gate 	 *
8890Sstevel@tonic-gate 	 * The free space in the data-bss chunk is used for nucleus
8900Sstevel@tonic-gate 	 * allocatable data structures and we reserve it using the
8910Sstevel@tonic-gate 	 * nalloc_base and nalloc_end variables.  This space is currently
8920Sstevel@tonic-gate 	 * being used for hat data structures required for tlb miss
8930Sstevel@tonic-gate 	 * handling operations.  We align nalloc_base to a l2 cache
8940Sstevel@tonic-gate 	 * linesize because this is the line size the hardware uses to
8950Sstevel@tonic-gate 	 * maintain cache coherency.
8960Sstevel@tonic-gate 	 * 256K is carved out for module data.
8970Sstevel@tonic-gate 	 */
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	nalloc_base = (caddr_t)roundup((uintptr_t)e_data, MMU_PAGESIZE);
9000Sstevel@tonic-gate 	moddata = nalloc_base;
9010Sstevel@tonic-gate 	e_moddata = nalloc_base + MODDATA;
9020Sstevel@tonic-gate 	nalloc_base = e_moddata;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	nalloc_end = (caddr_t)roundup((uintptr_t)nalloc_base, MMU_PAGESIZE4M);
9050Sstevel@tonic-gate 	valloc_base = nalloc_base;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	/*
9080Sstevel@tonic-gate 	 * Calculate the start of the data segment.
9090Sstevel@tonic-gate 	 */
9100Sstevel@tonic-gate 	sdata = (caddr_t)((uintptr_t)e_data & MMU_PAGEMASK4M);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9130Sstevel@tonic-gate 	PRM_DEBUG(nalloc_base);
9140Sstevel@tonic-gate 	PRM_DEBUG(nalloc_end);
9150Sstevel@tonic-gate 	PRM_DEBUG(sdata);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	/*
9180Sstevel@tonic-gate 	 * Remember any slop after e_text so we can give it to the modules.
9190Sstevel@tonic-gate 	 */
9200Sstevel@tonic-gate 	PRM_DEBUG(e_text);
9210Sstevel@tonic-gate 	modtext = (caddr_t)roundup((uintptr_t)e_text, MMU_PAGESIZE);
9220Sstevel@tonic-gate 	if (((uintptr_t)modtext & MMU_PAGEMASK4M) != (uintptr_t)s_text)
923*3764Sdp78419 		prom_panic("nucleus text overflow");
9240Sstevel@tonic-gate 	modtext_sz = (caddr_t)roundup((uintptr_t)modtext, MMU_PAGESIZE4M) -
9250Sstevel@tonic-gate 	    modtext;
9260Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9270Sstevel@tonic-gate 	PRM_DEBUG(modtext_sz);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
9300Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
9310Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
9320Sstevel@tonic-gate 	/*
9330Sstevel@tonic-gate 	 * Remember what the physically available highest page is
9340Sstevel@tonic-gate 	 * so that dumpsys works properly, and find out how much
9350Sstevel@tonic-gate 	 * memory is installed.
9360Sstevel@tonic-gate 	 */
9370Sstevel@tonic-gate 	installed_top_size_memlist_array(boot_physinstalled,
9380Sstevel@tonic-gate 	    boot_physinstalled_len, &physmax, &physinstalled);
9390Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9400Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	/* Fill out memory nodes config structure */
9430Sstevel@tonic-gate 	startup_build_mem_nodes(boot_physinstalled, boot_physinstalled_len);
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	/*
9460Sstevel@tonic-gate 	 * Get the list of physically available memory to size
9470Sstevel@tonic-gate 	 * the number of page structures needed.
9480Sstevel@tonic-gate 	 */
9490Sstevel@tonic-gate 	size_physavail(boot_physavail, boot_physavail_len, &npages, &memblocks);
9500Sstevel@tonic-gate 	/*
9510Sstevel@tonic-gate 	 * This first snap shot of npages can represent the pages used
9520Sstevel@tonic-gate 	 * by OBP's text and data approximately. This is used in the
9530Sstevel@tonic-gate 	 * the calculation of the kernel size
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 	obp_pages = physinstalled - npages;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * On small-memory systems (<MODTEXT_SM_SIZE MB, currently 256MB), the
9600Sstevel@tonic-gate 	 * in-nucleus module text is capped to MODTEXT_SM_CAP bytes (currently
9610Sstevel@tonic-gate 	 * 2MB) and any excess pages are put on physavail.  The assumption is
9620Sstevel@tonic-gate 	 * that small-memory systems will need more pages more than they'll
9630Sstevel@tonic-gate 	 * need efficiently-mapped module texts.
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	if ((physinstalled < mmu_btop(MODTEXT_SM_SIZE << 20)) &&
9660Sstevel@tonic-gate 	    modtext_sz > MODTEXT_SM_CAP) {
9670Sstevel@tonic-gate 		extra_etpg = mmu_btop(modtext_sz - MODTEXT_SM_CAP);
9680Sstevel@tonic-gate 		modtext_sz = MODTEXT_SM_CAP;
9690Sstevel@tonic-gate 	} else
9700Sstevel@tonic-gate 		extra_etpg = 0;
9710Sstevel@tonic-gate 	PRM_DEBUG(extra_etpg);
9720Sstevel@tonic-gate 	PRM_DEBUG(modtext_sz);
9730Sstevel@tonic-gate 	extra_etva = modtext + modtext_sz;
9740Sstevel@tonic-gate 	PRM_DEBUG(extra_etva);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	/*
9770Sstevel@tonic-gate 	 * Account for any pages after e_text and e_data.
9780Sstevel@tonic-gate 	 */
9790Sstevel@tonic-gate 	npages += extra_etpg;
9800Sstevel@tonic-gate 	npages += mmu_btopr(nalloc_end - nalloc_base);
9810Sstevel@tonic-gate 	PRM_DEBUG(npages);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	/*
9840Sstevel@tonic-gate 	 * npages is the maximum of available physical memory possible.
9850Sstevel@tonic-gate 	 * (ie. it will never be more than this)
9860Sstevel@tonic-gate 	 */
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	/*
9890Sstevel@tonic-gate 	 * initialize the nucleus memory allocator.
9900Sstevel@tonic-gate 	 */
9910Sstevel@tonic-gate 	ndata_alloc_init(&ndata, (uintptr_t)nalloc_base, (uintptr_t)nalloc_end);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	/*
9940Sstevel@tonic-gate 	 * Allocate mmu fault status area from the nucleus data area.
9950Sstevel@tonic-gate 	 */
9960Sstevel@tonic-gate 	if ((&ndata_alloc_mmfsa != NULL) && (ndata_alloc_mmfsa(&ndata) != 0))
9970Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after mfsa alloc");
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	/*
10000Sstevel@tonic-gate 	 * Allocate kernel TSBs from the nucleus data area.
10010Sstevel@tonic-gate 	 */
10020Sstevel@tonic-gate 	if (ndata_alloc_tsbs(&ndata, npages) != 0)
10030Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after tsbs alloc");
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	/*
10060Sstevel@tonic-gate 	 * Allocate dmv dispatch table from the nucleus data area.
10070Sstevel@tonic-gate 	 */
10080Sstevel@tonic-gate 	if (ndata_alloc_dmv(&ndata) != 0)
10090Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after dmv alloc");
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	page_coloring_init();
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/*
10150Sstevel@tonic-gate 	 * Allocate page_freelists bin headers for memnode 0 from the
10160Sstevel@tonic-gate 	 * nucleus data area.
10170Sstevel@tonic-gate 	 */
10180Sstevel@tonic-gate 	if (ndata_alloc_page_freelists(&ndata, 0) != 0)
10190Sstevel@tonic-gate 		cmn_err(CE_PANIC,
10200Sstevel@tonic-gate 		    "no more nucleus memory after page free lists alloc");
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if (kpm_enable) {
10230Sstevel@tonic-gate 		kpm_init();
10240Sstevel@tonic-gate 		/*
10250Sstevel@tonic-gate 		 * kpm page space -- Update kpm_npages and make the
10260Sstevel@tonic-gate 		 * same assumption about fragmenting as it is done
10270Sstevel@tonic-gate 		 * for memseg_sz.
10280Sstevel@tonic-gate 		 */
10290Sstevel@tonic-gate 		kpm_npages_setup(memblocks + 4);
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	/*
10330Sstevel@tonic-gate 	 * Allocate hat related structs from the nucleus data area.
10340Sstevel@tonic-gate 	 */
10350Sstevel@tonic-gate 	if (ndata_alloc_hat(&ndata, npages, kpm_npages) != 0)
10360Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after hat alloc");
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	/*
10390Sstevel@tonic-gate 	 * We want to do the BOP_ALLOCs before the real allocation of page
10400Sstevel@tonic-gate 	 * structs in order to not have to allocate page structs for this
10410Sstevel@tonic-gate 	 * memory.  We need to calculate a virtual address because we want
10420Sstevel@tonic-gate 	 * the page structs to come before other allocations in virtual address
10430Sstevel@tonic-gate 	 * space.  This is so some (if not all) of page structs can actually
10440Sstevel@tonic-gate 	 * live in the nucleus.
10450Sstevel@tonic-gate 	 */
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/*
10480Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10490Sstevel@tonic-gate 	 *
10500Sstevel@tonic-gate 	 * There are comments all over the SFMMU code warning of dire
10510Sstevel@tonic-gate 	 * consequences if the TSBs are moved out of 32-bit space.  This
10520Sstevel@tonic-gate 	 * is largely because the asm code uses "sethi %hi(addr)"-type
10530Sstevel@tonic-gate 	 * instructions which will not provide the expected result if the
10540Sstevel@tonic-gate 	 * address is a 64-bit one.
10550Sstevel@tonic-gate 	 *
10560Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10570Sstevel@tonic-gate 	 */
10580Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)nalloc_end, MMU_PAGESIZE);
10590Sstevel@tonic-gate 	alloc_base = sfmmu_ktsb_alloc(alloc_base);
10600Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
10610Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	/*
10640Sstevel@tonic-gate 	 * Allocate IOMMU TSB array.  We do this here so that the physical
10650Sstevel@tonic-gate 	 * memory gets deducted from the PROM's physical memory list.
10660Sstevel@tonic-gate 	 */
10670Sstevel@tonic-gate 	alloc_base = iommu_tsb_init(alloc_base);
10680Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
10690Sstevel@tonic-gate 	    ecache_alignsize);
10700Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	/*
10731772Sjl139090 	 * Platforms like Starcat and OPL need special structures assigned in
10741772Sjl139090 	 * 32-bit virtual address space because their probing routines execute
10751772Sjl139090 	 * FCode, and FCode can't handle 64-bit virtual addresses...
10760Sstevel@tonic-gate 	 */
10771772Sjl139090 	if (&plat_startup_memlist) {
10781772Sjl139090 		alloc_base = plat_startup_memlist(alloc_base);
10790Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
10800Sstevel@tonic-gate 		    ecache_alignsize);
10810Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	/*
10850Sstevel@tonic-gate 	 * Save off where the contiguous allocations to date have ended
10860Sstevel@tonic-gate 	 * in econtig32.
10870Sstevel@tonic-gate 	 */
10880Sstevel@tonic-gate 	econtig32 = alloc_base;
10890Sstevel@tonic-gate 	PRM_DEBUG(econtig32);
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	if (econtig32 > (caddr_t)KERNEL_LIMIT32)
10920Sstevel@tonic-gate 		cmn_err(CE_PANIC, "econtig32 too big");
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	/*
10950Sstevel@tonic-gate 	 * To avoid memory allocation collisions in the 32-bit virtual address
10960Sstevel@tonic-gate 	 * space, make allocations from this point forward in 64-bit virtual
1097*3764Sdp78419 	 * address space starting at syslimit and working up.
10980Sstevel@tonic-gate 	 *
10990Sstevel@tonic-gate 	 * All this is needed because on large memory systems, the default
11000Sstevel@tonic-gate 	 * Solaris allocations will collide with SYSBASE32, which is hard
11010Sstevel@tonic-gate 	 * coded to be at the virtual address 0x78000000.  Therefore, on 64-bit
11020Sstevel@tonic-gate 	 * kernels, move the allocations to a location in the 64-bit virtual
11030Sstevel@tonic-gate 	 * address space space, allowing those structures to grow without
11040Sstevel@tonic-gate 	 * worry.
11050Sstevel@tonic-gate 	 *
11060Sstevel@tonic-gate 	 * On current CPUs we'll run out of physical memory address bits before
11070Sstevel@tonic-gate 	 * we need to worry about the allocations running into anything else in
11080Sstevel@tonic-gate 	 * VM or the virtual address holes on US-I and II, as there's currently
11090Sstevel@tonic-gate 	 * about 1 TB of addressable space before the US-I/II VA hole.
11100Sstevel@tonic-gate 	 */
11110Sstevel@tonic-gate 	kmem64_base = (caddr_t)syslimit;
11120Sstevel@tonic-gate 	PRM_DEBUG(kmem64_base);
11130Sstevel@tonic-gate 
1114*3764Sdp78419 	/*
1115*3764Sdp78419 	 * Allocate addresses, but not physical memory. None of these locations
1116*3764Sdp78419 	 * can be touched until physical memory is allocated below.
1117*3764Sdp78419 	 */
1118*3764Sdp78419 	alloc_base = kmem64_base;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	/*
11210Sstevel@tonic-gate 	 * If KHME and/or UHME hash buckets won't fit in the nucleus, allocate
11220Sstevel@tonic-gate 	 * them here.
11230Sstevel@tonic-gate 	 */
11240Sstevel@tonic-gate 	if (khme_hash == NULL || uhme_hash == NULL) {
11250Sstevel@tonic-gate 		/*
11260Sstevel@tonic-gate 		 * alloc_hme_buckets() will align alloc_base properly before
11270Sstevel@tonic-gate 		 * assigning the hash buckets, so we don't need to do it
11280Sstevel@tonic-gate 		 * before the call...
11290Sstevel@tonic-gate 		 */
11300Sstevel@tonic-gate 		alloc_base = alloc_hme_buckets(alloc_base, alloc_alignsize);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11330Sstevel@tonic-gate 		PRM_DEBUG(khme_hash);
11340Sstevel@tonic-gate 		PRM_DEBUG(uhme_hash);
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	/*
11380Sstevel@tonic-gate 	 * Allocate the remaining page freelists.  NUMA systems can
11390Sstevel@tonic-gate 	 * have lots of page freelists, one per node, which quickly
11400Sstevel@tonic-gate 	 * outgrow the amount of nucleus memory available.
11410Sstevel@tonic-gate 	 */
11420Sstevel@tonic-gate 	if (max_mem_nodes > 1) {
11430Sstevel@tonic-gate 		int mnode;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		for (mnode = 1; mnode < max_mem_nodes; mnode++) {
11460Sstevel@tonic-gate 			alloc_base = alloc_page_freelists(mnode, alloc_base,
11470Sstevel@tonic-gate 				ecache_alignsize);
11480Sstevel@tonic-gate 		}
11490Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	if (!mml_table) {
11530Sstevel@tonic-gate 		size_t mmltable_sz;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 		/*
11560Sstevel@tonic-gate 		 * We need to allocate the mml_table here because there
11570Sstevel@tonic-gate 		 * was not enough space within the nucleus.
11580Sstevel@tonic-gate 		 */
11590Sstevel@tonic-gate 		mmltable_sz = sizeof (kmutex_t) * mml_table_sz;
11600Sstevel@tonic-gate 		alloc_sz = roundup(mmltable_sz, alloc_alignsize);
11610Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
11620Sstevel@tonic-gate 		    alloc_alignsize);
1163*3764Sdp78419 		mml_table = (kmutex_t *)alloc_base;
11640Sstevel@tonic-gate 		alloc_base += alloc_sz;
11650Sstevel@tonic-gate 		PRM_DEBUG(mml_table);
11660Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	if (kpm_enable && !(kpmp_table || kpmp_stable)) {
11700Sstevel@tonic-gate 		size_t kpmptable_sz;
11710Sstevel@tonic-gate 		caddr_t table;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 		/*
11740Sstevel@tonic-gate 		 * We need to allocate either kpmp_table or kpmp_stable here
11750Sstevel@tonic-gate 		 * because there was not enough space within the nucleus.
11760Sstevel@tonic-gate 		 */
11770Sstevel@tonic-gate 		kpmptable_sz = (kpm_smallpages == 0) ?
11780Sstevel@tonic-gate 				sizeof (kpm_hlk_t) * kpmp_table_sz :
11790Sstevel@tonic-gate 				sizeof (kpm_shlk_t) * kpmp_stable_sz;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 		alloc_sz = roundup(kpmptable_sz, alloc_alignsize);
11820Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
11830Sstevel@tonic-gate 		    alloc_alignsize);
11840Sstevel@tonic-gate 
1185*3764Sdp78419 		table = alloc_base;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 		if (kpm_smallpages == 0) {
11880Sstevel@tonic-gate 			kpmp_table = (kpm_hlk_t *)table;
11890Sstevel@tonic-gate 			PRM_DEBUG(kpmp_table);
11900Sstevel@tonic-gate 		} else {
11910Sstevel@tonic-gate 			kpmp_stable = (kpm_shlk_t *)table;
11920Sstevel@tonic-gate 			PRM_DEBUG(kpmp_stable);
11930Sstevel@tonic-gate 		}
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 		alloc_base += alloc_sz;
11960Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	if (&ecache_init_scrub_flush_area) {
12000Sstevel@tonic-gate 		/*
12010Sstevel@tonic-gate 		 * Pass alloc_base directly, as the routine itself is
12020Sstevel@tonic-gate 		 * responsible for any special alignment requirements...
12030Sstevel@tonic-gate 		 */
12040Sstevel@tonic-gate 		alloc_base = ecache_init_scrub_flush_area(alloc_base);
12050Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	/*
12090Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
12100Sstevel@tonic-gate 	 */
12110Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
12120Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
12130Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	/*
12160Sstevel@tonic-gate 	 * Reset npages and memblocks based on boot_physavail list.
12170Sstevel@tonic-gate 	 */
12180Sstevel@tonic-gate 	size_physavail(boot_physavail, boot_physavail_len, &npages, &memblocks);
12190Sstevel@tonic-gate 	PRM_DEBUG(npages);
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	/*
12220Sstevel@tonic-gate 	 * Account for extra memory after e_text.
12230Sstevel@tonic-gate 	 */
12240Sstevel@tonic-gate 	npages += extra_etpg;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	/*
12270Sstevel@tonic-gate 	 * Calculate the largest free memory chunk in the nucleus data area.
12280Sstevel@tonic-gate 	 * We need to figure out if page structs can fit in there or not.
12290Sstevel@tonic-gate 	 * We also make sure enough page structs get created for any physical
12300Sstevel@tonic-gate 	 * memory we might be returning to the system.
12310Sstevel@tonic-gate 	 */
12320Sstevel@tonic-gate 	ndata_remain_sz = ndata_maxsize(&ndata);
12330Sstevel@tonic-gate 	PRM_DEBUG(ndata_remain_sz);
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	pp_sz = sizeof (struct page) * npages;
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	/*
12380Sstevel@tonic-gate 	 * Here's a nice bit of code based on somewhat recursive logic:
12390Sstevel@tonic-gate 	 *
12400Sstevel@tonic-gate 	 * If the page array would fit within the nucleus, we want to
12410Sstevel@tonic-gate 	 * add npages to cover any extra memory we may be returning back
12420Sstevel@tonic-gate 	 * to the system.
12430Sstevel@tonic-gate 	 *
12440Sstevel@tonic-gate 	 * HOWEVER, the page array is sized by calculating the size of
12450Sstevel@tonic-gate 	 * (struct page * npages), as are the pagehash table, ctrs and
12460Sstevel@tonic-gate 	 * memseg_list, so the very act of performing the calculation below may
12470Sstevel@tonic-gate 	 * in fact make the array large enough that it no longer fits in the
12480Sstevel@tonic-gate 	 * nucleus, meaning there would now be a much larger area of the
12490Sstevel@tonic-gate 	 * nucleus free that should really be added to npages, which would
12500Sstevel@tonic-gate 	 * make the page array that much larger, and so on.
12510Sstevel@tonic-gate 	 *
12520Sstevel@tonic-gate 	 * This also ignores the memory possibly used in the nucleus for the
12530Sstevel@tonic-gate 	 * the page hash, ctrs and memseg list and the fact that whether they
12540Sstevel@tonic-gate 	 * fit there or not varies with the npages calculation below, but we
12550Sstevel@tonic-gate 	 * don't even factor them into the equation at this point; perhaps we
12560Sstevel@tonic-gate 	 * should or perhaps we should just take the approach that the few
12570Sstevel@tonic-gate 	 * extra pages we could add via this calculation REALLY aren't worth
12580Sstevel@tonic-gate 	 * the hassle...
12590Sstevel@tonic-gate 	 */
12600Sstevel@tonic-gate 	if (ndata_remain_sz > pp_sz) {
12610Sstevel@tonic-gate 		size_t spare = ndata_spare(&ndata, pp_sz, ecache_alignsize);
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 		npages += mmu_btop(spare);
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 		pp_sz = npages * sizeof (struct page);
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 		pp_base = ndata_alloc(&ndata, pp_sz, ecache_alignsize);
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	/*
12710Sstevel@tonic-gate 	 * If physmem is patched to be non-zero, use it instead of
12720Sstevel@tonic-gate 	 * the monitor value unless physmem is larger than the total
12730Sstevel@tonic-gate 	 * amount of memory on hand.
12740Sstevel@tonic-gate 	 */
12750Sstevel@tonic-gate 	if (physmem == 0 || physmem > npages)
12760Sstevel@tonic-gate 		physmem = npages;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	/*
12790Sstevel@tonic-gate 	 * If pp_base is NULL that means the routines above have determined
12800Sstevel@tonic-gate 	 * the page array will not fit in the nucleus; we'll have to
12810Sstevel@tonic-gate 	 * BOP_ALLOC() ourselves some space for them.
12820Sstevel@tonic-gate 	 */
12830Sstevel@tonic-gate 	if (pp_base == NULL) {
12840Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
12850Sstevel@tonic-gate 		    alloc_alignsize);
12860Sstevel@tonic-gate 		alloc_sz = roundup(pp_sz, alloc_alignsize);
12870Sstevel@tonic-gate 
1288*3764Sdp78419 		pp_base = (struct page *)alloc_base;
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 		alloc_base += alloc_sz;
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	/*
12940Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
12950Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
12960Sstevel@tonic-gate 	 */
12970Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
12980Sstevel@tonic-gate 	page_hashsz = 1 << highbit((ulong_t)page_hashsz);
12990Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	/*
13020Sstevel@tonic-gate 	 * We want to TRY to fit the page structure hash table,
13030Sstevel@tonic-gate 	 * the page size free list counters, the memseg list and
13040Sstevel@tonic-gate 	 * and the kpm page space in the nucleus if possible.
13050Sstevel@tonic-gate 	 *
13060Sstevel@tonic-gate 	 * alloc_sz counts how much memory needs to be allocated by
13070Sstevel@tonic-gate 	 * BOP_ALLOC().
13080Sstevel@tonic-gate 	 */
13090Sstevel@tonic-gate 	page_hash = ndata_alloc(&ndata, pagehash_sz, ecache_alignsize);
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	alloc_sz = (page_hash == NULL ? pagehash_sz : 0);
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	/*
13140Sstevel@tonic-gate 	 * Size up per page size free list counters.
13150Sstevel@tonic-gate 	 */
13160Sstevel@tonic-gate 	ctrs_sz = page_ctrs_sz();
13170Sstevel@tonic-gate 	ctrs_base = ndata_alloc(&ndata, ctrs_sz, ecache_alignsize);
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 	if (ctrs_base == NULL)
13200Sstevel@tonic-gate 		alloc_sz = roundup(alloc_sz, ecache_alignsize) + ctrs_sz;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/*
13230Sstevel@tonic-gate 	 * The memseg list is for the chunks of physical memory that
13240Sstevel@tonic-gate 	 * will be managed by the vm system.  The number calculated is
13250Sstevel@tonic-gate 	 * a guess as boot may fragment it more when memory allocations
13260Sstevel@tonic-gate 	 * are made before kphysm_init().  Currently, there are two
13270Sstevel@tonic-gate 	 * allocations before then, so we assume each causes fragmen-
13280Sstevel@tonic-gate 	 * tation, and add a couple more for good measure.
13290Sstevel@tonic-gate 	 */
13300Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + 4);
13310Sstevel@tonic-gate 	memseg_base = ndata_alloc(&ndata, memseg_sz, ecache_alignsize);
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	if (memseg_base == NULL)
13340Sstevel@tonic-gate 		alloc_sz = roundup(alloc_sz, ecache_alignsize) + memseg_sz;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	if (kpm_enable) {
13380Sstevel@tonic-gate 		/*
13390Sstevel@tonic-gate 		 * kpm page space -- Update kpm_npages and make the
13400Sstevel@tonic-gate 		 * same assumption about fragmenting as it is done
13410Sstevel@tonic-gate 		 * for memseg_sz above.
13420Sstevel@tonic-gate 		 */
13430Sstevel@tonic-gate 		kpm_npages_setup(memblocks + 4);
13440Sstevel@tonic-gate 		kpm_pp_sz = (kpm_smallpages == 0) ?
1345*3764Sdp78419 		    kpm_npages * sizeof (kpm_page_t):
1346*3764Sdp78419 		    kpm_npages * sizeof (kpm_spage_t);
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 		kpm_pp_base = (uintptr_t)ndata_alloc(&ndata, kpm_pp_sz,
13490Sstevel@tonic-gate 		    ecache_alignsize);
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 		if (kpm_pp_base == NULL)
13520Sstevel@tonic-gate 			alloc_sz = roundup(alloc_sz, ecache_alignsize) +
13530Sstevel@tonic-gate 			    kpm_pp_sz;
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	if (alloc_sz > 0) {
13570Sstevel@tonic-gate 		uintptr_t bop_base;
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 		/*
13600Sstevel@tonic-gate 		 * We need extra memory allocated through BOP_ALLOC.
13610Sstevel@tonic-gate 		 */
13620Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
13630Sstevel@tonic-gate 		    alloc_alignsize);
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 		alloc_sz = roundup(alloc_sz, alloc_alignsize);
13660Sstevel@tonic-gate 
1367*3764Sdp78419 		bop_base = (uintptr_t)alloc_base;
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 		alloc_base += alloc_sz;
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		if (page_hash == NULL) {
13720Sstevel@tonic-gate 			page_hash = (struct page **)bop_base;
13730Sstevel@tonic-gate 			bop_base = roundup(bop_base + pagehash_sz,
13740Sstevel@tonic-gate 			    ecache_alignsize);
13750Sstevel@tonic-gate 		}
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 		if (ctrs_base == NULL) {
13780Sstevel@tonic-gate 			ctrs_base = (caddr_t)bop_base;
13790Sstevel@tonic-gate 			bop_base = roundup(bop_base + ctrs_sz,
13800Sstevel@tonic-gate 			    ecache_alignsize);
13810Sstevel@tonic-gate 		}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 		if (memseg_base == NULL) {
13840Sstevel@tonic-gate 			memseg_base = (struct memseg *)bop_base;
13850Sstevel@tonic-gate 			bop_base = roundup(bop_base + memseg_sz,
13860Sstevel@tonic-gate 			    ecache_alignsize);
13870Sstevel@tonic-gate 		}
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 		if (kpm_enable && kpm_pp_base == NULL) {
13900Sstevel@tonic-gate 			kpm_pp_base = (uintptr_t)bop_base;
13910Sstevel@tonic-gate 			bop_base = roundup(bop_base + kpm_pp_sz,
13920Sstevel@tonic-gate 			    ecache_alignsize);
13930Sstevel@tonic-gate 		}
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 		ASSERT(bop_base <= (uintptr_t)alloc_base);
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	PRM_DEBUG(page_hash);
13990Sstevel@tonic-gate 	PRM_DEBUG(memseg_base);
14000Sstevel@tonic-gate 	PRM_DEBUG(kpm_pp_base);
14010Sstevel@tonic-gate 	PRM_DEBUG(kpm_pp_sz);
14020Sstevel@tonic-gate 	PRM_DEBUG(pp_base);
14030Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
14040Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate #ifdef	TRAPTRACE
14070Sstevel@tonic-gate 	alloc_base = trap_trace_alloc(alloc_base);
14080Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
14090Sstevel@tonic-gate #endif	/* TRAPTRACE */
14100Sstevel@tonic-gate 
1411*3764Sdp78419 	/*
1412*3764Sdp78419 	 * In theory it's possible that kmem64 chunk is 0 sized
1413*3764Sdp78419 	 * (on very small machines). Check for that.
1414*3764Sdp78419 	 */
1415*3764Sdp78419 	if (alloc_base == kmem64_base) {
1416*3764Sdp78419 		kmem64_base = NULL;
1417*3764Sdp78419 		kmem64_end = NULL;
1418*3764Sdp78419 		kmem64_aligned_end = NULL;
1419*3764Sdp78419 		goto kmem64_alloced;
14200Sstevel@tonic-gate 	}
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	/*
1423*3764Sdp78419 	 * Allocate kmem64 memory.
1424*3764Sdp78419 	 * Round up to end of large page and overmap.
1425*3764Sdp78419 	 * kmem64_end..kmem64_aligned_end is added to memory list for reuse
1426*3764Sdp78419 	 */
1427*3764Sdp78419 	kmem64_end = (caddr_t)roundup((uintptr_t)alloc_base,
1428*3764Sdp78419 	    MMU_PAGESIZE);
1429*3764Sdp78419 
1430*3764Sdp78419 	/*
1431*3764Sdp78419 	 * Make one large memory alloc after figuring out the 64-bit size. This
1432*3764Sdp78419 	 * will enable use of the largest page size appropriate for the system
1433*3764Sdp78419 	 * architecture.
1434*3764Sdp78419 	 */
1435*3764Sdp78419 	ASSERT(mmu_exported_pagesize_mask & (1 << TTE8K));
1436*3764Sdp78419 	ASSERT(IS_P2ALIGNED(kmem64_base, TTEBYTES(max_bootlp_tteszc)));
1437*3764Sdp78419 	for (i = max_bootlp_tteszc; i >= TTE8K; i--) {
1438*3764Sdp78419 		size_t asize;
1439*3764Sdp78419 #if !defined(C_OBP)
1440*3764Sdp78419 		unsigned long long pa;
1441*3764Sdp78419 #endif	/* !C_OBP */
1442*3764Sdp78419 
1443*3764Sdp78419 		if ((mmu_exported_pagesize_mask & (1 << i)) == 0)
1444*3764Sdp78419 			continue;
1445*3764Sdp78419 		kmem64_alignsize = TTEBYTES(i);
1446*3764Sdp78419 		kmem64_szc = i;
1447*3764Sdp78419 
1448*3764Sdp78419 		/* limit page size for small memory */
1449*3764Sdp78419 		if (mmu_btop(kmem64_alignsize) > (npages >> 2))
1450*3764Sdp78419 			continue;
1451*3764Sdp78419 
1452*3764Sdp78419 		kmem64_aligned_end = (caddr_t)roundup((uintptr_t)kmem64_end,
1453*3764Sdp78419 		    kmem64_alignsize);
1454*3764Sdp78419 		asize = kmem64_aligned_end - kmem64_base;
1455*3764Sdp78419 #if !defined(C_OBP)
1456*3764Sdp78419 		if (prom_allocate_phys(asize, kmem64_alignsize, &pa) == 0) {
1457*3764Sdp78419 			if (prom_claim_virt(asize, kmem64_base) !=
1458*3764Sdp78419 			    (caddr_t)-1) {
1459*3764Sdp78419 				kmem64_pabase = pa;
1460*3764Sdp78419 				install_kmem64_tte();
1461*3764Sdp78419 				break;
1462*3764Sdp78419 			} else {
1463*3764Sdp78419 				prom_free_phys(asize, pa);
1464*3764Sdp78419 			}
1465*3764Sdp78419 		}
1466*3764Sdp78419 #else	/* !C_OBP */
1467*3764Sdp78419 		if ((caddr_t)BOP_ALLOC(bootops, kmem64_base, asize,
1468*3764Sdp78419 		    kmem64_alignsize) == kmem64_base) {
1469*3764Sdp78419 			kmem64_pabase = va_to_pa(kmem64_base);
1470*3764Sdp78419 			break;
1471*3764Sdp78419 		}
1472*3764Sdp78419 #endif	/* !C_OBP */
1473*3764Sdp78419 		if (i == TTE8K) {
1474*3764Sdp78419 			prom_panic("kmem64 allocation failure");
1475*3764Sdp78419 		}
1476*3764Sdp78419 	}
1477*3764Sdp78419 
1478*3764Sdp78419 	PRM_DEBUG(kmem64_base);
1479*3764Sdp78419 	PRM_DEBUG(kmem64_end);
1480*3764Sdp78419 	PRM_DEBUG(kmem64_aligned_end);
1481*3764Sdp78419 	PRM_DEBUG(kmem64_alignsize);
1482*3764Sdp78419 
1483*3764Sdp78419 	/*
1484*3764Sdp78419 	 * Now set pa using saved va from above.
1485*3764Sdp78419 	 */
1486*3764Sdp78419 	if (&ecache_init_scrub_flush_area) {
1487*3764Sdp78419 		(void) ecache_init_scrub_flush_area(NULL);
1488*3764Sdp78419 	}
1489*3764Sdp78419 
1490*3764Sdp78419 kmem64_alloced:
1491*3764Sdp78419 
1492*3764Sdp78419 	/*
1493*3764Sdp78419 	 * Initialize per page size free list counters.
1494*3764Sdp78419 	 */
1495*3764Sdp78419 	ctrs_end = page_ctrs_alloc(ctrs_base);
1496*3764Sdp78419 	ASSERT(ctrs_base + ctrs_sz >= ctrs_end);
1497*3764Sdp78419 
1498*3764Sdp78419 	/*
14992973Sgovinda 	 * Allocate space for the interrupt vector table and also for the
15002973Sgovinda 	 * reserved interrupt vector data structures.
15010Sstevel@tonic-gate 	 */
15022973Sgovinda 	memspace = (caddr_t)BOP_ALLOC(bootops, (caddr_t)intr_vec_table,
15030Sstevel@tonic-gate 	    IVSIZE, MMU_PAGESIZE);
15042973Sgovinda 	if (memspace != (caddr_t)intr_vec_table)
1505*3764Sdp78419 		prom_panic("interrupt vector table allocation failure");
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	/*
15080Sstevel@tonic-gate 	 * The memory lists from boot are allocated from the heap arena
15090Sstevel@tonic-gate 	 * so that later they can be freed and/or reallocated.
15100Sstevel@tonic-gate 	 */
15110Sstevel@tonic-gate 	if (BOP_GETPROP(bootops, "extent", &memlist_sz) == -1)
1512*3764Sdp78419 		prom_panic("could not retrieve property \"extent\"");
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	/*
15150Sstevel@tonic-gate 	 * Between now and when we finish copying in the memory lists,
15160Sstevel@tonic-gate 	 * allocations happen so the space gets fragmented and the
15170Sstevel@tonic-gate 	 * lists longer.  Leave enough space for lists twice as long
15180Sstevel@tonic-gate 	 * as what boot says it has now; roundup to a pagesize.
15190Sstevel@tonic-gate 	 * Also add space for the final phys-avail copy in the fixup
15200Sstevel@tonic-gate 	 * routine.
15210Sstevel@tonic-gate 	 */
15220Sstevel@tonic-gate 	va = (caddr_t)(sysbase + PAGESIZE + PANICBUFSIZE +
15230Sstevel@tonic-gate 	    roundup(IVSIZE, MMU_PAGESIZE));
15240Sstevel@tonic-gate 	memlist_sz *= 4;
15250Sstevel@tonic-gate 	memlist_sz = roundup(memlist_sz, MMU_PAGESIZE);
15260Sstevel@tonic-gate 	memspace = (caddr_t)BOP_ALLOC(bootops, va, memlist_sz, BO_NO_ALIGN);
15270Sstevel@tonic-gate 	if (memspace == NULL)
15280Sstevel@tonic-gate 		halt("Boot allocation failed.");
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	memlist = (struct memlist *)memspace;
15310Sstevel@tonic-gate 	memlist_end = (char *)memspace + memlist_sz;
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	PRM_DEBUG(memlist);
15340Sstevel@tonic-gate 	PRM_DEBUG(memlist_end);
15350Sstevel@tonic-gate 	PRM_DEBUG(sysbase);
15360Sstevel@tonic-gate 	PRM_DEBUG(syslimit);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	kernelheap_init((void *)sysbase, (void *)syslimit,
15390Sstevel@tonic-gate 	    (caddr_t)sysbase + PAGESIZE, NULL, NULL);
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	/*
15420Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
15430Sstevel@tonic-gate 	 */
15440Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
15450Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
15460Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	/*
15490Sstevel@tonic-gate 	 * Remove the space used by BOP_ALLOC from the kernel heap
15500Sstevel@tonic-gate 	 * plus the area actually used by the OBP (if any)
15510Sstevel@tonic-gate 	 * ignoring virtual addresses in virt_avail, above syslimit.
15520Sstevel@tonic-gate 	 */
15530Sstevel@tonic-gate 	virt_avail = memlist;
15540Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	for (cur = virt_avail; cur->next; cur = cur->next) {
15570Sstevel@tonic-gate 		uint64_t range_base, range_size;
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 		if ((range_base = cur->address + cur->size) < (uint64_t)sysbase)
15600Sstevel@tonic-gate 			continue;
15610Sstevel@tonic-gate 		if (range_base >= (uint64_t)syslimit)
15620Sstevel@tonic-gate 			break;
15630Sstevel@tonic-gate 		/*
15640Sstevel@tonic-gate 		 * Limit the range to end at syslimit.
15650Sstevel@tonic-gate 		 */
15660Sstevel@tonic-gate 		range_size = MIN(cur->next->address,
15670Sstevel@tonic-gate 		    (uint64_t)syslimit) - range_base;
15680Sstevel@tonic-gate 		(void) vmem_xalloc(heap_arena, (size_t)range_size, PAGESIZE,
15690Sstevel@tonic-gate 		    0, 0, (void *)range_base, (void *)(range_base + range_size),
15700Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	phys_avail = memlist;
15740Sstevel@tonic-gate 	(void) copy_physavail(boot_physavail, boot_physavail_len,
15750Sstevel@tonic-gate 	    &memlist, 0, 0);
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	/*
1578*3764Sdp78419 	 * Add any unused kmem64 memory from overmapped page
1579*3764Sdp78419 	 * (Note: va_to_pa does not work for kmem64_end)
1580*3764Sdp78419 	 */
1581*3764Sdp78419 	if (kmem64_end < kmem64_aligned_end) {
1582*3764Sdp78419 		uint64_t overlap_size = kmem64_aligned_end - kmem64_end;
1583*3764Sdp78419 		uint64_t overlap_pa = kmem64_pabase +
1584*3764Sdp78419 		    (kmem64_end - kmem64_base);
1585*3764Sdp78419 
1586*3764Sdp78419 		PRM_DEBUG(overlap_pa);
1587*3764Sdp78419 		PRM_DEBUG(overlap_size);
1588*3764Sdp78419 		memlist_add(overlap_pa, overlap_size, &memlist, &phys_avail);
1589*3764Sdp78419 	}
1590*3764Sdp78419 
1591*3764Sdp78419 	/*
15920Sstevel@tonic-gate 	 * Add any extra memory after e_text to the phys_avail list, as long
15930Sstevel@tonic-gate 	 * as there's at least a page to add.
15940Sstevel@tonic-gate 	 */
15950Sstevel@tonic-gate 	if (extra_etpg)
15960Sstevel@tonic-gate 		memlist_add(va_to_pa(extra_etva), mmu_ptob(extra_etpg),
15970Sstevel@tonic-gate 		    &memlist, &phys_avail);
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 	/*
16000Sstevel@tonic-gate 	 * Add any extra memory after e_data to the phys_avail list as long
16010Sstevel@tonic-gate 	 * as there's at least a page to add.  Usually, there isn't any,
16020Sstevel@tonic-gate 	 * since extra HME blocks typically get allocated there first before
16030Sstevel@tonic-gate 	 * using RAM elsewhere.
16040Sstevel@tonic-gate 	 */
16050Sstevel@tonic-gate 	if ((nalloc_base = ndata_extra_base(&ndata, MMU_PAGESIZE)) == NULL)
16060Sstevel@tonic-gate 		nalloc_base = nalloc_end;
16070Sstevel@tonic-gate 	ndata_remain_sz = nalloc_end - nalloc_base;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	if (ndata_remain_sz >= MMU_PAGESIZE)
16100Sstevel@tonic-gate 		memlist_add(va_to_pa(nalloc_base),
16110Sstevel@tonic-gate 		    (uint64_t)ndata_remain_sz, &memlist, &phys_avail);
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	PRM_DEBUG(memlist);
16140Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
16150Sstevel@tonic-gate 	PRM_DEBUG(memspace);
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	if ((caddr_t)memlist > (memspace + memlist_sz))
1618*3764Sdp78419 		prom_panic("memlist overflow");
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	PRM_DEBUG(pp_base);
16210Sstevel@tonic-gate 	PRM_DEBUG(memseg_base);
16220Sstevel@tonic-gate 	PRM_DEBUG(npages);
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	/*
16250Sstevel@tonic-gate 	 * Initialize the page structures from the memory lists.
16260Sstevel@tonic-gate 	 */
16270Sstevel@tonic-gate 	kphysm_init(pp_base, memseg_base, npages, kpm_pp_base, kpm_npages);
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	availrmem_initial = availrmem = freemem;
16300Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	/*
16330Sstevel@tonic-gate 	 * Some of the locks depend on page_hashsz being set!
16340Sstevel@tonic-gate 	 * kmem_init() depends on this; so, keep it here.
16350Sstevel@tonic-gate 	 */
16360Sstevel@tonic-gate 	page_lock_init();
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	/*
16390Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
16400Sstevel@tonic-gate 	 */
16410Sstevel@tonic-gate 	kmem_init();
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	/*
16443717Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
16453717Sdp78419 	 */
16463733Sdp78419 	if (&page_set_colorequiv_arr_cpu != NULL)
16473733Sdp78419 		page_set_colorequiv_arr_cpu();
16483733Sdp78419 	else
16493733Sdp78419 		page_set_colorequiv_arr();
16503717Sdp78419 
16513717Sdp78419 	/*
16520Sstevel@tonic-gate 	 * Initialize bp_mapin().
16530Sstevel@tonic-gate 	 */
16540Sstevel@tonic-gate 	bp_init(shm_alignment, HAT_STRICTORDER);
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	/*
16572973Sgovinda 	 * Reserve space for panicbuf, intr_vec_table and reserved interrupt
16582973Sgovinda 	 * vector data structures from the 32-bit heap.
16590Sstevel@tonic-gate 	 */
16600Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, PANICBUFSIZE, PAGESIZE, 0, 0,
16610Sstevel@tonic-gate 	    panicbuf, panicbuf + PANICBUFSIZE,
16620Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, IVSIZE, PAGESIZE, 0, 0,
16652973Sgovinda 	    intr_vec_table, (caddr_t)intr_vec_table + IVSIZE,
16660Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	mem_config_init();
16690Sstevel@tonic-gate }
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate static void
16720Sstevel@tonic-gate startup_modules(void)
16730Sstevel@tonic-gate {
16740Sstevel@tonic-gate 	int proplen, nhblk1, nhblk8;
16750Sstevel@tonic-gate 	size_t  nhblksz;
1676*3764Sdp78419 	pgcnt_t pages_per_hblk;
16770Sstevel@tonic-gate 	size_t hme8blk_sz, hme1blk_sz;
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	/*
16800Sstevel@tonic-gate 	 * Log any optional messages from the boot program
16810Sstevel@tonic-gate 	 */
16820Sstevel@tonic-gate 	proplen = (size_t)BOP_GETPROPLEN(bootops, "boot-message");
16830Sstevel@tonic-gate 	if (proplen > 0) {
16840Sstevel@tonic-gate 		char *msg;
16850Sstevel@tonic-gate 		size_t len = (size_t)proplen;
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 		msg = kmem_zalloc(len, KM_SLEEP);
16880Sstevel@tonic-gate 		(void) BOP_GETPROP(bootops, "boot-message", msg);
16890Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s\n", msg);
16900Sstevel@tonic-gate 		kmem_free(msg, len);
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	/*
16940Sstevel@tonic-gate 	 * Let the platforms have a chance to change default
16950Sstevel@tonic-gate 	 * values before reading system file.
16960Sstevel@tonic-gate 	 */
16970Sstevel@tonic-gate 	if (&set_platform_defaults)
16980Sstevel@tonic-gate 		set_platform_defaults();
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	/*
17010Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
17020Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
17030Sstevel@tonic-gate 	 */
17040Sstevel@tonic-gate 	param_calc(0);
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	mod_setup();
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	/*
17090Sstevel@tonic-gate 	 * If this is a positron, complain and halt.
17100Sstevel@tonic-gate 	 */
17110Sstevel@tonic-gate 	if (&iam_positron && iam_positron()) {
17120Sstevel@tonic-gate 		cmn_err(CE_WARN, "This hardware platform is not supported"
17130Sstevel@tonic-gate 		    " by this release of Solaris.\n");
17140Sstevel@tonic-gate #ifdef DEBUG
17150Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to resume */
17160Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting an unsupported platform.\n");
17170Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware.\n");
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate #else /* DEBUG */
17200Sstevel@tonic-gate 		halt(0);
17210Sstevel@tonic-gate #endif /* DEBUG */
17220Sstevel@tonic-gate 	}
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 	/*
17250Sstevel@tonic-gate 	 * If we are running firmware that isn't 64-bit ready
17260Sstevel@tonic-gate 	 * then complain and halt.
17270Sstevel@tonic-gate 	 */
17280Sstevel@tonic-gate 	do_prom_version_check();
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	/*
17310Sstevel@tonic-gate 	 * Initialize system parameters
17320Sstevel@tonic-gate 	 */
17330Sstevel@tonic-gate 	param_init();
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	/*
17360Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
17370Sstevel@tonic-gate 	 */
17380Sstevel@tonic-gate 	maxmem = physmem;
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	/* Set segkp limits. */
17413446Smrj 	ncbase = kdi_segdebugbase;
17423446Smrj 	ncend = kdi_segdebugbase;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	/*
17450Sstevel@tonic-gate 	 * Initialize the hat layer.
17460Sstevel@tonic-gate 	 */
17470Sstevel@tonic-gate 	hat_init();
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	/*
17500Sstevel@tonic-gate 	 * Initialize segment management stuff.
17510Sstevel@tonic-gate 	 */
17520Sstevel@tonic-gate 	seg_init();
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	/*
17550Sstevel@tonic-gate 	 * Create the va>tte handler, so the prom can understand
17560Sstevel@tonic-gate 	 * kernel translations.  The handler is installed later, just
17570Sstevel@tonic-gate 	 * as we are about to take over the trap table from the prom.
17580Sstevel@tonic-gate 	 */
17590Sstevel@tonic-gate 	create_va_to_tte();
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	/*
17620Sstevel@tonic-gate 	 * Load the forthdebugger (optional)
17630Sstevel@tonic-gate 	 */
17640Sstevel@tonic-gate 	forthdebug_init();
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	/*
17670Sstevel@tonic-gate 	 * Create OBP node for console input callbacks
17680Sstevel@tonic-gate 	 * if it is needed.
17690Sstevel@tonic-gate 	 */
17701253Slq150181 	startup_create_io_node();
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	if (modloadonly("fs", "specfs") == -1)
17730Sstevel@tonic-gate 		halt("Can't load specfs");
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	if (modloadonly("fs", "devfs") == -1)
17760Sstevel@tonic-gate 		halt("Can't load devfs");
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 	if (modloadonly("misc", "swapgeneric") == -1)
17790Sstevel@tonic-gate 		halt("Can't load swapgeneric");
17800Sstevel@tonic-gate 
17811676Sjpk 	(void) modloadonly("sys", "lbl_edition");
17821676Sjpk 
17830Sstevel@tonic-gate 	dispinit();
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	/*
17860Sstevel@tonic-gate 	 * Infer meanings to the members of the idprom buffer.
17870Sstevel@tonic-gate 	 */
17880Sstevel@tonic-gate 	parse_idprom();
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	/* Read cluster configuration data. */
17910Sstevel@tonic-gate 	clconf_init();
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	setup_ddi();
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	/*
17960Sstevel@tonic-gate 	 * Lets take this opportunity to load the root device.
17970Sstevel@tonic-gate 	 */
17980Sstevel@tonic-gate 	if (loadrootmodules() != 0)
17990Sstevel@tonic-gate 		debug_enter("Can't load the root filesystem");
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	/*
18020Sstevel@tonic-gate 	 * Load tod driver module for the tod part found on this system.
18030Sstevel@tonic-gate 	 * Recompute the cpu frequency/delays based on tod as tod part
18040Sstevel@tonic-gate 	 * tends to keep time more accurately.
18050Sstevel@tonic-gate 	 */
18060Sstevel@tonic-gate 	if (&load_tod_module)
18070Sstevel@tonic-gate 		load_tod_module();
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	/*
18100Sstevel@tonic-gate 	 * Allow platforms to load modules which might
18110Sstevel@tonic-gate 	 * be needed after bootops are gone.
18120Sstevel@tonic-gate 	 */
18130Sstevel@tonic-gate 	if (&load_platform_modules)
18140Sstevel@tonic-gate 		load_platform_modules();
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	setcpudelay();
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
18190Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
18200Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	/*
18230Sstevel@tonic-gate 	 * Calculation and allocation of hmeblks needed to remap
1824*3764Sdp78419 	 * the memory allocated by PROM till now.
1825*3764Sdp78419 	 * Overestimate the number of hblk1 elements by assuming
1826*3764Sdp78419 	 * worst case of TTE64K mappings.
1827*3764Sdp78419 	 * sfmmu_hblk_alloc will panic if this calculation is wrong.
18280Sstevel@tonic-gate 	 */
1829*3764Sdp78419 	bop_alloc_pages = btopr(kmem64_end - kmem64_base);
1830*3764Sdp78419 	pages_per_hblk = btop(HMEBLK_SPAN(TTE64K));
1831*3764Sdp78419 	bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
1832*3764Sdp78419 	nhblk1 = bop_alloc_pages / pages_per_hblk + hblk1_min;
1833*3764Sdp78419 
1834*3764Sdp78419 	bop_alloc_pages = size_virtalloc(boot_virtavail, boot_virtavail_len);
1835*3764Sdp78419 
1836*3764Sdp78419 	/* sfmmu_init_nucleus_hblks expects properly aligned data structures */
18370Sstevel@tonic-gate 	hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t));
18380Sstevel@tonic-gate 	hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t));
18390Sstevel@tonic-gate 
1840*3764Sdp78419 	bop_alloc_pages += btopr(nhblk1 * hme1blk_sz);
1841*3764Sdp78419 
18420Sstevel@tonic-gate 	pages_per_hblk = btop(HMEBLK_SPAN(TTE8K));
1843*3764Sdp78419 	nhblk8 = 0;
1844*3764Sdp78419 	while (bop_alloc_pages > 1) {
1845*3764Sdp78419 		bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
1846*3764Sdp78419 		nhblk8 += bop_alloc_pages /= pages_per_hblk;
1847*3764Sdp78419 		bop_alloc_pages *= hme8blk_sz;
1848*3764Sdp78419 		bop_alloc_pages = btopr(bop_alloc_pages);
1849*3764Sdp78419 	}
1850*3764Sdp78419 	nhblk8 += 2;
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	/*
18530Sstevel@tonic-gate 	 * Since hblk8's can hold up to 64k of mappings aligned on a 64k
18540Sstevel@tonic-gate 	 * boundary, the number of hblk8's needed to map the entries in the
18550Sstevel@tonic-gate 	 * boot_virtavail list needs to be adjusted to take this into
18560Sstevel@tonic-gate 	 * consideration.  Thus, we need to add additional hblk8's since it
18570Sstevel@tonic-gate 	 * is possible that an hblk8 will not have all 8 slots used due to
18580Sstevel@tonic-gate 	 * alignment constraints.  Since there were boot_virtavail_len entries
18590Sstevel@tonic-gate 	 * in that list, we need to add that many hblk8's to the number
18600Sstevel@tonic-gate 	 * already calculated to make sure we don't underestimate.
18610Sstevel@tonic-gate 	 */
18620Sstevel@tonic-gate 	nhblk8 += boot_virtavail_len;
18630Sstevel@tonic-gate 	nhblksz = nhblk8 * hme8blk_sz + nhblk1 * hme1blk_sz;
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	/* Allocate in pagesize chunks */
18660Sstevel@tonic-gate 	nhblksz = roundup(nhblksz, MMU_PAGESIZE);
18670Sstevel@tonic-gate 	hblk_base = kmem_zalloc(nhblksz, KM_SLEEP);
18680Sstevel@tonic-gate 	sfmmu_init_nucleus_hblks(hblk_base, nhblksz, nhblk8, nhblk1);
18690Sstevel@tonic-gate }
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate static void
18720Sstevel@tonic-gate startup_bop_gone(void)
18730Sstevel@tonic-gate {
18740Sstevel@tonic-gate 	extern int bop_io_quiesced;
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	/*
18771991Sheppo 	 * Destroy the MD initialized at startup
18781991Sheppo 	 * The startup initializes the MD framework
18791991Sheppo 	 * using prom and BOP alloc free it now.
18801991Sheppo 	 */
18811991Sheppo 	mach_descrip_startup_fini();
18821991Sheppo 
18831991Sheppo 	/*
18840Sstevel@tonic-gate 	 * Call back into boot and release boots resources.
18850Sstevel@tonic-gate 	 */
18860Sstevel@tonic-gate 	BOP_QUIESCE_IO(bootops);
18870Sstevel@tonic-gate 	bop_io_quiesced = 1;
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
18900Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
18910Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
18920Sstevel@tonic-gate 	/*
18930Sstevel@tonic-gate 	 * Copy physinstalled list into kernel space.
18940Sstevel@tonic-gate 	 */
18950Sstevel@tonic-gate 	phys_install = memlist;
18960Sstevel@tonic-gate 	copy_memlist(boot_physinstalled, boot_physinstalled_len, &memlist);
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	/*
18990Sstevel@tonic-gate 	 * setup physically contiguous area twice as large as the ecache.
19000Sstevel@tonic-gate 	 * this is used while doing displacement flush of ecaches
19010Sstevel@tonic-gate 	 */
19020Sstevel@tonic-gate 	if (&ecache_flush_address) {
19030Sstevel@tonic-gate 		ecache_flushaddr = ecache_flush_address();
19040Sstevel@tonic-gate 		if (ecache_flushaddr == (uint64_t)-1) {
19050Sstevel@tonic-gate 			cmn_err(CE_PANIC,
19060Sstevel@tonic-gate 			    "startup: no memory to set ecache_flushaddr");
19070Sstevel@tonic-gate 		}
19080Sstevel@tonic-gate 	}
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	/*
19110Sstevel@tonic-gate 	 * Virtual available next.
19120Sstevel@tonic-gate 	 */
19130Sstevel@tonic-gate 	ASSERT(virt_avail != NULL);
19140Sstevel@tonic-gate 	memlist_free_list(virt_avail);
19150Sstevel@tonic-gate 	virt_avail = memlist;
19160Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	/*
19190Sstevel@tonic-gate 	 * Last chance to ask our booter questions ..
19200Sstevel@tonic-gate 	 */
19210Sstevel@tonic-gate }
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate /*
19250Sstevel@tonic-gate  * startup_fixup_physavail - called from mach_sfmmu.c after the final
19260Sstevel@tonic-gate  * allocations have been performed.  We can't call it in startup_bop_gone
19270Sstevel@tonic-gate  * since later operations can cause obp to allocate more memory.
19280Sstevel@tonic-gate  */
19290Sstevel@tonic-gate void
19300Sstevel@tonic-gate startup_fixup_physavail(void)
19310Sstevel@tonic-gate {
19320Sstevel@tonic-gate 	struct memlist *cur;
1933*3764Sdp78419 	size_t kmem64_overmap_size = kmem64_aligned_end - kmem64_end;
1934*3764Sdp78419 
1935*3764Sdp78419 	PRM_DEBUG(kmem64_overmap_size);
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 	/*
19380Sstevel@tonic-gate 	 * take the most current snapshot we can by calling mem-update
19390Sstevel@tonic-gate 	 */
19400Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
19410Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
19420Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 	/*
19450Sstevel@tonic-gate 	 * Copy phys_avail list, again.
19460Sstevel@tonic-gate 	 * Both the kernel/boot and the prom have been allocating
19470Sstevel@tonic-gate 	 * from the original list we copied earlier.
19480Sstevel@tonic-gate 	 */
19490Sstevel@tonic-gate 	cur = memlist;
19500Sstevel@tonic-gate 	(void) copy_physavail(boot_physavail, boot_physavail_len,
19510Sstevel@tonic-gate 	    &memlist, 0, 0);
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	/*
1954*3764Sdp78419 	 * Add any unused kmem64 memory from overmapped page
1955*3764Sdp78419 	 * (Note: va_to_pa does not work for kmem64_end)
1956*3764Sdp78419 	 */
1957*3764Sdp78419 	if (kmem64_overmap_size) {
1958*3764Sdp78419 		memlist_add(kmem64_pabase + (kmem64_end - kmem64_base),
1959*3764Sdp78419 		    kmem64_overmap_size,
1960*3764Sdp78419 		    &memlist, &cur);
1961*3764Sdp78419 	}
1962*3764Sdp78419 
1963*3764Sdp78419 	/*
19640Sstevel@tonic-gate 	 * Add any extra memory after e_text we added to the phys_avail list
19650Sstevel@tonic-gate 	 * back to the old list.
19660Sstevel@tonic-gate 	 */
19670Sstevel@tonic-gate 	if (extra_etpg)
19680Sstevel@tonic-gate 		memlist_add(va_to_pa(extra_etva), mmu_ptob(extra_etpg),
19690Sstevel@tonic-gate 		    &memlist, &cur);
19700Sstevel@tonic-gate 	if (ndata_remain_sz >= MMU_PAGESIZE)
19710Sstevel@tonic-gate 		memlist_add(va_to_pa(nalloc_base),
19720Sstevel@tonic-gate 		    (uint64_t)ndata_remain_sz, &memlist, &cur);
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 	/*
19750Sstevel@tonic-gate 	 * There isn't any bounds checking on the memlist area
19760Sstevel@tonic-gate 	 * so ensure it hasn't overgrown.
19770Sstevel@tonic-gate 	 */
19780Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
19790Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: memlist size exceeded");
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	/*
19820Sstevel@tonic-gate 	 * The kernel removes the pages that were allocated for it from
19830Sstevel@tonic-gate 	 * the freelist, but we now have to find any -extra- pages that
19840Sstevel@tonic-gate 	 * the prom has allocated for it's own book-keeping, and remove
19850Sstevel@tonic-gate 	 * them from the freelist too. sigh.
19860Sstevel@tonic-gate 	 */
19870Sstevel@tonic-gate 	fix_prom_pages(phys_avail, cur);
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	ASSERT(phys_avail != NULL);
19900Sstevel@tonic-gate 	memlist_free_list(phys_avail);
19910Sstevel@tonic-gate 	phys_avail = cur;
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	/*
19940Sstevel@tonic-gate 	 * We're done with boot.  Just after this point in time, boot
19950Sstevel@tonic-gate 	 * gets unmapped, so we can no longer rely on its services.
19960Sstevel@tonic-gate 	 * Zero the bootops to indicate this fact.
19970Sstevel@tonic-gate 	 */
19980Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
19990Sstevel@tonic-gate 	BOOTOPS_GONE();
20000Sstevel@tonic-gate }
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate static void
20030Sstevel@tonic-gate startup_vm(void)
20040Sstevel@tonic-gate {
20050Sstevel@tonic-gate 	size_t	i;
20060Sstevel@tonic-gate 	struct segmap_crargs a;
20070Sstevel@tonic-gate 	struct segkpm_crargs b;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	uint64_t avmem;
20100Sstevel@tonic-gate 	caddr_t va;
20110Sstevel@tonic-gate 	pgcnt_t	max_phys_segkp;
20120Sstevel@tonic-gate 	int	mnode;
20130Sstevel@tonic-gate 
20142991Ssusans 	extern int use_brk_lpg, use_stk_lpg;
2015423Sdavemq 
20160Sstevel@tonic-gate 	/*
20170Sstevel@tonic-gate 	 * get prom's mappings, create hments for them and switch
20180Sstevel@tonic-gate 	 * to the kernel context.
20190Sstevel@tonic-gate 	 */
20200Sstevel@tonic-gate 	hat_kern_setup();
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	/*
20230Sstevel@tonic-gate 	 * Take over trap table
20240Sstevel@tonic-gate 	 */
20250Sstevel@tonic-gate 	setup_trap_table();
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 	/*
20280Sstevel@tonic-gate 	 * Install the va>tte handler, so that the prom can handle
20290Sstevel@tonic-gate 	 * misses and understand the kernel table layout in case
20300Sstevel@tonic-gate 	 * we need call into the prom.
20310Sstevel@tonic-gate 	 */
20320Sstevel@tonic-gate 	install_va_to_tte();
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	/*
20350Sstevel@tonic-gate 	 * Set a flag to indicate that the tba has been taken over.
20360Sstevel@tonic-gate 	 */
20370Sstevel@tonic-gate 	tba_taken_over = 1;
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	/* initialize MMU primary context register */
20400Sstevel@tonic-gate 	mmu_init_kcontext();
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	/*
20430Sstevel@tonic-gate 	 * The boot cpu can now take interrupts, x-calls, x-traps
20440Sstevel@tonic-gate 	 */
20450Sstevel@tonic-gate 	CPUSET_ADD(cpu_ready_set, CPU->cpu_id);
20460Sstevel@tonic-gate 	CPU->cpu_flags |= (CPU_READY | CPU_ENABLE | CPU_EXISTS);
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	/*
20490Sstevel@tonic-gate 	 * Set a flag to tell write_scb_int() that it can access V_TBR_WR_ADDR.
20500Sstevel@tonic-gate 	 */
20510Sstevel@tonic-gate 	tbr_wr_addr_inited = 1;
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	/*
20540Sstevel@tonic-gate 	 * Initialize VM system, and map kernel address space.
20550Sstevel@tonic-gate 	 */
20560Sstevel@tonic-gate 	kvm_init();
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 	/*
20590Sstevel@tonic-gate 	 * XXX4U: previously, we initialized and turned on
20600Sstevel@tonic-gate 	 * the caches at this point. But of course we have
20610Sstevel@tonic-gate 	 * nothing to do, as the prom has already done this
20620Sstevel@tonic-gate 	 * for us -- main memory must be E$able at all times.
20630Sstevel@tonic-gate 	 */
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	/*
20660Sstevel@tonic-gate 	 * If the following is true, someone has patched
20670Sstevel@tonic-gate 	 * phsymem to be less than the number of pages that
20680Sstevel@tonic-gate 	 * the system actually has.  Remove pages until system
20690Sstevel@tonic-gate 	 * memory is limited to the requested amount.  Since we
20700Sstevel@tonic-gate 	 * have allocated page structures for all pages, we
20710Sstevel@tonic-gate 	 * correct the amount of memory we want to remove
20720Sstevel@tonic-gate 	 * by the size of the memory used to hold page structures
20730Sstevel@tonic-gate 	 * for the non-used pages.
20740Sstevel@tonic-gate 	 */
20750Sstevel@tonic-gate 	if (physmem < npages) {
20760Sstevel@tonic-gate 		pgcnt_t diff, off;
20770Sstevel@tonic-gate 		struct page *pp;
20780Sstevel@tonic-gate 		struct seg kseg;
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 		cmn_err(CE_WARN, "limiting physmem to %ld pages", physmem);
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 		off = 0;
20830Sstevel@tonic-gate 		diff = npages - physmem;
20840Sstevel@tonic-gate 		diff -= mmu_btopr(diff * sizeof (struct page));
20850Sstevel@tonic-gate 		kseg.s_as = &kas;
20860Sstevel@tonic-gate 		while (diff--) {
20870Sstevel@tonic-gate 			pp = page_create_va(&unused_pages_vp, (offset_t)off,
20880Sstevel@tonic-gate 			    MMU_PAGESIZE, PG_WAIT | PG_EXCL,
20890Sstevel@tonic-gate 			    &kseg, (caddr_t)off);
20900Sstevel@tonic-gate 			if (pp == NULL)
20910Sstevel@tonic-gate 				cmn_err(CE_PANIC, "limited physmem too much!");
20920Sstevel@tonic-gate 			page_io_unlock(pp);
20930Sstevel@tonic-gate 			page_downgrade(pp);
20940Sstevel@tonic-gate 			availrmem--;
20950Sstevel@tonic-gate 			off += MMU_PAGESIZE;
20960Sstevel@tonic-gate 		}
20970Sstevel@tonic-gate 	}
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	/*
21000Sstevel@tonic-gate 	 * When printing memory, show the total as physmem less
21010Sstevel@tonic-gate 	 * that stolen by a debugger.
21020Sstevel@tonic-gate 	 */
21030Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %ldK (0x%lx000)\n",
21040Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 10),
21050Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 12));
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 	avmem = (uint64_t)freemem << PAGESHIFT;
21080Sstevel@tonic-gate 	cmn_err(CE_CONT, "?avail mem = %lld\n", (unsigned long long)avmem);
21090Sstevel@tonic-gate 
21102991Ssusans 	/*
21112991Ssusans 	 * For small memory systems disable automatic large pages.
21122991Ssusans 	 */
21132991Ssusans 	if (physmem < privm_lpg_min_physmem) {
2114423Sdavemq 		use_brk_lpg = 0;
2115423Sdavemq 		use_stk_lpg = 0;
2116423Sdavemq 	}
2117423Sdavemq 
21180Sstevel@tonic-gate 	/*
21190Sstevel@tonic-gate 	 * Perform platform specific freelist processing
21200Sstevel@tonic-gate 	 */
21210Sstevel@tonic-gate 	if (&plat_freelist_process) {
21220Sstevel@tonic-gate 		for (mnode = 0; mnode < max_mem_nodes; mnode++)
21230Sstevel@tonic-gate 			if (mem_node_config[mnode].exists)
21240Sstevel@tonic-gate 				plat_freelist_process(mnode);
21250Sstevel@tonic-gate 	}
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	/*
21280Sstevel@tonic-gate 	 * Initialize the segkp segment type.  We position it
21290Sstevel@tonic-gate 	 * after the configured tables and buffers (whose end
21300Sstevel@tonic-gate 	 * is given by econtig) and before V_WKBASE_ADDR.
21310Sstevel@tonic-gate 	 * Also in this area is segkmap (size SEGMAPSIZE).
21320Sstevel@tonic-gate 	 */
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	/* XXX - cache alignment? */
21350Sstevel@tonic-gate 	va = (caddr_t)SEGKPBASE;
21360Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & PAGEOFFSET) == 0);
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	max_phys_segkp = (physmem * 2);
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	if (segkpsize < btop(SEGKPMINSIZE) || segkpsize > btop(SEGKPMAXSIZE)) {
21410Sstevel@tonic-gate 		segkpsize = btop(SEGKPDEFSIZE);
21420Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal value for segkpsize. "
21430Sstevel@tonic-gate 		    "segkpsize has been reset to %ld pages", segkpsize);
21440Sstevel@tonic-gate 	}
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	i = ptob(MIN(segkpsize, max_phys_segkp));
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
21490Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkp) < 0)
21500Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: cannot attach segkp");
21510Sstevel@tonic-gate 	if (segkp_create(segkp) != 0)
21520Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: segkp_create failed");
21530Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	/*
21560Sstevel@tonic-gate 	 * kpm segment
21570Sstevel@tonic-gate 	 */
21580Sstevel@tonic-gate 	segmap_kpm = kpm_enable &&
21590Sstevel@tonic-gate 		segmap_kpm && PAGESIZE == MAXBSIZE;
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	if (kpm_enable) {
21620Sstevel@tonic-gate 		rw_enter(&kas.a_lock, RW_WRITER);
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 		/*
21650Sstevel@tonic-gate 		 * The segkpm virtual range range is larger than the
21660Sstevel@tonic-gate 		 * actual physical memory size and also covers gaps in
21670Sstevel@tonic-gate 		 * the physical address range for the following reasons:
21680Sstevel@tonic-gate 		 * . keep conversion between segkpm and physical addresses
21690Sstevel@tonic-gate 		 *   simple, cheap and unambiguous.
21700Sstevel@tonic-gate 		 * . avoid extension/shrink of the the segkpm in case of DR.
21710Sstevel@tonic-gate 		 * . avoid complexity for handling of virtual addressed
21720Sstevel@tonic-gate 		 *   caches, segkpm and the regular mapping scheme must be
21730Sstevel@tonic-gate 		 *   kept in sync wrt. the virtual color of mapped pages.
21740Sstevel@tonic-gate 		 * Any accesses to virtual segkpm ranges not backed by
21750Sstevel@tonic-gate 		 * physical memory will fall through the memseg pfn hash
21760Sstevel@tonic-gate 		 * and will be handled in segkpm_fault.
21770Sstevel@tonic-gate 		 * Additional kpm_size spaces needed for vac alias prevention.
21780Sstevel@tonic-gate 		 */
21790Sstevel@tonic-gate 		if (seg_attach(&kas, kpm_vbase, kpm_size * vac_colors,
21800Sstevel@tonic-gate 		    segkpm) < 0)
21810Sstevel@tonic-gate 			cmn_err(CE_PANIC, "cannot attach segkpm");
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 		b.prot = PROT_READ | PROT_WRITE;
21840Sstevel@tonic-gate 		b.nvcolors = shm_alignment >> MMU_PAGESHIFT;
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 		if (segkpm_create(segkpm, (caddr_t)&b) != 0)
21870Sstevel@tonic-gate 			panic("segkpm_create segkpm");
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 		rw_exit(&kas.a_lock);
21902296Sae112802 
21912296Sae112802 		mach_kpm_init();
21920Sstevel@tonic-gate 	}
21930Sstevel@tonic-gate 
21943290Sjohansen 	if (!segzio_fromheap) {
21953290Sjohansen 		size_t size;
21963552Sjohansen 		size_t physmem_b = mmu_ptob(physmem);
21973290Sjohansen 
21983290Sjohansen 		/* size is in bytes, segziosize is in pages */
21993290Sjohansen 		if (segziosize == 0) {
22003552Sjohansen 			size = physmem_b;
22013290Sjohansen 		} else {
22023290Sjohansen 			size = mmu_ptob(segziosize);
22033290Sjohansen 		}
22043290Sjohansen 
22053290Sjohansen 		if (size < SEGZIOMINSIZE) {
22063290Sjohansen 			size = SEGZIOMINSIZE;
22073552Sjohansen 		} else if (size > SEGZIOMAXSIZE) {
22083552Sjohansen 			size = SEGZIOMAXSIZE;
22093552Sjohansen 			/*
22103552Sjohansen 			 * On 64-bit x86, we only have 2TB of KVA.  This exists
22113552Sjohansen 			 * for parity with x86.
22123552Sjohansen 			 *
22133552Sjohansen 			 * SEGZIOMAXSIZE is capped at 512gb so that segzio
22143552Sjohansen 			 * doesn't consume all of KVA.  However, if we have a
22153552Sjohansen 			 * system that has more thant 512gb of physical memory,
22163552Sjohansen 			 * we can actually consume about half of the difference
22173552Sjohansen 			 * between 512gb and the rest of the available physical
22183552Sjohansen 			 * memory.
22193552Sjohansen 			 */
22203552Sjohansen 			if (physmem_b > SEGZIOMAXSIZE) {
22213552Sjohansen 				size += (physmem_b - SEGZIOMAXSIZE) / 2;
22223552Sjohansen 		}
22233290Sjohansen 		}
22243290Sjohansen 		segziosize = mmu_btop(roundup(size, MMU_PAGESIZE));
22253290Sjohansen 		/* put the base of the ZIO segment after the kpm segment */
22263290Sjohansen 		segzio_base = kpm_vbase + (kpm_size * vac_colors);
22273290Sjohansen 		PRM_DEBUG(segziosize);
22283290Sjohansen 		PRM_DEBUG(segzio_base);
22293290Sjohansen 
22303290Sjohansen 		/*
22313290Sjohansen 		 * On some platforms, kvm_init is called after the kpm
22323290Sjohansen 		 * sizes have been determined.  On SPARC, kvm_init is called
22333290Sjohansen 		 * before, so we have to attach the kzioseg after kvm is
22343290Sjohansen 		 * initialized, otherwise we'll try to allocate from the boot
22353290Sjohansen 		 * area since the kernel heap hasn't yet been configured.
22363290Sjohansen 		 */
22373290Sjohansen 		rw_enter(&kas.a_lock, RW_WRITER);
22383290Sjohansen 
22393290Sjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
22403290Sjohansen 		    &kzioseg);
22413290Sjohansen 		(void) segkmem_zio_create(&kzioseg);
22423290Sjohansen 
22433290Sjohansen 		/* create zio area covering new segment */
22443290Sjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
22453290Sjohansen 
22463290Sjohansen 		rw_exit(&kas.a_lock);
22473290Sjohansen 	}
22483290Sjohansen 
22493290Sjohansen 
22500Sstevel@tonic-gate 	/*
22510Sstevel@tonic-gate 	 * Now create generic mapping segment.  This mapping
22520Sstevel@tonic-gate 	 * goes SEGMAPSIZE beyond SEGMAPBASE.  But if the total
22530Sstevel@tonic-gate 	 * virtual address is greater than the amount of free
22540Sstevel@tonic-gate 	 * memory that is available, then we trim back the
22550Sstevel@tonic-gate 	 * segment size to that amount
22560Sstevel@tonic-gate 	 */
22570Sstevel@tonic-gate 	va = (caddr_t)SEGMAPBASE;
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	/*
22600Sstevel@tonic-gate 	 * 1201049: segkmap base address must be MAXBSIZE aligned
22610Sstevel@tonic-gate 	 */
22620Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & MAXBOFFSET) == 0);
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 	/*
22650Sstevel@tonic-gate 	 * Set size of segmap to percentage of freemem at boot,
22660Sstevel@tonic-gate 	 * but stay within the allowable range
22670Sstevel@tonic-gate 	 * Note we take percentage  before converting from pages
22680Sstevel@tonic-gate 	 * to bytes to avoid an overflow on 32-bit kernels.
22690Sstevel@tonic-gate 	 */
22700Sstevel@tonic-gate 	i = mmu_ptob((freemem * segmap_percent) / 100);
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	if (i < MINMAPSIZE)
22730Sstevel@tonic-gate 		i = MINMAPSIZE;
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	if (i > MIN(SEGMAPSIZE, mmu_ptob(freemem)))
22760Sstevel@tonic-gate 		i = MIN(SEGMAPSIZE, mmu_ptob(freemem));
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	i &= MAXBMASK;	/* 1201049: segkmap size must be MAXBSIZE aligned */
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
22810Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkmap) < 0)
22820Sstevel@tonic-gate 		cmn_err(CE_PANIC, "cannot attach segkmap");
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
22850Sstevel@tonic-gate 	a.shmsize = shm_alignment;
22860Sstevel@tonic-gate 	a.nfreelist = 0;	/* use segmap driver defaults */
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	if (segmap_create(segkmap, (caddr_t)&a) != 0)
22890Sstevel@tonic-gate 		panic("segmap_create segkmap");
22900Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 	segdev_init();
22930Sstevel@tonic-gate }
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate static void
22960Sstevel@tonic-gate startup_end(void)
22970Sstevel@tonic-gate {
22980Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
22990Sstevel@tonic-gate 		panic("memlist overflow 2");
23000Sstevel@tonic-gate 	memlist_free_block((caddr_t)memlist,
23010Sstevel@tonic-gate 	    ((caddr_t)memlist_end - (caddr_t)memlist));
23020Sstevel@tonic-gate 	memlist = NULL;
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	/* enable page_relocation since OBP is now done */
23050Sstevel@tonic-gate 	page_relocate_ready = 1;
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	/*
23080Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
23090Sstevel@tonic-gate 	 * initialization has been done but before the clock
23100Sstevel@tonic-gate 	 * and other devices get started.
23110Sstevel@tonic-gate 	 */
23120Sstevel@tonic-gate 	kern_setup1();
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 	/*
23150Sstevel@tonic-gate 	 * Intialize the VM arenas for allocating physically
23160Sstevel@tonic-gate 	 * contiguus memory chunk for interrupt queues snd
23170Sstevel@tonic-gate 	 * allocate/register boot cpu's queues, if any and
23180Sstevel@tonic-gate 	 * allocate dump buffer for sun4v systems to store
23190Sstevel@tonic-gate 	 * extra crash information during crash dump
23200Sstevel@tonic-gate 	 */
23210Sstevel@tonic-gate 	contig_mem_init();
23220Sstevel@tonic-gate 	mach_descrip_init();
23230Sstevel@tonic-gate 	cpu_intrq_setup(CPU);
23240Sstevel@tonic-gate 	cpu_intrq_register(CPU);
23251077Ssvemuri 	mach_htraptrace_setup(CPU->cpu_id);
23261077Ssvemuri 	mach_htraptrace_configure(CPU->cpu_id);
23270Sstevel@tonic-gate 	mach_dump_buffer_init();
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 	/*
23300Sstevel@tonic-gate 	 * Initialize interrupt related stuff
23310Sstevel@tonic-gate 	 */
23321455Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 	(void) splzs();			/* allow hi clock ints but not zs */
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	/*
23370Sstevel@tonic-gate 	 * Initialize errors.
23380Sstevel@tonic-gate 	 */
23390Sstevel@tonic-gate 	error_init();
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	/*
23420Sstevel@tonic-gate 	 * Note that we may have already used kernel bcopy before this
23430Sstevel@tonic-gate 	 * point - but if you really care about this, adb the use_hw_*
23440Sstevel@tonic-gate 	 * variables to 0 before rebooting.
23450Sstevel@tonic-gate 	 */
23460Sstevel@tonic-gate 	mach_hw_copy_limit();
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	/*
23490Sstevel@tonic-gate 	 * Install the "real" preemption guards before DDI services
23500Sstevel@tonic-gate 	 * are available.
23510Sstevel@tonic-gate 	 */
23520Sstevel@tonic-gate 	(void) prom_set_preprom(kern_preprom);
23530Sstevel@tonic-gate 	(void) prom_set_postprom(kern_postprom);
23540Sstevel@tonic-gate 	CPU->cpu_m.mutex_ready = 1;
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 	/*
23570Sstevel@tonic-gate 	 * Initialize segnf (kernel support for non-faulting loads).
23580Sstevel@tonic-gate 	 */
23590Sstevel@tonic-gate 	segnf_init();
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	/*
23620Sstevel@tonic-gate 	 * Configure the root devinfo node.
23630Sstevel@tonic-gate 	 */
23640Sstevel@tonic-gate 	configure();		/* set up devices */
23650Sstevel@tonic-gate 	mach_cpu_halt_idle();
23660Sstevel@tonic-gate }
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate void
23700Sstevel@tonic-gate post_startup(void)
23710Sstevel@tonic-gate {
23720Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
23730Sstevel@tonic-gate 	extern void init_ptl1_thread(void);
23740Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
23750Sstevel@tonic-gate 	extern void abort_sequence_init(void);
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	/*
23780Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
23790Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
23800Sstevel@tonic-gate 	 * instruction set extensions.
23810Sstevel@tonic-gate 	 */
23820Sstevel@tonic-gate 	bind_hwcap();
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	/*
23850Sstevel@tonic-gate 	 * Startup memory scrubber (if any)
23860Sstevel@tonic-gate 	 */
23870Sstevel@tonic-gate 	mach_memscrub();
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate 	/*
23900Sstevel@tonic-gate 	 * Allocate soft interrupt to handle abort sequence.
23910Sstevel@tonic-gate 	 */
23920Sstevel@tonic-gate 	abort_sequence_init();
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	/*
23950Sstevel@tonic-gate 	 * Configure the rest of the system.
23960Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
23970Sstevel@tonic-gate 	 */
23980Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
23990Sstevel@tonic-gate 	/*
24000Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
24010Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
24020Sstevel@tonic-gate 	 */
24030Sstevel@tonic-gate 	(void) modload("fs", "procfs");
24040Sstevel@tonic-gate 
24051991Sheppo 	/* load machine class specific drivers */
24061991Sheppo 	load_mach_drivers();
24071991Sheppo 
24081991Sheppo 	/* load platform specific drivers */
24090Sstevel@tonic-gate 	if (&load_platform_drivers)
24100Sstevel@tonic-gate 		load_platform_drivers();
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	/* load vis simulation module, if we are running w/fpu off */
24130Sstevel@tonic-gate 	if (!fpu_exists) {
24140Sstevel@tonic-gate 		if (modload("misc", "vis") == -1)
24150Sstevel@tonic-gate 			halt("Can't load vis");
24160Sstevel@tonic-gate 	}
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 	mach_fpras();
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	maxmem = freemem;
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
24230Sstevel@tonic-gate 	init_ptl1_thread();
24240Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
24251991Sheppo 
24261991Sheppo 	if (&cif_init)
24271991Sheppo 		cif_init();
24280Sstevel@tonic-gate }
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
24310Sstevel@tonic-gate int		ptl1_panic_test = 0;
24320Sstevel@tonic-gate int		ptl1_panic_xc_one_test = 0;
24330Sstevel@tonic-gate int		ptl1_panic_xc_all_test = 0;
24340Sstevel@tonic-gate int		ptl1_panic_xt_one_test = 0;
24350Sstevel@tonic-gate int		ptl1_panic_xt_all_test = 0;
24360Sstevel@tonic-gate kthread_id_t	ptl1_thread_p = NULL;
24370Sstevel@tonic-gate kcondvar_t	ptl1_cv;
24380Sstevel@tonic-gate kmutex_t	ptl1_mutex;
24390Sstevel@tonic-gate int		ptl1_recurse_count_threshold = 0x40;
24400Sstevel@tonic-gate int		ptl1_recurse_trap_threshold = 0x3d;
24410Sstevel@tonic-gate extern void	ptl1_recurse(int, int);
24420Sstevel@tonic-gate extern void	ptl1_panic_xt(int, int);
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate /*
24450Sstevel@tonic-gate  * Called once per second by timeout() to wake up
24460Sstevel@tonic-gate  * the ptl1_panic thread to see if it should cause
24470Sstevel@tonic-gate  * a trap to the ptl1_panic() code.
24480Sstevel@tonic-gate  */
24490Sstevel@tonic-gate /* ARGSUSED */
24500Sstevel@tonic-gate static void
24510Sstevel@tonic-gate ptl1_wakeup(void *arg)
24520Sstevel@tonic-gate {
24530Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
24540Sstevel@tonic-gate 	cv_signal(&ptl1_cv);
24550Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
24560Sstevel@tonic-gate }
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate /*
24590Sstevel@tonic-gate  * ptl1_panic cross call function:
24600Sstevel@tonic-gate  *     Needed because xc_one() and xc_some() can pass
24610Sstevel@tonic-gate  *	64 bit args but ptl1_recurse() expects ints.
24620Sstevel@tonic-gate  */
24630Sstevel@tonic-gate static void
24640Sstevel@tonic-gate ptl1_panic_xc(void)
24650Sstevel@tonic-gate {
24660Sstevel@tonic-gate 	ptl1_recurse(ptl1_recurse_count_threshold,
24670Sstevel@tonic-gate 	    ptl1_recurse_trap_threshold);
24680Sstevel@tonic-gate }
24690Sstevel@tonic-gate 
24700Sstevel@tonic-gate /*
24710Sstevel@tonic-gate  * The ptl1 thread waits for a global flag to be set
24720Sstevel@tonic-gate  * and uses the recurse thresholds to set the stack depth
24730Sstevel@tonic-gate  * to cause a ptl1_panic() directly via a call to ptl1_recurse
24740Sstevel@tonic-gate  * or indirectly via the cross call and cross trap functions.
24750Sstevel@tonic-gate  *
24760Sstevel@tonic-gate  * This is useful testing stack overflows and normal
24770Sstevel@tonic-gate  * ptl1_panic() states with a know stack frame.
24780Sstevel@tonic-gate  *
24790Sstevel@tonic-gate  * ptl1_recurse() is an asm function in ptl1_panic.s that
24800Sstevel@tonic-gate  * sets the {In, Local, Out, and Global} registers to a
24810Sstevel@tonic-gate  * know state on the stack and just prior to causing a
24820Sstevel@tonic-gate  * test ptl1_panic trap.
24830Sstevel@tonic-gate  */
24840Sstevel@tonic-gate static void
24850Sstevel@tonic-gate ptl1_thread(void)
24860Sstevel@tonic-gate {
24870Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
24880Sstevel@tonic-gate 	while (ptl1_thread_p) {
24890Sstevel@tonic-gate 		cpuset_t	other_cpus;
24900Sstevel@tonic-gate 		int		cpu_id;
24910Sstevel@tonic-gate 		int		my_cpu_id;
24920Sstevel@tonic-gate 		int		target_cpu_id;
24930Sstevel@tonic-gate 		int		target_found;
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate 		if (ptl1_panic_test) {
24960Sstevel@tonic-gate 			ptl1_recurse(ptl1_recurse_count_threshold,
24970Sstevel@tonic-gate 			    ptl1_recurse_trap_threshold);
24980Sstevel@tonic-gate 		}
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 		/*
25010Sstevel@tonic-gate 		 * Find potential targets for x-call and x-trap,
25020Sstevel@tonic-gate 		 * if any exist while preempt is disabled we
25030Sstevel@tonic-gate 		 * start a ptl1_panic if requested via a
25040Sstevel@tonic-gate 		 * globals.
25050Sstevel@tonic-gate 		 */
25060Sstevel@tonic-gate 		kpreempt_disable();
25070Sstevel@tonic-gate 		my_cpu_id = CPU->cpu_id;
25080Sstevel@tonic-gate 		other_cpus = cpu_ready_set;
25090Sstevel@tonic-gate 		CPUSET_DEL(other_cpus, CPU->cpu_id);
25100Sstevel@tonic-gate 		target_found = 0;
25110Sstevel@tonic-gate 		if (!CPUSET_ISNULL(other_cpus)) {
25120Sstevel@tonic-gate 			/*
25130Sstevel@tonic-gate 			 * Pick the first one
25140Sstevel@tonic-gate 			 */
25150Sstevel@tonic-gate 			for (cpu_id = 0; cpu_id < NCPU; cpu_id++) {
25160Sstevel@tonic-gate 				if (cpu_id == my_cpu_id)
25170Sstevel@tonic-gate 					continue;
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 				if (CPU_XCALL_READY(cpu_id)) {
25200Sstevel@tonic-gate 					target_cpu_id = cpu_id;
25210Sstevel@tonic-gate 					target_found = 1;
25220Sstevel@tonic-gate 					break;
25230Sstevel@tonic-gate 				}
25240Sstevel@tonic-gate 			}
25250Sstevel@tonic-gate 			ASSERT(target_found);
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 			if (ptl1_panic_xc_one_test) {
25280Sstevel@tonic-gate 				xc_one(target_cpu_id,
25290Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
25300Sstevel@tonic-gate 			}
25310Sstevel@tonic-gate 			if (ptl1_panic_xc_all_test) {
25320Sstevel@tonic-gate 				xc_some(other_cpus,
25330Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
25340Sstevel@tonic-gate 			}
25350Sstevel@tonic-gate 			if (ptl1_panic_xt_one_test) {
25360Sstevel@tonic-gate 				xt_one(target_cpu_id,
25370Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
25380Sstevel@tonic-gate 			}
25390Sstevel@tonic-gate 			if (ptl1_panic_xt_all_test) {
25400Sstevel@tonic-gate 				xt_some(other_cpus,
25410Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
25420Sstevel@tonic-gate 			}
25430Sstevel@tonic-gate 		}
25440Sstevel@tonic-gate 		kpreempt_enable();
25450Sstevel@tonic-gate 		(void) timeout(ptl1_wakeup, NULL, hz);
25460Sstevel@tonic-gate 		(void) cv_wait(&ptl1_cv, &ptl1_mutex);
25470Sstevel@tonic-gate 	}
25480Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
25490Sstevel@tonic-gate }
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate /*
25520Sstevel@tonic-gate  * Called during early startup to create the ptl1_thread
25530Sstevel@tonic-gate  */
25540Sstevel@tonic-gate void
25550Sstevel@tonic-gate init_ptl1_thread(void)
25560Sstevel@tonic-gate {
25570Sstevel@tonic-gate 	ptl1_thread_p = thread_create(NULL, 0, ptl1_thread, NULL, 0,
25580Sstevel@tonic-gate 	    &p0, TS_RUN, 0);
25590Sstevel@tonic-gate }
25600Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate /*
25640Sstevel@tonic-gate  * Add to a memory list.
25650Sstevel@tonic-gate  * start = start of new memory segment
25660Sstevel@tonic-gate  * len = length of new memory segment in bytes
25670Sstevel@tonic-gate  * memlistp = pointer to array of available memory segment structures
25680Sstevel@tonic-gate  * curmemlistp = memory list to which to add segment.
25690Sstevel@tonic-gate  */
25700Sstevel@tonic-gate static void
25710Sstevel@tonic-gate memlist_add(uint64_t start, uint64_t len, struct memlist **memlistp,
25720Sstevel@tonic-gate 	struct memlist **curmemlistp)
25730Sstevel@tonic-gate {
25740Sstevel@tonic-gate 	struct memlist *new;
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate 	new = *memlistp;
25770Sstevel@tonic-gate 	new->address = start;
25780Sstevel@tonic-gate 	new->size = len;
25790Sstevel@tonic-gate 	*memlistp = new + 1;
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	memlist_insert(new, curmemlistp);
25820Sstevel@tonic-gate }
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate /*
25850Sstevel@tonic-gate  * In the case of architectures that support dynamic addition of
25860Sstevel@tonic-gate  * memory at run-time there are two cases where memsegs need to
25870Sstevel@tonic-gate  * be initialized and added to the memseg list.
25880Sstevel@tonic-gate  * 1) memsegs that are constructed at startup.
25890Sstevel@tonic-gate  * 2) memsegs that are constructed at run-time on
25900Sstevel@tonic-gate  *    hot-plug capable architectures.
25910Sstevel@tonic-gate  * This code was originally part of the function kphysm_init().
25920Sstevel@tonic-gate  */
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate static void
25950Sstevel@tonic-gate memseg_list_add(struct memseg *memsegp)
25960Sstevel@tonic-gate {
25970Sstevel@tonic-gate 	struct memseg **prev_memsegp;
25980Sstevel@tonic-gate 	pgcnt_t num;
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 	/* insert in memseg list, decreasing number of pages order */
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 	num = MSEG_NPAGES(memsegp);
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	for (prev_memsegp = &memsegs; *prev_memsegp;
26050Sstevel@tonic-gate 	    prev_memsegp = &((*prev_memsegp)->next)) {
26060Sstevel@tonic-gate 		if (num > MSEG_NPAGES(*prev_memsegp))
26070Sstevel@tonic-gate 			break;
26080Sstevel@tonic-gate 	}
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 	memsegp->next = *prev_memsegp;
26110Sstevel@tonic-gate 	*prev_memsegp = memsegp;
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 	if (kpm_enable) {
26140Sstevel@tonic-gate 		memsegp->nextpa = (memsegp->next) ?
26150Sstevel@tonic-gate 			va_to_pa(memsegp->next) : MSEG_NULLPTR_PA;
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 		if (prev_memsegp != &memsegs) {
26180Sstevel@tonic-gate 			struct memseg *msp;
26190Sstevel@tonic-gate 			msp = (struct memseg *)((caddr_t)prev_memsegp -
26200Sstevel@tonic-gate 				offsetof(struct memseg, next));
26210Sstevel@tonic-gate 			msp->nextpa = va_to_pa(memsegp);
26220Sstevel@tonic-gate 		} else {
26230Sstevel@tonic-gate 			memsegspa = va_to_pa(memsegs);
26240Sstevel@tonic-gate 		}
26250Sstevel@tonic-gate 	}
26260Sstevel@tonic-gate }
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate /*
26290Sstevel@tonic-gate  * PSM add_physmem_cb(). US-II and newer processors have some
26300Sstevel@tonic-gate  * flavor of the prefetch capability implemented. We exploit
26310Sstevel@tonic-gate  * this capability for optimum performance.
26320Sstevel@tonic-gate  */
26330Sstevel@tonic-gate #define	PREFETCH_BYTES	64
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate void
26360Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
26370Sstevel@tonic-gate {
26380Sstevel@tonic-gate 	extern void	 prefetch_page_w(void *);
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	pp->p_pagenum = pnum;
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	/*
26430Sstevel@tonic-gate 	 * Prefetch one more page_t into E$. To prevent future
26440Sstevel@tonic-gate 	 * mishaps with the sizeof(page_t) changing on us, we
26450Sstevel@tonic-gate 	 * catch this on debug kernels if we can't bring in the
26460Sstevel@tonic-gate 	 * entire hpage with 2 PREFETCH_BYTES reads. See
26470Sstevel@tonic-gate 	 * also, sun4u/cpu/cpu_module.c
26480Sstevel@tonic-gate 	 */
26490Sstevel@tonic-gate 	/*LINTED*/
26500Sstevel@tonic-gate 	ASSERT(sizeof (page_t) <= 2*PREFETCH_BYTES);
26510Sstevel@tonic-gate 	prefetch_page_w((char *)pp);
26520Sstevel@tonic-gate }
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate /*
26550Sstevel@tonic-gate  * kphysm_init() tackles the problem of initializing physical memory.
26560Sstevel@tonic-gate  * The old startup made some assumptions about the kernel living in
26570Sstevel@tonic-gate  * physically contiguous space which is no longer valid.
26580Sstevel@tonic-gate  */
26590Sstevel@tonic-gate static void
26600Sstevel@tonic-gate kphysm_init(page_t *pp, struct memseg *memsegp, pgcnt_t npages,
26610Sstevel@tonic-gate 	uintptr_t kpm_pp, pgcnt_t kpm_npages)
26620Sstevel@tonic-gate {
26630Sstevel@tonic-gate 	struct memlist	*pmem;
26640Sstevel@tonic-gate 	struct memseg	*msp;
26650Sstevel@tonic-gate 	pfn_t		 base;
26660Sstevel@tonic-gate 	pgcnt_t		 num;
26670Sstevel@tonic-gate 	pfn_t		 lastseg_pages_end = 0;
26680Sstevel@tonic-gate 	pgcnt_t		 nelem_used = 0;
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 	msp = memsegp;
26730Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 		/*
26760Sstevel@tonic-gate 		 * Build the memsegs entry
26770Sstevel@tonic-gate 		 */
26780Sstevel@tonic-gate 		num = btop(pmem->size);
26790Sstevel@tonic-gate 		if (num > npages)
26800Sstevel@tonic-gate 			num = npages;
26810Sstevel@tonic-gate 		npages -= num;
26820Sstevel@tonic-gate 		base = btop(pmem->address);
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 		msp->pages = pp;
26850Sstevel@tonic-gate 		msp->epages = pp + num;
26860Sstevel@tonic-gate 		msp->pages_base = base;
26870Sstevel@tonic-gate 		msp->pages_end = base + num;
26880Sstevel@tonic-gate 
26890Sstevel@tonic-gate 		if (kpm_enable) {
26900Sstevel@tonic-gate 			pfn_t pbase_a;
26910Sstevel@tonic-gate 			pfn_t pend_a;
26920Sstevel@tonic-gate 			pfn_t prev_pend_a;
26930Sstevel@tonic-gate 			pgcnt_t	nelem;
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 			msp->pagespa = va_to_pa(pp);
26960Sstevel@tonic-gate 			msp->epagespa = va_to_pa(pp + num);
26970Sstevel@tonic-gate 			pbase_a = kpmptop(ptokpmp(base));
26980Sstevel@tonic-gate 			pend_a = kpmptop(ptokpmp(base + num - 1)) + kpmpnpgs;
26990Sstevel@tonic-gate 			nelem = ptokpmp(pend_a - pbase_a);
27000Sstevel@tonic-gate 			msp->kpm_nkpmpgs = nelem;
27010Sstevel@tonic-gate 			msp->kpm_pbase = pbase_a;
27020Sstevel@tonic-gate 			if (lastseg_pages_end) {
27030Sstevel@tonic-gate 				/*
27040Sstevel@tonic-gate 				 * Assume phys_avail is in ascending order
27050Sstevel@tonic-gate 				 * of physical addresses.
27060Sstevel@tonic-gate 				 */
27070Sstevel@tonic-gate 				ASSERT(base + num > lastseg_pages_end);
27080Sstevel@tonic-gate 				prev_pend_a = kpmptop(
27090Sstevel@tonic-gate 				    ptokpmp(lastseg_pages_end - 1)) + kpmpnpgs;
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 				if (prev_pend_a > pbase_a) {
27120Sstevel@tonic-gate 					/*
27130Sstevel@tonic-gate 					 * Overlap, more than one memseg may
27140Sstevel@tonic-gate 					 * point to the same kpm_page range.
27150Sstevel@tonic-gate 					 */
27160Sstevel@tonic-gate 					if (kpm_smallpages == 0) {
27170Sstevel@tonic-gate 						msp->kpm_pages =
27180Sstevel@tonic-gate 						    (kpm_page_t *)kpm_pp - 1;
27190Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
27200Sstevel@tonic-gate 							((kpm_page_t *)kpm_pp
27210Sstevel@tonic-gate 							+ nelem - 1);
27220Sstevel@tonic-gate 					} else {
27230Sstevel@tonic-gate 						msp->kpm_spages =
27240Sstevel@tonic-gate 						    (kpm_spage_t *)kpm_pp - 1;
27250Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
27260Sstevel@tonic-gate 							((kpm_spage_t *)kpm_pp
27270Sstevel@tonic-gate 							+ nelem - 1);
27280Sstevel@tonic-gate 					}
27290Sstevel@tonic-gate 					nelem_used += nelem - 1;
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 				} else {
27320Sstevel@tonic-gate 					if (kpm_smallpages == 0) {
27330Sstevel@tonic-gate 						msp->kpm_pages =
27340Sstevel@tonic-gate 						    (kpm_page_t *)kpm_pp;
27350Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
27360Sstevel@tonic-gate 							((kpm_page_t *)kpm_pp
27370Sstevel@tonic-gate 							+ nelem);
27380Sstevel@tonic-gate 					} else {
27390Sstevel@tonic-gate 						msp->kpm_spages =
27400Sstevel@tonic-gate 						    (kpm_spage_t *)kpm_pp;
27410Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
27420Sstevel@tonic-gate 							((kpm_spage_t *)
27430Sstevel@tonic-gate 							kpm_pp + nelem);
27440Sstevel@tonic-gate 					}
27450Sstevel@tonic-gate 					nelem_used += nelem;
27460Sstevel@tonic-gate 				}
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 			} else {
27490Sstevel@tonic-gate 				if (kpm_smallpages == 0) {
27500Sstevel@tonic-gate 					msp->kpm_pages = (kpm_page_t *)kpm_pp;
27510Sstevel@tonic-gate 					kpm_pp = (uintptr_t)
27520Sstevel@tonic-gate 						((kpm_page_t *)kpm_pp + nelem);
27530Sstevel@tonic-gate 				} else {
27540Sstevel@tonic-gate 					msp->kpm_spages = (kpm_spage_t *)kpm_pp;
27550Sstevel@tonic-gate 					kpm_pp = (uintptr_t)
27560Sstevel@tonic-gate 						((kpm_spage_t *)kpm_pp + nelem);
27570Sstevel@tonic-gate 				}
27580Sstevel@tonic-gate 				nelem_used = nelem;
27590Sstevel@tonic-gate 			}
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 			if (nelem_used > kpm_npages)
27620Sstevel@tonic-gate 				panic("kphysm_init: kpm_pp overflow\n");
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate 			msp->kpm_pagespa = va_to_pa(msp->kpm_pages);
27650Sstevel@tonic-gate 			lastseg_pages_end = msp->pages_end;
27660Sstevel@tonic-gate 		}
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 		memseg_list_add(msp);
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 		/*
27710Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
27720Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
27730Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
27740Sstevel@tonic-gate 		 * it initializes them.
27750Sstevel@tonic-gate 		 */
27760Sstevel@tonic-gate 		add_physmem(pp, num, base);
27770Sstevel@tonic-gate 		pp += num;
27780Sstevel@tonic-gate 		msp++;
27790Sstevel@tonic-gate 	}
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	build_pfn_hash();
27820Sstevel@tonic-gate }
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate /*
27850Sstevel@tonic-gate  * Kernel VM initialization.
27860Sstevel@tonic-gate  * Assumptions about kernel address space ordering:
27870Sstevel@tonic-gate  *	(1) gap (user space)
27880Sstevel@tonic-gate  *	(2) kernel text
27890Sstevel@tonic-gate  *	(3) kernel data/bss
27900Sstevel@tonic-gate  *	(4) gap
27910Sstevel@tonic-gate  *	(5) kernel data structures
27920Sstevel@tonic-gate  *	(6) gap
27930Sstevel@tonic-gate  *	(7) debugger (optional)
27940Sstevel@tonic-gate  *	(8) monitor
27950Sstevel@tonic-gate  *	(9) gap (possibly null)
27960Sstevel@tonic-gate  *	(10) dvma
27970Sstevel@tonic-gate  *	(11) devices
27980Sstevel@tonic-gate  */
27990Sstevel@tonic-gate static void
28000Sstevel@tonic-gate kvm_init(void)
28010Sstevel@tonic-gate {
28020Sstevel@tonic-gate 	/*
28030Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
28040Sstevel@tonic-gate 	 */
28050Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
28060Sstevel@tonic-gate 	as_avlinit(&kas);
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)KERNELBASE,
28090Sstevel@tonic-gate 	    (size_t)(e_moddata - KERNELBASE), &ktextseg);
28100Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)(KERNELBASE + MMU_PAGESIZE4M),
28130Sstevel@tonic-gate 	    (size_t)(MMU_PAGESIZE4M), &ktexthole);
28140Sstevel@tonic-gate 	(void) segkmem_create(&ktexthole);
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base,
28170Sstevel@tonic-gate 	    (size_t)(econtig32 - valloc_base), &kvalloc);
28180Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate 	if (kmem64_base) {
28210Sstevel@tonic-gate 	    (void) seg_attach(&kas, (caddr_t)kmem64_base,
28220Sstevel@tonic-gate 		(size_t)(kmem64_end - kmem64_base), &kmem64);
28230Sstevel@tonic-gate 	    (void) segkmem_create(&kmem64);
28240Sstevel@tonic-gate 	}
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 	/*
28270Sstevel@tonic-gate 	 * We're about to map out /boot.  This is the beginning of the
28280Sstevel@tonic-gate 	 * system resource management transition. We can no longer
28290Sstevel@tonic-gate 	 * call into /boot for I/O or memory allocations.
28300Sstevel@tonic-gate 	 */
28310Sstevel@tonic-gate 	(void) seg_attach(&kas, kernelheap, ekernelheap - kernelheap, &kvseg);
28320Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
28330Sstevel@tonic-gate 	hblk_alloc_dynamic = 1;
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate 	/*
28360Sstevel@tonic-gate 	 * we need to preallocate pages for DR operations before enabling large
28370Sstevel@tonic-gate 	 * page kernel heap because of memseg_remap_init() hat_unload() hack.
28380Sstevel@tonic-gate 	 */
28390Sstevel@tonic-gate 	memseg_remap_init();
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	/* at this point we are ready to use large page heap */
28420Sstevel@tonic-gate 	segkmem_heap_lp_init();
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)SYSBASE32, SYSLIMIT32 - SYSBASE32,
28450Sstevel@tonic-gate 	    &kvseg32);
28460Sstevel@tonic-gate 	(void) segkmem_create(&kvseg32);
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	/*
28490Sstevel@tonic-gate 	 * Create a segment for the debugger.
28500Sstevel@tonic-gate 	 */
28513446Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
28520Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
28550Sstevel@tonic-gate }
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate char obp_tte_str[] =
28580Sstevel@tonic-gate 	"h# %x constant MMU_PAGESHIFT "
28590Sstevel@tonic-gate 	"h# %x constant TTE8K "
28600Sstevel@tonic-gate 	"h# %x constant SFHME_SIZE "
28610Sstevel@tonic-gate 	"h# %x constant SFHME_TTE "
28620Sstevel@tonic-gate 	"h# %x constant HMEBLK_TAG "
28630Sstevel@tonic-gate 	"h# %x constant HMEBLK_NEXT "
28640Sstevel@tonic-gate 	"h# %x constant HMEBLK_MISC "
28650Sstevel@tonic-gate 	"h# %x constant HMEBLK_HME1 "
28660Sstevel@tonic-gate 	"h# %x constant NHMENTS "
28670Sstevel@tonic-gate 	"h# %x constant HBLK_SZMASK "
28680Sstevel@tonic-gate 	"h# %x constant HBLK_RANGE_SHIFT "
28690Sstevel@tonic-gate 	"h# %x constant HMEBP_HBLK "
28700Sstevel@tonic-gate 	"h# %x constant HMEBUCKET_SIZE "
28710Sstevel@tonic-gate 	"h# %x constant HTAG_SFMMUPSZ "
28720Sstevel@tonic-gate 	"h# %x constant HTAG_REHASHSZ "
28730Sstevel@tonic-gate 	"h# %x constant mmu_hashcnt "
28740Sstevel@tonic-gate 	"h# %p constant uhme_hash "
28750Sstevel@tonic-gate 	"h# %p constant khme_hash "
28760Sstevel@tonic-gate 	"h# %x constant UHMEHASH_SZ "
28770Sstevel@tonic-gate 	"h# %x constant KHMEHASH_SZ "
28782241Shuah 	"h# %p constant KCONTEXT "
28790Sstevel@tonic-gate 	"h# %p constant KHATID "
28800Sstevel@tonic-gate 	"h# %x constant ASI_MEM "
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	": PHYS-X@ ( phys -- data ) "
28830Sstevel@tonic-gate 	"   ASI_MEM spacex@ "
28840Sstevel@tonic-gate 	"; "
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	": PHYS-W@ ( phys -- data ) "
28870Sstevel@tonic-gate 	"   ASI_MEM spacew@ "
28880Sstevel@tonic-gate 	"; "
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate 	": PHYS-L@ ( phys -- data ) "
28910Sstevel@tonic-gate 	"   ASI_MEM spaceL@ "
28920Sstevel@tonic-gate 	"; "
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate 	": TTE_PAGE_SHIFT ( ttesz -- hmeshift ) "
28950Sstevel@tonic-gate 	"   3 * MMU_PAGESHIFT + "
28960Sstevel@tonic-gate 	"; "
28970Sstevel@tonic-gate 
28980Sstevel@tonic-gate 	": TTE_IS_VALID ( ttep -- flag ) "
28990Sstevel@tonic-gate 	"   PHYS-X@ 0< "
29000Sstevel@tonic-gate 	"; "
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 	": HME_HASH_SHIFT ( ttesz -- hmeshift ) "
29030Sstevel@tonic-gate 	"   dup TTE8K =  if "
29040Sstevel@tonic-gate 	"      drop HBLK_RANGE_SHIFT "
29050Sstevel@tonic-gate 	"   else "
29060Sstevel@tonic-gate 	"      TTE_PAGE_SHIFT "
29070Sstevel@tonic-gate 	"   then "
29080Sstevel@tonic-gate 	"; "
29090Sstevel@tonic-gate 
29100Sstevel@tonic-gate 	": HME_HASH_BSPAGE ( addr hmeshift -- bspage ) "
29110Sstevel@tonic-gate 	"   tuck >> swap MMU_PAGESHIFT - << "
29120Sstevel@tonic-gate 	"; "
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate 	": HME_HASH_FUNCTION ( sfmmup addr hmeshift -- hmebp ) "
29150Sstevel@tonic-gate 	"   >> over xor swap                    ( hash sfmmup ) "
29160Sstevel@tonic-gate 	"   KHATID <>  if                       ( hash ) "
29170Sstevel@tonic-gate 	"      UHMEHASH_SZ and                  ( bucket ) "
29180Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * uhme_hash +     ( hmebp ) "
29190Sstevel@tonic-gate 	"   else                                ( hash ) "
29200Sstevel@tonic-gate 	"      KHMEHASH_SZ and                  ( bucket ) "
29210Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * khme_hash +     ( hmebp ) "
29220Sstevel@tonic-gate 	"   then                                ( hmebp ) "
29230Sstevel@tonic-gate 	"; "
29240Sstevel@tonic-gate 
29250Sstevel@tonic-gate 	": HME_HASH_TABLE_SEARCH "
29260Sstevel@tonic-gate 	"       ( sfmmup hmebp hblktag --  sfmmup null | sfmmup hmeblkp ) "
29270Sstevel@tonic-gate 	"   >r hmebp_hblk + phys-x@ begin ( sfmmup hmeblkp ) ( r: hblktag ) "
29280Sstevel@tonic-gate 	"      dup if   		( sfmmup hmeblkp ) ( r: hblktag ) "
29290Sstevel@tonic-gate 	"         dup hmeblk_tag + phys-x@ r@ = if ( sfmmup hmeblkp )	  "
29300Sstevel@tonic-gate 	"	     dup hmeblk_tag + 8 + phys-x@ 2 pick = if		  "
29310Sstevel@tonic-gate 	"		  true 	( sfmmup hmeblkp true ) ( r: hblktag )	  "
29320Sstevel@tonic-gate 	"	     else						  "
29330Sstevel@tonic-gate 	"	     	  hmeblk_next + phys-x@ false 			  "
29340Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
29350Sstevel@tonic-gate 	"	     then  						  "
29360Sstevel@tonic-gate 	"	  else							  "
29370Sstevel@tonic-gate 	"	     hmeblk_next + phys-x@ false 			  "
29380Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
29390Sstevel@tonic-gate 	"	  then 							  "
29400Sstevel@tonic-gate 	"      else							  "
29410Sstevel@tonic-gate 	"         true 							  "
29420Sstevel@tonic-gate 	"      then  							  "
29430Sstevel@tonic-gate 	"   until r> drop 						  "
29440Sstevel@tonic-gate 	"; "
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 	": HME_HASH_TAG ( sfmmup rehash addr -- hblktag ) "
29470Sstevel@tonic-gate 	"   over HME_HASH_SHIFT HME_HASH_BSPAGE      ( sfmmup rehash bspage ) "
29480Sstevel@tonic-gate 	"   HTAG_REHASHSZ << or nip		     ( hblktag ) "
29490Sstevel@tonic-gate 	"; "
29500Sstevel@tonic-gate 
29510Sstevel@tonic-gate 	": HBLK_TO_TTEP ( hmeblkp addr -- ttep ) "
29520Sstevel@tonic-gate 	"   over HMEBLK_MISC + PHYS-L@ HBLK_SZMASK and  ( hmeblkp addr ttesz ) "
29530Sstevel@tonic-gate 	"   TTE8K =  if                            ( hmeblkp addr ) "
29540Sstevel@tonic-gate 	"      MMU_PAGESHIFT >> NHMENTS 1- and     ( hmeblkp hme-index ) "
29550Sstevel@tonic-gate 	"   else                                   ( hmeblkp addr ) "
29560Sstevel@tonic-gate 	"      drop 0                              ( hmeblkp 0 ) "
29570Sstevel@tonic-gate 	"   then                                   ( hmeblkp hme-index ) "
29580Sstevel@tonic-gate 	"   SFHME_SIZE * + HMEBLK_HME1 +           ( hmep ) "
29590Sstevel@tonic-gate 	"   SFHME_TTE +                            ( ttep ) "
29600Sstevel@tonic-gate 	"; "
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate 	": unix-tte ( addr cnum -- false | tte-data true ) "
29632241Shuah 	"    KCONTEXT = if                   ( addr ) "
29642241Shuah 	"	KHATID                       ( addr khatid ) "
29652241Shuah 	"    else                            ( addr ) "
29662241Shuah 	"       drop false exit              ( false ) "
29672241Shuah 	"    then "
29682241Shuah 	"      ( addr khatid ) "
29690Sstevel@tonic-gate 	"      mmu_hashcnt 1+ 1  do           ( addr sfmmup ) "
29700Sstevel@tonic-gate 	"         2dup swap i HME_HASH_SHIFT  "
29710Sstevel@tonic-gate 					"( addr sfmmup sfmmup addr hmeshift ) "
29720Sstevel@tonic-gate 	"         HME_HASH_FUNCTION           ( addr sfmmup hmebp ) "
29730Sstevel@tonic-gate 	"         over i 4 pick               "
29740Sstevel@tonic-gate 				"( addr sfmmup hmebp sfmmup rehash addr ) "
29750Sstevel@tonic-gate 	"         HME_HASH_TAG                ( addr sfmmup hmebp hblktag ) "
29760Sstevel@tonic-gate 	"         HME_HASH_TABLE_SEARCH       "
29770Sstevel@tonic-gate 					"( addr sfmmup { null | hmeblkp } ) "
29780Sstevel@tonic-gate 	"         ?dup  if                    ( addr sfmmup hmeblkp ) "
29790Sstevel@tonic-gate 	"            nip swap HBLK_TO_TTEP    ( ttep ) "
29800Sstevel@tonic-gate 	"            dup TTE_IS_VALID  if     ( valid-ttep ) "
29810Sstevel@tonic-gate 	"               PHYS-X@ true          ( tte-data true ) "
29820Sstevel@tonic-gate 	"            else                     ( invalid-tte ) "
29830Sstevel@tonic-gate 	"               drop false            ( false ) "
29840Sstevel@tonic-gate 	"            then                     ( false | tte-data true ) "
29850Sstevel@tonic-gate 	"            unloop exit              ( false | tte-data true ) "
29860Sstevel@tonic-gate 	"         then                        ( addr sfmmup ) "
29870Sstevel@tonic-gate 	"      loop                           ( addr sfmmup ) "
29880Sstevel@tonic-gate 	"      2drop false                    ( false ) "
29890Sstevel@tonic-gate 	"; "
29900Sstevel@tonic-gate ;
29910Sstevel@tonic-gate 
29920Sstevel@tonic-gate void
29930Sstevel@tonic-gate create_va_to_tte(void)
29940Sstevel@tonic-gate {
29950Sstevel@tonic-gate 	char *bp;
29960Sstevel@tonic-gate 	extern int khmehash_num, uhmehash_num;
29970Sstevel@tonic-gate 	extern struct hmehash_bucket *khme_hash, *uhme_hash;
29980Sstevel@tonic-gate 
29990Sstevel@tonic-gate #define	OFFSET(type, field)	((uintptr_t)(&((type *)0)->field))
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 	bp = (char *)kobj_zalloc(MMU_PAGESIZE, KM_SLEEP);
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 	/*
30040Sstevel@tonic-gate 	 * Teach obp how to parse our sw ttes.
30050Sstevel@tonic-gate 	 */
30060Sstevel@tonic-gate 	(void) sprintf(bp, obp_tte_str,
30070Sstevel@tonic-gate 	    MMU_PAGESHIFT,
30080Sstevel@tonic-gate 	    TTE8K,
30090Sstevel@tonic-gate 	    sizeof (struct sf_hment),
30100Sstevel@tonic-gate 	    OFFSET(struct sf_hment, hme_tte),
30110Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_tag),
30120Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_nextpa),
30130Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_misc),
30140Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_hme),
30150Sstevel@tonic-gate 	    NHMENTS,
30160Sstevel@tonic-gate 	    HBLK_SZMASK,
30170Sstevel@tonic-gate 	    HBLK_RANGE_SHIFT,
30180Sstevel@tonic-gate 	    OFFSET(struct hmehash_bucket, hmeh_nextpa),
30190Sstevel@tonic-gate 	    sizeof (struct hmehash_bucket),
30200Sstevel@tonic-gate 	    HTAG_SFMMUPSZ,
30210Sstevel@tonic-gate 	    HTAG_REHASHSZ,
30220Sstevel@tonic-gate 	    mmu_hashcnt,
30230Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)uhme_hash),
30240Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)khme_hash),
30250Sstevel@tonic-gate 	    UHMEHASH_SZ,
30260Sstevel@tonic-gate 	    KHMEHASH_SZ,
30272241Shuah 	    KCONTEXT,
30280Sstevel@tonic-gate 	    KHATID,
30290Sstevel@tonic-gate 	    ASI_MEM);
30300Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
30310Sstevel@tonic-gate 
30320Sstevel@tonic-gate 	kobj_free(bp, MMU_PAGESIZE);
30330Sstevel@tonic-gate }
30340Sstevel@tonic-gate 
30350Sstevel@tonic-gate void
30360Sstevel@tonic-gate install_va_to_tte(void)
30370Sstevel@tonic-gate {
30380Sstevel@tonic-gate 	/*
30390Sstevel@tonic-gate 	 * advise prom that he can use unix-tte
30400Sstevel@tonic-gate 	 */
30410Sstevel@tonic-gate 	prom_interpret("' unix-tte is va>tte-data", 0, 0, 0, 0, 0);
30420Sstevel@tonic-gate }
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 
30451253Slq150181 /*
30461253Slq150181  * Because kmdb links prom_stdout_is_framebuffer into its own
30471253Slq150181  * module, we add "device-type=display" here for /os-io node, so that
30481253Slq150181  * prom_stdout_is_framebuffer still works corrrectly  after /os-io node
30491253Slq150181  * is registered into OBP.
30501253Slq150181  */
30510Sstevel@tonic-gate static char *create_node =
30521492Slq150181 	"\" /\" find-device "
30530Sstevel@tonic-gate 	"new-device "
30540Sstevel@tonic-gate 	"\" os-io\" device-name "
30551253Slq150181 	"\" display\" device-type "
30560Sstevel@tonic-gate 	": cb-r/w  ( adr,len method$ -- #read/#written ) "
30570Sstevel@tonic-gate 	"   2>r swap 2 2r> ['] $callback  catch  if "
30580Sstevel@tonic-gate 	"      2drop 3drop 0 "
30590Sstevel@tonic-gate 	"   then "
30600Sstevel@tonic-gate 	"; "
30610Sstevel@tonic-gate 	": read ( adr,len -- #read ) "
30620Sstevel@tonic-gate 	"       \" read\" ['] cb-r/w catch  if  2drop 2drop -2 exit then "
30630Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
30640Sstevel@tonic-gate 	"       ?dup  if "
30650Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
30660Sstevel@tonic-gate 	"       else "
30670Sstevel@tonic-gate 	"               -2 "
30681492Slq150181 	"       then "
30690Sstevel@tonic-gate 	";    "
30700Sstevel@tonic-gate 	": write ( adr,len -- #written ) "
30710Sstevel@tonic-gate 	"       \" write\" ['] cb-r/w catch  if  2drop 2drop 0 exit  then "
30720Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
30730Sstevel@tonic-gate 	"       ?dup  if "
30740Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
30750Sstevel@tonic-gate 	"        else "
30760Sstevel@tonic-gate 	"               0 "
30770Sstevel@tonic-gate 	"       then "
30780Sstevel@tonic-gate 	"; "
30790Sstevel@tonic-gate 	": poll-tty ( -- ) ; "
30800Sstevel@tonic-gate 	": install-abort  ( -- )  ['] poll-tty d# 10 alarm ; "
30810Sstevel@tonic-gate 	": remove-abort ( -- )  ['] poll-tty 0 alarm ; "
30820Sstevel@tonic-gate 	": cb-give/take ( $method -- ) "
30830Sstevel@tonic-gate 	"       0 -rot ['] $callback catch  ?dup  if "
30840Sstevel@tonic-gate 	"               >r 2drop 2drop r> throw "
30850Sstevel@tonic-gate 	"       else "
30860Sstevel@tonic-gate 	"               0  ?do  drop  loop "
30870Sstevel@tonic-gate 	"       then "
30880Sstevel@tonic-gate 	"; "
30890Sstevel@tonic-gate 	": give ( -- )  \" exit-input\" cb-give/take ; "
30900Sstevel@tonic-gate 	": take ( -- )  \" enter-input\" cb-give/take ; "
30910Sstevel@tonic-gate 	": open ( -- ok? )  true ; "
30920Sstevel@tonic-gate 	": close ( -- ) ; "
30930Sstevel@tonic-gate 	"finish-device "
30940Sstevel@tonic-gate 	"device-end ";
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate /*
30971253Slq150181  * Create the OBP input/output node (FCode serial driver).
30981253Slq150181  * It is needed for both USB console keyboard and for
30991253Slq150181  * the kernel terminal emulator.  It is too early to check for a
31001253Slq150181  * kernel console compatible framebuffer now, so we create this
31011253Slq150181  * so that we're ready if we need to enable kernel terminal emulation.
31020Sstevel@tonic-gate  *
31031253Slq150181  * When the USB software takes over the input device at the time
31041253Slq150181  * consconfig runs, OBP's stdin is redirected to this node.
31051253Slq150181  * Whenever the FORTH user interface is used after this switch,
31061253Slq150181  * the node will call back into the kernel for console input.
31071253Slq150181  * If a serial device such as ttya or a UART with a Type 5 keyboard
31081253Slq150181  * attached is used, OBP takes over the serial device when the system
31091253Slq150181  * goes to the debugger after the system is booted.  This sharing
31101253Slq150181  * of the relatively simple serial device is difficult but possible.
31111253Slq150181  * Sharing the USB host controller is impossible due its complexity.
31121253Slq150181  *
31131253Slq150181  * Similarly to USB keyboard input redirection, after consconfig_dacf
31141253Slq150181  * configures a kernel console framebuffer as the standard output
31151253Slq150181  * device, OBP's stdout is switched to to vector through the
31161253Slq150181  * /os-io node into the kernel terminal emulator.
31170Sstevel@tonic-gate  */
31180Sstevel@tonic-gate static void
31191253Slq150181 startup_create_io_node(void)
31200Sstevel@tonic-gate {
31211253Slq150181 	prom_interpret(create_node, 0, 0, 0, 0, 0);
31220Sstevel@tonic-gate }
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate static void
31260Sstevel@tonic-gate do_prom_version_check(void)
31270Sstevel@tonic-gate {
31280Sstevel@tonic-gate 	int i;
3129789Sahrens 	pnode_t node;
31300Sstevel@tonic-gate 	char buf[64];
31310Sstevel@tonic-gate 	static char drev[] = "Down-rev firmware detected%s\n"
31320Sstevel@tonic-gate 		"\tPlease upgrade to the following minimum version:\n"
31330Sstevel@tonic-gate 		"\t\t%s\n";
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 	i = prom_version_check(buf, sizeof (buf), &node);
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	if (i == PROM_VER64_OK)
31380Sstevel@tonic-gate 		return;
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 	if (i == PROM_VER64_UPGRADE) {
31410Sstevel@tonic-gate 		cmn_err(CE_WARN, drev, "", buf);
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate #ifdef	DEBUG
31440Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to continue */
31450Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware\n");
31460Sstevel@tonic-gate 		return;
31470Sstevel@tonic-gate #else
31480Sstevel@tonic-gate 		halt(0);
31490Sstevel@tonic-gate #endif
31500Sstevel@tonic-gate 	}
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 	/*
31530Sstevel@tonic-gate 	 * The other possibility is that this is a server running
31540Sstevel@tonic-gate 	 * good firmware, but down-rev firmware was detected on at
31550Sstevel@tonic-gate 	 * least one other cpu board. We just complain if we see
31560Sstevel@tonic-gate 	 * that.
31570Sstevel@tonic-gate 	 */
31580Sstevel@tonic-gate 	cmn_err(CE_WARN, drev, " on one or more CPU boards", buf);
31590Sstevel@tonic-gate }
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate static void
31620Sstevel@tonic-gate kpm_init()
31630Sstevel@tonic-gate {
31640Sstevel@tonic-gate 	kpm_pgshft = (kpm_smallpages == 0) ? MMU_PAGESHIFT4M : MMU_PAGESHIFT;
31650Sstevel@tonic-gate 	kpm_pgsz = 1ull << kpm_pgshft;
31660Sstevel@tonic-gate 	kpm_pgoff = kpm_pgsz - 1;
31670Sstevel@tonic-gate 	kpmp2pshft = kpm_pgshft - PAGESHIFT;
31680Sstevel@tonic-gate 	kpmpnpgs = 1 << kpmp2pshft;
31690Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
31700Sstevel@tonic-gate }
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate void
31730Sstevel@tonic-gate kpm_npages_setup(int memblocks)
31740Sstevel@tonic-gate {
31750Sstevel@tonic-gate 	/*
31760Sstevel@tonic-gate 	 * npages can be scattered in a maximum of 'memblocks'
31770Sstevel@tonic-gate 	 */
31780Sstevel@tonic-gate 	kpm_npages = ptokpmpr(npages) + memblocks;
31790Sstevel@tonic-gate }
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate /*
31820Sstevel@tonic-gate  * Must be defined in platform dependent code.
31830Sstevel@tonic-gate  */
31840Sstevel@tonic-gate extern caddr_t modtext;
31850Sstevel@tonic-gate extern size_t modtext_sz;
31860Sstevel@tonic-gate extern caddr_t moddata;
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate #define	HEAPTEXT_ARENA(addr)	\
31890Sstevel@tonic-gate 	((uintptr_t)(addr) < KERNELBASE + 2 * MMU_PAGESIZE4M ? 0 : \
31900Sstevel@tonic-gate 	(((uintptr_t)(addr) - HEAPTEXT_BASE) / \
31910Sstevel@tonic-gate 	(HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) + 1))
31920Sstevel@tonic-gate 
31930Sstevel@tonic-gate #define	HEAPTEXT_OVERSIZED(addr)	\
31940Sstevel@tonic-gate 	((uintptr_t)(addr) >= HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE)
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate vmem_t *texthole_source[HEAPTEXT_NARENAS];
31970Sstevel@tonic-gate vmem_t *texthole_arena[HEAPTEXT_NARENAS];
31980Sstevel@tonic-gate kmutex_t texthole_lock;
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate void
32030Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
32040Sstevel@tonic-gate {
32050Sstevel@tonic-gate 	uintptr_t addr, limit;
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 	addr = HEAPTEXT_BASE;
32080Sstevel@tonic-gate 	limit = addr + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE;
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 	/*
32110Sstevel@tonic-gate 	 * Before we initialize the text_arena, we want to punch holes in the
32120Sstevel@tonic-gate 	 * underlying heaptext_arena.  This guarantees that for any text
32130Sstevel@tonic-gate 	 * address we can find a text hole less than HEAPTEXT_MAPPED away.
32140Sstevel@tonic-gate 	 */
32150Sstevel@tonic-gate 	for (; addr + HEAPTEXT_UNMAPPED <= limit;
32160Sstevel@tonic-gate 	    addr += HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) {
32170Sstevel@tonic-gate 		(void) vmem_xalloc(heaptext_arena, HEAPTEXT_UNMAPPED, PAGESIZE,
32180Sstevel@tonic-gate 		    0, 0, (void *)addr, (void *)(addr + HEAPTEXT_UNMAPPED),
32190Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
32200Sstevel@tonic-gate 	}
32210Sstevel@tonic-gate 
32220Sstevel@tonic-gate 	/*
32230Sstevel@tonic-gate 	 * Allocate one page at the oversize to break up the text region
32240Sstevel@tonic-gate 	 * from the oversized region.
32250Sstevel@tonic-gate 	 */
32260Sstevel@tonic-gate 	(void) vmem_xalloc(heaptext_arena, PAGESIZE, PAGESIZE, 0, 0,
32270Sstevel@tonic-gate 	    (void *)limit, (void *)(limit + PAGESIZE),
32280Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", modtext, modtext_sz,
32310Sstevel@tonic-gate 	    sizeof (uintptr_t), segkmem_alloc, segkmem_free,
32320Sstevel@tonic-gate 	    heaptext_arena, 0, VM_SLEEP);
32330Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", moddata, MODDATA, 1,
32340Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
32350Sstevel@tonic-gate }
32360Sstevel@tonic-gate 
32370Sstevel@tonic-gate caddr_t
32380Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
32390Sstevel@tonic-gate {
32400Sstevel@tonic-gate 	caddr_t rval, better;
32410Sstevel@tonic-gate 
32420Sstevel@tonic-gate 	/*
32430Sstevel@tonic-gate 	 * First, try a sleeping allocation.
32440Sstevel@tonic-gate 	 */
32450Sstevel@tonic-gate 	rval = vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT);
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 	if (size >= HEAPTEXT_MAPPED || !HEAPTEXT_OVERSIZED(rval))
32480Sstevel@tonic-gate 		return (rval);
32490Sstevel@tonic-gate 
32500Sstevel@tonic-gate 	/*
32510Sstevel@tonic-gate 	 * We didn't get the area that we wanted.  We're going to try to do an
32520Sstevel@tonic-gate 	 * allocation with explicit constraints.
32530Sstevel@tonic-gate 	 */
32540Sstevel@tonic-gate 	better = vmem_xalloc(arena, size, sizeof (uintptr_t), 0, 0, NULL,
32550Sstevel@tonic-gate 	    (void *)(HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE),
32560Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT);
32570Sstevel@tonic-gate 
32580Sstevel@tonic-gate 	if (better != NULL) {
32590Sstevel@tonic-gate 		/*
32600Sstevel@tonic-gate 		 * That worked.  Free our first attempt and return.
32610Sstevel@tonic-gate 		 */
32620Sstevel@tonic-gate 		vmem_free(arena, rval, size);
32630Sstevel@tonic-gate 		return (better);
32640Sstevel@tonic-gate 	}
32650Sstevel@tonic-gate 
32660Sstevel@tonic-gate 	/*
32670Sstevel@tonic-gate 	 * That didn't work; we'll have to return our first attempt.
32680Sstevel@tonic-gate 	 */
32690Sstevel@tonic-gate 	return (rval);
32700Sstevel@tonic-gate }
32710Sstevel@tonic-gate 
32720Sstevel@tonic-gate caddr_t
32730Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
32740Sstevel@tonic-gate {
32750Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
32760Sstevel@tonic-gate 	char c[30];
32770Sstevel@tonic-gate 	uintptr_t base;
32780Sstevel@tonic-gate 
32790Sstevel@tonic-gate 	if (HEAPTEXT_OVERSIZED(addr)) {
32800Sstevel@tonic-gate 		/*
32810Sstevel@tonic-gate 		 * If this is an oversized allocation, there is no text hole
32820Sstevel@tonic-gate 		 * available for it; return NULL.
32830Sstevel@tonic-gate 		 */
32840Sstevel@tonic-gate 		return (NULL);
32850Sstevel@tonic-gate 	}
32860Sstevel@tonic-gate 
32870Sstevel@tonic-gate 	mutex_enter(&texthole_lock);
32880Sstevel@tonic-gate 
32890Sstevel@tonic-gate 	if (texthole_arena[arena] == NULL) {
32900Sstevel@tonic-gate 		ASSERT(texthole_source[arena] == NULL);
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 		if (arena == 0) {
32930Sstevel@tonic-gate 			texthole_source[0] = vmem_create("module_text_holesrc",
32940Sstevel@tonic-gate 			    (void *)(KERNELBASE + MMU_PAGESIZE4M),
32950Sstevel@tonic-gate 			    MMU_PAGESIZE4M, PAGESIZE, NULL, NULL, NULL,
32960Sstevel@tonic-gate 			    0, VM_SLEEP);
32970Sstevel@tonic-gate 		} else {
32980Sstevel@tonic-gate 			base = HEAPTEXT_BASE +
32990Sstevel@tonic-gate 			    (arena - 1) * (HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED);
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c),
33020Sstevel@tonic-gate 			    "heaptext_holesrc_%d", arena);
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate 			texthole_source[arena] = vmem_create(c, (void *)base,
33050Sstevel@tonic-gate 			    HEAPTEXT_UNMAPPED, PAGESIZE, NULL, NULL, NULL,
33060Sstevel@tonic-gate 			    0, VM_SLEEP);
33070Sstevel@tonic-gate 		}
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate 		(void) snprintf(c, sizeof (c), "heaptext_hole_%d", arena);
33100Sstevel@tonic-gate 
33110Sstevel@tonic-gate 		texthole_arena[arena] = vmem_create(c, NULL, 0,
33120Sstevel@tonic-gate 		    sizeof (uint32_t), segkmem_alloc_permanent, segkmem_free,
33130Sstevel@tonic-gate 		    texthole_source[arena], 0, VM_SLEEP);
33140Sstevel@tonic-gate 	}
33150Sstevel@tonic-gate 
33160Sstevel@tonic-gate 	mutex_exit(&texthole_lock);
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
33190Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
33200Sstevel@tonic-gate 	return (vmem_alloc(texthole_arena[arena], size,
33210Sstevel@tonic-gate 	    VM_BESTFIT | VM_NOSLEEP));
33220Sstevel@tonic-gate }
33230Sstevel@tonic-gate 
33240Sstevel@tonic-gate void
33250Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
33260Sstevel@tonic-gate {
33270Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
33300Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
33310Sstevel@tonic-gate 	vmem_free(texthole_arena[arena], addr, size);
33320Sstevel@tonic-gate }
3333