Skip to content

Commit 80947e7

Browse files
Geert Uytterhoevenozbenh
authored andcommitted
powerpc: Keep track of emulated instructions
If CONFIG_PPC_EMULATED_STATS is enabled, make available counters for the various classes of emulated instructions under /sys/kernel/debug/powerpc/emulated_instructions/ (assumed debugfs is mounted on /sys/kernel/debug). Optionally (controlled by /sys/kernel/debug/powerpc/emulated_instructions/do_warn), rate-limited warnings can be printed to the console when instructions are emulated. Signed-off-by: Geert Uytterhoeven <Geert.Uytterhoeven@sonycom.com> Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
1 parent f312deb commit 80947e7

4 files changed

Lines changed: 196 additions & 6 deletions

File tree

‎arch/powerpc/Kconfig.debug‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ config HCALL_STATS
4141
This option will add a small amount of overhead to all hypervisor
4242
calls.
4343

44+
config PPC_EMULATED_STATS
45+
bool "Emulated instructions tracking"
46+
depends on DEBUG_FS
47+
help
48+
Adds code to keep track of the number of instructions that are
49+
emulated by the in-kernel emulator. Counters for the various classes
50+
of emulated instructions are available under
51+
powerpc/emulated_instructions/ in the root of the debugfs file
52+
system. Optionally (controlled by
53+
powerpc/emulated_instructions/do_warn in debugfs), rate-limited
54+
warnings can be printed to the console when instructions are
55+
emulated.
56+
4457
config CODE_PATCHING_SELFTEST
4558
bool "Run self-tests of the code-patching code."
4659
depends on DEBUG_KERNEL
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2007 Sony Corporation
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; version 2 of the License.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program.
15+
* If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef _ASM_POWERPC_EMULATED_OPS_H
19+
#define _ASM_POWERPC_EMULATED_OPS_H
20+
21+
#include <asm/atomic.h>
22+
23+
24+
#ifdef CONFIG_PPC_EMULATED_STATS
25+
26+
struct ppc_emulated_entry {
27+
const char *name;
28+
atomic_t val;
29+
};
30+
31+
extern struct ppc_emulated {
32+
#ifdef CONFIG_ALTIVEC
33+
struct ppc_emulated_entry altivec;
34+
#endif
35+
struct ppc_emulated_entry dcba;
36+
struct ppc_emulated_entry dcbz;
37+
struct ppc_emulated_entry fp_pair;
38+
struct ppc_emulated_entry isel;
39+
struct ppc_emulated_entry mcrxr;
40+
struct ppc_emulated_entry mfpvr;
41+
struct ppc_emulated_entry multiple;
42+
struct ppc_emulated_entry popcntb;
43+
struct ppc_emulated_entry spe;
44+
struct ppc_emulated_entry string;
45+
struct ppc_emulated_entry unaligned;
46+
#ifdef CONFIG_MATH_EMULATION
47+
struct ppc_emulated_entry math;
48+
#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
49+
struct ppc_emulated_entry 8xx;
50+
#endif
51+
#ifdef CONFIG_VSX
52+
struct ppc_emulated_entry vsx;
53+
#endif
54+
} ppc_emulated;
55+
56+
extern u32 ppc_warn_emulated;
57+
58+
extern void ppc_warn_emulated_print(const char *type);
59+
60+
#define PPC_WARN_EMULATED(type) \
61+
do { \
62+
atomic_inc(&ppc_emulated.type.val); \
63+
if (ppc_warn_emulated) \
64+
ppc_warn_emulated_print(ppc_emulated.type.name); \
65+
} while (0)
66+
67+
#else /* !CONFIG_PPC_EMULATED_STATS */
68+
69+
#define PPC_WARN_EMULATED(type) do { } while (0)
70+
71+
#endif /* !CONFIG_PPC_EMULATED_STATS */
72+
73+
#endif /* _ASM_POWERPC_EMULATED_OPS_H */

‎arch/powerpc/kernel/align.c‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <asm/system.h>
2525
#include <asm/cache.h>
2626
#include <asm/cputable.h>
27+
#include <asm/emulated_ops.h>
2728

