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(PMC_HR, pm, tf);
294 /* Clear/disable the relevant counter */
299 mips_stop_pmc(cpu, ri);
302 /* Reload sampling count */
303 mips_write_pmc(cpu, ri, pm->pm_sc.pm_reloadcount);
307 * Re-enable the PMC counters where they left off.
309 * Any counter which overflowed will have its sample count
310 * reloaded in the loop above.
312 mips_wr_perfcnt0(r0);
313 mips_wr_perfcnt2(r2);
319 mips_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
323 char mips_name[PMC_NAME_MAX];
325 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
326 ("[mips,%d], illegal CPU %d", __LINE__, cpu));
327 KASSERT(ri >= 0 && ri < mips_npmcs,
328 ("[mips,%d] row-index %d out of range", __LINE__, ri));
330 phw = &mips_pcpu[cpu]->pc_mipspmcs[ri];
331 snprintf(mips_name, sizeof(mips_name), "MIPS-%d", ri);
332 if ((error = copystr(mips_name, pi->pm_name, PMC_NAME_MAX,
335 pi->pm_class = mips_pmc_spec.ps_cpuclass;
336 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
337 pi->pm_enabled = TRUE;
338 *ppmc = phw->phw_pmc;
340 pi->pm_enabled = FALSE;
348 mips_get_config(int cpu, int ri, struct pmc **ppm)
350 *ppm = mips_pcpu[cpu]->pc_mipspmcs[ri].phw_pmc;
356 * XXX don't know what we should do here.
359 mips_pmc_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
365 mips_pmc_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
371 mips_pcpu_init(struct pmc_mdep *md, int cpu)
375 struct mips_cpu *pac;
378 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
379 ("[mips,%d] wrong cpu number %d", __LINE__, cpu));
380 PMCDBG1(MDP,INI,1,"mips-init cpu=%d", cpu);
382 mips_pcpu[cpu] = pac = malloc(sizeof(struct mips_cpu), M_PMC,
384 pac->pc_mipspmcs = malloc(sizeof(struct pmc_hw) * mips_npmcs,
385 M_PMC, M_WAITOK|M_ZERO);
387 first_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_MIPS].pcd_ri;
388 KASSERT(pc != NULL, ("[mips,%d] NULL per-cpu pointer", __LINE__));
390 for (i = 0, phw = pac->pc_mipspmcs; i < mips_npmcs; i++, phw++) {
391 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED |
392 PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(i);
394 pc->pc_hwpmcs[i + first_ri] = phw;
398 * Clear the counter control register which has the effect
399 * of disabling counting.
401 for (i = 0; i < mips_npmcs; i++)
402 mips_pmcn_write(i, 0);
408 mips_pcpu_fini(struct pmc_mdep *md, int cpu)
414 pmc_mips_initialize()
416 struct pmc_mdep *pmc_mdep;
417 struct pmc_classdep *pcd;
420 * TODO: Use More bit of PerfCntlX register to detect actual
425 PMCDBG1(MDP,INI,1,"mips-init npmcs=%d", mips_npmcs);
428 * Allocate space for pointers to PMC HW descriptors and for
429 * the MDEP structure used by MI code.
431 mips_pcpu = malloc(sizeof(struct mips_cpu *) * pmc_cpu_max(), M_PMC,
435 pmc_mdep = pmc_mdep_alloc(1);
437 pmc_mdep->pmd_cputype = mips_pmc_spec.ps_cputype;
439 pcd = &pmc_mdep->pmd_classdep[PMC_MDEP_CLASS_INDEX_MIPS];
440 pcd->pcd_caps = mips_pmc_spec.ps_capabilities;
441 pcd->pcd_class = mips_pmc_spec.ps_cpuclass;
442 pcd->pcd_num = mips_npmcs;
443 pcd->pcd_ri = pmc_mdep->pmd_npmc;
444 pcd->pcd_width = mips_pmc_spec.ps_counter_width;
446 pcd->pcd_allocate_pmc = mips_allocate_pmc;
447 pcd->pcd_config_pmc = mips_config_pmc;
448 pcd->pcd_pcpu_fini = mips_pcpu_fini;
449 pcd->pcd_pcpu_init = mips_pcpu_init;
450 pcd->pcd_describe = mips_describe;
451 pcd->pcd_get_config = mips_get_config;
452 pcd->pcd_read_pmc = mips_read_pmc;
453 pcd->pcd_release_pmc = mips_release_pmc;
454 pcd->pcd_start_pmc = mips_start_pmc;
455 pcd->pcd_stop_pmc = mips_stop_pmc;
456 pcd->pcd_write_pmc = mips_write_pmc;
458 pmc_mdep->pmd_intr = mips_pmc_intr;
459 pmc_mdep->pmd_switch_in = mips_pmc_switch_in;
460 pmc_mdep->pmd_switch_out = mips_pmc_switch_out;
462 pmc_mdep->pmd_npmc += mips_npmcs;
468 pmc_mips_finalize(struct pmc_mdep *md)
473 #ifdef HWPMC_MIPS_BACKTRACE
476 pmc_next_frame(register_t *pc, register_t *sp)
480 uint32_t instr, mask;
484 /* Jump here after a nonstandard (interrupt handler) frame */
487 /* check for bad SP: could foul up next frame */
488 if (!MIPS_IS_VALID_KERNELADDR(*sp)) {
492 /* check for bad PC */
493 if (!MIPS_IS_VALID_KERNELADDR(*pc)) {
498 * Find the beginning of the current subroutine by scanning
499 * backwards from the current PC for the end of the previous
502 va = *pc - sizeof(int);
504 instr = *((uint32_t *)va);
506 /* [d]addiu sp,sp,-X */
507 if (((instr & 0xffff8000) == 0x27bd8000)
508 || ((instr & 0xffff8000) == 0x67bd8000))
512 if (instr == 0x03e00008) {
513 /* skip over branch-delay slot instruction */
514 va += 2 * sizeof(int);
521 /* skip over nulls which might separate .o files */
522 while ((instr = *((uint32_t *)va)) == 0)
525 /* scan forwards to find stack size and any saved registers */
529 for (; more; va += sizeof(int),
530 more = (more == 3) ? 3 : more - 1) {
531 /* stop if hit our current position */
534 instr = *((uint32_t *)va);
536 switch (i.JType.op) {
538 switch (i.RType.func) {
541 more = 2; /* stop after next instruction */
546 more = 1; /* stop now */
557 more = 2; /* stop after next instruction */
564 switch (i.RType.rs) {
567 more = 2; /* stop after next instruction */
574 * SP is being saved using S8(FP). Most likely it indicates
575 * that SP is modified in the function and we can't get
576 * its value safely without emulating code backward
577 * So just bail out on functions like this
579 if ((i.IType.rs == 30) && (i.IType.rt = 29))
582 /* look for saved registers on the stack */
583 if (i.IType.rs != 29)
585 /* only restore the first one */
586 if (mask & (1 << i.IType.rt))
588 mask |= (1 << i.IType.rt);
589 if (i.IType.rt == 31)
590 ra = *((register_t *)(*sp + (short)i.IType.imm));
597 /* look for stack pointer adjustment */
598 if (i.IType.rs != 29 || i.IType.rt != 29)
600 stksize = -((short)i.IType.imm);
604 if (!MIPS_IS_VALID_KERNELADDR(ra))
617 pmc_next_uframe(register_t *pc, register_t *sp, register_t *ra)
619 int offset, registers_on_stack;
620 uint32_t opcode, mask;
621 register_t function_start;
625 registers_on_stack = 0;
631 while (offset < MAX_FUNCTION_SIZE) {
632 opcode = fuword32((void *)(*pc - offset));
634 /* [d]addiu sp, sp, -X*/
635 if (((opcode & 0xffff8000) == 0x27bd8000)
636 || ((opcode & 0xffff8000) == 0x67bd8000)) {
637 function_start = *pc - offset;
638 registers_on_stack = 1;
643 if ((opcode & 0xffff8000) == 0x3c1c0000) {
645 * Function might start with this instruction
646 * Keep an eye on "jr ra" and sp correction
647 * with positive value further on
649 function_start = *pc - offset;
652 if (function_start) {
654 * Stop looking further. Possible end of
655 * function instruction: it means there is no
656 * stack modifications, sp is unchanged
659 /* [d]addiu sp,sp,X */
660 if (((opcode & 0xffff8000) == 0x27bd0000)
661 || ((opcode & 0xffff8000) == 0x67bd0000))
664 if (opcode == 0x03e00008)
668 offset += sizeof(int);
674 if (registers_on_stack) {
676 while ((offset < MAX_PROLOGUE_SIZE)
677 && ((function_start + offset) < *pc)) {
678 i.word = fuword32((void *)(function_start + offset));
679 switch (i.JType.op) {
681 /* look for saved registers on the stack */
682 if (i.IType.rs != 29)
684 /* only restore the first one */
685 if (mask & (1 << i.IType.rt))
687 mask |= (1 << i.IType.rt);
688 if (i.IType.rt == 31)
689 *ra = fuword32((void *)(*sp + (short)i.IType.imm));
692 #if defined(__mips_n64)
694 /* look for saved registers on the stack */
695 if (i.IType.rs != 29)
697 /* only restore the first one */
698 if (mask & (1 << i.IType.rt))
700 mask |= (1 << i.IType.rt);
702 if (i.IType.rt == 31)
703 *ra = fuword64((void *)(*sp + (short)i.IType.imm));
711 /* look for stack pointer adjustment */
712 if (i.IType.rs != 29 || i.IType.rt != 29)
714 stksize = -((short)i.IType.imm);
717 offset += sizeof(int);
722 * We reached the end of backtrace
733 #endif /* HWPMC_MIPS_BACKTRACE */
738 return pmc_mips_initialize();
742 pmc_md_finalize(struct pmc_mdep *md)
744 return pmc_mips_finalize(md);
748 pmc_save_kernel_callchain(uintptr_t *cc, int nframes,
749 struct trapframe *tf)
751 register_t pc, ra, sp;
760 #ifdef HWPMC_MIPS_BACKTRACE
762 * Unwind, and unwind, and unwind
765 if (frames >= nframes)
768 if (pmc_next_frame(&pc, &sp) < 0)
779 pmc_save_user_callchain(uintptr_t *cc, int nframes,
780 struct trapframe *tf)
782 register_t pc, ra, sp;
791 #ifdef HWPMC_MIPS_BACKTRACE
794 * Unwind, and unwind, and unwind
797 if (frames >= nframes)
800 if (pmc_next_uframe(&pc, &sp, &ra) < 0)