Bug Summary

File:arch/amd64/amd64/machdep.c
Warning:line 1668, column 8
Access to field 'avail_end' results in a dereference of a null pointer (loaded from variable 'vps')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name machdep.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -target-feature +retpoline-external-thunk -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/legacy-dpm -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu13 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/inc -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc/pmfw_if -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D SUSPEND -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /home/ben/Projects/scan/2024-01-11-110808-61670-1 -x c /usr/src/sys/arch/amd64/amd64/machdep.c
1/* $OpenBSD: machdep.c,v 1.288 2023/09/08 20:47:22 kn Exp $ */
2/* $NetBSD: machdep.c,v 1.3 2003/05/07 22:58:18 fvdl Exp $ */
3
4/*-
5 * Copyright (c) 1996, 1997, 1998, 2000 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
10 * Simulation Facility, NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*-
35 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
36 * All rights reserved.
37 *
38 * This code is derived from software contributed to Berkeley by
39 * William Jolitz.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)machdep.c 7.4 (Berkeley) 6/3/91
66 */
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/signal.h>
71#include <sys/signalvar.h>
72#include <sys/proc.h>
73#include <sys/user.h>
74#include <sys/exec.h>
75#include <sys/buf.h>
76#include <sys/reboot.h>
77#include <sys/conf.h>
78#include <sys/msgbuf.h>
79#include <sys/mount.h>
80#include <sys/extent.h>
81#include <sys/core.h>
82#include <sys/kcore.h>
83#include <sys/syscallargs.h>
84
85#include <dev/cons.h>
86#include <stand/boot/bootarg.h>
87
88#include <net/if.h>
89#include <uvm/uvm_extern.h>
90
91#include <sys/sysctl.h>
92
93#include <machine/cpu_full.h>
94#include <machine/cpufunc.h>
95#include <machine/pio.h>
96#include <machine/psl.h>
97#include <machine/reg.h>
98#include <machine/fpu.h>
99#include <machine/biosvar.h>
100#include <machine/mpbiosvar.h>
101#include <machine/kcore.h>
102#include <machine/tss.h>
103
104#include <dev/isa/isareg.h>
105#include <dev/ic/i8042reg.h>
106
107#ifdef DDB1
108#include <machine/db_machdep.h>
109#include <ddb/db_extern.h>
110extern int db_console;
111#endif
112
113#include "isa.h"
114#include "isadma.h"
115#include "ksyms.h"
116
117#include "acpi.h"
118#if NACPI1 > 0
119#include <dev/acpi/acpivar.h>
120#endif
121
122#include "com.h"
123#if NCOM1 > 0
124#include <sys/tty.h>
125#include <dev/ic/comvar.h>
126#include <dev/ic/comreg.h>
127#endif
128
129#include "efi.h"
130#if NEFI1 > 0
131#include <dev/efi/efi.h>
132#endif
133
134#include "softraid.h"
135#if NSOFTRAID1 > 0
136#include <dev/softraidvar.h>
137#endif
138
139#ifdef HIBERNATE1
140#include <machine/hibernate_var.h>
141#endif /* HIBERNATE */
142
143#include "ukbd.h"
144#include "pckbc.h"
145#if NPCKBC1 > 0 && NUKBD1 > 0
146#include <dev/ic/pckbcvar.h>
147#endif
148
149/* #define MACHDEP_DEBUG */
150
151#ifdef MACHDEP_DEBUG
152#define DPRINTF(x...) do { printf(x); } while(0)
153#else
154#define DPRINTF(x...)
155#endif /* MACHDEP_DEBUG */
156
157/* the following is used externally (sysctl_hw) */
158char machine[] = MACHINE"amd64";
159
160/*
161 * switchto vectors
162 */
163void cpu_idle_cycle_hlt(void);
164void (*cpu_idle_cycle_fcn)(void) = &cpu_idle_cycle_hlt;
165
166/* the following is used externally for concurrent handlers */
167int setperf_prio = 0;
168
169#ifdef CPURESET_DELAY
170int cpureset_delay = CPURESET_DELAY;
171#else
172int cpureset_delay = 0;
173#endif
174
175char *ssym = 0, *esym = 0; /* start and end of symbol table */
176dev_t bootdev = 0; /* device we booted from */
177int biosbasemem = 0; /* base memory reported by BIOS */
178u_int bootapiver = 0; /* /boot API version */
179
180int physmem;
181u_int64_t dumpmem_low;
182u_int64_t dumpmem_high;
183extern int boothowto;
184int cpu_class;
185
186paddr_t dumpmem_paddr;
187vaddr_t dumpmem_vaddr;
188psize_t dumpmem_sz;
189
190vaddr_t kern_end;
191
192vaddr_t msgbuf_vaddr;
193paddr_t msgbuf_paddr;
194
195vaddr_t idt_vaddr;
196paddr_t idt_paddr;
197
198vaddr_t lo32_vaddr;
199paddr_t lo32_paddr;
200paddr_t tramp_pdirpa;
201
202int kbd_reset;
203int lid_action = 1;
204int pwr_action = 1;
205int forceukbd;
206
207/*
208 * safepri is a safe priority for sleep to set for a spin-wait
209 * during autoconfiguration or after a panic.
210 */
211int safepri = 0;
212
213struct vm_map *exec_map = NULL((void *)0);
214struct vm_map *phys_map = NULL((void *)0);
215
216/* UVM constraint ranges. */
217struct uvm_constraint_range isa_constraint = { 0x0, 0x00ffffffUL };
218struct uvm_constraint_range dma_constraint = { 0x0, 0xffffffffUL };
219struct uvm_constraint_range *uvm_md_constraints[] = {
220 &isa_constraint,
221 &dma_constraint,
222 NULL((void *)0),
223};
224
225paddr_t avail_start;
226paddr_t avail_end;
227
228void (*delay_func)(int) = i8254_delay;
229void (*initclock_func)(void) = i8254_initclocks;
230void (*startclock_func)(void) = i8254_start_both_clocks;
231
232/*
233 * Format of boot information passed to us by 32-bit /boot
234 */
235typedef struct _boot_args32 {
236 int ba_type;
237 int ba_size;
238 int ba_nextX; /* a ptr in 32-bit world, but not here */
239 char ba_arg[1];
240} bootarg32_t;
241
242#define BOOTARGC_MAX(1 << 12) NBPG(1 << 12) /* one page */
243
244bios_bootmac_t *bios_bootmac;
245
246/* locore copies the arguments from /boot to here for us */
247char bootinfo[BOOTARGC_MAX(1 << 12)];
248int bootinfo_size = BOOTARGC_MAX(1 << 12);
249
250void getbootinfo(char *, int);
251
252/* Data passed to us by /boot, filled in by getbootinfo() */
253bios_diskinfo_t *bios_diskinfo;
254bios_memmap_t *bios_memmap;
255u_int32_t bios_cksumlen;
256bios_efiinfo_t *bios_efiinfo;
257bios_ucode_t *bios_ucode;
258
259#if NEFI1 > 0
260EFI_MEMORY_DESCRIPTOR *mmap;
261#endif
262
263/*
264 * Size of memory segments, before any memory is stolen.
265 */
266phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX16];
267int mem_cluster_cnt;
268
269int cpu_dump(void);
270int cpu_dumpsize(void);
271u_long cpu_dump_mempagecnt(void);
272void dumpsys(void);
273void cpu_init_extents(void);
274void map_tramps(void);
275void init_x86_64(paddr_t);
276void (*cpuresetfn)(void);
277void enter_shared_special_pages(void);
278
279#ifdef APERTURE1
280int allowaperture = 0;
281#endif
282
283/*
284 * Machine-dependent startup code
285 */
286void
287cpu_startup(void)
288{
289 vaddr_t minaddr, maxaddr;
290
291 msgbuf_vaddr = PMAP_DIRECT_MAP(msgbuf_paddr)((vaddr_t)(((((511 - 4) * (1ULL << 39))) | 0xffff000000000000
)) + (msgbuf_paddr))
;
292 initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE)((((32 * (1 << 12))) + ((1 << 12) - 1)) & ~((
1 << 12) - 1))
);
293
294 printf("%s", version);
295 startclocks();
296 rtcinit();
297
298 printf("real mem = %lu (%luMB)\n", ptoa((psize_t)physmem)((paddr_t)((psize_t)physmem) << 12),
299 ptoa((psize_t)physmem)((paddr_t)((psize_t)physmem) << 12)/1024/1024);
300
301 /*
302 * Allocate a submap for exec arguments. This map effectively
303 * limits the number of processes exec'ing at any time.
304 */
305 minaddr = vm_map_min(kernel_map)((kernel_map)->min_offset);
306 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
307 16*NCARGS(512 * 1024), VM_MAP_PAGEABLE0x01, FALSE0, NULL((void *)0));
308
309 /*
310 * Allocate a submap for physio
311 */
312 minaddr = vm_map_min(kernel_map)((kernel_map)->min_offset);
313 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
314 VM_PHYS_SIZE(300*(1 << 12)), 0, FALSE0, NULL((void *)0));
315
316 printf("avail mem = %lu (%luMB)\n", ptoa((psize_t)uvmexp.free)((paddr_t)((psize_t)uvmexp.free) << 12),
317 ptoa((psize_t)uvmexp.free)((paddr_t)((psize_t)uvmexp.free) << 12)/1024/1024);
318
319 bufinit();
320
321 if (boothowto & RB_CONFIG0x00400) {
322#ifdef BOOT_CONFIG1
323 user_config();
324#else
325 printf("kernel does not support -c; continuing..\n");
326#endif
327 }
328
329 /* Safe for i/o port / memory space allocation to use malloc now. */
330 x86_bus_space_mallocok();
331
332#ifndef SMALL_KERNEL
333 cpu_ucode_setup();
334 cpu_ucode_apply(&cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
);
335#endif
336 cpu_tsx_disable(&cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
);
337
338 /* enter the IDT and trampoline code in the u-k maps */
339 enter_shared_special_pages();
340
341 /* initialize CPU0's TSS and GDT and put them in the u-k maps */
342 cpu_enter_pages(&cpu_info_full_primary);
343}
344
345/*
346 * enter_shared_special_pages
347 *
348 * Requests mapping of various special pages required in the Intel Meltdown
349 * case (to be entered into the U-K page table):
350 *
351 * 1 IDT page
352 * Various number of pages covering the U-K ".kutext" section. This section
353 * contains code needed during trampoline operation
354 * Various number of pages covering the U-K ".kudata" section. This section
355 * contains data accessed by the trampoline, before switching to U+K
356 * (for example, various shared global variables used by IPIs, etc)
357 *
358 * The linker script places the required symbols in the sections above.
359 *
360 * On CPUs not affected by Meltdown, the calls to pmap_enter_special below
361 * become no-ops.
362 */
363void
364enter_shared_special_pages(void)
365{
366 extern char __kutext_start[], __kutext_end[], __kernel_kutext_phys[];
367 extern char __text_page_start[], __text_page_end[];
368 extern char __kernel_kutext_page_phys[];
369 extern char __kudata_start[], __kudata_end[], __kernel_kudata_phys[];
370 vaddr_t va;
371 paddr_t pa;
372
373 /* idt */
374 pmap_enter_special(idt_vaddr, idt_paddr, PROT_READ0x01);
375 DPRINTF("%s: entered idt page va 0x%llx pa 0x%llx\n", __func__,
376 (uint64_t)idt_vaddr, (uint64_t)idt_paddr);
377
378 /* .kutext section */
379 va = (vaddr_t)__kutext_start;
380 pa = (paddr_t)__kernel_kutext_phys;
381 while (va < (vaddr_t)__kutext_end) {
382 pmap_enter_special(va, pa, PROT_READ0x01 | PROT_EXEC0x04);
383 DPRINTF("%s: entered kutext page va 0x%llx pa 0x%llx\n",
384 __func__, (uint64_t)va, (uint64_t)pa);
385 va += PAGE_SIZE(1 << 12);
386 pa += PAGE_SIZE(1 << 12);
387 }
388
389 /* .kutext.page section */
390 va = (vaddr_t)__text_page_start;
391 pa = (paddr_t)__kernel_kutext_page_phys;
392 while (va < (vaddr_t)__text_page_end) {
393 pmap_enter_special(va, pa, PROT_READ0x01 | PROT_EXEC0x04);
394 DPRINTF("%s: entered kutext.page va 0x%llx pa 0x%llx\n",
395 __func__, (uint64_t)va, (uint64_t)pa);
396 va += PAGE_SIZE(1 << 12);
397 pa += PAGE_SIZE(1 << 12);
398 }
399
400 /* .kudata section */
401 va = (vaddr_t)__kudata_start;
402 pa = (paddr_t)__kernel_kudata_phys;
403 while (va < (vaddr_t)__kudata_end) {
404 pmap_enter_special(va, pa, PROT_READ0x01 | PROT_WRITE0x02);
405 DPRINTF("%s: entered kudata page va 0x%llx pa 0x%llx\n",
406 __func__, (uint64_t)va, (uint64_t)pa);
407 va += PAGE_SIZE(1 << 12);
408 pa += PAGE_SIZE(1 << 12);
409 }
410}
411
412/*
413 * Set up proc0's PCB and the cpu's TSS.
414 */
415void
416x86_64_proc0_tss_ldt_init(void)
417{
418 struct pcb *pcb;
419
420 cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
.ci_curpcb = pcb = &proc0.p_addr->u_pcb;
421 pcb->pcb_fsbase = 0;
422 pcb->pcb_kstack = (u_int64_t)proc0.p_addr + USPACE(6 * (1 << 12)) - 16;
423 proc0.p_md.md_regs = (struct trapframe *)pcb->pcb_kstack - 1;
424
425 ltr(GSYSSEL(GPROC0_SEL, SEL_KPL)((((0) << 4) + (6 << 3)) | 0));
426 lldt(0);
427}
428
429bios_diskinfo_t *
430bios_getdiskinfo(dev_t dev)
431{
432 bios_diskinfo_t *pdi;
433
434 if (bios_diskinfo == NULL((void *)0))
435 return NULL((void *)0);
436
437 for (pdi = bios_diskinfo; pdi->bios_number != -1; pdi++) {
438 if ((dev & B_MAGICMASK0xf0000000) == B_DEVMAGIC0xa0000000) { /* search by bootdev */
439 if (pdi->bsd_dev == dev)
440 break;
441 } else {
442 if (pdi->bios_number == dev)
443 break;
444 }
445 }
446
447 if (pdi->bios_number == -1)
448 return NULL((void *)0);
449 else
450 return pdi;
451}
452
453int
454bios_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
455 size_t newlen, struct proc *p)
456{
457 bios_diskinfo_t *pdi;
458 int biosdev;
459
460 /* all sysctl names at this level except diskinfo are terminal */
461 if (namelen != 1 && name[0] != BIOS_DISKINFO2)
462 return (ENOTDIR20); /* overloaded */
463
464 if (!(bootapiver & BAPIV_VECTOR0x00000002))
465 return EOPNOTSUPP45;
466
467 switch (name[0]) {
468 case BIOS_DEV1:
469 if ((pdi = bios_getdiskinfo(bootdev)) == NULL((void *)0))
470 return ENXIO6;
471 biosdev = pdi->bios_number;
472 return sysctl_rdint(oldp, oldlenp, newp, biosdev);
473 case BIOS_DISKINFO2:
474 if (namelen != 2)
475 return ENOTDIR20;
476 if ((pdi = bios_getdiskinfo(name[1])) == NULL((void *)0))
477 return ENXIO6;
478 return sysctl_rdstruct(oldp, oldlenp, newp, pdi, sizeof(*pdi));
479 case BIOS_CKSUMLEN3:
480 return sysctl_rdint(oldp, oldlenp, newp, bios_cksumlen);
481 default:
482 return EOPNOTSUPP45;
483 }
484 /* NOTREACHED */
485}
486
487extern int tsc_is_invariant;
488extern int amd64_has_xcrypt;
489
490const struct sysctl_bounded_args cpuctl_vars[] = {
491 { CPU_LIDACTION14, &lid_action, 0, 2 },
492 { CPU_PWRACTION18, &pwr_action, 0, 2 },
493 { CPU_CPUID7, &cpu_id, SYSCTL_INT_READONLY1,0 },
494 { CPU_CPUFEATURE8, &cpu_feature, SYSCTL_INT_READONLY1,0 },
495 { CPU_XCRYPT12, &amd64_has_xcrypt, SYSCTL_INT_READONLY1,0 },
496 { CPU_INVARIANTTSC17, &tsc_is_invariant, SYSCTL_INT_READONLY1,0 },
497};
498
499/*
500 * machine dependent system variables.
501 */
502int
503cpu_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
504 size_t newlen, struct proc *p)
505{
506 extern uint64_t tsc_frequency;
507 dev_t consdev;
508 dev_t dev;
509
510 switch (name[0]) {
511 case CPU_CONSDEV1:
512 if (namelen != 1)
513 return (ENOTDIR20); /* overloaded */
514 if (cn_tab != NULL((void *)0))
515 consdev = cn_tab->cn_dev;
516 else
517 consdev = NODEV(dev_t)(-1);
518 return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
519 sizeof consdev));
520 case CPU_CHR2BLK4:
521 if (namelen != 2)
522 return (ENOTDIR20); /* overloaded */
523 dev = chrtoblk((dev_t)name[1]);
524 return sysctl_rdstruct(oldp, oldlenp, newp, &dev, sizeof(dev));
525 case CPU_BIOS2:
526 return bios_sysctl(name + 1, namelen - 1, oldp, oldlenp,
527 newp, newlen, p);
528 case CPU_CPUVENDOR6:
529 return (sysctl_rdstring(oldp, oldlenp, newp, cpu_vendor));
530 case CPU_KBDRESET10:
531 return (sysctl_securelevel_int(oldp, oldlenp, newp, newlen,
532 &kbd_reset));
533 case CPU_ALLOWAPERTURE5:
534 if (namelen != 1)
535 return (ENOTDIR20); /* overloaded */
536#ifdef APERTURE1
537 if (securelevel > 0)
538 return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
539 &allowaperture));
540 else
541 return (sysctl_int(oldp, oldlenp, newp, newlen,
542 &allowaperture));
543#else
544 return (sysctl_rdint(oldp, oldlenp, newp, 0));
545#endif
546#if NPCKBC1 > 0 && NUKBD1 > 0
547 case CPU_FORCEUKBD15:
548 {
549 int error;
550
551 if (forceukbd)
552 return (sysctl_rdint(oldp, oldlenp, newp, forceukbd));
553
554 error = sysctl_int(oldp, oldlenp, newp, newlen, &forceukbd);
555 if (forceukbd)
556 pckbc_release_console();
557 return (error);
558 }
559#endif
560 case CPU_TSCFREQ16:
561 return (sysctl_rdquad(oldp, oldlenp, newp, tsc_frequency));
562 default:
563 return (sysctl_bounded_arr(cpuctl_vars, nitems(cpuctl_vars)(sizeof((cpuctl_vars)) / sizeof((cpuctl_vars)[0])),
564 name, namelen, oldp, oldlenp, newp, newlen));
565 }
566 /* NOTREACHED */
567}
568
569static inline void
570maybe_enable_user_cet(struct proc *p)
571{
572#ifndef SMALL_KERNEL
573 /* Enable indirect-branch tracking if present and not disabled */
574 if ((xsave_mask & XFEATURE_CET_U0x00000800) &&
575 (p->p_p->ps_flags & PS_NOBTCFI0x02000000) == 0) {
576 uint64_t msr = rdmsr(MSR_U_CET0x6a0);
577 wrmsr(MSR_U_CET0x6a0, msr | MSR_CET_ENDBR_EN(1 << 2) | MSR_CET_NO_TRACK_EN(1 << 4));
578 }
579#endif
580}
581
582static inline void
583initialize_thread_xstate(struct proc *p)
584{
585 if (cpu_use_xsaves) {
586 xrstors(fpu_cleandata(&proc0.p_addr->u_pcb.pcb_savefpu), xsave_mask);
587 maybe_enable_user_cet(p);
588 } else {
589 /* Reset FPU state in PCB */
590 memcpy(&p->p_addr->u_pcb.pcb_savefpu, fpu_cleandata,__builtin_memcpy((&p->p_addr->u_pcb.pcb_savefpu), (
(&proc0.p_addr->u_pcb.pcb_savefpu)), (fpu_save_len))
591 fpu_save_len)__builtin_memcpy((&p->p_addr->u_pcb.pcb_savefpu), (
(&proc0.p_addr->u_pcb.pcb_savefpu)), (fpu_save_len))
;
592
593 if (curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_pflags & CPUPF_USERXSTATE0x02) {
594 /* state in CPU is obsolete; reset it */
595 fpureset()xrstor_kern((&proc0.p_addr->u_pcb.pcb_savefpu), xsave_mask
)
;
596 }
597 }
598
599 /* The reset state _is_ the userspace state for this thread now */
600 curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_pflags |= CPUPF_USERXSTATE0x02;
601}
602
603/*
604 * Copy out the FPU state, massaging it to be usable from userspace
605 * and acceptable to xrstor_user()
606 */
607static inline int
608copyoutfpu(struct savefpu *sfp, char *sp, size_t len)
609{
610 uint64_t bvs[2];
611
612 if (copyout(sfp, sp, len))
613 return 1;
614 if (len > offsetof(struct savefpu, fp_xstate.xstate_bv)__builtin_offsetof(struct savefpu, fp_xstate.xstate_bv)) {
615 sp += offsetof(struct savefpu, fp_xstate.xstate_bv)__builtin_offsetof(struct savefpu, fp_xstate.xstate_bv);
616 len -= offsetof(struct savefpu, fp_xstate.xstate_bv)__builtin_offsetof(struct savefpu, fp_xstate.xstate_bv);
617 bvs[0] = sfp->fp_xstate.xstate_bv & XFEATURE_XCR0_MASK(0x00000001 | 0x00000002 | 0x00000004 | (0x00000008 | 0x00000010
) | (0x00000020 | 0x00000040 | 0x00000080) | 0x00000200 | (0x00040000
| 0x00040000))
;
618 bvs[1] = sfp->fp_xstate.xstate_xcomp_bv &
619 (XFEATURE_XCR0_MASK(0x00000001 | 0x00000002 | 0x00000004 | (0x00000008 | 0x00000010
) | (0x00000020 | 0x00000040 | 0x00000080) | 0x00000200 | (0x00040000
| 0x00040000))
| XFEATURE_COMPRESSED(1ULL << 63));
620 if (copyout(bvs, sp, min(len, sizeof bvs)))
621 return 1;
622 }
623 return 0;
624}
625
626/*
627 * Send an interrupt to process.
628 *
629 * Stack is set up to allow sigcode to call routine, followed by
630 * syscall to sigreturn routine below. After sigreturn resets the
631 * signal mask, the stack, and the frame pointer, it returns to the
632 * user specified pc.
633 */
634int
635sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip,
636 int info, int onstack)
637{
638 struct proc *p = curproc({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})->ci_curproc
;
639 struct trapframe *tf = p->p_md.md_regs;
640 struct sigcontext ksc;
641 struct savefpu *sfp = &p->p_addr->u_pcb.pcb_savefpu;
642 register_t sp, scp, sip;
643 u_long sss;
644
645 memset(&ksc, 0, sizeof ksc)__builtin_memset((&ksc), (0), (sizeof ksc));
646 ksc.sc_rdi = tf->tf_rdi;
647 ksc.sc_rsi = tf->tf_rsi;
648 ksc.sc_rdx = tf->tf_rdx;
649 ksc.sc_rcx = tf->tf_rcx;
650 ksc.sc_r8 = tf->tf_r8;
651 ksc.sc_r9 = tf->tf_r9;
652 ksc.sc_r10 = tf->tf_r10;
653 ksc.sc_r11 = tf->tf_r11;
654 ksc.sc_r12 = tf->tf_r12;
655 ksc.sc_r13 = tf->tf_r13;
656 ksc.sc_r14 = tf->tf_r14;
657 ksc.sc_r15 = tf->tf_r15;
658 ksc.sc_rbx = tf->tf_rbx;
659 ksc.sc_rax = tf->tf_rax;
660 ksc.sc_rbp = tf->tf_rbp;
661 ksc.sc_rip = tf->tf_rip;
662 ksc.sc_cs = tf->tf_cs;
663 ksc.sc_rflags = tf->tf_rflags;
664 ksc.sc_rsp = tf->tf_rsp;
665 ksc.sc_ss = tf->tf_ss;
666 ksc.sc_mask = mask;
667
668 /* Allocate space for the signal handler context. */
669 if ((p->p_sigstk.ss_flags & SS_DISABLE0x0004) == 0 &&
670 !sigonstack(tf->tf_rsp) && onstack)
671 sp = trunc_page((vaddr_t)p->p_sigstk.ss_sp + p->p_sigstk.ss_size)(((vaddr_t)p->p_sigstk.ss_sp + p->p_sigstk.ss_size) &
~((1 << 12) - 1))
;
672 else
673 sp = tf->tf_rsp - 128;
674
675 sp -= fpu_save_len;
676 if (cpu_use_xsaves)
677 sp &= ~63ULL; /* just in case */
678 else
679 sp &= ~15ULL; /* just in case */
680
681 /* Save FPU state to PCB if necessary, then copy it out */
682 if (curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_pflags & CPUPF_USERXSTATE0x02)
683 fpusave(&p->p_addr->u_pcb.pcb_savefpu);
684 if (copyoutfpu(sfp, (void *)sp, fpu_save_len))
685 return 1;
686
687 initialize_thread_xstate(p);
688
689 ksc.sc_fpstate = (struct fxsave64 *)sp;
690 sss = (sizeof(ksc) + 15) & ~15;
691 sip = 0;
692 if (info) {
693 sip = sp - ((sizeof(*ksip) + 15) & ~15);
694 sss += (sizeof(*ksip) + 15) & ~15;
695
696 if (copyout(ksip, (void *)sip, sizeof(*ksip)))
697 return 1;
698 }
699 scp = sp - sss;
700
701 ksc.sc_cookie = (long)scp ^ p->p_p->ps_sigcookie;
702 if (copyout(&ksc, (void *)scp, sizeof(ksc)))
703 return 1;
704
705 /*
706 * Build context to run handler in.
707 */
708 tf->tf_rax = (u_int64_t)catcher;
709 tf->tf_rdi = sig;
710 tf->tf_rsi = sip;
711 tf->tf_rdx = scp;
712
713 tf->tf_rip = (u_int64_t)p->p_p->ps_sigcode;
714 tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL)(((5) << 3) | 3);
715 tf->tf_rflags &= ~(PSL_T0x00000100|PSL_D0x00000400|PSL_VM0x00020000|PSL_AC0x00040000);
716 tf->tf_rsp = scp;
717 tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL)(((4) << 3) | 3);
718
719 return 0;
720}
721
722/*
723 * System call to cleanup state after a signal
724 * has been taken. Reset signal mask and
725 * stack state from context left by sendsig (above).
726 * Return to previous pc and psl as specified by
727 * context left by sendsig. Check carefully to
728 * make sure that the user has not modified the
729 * psl to gain improper privileges or to cause
730 * a machine fault.
731 */
732int
733sys_sigreturn(struct proc *p, void *v, register_t *retval)
734{
735 struct sys_sigreturn_args /* {
736 syscallarg(struct sigcontext *) sigcntxp;
737 } */ *uap = v;
738 struct sigcontext ksc, *scp = SCARG(uap, sigcntxp)((uap)->sigcntxp.le.datum);
739 struct trapframe *tf = p->p_md.md_regs;
740 struct savefpu *sfp = &p->p_addr->u_pcb.pcb_savefpu;
741 int error;
742
743 if (PROC_PC(p)((p)->p_md.md_regs->tf_rip) != p->p_p->ps_sigcoderet) {
744 sigexit(p, SIGILL4);
745 return (EPERM1);
746 }
747
748 if ((error = copyin((caddr_t)scp, &ksc, sizeof ksc)))
749 return (error);
750
751 if (ksc.sc_cookie != ((long)scp ^ p->p_p->ps_sigcookie)) {
752 sigexit(p, SIGILL4);
753 return (EFAULT14);
754 }
755
756 /* Prevent reuse of the sigcontext cookie */
757 ksc.sc_cookie = 0;
758 (void)copyout(&ksc.sc_cookie, (caddr_t)scp +
759 offsetof(struct sigcontext, sc_cookie)__builtin_offsetof(struct sigcontext, sc_cookie), sizeof (ksc.sc_cookie));
760
761 if (((ksc.sc_rflags ^ tf->tf_rflags) & PSL_USERSTATIC(0x00000002 | 0xffc08028 | 0x00000200 | 0x00003000 | 0x00004000
| 0x00020000 | 0x00080000 | 0x00100000)
) != 0 ||
762 !USERMODE(ksc.sc_cs, ksc.sc_eflags)(((ksc.sc_cs) & 3) == 3))
763 return (EINVAL22);
764
765 /* Current FPU state is obsolete; toss it and force a reload */
766 if (curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_pflags & CPUPF_USERXSTATE0x02) {
767 curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_pflags &= ~CPUPF_USERXSTATE0x02;
768 fpureset()xrstor_kern((&proc0.p_addr->u_pcb.pcb_savefpu), xsave_mask
)
;
769 }
770
771 /* Copy in the FPU state to restore */
772 if (__predict_true(ksc.sc_fpstate != NULL)__builtin_expect(((ksc.sc_fpstate != ((void *)0)) != 0), 1)) {
773 if ((error = copyin(ksc.sc_fpstate, sfp, fpu_save_len)))
774 return error;
775 if (xrstor_user(sfp, xsave_mask)) {
776 memcpy(sfp, fpu_cleandata, fpu_save_len)__builtin_memcpy((sfp), ((&proc0.p_addr->u_pcb.pcb_savefpu
)), (fpu_save_len))
;
777 return EINVAL22;
778 }
779 maybe_enable_user_cet(p);
780 curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_pflags |= CPUPF_USERXSTATE0x02;
781 } else {
782 /* shouldn't happen, but handle it */
783 initialize_thread_xstate(p);
784 }
785
786 tf->tf_rdi = ksc.sc_rdi;
787 tf->tf_rsi = ksc.sc_rsi;
788 tf->tf_rdx = ksc.sc_rdx;
789 tf->tf_rcx = ksc.sc_rcx;
790 tf->tf_r8 = ksc.sc_r8;
791 tf->tf_r9 = ksc.sc_r9;
792 tf->tf_r10 = ksc.sc_r10;
793 tf->tf_r11 = ksc.sc_r11;
794 tf->tf_r12 = ksc.sc_r12;
795 tf->tf_r13 = ksc.sc_r13;
796 tf->tf_r14 = ksc.sc_r14;
797 tf->tf_r15 = ksc.sc_r15;
798 tf->tf_rbx = ksc.sc_rbx;
799 tf->tf_rax = ksc.sc_rax;
800 tf->tf_rbp = ksc.sc_rbp;
801 tf->tf_rip = ksc.sc_rip;
802 tf->tf_cs = ksc.sc_cs;
803 tf->tf_rflags = ksc.sc_rflags;
804 tf->tf_rsp = ksc.sc_rsp;
805 tf->tf_ss = ksc.sc_ss;
806
807 /* Restore signal mask. */
808 p->p_sigmask = ksc.sc_mask & ~sigcantmask((1U << ((9)-1)) | (1U << ((17)-1)));
809
810 /*
811 * sigreturn() needs to return to userspace via the 'iretq'
812 * method, so that if the process was interrupted (by tick,
813 * an IPI, whatever) as opposed to already being in the kernel
814 * when a signal was being delivered, the process will be
815 * completely restored, including the userland %rcx and %r11
816 * registers which the 'sysretq' instruction cannot restore.
817 * Also need to make sure we can handle faulting on xrstor.
818 */
819 p->p_md.md_flags |= MDP_IRET0x0002;
820
821 return (EJUSTRETURN-2);
822}
823
824#ifdef MULTIPROCESSOR1
825/* force a CPU into the kernel, whether or not it's idle */
826void
827cpu_kick(struct cpu_info *ci)
828{
829 /* only need to kick other CPUs */
830 if (ci != curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
) {
831 if (cpu_mwait_size > 0) {
832 /*
833 * If not idling, then send an IPI, else
834 * just clear the "keep idling" bit.
835 */
836 if ((ci->ci_mwait & MWAIT_IN_IDLE0x1) == 0)
837 x86_send_ipi(ci, X86_IPI_NOP0x00000002);
838 else
839 atomic_clearbits_intx86_atomic_clearbits_u32(&ci->ci_mwait,
840 MWAIT_KEEP_IDLING0x2);
841 } else {
842 /* no mwait, so need an IPI */
843 x86_send_ipi(ci, X86_IPI_NOP0x00000002);
844 }
845 }
846}
847#endif
848
849/*
850 * Notify the current process (p) that it has a signal pending,
851 * process as soon as possible.
852 */
853void
854signotify(struct proc *p)
855{
856 aston(p)((p)->p_md.md_astpending = 1);
857 cpu_kick(p->p_cpu);
858}
859
860#ifdef MULTIPROCESSOR1
861void
862cpu_unidle(struct cpu_info *ci)
863{
864 if (cpu_mwait_size > 0 && (ci->ci_mwait & MWAIT_ONLY0x4)) {
865 /*
866 * Just clear the "keep idling" bit; if it wasn't
867 * idling then we didn't need to do anything anyway.
868 */
869 atomic_clearbits_intx86_atomic_clearbits_u32(&ci->ci_mwait, MWAIT_KEEP_IDLING0x2);
870 return;
871 }
872
873 if (ci != curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
)
874 x86_send_ipi(ci, X86_IPI_NOP0x00000002);
875}
876#endif
877
878int waittime = -1;
879struct pcb dumppcb;
880
881__dead__attribute__((__noreturn__)) void
882boot(int howto)
883{
884 if ((howto & RB_POWERDOWN0x01000) != 0)
885 lid_action = 0;
886
887 if ((howto & RB_RESET0x08000) != 0)
888 goto doreset;
889
890 if (cold) {
891 if ((howto & RB_USERREQ0x04000) == 0)
892 howto |= RB_HALT0x00008;
893 goto haltsys;
894 }
895
896 boothowto = howto;
897 if ((howto & RB_NOSYNC0x00004) == 0 && waittime < 0) {
898 waittime = 0;
899 vfs_shutdown(curproc({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})->ci_curproc
);
900
901 if ((howto & RB_TIMEBAD0x00800) == 0) {
902 resettodr();
903 } else {
904 printf("WARNING: not updating battery clock\n");
905 }
906 }
907 if_downall();
908
909 uvm_shutdown();
910 splhigh()splraise(0xd);
911 cold = 1;
912
913 if ((howto & RB_DUMP0x00100) != 0)
914 dumpsys();
915
916haltsys:
917 config_suspend_all(DVACT_POWERDOWN6);
918
919#ifdef MULTIPROCESSOR1
920 x86_broadcast_ipi(X86_IPI_HALT0x00000001);
921#endif
922
923 if ((howto & RB_HALT0x00008) != 0) {
924#if NACPI1 > 0 && !defined(SMALL_KERNEL)
925 extern int acpi_enabled;
926
927 if (acpi_enabled) {
928 delay(500000)(*delay_func)(500000);
929 if ((howto & RB_POWERDOWN0x01000) != 0)
930 acpi_powerdown();
931 }
932#endif
933 printf("\n");
934 printf("The operating system has halted.\n");
935 printf("Please press any key to reboot.\n\n");
936 cnpollc(1); /* for proper keyboard command handling */
937 cngetc();
938 cnpollc(0);
939 }
940
941doreset:
942 printf("rebooting...\n");
943 if (cpureset_delay > 0)
944 delay(cpureset_delay * 1000)(*delay_func)(cpureset_delay * 1000);
945 cpu_reset();
946 for (;;)
947 continue;
948 /* NOTREACHED */
949}
950
951/*
952 * These variables are needed by /sbin/savecore
953 */
954u_long dumpmag = 0x8fca0101; /* magic number */
955int dumpsize = 0; /* pages */
956long dumplo = 0; /* blocks */
957
958/*
959 * cpu_dump: dump the machine-dependent kernel core dump headers.
960 */
961int
962cpu_dump(void)
963{
964 int (*dump)(dev_t, daddr_t, caddr_t, size_t);
965 char buf[dbtob(1)((1) << 9)];
966 kcore_seg_t *segp;
967 cpu_kcore_hdr_t *cpuhdrp;
968 phys_ram_seg_t *memsegp;
969 caddr_t va;
970 int i;
971
972 dump = bdevsw[major(dumpdev)(((unsigned)(dumpdev) >> 8) & 0xff)].d_dump;
973
974 memset(buf, 0, sizeof buf)__builtin_memset((buf), (0), (sizeof buf));
975 segp = (kcore_seg_t *)buf;
976 cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))(((unsigned long)(sizeof(*segp)) + (sizeof(long) - 1)) &~
(sizeof(long) - 1))
];
977 memsegp = (phys_ram_seg_t *)&buf[ALIGN(sizeof(*segp))(((unsigned long)(sizeof(*segp)) + (sizeof(long) - 1)) &~
(sizeof(long) - 1))
+
978 ALIGN(sizeof(*cpuhdrp))(((unsigned long)(sizeof(*cpuhdrp)) + (sizeof(long) - 1)) &
~(sizeof(long) - 1))
];
979
980 /*
981 * Generate a segment header.
982 */
983 CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU)( (*segp).c_midmag = (__uint32_t)(__builtin_constant_p(( ((1)
& 0x3f) << 26) | ( ((157) & 0x03ff) << 16
) | ( ((0x8fca) & 0xffff) )) ? (__uint32_t)(((__uint32_t)
(( ((1) & 0x3f) << 26) | ( ((157) & 0x03ff) <<
16) | ( ((0x8fca) & 0xffff) )) & 0xff) << 24 |
((__uint32_t)(( ((1) & 0x3f) << 26) | ( ((157) &
0x03ff) << 16) | ( ((0x8fca) & 0xffff) )) & 0xff00
) << 8 | ((__uint32_t)(( ((1) & 0x3f) << 26) |
( ((157) & 0x03ff) << 16) | ( ((0x8fca) & 0xffff
) )) & 0xff0000) >> 8 | ((__uint32_t)(( ((1) & 0x3f
) << 26) | ( ((157) & 0x03ff) << 16) | ( ((0x8fca
) & 0xffff) )) & 0xff000000) >> 24) : __swap32md
(( ((1) & 0x3f) << 26) | ( ((157) & 0x03ff) <<
16) | ( ((0x8fca) & 0xffff) ))) )
;
984 segp->c_size = dbtob(1)((1) << 9) - ALIGN(sizeof(*segp))(((unsigned long)(sizeof(*segp)) + (sizeof(long) - 1)) &~
(sizeof(long) - 1))
;
985
986 /*
987 * Add the machine-dependent header info.
988 */
989 cpuhdrp->ptdpaddr = proc0.p_addr->u_pcb.pcb_cr3;
990 cpuhdrp->nmemsegs = mem_cluster_cnt;
991
992 /*
993 * Fill in the memory segment descriptors.
994 */
995 for (i = 0; i < mem_cluster_cnt; i++) {
996 memsegp[i].start = mem_clusters[i].start;
997 memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK((1 << 12) - 1);
998 }
999
1000 /*
1001 * If we have dump memory then assume the kernel stack is in high
1002 * memory and bounce
1003 */
1004 if (dumpmem_vaddr != 0) {
1005 memcpy((char *)dumpmem_vaddr, buf, sizeof(buf))__builtin_memcpy(((char *)dumpmem_vaddr), (buf), (sizeof(buf)
))
;
1006 va = (caddr_t)dumpmem_vaddr;
1007 } else {
1008 va = (caddr_t)buf;
1009 }
1010 return (dump(dumpdev, dumplo, va, dbtob(1)((1) << 9)));
1011}
1012
1013/*
1014 * This is called by main to set dumplo and dumpsize.
1015 * Dumps always skip the first PAGE_SIZE of disk space
1016 * in case there might be a disk label stored there.
1017 * If there is extra space, put dump at the end to
1018 * reduce the chance that swapping trashes it.
1019 */
1020void
1021dumpconf(void)
1022{
1023 int nblks, dumpblks; /* size of dump area */
1024
1025 if (dumpdev == NODEV(dev_t)(-1) ||
1026 (nblks = (bdevsw[major(dumpdev)(((unsigned)(dumpdev) >> 8) & 0xff)].d_psize)(dumpdev)) == 0)
1027 return;
1028 if (nblks <= ctod(1)((1) << (12 - 9)))
1029 return;
1030
1031 dumpblks = cpu_dumpsize();
1032 if (dumpblks < 0)
1033 return;
1034 dumpblks += ctod(cpu_dump_mempagecnt())((cpu_dump_mempagecnt()) << (12 - 9));
1035
1036 /* If dump won't fit (incl. room for possible label), punt. */
1037 if (dumpblks > (nblks - ctod(1)((1) << (12 - 9))))
1038 return;
1039
1040 /* Put dump at end of partition */
1041 dumplo = nblks - dumpblks;
1042
1043 /* dumpsize is in page units, and doesn't include headers. */
1044 dumpsize = cpu_dump_mempagecnt();
1045}
1046
1047/*
1048 * Doadump comes here after turning off memory management and
1049 * getting on the dump stack, either when called above, or by
1050 * the auto-restart code.
1051 */
1052#define BYTES_PER_DUMP(64 * 1024) MAXPHYS(64 * 1024) /* must be a multiple of pagesize */
1053
1054void
1055dumpsys(void)
1056{
1057 u_long totalbytesleft, bytes, i, n, memseg;
1058 u_long maddr;
1059 daddr_t blkno;
1060 void *va;
1061 int (*dump)(dev_t, daddr_t, caddr_t, size_t);
1062 int error;
1063
1064 /* Save registers. */
1065 savectx(&dumppcb);
1066
1067 if (dumpdev == NODEV(dev_t)(-1))
1068 return;
1069
1070 /*
1071 * For dumps during autoconfiguration,
1072 * if dump device has already configured...
1073 */
1074 if (dumpsize == 0)
1075 dumpconf();
1076 if (dumplo <= 0 || dumpsize == 0) {
1077 printf("\ndump to dev %u,%u not possible\n", major(dumpdev)(((unsigned)(dumpdev) >> 8) & 0xff),
1078 minor(dumpdev)((unsigned)((dumpdev) & 0xff) | (((dumpdev) & 0xffff0000
) >> 8))
);
1079 return;
1080 }
1081 printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev)(((unsigned)(dumpdev) >> 8) & 0xff),
1082 minor(dumpdev)((unsigned)((dumpdev) & 0xff) | (((dumpdev) & 0xffff0000
) >> 8))
, dumplo);
1083
1084 error = (*bdevsw[major(dumpdev)(((unsigned)(dumpdev) >> 8) & 0xff)].d_psize)(dumpdev);
1085 printf("dump ");
1086 if (error == -1) {
1087 printf("area unavailable\n");
1088 return;
1089 }
1090
1091 if ((error = cpu_dump()) != 0)
1092 goto err;
1093
1094 totalbytesleft = ptoa(cpu_dump_mempagecnt())((paddr_t)(cpu_dump_mempagecnt()) << 12);
1095 blkno = dumplo + cpu_dumpsize();
1096 dump = bdevsw[major(dumpdev)(((unsigned)(dumpdev) >> 8) & 0xff)].d_dump;
1097 error = 0;
1098
1099 for (memseg = 0; memseg < mem_cluster_cnt; memseg++) {
1100 maddr = mem_clusters[memseg].start;
1101 bytes = mem_clusters[memseg].size;
1102
1103 for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
1104 /* Print out how many MBs we have left to go. */
1105 if ((totalbytesleft % (1024*1024)) < BYTES_PER_DUMP(64 * 1024))
1106 printf("%ld ", totalbytesleft / (1024 * 1024));
1107
1108 /* Limit size for next transfer. */
1109 n = bytes - i;
1110 if (n > BYTES_PER_DUMP(64 * 1024))
1111 n = BYTES_PER_DUMP(64 * 1024);
1112 if (maddr > 0xffffffff) {
1113 va = (void *)dumpmem_vaddr;
1114 if (n > dumpmem_sz)
1115 n = dumpmem_sz;
1116 memcpy(va, (void *)PMAP_DIRECT_MAP(maddr), n)__builtin_memcpy((va), ((void *)((vaddr_t)(((((511 - 4) * (1ULL
<< 39))) | 0xffff000000000000)) + (maddr))), (n))
;
1117 } else {
1118 va = (void *)PMAP_DIRECT_MAP(maddr)((vaddr_t)(((((511 - 4) * (1ULL << 39))) | 0xffff000000000000
)) + (maddr))
;
1119 }
1120
1121 error = (*dump)(dumpdev, blkno, va, n);
1122 if (error)
1123 goto err;
1124 maddr += n;
1125 blkno += btodb(n)((n) >> 9); /* XXX? */
1126
1127#if 0 /* XXX this doesn't work. grr. */
1128 /* operator aborting dump? */
1129 if (sget() != NULL((void *)0)) {
1130 error = EINTR4;
1131 break;
1132 }
1133#endif
1134 }
1135 }
1136
1137 err:
1138 switch (error) {
1139
1140 case ENXIO6:
1141 printf("device bad\n");
1142 break;
1143
1144 case EFAULT14:
1145 printf("device not ready\n");
1146 break;
1147
1148 case EINVAL22:
1149 printf("area improper\n");
1150 break;
1151
1152 case EIO5:
1153 printf("i/o error\n");
1154 break;
1155
1156 case EINTR4:
1157 printf("aborted from console\n");
1158 break;
1159
1160 case 0:
1161 printf("succeeded\n");
1162 break;
1163
1164 default:
1165 printf("error %d\n", error);
1166 break;
1167 }
1168 printf("\n\n");
1169 delay(5000000)(*delay_func)(5000000); /* 5 seconds */
1170}
1171
1172/*
1173 * Force the userspace FS.base to be reloaded from the PCB on return from
1174 * the kernel, and reset the segment registers (%ds, %es, %fs, and %gs)
1175 * to their expected userspace value.
1176 */
1177void
1178reset_segs(void)
1179{
1180 /*
1181 * This operates like the cpu_switchto() sequence: if we
1182 * haven't reset %[defg]s already, do so now.
1183 */
1184 if (curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_pflags & CPUPF_USERSEGS0x01) {
1185 curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_pflags &= ~CPUPF_USERSEGS0x01;
1186 __asm volatile(
1187 "movw %%ax,%%ds\n\t"
1188 "movw %%ax,%%es\n\t"
1189 "movw %%ax,%%fs\n\t"
1190 "cli\n\t" /* block intr when on user GS.base */
1191 "swapgs\n\t" /* swap from kernel to user GS.base */
1192 "movw %%ax,%%gs\n\t"/* set %gs to UDATA and GS.base to 0 */
1193 "swapgs\n\t" /* back to kernel GS.base */
1194 "sti" : : "a"(GSEL(GUDATA_SEL, SEL_UPL)(((4) << 3) | 3)));
1195 }
1196}
1197
1198/*
1199 * Clear registers on exec
1200 */
1201void
1202setregs(struct proc *p, struct exec_package *pack, u_long stack,
1203 struct ps_strings *arginfo)
1204{
1205 struct trapframe *tf;
1206
1207 initialize_thread_xstate(p);
1208
1209 /* To reset all registers we have to return via iretq */
1210 p->p_md.md_flags |= MDP_IRET0x0002;
1211
1212 reset_segs();
1213 p->p_addr->u_pcb.pcb_fsbase = 0;
1214
1215 tf = p->p_md.md_regs;
1216 memset(tf, 0, sizeof *tf)__builtin_memset((tf), (0), (sizeof *tf));
1217 tf->tf_rip = pack->ep_entry;
1218 tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL)(((5) << 3) | 3);
1219 tf->tf_rflags = PSL_USERSET(0x00000002 | 0x00000200);
1220 tf->tf_rsp = stack;
1221 tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL)(((4) << 3) | 3);
1222}
1223
1224/*
1225 * Initialize segments and descriptor tables
1226 */
1227
1228struct gate_descriptor *idt;
1229char idt_allocmap[NIDT256];
1230struct user *proc0paddr = NULL((void *)0);
1231
1232void
1233setgate(struct gate_descriptor *gd, void *func, int ist, int type, int dpl,
1234 int sel)
1235{
1236 gd->gd_looffset = (u_int64_t)func & 0xffff;
1237 gd->gd_selector = sel;
1238 gd->gd_ist = ist;
1239 gd->gd_type = type;
1240 gd->gd_dpl = dpl;
1241 gd->gd_p = 1;
1242 gd->gd_hioffset = (u_int64_t)func >> 16;
1243 gd->gd_zero = 0;
1244 gd->gd_xx1 = 0;
1245 gd->gd_xx2 = 0;
1246 gd->gd_xx3 = 0;
1247}
1248
1249void
1250unsetgate(struct gate_descriptor *gd)
1251{
1252 memset(gd, 0, sizeof (*gd))__builtin_memset((gd), (0), (sizeof (*gd)));
1253}
1254
1255void
1256setregion(struct region_descriptor *rd, void *base, u_int16_t limit)
1257{
1258 rd->rd_limit = limit;
1259 rd->rd_base = (u_int64_t)base;
1260}
1261
1262/*
1263 * Note that the base and limit fields are ignored in long mode.
1264 */
1265void
1266set_mem_segment(struct mem_segment_descriptor *sd, void *base, size_t limit,
1267 int type, int dpl, int gran, int def32, int is64)
1268{
1269 sd->sd_lolimit = (unsigned)limit;
1270 sd->sd_lobase = (unsigned long)base;
1271 sd->sd_type = type;
1272 sd->sd_dpl = dpl;
1273 sd->sd_p = 1;
1274 sd->sd_hilimit = (unsigned)limit >> 16;
1275 sd->sd_avl = 0;
1276 sd->sd_long = is64;
1277 sd->sd_def32 = def32;
1278 sd->sd_gran = gran;
1279 sd->sd_hibase = (unsigned long)base >> 24;
1280}
1281
1282void
1283set_sys_segment(struct sys_segment_descriptor *sd, void *base, size_t limit,
1284 int type, int dpl, int gran)
1285{
1286 memset(sd, 0, sizeof *sd)__builtin_memset((sd), (0), (sizeof *sd));
1287 sd->sd_lolimit = (unsigned)limit;
1288 sd->sd_lobase = (u_int64_t)base;
1289 sd->sd_type = type;
1290 sd->sd_dpl = dpl;
1291 sd->sd_p = 1;
1292 sd->sd_hilimit = (unsigned)limit >> 16;
1293 sd->sd_gran = gran;
1294 sd->sd_hibase = (u_int64_t)base >> 24;
1295}
1296
1297void cpu_init_idt(void)
1298{
1299 struct region_descriptor region;
1300
1301 setregion(&region, idt, NIDT256 * sizeof(idt[0]) - 1);
1302 lidt(&region);
1303}
1304
1305void
1306cpu_init_extents(void)
1307{
1308 extern struct extent *iomem_ex;
1309 static int already_done;
1310 int i;
1311
1312 /* We get called for each CPU, only first should do this */
1313 if (already_done)
1314 return;
1315
1316 /*
1317 * Allocate the physical addresses used by RAM from the iomem
1318 * extent map.
1319 */
1320 for (i = 0; i < mem_cluster_cnt; i++) {
1321 if (extent_alloc_region(iomem_ex, mem_clusters[i].start,
1322 mem_clusters[i].size, EX_NOWAIT0x0000)) {
1323 /* XXX What should we do? */
1324 printf("WARNING: CAN'T ALLOCATE RAM (%llx-%llx)"
1325 " FROM IOMEM EXTENT MAP!\n", mem_clusters[i].start,
1326 mem_clusters[i].start + mem_clusters[i].size - 1);
1327 }
1328 }
1329
1330 already_done = 1;
1331}
1332
1333void
1334map_tramps(void)
1335{
1336#if defined(MULTIPROCESSOR1) || \
1337 (NACPI1 > 0 && !defined(SMALL_KERNEL))
1338 struct pmap *kmp = pmap_kernel()(&kernel_pmap_store);
1339 extern paddr_t tramp_pdirpa;
1340#ifdef MULTIPROCESSOR1
1341 extern u_char cpu_spinup_trampoline[];
1342 extern u_char cpu_spinup_trampoline_end[];
1343 extern u_char mp_tramp_data_start[];
1344 extern u_char mp_tramp_data_end[];
1345 extern u_int32_t mp_pdirpa;
1346#endif
1347
1348 /*
1349 * The initial PML4 pointer must be below 4G, so if the
1350 * current one isn't, use a "bounce buffer" and save it
1351 * for tramps to use.
1352 */
1353 if (kmp->pm_pdirpa > 0xffffffff) {
1354 pmap_kenter_pa(lo32_vaddr, lo32_paddr, PROT_READ0x01 | PROT_WRITE0x02);
1355 memcpy((void *)lo32_vaddr, kmp->pm_pdir, PAGE_SIZE)__builtin_memcpy(((void *)lo32_vaddr), (kmp->pm_pdir), ((1
<< 12)))
;
1356 tramp_pdirpa = lo32_paddr;
1357 pmap_kremove(lo32_vaddr, PAGE_SIZE(1 << 12));
1358 } else
1359 tramp_pdirpa = kmp->pm_pdirpa;
1360
1361
1362#ifdef MULTIPROCESSOR1
1363 /* Map MP tramp code and data pages RW for copy */
1364 pmap_kenter_pa(MP_TRAMPOLINE(16 * (1 << 12)), MP_TRAMPOLINE(16 * (1 << 12)),
1365 PROT_READ0x01 | PROT_WRITE0x02);
1366
1367 pmap_kenter_pa(MP_TRAMP_DATA(17 * (1 << 12)), MP_TRAMP_DATA(17 * (1 << 12)),
1368 PROT_READ0x01 | PROT_WRITE0x02);
1369
1370 memset((caddr_t)MP_TRAMPOLINE, 0xcc, PAGE_SIZE)__builtin_memset(((caddr_t)(16 * (1 << 12))), (0xcc), (
(1 << 12)))
;
1371 memset((caddr_t)MP_TRAMP_DATA, 0xcc, PAGE_SIZE)__builtin_memset(((caddr_t)(17 * (1 << 12))), (0xcc), (
(1 << 12)))
;
1372
1373 memcpy((caddr_t)MP_TRAMPOLINE,__builtin_memcpy(((caddr_t)(16 * (1 << 12))), (cpu_spinup_trampoline
), (cpu_spinup_trampoline_end-cpu_spinup_trampoline))
1374 cpu_spinup_trampoline,__builtin_memcpy(((caddr_t)(16 * (1 << 12))), (cpu_spinup_trampoline
), (cpu_spinup_trampoline_end-cpu_spinup_trampoline))
1375 cpu_spinup_trampoline_end-cpu_spinup_trampoline)__builtin_memcpy(((caddr_t)(16 * (1 << 12))), (cpu_spinup_trampoline
), (cpu_spinup_trampoline_end-cpu_spinup_trampoline))
;
1376
1377 memcpy((caddr_t)MP_TRAMP_DATA,__builtin_memcpy(((caddr_t)(17 * (1 << 12))), (mp_tramp_data_start
), (mp_tramp_data_end - mp_tramp_data_start))
1378 mp_tramp_data_start,__builtin_memcpy(((caddr_t)(17 * (1 << 12))), (mp_tramp_data_start
), (mp_tramp_data_end - mp_tramp_data_start))
1379 mp_tramp_data_end - mp_tramp_data_start)__builtin_memcpy(((caddr_t)(17 * (1 << 12))), (mp_tramp_data_start
), (mp_tramp_data_end - mp_tramp_data_start))
;
1380
1381 /*
1382 * We need to patch this after we copy the tramp data,
1383 * the symbol points into the copied tramp data page.
1384 */
1385 mp_pdirpa = tramp_pdirpa;
1386
1387 /* Unmap, will be remapped in cpu_start_secondary */
1388 pmap_kremove(MP_TRAMPOLINE(16 * (1 << 12)), PAGE_SIZE(1 << 12));
1389 pmap_kremove(MP_TRAMP_DATA(17 * (1 << 12)), PAGE_SIZE(1 << 12));
1390#endif /* MULTIPROCESSOR */
1391#endif
1392}
1393
1394#define IDTVEC(name)Xname __CONCAT(X, name)Xname
1395typedef void (vector)(void);
1396extern vector *IDTVEC(exceptions)Xexceptions[];
1397
1398paddr_t early_pte_pages;
1399
1400void
1401init_x86_64(paddr_t first_avail)
1402{
1403 struct region_descriptor region;
1404 bios_memmap_t *bmp;
1405 int x, ist;
1406 uint64_t max_dm_size = ((uint64_t)512 * NUM_L4_SLOT_DIRECT4) << 30;
1407
1408 /*
1409 * locore0 mapped 3 pages for use before the pmap is initialized
1410 * starting at first_avail. These pages are currently used by
1411 * efifb to create early-use VAs for the framebuffer before efifb
1412 * is attached.
1413 */
1414 early_pte_pages = first_avail;
1415 first_avail += 3 * NBPG(1 << 12);
1416
1417 cpu_init_msrs(&cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
);
1418
1419 proc0.p_addr = proc0paddr;
1420 cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
.ci_curpcb = &proc0.p_addr->u_pcb;
1421
1422 x86_bus_space_init();
1423
1424 i8254_startclock();
1425
1426 /*
1427 * Initialize PAGE_SIZE-dependent variables.
1428 */
1429 uvm_setpagesize();
1430
1431 /*
1432 * Boot arguments are in a single page specified by /boot.
1433 *
1434 * We require the "new" vector form, as well as memory ranges
1435 * to be given in bytes rather than KB.
1436 *
1437 * locore copies the data into bootinfo[] for us.
1438 */
1439 if ((bootapiver & (BAPIV_VECTOR0x00000002 | BAPIV_BMEMMAP0x00000008)) ==
1
Assuming the condition is true
2
Taking true branch
1440 (BAPIV_VECTOR0x00000002 | BAPIV_BMEMMAP0x00000008)) {
1441 if (bootinfo_size >= sizeof(bootinfo))
3
Assuming the condition is false
4
Taking false branch
1442 panic("boot args too big");
1443
1444 getbootinfo(bootinfo, bootinfo_size);
1445 } else
1446 panic("invalid /boot");
1447
1448 cninit();
1449
1450/*
1451 * Memory on the AMD64 port is described by three different things.
1452 *
1453 * 1. biosbasemem - This is outdated, and should really only be used to
1454 * sanitize the other values. This is what we get back from the BIOS
1455 * using the legacy routines, describing memory below 640KB.
1456 *
1457 * 2. bios_memmap[] - This is the memory map as the bios has returned
1458 * it to us. It includes memory the kernel occupies, etc.
1459 *
1460 * 3. mem_cluster[] - This is the massaged free memory segments after
1461 * taking into account the contents of bios_memmap, biosbasemem,
1462 * and locore/machdep/pmap kernel allocations of physical
1463 * pages.
1464 *
1465 * The other thing is that the physical page *RANGE* is described by
1466 * three more variables:
1467 *
1468 * avail_start - This is a physical address of the start of available
1469 * pages, until IOM_BEGIN. This is basically the start
1470 * of the UVM managed range of memory, with some holes...
1471 *
1472 * avail_end - This is the end of physical pages. All physical pages
1473 * that UVM manages are between avail_start and avail_end.
1474 * There are holes...
1475 *
1476 * first_avail - This is the first available physical page after the
1477 * kernel, page tables, etc.
1478 *
1479 * We skip the first few pages for trampolines, hibernate, and to avoid
1480 * buggy SMI implementations that could corrupt the first 64KB.
1481 */
1482 avail_start = 16*PAGE_SIZE(1 << 12);
1483
1484#ifdef MULTIPROCESSOR1
1485 if (avail_start < MP_TRAMPOLINE(16 * (1 << 12)) + PAGE_SIZE(1 << 12))
5
Taking true branch
1486 avail_start = MP_TRAMPOLINE(16 * (1 << 12)) + PAGE_SIZE(1 << 12);
1487 if (avail_start < MP_TRAMP_DATA(17 * (1 << 12)) + PAGE_SIZE(1 << 12))
6
Taking true branch
1488 avail_start = MP_TRAMP_DATA(17 * (1 << 12)) + PAGE_SIZE(1 << 12);
1489#endif
1490
1491#if (NACPI1 > 0 && !defined(SMALL_KERNEL))
1492 if (avail_start < ACPI_TRAMPOLINE(19 * (1 << 12)) + PAGE_SIZE(1 << 12))
7
Taking true branch
1493 avail_start = ACPI_TRAMPOLINE(19 * (1 << 12)) + PAGE_SIZE(1 << 12);
1494 if (avail_start < ACPI_TRAMP_DATA(20 * (1 << 12)) + PAGE_SIZE(1 << 12))
8
Taking true branch
1495 avail_start = ACPI_TRAMP_DATA(20 * (1 << 12)) + PAGE_SIZE(1 << 12);
1496#endif
1497
1498#ifdef HIBERNATE1
1499 if (avail_start < HIBERNATE_HIBALLOC_PAGE((1 << 12) * 34) + PAGE_SIZE(1 << 12))
9
Taking true branch
1500 avail_start = HIBERNATE_HIBALLOC_PAGE((1 << 12) * 34) + PAGE_SIZE(1 << 12);
1501#endif /* HIBERNATE */
1502
1503 /*
1504 * We need to go through the BIOS memory map given, and
1505 * fill out mem_clusters and mem_cluster_cnt stuff, taking
1506 * into account all the points listed above.
1507 */
1508 avail_end = mem_cluster_cnt = 0;
1509 for (bmp = bios_memmap; bmp->type != BIOS_MAP_END0x00; bmp++) {
10
Assuming field 'type' is equal to BIOS_MAP_END
11
Loop condition is false. Execution continues on line 1584
1510 paddr_t s1, s2, e1, e2;
1511
1512 /* Ignore non-free memory */
1513 if (bmp->type != BIOS_MAP_FREE0x01)
1514 continue;
1515 if (bmp->size < PAGE_SIZE(1 << 12))
1516 continue;
1517
1518 /* Init our segment(s), round/trunc to pages */
1519 s1 = round_page(bmp->addr)(((bmp->addr) + ((1 << 12) - 1)) & ~((1 <<
12) - 1))
;
1520 e1 = trunc_page(bmp->addr + bmp->size)((bmp->addr + bmp->size) & ~((1 << 12) - 1));
1521 s2 = e2 = 0;
1522
1523 /*
1524 * XXX Some buggy ACPI BIOSes use memory that they
1525 * declare as free. Current worst offender is
1526 * Supermicro 5019D-FTN4. Typically the affected memory
1527 * areas are small blocks between areas reserved for
1528 * ACPI and other BIOS goo. So skip areas smaller
1529 * than 32 MB above the 16 MB boundary (to avoid
1530 * affecting legacy stuff).
1531 */
1532 if (s1 > 16*1024*1024 && (e1 - s1) < 32*1024*1024)
1533 continue;
1534
1535 /* Check and adjust our segment(s) */
1536 /* Nuke low pages */
1537 if (s1 < avail_start) {
1538 s1 = avail_start;
1539 if (s1 > e1)
1540 continue;
1541 }
1542
1543 /*
1544 * The direct map is limited to 512GB * NUM_L4_SLOT_DIRECT of
1545 * memory, so discard anything above that.
1546 */
1547 if (e1 >= max_dm_size) {
1548 e1 = max_dm_size;
1549 if (s1 > e1)
1550 continue;
1551 }
1552
1553 /* Crop stuff into "640K hole" */
1554 if (s1 < IOM_BEGIN0x0a0000 && e1 > IOM_BEGIN0x0a0000)
1555 e1 = IOM_BEGIN0x0a0000;
1556 if (s1 < biosbasemem && e1 > biosbasemem)
1557 e1 = biosbasemem;
1558
1559 /* Split any segments straddling the 16MB boundary */
1560 if (s1 < 16*1024*1024 && e1 > 16*1024*1024) {
1561 e2 = e1;
1562 s2 = e1 = 16*1024*1024;
1563 }
1564
1565 /* Store segment(s) */
1566 if (e1 - s1 >= PAGE_SIZE(1 << 12)) {
1567 mem_clusters[mem_cluster_cnt].start = s1;
1568 mem_clusters[mem_cluster_cnt].size = e1 - s1;
1569 mem_cluster_cnt++;
1570 }
1571 if (e2 - s2 >= PAGE_SIZE(1 << 12)) {
1572 mem_clusters[mem_cluster_cnt].start = s2;
1573 mem_clusters[mem_cluster_cnt].size = e2 - s2;
1574 mem_cluster_cnt++;
1575 }
1576 if (avail_end < e1) avail_end = e1;
1577 if (avail_end < e2) avail_end = e2;
1578 }
1579
1580 /*
1581 * Call pmap initialization to make new kernel address space.
1582 * We must do this before loading pages into the VM system.
1583 */
1584 first_avail = pmap_bootstrap(first_avail, trunc_page(avail_end)((avail_end) & ~((1 << 12) - 1)));
1585
1586#if NEFI1 > 0
1587 /* Relocate the EFI memory map. */
1588 if (bios_efiinfo && bios_efiinfo->mmap_start) {
12
Assuming 'bios_efiinfo' is null
1589 mmap = (EFI_MEMORY_DESCRIPTOR *)PMAP_DIRECT_MAP(first_avail)((vaddr_t)(((((511 - 4) * (1ULL << 39))) | 0xffff000000000000
)) + (first_avail))
;
1590 memcpy(mmap, (void *)PMAP_DIRECT_MAP(bios_efiinfo->mmap_start),__builtin_memcpy((mmap), ((void *)((vaddr_t)(((((511 - 4) * (
1ULL << 39))) | 0xffff000000000000)) + (bios_efiinfo->
mmap_start))), (bios_efiinfo->mmap_size))
1591 bios_efiinfo->mmap_size)__builtin_memcpy((mmap), ((void *)((vaddr_t)(((((511 - 4) * (
1ULL << 39))) | 0xffff000000000000)) + (bios_efiinfo->
mmap_start))), (bios_efiinfo->mmap_size))
;
1592 first_avail += round_page(bios_efiinfo->mmap_size)(((bios_efiinfo->mmap_size) + ((1 << 12) - 1)) &
~((1 << 12) - 1))
;
1593 }
1594#endif
1595
1596 /* Allocate these out of the 640KB base memory */
1597 if (avail_start != PAGE_SIZE(1 << 12))
13
Assuming the condition is false
14
Taking false branch
1598 avail_start = pmap_prealloc_lowmem_ptps(avail_start);
1599
1600 cpu_init_extents();
1601
1602 /* Make sure the end of the space used by the kernel is rounded. */
1603 first_avail = round_page(first_avail)(((first_avail) + ((1 << 12) - 1)) & ~((1 << 12
) - 1))
;
1604 kern_end = KERNBASE0xffffffff80000000 + first_avail;
1605
1606 /*
1607 * Now, load the memory clusters (which have already been
1608 * flensed) into the VM system.
1609 */
1610 for (x = 0; x < mem_cluster_cnt; x++) {
15
Assuming 'x' is >= 'mem_cluster_cnt'
16
Loop condition is false. Execution continues on line 1633
1611 paddr_t seg_start = mem_clusters[x].start;
1612 paddr_t seg_end = seg_start + mem_clusters[x].size;
1613
1614 if (seg_start < first_avail) seg_start = first_avail;
1615 if (seg_start > seg_end) continue;
1616 if (seg_end - seg_start < PAGE_SIZE(1 << 12)) continue;
1617
1618 physmem += atop(mem_clusters[x].size)((mem_clusters[x].size) >> 12);
1619
1620#if DEBUG_MEMLOAD
1621 printf("loading 0x%lx-0x%lx (0x%lx-0x%lx)\n",
1622 seg_start, seg_end, atop(seg_start)((seg_start) >> 12), atop(seg_end)((seg_end) >> 12));
1623#endif
1624 uvm_page_physload(atop(seg_start)((seg_start) >> 12), atop(seg_end)((seg_end) >> 12),
1625 atop(seg_start)((seg_start) >> 12), atop(seg_end)((seg_end) >> 12), 0);
1626 }
1627
1628 /*
1629 * Now, load the memory between the end of I/O memory "hole"
1630 * and the kernel.
1631 */
1632 {
1633 paddr_t seg_start = round_page(IOM_END)(((0x100000) + ((1 << 12) - 1)) & ~((1 << 12)
- 1))
;
1634 paddr_t seg_end = trunc_page(KERNTEXTOFF - KERNBASE)(((0xffffffff80000000 +0x1000000) - 0xffffffff80000000) &
~((1 << 12) - 1))
;
1635
1636 if (seg_start
16.1
'seg_start' is < 'seg_end'
< seg_end) {
17
Taking true branch
1637#if DEBUG_MEMLOAD
1638 printf("loading 0x%lx-0x%lx\n", seg_start, seg_end);
1639#endif
1640 uvm_page_physload(atop(seg_start)((seg_start) >> 12), atop(seg_end)((seg_end) >> 12),
1641 atop(seg_start)((seg_start) >> 12), atop(seg_end)((seg_end) >> 12), 0);
1642 }
1643 }
1644
1645#if DEBUG_MEMLOAD
1646 printf("avail_start = 0x%lx\n", avail_start);
1647 printf("avail_end = 0x%lx\n", avail_end);
1648 printf("first_avail = 0x%lx\n", first_avail);
1649#endif
1650
1651 /*
1652 * Steal memory for the message buffer (at end of core).
1653 */
1654 {
1655 struct vm_physseg *vps = NULL((void *)0);
18
'vps' initialized to a null pointer value
1656 psize_t sz = round_page(MSGBUFSIZE)((((32 * (1 << 12))) + ((1 << 12) - 1)) & ~((
1 << 12) - 1))
;
1657 psize_t reqsz = sz;
1658
1659 for (x = 0; x < vm_nphysseg; x++) {
19
Assuming 'x' is >= 'vm_nphysseg'
20
Loop condition is false. Execution continues on line 1664
1660 vps = &vm_physmem[x];
1661 if (ptoa(vps->avail_end)((paddr_t)(vps->avail_end) << 12) == avail_end)
1662 break;
1663 }
1664 if (x == vm_nphysseg)
21
Assuming 'x' is not equal to 'vm_nphysseg'
22
Taking false branch
1665 panic("init_x86_64: can't find end of memory");
1666
1667 /* Shrink so it'll fit in the last segment. */
1668 if ((vps->avail_end - vps->avail_start) < atop(sz)((sz) >> 12))
23
Access to field 'avail_end' results in a dereference of a null pointer (loaded from variable 'vps')
1669 sz = ptoa(vps->avail_end - vps->avail_start)((paddr_t)(vps->avail_end - vps->avail_start) << 12
)
;
1670
1671 vps->avail_end -= atop(sz)((sz) >> 12);
1672 vps->end -= atop(sz)((sz) >> 12);
1673 msgbuf_paddr = ptoa(vps->avail_end)((paddr_t)(vps->avail_end) << 12);
1674
1675 /* Remove the last segment if it now has no pages. */
1676 if (vps->start == vps->end) {
1677 for (vm_nphysseg--; x < vm_nphysseg; x++)
1678 vm_physmem[x] = vm_physmem[x + 1];
1679 }
1680
1681 /* Now find where the new avail_end is. */
1682 for (avail_end = 0, x = 0; x < vm_nphysseg; x++)
1683 if (vm_physmem[x].avail_end > avail_end)
1684 avail_end = vm_physmem[x].avail_end;
1685 avail_end = ptoa(avail_end)((paddr_t)(avail_end) << 12);
1686
1687 /* Warn if the message buffer had to be shrunk. */
1688 if (sz != reqsz)
1689 printf("WARNING: %ld bytes not available for msgbuf "
1690 "in last cluster (%ld used)\n", reqsz, sz);
1691 }
1692
1693 /*
1694 * Steal some memory for a dump bouncebuffer if we have memory over
1695 * the 32-bit barrier.
1696 */
1697 if (avail_end > 0xffffffff) {
1698 struct vm_physseg *vps = NULL((void *)0);
1699 psize_t sz = round_page(MAX(BYTES_PER_DUMP, dbtob(1)))(((((((64 * 1024))>(((1) << 9)))?((64 * 1024)):(((1)
<< 9)))) + ((1 << 12) - 1)) & ~((1 << 12
) - 1))
;
1700
1701 /* XXX assumes segments are ordered */
1702 for (x = 0; x < vm_nphysseg; x++) {
1703 vps = &vm_physmem[x];
1704 /* Find something between 16meg and 4gig */
1705 if (ptoa(vps->avail_end)((paddr_t)(vps->avail_end) << 12) <= 0xffffffff &&
1706 ptoa(vps->avail_start)((paddr_t)(vps->avail_start) << 12) >= 0xffffff)
1707 break;
1708 }
1709 if (x == vm_nphysseg)
1710 panic("init_x86_64: no memory between "
1711 "0xffffff-0xffffffff");
1712
1713 /* Shrink so it'll fit in the segment. */
1714 if ((vps->avail_end - vps->avail_start) < atop(sz)((sz) >> 12))
1715 sz = ptoa(vps->avail_end - vps->avail_start)((paddr_t)(vps->avail_end - vps->avail_start) << 12
)
;
1716
1717 vps->avail_end -= atop(sz)((sz) >> 12);
1718 vps->end -= atop(sz)((sz) >> 12);
1719 dumpmem_paddr = ptoa(vps->avail_end)((paddr_t)(vps->avail_end) << 12);
1720 dumpmem_vaddr = PMAP_DIRECT_MAP(dumpmem_paddr)((vaddr_t)(((((511 - 4) * (1ULL << 39))) | 0xffff000000000000
)) + (dumpmem_paddr))
;
1721 dumpmem_sz = sz;
1722
1723 /* Remove the last segment if it now has no pages. */
1724 if (vps->start == vps->end) {
1725 for (vm_nphysseg--; x < vm_nphysseg; x++)
1726 vm_physmem[x] = vm_physmem[x + 1];
1727 }
1728 }
1729
1730 pmap_growkernel(VM_MIN_KERNEL_ADDRESS0xffff800000000000 + 32 * 1024 * 1024);
1731
1732 pmap_kenter_pa(idt_vaddr, idt_paddr, PROT_READ0x01 | PROT_WRITE0x02);
1733
1734 idt = (struct gate_descriptor *)idt_vaddr;
1735 cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
.ci_tss = &cpu_info_full_primary.cif_tsscif_RO.u_tssgdt.uu_tss;
1736 cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
.ci_gdt = &cpu_info_full_primary.cif_gdtcif_RO.u_tssgdt.uu_gdt;
1737
1738 /* make gdt gates and memory segments */
1739 set_mem_segment(GDT_ADDR_MEM(cpu_info_primary.ci_gdt, GCODE_SEL)((struct mem_segment_descriptor *)((char *)((*(struct cpu_info
*)((char *)&cpu_info_full_primary + 4096*2 - __builtin_offsetof
(struct cpu_info, ci_dev))).ci_gdt) + ((1) << 3)))
, 0,
1740 0xfffff, SDT_MEMERA27, SEL_KPL0, 1, 0, 1);
1741
1742 set_mem_segment(GDT_ADDR_MEM(cpu_info_primary.ci_gdt, GDATA_SEL)((struct mem_segment_descriptor *)((char *)((*(struct cpu_info
*)((char *)&cpu_info_full_primary + 4096*2 - __builtin_offsetof
(struct cpu_info, ci_dev))).ci_gdt) + ((2) << 3)))
, 0,
1743 0xfffff, SDT_MEMRWA19, SEL_KPL0, 1, 0, 1);
1744
1745 set_mem_segment(GDT_ADDR_MEM(cpu_info_primary.ci_gdt, GUCODE32_SEL)((struct mem_segment_descriptor *)((char *)((*(struct cpu_info
*)((char *)&cpu_info_full_primary + 4096*2 - __builtin_offsetof
(struct cpu_info, ci_dev))).ci_gdt) + ((3) << 3)))
, 0,
1746 atop(VM_MAXUSER_ADDRESS32)((0xffffc000) >> 12) - 1, SDT_MEMERA27, SEL_UPL3, 1, 1, 0);
1747
1748 set_mem_segment(GDT_ADDR_MEM(cpu_info_primary.ci_gdt, GUDATA_SEL)((struct mem_segment_descriptor *)((char *)((*(struct cpu_info
*)((char *)&cpu_info_full_primary + 4096*2 - __builtin_offsetof
(struct cpu_info, ci_dev))).ci_gdt) + ((4) << 3)))
, 0,
1749 atop(VM_MAXUSER_ADDRESS)((0x00007f7fffffc000) >> 12) - 1, SDT_MEMRWA19, SEL_UPL3, 1, 0, 1);
1750
1751 set_mem_segment(GDT_ADDR_MEM(cpu_info_primary.ci_gdt, GUCODE_SEL)((struct mem_segment_descriptor *)((char *)((*(struct cpu_info
*)((char *)&cpu_info_full_primary + 4096*2 - __builtin_offsetof
(struct cpu_info, ci_dev))).ci_gdt) + ((5) << 3)))
, 0,
1752 atop(VM_MAXUSER_ADDRESS)((0x00007f7fffffc000) >> 12) - 1, SDT_MEMERA27, SEL_UPL3, 1, 0, 1);
1753
1754 set_sys_segment(GDT_ADDR_SYS(cpu_info_primary.ci_gdt, GPROC0_SEL)((struct sys_segment_descriptor *)((char *)((*(struct cpu_info
*)((char *)&cpu_info_full_primary + 4096*2 - __builtin_offsetof
(struct cpu_info, ci_dev))).ci_gdt) + ((0) << 4) + (6 <<
3)))
,
1755 cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
.ci_tss, sizeof (struct x86_64_tss)-1,
1756 SDT_SYS386TSS9, SEL_KPL0, 0);
1757
1758 /* exceptions */
1759 for (x = 0; x < 32; x++) {
1760 /* trap2 == NMI, trap8 == double fault */
1761 ist = (x == 2) ? 2 : (x == 8) ? 1 : 0;
1762 setgate(&idt[x], IDTVEC(exceptions)Xexceptions[x], ist, SDT_SYS386IGT14,
1763 (x == 3) ? SEL_UPL3 : SEL_KPL0,
1764 GSEL(GCODE_SEL, SEL_KPL)(((1) << 3) | 0));
1765 idt_allocmap[x] = 1;
1766 }
1767
1768 setregion(&region, cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
.ci_gdt, GDT_SIZE((6 << 3) + (1 << 4)) - 1);
1769 lgdt(&region);
1770
1771 cpu_init_idt();
1772
1773 intr_default_setup();
1774
1775 fpuinit(&cpu_info_primary(*(struct cpu_info *)((char *)&cpu_info_full_primary + 4096
*2 - __builtin_offsetof(struct cpu_info, ci_dev)))
);
1776
1777 softintr_init();
1778 splraise(IPL_IPI0xe);
1779 intr_enable();
1780
1781#ifdef DDB1
1782 db_machine_init();
1783 ddb_init();
1784 if (boothowto & RB_KDB0x00040)
1785 db_enter();
1786#endif
1787}
1788
1789void
1790cpu_reset(void)
1791{
1792 intr_disable();
1793
1794 if (cpuresetfn)
1795 (*cpuresetfn)();
1796
1797 /*
1798 * The keyboard controller has 4 random output pins, one of which is
1799 * connected to the RESET pin on the CPU in many PCs. We tell the
1800 * keyboard controller to pulse this line a couple of times.
1801 */
1802 outb(IO_KBD + KBCMDP, KBC_PULSE0)( (__builtin_constant_p((0x060 + 4)) && (0x060 + 4) <
0x100) ? __outbc(0x060 + 4, 0xfe) : __outb(0x060 + 4, 0xfe))
;
1803 delay(100000)(*delay_func)(100000);
1804 outb(IO_KBD + KBCMDP, KBC_PULSE0)( (__builtin_constant_p((0x060 + 4)) && (0x060 + 4) <
0x100) ? __outbc(0x060 + 4, 0xfe) : __outb(0x060 + 4, 0xfe))
;
1805 delay(100000)(*delay_func)(100000);
1806
1807 /*
1808 * Try to cause a triple fault and watchdog reset by making the IDT
1809 * invalid and causing a fault.
1810 */
1811 memset((caddr_t)idt, 0, NIDT * sizeof(idt[0]))__builtin_memset(((caddr_t)idt), (0), (256 * sizeof(idt[0])));
1812 __asm volatile("divl %0,%1" : : "q" (0), "a" (0));
1813
1814 for (;;)
1815 continue;
1816 /* NOTREACHED */
1817}
1818
1819/*
1820 * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
1821 */
1822int
1823cpu_dumpsize(void)
1824{
1825 int size;
1826
1827 size = ALIGN(sizeof(kcore_seg_t))(((unsigned long)(sizeof(kcore_seg_t)) + (sizeof(long) - 1)) &
~(sizeof(long) - 1))
+
1828 ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t))(((unsigned long)(mem_cluster_cnt * sizeof(phys_ram_seg_t)) +
(sizeof(long) - 1)) &~(sizeof(long) - 1))
;
1829 if (roundup(size, dbtob(1))((((size)+((((1) << 9))-1))/(((1) << 9)))*(((1) <<
9)))
!= dbtob(1)((1) << 9))
1830 return (-1);
1831
1832 return (1);
1833}
1834
1835/*
1836 * cpu_dump_mempagecnt: calculate the size of RAM (in pages) to be dumped.
1837 */
1838u_long
1839cpu_dump_mempagecnt(void)
1840{
1841 u_long i, n;
1842
1843 n = 0;
1844 for (i = 0; i < mem_cluster_cnt; i++)
1845 n += atop(mem_clusters[i].size)((mem_clusters[i].size) >> 12);
1846 return (n);
1847}
1848
1849/*
1850 * Figure out which portions of memory are used by the kernel/system.
1851 */
1852int
1853amd64_pa_used(paddr_t addr)
1854{
1855 struct vm_page *pg;
1856
1857 /* Kernel manages these */
1858 if ((pg = PHYS_TO_VM_PAGE(addr)) && (pg->pg_flags & PG_DEV0x00000200) == 0)
1859 return 1;
1860
1861 /* Kernel is loaded here */
1862 if (addr > IOM_END0x100000 && addr < (kern_end - KERNBASE0xffffffff80000000))
1863 return 1;
1864
1865 /* Low memory used for various bootstrap things */
1866 if (addr < avail_start)
1867 return 1;
1868
1869 /*
1870 * The only regions I can think of that are left are the things
1871 * we steal away from UVM. The message buffer?
1872 * XXX - ignore these for now.
1873 */
1874
1875 return 0;
1876}
1877
1878void
1879cpu_initclocks(void)
1880{
1881 (*initclock_func)();
1882}
1883
1884void
1885cpu_startclock(void)
1886{
1887 (*startclock_func)();
1888}
1889
1890void
1891need_resched(struct cpu_info *ci)
1892{
1893 ci->ci_want_resched = 1;
1894
1895 /* There's a risk we'll be called before the idle threads start */
1896 if (ci->ci_curproc) {
1897 aston(ci->ci_curproc)((ci->ci_curproc)->p_md.md_astpending = 1);
1898 cpu_kick(ci);
1899 }
1900}
1901
1902/*
1903 * Allocate an IDT vector slot within the given range.
1904 * XXX needs locking to avoid MP allocation races.
1905 */
1906
1907int
1908idt_vec_alloc(int low, int high)
1909{
1910 int vec;
1911
1912 for (vec = low; vec <= high; vec++) {
1913 if (idt_allocmap[vec] == 0) {
1914 idt_allocmap[vec] = 1;
1915 return vec;
1916 }
1917 }
1918 return 0;
1919}
1920
1921void
1922idt_vec_set(int vec, void (*function)(void))
1923{
1924 /*
1925 * Vector should be allocated, so no locking needed.
1926 */
1927 KASSERT(idt_allocmap[vec] == 1)((idt_allocmap[vec] == 1) ? (void)0 : __assert("diagnostic ",
"/usr/src/sys/arch/amd64/amd64/machdep.c", 1927, "idt_allocmap[vec] == 1"
))
;
1928 setgate(&idt[vec], function, 0, SDT_SYS386IGT14, SEL_KPL0,
1929 GSEL(GCODE_SEL, SEL_KPL)(((1) << 3) | 0));
1930}
1931
1932void
1933idt_vec_free(int vec)
1934{
1935 unsetgate(&idt[vec]);
1936 idt_allocmap[vec] = 0;
1937}
1938
1939#ifdef DIAGNOSTIC1
1940void
1941splassert_check(int wantipl, const char *func)
1942{
1943 int cpl = curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_ilevel;
1944 int floor = curcpu()({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})
->ci_handled_intr_level;
1945
1946 if (cpl < wantipl) {
1947 splassert_fail(wantipl, cpl, func);
1948 }
1949 if (floor > wantipl) {
1950 splassert_fail(wantipl, floor, func);
1951 }
1952
1953}
1954#endif
1955
1956int
1957copyin32(const uint32_t *uaddr, uint32_t *kaddr)
1958{
1959 if ((vaddr_t)uaddr & 0x3)
1960 return EFAULT14;
1961
1962 /* copyin(9) is atomic */
1963 return copyin(uaddr, kaddr, sizeof(uint32_t));
1964}
1965
1966void
1967getbootinfo(char *bootinfo, int bootinfo_size)
1968{
1969 bootarg32_t *q;
1970 bios_ddb_t *bios_ddb;
1971 bios_bootduid_t *bios_bootduid;
1972 bios_bootsr_t *bios_bootsr;
1973#undef BOOTINFO_DEBUG
1974#ifdef BOOTINFO_DEBUG
1975 printf("bootargv:");
1976#endif
1977
1978 for (q = (bootarg32_t *)bootinfo;
1979 (q->ba_type != BOOTARG_END-1) &&
1980 ((((char *)q) - bootinfo) < bootinfo_size);
1981 q = (bootarg32_t *)(((char *)q) + q->ba_size)) {
1982
1983 switch (q->ba_type) {
1984 case BOOTARG_MEMMAP0:
1985 bios_memmap = (bios_memmap_t *)q->ba_arg;
1986#ifdef BOOTINFO_DEBUG
1987 printf(" memmap %p", bios_memmap);
1988#endif
1989 break;
1990 case BOOTARG_DISKINFO1:
1991 bios_diskinfo = (bios_diskinfo_t *)q->ba_arg;
1992#ifdef BOOTINFO_DEBUG
1993 printf(" diskinfo %p", bios_diskinfo);
1994#endif
1995 break;
1996 case BOOTARG_APMINFO2:
1997 /* generated by i386 boot loader */
1998 break;
1999 case BOOTARG_CKSUMLEN3:
2000 bios_cksumlen = *(u_int32_t *)q->ba_arg;
2001#ifdef BOOTINFO_DEBUG
2002 printf(" cksumlen %d", bios_cksumlen);
2003#endif
2004 break;
2005 case BOOTARG_PCIINFO4:
2006 /* generated by i386 boot loader */
2007 break;
2008 case BOOTARG_CONSDEV5: {
2009#if NCOM1 > 0
2010 bios_consdev_t *cdp = (bios_consdev_t*)q->ba_arg;
2011 static const int ports[] =
2012 { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
2013 int unit = minor(cdp->consdev)((unsigned)((cdp->consdev) & 0xff) | (((cdp->consdev
) & 0xffff0000) >> 8))
;
2014 uint64_t consaddr = cdp->consaddr;
2015 if (consaddr == -1 && unit >= 0 && unit < nitems(ports)(sizeof((ports)) / sizeof((ports)[0])))
2016 consaddr = ports[unit];
2017 if (major(cdp->consdev)(((unsigned)(cdp->consdev) >> 8) & 0xff) == 8 && consaddr != -1) {
2018 comconsunit = unit;
2019 comconsaddr = consaddr;
2020 comconsrate = cdp->conspeed;
2021 comconsfreq = cdp->consfreq;
2022 comcons_reg_width = cdp->reg_width;
2023 comcons_reg_shift = cdp->reg_shift;
2024 if (cdp->flags & BCD_MMIO0x00000001)
2025 comconsiot = X86_BUS_SPACE_MEM(&x86_bus_space_mem_ops);
2026 else
2027 comconsiot = X86_BUS_SPACE_IO(&x86_bus_space_io_ops);
2028 }
2029#endif
2030#ifdef BOOTINFO_DEBUG
2031 printf(" console 0x%x:%d", cdp->consdev, cdp->conspeed);
2032#endif
2033 break;
2034 }
2035 case BOOTARG_BOOTMAC7:
2036 bios_bootmac = (bios_bootmac_t *)q->ba_arg;
2037 break;
2038
2039 case BOOTARG_DDB8:
2040 bios_ddb = (bios_ddb_t *)q->ba_arg;
2041#ifdef DDB1
2042 db_console = bios_ddb->db_console;
2043#endif
2044 break;
2045
2046 case BOOTARG_BOOTDUID9:
2047 bios_bootduid = (bios_bootduid_t *)q->ba_arg;
2048 memcpy(bootduid, bios_bootduid, sizeof(bootduid))__builtin_memcpy((bootduid), (bios_bootduid), (sizeof(bootduid
)))
;
2049 break;
2050
2051 case BOOTARG_BOOTSR10:
2052 bios_bootsr = (bios_bootsr_t *)q->ba_arg;
2053#if NSOFTRAID1 > 0
2054 memcpy(&sr_bootuuid, &bios_bootsr->uuid,__builtin_memcpy((&sr_bootuuid), (&bios_bootsr->uuid
), (sizeof(sr_bootuuid)))
2055 sizeof(sr_bootuuid))__builtin_memcpy((&sr_bootuuid), (&bios_bootsr->uuid
), (sizeof(sr_bootuuid)))
;
2056 memcpy(&sr_bootkey, &bios_bootsr->maskkey,__builtin_memcpy((&sr_bootkey), (&bios_bootsr->maskkey
), (sizeof(sr_bootkey)))
2057 sizeof(sr_bootkey))__builtin_memcpy((&sr_bootkey), (&bios_bootsr->maskkey
), (sizeof(sr_bootkey)))
;
2058#endif
2059 explicit_bzero(bios_bootsr, sizeof(bios_bootsr_t));
2060 break;
2061
2062 case BOOTARG_EFIINFO11:
2063 bios_efiinfo = (bios_efiinfo_t *)q->ba_arg;
2064 break;
2065
2066 case BOOTARG_UCODE12:
2067 bios_ucode = (bios_ucode_t *)q->ba_arg;
2068 break;
2069
2070 default:
2071#ifdef BOOTINFO_DEBUG
2072 printf(" unsupported arg (%d) %p", q->ba_type,
2073 q->ba_arg);
2074#endif
2075 break;
2076 }
2077 }
2078#ifdef BOOTINFO_DEBUG
2079 printf("\n");
2080#endif
2081}
2082
2083int
2084check_context(const struct reg *regs, struct trapframe *tf)
2085{
2086 uint16_t sel;
2087
2088 if (((regs->r_rflags ^ tf->tf_rflags) & PSL_USERSTATIC(0x00000002 | 0xffc08028 | 0x00000200 | 0x00003000 | 0x00004000
| 0x00020000 | 0x00080000 | 0x00100000)
) != 0)
2089 return EINVAL22;
2090
2091 sel = regs->r_ss & 0xffff;
2092 if (!VALID_USER_DSEL(sel)((sel) == (((4) << 3) | 3)))
2093 return EINVAL22;
2094
2095 sel = regs->r_cs & 0xffff;
2096 if (!VALID_USER_CSEL(sel)((sel) == (((5) << 3) | 3)))
2097 return EINVAL22;
2098
2099 if (regs->r_rip >= VM_MAXUSER_ADDRESS0x00007f7fffffc000)
2100 return EINVAL22;
2101
2102 return 0;
2103}
2104
2105int amd64_delay_quality;
2106
2107void
2108delay_init(void(*fn)(int), int fn_quality)
2109{
2110 if (fn_quality > amd64_delay_quality) {
2111 delay_func = fn;
2112 amd64_delay_quality = fn_quality;
2113 }
2114}
2115
2116void
2117delay_fini(void (*fn)(int))
2118{
2119 if (fn == delay_func) {
2120 delay_func = i8254_delay;
2121 amd64_delay_quality = 0;
2122 }
2123}