2829
struct aligninfo {
2930
unsigned char len;
@@ -730,8 +731,10 @@ int fix_alignment(struct pt_regs *regs)
730731
areg = dsisr & 0x1f; /* register to update */
731732

732733
#ifdef CONFIG_SPE
733-
if ((instr >> 26) == 0x4)
734+
if ((instr >> 26) == 0x4) {
735+
PPC_WARN_EMULATED(spe);
734736
return emulate_spe(regs, reg, instr);
737+
}
735738
#endif
736739

737740
instr = (dsisr >> 10) & 0x7f;
@@ -783,23 +786,28 @@ int fix_alignment(struct pt_regs *regs)
783786
flags |= SPLT;
784787
nb = 8;
785788
}
789+
PPC_WARN_EMULATED(vsx);
786790
return emulate_vsx(addr, reg, areg, regs, flags, nb);
787791
}
788792
#endif
789793
/* A size of 0 indicates an instruction we don't support, with
790794
* the exception of DCBZ which is handled as a special case here
791795
*/
792-
if (instr == DCBZ)
796+
if (instr == DCBZ) {
797+
PPC_WARN_EMULATED(dcbz);
793798
return emulate_dcbz(regs, addr);
799+
}
794800
if (unlikely(nb == 0))
795801
return 0;
796802

797803
/* Load/Store Multiple instructions are handled in their own
798804
* function
799805
*/
800-
if (flags & M)
806+
if (flags & M) {
807+
PPC_WARN_EMULATED(multiple);
801808
return emulate_multiple(regs, addr, reg, nb,
802809
flags, instr, swiz);
810+
}
803811

804812
/* Verify the address of the operand */
805813
if (unlikely(user_mode(regs) &&
@@ -816,8 +824,12 @@ int fix_alignment(struct pt_regs *regs)
816824
}
817825

818826
/* Special case for 16-byte FP loads and stores */
819-
if (nb == 16)
827+
if (nb == 16) {
828+
PPC_WARN_EMULATED(fp_pair);
820829
return emulate_fp_pair(addr, reg, flags);
830+
}
831+
832+
PPC_WARN_EMULATED(unaligned);
821833

822834
/* If we are loading, get the data from user space, else
823835
* get it from register values

‎arch/powerpc/kernel/traps.c‎

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#include <linux/backlight.h>
3434
#include <linux/bug.h>
3535
#include <linux/kdebug.h>
36+
#include <linux/debugfs.h>
3637

38+
#include <asm/emulated_ops.h>
3739
#include <asm/pgtable.h>
3840
#include <asm/uaccess.h>
3941
#include <asm/system.h>
@@ -757,36 +759,44 @@ static int emulate_instruction(struct pt_regs *regs)
757759

758760
/* Emulate the mfspr rD, PVR. */
759761
if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
762+
PPC_WARN_EMULATED(mfpvr);
760763
rd = (instword >> 21) & 0x1f;
761764
regs->gpr[rd] = mfspr(SPRN_PVR);
762765
return 0;
763766
}
764767

765768
/* Emulating the dcba insn is just a no-op. */
766-
if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA)
769+
if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
770+
PPC_WARN_EMULATED(dcba);
767771
return 0;
772+
}
768773

769774
/* Emulate the mcrxr insn. */
770775
if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
771776
int shift = (instword >> 21) & 0x1c;
772777
unsigned long msk = 0xf0000000UL >> shift;
773778

779+
PPC_WARN_EMULATED(mcrxr);
774780
regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
775781
regs->xer &= ~0xf0000000UL;
776782
return 0;
777783
}
778784

779785
/* Emulate load/store string insn. */
780-
if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING)
786+
if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
787+
PPC_WARN_EMULATED(string);
781788
return emulate_string_inst(regs, instword);
789+
}
782790

783791
/* Emulate the popcntb (Population Count Bytes) instruction. */
784792
if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
793+
PPC_WARN_EMULATED(popcntb);
785794
return emulate_popcntb_inst(regs, instword);
786795
}
787796

788797
/* Emulate isel (Integer Select) instruction */
789798
if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
799+
PPC_WARN_EMULATED(isel);
790800
return emulate_isel(regs, instword);
791801
}
792802

