Print this page
    
9059 Simplify SMAP relocations with krtld
Portions contributed by: John Levon <john.levon@joyent.com>
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/i86pc/os/startup.c
          +++ new/usr/src/uts/i86pc/os/startup.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
  25   25   * Copyright 2017 Nexenta Systems, Inc.
  26   26   * Copyright (c) 2018 Joyent, Inc.
  27   27   * Copyright (c) 2015 by Delphix. All rights reserved.
  28   28   */
  29   29  /*
  30   30   * Copyright (c) 2010, Intel Corporation.
  31   31   * All rights reserved.
  32   32   */
  33   33  
  34   34  #include <sys/types.h>
  35   35  #include <sys/t_lock.h>
  36   36  #include <sys/param.h>
  37   37  #include <sys/sysmacros.h>
  38   38  #include <sys/signal.h>
  39   39  #include <sys/systm.h>
  40   40  #include <sys/user.h>
  41   41  #include <sys/mman.h>
  42   42  #include <sys/vm.h>
  43   43  #include <sys/conf.h>
  44   44  #include <sys/avintr.h>
  45   45  #include <sys/autoconf.h>
  46   46  #include <sys/disp.h>
  47   47  #include <sys/class.h>
  48   48  #include <sys/bitmap.h>
  49   49  
  50   50  #include <sys/privregs.h>
  51   51  
  52   52  #include <sys/proc.h>
  53   53  #include <sys/buf.h>
  54   54  #include <sys/kmem.h>
  55   55  #include <sys/mem.h>
  56   56  #include <sys/kstat.h>
  57   57  
  58   58  #include <sys/reboot.h>
  59   59  
  60   60  #include <sys/cred.h>
  61   61  #include <sys/vnode.h>
  62   62  #include <sys/file.h>
  63   63  
  64   64  #include <sys/procfs.h>
  65   65  
  66   66  #include <sys/vfs.h>
  67   67  #include <sys/cmn_err.h>
  68   68  #include <sys/utsname.h>
  69   69  #include <sys/debug.h>
  70   70  #include <sys/kdi.h>
  71   71  
  72   72  #include <sys/dumphdr.h>
  73   73  #include <sys/bootconf.h>
  74   74  #include <sys/memlist_plat.h>
  75   75  #include <sys/varargs.h>
  76   76  #include <sys/promif.h>
  77   77  #include <sys/modctl.h>
  78   78  
  79   79  #include <sys/sunddi.h>
  80   80  #include <sys/sunndi.h>
  81   81  #include <sys/ndi_impldefs.h>
  82   82  #include <sys/ddidmareq.h>
  83   83  #include <sys/psw.h>
  84   84  #include <sys/regset.h>
  85   85  #include <sys/clock.h>
  86   86  #include <sys/pte.h>
  87   87  #include <sys/tss.h>
  88   88  #include <sys/stack.h>
  89   89  #include <sys/trap.h>
  90   90  #include <sys/fp.h>
  91   91  #include <vm/kboot_mmu.h>
  92   92  #include <vm/anon.h>
  93   93  #include <vm/as.h>
  94   94  #include <vm/page.h>
  95   95  #include <vm/seg.h>
  96   96  #include <vm/seg_dev.h>
  97   97  #include <vm/seg_kmem.h>
  98   98  #include <vm/seg_kpm.h>
  99   99  #include <vm/seg_map.h>
 100  100  #include <vm/seg_vn.h>
 101  101  #include <vm/seg_kp.h>
 102  102  #include <sys/memnode.h>
 103  103  #include <vm/vm_dep.h>
 104  104  #include <sys/thread.h>
 105  105  #include <sys/sysconf.h>
 106  106  #include <sys/vm_machparam.h>
 107  107  #include <sys/archsystm.h>
 108  108  #include <sys/machsystm.h>
 109  109  #include <vm/hat.h>
 110  110  #include <vm/hat_i86.h>
 111  111  #include <sys/pmem.h>
 112  112  #include <sys/smp_impldefs.h>
 113  113  #include <sys/x86_archext.h>
 114  114  #include <sys/cpuvar.h>
 115  115  #include <sys/segments.h>
 116  116  #include <sys/clconf.h>
 117  117  #include <sys/kobj.h>
 118  118  #include <sys/kobj_lex.h>
 119  119  #include <sys/cpc_impl.h>
 120  120  #include <sys/cpu_module.h>
 121  121  #include <sys/smbios.h>
 122  122  #include <sys/debug_info.h>
 123  123  #include <sys/bootinfo.h>
 124  124  #include <sys/ddi_periodic.h>
 125  125  #include <sys/systeminfo.h>
 126  126  #include <sys/multiboot.h>
 127  127  #include <sys/ramdisk.h>
 128  128  
 129  129  #ifdef  __xpv
 130  130  
 131  131  #include <sys/hypervisor.h>
 132  132  #include <sys/xen_mmu.h>
 133  133  #include <sys/evtchn_impl.h>
 134  134  #include <sys/gnttab.h>
 135  135  #include <sys/xpv_panic.h>
 136  136  #include <xen/sys/xenbus_comms.h>
 137  137  #include <xen/public/physdev.h>
 138  138  
 139  139  extern void xen_late_startup(void);
 140  140  
 141  141  struct xen_evt_data cpu0_evt_data;
 142  142  
 143  143  #else   /* __xpv */
 144  144  #include <sys/memlist_impl.h>
 145  145  
 146  146  extern void mem_config_init(void);
 147  147  #endif /* __xpv */
 148  148  
 149  149  extern void progressbar_init(void);
 150  150  extern void brand_init(void);
 151  151  extern void pcf_init(void);
 152  152  extern void pg_init(void);
 153  153  extern void ssp_init(void);
 154  154  
 155  155  extern int size_pse_array(pgcnt_t, int);
 156  156  
 157  157  #if defined(_SOFT_HOSTID)
 158  158  
 159  159  #include <sys/rtc.h>
 160  160  
 161  161  static int32_t set_soft_hostid(void);
 162  162  static char hostid_file[] = "/etc/hostid";
 163  163  
 164  164  #endif
 165  165  
 166  166  void *gfx_devinfo_list;
 167  167  
 168  168  #if defined(__amd64) && !defined(__xpv)
 169  169  extern void immu_startup(void);
 170  170  #endif
 171  171  
 172  172  /*
 173  173   * XXX make declaration below "static" when drivers no longer use this
 174  174   * interface.
 175  175   */
 176  176  extern caddr_t p0_va;   /* Virtual address for accessing physical page 0 */
 177  177  
 178  178  /*
 179  179   * segkp
 180  180   */
 181  181  extern int segkp_fromheap;
 182  182  
 183  183  static void kvm_init(void);
 184  184  static void startup_init(void);
 185  185  static void startup_memlist(void);
 186  186  static void startup_kmem(void);
 187  187  static void startup_modules(void);
 188  188  static void startup_vm(void);
 189  189  static void startup_end(void);
 190  190  static void layout_kernel_va(void);
 191  191  
 192  192  /*
 193  193   * Declare these as initialized data so we can patch them.
 194  194   */
 195  195  #ifdef __i386
 196  196  
 197  197  /*
 198  198   * Due to virtual address space limitations running in 32 bit mode, restrict
 199  199   * the amount of physical memory configured to a max of PHYSMEM pages (16g).
 200  200   *
 201  201   * If the physical max memory size of 64g were allowed to be configured, the
 202  202   * size of user virtual address space will be less than 1g. A limited user
 203  203   * address space greatly reduces the range of applications that can run.
 204  204   *
 205  205   * If more physical memory than PHYSMEM is required, users should preferably
 206  206   * run in 64 bit mode which has far looser virtual address space limitations.
 207  207   *
 208  208   * If 64 bit mode is not available (as in IA32) and/or more physical memory
 209  209   * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired
 210  210   * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase
 211  211   * should also be carefully tuned to balance out the need of the user
 212  212   * application while minimizing the risk of kernel heap exhaustion due to
 213  213   * kernelbase being set too high.
 214  214   */
 215  215  #define PHYSMEM 0x400000
 216  216  
 217  217  #else /* __amd64 */
 218  218  
 219  219  /*
 220  220   * For now we can handle memory with physical addresses up to about
 221  221   * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly
 222  222   * half the VA space for seg_kpm. When systems get bigger than 64TB this
 223  223   * code will need revisiting. There is an implicit assumption that there
 224  224   * are no *huge* holes in the physical address space too.
 225  225   */
 226  226  #define TERABYTE                (1ul << 40)
 227  227  #define PHYSMEM_MAX64           mmu_btop(64 * TERABYTE)
 228  228  #define PHYSMEM                 PHYSMEM_MAX64
 229  229  #define AMD64_VA_HOLE_END       0xFFFF800000000000ul
 230  230  
 231  231  #endif /* __amd64 */
 232  232  
 233  233  pgcnt_t physmem = PHYSMEM;
 234  234  pgcnt_t obp_pages;      /* Memory used by PROM for its text and data */
 235  235  
 236  236  char *kobj_file_buf;
 237  237  int kobj_file_bufsize;  /* set in /etc/system */
 238  238  
 239  239  /* Global variables for MP support. Used in mp_startup */
 240  240  caddr_t rm_platter_va = 0;
 241  241  uint32_t rm_platter_pa;
 242  242  
 243  243  int     auto_lpg_disable = 1;
 244  244  
 245  245  /*
 246  246   * Some CPUs have holes in the middle of the 64-bit virtual address range.
 247  247   */
 248  248  uintptr_t hole_start, hole_end;
 249  249  
 250  250  /*
 251  251   * kpm mapping window
 252  252   */
 253  253  caddr_t kpm_vbase;
 254  254  size_t  kpm_size;
 255  255  static int kpm_desired;
 256  256  #ifdef __amd64
 257  257  static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE;
 258  258  #endif
 259  259  
 260  260  /*
 261  261   * Configuration parameters set at boot time.
 262  262   */
 263  263  
 264  264  caddr_t econtig;                /* end of first block of contiguous kernel */
 265  265  
 266  266  struct bootops          *bootops = 0;   /* passed in from boot */
 267  267  struct bootops          **bootopsp;
 268  268  struct boot_syscalls    *sysp;          /* passed in from boot */
 269  269  
 270  270  char bootblock_fstype[16];
 271  271  
 272  272  char kern_bootargs[OBP_MAXPATHLEN];
 273  273  char kern_bootfile[OBP_MAXPATHLEN];
 274  274  
 275  275  /*
 276  276   * ZFS zio segment.  This allows us to exclude large portions of ZFS data that
 277  277   * gets cached in kmem caches on the heap.  If this is set to zero, we allocate
 278  278   * zio buffers from their own segment, otherwise they are allocated from the
 279  279   * heap.  The optimization of allocating zio buffers from their own segment is
 280  280   * only valid on 64-bit kernels.
 281  281   */
 282  282  #if defined(__amd64)
 283  283  int segzio_fromheap = 0;
 284  284  #else
 285  285  int segzio_fromheap = 1;
 286  286  #endif
 287  287  
 288  288  /*
 289  289   * Give folks an escape hatch for disabling SMAP via kmdb. Doesn't work
 290  290   * post-boot.
 291  291   */
 292  292  int disable_smap = 0;
 293  293  
 294  294  /*
 295  295   * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this
 296  296   * depends on number of BOP_ALLOC calls made and requested size, memory size
 297  297   * combination and whether boot.bin memory needs to be freed.
 298  298   */
 299  299  #define POSS_NEW_FRAGMENTS      12
 300  300  
 301  301  /*
 302  302   * VM data structures
 303  303   */
 304  304  long page_hashsz;               /* Size of page hash table (power of two) */
 305  305  unsigned int page_hashsz_shift; /* log2(page_hashsz) */
 306  306  struct page *pp_base;           /* Base of initial system page struct array */
 307  307  struct page **page_hash;        /* Page hash table */
 308  308  pad_mutex_t *pse_mutex;         /* Locks protecting pp->p_selock */
 309  309  size_t pse_table_size;          /* Number of mutexes in pse_mutex[] */
 310  310  int pse_shift;                  /* log2(pse_table_size) */
 311  311  struct seg ktextseg;            /* Segment used for kernel executable image */
 312  312  struct seg kvalloc;             /* Segment used for "valloc" mapping */
 313  313  struct seg kpseg;               /* Segment used for pageable kernel virt mem */
 314  314  struct seg kmapseg;             /* Segment used for generic kernel mappings */
 315  315  struct seg kdebugseg;           /* Segment used for the kernel debugger */
 316  316  
 317  317  struct seg *segkmap = &kmapseg; /* Kernel generic mapping segment */
 318  318  static struct seg *segmap = &kmapseg;   /* easier to use name for in here */
 319  319  
 320  320  struct seg *segkp = &kpseg;     /* Pageable kernel virtual memory segment */
 321  321  
 322  322  #if defined(__amd64)
 323  323  struct seg kvseg_core;          /* Segment used for the core heap */
 324  324  struct seg kpmseg;              /* Segment used for physical mapping */
 325  325  struct seg *segkpm = &kpmseg;   /* 64bit kernel physical mapping segment */
 326  326  #else
 327  327  struct seg *segkpm = NULL;      /* Unused on IA32 */
 328  328  #endif
 329  329  
 330  330  caddr_t segkp_base;             /* Base address of segkp */
 331  331  caddr_t segzio_base;            /* Base address of segzio */
 332  332  #if defined(__amd64)
 333  333  pgcnt_t segkpsize = btop(SEGKPDEFSIZE); /* size of segkp segment in pages */
 334  334  #else
 335  335  pgcnt_t segkpsize = 0;
 336  336  #endif
 337  337  pgcnt_t segziosize = 0;         /* size of zio segment in pages */
 338  338  
 339  339  /*
 340  340   * A static DR page_t VA map is reserved that can map the page structures
 341  341   * for a domain's entire RA space. The pages that back this space are
 342  342   * dynamically allocated and need not be physically contiguous.  The DR
 343  343   * map size is derived from KPM size.
 344  344   * This mechanism isn't used by x86 yet, so just stubs here.
 345  345   */
 346  346  int ppvm_enable = 0;            /* Static virtual map for page structs */
 347  347  page_t *ppvm_base = NULL;       /* Base of page struct map */
 348  348  pgcnt_t ppvm_size = 0;          /* Size of page struct map */
 349  349  
 350  350  /*
 351  351   * VA range available to the debugger
 352  352   */
 353  353  const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
 354  354  const size_t kdi_segdebugsize = SEGDEBUGSIZE;
 355  355  
 356  356  struct memseg *memseg_base;
 357  357  struct vnode unused_pages_vp;
 358  358  
 359  359  #define FOURGB  0x100000000LL
 360  360  
 361  361  struct memlist *memlist;
 362  362  
 363  363  caddr_t s_text;         /* start of kernel text segment */
 364  364  caddr_t e_text;         /* end of kernel text segment */
 365  365  caddr_t s_data;         /* start of kernel data segment */
 366  366  caddr_t e_data;         /* end of kernel data segment */
 367  367  caddr_t modtext;        /* start of loadable module text reserved */
 368  368  caddr_t e_modtext;      /* end of loadable module text reserved */
 369  369  caddr_t moddata;        /* start of loadable module data reserved */
 370  370  caddr_t e_moddata;      /* end of loadable module data reserved */
 371  371  
 372  372  struct memlist *phys_install;   /* Total installed physical memory */
 373  373  struct memlist *phys_avail;     /* Total available physical memory */
 374  374  struct memlist *bios_rsvd;      /* Bios reserved memory */
 375  375  
 376  376  /*
 377  377   * kphysm_init returns the number of pages that were processed
 378  378   */
 379  379  static pgcnt_t kphysm_init(page_t *, pgcnt_t);
 380  380  
 381  381  #define IO_PROP_SIZE    64      /* device property size */
 382  382  
 383  383  /*
 384  384   * a couple useful roundup macros
 385  385   */
 386  386  #define ROUND_UP_PAGE(x)        \
 387  387          ((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE))
 388  388  #define ROUND_UP_LPAGE(x)       \
 389  389          ((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1]))
 390  390  #define ROUND_UP_4MEG(x)        \
 391  391          ((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG))
 392  392  #define ROUND_UP_TOPLEVEL(x)    \
 393  393          ((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level]))
 394  394  
 395  395  /*
 396  396   *      32-bit Kernel's Virtual memory layout.
 397  397   *              +-----------------------+
 398  398   *              |                       |
 399  399   * 0xFFC00000  -|-----------------------|- ARGSBASE
 400  400   *              |       debugger        |
 401  401   * 0xFF800000  -|-----------------------|- SEGDEBUGBASE
 402  402   *              |      Kernel Data      |
 403  403   * 0xFEC00000  -|-----------------------|
 404  404   *              |      Kernel Text      |
 405  405   * 0xFE800000  -|-----------------------|- KERNEL_TEXT (0xFB400000 on Xen)
 406  406   *              |---       GDT       ---|- GDT page (GDT_VA)
 407  407   *              |---    debug info   ---|- debug info (DEBUG_INFO_VA)
 408  408   *              |                       |
 409  409   *              |   page_t structures   |
 410  410   *              |   memsegs, memlists,  |
 411  411   *              |   page hash, etc.     |
 412  412   * ---         -|-----------------------|- ekernelheap, valloc_base (floating)
 413  413   *              |                       |  (segkp is just an arena in the heap)
 414  414   *              |                       |
 415  415   *              |       kvseg           |
 416  416   *              |                       |
 417  417   *              |                       |
 418  418   * ---         -|-----------------------|- kernelheap (floating)
 419  419   *              |        Segkmap        |
 420  420   * 0xC3002000  -|-----------------------|- segmap_start (floating)
 421  421   *              |       Red Zone        |
 422  422   * 0xC3000000  -|-----------------------|- kernelbase / userlimit (floating)
 423  423   *              |                       |                       ||
 424  424   *              |     Shared objects    |                       \/
 425  425   *              |                       |
 426  426   *              :                       :
 427  427   *              |       user data       |
 428  428   *              |-----------------------|
 429  429   *              |       user text       |
 430  430   * 0x08048000  -|-----------------------|
 431  431   *              |       user stack      |
 432  432   *              :                       :
 433  433   *              |       invalid         |
 434  434   * 0x00000000   +-----------------------+
 435  435   *
 436  436   *
 437  437   *              64-bit Kernel's Virtual memory layout. (assuming 64 bit app)
 438  438   *                      +-----------------------+
 439  439   *                      |                       |
 440  440   * 0xFFFFFFFF.FFC00000  |-----------------------|- ARGSBASE
 441  441   *                      |       debugger (?)    |
 442  442   * 0xFFFFFFFF.FF800000  |-----------------------|- SEGDEBUGBASE
 443  443   *                      |      unused           |
 444  444   *                      +-----------------------+
 445  445   *                      |      Kernel Data      |
 446  446   * 0xFFFFFFFF.FBC00000  |-----------------------|
 447  447   *                      |      Kernel Text      |
 448  448   * 0xFFFFFFFF.FB800000  |-----------------------|- KERNEL_TEXT
 449  449   *                      |---    debug info   ---|- debug info (DEBUG_INFO_VA)
 450  450   *                      |---       GDT       ---|- GDT page (GDT_VA)
 451  451   *                      |---       IDT       ---|- IDT page (IDT_VA)
 452  452   *                      |---       LDT       ---|- LDT pages (LDT_VA)
 453  453   *                      |                       |
 454  454   *                      |      Core heap        | (used for loadable modules)
 455  455   * 0xFFFFFFFF.C0000000  |-----------------------|- core_base / ekernelheap
 456  456   *                      |        Kernel         |
 457  457   *                      |         heap          |
 458  458   * 0xFFFFFXXX.XXX00000  |-----------------------|- kernelheap (floating)
 459  459   *                      |        segmap         |
 460  460   * 0xFFFFFXXX.XXX00000  |-----------------------|- segmap_start (floating)
 461  461   *                      |    device mappings    |
 462  462   * 0xFFFFFXXX.XXX00000  |-----------------------|- toxic_addr (floating)
 463  463   *                      |         segzio        |
 464  464   * 0xFFFFFXXX.XXX00000  |-----------------------|- segzio_base (floating)
 465  465   *                      |         segkp         |
 466  466   * ---                  |-----------------------|- segkp_base (floating)
 467  467   *                      |   page_t structures   |  valloc_base + valloc_sz
 468  468   *                      |   memsegs, memlists,  |
 469  469   *                      |   page hash, etc.     |
 470  470   * 0xFFFFFF00.00000000  |-----------------------|- valloc_base (lower if >256GB)
 471  471   *                      |        segkpm         |
 472  472   * 0xFFFFFE00.00000000  |-----------------------|
 473  473   *                      |       Red Zone        |
 474  474   * 0xFFFFFD80.00000000  |-----------------------|- KERNELBASE (lower if >256GB)
 475  475   *                      |     User stack        |- User space memory
 476  476   *                      |                       |
 477  477   *                      | shared objects, etc   |       (grows downwards)
 478  478   *                      :                       :
 479  479   *                      |                       |
 480  480   * 0xFFFF8000.00000000  |-----------------------|
 481  481   *                      |                       |
 482  482   *                      | VA Hole / unused      |
 483  483   *                      |                       |
 484  484   * 0x00008000.00000000  |-----------------------|
 485  485   *                      |                       |
 486  486   *                      |                       |
 487  487   *                      :                       :
 488  488   *                      |       user heap       |       (grows upwards)
 489  489   *                      |                       |
 490  490   *                      |       user data       |
 491  491   *                      |-----------------------|
 492  492   *                      |       user text       |
 493  493   * 0x00000000.04000000  |-----------------------|
 494  494   *                      |       invalid         |
 495  495   * 0x00000000.00000000  +-----------------------+
 496  496   *
 497  497   * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit
 498  498   * kernel, except that userlimit is raised to 0xfe000000
 499  499   *
 500  500   * Floating values:
 501  501   *
 502  502   * valloc_base: start of the kernel's memory management/tracking data
 503  503   * structures.  This region contains page_t structures for
 504  504   * physical memory, memsegs, memlists, and the page hash.
 505  505   *
 506  506   * core_base: start of the kernel's "core" heap area on 64-bit systems.
 507  507   * This area is intended to be used for global data as well as for module
 508  508   * text/data that does not fit into the nucleus pages.  The core heap is
 509  509   * restricted to a 2GB range, allowing every address within it to be
 510  510   * accessed using rip-relative addressing
 511  511   *
 512  512   * ekernelheap: end of kernelheap and start of segmap.
 513  513   *
 514  514   * kernelheap: start of kernel heap.  On 32-bit systems, this starts right
 515  515   * above a red zone that separates the user's address space from the
 516  516   * kernel's.  On 64-bit systems, it sits above segkp and segkpm.
 517  517   *
 518  518   * segmap_start: start of segmap. The length of segmap can be modified
 519  519   * through eeprom. The default length is 16MB on 32-bit systems and 64MB
 520  520   * on 64-bit systems.
 521  521   *
 522  522   * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be
 523  523   * decreased by 2X the size required for page_t.  This allows the kernel
 524  524   * heap to grow in size with physical memory.  With sizeof(page_t) == 80
 525  525   * bytes, the following shows the values of kernelbase and kernel heap
 526  526   * sizes for different memory configurations (assuming default segmap and
 527  527   * segkp sizes).
 528  528   *
 529  529   *      mem     size for        kernelbase      kernel heap
 530  530   *      size    page_t's                        size
 531  531   *      ----    ---------       ----------      -----------
 532  532   *      1gb     0x01400000      0xd1800000      684MB
 533  533   *      2gb     0x02800000      0xcf000000      704MB
 534  534   *      4gb     0x05000000      0xca000000      744MB
 535  535   *      6gb     0x07800000      0xc5000000      784MB
 536  536   *      8gb     0x0a000000      0xc0000000      824MB
 537  537   *      16gb    0x14000000      0xac000000      984MB
 538  538   *      32gb    0x28000000      0x84000000      1304MB
 539  539   *      64gb    0x50000000      0x34000000      1944MB (*)
 540  540   *
 541  541   * kernelbase is less than the abi minimum of 0xc0000000 for memory
 542  542   * configurations above 8gb.
 543  543   *
 544  544   * (*) support for memory configurations above 32gb will require manual tuning
 545  545   * of kernelbase to balance out the need of user applications.
 546  546   */
 547  547  
 548  548  /* real-time-clock initialization parameters */
 549  549  extern time_t process_rtc_config_file(void);
 550  550  
 551  551  uintptr_t       kernelbase;
 552  552  uintptr_t       postbootkernelbase;     /* not set till boot loader is gone */
 553  553  uintptr_t       eprom_kernelbase;
 554  554  size_t          segmapsize;
 555  555  uintptr_t       segmap_start;
 556  556  int             segmapfreelists;
 557  557  pgcnt_t         npages;
 558  558  pgcnt_t         orig_npages;
 559  559  size_t          core_size;              /* size of "core" heap */
 560  560  uintptr_t       core_base;              /* base address of "core" heap */
 561  561  
 562  562  /*
 563  563   * List of bootstrap pages. We mark these as allocated in startup.
 564  564   * release_bootstrap() will free them when we're completely done with
 565  565   * the bootstrap.
 566  566   */
 567  567  static page_t *bootpages;
 568  568  
 569  569  /*
 570  570   * boot time pages that have a vnode from the ramdisk will keep that forever.
 571  571   */
 572  572  static page_t *rd_pages;
 573  573  
 574  574  /*
 575  575   * Lower 64K
 576  576   */
 577  577  static page_t *lower_pages = NULL;
 578  578  static int lower_pages_count = 0;
 579  579  
 580  580  struct system_hardware system_hardware;
 581  581  
 582  582  /*
 583  583   * Enable some debugging messages concerning memory usage...
 584  584   */
 585  585  static void
 586  586  print_memlist(char *title, struct memlist *mp)
 587  587  {
 588  588          prom_printf("MEMLIST: %s:\n", title);
 589  589          while (mp != NULL)  {
 590  590                  prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n",
 591  591                      mp->ml_address, mp->ml_size);
 592  592                  mp = mp->ml_next;
 593  593          }
 594  594  }
 595  595  
 596  596  /*
 597  597   * XX64 need a comment here.. are these just default values, surely
 598  598   * we read the "cpuid" type information to figure this out.
 599  599   */
 600  600  int     l2cache_sz = 0x80000;
 601  601  int     l2cache_linesz = 0x40;
 602  602  int     l2cache_assoc = 1;
 603  603  
 604  604  static size_t   textrepl_min_gb = 10;
 605  605  
 606  606  /*
 607  607   * on 64 bit we use a predifined VA range for mapping devices in the kernel
 608  608   * on 32 bit the mappings are intermixed in the heap, so we use a bit map
 609  609   */
 610  610  #ifdef __amd64
 611  611  
 612  612  vmem_t          *device_arena;
 613  613  uintptr_t       toxic_addr = (uintptr_t)NULL;
 614  614  size_t          toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */
 615  615  
 616  616  #else   /* __i386 */
 617  617  
 618  618  ulong_t         *toxic_bit_map; /* one bit for each 4k of VA in heap_arena */
 619  619  size_t          toxic_bit_map_len = 0;  /* in bits */
 620  620  
 621  621  #endif  /* __i386 */
 622  622  
 623  623  /*
 624  624   * Simple boot time debug facilities
 625  625   */
 626  626  static char *prm_dbg_str[] = {
 627  627          "%s:%d: '%s' is 0x%x\n",
 628  628          "%s:%d: '%s' is 0x%llx\n"
 629  629  };
 630  630  
 631  631  int prom_debug;
 632  632  
 633  633  #define PRM_DEBUG(q)    if (prom_debug)         \
 634  634          prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q);
 635  635  #define PRM_POINT(q)    if (prom_debug)         \
 636  636          prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q);
 637  637  
 638  638  /*
 639  639   * This structure is used to keep track of the intial allocations
 640  640   * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to
 641  641   * be >= the number of ADD_TO_ALLOCATIONS() executed in the code.
 642  642   */
 643  643  #define NUM_ALLOCATIONS 8
 644  644  int num_allocations = 0;
 645  645  struct {
 646  646          void **al_ptr;
 647  647          size_t al_size;
 648  648  } allocations[NUM_ALLOCATIONS];
 649  649  size_t valloc_sz = 0;
 650  650  uintptr_t valloc_base;
 651  651  
 652  652  #define ADD_TO_ALLOCATIONS(ptr, size) {                                 \
 653  653                  size = ROUND_UP_PAGE(size);                             \
 654  654                  if (num_allocations == NUM_ALLOCATIONS)                 \
 655  655                          panic("too many ADD_TO_ALLOCATIONS()");         \
 656  656                  allocations[num_allocations].al_ptr = (void**)&ptr;     \
 657  657                  allocations[num_allocations].al_size = size;            \
 658  658                  valloc_sz += size;                                      \
 659  659                  ++num_allocations;                                      \
 660  660          }
 661  661  
 662  662  /*
 663  663   * Allocate all the initial memory needed by the page allocator.
 664  664   */
 665  665  static void
 666  666  perform_allocations(void)
 667  667  {
 668  668          caddr_t mem;
 669  669          int i;
 670  670          int valloc_align;
 671  671  
 672  672          PRM_DEBUG(valloc_base);
 673  673          PRM_DEBUG(valloc_sz);
 674  674          valloc_align = mmu.level_size[mmu.max_page_level > 0];
 675  675          mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align);
 676  676          if (mem != (caddr_t)valloc_base)
 677  677                  panic("BOP_ALLOC() failed");
 678  678          bzero(mem, valloc_sz);
 679  679          for (i = 0; i < num_allocations; ++i) {
 680  680                  *allocations[i].al_ptr = (void *)mem;
 681  681                  mem += allocations[i].al_size;
 682  682          }
 683  683  }
 684  684  
 685  685  /*
 686  686   * Set up and enable SMAP now before we start other CPUs, but after the kernel's
 687  687   * VM has been set up so we can use hot_patch_kernel_text().
 688  688   *
 689  689   * We can only patch 1, 2, or 4 bytes, but not three bytes. So instead, we
  
    | 
      ↓ open down ↓ | 
    689 lines elided | 
    
      ↑ open up ↑ | 
  
 690  690   * replace the four byte word at the patch point. See uts/intel/ia32/ml/copy.s
 691  691   * for more information on what's going on here.
 692  692   */
 693  693  static void
 694  694  startup_smap(void)
 695  695  {
 696  696          int i;
 697  697          uint32_t inst;
 698  698          uint8_t *instp;
 699  699          char sym[128];
      700 +        struct modctl *modp;
 700  701  
 701  702          extern int _smap_enable_patch_count;
 702  703          extern int _smap_disable_patch_count;
 703  704  
 704  705          if (disable_smap != 0)
 705  706                  remove_x86_feature(x86_featureset, X86FSET_SMAP);
 706  707  
 707  708          if (is_x86_feature(x86_featureset, X86FSET_SMAP) == B_FALSE)
 708  709                  return;
 709  710  
 710  711          for (i = 0; i < _smap_enable_patch_count; i++) {
 711  712                  int sizep;
 712  713  
 713  714                  VERIFY3U(i, <, _smap_enable_patch_count);
 714  715                  VERIFY(snprintf(sym, sizeof (sym), "_smap_enable_patch_%d", i) <
 715  716                      sizeof (sym));
 716  717                  instp = (uint8_t *)(void *)kobj_getelfsym(sym, NULL, &sizep);
 717  718                  VERIFY(instp != 0);
 718  719                  inst = (instp[3] << 24) | (SMAP_CLAC_INSTR & 0x00ffffff);
 719  720                  hot_patch_kernel_text((caddr_t)instp, inst, 4);
 720  721          }
 721  722  
 722  723          for (i = 0; i < _smap_disable_patch_count; i++) {
  
    | 
      ↓ open down ↓ | 
    13 lines elided | 
    
      ↑ open up ↑ | 
  
 723  724                  int sizep;
 724  725  
 725  726                  VERIFY(snprintf(sym, sizeof (sym), "_smap_disable_patch_%d",
 726  727                      i) < sizeof (sym));
 727  728                  instp = (uint8_t *)(void *)kobj_getelfsym(sym, NULL, &sizep);
 728  729                  VERIFY(instp != 0);
 729  730                  inst = (instp[3] << 24) | (SMAP_STAC_INSTR & 0x00ffffff);
 730  731                  hot_patch_kernel_text((caddr_t)instp, inst, 4);
 731  732          }
 732  733  
 733      -        hot_patch_kernel_text((caddr_t)smap_enable, SMAP_CLAC_INSTR, 4);
 734      -        hot_patch_kernel_text((caddr_t)smap_disable, SMAP_STAC_INSTR, 4);
      734 +        /*
      735 +         * Hotinline calls to smap_enable and smap_disable within
      736 +         * unix module. Hotinlines in other modules are done on
      737 +         * mod_load().
      738 +         */
      739 +        modp = mod_hold_by_name("unix");
      740 +        do_hotinlines(modp->mod_mp);
      741 +        mod_release_mod(modp);
      742 +
 735  743          setcr4(getcr4() | CR4_SMAP);
 736  744          smap_enable();
 737  745  }
 738  746  
 739  747  /*
 740  748   * Our world looks like this at startup time.
 741  749   *
 742  750   * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data
 743  751   * at 0xfec00000.  On a 64-bit OS, kernel text and data are loaded at
 744  752   * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively.  Those
 745  753   * addresses are fixed in the binary at link time.
 746  754   *
 747  755   * On the text page:
 748  756   * unix/genunix/krtld/module text loads.
 749  757   *
 750  758   * On the data page:
 751  759   * unix/genunix/krtld/module data loads.
 752  760   *
 753  761   * Machine-dependent startup code
 754  762   */
 755  763  void
 756  764  startup(void)
 757  765  {
 758  766  #if !defined(__xpv)
 759  767          extern void startup_pci_bios(void);
 760  768  #endif
 761  769          extern cpuset_t cpu_ready_set;
 762  770  
 763  771          /*
 764  772           * Make sure that nobody tries to use sekpm until we have
 765  773           * initialized it properly.
 766  774           */
 767  775  #if defined(__amd64)
 768  776          kpm_desired = 1;
 769  777  #endif
 770  778          kpm_enable = 0;
 771  779          CPUSET_ONLY(cpu_ready_set, 0);  /* cpu 0 is boot cpu */
 772  780  
 773  781  #if defined(__xpv)      /* XXPV fix me! */
 774  782          {
 775  783                  extern int segvn_use_regions;
 776  784                  segvn_use_regions = 0;
 777  785          }
 778  786  #endif
 779  787          ssp_init();
 780  788          progressbar_init();
 781  789          startup_init();
 782  790  #if defined(__xpv)
 783  791          startup_xen_version();
 784  792  #endif
 785  793          startup_memlist();
 786  794          startup_kmem();
 787  795          startup_vm();
 788  796  #if !defined(__xpv)
 789  797          /*
 790  798           * Note we need to do this even on fast reboot in order to access
 791  799           * the irq routing table (used for pci labels).
 792  800           */
 793  801          startup_pci_bios();
 794  802          startup_smap();
 795  803  #endif
 796  804  #if defined(__xpv)
 797  805          startup_xen_mca();
 798  806  #endif
 799  807          startup_modules();
 800  808  
 801  809          startup_end();
 802  810  }
 803  811  
 804  812  static void
 805  813  startup_init()
 806  814  {
 807  815          PRM_POINT("startup_init() starting...");
 808  816  
 809  817          /*
 810  818           * Complete the extraction of cpuid data
 811  819           */
 812  820          cpuid_pass2(CPU);
 813  821  
 814  822          (void) check_boot_version(BOP_GETVERSION(bootops));
 815  823  
 816  824          /*
 817  825           * Check for prom_debug in boot environment
 818  826           */
 819  827          if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) {
 820  828                  ++prom_debug;
 821  829                  PRM_POINT("prom_debug found in boot enviroment");
 822  830          }
 823  831  
 824  832          /*
 825  833           * Collect node, cpu and memory configuration information.
 826  834           */
 827  835          get_system_configuration();
 828  836  
 829  837          /*
 830  838           * Halt if this is an unsupported processor.
 831  839           */
 832  840          if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) {
 833  841                  printf("\n486 processor (\"%s\") detected.\n",
 834  842                      CPU->cpu_brandstr);
 835  843                  halt("This processor is not supported by this release "
 836  844                      "of Solaris.");
 837  845          }
 838  846  
 839  847          PRM_POINT("startup_init() done");
 840  848  }
 841  849  
 842  850  /*
 843  851   * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie.
 844  852   * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it
 845  853   * also filters out physical page zero.  There is some reliance on the
 846  854   * boot loader allocating only a few contiguous physical memory chunks.
 847  855   */
 848  856  static void
 849  857  avail_filter(uint64_t *addr, uint64_t *size)
 850  858  {
 851  859          uintptr_t va;
 852  860          uintptr_t next_va;
 853  861          pfn_t pfn;
 854  862          uint64_t pfn_addr;
 855  863          uint64_t pfn_eaddr;
 856  864          uint_t prot;
 857  865          size_t len;
 858  866          uint_t change;
 859  867  
 860  868          if (prom_debug)
 861  869                  prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n",
 862  870                      *addr, *size);
 863  871  
 864  872          /*
 865  873           * page zero is required for BIOS.. never make it available
 866  874           */
 867  875          if (*addr == 0) {
 868  876                  *addr += MMU_PAGESIZE;
 869  877                  *size -= MMU_PAGESIZE;
 870  878          }
 871  879  
 872  880          /*
 873  881           * First we trim from the front of the range. Since kbm_probe()
 874  882           * walks ranges in virtual order, but addr/size are physical, we need
 875  883           * to the list until no changes are seen.  This deals with the case
 876  884           * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w
 877  885           * but w < v.
 878  886           */
 879  887          do {
 880  888                  change = 0;
 881  889                  for (va = KERNEL_TEXT;
 882  890                      *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
 883  891                      va = next_va) {
 884  892  
 885  893                          next_va = va + len;
 886  894                          pfn_addr = pfn_to_pa(pfn);
 887  895                          pfn_eaddr = pfn_addr + len;
 888  896  
 889  897                          if (pfn_addr <= *addr && pfn_eaddr > *addr) {
 890  898                                  change = 1;
 891  899                                  while (*size > 0 && len > 0) {
 892  900                                          *addr += MMU_PAGESIZE;
 893  901                                          *size -= MMU_PAGESIZE;
 894  902                                          len -= MMU_PAGESIZE;
 895  903                                  }
 896  904                          }
 897  905                  }
 898  906                  if (change && prom_debug)
 899  907                          prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n",
 900  908                              *addr, *size);
 901  909          } while (change);
 902  910  
 903  911          /*
 904  912           * Trim pages from the end of the range.
 905  913           */
 906  914          for (va = KERNEL_TEXT;
 907  915              *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0;
 908  916              va = next_va) {
 909  917  
 910  918                  next_va = va + len;
 911  919                  pfn_addr = pfn_to_pa(pfn);
 912  920  
 913  921                  if (pfn_addr >= *addr && pfn_addr < *addr + *size)
 914  922                          *size = pfn_addr - *addr;
 915  923          }
 916  924  
 917  925          if (prom_debug)
 918  926                  prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n",
 919  927                      *addr, *size);
 920  928  }
 921  929  
 922  930  static void
 923  931  kpm_init()
 924  932  {
 925  933          struct segkpm_crargs b;
 926  934  
 927  935          /*
 928  936           * These variables were all designed for sfmmu in which segkpm is
 929  937           * mapped using a single pagesize - either 8KB or 4MB.  On x86, we
 930  938           * might use 2+ page sizes on a single machine, so none of these
 931  939           * variables have a single correct value.  They are set up as if we
 932  940           * always use a 4KB pagesize, which should do no harm.  In the long
 933  941           * run, we should get rid of KPM's assumption that only a single
 934  942           * pagesize is used.
 935  943           */
 936  944          kpm_pgshft = MMU_PAGESHIFT;
 937  945          kpm_pgsz =  MMU_PAGESIZE;
 938  946          kpm_pgoff = MMU_PAGEOFFSET;
 939  947          kpmp2pshft = 0;
 940  948          kpmpnpgs = 1;
 941  949          ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
 942  950  
 943  951          PRM_POINT("about to create segkpm");
 944  952          rw_enter(&kas.a_lock, RW_WRITER);
 945  953  
 946  954          if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0)
 947  955                  panic("cannot attach segkpm");
 948  956  
 949  957          b.prot = PROT_READ | PROT_WRITE;
 950  958          b.nvcolors = 1;
 951  959  
 952  960          if (segkpm_create(segkpm, (caddr_t)&b) != 0)
 953  961                  panic("segkpm_create segkpm");
 954  962  
 955  963          rw_exit(&kas.a_lock);
 956  964  
 957  965          kpm_enable = 1;
 958  966  
 959  967          /*
 960  968           * As the KPM was disabled while setting up the system, go back and fix
 961  969           * CPU zero's access to its user page table. This is a bit gross, but
 962  970           * we have a chicken and egg problem otherwise.
 963  971           */
 964  972          ASSERT(CPU->cpu_hat_info->hci_user_l3ptes == NULL);
 965  973          CPU->cpu_hat_info->hci_user_l3ptes =
 966  974              (x86pte_t *)hat_kpm_mapin_pfn(CPU->cpu_hat_info->hci_user_l3pfn);
 967  975  }
 968  976  
 969  977  /*
 970  978   * The debug info page provides enough information to allow external
 971  979   * inspectors (e.g. when running under a hypervisor) to bootstrap
 972  980   * themselves into allowing full-blown kernel debugging.
 973  981   */
 974  982  static void
 975  983  init_debug_info(void)
 976  984  {
 977  985          caddr_t mem;
 978  986          debug_info_t *di;
 979  987  
 980  988  #ifndef __lint
 981  989          ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE);
 982  990  #endif
 983  991  
 984  992          mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE,
 985  993              MMU_PAGESIZE);
 986  994  
 987  995          if (mem != (caddr_t)DEBUG_INFO_VA)
 988  996                  panic("BOP_ALLOC() failed");
 989  997          bzero(mem, MMU_PAGESIZE);
 990  998  
 991  999          di = (debug_info_t *)mem;
 992 1000  
 993 1001          di->di_magic = DEBUG_INFO_MAGIC;
 994 1002          di->di_version = DEBUG_INFO_VERSION;
 995 1003          di->di_modules = (uintptr_t)&modules;
 996 1004          di->di_s_text = (uintptr_t)s_text;
 997 1005          di->di_e_text = (uintptr_t)e_text;
 998 1006          di->di_s_data = (uintptr_t)s_data;
 999 1007          di->di_e_data = (uintptr_t)e_data;
