mce.c 9.9 KB

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