@@ -984,6 +994,8 @@ void SoftwareEmulation(struct pt_regs *regs)
984994

985995
#ifdef CONFIG_MATH_EMULATION
986996
errcode = do_mathemu(regs);
997+
if (errcode >= 0)
998+
PPC_WARN_EMULATED(math);
987999

9881000
switch (errcode) {
9891001
case 0:
@@ -1005,6 +1017,9 @@ void SoftwareEmulation(struct pt_regs *regs)
10051017

10061018
#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
10071019
errcode = Soft_emulate_8xx(regs);
1020+
if (errcode >= 0)
1021+
PPC_WARN_EMULATED(8xx);
1022+
10081023
switch (errcode) {
10091024
case 0:
10101025
emulate_single_step(regs);
@@ -1088,6 +1103,7 @@ void altivec_assist_exception(struct pt_regs *regs)
10881103

10891104
flush_altivec_to_thread(current);
10901105

1106+
PPC_WARN_EMULATED(altivec);
10911107
err = emulate_altivec(regs);
10921108
if (err == 0) {
10931109
regs->nip += 4; /* skip emulated instruction */
@@ -1286,3 +1302,79 @@ void kernel_bad_stack(struct pt_regs *regs)
12861302
void __init trap_init(void)
12871303
{
12881304
}
1305+
1306+
1307+
#ifdef CONFIG_PPC_EMULATED_STATS
1308+
1309+
#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1310+
1311+
struct ppc_emulated ppc_emulated = {
1312+
#ifdef CONFIG_ALTIVEC
1313+
WARN_EMULATED_SETUP(altivec),
1314+
#endif
1315+
WARN_EMULATED_SETUP(dcba),
1316+
WARN_EMULATED_SETUP(dcbz),
1317+
WARN_EMULATED_SETUP(fp_pair),
1318+
WARN_EMULATED_SETUP(isel),
1319+
WARN_EMULATED_SETUP(mcrxr),
1320+
WARN_EMULATED_SETUP(mfpvr),
1321+
WARN_EMULATED_SETUP(multiple),
1322+
WARN_EMULATED_SETUP(popcntb),
1323+
WARN_EMULATED_SETUP(spe),
1324+
WARN_EMULATED_SETUP(string),
1325+
WARN_EMULATED_SETUP(unaligned),
1326+
#ifdef CONFIG_MATH_EMULATION
1327+
WARN_EMULATED_SETUP(math),
1328+
#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1329+
WARN_EMULATED_SETUP(8xx),
1330+
#endif
1331+
#ifdef CONFIG_VSX
1332+
WARN_EMULATED_SETUP(vsx),
1333+
#endif
1334+
};
1335+
1336+
u32 ppc_warn_emulated;
1337+
1338+
void ppc_warn_emulated_print(const char *type)
1339+
{
1340+
if (printk_ratelimit())
1341+
pr_warning("%s used emulated %s instruction\n", current->comm,
1342+
type);
1343+
}
1344+
1345+
static int __init ppc_warn_emulated_init(void)
1346+
{
1347+
struct dentry *dir, *d;
1348+
unsigned int i;
1349+
struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1350+
1351+
if (!powerpc_debugfs_root)
1352+
return -ENODEV;
1353+
1354+
dir = debugfs_create_dir("emulated_instructions",
1355+
powerpc_debugfs_root);
1356+
if (!dir)
1357+
return -ENOMEM;
1358+
1359+
d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1360+
&ppc_warn_emulated);
1361+
if (!d)
1362+
goto fail;
1363+
1364+
for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1365+
d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1366+
(u32 *)&entries[i].val.counter);
1367+
if (!d)
1368+
goto fail;
1369+
}
1370+
1371+
return 0;
1372+
1373+
fail:
1374+
debugfs_remove_recursive(dir);
1375+
return -ENOMEM;
1376+
}
1377+
1378+
device_initcall(ppc_warn_emulated_init);
1379+
1380+
#endif /* CONFIG_PPC_EMULATED_STATS */

0 commit comments

Comments
 (0)