2 * Performance event support for s390x
4 * Copyright IBM Corp. 2012
5 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (version 2 only)
9 * as published by the Free Software Foundation.
11 #define KMSG_COMPONENT "perf"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/perf_event.h>
16 #include <linux/kvm_host.h>
17 #include <linux/percpu.h>
18 #include <linux/export.h>
20 #include <asm/cpu_mf.h>
21 #include <asm/lowcore.h>
22 #include <asm/processor.h>
24 const char *perf_pmu_name(void)
26 if (cpum_cf_avail() || cpum_sf_avail())
27 return "CPU-measurement facilities (CPUMF)";
30 EXPORT_SYMBOL(perf_pmu_name
);
32 int perf_num_counters(void)
37 num
+= PERF_CPUM_CF_MAX_CTR
;
41 EXPORT_SYMBOL(perf_num_counters
);
43 static struct kvm_s390_sie_block
*sie_block(struct pt_regs
*regs
)
45 struct stack_frame
*stack
= (struct stack_frame
*) regs
->gprs
[15];
50 return (struct kvm_s390_sie_block
*) stack
->empty1
[0];
53 static bool is_in_guest(struct pt_regs
*regs
)
57 #if defined(CONFIG_KVM) || defined(CONFIG_KVM_MODULE)
58 return instruction_pointer(regs
) == (unsigned long) &sie_exit
;
64 static unsigned long guest_is_user_mode(struct pt_regs
*regs
)
66 return sie_block(regs
)->gpsw
.mask
& PSW_MASK_PSTATE
;
69 static unsigned long instruction_pointer_guest(struct pt_regs
*regs
)
71 return sie_block(regs
)->gpsw
.addr
& PSW_ADDR_INSN
;
74 unsigned long perf_instruction_pointer(struct pt_regs
*regs
)
76 return is_in_guest(regs
) ? instruction_pointer_guest(regs
)
77 : instruction_pointer(regs
);
80 static unsigned long perf_misc_guest_flags(struct pt_regs
*regs
)
82 return guest_is_user_mode(regs
) ? PERF_RECORD_MISC_GUEST_USER
83 : PERF_RECORD_MISC_GUEST_KERNEL
;
86 unsigned long perf_misc_flags(struct pt_regs
*regs
)
88 if (is_in_guest(regs
))
89 return perf_misc_guest_flags(regs
);
91 return user_mode(regs
) ? PERF_RECORD_MISC_USER
92 : PERF_RECORD_MISC_KERNEL
;
95 void perf_event_print_debug(void)
97 struct cpumf_ctr_info cf_info
;
101 if (!cpum_cf_avail())
104 local_irq_save(flags
);
106 cpu
= smp_processor_id();
107 memset(&cf_info
, 0, sizeof(cf_info
));
108 if (!qctri(&cf_info
))
109 pr_info("CPU[%i] CPUM_CF: ver=%u.%u A=%04x E=%04x C=%04x\n",
110 cpu
, cf_info
.cfvn
, cf_info
.csvn
,
111 cf_info
.auth_ctl
, cf_info
.enable_ctl
, cf_info
.act_ctl
);
113 local_irq_restore(flags
);
116 /* See also arch/s390/kernel/traps.c */
117 static unsigned long __store_trace(struct perf_callchain_entry
*entry
,
119 unsigned long low
, unsigned long high
)
121 struct stack_frame
*sf
;
122 struct pt_regs
*regs
;
125 sp
= sp
& PSW_ADDR_INSN
;
126 if (sp
< low
|| sp
> high
- sizeof(*sf
))
128 sf
= (struct stack_frame
*) sp
;
129 perf_callchain_store(entry
, sf
->gprs
[8] & PSW_ADDR_INSN
);
130 /* Follow the backchain. */
133 sp
= sf
->back_chain
& PSW_ADDR_INSN
;
136 if (sp
<= low
|| sp
> high
- sizeof(*sf
))
138 sf
= (struct stack_frame
*) sp
;
139 perf_callchain_store(entry
,
140 sf
->gprs
[8] & PSW_ADDR_INSN
);
142 /* Zero backchain detected, check for interrupt frame. */
143 sp
= (unsigned long) (sf
+ 1);
144 if (sp
<= low
|| sp
> high
- sizeof(*regs
))
146 regs
= (struct pt_regs
*) sp
;
147 perf_callchain_store(entry
, sf
->gprs
[8] & PSW_ADDR_INSN
);
153 void perf_callchain_kernel(struct perf_callchain_entry
*entry
,
154 struct pt_regs
*regs
)
157 struct stack_frame
*head_sf
;
162 head
= regs
->gprs
[15];
163 head_sf
= (struct stack_frame
*) head
;
165 if (!head_sf
|| !head_sf
->back_chain
)
168 head
= head_sf
->back_chain
;
169 head
= __store_trace(entry
, head
, S390_lowcore
.async_stack
- ASYNC_SIZE
,
170 S390_lowcore
.async_stack
);
172 __store_trace(entry
, head
, S390_lowcore
.thread_info
,
173 S390_lowcore
.thread_info
+ THREAD_SIZE
);