mce.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /*
  183. * For now just print it to console.
  184. * TODO: log this error event to FSP or nvram.
  185. */
  186. while (__this_cpu_read(mce_queue_count) > 0) {
  187. index = __this_cpu_read(mce_queue_count) - 1;
  188. machine_check_print_event_info(
  189. this_cpu_ptr(&mce_event_queue[index]));
  190. __this_cpu_dec(mce_queue_count);
  191. }
  192. }
  193. void machine_check_print_event_info(struct machine_check_event *evt)
  194. {
  195. const char *level, *sevstr, *subtype;
  196. static const char *mc_ue_types[] = {
  197. "Indeterminate",
  198. "Instruction fetch",
  199. "Page table walk ifetch",
  200. "Load/Store",
  201. "Page table walk Load/Store",
  202. };
  203. static const char *mc_slb_types[] = {
  204. "Indeterminate",
  205. "Parity",
  206. "Multihit",
  207. };
  208. static const char *mc_erat_types[] = {
  209. "Indeterminate",
  210. "Parity",
  211. "Multihit",
  212. };
  213. static const char *mc_tlb_types[] = {
  214. "Indeterminate",
  215. "Parity",
  216. "Multihit",
  217. };
  218. /* Print things out */
  219. if (evt->version != MCE_V1) {
  220. pr_err("Machine Check Exception, Unknown event version %d !\n",
  221. evt->version);
  222. return;
  223. }
  224. switch (evt->severity) {
  225. case MCE_SEV_NO_ERROR:
  226. level = KERN_INFO;
  227. sevstr = "Harmless";
  228. break;
  229. case MCE_SEV_WARNING:
  230. level = KERN_WARNING;
  231. sevstr = "";
  232. break;
  233. case MCE_SEV_ERROR_SYNC:
  234. level = KERN_ERR;
  235. sevstr = "Severe";
  236. break;
  237. case MCE_SEV_FATAL:
  238. default:
  239. level = KERN_ERR;
  240. sevstr = "Fatal";
  241. break;
  242. }
  243. printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
  244. evt->disposition == MCE_DISPOSITION_RECOVERED ?
  245. "Recovered" : "[Not recovered");
  246. printk("%s Initiator: %s\n", level,
  247. evt->initiator == MCE_INITIATOR_CPU ? "CPU" : "Unknown");
  248. switch (evt->error_type) {
  249. case MCE_ERROR_TYPE_UE:
  250. subtype = evt->u.ue_error.ue_error_type <
  251. ARRAY_SIZE(mc_ue_types) ?
  252. mc_ue_types[evt->u.ue_error.ue_error_type]
  253. : "Unknown";
  254. printk("%s Error type: UE [%s]\n", level, subtype);
  255. if (evt->u.ue_error.effective_address_provided)
  256. printk("%s Effective address: %016llx\n",
  257. level, evt->u.ue_error.effective_address);
  258. if (evt->u.ue_error.physical_address_provided)
  259. printk("%s Physial address: %016llx\n",
  260. level, evt->u.ue_error.physical_address);
  261. break;
  262. case MCE_ERROR_TYPE_SLB:
  263. subtype = evt->u.slb_error.slb_error_type <
  264. ARRAY_SIZE(mc_slb_types) ?
  265. mc_slb_types[evt->u.slb_error.slb_error_type]
  266. : "Unknown";
  267. printk("%s Error type: SLB [%s]\n", level, subtype);
  268. if (evt->u.slb_error.effective_address_provided)
  269. printk("%s Effective address: %016llx\n",
  270. level, evt->u.slb_error.effective_address);
  271. break;
  272. case MCE_ERROR_TYPE_ERAT:
  273. subtype = evt->u.erat_error.erat_error_type <
  274. ARRAY_SIZE(mc_erat_types) ?
  275. mc_erat_types[evt->u.erat_error.erat_error_type]
  276. : "Unknown";
  277. printk("%s Error type: ERAT [%s]\n", level, subtype);
  278. if (evt->u.erat_error.effective_address_provided)
  279. printk("%s Effective address: %016llx\n",
  280. level, evt->u.erat_error.effective_address);
  281. break;
  282. case MCE_ERROR_TYPE_TLB:
  283. subtype = evt->u.tlb_error.tlb_error_type <
  284. ARRAY_SIZE(mc_tlb_types) ?
  285. mc_tlb_types[evt->u.tlb_error.tlb_error_type]
  286. : "Unknown";
  287. printk("%s Error type: TLB [%s]\n", level, subtype);
  288. if (evt->u.tlb_error.effective_address_provided)
  289. printk("%s Effective address: %016llx\n",
  290. level, evt->u.tlb_error.effective_address);
  291. break;
  292. default:
  293. case MCE_ERROR_TYPE_UNKNOWN:
  294. printk("%s Error type: Unknown\n", level);
  295. break;
  296. }
  297. }
  298. uint64_t get_mce_fault_addr(struct machine_check_event *evt)
  299. {
  300. switch (evt->error_type) {
  301. case MCE_ERROR_TYPE_UE:
  302. if (evt->u.ue_error.effective_address_provided)
  303. return evt->u.ue_error.effective_address;
  304. break;
  305. case MCE_ERROR_TYPE_SLB:
  306. if (evt->u.slb_error.effective_address_provided)
  307. return evt->u.slb_error.effective_address;
  308. break;
  309. case MCE_ERROR_TYPE_ERAT:
  310. if (evt->u.erat_error.effective_address_provided)
  311. return evt->u.erat_error.effective_address;
  312. break;
  313. case MCE_ERROR_TYPE_TLB:
  314. if (evt->u.tlb_error.effective_address_provided)
  315. return evt->u.tlb_error.effective_address;
  316. break;
  317. default:
  318. case MCE_ERROR_TYPE_UNKNOWN:
  319. break;
  320. }
  321. return 0;
  322. }
  323. EXPORT_SYMBOL(get_mce_fault_addr);