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