#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/signal.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/mman.h>
#include <sys/vm.h>
#include <sys/conf.h>
#include <sys/avintr.h>
#include <sys/autoconf.h>
#include <sys/disp.h>
#include <sys/class.h>
#include <sys/bitmap.h>
#include <sys/privregs.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/kmem.h>
#include <sys/mem.h>
#include <sys/kstat.h>
#include <sys/reboot.h>
#include <sys/cred.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/procfs.h>
#include <sys/vfs.h>
#include <sys/cmn_err.h>
#include <sys/utsname.h>
#include <sys/debug.h>
#include <sys/kdi.h>
#include <sys/dumphdr.h>
#include <sys/bootconf.h>
#include <sys/memlist_plat.h>
#include <sys/varargs.h>
#include <sys/promif.h>
#include <sys/prom_debug.h>
#include <sys/modctl.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/ndi_impldefs.h>
#include <sys/ddidmareq.h>
#include <sys/psw.h>
#include <sys/regset.h>
#include <sys/clock.h>
#include <sys/pte.h>
#include <sys/tss.h>
#include <sys/stack.h>
#include <sys/trap.h>
#include <sys/fp.h>
#include <vm/kboot_mmu.h>
#include <vm/anon.h>
#include <vm/as.h>
#include <vm/page.h>
#include <vm/seg.h>
#include <vm/seg_dev.h>
#include <vm/seg_kmem.h>
#include <vm/seg_kpm.h>
#include <vm/seg_map.h>
#include <vm/seg_vn.h>
#include <vm/seg_kp.h>
#include <sys/memnode.h>
#include <vm/vm_dep.h>
#include <sys/thread.h>
#include <sys/sysconf.h>
#include <sys/vm_machparam.h>
#include <sys/archsystm.h>
#include <sys/machsystm.h>
#include <vm/hat.h>
#include <vm/hat_i86.h>
#include <sys/pmem.h>
#include <sys/smp_impldefs.h>
#include <sys/x86_archext.h>
#include <sys/cpuvar.h>
#include <sys/segments.h>
#include <sys/clconf.h>
#include <sys/kobj.h>
#include <sys/kobj_lex.h>
#include <sys/cpc_impl.h>
#include <sys/cpu_module.h>
#include <sys/smbios.h>
#include <sys/debug_info.h>
#include <sys/bootinfo.h>
#include <sys/ddi_periodic.h>
#include <sys/systeminfo.h>
#include <sys/multiboot.h>
#include <sys/ramdisk.h>
#include <sys/tsc.h>
#include <sys/clock.h>
#ifdef __xpv
#include <sys/hypervisor.h>
#include <sys/xen_mmu.h>
#include <sys/evtchn_impl.h>
#include <sys/gnttab.h>
#include <sys/xpv_panic.h>
#include <xen/sys/xenbus_comms.h>
#include <xen/public/physdev.h>
extern void xen_late_startup(void);
struct xen_evt_data cpu0_evt_data;
#else
#include <sys/memlist_impl.h>
extern void mem_config_init(void);
#endif
extern void progressbar_init(void);
extern void brand_init(void);
extern void pcf_init(void);
extern void pg_init(void);
extern void ssp_init(void);
extern int size_pse_array(pgcnt_t, int);
#if defined(_SOFT_HOSTID)
static int32_t set_soft_hostid(void);
static char hostid_file[] = "/etc/hostid";
#endif
void *gfx_devinfo_list;
#if !defined(__xpv)
extern void immu_startup(void);
#endif
extern caddr_t p0_va;
extern int segkp_fromheap;
static void kvm_init(void);
static void startup_init(void);
static void startup_memlist(void);
static void startup_kmem(void);
static void startup_modules(void);
static void startup_vm(void);
#ifndef __xpv
static void startup_tsc(void);
#endif
static void startup_end(void);
static void layout_kernel_va(void);
static void setx86isalist(void);
#define TERABYTE (1ul << 40)
#define PHYSMEM_MAX64 mmu_btop(64 * TERABYTE)
#define PHYSMEM PHYSMEM_MAX64
#define AMD64_VA_HOLE_END 0xFFFF800000000000ul
pgcnt_t physmem = PHYSMEM;
pgcnt_t obp_pages;
extern char *kobj_file_buf;
extern int kobj_file_bufsize;
caddr_t rm_platter_va = 0;
uint32_t rm_platter_pa;
int auto_lpg_disable = 1;
uintptr_t hole_start, hole_end;
caddr_t kpm_vbase;
size_t kpm_size;
static int kpm_desired;
static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
caddr_t econtig;
struct bootops *bootops = 0;
struct bootops **bootopsp;
struct boot_syscalls *sysp;
char bootblock_fstype[16];
char kern_bootargs[OBP_MAXPATHLEN];
char kern_bootfile[OBP_MAXPATHLEN];
int segzio_fromheap = 0;
int disable_smap = 0;
#define POSS_NEW_FRAGMENTS 12
long page_hashsz;
unsigned int page_hashsz_shift;
struct page *pp_base;
struct page **page_hash;
pad_mutex_t *pse_mutex;
size_t pse_table_size;
int pse_shift;
struct seg ktextseg;
struct seg kvalloc;
struct seg kpseg;
struct seg kmapseg;
struct seg kdebugseg;
struct seg *segkmap = &kmapseg;
static struct seg *segmap = &kmapseg;
struct seg *segkp = &kpseg;
extern struct seg kvseg_core;
struct seg kpmseg;
struct seg *segkpm = &kpmseg;
caddr_t segkp_base;
caddr_t segzio_base;
pgcnt_t segkpsize;
caddr_t segkvmm_base;
pgcnt_t segkvmmsize;
pgcnt_t segziosize;
int ppvm_enable = 0;
page_t *ppvm_base = NULL;
pgcnt_t ppvm_size = 0;
const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
const size_t kdi_segdebugsize = SEGDEBUGSIZE;
struct memseg *memseg_base;
struct vnode unused_pages_vp;
#define FOURGB 0x100000000LL
struct memlist *memlist;
caddr_t s_text;
caddr_t e_text;
caddr_t s_data;
caddr_t e_data;
caddr_t modtext;
caddr_t e_modtext;
caddr_t moddata;
caddr_t e_moddata;
struct memlist *phys_install;
struct memlist *phys_avail;
struct memlist *bios_rsvd;
static pgcnt_t kphysm_init(page_t *, pgcnt_t);
#define IO_PROP_SIZE 64
#define ROUND_UP_PAGE(x) \
((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
#define ROUND_UP_LPAGE(x) \
((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
#define ROUND_UP_4MEG(x) \
((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
#define ROUND_UP_TOPLEVEL(x) \
((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
extern time_t process_rtc_config_file(void);
uintptr_t kernelbase;
uintptr_t postbootkernelbase;
uintptr_t eprom_kernelbase;
size_t segmapsize;
uintptr_t segmap_start;
int segmapfreelists;
pgcnt_t npages;
pgcnt_t orig_npages;
size_t core_size;
uintptr_t core_base;
static page_t *bootpages;
static page_t *rd_pages;
static page_t *lower_pages = NULL;
static int lower_pages_count = 0;
struct system_hardware system_hardware;
static void
print_memlist(char *title, struct memlist *mp)
{
prom_printf("MEMLIST: %s:\n", title);
while (mp != NULL) {
prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
mp->ml_address, mp->ml_size);
mp = mp->ml_next;
}
}
int l2cache_sz = 0x80000;
int l2cache_linesz = 0x40;
int l2cache_assoc = 1;
static size_t textrepl_min_gb = 10;
vmem_t *device_arena;
uintptr_t toxic_addr = (uintptr_t)NULL;
size_t toxic_size = 1024 * 1024 * 1024;
int prom_debug;
#define NUM_ALLOCATIONS 8
int num_allocations = 0;
struct {
void **al_ptr;
size_t al_size;
} allocations[NUM_ALLOCATIONS];
size_t valloc_sz = 0;
uintptr_t valloc_base;
#define ADD_TO_ALLOCATIONS(ptr, size) { \
size = ROUND_UP_PAGE(size); \
if (num_allocations == NUM_ALLOCATIONS) \
panic("too many ADD_TO_ALLOCATIONS()"); \
allocations[num_allocations].al_ptr = (void**)&ptr; \
allocations[num_allocations].al_size = size; \
valloc_sz += size; \
++num_allocations; \
}
static void
perform_allocations(void)
{
caddr_t mem;
int i;
int valloc_align;
PRM_DEBUG(valloc_base);
PRM_DEBUG(valloc_sz);
valloc_align = mmu.level_size[mmu.max_page_level > 0];
mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
if (mem != (caddr_t)valloc_base)
panic("BOP_ALLOC() failed");
bzero(mem, valloc_sz);
for (i = 0; i < num_allocations; ++i) {
*allocations[i].al_ptr = (void *)mem;
mem += allocations[i].al_size;
}
}
static void
startup_smap(void)
{
int i;
uint32_t inst;
uint8_t *instp;
char sym[128];
struct modctl *modp;
extern int _smap_enable_patch_count;
extern int _smap_disable_patch_count;
if (disable_smap != 0)
remove_x86_feature(x86_featureset, X86FSET_SMAP);
if (is_x86_feature(x86_featureset, X86FSET_SMAP) == B_FALSE)
return;
for (i = 0; i < _smap_enable_patch_count; i++) {
int sizep;
VERIFY3U(i, <, _smap_enable_patch_count);
VERIFY(snprintf(sym, sizeof (sym), "_smap_enable_patch_%d", i) <
sizeof (sym));
instp = (uint8_t *)(void *)kobj_getelfsym(sym, NULL, &sizep);
VERIFY(instp != 0);
inst = (instp[3] << 24) | (SMAP_CLAC_INSTR & 0x00ffffff);
hot_patch_kernel_text((caddr_t)instp, inst, 4);
}
for (i = 0; i < _smap_disable_patch_count; i++) {
int sizep;
VERIFY(snprintf(sym, sizeof (sym), "_smap_disable_patch_%d",
i) < sizeof (sym));
instp = (uint8_t *)(void *)kobj_getelfsym(sym, NULL, &sizep);
VERIFY(instp != 0);
inst = (instp[3] << 24) | (SMAP_STAC_INSTR & 0x00ffffff);
hot_patch_kernel_text((caddr_t)instp, inst, 4);
}
modp = mod_hold_by_name("unix");
do_hotinlines(modp->mod_mp);
mod_release_mod(modp);
setcr4(getcr4() | CR4_SMAP);
smap_enable();
}
void
startup(void)
{
#if !defined(__xpv)
extern void startup_pci_bios(void);
#endif
extern cpuset_t cpu_ready_set;
kpm_desired = 1;
kpm_enable = 0;
CPUSET_ONLY(cpu_ready_set, 0);
#if defined(__xpv)
{
extern int segvn_use_regions;
segvn_use_regions = 0;
}
#endif
ssp_init();
progressbar_init();
startup_init();
#if defined(__xpv)
startup_xen_version();
#endif
startup_memlist();
startup_kmem();
startup_vm();
#if !defined(__xpv)
startup_tsc();
startup_pci_bios();
startup_smap();
#endif
#if defined(__xpv)
startup_xen_mca();
#endif
startup_modules();
startup_end();
}
static void
startup_init()
{
PRM_POINT("startup_init() starting...");
cpuid_execpass(CPU, CPUID_PASS_EXTENDED, NULL);
(void) check_boot_version(BOP_GETVERSION(bootops));
if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
++prom_debug;
PRM_POINT("prom_debug found in boot enviroment");
}
get_system_configuration();
if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
printf("\n486 processor (\"%s\") detected.\n",
CPU->cpu_brandstr);
halt("This processor is not supported by this release "
"of Solaris.");
}
PRM_POINT("startup_init() done");
}
static void
avail_filter(uint64_t *addr, uint64_t *size)
{
uintptr_t va;
uintptr_t next_va;
pfn_t pfn;
uint64_t pfn_addr;
uint64_t pfn_eaddr;
uint_t prot;
size_t len;
uint_t change;
if (prom_debug)
prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
*addr, *size);
if (*addr == 0) {
*addr += MMU_PAGESIZE;
*size -= MMU_PAGESIZE;
}
do {
change = 0;
for (va = KERNEL_TEXT;
*size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
va = next_va) {
next_va = va + len;
pfn_addr = pfn_to_pa(pfn);
pfn_eaddr = pfn_addr + len;
if (pfn_addr <= *addr && pfn_eaddr > *addr) {
change = 1;
while (*size > 0 && len > 0) {
*addr += MMU_PAGESIZE;
*size -= MMU_PAGESIZE;
len -= MMU_PAGESIZE;
}
}
}
if (change && prom_debug)
prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
*addr, *size);
} while (change);
for (va = KERNEL_TEXT;
*size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
va = next_va) {
next_va = va + len;
pfn_addr = pfn_to_pa(pfn);
if (pfn_addr >= *addr && pfn_addr < *addr + *size)
*size = pfn_addr - *addr;
}
if (prom_debug)
prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
*addr, *size);
}
static void
kpm_init()
{
struct segkpm_crargs b;
kpm_pgshft = MMU_PAGESHIFT;
kpm_pgsz = MMU_PAGESIZE;
kpm_pgoff = MMU_PAGEOFFSET;
kpmp2pshft = 0;
kpmpnpgs = 1;
ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
PRM_POINT("about to create segkpm");
rw_enter(&kas.a_lock, RW_WRITER);
if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
panic("cannot attach segkpm");
b.prot = PROT_READ | PROT_WRITE;
b.nvcolors = 1;
if (segkpm_create(segkpm, (caddr_t)&b) != 0)
panic("segkpm_create segkpm");
rw_exit(&kas.a_lock);
kpm_enable = 1;
ASSERT(CPU->cpu_hat_info->hci_user_l3ptes == NULL);
CPU->cpu_hat_info->hci_user_l3ptes =
(x86pte_t *)hat_kpm_mapin_pfn(CPU->cpu_hat_info->hci_user_l3pfn);
}
static void
init_debug_info(void)
{
caddr_t mem;
debug_info_t *di;
#ifndef __lint
ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
#endif
mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
MMU_PAGESIZE);
if (mem != (caddr_t)DEBUG_INFO_VA)
panic("BOP_ALLOC() failed");
bzero(mem, MMU_PAGESIZE);
di = (debug_info_t *)mem;
di->di_magic = DEBUG_INFO_MAGIC;
di->di_version = DEBUG_INFO_VERSION;
di->di_modules = (uintptr_t)&modules;
di->di_s_text = (uintptr_t)s_text;
di->di_e_text = (uintptr_t)e_text;
di->di_s_data = (uintptr_t)s_data;
di->di_e_data = (uintptr_t)e_data;
di->di_hat_htable_off = offsetof(hat_t, hat_htable);
di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
}
static void
startup_memlist(void)
{
size_t memlist_sz;
size_t memseg_sz;
size_t pagehash_sz;
size_t pp_sz;
uintptr_t va;
size_t len;
uint_t prot;
pfn_t pfn;
int memblocks;
pfn_t rsvd_high_pfn;
pgcnt_t rsvd_pgcnt;
size_t rsvdmemlist_sz;
int rsvdmemblocks;
caddr_t pagecolor_mem;
size_t pagecolor_memsz;
caddr_t page_ctrs_mem;
size_t page_ctrs_size;
size_t pse_table_alloc_size;
struct memlist *current;
PRM_POINT("startup_memlist() starting...");
len = kbm_nucleus_size;
ASSERT(len > MMU_PAGESIZE);
moddata = (caddr_t)ROUND_UP_PAGE(e_data);
e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
if (e_moddata - moddata > MODDATA)
e_moddata = moddata + MODDATA;
modtext = (caddr_t)ROUND_UP_PAGE(e_text);
e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
if (e_modtext - modtext > MODTEXT)
e_modtext = modtext + MODTEXT;
econtig = e_moddata;
PRM_DEBUG(modtext);
PRM_DEBUG(e_modtext);
PRM_DEBUG(moddata);
PRM_DEBUG(e_moddata);
PRM_DEBUG(econtig);
if (prom_debug)
print_memlist("boot physinstalled",
bootops->boot_mem->physinstalled);
installed_top_size_ex(bootops->boot_mem->physinstalled, &physmax,
&physinstalled, &memblocks);
PRM_DEBUG(physmax);
PRM_DEBUG(physinstalled);
PRM_DEBUG(memblocks);
plat_dr_physmax = 0;
if (prom_debug)
print_memlist("boot reserved mem",
bootops->boot_mem->rsvdmem);
installed_top_size_ex(bootops->boot_mem->rsvdmem, &rsvd_high_pfn,
&rsvd_pgcnt, &rsvdmemblocks);
PRM_DEBUG(rsvd_high_pfn);
PRM_DEBUG(rsvd_pgcnt);
PRM_DEBUG(rsvdmemblocks);
mmu_init();
startup_build_mem_nodes(bootops->boot_mem->physinstalled);
if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
char value[8];
if (len < 8)
(void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
else
(void) strcpy(value, "");
if (strcmp(value, "off") == 0)
mmu.pt_nx = 0;
}
PRM_DEBUG(mmu.pt_nx);
npages = physinstalled - 1;
obp_pages = 0;
va = KERNEL_TEXT;
while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
npages -= len >> MMU_PAGESHIFT;
if (va >= (uintptr_t)e_moddata)
obp_pages += len >> MMU_PAGESHIFT;
va += len;
}
PRM_DEBUG(npages);
PRM_DEBUG(obp_pages);
if (physmem == 0 || physmem > npages) {
physmem = npages;
} else if (physmem < npages) {
orig_npages = npages;
npages = physmem;
}
PRM_DEBUG(physmem);
memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
PRM_DEBUG(memseg_sz);
memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
(memblocks + POSS_NEW_FRAGMENTS));
ADD_TO_ALLOCATIONS(memlist, memlist_sz);
PRM_DEBUG(memlist_sz);
rsvdmemlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
(rsvdmemblocks + POSS_NEW_FRAGMENTS));
ADD_TO_ALLOCATIONS(bios_rsvd, rsvdmemlist_sz);
PRM_DEBUG(rsvdmemlist_sz);
ASSERT(P2SAMEHIGHBIT((1 << PP_SHIFT), sizeof (struct page)));
page_hashsz = npages / PAGE_HASHAVELEN;
page_hashsz_shift = highbit(page_hashsz);
page_hashsz = 1 << page_hashsz_shift;
pagehash_sz = sizeof (struct page *) * page_hashsz;
ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
PRM_DEBUG(pagehash_sz);
PRM_DEBUG(npages);
pp_sz = sizeof (struct page) * npages;
ADD_TO_ALLOCATIONS(pp_base, pp_sz);
PRM_DEBUG(pp_sz);
(void) getl2cacheinfo(CPU,
&l2cache_sz, &l2cache_linesz, &l2cache_assoc);
pagecolor_memsz =
page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
PRM_DEBUG(pagecolor_memsz);
page_ctrs_size = page_ctrs_sz();
ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
PRM_DEBUG(page_ctrs_size);
pse_shift = size_pse_array(physmem, max_ncpus);
pse_table_size = 1 << pse_shift;
pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
valloc_sz = ROUND_UP_LPAGE(valloc_sz);
valloc_base = VALLOC_BASE;
if (physmax + 1 > mmu_btop(TERABYTE / 4)) {
uint64_t physmem_bytes = mmu_ptob(physmax + 1);
uint64_t adjustment = 8 * (physmem_bytes - (TERABYTE / 4));
PRM_DEBUG(adjustment);
segkpm_base -= P2ROUNDUP(adjustment, KERNEL_REDZONE_SIZE);
segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
ASSERT(segkpm_base <= SEGKPM_BASE);
valloc_base = segkpm_base + P2ROUNDUP(physmem_bytes, ONE_GIG);
if (valloc_base < segkpm_base)
panic("not enough kernel VA to support memory size");
}
PRM_DEBUG(segkpm_base);
PRM_DEBUG(valloc_base);
perform_allocations();
current = phys_install = memlist;
copy_memlist_filter(bootops->boot_mem->physinstalled, ¤t, NULL);
if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
panic("physinstalled was too big!");
if (prom_debug)
print_memlist("phys_install", phys_install);
phys_avail = current;
PRM_POINT("Building phys_avail:\n");
copy_memlist_filter(bootops->boot_mem->physinstalled, ¤t,
avail_filter);
if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
panic("physavail was too big!");
if (prom_debug)
print_memlist("phys_avail", phys_avail);
#ifndef __xpv
if ((caddr_t)current < (caddr_t)memlist + memlist_sz) {
memlist_free_block((caddr_t)current,
(caddr_t)memlist + memlist_sz - (caddr_t)current);
}
#endif
current = bios_rsvd;
copy_memlist_filter(bootops->boot_mem->rsvdmem, ¤t, NULL);
if ((caddr_t)current > (caddr_t)bios_rsvd + rsvdmemlist_sz)
panic("bios_rsvd was too big!");
if (prom_debug)
print_memlist("bios_rsvd", bios_rsvd);
#ifndef __xpv
if ((caddr_t)current < (caddr_t)bios_rsvd + rsvdmemlist_sz) {
memlist_free_block((caddr_t)current,
(caddr_t)bios_rsvd + rsvdmemlist_sz - (caddr_t)current);
}
#endif
page_coloring_setup(pagecolor_mem);
page_lock_init();
(void) page_ctrs_alloc(page_ctrs_mem);
pcf_init();
availrmem_initial = availrmem = freemem = 0;
PRM_POINT("Calling kphysm_init()...");
npages = kphysm_init(pp_base, npages);
PRM_POINT("kphysm_init() done");
PRM_DEBUG(npages);
init_debug_info();
boot_mapin((caddr_t)valloc_base, valloc_sz);
boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
PRM_POINT("startup_memlist() done");
PRM_DEBUG(valloc_sz);
if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
textrepl_min_gb && l2cache_sz <= 2 << 20) {
extern size_t textrepl_size_thresh;
textrepl_size_thresh = (16 << 20) - 1;
}
}
static void
startup_kmem(void)
{
#if !defined(__xpv)
extern uint64_t kpti_kbase;
#endif
PRM_POINT("startup_kmem() starting...");
if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
"systems.");
kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
core_base = (uintptr_t)COREHEAP_BASE;
core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
PRM_DEBUG(core_base);
PRM_DEBUG(core_size);
PRM_DEBUG(kernelbase);
ekernelheap = (char *)core_base;
PRM_DEBUG(ekernelheap);
*(uintptr_t *)&_kernelbase = kernelbase;
*(uintptr_t *)&_userlimit = kernelbase;
*(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
#if !defined(__xpv)
kpti_kbase = kernelbase;
#endif
PRM_DEBUG(_kernelbase);
PRM_DEBUG(_userlimit);
PRM_DEBUG(_userlimit32);
mmu_calc_user_slots();
layout_kernel_va();
kernelheap_init(kernelheap, ekernelheap,
kernelheap + MMU_PAGESIZE,
(void *)core_base, (void *)(core_base + core_size));
#if defined(__xpv)
CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
#endif
kmem_init();
page_set_colorequiv_arr();
print_x86_featureset(x86_featureset);
bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
if (orig_npages) {
cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
(npages == PHYSMEM ? "Due to virtual address space " : ""),
npages, orig_npages);
}
#ifdef KERNELBASE_ABI_MIN
if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
"i386 ABI compliant.", (uintptr_t)kernelbase);
}
#endif
#ifndef __xpv
if (plat_dr_support_memory()) {
mem_config_init();
}
#else
PRM_POINT("calling xen_relocate_start_info()");
xen_relocate_start_info();
PRM_POINT("xen_relocate_start_info() done");
CPU->cpu_m.mcpu_vcpu_info =
&HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
#endif
PRM_POINT("startup_kmem() done");
}
#ifndef __xpv
#define HVM_MOD_DIR "/platform/i86hvm/kernel"
static void
update_default_path()
{
char *current, *newpath;
int newlen;
current = (default_path == NULL) ? kobj_module_path : default_path;
newlen = strlen(HVM_MOD_DIR) + strlen(current) + 2;
newpath = kmem_alloc(newlen, KM_SLEEP);
(void) strcpy(newpath, HVM_MOD_DIR);
(void) strcat(newpath, " ");
(void) strcat(newpath, current);
default_path = newpath;
}
#endif
static void
startup_modules(void)
{
int cnt;
extern void prom_setup(void);
int32_t v, h;
char d[11];
char *cp;
cmi_hdl_t hdl;
PRM_POINT("startup_modules() starting...");
#ifndef __xpv
if ((get_hwenv() & HW_XEN_HVM) != 0)
update_default_path();
#endif
sgmtl(process_rtc_config_file());
param_calc(0);
mod_setup();
param_init();
brand_init();
maxmem = physmem;
seg_init();
if (modload("fs", "specfs") == -1)
halt("Can't load specfs");
if (modload("fs", "devfs") == -1)
halt("Can't load devfs");
if (modload("fs", "dev") == -1)
halt("Can't load dev");
if (modload("fs", "procfs") == -1)
halt("Can't load procfs");
(void) modloadonly("sys", "lbl_edition");
dispinit();
clconf_init();
#if defined(__xpv)
(void) ec_init();
gnttab_init();
(void) xs_early_init();
#endif
setup_ddi();
#ifdef __xpv
if (DOMAIN_IS_INITDOMAIN(xen_info))
#endif
{
id_t smid;
smbios_system_t smsys;
smbios_info_t sminfo;
char *mfg;
ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
if (ksmbios != NULL &&
((smid = smbios_info_system(ksmbios, &smsys)) != SMB_ERR) &&
(smbios_info_common(ksmbios, smid, &sminfo)) != SMB_ERR) {
mfg = (char *)sminfo.smbi_manufacturer;
if (BOP_GETPROPLEN(bootops, "si-hw-provider") < 0) {
extern char hw_provider[];
int i;
for (i = 0; i < SYS_NMLN; i++) {
if (isprint(mfg[i]))
hw_provider[i] = mfg[i];
else {
hw_provider[i] = '\0';
break;
}
}
hw_provider[SYS_NMLN - 1] = '\0';
}
}
}
if ((h = set_soft_hostid()) == HW_INVALID_HOSTID) {
cmn_err(CE_WARN, "Unable to set hostid");
} else {
for (v = h, cnt = 0; cnt < 10; cnt++) {
d[cnt] = (char)(v % 10);
v /= 10;
if (v == 0)
break;
}
for (cp = hw_serial; cnt >= 0; cnt--)
*cp++ = d[cnt] + '0';
*cp = 0;
}
#ifdef __xpv
if (DOMAIN_IS_INITDOMAIN(xen_info)) {
xen_mc_lcpu_cookie_t cpi;
for (cpi = xen_physcpu_next(NULL); cpi != NULL;
cpi = xen_physcpu_next(cpi)) {
if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
xen_physcpu_strandid(cpi))) != NULL &&
is_x86_feature(x86_featureset, X86FSET_MCA))
cmi_mca_init(hdl);
}
}
#else
if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
if (is_x86_feature(x86_featureset, X86FSET_MCA))
cmi_mca_init(hdl);
CPU->cpu_m.mcpu_cmi_hdl = hdl;
}
#endif
PRM_POINT("startup_modules: calling prom_setup...");
prom_setup();
PRM_POINT("startup_modules: done");
PRM_POINT("startup_modules: calling psm_modload...");
psm_modload();
PRM_POINT("startup_modules() done");
}
page_t *
boot_claim_page(pfn_t pfn)
{
page_t *pp;
pp = page_numtopp_nolock(pfn);
ASSERT(pp != NULL);
if (PP_ISBOOTPAGES(pp)) {
if (pp->p_next != NULL)
pp->p_next->p_prev = pp->p_prev;
if (pp->p_prev == NULL)
bootpages = pp->p_next;
else
pp->p_prev->p_next = pp->p_next;
} else {
if (pp->p_szc != 0)
page_boot_demote(pp);
pp = page_numtopp(pfn, SE_EXCL);
}
return (pp);
}
static void
protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
{
uintptr_t va = low;
size_t len;
uint_t prot;
pfn_t pfn;
page_t *pp;
pgcnt_t boot_protect_cnt = 0;
while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
if (va + len >= high)
panic("0x%lx byte mapping at 0x%p exceeds boot's "
"legal range.", len, (void *)va);
while (len > 0) {
pp = page_numtopp_alloc(pfn);
if (pp != NULL) {
if (setaside == 0)
panic("Unexpected mapping by boot. "
"addr=%p pfn=%lx\n",
(void *)va, pfn);
pp->p_next = bootpages;
pp->p_prev = NULL;
PP_SETBOOTPAGES(pp);
if (bootpages != NULL) {
bootpages->p_prev = pp;
}
bootpages = pp;
++boot_protect_cnt;
}
++pfn;
len -= MMU_PAGESIZE;
va += MMU_PAGESIZE;
}
}
PRM_DEBUG(boot_protect_cnt);
}
static void
layout_kernel_va(void)
{
const size_t physmem_size = mmu_ptob(physmem);
size_t size;
PRM_POINT("layout_kernel_va() starting...");
kpm_vbase = (caddr_t)segkpm_base;
kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
panic("not enough room for kpm!");
PRM_DEBUG(kpm_size);
PRM_DEBUG(kpm_vbase);
segkp_base = (caddr_t)valloc_base + valloc_sz;
if (!segkp_fromheap) {
size = mmu_ptob(segkpsize);
if (size < SEGKPMINSIZE || size > SEGKPMAXSIZE) {
size = SEGKPDEFSIZE;
cmn_err(CE_WARN, "!Illegal value for segkpsize. "
"segkpsize has been reset to %ld pages",
mmu_btop(size));
}
size = MIN(size, MAX(SEGKPMINSIZE, physmem_size));
segkpsize = mmu_btop(ROUND_UP_LPAGE(size));
}
PRM_DEBUG(segkp_base);
PRM_DEBUG(segkpsize);
segkvmm_base = segkp_base + mmu_ptob(segkpsize);
size = segkvmmsize != 0 ? mmu_ptob(segkvmmsize) : (physmem_size * 4);
size = MAX(size, SEGVMMMINSIZE);
segkvmmsize = mmu_btop(ROUND_UP_LPAGE(size));
PRM_DEBUG(segkvmmsize);
PRM_DEBUG(segkvmm_base);
segzio_base = segkvmm_base + mmu_ptob(segkvmmsize);
if (segzio_fromheap) {
segziosize = 0;
} else {
size = (segziosize != 0) ? mmu_ptob(segziosize) :
(physmem_size * 3) / 2;
size = MAX(size, SEGZIOMINSIZE);
segziosize = mmu_btop(ROUND_UP_LPAGE(size));
}
PRM_DEBUG(segziosize);
PRM_DEBUG(segzio_base);
toxic_addr =
ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
PRM_DEBUG(toxic_addr);
segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
PRM_DEBUG(segmap_start);
PRM_DEBUG(segmapsize);
kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
PRM_DEBUG(kernelheap);
PRM_POINT("layout_kernel_va() done...");
}
static void
startup_vm(void)
{
struct segmap_crargs a;
extern int use_brk_lpg, use_stk_lpg;
PRM_POINT("startup_vm() starting...");
hat_init();
PRM_POINT("Calling hat_kern_alloc()...");
hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
PRM_POINT("hat_kern_alloc() done");
#ifndef __xpv
pat_sync();
#endif
bootpages = NULL;
PRM_POINT("Protecting boot pages");
protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
protect_boot_range(0, kernelbase, 1);
PRM_POINT("Calling hat_kern_setup()...");
hat_kern_setup();
bop_no_more_mem();
PRM_POINT("hat_kern_setup() done");
hat_cpu_online(CPU);
PRM_POINT("Calling kvm_init()...");
kvm_init();
PRM_POINT("kvm_init() done");
if (boothowto & RB_DEBUG)
kdi_dvec_vmready();
#if defined(__xpv)
if (DOMAIN_IS_INITDOMAIN(xen_info)) {
extern long populate_io_pool(void);
long init_io_pool_cnt;
PRM_POINT("Populating reserve I/O page pool");
init_io_pool_cnt = populate_io_pool();
PRM_DEBUG(init_io_pool_cnt);
}
#endif
cpuid_execpass(CPU, CPUID_PASS_DYNAMIC, NULL);
device_arena = vmem_create("device", (void *)toxic_addr,
toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
if (boothowto & RB_DEBUG)
kdi_dvec_memavail();
#if !defined(__xpv)
PRM_POINT("setup up p0_va");
p0_va = i86devmap(0, 1, PROT_READ);
PRM_DEBUG(p0_va);
#endif
cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
if (!auto_lpg_disable && mmu.max_page_level > 0) {
max_uheap_lpsize = LEVEL_SIZE(1);
max_ustack_lpsize = LEVEL_SIZE(1);
max_privmap_lpsize = LEVEL_SIZE(1);
max_uidata_lpsize = LEVEL_SIZE(1);
max_utext_lpsize = LEVEL_SIZE(1);
max_shm_lpsize = LEVEL_SIZE(1);
}
if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
auto_lpg_disable) {
use_brk_lpg = 0;
use_stk_lpg = 0;
}
mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
PRM_POINT("Calling hat_init_finish()...");
hat_init_finish();
PRM_POINT("hat_init_finish() done");
rw_enter(&kas.a_lock, RW_WRITER);
PRM_POINT("Attaching segkp");
if (segkp_fromheap) {
segkp->s_as = &kas;
} else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
segkp) < 0) {
panic("startup: cannot attach segkp");
}
PRM_POINT("Doing segkp_create()");
if (segkp_create(segkp) != 0) {
panic("startup: segkp_create failed");
}
PRM_DEBUG(segkp);
rw_exit(&kas.a_lock);
segmap_kpm = 0;
if (kpm_desired)
kpm_init();
rw_enter(&kas.a_lock, RW_WRITER);
if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
panic("cannot attach segmap");
}
PRM_DEBUG(segmap);
a.prot = PROT_READ | PROT_WRITE;
a.shmsize = 0;
a.nfreelist = segmapfreelists;
if (segmap_create(segmap, (caddr_t)&a) != 0)
panic("segmap_create segmap");
rw_exit(&kas.a_lock);
setup_vaddr_for_ppcopy(CPU);
segdev_init();
#if defined(__xpv)
if (DOMAIN_IS_INITDOMAIN(xen_info))
#endif
pmem_init();
PRM_POINT("startup_vm() done");
}
static void
load_tod_module(char *todmod)
{
if (modload("tod", todmod) == -1)
halt("Can't load TOD module");
}
#ifndef __xpv
static void
startup_tsc(void)
{
uint64_t tsc_freq;
PRM_POINT("startup_tsc() starting...");
tsc_freq = tsc_calibrate();
PRM_DEBUG(tsc_freq);
tsc_hrtimeinit(tsc_freq);
}
#endif
static void
startup_end(void)
{
int i;
extern void cpu_event_init(void);
PRM_POINT("startup_end() starting...");
kern_setup1();
kcpc_hw_init(CPU);
cpu_event_init();
#if defined(OPTERON_ERRATUM_147)
if (opteron_erratum_147)
patch_erratum_147();
#endif
if (tod_module_name != NULL) {
PRM_POINT("load_tod_module()");
load_tod_module(tod_module_name);
}
#if defined(__xpv)
PRM_POINT("load_tod_module()");
load_tod_module("xpvtod");
#endif
PRM_POINT("Calling configure()...");
configure();
PRM_POINT("configure() done");
if (fp_save_mech == FP_XSAVE) {
PRM_POINT("xsave_setup_msr()");
xsave_setup_msr(CPU);
}
fpu_save_cache_init();
lwp_fp_init(&lwp0);
setx86isalist();
PRM_POINT("cpu_intr_alloc()");
cpu_intr_alloc(CPU, NINTR_THREADS);
PRM_POINT("psm_install()");
psm_install();
PRM_POINT("NULLing out bootops");
*bootopsp = (struct bootops *)NULL;
bootops = (struct bootops *)NULL;
#if defined(__xpv)
ec_init_debug_irq();
xs_domu_init();
#endif
#if !defined(__xpv)
immu_startup();
enable_pcid();
#endif
PRM_POINT("Enabling interrupts");
(*picinitf)();
sti();
#if defined(__xpv)
ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
xen_late_startup();
#endif
(void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
"softlevel1", NULL, NULL);
for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
(void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
(avfunc)(uintptr_t)ddi_periodic_softintr, "ddi_periodic",
(caddr_t)(uintptr_t)i, NULL);
}
#if !defined(__xpv)
if (modload("drv", "amd_iommu") < 0) {
PRM_POINT("No AMD IOMMU present\n");
} else if (ddi_hold_installed_driver(ddi_name_to_major(
"amd_iommu")) == NULL) {
PRM_POINT("AMD IOMMU failed to attach\n");
}
#endif
post_startup_cpu_fixups();
PRM_POINT("startup_end() done");
}
char *_hs1107 = hw_serial;
ulong_t _bdhs34;
void
post_startup(void)
{
extern void cpupm_init(cpu_t *);
extern void cpu_event_init_cpu(cpu_t *);
bind_hwcap();
#ifdef __xpv
if (DOMAIN_IS_INITDOMAIN(xen_info))
#endif
{
#if defined(__xpv)
xpv_panic_init();
#else
if ((get_hwenv() & HW_VIRTUAL) == 0)
memscrub_init();
#endif
}
cmi_post_startup();
(void) mod_sysctl(SYS_FORCELOAD, NULL);
(void) modload("fs", "procfs");
(void) i_ddi_attach_hw_nodes("pit_beep");
maxmem = freemem;
cpu_event_init_cpu(CPU);
cpupm_init(CPU);
(void) mach_cpu_create_device_node(CPU, NULL);
pg_init();
}
static int
pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
{
return ((pp->p_pagenum >= btop(low_addr)) &&
(pp->p_pagenum < btopr(high_addr)));
}
static int
pp_in_module(page_t *pp, const rd_existing_t *modranges)
{
uint_t i;
for (i = 0; modranges[i].phys != 0; i++) {
if (pp_in_range(pp, modranges[i].phys,
modranges[i].phys + modranges[i].size))
return (1);
}
return (0);
}
void
release_bootstrap(void)
{
int root_is_ramdisk;
page_t *pp;
extern void kobj_boot_unmountroot(void);
extern dev_t rootdev;
uint_t i;
char propname[32];
rd_existing_t *modranges;
#if !defined(__xpv)
pfn_t pfn;
#endif
modranges = kmem_alloc(sizeof (rd_existing_t) * MAX_BOOT_MODULES,
KM_SLEEP);
for (i = 0; ; i++) {
uint64_t start, size;
modranges[i].phys = 0;
(void) snprintf(propname, sizeof (propname),
"module-addr-%u", i);
if (do_bsys_getproplen(NULL, propname) <= 0)
break;
(void) do_bsys_getprop(NULL, propname, &start);
(void) snprintf(propname, sizeof (propname),
"module-size-%u", i);
if (do_bsys_getproplen(NULL, propname) <= 0)
break;
(void) do_bsys_getprop(NULL, propname, &size);
modranges[i].phys = start;
modranges[i].size = size;
}
kobj_boot_unmountroot();
PRM_POINT("Unmapping lower boot pages");
clear_boot_mappings(0, _userlimit);
postbootkernelbase = kernelbase;
root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
if (!root_is_ramdisk) {
dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
ndi_rele_devi(dip);
(void) ddi_remove_child(dip, 0);
}
PRM_POINT("Releasing boot pages");
while (bootpages) {
extern uint64_t ramdisk_start, ramdisk_end;
pp = bootpages;
bootpages = pp->p_next;
if (pp_in_range(pp, 0, 0x40000)) {
pp->p_next = lower_pages;
lower_pages = pp;
lower_pages_count++;
continue;
}
if ((root_is_ramdisk && pp_in_range(pp, ramdisk_start,
ramdisk_end)) || pp_in_module(pp, modranges)) {
pp->p_next = rd_pages;
rd_pages = pp;
continue;
}
pp->p_next = (struct page *)0;
pp->p_prev = (struct page *)0;
PP_CLRBOOTPAGES(pp);
page_free(pp, 1);
}
PRM_POINT("Boot pages released");
kmem_free(modranges, sizeof (rd_existing_t) * 99);
#if !defined(__xpv)
for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
if (page_numtopp_alloc(pfn) == NULL)
continue;
rm_platter_va = i86devmap(pfn, 1,
PROT_READ | PROT_WRITE | PROT_EXEC);
rm_platter_pa = ptob(pfn);
break;
}
if (pfn == btop(1*1024*1024) && use_mp)
panic("No page below 1M available for starting "
"other processors or for resuming from system-suspend");
#endif
}
void
add_physmem_cb(page_t *pp, pfn_t pnum)
{
pp->p_pagenum = pnum;
pp->p_mapping = NULL;
pp->p_embed = 0;
pp->p_share = 0;
pp->p_mlentry = 0;
}
static pgcnt_t
kphysm_init(page_t *pp, pgcnt_t npages)
{
struct memlist *pmem;
struct memseg *cur_memseg;
pfn_t base_pfn;
pfn_t end_pfn;
pgcnt_t num;
pgcnt_t pages_done = 0;
uint64_t addr;
uint64_t size;
extern pfn_t ddiphysmin;
extern int mnode_xwa;
int ms = 0, me = 0;
ASSERT(page_hash != NULL && page_hashsz != 0);
cur_memseg = memseg_base;
for (pmem = phys_avail; pmem && npages; pmem = pmem->ml_next) {
addr = pmem->ml_address;
size = pmem->ml_size;
if (btop(addr) > physmax)
continue;
if ((addr & MMU_PAGEOFFSET) != 0) {
addr += MMU_PAGEOFFSET;
addr &= ~(uint64_t)MMU_PAGEOFFSET;
size -= addr - pmem->ml_address;
}
if ((btop(addr + size) - 1) > physmax)
size = ptob(physmax - btop(addr) + 1);
num = btop(size);
if (num == 0)
continue;
if (num > npages)
num = npages;
npages -= num;
pages_done += num;
base_pfn = btop(addr);
if (prom_debug)
prom_printf("MEMSEG addr=0x%" PRIx64
" pgs=0x%lx pfn 0x%lx-0x%lx\n",
addr, num, base_pfn, base_pfn + num);
if (base_pfn < ddiphysmin) {
if (base_pfn + num <= ddiphysmin)
continue;
pp += (ddiphysmin - base_pfn);
num -= (ddiphysmin - base_pfn);
base_pfn = ddiphysmin;
}
end_pfn = base_pfn + num - 1;
if (mnode_xwa > 1) {
ms = PFN_2_MEM_NODE(base_pfn);
me = PFN_2_MEM_NODE(end_pfn);
if (ms != me) {
num = mem_node_config[ms].physmax - base_pfn
+ 1;
ASSERT(end_pfn > mem_node_config[ms].physmax);
}
}
for (;;) {
cur_memseg->pages = pp;
cur_memseg->epages = pp + num;
cur_memseg->pages_base = base_pfn;
cur_memseg->pages_end = base_pfn + num;
if (memsegs != NULL) {
ASSERT(cur_memseg->pages_base >=
memsegs->pages_end);
cur_memseg->next = memsegs;
}
memsegs = cur_memseg;
add_physmem(pp, num, base_pfn);
cur_memseg++;
availrmem_initial += num;
availrmem += num;
pp += num;
if (ms >= me)
break;
ms++;
base_pfn = mem_node_config[ms].physbase;
if (mnode_xwa > 1) {
num = MIN(mem_node_config[ms].physmax,
end_pfn) - base_pfn + 1;
} else {
num = mem_node_config[ms].physmax -
base_pfn + 1;
}
}
}
PRM_DEBUG(availrmem_initial);
PRM_DEBUG(availrmem);
PRM_DEBUG(freemem);
build_pfn_hash();
return (pages_done);
}
static void
kvm_init(void)
{
ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
rw_enter(&kas.a_lock, RW_WRITER);
as_avlinit(&kas);
(void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
(void) segkmem_create(&ktextseg);
(void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
(void) segkmem_create(&kvalloc);
(void) seg_attach(&kas, kernelheap,
ekernelheap - kernelheap, &kvseg);
(void) segkmem_create(&kvseg);
if (core_size > 0) {
PRM_POINT("attaching kvseg_core");
(void) seg_attach(&kas, (caddr_t)core_base, core_size,
&kvseg_core);
(void) segkmem_create(&kvseg_core);
}
PRM_POINT("attaching segkvmm");
(void) seg_attach(&kas, segkvmm_base, mmu_ptob(segkvmmsize), &kvmmseg);
(void) segkmem_create(&kvmmseg);
segkmem_kvmm_init(segkvmm_base, mmu_ptob(segkvmmsize));
if (segziosize > 0) {
PRM_POINT("attaching segzio");
(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
&kzioseg);
(void) segkmem_create(&kzioseg);
segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
}
(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
(void) segkmem_create(&kdebugseg);
rw_exit(&kas.a_lock);
PRM_POINT("protecting redzone");
(void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
(void) as_setprot(&kas, s_text, e_modtext - s_text,
PROT_READ | PROT_WRITE | PROT_EXEC);
(void) as_setprot(&kas, s_data, e_moddata - s_data,
PROT_READ | PROT_WRITE | PROT_EXEC);
}
#ifndef __xpv
static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
void
pat_sync(void)
{
ulong_t cr0, cr0_orig, cr4;
if (!is_x86_feature(x86_featureset, X86FSET_PAT))
return;
cr0_orig = cr0 = getcr0();
cr4 = getcr4();
cr0 |= CR0_CD;
cr0 &= ~CR0_NW;
setcr0(cr0);
invalidate_cache();
if (cr4 & CR4_PGE) {
setcr4(cr4 & ~(ulong_t)CR4_PGE);
setcr4(cr4);
} else {
reload_cr3();
}
wrmsr(REG_PAT, pat_attr_reg);
if (cr4 & CR4_PGE) {
setcr4(cr4 & ~(ulong_t)CR4_PGE);
setcr4(cr4);
} else {
reload_cr3();
}
invalidate_cache();
setcr0(cr0_orig);
}
#endif
#if defined(_SOFT_HOSTID)
static int atoi(char *);
int smbios_broken_uuid = 0;
static unsigned char
smbios_uuid_blacklist[][16] = {
{
0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05,
0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09,
},
{
0x4C, 0x4C, 0x45, 0x44, 0x00, 0x00, 0x20, 0x10,
0x80, 0x20, 0x80, 0xC0, 0x4F, 0x20, 0x20, 0x20,
},
{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
},
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
},
};
static int32_t
uuid_to_hostid(const uint8_t *uuid)
{
uint32_t id = 0;
for (int i = 0; i < (sizeof (smbios_uuid_blacklist) / 16); i++) {
if (bcmp(smbios_uuid_blacklist[0], uuid, 16) == 0) {
cmn_err(CE_CONT, "?Broken SMBIOS UUID. "
"Contact BIOS manufacturer for repair.\n");
return ((int32_t)HW_INVALID_HOSTID);
}
}
for (int i = 0; i < 16; i++)
id ^= ((uuid[i]) << (8 * (i % sizeof (id))));
return (id & 0x7fffffff);
}
static int32_t
set_soft_hostid(void)
{
struct _buf *file;
char tokbuf[MAXNAMELEN];
token_t token;
int done = 0;
u_longlong_t tmp;
int i;
int32_t hostid = (int32_t)HW_INVALID_HOSTID;
unsigned char *c;
smbios_system_t smsys;
if ((file = kobj_open_file(hostid_file)) == (struct _buf *)-1) {
if ((i = modload("misc", "sysinit")) != -1) {
if (strlen(hw_serial) > 0)
hostid = (int32_t)atoi(hw_serial);
(void) modunload(i);
}
if ((hostid == HW_INVALID_HOSTID) &&
(smbios_broken_uuid == 0) &&
(ksmbios != NULL) &&
(smbios_info_system(ksmbios, &smsys) != SMB_ERR) &&
(smsys.smbs_uuidlen >= 16)) {
hostid = uuid_to_hostid(smsys.smbs_uuid);
}
if (hostid == HW_INVALID_HOSTID) {
hostid = tsc_read() & 0x0CFFFFF;
}
} else {
while (!done) {
token = kobj_lex(file, tokbuf, sizeof (tokbuf));
switch (token) {
case POUND:
kobj_find_eol(file);
break;
case STRING:
for (c = (unsigned char *)tokbuf;
*c != '\0'; c++) {
*c += 47;
if (*c > '~')
*c -= 94;
else if (*c < '!')
*c += 94;
}
if (kobj_getvalue(tokbuf, &tmp) != 0)
kobj_file_err(CE_WARN, file,
"Bad value %s for hostid",
tokbuf);
else
hostid = (int32_t)tmp;
break;
case EOF:
done = 1;
case NEWLINE:
kobj_newline(file);
break;
default:
break;
}
}
if (hostid == HW_INVALID_HOSTID)
kobj_file_err(CE_WARN, file,
"hostid missing or corrupt");
kobj_close_file(file);
}
return (hostid);
}
static int
atoi(char *p)
{
int i = 0;
while (*p != '\0')
i = 10 * i + (*p++ - '0');
return (i);
}
#endif
void
get_system_configuration(void)
{
char prop[32];
u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
BOP_GETPROP(bootops, "nodes", prop) < 0 ||
kobj_getvalue(prop, &nodes_ll) == -1 ||
nodes_ll > MAXNODES ||
BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
system_hardware.hd_nodes = 1;
system_hardware.hd_cpus_per_node = 0;
} else {
system_hardware.hd_nodes = (int)nodes_ll;
system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
}
if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
kobj_getvalue(prop, &lvalue) == -1)
eprom_kernelbase = 0;
else
eprom_kernelbase = (uintptr_t)lvalue;
if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
kobj_getvalue(prop, &lvalue) == -1)
segmapsize = SEGMAPDEFAULT;
else
segmapsize = (uintptr_t)lvalue;
if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
kobj_getvalue(prop, &lvalue) == -1)
segmapfreelists = 0;
else
segmapfreelists = (int)lvalue;
if (BOP_GETPROPLEN(bootops, "segkpsize") > sizeof (prop) ||
BOP_GETPROP(bootops, "segkpsize", prop) < 0 ||
kobj_getvalue(prop, &lvalue) == -1)
segkpsize = mmu_btop(SEGKPDEFSIZE);
else
segkpsize = mmu_btop((size_t)lvalue);
}
void
memlist_add(
uint64_t start,
uint64_t len,
struct memlist *new,
struct memlist **memlistp)
{
struct memlist *cur;
uint64_t end = start + len;
new->ml_address = start;
new->ml_size = len;
cur = *memlistp;
while (cur) {
if (cur->ml_address >= end) {
new->ml_next = cur;
*memlistp = new;
new->ml_prev = cur->ml_prev;
cur->ml_prev = new;
return;
}
ASSERT(cur->ml_address + cur->ml_size <= start);
if (cur->ml_next == NULL) {
cur->ml_next = new;
new->ml_prev = cur;
new->ml_next = NULL;
return;
}
memlistp = &cur->ml_next;
cur = cur->ml_next;
}
}
void
kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
{
size_t tsize = e_modtext - modtext;
size_t dsize = e_moddata - moddata;
*text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
*data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
}
caddr_t
kobj_text_alloc(vmem_t *arena, size_t size)
{
return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
}
caddr_t
kobj_texthole_alloc(caddr_t addr, size_t size)
{
panic("unexpected call to kobj_texthole_alloc()");
return (0);
}
void
kobj_texthole_free(caddr_t addr, size_t size)
{
panic("unexpected call to kobj_texthole_free()");
}
static void
setx86isalist(void)
{
char *tp;
size_t len;
extern char *isa_list;
#define TBUFSIZE 1024
tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
*tp = '\0';
(void) strcpy(tp, "amd64 ");
switch (x86_vendor) {
case X86_VENDOR_Intel:
case X86_VENDOR_AMD:
case X86_VENDOR_HYGON:
case X86_VENDOR_TM:
if (is_x86_feature(x86_featureset, X86FSET_CMOV)) {
(void) strcat(tp, "pentium_pro");
(void) strcat(tp,
is_x86_feature(x86_featureset, X86FSET_MMX) ?
"+mmx pentium_pro " : " ");
}
case X86_VENDOR_Cyrix:
ASSERT(is_x86_feature(x86_featureset, X86FSET_CPUID));
(void) strcat(tp, "pentium");
(void) strcat(tp,
is_x86_feature(x86_featureset, X86FSET_MMX) ?
"+mmx pentium " : " ");
break;
default:
break;
}
(void) strcat(tp, "i486 i386 i86");
len = strlen(tp) + 1;
isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
kmem_free(tp, TBUFSIZE);
#undef TBUFSIZE
}
void *
device_arena_alloc(size_t size, int vm_flag)
{
return (vmem_alloc(device_arena, size, vm_flag));
}
void
device_arena_free(void *vaddr, size_t size)
{
vmem_free(device_arena, vaddr, size);
}