1 /* 2 * Machine check exception handling. 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; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 * Copyright 2013 IBM Corporation 19 * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com> 20 */ 21 22 #undef DEBUG 23 #define pr_fmt(fmt) "mce: " fmt 24 25 #include <linux/types.h> 26 #include <linux/ptrace.h> 27 #include <linux/percpu.h> 28 #include <linux/export.h> 29 #include <linux/irq_work.h> 30 #include <asm/mce.h> 31 32 static DEFINE_PER_CPU(int, mce_nest_count); 33 static DEFINE_PER_CPU(struct machine_check_event[MAX_MC_EVT], mce_event); 34 35 /* Queue for delayed MCE events. */ 36 static DEFINE_PER_CPU(int, mce_queue_count); 37 static DEFINE_PER_CPU(struct machine_check_event[MAX_MC_EVT], mce_event_queue); 38 39 static void machine_check_process_queued_event(struct irq_work *work); 40 static struct irq_work mce_event_process_work = { 41 .func = machine_check_process_queued_event, 42 }; 43 44 static void mce_set_error_info(struct machine_check_event *mce, 45 struct mce_error_info *mce_err) 46 { 47 mce->error_type = mce_err->error_type; 48 switch (mce_err->error_type) { 49 case MCE_ERROR_TYPE_UE: 50 mce->u.ue_error.ue_error_type = mce_err->u.ue_error_type; 51 break; 52 case MCE_ERROR_TYPE_SLB: 53 mce->u.slb_error.slb_error_type = mce_err->u.slb_error_type; 54 break; 55 case MCE_ERROR_TYPE_ERAT: 56 mce->u.erat_error.erat_error_type = mce_err->u.erat_error_type; 57 break; 58 case MCE_ERROR_TYPE_TLB: 59 mce->u.tlb_error.tlb_error_type = mce_err->u.tlb_error_type; 60 break; 61 case MCE_ERROR_TYPE_UNKNOWN: 62 default: 63 break; 64 } 65 } 66 67 /* 68 * Decode and save high level MCE information into per cpu buffer which 69 * is an array of machine_check_event structure. 70 */ 71 void save_mce_event(struct pt_regs *regs, long handled, 72 struct mce_error_info *mce_err, 73 uint64_t nip, uint64_t addr) 74 { 75 int index = __this_cpu_inc_return(mce_nest_count) - 1; 76 struct machine_check_event *mce = this_cpu_ptr(&mce_event[index]); 77 78 /* 79 * Return if we don't have enough space to log mce event. 80 * mce_nest_count may go beyond MAX_MC_EVT but that's ok, 81 * the check below will stop buffer overrun. 82 */ 83 if (index >= MAX_MC_EVT) 84 return; 85 86 /* Populate generic machine check info */ 87 mce->version = MCE_V1; 88 mce->srr0 = nip; 89 mce->srr1 = regs->msr; 90 mce->gpr3 = regs->gpr[3]; 91 mce->in_use = 1; 92 93 /* Mark it recovered if we have handled it and MSR(RI=1). */ 94 if (handled && (regs->msr & MSR_RI)) 95 mce->disposition = MCE_DISPOSITION_RECOVERED; 96 else 97 mce->disposition = MCE_DISPOSITION_NOT_RECOVERED; 98 99 mce->initiator = mce_err->initiator; 100 mce->severity = mce_err->severity; 101 102 /* 103 * Populate the mce error_type and type-specific error_type. 104 */ 105 mce_set_error_info(mce, mce_err); 106 107 if (!addr) 108 return; 109 110 if (mce->error_type == MCE_ERROR_TYPE_TLB) { 111 mce->u.tlb_error.effective_address_provided = true; 112 mce->u.tlb_error.effective_address = addr; 113 } else if (mce->error_type == MCE_ERROR_TYPE_SLB) { 114 mce->u.slb_error.effective_address_provided = true; 115 mce->u.slb_error.effective_address = addr; 116 } else if (mce->error_type == MCE_ERROR_TYPE_ERAT) { 117 mce->u.erat_error.effective_address_provided = true; 118 mce->u.erat_error.effective_address = addr; 119 } else if (mce->error_type == MCE_ERROR_TYPE_UE) { 120 mce->u.ue_error.effective_address_provided = true; 121 mce->u.ue_error.effective_address = addr; 122 } 123 return; 124 } 125 126 /* 127 * get_mce_event: 128 * mce Pointer to machine_check_event structure to be filled. 129 * release Flag to indicate whether to free the event slot or not. 130 * 0 <= do not release the mce event. Caller will invoke 131 * release_mce_event() once event has been consumed. 132 * 1 <= release the slot. 133 * 134 * return 1 = success 135 * 0 = failure 136 * 137 * get_mce_event() will be called by platform specific machine check 138 * handle routine and in KVM. 139 * When we call get_mce_event(), we are still in interrupt context and 140 * preemption will not be scheduled until ret_from_expect() routine 141 * is called. 142 */ 143 int get_mce_event(struct machine_check_event *mce, bool release) 144 { 145 int index = __this_cpu_read(mce_nest_count) - 1; 146 struct machine_check_event *mc_evt; 147 int ret = 0; 148 149 /* Sanity check */ 150 if (index < 0) 151 return ret; 152 153 /* Check if we have MCE info to process. */ 154 if (index < MAX_MC_EVT) { 155 mc_evt = this_cpu_ptr(&mce_event[index]); 156 /* Copy the event structure and release the original */ 157 if (mce) 158 *mce = *mc_evt; 159 if (release) 160 mc_evt->in_use = 0; 161 ret = 1; 162 } 163 /* Decrement the count to free the slot. */ 164 if (release) 165 __this_cpu_dec(mce_nest_count); 166 167 return ret; 168 } 169 170 void release_mce_event(void) 171 { 172 get_mce_event(NULL, true); 173 } 174 175 /* 176 * Queue up the MCE event which then can be handled later. 177 */ 178 void machine_check_queue_event(void) 179 { 180 int index; 181 struct machine_check_event evt; 182 183 if (!get_mce_event(&evt, MCE_EVENT_RELEASE)) 184 return; 185 186 index = __this_cpu_inc_return(mce_queue_count) - 1; 187 /* If queue is full, just return for now. */ 188 if (index >= MAX_MC_EVT) { 189 __this_cpu_dec(mce_queue_count); 190 return; 191 } 192 memcpy(this_cpu_ptr(&mce_event_queue[index]), &evt, sizeof(evt)); 193 194 /* Queue irq work to process this event later. */ 195 irq_work_queue(&mce_event_process_work); 196 } 197 198 /* 199 * process pending MCE event from the mce event queue. This function will be 200 * called during syscall exit. 201 */ 202 static void machine_check_process_queued_event(struct irq_work *work) 203 { 204 int index; 205 206 /* 207 * For now just print it to console. 208 * TODO: log this error event to FSP or nvram. 209 */ 210 while (__this_cpu_read(mce_queue_count) > 0) { 211 index = __this_cpu_read(mce_queue_count) - 1; 212 machine_check_print_event_info( 213 this_cpu_ptr(&mce_event_queue[index])); 214 __this_cpu_dec(mce_queue_count); 215 } 216 } 217 218 void machine_check_print_event_info(struct machine_check_event *evt) 219 { 220 const char *level, *sevstr, *subtype; 221 static const char *mc_ue_types[] = { 222 "Indeterminate", 223 "Instruction fetch", 224 "Page table walk ifetch", 225 "Load/Store", 226 "Page table walk Load/Store", 227 }; 228 static const char *mc_slb_types[] = { 229 "Indeterminate", 230 "Parity", 231 "Multihit", 232 }; 233 static const char *mc_erat_types[] = { 234 "Indeterminate", 235 "Parity", 236 "Multihit", 237 }; 238 static const char *mc_tlb_types[] = { 239 "Indeterminate", 240 "Parity", 241 "Multihit", 242 }; 243 244 /* Print things out */ 245 if (evt->version != MCE_V1) { 246 pr_err("Machine Check Exception, Unknown event version %d !\n", 247 evt->version); 248 return; 249 } 250 switch (evt->severity) { 251 case MCE_SEV_NO_ERROR: 252 level = KERN_INFO; 253 sevstr = "Harmless"; 254 break; 255 case MCE_SEV_WARNING: 256 level = KERN_WARNING; 257 sevstr = ""; 258 break; 259 case MCE_SEV_ERROR_SYNC: 260 level = KERN_ERR; 261 sevstr = "Severe"; 262 break; 263 case MCE_SEV_FATAL: 264 default: 265 level = KERN_ERR; 266 sevstr = "Fatal"; 267 break; 268 } 269 270 printk("%s%s Machine check interrupt [%s]\n", level, sevstr, 271 evt->disposition == MCE_DISPOSITION_RECOVERED ? 272 "Recovered" : "[Not recovered"); 273 printk("%s Initiator: %s\n", level, 274 evt->initiator == MCE_INITIATOR_CPU ? "CPU" : "Unknown"); 275 switch (evt->error_type) { 276 case MCE_ERROR_TYPE_UE: 277 subtype = evt->u.ue_error.ue_error_type < 278 ARRAY_SIZE(mc_ue_types) ? 279 mc_ue_types[evt->u.ue_error.ue_error_type] 280 : "Unknown"; 281 printk("%s Error type: UE [%s]\n", level, subtype); 282 if (evt->u.ue_error.effective_address_provided) 283 printk("%s Effective address: %016llx\n", 284 level, evt->u.ue_error.effective_address); 285 if (evt->u.ue_error.physical_address_provided) 286 printk("%s Physical address: %016llx\n", 287 level, evt->u.ue_error.physical_address); 288 break; 289 case MCE_ERROR_TYPE_SLB: 290 subtype = evt->u.slb_error.slb_error_type < 291 ARRAY_SIZE(mc_slb_types) ? 292 mc_slb_types[evt->u.slb_error.slb_error_type] 293 : "Unknown"; 294 printk("%s Error type: SLB [%s]\n", level, subtype); 295 if (evt->u.slb_error.effective_address_provided) 296 printk("%s Effective address: %016llx\n", 297 level, evt->u.slb_error.effective_address); 298 break; 299 case MCE_ERROR_TYPE_ERAT: 300 subtype = evt->u.erat_error.erat_error_type < 301 ARRAY_SIZE(mc_erat_types) ? 302 mc_erat_types[evt->u.erat_error.erat_error_type] 303 : "Unknown"; 304 printk("%s Error type: ERAT [%s]\n", level, subtype); 305 if (evt->u.erat_error.effective_address_provided) 306 printk("%s Effective address: %016llx\n", 307 level, evt->u.erat_error.effective_address); 308 break; 309 case MCE_ERROR_TYPE_TLB: 310 subtype = evt->u.tlb_error.tlb_error_type < 311 ARRAY_SIZE(mc_tlb_types) ? 312 mc_tlb_types[evt->u.tlb_error.tlb_error_type] 313 : "Unknown"; 314 printk("%s Error type: TLB [%s]\n", level, subtype); 315 if (evt->u.tlb_error.effective_address_provided) 316 printk("%s Effective address: %016llx\n", 317 level, evt->u.tlb_error.effective_address); 318 break; 319 default: 320 case MCE_ERROR_TYPE_UNKNOWN: 321 printk("%s Error type: Unknown\n", level); 322 break; 323 } 324 } 325 326 uint64_t get_mce_fault_addr(struct machine_check_event *evt) 327 { 328 switch (evt->error_type) { 329 case MCE_ERROR_TYPE_UE: 330 if (evt->u.ue_error.effective_address_provided) 331 return evt->u.ue_error.effective_address; 332 break; 333 case MCE_ERROR_TYPE_SLB: 334 if (evt->u.slb_error.effective_address_provided) 335 return evt->u.slb_error.effective_address; 336 break; 337 case MCE_ERROR_TYPE_ERAT: 338 if (evt->u.erat_error.effective_address_provided) 339 return evt->u.erat_error.effective_address; 340 break; 341 case MCE_ERROR_TYPE_TLB: 342 if (evt->u.tlb_error.effective_address_provided) 343 return evt->u.tlb_error.effective_address; 344 break; 345 default: 346 case MCE_ERROR_TYPE_UNKNOWN: 347 break; 348 } 349 return 0; 350 } 351 EXPORT_SYMBOL(get_mce_fault_addr); 352