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 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright (c) 2010, Intel Corporation.
27 * All rights reserved.
28 *
29 * Copyright 2013 Joyent, Inc. All rights reserved.
30 */
31
32 /*
33 * This file contains the functionality that mimics the boot operations
34 * on SPARC systems or the old boot.bin/multiboot programs on x86 systems.
35 * The x86 kernel now does everything on its own.
36 */
37
38 #include <sys/types.h>
39 #include <sys/bootconf.h>
40 #include <sys/bootsvcs.h>
41 #include <sys/bootinfo.h>
42 #include <sys/multiboot.h>
43 #include <sys/multiboot2.h>
44 #include <sys/multiboot2_impl.h>
45 #include <sys/bootvfs.h>
46 #include <sys/bootprops.h>
47 #include <sys/varargs.h>
48 #include <sys/param.h>
49 #include <sys/machparam.h>
50 #include <sys/machsystm.h>
51 #include <sys/archsystm.h>
52 #include <sys/boot_console.h>
53 #include <sys/cmn_err.h>
54 #include <sys/systm.h>
55 #include <sys/promif.h>
56 #include <sys/archsystm.h>
57 #include <sys/x86_archext.h>
58 #include <sys/kobj.h>
59 #include <sys/privregs.h>
60 #include <sys/sysmacros.h>
61 #include <sys/ctype.h>
62 #include <sys/fastboot.h>
63 #ifdef __xpv
64 #include <sys/hypervisor.h>
65 #include <net/if.h>
66 #endif
67 #include <vm/kboot_mmu.h>
68 #include <vm/hat_pte.h>
69 #include <sys/kobj.h>
70 #include <sys/kobj_lex.h>
71 #include <sys/pci_cfgspace_impl.h>
72 #include <sys/fastboot_impl.h>
73 #include <sys/acpi/acconfig.h>
74 #include <sys/acpi/acpi.h>
75
76 static int have_console = 0; /* set once primitive console is initialized */
77 static char *boot_args = "";
78
79 /*
80 * Debugging macros
81 */
82 static uint_t kbm_debug = 0;
83 #define DBG_MSG(s) { if (kbm_debug) bop_printf(NULL, "%s", s); }
84 #define DBG(x) { if (kbm_debug) \
85 bop_printf(NULL, "%s is %" PRIx64 "\n", #x, (uint64_t)(x)); \
86 }
87
88 #define PUT_STRING(s) { \
89 char *cp; \
90 for (cp = (s); *cp; ++cp) \
91 bcons_putchar(*cp); \
92 }
93
94 bootops_t bootop; /* simple bootops we'll pass on to kernel */
95 struct bsys_mem bm;
96
97 /*
98 * Boot info from "glue" code in low memory. xbootp is used by:
99 * do_bop_phys_alloc(), do_bsys_alloc() and boot_prop_finish().
100 */
101 static struct xboot_info *xbootp;
102 static uintptr_t next_virt; /* next available virtual address */
103 static paddr_t next_phys; /* next available physical address from dboot */
104 static paddr_t high_phys = -(paddr_t)1; /* last used physical address */
105
106 /*
107 * buffer for vsnprintf for console I/O
108 */
109 #define BUFFERSIZE 512
110 static char buffer[BUFFERSIZE];
111
112 /*
113 * stuff to store/report/manipulate boot property settings.
114 */
115 typedef struct bootprop {
116 struct bootprop *bp_next;
117 char *bp_name;
118 uint_t bp_vlen;
119 char *bp_value;
120 } bootprop_t;
121
122 static bootprop_t *bprops = NULL;
123 static char *curr_page = NULL; /* ptr to avail bprop memory */
124 static int curr_space = 0; /* amount of memory at curr_page */
125
126 #ifdef __xpv
127 start_info_t *xen_info;
128 shared_info_t *HYPERVISOR_shared_info;
129 #endif
130
131 /*
132 * some allocator statistics
133 */
134 static ulong_t total_bop_alloc_scratch = 0;
135 static ulong_t total_bop_alloc_kernel = 0;
136
137 static void build_firmware_properties(struct xboot_info *);
138
139 static int early_allocation = 1;
140
141 int force_fastreboot = 0;
142 volatile int fastreboot_onpanic = 0;
143 int post_fastreboot = 0;
144 #ifdef __xpv
145 volatile int fastreboot_capable = 0;
146 #else
147 volatile int fastreboot_capable = 1;
148 #endif
149
150 /*
151 * Information saved from current boot for fast reboot.
152 * If the information size exceeds what we have allocated, fast reboot
153 * will not be supported.
154 */
155 multiboot_info_t saved_mbi;
156 mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
157 uint8_t saved_drives[FASTBOOT_SAVED_DRIVES_SIZE];
158 char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
159 int saved_cmdline_len = 0;
160 size_t saved_file_size[FASTBOOT_MAX_FILES_MAP];
161
162 /*
163 * Turn off fastreboot_onpanic to avoid panic loop.
164 */
165 char fastreboot_onpanic_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
166 static const char fastreboot_onpanic_args[] = " -B fastreboot_onpanic=0";
167
168 /*
169 * Pointers to where System Resource Affinity Table (SRAT), System Locality
170 * Information Table (SLIT) and Maximum System Capability Table (MSCT)
171 * are mapped into virtual memory
172 */
173 ACPI_TABLE_SRAT *srat_ptr = NULL;
174 ACPI_TABLE_SLIT *slit_ptr = NULL;
175 ACPI_TABLE_MSCT *msct_ptr = NULL;
176
177 /*
178 * Arbitrary limit on number of localities we handle; if
179 * this limit is raised to more than UINT16_MAX, make sure
180 * process_slit() knows how to handle it.
181 */
182 #define SLIT_LOCALITIES_MAX (4096)
183
184 #define SLIT_NUM_PROPNAME "acpi-slit-localities"
185 #define SLIT_PROPNAME "acpi-slit"
186
187 /*
188 * Allocate aligned physical memory at boot time. This allocator allocates
189 * from the highest possible addresses. This avoids exhausting memory that
190 * would be useful for DMA buffers.
191 */
192 paddr_t
do_bop_phys_alloc(uint64_t size,uint64_t align)193 do_bop_phys_alloc(uint64_t size, uint64_t align)
194 {
195 paddr_t pa = 0;
196 paddr_t start;
197 paddr_t end;
198 struct memlist *ml = (struct memlist *)xbootp->bi_phys_install;
199
200 /*
201 * Be careful if high memory usage is limited in startup.c
202 * Since there are holes in the low part of the physical address
203 * space we can treat physmem as a pfn (not just a pgcnt) and
204 * get a conservative upper limit.
205 */
206 if (physmem != 0 && high_phys > pfn_to_pa(physmem))
207 high_phys = pfn_to_pa(physmem);
208
209 /*
210 * find the lowest or highest available memory in physinstalled
211 * On 32 bit avoid physmem above 4Gig if PAE isn't enabled
212 */
213 #if defined(__i386)
214 if (xbootp->bi_use_pae == 0 && high_phys > FOUR_GIG)
215 high_phys = FOUR_GIG;
216 #endif
217
218 /*
219 * find the highest available memory in physinstalled
220 */
221 size = P2ROUNDUP(size, align);
222 for (; ml; ml = ml->ml_next) {
223 start = P2ROUNDUP(ml->ml_address, align);
224 end = P2ALIGN(ml->ml_address + ml->ml_size, align);
225 if (start < next_phys)
226 start = P2ROUNDUP(next_phys, align);
227 if (end > high_phys)
228 end = P2ALIGN(high_phys, align);
229
230 if (end <= start)
231 continue;
232 if (end - start < size)
233 continue;
234
235 /*
236 * Early allocations need to use low memory, since
237 * physmem might be further limited by bootenv.rc
238 */
239 if (early_allocation) {
240 if (pa == 0 || start < pa)
241 pa = start;
242 } else {
243 if (end - size > pa)
244 pa = end - size;
245 }
246 }
247 if (pa != 0) {
248 if (early_allocation)
249 next_phys = pa + size;
250 else
251 high_phys = pa;
252 return (pa);
253 }
254 bop_panic("do_bop_phys_alloc(0x%" PRIx64 ", 0x%" PRIx64
255 ") Out of memory\n", size, align);
256 /*NOTREACHED*/
257 }
258
259 uintptr_t
alloc_vaddr(size_t size,paddr_t align)260 alloc_vaddr(size_t size, paddr_t align)
261 {
262 uintptr_t rv;
263
264 next_virt = P2ROUNDUP(next_virt, (uintptr_t)align);
265 rv = (uintptr_t)next_virt;
266 next_virt += size;
267 return (rv);
268 }
269
270 /*
271 * Allocate virtual memory. The size is always rounded up to a multiple
272 * of base pagesize.
273 */
274
275 /*ARGSUSED*/
276 static caddr_t
do_bsys_alloc(bootops_t * bop,caddr_t virthint,size_t size,int align)277 do_bsys_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
278 {
279 paddr_t a = align; /* same type as pa for masking */
280 uint_t pgsize;
281 paddr_t pa;
282 uintptr_t va;
283 ssize_t s; /* the aligned size */
284 uint_t level;
285 uint_t is_kernel = (virthint != 0);
286
287 if (a < MMU_PAGESIZE)
288 a = MMU_PAGESIZE;
289 else if (!ISP2(a))
290 prom_panic("do_bsys_alloc() incorrect alignment");
291 size = P2ROUNDUP(size, MMU_PAGESIZE);
292
293 /*
294 * Use the next aligned virtual address if we weren't given one.
295 */
296 if (virthint == NULL) {
297 virthint = (caddr_t)alloc_vaddr(size, a);
298 total_bop_alloc_scratch += size;
299 } else {
300 total_bop_alloc_kernel += size;
301 }
302
303 /*
304 * allocate the physical memory
305 */
306 pa = do_bop_phys_alloc(size, a);
307
308 /*
309 * Add the mappings to the page tables, try large pages first.
310 */
311 va = (uintptr_t)virthint;
312 s = size;
313 level = 1;
314 pgsize = xbootp->bi_use_pae ? TWO_MEG : FOUR_MEG;
315 if (xbootp->bi_use_largepage && a == pgsize) {
316 while (IS_P2ALIGNED(pa, pgsize) && IS_P2ALIGNED(va, pgsize) &&
317 s >= pgsize) {
318 kbm_map(va, pa, level, is_kernel);
319 va += pgsize;
320 pa += pgsize;
321 s -= pgsize;
322 }
323 }
324
325 /*
326 * Map remaining pages use small mappings
327 */
328 level = 0;
329 pgsize = MMU_PAGESIZE;
330 while (s > 0) {
331 kbm_map(va, pa, level, is_kernel);
332 va += pgsize;
333 pa += pgsize;
334 s -= pgsize;
335 }
336 return (virthint);
337 }
338
339 /*
340 * Free virtual memory - we'll just ignore these.
341 */
342 /*ARGSUSED*/
343 static void
do_bsys_free(bootops_t * bop,caddr_t virt,size_t size)344 do_bsys_free(bootops_t *bop, caddr_t virt, size_t size)
345 {
346 bop_printf(NULL, "do_bsys_free(virt=0x%p, size=0x%lx) ignored\n",
347 (void *)virt, size);
348 }
349
350 /*
351 * Old interface
352 */
353 /*ARGSUSED*/
354 static caddr_t
do_bsys_ealloc(bootops_t * bop,caddr_t virthint,size_t size,int align,int flags)355 do_bsys_ealloc(bootops_t *bop, caddr_t virthint, size_t size,
356 int align, int flags)
357 {
358 prom_panic("unsupported call to BOP_EALLOC()\n");
359 return (0);
360 }
361
362
363 static void
bsetprop(char * name,int nlen,void * value,int vlen)364 bsetprop(char *name, int nlen, void *value, int vlen)
365 {
366 uint_t size;
367 uint_t need_size;
368 bootprop_t *b;
369
370 /*
371 * align the size to 16 byte boundary
372 */
373 size = sizeof (bootprop_t) + nlen + 1 + vlen;
374 size = (size + 0xf) & ~0xf;
375 if (size > curr_space) {
376 need_size = (size + (MMU_PAGEOFFSET)) & MMU_PAGEMASK;
377 curr_page = do_bsys_alloc(NULL, 0, need_size, MMU_PAGESIZE);
378 curr_space = need_size;
379 }
380
381 /*
382 * use a bootprop_t at curr_page and link into list
383 */
384 b = (bootprop_t *)curr_page;
385 curr_page += sizeof (bootprop_t);
386 curr_space -= sizeof (bootprop_t);
387 b->bp_next = bprops;
388 bprops = b;
389
390 /*
391 * follow by name and ending zero byte
392 */
393 b->bp_name = curr_page;
394 bcopy(name, curr_page, nlen);
395 curr_page += nlen;
396 *curr_page++ = 0;
397 curr_space -= nlen + 1;
398
399 /*
400 * copy in value, but no ending zero byte
401 */
402 b->bp_value = curr_page;
403 b->bp_vlen = vlen;
404 if (vlen > 0) {
405 bcopy(value, curr_page, vlen);
406 curr_page += vlen;
407 curr_space -= vlen;
408 }
409
410 /*
411 * align new values of curr_page, curr_space
412 */
413 while (curr_space & 0xf) {
414 ++curr_page;
415 --curr_space;
416 }
417 }
418
419 static void
bsetprops(char * name,char * value)420 bsetprops(char *name, char *value)
421 {
422 bsetprop(name, strlen(name), value, strlen(value) + 1);
423 }
424
425 static void
bsetprop64(char * name,uint64_t value)426 bsetprop64(char *name, uint64_t value)
427 {
428 bsetprop(name, strlen(name), (void *)&value, sizeof (value));
429 }
430
431 static void
bsetpropsi(char * name,int value)432 bsetpropsi(char *name, int value)
433 {
434 char prop_val[32];
435
436 (void) snprintf(prop_val, sizeof (prop_val), "%d", value);
437 bsetprops(name, prop_val);
438 }
439
440 /*
441 * to find the size of the buffer to allocate
442 */
443 /*ARGSUSED*/
444 int
do_bsys_getproplen(bootops_t * bop,const char * name)445 do_bsys_getproplen(bootops_t *bop, const char *name)
446 {
447 bootprop_t *b;
448
449 for (b = bprops; b; b = b->bp_next) {
450 if (strcmp(name, b->bp_name) != 0)
451 continue;
452 return (b->bp_vlen);
453 }
454 return (-1);
455 }
456
457 /*
458 * get the value associated with this name
459 */
460 /*ARGSUSED*/
461 int
do_bsys_getprop(bootops_t * bop,const char * name,void * value)462 do_bsys_getprop(bootops_t *bop, const char *name, void *value)
463 {
464 bootprop_t *b;
465
466 for (b = bprops; b; b = b->bp_next) {
467 if (strcmp(name, b->bp_name) != 0)
468 continue;
469 bcopy(b->bp_value, value, b->bp_vlen);
470 return (0);
471 }
472 return (-1);
473 }
474
475 /*
476 * get the name of the next property in succession from the standalone
477 */
478 /*ARGSUSED*/
479 static char *
do_bsys_nextprop(bootops_t * bop,char * name)480 do_bsys_nextprop(bootops_t *bop, char *name)
481 {
482 bootprop_t *b;
483
484 /*
485 * A null name is a special signal for the 1st boot property
486 */
487 if (name == NULL || strlen(name) == 0) {
488 if (bprops == NULL)
489 return (NULL);
490 return (bprops->bp_name);
491 }
492
493 for (b = bprops; b; b = b->bp_next) {
494 if (name != b->bp_name)
495 continue;
496 b = b->bp_next;
497 if (b == NULL)
498 return (NULL);
499 return (b->bp_name);
500 }
501 return (NULL);
502 }
503
504 /*
505 * Parse numeric value from a string. Understands decimal, hex, octal, - and ~
506 */
507 static int
parse_value(char * p,uint64_t * retval)508 parse_value(char *p, uint64_t *retval)
509 {
510 int adjust = 0;
511 uint64_t tmp = 0;
512 int digit;
513 int radix = 10;
514
515 *retval = 0;
516 if (*p == '-' || *p == '~')
517 adjust = *p++;
518
519 if (*p == '0') {
520 ++p;
521 if (*p == 0)
522 return (0);
523 if (*p == 'x' || *p == 'X') {
524 radix = 16;
525 ++p;
526 } else {
527 radix = 8;
528 ++p;
529 }
530 }
531 while (*p) {
532 if ('0' <= *p && *p <= '9')
533 digit = *p - '0';
534 else if ('a' <= *p && *p <= 'f')
535 digit = 10 + *p - 'a';
536 else if ('A' <= *p && *p <= 'F')
537 digit = 10 + *p - 'A';
538 else
539 return (-1);
540 if (digit >= radix)
541 return (-1);
542 tmp = tmp * radix + digit;
543 ++p;
544 }
545 if (adjust == '-')
546 tmp = -tmp;
547 else if (adjust == '~')
548 tmp = ~tmp;
549 *retval = tmp;
550 return (0);
551 }
552
553 static boolean_t
unprintable(char * value,int size)554 unprintable(char *value, int size)
555 {
556 int i;
557
558 if (size <= 0 || value[0] == '\0')
559 return (B_TRUE);
560
561 for (i = 0; i < size; i++) {
562 if (value[i] == '\0')
563 return (i != (size - 1));
564
565 if (!isprint(value[i]))
566 return (B_TRUE);
567 }
568 return (B_FALSE);
569 }
570
571 /*
572 * Print out information about all boot properties.
573 * buffer is pointer to pre-allocated space to be used as temporary
574 * space for property values.
575 */
576 static void
boot_prop_display(char * buffer)577 boot_prop_display(char *buffer)
578 {
579 char *name = "";
580 int i, len;
581
582 bop_printf(NULL, "\nBoot properties:\n");
583
584 while ((name = do_bsys_nextprop(NULL, name)) != NULL) {
585 bop_printf(NULL, "\t0x%p %s = ", (void *)name, name);
586 (void) do_bsys_getprop(NULL, name, buffer);
587 len = do_bsys_getproplen(NULL, name);
588 bop_printf(NULL, "len=%d ", len);
589 if (!unprintable(buffer, len)) {
590 buffer[len] = 0;
591 bop_printf(NULL, "%s\n", buffer);
592 continue;
593 }
594 for (i = 0; i < len; i++) {
595 bop_printf(NULL, "%02x", buffer[i] & 0xff);
596 if (i < len - 1)
597 bop_printf(NULL, ".");
598 }
599 bop_printf(NULL, "\n");
600 }
601 }
602
603 /*
604 * 2nd part of building the table of boot properties. This includes:
605 * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
606 *
607 * lines look like one of:
608 * ^$
609 * ^# comment till end of line
610 * setprop name 'value'
611 * setprop name value
612 * setprop name "value"
613 *
614 * we do single character I/O since this is really just looking at memory
615 */
616 void
boot_prop_finish(void)617 boot_prop_finish(void)
618 {
619 int fd;
620 char *line;
621 int c;
622 int bytes_read;
623 char *name;
624 int n_len;
625 char *value;
626 int v_len;
627 char *inputdev; /* these override the command line if serial ports */
628 char *outputdev;
629 char *consoledev;
630 uint64_t lvalue;
631 int use_xencons = 0;
632
633 #ifdef __xpv
634 if (!DOMAIN_IS_INITDOMAIN(xen_info))
635 use_xencons = 1;
636 #endif /* __xpv */
637
638 DBG_MSG("Opening /boot/solaris/bootenv.rc\n");
639 fd = BRD_OPEN(bfs_ops, "/boot/solaris/bootenv.rc", 0);
640 DBG(fd);
641
642 line = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
643 while (fd >= 0) {
644
645 /*
646 * get a line
647 */
648 for (c = 0; ; ++c) {
649 bytes_read = BRD_READ(bfs_ops, fd, line + c, 1);
650 if (bytes_read == 0) {
651 if (c == 0)
652 goto done;
653 break;
654 }
655 if (line[c] == '\n')
656 break;
657 }
658 line[c] = 0;
659
660 /*
661 * ignore comment lines
662 */
663 c = 0;
664 while (ISSPACE(line[c]))
665 ++c;
666 if (line[c] == '#' || line[c] == 0)
667 continue;
668
669 /*
670 * must have "setprop " or "setprop\t"
671 */
672 if (strncmp(line + c, "setprop ", 8) != 0 &&
673 strncmp(line + c, "setprop\t", 8) != 0)
674 continue;
675 c += 8;
676 while (ISSPACE(line[c]))
677 ++c;
678 if (line[c] == 0)
679 continue;
680
681 /*
682 * gather up the property name
683 */
684 name = line + c;
685 n_len = 0;
686 while (line[c] && !ISSPACE(line[c]))
687 ++n_len, ++c;
688
689 /*
690 * gather up the value, if any
691 */
692 value = "";
693 v_len = 0;
694 while (ISSPACE(line[c]))
695 ++c;
696 if (line[c] != 0) {
697 value = line + c;
698 while (line[c] && !ISSPACE(line[c]))
699 ++v_len, ++c;
700 }
701
702 if (v_len >= 2 && value[0] == value[v_len - 1] &&
703 (value[0] == '\'' || value[0] == '"')) {
704 ++value;
705 v_len -= 2;
706 }
707 name[n_len] = 0;
708 if (v_len > 0)
709 value[v_len] = 0;
710 else
711 continue;
712
713 /*
714 * ignore "boot-file" property, it's now meaningless
715 */
716 if (strcmp(name, "boot-file") == 0)
717 continue;
718 if (strcmp(name, "boot-args") == 0 &&
719 strlen(boot_args) > 0)
720 continue;
721
722 /*
723 * If a property was explicitly set on the command line
724 * it will override a setting in bootenv.rc
725 */
726 if (do_bsys_getproplen(NULL, name) > 0)
727 continue;
728
729 bsetprop(name, n_len, value, v_len + 1);
730 }
731 done:
732 if (fd >= 0)
733 (void) BRD_CLOSE(bfs_ops, fd);
734
735 /*
736 * Check if we have to limit the boot time allocator
737 */
738 if (do_bsys_getproplen(NULL, "physmem") != -1 &&
739 do_bsys_getprop(NULL, "physmem", line) >= 0 &&
740 parse_value(line, &lvalue) != -1) {
741 if (0 < lvalue && (lvalue < physmem || physmem == 0)) {
742 physmem = (pgcnt_t)lvalue;
743 DBG(physmem);
744 }
745 }
746 early_allocation = 0;
747
748 /*
749 * check to see if we have to override the default value of the console
750 */
751 if (!use_xencons) {
752 inputdev = line;
753 v_len = do_bsys_getproplen(NULL, "input-device");
754 if (v_len > 0)
755 (void) do_bsys_getprop(NULL, "input-device", inputdev);
756 else
757 v_len = 0;
758 inputdev[v_len] = 0;
759
760 outputdev = inputdev + v_len + 1;
761 v_len = do_bsys_getproplen(NULL, "output-device");
762 if (v_len > 0)
763 (void) do_bsys_getprop(NULL, "output-device",
764 outputdev);
765 else
766 v_len = 0;
767 outputdev[v_len] = 0;
768
769 consoledev = outputdev + v_len + 1;
770 v_len = do_bsys_getproplen(NULL, "console");
771 if (v_len > 0) {
772 (void) do_bsys_getprop(NULL, "console", consoledev);
773 if (post_fastreboot &&
774 strcmp(consoledev, "graphics") == 0) {
775 bsetprops("console", "text");
776 v_len = strlen("text");
777 bcopy("text", consoledev, v_len);
778 }
779 } else {
780 v_len = 0;
781 }
782 consoledev[v_len] = 0;
783 bcons_init2(inputdev, outputdev, consoledev);
784 } else {
785 /*
786 * Ensure console property exists
787 * If not create it as "hypervisor"
788 */
789 v_len = do_bsys_getproplen(NULL, "console");
790 if (v_len < 0)
791 bsetprops("console", "hypervisor");
792 inputdev = outputdev = consoledev = "hypervisor";
793 bcons_init2(inputdev, outputdev, consoledev);
794 }
795
796 if (strstr((char *)xbootp->bi_cmdline, "prom_debug") || kbm_debug)
797 boot_prop_display(line);
798 }
799
800 /*
801 * print formatted output
802 */
803 /*PRINTFLIKE2*/
804 /*ARGSUSED*/
805 void
bop_printf(bootops_t * bop,const char * fmt,...)806 bop_printf(bootops_t *bop, const char *fmt, ...)
807 {
808 va_list ap;
809
810 if (have_console == 0)
811 return;
812
813 va_start(ap, fmt);
814 (void) vsnprintf(buffer, BUFFERSIZE, fmt, ap);
815 va_end(ap);
816 PUT_STRING(buffer);
817 }
818
819 /*
820 * Another panic() variant; this one can be used even earlier during boot than
821 * prom_panic().
822 */
823 /*PRINTFLIKE1*/
824 void
bop_panic(const char * fmt,...)825 bop_panic(const char *fmt, ...)
826 {
827 va_list ap;
828
829 va_start(ap, fmt);
830 bop_printf(NULL, fmt, ap);
831 va_end(ap);
832
833 bop_printf(NULL, "\nPress any key to reboot.\n");
834 (void) bcons_getchar();
835 bop_printf(NULL, "Resetting...\n");
836 pc_reset();
837 }
838
839 /*
840 * Do a real mode interrupt BIOS call
841 */
842 typedef struct bios_regs {
843 unsigned short ax, bx, cx, dx, si, di, bp, es, ds;
844 } bios_regs_t;
845 typedef int (*bios_func_t)(int, bios_regs_t *);
846
847 /*ARGSUSED*/
848 static void
do_bsys_doint(bootops_t * bop,int intnum,struct bop_regs * rp)849 do_bsys_doint(bootops_t *bop, int intnum, struct bop_regs *rp)
850 {
851 #if defined(__xpv)
852 prom_panic("unsupported call to BOP_DOINT()\n");
853 #else /* __xpv */
854 static int firsttime = 1;
855 bios_func_t bios_func = (bios_func_t)(void *)(uintptr_t)0x5000;
856 bios_regs_t br;
857
858 /*
859 * The first time we do this, we have to copy the pre-packaged
860 * low memory bios call code image into place.
861 */
862 if (firsttime) {
863 extern char bios_image[];
864 extern uint32_t bios_size;
865
866 bcopy(bios_image, (void *)bios_func, bios_size);
867 firsttime = 0;
868 }
869
870 br.ax = rp->eax.word.ax;
871 br.bx = rp->ebx.word.bx;
872 br.cx = rp->ecx.word.cx;
873 br.dx = rp->edx.word.dx;
874 br.bp = rp->ebp.word.bp;
875 br.si = rp->esi.word.si;
876 br.di = rp->edi.word.di;
877 br.ds = rp->ds;
878 br.es = rp->es;
879
880 DBG_MSG("Doing BIOS call...");
881 DBG(br.ax);
882 DBG(br.bx);
883 DBG(br.dx);
884 rp->eflags = bios_func(intnum, &br);
885 DBG_MSG("done\n");
886
887 rp->eax.word.ax = br.ax;
888 rp->ebx.word.bx = br.bx;
889 rp->ecx.word.cx = br.cx;
890 rp->edx.word.dx = br.dx;
891 rp->ebp.word.bp = br.bp;
892 rp->esi.word.si = br.si;
893 rp->edi.word.di = br.di;
894 rp->ds = br.ds;
895 rp->es = br.es;
896 #endif /* __xpv */
897 }
898
899 static struct boot_syscalls bop_sysp = {
900 bcons_getchar,
901 bcons_putchar,
902 bcons_ischar,
903 };
904
905 static char *whoami;
906
907 #define BUFLEN 64
908
909 #if defined(__xpv)
910
911 static char namebuf[32];
912
913 static void
xen_parse_props(char * s,char * prop_map[],int n_prop)914 xen_parse_props(char *s, char *prop_map[], int n_prop)
915 {
916 char **prop_name = prop_map;
917 char *cp = s, *scp;
918
919 do {
920 scp = cp;
921 while ((*cp != NULL) && (*cp != ':'))
922 cp++;
923
924 if ((scp != cp) && (*prop_name != NULL)) {
925 *cp = NULL;
926 bsetprops(*prop_name, scp);
927 }
928
929 cp++;
930 prop_name++;
931 n_prop--;
932 } while (n_prop > 0);
933 }
934
935 #define VBDPATHLEN 64
936
937 /*
938 * parse the 'xpv-root' property to create properties used by
939 * ufs_mountroot.
940 */
941 static void
xen_vbdroot_props(char * s)942 xen_vbdroot_props(char *s)
943 {
944 char vbdpath[VBDPATHLEN] = "/xpvd/xdf@";
945 const char lnamefix[] = "/dev/dsk/c0d";
946 char *pnp;
947 char *prop_p;
948 char mi;
949 short minor;
950 long addr = 0;
951
952 pnp = vbdpath + strlen(vbdpath);
953 prop_p = s + strlen(lnamefix);
954 while ((*prop_p != '\0') && (*prop_p != 's') && (*prop_p != 'p'))
955 addr = addr * 10 + *prop_p++ - '0';
956 (void) snprintf(pnp, VBDPATHLEN, "%lx", addr);
957 pnp = vbdpath + strlen(vbdpath);
958 if (*prop_p == 's')
959 mi = 'a';
960 else if (*prop_p == 'p')
961 mi = 'q';
962 else
963 ASSERT(0); /* shouldn't be here */
964 prop_p++;
965 ASSERT(*prop_p != '\0');
966 if (ISDIGIT(*prop_p)) {
967 minor = *prop_p - '0';
968 prop_p++;
969 if (ISDIGIT(*prop_p)) {
970 minor = minor * 10 + *prop_p - '0';
971 }
972 } else {
973 /* malformed root path, use 0 as default */
974 minor = 0;
975 }
976 ASSERT(minor < 16); /* at most 16 partitions */
977 mi += minor;
978 *pnp++ = ':';
979 *pnp++ = mi;
980 *pnp++ = '\0';
981 bsetprops("fstype", "ufs");
982 bsetprops("bootpath", vbdpath);
983
984 DBG_MSG("VBD bootpath set to ");
985 DBG_MSG(vbdpath);
986 DBG_MSG("\n");
987 }
988
989 /*
990 * parse the xpv-nfsroot property to create properties used by
991 * nfs_mountroot.
992 */
993 static void
xen_nfsroot_props(char * s)994 xen_nfsroot_props(char *s)
995 {
996 char *prop_map[] = {
997 BP_SERVER_IP, /* server IP address */
998 BP_SERVER_NAME, /* server hostname */
999 BP_SERVER_PATH, /* root path */
1000 };
1001 int n_prop = sizeof (prop_map) / sizeof (prop_map[0]);
1002
1003 bsetprop("fstype", 6, "nfs", 4);
1004
1005 xen_parse_props(s, prop_map, n_prop);
1006
1007 /*
1008 * If a server name wasn't specified, use a default.
1009 */
1010 if (do_bsys_getproplen(NULL, BP_SERVER_NAME) == -1)
1011 bsetprops(BP_SERVER_NAME, "unknown");
1012 }
1013
1014 /*
1015 * Extract our IP address, etc. from the "xpv-ip" property.
1016 */
1017 static void
xen_ip_props(char * s)1018 xen_ip_props(char *s)
1019 {
1020 char *prop_map[] = {
1021 BP_HOST_IP, /* IP address */
1022 NULL, /* NFS server IP address (ignored in */
1023 /* favour of xpv-nfsroot) */
1024 BP_ROUTER_IP, /* IP gateway */
1025 BP_SUBNET_MASK, /* IP subnet mask */
1026 "xpv-hostname", /* hostname (ignored) */
1027 BP_NETWORK_INTERFACE, /* interface name */
1028 "xpv-hcp", /* host configuration protocol */
1029 };
1030 int n_prop = sizeof (prop_map) / sizeof (prop_map[0]);
1031 char ifname[IFNAMSIZ];
1032
1033 xen_parse_props(s, prop_map, n_prop);
1034
1035 /*
1036 * A Linux dom0 administrator expects all interfaces to be
1037 * called "ethX", which is not the case here.
1038 *
1039 * If the interface name specified is "eth0", presume that
1040 * this is really intended to be "xnf0" (the first domU ->
1041 * dom0 interface for this domain).
1042 */
1043 if ((do_bsys_getprop(NULL, BP_NETWORK_INTERFACE, ifname) == 0) &&
1044 (strcmp("eth0", ifname) == 0)) {
1045 bsetprops(BP_NETWORK_INTERFACE, "xnf0");
1046 bop_printf(NULL,
1047 "network interface name 'eth0' replaced with 'xnf0'\n");
1048 }
1049 }
1050
1051 #else /* __xpv */
1052
1053 static void
setup_rarp_props(struct sol_netinfo * sip)1054 setup_rarp_props(struct sol_netinfo *sip)
1055 {
1056 char buf[BUFLEN]; /* to hold ip/mac addrs */
1057 uint8_t *val;
1058
1059 val = (uint8_t *)&sip->sn_ciaddr;
1060 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1061 val[0], val[1], val[2], val[3]);
1062 bsetprops(BP_HOST_IP, buf);
1063
1064 val = (uint8_t *)&sip->sn_siaddr;
1065 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1066 val[0], val[1], val[2], val[3]);
1067 bsetprops(BP_SERVER_IP, buf);
1068
1069 if (sip->sn_giaddr != 0) {
1070 val = (uint8_t *)&sip->sn_giaddr;
1071 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1072 val[0], val[1], val[2], val[3]);
1073 bsetprops(BP_ROUTER_IP, buf);
1074 }
1075
1076 if (sip->sn_netmask != 0) {
1077 val = (uint8_t *)&sip->sn_netmask;
1078 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1079 val[0], val[1], val[2], val[3]);
1080 bsetprops(BP_SUBNET_MASK, buf);
1081 }
1082
1083 if (sip->sn_mactype != 4 || sip->sn_maclen != 6) {
1084 bop_printf(NULL, "unsupported mac type %d, mac len %d\n",
1085 sip->sn_mactype, sip->sn_maclen);
1086 } else {
1087 val = sip->sn_macaddr;
1088 (void) snprintf(buf, BUFLEN, "%x:%x:%x:%x:%x:%x",
1089 val[0], val[1], val[2], val[3], val[4], val[5]);
1090 bsetprops(BP_BOOT_MAC, buf);
1091 }
1092 }
1093
1094 #endif /* __xpv */
1095
1096 static void
build_panic_cmdline(const char * cmd,int cmdlen)1097 build_panic_cmdline(const char *cmd, int cmdlen)
1098 {
1099 int proplen;
1100 size_t arglen;
1101
1102 arglen = sizeof (fastreboot_onpanic_args);
1103 /*
1104 * If we allready have fastreboot-onpanic set to zero,
1105 * don't add them again.
1106 */
1107 if ((proplen = do_bsys_getproplen(NULL, FASTREBOOT_ONPANIC)) > 0 &&
1108 proplen <= sizeof (fastreboot_onpanic_cmdline)) {
1109 (void) do_bsys_getprop(NULL, FASTREBOOT_ONPANIC,
1110 fastreboot_onpanic_cmdline);
1111 if (FASTREBOOT_ONPANIC_NOTSET(fastreboot_onpanic_cmdline))
1112 arglen = 1;
1113 }
1114
1115 /*
1116 * construct fastreboot_onpanic_cmdline
1117 */
1118 if (cmdlen + arglen > sizeof (fastreboot_onpanic_cmdline)) {
1119 DBG_MSG("Command line too long: clearing "
1120 FASTREBOOT_ONPANIC "\n");
1121 fastreboot_onpanic = 0;
1122 } else {
1123 bcopy(cmd, fastreboot_onpanic_cmdline, cmdlen);
1124 if (arglen != 1)
1125 bcopy(fastreboot_onpanic_args,
1126 fastreboot_onpanic_cmdline + cmdlen, arglen);
1127 else
1128 fastreboot_onpanic_cmdline[cmdlen] = 0;
1129 }
1130 }
1131
1132
1133 #ifndef __xpv
1134 /*
1135 * Construct boot command line for Fast Reboot. The saved_cmdline
1136 * is also reported by "eeprom bootcmd".
1137 */
1138 static void
build_fastboot_cmdline(struct xboot_info * xbp)1139 build_fastboot_cmdline(struct xboot_info *xbp)
1140 {
1141 saved_cmdline_len = strlen(xbp->bi_cmdline) + 1;
1142 if (saved_cmdline_len > FASTBOOT_SAVED_CMDLINE_LEN) {
1143 DBG(saved_cmdline_len);
1144 DBG_MSG("Command line too long: clearing fastreboot_capable\n");
1145 fastreboot_capable = 0;
1146 } else {
1147 bcopy((void *)(xbp->bi_cmdline), (void *)saved_cmdline,
1148 saved_cmdline_len);
1149 saved_cmdline[saved_cmdline_len - 1] = '\0';
1150 build_panic_cmdline(saved_cmdline, saved_cmdline_len - 1);
1151 }
1152 }
1153
1154 /*
1155 * Save memory layout, disk drive information, unix and boot archive sizes for
1156 * Fast Reboot.
1157 */
1158 static void
save_boot_info(struct xboot_info * xbi)1159 save_boot_info(struct xboot_info *xbi)
1160 {
1161 multiboot_info_t *mbi = xbi->bi_mb_info;
1162 struct boot_modules *modp;
1163 int i;
1164
1165 bcopy(mbi, &saved_mbi, sizeof (multiboot_info_t));
1166 if (mbi->mmap_length > sizeof (saved_mmap)) {
1167 DBG_MSG("mbi->mmap_length too big: clearing "
1168 "fastreboot_capable\n");
1169 fastreboot_capable = 0;
1170 } else {
1171 bcopy((void *)(uintptr_t)mbi->mmap_addr, (void *)saved_mmap,
1172 mbi->mmap_length);
1173 }
1174
1175 if ((mbi->flags & MB_INFO_DRIVE_INFO) != 0) {
1176 if (mbi->drives_length > sizeof (saved_drives)) {
1177 DBG(mbi->drives_length);
1178 DBG_MSG("mbi->drives_length too big: clearing "
1179 "fastreboot_capable\n");
1180 fastreboot_capable = 0;
1181 } else {
1182 bcopy((void *)(uintptr_t)mbi->drives_addr,
1183 (void *)saved_drives, mbi->drives_length);
1184 }
1185 } else {
1186 saved_mbi.drives_length = 0;
1187 saved_mbi.drives_addr = NULL;
1188 }
1189
1190 /*
1191 * Current file sizes. Used by fastboot.c to figure out how much
1192 * memory to reserve for panic reboot.
1193 * Use the module list from the dboot-constructed xboot_info
1194 * instead of the list referenced by the multiboot structure
1195 * because that structure may not be addressable now.
1196 */
1197 saved_file_size[FASTBOOT_NAME_UNIX] = FOUR_MEG - PAGESIZE;
1198 for (i = 0, modp = (struct boot_modules *)(uintptr_t)xbi->bi_modules;
1199 i < xbi->bi_module_cnt; i++, modp++) {
1200 saved_file_size[FASTBOOT_NAME_BOOTARCHIVE] += modp->bm_size;
1201 }
1202 }
1203 #endif /* __xpv */
1204
1205
1206 /*
1207 * 1st pass at building the table of boot properties. This includes:
1208 * - values set on the command line: -B a=x,b=y,c=z ....
1209 * - known values we just compute (ie. from xbp)
1210 * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
1211 *
1212 * the grub command line looked like:
1213 * kernel boot-file [-B prop=value[,prop=value]...] [boot-args]
1214 *
1215 * whoami is the same as boot-file
1216 */
1217 static void
build_boot_properties(struct xboot_info * xbp)1218 build_boot_properties(struct xboot_info *xbp)
1219 {
1220 char *name;
1221 int name_len;
1222 char *value;
1223 int value_len;
1224 struct boot_modules *bm, *rdbm;
1225 char *propbuf;
1226 int quoted = 0;
1227 int boot_arg_len;
1228 uint_t i, midx;
1229 char modid[32];
1230 #ifndef __xpv
1231 static int stdout_val = 0;
1232 uchar_t boot_device;
1233 char str[3];
1234 #endif
1235
1236 /*
1237 * These have to be done first, so that kobj_mount_root() works
1238 */
1239 DBG_MSG("Building boot properties\n");
1240 propbuf = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, 0);
1241 DBG((uintptr_t)propbuf);
1242 if (xbp->bi_module_cnt > 0) {
1243 bm = xbp->bi_modules;
1244 rdbm = NULL;
1245 for (midx = i = 0; i < xbp->bi_module_cnt; i++) {
1246 if (bm[i].bm_type == BMT_ROOTFS) {
1247 rdbm = &bm[i];
1248 continue;
1249 }
1250 if (bm[i].bm_type == BMT_HASH || bm[i].bm_name == NULL)
1251 continue;
1252
1253 (void) snprintf(modid, sizeof (modid),
1254 "module-name-%u", midx);
1255 bsetprops(modid, (char *)bm[i].bm_name);
1256 (void) snprintf(modid, sizeof (modid),
1257 "module-addr-%u", midx);
1258 bsetprop64(modid, (uint64_t)(uintptr_t)bm[i].bm_addr);
1259 (void) snprintf(modid, sizeof (modid),
1260 "module-size-%u", midx);
1261 bsetprop64(modid, (uint64_t)bm[i].bm_size);
1262 ++midx;
1263 }
1264 if (rdbm != NULL) {
1265 bsetprop64("ramdisk_start",
1266 (uint64_t)(uintptr_t)rdbm->bm_addr);
1267 bsetprop64("ramdisk_end",
1268 (uint64_t)(uintptr_t)rdbm->bm_addr + rdbm->bm_size);
1269 }
1270 }
1271
1272 /*
1273 * If there are any boot time modules or hashes present, then disable
1274 * fast reboot.
1275 */
1276 if (xbp->bi_module_cnt > 1) {
1277 fastreboot_disable(FBNS_BOOTMOD);
1278 }
1279
1280 #ifndef __xpv
1281 /*
1282 * Disable fast reboot if we're using the Multiboot 2 boot protocol,
1283 * since we don't currently support MB2 info and module relocation.
1284 * Note that fast reboot will have already been disabled if multiple
1285 * modules are present, since the current implementation assumes that
1286 * we only have a single module, the boot_archive.
1287 */
1288 if (xbp->bi_mb_version != 1) {
1289 fastreboot_disable(FBNS_MULTIBOOT2);
1290 }
1291 #endif
1292
1293 DBG_MSG("Parsing command line for boot properties\n");
1294 value = xbp->bi_cmdline;
1295
1296 /*
1297 * allocate memory to collect boot_args into
1298 */
1299 boot_arg_len = strlen(xbp->bi_cmdline) + 1;
1300 boot_args = do_bsys_alloc(NULL, NULL, boot_arg_len, MMU_PAGESIZE);
1301 boot_args[0] = 0;
1302 boot_arg_len = 0;
1303
1304 #ifdef __xpv
1305 /*
1306 * Xen puts a lot of device information in front of the kernel name
1307 * let's grab them and make them boot properties. The first
1308 * string w/o an "=" in it will be the boot-file property.
1309 */
1310 (void) strcpy(namebuf, "xpv-");
1311 for (;;) {
1312 /*
1313 * get to next property
1314 */
1315 while (ISSPACE(*value))
1316 ++value;
1317 name = value;
1318 /*
1319 * look for an "="
1320 */
1321 while (*value && !ISSPACE(*value) && *value != '=') {
1322 value++;
1323 }
1324 if (*value != '=') { /* no "=" in the property */
1325 value = name;
1326 break;
1327 }
1328 name_len = value - name;
1329 value_len = 0;
1330 /*
1331 * skip over the "="
1332 */
1333 value++;
1334 while (value[value_len] && !ISSPACE(value[value_len])) {
1335 ++value_len;
1336 }
1337 /*
1338 * build property name with "xpv-" prefix
1339 */
1340 if (name_len + 4 > 32) { /* skip if name too long */
1341 value += value_len;
1342 continue;
1343 }
1344 bcopy(name, &namebuf[4], name_len);
1345 name_len += 4;
1346 namebuf[name_len] = 0;
1347 bcopy(value, propbuf, value_len);
1348 propbuf[value_len] = 0;
1349 bsetprops(namebuf, propbuf);
1350
1351 /*
1352 * xpv-root is set to the logical disk name of the xen
1353 * VBD when booting from a disk-based filesystem.
1354 */
1355 if (strcmp(namebuf, "xpv-root") == 0)
1356 xen_vbdroot_props(propbuf);
1357 /*
1358 * While we're here, if we have a "xpv-nfsroot" property
1359 * then we need to set "fstype" to "nfs" so we mount
1360 * our root from the nfs server. Also parse the xpv-nfsroot
1361 * property to create the properties that nfs_mountroot will
1362 * need to find the root and mount it.
1363 */
1364 if (strcmp(namebuf, "xpv-nfsroot") == 0)
1365 xen_nfsroot_props(propbuf);
1366
1367 if (strcmp(namebuf, "xpv-ip") == 0)
1368 xen_ip_props(propbuf);
1369 value += value_len;
1370 }
1371 #endif
1372
1373 while (ISSPACE(*value))
1374 ++value;
1375 /*
1376 * value now points at the boot-file
1377 */
1378 value_len = 0;
1379 while (value[value_len] && !ISSPACE(value[value_len]))
1380 ++value_len;
1381 if (value_len > 0) {
1382 whoami = propbuf;
1383 bcopy(value, whoami, value_len);
1384 whoami[value_len] = 0;
1385 bsetprops("boot-file", whoami);
1386 /*
1387 * strip leading path stuff from whoami, so running from
1388 * PXE/miniroot makes sense.
1389 */
1390 if (strstr(whoami, "/platform/") != NULL)
1391 whoami = strstr(whoami, "/platform/");
1392 bsetprops("whoami", whoami);
1393 }
1394
1395 /*
1396 * Values forcibly set boot properties on the command line via -B.
1397 * Allow use of quotes in values. Other stuff goes on kernel
1398 * command line.
1399 */
1400 name = value + value_len;
1401 while (*name != 0) {
1402 /*
1403 * anything not " -B" is copied to the command line
1404 */
1405 if (!ISSPACE(name[0]) || name[1] != '-' || name[2] != 'B') {
1406 boot_args[boot_arg_len++] = *name;
1407 boot_args[boot_arg_len] = 0;
1408 ++name;
1409 continue;
1410 }
1411
1412 /*
1413 * skip the " -B" and following white space
1414 */
1415 name += 3;
1416 while (ISSPACE(*name))
1417 ++name;
1418 while (*name && !ISSPACE(*name)) {
1419 value = strstr(name, "=");
1420 if (value == NULL)
1421 break;
1422 name_len = value - name;
1423 ++value;
1424 value_len = 0;
1425 quoted = 0;
1426 for (; ; ++value_len) {
1427 if (!value[value_len])
1428 break;
1429
1430 /*
1431 * is this value quoted?
1432 */
1433 if (value_len == 0 &&
1434 (value[0] == '\'' || value[0] == '"')) {
1435 quoted = value[0];
1436 ++value_len;
1437 }
1438
1439 /*
1440 * In the quote accept any character,
1441 * but look for ending quote.
1442 */
1443 if (quoted) {
1444 if (value[value_len] == quoted)
1445 quoted = 0;
1446 continue;
1447 }
1448
1449 /*
1450 * a comma or white space ends the value
1451 */
1452 if (value[value_len] == ',' ||
1453 ISSPACE(value[value_len]))
1454 break;
1455 }
1456
1457 if (value_len == 0) {
1458 bsetprop(name, name_len, "true", 5);
1459 } else {
1460 char *v = value;
1461 int l = value_len;
1462 if (v[0] == v[l - 1] &&
1463 (v[0] == '\'' || v[0] == '"')) {
1464 ++v;
1465 l -= 2;
1466 }
1467 bcopy(v, propbuf, l);
1468 propbuf[l] = '\0';
1469 bsetprop(name, name_len, propbuf,
1470 l + 1);
1471 }
1472 name = value + value_len;
1473 while (*name == ',')
1474 ++name;
1475 }
1476 }
1477
1478 /*
1479 * set boot-args property
1480 * 1275 name is bootargs, so set
1481 * that too
1482 */
1483 bsetprops("boot-args", boot_args);
1484 bsetprops("bootargs", boot_args);
1485
1486 #ifndef __xpv
1487 /*
1488 * Build boot command line for Fast Reboot
1489 */
1490 build_fastboot_cmdline(xbp);
1491
1492 if (xbp->bi_mb_version == 1) {
1493 multiboot_info_t *mbi = xbp->bi_mb_info;
1494 int netboot;
1495 struct sol_netinfo *sip;
1496
1497 /*
1498 * set the BIOS boot device from GRUB
1499 */
1500 netboot = 0;
1501
1502 /*
1503 * Save various boot information for Fast Reboot
1504 */
1505 save_boot_info(xbp);
1506
1507 if (mbi != NULL && mbi->flags & MB_INFO_BOOTDEV) {
1508 boot_device = mbi->boot_device >> 24;
1509 if (boot_device == 0x20)
1510 netboot++;
1511 str[0] = (boot_device >> 4) + '0';
1512 str[1] = (boot_device & 0xf) + '0';
1513 str[2] = 0;
1514 bsetprops("bios-boot-device", str);
1515 } else {
1516 netboot = 1;
1517 }
1518
1519 /*
1520 * In the netboot case, drives_info is overloaded with the
1521 * dhcp ack. This is not multiboot compliant and requires
1522 * special pxegrub!
1523 */
1524 if (netboot && mbi->drives_length != 0) {
1525 sip = (struct sol_netinfo *)(uintptr_t)mbi->drives_addr;
1526 if (sip->sn_infotype == SN_TYPE_BOOTP)
1527 bsetprop("bootp-response",
1528 sizeof ("bootp-response"),
1529 (void *)(uintptr_t)mbi->drives_addr,
1530 mbi->drives_length);
1531 else if (sip->sn_infotype == SN_TYPE_RARP)
1532 setup_rarp_props(sip);
1533 }
1534 } else {
1535 multiboot2_info_header_t *mbi = xbp->bi_mb_info;
1536 multiboot_tag_bootdev_t *bootdev = NULL;
1537 multiboot_tag_network_t *netdev = NULL;
1538
1539 if (mbi != NULL) {
1540 bootdev = dboot_multiboot2_find_tag(mbi,
1541 MULTIBOOT_TAG_TYPE_BOOTDEV);
1542 netdev = dboot_multiboot2_find_tag(mbi,
1543 MULTIBOOT_TAG_TYPE_NETWORK);
1544 }
1545 if (bootdev != NULL) {
1546 DBG(bootdev->mb_biosdev);
1547 boot_device = bootdev->mb_biosdev;
1548 str[0] = (boot_device >> 4) + '0';
1549 str[1] = (boot_device & 0xf) + '0';
1550 str[2] = 0;
1551 bsetprops("bios-boot-device", str);
1552 }
1553 if (netdev != NULL) {
1554 bsetprop("bootp-response", sizeof ("bootp-response"),
1555 (void *)(uintptr_t)netdev->mb_dhcpack,
1556 netdev->mb_size -
1557 sizeof (multiboot_tag_network_t));
1558 }
1559 }
1560
1561 bsetprop("stdout", strlen("stdout"),
1562 &stdout_val, sizeof (stdout_val));
1563 #endif /* __xpv */
1564
1565 /*
1566 * more conjured up values for made up things....
1567 */
1568 #if defined(__xpv)
1569 bsetprops("mfg-name", "i86xpv");
1570 bsetprops("impl-arch-name", "i86xpv");
1571 #else
1572 bsetprops("mfg-name", "i86pc");
1573 bsetprops("impl-arch-name", "i86pc");
1574 #endif
1575
1576 /*
1577 * Build firmware-provided system properties
1578 */
1579 build_firmware_properties(xbp);
1580
1581 /*
1582 * XXPV
1583 *
1584 * Find out what these are:
1585 * - cpuid_feature_ecx_include
1586 * - cpuid_feature_ecx_exclude
1587 * - cpuid_feature_edx_include
1588 * - cpuid_feature_edx_exclude
1589 *
1590 * Find out what these are in multiboot:
1591 * - netdev-path
1592 * - fstype
1593 */
1594 }
1595
1596 #ifdef __xpv
1597 /*
1598 * Under the Hypervisor, memory usable for DMA may be scarce. One
1599 * very likely large pool of DMA friendly memory is occupied by
1600 * the boot_archive, as it was loaded by grub into low MFNs.
1601 *
1602 * Here we free up that memory by copying the boot archive to what are
1603 * likely higher MFN pages and then swapping the mfn/pfn mappings.
1604 */
1605 #define PFN_2GIG 0x80000
1606 static void
relocate_boot_archive(struct xboot_info * xbp)1607 relocate_boot_archive(struct xboot_info *xbp)
1608 {
1609 mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
1610 struct boot_modules *bm = xbp->bi_modules;
1611 uintptr_t va;
1612 pfn_t va_pfn;
1613 mfn_t va_mfn;
1614 caddr_t copy;
1615 pfn_t copy_pfn;
1616 mfn_t copy_mfn;
1617 size_t len;
1618 int slop;
1619 int total = 0;
1620 int relocated = 0;
1621 int mmu_update_return;
1622 mmu_update_t t[2];
1623 x86pte_t pte;
1624
1625 /*
1626 * If all MFN's are below 2Gig, don't bother doing this.
1627 */
1628 if (max_mfn < PFN_2GIG)
1629 return;
1630 if (xbp->bi_module_cnt < 1) {
1631 DBG_MSG("no boot_archive!");
1632 return;
1633 }
1634
1635 DBG_MSG("moving boot_archive to high MFN memory\n");
1636 va = (uintptr_t)bm->bm_addr;
1637 len = bm->bm_size;
1638 slop = va & MMU_PAGEOFFSET;
1639 if (slop) {
1640 va += MMU_PAGESIZE - slop;
1641 len -= MMU_PAGESIZE - slop;
1642 }
1643 len = P2ALIGN(len, MMU_PAGESIZE);
1644
1645 /*
1646 * Go through all boot_archive pages, swapping any low MFN pages
1647 * with memory at next_phys.
1648 */
1649 while (len != 0) {
1650 ++total;
1651 va_pfn = mmu_btop(va - ONE_GIG);
1652 va_mfn = mfn_list[va_pfn];
1653 if (mfn_list[va_pfn] < PFN_2GIG) {
1654 copy = kbm_remap_window(next_phys, 1);
1655 bcopy((void *)va, copy, MMU_PAGESIZE);
1656 copy_pfn = mmu_btop(next_phys);
1657 copy_mfn = mfn_list[copy_pfn];
1658
1659 pte = mfn_to_ma(copy_mfn) | PT_NOCONSIST | PT_VALID;
1660 if (HYPERVISOR_update_va_mapping(va, pte,
1661 UVMF_INVLPG | UVMF_LOCAL))
1662 bop_panic("relocate_boot_archive(): "
1663 "HYPERVISOR_update_va_mapping() failed");
1664
1665 mfn_list[va_pfn] = copy_mfn;
1666 mfn_list[copy_pfn] = va_mfn;
1667
1668 t[0].ptr = mfn_to_ma(copy_mfn) | MMU_MACHPHYS_UPDATE;
1669 t[0].val = va_pfn;
1670 t[1].ptr = mfn_to_ma(va_mfn) | MMU_MACHPHYS_UPDATE;
1671 t[1].val = copy_pfn;
1672 if (HYPERVISOR_mmu_update(t, 2, &mmu_update_return,
1673 DOMID_SELF) != 0 || mmu_update_return != 2)
1674 bop_panic("relocate_boot_archive(): "
1675 "HYPERVISOR_mmu_update() failed");
1676
1677 next_phys += MMU_PAGESIZE;
1678 ++relocated;
1679 }
1680 len -= MMU_PAGESIZE;
1681 va += MMU_PAGESIZE;
1682 }
1683 DBG_MSG("Relocated pages:\n");
1684 DBG(relocated);
1685 DBG_MSG("Out of total pages:\n");
1686 DBG(total);
1687 }
1688 #endif /* __xpv */
1689
1690 #if !defined(__xpv)
1691 /*
1692 * Install a temporary IDT that lets us catch errors in the boot time code.
1693 * We shouldn't get any faults at all while this is installed, so we'll
1694 * just generate a traceback and exit.
1695 */
1696 #ifdef __amd64
1697 static const int bcode_sel = B64CODE_SEL;
1698 #else
1699 static const int bcode_sel = B32CODE_SEL;
1700 #endif
1701
1702 /*
1703 * simple description of a stack frame (args are 32 bit only currently)
1704 */
1705 typedef struct bop_frame {
1706 struct bop_frame *old_frame;
1707 pc_t retaddr;
1708 long arg[1];
1709 } bop_frame_t;
1710
1711 void
bop_traceback(bop_frame_t * frame)1712 bop_traceback(bop_frame_t *frame)
1713 {
1714 pc_t pc;
1715 int cnt;
1716 char *ksym;
1717 ulong_t off;
1718 #if defined(__i386)
1719 int a;
1720 #endif
1721
1722 bop_printf(NULL, "Stack traceback:\n");
1723 for (cnt = 0; cnt < 30; ++cnt) { /* up to 30 frames */
1724 pc = frame->retaddr;
1725 if (pc == 0)
1726 break;
1727 ksym = kobj_getsymname(pc, &off);
1728 if (ksym)
1729 bop_printf(NULL, " %s+%lx", ksym, off);
1730 else
1731 bop_printf(NULL, " 0x%lx", pc);
1732
1733 frame = frame->old_frame;
1734 if (frame == 0) {
1735 bop_printf(NULL, "\n");
1736 break;
1737 }
1738 #if defined(__i386)
1739 for (a = 0; a < 6; ++a) { /* try for 6 args */
1740 if ((void *)&frame->arg[a] == (void *)frame->old_frame)
1741 break;
1742 if (a == 0)
1743 bop_printf(NULL, "(");
1744 else
1745 bop_printf(NULL, ",");
1746 bop_printf(NULL, "0x%lx", frame->arg[a]);
1747 }
1748 bop_printf(NULL, ")");
1749 #endif
1750 bop_printf(NULL, "\n");
1751 }
1752 }
1753
1754 struct trapframe {
1755 ulong_t error_code; /* optional */
1756 ulong_t inst_ptr;
1757 ulong_t code_seg;
1758 ulong_t flags_reg;
1759 #ifdef __amd64
1760 ulong_t stk_ptr;
1761 ulong_t stk_seg;
1762 #endif
1763 };
1764
1765 void
bop_trap(ulong_t * tfp)1766 bop_trap(ulong_t *tfp)
1767 {
1768 struct trapframe *tf = (struct trapframe *)tfp;
1769 bop_frame_t fakeframe;
1770 static int depth = 0;
1771
1772 /*
1773 * Check for an infinite loop of traps.
1774 */
1775 if (++depth > 2)
1776 bop_panic("Nested trap");
1777
1778 bop_printf(NULL, "Unexpected trap\n");
1779
1780 /*
1781 * adjust the tf for optional error_code by detecting the code selector
1782 */
1783 if (tf->code_seg != bcode_sel)
1784 tf = (struct trapframe *)(tfp - 1);
1785 else
1786 bop_printf(NULL, "error code 0x%lx\n",
1787 tf->error_code & 0xffffffff);
1788
1789 bop_printf(NULL, "instruction pointer 0x%lx\n", tf->inst_ptr);
1790 bop_printf(NULL, "code segment 0x%lx\n", tf->code_seg & 0xffff);
1791 bop_printf(NULL, "flags register 0x%lx\n", tf->flags_reg);
1792 #ifdef __amd64
1793 bop_printf(NULL, "return %%rsp 0x%lx\n", tf->stk_ptr);
1794 bop_printf(NULL, "return %%ss 0x%lx\n", tf->stk_seg & 0xffff);
1795 #endif
1796
1797 /* grab %[er]bp pushed by our code from the stack */
1798 fakeframe.old_frame = (bop_frame_t *)*(tfp - 3);
1799 fakeframe.retaddr = (pc_t)tf->inst_ptr;
1800 bop_printf(NULL, "Attempting stack backtrace:\n");
1801 bop_traceback(&fakeframe);
1802 bop_panic("unexpected trap in early boot");
1803 }
1804
1805 extern void bop_trap_handler(void);
1806
1807 static gate_desc_t *bop_idt;
1808
1809 static desctbr_t bop_idt_info;
1810
1811 static void
bop_idt_init(void)1812 bop_idt_init(void)
1813 {
1814 int t;
1815
1816 bop_idt = (gate_desc_t *)
1817 do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
1818 bzero(bop_idt, MMU_PAGESIZE);
1819 for (t = 0; t < NIDT; ++t) {
1820 /*
1821 * Note that since boot runs without a TSS, the
1822 * double fault handler cannot use an alternate stack
1823 * (64-bit) or a task gate (32-bit).
1824 */
1825 set_gatesegd(&bop_idt[t], &bop_trap_handler, bcode_sel,
1826 SDT_SYSIGT, TRP_KPL, 0);
1827 }
1828 bop_idt_info.dtr_limit = (NIDT * sizeof (gate_desc_t)) - 1;
1829 bop_idt_info.dtr_base = (uintptr_t)bop_idt;
1830 wr_idtr(&bop_idt_info);
1831 }
1832 #endif /* !defined(__xpv) */
1833
1834 /*
1835 * This is where we enter the kernel. It dummies up the boot_ops and
1836 * boot_syscalls vectors and jumps off to _kobj_boot()
1837 */
1838 void
_start(struct xboot_info * xbp)1839 _start(struct xboot_info *xbp)
1840 {
1841 bootops_t *bops = &bootop;
1842 extern void _kobj_boot();
1843
1844 /*
1845 * 1st off - initialize the console for any error messages
1846 */
1847 xbootp = xbp;
1848 #ifdef __xpv
1849 HYPERVISOR_shared_info = (void *)xbp->bi_shared_info;
1850 xen_info = xbp->bi_xen_start_info;
1851 #endif
1852
1853 #ifndef __xpv
1854 if (*((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) ==
1855 FASTBOOT_MAGIC) {
1856 post_fastreboot = 1;
1857 *((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) = 0;
1858 }
1859 #endif
1860
1861 bcons_init((void *)xbp->bi_cmdline);
1862 have_console = 1;
1863
1864 /*
1865 * enable debugging
1866 */
1867 if (strstr((char *)xbp->bi_cmdline, "kbm_debug"))
1868 kbm_debug = 1;
1869
1870 DBG_MSG("\n\n*** Entered Solaris in _start() cmdline is: ");
1871 DBG_MSG((char *)xbp->bi_cmdline);
1872 DBG_MSG("\n\n\n");
1873
1874 /*
1875 * physavail is no longer used by startup
1876 */
1877 bm.physinstalled = xbp->bi_phys_install;
1878 bm.pcimem = xbp->bi_pcimem;
1879 bm.rsvdmem = xbp->bi_rsvdmem;
1880 bm.physavail = NULL;
1881
1882 /*
1883 * initialize the boot time allocator
1884 */
1885 next_phys = xbp->bi_next_paddr;
1886 DBG(next_phys);
1887 next_virt = (uintptr_t)xbp->bi_next_vaddr;
1888 DBG(next_virt);
1889 DBG_MSG("Initializing boot time memory management...");
1890 #ifdef __xpv
1891 {
1892 xen_platform_parameters_t p;
1893
1894 /* This call shouldn't fail, dboot already did it once. */
1895 (void) HYPERVISOR_xen_version(XENVER_platform_parameters, &p);
1896 mfn_to_pfn_mapping = (pfn_t *)(xen_virt_start = p.virt_start);
1897 DBG(xen_virt_start);
1898 }
1899 #endif
1900 kbm_init(xbp);
1901 DBG_MSG("done\n");
1902
1903 /*
1904 * Fill in the bootops vector
1905 */
1906 bops->bsys_version = BO_VERSION;
1907 bops->boot_mem = &bm;
1908 bops->bsys_alloc = do_bsys_alloc;
1909 bops->bsys_free = do_bsys_free;
1910 bops->bsys_getproplen = do_bsys_getproplen;
1911 bops->bsys_getprop = do_bsys_getprop;
1912 bops->bsys_nextprop = do_bsys_nextprop;
1913 bops->bsys_printf = bop_printf;
1914 bops->bsys_doint = do_bsys_doint;
1915
1916 /*
1917 * BOP_EALLOC() is no longer needed
1918 */
1919 bops->bsys_ealloc = do_bsys_ealloc;
1920
1921 #ifdef __xpv
1922 /*
1923 * On domain 0 we need to free up some physical memory that is
1924 * usable for DMA. Since GRUB loaded the boot_archive, it is
1925 * sitting in low MFN memory. We'll relocated the boot archive
1926 * pages to high PFN memory.
1927 */
1928 if (DOMAIN_IS_INITDOMAIN(xen_info))
1929 relocate_boot_archive(xbp);
1930 #endif
1931
1932 #ifndef __xpv
1933 /*
1934 * Install an IDT to catch early pagefaults (shouldn't have any).
1935 * Also needed for kmdb.
1936 */
1937 bop_idt_init();
1938 #endif
1939
1940 /*
1941 * Start building the boot properties from the command line
1942 */
1943 DBG_MSG("Initializing boot properties:\n");
1944 build_boot_properties(xbp);
1945
1946 if (strstr((char *)xbp->bi_cmdline, "prom_debug") || kbm_debug) {
1947 char *value;
1948
1949 value = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
1950 boot_prop_display(value);
1951 }
1952
1953 /*
1954 * jump into krtld...
1955 */
1956 _kobj_boot(&bop_sysp, NULL, bops, NULL);
1957 }
1958
1959
1960 /*ARGSUSED*/
1961 static caddr_t
no_more_alloc(bootops_t * bop,caddr_t virthint,size_t size,int align)1962 no_more_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
1963 {
1964 panic("Attempt to bsys_alloc() too late\n");
1965 return (NULL);
1966 }
1967
1968 /*ARGSUSED*/
1969 static void
no_more_free(bootops_t * bop,caddr_t virt,size_t size)1970 no_more_free(bootops_t *bop, caddr_t virt, size_t size)
1971 {
1972 panic("Attempt to bsys_free() too late\n");
1973 }
1974
1975 void
bop_no_more_mem(void)1976 bop_no_more_mem(void)
1977 {
1978 DBG(total_bop_alloc_scratch);
1979 DBG(total_bop_alloc_kernel);
1980 bootops->bsys_alloc = no_more_alloc;
1981 bootops->bsys_free = no_more_free;
1982 }
1983
1984
1985 /*
1986 * Set ACPI firmware properties
1987 */
1988
1989 static caddr_t
vmap_phys(size_t length,paddr_t pa)1990 vmap_phys(size_t length, paddr_t pa)
1991 {
1992 paddr_t start, end;
1993 caddr_t va;
1994 size_t len, page;
1995
1996 #ifdef __xpv
1997 pa = pfn_to_pa(xen_assign_pfn(mmu_btop(pa))) | (pa & MMU_PAGEOFFSET);
1998 #endif
1999 start = P2ALIGN(pa, MMU_PAGESIZE);
2000 end = P2ROUNDUP(pa + length, MMU_PAGESIZE);
2001 len = end - start;
2002 va = (caddr_t)alloc_vaddr(len, MMU_PAGESIZE);
2003 for (page = 0; page < len; page += MMU_PAGESIZE)
2004 kbm_map((uintptr_t)va + page, start + page, 0, 0);
2005 return (va + (pa & MMU_PAGEOFFSET));
2006 }
2007
2008 static uint8_t
checksum_table(uint8_t * tp,size_t len)2009 checksum_table(uint8_t *tp, size_t len)
2010 {
2011 uint8_t sum = 0;
2012
2013 while (len-- > 0)
2014 sum += *tp++;
2015
2016 return (sum);
2017 }
2018
2019 static int
valid_rsdp(ACPI_TABLE_RSDP * rp)2020 valid_rsdp(ACPI_TABLE_RSDP *rp)
2021 {
2022
2023 /* validate the V1.x checksum */
2024 if (checksum_table((uint8_t *)rp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
2025 return (0);
2026
2027 /* If pre-ACPI 2.0, this is a valid RSDP */
2028 if (rp->Revision < 2)
2029 return (1);
2030
2031 /* validate the V2.x checksum */
2032 if (checksum_table((uint8_t *)rp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)
2033 return (0);
2034
2035 return (1);
2036 }
2037
2038 /*
2039 * Scan memory range for an RSDP;
2040 * see ACPI 3.0 Spec, 5.2.5.1
2041 */
2042 static ACPI_TABLE_RSDP *
scan_rsdp(paddr_t start,paddr_t end)2043 scan_rsdp(paddr_t start, paddr_t end)
2044 {
2045 ssize_t len = end - start;
2046 caddr_t ptr;
2047
2048 ptr = vmap_phys(len, start);
2049 while (len > 0) {
2050 if (strncmp(ptr, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP)) == 0 &&
2051 valid_rsdp((ACPI_TABLE_RSDP *)ptr))
2052 return ((ACPI_TABLE_RSDP *)ptr);
2053
2054 ptr += ACPI_RSDP_SCAN_STEP;
2055 len -= ACPI_RSDP_SCAN_STEP;
2056 }
2057
2058 return (NULL);
2059 }
2060
2061 /*
2062 * Refer to ACPI 3.0 Spec, section 5.2.5.1 to understand this function
2063 */
2064 static ACPI_TABLE_RSDP *
find_rsdp()2065 find_rsdp()
2066 {
2067 ACPI_TABLE_RSDP *rsdp;
2068 uint64_t rsdp_val = 0;
2069 uint16_t *ebda_seg;
2070 paddr_t ebda_addr;
2071
2072 /* check for "acpi-root-tab" property */
2073 if (do_bsys_getproplen(NULL, "acpi-root-tab") == sizeof (uint64_t)) {
2074 (void) do_bsys_getprop(NULL, "acpi-root-tab", &rsdp_val);
2075 if (rsdp_val != 0) {
2076 rsdp = scan_rsdp(rsdp_val, rsdp_val + sizeof (*rsdp));
2077 if (rsdp != NULL) {
2078 if (kbm_debug) {
2079 bop_printf(NULL,
2080 "Using RSDP from bootloader: "
2081 "0x%p\n", (void *)rsdp);
2082 }
2083 return (rsdp);
2084 }
2085 }
2086 }
2087
2088 /*
2089 * Get the EBDA segment and scan the first 1K
2090 */
2091 ebda_seg = (uint16_t *)vmap_phys(sizeof (uint16_t),
2092 ACPI_EBDA_PTR_LOCATION);
2093 ebda_addr = *ebda_seg << 4;
2094 rsdp = scan_rsdp(ebda_addr, ebda_addr + ACPI_EBDA_WINDOW_SIZE);
2095 if (rsdp == NULL)
2096 /* if EBDA doesn't contain RSDP, look in BIOS memory */
2097 rsdp = scan_rsdp(ACPI_HI_RSDP_WINDOW_BASE,
2098 ACPI_HI_RSDP_WINDOW_BASE + ACPI_HI_RSDP_WINDOW_SIZE);
2099 return (rsdp);
2100 }
2101
2102 static ACPI_TABLE_HEADER *
map_fw_table(paddr_t table_addr)2103 map_fw_table(paddr_t table_addr)
2104 {
2105 ACPI_TABLE_HEADER *tp;
2106 size_t len = MAX(sizeof (*tp), MMU_PAGESIZE);
2107
2108 /*
2109 * Map at least a page; if the table is larger than this, remap it
2110 */
2111 tp = (ACPI_TABLE_HEADER *)vmap_phys(len, table_addr);
2112 if (tp->Length > len)
2113 tp = (ACPI_TABLE_HEADER *)vmap_phys(tp->Length, table_addr);
2114 return (tp);
2115 }
2116
2117 static ACPI_TABLE_HEADER *
find_fw_table(char * signature)2118 find_fw_table(char *signature)
2119 {
2120 static int revision = 0;
2121 static ACPI_TABLE_XSDT *xsdt;
2122 static int len;
2123 paddr_t xsdt_addr;
2124 ACPI_TABLE_RSDP *rsdp;
2125 ACPI_TABLE_HEADER *tp;
2126 paddr_t table_addr;
2127 int n;
2128
2129 if (strlen(signature) != ACPI_NAME_SIZE)
2130 return (NULL);
2131
2132 /*
2133 * Reading the ACPI 3.0 Spec, section 5.2.5.3 will help
2134 * understand this code. If we haven't already found the RSDT/XSDT,
2135 * revision will be 0. Find the RSDP and check the revision
2136 * to find out whether to use the RSDT or XSDT. If revision is
2137 * 0 or 1, use the RSDT and set internal revision to 1; if it is 2,
2138 * use the XSDT. If the XSDT address is 0, though, fall back to
2139 * revision 1 and use the RSDT.
2140 */
2141 if (revision == 0) {
2142 if ((rsdp = find_rsdp()) != NULL) {
2143 revision = rsdp->Revision;
2144 /*
2145 * ACPI 6.0 states that current revision is 2
2146 * from acpi_table_rsdp definition:
2147 * Must be (0) for ACPI 1.0 or (2) for ACPI 2.0+
2148 */
2149 if (revision > 2)
2150 revision = 2;
2151 switch (revision) {
2152 case 2:
2153 /*
2154 * Use the XSDT unless BIOS is buggy and
2155 * claims to be rev 2 but has a null XSDT
2156 * address
2157 */
2158 xsdt_addr = rsdp->XsdtPhysicalAddress;
2159 if (xsdt_addr != 0)
2160 break;
2161 /* FALLTHROUGH */
2162 case 0:
2163 /* treat RSDP rev 0 as revision 1 internally */
2164 revision = 1;
2165 /* FALLTHROUGH */
2166 case 1:
2167 /* use the RSDT for rev 0/1 */
2168 xsdt_addr = rsdp->RsdtPhysicalAddress;
2169 break;
2170 default:
2171 /* unknown revision */
2172 revision = 0;
2173 break;
2174 }
2175 }
2176 if (revision == 0)
2177 return (NULL);
2178
2179 /* cache the XSDT info */
2180 xsdt = (ACPI_TABLE_XSDT *)map_fw_table(xsdt_addr);
2181 len = (xsdt->Header.Length - sizeof (xsdt->Header)) /
2182 ((revision == 1) ? sizeof (uint32_t) : sizeof (uint64_t));
2183 }
2184
2185 /*
2186 * Scan the table headers looking for a signature match
2187 */
2188 for (n = 0; n < len; n++) {
2189 ACPI_TABLE_RSDT *rsdt = (ACPI_TABLE_RSDT *)xsdt;
2190 table_addr = (revision == 1) ? rsdt->TableOffsetEntry[n] :
2191 xsdt->TableOffsetEntry[n];
2192
2193 if (table_addr == 0)
2194 continue;
2195 tp = map_fw_table(table_addr);
2196 if (strncmp(tp->Signature, signature, ACPI_NAME_SIZE) == 0) {
2197 return (tp);
2198 }
2199 }
2200 return (NULL);
2201 }
2202
2203 static void
process_mcfg(ACPI_TABLE_MCFG * tp)2204 process_mcfg(ACPI_TABLE_MCFG *tp)
2205 {
2206 ACPI_MCFG_ALLOCATION *cfg_baap;
2207 char *cfg_baa_endp;
2208 int64_t ecfginfo[4];
2209
2210 cfg_baap = (ACPI_MCFG_ALLOCATION *)((uintptr_t)tp + sizeof (*tp));
2211 cfg_baa_endp = ((char *)tp) + tp->Header.Length;
2212 while ((char *)cfg_baap < cfg_baa_endp) {
2213 if (cfg_baap->Address != 0 && cfg_baap->PciSegment == 0) {
2214 ecfginfo[0] = cfg_baap->Address;
2215 ecfginfo[1] = cfg_baap->PciSegment;
2216 ecfginfo[2] = cfg_baap->StartBusNumber;
2217 ecfginfo[3] = cfg_baap->EndBusNumber;
2218 bsetprop(MCFG_PROPNAME, strlen(MCFG_PROPNAME),
2219 ecfginfo, sizeof (ecfginfo));
2220 break;
2221 }
2222 cfg_baap++;
2223 }
2224 }
2225
2226 #ifndef __xpv
2227 static void
process_madt_entries(ACPI_TABLE_MADT * tp,uint32_t * cpu_countp,uint32_t * cpu_possible_countp,uint32_t * cpu_apicid_array)2228 process_madt_entries(ACPI_TABLE_MADT *tp, uint32_t *cpu_countp,
2229 uint32_t *cpu_possible_countp, uint32_t *cpu_apicid_array)
2230 {
2231 ACPI_SUBTABLE_HEADER *item, *end;
2232 uint32_t cpu_count = 0;
2233 uint32_t cpu_possible_count = 0;
2234
2235 /*
2236 * Determine number of CPUs and keep track of "final" APIC ID
2237 * for each CPU by walking through ACPI MADT processor list
2238 */
2239 end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2240 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2241
2242 while (item < end) {
2243 switch (item->Type) {
2244 case ACPI_MADT_TYPE_LOCAL_APIC: {
2245 ACPI_MADT_LOCAL_APIC *cpu =
2246 (ACPI_MADT_LOCAL_APIC *) item;
2247
2248 if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2249 if (cpu_apicid_array != NULL)
2250 cpu_apicid_array[cpu_count] = cpu->Id;
2251 cpu_count++;
2252 }
2253 cpu_possible_count++;
2254 break;
2255 }
2256 case ACPI_MADT_TYPE_LOCAL_X2APIC: {
2257 ACPI_MADT_LOCAL_X2APIC *cpu =
2258 (ACPI_MADT_LOCAL_X2APIC *) item;
2259
2260 if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2261 if (cpu_apicid_array != NULL)
2262 cpu_apicid_array[cpu_count] =
2263 cpu->LocalApicId;
2264 cpu_count++;
2265 }
2266 cpu_possible_count++;
2267 break;
2268 }
2269 default:
2270 if (kbm_debug)
2271 bop_printf(NULL, "MADT type %d\n", item->Type);
2272 break;
2273 }
2274
2275 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)item + item->Length);
2276 }
2277 if (cpu_countp)
2278 *cpu_countp = cpu_count;
2279 if (cpu_possible_countp)
2280 *cpu_possible_countp = cpu_possible_count;
2281 }
2282
2283 static void
process_madt(ACPI_TABLE_MADT * tp)2284 process_madt(ACPI_TABLE_MADT *tp)
2285 {
2286 uint32_t cpu_count = 0;
2287 uint32_t cpu_possible_count = 0;
2288 uint32_t *cpu_apicid_array; /* x2APIC ID is 32bit! */
2289
2290 if (tp != NULL) {
2291 /* count cpu's */
2292 process_madt_entries(tp, &cpu_count, &cpu_possible_count, NULL);
2293
2294 cpu_apicid_array = (uint32_t *)do_bsys_alloc(NULL, NULL,
2295 cpu_count * sizeof (*cpu_apicid_array), MMU_PAGESIZE);
2296 if (cpu_apicid_array == NULL)
2297 bop_panic("Not enough memory for APIC ID array");
2298
2299 /* copy IDs */
2300 process_madt_entries(tp, NULL, NULL, cpu_apicid_array);
2301
2302 /*
2303 * Make boot property for array of "final" APIC IDs for each
2304 * CPU
2305 */
2306 bsetprop(BP_CPU_APICID_ARRAY, strlen(BP_CPU_APICID_ARRAY),
2307 cpu_apicid_array, cpu_count * sizeof (*cpu_apicid_array));
2308 }
2309
2310 /*
2311 * Check whether property plat-max-ncpus is already set.
2312 */
2313 if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2314 /*
2315 * Set plat-max-ncpus to number of maximum possible CPUs given
2316 * in MADT if it hasn't been set.
2317 * There's no formal way to detect max possible CPUs supported
2318 * by platform according to ACPI spec3.0b. So current CPU
2319 * hotplug implementation expects that all possible CPUs will
2320 * have an entry in MADT table and set plat-max-ncpus to number
2321 * of entries in MADT.
2322 * With introducing of ACPI4.0, Maximum System Capability Table
2323 * (MSCT) provides maximum number of CPUs supported by platform.
2324 * If MSCT is unavailable, fall back to old way.
2325 */
2326 if (tp != NULL)
2327 bsetpropsi(PLAT_MAX_NCPUS_NAME, cpu_possible_count);
2328 }
2329
2330 /*
2331 * Set boot property boot-max-ncpus to number of CPUs existing at
2332 * boot time. boot-max-ncpus is mainly used for optimization.
2333 */
2334 if (tp != NULL)
2335 bsetpropsi(BOOT_MAX_NCPUS_NAME, cpu_count);
2336
2337 /*
2338 * User-set boot-ncpus overrides firmware count
2339 */
2340 if (do_bsys_getproplen(NULL, BOOT_NCPUS_NAME) >= 0)
2341 return;
2342
2343 /*
2344 * Set boot property boot-ncpus to number of active CPUs given in MADT
2345 * if it hasn't been set yet.
2346 */
2347 if (tp != NULL)
2348 bsetpropsi(BOOT_NCPUS_NAME, cpu_count);
2349 }
2350
2351 static void
process_srat(ACPI_TABLE_SRAT * tp)2352 process_srat(ACPI_TABLE_SRAT *tp)
2353 {
2354 ACPI_SUBTABLE_HEADER *item, *end;
2355 int i;
2356 int proc_num, mem_num;
2357 #pragma pack(1)
2358 struct {
2359 uint32_t domain;
2360 uint32_t apic_id;
2361 uint32_t sapic_id;
2362 } processor;
2363 struct {
2364 uint32_t domain;
2365 uint32_t x2apic_id;
2366 } x2apic;
2367 struct {
2368 uint32_t domain;
2369 uint64_t addr;
2370 uint64_t length;
2371 uint32_t flags;
2372 } memory;
2373 #pragma pack()
2374 char prop_name[30];
2375 uint64_t maxmem = 0;
2376
2377 if (tp == NULL)
2378 return;
2379
2380 proc_num = mem_num = 0;
2381 end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2382 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2383 while (item < end) {
2384 switch (item->Type) {
2385 case ACPI_SRAT_TYPE_CPU_AFFINITY: {
2386 ACPI_SRAT_CPU_AFFINITY *cpu =
2387 (ACPI_SRAT_CPU_AFFINITY *) item;
2388
2389 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2390 break;
2391 processor.domain = cpu->ProximityDomainLo;
2392 for (i = 0; i < 3; i++)
2393 processor.domain +=
2394 cpu->ProximityDomainHi[i] << ((i + 1) * 8);
2395 processor.apic_id = cpu->ApicId;
2396 processor.sapic_id = cpu->LocalSapicEid;
2397 (void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2398 proc_num);
2399 bsetprop(prop_name, strlen(prop_name), &processor,
2400 sizeof (processor));
2401 proc_num++;
2402 break;
2403 }
2404 case ACPI_SRAT_TYPE_MEMORY_AFFINITY: {
2405 ACPI_SRAT_MEM_AFFINITY *mem =
2406 (ACPI_SRAT_MEM_AFFINITY *)item;
2407
2408 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
2409 break;
2410 memory.domain = mem->ProximityDomain;
2411 memory.addr = mem->BaseAddress;
2412 memory.length = mem->Length;
2413 memory.flags = mem->Flags;
2414 (void) snprintf(prop_name, 30, "acpi-srat-memory-%d",
2415 mem_num);
2416 bsetprop(prop_name, strlen(prop_name), &memory,
2417 sizeof (memory));
2418 if ((mem->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) &&
2419 (memory.addr + memory.length > maxmem)) {
2420 maxmem = memory.addr + memory.length;
2421 }
2422 mem_num++;
2423 break;
2424 }
2425 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: {
2426 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2cpu =
2427 (ACPI_SRAT_X2APIC_CPU_AFFINITY *) item;
2428
2429 if (!(x2cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2430 break;
2431 x2apic.domain = x2cpu->ProximityDomain;
2432 x2apic.x2apic_id = x2cpu->ApicId;
2433 (void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2434 proc_num);
2435 bsetprop(prop_name, strlen(prop_name), &x2apic,
2436 sizeof (x2apic));
2437 proc_num++;
2438 break;
2439 }
2440 default:
2441 if (kbm_debug)
2442 bop_printf(NULL, "SRAT type %d\n", item->Type);
2443 break;
2444 }
2445
2446 item = (ACPI_SUBTABLE_HEADER *)
2447 (item->Length + (uintptr_t)item);
2448 }
2449
2450 /*
2451 * The maximum physical address calculated from the SRAT table is more
2452 * accurate than that calculated from the MSCT table.
2453 */
2454 if (maxmem != 0) {
2455 plat_dr_physmax = btop(maxmem);
2456 }
2457 }
2458
2459 static void
process_slit(ACPI_TABLE_SLIT * tp)2460 process_slit(ACPI_TABLE_SLIT *tp)
2461 {
2462
2463 /*
2464 * Check the number of localities; if it's too huge, we just
2465 * return and locality enumeration code will handle this later,
2466 * if possible.
2467 *
2468 * Note that the size of the table is the square of the
2469 * number of localities; if the number of localities exceeds
2470 * UINT16_MAX, the table size may overflow an int when being
2471 * passed to bsetprop() below.
2472 */
2473 if (tp->LocalityCount >= SLIT_LOCALITIES_MAX)
2474 return;
2475
2476 bsetprop(SLIT_NUM_PROPNAME, strlen(SLIT_NUM_PROPNAME),
2477 &tp->LocalityCount, sizeof (tp->LocalityCount));
2478 bsetprop(SLIT_PROPNAME, strlen(SLIT_PROPNAME), &tp->Entry,
2479 tp->LocalityCount * tp->LocalityCount);
2480 }
2481
2482 static ACPI_TABLE_MSCT *
process_msct(ACPI_TABLE_MSCT * tp)2483 process_msct(ACPI_TABLE_MSCT *tp)
2484 {
2485 int last_seen = 0;
2486 int proc_num = 0;
2487 ACPI_MSCT_PROXIMITY *item, *end;
2488 extern uint64_t plat_dr_options;
2489
2490 ASSERT(tp != NULL);
2491
2492 end = (ACPI_MSCT_PROXIMITY *)(tp->Header.Length + (uintptr_t)tp);
2493 for (item = (void *)((uintptr_t)tp + tp->ProximityOffset);
2494 item < end;
2495 item = (void *)(item->Length + (uintptr_t)item)) {
2496 /*
2497 * Sanity check according to section 5.2.19.1 of ACPI 4.0.
2498 * Revision 1
2499 * Length 22
2500 */
2501 if (item->Revision != 1 || item->Length != 22) {
2502 cmn_err(CE_CONT,
2503 "?boot: unknown proximity domain structure in MSCT "
2504 "with Revision(%d), Length(%d).\n",
2505 (int)item->Revision, (int)item->Length);
2506 return (NULL);
2507 } else if (item->RangeStart > item->RangeEnd) {
2508 cmn_err(CE_CONT,
2509 "?boot: invalid proximity domain structure in MSCT "
2510 "with RangeStart(%u), RangeEnd(%u).\n",
2511 item->RangeStart, item->RangeEnd);
2512 return (NULL);
2513 } else if (item->RangeStart != last_seen) {
2514 /*
2515 * Items must be organized in ascending order of the
2516 * proximity domain enumerations.
2517 */
2518 cmn_err(CE_CONT,
2519 "?boot: invalid proximity domain structure in MSCT,"
2520 " items are not orginized in ascending order.\n");
2521 return (NULL);
2522 }
2523
2524 /*
2525 * If ProcessorCapacity is 0 then there would be no CPUs in this
2526 * domain.
2527 */
2528 if (item->ProcessorCapacity != 0) {
2529 proc_num += (item->RangeEnd - item->RangeStart + 1) *
2530 item->ProcessorCapacity;
2531 }
2532
2533 last_seen = item->RangeEnd - item->RangeStart + 1;
2534 /*
2535 * Break out if all proximity domains have been processed.
2536 * Some BIOSes may have unused items at the end of MSCT table.
2537 */
2538 if (last_seen > tp->MaxProximityDomains) {
2539 break;
2540 }
2541 }
2542 if (last_seen != tp->MaxProximityDomains + 1) {
2543 cmn_err(CE_CONT,
2544 "?boot: invalid proximity domain structure in MSCT, "
2545 "proximity domain count doesn't match.\n");
2546 return (NULL);
2547 }
2548
2549 /*
2550 * Set plat-max-ncpus property if it hasn't been set yet.
2551 */
2552 if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2553 if (proc_num != 0) {
2554 bsetpropsi(PLAT_MAX_NCPUS_NAME, proc_num);
2555 }
2556 }
2557
2558 /*
2559 * Use Maximum Physical Address from the MSCT table as upper limit for
2560 * memory hot-adding by default. It may be overridden by value from
2561 * the SRAT table or the "plat-dr-physmax" boot option.
2562 */
2563 plat_dr_physmax = btop(tp->MaxAddress + 1);
2564
2565 /*
2566 * Existence of MSCT implies CPU/memory hotplug-capability for the
2567 * platform.
2568 */
2569 plat_dr_options |= PLAT_DR_FEATURE_CPU;
2570 plat_dr_options |= PLAT_DR_FEATURE_MEMORY;
2571
2572 return (tp);
2573 }
2574
2575 #else /* __xpv */
2576 static void
enumerate_xen_cpus()2577 enumerate_xen_cpus()
2578 {
2579 processorid_t id, max_id;
2580
2581 /*
2582 * User-set boot-ncpus overrides enumeration
2583 */
2584 if (do_bsys_getproplen(NULL, BOOT_NCPUS_NAME) >= 0)
2585 return;
2586
2587 /*
2588 * Probe every possible virtual CPU id and remember the
2589 * highest id present; the count of CPUs is one greater
2590 * than this. This tacitly assumes at least cpu 0 is present.
2591 */
2592 max_id = 0;
2593 for (id = 0; id < MAX_VIRT_CPUS; id++)
2594 if (HYPERVISOR_vcpu_op(VCPUOP_is_up, id, NULL) == 0)
2595 max_id = id;
2596
2597 bsetpropsi(BOOT_NCPUS_NAME, max_id+1);
2598
2599 }
2600 #endif /* __xpv */
2601
2602 /*ARGSUSED*/
2603 static void
build_firmware_properties(struct xboot_info * xbp)2604 build_firmware_properties(struct xboot_info *xbp)
2605 {
2606 ACPI_TABLE_HEADER *tp = NULL;
2607
2608 #ifndef __xpv
2609 if (xbp->bi_acpi_rsdp) {
2610 bsetprop64("acpi-root-tab",
2611 (uint64_t)(uintptr_t)xbp->bi_acpi_rsdp);
2612 }
2613
2614 if ((tp = find_fw_table(ACPI_SIG_MSCT)) != NULL)
2615 msct_ptr = process_msct((ACPI_TABLE_MSCT *)tp);
2616 else
2617 msct_ptr = NULL;
2618
2619 if ((tp = find_fw_table(ACPI_SIG_MADT)) != NULL)
2620 process_madt((ACPI_TABLE_MADT *)tp);
2621
2622 if ((srat_ptr = (ACPI_TABLE_SRAT *)
2623 find_fw_table(ACPI_SIG_SRAT)) != NULL)
2624 process_srat(srat_ptr);
2625
2626 if (slit_ptr = (ACPI_TABLE_SLIT *)find_fw_table(ACPI_SIG_SLIT))
2627 process_slit(slit_ptr);
2628
2629 tp = find_fw_table(ACPI_SIG_MCFG);
2630 #else /* __xpv */
2631 enumerate_xen_cpus();
2632 if (DOMAIN_IS_INITDOMAIN(xen_info))
2633 tp = find_fw_table(ACPI_SIG_MCFG);
2634 #endif /* __xpv */
2635 if (tp != NULL)
2636 process_mcfg((ACPI_TABLE_MCFG *)tp);
2637 }
2638
2639 /*
2640 * fake up a boot property for deferred early console output
2641 * this is used by both graphical boot and the (developer only)
2642 * USB serial console
2643 */
2644 void *
defcons_init(size_t size)2645 defcons_init(size_t size)
2646 {
2647 static char *p = NULL;
2648
2649 p = do_bsys_alloc(NULL, NULL, size, MMU_PAGESIZE);
2650 *p = 0;
2651 bsetprop("deferred-console-buf", strlen("deferred-console-buf") + 1,
2652 &p, sizeof (p));
2653 return (p);
2654 }
2655
2656 /*ARGSUSED*/
2657 int
boot_compinfo(int fd,struct compinfo * cbp)2658 boot_compinfo(int fd, struct compinfo *cbp)
2659 {
2660 cbp->iscmp = 0;
2661 cbp->blksize = MAXBSIZE;
2662 return (0);
2663 }
2664
2665 #define BP_MAX_STRLEN 32
2666
2667 /*
2668 * Get value for given boot property
2669 */
2670 int
bootprop_getval(const char * prop_name,u_longlong_t * prop_value)2671 bootprop_getval(const char *prop_name, u_longlong_t *prop_value)
2672 {
2673 int boot_prop_len;
2674 char str[BP_MAX_STRLEN];
2675 u_longlong_t value;
2676
2677 boot_prop_len = BOP_GETPROPLEN(bootops, prop_name);
2678 if (boot_prop_len < 0 || boot_prop_len > sizeof (str) ||
2679 BOP_GETPROP(bootops, prop_name, str) < 0 ||
2680 kobj_getvalue(str, &value) == -1)
2681 return (-1);
2682
2683 if (prop_value)
2684 *prop_value = value;
2685
2686 return (0);
2687 }
2688