1000 1008          di->di_hat_htable_off = offsetof(hat_t, hat_htable);
1001 1009          di->di_ht_pfn_off = offsetof(htable_t, ht_pfn);
1002 1010  }
1003 1011  
1004 1012  /*
1005 1013   * Build the memlists and other kernel essential memory system data structures.
1006 1014   * This is everything at valloc_base.
1007 1015   */
1008 1016  static void
1009 1017  startup_memlist(void)
1010 1018  {
1011 1019          size_t memlist_sz;
1012 1020          size_t memseg_sz;
1013 1021          size_t pagehash_sz;
1014 1022          size_t pp_sz;
1015 1023          uintptr_t va;
1016 1024          size_t len;
1017 1025          uint_t prot;
1018 1026          pfn_t pfn;
1019 1027          int memblocks;
1020 1028          pfn_t rsvd_high_pfn;
1021 1029          pgcnt_t rsvd_pgcnt;
1022 1030          size_t rsvdmemlist_sz;
1023 1031          int rsvdmemblocks;
1024 1032          caddr_t pagecolor_mem;
1025 1033          size_t pagecolor_memsz;
1026 1034          caddr_t page_ctrs_mem;
1027 1035          size_t page_ctrs_size;
1028 1036          size_t pse_table_alloc_size;
1029 1037          struct memlist *current;
1030 1038          extern void startup_build_mem_nodes(struct memlist *);
1031 1039  
1032 1040          /* XX64 fix these - they should be in include files */
1033 1041          extern size_t page_coloring_init(uint_t, int, int);
1034 1042          extern void page_coloring_setup(caddr_t);
1035 1043  
1036 1044          PRM_POINT("startup_memlist() starting...");
1037 1045  
1038 1046          /*
1039 1047           * Use leftover large page nucleus text/data space for loadable modules.
1040 1048           * Use at most MODTEXT/MODDATA.
1041 1049           */
1042 1050          len = kbm_nucleus_size;
1043 1051          ASSERT(len > MMU_PAGESIZE);
1044 1052  
1045 1053          moddata = (caddr_t)ROUND_UP_PAGE(e_data);
1046 1054          e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len);
1047 1055          if (e_moddata - moddata > MODDATA)
1048 1056                  e_moddata = moddata + MODDATA;
1049 1057  
1050 1058          modtext = (caddr_t)ROUND_UP_PAGE(e_text);
1051 1059          e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len);
1052 1060          if (e_modtext - modtext > MODTEXT)
1053 1061                  e_modtext = modtext + MODTEXT;
1054 1062  
1055 1063          econtig = e_moddata;
1056 1064  
1057 1065          PRM_DEBUG(modtext);
1058 1066          PRM_DEBUG(e_modtext);
1059 1067          PRM_DEBUG(moddata);
1060 1068          PRM_DEBUG(e_moddata);
1061 1069          PRM_DEBUG(econtig);
1062 1070  
1063 1071          /*
1064 1072           * Examine the boot loader physical memory map to find out:
1065 1073           * - total memory in system - physinstalled
1066 1074           * - the max physical address - physmax
1067 1075           * - the number of discontiguous segments of memory.
1068 1076           */
1069 1077          if (prom_debug)
1070 1078                  print_memlist("boot physinstalled",
1071 1079                      bootops->boot_mem->physinstalled);
1072 1080          installed_top_size_ex(bootops->boot_mem->physinstalled, &physmax,
1073 1081              &physinstalled, &memblocks);
1074 1082          PRM_DEBUG(physmax);
1075 1083          PRM_DEBUG(physinstalled);
1076 1084          PRM_DEBUG(memblocks);
1077 1085  
1078 1086          /*
1079 1087           * Compute maximum physical address for memory DR operations.
1080 1088           * Memory DR operations are unsupported on xpv or 32bit OSes.
1081 1089           */
1082 1090  #ifdef  __amd64
1083 1091          if (plat_dr_support_memory()) {
1084 1092                  if (plat_dr_physmax == 0) {
1085 1093                          uint_t pabits = UINT_MAX;
1086 1094  
1087 1095                          cpuid_get_addrsize(CPU, &pabits, NULL);
1088 1096                          plat_dr_physmax = btop(1ULL << pabits);
1089 1097                  }
1090 1098                  if (plat_dr_physmax > PHYSMEM_MAX64)
1091 1099                          plat_dr_physmax = PHYSMEM_MAX64;
1092 1100          } else
1093 1101  #endif
1094 1102                  plat_dr_physmax = 0;
1095 1103  
1096 1104          /*
1097 1105           * Examine the bios reserved memory to find out:
1098 1106           * - the number of discontiguous segments of memory.
1099 1107           */
1100 1108          if (prom_debug)
1101 1109                  print_memlist("boot reserved mem",
1102 1110                      bootops->boot_mem->rsvdmem);
1103 1111          installed_top_size_ex(bootops->boot_mem->rsvdmem, &rsvd_high_pfn,
1104 1112              &rsvd_pgcnt, &rsvdmemblocks);
1105 1113          PRM_DEBUG(rsvd_high_pfn);
1106 1114          PRM_DEBUG(rsvd_pgcnt);
1107 1115          PRM_DEBUG(rsvdmemblocks);
1108 1116  
1109 1117          /*
1110 1118           * Initialize hat's mmu parameters.
1111 1119           * Check for enforce-prot-exec in boot environment. It's used to
1112 1120           * enable/disable support for the page table entry NX bit.
1113 1121           * The default is to enforce PROT_EXEC on processors that support NX.
1114 1122           * Boot seems to round up the "len", but 8 seems to be big enough.
1115 1123           */
1116 1124          mmu_init();
1117 1125  
1118 1126  #ifdef  __i386
1119 1127          /*
1120 1128           * physmax is lowered if there is more memory than can be
1121 1129           * physically addressed in 32 bit (PAE/non-PAE) modes.
1122 1130           */
1123 1131          if (mmu.pae_hat) {
1124 1132                  if (PFN_ABOVE64G(physmax)) {
1125 1133                          physinstalled -= (physmax - (PFN_64G - 1));
1126 1134                          physmax = PFN_64G - 1;
1127 1135                  }
1128 1136          } else {
1129 1137                  if (PFN_ABOVE4G(physmax)) {
1130 1138                          physinstalled -= (physmax - (PFN_4G - 1));
1131 1139                          physmax = PFN_4G - 1;
1132 1140                  }
1133 1141          }
1134 1142  #endif
1135 1143  
1136 1144          startup_build_mem_nodes(bootops->boot_mem->physinstalled);
1137 1145  
1138 1146          if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) {
1139 1147                  int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec");
1140 1148                  char value[8];
1141 1149  
1142 1150                  if (len < 8)
1143 1151                          (void) BOP_GETPROP(bootops, "enforce-prot-exec", value);
1144 1152                  else
1145 1153                          (void) strcpy(value, "");
1146 1154                  if (strcmp(value, "off") == 0)
1147 1155                          mmu.pt_nx = 0;
1148 1156          }
1149 1157          PRM_DEBUG(mmu.pt_nx);
1150 1158  
1151 1159          /*
1152 1160           * We will need page_t's for every page in the system, except for
1153 1161           * memory mapped at or above above the start of the kernel text segment.
1154 1162           *
1155 1163           * pages above e_modtext are attributed to kernel debugger (obp_pages)
1156 1164           */
1157 1165          npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */
1158 1166          obp_pages = 0;
1159 1167          va = KERNEL_TEXT;
1160 1168          while (kbm_probe(&va, &len, &pfn, &prot) != 0) {
1161 1169                  npages -= len >> MMU_PAGESHIFT;
1162 1170                  if (va >= (uintptr_t)e_moddata)
1163 1171                          obp_pages += len >> MMU_PAGESHIFT;
1164 1172                  va += len;
1165 1173          }
1166 1174          PRM_DEBUG(npages);
1167 1175          PRM_DEBUG(obp_pages);
1168 1176  
1169 1177          /*
1170 1178           * If physmem is patched to be non-zero, use it instead of the computed
1171 1179           * value unless it is larger than the actual amount of memory on hand.
1172 1180           */
1173 1181          if (physmem == 0 || physmem > npages) {
1174 1182                  physmem = npages;
1175 1183          } else if (physmem < npages) {
1176 1184                  orig_npages = npages;
1177 1185                  npages = physmem;
1178 1186          }
1179 1187          PRM_DEBUG(physmem);
1180 1188  
1181 1189          /*
1182 1190           * We now compute the sizes of all the  initial allocations for
1183 1191           * structures the kernel needs in order do kmem_alloc(). These
1184 1192           * include:
1185 1193           *      memsegs
1186 1194           *      memlists
1187 1195           *      page hash table
1188 1196           *      page_t's
1189 1197           *      page coloring data structs
1190 1198           */
1191 1199          memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS);
1192 1200          ADD_TO_ALLOCATIONS(memseg_base, memseg_sz);
1193 1201          PRM_DEBUG(memseg_sz);
1194 1202  
1195 1203          /*
1196 1204           * Reserve space for memlists. There's no real good way to know exactly
1197 1205           * how much room we'll need, but this should be a good upper bound.
1198 1206           */
1199 1207          memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
1200 1208              (memblocks + POSS_NEW_FRAGMENTS));
1201 1209          ADD_TO_ALLOCATIONS(memlist, memlist_sz);
1202 1210          PRM_DEBUG(memlist_sz);
1203 1211  
1204 1212          /*
1205 1213           * Reserve space for bios reserved memlists.
1206 1214           */
1207 1215          rsvdmemlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) *
1208 1216              (rsvdmemblocks + POSS_NEW_FRAGMENTS));
1209 1217          ADD_TO_ALLOCATIONS(bios_rsvd, rsvdmemlist_sz);
1210 1218          PRM_DEBUG(rsvdmemlist_sz);
1211 1219  
1212 1220          /* LINTED */
1213 1221          ASSERT(P2SAMEHIGHBIT((1 << PP_SHIFT), sizeof (struct page)));
1214 1222          /*
1215 1223           * The page structure hash table size is a power of 2
1216 1224           * such that the average hash chain length is PAGE_HASHAVELEN.
1217 1225           */
1218 1226          page_hashsz = npages / PAGE_HASHAVELEN;
1219 1227          page_hashsz_shift = highbit(page_hashsz);
1220 1228          page_hashsz = 1 << page_hashsz_shift;
1221 1229          pagehash_sz = sizeof (struct page *) * page_hashsz;
1222 1230          ADD_TO_ALLOCATIONS(page_hash, pagehash_sz);
1223 1231          PRM_DEBUG(pagehash_sz);
1224 1232  
1225 1233          /*
1226 1234           * Set aside room for the page structures themselves.
1227 1235           */
1228 1236          PRM_DEBUG(npages);
1229 1237          pp_sz = sizeof (struct page) * npages;
1230 1238          ADD_TO_ALLOCATIONS(pp_base, pp_sz);
1231 1239          PRM_DEBUG(pp_sz);
1232 1240  
1233 1241          /*
1234 1242           * determine l2 cache info and memory size for page coloring
1235 1243           */
1236 1244          (void) getl2cacheinfo(CPU,
1237 1245              &l2cache_sz, &l2cache_linesz, &l2cache_assoc);
1238 1246          pagecolor_memsz =
1239 1247              page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc);
1240 1248          ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz);
1241 1249          PRM_DEBUG(pagecolor_memsz);
1242 1250  
1243 1251          page_ctrs_size = page_ctrs_sz();
1244 1252          ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size);
1245 1253          PRM_DEBUG(page_ctrs_size);
1246 1254  
1247 1255          /*
1248 1256           * Allocate the array that protects pp->p_selock.
1249 1257           */
1250 1258          pse_shift = size_pse_array(physmem, max_ncpus);
1251 1259          pse_table_size = 1 << pse_shift;
1252 1260          pse_table_alloc_size = pse_table_size * sizeof (pad_mutex_t);
1253 1261          ADD_TO_ALLOCATIONS(pse_mutex, pse_table_alloc_size);
1254 1262  
1255 1263  #if defined(__amd64)
1256 1264          valloc_sz = ROUND_UP_LPAGE(valloc_sz);
1257 1265          valloc_base = VALLOC_BASE;
1258 1266  
1259 1267          /*
1260 1268           * The default values of VALLOC_BASE and SEGKPM_BASE should work
1261 1269           * for values of physmax up to 256GB (1/4 TB). They need adjusting when
1262 1270           * memory is at addresses above 256GB. When adjusted, segkpm_base must
1263 1271           * be aligned on KERNEL_REDZONE_SIZE boundary (span of top level pte).
1264 1272           *
1265 1273           * In the general case (>256GB), we use (4 * physmem) for the
1266 1274           * kernel's virtual addresses, which is divided approximately
1267 1275           * as follows:
1268 1276           *  - 1 * physmem for segkpm
1269 1277           *  - 1.5 * physmem for segzio
1270 1278           *  - 1.5 * physmem for heap
1271 1279           * Total: 4.0 * physmem
1272 1280           *
1273 1281           * Note that the segzio and heap sizes are more than physmem so that
1274 1282           * VA fragmentation does not prevent either of them from being
1275 1283           * able to use nearly all of physmem.  The value of 1.5x is determined
1276 1284           * experimentally and may need to change if the workload changes.
1277 1285           */
1278 1286          if (physmax + 1 > mmu_btop(TERABYTE / 4) ||
1279 1287              plat_dr_physmax > mmu_btop(TERABYTE / 4)) {
1280 1288                  uint64_t kpm_resv_amount = mmu_ptob(physmax + 1);
1281 1289  
1282 1290                  if (kpm_resv_amount < mmu_ptob(plat_dr_physmax)) {
1283 1291                          kpm_resv_amount = mmu_ptob(plat_dr_physmax);
1284 1292                  }
1285 1293  
1286 1294                  /*
1287 1295                   * This is what actually controls the KVA : UVA split.
1288 1296                   * The kernel uses high VA, and this is lowering the
1289 1297                   * boundary, thus increasing the amount of VA for the kernel.
1290 1298                   * This gives the kernel 4 * (amount of physical memory) VA.
1291 1299                   *
1292 1300                   * The maximum VA is UINT64_MAX and we are using
1293 1301                   * 64-bit 2's complement math, so e.g. if you have 512GB
1294 1302                   * of memory, segkpm_base = -(4 * 512GB) == -2TB ==
1295 1303                   * UINT64_MAX - 2TB (approximately).  So the kernel's
1296 1304                   * VA is [UINT64_MAX-2TB to UINT64_MAX].
1297 1305                   */
1298 1306                  segkpm_base = -(P2ROUNDUP((4 * kpm_resv_amount),
1299 1307                      KERNEL_REDZONE_SIZE));
1300 1308  
1301 1309                  /* make sure we leave some space for user apps above hole */
1302 1310                  segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE);
1303 1311                  if (segkpm_base > SEGKPM_BASE)
1304 1312                          segkpm_base = SEGKPM_BASE;
1305 1313                  PRM_DEBUG(segkpm_base);
1306 1314  
1307 1315                  valloc_base = segkpm_base + P2ROUNDUP(kpm_resv_amount, ONE_GIG);
1308 1316                  if (valloc_base < segkpm_base)
1309 1317                          panic("not enough kernel VA to support memory size");
1310 1318                  PRM_DEBUG(valloc_base);
1311 1319          }
1312 1320  #else   /* __i386 */
1313 1321          valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz);
1314 1322          valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]);
1315 1323          PRM_DEBUG(valloc_base);
1316 1324  #endif  /* __i386 */
1317 1325  
1318 1326          /*
1319 1327           * do all the initial allocations
1320 1328           */
1321 1329          perform_allocations();
1322 1330  
1323 1331          /*
1324 1332           * Build phys_install and phys_avail in kernel memspace.
1325 1333           * - phys_install should be all memory in the system.
1326 1334           * - phys_avail is phys_install minus any memory mapped before this
1327 1335           *    point above KERNEL_TEXT.
1328 1336           */
1329 1337          current = phys_install = memlist;
1330 1338          copy_memlist_filter(bootops->boot_mem->physinstalled, ¤t, NULL);
1331 1339          if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1332 1340                  panic("physinstalled was too big!");
1333 1341          if (prom_debug)
1334 1342                  print_memlist("phys_install", phys_install);
1335 1343  
1336 1344          phys_avail = current;
1337 1345          PRM_POINT("Building phys_avail:\n");
1338 1346          copy_memlist_filter(bootops->boot_mem->physinstalled, ¤t,
1339 1347              avail_filter);
1340 1348          if ((caddr_t)current > (caddr_t)memlist + memlist_sz)
1341 1349                  panic("physavail was too big!");
1342 1350          if (prom_debug)
1343 1351                  print_memlist("phys_avail", phys_avail);
1344 1352  #ifndef __xpv
1345 1353          /*
1346 1354           * Free unused memlist items, which may be used by memory DR driver
1347 1355           * at runtime.
1348 1356           */
1349 1357          if ((caddr_t)current < (caddr_t)memlist + memlist_sz) {
1350 1358                  memlist_free_block((caddr_t)current,
1351 1359                      (caddr_t)memlist + memlist_sz - (caddr_t)current);
1352 1360          }
1353 1361  #endif
1354 1362  
1355 1363          /*
1356 1364           * Build bios reserved memspace
1357 1365           */
1358 1366          current = bios_rsvd;
1359 1367          copy_memlist_filter(bootops->boot_mem->rsvdmem, ¤t, NULL);
1360 1368          if ((caddr_t)current > (caddr_t)bios_rsvd + rsvdmemlist_sz)
1361 1369                  panic("bios_rsvd was too big!");
1362 1370          if (prom_debug)
1363 1371                  print_memlist("bios_rsvd", bios_rsvd);
1364 1372  #ifndef __xpv
1365 1373          /*
1366 1374           * Free unused memlist items, which may be used by memory DR driver
1367 1375           * at runtime.
1368 1376           */
1369 1377          if ((caddr_t)current < (caddr_t)bios_rsvd + rsvdmemlist_sz) {
1370 1378                  memlist_free_block((caddr_t)current,
1371 1379                      (caddr_t)bios_rsvd + rsvdmemlist_sz - (caddr_t)current);
1372 1380          }
1373 1381  #endif
1374 1382  
1375 1383          /*
1376 1384           * setup page coloring
1377 1385           */
1378 1386          page_coloring_setup(pagecolor_mem);
1379 1387          page_lock_init();       /* currently a no-op */
1380 1388  
1381 1389          /*
1382 1390           * free page list counters
1383 1391           */
1384 1392          (void) page_ctrs_alloc(page_ctrs_mem);
1385 1393  
1386 1394          /*
1387 1395           * Size the pcf array based on the number of cpus in the box at
1388 1396           * boot time.
1389 1397           */
1390 1398  
1391 1399          pcf_init();
1392 1400  
1393 1401          /*
1394 1402           * Initialize the page structures from the memory lists.
1395 1403           */
1396 1404          availrmem_initial = availrmem = freemem = 0;
1397 1405          PRM_POINT("Calling kphysm_init()...");
1398 1406          npages = kphysm_init(pp_base, npages);
1399 1407          PRM_POINT("kphysm_init() done");
1400 1408          PRM_DEBUG(npages);
1401 1409  
1402 1410          init_debug_info();
1403 1411  
1404 1412          /*
1405 1413           * Now that page_t's have been initialized, remove all the
1406 1414           * initial allocation pages from the kernel free page lists.
1407 1415           */
1408 1416          boot_mapin((caddr_t)valloc_base, valloc_sz);
1409 1417          boot_mapin((caddr_t)MISC_VA_BASE, MISC_VA_SIZE);
1410 1418          PRM_POINT("startup_memlist() done");
1411 1419  
1412 1420          PRM_DEBUG(valloc_sz);
1413 1421  
1414 1422  #if defined(__amd64)
1415 1423          if ((availrmem >> (30 - MMU_PAGESHIFT)) >=
1416 1424              textrepl_min_gb && l2cache_sz <= 2 << 20) {
1417 1425                  extern size_t textrepl_size_thresh;
1418 1426                  textrepl_size_thresh = (16 << 20) - 1;
1419 1427          }
1420 1428  #endif
1421 1429  }
1422 1430  
1423 1431  /*
1424 1432   * Layout the kernel's part of address space and initialize kmem allocator.
1425 1433   */
1426 1434  static void
1427 1435  startup_kmem(void)
1428 1436  {
1429 1437          extern void page_set_colorequiv_arr(void);
1430 1438  #if !defined(__xpv)
1431 1439          extern uint64_t kpti_kbase;
1432 1440  #endif
1433 1441  
1434 1442          PRM_POINT("startup_kmem() starting...");
1435 1443  
1436 1444  #if defined(__amd64)
1437 1445          if (eprom_kernelbase && eprom_kernelbase != KERNELBASE)
1438 1446                  cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit "
1439 1447                      "systems.");
1440 1448          kernelbase = segkpm_base - KERNEL_REDZONE_SIZE;
1441 1449          core_base = (uintptr_t)COREHEAP_BASE;
1442 1450          core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE;
1443 1451  #else   /* __i386 */
1444 1452          /*
1445 1453           * We configure kernelbase based on:
1446 1454           *
1447 1455           * 1. user specified kernelbase via eeprom command. Value cannot exceed
1448 1456           *    KERNELBASE_MAX. we large page align eprom_kernelbase
1449 1457           *
1450 1458           * 2. Default to KERNELBASE and adjust to 2X less the size for page_t.
1451 1459           *    On large memory systems we must lower kernelbase to allow
1452 1460           *    enough room for page_t's for all of memory.
1453 1461           *
1454 1462           * The value set here, might be changed a little later.
1455 1463           */
1456 1464          if (eprom_kernelbase) {
1457 1465                  kernelbase = eprom_kernelbase & mmu.level_mask[1];
1458 1466                  if (kernelbase > KERNELBASE_MAX)
1459 1467                          kernelbase = KERNELBASE_MAX;
1460 1468          } else {
1461 1469                  kernelbase = (uintptr_t)KERNELBASE;
1462 1470                  kernelbase -= ROUND_UP_4MEG(2 * valloc_sz);
1463 1471          }
1464 1472          ASSERT((kernelbase & mmu.level_offset[1]) == 0);
1465 1473          core_base = valloc_base;
1466 1474          core_size = 0;
1467 1475  #endif  /* __i386 */
1468 1476  
1469 1477          PRM_DEBUG(core_base);
1470 1478          PRM_DEBUG(core_size);
1471 1479          PRM_DEBUG(kernelbase);
1472 1480  
1473 1481  #if defined(__i386)
1474 1482          segkp_fromheap = 1;
1475 1483  #endif  /* __i386 */
1476 1484  
1477 1485          ekernelheap = (char *)core_base;
1478 1486          PRM_DEBUG(ekernelheap);
1479 1487  
1480 1488          /*
1481 1489           * Now that we know the real value of kernelbase,
1482 1490           * update variables that were initialized with a value of
1483 1491           * KERNELBASE (in common/conf/param.c).
1484 1492           *
1485 1493           * XXX  The problem with this sort of hackery is that the
1486 1494           *      compiler just may feel like putting the const declarations
1487 1495           *      (in param.c) into the .text section.  Perhaps they should
1488 1496           *      just be declared as variables there?
1489 1497           */
1490 1498  
1491 1499          *(uintptr_t *)&_kernelbase = kernelbase;
1492 1500          *(uintptr_t *)&_userlimit = kernelbase;
1493 1501  #if defined(__amd64)
1494 1502          *(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT;
1495 1503  #if !defined(__xpv)
1496 1504          kpti_kbase = kernelbase;
1497 1505  #endif
1498 1506  #else
1499 1507          *(uintptr_t *)&_userlimit32 = _userlimit;
1500 1508  #endif
1501 1509          PRM_DEBUG(_kernelbase);
1502 1510          PRM_DEBUG(_userlimit);
1503 1511          PRM_DEBUG(_userlimit32);
1504 1512  
1505 1513          /* We have to re-do this now that we've modified _userlimit. */
1506 1514          mmu_calc_user_slots();
1507 1515  
1508 1516          layout_kernel_va();
1509 1517  
1510 1518  #if defined(__i386)
1511 1519          /*
1512 1520           * If segmap is too large we can push the bottom of the kernel heap
1513 1521           * higher than the base.  Or worse, it could exceed the top of the
1514 1522           * VA space entirely, causing it to wrap around.
1515 1523           */
1516 1524          if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase)
1517 1525                  panic("too little address space available for kernelheap,"
1518 1526                      " use eeprom for lower kernelbase or smaller segmapsize");
1519 1527  #endif  /* __i386 */
1520 1528  
1521 1529          /*
1522 1530           * Initialize the kernel heap. Note 3rd argument must be > 1st.
1523 1531           */
1524 1532          kernelheap_init(kernelheap, ekernelheap,
1525 1533              kernelheap + MMU_PAGESIZE,
1526 1534              (void *)core_base, (void *)(core_base + core_size));
1527 1535  
1528 1536  #if defined(__xpv)
1529 1537          /*
1530 1538           * Link pending events struct into cpu struct
1531 1539           */
1532 1540          CPU->cpu_m.mcpu_evt_pend = &cpu0_evt_data;
1533 1541  #endif
1534 1542          /*
1535 1543           * Initialize kernel memory allocator.
1536 1544           */
1537 1545          kmem_init();
1538 1546  
1539 1547          /*
1540 1548           * Factor in colorequiv to check additional 'equivalent' bins
1541 1549           */
1542 1550          page_set_colorequiv_arr();
1543 1551  
1544 1552          /*
1545 1553           * print this out early so that we know what's going on
1546 1554           */
1547 1555          print_x86_featureset(x86_featureset);
1548 1556  
1549 1557          /*
1550 1558           * Initialize bp_mapin().
1551 1559           */
1552 1560          bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK);
1553 1561  
1554 1562          /*
1555 1563           * orig_npages is non-zero if physmem has been configured for less
1556 1564           * than the available memory.
1557 1565           */
1558 1566          if (orig_npages) {
1559 1567                  cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages",
1560 1568                      (npages == PHYSMEM ? "Due to virtual address space " : ""),
1561 1569                      npages, orig_npages);
1562 1570          }
1563 1571  #if defined(__i386)
1564 1572          if (eprom_kernelbase && (eprom_kernelbase != kernelbase))
1565 1573                  cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, "
1566 1574                      "System using 0x%lx",
1567 1575                      (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase);
1568 1576  #endif
1569 1577  
1570 1578  #ifdef  KERNELBASE_ABI_MIN
1571 1579          if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) {
1572 1580                  cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not "
1573 1581                      "i386 ABI compliant.", (uintptr_t)kernelbase);
1574 1582          }
1575 1583  #endif
1576 1584  
1577 1585  #ifndef __xpv
1578 1586          if (plat_dr_support_memory()) {
1579 1587                  mem_config_init();
1580 1588          }
1581 1589  #else   /* __xpv */
1582 1590          /*
1583 1591           * Some of the xen start information has to be relocated up
1584 1592           * into the kernel's permanent address space.
1585 1593           */
1586 1594          PRM_POINT("calling xen_relocate_start_info()");
1587 1595          xen_relocate_start_info();
1588 1596          PRM_POINT("xen_relocate_start_info() done");
1589 1597  
1590 1598          /*
1591 1599           * (Update the vcpu pointer in our cpu structure to point into
1592 1600           * the relocated shared info.)
1593 1601           */
1594 1602          CPU->cpu_m.mcpu_vcpu_info =
1595 1603              &HYPERVISOR_shared_info->vcpu_info[CPU->cpu_id];
1596 1604  #endif  /* __xpv */
1597 1605  
1598 1606          PRM_POINT("startup_kmem() done");
1599 1607  }
1600 1608  
1601 1609  #ifndef __xpv
1602 1610  /*
1603 1611   * If we have detected that we are running in an HVM environment, we need
1604 1612   * to prepend the PV driver directory to the module search path.
1605 1613   */
1606 1614  #define HVM_MOD_DIR "/platform/i86hvm/kernel"
1607 1615  static void
1608 1616  update_default_path()
1609 1617  {
1610 1618          char *current, *newpath;
1611 1619          int newlen;
1612 1620  
1613 1621          /*
1614 1622           * We are about to resync with krtld.  krtld will reset its
1615 1623           * internal module search path iff Solaris has set default_path.
1616 1624           * We want to be sure we're prepending this new directory to the
1617 1625           * right search path.
1618 1626           */
1619 1627          current = (default_path == NULL) ? kobj_module_path : default_path;
1620 1628  
1621 1629          newlen = strlen(HVM_MOD_DIR) + strlen(current) + 2;
1622 1630          newpath = kmem_alloc(newlen, KM_SLEEP);
1623 1631          (void) strcpy(newpath, HVM_MOD_DIR);
1624 1632          (void) strcat(newpath, " ");
1625 1633          (void) strcat(newpath, current);
1626 1634  
1627 1635          default_path = newpath;
1628 1636  }
1629 1637  #endif
1630 1638  
1631 1639  static void
1632 1640  startup_modules(void)
1633 1641  {
1634 1642          int cnt;
1635 1643          extern void prom_setup(void);
1636 1644          int32_t v, h;
1637 1645          char d[11];
1638 1646          char *cp;
1639 1647          cmi_hdl_t hdl;
1640 1648  
1641 1649          PRM_POINT("startup_modules() starting...");
1642 1650  
1643 1651  #ifndef __xpv
1644 1652          /*
1645 1653           * Initialize ten-micro second timer so that drivers will
1646 1654           * not get short changed in their init phase. This was
1647 1655           * not getting called until clkinit which, on fast cpu's
1648 1656           * caused the drv_usecwait to be way too short.
1649 1657           */
1650 1658          microfind();
1651 1659  
1652 1660          if ((get_hwenv() & HW_XEN_HVM) != 0)
1653 1661                  update_default_path();
1654 1662  #endif
1655 1663  
1656 1664          /*
1657 1665           * Read the GMT lag from /etc/rtc_config.
1658 1666           */
1659 1667          sgmtl(process_rtc_config_file());
1660 1668  
1661 1669          /*
1662 1670           * Calculate default settings of system parameters based upon
1663 1671           * maxusers, yet allow to be overridden via the /etc/system file.
1664 1672           */
1665 1673          param_calc(0);
1666 1674  
1667 1675          mod_setup();
1668 1676  
1669 1677          /*
1670 1678           * Initialize system parameters.
1671 1679           */
1672 1680          param_init();
1673 1681  
1674 1682          /*
1675 1683           * Initialize the default brands
1676 1684           */
1677 1685          brand_init();
1678 1686  
1679 1687          /*
1680 1688           * maxmem is the amount of physical memory we're playing with.
1681 1689           */
1682 1690          maxmem = physmem;
1683 1691  
1684 1692          /*
1685 1693           * Initialize segment management stuff.
1686 1694           */
1687 1695          seg_init();
1688 1696  
1689 1697          if (modload("fs", "specfs") == -1)
1690 1698                  halt("Can't load specfs");
1691 1699  
1692 1700          if (modload("fs", "devfs") == -1)
1693 1701                  halt("Can't load devfs");
1694 1702  
1695 1703          if (modload("fs", "dev") == -1)
1696 1704                  halt("Can't load dev");
1697 1705  
1698 1706          if (modload("fs", "procfs") == -1)
1699 1707                  halt("Can't load procfs");
1700 1708  
1701 1709          (void) modloadonly("sys", "lbl_edition");
1702 1710  
1703 1711          dispinit();
1704 1712  
1705 1713          /* Read cluster configuration data. */
1706 1714          clconf_init();
1707 1715  
1708 1716  #if defined(__xpv)
1709 1717          (void) ec_init();
1710 1718          gnttab_init();
1711 1719          (void) xs_early_init();
1712 1720  #endif /* __xpv */
1713 1721  
1714 1722          /*
1715 1723           * Create a kernel device tree. First, create rootnex and
1716 1724           * then invoke bus specific code to probe devices.
1717 1725           */
1718 1726          setup_ddi();
1719 1727  
1720 1728  #ifdef __xpv
1721 1729          if (DOMAIN_IS_INITDOMAIN(xen_info))
1722 1730  #endif
1723 1731          {
1724 1732                  id_t smid;
1725 1733                  smbios_system_t smsys;
1726 1734                  smbios_info_t sminfo;
1727 1735                  char *mfg;
1728 1736                  /*
1729 1737                   * Load the System Management BIOS into the global ksmbios
1730 1738                   * handle, if an SMBIOS is present on this system.
1731 1739                   * Also set "si-hw-provider" property, if not already set.
1732 1740                   */
1733 1741                  ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL);
1734 1742                  if (ksmbios != NULL &&
1735 1743                      ((smid = smbios_info_system(ksmbios, &smsys)) != SMB_ERR) &&
1736 1744                      (smbios_info_common(ksmbios, smid, &sminfo)) != SMB_ERR) {
1737 1745                          mfg = (char *)sminfo.smbi_manufacturer;
1738 1746                          if (BOP_GETPROPLEN(bootops, "si-hw-provider") < 0) {
1739 1747                                  extern char hw_provider[];
1740 1748                                  int i;
1741 1749                                  for (i = 0; i < SYS_NMLN; i++) {
1742 1750                                          if (isprint(mfg[i]))
1743 1751                                                  hw_provider[i] = mfg[i];
1744 1752                                          else {
1745 1753                                                  hw_provider[i] = '\0';
1746 1754                                                  break;
1747 1755                                          }
1748 1756                                  }
1749 1757                                  hw_provider[SYS_NMLN - 1] = '\0';
1750 1758                          }
1751 1759                  }
1752 1760          }
1753 1761  
1754 1762  
1755 1763          /*
1756 1764           * Originally clconf_init() apparently needed the hostid.  But
1757 1765           * this no longer appears to be true - it uses its own nodeid.
1758 1766           * By placing the hostid logic here, we are able to make use of
1759 1767           * the SMBIOS UUID.
1760 1768           */
1761 1769          if ((h = set_soft_hostid()) == HW_INVALID_HOSTID) {
1762 1770                  cmn_err(CE_WARN, "Unable to set hostid");
1763 1771          } else {
1764 1772                  for (v = h, cnt = 0; cnt < 10; cnt++) {
1765 1773                          d[cnt] = (char)(v % 10);
1766 1774                          v /= 10;
1767 1775                          if (v == 0)
1768 1776                                  break;
1769 1777                  }
1770 1778                  for (cp = hw_serial; cnt >= 0; cnt--)
1771 1779                          *cp++ = d[cnt] + '0';
1772 1780                  *cp = 0;
1773 1781          }
1774 1782  
1775 1783          /*
1776 1784           * Set up the CPU module subsystem for the boot cpu in the native
1777 1785           * case, and all physical cpu resource in the xpv dom0 case.
1778 1786           * Modifies the device tree, so this must be done after
1779 1787           * setup_ddi().
1780 1788           */
1781 1789  #ifdef __xpv
1782 1790          /*
1783 1791           * If paravirtualized and on dom0 then we initialize all physical
1784 1792           * cpu handles now;  if paravirtualized on a domU then do not
1785 1793           * initialize.
1786 1794           */
1787 1795          if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1788 1796                  xen_mc_lcpu_cookie_t cpi;
1789 1797  
1790 1798                  for (cpi = xen_physcpu_next(NULL); cpi != NULL;
1791 1799                      cpi = xen_physcpu_next(cpi)) {
1792 1800                          if ((hdl = cmi_init(CMI_HDL_SOLARIS_xVM_MCA,
1793 1801                              xen_physcpu_chipid(cpi), xen_physcpu_coreid(cpi),
1794 1802                              xen_physcpu_strandid(cpi))) != NULL &&
1795 1803                              is_x86_feature(x86_featureset, X86FSET_MCA))
1796 1804                                  cmi_mca_init(hdl);
1797 1805                  }
1798 1806          }
1799 1807  #else
1800 1808          /*
1801 1809           * Initialize a handle for the boot cpu - others will initialize
1802 1810           * as they startup.
1803 1811           */
1804 1812          if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
1805 1813              cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
1806 1814                  if (is_x86_feature(x86_featureset, X86FSET_MCA))
1807 1815                          cmi_mca_init(hdl);
1808 1816                  CPU->cpu_m.mcpu_cmi_hdl = hdl;
1809 1817          }
1810 1818  #endif  /* __xpv */
1811 1819  
1812 1820          /*
1813 1821           * Fake a prom tree such that /dev/openprom continues to work
1814 1822           */
1815 1823          PRM_POINT("startup_modules: calling prom_setup...");
1816 1824          prom_setup();
1817 1825          PRM_POINT("startup_modules: done");
1818 1826  
1819 1827          /*
1820 1828           * Load all platform specific modules
1821 1829           */
1822 1830          PRM_POINT("startup_modules: calling psm_modload...");
1823 1831          psm_modload();
1824 1832  
1825 1833          PRM_POINT("startup_modules() done");
1826 1834  }
1827 1835  
1828 1836  /*
1829 1837   * claim a "setaside" boot page for use in the kernel
1830 1838   */
1831 1839  page_t *
1832 1840  boot_claim_page(pfn_t pfn)
1833 1841  {
1834 1842          page_t *pp;
1835 1843  
1836 1844          pp = page_numtopp_nolock(pfn);
1837 1845          ASSERT(pp != NULL);
1838 1846  
1839 1847          if (PP_ISBOOTPAGES(pp)) {
1840 1848                  if (pp->p_next != NULL)
1841 1849                          pp->p_next->p_prev = pp->p_prev;
1842 1850                  if (pp->p_prev == NULL)
1843 1851                          bootpages = pp->p_next;
1844 1852                  else
1845 1853                          pp->p_prev->p_next = pp->p_next;
1846 1854          } else {
1847 1855                  /*
1848 1856                   * htable_attach() expects a base pagesize page
1849 1857                   */
1850 1858                  if (pp->p_szc != 0)
1851 1859                          page_boot_demote(pp);
1852 1860                  pp = page_numtopp(pfn, SE_EXCL);
1853 1861          }
1854 1862          return (pp);
1855 1863  }
1856 1864  
1857 1865  /*
1858 1866   * Walk through the pagetables looking for pages mapped in by boot.  If the
1859 1867   * setaside flag is set the pages are expected to be returned to the
1860 1868   * kernel later in boot, so we add them to the bootpages list.
1861 1869   */
1862 1870  static void
1863 1871  protect_boot_range(uintptr_t low, uintptr_t high, int setaside)
1864 1872  {
1865 1873          uintptr_t va = low;
1866 1874          size_t len;
1867 1875          uint_t prot;
1868 1876          pfn_t pfn;
1869 1877          page_t *pp;
1870 1878          pgcnt_t boot_protect_cnt = 0;
1871 1879  
1872 1880          while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) {
1873 1881                  if (va + len >= high)
1874 1882                          panic("0x%lx byte mapping at 0x%p exceeds boot's "
1875 1883                              "legal range.", len, (void *)va);
1876 1884  
1877 1885                  while (len > 0) {
1878 1886                          pp = page_numtopp_alloc(pfn);
1879 1887                          if (pp != NULL) {
1880 1888                                  if (setaside == 0)
1881 1889                                          panic("Unexpected mapping by boot.  "
1882 1890                                              "addr=%p pfn=%lx\n",
1883 1891                                              (void *)va, pfn);
1884 1892  
1885 1893                                  pp->p_next = bootpages;
1886 1894                                  pp->p_prev = NULL;
1887 1895                                  PP_SETBOOTPAGES(pp);
1888 1896                                  if (bootpages != NULL) {
1889 1897                                          bootpages->p_prev = pp;
1890 1898                                  }
1891 1899                                  bootpages = pp;
1892 1900                                  ++boot_protect_cnt;
1893 1901                          }
1894 1902  
1895 1903                          ++pfn;
1896 1904                          len -= MMU_PAGESIZE;
1897 1905                          va += MMU_PAGESIZE;
1898 1906                  }
1899 1907          }
1900 1908          PRM_DEBUG(boot_protect_cnt);
1901 1909  }
1902 1910  
1903 1911  /*
1904 1912   *
1905 1913   */
1906 1914  static void
1907 1915  layout_kernel_va(void)
1908 1916  {
1909 1917          PRM_POINT("layout_kernel_va() starting...");
1910 1918          /*
1911 1919           * Establish the final size of the kernel's heap, size of segmap,
1912 1920           * segkp, etc.
1913 1921           */
1914 1922  
1915 1923  #if defined(__amd64)
1916 1924  
1917 1925          kpm_vbase = (caddr_t)segkpm_base;
1918 1926          if (physmax + 1 < plat_dr_physmax) {
1919 1927                  kpm_size = ROUND_UP_LPAGE(mmu_ptob(plat_dr_physmax));
1920 1928          } else {
1921 1929                  kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1));
1922 1930          }
1923 1931          if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base)
1924 1932                  panic("not enough room for kpm!");
1925 1933          PRM_DEBUG(kpm_size);
1926 1934          PRM_DEBUG(kpm_vbase);
1927 1935  
1928 1936          /*
1929 1937           * By default we create a seg_kp in 64 bit kernels, it's a little
1930 1938           * faster to access than embedding it in the heap.
1931 1939           */
1932 1940          segkp_base = (caddr_t)valloc_base + valloc_sz;
1933 1941          if (!segkp_fromheap) {
1934 1942                  size_t sz = mmu_ptob(segkpsize);
1935 1943  
1936 1944                  /*
1937 1945                   * determine size of segkp
1938 1946                   */
1939 1947                  if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) {
1940 1948                          sz = SEGKPDEFSIZE;
1941 1949                          cmn_err(CE_WARN, "!Illegal value for segkpsize. "
1942 1950                              "segkpsize has been reset to %ld pages",
1943 1951                              mmu_btop(sz));
1944 1952                  }
1945 1953                  sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem)));
1946 1954  
1947 1955                  segkpsize = mmu_btop(ROUND_UP_LPAGE(sz));
1948 1956          }
1949 1957          PRM_DEBUG(segkp_base);
1950 1958          PRM_DEBUG(segkpsize);
1951 1959  
1952 1960          /*
1953 1961           * segzio is used for ZFS cached data. It uses a distinct VA
1954 1962           * segment (from kernel heap) so that we can easily tell not to
1955 1963           * include it in kernel crash dumps on 64 bit kernels. The trick is
1956 1964           * to give it lots of VA, but not constrain the kernel heap.
1957 1965           * We can use 1.5x physmem for segzio, leaving approximately
1958 1966           * another 1.5x physmem for heap.  See also the comment in
1959 1967           * startup_memlist().
1960 1968           */
1961 1969          segzio_base = segkp_base + mmu_ptob(segkpsize);
1962 1970          if (segzio_fromheap) {
1963 1971                  segziosize = 0;
1964 1972          } else {
1965 1973                  size_t physmem_size = mmu_ptob(physmem);
1966 1974                  size_t size = (segziosize == 0) ?
1967 1975                      physmem_size * 3 / 2 : mmu_ptob(segziosize);
1968 1976  
1969 1977                  if (size < SEGZIOMINSIZE)
1970 1978                          size = SEGZIOMINSIZE;
1971 1979                  segziosize = mmu_btop(ROUND_UP_LPAGE(size));
1972 1980          }
1973 1981          PRM_DEBUG(segziosize);
1974 1982          PRM_DEBUG(segzio_base);
1975 1983  
1976 1984          /*
1977 1985           * Put the range of VA for device mappings next, kmdb knows to not
1978 1986           * grep in this range of addresses.
1979 1987           */
1980 1988          toxic_addr =
1981 1989              ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize));
1982 1990          PRM_DEBUG(toxic_addr);
1983 1991          segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size);
1984 1992  #else /* __i386 */
1985 1993          segmap_start = ROUND_UP_LPAGE(kernelbase);
1986 1994  #endif /* __i386 */
1987 1995          PRM_DEBUG(segmap_start);
1988 1996  
1989 1997          /*
1990 1998           * Users can change segmapsize through eeprom. If the variable
1991 1999           * is tuned through eeprom, there is no upper bound on the
1992 2000           * size of segmap.
1993 2001           */
1994 2002          segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT);
1995 2003  
1996 2004  #if defined(__i386)
1997 2005          /*
1998 2006           * 32-bit systems don't have segkpm or segkp, so segmap appears at
1999 2007           * the bottom of the kernel's address range.  Set aside space for a
2000 2008           * small red zone just below the start of segmap.
2001 2009           */
2002 2010          segmap_start += KERNEL_REDZONE_SIZE;
2003 2011          segmapsize -= KERNEL_REDZONE_SIZE;
2004 2012  #endif
2005 2013  
2006 2014          PRM_DEBUG(segmap_start);
2007 2015          PRM_DEBUG(segmapsize);
2008 2016          kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize);
2009 2017          PRM_DEBUG(kernelheap);
2010 2018          PRM_POINT("layout_kernel_va() done...");
2011 2019  }
2012 2020  
2013 2021  /*
2014 2022   * Finish initializing the VM system, now that we are no longer
2015 2023   * relying on the boot time memory allocators.
2016 2024   */
2017 2025  static void
2018 2026  startup_vm(void)
2019 2027  {
2020 2028          struct segmap_crargs a;
2021 2029  
2022 2030          extern int use_brk_lpg, use_stk_lpg;
2023 2031  
2024 2032          PRM_POINT("startup_vm() starting...");
2025 2033  
2026 2034          /*
2027 2035           * Initialize the hat layer.
2028 2036           */
2029 2037          hat_init();
2030 2038  
2031 2039          /*
2032 2040           * Do final allocations of HAT data structures that need to
2033 2041           * be allocated before quiescing the boot loader.
2034 2042           */
2035 2043          PRM_POINT("Calling hat_kern_alloc()...");
2036 2044          hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap);
2037 2045          PRM_POINT("hat_kern_alloc() done");
2038 2046  
2039 2047  #ifndef __xpv
2040 2048          /*
2041 2049           * Setup Page Attribute Table
2042 2050           */
2043 2051          pat_sync();
2044 2052  #endif
2045 2053  
2046 2054          /*
2047 2055           * The next two loops are done in distinct steps in order
2048 2056           * to be sure that any page that is doubly mapped (both above
2049 2057           * KERNEL_TEXT and below kernelbase) is dealt with correctly.
2050 2058           * Note this may never happen, but it might someday.
2051 2059           */
2052 2060          bootpages = NULL;
2053 2061          PRM_POINT("Protecting boot pages");
2054 2062  
2055 2063          /*
2056 2064           * Protect any pages mapped above KERNEL_TEXT that somehow have
2057 2065           * page_t's. This can only happen if something weird allocated
2058 2066           * in this range (like kadb/kmdb).
2059 2067           */
2060 2068          protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0);
2061 2069  
2062 2070          /*
2063 2071           * Before we can take over memory allocation/mapping from the boot
2064 2072           * loader we must remove from our free page lists any boot allocated
2065 2073           * pages that stay mapped until release_bootstrap().
2066 2074           */
2067 2075          protect_boot_range(0, kernelbase, 1);
2068 2076  
2069 2077  
2070 2078          /*
2071 2079           * Switch to running on regular HAT (not boot_mmu)
2072 2080           */
2073 2081          PRM_POINT("Calling hat_kern_setup()...");
2074 2082          hat_kern_setup();
2075 2083  
2076 2084          /*
2077 2085           * It is no longer safe to call BOP_ALLOC(), so make sure we don't.
2078 2086           */
2079 2087          bop_no_more_mem();
2080 2088  
2081 2089          PRM_POINT("hat_kern_setup() done");
2082 2090  
2083 2091          hat_cpu_online(CPU);
2084 2092  
2085 2093          /*
2086 2094           * Initialize VM system
2087 2095           */
2088 2096          PRM_POINT("Calling kvm_init()...");
2089 2097          kvm_init();
2090 2098          PRM_POINT("kvm_init() done");
2091 2099  
2092 2100          /*
2093 2101           * Tell kmdb that the VM system is now working
2094 2102           */
2095 2103          if (boothowto & RB_DEBUG)
2096 2104                  kdi_dvec_vmready();
2097 2105  
2098 2106  #if defined(__xpv)
2099 2107          /*
2100 2108           * Populate the I/O pool on domain 0
2101 2109           */
2102 2110          if (DOMAIN_IS_INITDOMAIN(xen_info)) {
2103 2111                  extern long populate_io_pool(void);
2104 2112                  long init_io_pool_cnt;
2105 2113  
2106 2114                  PRM_POINT("Populating reserve I/O page pool");
2107 2115                  init_io_pool_cnt = populate_io_pool();
2108 2116                  PRM_DEBUG(init_io_pool_cnt);
2109 2117          }
2110 2118  #endif
2111 2119          /*
2112 2120           * Mangle the brand string etc.
2113 2121           */
2114 2122          cpuid_pass3(CPU);
2115 2123  
2116 2124  #if defined(__amd64)
2117 2125  
2118 2126          /*
2119 2127           * Create the device arena for toxic (to dtrace/kmdb) mappings.
2120 2128           */
2121 2129          device_arena = vmem_create("device", (void *)toxic_addr,
2122 2130              toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP);
2123 2131  
2124 2132  #else   /* __i386 */
2125 2133  
2126 2134          /*
2127 2135           * allocate the bit map that tracks toxic pages
2128 2136           */
2129 2137          toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase));
2130 2138          PRM_DEBUG(toxic_bit_map_len);
2131 2139          toxic_bit_map =
2132 2140              kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP);
2133 2141          ASSERT(toxic_bit_map != NULL);
2134 2142          PRM_DEBUG(toxic_bit_map);
2135 2143  
2136 2144  #endif  /* __i386 */
2137 2145  
2138 2146  
2139 2147          /*
2140 2148           * Now that we've got more VA, as well as the ability to allocate from
2141 2149           * it, tell the debugger.
2142 2150           */
2143 2151          if (boothowto & RB_DEBUG)
2144 2152                  kdi_dvec_memavail();
2145 2153  
2146 2154  #if !defined(__xpv)
2147 2155          /*
2148 2156           * Map page pfn=0 for drivers, such as kd, that need to pick up
2149 2157           * parameters left there by controllers/BIOS.
2150 2158           */
2151 2159          PRM_POINT("setup up p0_va");
2152 2160          p0_va = i86devmap(0, 1, PROT_READ);
2153 2161          PRM_DEBUG(p0_va);
2154 2162  #endif
2155 2163  
2156 2164          cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n",
2157 2165              physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled));
2158 2166  
2159 2167          /*
2160 2168           * disable automatic large pages for small memory systems or
2161 2169           * when the disable flag is set.
2162 2170           *
2163 2171           * Do not yet consider page sizes larger than 2m/4m.
2164 2172           */
2165 2173          if (!auto_lpg_disable && mmu.max_page_level > 0) {
2166 2174                  max_uheap_lpsize = LEVEL_SIZE(1);
2167 2175                  max_ustack_lpsize = LEVEL_SIZE(1);
2168 2176                  max_privmap_lpsize = LEVEL_SIZE(1);
2169 2177                  max_uidata_lpsize = LEVEL_SIZE(1);
2170 2178                  max_utext_lpsize = LEVEL_SIZE(1);
2171 2179                  max_shm_lpsize = LEVEL_SIZE(1);
2172 2180          }
2173 2181          if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 ||
2174 2182              auto_lpg_disable) {
2175 2183                  use_brk_lpg = 0;
2176 2184                  use_stk_lpg = 0;
2177 2185          }
2178 2186          mcntl0_lpsize = LEVEL_SIZE(mmu.umax_page_level);
2179 2187  
2180 2188          PRM_POINT("Calling hat_init_finish()...");
2181 2189          hat_init_finish();
2182 2190          PRM_POINT("hat_init_finish() done");
2183 2191  
2184 2192          /*
2185 2193           * Initialize the segkp segment type.
2186 2194           */
2187 2195          rw_enter(&kas.a_lock, RW_WRITER);
2188 2196          PRM_POINT("Attaching segkp");
2189 2197          if (segkp_fromheap) {
2190 2198                  segkp->s_as = &kas;
2191 2199          } else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize),
2192 2200              segkp) < 0) {
2193 2201                  panic("startup: cannot attach segkp");
2194 2202                  /*NOTREACHED*/
2195 2203          }
2196 2204          PRM_POINT("Doing segkp_create()");
2197 2205          if (segkp_create(segkp) != 0) {
2198 2206                  panic("startup: segkp_create failed");
2199 2207                  /*NOTREACHED*/
2200 2208          }
2201 2209          PRM_DEBUG(segkp);
2202 2210          rw_exit(&kas.a_lock);
2203 2211  
2204 2212          /*
2205 2213           * kpm segment
2206 2214           */
2207 2215          segmap_kpm = 0;
2208 2216          if (kpm_desired)
2209 2217                  kpm_init();
2210 2218  
2211 2219          /*
2212 2220           * Now create segmap segment.
2213 2221           */
2214 2222          rw_enter(&kas.a_lock, RW_WRITER);
2215 2223          if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) {
2216 2224                  panic("cannot attach segmap");
2217 2225                  /*NOTREACHED*/
2218 2226          }
2219 2227          PRM_DEBUG(segmap);
2220 2228  
2221 2229          a.prot = PROT_READ | PROT_WRITE;
2222 2230          a.shmsize = 0;
2223 2231          a.nfreelist = segmapfreelists;
2224 2232  
2225 2233          if (segmap_create(segmap, (caddr_t)&a) != 0)
2226 2234                  panic("segmap_create segmap");
2227 2235          rw_exit(&kas.a_lock);
2228 2236  
2229 2237          setup_vaddr_for_ppcopy(CPU);
2230 2238  
2231 2239          segdev_init();
2232 2240  #if defined(__xpv)
2233 2241          if (DOMAIN_IS_INITDOMAIN(xen_info))
2234 2242  #endif
2235 2243                  pmem_init();
2236 2244  
2237 2245          PRM_POINT("startup_vm() done");
2238 2246  }
2239 2247  
2240 2248  /*
2241 2249   * Load a tod module for the non-standard tod part found on this system.
2242 2250   */
2243 2251  static void
2244 2252  load_tod_module(char *todmod)
2245 2253  {
2246 2254          if (modload("tod", todmod) == -1)
2247 2255                  halt("Can't load TOD module");
2248 2256  }
2249 2257  
2250 2258  static void
2251 2259  startup_end(void)
2252 2260  {
2253 2261          int i;
2254 2262          extern void setx86isalist(void);
2255 2263          extern void cpu_event_init(void);
2256 2264  
2257 2265          PRM_POINT("startup_end() starting...");
2258 2266  
2259 2267          /*
2260 2268           * Perform tasks that get done after most of the VM
2261 2269           * initialization has been done but before the clock
2262 2270           * and other devices get started.
2263 2271           */
2264 2272          kern_setup1();
2265 2273  
2266 2274          /*
2267 2275           * Perform CPC initialization for this CPU.
2268 2276           */
2269 2277          kcpc_hw_init(CPU);
2270 2278  
2271 2279          /*
2272 2280           * Initialize cpu event framework.
2273 2281           */
2274 2282          cpu_event_init();
2275 2283  
2276 2284  #if defined(OPTERON_WORKAROUND_6323525)
2277 2285          if (opteron_workaround_6323525)
2278 2286                  patch_workaround_6323525();
2279 2287  #endif
2280 2288          /*
2281 2289           * If needed, load TOD module now so that ddi_get_time(9F) etc. work
2282 2290           * (For now, "needed" is defined as set tod_module_name in /etc/system)
2283 2291           */
2284 2292          if (tod_module_name != NULL) {
2285 2293                  PRM_POINT("load_tod_module()");
2286 2294                  load_tod_module(tod_module_name);
2287 2295          }
2288 2296  
2289 2297  #if defined(__xpv)
2290 2298          /*
2291 2299           * Forceload interposing TOD module for the hypervisor.
2292 2300           */
2293 2301          PRM_POINT("load_tod_module()");
2294 2302          load_tod_module("xpvtod");
2295 2303  #endif
2296 2304  
2297 2305          /*
2298 2306           * Configure the system.
2299 2307           */
2300 2308          PRM_POINT("Calling configure()...");
2301 2309          configure();            /* set up devices */
2302 2310          PRM_POINT("configure() done");
2303 2311  
2304 2312          /*
2305 2313           * We can now setup for XSAVE because fpu_probe is done in configure().
2306 2314           */
2307 2315          if (fp_save_mech == FP_XSAVE) {
2308 2316                  xsave_setup_msr(CPU);
2309 2317          }
2310 2318  
2311 2319          /*
2312 2320           * Set the isa_list string to the defined instruction sets we
2313 2321           * support.
2314 2322           */
2315 2323          setx86isalist();
2316 2324          cpu_intr_alloc(CPU, NINTR_THREADS);
2317 2325          psm_install();
2318 2326  
2319 2327          /*
2320 2328           * We're done with bootops.  We don't unmap the bootstrap yet because
2321 2329           * we're still using bootsvcs.
2322 2330           */
2323 2331          PRM_POINT("NULLing out bootops");
2324 2332          *bootopsp = (struct bootops *)NULL;
2325 2333          bootops = (struct bootops *)NULL;
2326 2334  
2327 2335  #if defined(__xpv)
2328 2336          ec_init_debug_irq();
2329 2337          xs_domu_init();
2330 2338  #endif
2331 2339  
2332 2340  #if !defined(__xpv)
2333 2341          /*
2334 2342           * Intel IOMMU has been setup/initialized in ddi_impl.c
2335 2343           * Start it up now.
2336 2344           */
2337 2345          immu_startup();
2338 2346  
2339 2347          /*
2340 2348           * Now that we're no longer going to drop into real mode for a BIOS call
2341 2349           * via bootops, we can enable PCID (which requires CR0.PG).
2342 2350           */
2343 2351          enable_pcid();
2344 2352  #endif
2345 2353  
2346 2354          PRM_POINT("Enabling interrupts");
2347 2355          (*picinitf)();
2348 2356          sti();
2349 2357  #if defined(__xpv)
2350 2358          ASSERT(CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask == 0);
2351 2359          xen_late_startup();
2352 2360  #endif
2353 2361  
2354 2362          (void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1,
2355 2363              "softlevel1", NULL, NULL); /* XXX to be moved later */
2356 2364  
2357 2365          /*
2358 2366           * Register software interrupt handlers for ddi_periodic_add(9F).
2359 2367           * Software interrupts up to the level 10 are supported.
2360 2368           */
2361 2369          for (i = DDI_IPL_1; i <= DDI_IPL_10; i++) {
2362 2370                  (void) add_avsoftintr((void *)&softlevel_hdl[i-1], i,
2363 2371                      (avfunc)(uintptr_t)ddi_periodic_softintr, "ddi_periodic",
2364 2372                      (caddr_t)(uintptr_t)i, NULL);
2365 2373          }
2366 2374  
2367 2375  #if !defined(__xpv)
2368 2376          if (modload("drv", "amd_iommu") < 0) {
2369 2377                  PRM_POINT("No AMD IOMMU present\n");
2370 2378          } else if (ddi_hold_installed_driver(ddi_name_to_major(
2371 2379              "amd_iommu")) == NULL) {
2372 2380                  prom_printf("ERROR: failed to attach AMD IOMMU\n");
2373 2381          }
2374 2382  #endif
2375 2383          post_startup_cpu_fixups();
2376 2384  
2377 2385          PRM_POINT("startup_end() done");
2378 2386  }
2379 2387  
2380 2388  /*
2381 2389   * Don't remove the following 2 variables.  They are necessary
2382 2390   * for reading the hostid from the legacy file (/kernel/misc/sysinit).
2383 2391   */
2384 2392  char *_hs1107 = hw_serial;
2385 2393  ulong_t  _bdhs34;
2386 2394  
2387 2395  void
2388 2396  post_startup(void)
2389 2397  {
2390 2398          extern void cpupm_init(cpu_t *);
2391 2399          extern void cpu_event_init_cpu(cpu_t *);
2392 2400  
2393 2401          /*
2394 2402           * Set the system wide, processor-specific flags to be passed
2395 2403           * to userland via the aux vector for performance hints and
2396 2404           * instruction set extensions.
2397 2405           */
2398 2406          bind_hwcap();
2399 2407  
2400 2408  #ifdef __xpv
2401 2409          if (DOMAIN_IS_INITDOMAIN(xen_info))
2402 2410  #endif
2403 2411          {
2404 2412  #if defined(__xpv)
2405 2413                  xpv_panic_init();
2406 2414  #else
2407 2415                  /*
2408 2416                   * Startup the memory scrubber.
2409 2417                   * XXPV This should be running somewhere ..
2410 2418                   */
2411 2419                  if ((get_hwenv() & HW_VIRTUAL) == 0)
2412 2420                          memscrub_init();
2413 2421  #endif
2414 2422          }
2415 2423  
2416 2424          /*
2417 2425           * Complete CPU module initialization
2418 2426           */
2419 2427          cmi_post_startup();
2420 2428  
2421 2429          /*
2422 2430           * Perform forceloading tasks for /etc/system.
2423 2431           */
2424 2432          (void) mod_sysctl(SYS_FORCELOAD, NULL);
2425 2433  
2426 2434          /*
2427 2435           * ON4.0: Force /proc module in until clock interrupt handle fixed
2428 2436           * ON4.0: This must be fixed or restated in /etc/systems.
2429 2437           */
2430 2438          (void) modload("fs", "procfs");
2431 2439  
2432 2440          (void) i_ddi_attach_hw_nodes("pit_beep");
2433 2441  
2434 2442  #if defined(__i386)
2435 2443          /*
2436 2444           * Check for required functional Floating Point hardware,
2437 2445           * unless FP hardware explicitly disabled.
2438 2446           */
2439 2447          if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO))
2440 2448                  halt("No working FP hardware found");
2441 2449  #endif
2442 2450  
2443 2451          maxmem = freemem;
2444 2452  
2445 2453          cpu_event_init_cpu(CPU);
2446 2454          cpupm_init(CPU);
2447 2455          (void) mach_cpu_create_device_node(CPU, NULL);
2448 2456  
2449 2457          pg_init();
2450 2458  }
2451 2459  
2452 2460  static int
2453 2461  pp_in_range(page_t *pp, uint64_t low_addr, uint64_t high_addr)
2454 2462  {
2455 2463          return ((pp->p_pagenum >= btop(low_addr)) &&
2456 2464              (pp->p_pagenum < btopr(high_addr)));
2457 2465  }
2458 2466  
2459 2467  static int
2460 2468  pp_in_module(page_t *pp, const rd_existing_t *modranges)
2461 2469  {
2462 2470          uint_t i;
2463 2471  
2464 2472          for (i = 0; modranges[i].phys != 0; i++) {
2465 2473                  if (pp_in_range(pp, modranges[i].phys,
2466 2474                      modranges[i].phys + modranges[i].size))
2467 2475                          return (1);
2468 2476          }
2469 2477  
2470 2478          return (0);
2471 2479  }
2472 2480  
2473 2481  void
2474 2482  release_bootstrap(void)
2475 2483  {
2476 2484          int root_is_ramdisk;
2477 2485          page_t *pp;
2478 2486          extern void kobj_boot_unmountroot(void);
2479 2487          extern dev_t rootdev;
2480 2488          uint_t i;
2481 2489          char propname[32];
2482 2490          rd_existing_t *modranges;
2483 2491  #if !defined(__xpv)
2484 2492          pfn_t   pfn;
2485 2493  #endif
2486 2494  
2487 2495          /*
2488 2496           * Save the bootfs module ranges so that we can reserve them below
2489 2497           * for the real bootfs.
2490 2498           */
2491 2499          modranges = kmem_alloc(sizeof (rd_existing_t) * MAX_BOOT_MODULES,
2492 2500              KM_SLEEP);
2493 2501          for (i = 0; ; i++) {
2494 2502                  uint64_t start, size;
2495 2503  
2496 2504                  modranges[i].phys = 0;
2497 2505  
2498 2506                  (void) snprintf(propname, sizeof (propname),
2499 2507                      "module-addr-%u", i);
2500 2508                  if (do_bsys_getproplen(NULL, propname) <= 0)
2501 2509                          break;
2502 2510                  (void) do_bsys_getprop(NULL, propname, &start);
2503 2511  
2504 2512                  (void) snprintf(propname, sizeof (propname),
2505 2513                      "module-size-%u", i);
2506 2514                  if (do_bsys_getproplen(NULL, propname) <= 0)
2507 2515                          break;
2508 2516                  (void) do_bsys_getprop(NULL, propname, &size);
2509 2517  
2510 2518                  modranges[i].phys = start;
2511 2519                  modranges[i].size = size;
2512 2520          }
2513 2521  
2514 2522          /* unmount boot ramdisk and release kmem usage */
2515 2523          kobj_boot_unmountroot();
2516 2524  
2517 2525          /*
2518 2526           * We're finished using the boot loader so free its pages.
2519 2527           */
2520 2528          PRM_POINT("Unmapping lower boot pages");
2521 2529  
2522 2530          clear_boot_mappings(0, _userlimit);
2523 2531  
2524 2532          postbootkernelbase = kernelbase;
2525 2533  
2526 2534          /*
2527 2535           * If root isn't on ramdisk, destroy the hardcoded
2528 2536           * ramdisk node now and release the memory. Else,
2529 2537           * ramdisk memory is kept in rd_pages.
2530 2538           */
2531 2539          root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk"));
2532 2540          if (!root_is_ramdisk) {
2533 2541                  dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0);
2534 2542                  ASSERT(dip && ddi_get_parent(dip) == ddi_root_node());
2535 2543                  ndi_rele_devi(dip);     /* held from ddi_find_devinfo */
2536 2544                  (void) ddi_remove_child(dip, 0);
2537 2545          }
2538 2546  
2539 2547          PRM_POINT("Releasing boot pages");
2540 2548          while (bootpages) {
2541 2549                  extern uint64_t ramdisk_start, ramdisk_end;
2542 2550                  pp = bootpages;
2543 2551                  bootpages = pp->p_next;
2544 2552  
2545 2553  
2546 2554                  /* Keep pages for the lower 64K */
2547 2555                  if (pp_in_range(pp, 0, 0x40000)) {
2548 2556                          pp->p_next = lower_pages;
2549 2557                          lower_pages = pp;
2550 2558                          lower_pages_count++;
2551 2559                          continue;
2552 2560                  }
2553 2561  
2554 2562                  if (root_is_ramdisk && pp_in_range(pp, ramdisk_start,
2555 2563                      ramdisk_end) || pp_in_module(pp, modranges)) {
2556 2564                          pp->p_next = rd_pages;
2557 2565                          rd_pages = pp;
2558 2566                          continue;
2559 2567                  }
2560 2568                  pp->p_next = (struct page *)0;
2561 2569                  pp->p_prev = (struct page *)0;
2562 2570                  PP_CLRBOOTPAGES(pp);
2563 2571                  page_free(pp, 1);
2564 2572          }
2565 2573          PRM_POINT("Boot pages released");
2566 2574  
2567 2575          kmem_free(modranges, sizeof (rd_existing_t) * 99);
2568 2576  
2569 2577  #if !defined(__xpv)
2570 2578  /* XXPV -- note this following bunch of code needs to be revisited in Xen 3.0 */
2571 2579          /*
2572 2580           * Find 1 page below 1 MB so that other processors can boot up or
2573 2581           * so that any processor can resume.
2574 2582           * Make sure it has a kernel VA as well as a 1:1 mapping.
2575 2583           * We should have just free'd one up.
2576 2584           */
2577 2585  
2578 2586          /*
2579 2587           * 0x10 pages is 64K.  Leave the bottom 64K alone
2580 2588           * for BIOS.
2581 2589           */
2582 2590          for (pfn = 0x10; pfn < btop(1*1024*1024); pfn++) {
2583 2591                  if (page_numtopp_alloc(pfn) == NULL)
2584 2592                          continue;
2585 2593                  rm_platter_va = i86devmap(pfn, 1,
2586 2594                      PROT_READ | PROT_WRITE | PROT_EXEC);
2587 2595                  rm_platter_pa = ptob(pfn);
2588 2596                  break;
2589 2597          }
2590 2598          if (pfn == btop(1*1024*1024) && use_mp)
2591 2599                  panic("No page below 1M available for starting "
2592 2600                      "other processors or for resuming from system-suspend");
2593 2601  #endif  /* !__xpv */
2594 2602  }
2595 2603  
2596 2604  /*
2597 2605   * Initialize the platform-specific parts of a page_t.
2598 2606   */
2599 2607  void
2600 2608  add_physmem_cb(page_t *pp, pfn_t pnum)
2601 2609  {
2602 2610          pp->p_pagenum = pnum;
2603 2611          pp->p_mapping = NULL;
2604 2612          pp->p_embed = 0;
2605 2613          pp->p_share = 0;
2606 2614          pp->p_mlentry = 0;
2607 2615  }
2608 2616  
2609 2617  /*
2610 2618   * kphysm_init() initializes physical memory.
2611 2619   */
2612 2620  static pgcnt_t
2613 2621  kphysm_init(page_t *pp, pgcnt_t npages)
2614 2622  {
2615 2623          struct memlist  *pmem;
2616 2624          struct memseg   *cur_memseg;
2617 2625          pfn_t           base_pfn;
2618 2626          pfn_t           end_pfn;
2619 2627          pgcnt_t         num;
2620 2628          pgcnt_t         pages_done = 0;
2621 2629          uint64_t        addr;
2622 2630          uint64_t        size;
2623 2631          extern pfn_t    ddiphysmin;
2624 2632          extern int      mnode_xwa;
2625 2633          int             ms = 0, me = 0;
2626 2634  
2627 2635          ASSERT(page_hash != NULL && page_hashsz != 0);
2628 2636  
2629 2637          cur_memseg = memseg_base;
2630 2638          for (pmem = phys_avail; pmem && npages; pmem = pmem->ml_next) {
2631 2639                  /*
2632 2640                   * In a 32 bit kernel can't use higher memory if we're
2633 2641                   * not booting in PAE mode. This check takes care of that.
2634 2642                   */
2635 2643                  addr = pmem->ml_address;
2636 2644                  size = pmem->ml_size;
2637 2645                  if (btop(addr) > physmax)
2638 2646                          continue;
2639 2647  
2640 2648                  /*
2641 2649                   * align addr and size - they may not be at page boundaries
2642 2650                   */
2643 2651                  if ((addr & MMU_PAGEOFFSET) != 0) {
2644 2652                          addr += MMU_PAGEOFFSET;
2645 2653                          addr &= ~(uint64_t)MMU_PAGEOFFSET;
2646 2654                          size -= addr - pmem->ml_address;
2647 2655                  }
2648 2656  
2649 2657                  /* only process pages below or equal to physmax */
2650 2658                  if ((btop(addr + size) - 1) > physmax)
2651 2659                          size = ptob(physmax - btop(addr) + 1);
2652 2660  
2653 2661                  num = btop(size);
2654 2662                  if (num == 0)
2655 2663                          continue;
2656 2664  
2657 2665                  if (num > npages)
2658 2666                          num = npages;
2659 2667  
2660 2668                  npages -= num;
2661 2669                  pages_done += num;
2662 2670                  base_pfn = btop(addr);
2663 2671  
2664 2672                  if (prom_debug)
2665 2673                          prom_printf("MEMSEG addr=0x%" PRIx64
2666 2674                              " pgs=0x%lx pfn 0x%lx-0x%lx\n",
2667 2675                              addr, num, base_pfn, base_pfn + num);
2668 2676  
2669 2677                  /*
2670 2678                   * Ignore pages below ddiphysmin to simplify ddi memory
2671 2679                   * allocation with non-zero addr_lo requests.
2672 2680                   */
2673 2681                  if (base_pfn < ddiphysmin) {
2674 2682                          if (base_pfn + num <= ddiphysmin)
2675 2683                                  continue;
2676 2684                          pp += (ddiphysmin - base_pfn);
2677 2685                          num -= (ddiphysmin - base_pfn);
2678 2686                          base_pfn = ddiphysmin;
2679 2687                  }
2680 2688  
2681 2689                  /*
2682 2690                   * mnode_xwa is greater than 1 when large pages regions can
2683 2691                   * cross memory node boundaries. To prevent the formation
2684 2692                   * of these large pages, configure the memsegs based on the
2685 2693                   * memory node ranges which had been made non-contiguous.
2686 2694                   */
2687 2695                  end_pfn = base_pfn + num - 1;
2688 2696                  if (mnode_xwa > 1) {
2689 2697                          ms = PFN_2_MEM_NODE(base_pfn);
2690 2698                          me = PFN_2_MEM_NODE(end_pfn);
2691 2699  
2692 2700                          if (ms != me) {
2693 2701                                  /*
2694 2702                                   * current range spans more than 1 memory node.
2695 2703                                   * Set num to only the pfn range in the start
2696 2704                                   * memory node.
2697 2705                                   */
2698 2706                                  num = mem_node_config[ms].physmax - base_pfn
2699 2707                                      + 1;
2700 2708                                  ASSERT(end_pfn > mem_node_config[ms].physmax);
2701 2709                          }
2702 2710                  }
2703 2711  
2704 2712                  for (;;) {
2705 2713                          /*
2706 2714                           * Build the memsegs entry
2707 2715                           */
2708 2716                          cur_memseg->pages = pp;
2709 2717                          cur_memseg->epages = pp + num;
2710 2718                          cur_memseg->pages_base = base_pfn;
2711 2719                          cur_memseg->pages_end = base_pfn + num;
2712 2720  
2713 2721                          /*
2714 2722                           * Insert into memseg list in decreasing pfn range
2715 2723                           * order. Low memory is typically more fragmented such
2716 2724                           * that this ordering keeps the larger ranges at the
2717 2725                           * front of the list for code that searches memseg.
2718 2726                           * This ASSERTS that the memsegs coming in from boot
2719 2727                           * are in increasing physical address order and not
2720 2728                           * contiguous.
2721 2729                           */
2722 2730                          if (memsegs != NULL) {
2723 2731                                  ASSERT(cur_memseg->pages_base >=
2724 2732                                      memsegs->pages_end);
2725 2733                                  cur_memseg->next = memsegs;
2726 2734                          }
2727 2735                          memsegs = cur_memseg;
2728 2736  
2729 2737                          /*
2730 2738                           * add_physmem() initializes the PSM part of the page
2731 2739                           * struct by calling the PSM back with add_physmem_cb().
2732 2740                           * In addition it coalesces pages into larger pages as
2733 2741                           * it initializes them.
2734 2742                           */
2735 2743                          add_physmem(pp, num, base_pfn);
2736 2744                          cur_memseg++;
2737 2745                          availrmem_initial += num;
2738 2746                          availrmem += num;
2739 2747  
2740 2748                          pp += num;
2741 2749                          if (ms >= me)
2742 2750                                  break;
2743 2751  
2744 2752                          /* process next memory node range */
2745 2753                          ms++;
2746 2754                          base_pfn = mem_node_config[ms].physbase;
2747 2755  
2748 2756                          if (mnode_xwa > 1) {
2749 2757                                  num = MIN(mem_node_config[ms].physmax,
2750 2758                                      end_pfn) - base_pfn + 1;
2751 2759                          } else {
2752 2760                                  num = mem_node_config[ms].physmax -
2753 2761                                      base_pfn + 1;
2754 2762                          }
2755 2763                  }
2756 2764          }
2757 2765  
2758 2766          PRM_DEBUG(availrmem_initial);
2759 2767          PRM_DEBUG(availrmem);
2760 2768          PRM_DEBUG(freemem);
2761 2769          build_pfn_hash();
2762 2770          return (pages_done);
2763 2771  }
2764 2772  
2765 2773  /*
2766 2774   * Kernel VM initialization.
2767 2775   */
2768 2776  static void
2769 2777  kvm_init(void)
2770 2778  {
2771 2779          ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0);
2772 2780  
2773 2781          /*
2774 2782           * Put the kernel segments in kernel address space.
2775 2783           */
2776 2784          rw_enter(&kas.a_lock, RW_WRITER);
2777 2785          as_avlinit(&kas);
2778 2786  
2779 2787          (void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg);
2780 2788          (void) segkmem_create(&ktextseg);
2781 2789  
2782 2790          (void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc);
2783 2791          (void) segkmem_create(&kvalloc);
2784 2792  
2785 2793          (void) seg_attach(&kas, kernelheap,
2786 2794              ekernelheap - kernelheap, &kvseg);
2787 2795          (void) segkmem_create(&kvseg);
2788 2796  
2789 2797          if (core_size > 0) {
2790 2798                  PRM_POINT("attaching kvseg_core");
2791 2799                  (void) seg_attach(&kas, (caddr_t)core_base, core_size,
2792 2800                      &kvseg_core);
2793 2801                  (void) segkmem_create(&kvseg_core);
2794 2802          }
2795 2803  
2796 2804          if (segziosize > 0) {
2797 2805                  PRM_POINT("attaching segzio");
2798 2806                  (void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2799 2807                      &kzioseg);
2800 2808                  (void) segkmem_zio_create(&kzioseg);
2801 2809  
2802 2810                  /* create zio area covering new segment */
2803 2811                  segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2804 2812          }
2805 2813  
2806 2814          (void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
2807 2815          (void) segkmem_create(&kdebugseg);
2808 2816  
2809 2817          rw_exit(&kas.a_lock);
2810 2818  
2811 2819          /*
2812 2820           * Ensure that the red zone at kernelbase is never accessible.
2813 2821           */
2814 2822          PRM_POINT("protecting redzone");
2815 2823          (void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0);
2816 2824  
2817 2825          /*
2818 2826           * Make the text writable so that it can be hot patched by DTrace.
2819 2827           */
2820 2828          (void) as_setprot(&kas, s_text, e_modtext - s_text,
2821 2829              PROT_READ | PROT_WRITE | PROT_EXEC);
2822 2830  
2823 2831          /*
2824 2832           * Make data writable until end.
2825 2833           */
2826 2834          (void) as_setprot(&kas, s_data, e_moddata - s_data,
2827 2835              PROT_READ | PROT_WRITE | PROT_EXEC);
2828 2836  }
2829 2837  
2830 2838  #ifndef __xpv
2831 2839  /*
2832 2840   * Solaris adds an entry for Write Combining caching to the PAT
2833 2841   */
2834 2842  static uint64_t pat_attr_reg = PAT_DEFAULT_ATTRIBUTE;
2835 2843  
2836 2844  void
2837 2845  pat_sync(void)
2838 2846  {
2839 2847          ulong_t cr0, cr0_orig, cr4;
2840 2848  
2841 2849          if (!is_x86_feature(x86_featureset, X86FSET_PAT))
2842 2850                  return;
2843 2851          cr0_orig = cr0 = getcr0();
2844 2852          cr4 = getcr4();
2845 2853  
2846 2854          /* disable caching and flush all caches and TLBs */
2847 2855          cr0 |= CR0_CD;
2848 2856          cr0 &= ~CR0_NW;
2849 2857          setcr0(cr0);
2850 2858          invalidate_cache();
2851 2859          if (cr4 & CR4_PGE) {
2852 2860                  setcr4(cr4 & ~(ulong_t)CR4_PGE);
2853 2861                  setcr4(cr4);
2854 2862          } else {
2855 2863                  reload_cr3();
2856 2864          }
2857 2865  
2858 2866          /* add our entry to the PAT */
2859 2867          wrmsr(REG_PAT, pat_attr_reg);
2860 2868  
2861 2869          /* flush TLBs and cache again, then reenable cr0 caching */
2862 2870          if (cr4 & CR4_PGE) {
2863 2871                  setcr4(cr4 & ~(ulong_t)CR4_PGE);
2864 2872                  setcr4(cr4);
2865 2873          } else {
2866 2874                  reload_cr3();
2867 2875          }
2868 2876          invalidate_cache();
2869 2877          setcr0(cr0_orig);
2870 2878  }
2871 2879  
2872 2880  #endif /* !__xpv */
2873 2881  
2874 2882  #if defined(_SOFT_HOSTID)
2875 2883  /*
2876 2884   * On platforms that do not have a hardware serial number, attempt
2877 2885   * to set one based on the contents of /etc/hostid.  If this file does
2878 2886   * not exist, assume that we are to generate a new hostid and set
2879 2887   * it in the kernel, for subsequent saving by a userland process
2880 2888   * once the system is up and the root filesystem is mounted r/w.
2881 2889   *
2882 2890   * In order to gracefully support upgrade on OpenSolaris, if
2883 2891   * /etc/hostid does not exist, we will attempt to get a serial number
2884 2892   * using the legacy method (/kernel/misc/sysinit).
2885 2893   *
2886 2894   * If that isn't present, we attempt to use an SMBIOS UUID, which is
2887 2895   * a hardware serial number.  Note that we don't automatically trust
2888 2896   * all SMBIOS UUIDs (some older platforms are defective and ship duplicate
2889 2897   * UUIDs in violation of the standard), we check against a blacklist.
2890 2898   *
2891 2899   * In an attempt to make the hostid less prone to abuse
2892 2900   * (for license circumvention, etc), we store it in /etc/hostid
2893 2901   * in rot47 format.
2894 2902   */
2895 2903  extern volatile unsigned long tenmicrodata;
2896 2904  static int atoi(char *);
2897 2905  
2898 2906  /*
2899 2907   * Set this to non-zero in /etc/system if you think your SMBIOS returns a
2900 2908   * UUID that is not unique. (Also report it so that the smbios_uuid_blacklist
2901 2909   * array can be updated.)
2902 2910   */
2903 2911  int smbios_broken_uuid = 0;
2904 2912  
2905 2913  /*
2906 2914   * List of known bad UUIDs.  This is just the lower 32-bit values, since
2907 2915   * that's what we use for the host id.  If your hostid falls here, you need
2908 2916   * to contact your hardware OEM for a fix for your BIOS.
2909 2917   */
2910 2918  static unsigned char
2911 2919  smbios_uuid_blacklist[][16] = {
2912 2920  
2913 2921          {       /* Reported bad UUID (Google search) */
2914 2922                  0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05,
2915 2923                  0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09,
2916 2924          },
2917 2925          {       /* Known bad DELL UUID */
2918 2926                  0x4C, 0x4C, 0x45, 0x44, 0x00, 0x00, 0x20, 0x10,
2919 2927                  0x80, 0x20, 0x80, 0xC0, 0x4F, 0x20, 0x20, 0x20,
2920 2928          },
2921 2929          {       /* Uninitialized flash */
2922 2930                  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2923 2931                  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2924 2932          },
2925 2933          {       /* All zeros */
2926 2934                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2927 2935                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2928 2936          },
2929 2937  };
2930 2938  
2931 2939  static int32_t
2932 2940  uuid_to_hostid(const uint8_t *uuid)
2933 2941  {
2934 2942          /*
2935 2943           * Although the UUIDs are 128-bits, they may not distribute entropy
2936 2944           * evenly.  We would like to use SHA or MD5, but those are located
2937 2945           * in loadable modules and not available this early in boot.  As we
2938 2946           * don't need the values to be cryptographically strong, we just
2939 2947           * generate 32-bit vaue by xor'ing the various sequences together,
2940 2948           * which ensures that the entire UUID contributes to the hostid.
2941 2949           */
2942 2950          uint32_t        id = 0;
2943 2951  
2944 2952          /* first check against the blacklist */
2945 2953          for (int i = 0; i < (sizeof (smbios_uuid_blacklist) / 16); i++) {
2946 2954                  if (bcmp(smbios_uuid_blacklist[0], uuid, 16) == 0) {
2947 2955                          cmn_err(CE_CONT, "?Broken SMBIOS UUID. "
2948 2956                              "Contact BIOS manufacturer for repair.\n");
2949 2957                          return ((int32_t)HW_INVALID_HOSTID);
2950 2958                  }
2951 2959          }
2952 2960  
2953 2961          for (int i = 0; i < 16; i++)
2954 2962                  id ^= ((uuid[i]) << (8 * (i % sizeof (id))));
2955 2963  
2956 2964          /* Make sure return value is positive */
2957 2965          return (id & 0x7fffffff);
2958 2966  }
2959 2967  
2960 2968  static int32_t
2961 2969  set_soft_hostid(void)
2962 2970  {
2963 2971          struct _buf *file;
2964 2972          char tokbuf[MAXNAMELEN];
2965 2973          token_t token;
2966 2974          int done = 0;
2967 2975          u_longlong_t tmp;
2968 2976          int i;
2969 2977          int32_t hostid = (int32_t)HW_INVALID_HOSTID;
2970 2978          unsigned char *c;
2971 2979          hrtime_t tsc;
2972 2980          smbios_system_t smsys;
2973 2981  
2974 2982          /*
2975 2983           * If /etc/hostid file not found, we'd like to get a pseudo
2976 2984           * random number to use at the hostid.  A nice way to do this
2977 2985           * is to read the real time clock.  To remain xen-compatible,
2978 2986           * we can't poke the real hardware, so we use tsc_read() to
2979 2987           * read the real time clock.  However, there is an ominous
2980 2988           * warning in tsc_read that says it can return zero, so we
2981 2989           * deal with that possibility by falling back to using the
2982 2990           * (hopefully random enough) value in tenmicrodata.
2983 2991           */
2984 2992  
2985 2993          if ((file = kobj_open_file(hostid_file)) == (struct _buf *)-1) {
2986 2994                  /*
2987 2995                   * hostid file not found - try to load sysinit module
2988 2996                   * and see if it has a nonzero hostid value...use that
2989 2997                   * instead of generating a new hostid here if so.
2990 2998                   */
2991 2999                  if ((i = modload("misc", "sysinit")) != -1) {
2992 3000                          if (strlen(hw_serial) > 0)
2993 3001                                  hostid = (int32_t)atoi(hw_serial);
2994 3002                          (void) modunload(i);
2995 3003                  }
2996 3004  
2997 3005                  /*
2998 3006                   * We try to use the SMBIOS UUID. But not if it is blacklisted
2999 3007                   * in /etc/system.
3000 3008                   */
3001 3009                  if ((hostid == HW_INVALID_HOSTID) &&
3002 3010                      (smbios_broken_uuid == 0) &&
3003 3011                      (ksmbios != NULL) &&
3004 3012                      (smbios_info_system(ksmbios, &smsys) != SMB_ERR) &&
3005 3013                      (smsys.smbs_uuidlen >= 16)) {
3006 3014                          hostid = uuid_to_hostid(smsys.smbs_uuid);
3007 3015                  }
3008 3016  
3009 3017                  /*
3010 3018                   * Generate a "random" hostid using the clock.  These
3011 3019                   * hostids will change on each boot if the value is not
3012 3020                   * saved to a persistent /etc/hostid file.
3013 3021                   */
3014 3022                  if (hostid == HW_INVALID_HOSTID) {
3015 3023                          tsc = tsc_read();
3016 3024                          if (tsc == 0)   /* tsc_read can return zero sometimes */
3017 3025                                  hostid = (int32_t)tenmicrodata & 0x0CFFFFF;
3018 3026                          else
3019 3027                                  hostid = (int32_t)tsc & 0x0CFFFFF;
3020 3028                  }
3021 3029          } else {
3022 3030                  /* hostid file found */
3023 3031                  while (!done) {
3024 3032                          token = kobj_lex(file, tokbuf, sizeof (tokbuf));
3025 3033  
3026 3034                          switch (token) {
3027 3035                          case POUND:
3028 3036                                  /*
3029 3037                                   * skip comments
3030 3038                                   */
3031 3039                                  kobj_find_eol(file);
3032 3040                                  break;
3033 3041                          case STRING:
3034 3042                                  /*
3035 3043                                   * un-rot47 - obviously this
3036 3044                                   * nonsense is ascii-specific
3037 3045                                   */
3038 3046                                  for (c = (unsigned char *)tokbuf;
3039 3047                                      *c != '\0'; c++) {
3040 3048                                          *c += 47;
3041 3049                                          if (*c > '~')
3042 3050                                                  *c -= 94;
3043 3051                                          else if (*c < '!')
3044 3052                                                  *c += 94;
3045 3053                                  }
3046 3054                                  /*
3047 3055                                   * now we should have a real number
3048 3056                                   */
3049 3057  
3050 3058                                  if (kobj_getvalue(tokbuf, &tmp) != 0)
3051 3059                                          kobj_file_err(CE_WARN, file,
3052 3060                                              "Bad value %s for hostid",
3053 3061                                              tokbuf);
3054 3062                                  else
3055 3063                                          hostid = (int32_t)tmp;
3056 3064  
3057 3065                                  break;
3058 3066                          case EOF:
3059 3067                                  done = 1;
3060 3068                                  /* FALLTHROUGH */
3061 3069                          case NEWLINE:
3062 3070                                  kobj_newline(file);
3063 3071                                  break;
3064 3072                          default:
3065 3073                                  break;
3066 3074  
3067 3075                          }
3068 3076                  }
3069 3077                  if (hostid == HW_INVALID_HOSTID) /* didn't find a hostid */
3070 3078                          kobj_file_err(CE_WARN, file,
3071 3079                              "hostid missing or corrupt");
3072 3080  
3073 3081                  kobj_close_file(file);
3074 3082          }
3075 3083          /*
3076 3084           * hostid is now the value read from /etc/hostid, or the
3077 3085           * new hostid we generated in this routine or HW_INVALID_HOSTID if not
3078 3086           * set.
3079 3087           */
3080 3088          return (hostid);
3081 3089  }
3082 3090  
3083 3091  static int
3084 3092  atoi(char *p)
3085 3093  {
3086 3094          int i = 0;
3087 3095  
3088 3096          while (*p != '\0')
3089 3097                  i = 10 * i + (*p++ - '0');
3090 3098  
3091 3099          return (i);
3092 3100  }
3093 3101  
3094 3102  #endif /* _SOFT_HOSTID */
3095 3103  
3096 3104  void
3097 3105  get_system_configuration(void)
3098 3106  {
3099 3107          char    prop[32];
3100 3108          u_longlong_t nodes_ll, cpus_pernode_ll, lvalue;
3101 3109  
3102 3110          if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) ||
3103 3111              BOP_GETPROP(bootops, "nodes", prop) < 0 ||
3104 3112              kobj_getvalue(prop, &nodes_ll) == -1 ||
3105 3113              nodes_ll > MAXNODES ||
3106 3114              BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) ||
3107 3115              BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 ||
3108 3116              kobj_getvalue(prop, &cpus_pernode_ll) == -1) {
3109 3117                  system_hardware.hd_nodes = 1;
3110 3118                  system_hardware.hd_cpus_per_node = 0;
3111 3119          } else {
3112 3120                  system_hardware.hd_nodes = (int)nodes_ll;
3113 3121                  system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll;
3114 3122          }
3115 3123  
3116 3124          if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) ||
3117 3125              BOP_GETPROP(bootops, "kernelbase", prop) < 0 ||
3118 3126              kobj_getvalue(prop, &lvalue) == -1)
3119 3127                  eprom_kernelbase = 0;
3120 3128          else
3121 3129                  eprom_kernelbase = (uintptr_t)lvalue;
3122 3130  
3123 3131          if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) ||
3124 3132              BOP_GETPROP(bootops, "segmapsize", prop) < 0 ||
3125 3133              kobj_getvalue(prop, &lvalue) == -1)
3126 3134                  segmapsize = SEGMAPDEFAULT;
3127 3135          else
3128 3136                  segmapsize = (uintptr_t)lvalue;
3129 3137  
3130 3138          if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) ||
3131 3139              BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 ||
3132 3140              kobj_getvalue(prop, &lvalue) == -1)
3133 3141                  segmapfreelists = 0;    /* use segmap driver default */
3134 3142          else
3135 3143                  segmapfreelists = (int)lvalue;
3136 3144  
3137 3145          /* physmem used to be here, but moved much earlier to fakebop.c */
3138 3146  }
3139 3147  
3140 3148  /*
3141 3149   * Add to a memory list.
3142 3150   * start = start of new memory segment
3143 3151   * len = length of new memory segment in bytes
3144 3152   * new = pointer to a new struct memlist
3145 3153   * memlistp = memory list to which to add segment.
3146 3154   */
3147 3155  void
3148 3156  memlist_add(
3149 3157          uint64_t start,
3150 3158          uint64_t len,
3151 3159          struct memlist *new,
3152 3160          struct memlist **memlistp)
3153 3161  {
3154 3162          struct memlist *cur;
3155 3163          uint64_t end = start + len;
3156 3164  
3157 3165          new->ml_address = start;
3158 3166          new->ml_size = len;
3159 3167  
3160 3168          cur = *memlistp;
3161 3169  
3162 3170          while (cur) {
3163 3171                  if (cur->ml_address >= end) {
3164 3172                          new->ml_next = cur;
3165 3173                          *memlistp = new;
3166 3174                          new->ml_prev = cur->ml_prev;
3167 3175                          cur->ml_prev = new;
3168 3176                          return;
3169 3177                  }
3170 3178                  ASSERT(cur->ml_address + cur->ml_size <= start);
3171 3179                  if (cur->ml_next == NULL) {
3172 3180                          cur->ml_next = new;
3173 3181                          new->ml_prev = cur;
3174 3182                          new->ml_next = NULL;
3175 3183                          return;
3176 3184                  }
3177 3185                  memlistp = &cur->ml_next;
3178 3186                  cur = cur->ml_next;
3179 3187          }
3180 3188  }
3181 3189  
3182 3190  void
3183 3191  kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
3184 3192  {
3185 3193          size_t tsize = e_modtext - modtext;
3186 3194          size_t dsize = e_moddata - moddata;
3187 3195  
3188 3196          *text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize,
3189 3197              1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP);
3190 3198          *data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize,
3191 3199              1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
3192 3200  }
3193 3201  
3194 3202  caddr_t
3195 3203  kobj_text_alloc(vmem_t *arena, size_t size)
3196 3204  {
3197 3205          return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT));
3198 3206  }
3199 3207  
3200 3208  /*ARGSUSED*/
3201 3209  caddr_t
3202 3210  kobj_texthole_alloc(caddr_t addr, size_t size)
3203 3211  {
3204 3212          panic("unexpected call to kobj_texthole_alloc()");
3205 3213          /*NOTREACHED*/
3206 3214          return (0);
3207 3215  }
3208 3216  
3209 3217  /*ARGSUSED*/
3210 3218  void
3211 3219  kobj_texthole_free(caddr_t addr, size_t size)
3212 3220  {
3213 3221          panic("unexpected call to kobj_texthole_free()");
3214 3222  }
3215 3223  
3216 3224  /*
3217 3225   * This is called just after configure() in startup().
3218 3226   *
3219 3227   * The ISALIST concept is a bit hopeless on Intel, because
3220 3228   * there's no guarantee of an ever-more-capable processor
3221 3229   * given that various parts of the instruction set may appear
3222 3230   * and disappear between different implementations.
3223 3231   *
3224 3232   * While it would be possible to correct it and even enhance
3225 3233   * it somewhat, the explicit hardware capability bitmask allows
3226 3234   * more flexibility.
3227 3235   *
3228 3236   * So, we just leave this alone.
3229 3237   */
3230 3238  void
3231 3239  setx86isalist(void)
3232 3240  {
3233 3241          char *tp;
3234 3242          size_t len;
3235 3243          extern char *isa_list;
3236 3244  
3237 3245  #define TBUFSIZE        1024
3238 3246  
3239 3247          tp = kmem_alloc(TBUFSIZE, KM_SLEEP);
3240 3248          *tp = '\0';
3241 3249  
3242 3250  #if defined(__amd64)
3243 3251          (void) strcpy(tp, "amd64 ");
3244 3252  #endif
3245 3253  
3246 3254          switch (x86_vendor) {
3247 3255          case X86_VENDOR_Intel:
3248 3256          case X86_VENDOR_AMD:
3249 3257          case X86_VENDOR_TM:
3250 3258                  if (is_x86_feature(x86_featureset, X86FSET_CMOV)) {
3251 3259                          /*
3252 3260                           * Pentium Pro or later
3253 3261                           */
3254 3262                          (void) strcat(tp, "pentium_pro");
3255 3263                          (void) strcat(tp,
3256 3264                              is_x86_feature(x86_featureset, X86FSET_MMX) ?
3257 3265                              "+mmx pentium_pro " : " ");
3258 3266                  }
3259 3267                  /*FALLTHROUGH*/
3260 3268          case X86_VENDOR_Cyrix:
3261 3269                  /*
3262 3270                   * The Cyrix 6x86 does not have any Pentium features
3263 3271                   * accessible while not at privilege level 0.
3264 3272                   */
3265 3273                  if (is_x86_feature(x86_featureset, X86FSET_CPUID)) {
3266 3274                          (void) strcat(tp, "pentium");
3267 3275                          (void) strcat(tp,
3268 3276                              is_x86_feature(x86_featureset, X86FSET_MMX) ?
3269 3277                              "+mmx pentium " : " ");
3270 3278                  }
3271 3279                  break;
3272 3280          default:
3273 3281                  break;
3274 3282          }
3275 3283          (void) strcat(tp, "i486 i386 i86");
3276 3284          len = strlen(tp) + 1;   /* account for NULL at end of string */
3277 3285          isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp);
3278 3286          kmem_free(tp, TBUFSIZE);
3279 3287  
3280 3288  #undef TBUFSIZE
3281 3289  }
3282 3290  
3283 3291  
3284 3292  #ifdef __amd64
3285 3293  
3286 3294  void *
3287 3295  device_arena_alloc(size_t size, int vm_flag)
3288 3296  {
3289 3297          return (vmem_alloc(device_arena, size, vm_flag));
3290 3298  }
3291 3299  
3292 3300  void
3293 3301  device_arena_free(void *vaddr, size_t size)
3294 3302  {
3295 3303          vmem_free(device_arena, vaddr, size);
3296 3304  }
3297 3305  
3298 3306  #else /* __i386 */
3299 3307  
3300 3308  void *
3301 3309  device_arena_alloc(size_t size, int vm_flag)
3302 3310  {
3303 3311          caddr_t vaddr;
3304 3312          uintptr_t v;
3305 3313          size_t  start;
3306 3314          size_t  end;
3307 3315  
3308 3316          vaddr = vmem_alloc(heap_arena, size, vm_flag);
3309 3317          if (vaddr == NULL)
3310 3318                  return (NULL);
3311 3319  
3312 3320          v = (uintptr_t)vaddr;
3313 3321          ASSERT(v >= kernelbase);
3314 3322          ASSERT(v + size <= valloc_base);
3315 3323  
3316 3324          start = btop(v - kernelbase);
3317 3325          end = btop(v + size - 1 - kernelbase);
3318 3326          ASSERT(start < toxic_bit_map_len);
3319 3327          ASSERT(end < toxic_bit_map_len);
3320 3328  
3321 3329          while (start <= end) {
3322 3330                  BT_ATOMIC_SET(toxic_bit_map, start);
3323 3331                  ++start;
3324 3332          }
3325 3333          return (vaddr);
3326 3334  }
3327 3335  
3328 3336  void
3329 3337  device_arena_free(void *vaddr, size_t size)
3330 3338  {
3331 3339          uintptr_t v = (uintptr_t)vaddr;
3332 3340          size_t  start;
3333 3341          size_t  end;
3334 3342  
3335 3343          ASSERT(v >= kernelbase);
3336 3344          ASSERT(v + size <= valloc_base);
3337 3345  
3338 3346          start = btop(v - kernelbase);
3339 3347          end = btop(v + size - 1 - kernelbase);
3340 3348          ASSERT(start < toxic_bit_map_len);
3341 3349          ASSERT(end < toxic_bit_map_len);
3342 3350  
3343 3351          while (start <= end) {
3344 3352                  ASSERT(BT_TEST(toxic_bit_map, start) != 0);
3345 3353                  BT_ATOMIC_CLEAR(toxic_bit_map, start);
3346 3354                  ++start;
3347 3355          }
3348 3356          vmem_free(heap_arena, vaddr, size);
3349 3357  }
3350 3358  
3351 3359  /*
3352 3360   * returns 1st address in range that is in device arena, or NULL
3353 3361   * if len is not NULL it returns the length of the toxic range
3354 3362   */
3355 3363  void *
3356 3364  device_arena_contains(void *vaddr, size_t size, size_t *len)
3357 3365  {
3358 3366          uintptr_t v = (uintptr_t)vaddr;
3359 3367          uintptr_t eaddr = v + size;
3360 3368          size_t start;
3361 3369          size_t end;
3362 3370  
3363 3371          /*
3364 3372           * if called very early by kmdb, just return NULL
3365 3373           */
3366 3374          if (toxic_bit_map == NULL)
3367 3375                  return (NULL);
3368 3376  
3369 3377          /*
3370 3378           * First check if we're completely outside the bitmap range.
3371 3379           */
3372 3380          if (v >= valloc_base || eaddr < kernelbase)
3373 3381                  return (NULL);
3374 3382  
3375 3383          /*
3376 3384           * Trim ends of search to look at only what the bitmap covers.
3377 3385           */
3378 3386          if (v < kernelbase)
3379 3387                  v = kernelbase;
3380 3388          start = btop(v - kernelbase);
3381 3389          end = btop(eaddr - kernelbase);
3382 3390          if (end >= toxic_bit_map_len)
3383 3391                  end = toxic_bit_map_len;
3384 3392  
3385 3393          if (bt_range(toxic_bit_map, &start, &end, end) == 0)
3386 3394                  return (NULL);
3387 3395  
3388 3396          v = kernelbase + ptob(start);
3389 3397          if (len != NULL)
3390 3398                  *len = ptob(end - start);
3391 3399          return ((void *)v);
3392 3400  }
3393 3401  
3394 3402  #endif  /* __i386 */
  
    | 
      ↓ open down ↓ | 
    2650 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX