2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010, George V. Neville-Neil <gnn@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_hwpmc_hooks.h"
35 #include <sys/param.h>
37 #include <sys/pmckern.h>
38 #include <sys/systm.h>
40 #include <machine/pmc_mdep.h>
41 #include <machine/md_var.h>
42 #include <machine/mips_opcode.h>
43 #include <machine/vmparam.h>
48 * Per-processor information.
51 struct pmc_hw *pc_mipspmcs;
54 static struct mips_cpu **mips_pcpu;
56 #if defined(__mips_n64)
57 # define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \
58 ((vm_offset_t)(reg) >= MIPS_XKPHYS_START))
60 # define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \
61 ((vm_offset_t)(reg) >= MIPS_KSEG0_START))
65 * We need some reasonable default to prevent backtrace code
66 * from wandering too far
68 #define MAX_FUNCTION_SIZE 0x10000
69 #define MAX_PROLOGUE_SIZE 0x100
72 mips_allocate_pmc(int cpu, int ri, struct pmc *pm,
73 const struct pmc_op_pmcallocate *a)
76 uint32_t caps, config, counter;
80 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
81 ("[mips,%d] illegal CPU value %d", __LINE__, cpu));
82 KASSERT(ri >= 0 && ri < mips_npmcs,
83 ("[mips,%d] illegal row index %d", __LINE__, ri));
86 if (a->pm_class != mips_pmc_spec.ps_cpuclass)
89 counter = MIPS_CTR_ALL;
91 for (i = 0; i < mips_event_codes_size; i++) {
92 if (mips_event_codes[i].pe_ev == pe) {
93 event = mips_event_codes[i].pe_code;
94 counter = mips_event_codes[i].pe_counter;
99 if (i == mips_event_codes_size)
102 if ((counter != MIPS_CTR_ALL) && (counter != ri))
105 config = mips_get_perfctl(cpu, ri, event, caps);
107 pm->pm_md.pm_mips_evsel = config;
109 PMCDBG2(MDP,ALL,2,"mips-allocate ri=%d -> config=0x%x", ri, config);
116 mips_read_pmc(int cpu, int ri, pmc_value_t *v)
121 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
122 ("[mips,%d] illegal CPU value %d", __LINE__, cpu));
123 KASSERT(ri >= 0 && ri < mips_npmcs,
124 ("[mips,%d] illegal row index %d", __LINE__, ri));
126 pm = mips_pcpu[cpu]->pc_mipspmcs[ri].phw_pmc;
127 tmp = mips_pmcn_read(ri);
128 PMCDBG2(MDP,REA,2,"mips-read id=%d -> %jd", ri, tmp);
130 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
131 *v = tmp - (1UL << (mips_pmc_spec.ps_counter_width - 1));
139 mips_write_pmc(int cpu, int ri, pmc_value_t v)
143 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
144 ("[mips,%d] illegal CPU value %d", __LINE__, cpu));
145 KASSERT(ri >= 0 && ri < mips_npmcs,
146 ("[mips,%d] illegal row-index %d", __LINE__, ri));
148 pm = mips_pcpu[cpu]->pc_mipspmcs[ri].phw_pmc;
150 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
151 v = (1UL << (mips_pmc_spec.ps_counter_width - 1)) - v;
153 PMCDBG3(MDP,WRI,1,"mips-write cpu=%d ri=%d v=%jx", cpu, ri, v);
155 mips_pmcn_write(ri, v);
161 mips_config_pmc(int cpu, int ri, struct pmc *pm)
165 PMCDBG3(MDP,CFG,1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
167 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
168 ("[mips,%d] illegal CPU value %d", __LINE__, cpu));
169 KASSERT(ri >= 0 && ri < mips_npmcs,
170 ("[mips,%d] illegal row-index %d", __LINE__, ri));
172 phw = &mips_pcpu[cpu]->pc_mipspmcs[ri];
174 KASSERT(pm == NULL || phw->phw_pmc == NULL,
175 ("[mips,%d] pm=%p phw->pm=%p hwpmc not unconfigured",
176 __LINE__, pm, phw->phw_pmc));
184 mips_start_pmc(int cpu, int ri)
190 phw = &mips_pcpu[cpu]->pc_mipspmcs[ri];
192 config = pm->pm_md.pm_mips_evsel;
194 /* Enable the PMC. */
197 mips_wr_perfcnt0(config);
200 mips_wr_perfcnt2(config);
210 mips_stop_pmc(int cpu, int ri)
215 phw = &mips_pcpu[cpu]->pc_mipspmcs[ri];
221 * Clearing the entire register turns the counter off as well
222 * as removes the previously sampled event.
238 mips_release_pmc(int cpu, int ri, struct pmc *pmc)
242 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
243 ("[mips,%d] illegal CPU value %d", __LINE__, cpu));
244 KASSERT(ri >= 0 && ri < mips_npmcs,
245 ("[mips,%d] illegal row-index %d", __LINE__, ri));
247 phw = &mips_pcpu[cpu]->pc_mipspmcs[ri];
248 KASSERT(phw->phw_pmc == NULL,
249 ("[mips,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc));
255 mips_pmc_intr(int cpu, struct trapframe *tf)
264 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
265 ("[mips,%d] CPU %d out of range", __LINE__, cpu));
270 /* Stop PMCs without clearing the counter */
271 r0 = mips_rd_perfcnt0();
272 mips_wr_perfcnt0(r0 & ~(0x1f));
273 r2 = mips_rd_perfcnt2();
274 mips_wr_perfcnt2(r2 & ~(0x1f));
276 for (ri = 0; ri < mips_npmcs; ri++) {
277 pm = mips_pcpu[cpu]->pc_mipspmcs[ri].phw_pmc;
280 if (! PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
283 r = mips_pmcn_read(ri);
285 /* If bit 31 is set, the counter has overflowed */
286 if ((r & (1UL << (mips_pmc_spec.ps_counter_width - 1))) == 0)
290 if (pm->pm_state != PMC_STATE_RUNNING)
292 error = pmc_process_interrupt(cpu, PMC_HR, pm, tf,
295 /* Clear/disable the relevant counter */
300 mips_stop_pmc(cpu, ri);
303 /* Reload sampling count */
304 mips_write_pmc(cpu, ri, pm->pm_sc.pm_reloadcount);
308 * Re-enable the PMC counters where they left off.
310 * Any counter which overflowed will have its sample count
311 * reloaded in the loop above.
313 mips_wr_perfcnt0(r0);
314 mips_wr_perfcnt2(r2);
320 mips_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
324 char mips_name[PMC_NAME_MAX];
326 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
327 ("[mips,%d], illegal CPU %d", __LINE__, cpu));
328 KASSERT(ri >= 0 && ri < mips_npmcs,
329 ("[mips,%d] row-index %d out of range", __LINE__, ri));
331 phw = &mips_pcpu[cpu]->pc_mipspmcs[ri];
332 snprintf(mips_name, sizeof(mips_name), "MIPS-%d", ri);
333 if ((error = copystr(mips_name, pi->pm_name, PMC_NAME_MAX,
336 pi->pm_class = mips_pmc_spec.ps_cpuclass;
337 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
338 pi->pm_enabled = TRUE;
339 *ppmc = phw->phw_pmc;
341 pi->pm_enabled = FALSE;
349 mips_get_config(int cpu, int ri, struct pmc **ppm)
351 *ppm = mips_pcpu[cpu]->pc_mipspmcs[ri].phw_pmc;
357 * XXX don't know what we should do here.
360 mips_pmc_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
366 mips_pmc_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
372 mips_pcpu_init(struct pmc_mdep *md, int cpu)
376 struct mips_cpu *pac;
379 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
380 ("[mips,%d] wrong cpu number %d", __LINE__, cpu));
381 PMCDBG1(MDP,INI,1,"mips-init cpu=%d", cpu);
383 mips_pcpu[cpu] = pac = malloc(sizeof(struct mips_cpu), M_PMC,
385 pac->pc_mipspmcs = malloc(sizeof(struct pmc_hw) * mips_npmcs,
386 M_PMC, M_WAITOK|M_ZERO);
388 first_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_MIPS].pcd_ri;
389 KASSERT(pc != NULL, ("[mips,%d] NULL per-cpu pointer", __LINE__));
391 for (i = 0, phw = pac->pc_mipspmcs; i < mips_npmcs; i++, phw++) {
392 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED |
393 PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(i);
395 pc->pc_hwpmcs[i + first_ri] = phw;
399 * Clear the counter control register which has the effect
400 * of disabling counting.
402 for (i = 0; i < mips_npmcs; i++)
403 mips_pmcn_write(i, 0);
409 mips_pcpu_fini(struct pmc_mdep *md, int cpu)
415 pmc_mips_initialize()
417 struct pmc_mdep *pmc_mdep;
418 struct pmc_classdep *pcd;
421 * TODO: Use More bit of PerfCntlX register to detect actual
426 PMCDBG1(MDP,INI,1,"mips-init npmcs=%d", mips_npmcs);
429 * Allocate space for pointers to PMC HW descriptors and for
430 * the MDEP structure used by MI code.
432 mips_pcpu = malloc(sizeof(struct mips_cpu *) * pmc_cpu_max(), M_PMC,
436 pmc_mdep = pmc_mdep_alloc(1);
438 pmc_mdep->pmd_cputype = mips_pmc_spec.ps_cputype;
440 pcd = &pmc_mdep->pmd_classdep[PMC_MDEP_CLASS_INDEX_MIPS];
441 pcd->pcd_caps = mips_pmc_spec.ps_capabilities;
442 pcd->pcd_class = mips_pmc_spec.ps_cpuclass;
443 pcd->pcd_num = mips_npmcs;
444 pcd->pcd_ri = pmc_mdep->pmd_npmc;
445 pcd->pcd_width = mips_pmc_spec.ps_counter_width;
447 pcd->pcd_allocate_pmc = mips_allocate_pmc;
448 pcd->pcd_config_pmc = mips_config_pmc;
449 pcd->pcd_pcpu_fini = mips_pcpu_fini;
450 pcd->pcd_pcpu_init = mips_pcpu_init;
451 pcd->pcd_describe = mips_describe;
452 pcd->pcd_get_config = mips_get_config;
453 pcd->pcd_read_pmc = mips_read_pmc;
454 pcd->pcd_release_pmc = mips_release_pmc;
455 pcd->pcd_start_pmc = mips_start_pmc;
456 pcd->pcd_stop_pmc = mips_stop_pmc;
457 pcd->pcd_write_pmc = mips_write_pmc;
459 pmc_mdep->pmd_intr = mips_pmc_intr;
460 pmc_mdep->pmd_switch_in = mips_pmc_switch_in;
461 pmc_mdep->pmd_switch_out = mips_pmc_switch_out;
463 pmc_mdep->pmd_npmc += mips_npmcs;
469 pmc_mips_finalize(struct pmc_mdep *md)
474 #ifdef HWPMC_MIPS_BACKTRACE
477 pmc_next_frame(register_t *pc, register_t *sp)
481 uint32_t instr, mask;
485 /* Jump here after a nonstandard (interrupt handler) frame */
488 /* check for bad SP: could foul up next frame */
489 if (!MIPS_IS_VALID_KERNELADDR(*sp)) {
493 /* check for bad PC */
494 if (!MIPS_IS_VALID_KERNELADDR(*pc)) {
499 * Find the beginning of the current subroutine by scanning
500 * backwards from the current PC for the end of the previous
503 va = *pc - sizeof(int);
505 instr = *((uint32_t *)va);
507 /* [d]addiu sp,sp,-X */
508 if (((instr & 0xffff8000) == 0x27bd8000)
509 || ((instr & 0xffff8000) == 0x67bd8000))
513 if (instr == 0x03e00008) {
514 /* skip over branch-delay slot instruction */
515 va += 2 * sizeof(int);
522 /* skip over nulls which might separate .o files */
523 while ((instr = *((uint32_t *)va)) == 0)
526 /* scan forwards to find stack size and any saved registers */
530 for (; more; va += sizeof(int),
531 more = (more == 3) ? 3 : more - 1) {
532 /* stop if hit our current position */
535 instr = *((uint32_t *)va);
537 switch (i.JType.op) {
539 switch (i.RType.func) {
542 more = 2; /* stop after next instruction */
547 more = 1; /* stop now */
558 more = 2; /* stop after next instruction */
565 switch (i.RType.rs) {
568 more = 2; /* stop after next instruction */
575 * SP is being saved using S8(FP). Most likely it indicates
576 * that SP is modified in the function and we can't get
577 * its value safely without emulating code backward
578 * So just bail out on functions like this
580 if ((i.IType.rs == 30) && (i.IType.rt = 29))
583 /* look for saved registers on the stack */
584 if (i.IType.rs != 29)
586 /* only restore the first one */
587 if (mask & (1 << i.IType.rt))
589 mask |= (1 << i.IType.rt);
590 if (i.IType.rt == 31)
591 ra = *((register_t *)(*sp + (short)i.IType.imm));
598 /* look for stack pointer adjustment */
599 if (i.IType.rs != 29 || i.IType.rt != 29)
601 stksize = -((short)i.IType.imm);
605 if (!MIPS_IS_VALID_KERNELADDR(ra))
618 pmc_next_uframe(register_t *pc, register_t *sp, register_t *ra)
620 int offset, registers_on_stack;
621 uint32_t opcode, mask;
622 register_t function_start;
626 registers_on_stack = 0;
632 while (offset < MAX_FUNCTION_SIZE) {
633 opcode = fuword32((void *)(*pc - offset));
635 /* [d]addiu sp, sp, -X*/
636 if (((opcode & 0xffff8000) == 0x27bd8000)
637 || ((opcode & 0xffff8000) == 0x67bd8000)) {
638 function_start = *pc - offset;
639 registers_on_stack = 1;
644 if ((opcode & 0xffff8000) == 0x3c1c0000) {
646 * Function might start with this instruction
647 * Keep an eye on "jr ra" and sp correction
648 * with positive value further on
650 function_start = *pc - offset;
653 if (function_start) {
655 * Stop looking further. Possible end of
656 * function instruction: it means there is no
657 * stack modifications, sp is unchanged
660 /* [d]addiu sp,sp,X */
661 if (((opcode & 0xffff8000) == 0x27bd0000)
662 || ((opcode & 0xffff8000) == 0x67bd0000))
665 if (opcode == 0x03e00008)
669 offset += sizeof(int);
675 if (registers_on_stack) {
677 while ((offset < MAX_PROLOGUE_SIZE)
678 && ((function_start + offset) < *pc)) {
679 i.word = fuword32((void *)(function_start + offset));
680 switch (i.JType.op) {
682 /* look for saved registers on the stack */
683 if (i.IType.rs != 29)
685 /* only restore the first one */
686 if (mask & (1 << i.IType.rt))
688 mask |= (1 << i.IType.rt);
689 if (i.IType.rt == 31)
690 *ra = fuword32((void *)(*sp + (short)i.IType.imm));
693 #if defined(__mips_n64)
695 /* look for saved registers on the stack */
696 if (i.IType.rs != 29)
698 /* only restore the first one */
699 if (mask & (1 << i.IType.rt))
701 mask |= (1 << i.IType.rt);
703 if (i.IType.rt == 31)
704 *ra = fuword64((void *)(*sp + (short)i.IType.imm));
712 /* look for stack pointer adjustment */
713 if (i.IType.rs != 29 || i.IType.rt != 29)
715 stksize = -((short)i.IType.imm);
718 offset += sizeof(int);
723 * We reached the end of backtrace
734 #endif /* HWPMC_MIPS_BACKTRACE */
739 return pmc_mips_initialize();
743 pmc_md_finalize(struct pmc_mdep *md)
745 return pmc_mips_finalize(md);
749 pmc_save_kernel_callchain(uintptr_t *cc, int nframes,
750 struct trapframe *tf)
752 register_t pc, ra, sp;
761 #ifdef HWPMC_MIPS_BACKTRACE
763 * Unwind, and unwind, and unwind
766 if (frames >= nframes)
769 if (pmc_next_frame(&pc, &sp) < 0)
780 pmc_save_user_callchain(uintptr_t *cc, int nframes,
781 struct trapframe *tf)
783 register_t pc, ra, sp;
792 #ifdef HWPMC_MIPS_BACKTRACE
795 * Unwind, and unwind, and unwind
798 if (frames >= nframes)
801 if (pmc_next_uframe(&pc, &sp, &ra) < 0)