xref: /onnv-gate/usr/src/uts/sun4/os/startup.c (revision 1676)
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 /*
231253Slq150181  * Copyright 2006 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>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate extern void setup_trap_table(void);
690Sstevel@tonic-gate extern void cpu_intrq_setup(struct cpu *);
700Sstevel@tonic-gate extern void cpu_intrq_register(struct cpu *);
710Sstevel@tonic-gate extern void contig_mem_init(void);
720Sstevel@tonic-gate extern void mach_dump_buffer_init(void);
730Sstevel@tonic-gate extern void mach_descrip_init(void);
740Sstevel@tonic-gate extern void mach_memscrub(void);
750Sstevel@tonic-gate extern void mach_fpras(void);
760Sstevel@tonic-gate extern void mach_cpu_halt_idle(void);
770Sstevel@tonic-gate extern void mach_hw_copy_limit(void);
780Sstevel@tonic-gate extern void load_tod_module(void);
790Sstevel@tonic-gate #pragma weak load_tod_module
800Sstevel@tonic-gate 
810Sstevel@tonic-gate extern int ndata_alloc_mmfsa(struct memlist *ndata);
820Sstevel@tonic-gate #pragma weak ndata_alloc_mmfsa
830Sstevel@tonic-gate 
840Sstevel@tonic-gate extern void parse_idprom(void);
850Sstevel@tonic-gate extern void add_vx_handler(char *, int, void (*)(cell_t *));
860Sstevel@tonic-gate extern void mem_config_init(void);
870Sstevel@tonic-gate extern void memseg_remap_init(void);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * External Data:
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate extern int vac_size;	/* cache size in bytes */
930Sstevel@tonic-gate extern uint_t vac_mask;	/* VAC alignment consistency mask */
940Sstevel@tonic-gate extern uint_t vac_colors;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Global Data Definitions:
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * XXX - Don't port this to new architectures
1020Sstevel@tonic-gate  * A 3rd party volume manager driver (vxdm) depends on the symbol romp.
1030Sstevel@tonic-gate  * 'romp' has no use with a prom with an IEEE 1275 client interface.
1040Sstevel@tonic-gate  * The driver doesn't use the value, but it depends on the symbol.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate void *romp;		/* veritas driver won't load without romp 4154976 */
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate pgcnt_t physmem = 0;	/* memory size in pages, patch if you want less */
1110Sstevel@tonic-gate pgcnt_t segkpsize =
1120Sstevel@tonic-gate     btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
1130Sstevel@tonic-gate uint_t segmap_percent = 12; /* Size of segmap segment */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate int use_cache = 1;		/* cache not reliable (605 bugs) with MP */
1160Sstevel@tonic-gate int vac_copyback = 1;
1170Sstevel@tonic-gate char *cache_mode = NULL;
1180Sstevel@tonic-gate int use_mix = 1;
1190Sstevel@tonic-gate int prom_debug = 0;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate struct bootops *bootops = 0;	/* passed in from boot in %o2 */
1220Sstevel@tonic-gate caddr_t boot_tba;		/* %tba at boot - used by kmdb */
1230Sstevel@tonic-gate uint_t	tba_taken_over = 0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate caddr_t s_text;			/* start of kernel text segment */
1260Sstevel@tonic-gate caddr_t e_text;			/* end of kernel text segment */
1270Sstevel@tonic-gate caddr_t s_data;			/* start of kernel data segment */
1280Sstevel@tonic-gate caddr_t e_data;			/* end of kernel data segment */
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate caddr_t modtext;		/* beginning of module text */
1310Sstevel@tonic-gate size_t	modtext_sz;		/* size of module text */
1320Sstevel@tonic-gate caddr_t moddata;		/* beginning of module data reserve */
1330Sstevel@tonic-gate caddr_t e_moddata;		/* end of module data reserve */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * End of first block of contiguous kernel in 32-bit virtual address space
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate caddr_t		econtig32;	/* end of first blk of contiguous kernel */
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate caddr_t		ncbase;		/* beginning of non-cached segment */
1410Sstevel@tonic-gate caddr_t		ncend;		/* end of non-cached segment */
1420Sstevel@tonic-gate caddr_t		sdata;		/* beginning of data segment */
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate caddr_t		extra_etva;	/* beginning of unused nucleus text */
1450Sstevel@tonic-gate pgcnt_t		extra_etpg;	/* number of pages of unused nucleus text */
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate size_t	ndata_remain_sz;	/* bytes from end of data to 4MB boundary */
1480Sstevel@tonic-gate caddr_t	nalloc_base;		/* beginning of nucleus allocation */
1490Sstevel@tonic-gate caddr_t nalloc_end;		/* end of nucleus allocatable memory */
1500Sstevel@tonic-gate caddr_t valloc_base;		/* beginning of kvalloc segment	*/
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate caddr_t kmem64_base;		/* base of kernel mem segment in 64-bit space */
1530Sstevel@tonic-gate caddr_t kmem64_end;		/* end of kernel mem segment in 64-bit space */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate uintptr_t shm_alignment = 0;	/* VAC address consistency modulus */
1560Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
1570Sstevel@tonic-gate struct memlist *phys_avail;	/* Available (unreserved) physical memory */
1580Sstevel@tonic-gate struct memlist *virt_avail;	/* Available (unmapped?) virtual memory */
1590Sstevel@tonic-gate struct memlist ndata;		/* memlist of nucleus allocatable memory */
1600Sstevel@tonic-gate int memexp_flag;		/* memory expansion card flag */
1610Sstevel@tonic-gate uint64_t ecache_flushaddr;	/* physical address used for flushing E$ */
1620Sstevel@tonic-gate pgcnt_t obp_pages;		/* Physical pages used by OBP */
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate  * VM data structures
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
1680Sstevel@tonic-gate struct page *pp_base;		/* Base of system page struct array */
1690Sstevel@tonic-gate size_t pp_sz;			/* Size in bytes of page struct array */
1700Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
1710Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
1720Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
1730Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
1740Sstevel@tonic-gate struct seg ktexthole;		/* Segment used for nucleus text hole */
1750Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
1760Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
1770Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate uintptr_t kpm_pp_base;		/* Base of system kpm_page array */
1800Sstevel@tonic-gate size_t	kpm_pp_sz;		/* Size of system kpm_page array */
1810Sstevel@tonic-gate pgcnt_t	kpm_npages;		/* How many kpm pages are managed */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
1840Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
1850Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  * debugger pages (if allocated)
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate struct vnode kdebugvp;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * Segment for relocated kernel structures in 64-bit large RAM kernels
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate struct seg kmem64;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate struct memseg *memseg_base;
1980Sstevel@tonic-gate size_t memseg_sz;		/* Used to translate a va to page */
1990Sstevel@tonic-gate struct vnode unused_pages_vp;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * VM data structures allocated early during boot.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate size_t pagehash_sz;
2050Sstevel@tonic-gate uint64_t memlist_sz;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate char tbr_wr_addr_inited = 0;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * Static Routines:
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate static void memlist_add(uint64_t, uint64_t, struct memlist **,
2140Sstevel@tonic-gate 	struct memlist **);
2150Sstevel@tonic-gate static void kphysm_init(page_t *, struct memseg *, pgcnt_t, uintptr_t,
2160Sstevel@tonic-gate 	pgcnt_t);
2170Sstevel@tonic-gate static void kvm_init(void);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static void startup_init(void);
2200Sstevel@tonic-gate static void startup_memlist(void);
2210Sstevel@tonic-gate static void startup_modules(void);
2220Sstevel@tonic-gate static void startup_bop_gone(void);
2230Sstevel@tonic-gate static void startup_vm(void);
2240Sstevel@tonic-gate static void startup_end(void);
2250Sstevel@tonic-gate static void setup_cage_params(void);
2261253Slq150181 static void startup_create_io_node(void);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static pgcnt_t npages;
2290Sstevel@tonic-gate static struct memlist *memlist;
2300Sstevel@tonic-gate void *memlist_end;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate static pgcnt_t bop_alloc_pages;
2330Sstevel@tonic-gate static caddr_t hblk_base;
2340Sstevel@tonic-gate uint_t hblk_alloc_dynamic = 0;
2350Sstevel@tonic-gate uint_t hblk1_min = H1MIN;
2360Sstevel@tonic-gate uint_t hblk8_min;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * Hooks for unsupported platforms and down-rev firmware
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate int iam_positron(void);
2430Sstevel@tonic-gate #pragma weak iam_positron
2440Sstevel@tonic-gate static void do_prom_version_check(void);
2450Sstevel@tonic-gate static void kpm_init(void);
2460Sstevel@tonic-gate static void kpm_npages_setup(int);
2470Sstevel@tonic-gate static void kpm_memseg_init(void);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate  * After receiving a thermal interrupt, this is the number of seconds
2510Sstevel@tonic-gate  * to delay before shutting off the system, assuming
2520Sstevel@tonic-gate  * shutdown fails.  Use /etc/system to change the delay if this isn't
2530Sstevel@tonic-gate  * large enough.
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate int thermal_powerdown_delay = 1200;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate  * Used to hold off page relocations into the cage until OBP has completed
2590Sstevel@tonic-gate  * its boot-time handoff of its resources to the kernel.
2600Sstevel@tonic-gate  */
2610Sstevel@tonic-gate int page_relocate_ready = 0;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
2650Sstevel@tonic-gate  */
2660Sstevel@tonic-gate #ifdef  DEBUGGING_MEM
2670Sstevel@tonic-gate static int debugging_mem;
2680Sstevel@tonic-gate static void
2690Sstevel@tonic-gate printmemlist(char *title, struct memlist *list)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	if (!debugging_mem)
2720Sstevel@tonic-gate 		return;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	printf("%s\n", title);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	while (list) {
2770Sstevel@tonic-gate 		prom_printf("\taddr = 0x%x %8x, size = 0x%x %8x\n",
2780Sstevel@tonic-gate 		    (uint32_t)(list->address >> 32), (uint32_t)list->address,
2790Sstevel@tonic-gate 		    (uint32_t)(list->size >> 32), (uint32_t)(list->size));
2800Sstevel@tonic-gate 		list = list->next;
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate void
2850Sstevel@tonic-gate printmemseg(struct memseg *memseg)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	if (!debugging_mem)
2880Sstevel@tonic-gate 		return;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	printf("memseg\n");
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	while (memseg) {
2930Sstevel@tonic-gate 		prom_printf("\tpage = 0x%p, epage = 0x%p, "
2940Sstevel@tonic-gate 		    "pfn = 0x%x, epfn = 0x%x\n",
2950Sstevel@tonic-gate 		    memseg->pages, memseg->epages,
2960Sstevel@tonic-gate 		    memseg->pages_base, memseg->pages_end);
2970Sstevel@tonic-gate 		memseg = memseg->next;
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate #define	debug_pause(str)	halt((str))
3020Sstevel@tonic-gate #define	MPRINTF(str)		if (debugging_mem) prom_printf((str))
3030Sstevel@tonic-gate #define	MPRINTF1(str, a)	if (debugging_mem) prom_printf((str), (a))
3040Sstevel@tonic-gate #define	MPRINTF2(str, a, b)	if (debugging_mem) prom_printf((str), (a), (b))
3050Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c) \
3060Sstevel@tonic-gate 	if (debugging_mem) prom_printf((str), (a), (b), (c))
3070Sstevel@tonic-gate #else	/* DEBUGGING_MEM */
3080Sstevel@tonic-gate #define	MPRINTF(str)
3090Sstevel@tonic-gate #define	MPRINTF1(str, a)
3100Sstevel@tonic-gate #define	MPRINTF2(str, a, b)
3110Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c)
3120Sstevel@tonic-gate #endif	/* DEBUGGING_MEM */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /* Simple message to indicate that the bootops pointer has been zeroed */
3150Sstevel@tonic-gate #ifdef DEBUG
3160Sstevel@tonic-gate static int bootops_gone_on = 0;
3170Sstevel@tonic-gate #define	BOOTOPS_GONE() \
3180Sstevel@tonic-gate 	if (bootops_gone_on) \
3190Sstevel@tonic-gate 		prom_printf("The bootops vec is zeroed now!\n");
3200Sstevel@tonic-gate #else
3210Sstevel@tonic-gate #define	BOOTOPS_GONE()
3220Sstevel@tonic-gate #endif /* DEBUG */
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * Monitor pages may not be where this says they are.
3260Sstevel@tonic-gate  * and the debugger may not be there either.
3270Sstevel@tonic-gate  *
3280Sstevel@tonic-gate  * Note that 'pages' here are *physical* pages, which are 8k on sun4u.
3290Sstevel@tonic-gate  *
3300Sstevel@tonic-gate  *                        Physical memory layout
3310Sstevel@tonic-gate  *                     (not necessarily contiguous)
3320Sstevel@tonic-gate  *                       (THIS IS SOMEWHAT WRONG)
3330Sstevel@tonic-gate  *                       /-----------------------\
3340Sstevel@tonic-gate  *                       |       monitor pages   |
3350Sstevel@tonic-gate  *             availmem -|-----------------------|
3360Sstevel@tonic-gate  *                       |                       |
3370Sstevel@tonic-gate  *                       |       page pool       |
3380Sstevel@tonic-gate  *                       |                       |
3390Sstevel@tonic-gate  *                       |-----------------------|
3400Sstevel@tonic-gate  *                       |   configured tables   |
3410Sstevel@tonic-gate  *                       |       buffers         |
3420Sstevel@tonic-gate  *            firstaddr -|-----------------------|
3430Sstevel@tonic-gate  *                       |   hat data structures |
3440Sstevel@tonic-gate  *                       |-----------------------|
3450Sstevel@tonic-gate  *                       |    kernel data, bss   |
3460Sstevel@tonic-gate  *                       |-----------------------|
3470Sstevel@tonic-gate  *                       |    interrupt stack    |
3480Sstevel@tonic-gate  *                       |-----------------------|
3490Sstevel@tonic-gate  *                       |    kernel text (RO)   |
3500Sstevel@tonic-gate  *                       |-----------------------|
3510Sstevel@tonic-gate  *                       |    trap table (4k)    |
3520Sstevel@tonic-gate  *                       |-----------------------|
3530Sstevel@tonic-gate  *               page 1  |      panicbuf         |
3540Sstevel@tonic-gate  *                       |-----------------------|
3550Sstevel@tonic-gate  *               page 0  |       reclaimed       |
3560Sstevel@tonic-gate  *                       |_______________________|
3570Sstevel@tonic-gate  *
3580Sstevel@tonic-gate  *
3590Sstevel@tonic-gate  *
3600Sstevel@tonic-gate  *                    Kernel's Virtual Memory Layout.
3610Sstevel@tonic-gate  *                       /-----------------------\
3620Sstevel@tonic-gate  * 0xFFFFFFFF.FFFFFFFF  -|                       |-
3630Sstevel@tonic-gate  *                       |   OBP's virtual page  |
3640Sstevel@tonic-gate  *                       |        tables         |
3650Sstevel@tonic-gate  * 0xFFFFFFFC.00000000  -|-----------------------|-
3660Sstevel@tonic-gate  *                       :                       :
3670Sstevel@tonic-gate  *                       :                       :
3680Sstevel@tonic-gate  * 0xFFFFFE00.00000000  -|-----------------------|-
3690Sstevel@tonic-gate  *                       |                       |  Ultrasparc I/II support
3700Sstevel@tonic-gate  *                       |    segkpm segment     |  up to 2TB of physical
3710Sstevel@tonic-gate  *                       | (64-bit kernel ONLY)  |  memory, VAC has 2 colors
3720Sstevel@tonic-gate  *                       |                       |
3730Sstevel@tonic-gate  * 0xFFFFFA00.00000000  -|-----------------------|- 2TB segkpm alignment
3740Sstevel@tonic-gate  *                       :                       :
3750Sstevel@tonic-gate  *                       :                       :
3760Sstevel@tonic-gate  * 0xFFFFF810.00000000  -|-----------------------|- hole_end
3770Sstevel@tonic-gate  *                       |                       |      ^
3780Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
3790Sstevel@tonic-gate  *                       | bug requires an extra |      |
3800Sstevel@tonic-gate  *                       | 4 GB of space between |      |
3810Sstevel@tonic-gate  *                       |   hole and used RAM   |	|
3820Sstevel@tonic-gate  *                       |                       |      |
3830Sstevel@tonic-gate  * 0xFFFFF800.00000000  -|-----------------------|-     |
3840Sstevel@tonic-gate  *                       |                       |      |
3850Sstevel@tonic-gate  *                       | Virtual Address Hole  |   UltraSPARC
3860Sstevel@tonic-gate  *                       |  on UltraSPARC I/II   |  I/II * ONLY *
3870Sstevel@tonic-gate  *                       |                       |      |
3880Sstevel@tonic-gate  * 0x00000800.00000000  -|-----------------------|-     |
3890Sstevel@tonic-gate  *                       |                       |      |
3900Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
3910Sstevel@tonic-gate  *                       | bug requires an extra |      |
3920Sstevel@tonic-gate  *                       | 4 GB of space between |      |
3930Sstevel@tonic-gate  *                       |   hole and used RAM   |      |
3940Sstevel@tonic-gate  *                       |                       |      v
3950Sstevel@tonic-gate  * 0x000007FF.00000000  -|-----------------------|- hole_start -----
3960Sstevel@tonic-gate  *                       :                       :		   ^
3970Sstevel@tonic-gate  *                       :                       :		   |
3980Sstevel@tonic-gate  * 0x00000XXX.XXXXXXXX  -|-----------------------|- kmem64_end	   |
3990Sstevel@tonic-gate  *                       |                       |		   |
4000Sstevel@tonic-gate  *                       |   64-bit kernel ONLY  |		   |
4010Sstevel@tonic-gate  *                       |                       |		   |
4020Sstevel@tonic-gate  *                       |    kmem64 segment     |		   |
4030Sstevel@tonic-gate  *                       |                       |		   |
4040Sstevel@tonic-gate  *                       | (Relocated extra HME  |	     Approximately
4050Sstevel@tonic-gate  *                       |   block allocations,  |	    1 TB of virtual
4060Sstevel@tonic-gate  *                       |   memnode freelists,  |	     address space
4070Sstevel@tonic-gate  *                       |    HME hash buckets,  |		   |
4080Sstevel@tonic-gate  *                       | mml_table, kpmp_table,|		   |
4090Sstevel@tonic-gate  *                       |  page_t array and     |		   |
4100Sstevel@tonic-gate  *                       |  hashblock pool to    |		   |
4110Sstevel@tonic-gate  *                       |   avoid hard-coded    |		   |
4120Sstevel@tonic-gate  *                       |     32-bit vaddr      |		   |
4130Sstevel@tonic-gate  *                       |     limitations)      |		   |
4140Sstevel@tonic-gate  *                       |                       |		   v
4150Sstevel@tonic-gate  * 0x00000700.00000000  -|-----------------------|- SYSLIMIT (kmem64_base)
4160Sstevel@tonic-gate  *                       |                       |
4170Sstevel@tonic-gate  *                       |  segkmem segment      | (SYSLIMIT - SYSBASE = 4TB)
4180Sstevel@tonic-gate  *                       |                       |
4190Sstevel@tonic-gate  * 0x00000300.00000000  -|-----------------------|- SYSBASE
4200Sstevel@tonic-gate  *                       :                       :
4210Sstevel@tonic-gate  *                       :                       :
4220Sstevel@tonic-gate  *                      -|-----------------------|-
4230Sstevel@tonic-gate  *                       |                       |
4240Sstevel@tonic-gate  *                       |  segmap segment       |   SEGMAPSIZE (1/8th physmem,
4250Sstevel@tonic-gate  *                       |                       |               256G MAX)
4260Sstevel@tonic-gate  * 0x000002a7.50000000  -|-----------------------|- SEGMAPBASE
4270Sstevel@tonic-gate  *                       :                       :
4280Sstevel@tonic-gate  *                       :                       :
4290Sstevel@tonic-gate  *                      -|-----------------------|-
4300Sstevel@tonic-gate  *                       |                       |
4310Sstevel@tonic-gate  *                       |       segkp           |    SEGKPSIZE (2GB)
4320Sstevel@tonic-gate  *                       |                       |
4330Sstevel@tonic-gate  *                       |                       |
4340Sstevel@tonic-gate  * 0x000002a1.00000000  -|-----------------------|- SEGKPBASE
4350Sstevel@tonic-gate  *                       |                       |
4360Sstevel@tonic-gate  * 0x000002a0.00000000  -|-----------------------|- MEMSCRUBBASE
4370Sstevel@tonic-gate  *                       |                       |       (SEGKPBASE - 0x400000)
4380Sstevel@tonic-gate  * 0x0000029F.FFE00000  -|-----------------------|- ARGSBASE
4390Sstevel@tonic-gate  *                       |                       |       (MEMSCRUBBASE - NCARGS)
4400Sstevel@tonic-gate  * 0x0000029F.FFD80000  -|-----------------------|- PPMAPBASE
4410Sstevel@tonic-gate  *                       |                       |       (ARGSBASE - PPMAPSIZE)
4420Sstevel@tonic-gate  * 0x0000029F.FFD00000  -|-----------------------|- PPMAP_FAST_BASE
4430Sstevel@tonic-gate  *                       |                       |
4440Sstevel@tonic-gate  * 0x0000029F.FF980000  -|-----------------------|- PIOMAPBASE
4450Sstevel@tonic-gate  *                       |                       |
4460Sstevel@tonic-gate  * 0x0000029F.FF580000  -|-----------------------|- NARG_BASE
4470Sstevel@tonic-gate  *                       :                       :
4480Sstevel@tonic-gate  *                       :                       :
4490Sstevel@tonic-gate  * 0x00000000.FFFFFFFF  -|-----------------------|- OFW_END_ADDR
4500Sstevel@tonic-gate  *                       |                       |
4510Sstevel@tonic-gate  *                       |         OBP           |
4520Sstevel@tonic-gate  *                       |                       |
4530Sstevel@tonic-gate  * 0x00000000.F0000000  -|-----------------------|- OFW_START_ADDR
4540Sstevel@tonic-gate  *                       |         kmdb          |
4550Sstevel@tonic-gate  * 0x00000000.EDD00000  -|-----------------------|- SEGDEBUGBASE
4560Sstevel@tonic-gate  *                       :                       :
4570Sstevel@tonic-gate  *                       :                       :
4580Sstevel@tonic-gate  * 0x00000000.7c000000  -|-----------------------|- SYSLIMIT32
4590Sstevel@tonic-gate  *                       |                       |
4600Sstevel@tonic-gate  *                       |  segkmem32 segment    | (SYSLIMIT32 - SYSBASE32 =
4610Sstevel@tonic-gate  *                       |                       |    ~64MB)
4620Sstevel@tonic-gate  * 0x00000000.78002000  -|-----------------------|
4630Sstevel@tonic-gate  *                       |     panicbuf          |
4640Sstevel@tonic-gate  * 0x00000000.78000000  -|-----------------------|- SYSBASE32
4650Sstevel@tonic-gate  *                       :                       :
4660Sstevel@tonic-gate  *                       :                       :
4670Sstevel@tonic-gate  *                       |                       |
4680Sstevel@tonic-gate  *                       |-----------------------|- econtig32
4690Sstevel@tonic-gate  *                       |    vm structures      |
4700Sstevel@tonic-gate  * 0x00000000.01C00000   |-----------------------|- nalloc_end
4710Sstevel@tonic-gate  *                       |         TSBs          |
4720Sstevel@tonic-gate  *                       |-----------------------|- end/nalloc_base
4730Sstevel@tonic-gate  *                       |   kernel data & bss   |
4740Sstevel@tonic-gate  * 0x00000000.01800000  -|-----------------------|
4750Sstevel@tonic-gate  *                       :   nucleus text hole   :
4760Sstevel@tonic-gate  * 0x00000000.01400000  -|-----------------------|
4770Sstevel@tonic-gate  *                       :                       :
4780Sstevel@tonic-gate  *                       |-----------------------|
4790Sstevel@tonic-gate  *                       |      module text      |
4800Sstevel@tonic-gate  *                       |-----------------------|- e_text/modtext
4810Sstevel@tonic-gate  *                       |      kernel text      |
4820Sstevel@tonic-gate  *                       |-----------------------|
4830Sstevel@tonic-gate  *                       |    trap table (48k)   |
4840Sstevel@tonic-gate  * 0x00000000.01000000  -|-----------------------|- KERNELBASE
4850Sstevel@tonic-gate  *                       | reserved for trapstat |} TSTAT_TOTAL_SIZE
4860Sstevel@tonic-gate  *                       |-----------------------|
4870Sstevel@tonic-gate  *                       |                       |
4880Sstevel@tonic-gate  *                       |        invalid        |
4890Sstevel@tonic-gate  *                       |                       |
4900Sstevel@tonic-gate  * 0x00000000.00000000  _|_______________________|
4910Sstevel@tonic-gate  *
4920Sstevel@tonic-gate  *
4930Sstevel@tonic-gate  *
4940Sstevel@tonic-gate  *                   32-bit User Virtual Memory Layout.
4950Sstevel@tonic-gate  *                       /-----------------------\
4960Sstevel@tonic-gate  *                       |                       |
4970Sstevel@tonic-gate  *                       |        invalid        |
4980Sstevel@tonic-gate  *                       |                       |
4990Sstevel@tonic-gate  *          0xFFC00000  -|-----------------------|- USERLIMIT
5000Sstevel@tonic-gate  *                       |       user stack      |
5010Sstevel@tonic-gate  *                       :                       :
5020Sstevel@tonic-gate  *                       :                       :
5030Sstevel@tonic-gate  *                       :                       :
5040Sstevel@tonic-gate  *                       |       user data       |
5050Sstevel@tonic-gate  *                      -|-----------------------|-
5060Sstevel@tonic-gate  *                       |       user text       |
5070Sstevel@tonic-gate  *          0x00002000  -|-----------------------|-
5080Sstevel@tonic-gate  *                       |       invalid         |
5090Sstevel@tonic-gate  *          0x00000000  _|_______________________|
5100Sstevel@tonic-gate  *
5110Sstevel@tonic-gate  *
5120Sstevel@tonic-gate  *
5130Sstevel@tonic-gate  *                   64-bit User Virtual Memory Layout.
5140Sstevel@tonic-gate  *                       /-----------------------\
5150Sstevel@tonic-gate  *                       |                       |
5160Sstevel@tonic-gate  *                       |        invalid        |
5170Sstevel@tonic-gate  *                       |                       |
5180Sstevel@tonic-gate  *  0xFFFFFFFF.80000000 -|-----------------------|- USERLIMIT
5190Sstevel@tonic-gate  *                       |       user stack      |
5200Sstevel@tonic-gate  *                       :                       :
5210Sstevel@tonic-gate  *                       :                       :
5220Sstevel@tonic-gate  *                       :                       :
5230Sstevel@tonic-gate  *                       |       user data       |
5240Sstevel@tonic-gate  *                      -|-----------------------|-
5250Sstevel@tonic-gate  *                       |       user text       |
5260Sstevel@tonic-gate  *  0x00000000.00100000 -|-----------------------|-
5270Sstevel@tonic-gate  *                       |       invalid         |
5280Sstevel@tonic-gate  *  0x00000000.00000000 _|_______________________|
5290Sstevel@tonic-gate  */
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate extern caddr_t ecache_init_scrub_flush_area(caddr_t alloc_base);
5320Sstevel@tonic-gate extern uint64_t ecache_flush_address(void);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate #pragma weak load_platform_modules
5350Sstevel@tonic-gate #pragma weak starcat_startup_memlist
5360Sstevel@tonic-gate #pragma weak ecache_init_scrub_flush_area
5370Sstevel@tonic-gate #pragma weak ecache_flush_address
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate  * By default the DR Cage is enabled for maximum OS
5420Sstevel@tonic-gate  * MPSS performance.  Users needing to disable the cage mechanism
5430Sstevel@tonic-gate  * can set this variable to zero via /etc/system.
5440Sstevel@tonic-gate  * Disabling the cage on systems supporting Dynamic Reconfiguration (DR)
5450Sstevel@tonic-gate  * will result in loss of DR functionality.
5460Sstevel@tonic-gate  * Platforms wishing to disable kernel Cage by default
5470Sstevel@tonic-gate  * should do so in their set_platform_defaults() routine.
5480Sstevel@tonic-gate  */
5490Sstevel@tonic-gate int	kernel_cage_enable = 1;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate static void
5520Sstevel@tonic-gate setup_cage_params(void)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	void (*func)(void);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	func = (void (*)(void))kobj_getsymvalue("set_platform_cage_params", 0);
5570Sstevel@tonic-gate 	if (func != NULL) {
5580Sstevel@tonic-gate 		(*func)();
5590Sstevel@tonic-gate 		return;
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	if (kernel_cage_enable == 0) {
5630Sstevel@tonic-gate 		return;
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 	kcage_range_lock();
5660Sstevel@tonic-gate 	if (kcage_range_init(phys_avail, 1) == 0) {
5670Sstevel@tonic-gate 		kcage_init(total_pages / 256);
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 	kcage_range_unlock();
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if (kcage_on) {
5720Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is ENABLED");
5730Sstevel@tonic-gate 	} else {
5740Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is DISABLED");
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate /*
5800Sstevel@tonic-gate  * Machine-dependent startup code
5810Sstevel@tonic-gate  */
5820Sstevel@tonic-gate void
5830Sstevel@tonic-gate startup(void)
5840Sstevel@tonic-gate {
5850Sstevel@tonic-gate 	startup_init();
5860Sstevel@tonic-gate 	if (&startup_platform)
5870Sstevel@tonic-gate 		startup_platform();
5880Sstevel@tonic-gate 	startup_memlist();
5890Sstevel@tonic-gate 	startup_modules();
5900Sstevel@tonic-gate 	setup_cage_params();
5910Sstevel@tonic-gate 	startup_bop_gone();
5920Sstevel@tonic-gate 	startup_vm();
5930Sstevel@tonic-gate 	startup_end();
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate struct regs sync_reg_buf;
5970Sstevel@tonic-gate uint64_t sync_tt;
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate void
6000Sstevel@tonic-gate sync_handler(void)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	struct  trap_info 	ti;
6030Sstevel@tonic-gate 	int i;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	/*
6060Sstevel@tonic-gate 	 * Prevent trying to talk to the other CPUs since they are
6070Sstevel@tonic-gate 	 * sitting in the prom and won't reply.
6080Sstevel@tonic-gate 	 */
6090Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
6100Sstevel@tonic-gate 		if ((i != CPU->cpu_id) && CPU_XCALL_READY(i)) {
6110Sstevel@tonic-gate 			cpu[i]->cpu_flags &= ~CPU_READY;
6120Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
6130Sstevel@tonic-gate 			CPUSET_DEL(cpu_ready_set, cpu[i]->cpu_id);
6140Sstevel@tonic-gate 		}
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/*
6180Sstevel@tonic-gate 	 * We've managed to get here without going through the
6190Sstevel@tonic-gate 	 * normal panic code path. Try and save some useful
6200Sstevel@tonic-gate 	 * information.
6210Sstevel@tonic-gate 	 */
6220Sstevel@tonic-gate 	if (!panicstr && (curthread->t_panic_trap == NULL)) {
6230Sstevel@tonic-gate 		ti.trap_type = sync_tt;
6240Sstevel@tonic-gate 		ti.trap_regs = &sync_reg_buf;
6250Sstevel@tonic-gate 		ti.trap_addr = NULL;
6260Sstevel@tonic-gate 		ti.trap_mmu_fsr = 0x0;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 		curthread->t_panic_trap = &ti;
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	/*
6320Sstevel@tonic-gate 	 * If we're re-entering the panic path, update the signature
6330Sstevel@tonic-gate 	 * block so that the SC knows we're in the second part of panic.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	if (panicstr)
6360Sstevel@tonic-gate 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DUMP, -1);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	nopanicdebug = 1; /* do not perform debug_enter() prior to dump */
6390Sstevel@tonic-gate 	panic("sync initiated");
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate static void
6440Sstevel@tonic-gate startup_init(void)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * We want to save the registers while we're still in OBP
6480Sstevel@tonic-gate 	 * so that we know they haven't been fiddled with since.
6490Sstevel@tonic-gate 	 * (In principle, OBP can't change them just because it
6500Sstevel@tonic-gate 	 * makes a callback, but we'd rather not depend on that
6510Sstevel@tonic-gate 	 * behavior.)
6520Sstevel@tonic-gate 	 */
6530Sstevel@tonic-gate 	char		sync_str[] =
6540Sstevel@tonic-gate 		"warning @ warning off : sync "
6550Sstevel@tonic-gate 		"%%tl-c %%tstate h# %p x! "
6560Sstevel@tonic-gate 		"%%g1 h# %p x! %%g2 h# %p x! %%g3 h# %p x! "
6570Sstevel@tonic-gate 		"%%g4 h# %p x! %%g5 h# %p x! %%g6 h# %p x! "
6580Sstevel@tonic-gate 		"%%g7 h# %p x! %%o0 h# %p x! %%o1 h# %p x! "
6590Sstevel@tonic-gate 		"%%o2 h# %p x! %%o3 h# %p x! %%o4 h# %p x! "
6600Sstevel@tonic-gate 		"%%o5 h# %p x! %%o6 h# %p x! %%o7 h# %p x! "
6610Sstevel@tonic-gate 		"%%tl-c %%tpc h# %p x! %%tl-c %%tnpc h# %p x! "
6620Sstevel@tonic-gate 		"%%y h# %p l! %%tl-c %%tt h# %p x! "
6630Sstevel@tonic-gate 		"sync ; warning !";
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	/*
6660Sstevel@tonic-gate 	 * 20 == num of %p substrings
6670Sstevel@tonic-gate 	 * 16 == max num of chars %p will expand to.
6680Sstevel@tonic-gate 	 */
6690Sstevel@tonic-gate 	char 		bp[sizeof (sync_str) + 16 * 20];
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * Initialize ptl1 stack for the 1st CPU.
6750Sstevel@tonic-gate 	 */
6760Sstevel@tonic-gate 	ptl1_init_cpu(&cpu0);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 * Initialize the address map for cache consistent mappings
6800Sstevel@tonic-gate 	 * to random pages; must be done after vac_size is set.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	ppmapinit();
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	/*
6850Sstevel@tonic-gate 	 * Initialize the PROM callback handler.
6860Sstevel@tonic-gate 	 */
6870Sstevel@tonic-gate 	init_vx_handler();
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 * have prom call sync_callback() to handle the sync and
6910Sstevel@tonic-gate 	 * save some useful information which will be stored in the
6920Sstevel@tonic-gate 	 * core file later.
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	(void) sprintf((char *)bp, sync_str,
6950Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_tstate, (void *)&sync_reg_buf.r_g1,
6960Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_g2, (void *)&sync_reg_buf.r_g3,
6970Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_g4, (void *)&sync_reg_buf.r_g5,
6980Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_g6, (void *)&sync_reg_buf.r_g7,
6990Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o0, (void *)&sync_reg_buf.r_o1,
7000Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o2, (void *)&sync_reg_buf.r_o3,
7010Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o4, (void *)&sync_reg_buf.r_o5,
7020Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o6, (void *)&sync_reg_buf.r_o7,
7030Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_pc, (void *)&sync_reg_buf.r_npc,
7040Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_y, (void *)&sync_tt);
7050Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
7060Sstevel@tonic-gate 	add_vx_handler("sync", 1, (void (*)(cell_t *))sync_handler);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate static u_longlong_t *boot_physinstalled, *boot_physavail, *boot_virtavail;
7100Sstevel@tonic-gate static size_t boot_physinstalled_len, boot_physavail_len, boot_virtavail_len;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate #define	IVSIZE	((MAXIVNUM + 1) * sizeof (struct intr_vector))
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate /*
7150Sstevel@tonic-gate  * As OBP takes up some RAM when the system boots, pages will already be "lost"
7160Sstevel@tonic-gate  * to the system and reflected in npages by the time we see it.
7170Sstevel@tonic-gate  *
7180Sstevel@tonic-gate  * We only want to allocate kernel structures in the 64-bit virtual address
7190Sstevel@tonic-gate  * space on systems with enough RAM to make the overhead of keeping track of
7200Sstevel@tonic-gate  * an extra kernel memory segment worthwhile.
7210Sstevel@tonic-gate  *
7220Sstevel@tonic-gate  * Since OBP has already performed its memory allocations by this point, if we
7230Sstevel@tonic-gate  * have more than MINMOVE_RAM_MB MB of RAM left free, go ahead and map
7240Sstevel@tonic-gate  * memory in the 64-bit virtual address space; otherwise keep allocations
7250Sstevel@tonic-gate  * contiguous with we've mapped so far in the 32-bit virtual address space.
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate #define	MINMOVE_RAM_MB	((size_t)1900)
7280Sstevel@tonic-gate #define	MB_TO_BYTES(mb)	((mb) * 1048576ul)
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate pgcnt_t	tune_npages = (pgcnt_t)
7310Sstevel@tonic-gate 	(MB_TO_BYTES(MINMOVE_RAM_MB)/ (size_t)MMU_PAGESIZE);
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate static void
7340Sstevel@tonic-gate startup_memlist(void)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	size_t alloc_sz;
7370Sstevel@tonic-gate 	size_t ctrs_sz;
7380Sstevel@tonic-gate 	caddr_t alloc_base;
7390Sstevel@tonic-gate 	caddr_t ctrs_base, ctrs_end;
7400Sstevel@tonic-gate 	caddr_t memspace;
7410Sstevel@tonic-gate 	caddr_t va;
7420Sstevel@tonic-gate 	int memblocks = 0;
7430Sstevel@tonic-gate 	struct memlist *cur;
7440Sstevel@tonic-gate 	size_t syslimit = (size_t)SYSLIMIT;
7450Sstevel@tonic-gate 	size_t sysbase = (size_t)SYSBASE;
7460Sstevel@tonic-gate 	int alloc_alignsize = MMU_PAGESIZE;
7470Sstevel@tonic-gate 	extern void page_coloring_init(void);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	/*
7500Sstevel@tonic-gate 	 * Initialize enough of the system to allow kmem_alloc to work by
7510Sstevel@tonic-gate 	 * calling boot to allocate its memory until the time that
7520Sstevel@tonic-gate 	 * kvm_init is completed.  The page structs are allocated after
7530Sstevel@tonic-gate 	 * rounding up end to the nearest page boundary; the memsegs are
7540Sstevel@tonic-gate 	 * initialized and the space they use comes from the kernel heap.
7550Sstevel@tonic-gate 	 * With appropriate initialization, they can be reallocated later
7560Sstevel@tonic-gate 	 * to a size appropriate for the machine's configuration.
7570Sstevel@tonic-gate 	 *
7580Sstevel@tonic-gate 	 * At this point, memory is allocated for things that will never
7590Sstevel@tonic-gate 	 * need to be freed, this used to be "valloced".  This allows a
7600Sstevel@tonic-gate 	 * savings as the pages don't need page structures to describe
7610Sstevel@tonic-gate 	 * them because them will not be managed by the vm system.
7620Sstevel@tonic-gate 	 */
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/*
7650Sstevel@tonic-gate 	 * We're loaded by boot with the following configuration (as
7660Sstevel@tonic-gate 	 * specified in the sun4u/conf/Mapfile):
7670Sstevel@tonic-gate 	 *
7680Sstevel@tonic-gate 	 * 	text:		4 MB chunk aligned on a 4MB boundary
7690Sstevel@tonic-gate 	 * 	data & bss:	4 MB chunk aligned on a 4MB boundary
7700Sstevel@tonic-gate 	 *
7710Sstevel@tonic-gate 	 * These two chunks will eventually be mapped by 2 locked 4MB
7720Sstevel@tonic-gate 	 * ttes and will represent the nucleus of the kernel.  This gives
7730Sstevel@tonic-gate 	 * us some free space that is already allocated, some or all of
7740Sstevel@tonic-gate 	 * which is made available to kernel module text.
7750Sstevel@tonic-gate 	 *
7760Sstevel@tonic-gate 	 * The free space in the data-bss chunk is used for nucleus
7770Sstevel@tonic-gate 	 * allocatable data structures and we reserve it using the
7780Sstevel@tonic-gate 	 * nalloc_base and nalloc_end variables.  This space is currently
7790Sstevel@tonic-gate 	 * being used for hat data structures required for tlb miss
7800Sstevel@tonic-gate 	 * handling operations.  We align nalloc_base to a l2 cache
7810Sstevel@tonic-gate 	 * linesize because this is the line size the hardware uses to
7820Sstevel@tonic-gate 	 * maintain cache coherency.
7830Sstevel@tonic-gate 	 * 256K is carved out for module data.
7840Sstevel@tonic-gate 	 */
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	nalloc_base = (caddr_t)roundup((uintptr_t)e_data, MMU_PAGESIZE);
7870Sstevel@tonic-gate 	moddata = nalloc_base;
7880Sstevel@tonic-gate 	e_moddata = nalloc_base + MODDATA;
7890Sstevel@tonic-gate 	nalloc_base = e_moddata;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	nalloc_end = (caddr_t)roundup((uintptr_t)nalloc_base, MMU_PAGESIZE4M);
7920Sstevel@tonic-gate 	valloc_base = nalloc_base;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	/*
7950Sstevel@tonic-gate 	 * Calculate the start of the data segment.
7960Sstevel@tonic-gate 	 */
7970Sstevel@tonic-gate 	sdata = (caddr_t)((uintptr_t)e_data & MMU_PAGEMASK4M);
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	PRM_DEBUG(moddata);
8000Sstevel@tonic-gate 	PRM_DEBUG(nalloc_base);
8010Sstevel@tonic-gate 	PRM_DEBUG(nalloc_end);
8020Sstevel@tonic-gate 	PRM_DEBUG(sdata);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	/*
8050Sstevel@tonic-gate 	 * Remember any slop after e_text so we can give it to the modules.
8060Sstevel@tonic-gate 	 */
8070Sstevel@tonic-gate 	PRM_DEBUG(e_text);
8080Sstevel@tonic-gate 	modtext = (caddr_t)roundup((uintptr_t)e_text, MMU_PAGESIZE);
8090Sstevel@tonic-gate 	if (((uintptr_t)modtext & MMU_PAGEMASK4M) != (uintptr_t)s_text)
8100Sstevel@tonic-gate 		panic("nucleus text overflow");
8110Sstevel@tonic-gate 	modtext_sz = (caddr_t)roundup((uintptr_t)modtext, MMU_PAGESIZE4M) -
8120Sstevel@tonic-gate 	    modtext;
8130Sstevel@tonic-gate 	PRM_DEBUG(modtext);
8140Sstevel@tonic-gate 	PRM_DEBUG(modtext_sz);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
8170Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
8180Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
8190Sstevel@tonic-gate 	/*
8200Sstevel@tonic-gate 	 * Remember what the physically available highest page is
8210Sstevel@tonic-gate 	 * so that dumpsys works properly, and find out how much
8220Sstevel@tonic-gate 	 * memory is installed.
8230Sstevel@tonic-gate 	 */
8240Sstevel@tonic-gate 	installed_top_size_memlist_array(boot_physinstalled,
8250Sstevel@tonic-gate 	    boot_physinstalled_len, &physmax, &physinstalled);
8260Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
8270Sstevel@tonic-gate 	PRM_DEBUG(physmax);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	/* Fill out memory nodes config structure */
8300Sstevel@tonic-gate 	startup_build_mem_nodes(boot_physinstalled, boot_physinstalled_len);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	/*
8330Sstevel@tonic-gate 	 * Get the list of physically available memory to size
8340Sstevel@tonic-gate 	 * the number of page structures needed.
8350Sstevel@tonic-gate 	 */
8360Sstevel@tonic-gate 	size_physavail(boot_physavail, boot_physavail_len, &npages, &memblocks);
8370Sstevel@tonic-gate 	/*
8380Sstevel@tonic-gate 	 * This first snap shot of npages can represent the pages used
8390Sstevel@tonic-gate 	 * by OBP's text and data approximately. This is used in the
8400Sstevel@tonic-gate 	 * the calculation of the kernel size
8410Sstevel@tonic-gate 	 */
8420Sstevel@tonic-gate 	obp_pages = physinstalled - npages;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	/*
8460Sstevel@tonic-gate 	 * On small-memory systems (<MODTEXT_SM_SIZE MB, currently 256MB), the
8470Sstevel@tonic-gate 	 * in-nucleus module text is capped to MODTEXT_SM_CAP bytes (currently
8480Sstevel@tonic-gate 	 * 2MB) and any excess pages are put on physavail.  The assumption is
8490Sstevel@tonic-gate 	 * that small-memory systems will need more pages more than they'll
8500Sstevel@tonic-gate 	 * need efficiently-mapped module texts.
8510Sstevel@tonic-gate 	 */
8520Sstevel@tonic-gate 	if ((physinstalled < mmu_btop(MODTEXT_SM_SIZE << 20)) &&
8530Sstevel@tonic-gate 	    modtext_sz > MODTEXT_SM_CAP) {
8540Sstevel@tonic-gate 		extra_etpg = mmu_btop(modtext_sz - MODTEXT_SM_CAP);
8550Sstevel@tonic-gate 		modtext_sz = MODTEXT_SM_CAP;
8560Sstevel@tonic-gate 	} else
8570Sstevel@tonic-gate 		extra_etpg = 0;
8580Sstevel@tonic-gate 	PRM_DEBUG(extra_etpg);
8590Sstevel@tonic-gate 	PRM_DEBUG(modtext_sz);
8600Sstevel@tonic-gate 	extra_etva = modtext + modtext_sz;
8610Sstevel@tonic-gate 	PRM_DEBUG(extra_etva);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	/*
8640Sstevel@tonic-gate 	 * Account for any pages after e_text and e_data.
8650Sstevel@tonic-gate 	 */
8660Sstevel@tonic-gate 	npages += extra_etpg;
8670Sstevel@tonic-gate 	npages += mmu_btopr(nalloc_end - nalloc_base);
8680Sstevel@tonic-gate 	PRM_DEBUG(npages);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * npages is the maximum of available physical memory possible.
8720Sstevel@tonic-gate 	 * (ie. it will never be more than this)
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/*
8760Sstevel@tonic-gate 	 * initialize the nucleus memory allocator.
8770Sstevel@tonic-gate 	 */
8780Sstevel@tonic-gate 	ndata_alloc_init(&ndata, (uintptr_t)nalloc_base, (uintptr_t)nalloc_end);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	/*
8810Sstevel@tonic-gate 	 * Allocate mmu fault status area from the nucleus data area.
8820Sstevel@tonic-gate 	 */
8830Sstevel@tonic-gate 	if ((&ndata_alloc_mmfsa != NULL) && (ndata_alloc_mmfsa(&ndata) != 0))
8840Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after mfsa alloc");
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	/*
8870Sstevel@tonic-gate 	 * Allocate kernel TSBs from the nucleus data area.
8880Sstevel@tonic-gate 	 */
8890Sstevel@tonic-gate 	if (ndata_alloc_tsbs(&ndata, npages) != 0)
8900Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after tsbs alloc");
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	/*
8930Sstevel@tonic-gate 	 * Allocate cpus structs from the nucleus data area.
8940Sstevel@tonic-gate 	 */
8950Sstevel@tonic-gate 	if (ndata_alloc_cpus(&ndata) != 0)
8960Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after cpu alloc");
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	/*
8990Sstevel@tonic-gate 	 * Allocate dmv dispatch table from the nucleus data area.
9000Sstevel@tonic-gate 	 */
9010Sstevel@tonic-gate 	if (ndata_alloc_dmv(&ndata) != 0)
9020Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after dmv alloc");
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	page_coloring_init();
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	/*
9080Sstevel@tonic-gate 	 * Allocate page_freelists bin headers for memnode 0 from the
9090Sstevel@tonic-gate 	 * nucleus data area.
9100Sstevel@tonic-gate 	 */
9110Sstevel@tonic-gate 	if (ndata_alloc_page_freelists(&ndata, 0) != 0)
9120Sstevel@tonic-gate 		cmn_err(CE_PANIC,
9130Sstevel@tonic-gate 		    "no more nucleus memory after page free lists alloc");
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	if (kpm_enable) {
9160Sstevel@tonic-gate 		kpm_init();
9170Sstevel@tonic-gate 		/*
9180Sstevel@tonic-gate 		 * kpm page space -- Update kpm_npages and make the
9190Sstevel@tonic-gate 		 * same assumption about fragmenting as it is done
9200Sstevel@tonic-gate 		 * for memseg_sz.
9210Sstevel@tonic-gate 		 */
9220Sstevel@tonic-gate 		kpm_npages_setup(memblocks + 4);
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	/*
9260Sstevel@tonic-gate 	 * Allocate hat related structs from the nucleus data area.
9270Sstevel@tonic-gate 	 */
9280Sstevel@tonic-gate 	if (ndata_alloc_hat(&ndata, npages, kpm_npages) != 0)
9290Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after hat alloc");
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	/*
9320Sstevel@tonic-gate 	 * We want to do the BOP_ALLOCs before the real allocation of page
9330Sstevel@tonic-gate 	 * structs in order to not have to allocate page structs for this
9340Sstevel@tonic-gate 	 * memory.  We need to calculate a virtual address because we want
9350Sstevel@tonic-gate 	 * the page structs to come before other allocations in virtual address
9360Sstevel@tonic-gate 	 * space.  This is so some (if not all) of page structs can actually
9370Sstevel@tonic-gate 	 * live in the nucleus.
9380Sstevel@tonic-gate 	 */
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	/*
9410Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
9420Sstevel@tonic-gate 	 *
9430Sstevel@tonic-gate 	 * There are comments all over the SFMMU code warning of dire
9440Sstevel@tonic-gate 	 * consequences if the TSBs are moved out of 32-bit space.  This
9450Sstevel@tonic-gate 	 * is largely because the asm code uses "sethi %hi(addr)"-type
9460Sstevel@tonic-gate 	 * instructions which will not provide the expected result if the
9470Sstevel@tonic-gate 	 * address is a 64-bit one.
9480Sstevel@tonic-gate 	 *
9490Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
9500Sstevel@tonic-gate 	 */
9510Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)nalloc_end, MMU_PAGESIZE);
9520Sstevel@tonic-gate 	alloc_base = sfmmu_ktsb_alloc(alloc_base);
9530Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
9540Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	/*
9570Sstevel@tonic-gate 	 * Allocate IOMMU TSB array.  We do this here so that the physical
9580Sstevel@tonic-gate 	 * memory gets deducted from the PROM's physical memory list.
9590Sstevel@tonic-gate 	 */
9600Sstevel@tonic-gate 	alloc_base = iommu_tsb_init(alloc_base);
9610Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
9620Sstevel@tonic-gate 	    ecache_alignsize);
9630Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/*
9660Sstevel@tonic-gate 	 * Starcat needs its special structures assigned in 32-bit virtual
9670Sstevel@tonic-gate 	 * address space because its probing routines execute FCode, and FCode
9680Sstevel@tonic-gate 	 * can't handle 64-bit virtual addresses...
9690Sstevel@tonic-gate 	 */
9700Sstevel@tonic-gate 	if (&starcat_startup_memlist) {
9710Sstevel@tonic-gate 		alloc_base = starcat_startup_memlist(alloc_base);
9720Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
9730Sstevel@tonic-gate 		    ecache_alignsize);
9740Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	/*
9780Sstevel@tonic-gate 	 * If we have enough memory, use 4M pages for alignment because it
9790Sstevel@tonic-gate 	 * greatly reduces the number of TLB misses we take albeit at the cost
9800Sstevel@tonic-gate 	 * of possible RAM wastage (degenerate case of 4 MB - MMU_PAGESIZE per
9810Sstevel@tonic-gate 	 * allocation.) Still, the speedup on large memory systems (e.g. > 64
9820Sstevel@tonic-gate 	 * GB) is quite noticeable, so it is worth the effort to do if we can.
9830Sstevel@tonic-gate 	 *
9840Sstevel@tonic-gate 	 * Note, however, that this speedup will only occur if the boot PROM
9850Sstevel@tonic-gate 	 * uses the largest possible MMU page size possible to map memory
9860Sstevel@tonic-gate 	 * requests that are properly aligned and sized (for example, a request
9870Sstevel@tonic-gate 	 * for a multiple of 4MB of memory aligned to a 4MB boundary will
9880Sstevel@tonic-gate 	 * result in a mapping using a 4MB MMU page.)
9890Sstevel@tonic-gate 	 *
9900Sstevel@tonic-gate 	 * Even then, the large page mappings will only speed things up until
9910Sstevel@tonic-gate 	 * the startup process proceeds a bit further, as when
9920Sstevel@tonic-gate 	 * sfmmu_map_prom_mappings() copies page mappings from the PROM to the
9930Sstevel@tonic-gate 	 * kernel it remaps everything but the TSBs using 8K pages anyway...
9940Sstevel@tonic-gate 	 *
9950Sstevel@tonic-gate 	 * At some point in the future, sfmmu_map_prom_mappings() will be
9960Sstevel@tonic-gate 	 * rewritten to copy memory mappings to the kernel using the same MMU
9970Sstevel@tonic-gate 	 * page sizes the PROM used.  When that occurs, if the PROM did use
9980Sstevel@tonic-gate 	 * large MMU pages to map memory, the alignment/sizing work we're
9990Sstevel@tonic-gate 	 * doing now should give us a nice extra performance boost, albeit at
10000Sstevel@tonic-gate 	 * the cost of greater RAM usage...
10010Sstevel@tonic-gate 	 */
10020Sstevel@tonic-gate 	alloc_alignsize = ((npages >= tune_npages) ? MMU_PAGESIZE4M :
10030Sstevel@tonic-gate 	    MMU_PAGESIZE);
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	PRM_DEBUG(tune_npages);
10060Sstevel@tonic-gate 	PRM_DEBUG(alloc_alignsize);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	/*
10090Sstevel@tonic-gate 	 * Save off where the contiguous allocations to date have ended
10100Sstevel@tonic-gate 	 * in econtig32.
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 	econtig32 = alloc_base;
10130Sstevel@tonic-gate 	PRM_DEBUG(econtig32);
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	if (econtig32 > (caddr_t)KERNEL_LIMIT32)
10160Sstevel@tonic-gate 		cmn_err(CE_PANIC, "econtig32 too big");
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	/*
10190Sstevel@tonic-gate 	 * To avoid memory allocation collisions in the 32-bit virtual address
10200Sstevel@tonic-gate 	 * space, make allocations from this point forward in 64-bit virtual
10210Sstevel@tonic-gate 	 * address space starting at syslimit and working up.  Also use the
10220Sstevel@tonic-gate 	 * alignment specified by alloc_alignsize, as we may be able to save
10230Sstevel@tonic-gate 	 * ourselves TLB misses by using larger page sizes if they're
10240Sstevel@tonic-gate 	 * available.
10250Sstevel@tonic-gate 	 *
10260Sstevel@tonic-gate 	 * All this is needed because on large memory systems, the default
10270Sstevel@tonic-gate 	 * Solaris allocations will collide with SYSBASE32, which is hard
10280Sstevel@tonic-gate 	 * coded to be at the virtual address 0x78000000.  Therefore, on 64-bit
10290Sstevel@tonic-gate 	 * kernels, move the allocations to a location in the 64-bit virtual
10300Sstevel@tonic-gate 	 * address space space, allowing those structures to grow without
10310Sstevel@tonic-gate 	 * worry.
10320Sstevel@tonic-gate 	 *
10330Sstevel@tonic-gate 	 * On current CPUs we'll run out of physical memory address bits before
10340Sstevel@tonic-gate 	 * we need to worry about the allocations running into anything else in
10350Sstevel@tonic-gate 	 * VM or the virtual address holes on US-I and II, as there's currently
10360Sstevel@tonic-gate 	 * about 1 TB of addressable space before the US-I/II VA hole.
10370Sstevel@tonic-gate 	 */
10380Sstevel@tonic-gate 	kmem64_base = (caddr_t)syslimit;
10390Sstevel@tonic-gate 	PRM_DEBUG(kmem64_base);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)kmem64_base, alloc_alignsize);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	/*
10440Sstevel@tonic-gate 	 * If KHME and/or UHME hash buckets won't fit in the nucleus, allocate
10450Sstevel@tonic-gate 	 * them here.
10460Sstevel@tonic-gate 	 */
10470Sstevel@tonic-gate 	if (khme_hash == NULL || uhme_hash == NULL) {
10480Sstevel@tonic-gate 		/*
10490Sstevel@tonic-gate 		 * alloc_hme_buckets() will align alloc_base properly before
10500Sstevel@tonic-gate 		 * assigning the hash buckets, so we don't need to do it
10510Sstevel@tonic-gate 		 * before the call...
10520Sstevel@tonic-gate 		 */
10530Sstevel@tonic-gate 		alloc_base = alloc_hme_buckets(alloc_base, alloc_alignsize);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
10560Sstevel@tonic-gate 		PRM_DEBUG(khme_hash);
10570Sstevel@tonic-gate 		PRM_DEBUG(uhme_hash);
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	/*
10610Sstevel@tonic-gate 	 * Allocate the remaining page freelists.  NUMA systems can
10620Sstevel@tonic-gate 	 * have lots of page freelists, one per node, which quickly
10630Sstevel@tonic-gate 	 * outgrow the amount of nucleus memory available.
10640Sstevel@tonic-gate 	 */
10650Sstevel@tonic-gate 	if (max_mem_nodes > 1) {
10660Sstevel@tonic-gate 		int mnode;
10670Sstevel@tonic-gate 		caddr_t alloc_start = alloc_base;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 		for (mnode = 1; mnode < max_mem_nodes; mnode++) {
10700Sstevel@tonic-gate 			alloc_base = alloc_page_freelists(mnode, alloc_base,
10710Sstevel@tonic-gate 				ecache_alignsize);
10720Sstevel@tonic-gate 		}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 		if (alloc_base > alloc_start) {
10750Sstevel@tonic-gate 			alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
10760Sstevel@tonic-gate 				alloc_alignsize);
10770Sstevel@tonic-gate 			if ((caddr_t)BOP_ALLOC(bootops, alloc_start,
10780Sstevel@tonic-gate 				alloc_base - alloc_start,
10790Sstevel@tonic-gate 				alloc_alignsize) != alloc_start)
10800Sstevel@tonic-gate 				cmn_err(CE_PANIC,
10810Sstevel@tonic-gate 					"Unable to alloc page freelists\n");
10820Sstevel@tonic-gate 		}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	if (!mml_table) {
10880Sstevel@tonic-gate 		size_t mmltable_sz;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 		/*
10910Sstevel@tonic-gate 		 * We need to allocate the mml_table here because there
10920Sstevel@tonic-gate 		 * was not enough space within the nucleus.
10930Sstevel@tonic-gate 		 */
10940Sstevel@tonic-gate 		mmltable_sz = sizeof (kmutex_t) * mml_table_sz;
10950Sstevel@tonic-gate 		alloc_sz = roundup(mmltable_sz, alloc_alignsize);
10960Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
10970Sstevel@tonic-gate 		    alloc_alignsize);
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 		if ((mml_table = (kmutex_t *)BOP_ALLOC(bootops, alloc_base,
11000Sstevel@tonic-gate 		    alloc_sz, alloc_alignsize)) != (kmutex_t *)alloc_base)
11010Sstevel@tonic-gate 			panic("mml_table alloc failure");
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 		alloc_base += alloc_sz;
11040Sstevel@tonic-gate 		PRM_DEBUG(mml_table);
11050Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	if (kpm_enable && !(kpmp_table || kpmp_stable)) {
11090Sstevel@tonic-gate 		size_t kpmptable_sz;
11100Sstevel@tonic-gate 		caddr_t table;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 		/*
11130Sstevel@tonic-gate 		 * We need to allocate either kpmp_table or kpmp_stable here
11140Sstevel@tonic-gate 		 * because there was not enough space within the nucleus.
11150Sstevel@tonic-gate 		 */
11160Sstevel@tonic-gate 		kpmptable_sz = (kpm_smallpages == 0) ?
11170Sstevel@tonic-gate 				sizeof (kpm_hlk_t) * kpmp_table_sz :
11180Sstevel@tonic-gate 				sizeof (kpm_shlk_t) * kpmp_stable_sz;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 		alloc_sz = roundup(kpmptable_sz, alloc_alignsize);
11210Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
11220Sstevel@tonic-gate 		    alloc_alignsize);
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		table = BOP_ALLOC(bootops, alloc_base, alloc_sz,
11250Sstevel@tonic-gate 				alloc_alignsize);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 		if (table != alloc_base)
11280Sstevel@tonic-gate 			panic("kpmp_table or kpmp_stable alloc failure");
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 		if (kpm_smallpages == 0) {
11310Sstevel@tonic-gate 			kpmp_table = (kpm_hlk_t *)table;
11320Sstevel@tonic-gate 			PRM_DEBUG(kpmp_table);
11330Sstevel@tonic-gate 		} else {
11340Sstevel@tonic-gate 			kpmp_stable = (kpm_shlk_t *)table;
11350Sstevel@tonic-gate 			PRM_DEBUG(kpmp_stable);
11360Sstevel@tonic-gate 		}
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 		alloc_base += alloc_sz;
11390Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	if (&ecache_init_scrub_flush_area) {
11430Sstevel@tonic-gate 		/*
11440Sstevel@tonic-gate 		 * Pass alloc_base directly, as the routine itself is
11450Sstevel@tonic-gate 		 * responsible for any special alignment requirements...
11460Sstevel@tonic-gate 		 */
11470Sstevel@tonic-gate 		alloc_base = ecache_init_scrub_flush_area(alloc_base);
11480Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/*
11520Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
11530Sstevel@tonic-gate 	 */
11540Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
11550Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
11560Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/*
11590Sstevel@tonic-gate 	 * Reset npages and memblocks based on boot_physavail list.
11600Sstevel@tonic-gate 	 */
11610Sstevel@tonic-gate 	size_physavail(boot_physavail, boot_physavail_len, &npages, &memblocks);
11620Sstevel@tonic-gate 	PRM_DEBUG(npages);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	/*
11650Sstevel@tonic-gate 	 * Account for extra memory after e_text.
11660Sstevel@tonic-gate 	 */
11670Sstevel@tonic-gate 	npages += extra_etpg;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	/*
11700Sstevel@tonic-gate 	 * Calculate the largest free memory chunk in the nucleus data area.
11710Sstevel@tonic-gate 	 * We need to figure out if page structs can fit in there or not.
11720Sstevel@tonic-gate 	 * We also make sure enough page structs get created for any physical
11730Sstevel@tonic-gate 	 * memory we might be returning to the system.
11740Sstevel@tonic-gate 	 */
11750Sstevel@tonic-gate 	ndata_remain_sz = ndata_maxsize(&ndata);
11760Sstevel@tonic-gate 	PRM_DEBUG(ndata_remain_sz);
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	pp_sz = sizeof (struct page) * npages;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	/*
11810Sstevel@tonic-gate 	 * Here's a nice bit of code based on somewhat recursive logic:
11820Sstevel@tonic-gate 	 *
11830Sstevel@tonic-gate 	 * If the page array would fit within the nucleus, we want to
11840Sstevel@tonic-gate 	 * add npages to cover any extra memory we may be returning back
11850Sstevel@tonic-gate 	 * to the system.
11860Sstevel@tonic-gate 	 *
11870Sstevel@tonic-gate 	 * HOWEVER, the page array is sized by calculating the size of
11880Sstevel@tonic-gate 	 * (struct page * npages), as are the pagehash table, ctrs and
11890Sstevel@tonic-gate 	 * memseg_list, so the very act of performing the calculation below may
11900Sstevel@tonic-gate 	 * in fact make the array large enough that it no longer fits in the
11910Sstevel@tonic-gate 	 * nucleus, meaning there would now be a much larger area of the
11920Sstevel@tonic-gate 	 * nucleus free that should really be added to npages, which would
11930Sstevel@tonic-gate 	 * make the page array that much larger, and so on.
11940Sstevel@tonic-gate 	 *
11950Sstevel@tonic-gate 	 * This also ignores the memory possibly used in the nucleus for the
11960Sstevel@tonic-gate 	 * the page hash, ctrs and memseg list and the fact that whether they
11970Sstevel@tonic-gate 	 * fit there or not varies with the npages calculation below, but we
11980Sstevel@tonic-gate 	 * don't even factor them into the equation at this point; perhaps we
11990Sstevel@tonic-gate 	 * should or perhaps we should just take the approach that the few
12000Sstevel@tonic-gate 	 * extra pages we could add via this calculation REALLY aren't worth
12010Sstevel@tonic-gate 	 * the hassle...
12020Sstevel@tonic-gate 	 */
12030Sstevel@tonic-gate 	if (ndata_remain_sz > pp_sz) {
12040Sstevel@tonic-gate 		size_t spare = ndata_spare(&ndata, pp_sz, ecache_alignsize);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 		npages += mmu_btop(spare);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 		pp_sz = npages * sizeof (struct page);
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 		pp_base = ndata_alloc(&ndata, pp_sz, ecache_alignsize);
12110Sstevel@tonic-gate 	}
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	/*
12140Sstevel@tonic-gate 	 * If physmem is patched to be non-zero, use it instead of
12150Sstevel@tonic-gate 	 * the monitor value unless physmem is larger than the total
12160Sstevel@tonic-gate 	 * amount of memory on hand.
12170Sstevel@tonic-gate 	 */
12180Sstevel@tonic-gate 	if (physmem == 0 || physmem > npages)
12190Sstevel@tonic-gate 		physmem = npages;
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	/*
12220Sstevel@tonic-gate 	 * If pp_base is NULL that means the routines above have determined
12230Sstevel@tonic-gate 	 * the page array will not fit in the nucleus; we'll have to
12240Sstevel@tonic-gate 	 * BOP_ALLOC() ourselves some space for them.
12250Sstevel@tonic-gate 	 */
12260Sstevel@tonic-gate 	if (pp_base == NULL) {
12270Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
12280Sstevel@tonic-gate 		    alloc_alignsize);
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 		alloc_sz = roundup(pp_sz, alloc_alignsize);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 		if ((pp_base = (struct page *)BOP_ALLOC(bootops,
12330Sstevel@tonic-gate 		    alloc_base, alloc_sz, alloc_alignsize)) !=
12340Sstevel@tonic-gate 		    (struct page *)alloc_base)
12350Sstevel@tonic-gate 			panic("page alloc failure");
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 		alloc_base += alloc_sz;
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	/*
12410Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
12420Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
12430Sstevel@tonic-gate 	 */
12440Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
12450Sstevel@tonic-gate 	page_hashsz = 1 << highbit((ulong_t)page_hashsz);
12460Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	/*
12490Sstevel@tonic-gate 	 * We want to TRY to fit the page structure hash table,
12500Sstevel@tonic-gate 	 * the page size free list counters, the memseg list and
12510Sstevel@tonic-gate 	 * and the kpm page space in the nucleus if possible.
12520Sstevel@tonic-gate 	 *
12530Sstevel@tonic-gate 	 * alloc_sz counts how much memory needs to be allocated by
12540Sstevel@tonic-gate 	 * BOP_ALLOC().
12550Sstevel@tonic-gate 	 */
12560Sstevel@tonic-gate 	page_hash = ndata_alloc(&ndata, pagehash_sz, ecache_alignsize);
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	alloc_sz = (page_hash == NULL ? pagehash_sz : 0);
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	/*
12610Sstevel@tonic-gate 	 * Size up per page size free list counters.
12620Sstevel@tonic-gate 	 */
12630Sstevel@tonic-gate 	ctrs_sz = page_ctrs_sz();
12640Sstevel@tonic-gate 	ctrs_base = ndata_alloc(&ndata, ctrs_sz, ecache_alignsize);
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	if (ctrs_base == NULL)
12670Sstevel@tonic-gate 		alloc_sz = roundup(alloc_sz, ecache_alignsize) + ctrs_sz;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	/*
12700Sstevel@tonic-gate 	 * The memseg list is for the chunks of physical memory that
12710Sstevel@tonic-gate 	 * will be managed by the vm system.  The number calculated is
12720Sstevel@tonic-gate 	 * a guess as boot may fragment it more when memory allocations
12730Sstevel@tonic-gate 	 * are made before kphysm_init().  Currently, there are two
12740Sstevel@tonic-gate 	 * allocations before then, so we assume each causes fragmen-
12750Sstevel@tonic-gate 	 * tation, and add a couple more for good measure.
12760Sstevel@tonic-gate 	 */
12770Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + 4);
12780Sstevel@tonic-gate 	memseg_base = ndata_alloc(&ndata, memseg_sz, ecache_alignsize);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	if (memseg_base == NULL)
12810Sstevel@tonic-gate 		alloc_sz = roundup(alloc_sz, ecache_alignsize) + memseg_sz;
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	if (kpm_enable) {
12850Sstevel@tonic-gate 		/*
12860Sstevel@tonic-gate 		 * kpm page space -- Update kpm_npages and make the
12870Sstevel@tonic-gate 		 * same assumption about fragmenting as it is done
12880Sstevel@tonic-gate 		 * for memseg_sz above.
12890Sstevel@tonic-gate 		 */
12900Sstevel@tonic-gate 		kpm_npages_setup(memblocks + 4);
12910Sstevel@tonic-gate 		kpm_pp_sz = (kpm_smallpages == 0) ?
12920Sstevel@tonic-gate 				kpm_npages * sizeof (kpm_page_t):
12930Sstevel@tonic-gate 				kpm_npages * sizeof (kpm_spage_t);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 		kpm_pp_base = (uintptr_t)ndata_alloc(&ndata, kpm_pp_sz,
12960Sstevel@tonic-gate 		    ecache_alignsize);
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 		if (kpm_pp_base == NULL)
12990Sstevel@tonic-gate 			alloc_sz = roundup(alloc_sz, ecache_alignsize) +
13000Sstevel@tonic-gate 			    kpm_pp_sz;
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	if (alloc_sz > 0) {
13040Sstevel@tonic-gate 		uintptr_t bop_base;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 		/*
13070Sstevel@tonic-gate 		 * We need extra memory allocated through BOP_ALLOC.
13080Sstevel@tonic-gate 		 */
13090Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
13100Sstevel@tonic-gate 		    alloc_alignsize);
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 		alloc_sz = roundup(alloc_sz, alloc_alignsize);
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 		if ((bop_base = (uintptr_t)BOP_ALLOC(bootops, alloc_base,
13150Sstevel@tonic-gate 		    alloc_sz, alloc_alignsize)) != (uintptr_t)alloc_base)
13160Sstevel@tonic-gate 			panic("system page struct alloc failure");
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 		alloc_base += alloc_sz;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 		if (page_hash == NULL) {
13210Sstevel@tonic-gate 			page_hash = (struct page **)bop_base;
13220Sstevel@tonic-gate 			bop_base = roundup(bop_base + pagehash_sz,
13230Sstevel@tonic-gate 			    ecache_alignsize);
13240Sstevel@tonic-gate 		}
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 		if (ctrs_base == NULL) {
13270Sstevel@tonic-gate 			ctrs_base = (caddr_t)bop_base;
13280Sstevel@tonic-gate 			bop_base = roundup(bop_base + ctrs_sz,
13290Sstevel@tonic-gate 			    ecache_alignsize);
13300Sstevel@tonic-gate 		}
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 		if (memseg_base == NULL) {
13330Sstevel@tonic-gate 			memseg_base = (struct memseg *)bop_base;
13340Sstevel@tonic-gate 			bop_base = roundup(bop_base + memseg_sz,
13350Sstevel@tonic-gate 			    ecache_alignsize);
13360Sstevel@tonic-gate 		}
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 		if (kpm_enable && kpm_pp_base == NULL) {
13390Sstevel@tonic-gate 			kpm_pp_base = (uintptr_t)bop_base;
13400Sstevel@tonic-gate 			bop_base = roundup(bop_base + kpm_pp_sz,
13410Sstevel@tonic-gate 			    ecache_alignsize);
13420Sstevel@tonic-gate 		}
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 		ASSERT(bop_base <= (uintptr_t)alloc_base);
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	/*
13480Sstevel@tonic-gate 	 * Initialize per page size free list counters.
13490Sstevel@tonic-gate 	 */
13500Sstevel@tonic-gate 	ctrs_end = page_ctrs_alloc(ctrs_base);
13510Sstevel@tonic-gate 	ASSERT(ctrs_base + ctrs_sz >= ctrs_end);
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	PRM_DEBUG(page_hash);
13540Sstevel@tonic-gate 	PRM_DEBUG(memseg_base);
13550Sstevel@tonic-gate 	PRM_DEBUG(kpm_pp_base);
13560Sstevel@tonic-gate 	PRM_DEBUG(kpm_pp_sz);
13570Sstevel@tonic-gate 	PRM_DEBUG(pp_base);
13580Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
13590Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate #ifdef	TRAPTRACE
13620Sstevel@tonic-gate 	/*
13630Sstevel@tonic-gate 	 * Allocate trap trace buffer last so as not to affect
13640Sstevel@tonic-gate 	 * the 4M alignments of the allocations above on V9 SPARCs...
13650Sstevel@tonic-gate 	 */
13660Sstevel@tonic-gate 	alloc_base = trap_trace_alloc(alloc_base);
13670Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
13680Sstevel@tonic-gate #endif	/* TRAPTRACE */
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	if (kmem64_base) {
13710Sstevel@tonic-gate 		/*
13720Sstevel@tonic-gate 		 * Set the end of the kmem64 segment for V9 SPARCs, if
13730Sstevel@tonic-gate 		 * appropriate...
13740Sstevel@tonic-gate 		 */
13750Sstevel@tonic-gate 		kmem64_end = (caddr_t)roundup((uintptr_t)alloc_base,
13760Sstevel@tonic-gate 		    alloc_alignsize);
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 		PRM_DEBUG(kmem64_base);
13790Sstevel@tonic-gate 		PRM_DEBUG(kmem64_end);
13800Sstevel@tonic-gate 	}
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	/*
13830Sstevel@tonic-gate 	 * Allocate space for the interrupt vector table.
13840Sstevel@tonic-gate 	 */
13850Sstevel@tonic-gate 	memspace = (caddr_t)BOP_ALLOC(bootops, (caddr_t)intr_vector,
13860Sstevel@tonic-gate 	    IVSIZE, MMU_PAGESIZE);
13870Sstevel@tonic-gate 	if (memspace != (caddr_t)intr_vector)
13880Sstevel@tonic-gate 		panic("interrupt table allocation failure");
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	/*
13910Sstevel@tonic-gate 	 * The memory lists from boot are allocated from the heap arena
13920Sstevel@tonic-gate 	 * so that later they can be freed and/or reallocated.
13930Sstevel@tonic-gate 	 */
13940Sstevel@tonic-gate 	if (BOP_GETPROP(bootops, "extent", &memlist_sz) == -1)
13950Sstevel@tonic-gate 		panic("could not retrieve property \"extent\"");
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	/*
13980Sstevel@tonic-gate 	 * Between now and when we finish copying in the memory lists,
13990Sstevel@tonic-gate 	 * allocations happen so the space gets fragmented and the
14000Sstevel@tonic-gate 	 * lists longer.  Leave enough space for lists twice as long
14010Sstevel@tonic-gate 	 * as what boot says it has now; roundup to a pagesize.
14020Sstevel@tonic-gate 	 * Also add space for the final phys-avail copy in the fixup
14030Sstevel@tonic-gate 	 * routine.
14040Sstevel@tonic-gate 	 */
14050Sstevel@tonic-gate 	va = (caddr_t)(sysbase + PAGESIZE + PANICBUFSIZE +
14060Sstevel@tonic-gate 	    roundup(IVSIZE, MMU_PAGESIZE));
14070Sstevel@tonic-gate 	memlist_sz *= 4;
14080Sstevel@tonic-gate 	memlist_sz = roundup(memlist_sz, MMU_PAGESIZE);
14090Sstevel@tonic-gate 	memspace = (caddr_t)BOP_ALLOC(bootops, va, memlist_sz, BO_NO_ALIGN);
14100Sstevel@tonic-gate 	if (memspace == NULL)
14110Sstevel@tonic-gate 		halt("Boot allocation failed.");
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	memlist = (struct memlist *)memspace;
14140Sstevel@tonic-gate 	memlist_end = (char *)memspace + memlist_sz;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	PRM_DEBUG(memlist);
14170Sstevel@tonic-gate 	PRM_DEBUG(memlist_end);
14180Sstevel@tonic-gate 	PRM_DEBUG(sysbase);
14190Sstevel@tonic-gate 	PRM_DEBUG(syslimit);
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	kernelheap_init((void *)sysbase, (void *)syslimit,
14220Sstevel@tonic-gate 	    (caddr_t)sysbase + PAGESIZE, NULL, NULL);
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 	/*
14250Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
14260Sstevel@tonic-gate 	 */
14270Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
14280Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
14290Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	/*
14320Sstevel@tonic-gate 	 * Remove the space used by BOP_ALLOC from the kernel heap
14330Sstevel@tonic-gate 	 * plus the area actually used by the OBP (if any)
14340Sstevel@tonic-gate 	 * ignoring virtual addresses in virt_avail, above syslimit.
14350Sstevel@tonic-gate 	 */
14360Sstevel@tonic-gate 	virt_avail = memlist;
14370Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	for (cur = virt_avail; cur->next; cur = cur->next) {
14400Sstevel@tonic-gate 		uint64_t range_base, range_size;
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 		if ((range_base = cur->address + cur->size) < (uint64_t)sysbase)
14430Sstevel@tonic-gate 			continue;
14440Sstevel@tonic-gate 		if (range_base >= (uint64_t)syslimit)
14450Sstevel@tonic-gate 			break;
14460Sstevel@tonic-gate 		/*
14470Sstevel@tonic-gate 		 * Limit the range to end at syslimit.
14480Sstevel@tonic-gate 		 */
14490Sstevel@tonic-gate 		range_size = MIN(cur->next->address,
14500Sstevel@tonic-gate 		    (uint64_t)syslimit) - range_base;
14510Sstevel@tonic-gate 		(void) vmem_xalloc(heap_arena, (size_t)range_size, PAGESIZE,
14520Sstevel@tonic-gate 		    0, 0, (void *)range_base, (void *)(range_base + range_size),
14530Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
14540Sstevel@tonic-gate 	}
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	phys_avail = memlist;
14570Sstevel@tonic-gate 	(void) copy_physavail(boot_physavail, boot_physavail_len,
14580Sstevel@tonic-gate 	    &memlist, 0, 0);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	/*
14610Sstevel@tonic-gate 	 * Add any extra memory after e_text to the phys_avail list, as long
14620Sstevel@tonic-gate 	 * as there's at least a page to add.
14630Sstevel@tonic-gate 	 */
14640Sstevel@tonic-gate 	if (extra_etpg)
14650Sstevel@tonic-gate 		memlist_add(va_to_pa(extra_etva), mmu_ptob(extra_etpg),
14660Sstevel@tonic-gate 		    &memlist, &phys_avail);
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	/*
14690Sstevel@tonic-gate 	 * Add any extra memory after e_data to the phys_avail list as long
14700Sstevel@tonic-gate 	 * as there's at least a page to add.  Usually, there isn't any,
14710Sstevel@tonic-gate 	 * since extra HME blocks typically get allocated there first before
14720Sstevel@tonic-gate 	 * using RAM elsewhere.
14730Sstevel@tonic-gate 	 */
14740Sstevel@tonic-gate 	if ((nalloc_base = ndata_extra_base(&ndata, MMU_PAGESIZE)) == NULL)
14750Sstevel@tonic-gate 		nalloc_base = nalloc_end;
14760Sstevel@tonic-gate 	ndata_remain_sz = nalloc_end - nalloc_base;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	if (ndata_remain_sz >= MMU_PAGESIZE)
14790Sstevel@tonic-gate 		memlist_add(va_to_pa(nalloc_base),
14800Sstevel@tonic-gate 		    (uint64_t)ndata_remain_sz, &memlist, &phys_avail);
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	PRM_DEBUG(memlist);
14830Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
14840Sstevel@tonic-gate 	PRM_DEBUG(memspace);
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	if ((caddr_t)memlist > (memspace + memlist_sz))
14870Sstevel@tonic-gate 		panic("memlist overflow");
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	PRM_DEBUG(pp_base);
14900Sstevel@tonic-gate 	PRM_DEBUG(memseg_base);
14910Sstevel@tonic-gate 	PRM_DEBUG(npages);
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	/*
14940Sstevel@tonic-gate 	 * Initialize the page structures from the memory lists.
14950Sstevel@tonic-gate 	 */
14960Sstevel@tonic-gate 	kphysm_init(pp_base, memseg_base, npages, kpm_pp_base, kpm_npages);
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	availrmem_initial = availrmem = freemem;
14990Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	/*
15020Sstevel@tonic-gate 	 * Some of the locks depend on page_hashsz being set!
15030Sstevel@tonic-gate 	 * kmem_init() depends on this; so, keep it here.
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 	page_lock_init();
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	/*
15080Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
15090Sstevel@tonic-gate 	 */
15100Sstevel@tonic-gate 	kmem_init();
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	/*
15130Sstevel@tonic-gate 	 * Initialize bp_mapin().
15140Sstevel@tonic-gate 	 */
15150Sstevel@tonic-gate 	bp_init(shm_alignment, HAT_STRICTORDER);
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	/*
15180Sstevel@tonic-gate 	 * Reserve space for panicbuf and intr_vector from the 32-bit heap
15190Sstevel@tonic-gate 	 */
15200Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, PANICBUFSIZE, PAGESIZE, 0, 0,
15210Sstevel@tonic-gate 	    panicbuf, panicbuf + PANICBUFSIZE,
15220Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, IVSIZE, PAGESIZE, 0, 0,
15250Sstevel@tonic-gate 	    intr_vector, (caddr_t)intr_vector + IVSIZE,
15260Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	mem_config_init();
15290Sstevel@tonic-gate }
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate static void
15320Sstevel@tonic-gate startup_modules(void)
15330Sstevel@tonic-gate {
15340Sstevel@tonic-gate 	int proplen, nhblk1, nhblk8;
15350Sstevel@tonic-gate 	size_t  nhblksz;
15360Sstevel@tonic-gate 	pgcnt_t hblk_pages, pages_per_hblk;
15370Sstevel@tonic-gate 	size_t hme8blk_sz, hme1blk_sz;
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	/*
15400Sstevel@tonic-gate 	 * Log any optional messages from the boot program
15410Sstevel@tonic-gate 	 */
15420Sstevel@tonic-gate 	proplen = (size_t)BOP_GETPROPLEN(bootops, "boot-message");
15430Sstevel@tonic-gate 	if (proplen > 0) {
15440Sstevel@tonic-gate 		char *msg;
15450Sstevel@tonic-gate 		size_t len = (size_t)proplen;
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 		msg = kmem_zalloc(len, KM_SLEEP);
15480Sstevel@tonic-gate 		(void) BOP_GETPROP(bootops, "boot-message", msg);
15490Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s\n", msg);
15500Sstevel@tonic-gate 		kmem_free(msg, len);
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	/*
15540Sstevel@tonic-gate 	 * Let the platforms have a chance to change default
15550Sstevel@tonic-gate 	 * values before reading system file.
15560Sstevel@tonic-gate 	 */
15570Sstevel@tonic-gate 	if (&set_platform_defaults)
15580Sstevel@tonic-gate 		set_platform_defaults();
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	/*
15610Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
15620Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
15630Sstevel@tonic-gate 	 */
15640Sstevel@tonic-gate 	param_calc(0);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	mod_setup();
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	/*
15690Sstevel@tonic-gate 	 * If this is a positron, complain and halt.
15700Sstevel@tonic-gate 	 */
15710Sstevel@tonic-gate 	if (&iam_positron && iam_positron()) {
15720Sstevel@tonic-gate 		cmn_err(CE_WARN, "This hardware platform is not supported"
15730Sstevel@tonic-gate 		    " by this release of Solaris.\n");
15740Sstevel@tonic-gate #ifdef DEBUG
15750Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to resume */
15760Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting an unsupported platform.\n");
15770Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware.\n");
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate #else /* DEBUG */
15800Sstevel@tonic-gate 		halt(0);
15810Sstevel@tonic-gate #endif /* DEBUG */
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	/*
15850Sstevel@tonic-gate 	 * If we are running firmware that isn't 64-bit ready
15860Sstevel@tonic-gate 	 * then complain and halt.
15870Sstevel@tonic-gate 	 */
15880Sstevel@tonic-gate 	do_prom_version_check();
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	/*
15910Sstevel@tonic-gate 	 * Initialize system parameters
15920Sstevel@tonic-gate 	 */
15930Sstevel@tonic-gate 	param_init();
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	/*
15960Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
15970Sstevel@tonic-gate 	 */
15980Sstevel@tonic-gate 	maxmem = physmem;
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	/* Set segkp limits. */
16010Sstevel@tonic-gate 	ncbase = (caddr_t)SEGDEBUGBASE;
16020Sstevel@tonic-gate 	ncend = (caddr_t)SEGDEBUGBASE;
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 	/*
16050Sstevel@tonic-gate 	 * Initialize the hat layer.
16060Sstevel@tonic-gate 	 */
16070Sstevel@tonic-gate 	hat_init();
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	/*
16100Sstevel@tonic-gate 	 * Initialize segment management stuff.
16110Sstevel@tonic-gate 	 */
16120Sstevel@tonic-gate 	seg_init();
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	/*
16150Sstevel@tonic-gate 	 * Create the va>tte handler, so the prom can understand
16160Sstevel@tonic-gate 	 * kernel translations.  The handler is installed later, just
16170Sstevel@tonic-gate 	 * as we are about to take over the trap table from the prom.
16180Sstevel@tonic-gate 	 */
16190Sstevel@tonic-gate 	create_va_to_tte();
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	/*
16220Sstevel@tonic-gate 	 * Load the forthdebugger (optional)
16230Sstevel@tonic-gate 	 */
16240Sstevel@tonic-gate 	forthdebug_init();
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	/*
16270Sstevel@tonic-gate 	 * Create OBP node for console input callbacks
16280Sstevel@tonic-gate 	 * if it is needed.
16290Sstevel@tonic-gate 	 */
16301253Slq150181 	startup_create_io_node();
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	if (modloadonly("fs", "specfs") == -1)
16330Sstevel@tonic-gate 		halt("Can't load specfs");
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	if (modloadonly("fs", "devfs") == -1)
16360Sstevel@tonic-gate 		halt("Can't load devfs");
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	if (modloadonly("misc", "swapgeneric") == -1)
16390Sstevel@tonic-gate 		halt("Can't load swapgeneric");
16400Sstevel@tonic-gate 
1641*1676Sjpk 	(void) modloadonly("sys", "lbl_edition");
1642*1676Sjpk 
16430Sstevel@tonic-gate 	dispinit();
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	/*
16460Sstevel@tonic-gate 	 * Infer meanings to the members of the idprom buffer.
16470Sstevel@tonic-gate 	 */
16480Sstevel@tonic-gate 	parse_idprom();
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	/* Read cluster configuration data. */
16510Sstevel@tonic-gate 	clconf_init();
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	setup_ddi();
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	/*
16560Sstevel@tonic-gate 	 * Lets take this opportunity to load the root device.
16570Sstevel@tonic-gate 	 */
16580Sstevel@tonic-gate 	if (loadrootmodules() != 0)
16590Sstevel@tonic-gate 		debug_enter("Can't load the root filesystem");
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	/*
16620Sstevel@tonic-gate 	 * Load tod driver module for the tod part found on this system.
16630Sstevel@tonic-gate 	 * Recompute the cpu frequency/delays based on tod as tod part
16640Sstevel@tonic-gate 	 * tends to keep time more accurately.
16650Sstevel@tonic-gate 	 */
16660Sstevel@tonic-gate 	if (&load_tod_module)
16670Sstevel@tonic-gate 		load_tod_module();
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	/*
16700Sstevel@tonic-gate 	 * Allow platforms to load modules which might
16710Sstevel@tonic-gate 	 * be needed after bootops are gone.
16720Sstevel@tonic-gate 	 */
16730Sstevel@tonic-gate 	if (&load_platform_modules)
16740Sstevel@tonic-gate 		load_platform_modules();
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	setcpudelay();
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
16790Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
16800Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	bop_alloc_pages = size_virtalloc(boot_virtavail, boot_virtavail_len);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	/*
16850Sstevel@tonic-gate 	 * Calculation and allocation of hmeblks needed to remap
16860Sstevel@tonic-gate 	 * the memory allocated by PROM till now:
16870Sstevel@tonic-gate 	 *
16880Sstevel@tonic-gate 	 * (1)  calculate how much virtual memory has been bop_alloc'ed.
16890Sstevel@tonic-gate 	 * (2)  roundup this memory to span of hme8blk, i.e. 64KB
16900Sstevel@tonic-gate 	 * (3)  calculate number of hme8blk's needed to remap this memory
16910Sstevel@tonic-gate 	 * (4)  calculate amount of memory that's consumed by these hme8blk's
16920Sstevel@tonic-gate 	 * (5)  add memory calculated in steps (2) and (4) above.
16930Sstevel@tonic-gate 	 * (6)  roundup this memory to span of hme8blk, i.e. 64KB
16940Sstevel@tonic-gate 	 * (7)  calculate number of hme8blk's needed to remap this memory
16950Sstevel@tonic-gate 	 * (8)  calculate amount of memory that's consumed by these hme8blk's
16960Sstevel@tonic-gate 	 * (9)  allocate additional hme1blk's to hold large mappings.
16970Sstevel@tonic-gate 	 *	H8TOH1 determines this.  The current SWAG gives enough hblk1's
16980Sstevel@tonic-gate 	 *	to remap everything with 4M mappings.
16990Sstevel@tonic-gate 	 * (10) account for partially used hblk8's due to non-64K aligned
17000Sstevel@tonic-gate 	 *	PROM mapping entries.
17010Sstevel@tonic-gate 	 * (11) add memory calculated in steps (8), (9), and (10) above.
17020Sstevel@tonic-gate 	 * (12) kmem_zalloc the memory calculated in (11); since segkmem
17030Sstevel@tonic-gate 	 *	is not ready yet, this gets bop_alloc'ed.
17040Sstevel@tonic-gate 	 * (13) there will be very few bop_alloc's after this point before
17050Sstevel@tonic-gate 	 *	trap table takes over
17060Sstevel@tonic-gate 	 */
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	/* sfmmu_init_nucleus_hblks expects properly aligned data structures. */
17090Sstevel@tonic-gate 	hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t));
17100Sstevel@tonic-gate 	hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t));
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	pages_per_hblk = btop(HMEBLK_SPAN(TTE8K));
17130Sstevel@tonic-gate 	bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
17140Sstevel@tonic-gate 	nhblk8 = bop_alloc_pages / pages_per_hblk;
17150Sstevel@tonic-gate 	nhblk1 = roundup(nhblk8, H8TOH1) / H8TOH1;
17160Sstevel@tonic-gate 	hblk_pages = btopr(nhblk8 * hme8blk_sz + nhblk1 * hme1blk_sz);
17170Sstevel@tonic-gate 	bop_alloc_pages += hblk_pages;
17180Sstevel@tonic-gate 	bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
17190Sstevel@tonic-gate 	nhblk8 = bop_alloc_pages / pages_per_hblk;
17200Sstevel@tonic-gate 	nhblk1 = roundup(nhblk8, H8TOH1) / H8TOH1;
17210Sstevel@tonic-gate 	if (nhblk1 < hblk1_min)
17220Sstevel@tonic-gate 		nhblk1 = hblk1_min;
17230Sstevel@tonic-gate 	if (nhblk8 < hblk8_min)
17240Sstevel@tonic-gate 		nhblk8 = hblk8_min;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	/*
17270Sstevel@tonic-gate 	 * Since hblk8's can hold up to 64k of mappings aligned on a 64k
17280Sstevel@tonic-gate 	 * boundary, the number of hblk8's needed to map the entries in the
17290Sstevel@tonic-gate 	 * boot_virtavail list needs to be adjusted to take this into
17300Sstevel@tonic-gate 	 * consideration.  Thus, we need to add additional hblk8's since it
17310Sstevel@tonic-gate 	 * is possible that an hblk8 will not have all 8 slots used due to
17320Sstevel@tonic-gate 	 * alignment constraints.  Since there were boot_virtavail_len entries
17330Sstevel@tonic-gate 	 * in that list, we need to add that many hblk8's to the number
17340Sstevel@tonic-gate 	 * already calculated to make sure we don't underestimate.
17350Sstevel@tonic-gate 	 */
17360Sstevel@tonic-gate 	nhblk8 += boot_virtavail_len;
17370Sstevel@tonic-gate 	nhblksz = nhblk8 * hme8blk_sz + nhblk1 * hme1blk_sz;
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 	/* Allocate in pagesize chunks */
17400Sstevel@tonic-gate 	nhblksz = roundup(nhblksz, MMU_PAGESIZE);
17410Sstevel@tonic-gate 	hblk_base = kmem_zalloc(nhblksz, KM_SLEEP);
17420Sstevel@tonic-gate 	sfmmu_init_nucleus_hblks(hblk_base, nhblksz, nhblk8, nhblk1);
17430Sstevel@tonic-gate }
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate static void
17460Sstevel@tonic-gate startup_bop_gone(void)
17470Sstevel@tonic-gate {
17480Sstevel@tonic-gate 	extern int bop_io_quiesced;
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	/*
17510Sstevel@tonic-gate 	 * Call back into boot and release boots resources.
17520Sstevel@tonic-gate 	 */
17530Sstevel@tonic-gate 	BOP_QUIESCE_IO(bootops);
17540Sstevel@tonic-gate 	bop_io_quiesced = 1;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
17570Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
17580Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
17590Sstevel@tonic-gate 	/*
17600Sstevel@tonic-gate 	 * Copy physinstalled list into kernel space.
17610Sstevel@tonic-gate 	 */
17620Sstevel@tonic-gate 	phys_install = memlist;
17630Sstevel@tonic-gate 	copy_memlist(boot_physinstalled, boot_physinstalled_len, &memlist);
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	/*
17660Sstevel@tonic-gate 	 * setup physically contiguous area twice as large as the ecache.
17670Sstevel@tonic-gate 	 * this is used while doing displacement flush of ecaches
17680Sstevel@tonic-gate 	 */
17690Sstevel@tonic-gate 	if (&ecache_flush_address) {
17700Sstevel@tonic-gate 		ecache_flushaddr = ecache_flush_address();
17710Sstevel@tonic-gate 		if (ecache_flushaddr == (uint64_t)-1) {
17720Sstevel@tonic-gate 			cmn_err(CE_PANIC,
17730Sstevel@tonic-gate 			    "startup: no memory to set ecache_flushaddr");
17740Sstevel@tonic-gate 		}
17750Sstevel@tonic-gate 	}
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	/*
17780Sstevel@tonic-gate 	 * Virtual available next.
17790Sstevel@tonic-gate 	 */
17800Sstevel@tonic-gate 	ASSERT(virt_avail != NULL);
17810Sstevel@tonic-gate 	memlist_free_list(virt_avail);
17820Sstevel@tonic-gate 	virt_avail = memlist;
17830Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	/*
17860Sstevel@tonic-gate 	 * Last chance to ask our booter questions ..
17870Sstevel@tonic-gate 	 */
17880Sstevel@tonic-gate }
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate /*
17920Sstevel@tonic-gate  * startup_fixup_physavail - called from mach_sfmmu.c after the final
17930Sstevel@tonic-gate  * allocations have been performed.  We can't call it in startup_bop_gone
17940Sstevel@tonic-gate  * since later operations can cause obp to allocate more memory.
17950Sstevel@tonic-gate  */
17960Sstevel@tonic-gate void
17970Sstevel@tonic-gate startup_fixup_physavail(void)
17980Sstevel@tonic-gate {
17990Sstevel@tonic-gate 	struct memlist *cur;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	/*
18020Sstevel@tonic-gate 	 * take the most current snapshot we can by calling mem-update
18030Sstevel@tonic-gate 	 */
18040Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
18050Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
18060Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	/*
18090Sstevel@tonic-gate 	 * Copy phys_avail list, again.
18100Sstevel@tonic-gate 	 * Both the kernel/boot and the prom have been allocating
18110Sstevel@tonic-gate 	 * from the original list we copied earlier.
18120Sstevel@tonic-gate 	 */
18130Sstevel@tonic-gate 	cur = memlist;
18140Sstevel@tonic-gate 	(void) copy_physavail(boot_physavail, boot_physavail_len,
18150Sstevel@tonic-gate 	    &memlist, 0, 0);
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	/*
18180Sstevel@tonic-gate 	 * Add any extra memory after e_text we added to the phys_avail list
18190Sstevel@tonic-gate 	 * back to the old list.
18200Sstevel@tonic-gate 	 */
18210Sstevel@tonic-gate 	if (extra_etpg)
18220Sstevel@tonic-gate 		memlist_add(va_to_pa(extra_etva), mmu_ptob(extra_etpg),
18230Sstevel@tonic-gate 		    &memlist, &cur);
18240Sstevel@tonic-gate 	if (ndata_remain_sz >= MMU_PAGESIZE)
18250Sstevel@tonic-gate 		memlist_add(va_to_pa(nalloc_base),
18260Sstevel@tonic-gate 		    (uint64_t)ndata_remain_sz, &memlist, &cur);
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	/*
18290Sstevel@tonic-gate 	 * There isn't any bounds checking on the memlist area
18300Sstevel@tonic-gate 	 * so ensure it hasn't overgrown.
18310Sstevel@tonic-gate 	 */
18320Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
18330Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: memlist size exceeded");
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	/*
18360Sstevel@tonic-gate 	 * The kernel removes the pages that were allocated for it from
18370Sstevel@tonic-gate 	 * the freelist, but we now have to find any -extra- pages that
18380Sstevel@tonic-gate 	 * the prom has allocated for it's own book-keeping, and remove
18390Sstevel@tonic-gate 	 * them from the freelist too. sigh.
18400Sstevel@tonic-gate 	 */
18410Sstevel@tonic-gate 	fix_prom_pages(phys_avail, cur);
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	ASSERT(phys_avail != NULL);
18440Sstevel@tonic-gate 	memlist_free_list(phys_avail);
18450Sstevel@tonic-gate 	phys_avail = cur;
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	/*
18480Sstevel@tonic-gate 	 * We're done with boot.  Just after this point in time, boot
18490Sstevel@tonic-gate 	 * gets unmapped, so we can no longer rely on its services.
18500Sstevel@tonic-gate 	 * Zero the bootops to indicate this fact.
18510Sstevel@tonic-gate 	 */
18520Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
18530Sstevel@tonic-gate 	BOOTOPS_GONE();
18540Sstevel@tonic-gate }
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate static void
18570Sstevel@tonic-gate startup_vm(void)
18580Sstevel@tonic-gate {
18590Sstevel@tonic-gate 	size_t	i;
18600Sstevel@tonic-gate 	struct segmap_crargs a;
18610Sstevel@tonic-gate 	struct segkpm_crargs b;
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	uint64_t avmem;
18640Sstevel@tonic-gate 	caddr_t va;
18650Sstevel@tonic-gate 	pgcnt_t	max_phys_segkp;
18660Sstevel@tonic-gate 	int	mnode;
18670Sstevel@tonic-gate 
1868423Sdavemq 	extern int exec_lpg_disable, use_brk_lpg, use_stk_lpg, use_zmap_lpg;
1869423Sdavemq 
18700Sstevel@tonic-gate 	/*
18710Sstevel@tonic-gate 	 * get prom's mappings, create hments for them and switch
18720Sstevel@tonic-gate 	 * to the kernel context.
18730Sstevel@tonic-gate 	 */
18740Sstevel@tonic-gate 	hat_kern_setup();
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	/*
18770Sstevel@tonic-gate 	 * Take over trap table
18780Sstevel@tonic-gate 	 */
18790Sstevel@tonic-gate 	setup_trap_table();
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	/*
18820Sstevel@tonic-gate 	 * Install the va>tte handler, so that the prom can handle
18830Sstevel@tonic-gate 	 * misses and understand the kernel table layout in case
18840Sstevel@tonic-gate 	 * we need call into the prom.
18850Sstevel@tonic-gate 	 */
18860Sstevel@tonic-gate 	install_va_to_tte();
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	/*
18890Sstevel@tonic-gate 	 * Set a flag to indicate that the tba has been taken over.
18900Sstevel@tonic-gate 	 */
18910Sstevel@tonic-gate 	tba_taken_over = 1;
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 	/* initialize MMU primary context register */
18940Sstevel@tonic-gate 	mmu_init_kcontext();
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	/*
18970Sstevel@tonic-gate 	 * The boot cpu can now take interrupts, x-calls, x-traps
18980Sstevel@tonic-gate 	 */
18990Sstevel@tonic-gate 	CPUSET_ADD(cpu_ready_set, CPU->cpu_id);
19000Sstevel@tonic-gate 	CPU->cpu_flags |= (CPU_READY | CPU_ENABLE | CPU_EXISTS);
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	/*
19030Sstevel@tonic-gate 	 * Set a flag to tell write_scb_int() that it can access V_TBR_WR_ADDR.
19040Sstevel@tonic-gate 	 */
19050Sstevel@tonic-gate 	tbr_wr_addr_inited = 1;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 	/*
19080Sstevel@tonic-gate 	 * Initialize VM system, and map kernel address space.
19090Sstevel@tonic-gate 	 */
19100Sstevel@tonic-gate 	kvm_init();
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	/*
19130Sstevel@tonic-gate 	 * XXX4U: previously, we initialized and turned on
19140Sstevel@tonic-gate 	 * the caches at this point. But of course we have
19150Sstevel@tonic-gate 	 * nothing to do, as the prom has already done this
19160Sstevel@tonic-gate 	 * for us -- main memory must be E$able at all times.
19170Sstevel@tonic-gate 	 */
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	/*
19200Sstevel@tonic-gate 	 * If the following is true, someone has patched
19210Sstevel@tonic-gate 	 * phsymem to be less than the number of pages that
19220Sstevel@tonic-gate 	 * the system actually has.  Remove pages until system
19230Sstevel@tonic-gate 	 * memory is limited to the requested amount.  Since we
19240Sstevel@tonic-gate 	 * have allocated page structures for all pages, we
19250Sstevel@tonic-gate 	 * correct the amount of memory we want to remove
19260Sstevel@tonic-gate 	 * by the size of the memory used to hold page structures
19270Sstevel@tonic-gate 	 * for the non-used pages.
19280Sstevel@tonic-gate 	 */
19290Sstevel@tonic-gate 	if (physmem < npages) {
19300Sstevel@tonic-gate 		pgcnt_t diff, off;
19310Sstevel@tonic-gate 		struct page *pp;
19320Sstevel@tonic-gate 		struct seg kseg;
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 		cmn_err(CE_WARN, "limiting physmem to %ld pages", physmem);
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 		off = 0;
19370Sstevel@tonic-gate 		diff = npages - physmem;
19380Sstevel@tonic-gate 		diff -= mmu_btopr(diff * sizeof (struct page));
19390Sstevel@tonic-gate 		kseg.s_as = &kas;
19400Sstevel@tonic-gate 		while (diff--) {
19410Sstevel@tonic-gate 			pp = page_create_va(&unused_pages_vp, (offset_t)off,
19420Sstevel@tonic-gate 			    MMU_PAGESIZE, PG_WAIT | PG_EXCL,
19430Sstevel@tonic-gate 			    &kseg, (caddr_t)off);
19440Sstevel@tonic-gate 			if (pp == NULL)
19450Sstevel@tonic-gate 				cmn_err(CE_PANIC, "limited physmem too much!");
19460Sstevel@tonic-gate 			page_io_unlock(pp);
19470Sstevel@tonic-gate 			page_downgrade(pp);
19480Sstevel@tonic-gate 			availrmem--;
19490Sstevel@tonic-gate 			off += MMU_PAGESIZE;
19500Sstevel@tonic-gate 		}
19510Sstevel@tonic-gate 	}
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	/*
19540Sstevel@tonic-gate 	 * When printing memory, show the total as physmem less
19550Sstevel@tonic-gate 	 * that stolen by a debugger.
19560Sstevel@tonic-gate 	 */
19570Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %ldK (0x%lx000)\n",
19580Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 10),
19590Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 12));
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	avmem = (uint64_t)freemem << PAGESHIFT;
19620Sstevel@tonic-gate 	cmn_err(CE_CONT, "?avail mem = %lld\n", (unsigned long long)avmem);
19630Sstevel@tonic-gate 
1964423Sdavemq 	/* For small memory systems disable automatic large pages. */
1965423Sdavemq 	if (physmem < auto_lpg_min_physmem) {
1966423Sdavemq 		exec_lpg_disable = 1;
1967423Sdavemq 		use_brk_lpg = 0;
1968423Sdavemq 		use_stk_lpg = 0;
1969423Sdavemq 		use_zmap_lpg = 0;
1970423Sdavemq 	}
1971423Sdavemq 
19720Sstevel@tonic-gate 	/*
19730Sstevel@tonic-gate 	 * Perform platform specific freelist processing
19740Sstevel@tonic-gate 	 */
19750Sstevel@tonic-gate 	if (&plat_freelist_process) {
19760Sstevel@tonic-gate 		for (mnode = 0; mnode < max_mem_nodes; mnode++)
19770Sstevel@tonic-gate 			if (mem_node_config[mnode].exists)
19780Sstevel@tonic-gate 				plat_freelist_process(mnode);
19790Sstevel@tonic-gate 	}
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	/*
19820Sstevel@tonic-gate 	 * Initialize the segkp segment type.  We position it
19830Sstevel@tonic-gate 	 * after the configured tables and buffers (whose end
19840Sstevel@tonic-gate 	 * is given by econtig) and before V_WKBASE_ADDR.
19850Sstevel@tonic-gate 	 * Also in this area is segkmap (size SEGMAPSIZE).
19860Sstevel@tonic-gate 	 */
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	/* XXX - cache alignment? */
19890Sstevel@tonic-gate 	va = (caddr_t)SEGKPBASE;
19900Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & PAGEOFFSET) == 0);
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	max_phys_segkp = (physmem * 2);
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	if (segkpsize < btop(SEGKPMINSIZE) || segkpsize > btop(SEGKPMAXSIZE)) {
19950Sstevel@tonic-gate 		segkpsize = btop(SEGKPDEFSIZE);
19960Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal value for segkpsize. "
19970Sstevel@tonic-gate 		    "segkpsize has been reset to %ld pages", segkpsize);
19980Sstevel@tonic-gate 	}
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 	i = ptob(MIN(segkpsize, max_phys_segkp));
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
20030Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkp) < 0)
20040Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: cannot attach segkp");
20050Sstevel@tonic-gate 	if (segkp_create(segkp) != 0)
20060Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: segkp_create failed");
20070Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	/*
20100Sstevel@tonic-gate 	 * kpm segment
20110Sstevel@tonic-gate 	 */
20120Sstevel@tonic-gate 	segmap_kpm = kpm_enable &&
20130Sstevel@tonic-gate 		segmap_kpm && PAGESIZE == MAXBSIZE;
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	if (kpm_enable) {
20160Sstevel@tonic-gate 		rw_enter(&kas.a_lock, RW_WRITER);
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 		/*
20190Sstevel@tonic-gate 		 * The segkpm virtual range range is larger than the
20200Sstevel@tonic-gate 		 * actual physical memory size and also covers gaps in
20210Sstevel@tonic-gate 		 * the physical address range for the following reasons:
20220Sstevel@tonic-gate 		 * . keep conversion between segkpm and physical addresses
20230Sstevel@tonic-gate 		 *   simple, cheap and unambiguous.
20240Sstevel@tonic-gate 		 * . avoid extension/shrink of the the segkpm in case of DR.
20250Sstevel@tonic-gate 		 * . avoid complexity for handling of virtual addressed
20260Sstevel@tonic-gate 		 *   caches, segkpm and the regular mapping scheme must be
20270Sstevel@tonic-gate 		 *   kept in sync wrt. the virtual color of mapped pages.
20280Sstevel@tonic-gate 		 * Any accesses to virtual segkpm ranges not backed by
20290Sstevel@tonic-gate 		 * physical memory will fall through the memseg pfn hash
20300Sstevel@tonic-gate 		 * and will be handled in segkpm_fault.
20310Sstevel@tonic-gate 		 * Additional kpm_size spaces needed for vac alias prevention.
20320Sstevel@tonic-gate 		 */
20330Sstevel@tonic-gate 		if (seg_attach(&kas, kpm_vbase, kpm_size * vac_colors,
20340Sstevel@tonic-gate 		    segkpm) < 0)
20350Sstevel@tonic-gate 			cmn_err(CE_PANIC, "cannot attach segkpm");
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 		b.prot = PROT_READ | PROT_WRITE;
20380Sstevel@tonic-gate 		b.nvcolors = shm_alignment >> MMU_PAGESHIFT;
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 		if (segkpm_create(segkpm, (caddr_t)&b) != 0)
20410Sstevel@tonic-gate 			panic("segkpm_create segkpm");
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 		rw_exit(&kas.a_lock);
20440Sstevel@tonic-gate 	}
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	/*
20470Sstevel@tonic-gate 	 * Now create generic mapping segment.  This mapping
20480Sstevel@tonic-gate 	 * goes SEGMAPSIZE beyond SEGMAPBASE.  But if the total
20490Sstevel@tonic-gate 	 * virtual address is greater than the amount of free
20500Sstevel@tonic-gate 	 * memory that is available, then we trim back the
20510Sstevel@tonic-gate 	 * segment size to that amount
20520Sstevel@tonic-gate 	 */
20530Sstevel@tonic-gate 	va = (caddr_t)SEGMAPBASE;
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	/*
20560Sstevel@tonic-gate 	 * 1201049: segkmap base address must be MAXBSIZE aligned
20570Sstevel@tonic-gate 	 */
20580Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & MAXBOFFSET) == 0);
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 	/*
20610Sstevel@tonic-gate 	 * Set size of segmap to percentage of freemem at boot,
20620Sstevel@tonic-gate 	 * but stay within the allowable range
20630Sstevel@tonic-gate 	 * Note we take percentage  before converting from pages
20640Sstevel@tonic-gate 	 * to bytes to avoid an overflow on 32-bit kernels.
20650Sstevel@tonic-gate 	 */
20660Sstevel@tonic-gate 	i = mmu_ptob((freemem * segmap_percent) / 100);
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	if (i < MINMAPSIZE)
20690Sstevel@tonic-gate 		i = MINMAPSIZE;
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	if (i > MIN(SEGMAPSIZE, mmu_ptob(freemem)))
20720Sstevel@tonic-gate 		i = MIN(SEGMAPSIZE, mmu_ptob(freemem));
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	i &= MAXBMASK;	/* 1201049: segkmap size must be MAXBSIZE aligned */
20750Sstevel@tonic-gate 
20760Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
20770Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkmap) < 0)
20780Sstevel@tonic-gate 		cmn_err(CE_PANIC, "cannot attach segkmap");
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
20810Sstevel@tonic-gate 	a.shmsize = shm_alignment;
20820Sstevel@tonic-gate 	a.nfreelist = 0;	/* use segmap driver defaults */
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	if (segmap_create(segkmap, (caddr_t)&a) != 0)
20850Sstevel@tonic-gate 		panic("segmap_create segkmap");
20860Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	segdev_init();
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate static void
20920Sstevel@tonic-gate startup_end(void)
20930Sstevel@tonic-gate {
20940Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
20950Sstevel@tonic-gate 		panic("memlist overflow 2");
20960Sstevel@tonic-gate 	memlist_free_block((caddr_t)memlist,
20970Sstevel@tonic-gate 	    ((caddr_t)memlist_end - (caddr_t)memlist));
20980Sstevel@tonic-gate 	memlist = NULL;
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	/* enable page_relocation since OBP is now done */
21010Sstevel@tonic-gate 	page_relocate_ready = 1;
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	/*
21040Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
21050Sstevel@tonic-gate 	 * initialization has been done but before the clock
21060Sstevel@tonic-gate 	 * and other devices get started.
21070Sstevel@tonic-gate 	 */
21080Sstevel@tonic-gate 	kern_setup1();
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	/*
21110Sstevel@tonic-gate 	 * Intialize the VM arenas for allocating physically
21120Sstevel@tonic-gate 	 * contiguus memory chunk for interrupt queues snd
21130Sstevel@tonic-gate 	 * allocate/register boot cpu's queues, if any and
21140Sstevel@tonic-gate 	 * allocate dump buffer for sun4v systems to store
21150Sstevel@tonic-gate 	 * extra crash information during crash dump
21160Sstevel@tonic-gate 	 */
21170Sstevel@tonic-gate 	contig_mem_init();
21180Sstevel@tonic-gate 	mach_descrip_init();
21190Sstevel@tonic-gate 	cpu_intrq_setup(CPU);
21200Sstevel@tonic-gate 	cpu_intrq_register(CPU);
21211077Ssvemuri 	mach_htraptrace_init();
21221077Ssvemuri 	mach_htraptrace_setup(CPU->cpu_id);
21231077Ssvemuri 	mach_htraptrace_configure(CPU->cpu_id);
21240Sstevel@tonic-gate 	mach_dump_buffer_init();
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	/*
21270Sstevel@tonic-gate 	 * Initialize interrupt related stuff
21280Sstevel@tonic-gate 	 */
21291455Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 	(void) splzs();			/* allow hi clock ints but not zs */
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	/*
21340Sstevel@tonic-gate 	 * Initialize errors.
21350Sstevel@tonic-gate 	 */
21360Sstevel@tonic-gate 	error_init();
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	/*
21390Sstevel@tonic-gate 	 * Note that we may have already used kernel bcopy before this
21400Sstevel@tonic-gate 	 * point - but if you really care about this, adb the use_hw_*
21410Sstevel@tonic-gate 	 * variables to 0 before rebooting.
21420Sstevel@tonic-gate 	 */
21430Sstevel@tonic-gate 	mach_hw_copy_limit();
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 	/*
21460Sstevel@tonic-gate 	 * Install the "real" preemption guards before DDI services
21470Sstevel@tonic-gate 	 * are available.
21480Sstevel@tonic-gate 	 */
21490Sstevel@tonic-gate 	(void) prom_set_preprom(kern_preprom);
21500Sstevel@tonic-gate 	(void) prom_set_postprom(kern_postprom);
21510Sstevel@tonic-gate 	CPU->cpu_m.mutex_ready = 1;
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	/*
21540Sstevel@tonic-gate 	 * Initialize segnf (kernel support for non-faulting loads).
21550Sstevel@tonic-gate 	 */
21560Sstevel@tonic-gate 	segnf_init();
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	/*
21590Sstevel@tonic-gate 	 * Configure the root devinfo node.
21600Sstevel@tonic-gate 	 */
21610Sstevel@tonic-gate 	configure();		/* set up devices */
21620Sstevel@tonic-gate 	mach_cpu_halt_idle();
21630Sstevel@tonic-gate }
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate void
21670Sstevel@tonic-gate post_startup(void)
21680Sstevel@tonic-gate {
21690Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
21700Sstevel@tonic-gate 	extern void init_ptl1_thread(void);
21710Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
21720Sstevel@tonic-gate 	extern void abort_sequence_init(void);
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	/*
21750Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
21760Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
21770Sstevel@tonic-gate 	 * instruction set extensions.
21780Sstevel@tonic-gate 	 */
21790Sstevel@tonic-gate 	bind_hwcap();
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	/*
21820Sstevel@tonic-gate 	 * Startup memory scrubber (if any)
21830Sstevel@tonic-gate 	 */
21840Sstevel@tonic-gate 	mach_memscrub();
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 	/*
21870Sstevel@tonic-gate 	 * Allocate soft interrupt to handle abort sequence.
21880Sstevel@tonic-gate 	 */
21890Sstevel@tonic-gate 	abort_sequence_init();
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	/*
21920Sstevel@tonic-gate 	 * Configure the rest of the system.
21930Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
21940Sstevel@tonic-gate 	 */
21950Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
21960Sstevel@tonic-gate 	/*
21970Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
21980Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
21990Sstevel@tonic-gate 	 */
22000Sstevel@tonic-gate 	(void) modload("fs", "procfs");
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	if (&load_platform_drivers)
22030Sstevel@tonic-gate 		load_platform_drivers();
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	/* load vis simulation module, if we are running w/fpu off */
22060Sstevel@tonic-gate 	if (!fpu_exists) {
22070Sstevel@tonic-gate 		if (modload("misc", "vis") == -1)
22080Sstevel@tonic-gate 			halt("Can't load vis");
22090Sstevel@tonic-gate 	}
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 	mach_fpras();
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 	maxmem = freemem;
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
22160Sstevel@tonic-gate 	init_ptl1_thread();
22170Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
22180Sstevel@tonic-gate }
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
22210Sstevel@tonic-gate int		ptl1_panic_test = 0;
22220Sstevel@tonic-gate int		ptl1_panic_xc_one_test = 0;
22230Sstevel@tonic-gate int		ptl1_panic_xc_all_test = 0;
22240Sstevel@tonic-gate int		ptl1_panic_xt_one_test = 0;
22250Sstevel@tonic-gate int		ptl1_panic_xt_all_test = 0;
22260Sstevel@tonic-gate kthread_id_t	ptl1_thread_p = NULL;
22270Sstevel@tonic-gate kcondvar_t	ptl1_cv;
22280Sstevel@tonic-gate kmutex_t	ptl1_mutex;
22290Sstevel@tonic-gate int		ptl1_recurse_count_threshold = 0x40;
22300Sstevel@tonic-gate int		ptl1_recurse_trap_threshold = 0x3d;
22310Sstevel@tonic-gate extern void	ptl1_recurse(int, int);
22320Sstevel@tonic-gate extern void	ptl1_panic_xt(int, int);
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate /*
22350Sstevel@tonic-gate  * Called once per second by timeout() to wake up
22360Sstevel@tonic-gate  * the ptl1_panic thread to see if it should cause
22370Sstevel@tonic-gate  * a trap to the ptl1_panic() code.
22380Sstevel@tonic-gate  */
22390Sstevel@tonic-gate /* ARGSUSED */
22400Sstevel@tonic-gate static void
22410Sstevel@tonic-gate ptl1_wakeup(void *arg)
22420Sstevel@tonic-gate {
22430Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
22440Sstevel@tonic-gate 	cv_signal(&ptl1_cv);
22450Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
22460Sstevel@tonic-gate }
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate /*
22490Sstevel@tonic-gate  * ptl1_panic cross call function:
22500Sstevel@tonic-gate  *     Needed because xc_one() and xc_some() can pass
22510Sstevel@tonic-gate  *	64 bit args but ptl1_recurse() expects ints.
22520Sstevel@tonic-gate  */
22530Sstevel@tonic-gate static void
22540Sstevel@tonic-gate ptl1_panic_xc(void)
22550Sstevel@tonic-gate {
22560Sstevel@tonic-gate 	ptl1_recurse(ptl1_recurse_count_threshold,
22570Sstevel@tonic-gate 	    ptl1_recurse_trap_threshold);
22580Sstevel@tonic-gate }
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate /*
22610Sstevel@tonic-gate  * The ptl1 thread waits for a global flag to be set
22620Sstevel@tonic-gate  * and uses the recurse thresholds to set the stack depth
22630Sstevel@tonic-gate  * to cause a ptl1_panic() directly via a call to ptl1_recurse
22640Sstevel@tonic-gate  * or indirectly via the cross call and cross trap functions.
22650Sstevel@tonic-gate  *
22660Sstevel@tonic-gate  * This is useful testing stack overflows and normal
22670Sstevel@tonic-gate  * ptl1_panic() states with a know stack frame.
22680Sstevel@tonic-gate  *
22690Sstevel@tonic-gate  * ptl1_recurse() is an asm function in ptl1_panic.s that
22700Sstevel@tonic-gate  * sets the {In, Local, Out, and Global} registers to a
22710Sstevel@tonic-gate  * know state on the stack and just prior to causing a
22720Sstevel@tonic-gate  * test ptl1_panic trap.
22730Sstevel@tonic-gate  */
22740Sstevel@tonic-gate static void
22750Sstevel@tonic-gate ptl1_thread(void)
22760Sstevel@tonic-gate {
22770Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
22780Sstevel@tonic-gate 	while (ptl1_thread_p) {
22790Sstevel@tonic-gate 		cpuset_t	other_cpus;
22800Sstevel@tonic-gate 		int		cpu_id;
22810Sstevel@tonic-gate 		int		my_cpu_id;
22820Sstevel@tonic-gate 		int		target_cpu_id;
22830Sstevel@tonic-gate 		int		target_found;
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 		if (ptl1_panic_test) {
22860Sstevel@tonic-gate 			ptl1_recurse(ptl1_recurse_count_threshold,
22870Sstevel@tonic-gate 			    ptl1_recurse_trap_threshold);
22880Sstevel@tonic-gate 		}
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 		/*
22910Sstevel@tonic-gate 		 * Find potential targets for x-call and x-trap,
22920Sstevel@tonic-gate 		 * if any exist while preempt is disabled we
22930Sstevel@tonic-gate 		 * start a ptl1_panic if requested via a
22940Sstevel@tonic-gate 		 * globals.
22950Sstevel@tonic-gate 		 */
22960Sstevel@tonic-gate 		kpreempt_disable();
22970Sstevel@tonic-gate 		my_cpu_id = CPU->cpu_id;
22980Sstevel@tonic-gate 		other_cpus = cpu_ready_set;
22990Sstevel@tonic-gate 		CPUSET_DEL(other_cpus, CPU->cpu_id);
23000Sstevel@tonic-gate 		target_found = 0;
23010Sstevel@tonic-gate 		if (!CPUSET_ISNULL(other_cpus)) {
23020Sstevel@tonic-gate 			/*
23030Sstevel@tonic-gate 			 * Pick the first one
23040Sstevel@tonic-gate 			 */
23050Sstevel@tonic-gate 			for (cpu_id = 0; cpu_id < NCPU; cpu_id++) {
23060Sstevel@tonic-gate 				if (cpu_id == my_cpu_id)
23070Sstevel@tonic-gate 					continue;
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate 				if (CPU_XCALL_READY(cpu_id)) {
23100Sstevel@tonic-gate 					target_cpu_id = cpu_id;
23110Sstevel@tonic-gate 					target_found = 1;
23120Sstevel@tonic-gate 					break;
23130Sstevel@tonic-gate 				}
23140Sstevel@tonic-gate 			}
23150Sstevel@tonic-gate 			ASSERT(target_found);
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 			if (ptl1_panic_xc_one_test) {
23180Sstevel@tonic-gate 				xc_one(target_cpu_id,
23190Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
23200Sstevel@tonic-gate 			}
23210Sstevel@tonic-gate 			if (ptl1_panic_xc_all_test) {
23220Sstevel@tonic-gate 				xc_some(other_cpus,
23230Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
23240Sstevel@tonic-gate 			}
23250Sstevel@tonic-gate 			if (ptl1_panic_xt_one_test) {
23260Sstevel@tonic-gate 				xt_one(target_cpu_id,
23270Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
23280Sstevel@tonic-gate 			}
23290Sstevel@tonic-gate 			if (ptl1_panic_xt_all_test) {
23300Sstevel@tonic-gate 				xt_some(other_cpus,
23310Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
23320Sstevel@tonic-gate 			}
23330Sstevel@tonic-gate 		}
23340Sstevel@tonic-gate 		kpreempt_enable();
23350Sstevel@tonic-gate 		(void) timeout(ptl1_wakeup, NULL, hz);
23360Sstevel@tonic-gate 		(void) cv_wait(&ptl1_cv, &ptl1_mutex);
23370Sstevel@tonic-gate 	}
23380Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
23390Sstevel@tonic-gate }
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate /*
23420Sstevel@tonic-gate  * Called during early startup to create the ptl1_thread
23430Sstevel@tonic-gate  */
23440Sstevel@tonic-gate void
23450Sstevel@tonic-gate init_ptl1_thread(void)
23460Sstevel@tonic-gate {
23470Sstevel@tonic-gate 	ptl1_thread_p = thread_create(NULL, 0, ptl1_thread, NULL, 0,
23480Sstevel@tonic-gate 	    &p0, TS_RUN, 0);
23490Sstevel@tonic-gate }
23500Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate /*
23540Sstevel@tonic-gate  * Add to a memory list.
23550Sstevel@tonic-gate  * start = start of new memory segment
23560Sstevel@tonic-gate  * len = length of new memory segment in bytes
23570Sstevel@tonic-gate  * memlistp = pointer to array of available memory segment structures
23580Sstevel@tonic-gate  * curmemlistp = memory list to which to add segment.
23590Sstevel@tonic-gate  */
23600Sstevel@tonic-gate static void
23610Sstevel@tonic-gate memlist_add(uint64_t start, uint64_t len, struct memlist **memlistp,
23620Sstevel@tonic-gate 	struct memlist **curmemlistp)
23630Sstevel@tonic-gate {
23640Sstevel@tonic-gate 	struct memlist *new;
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 	new = *memlistp;
23670Sstevel@tonic-gate 	new->address = start;
23680Sstevel@tonic-gate 	new->size = len;
23690Sstevel@tonic-gate 	*memlistp = new + 1;
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	memlist_insert(new, curmemlistp);
23720Sstevel@tonic-gate }
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate /*
23750Sstevel@tonic-gate  * In the case of architectures that support dynamic addition of
23760Sstevel@tonic-gate  * memory at run-time there are two cases where memsegs need to
23770Sstevel@tonic-gate  * be initialized and added to the memseg list.
23780Sstevel@tonic-gate  * 1) memsegs that are constructed at startup.
23790Sstevel@tonic-gate  * 2) memsegs that are constructed at run-time on
23800Sstevel@tonic-gate  *    hot-plug capable architectures.
23810Sstevel@tonic-gate  * This code was originally part of the function kphysm_init().
23820Sstevel@tonic-gate  */
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate static void
23850Sstevel@tonic-gate memseg_list_add(struct memseg *memsegp)
23860Sstevel@tonic-gate {
23870Sstevel@tonic-gate 	struct memseg **prev_memsegp;
23880Sstevel@tonic-gate 	pgcnt_t num;
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	/* insert in memseg list, decreasing number of pages order */
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	num = MSEG_NPAGES(memsegp);
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	for (prev_memsegp = &memsegs; *prev_memsegp;
23950Sstevel@tonic-gate 	    prev_memsegp = &((*prev_memsegp)->next)) {
23960Sstevel@tonic-gate 		if (num > MSEG_NPAGES(*prev_memsegp))
23970Sstevel@tonic-gate 			break;
23980Sstevel@tonic-gate 	}
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate 	memsegp->next = *prev_memsegp;
24010Sstevel@tonic-gate 	*prev_memsegp = memsegp;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 	if (kpm_enable) {
24040Sstevel@tonic-gate 		memsegp->nextpa = (memsegp->next) ?
24050Sstevel@tonic-gate 			va_to_pa(memsegp->next) : MSEG_NULLPTR_PA;
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 		if (prev_memsegp != &memsegs) {
24080Sstevel@tonic-gate 			struct memseg *msp;
24090Sstevel@tonic-gate 			msp = (struct memseg *)((caddr_t)prev_memsegp -
24100Sstevel@tonic-gate 				offsetof(struct memseg, next));
24110Sstevel@tonic-gate 			msp->nextpa = va_to_pa(memsegp);
24120Sstevel@tonic-gate 		} else {
24130Sstevel@tonic-gate 			memsegspa = va_to_pa(memsegs);
24140Sstevel@tonic-gate 		}
24150Sstevel@tonic-gate 	}
24160Sstevel@tonic-gate }
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate /*
24190Sstevel@tonic-gate  * PSM add_physmem_cb(). US-II and newer processors have some
24200Sstevel@tonic-gate  * flavor of the prefetch capability implemented. We exploit
24210Sstevel@tonic-gate  * this capability for optimum performance.
24220Sstevel@tonic-gate  */
24230Sstevel@tonic-gate #define	PREFETCH_BYTES	64
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate void
24260Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
24270Sstevel@tonic-gate {
24280Sstevel@tonic-gate 	extern void	 prefetch_page_w(void *);
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	pp->p_pagenum = pnum;
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	/*
24330Sstevel@tonic-gate 	 * Prefetch one more page_t into E$. To prevent future
24340Sstevel@tonic-gate 	 * mishaps with the sizeof(page_t) changing on us, we
24350Sstevel@tonic-gate 	 * catch this on debug kernels if we can't bring in the
24360Sstevel@tonic-gate 	 * entire hpage with 2 PREFETCH_BYTES reads. See
24370Sstevel@tonic-gate 	 * also, sun4u/cpu/cpu_module.c
24380Sstevel@tonic-gate 	 */
24390Sstevel@tonic-gate 	/*LINTED*/
24400Sstevel@tonic-gate 	ASSERT(sizeof (page_t) <= 2*PREFETCH_BYTES);
24410Sstevel@tonic-gate 	prefetch_page_w((char *)pp);
24420Sstevel@tonic-gate }
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate /*
24450Sstevel@tonic-gate  * kphysm_init() tackles the problem of initializing physical memory.
24460Sstevel@tonic-gate  * The old startup made some assumptions about the kernel living in
24470Sstevel@tonic-gate  * physically contiguous space which is no longer valid.
24480Sstevel@tonic-gate  */
24490Sstevel@tonic-gate static void
24500Sstevel@tonic-gate kphysm_init(page_t *pp, struct memseg *memsegp, pgcnt_t npages,
24510Sstevel@tonic-gate 	uintptr_t kpm_pp, pgcnt_t kpm_npages)
24520Sstevel@tonic-gate {
24530Sstevel@tonic-gate 	struct memlist	*pmem;
24540Sstevel@tonic-gate 	struct memseg	*msp;
24550Sstevel@tonic-gate 	pfn_t		 base;
24560Sstevel@tonic-gate 	pgcnt_t		 num;
24570Sstevel@tonic-gate 	pfn_t		 lastseg_pages_end = 0;
24580Sstevel@tonic-gate 	pgcnt_t		 nelem_used = 0;
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	msp = memsegp;
24630Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 		/*
24660Sstevel@tonic-gate 		 * Build the memsegs entry
24670Sstevel@tonic-gate 		 */
24680Sstevel@tonic-gate 		num = btop(pmem->size);
24690Sstevel@tonic-gate 		if (num > npages)
24700Sstevel@tonic-gate 			num = npages;
24710Sstevel@tonic-gate 		npages -= num;
24720Sstevel@tonic-gate 		base = btop(pmem->address);
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 		msp->pages = pp;
24750Sstevel@tonic-gate 		msp->epages = pp + num;
24760Sstevel@tonic-gate 		msp->pages_base = base;
24770Sstevel@tonic-gate 		msp->pages_end = base + num;
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 		if (kpm_enable) {
24800Sstevel@tonic-gate 			pfn_t pbase_a;
24810Sstevel@tonic-gate 			pfn_t pend_a;
24820Sstevel@tonic-gate 			pfn_t prev_pend_a;
24830Sstevel@tonic-gate 			pgcnt_t	nelem;
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 			msp->pagespa = va_to_pa(pp);
24860Sstevel@tonic-gate 			msp->epagespa = va_to_pa(pp + num);
24870Sstevel@tonic-gate 			pbase_a = kpmptop(ptokpmp(base));
24880Sstevel@tonic-gate 			pend_a = kpmptop(ptokpmp(base + num - 1)) + kpmpnpgs;
24890Sstevel@tonic-gate 			nelem = ptokpmp(pend_a - pbase_a);
24900Sstevel@tonic-gate 			msp->kpm_nkpmpgs = nelem;
24910Sstevel@tonic-gate 			msp->kpm_pbase = pbase_a;
24920Sstevel@tonic-gate 			if (lastseg_pages_end) {
24930Sstevel@tonic-gate 				/*
24940Sstevel@tonic-gate 				 * Assume phys_avail is in ascending order
24950Sstevel@tonic-gate 				 * of physical addresses.
24960Sstevel@tonic-gate 				 */
24970Sstevel@tonic-gate 				ASSERT(base + num > lastseg_pages_end);
24980Sstevel@tonic-gate 				prev_pend_a = kpmptop(
24990Sstevel@tonic-gate 				    ptokpmp(lastseg_pages_end - 1)) + kpmpnpgs;
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 				if (prev_pend_a > pbase_a) {
25020Sstevel@tonic-gate 					/*
25030Sstevel@tonic-gate 					 * Overlap, more than one memseg may
25040Sstevel@tonic-gate 					 * point to the same kpm_page range.
25050Sstevel@tonic-gate 					 */
25060Sstevel@tonic-gate 					if (kpm_smallpages == 0) {
25070Sstevel@tonic-gate 						msp->kpm_pages =
25080Sstevel@tonic-gate 						    (kpm_page_t *)kpm_pp - 1;
25090Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
25100Sstevel@tonic-gate 							((kpm_page_t *)kpm_pp
25110Sstevel@tonic-gate 							+ nelem - 1);
25120Sstevel@tonic-gate 					} else {
25130Sstevel@tonic-gate 						msp->kpm_spages =
25140Sstevel@tonic-gate 						    (kpm_spage_t *)kpm_pp - 1;
25150Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
25160Sstevel@tonic-gate 							((kpm_spage_t *)kpm_pp
25170Sstevel@tonic-gate 							+ nelem - 1);
25180Sstevel@tonic-gate 					}
25190Sstevel@tonic-gate 					nelem_used += nelem - 1;
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 				} else {
25220Sstevel@tonic-gate 					if (kpm_smallpages == 0) {
25230Sstevel@tonic-gate 						msp->kpm_pages =
25240Sstevel@tonic-gate 						    (kpm_page_t *)kpm_pp;
25250Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
25260Sstevel@tonic-gate 							((kpm_page_t *)kpm_pp
25270Sstevel@tonic-gate 							+ nelem);
25280Sstevel@tonic-gate 					} else {
25290Sstevel@tonic-gate 						msp->kpm_spages =
25300Sstevel@tonic-gate 						    (kpm_spage_t *)kpm_pp;
25310Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
25320Sstevel@tonic-gate 							((kpm_spage_t *)
25330Sstevel@tonic-gate 							kpm_pp + nelem);
25340Sstevel@tonic-gate 					}
25350Sstevel@tonic-gate 					nelem_used += nelem;
25360Sstevel@tonic-gate 				}
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 			} else {
25390Sstevel@tonic-gate 				if (kpm_smallpages == 0) {
25400Sstevel@tonic-gate 					msp->kpm_pages = (kpm_page_t *)kpm_pp;
25410Sstevel@tonic-gate 					kpm_pp = (uintptr_t)
25420Sstevel@tonic-gate 						((kpm_page_t *)kpm_pp + nelem);
25430Sstevel@tonic-gate 				} else {
25440Sstevel@tonic-gate 					msp->kpm_spages = (kpm_spage_t *)kpm_pp;
25450Sstevel@tonic-gate 					kpm_pp = (uintptr_t)
25460Sstevel@tonic-gate 						((kpm_spage_t *)kpm_pp + nelem);
25470Sstevel@tonic-gate 				}
25480Sstevel@tonic-gate 				nelem_used = nelem;
25490Sstevel@tonic-gate 			}
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 			if (nelem_used > kpm_npages)
25520Sstevel@tonic-gate 				panic("kphysm_init: kpm_pp overflow\n");
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 			msp->kpm_pagespa = va_to_pa(msp->kpm_pages);
25550Sstevel@tonic-gate 			lastseg_pages_end = msp->pages_end;
25560Sstevel@tonic-gate 		}
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate 		memseg_list_add(msp);
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 		/*
25610Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
25620Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
25630Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
25640Sstevel@tonic-gate 		 * it initializes them.
25650Sstevel@tonic-gate 		 */
25660Sstevel@tonic-gate 		add_physmem(pp, num, base);
25670Sstevel@tonic-gate 		pp += num;
25680Sstevel@tonic-gate 		msp++;
25690Sstevel@tonic-gate 	}
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 	build_pfn_hash();
25720Sstevel@tonic-gate }
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate /*
25750Sstevel@tonic-gate  * Kernel VM initialization.
25760Sstevel@tonic-gate  * Assumptions about kernel address space ordering:
25770Sstevel@tonic-gate  *	(1) gap (user space)
25780Sstevel@tonic-gate  *	(2) kernel text
25790Sstevel@tonic-gate  *	(3) kernel data/bss
25800Sstevel@tonic-gate  *	(4) gap
25810Sstevel@tonic-gate  *	(5) kernel data structures
25820Sstevel@tonic-gate  *	(6) gap
25830Sstevel@tonic-gate  *	(7) debugger (optional)
25840Sstevel@tonic-gate  *	(8) monitor
25850Sstevel@tonic-gate  *	(9) gap (possibly null)
25860Sstevel@tonic-gate  *	(10) dvma
25870Sstevel@tonic-gate  *	(11) devices
25880Sstevel@tonic-gate  */
25890Sstevel@tonic-gate static void
25900Sstevel@tonic-gate kvm_init(void)
25910Sstevel@tonic-gate {
25920Sstevel@tonic-gate 	/*
25930Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
25940Sstevel@tonic-gate 	 */
25950Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
25960Sstevel@tonic-gate 	as_avlinit(&kas);
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)KERNELBASE,
25990Sstevel@tonic-gate 	    (size_t)(e_moddata - KERNELBASE), &ktextseg);
26000Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)(KERNELBASE + MMU_PAGESIZE4M),
26030Sstevel@tonic-gate 	    (size_t)(MMU_PAGESIZE4M), &ktexthole);
26040Sstevel@tonic-gate 	(void) segkmem_create(&ktexthole);
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base,
26070Sstevel@tonic-gate 	    (size_t)(econtig32 - valloc_base), &kvalloc);
26080Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 	if (kmem64_base) {
26110Sstevel@tonic-gate 	    (void) seg_attach(&kas, (caddr_t)kmem64_base,
26120Sstevel@tonic-gate 		(size_t)(kmem64_end - kmem64_base), &kmem64);
26130Sstevel@tonic-gate 	    (void) segkmem_create(&kmem64);
26140Sstevel@tonic-gate 	}
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	/*
26170Sstevel@tonic-gate 	 * We're about to map out /boot.  This is the beginning of the
26180Sstevel@tonic-gate 	 * system resource management transition. We can no longer
26190Sstevel@tonic-gate 	 * call into /boot for I/O or memory allocations.
26200Sstevel@tonic-gate 	 */
26210Sstevel@tonic-gate 	(void) seg_attach(&kas, kernelheap, ekernelheap - kernelheap, &kvseg);
26220Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
26230Sstevel@tonic-gate 	hblk_alloc_dynamic = 1;
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 	/*
26260Sstevel@tonic-gate 	 * we need to preallocate pages for DR operations before enabling large
26270Sstevel@tonic-gate 	 * page kernel heap because of memseg_remap_init() hat_unload() hack.
26280Sstevel@tonic-gate 	 */
26290Sstevel@tonic-gate 	memseg_remap_init();
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	/* at this point we are ready to use large page heap */
26320Sstevel@tonic-gate 	segkmem_heap_lp_init();
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)SYSBASE32, SYSLIMIT32 - SYSBASE32,
26350Sstevel@tonic-gate 	    &kvseg32);
26360Sstevel@tonic-gate 	(void) segkmem_create(&kvseg32);
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	/*
26390Sstevel@tonic-gate 	 * Create a segment for the debugger.
26400Sstevel@tonic-gate 	 */
26410Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)SEGDEBUGBASE, (size_t)SEGDEBUGSIZE,
26420Sstevel@tonic-gate 	    &kdebugseg);
26430Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
26460Sstevel@tonic-gate }
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate char obp_tte_str[] =
26490Sstevel@tonic-gate 	"h# %x constant MMU_PAGESHIFT "
26500Sstevel@tonic-gate 	"h# %x constant TTE8K "
26510Sstevel@tonic-gate 	"h# %x constant SFHME_SIZE "
26520Sstevel@tonic-gate 	"h# %x constant SFHME_TTE "
26530Sstevel@tonic-gate 	"h# %x constant HMEBLK_TAG "
26540Sstevel@tonic-gate 	"h# %x constant HMEBLK_NEXT "
26550Sstevel@tonic-gate 	"h# %x constant HMEBLK_MISC "
26560Sstevel@tonic-gate 	"h# %x constant HMEBLK_HME1 "
26570Sstevel@tonic-gate 	"h# %x constant NHMENTS "
26580Sstevel@tonic-gate 	"h# %x constant HBLK_SZMASK "
26590Sstevel@tonic-gate 	"h# %x constant HBLK_RANGE_SHIFT "
26600Sstevel@tonic-gate 	"h# %x constant HMEBP_HBLK "
26610Sstevel@tonic-gate 	"h# %x constant HMEBUCKET_SIZE "
26620Sstevel@tonic-gate 	"h# %x constant HTAG_SFMMUPSZ "
26630Sstevel@tonic-gate 	"h# %x constant HTAG_REHASHSZ "
26640Sstevel@tonic-gate 	"h# %x constant mmu_hashcnt "
26650Sstevel@tonic-gate 	"h# %p constant uhme_hash "
26660Sstevel@tonic-gate 	"h# %p constant khme_hash "
26670Sstevel@tonic-gate 	"h# %x constant UHMEHASH_SZ "
26680Sstevel@tonic-gate 	"h# %x constant KHMEHASH_SZ "
26690Sstevel@tonic-gate 	"h# %p constant KHATID "
26700Sstevel@tonic-gate 	"h# %x constant CTX_SIZE "
26710Sstevel@tonic-gate 	"h# %x constant CTX_SFMMU "
26720Sstevel@tonic-gate 	"h# %p constant ctxs "
26730Sstevel@tonic-gate 	"h# %x constant ASI_MEM "
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	": PHYS-X@ ( phys -- data ) "
26760Sstevel@tonic-gate 	"   ASI_MEM spacex@ "
26770Sstevel@tonic-gate 	"; "
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate 	": PHYS-W@ ( phys -- data ) "
26800Sstevel@tonic-gate 	"   ASI_MEM spacew@ "
26810Sstevel@tonic-gate 	"; "
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate 	": PHYS-L@ ( phys -- data ) "
26840Sstevel@tonic-gate 	"   ASI_MEM spaceL@ "
26850Sstevel@tonic-gate 	"; "
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	": TTE_PAGE_SHIFT ( ttesz -- hmeshift ) "
26880Sstevel@tonic-gate 	"   3 * MMU_PAGESHIFT + "
26890Sstevel@tonic-gate 	"; "
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	": TTE_IS_VALID ( ttep -- flag ) "
26920Sstevel@tonic-gate 	"   PHYS-X@ 0< "
26930Sstevel@tonic-gate 	"; "
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 	": HME_HASH_SHIFT ( ttesz -- hmeshift ) "
26960Sstevel@tonic-gate 	"   dup TTE8K =  if "
26970Sstevel@tonic-gate 	"      drop HBLK_RANGE_SHIFT "
26980Sstevel@tonic-gate 	"   else "
26990Sstevel@tonic-gate 	"      TTE_PAGE_SHIFT "
27000Sstevel@tonic-gate 	"   then "
27010Sstevel@tonic-gate 	"; "
27020Sstevel@tonic-gate 
27030Sstevel@tonic-gate 	": HME_HASH_BSPAGE ( addr hmeshift -- bspage ) "
27040Sstevel@tonic-gate 	"   tuck >> swap MMU_PAGESHIFT - << "
27050Sstevel@tonic-gate 	"; "
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	": HME_HASH_FUNCTION ( sfmmup addr hmeshift -- hmebp ) "
27080Sstevel@tonic-gate 	"   >> over xor swap                    ( hash sfmmup ) "
27090Sstevel@tonic-gate 	"   KHATID <>  if                       ( hash ) "
27100Sstevel@tonic-gate 	"      UHMEHASH_SZ and                  ( bucket ) "
27110Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * uhme_hash +     ( hmebp ) "
27120Sstevel@tonic-gate 	"   else                                ( hash ) "
27130Sstevel@tonic-gate 	"      KHMEHASH_SZ and                  ( bucket ) "
27140Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * khme_hash +     ( hmebp ) "
27150Sstevel@tonic-gate 	"   then                                ( hmebp ) "
27160Sstevel@tonic-gate 	"; "
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	": HME_HASH_TABLE_SEARCH "
27190Sstevel@tonic-gate 	"       ( sfmmup hmebp hblktag --  sfmmup null | sfmmup hmeblkp ) "
27200Sstevel@tonic-gate 	"   >r hmebp_hblk + phys-x@ begin ( sfmmup hmeblkp ) ( r: hblktag ) "
27210Sstevel@tonic-gate 	"      dup if   		( sfmmup hmeblkp ) ( r: hblktag ) "
27220Sstevel@tonic-gate 	"         dup hmeblk_tag + phys-x@ r@ = if ( sfmmup hmeblkp )	  "
27230Sstevel@tonic-gate 	"	     dup hmeblk_tag + 8 + phys-x@ 2 pick = if		  "
27240Sstevel@tonic-gate 	"		  true 	( sfmmup hmeblkp true ) ( r: hblktag )	  "
27250Sstevel@tonic-gate 	"	     else						  "
27260Sstevel@tonic-gate 	"	     	  hmeblk_next + phys-x@ false 			  "
27270Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
27280Sstevel@tonic-gate 	"	     then  						  "
27290Sstevel@tonic-gate 	"	  else							  "
27300Sstevel@tonic-gate 	"	     hmeblk_next + phys-x@ false 			  "
27310Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
27320Sstevel@tonic-gate 	"	  then 							  "
27330Sstevel@tonic-gate 	"      else							  "
27340Sstevel@tonic-gate 	"         true 							  "
27350Sstevel@tonic-gate 	"      then  							  "
27360Sstevel@tonic-gate 	"   until r> drop 						  "
27370Sstevel@tonic-gate 	"; "
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate 	": CNUM_TO_SFMMUP ( cnum -- sfmmup ) "
27400Sstevel@tonic-gate 	"   CTX_SIZE * ctxs + CTX_SFMMU + "
27410Sstevel@tonic-gate 	"x@ "
27420Sstevel@tonic-gate 	"; "
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate 	": HME_HASH_TAG ( sfmmup rehash addr -- hblktag ) "
27450Sstevel@tonic-gate 	"   over HME_HASH_SHIFT HME_HASH_BSPAGE      ( sfmmup rehash bspage ) "
27460Sstevel@tonic-gate 	"   HTAG_REHASHSZ << or nip		     ( hblktag ) "
27470Sstevel@tonic-gate 	"; "
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	": HBLK_TO_TTEP ( hmeblkp addr -- ttep ) "
27500Sstevel@tonic-gate 	"   over HMEBLK_MISC + PHYS-L@ HBLK_SZMASK and  ( hmeblkp addr ttesz ) "
27510Sstevel@tonic-gate 	"   TTE8K =  if                            ( hmeblkp addr ) "
27520Sstevel@tonic-gate 	"      MMU_PAGESHIFT >> NHMENTS 1- and     ( hmeblkp hme-index ) "
27530Sstevel@tonic-gate 	"   else                                   ( hmeblkp addr ) "
27540Sstevel@tonic-gate 	"      drop 0                              ( hmeblkp 0 ) "
27550Sstevel@tonic-gate 	"   then                                   ( hmeblkp hme-index ) "
27560Sstevel@tonic-gate 	"   SFHME_SIZE * + HMEBLK_HME1 +           ( hmep ) "
27570Sstevel@tonic-gate 	"   SFHME_TTE +                            ( ttep ) "
27580Sstevel@tonic-gate 	"; "
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	": unix-tte ( addr cnum -- false | tte-data true ) "
27610Sstevel@tonic-gate 	"      CNUM_TO_SFMMUP                 ( addr sfmmup ) "
27620Sstevel@tonic-gate 	"      mmu_hashcnt 1+ 1  do           ( addr sfmmup ) "
27630Sstevel@tonic-gate 	"         2dup swap i HME_HASH_SHIFT  "
27640Sstevel@tonic-gate 					"( addr sfmmup sfmmup addr hmeshift ) "
27650Sstevel@tonic-gate 	"         HME_HASH_FUNCTION           ( addr sfmmup hmebp ) "
27660Sstevel@tonic-gate 	"         over i 4 pick               "
27670Sstevel@tonic-gate 				"( addr sfmmup hmebp sfmmup rehash addr ) "
27680Sstevel@tonic-gate 	"         HME_HASH_TAG                ( addr sfmmup hmebp hblktag ) "
27690Sstevel@tonic-gate 	"         HME_HASH_TABLE_SEARCH       "
27700Sstevel@tonic-gate 					"( addr sfmmup { null | hmeblkp } ) "
27710Sstevel@tonic-gate 	"         ?dup  if                    ( addr sfmmup hmeblkp ) "
27720Sstevel@tonic-gate 	"            nip swap HBLK_TO_TTEP    ( ttep ) "
27730Sstevel@tonic-gate 	"            dup TTE_IS_VALID  if     ( valid-ttep ) "
27740Sstevel@tonic-gate 	"               PHYS-X@ true          ( tte-data true ) "
27750Sstevel@tonic-gate 	"            else                     ( invalid-tte ) "
27760Sstevel@tonic-gate 	"               drop false            ( false ) "
27770Sstevel@tonic-gate 	"            then                     ( false | tte-data true ) "
27780Sstevel@tonic-gate 	"            unloop exit              ( false | tte-data true ) "
27790Sstevel@tonic-gate 	"         then                        ( addr sfmmup ) "
27800Sstevel@tonic-gate 	"      loop                           ( addr sfmmup ) "
27810Sstevel@tonic-gate 	"      2drop false                    ( false ) "
27820Sstevel@tonic-gate 	"; "
27830Sstevel@tonic-gate ;
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate void
27860Sstevel@tonic-gate create_va_to_tte(void)
27870Sstevel@tonic-gate {
27880Sstevel@tonic-gate 	char *bp;
27890Sstevel@tonic-gate 	extern int khmehash_num, uhmehash_num;
27900Sstevel@tonic-gate 	extern struct hmehash_bucket *khme_hash, *uhme_hash;
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate #define	OFFSET(type, field)	((uintptr_t)(&((type *)0)->field))
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 	bp = (char *)kobj_zalloc(MMU_PAGESIZE, KM_SLEEP);
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate 	/*
27970Sstevel@tonic-gate 	 * Teach obp how to parse our sw ttes.
27980Sstevel@tonic-gate 	 */
27990Sstevel@tonic-gate 	(void) sprintf(bp, obp_tte_str,
28000Sstevel@tonic-gate 	    MMU_PAGESHIFT,
28010Sstevel@tonic-gate 	    TTE8K,
28020Sstevel@tonic-gate 	    sizeof (struct sf_hment),
28030Sstevel@tonic-gate 	    OFFSET(struct sf_hment, hme_tte),
28040Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_tag),
28050Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_nextpa),
28060Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_misc),
28070Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_hme),
28080Sstevel@tonic-gate 	    NHMENTS,
28090Sstevel@tonic-gate 	    HBLK_SZMASK,
28100Sstevel@tonic-gate 	    HBLK_RANGE_SHIFT,
28110Sstevel@tonic-gate 	    OFFSET(struct hmehash_bucket, hmeh_nextpa),
28120Sstevel@tonic-gate 	    sizeof (struct hmehash_bucket),
28130Sstevel@tonic-gate 	    HTAG_SFMMUPSZ,
28140Sstevel@tonic-gate 	    HTAG_REHASHSZ,
28150Sstevel@tonic-gate 	    mmu_hashcnt,
28160Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)uhme_hash),
28170Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)khme_hash),
28180Sstevel@tonic-gate 	    UHMEHASH_SZ,
28190Sstevel@tonic-gate 	    KHMEHASH_SZ,
28200Sstevel@tonic-gate 	    KHATID,
28210Sstevel@tonic-gate 	    sizeof (struct ctx),
28220Sstevel@tonic-gate 	    OFFSET(struct ctx, ctx_sfmmu),
28230Sstevel@tonic-gate 	    ctxs,
28240Sstevel@tonic-gate 	    ASI_MEM);
28250Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 	kobj_free(bp, MMU_PAGESIZE);
28280Sstevel@tonic-gate }
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate void
28310Sstevel@tonic-gate install_va_to_tte(void)
28320Sstevel@tonic-gate {
28330Sstevel@tonic-gate 	/*
28340Sstevel@tonic-gate 	 * advise prom that he can use unix-tte
28350Sstevel@tonic-gate 	 */
28360Sstevel@tonic-gate 	prom_interpret("' unix-tte is va>tte-data", 0, 0, 0, 0, 0);
28370Sstevel@tonic-gate }
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 
28401253Slq150181 /*
28411253Slq150181  * Because kmdb links prom_stdout_is_framebuffer into its own
28421253Slq150181  * module, we add "device-type=display" here for /os-io node, so that
28431253Slq150181  * prom_stdout_is_framebuffer still works corrrectly  after /os-io node
28441253Slq150181  * is registered into OBP.
28451253Slq150181  */
28460Sstevel@tonic-gate static char *create_node =
28471492Slq150181 	"\" /\" find-device "
28480Sstevel@tonic-gate 	"new-device "
28490Sstevel@tonic-gate 	"\" os-io\" device-name "
28501253Slq150181 	"\" display\" device-type "
28510Sstevel@tonic-gate 	": cb-r/w  ( adr,len method$ -- #read/#written ) "
28520Sstevel@tonic-gate 	"   2>r swap 2 2r> ['] $callback  catch  if "
28530Sstevel@tonic-gate 	"      2drop 3drop 0 "
28540Sstevel@tonic-gate 	"   then "
28550Sstevel@tonic-gate 	"; "
28560Sstevel@tonic-gate 	": read ( adr,len -- #read ) "
28570Sstevel@tonic-gate 	"       \" read\" ['] cb-r/w catch  if  2drop 2drop -2 exit then "
28580Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
28590Sstevel@tonic-gate 	"       ?dup  if "
28600Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
28610Sstevel@tonic-gate 	"       else "
28620Sstevel@tonic-gate 	"               -2 "
28631492Slq150181 	"       then "
28640Sstevel@tonic-gate 	";    "
28650Sstevel@tonic-gate 	": write ( adr,len -- #written ) "
28660Sstevel@tonic-gate 	"       \" write\" ['] cb-r/w catch  if  2drop 2drop 0 exit  then "
28670Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
28680Sstevel@tonic-gate 	"       ?dup  if "
28690Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
28700Sstevel@tonic-gate 	"        else "
28710Sstevel@tonic-gate 	"               0 "
28720Sstevel@tonic-gate 	"       then "
28730Sstevel@tonic-gate 	"; "
28740Sstevel@tonic-gate 	": poll-tty ( -- ) ; "
28750Sstevel@tonic-gate 	": install-abort  ( -- )  ['] poll-tty d# 10 alarm ; "
28760Sstevel@tonic-gate 	": remove-abort ( -- )  ['] poll-tty 0 alarm ; "
28770Sstevel@tonic-gate 	": cb-give/take ( $method -- ) "
28780Sstevel@tonic-gate 	"       0 -rot ['] $callback catch  ?dup  if "
28790Sstevel@tonic-gate 	"               >r 2drop 2drop r> throw "
28800Sstevel@tonic-gate 	"       else "
28810Sstevel@tonic-gate 	"               0  ?do  drop  loop "
28820Sstevel@tonic-gate 	"       then "
28830Sstevel@tonic-gate 	"; "
28840Sstevel@tonic-gate 	": give ( -- )  \" exit-input\" cb-give/take ; "
28850Sstevel@tonic-gate 	": take ( -- )  \" enter-input\" cb-give/take ; "
28860Sstevel@tonic-gate 	": open ( -- ok? )  true ; "
28870Sstevel@tonic-gate 	": close ( -- ) ; "
28880Sstevel@tonic-gate 	"finish-device "
28890Sstevel@tonic-gate 	"device-end ";
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate /*
28921253Slq150181  * Create the OBP input/output node (FCode serial driver).
28931253Slq150181  * It is needed for both USB console keyboard and for
28941253Slq150181  * the kernel terminal emulator.  It is too early to check for a
28951253Slq150181  * kernel console compatible framebuffer now, so we create this
28961253Slq150181  * so that we're ready if we need to enable kernel terminal emulation.
28970Sstevel@tonic-gate  *
28981253Slq150181  * When the USB software takes over the input device at the time
28991253Slq150181  * consconfig runs, OBP's stdin is redirected to this node.
29001253Slq150181  * Whenever the FORTH user interface is used after this switch,
29011253Slq150181  * the node will call back into the kernel for console input.
29021253Slq150181  * If a serial device such as ttya or a UART with a Type 5 keyboard
29031253Slq150181  * attached is used, OBP takes over the serial device when the system
29041253Slq150181  * goes to the debugger after the system is booted.  This sharing
29051253Slq150181  * of the relatively simple serial device is difficult but possible.
29061253Slq150181  * Sharing the USB host controller is impossible due its complexity.
29071253Slq150181  *
29081253Slq150181  * Similarly to USB keyboard input redirection, after consconfig_dacf
29091253Slq150181  * configures a kernel console framebuffer as the standard output
29101253Slq150181  * device, OBP's stdout is switched to to vector through the
29111253Slq150181  * /os-io node into the kernel terminal emulator.
29120Sstevel@tonic-gate  */
29130Sstevel@tonic-gate static void
29141253Slq150181 startup_create_io_node(void)
29150Sstevel@tonic-gate {
29161253Slq150181 	prom_interpret(create_node, 0, 0, 0, 0, 0);
29170Sstevel@tonic-gate }
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate static void
29210Sstevel@tonic-gate do_prom_version_check(void)
29220Sstevel@tonic-gate {
29230Sstevel@tonic-gate 	int i;
2924789Sahrens 	pnode_t node;
29250Sstevel@tonic-gate 	char buf[64];
29260Sstevel@tonic-gate 	static char drev[] = "Down-rev firmware detected%s\n"
29270Sstevel@tonic-gate 		"\tPlease upgrade to the following minimum version:\n"
29280Sstevel@tonic-gate 		"\t\t%s\n";
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate 	i = prom_version_check(buf, sizeof (buf), &node);
29310Sstevel@tonic-gate 
29320Sstevel@tonic-gate 	if (i == PROM_VER64_OK)
29330Sstevel@tonic-gate 		return;
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate 	if (i == PROM_VER64_UPGRADE) {
29360Sstevel@tonic-gate 		cmn_err(CE_WARN, drev, "", buf);
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate #ifdef	DEBUG
29390Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to continue */
29400Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware\n");
29410Sstevel@tonic-gate 		return;
29420Sstevel@tonic-gate #else
29430Sstevel@tonic-gate 		halt(0);
29440Sstevel@tonic-gate #endif
29450Sstevel@tonic-gate 	}
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	/*
29480Sstevel@tonic-gate 	 * The other possibility is that this is a server running
29490Sstevel@tonic-gate 	 * good firmware, but down-rev firmware was detected on at
29500Sstevel@tonic-gate 	 * least one other cpu board. We just complain if we see
29510Sstevel@tonic-gate 	 * that.
29520Sstevel@tonic-gate 	 */
29530Sstevel@tonic-gate 	cmn_err(CE_WARN, drev, " on one or more CPU boards", buf);
29540Sstevel@tonic-gate }
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate static void
29570Sstevel@tonic-gate kpm_init()
29580Sstevel@tonic-gate {
29590Sstevel@tonic-gate 	kpm_pgshft = (kpm_smallpages == 0) ? MMU_PAGESHIFT4M : MMU_PAGESHIFT;
29600Sstevel@tonic-gate 	kpm_pgsz = 1ull << kpm_pgshft;
29610Sstevel@tonic-gate 	kpm_pgoff = kpm_pgsz - 1;
29620Sstevel@tonic-gate 	kpmp2pshft = kpm_pgshft - PAGESHIFT;
29630Sstevel@tonic-gate 	kpmpnpgs = 1 << kpmp2pshft;
29640Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
29650Sstevel@tonic-gate }
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate void
29680Sstevel@tonic-gate kpm_npages_setup(int memblocks)
29690Sstevel@tonic-gate {
29700Sstevel@tonic-gate 	/*
29710Sstevel@tonic-gate 	 * npages can be scattered in a maximum of 'memblocks'
29720Sstevel@tonic-gate 	 */
29730Sstevel@tonic-gate 	kpm_npages = ptokpmpr(npages) + memblocks;
29740Sstevel@tonic-gate }
29750Sstevel@tonic-gate 
29760Sstevel@tonic-gate /*
29770Sstevel@tonic-gate  * Must be defined in platform dependent code.
29780Sstevel@tonic-gate  */
29790Sstevel@tonic-gate extern caddr_t modtext;
29800Sstevel@tonic-gate extern size_t modtext_sz;
29810Sstevel@tonic-gate extern caddr_t moddata;
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate #define	HEAPTEXT_ARENA(addr)	\
29840Sstevel@tonic-gate 	((uintptr_t)(addr) < KERNELBASE + 2 * MMU_PAGESIZE4M ? 0 : \
29850Sstevel@tonic-gate 	(((uintptr_t)(addr) - HEAPTEXT_BASE) / \
29860Sstevel@tonic-gate 	(HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) + 1))
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate #define	HEAPTEXT_OVERSIZED(addr)	\
29890Sstevel@tonic-gate 	((uintptr_t)(addr) >= HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE)
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate vmem_t *texthole_source[HEAPTEXT_NARENAS];
29920Sstevel@tonic-gate vmem_t *texthole_arena[HEAPTEXT_NARENAS];
29930Sstevel@tonic-gate kmutex_t texthole_lock;
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate void
29980Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
29990Sstevel@tonic-gate {
30000Sstevel@tonic-gate 	uintptr_t addr, limit;
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate 	addr = HEAPTEXT_BASE;
30030Sstevel@tonic-gate 	limit = addr + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE;
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	/*
30060Sstevel@tonic-gate 	 * Before we initialize the text_arena, we want to punch holes in the
30070Sstevel@tonic-gate 	 * underlying heaptext_arena.  This guarantees that for any text
30080Sstevel@tonic-gate 	 * address we can find a text hole less than HEAPTEXT_MAPPED away.
30090Sstevel@tonic-gate 	 */
30100Sstevel@tonic-gate 	for (; addr + HEAPTEXT_UNMAPPED <= limit;
30110Sstevel@tonic-gate 	    addr += HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) {
30120Sstevel@tonic-gate 		(void) vmem_xalloc(heaptext_arena, HEAPTEXT_UNMAPPED, PAGESIZE,
30130Sstevel@tonic-gate 		    0, 0, (void *)addr, (void *)(addr + HEAPTEXT_UNMAPPED),
30140Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
30150Sstevel@tonic-gate 	}
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 	/*
30180Sstevel@tonic-gate 	 * Allocate one page at the oversize to break up the text region
30190Sstevel@tonic-gate 	 * from the oversized region.
30200Sstevel@tonic-gate 	 */
30210Sstevel@tonic-gate 	(void) vmem_xalloc(heaptext_arena, PAGESIZE, PAGESIZE, 0, 0,
30220Sstevel@tonic-gate 	    (void *)limit, (void *)(limit + PAGESIZE),
30230Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 	*text_arena = vmem_create("module_text", modtext, modtext_sz,
30260Sstevel@tonic-gate 	    sizeof (uintptr_t), segkmem_alloc, segkmem_free,
30270Sstevel@tonic-gate 	    heaptext_arena, 0, VM_SLEEP);
30280Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", moddata, MODDATA, 1,
30290Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
30300Sstevel@tonic-gate }
30310Sstevel@tonic-gate 
30320Sstevel@tonic-gate caddr_t
30330Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
30340Sstevel@tonic-gate {
30350Sstevel@tonic-gate 	caddr_t rval, better;
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 	/*
30380Sstevel@tonic-gate 	 * First, try a sleeping allocation.
30390Sstevel@tonic-gate 	 */
30400Sstevel@tonic-gate 	rval = vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT);
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 	if (size >= HEAPTEXT_MAPPED || !HEAPTEXT_OVERSIZED(rval))
30430Sstevel@tonic-gate 		return (rval);
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	/*
30460Sstevel@tonic-gate 	 * We didn't get the area that we wanted.  We're going to try to do an
30470Sstevel@tonic-gate 	 * allocation with explicit constraints.
30480Sstevel@tonic-gate 	 */
30490Sstevel@tonic-gate 	better = vmem_xalloc(arena, size, sizeof (uintptr_t), 0, 0, NULL,
30500Sstevel@tonic-gate 	    (void *)(HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE),
30510Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT);
30520Sstevel@tonic-gate 
30530Sstevel@tonic-gate 	if (better != NULL) {
30540Sstevel@tonic-gate 		/*
30550Sstevel@tonic-gate 		 * That worked.  Free our first attempt and return.
30560Sstevel@tonic-gate 		 */
30570Sstevel@tonic-gate 		vmem_free(arena, rval, size);
30580Sstevel@tonic-gate 		return (better);
30590Sstevel@tonic-gate 	}
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	/*
30620Sstevel@tonic-gate 	 * That didn't work; we'll have to return our first attempt.
30630Sstevel@tonic-gate 	 */
30640Sstevel@tonic-gate 	return (rval);
30650Sstevel@tonic-gate }
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate caddr_t
30680Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
30690Sstevel@tonic-gate {
30700Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
30710Sstevel@tonic-gate 	char c[30];
30720Sstevel@tonic-gate 	uintptr_t base;
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 	if (HEAPTEXT_OVERSIZED(addr)) {
30750Sstevel@tonic-gate 		/*
30760Sstevel@tonic-gate 		 * If this is an oversized allocation, there is no text hole
30770Sstevel@tonic-gate 		 * available for it; return NULL.
30780Sstevel@tonic-gate 		 */
30790Sstevel@tonic-gate 		return (NULL);
30800Sstevel@tonic-gate 	}
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate 	mutex_enter(&texthole_lock);
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 	if (texthole_arena[arena] == NULL) {
30850Sstevel@tonic-gate 		ASSERT(texthole_source[arena] == NULL);
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 		if (arena == 0) {
30880Sstevel@tonic-gate 			texthole_source[0] = vmem_create("module_text_holesrc",
30890Sstevel@tonic-gate 			    (void *)(KERNELBASE + MMU_PAGESIZE4M),
30900Sstevel@tonic-gate 			    MMU_PAGESIZE4M, PAGESIZE, NULL, NULL, NULL,
30910Sstevel@tonic-gate 			    0, VM_SLEEP);
30920Sstevel@tonic-gate 		} else {
30930Sstevel@tonic-gate 			base = HEAPTEXT_BASE +
30940Sstevel@tonic-gate 			    (arena - 1) * (HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED);
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c),
30970Sstevel@tonic-gate 			    "heaptext_holesrc_%d", arena);
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 			texthole_source[arena] = vmem_create(c, (void *)base,
31000Sstevel@tonic-gate 			    HEAPTEXT_UNMAPPED, PAGESIZE, NULL, NULL, NULL,
31010Sstevel@tonic-gate 			    0, VM_SLEEP);
31020Sstevel@tonic-gate 		}
31030Sstevel@tonic-gate 
31040Sstevel@tonic-gate 		(void) snprintf(c, sizeof (c), "heaptext_hole_%d", arena);
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate 		texthole_arena[arena] = vmem_create(c, NULL, 0,
31070Sstevel@tonic-gate 		    sizeof (uint32_t), segkmem_alloc_permanent, segkmem_free,
31080Sstevel@tonic-gate 		    texthole_source[arena], 0, VM_SLEEP);
31090Sstevel@tonic-gate 	}
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate 	mutex_exit(&texthole_lock);
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
31140Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
31150Sstevel@tonic-gate 	return (vmem_alloc(texthole_arena[arena], size,
31160Sstevel@tonic-gate 	    VM_BESTFIT | VM_NOSLEEP));
31170Sstevel@tonic-gate }
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate void
31200Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
31210Sstevel@tonic-gate {
31220Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
31250Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
31260Sstevel@tonic-gate 	vmem_free(texthole_arena[arena], addr, size);
31270Sstevel@tonic-gate }
3128