xref: /netbsd-src/usr.sbin/crash/arch/x86.c (revision 61bf19f212cc94e8691f701558d98df41d5d4979)
1*61bf19f2Schristos /*	$NetBSD: x86.c,v 1.4 2018/08/12 16:00:41 christos Exp $	*/
2acc16d96Schristos 
3acc16d96Schristos /*-
4acc16d96Schristos  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5acc16d96Schristos  * All rights reserved.
6acc16d96Schristos  *
7acc16d96Schristos  * This code is derived from software contributed to The NetBSD Foundation
8acc16d96Schristos  * by Andrew Doran.
9acc16d96Schristos  *
10acc16d96Schristos  * Redistribution and use in source and binary forms, with or without
11acc16d96Schristos  * modification, are permitted provided that the following conditions
12acc16d96Schristos  * are met:
13acc16d96Schristos  * 1. Redistributions of source code must retain the above copyright
14acc16d96Schristos  *    notice, this list of conditions and the following disclaimer.
15acc16d96Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16acc16d96Schristos  *    notice, this list of conditions and the following disclaimer in the
17acc16d96Schristos  *    documentation and/or other materials provided with the distribution.
18acc16d96Schristos  *
19acc16d96Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20acc16d96Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21acc16d96Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22acc16d96Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23acc16d96Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24acc16d96Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25acc16d96Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26acc16d96Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27acc16d96Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28acc16d96Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29acc16d96Schristos  * POSSIBILITY OF SUCH DAMAGE.
30acc16d96Schristos  */
31acc16d96Schristos 
32acc16d96Schristos #include <sys/cdefs.h>
33acc16d96Schristos #ifndef lint
34*61bf19f2Schristos __RCSID("$NetBSD: x86.c,v 1.4 2018/08/12 16:00:41 christos Exp $");
35acc16d96Schristos #endif /* not lint */
36acc16d96Schristos 
37acc16d96Schristos #include <ddb/ddb.h>
38acc16d96Schristos 
39acc16d96Schristos #include <kvm.h>
40acc16d96Schristos #include <nlist.h>
41acc16d96Schristos #include <err.h>
42acc16d96Schristos #include <stdlib.h>
43acc16d96Schristos 
44acc16d96Schristos #include <machine/frame.h>
45acc16d96Schristos #include <machine/pcb.h>
46acc16d96Schristos #include <x86/db_machdep.h>
47acc16d96Schristos 
48acc16d96Schristos #include "extern.h"
49acc16d96Schristos 
50*61bf19f2Schristos #ifdef VM_MIN_KERNEL_ADDRESS_DEFAULT
515b4ae9b8Schristos vaddr_t vm_min_kernel_address = VM_MIN_KERNEL_ADDRESS_DEFAULT;
52*61bf19f2Schristos #endif
535b4ae9b8Schristos 
54acc16d96Schristos static struct nlist nl[] = {
55acc16d96Schristos 	{ .n_name = "_dumppcb" },
56*61bf19f2Schristos #ifdef VM_MIN_KERNEL_ADDRESS_DEFAULT
575ceff8e2Schristos 	{ .n_name = "vm_min_kernel_address" },
58*61bf19f2Schristos #endif
59acc16d96Schristos 	{ .n_name = NULL },
60acc16d96Schristos };
61acc16d96Schristos 
62acc16d96Schristos struct pcb	pcb;
63acc16d96Schristos 
64acc16d96Schristos void
db_mach_init(kvm_t * kd)65acc16d96Schristos db_mach_init(kvm_t *kd)
66acc16d96Schristos {
67acc16d96Schristos 
68acc16d96Schristos 	if (kvm_nlist(kd, nl) == -1) {
69acc16d96Schristos 		errx(EXIT_FAILURE, "kvm_nlist: %s", kvm_geterr(kd));
70acc16d96Schristos 	}
71acc16d96Schristos 	if ((size_t)kvm_read(kd, nl[0].n_value, &pcb, sizeof(pcb)) !=
72acc16d96Schristos 	    sizeof(pcb)) {
73acc16d96Schristos 		errx(EXIT_FAILURE, "cannot read dumppcb: %s", kvm_geterr(kd));
74acc16d96Schristos 	}
75*61bf19f2Schristos #ifdef VM_MIN_KERNEL_ADDRESS_DEFAULT
765b4ae9b8Schristos 	if ((size_t)kvm_read(kd, nl[1].n_value, &vm_min_kernel_address,
775b4ae9b8Schristos 	    sizeof(vm_min_kernel_address)) != sizeof(vm_min_kernel_address)) {
785b4ae9b8Schristos 		errx(EXIT_FAILURE, "cannot read vm_min_kernel_address: %s",
795b4ae9b8Schristos 		    kvm_geterr(kd));
805b4ae9b8Schristos 	}
81*61bf19f2Schristos #endif
82acc16d96Schristos         ddb_regs.tf_sp = pcb.pcb_sp;
83acc16d96Schristos         ddb_regs.tf_bp = pcb.pcb_bp;
84acc16d96Schristos         if (ddb_regs.tf_bp != 0 && ddb_regs.tf_sp != 0) {
85acc16d96Schristos         	printf("Backtrace from time of crash is available.\n");
86acc16d96Schristos 	}
87acc16d96Schristos }
88