10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3446Smrj * Common Development and Distribution License (the "License").
6*3446Smrj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*3446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * This plugin supports debugging functionality unique to Intel processors based
300Sstevel@tonic-gate * on the P6 core (Pentium Pro, Pentium II, and Pentium III). It does not
310Sstevel@tonic-gate * support the Pentium M processor, which uses a P4-style branch trace stack.
320Sstevel@tonic-gate * The Pentium M is supported by the P4 plugin.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <kmdb/kvm_cpu_impl.h>
360Sstevel@tonic-gate #include <kmdb/kmdb_dpi.h>
370Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
380Sstevel@tonic-gate #include <kmdb/kvm.h>
390Sstevel@tonic-gate #include <mdb/mdb_err.h>
400Sstevel@tonic-gate #include <mdb/mdb.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <sys/x86_archext.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate typedef struct kmt_cpu_p6 {
450Sstevel@tonic-gate uint64_t p6_debugctl;
460Sstevel@tonic-gate } kmt_cpu_p6_t;
470Sstevel@tonic-gate
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate * The debugctl value in this struct needs to outlive the destruction of the
500Sstevel@tonic-gate * kmt_cpu_t. It needs to be around for the final exit from the debugger so
510Sstevel@tonic-gate * we can do the final write of the debugctl MSR.
520Sstevel@tonic-gate */
530Sstevel@tonic-gate static kmt_cpu_p6_t kmt_cpu_p6;
540Sstevel@tonic-gate
550Sstevel@tonic-gate static void
kmt_p6_branch(uint_t cpuid,const char * label,uint_t msr)560Sstevel@tonic-gate kmt_p6_branch(uint_t cpuid, const char *label, uint_t msr)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate char buf[BUFSIZ];
590Sstevel@tonic-gate uintptr_t addr;
600Sstevel@tonic-gate
610Sstevel@tonic-gate addr = (uintptr_t)kmdb_dpi_msr_get_by_cpu(cpuid, msr);
620Sstevel@tonic-gate
630Sstevel@tonic-gate mdb_printf("%s: %p %A\n", label, addr, addr);
640Sstevel@tonic-gate
650Sstevel@tonic-gate if (mdb_dis_ins2str(mdb.m_disasm, mdb.m_target,
660Sstevel@tonic-gate MDB_TGT_AS_VIRT, buf, sizeof (buf), addr) != addr)
670Sstevel@tonic-gate mdb_printf("%*s %s\n", strlen(label), "", buf);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*ARGSUSED*/
710Sstevel@tonic-gate static int
kmt_p6_branches(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)720Sstevel@tonic-gate kmt_p6_branches(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate intptr_t cpuid = DPI_MASTER_CPUID;
750Sstevel@tonic-gate
760Sstevel@tonic-gate if (mdb_getopts(argc, argv,
770Sstevel@tonic-gate 'c', MDB_OPT_UINTPTR, &cpuid,
780Sstevel@tonic-gate NULL) != argc)
790Sstevel@tonic-gate return (DCMD_USAGE);
800Sstevel@tonic-gate
810Sstevel@tonic-gate kmt_p6_branch(cpuid, "LastBranchToIP ", MSR_LBR_TO);
820Sstevel@tonic-gate kmt_p6_branch(cpuid, "LastBranchFromIP ", MSR_LBR_FROM);
830Sstevel@tonic-gate kmt_p6_branch(cpuid, "LastExceptionToIP ", MSR_LEX_TO);
840Sstevel@tonic-gate kmt_p6_branch(cpuid, "LastExceptionFromIP", MSR_LEX_FROM);
850Sstevel@tonic-gate
860Sstevel@tonic-gate return (0);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate * MSRs that we want to track. These will be read each time the debugger is
910Sstevel@tonic-gate * entered.
920Sstevel@tonic-gate */
93*3446Smrj static const kdi_msr_t kmt_p6_msr[] = {
94*3446Smrj { MSR_DEBUGCTL, KDI_MSR_CLEARENTRY },
95*3446Smrj { MSR_DEBUGCTL, KDI_MSR_WRITEDELAY, &kmt_cpu_p6.p6_debugctl },
96*3446Smrj { MSR_LBR_TO, KDI_MSR_READ },
97*3446Smrj { MSR_LBR_FROM, KDI_MSR_READ },
98*3446Smrj { MSR_LEX_TO, KDI_MSR_READ },
99*3446Smrj { MSR_LEX_FROM, KDI_MSR_READ },
1000Sstevel@tonic-gate { NULL }
1010Sstevel@tonic-gate };
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /*ARGSUSED*/
1040Sstevel@tonic-gate static void
kmt_p6_destroy(kmt_cpu_t * cpu)1050Sstevel@tonic-gate kmt_p6_destroy(kmt_cpu_t *cpu)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate /* Leave LBR on */
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate mdb_free(cpu, sizeof (kmt_cpu_t));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*ARGSUSED*/
1130Sstevel@tonic-gate static const char *
kmt_p6_name(kmt_cpu_t * cpu)1140Sstevel@tonic-gate kmt_p6_name(kmt_cpu_t *cpu)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate return ("Intel P6 family (Pentium Pro, Pentium II, Pentium III)");
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /*ARGSUSED*/
1200Sstevel@tonic-gate static void
kmt_p6_btf_clear(mdb_tgt_t * t,int id,void * arg)1210Sstevel@tonic-gate kmt_p6_btf_clear(mdb_tgt_t *t, int id, void *arg)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate kmt_cpu_p6_t *p6 = arg;
1240Sstevel@tonic-gate kreg_t efl;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate p6->p6_debugctl &= ~DEBUGCTL_BTF;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate (void) kmdb_dpi_get_register("eflags", &efl);
1290Sstevel@tonic-gate efl &= ~(1 << KREG_EFLAGS_TF_SHIFT);
1300Sstevel@tonic-gate (void) kmdb_dpi_set_register("eflags", efl);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /* Enable branch stepping, to be disabled on the next debugger entry */
1340Sstevel@tonic-gate static int
kmt_p6_step_branch(kmt_cpu_t * cpu,mdb_tgt_t * t)1350Sstevel@tonic-gate kmt_p6_step_branch(kmt_cpu_t *cpu, mdb_tgt_t *t)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate kmt_cpu_p6_t *p6 = cpu->kmt_cpu_data;
1380Sstevel@tonic-gate kreg_t efl;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate (void) kmdb_dpi_get_register("eflags", &efl);
1410Sstevel@tonic-gate (void) kmdb_dpi_set_register("eflags",
1420Sstevel@tonic-gate (efl | (1 << KREG_EFLAGS_TF_SHIFT)));
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate p6->p6_debugctl |= DEBUGCTL_BTF;
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate return (mdb_tgt_add_fault(t, KMT_TRAP_ALL,
1470Sstevel@tonic-gate MDB_TGT_SPEC_HIDDEN | MDB_TGT_SPEC_TEMPORARY,
1480Sstevel@tonic-gate kmt_p6_btf_clear, p6));
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate static kmt_cpu_ops_t kmt_p6_ops = {
1520Sstevel@tonic-gate kmt_p6_destroy,
1530Sstevel@tonic-gate kmt_p6_name,
1540Sstevel@tonic-gate kmt_p6_step_branch
1550Sstevel@tonic-gate };
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate static const mdb_dcmd_t kmt_p6_dcmds[] = {
1580Sstevel@tonic-gate { "branches", NULL, "describe the recently-taken branches",
1590Sstevel@tonic-gate kmt_p6_branches },
1600Sstevel@tonic-gate { NULL }
1610Sstevel@tonic-gate };
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /* See 07/04 AP-485 Intel Processor Identification and the CPUID Instruction */
1640Sstevel@tonic-gate #define KMT_CPU_FAMILY_P6 0x6
1650Sstevel@tonic-gate #define KMT_CPU_MODEL_PM_9 0x9 /* Pentium M, model 9 */
1660Sstevel@tonic-gate #define KMT_CPU_MODEL_PM_D 0xd /* Pentium M, model d */
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate kmt_cpu_t *
kmt_cpu_p6_create(mdb_tgt_t * t)1690Sstevel@tonic-gate kmt_cpu_p6_create(mdb_tgt_t *t)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate uint_t vendor, family, model;
1720Sstevel@tonic-gate kmt_cpu_t *cpu;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate if (kmdb_kdi_get_cpuinfo(&vendor, &family, &model) < 0)
1750Sstevel@tonic-gate return (NULL); /* errno is set for us */
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (vendor != X86_VENDOR_Intel || family != KMT_CPU_FAMILY_P6 ||
1780Sstevel@tonic-gate model == KMT_CPU_MODEL_PM_9 || model == KMT_CPU_MODEL_PM_D) {
1790Sstevel@tonic-gate (void) set_errno(ENOTSUP);
1800Sstevel@tonic-gate return (NULL);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate cpu = mdb_zalloc(sizeof (kmt_cpu_t), UM_SLEEP);
1840Sstevel@tonic-gate cpu->kmt_cpu_ops = &kmt_p6_ops;
1850Sstevel@tonic-gate cpu->kmt_cpu_data = &kmt_cpu_p6;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate kmdb_dpi_msr_add(kmt_p6_msr);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate kmt_cpu_p6.p6_debugctl = DEBUGCTL_LBR; /* enable LBR on resume */
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate (void) mdb_tgt_register_dcmds(t, kmt_p6_dcmds, MDB_MOD_FORCE);
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate return (cpu);
1940Sstevel@tonic-gate }
195