mce.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. 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. if (handled)
  86. mce->disposition = MCE_DISPOSITION_RECOVERED;
  87. else
  88. mce->disposition = MCE_DISPOSITION_NOT_RECOVERED;
  89. mce->severity = MCE_SEV_ERROR_SYNC;
  90. srr1 = regs->msr;
  91. /*
  92. * Populate the mce error_type and type-specific error_type.
  93. */
  94. mce_set_error_info(mce, mce_err);
  95. if (!addr)
  96. return;
  97. if (mce->error_type == MCE_ERROR_TYPE_TLB) {
  98. mce->u.tlb_error.effective_address_provided = true;
  99. mce->u.tlb_error.effective_address = addr;
  100. } else if (mce->error_type == MCE_ERROR_TYPE_SLB) {
  101. mce->u.slb_error.effective_address_provided = true;
  102. mce->u.slb_error.effective_address = addr;
  103. } else if (mce->error_type == MCE_ERROR_TYPE_ERAT) {
  104. mce->u.erat_error.effective_address_provided = true;
  105. mce->u.erat_error.effective_address = addr;
  106. } else if (mce->error_type == MCE_ERROR_TYPE_UE) {
  107. mce->u.ue_error.effective_address_provided = true;
  108. mce->u.ue_error.effective_address = addr;
  109. }
  110. return;
  111. }
  112. /*
  113. * get_mce_event:
  114. * mce Pointer to machine_check_event structure to be filled.
  115. * release Flag to indicate whether to free the event slot or not.
  116. * 0 <= do not release the mce event. Caller will invoke
  117. * release_mce_event() once event has been consumed.
  118. * 1 <= release the slot.
  119. *
  120. * return 1 = success
  121. * 0 = failure
  122. *
  123. * get_mce_event() will be called by platform specific machine check
  124. * handle routine and in KVM.
  125. * When we call get_mce_event(), we are still in interrupt context and
  126. * preemption will not be scheduled until ret_from_expect() routine
  127. * is called.
  128. */
  129. int get_mce_event(struct machine_check_event *mce, bool release)
  130. {
  131. int index = __this_cpu_read(mce_nest_count) - 1;
  132. struct machine_check_event *mc_evt;
  133. int ret = 0;
  134. /* Sanity check */
  135. if (index < 0)
  136. return ret;
  137. /* Check if we have MCE info to process. */
  138. if (index < MAX_MC_EVT) {
  139. mc_evt = this_cpu_ptr(&mce_event[index]);
  140. /* Copy the event structure and release the original */
  141. if (mce)
  142. *mce = *mc_evt;
  143. if (release)
  144. mc_evt->in_use = 0;
  145. ret = 1;
  146. }
  147. /* Decrement the count to free the slot. */
  148. if (release)
  149. __this_cpu_dec(mce_nest_count);
  150. return ret;
  151. }
  152. void release_mce_event(void)
  153. {
  154. get_mce_event(NULL, true);
  155. }
  156. /*
  157. * Queue up the MCE event which then can be handled later.
  158. */
  159. void machine_check_queue_event(void)
  160. {
  161. int index;
  162. struct machine_check_event evt;
  163. if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
  164. return;
  165. index = __this_cpu_inc_return(mce_queue_count) - 1;
  166. /* If queue is full, just return for now. */
  167. if (index >= MAX_MC_EVT) {
  168. __this_cpu_dec(mce_queue_count);
  169. return;
  170. }
  171. memcpy(this_cpu_ptr(&mce_event_queue[index]), &evt, sizeof(evt));
  172. /* Queue irq work to process this event later. */
  173. irq_work_queue(&mce_event_process_work);
  174. }
  175. /*
  176. * process pending MCE event from the mce event queue. This function will be
  177. * called during syscall exit.
  178. */
  179. static void machine_check_process_queued_event(struct irq_work *work)
  180. {
  181. int index;
  182. add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
  183. /*
  184. * For now just print it to console.
  185. * TODO: log this error event to FSP or nvram.
  186. */
  187. while (__this_cpu_read(mce_queue_count) > 0) {
  188. index = __this_cpu_read(mce_queue_count) - 1;
  189. machine_check_print_event_info(
  190. this_cpu_ptr(&mce_event_queue[index]));
  191. __this_cpu_dec(mce_queue_count);
  192. }
  193. }
  194. void machine_check_print_event_info(struct machine_check_event *evt)
  195. {
  196. const char *level, *sevstr, *subtype;
  197. static const char *mc_ue_types[] = {
  198. "Indeterminate",
  199. "Instruction fetch",
  200. "Page table walk ifetch",
  201. "Load/Store",
  202. "Page table walk Load/Store",
  203. };
  204. static const char *mc_slb_types[] = {
  205. "Indeterminate",
  206. "Parity",
  207. "Multihit",
  208. };
  209. static const char *mc_erat_types[] = {
  210. "Indeterminate",
  211. "Parity",
  212. "Multihit",
  213. };
  214. static const char *mc_tlb_types[] = {
  215. "Indeterminate",
  216. "Parity",
  217. "Multihit",
  218. };
  219. /* Print things out */
  220. if (evt->version != MCE_V1) {
  221. pr_err("Machine Check Exception, Unknown event version %d !\n",
  222. evt->version);
  223. return;
  224. }
  225. switch (evt->severity) {
  226. case MCE_SEV_NO_ERROR:
  227. level = KERN_INFO;
  228. sevstr = "Harmless";
  229. break;
  230. case MCE_SEV_WARNING:
  231. level = KERN_WARNING;
  232. sevstr = "";
  233. break;
  234. case MCE_SEV_ERROR_SYNC:
  235. level = KERN_ERR;
  236. sevstr = "Severe";
  237. break;
  238. case MCE_SEV_FATAL:
  239. default:
  240. level = KERN_ERR;
  241. sevstr = "Fatal";
  242. break;
  243. }
  244. printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
  245. evt->disposition == MCE_DISPOSITION_RECOVERED ?
  246. "Recovered" : "[Not recovered");
  247. printk("%s Initiator: %s\n", level,
  248. evt->initiator == MCE_INITIATOR_CPU ? "CPU" : "Unknown");
  249. switch (evt->error_type) {
  250. case MCE_ERROR_TYPE_UE:
  251. subtype = evt->u.ue_error.ue_error_type <
  252. ARRAY_SIZE(mc_ue_types) ?
  253. mc_ue_types[evt->u.ue_error.ue_error_type]
  254. : "Unknown";
  255. printk("%s Error type: UE [%s]\n", level, subtype);
  256. if (evt->u.ue_error.effective_address_provided)
  257. printk("%s Effective address: %016llx\n",
  258. level, evt->u.ue_error.effective_address);
  259. if (evt->u.ue_error.physical_address_provided)
  260. printk("%s Physial address: %016llx\n",
  261. level, evt->u.ue_error.physical_address);
  262. break;
  263. case MCE_ERROR_TYPE_SLB:
  264. subtype = evt->u.slb_error.slb_error_type <
  265. ARRAY_SIZE(mc_slb_types) ?
  266. mc_slb_types[evt->u.slb_error.slb_error_type]
  267. : "Unknown";
  268. printk("%s Error type: SLB [%s]\n", level, subtype);
  269. if (evt->u.slb_error.effective_address_provided)
  270. printk("%s Effective address: %016llx\n",
  271. level, evt->u.slb_error.effective_address);
  272. break;
  273. case MCE_ERROR_TYPE_ERAT:
  274. subtype = evt->u.erat_error.erat_error_type <
  275. ARRAY_SIZE(mc_erat_types) ?
  276. mc_erat_types[evt->u.erat_error.erat_error_type]
  277. : "Unknown";
  278. printk("%s Error type: ERAT [%s]\n", level, subtype);
  279. if (evt->u.erat_error.effective_address_provided)
  280. printk("%s Effective address: %016llx\n",
  281. level, evt->u.erat_error.effective_address);
  282. break;
  283. case MCE_ERROR_TYPE_TLB:
  284. subtype = evt->u.tlb_error.tlb_error_type <
  285. ARRAY_SIZE(mc_tlb_types) ?
  286. mc_tlb_types[evt->u.tlb_error.tlb_error_type]
  287. : "Unknown";
  288. printk("%s Error type: TLB [%s]\n", level, subtype);
  289. if (evt->u.tlb_error.effective_address_provided)
  290. printk("%s Effective address: %016llx\n",
  291. level, evt->u.tlb_error.effective_address);
  292. break;
  293. default:
  294. case MCE_ERROR_TYPE_UNKNOWN:
  295. printk("%s Error type: Unknown\n", level);
  296. break;
  297. }
  298. }
  299. uint64_t get_mce_fault_addr(struct machine_check_event *evt)
  300. {
  301. switch (evt->error_type) {
  302. case MCE_ERROR_TYPE_UE:
  303. if (evt->u.ue_error.effective_address_provided)
  304. return evt->u.ue_error.effective_address;
  305. break;
  306. case MCE_ERROR_TYPE_SLB:
  307. if (evt->u.slb_error.effective_address_provided)
  308. return evt->u.slb_error.effective_address;
  309. break;
  310. case MCE_ERROR_TYPE_ERAT:
  311. if (evt->u.erat_error.effective_address_provided)
  312. return evt->u.erat_error.effective_address;
  313. break;
  314. case MCE_ERROR_TYPE_TLB:
  315. if (evt->u.tlb_error.effective_address_provided)
  316. return evt->u.tlb_error.effective_address;
  317. break;
  318. default:
  319. case MCE_ERROR_TYPE_UNKNOWN:
  320. break;
  321. }
  322. return 0;
  323. }
  324. EXPORT_SYMBOL(get_mce_fault_addr);