Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 2 | /* |
| 3 | * arch/ia64/kernel/crash.c |
| 4 | * |
| 5 | * Architecture specific (ia64) functions for kexec based crash dumps. |
| 6 | * |
| 7 | * Created by: Khalid Aziz <khalid.aziz@hp.com> |
| 8 | * Copyright (C) 2005 Hewlett-Packard Development Company, L.P. |
| 9 | * Copyright (C) 2005 Intel Corp Zou Nan hai <nanhai.zou@intel.com> |
| 10 | * |
| 11 | */ |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/crash_dump.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 15 | #include <linux/memblock.h> |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 16 | #include <linux/kexec.h> |
| 17 | #include <linux/elfcore.h> |
| 18 | #include <linux/sysctl.h> |
| 19 | #include <linux/init.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 20 | #include <linux/kdebug.h> |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 21 | |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 22 | #include <asm/mca.h> |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 23 | |
| 24 | int kdump_status[NR_CPUS]; |
Simon Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 25 | static atomic_t kdump_cpu_frozen; |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 26 | atomic_t kdump_in_progress; |
Hidetoshi Seto | 1726b08 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 27 | static int kdump_freeze_monarch; |
Simon Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 28 | static int kdump_on_init = 1; |
Hidetoshi Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 29 | static int kdump_on_fatal_mca = 1; |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 30 | |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 31 | extern void ia64_dump_cpu_regs(void *); |
| 32 | |
| 33 | static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus); |
| 34 | |
| 35 | void |
Al Viro | ccbebda | 2007-02-09 16:38:10 +0000 | [diff] [blame] | 36 | crash_save_this_cpu(void) |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 37 | { |
| 38 | void *buf; |
| 39 | unsigned long cfm, sof, sol; |
| 40 | |
| 41 | int cpu = smp_processor_id(); |
| 42 | struct elf_prstatus *prstatus = &per_cpu(elf_prstatus, cpu); |
| 43 | |
| 44 | elf_greg_t *dst = (elf_greg_t *)&(prstatus->pr_reg); |
| 45 | memset(prstatus, 0, sizeof(*prstatus)); |
| 46 | prstatus->pr_pid = current->pid; |
| 47 | |
| 48 | ia64_dump_cpu_regs(dst); |
| 49 | cfm = dst[43]; |
| 50 | sol = (cfm >> 7) & 0x7f; |
| 51 | sof = cfm & 0x7f; |
| 52 | dst[46] = (unsigned long)ia64_rse_skip_regs((unsigned long *)dst[46], |
| 53 | sof - sol); |
| 54 | |
| 55 | buf = (u64 *) per_cpu_ptr(crash_notes, cpu); |
| 56 | if (!buf) |
| 57 | return; |
Simon Horman | 6672f76 | 2007-05-08 00:28:22 -0700 | [diff] [blame] | 58 | buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, prstatus, |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 59 | sizeof(*prstatus)); |
| 60 | final_note(buf); |
| 61 | } |
| 62 | |
Magnus Damm | bcb9b99 | 2007-02-05 15:43:42 +0900 | [diff] [blame] | 63 | #ifdef CONFIG_SMP |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 64 | static int |
| 65 | kdump_wait_cpu_freeze(void) |
| 66 | { |
| 67 | int cpu_num = num_online_cpus() - 1; |
| 68 | int timeout = 1000; |
| 69 | while(timeout-- > 0) { |
Simon Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 70 | if (atomic_read(&kdump_cpu_frozen) == cpu_num) |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 71 | return 0; |
| 72 | udelay(1000); |
| 73 | } |
| 74 | return 1; |
| 75 | } |
Magnus Damm | bcb9b99 | 2007-02-05 15:43:42 +0900 | [diff] [blame] | 76 | #endif |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 77 | |
| 78 | void |
| 79 | machine_crash_shutdown(struct pt_regs *pt) |
| 80 | { |
| 81 | /* This function is only called after the system |
| 82 | * has paniced or is otherwise in a critical state. |
| 83 | * The minimum amount of code to allow a kexec'd kernel |
| 84 | * to run successfully needs to happen here. |
| 85 | * |
| 86 | * In practice this means shooting down the other cpus in |
| 87 | * an SMP system. |
| 88 | */ |
| 89 | kexec_disable_iosapic(); |
| 90 | #ifdef CONFIG_SMP |
Hidetoshi Seto | 1726b08 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 91 | /* |
| 92 | * If kdump_on_init is set and an INIT is asserted here, kdump will |
| 93 | * be started again via INIT monarch. |
| 94 | */ |
| 95 | local_irq_disable(); |
| 96 | ia64_set_psr_mc(); /* mask MCA/INIT */ |
| 97 | if (atomic_inc_return(&kdump_in_progress) != 1) |
| 98 | unw_init_running(kdump_cpu_freeze, NULL); |
| 99 | |
| 100 | /* |
| 101 | * Now this cpu is ready for kdump. |
| 102 | * Stop all others by IPI or INIT. They could receive INIT from |
| 103 | * outside and might be INIT monarch, but only thing they have to |
| 104 | * do is falling into kdump_cpu_freeze(). |
| 105 | * |
| 106 | * If an INIT is asserted here: |
| 107 | * - All receivers might be slaves, since some of cpus could already |
| 108 | * be frozen and INIT might be masked on monarch. In this case, |
Hidetoshi Seto | 0cced40 | 2009-08-06 14:51:58 -0700 | [diff] [blame] | 109 | * all slaves will be frozen soon since kdump_in_progress will let |
| 110 | * them into DIE_INIT_SLAVE_LEAVE. |
Hidetoshi Seto | 1726b08 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 111 | * - One might be a monarch, but INIT rendezvous will fail since |
| 112 | * at least this cpu already have INIT masked so it never join |
| 113 | * to the rendezvous. In this case, all slaves and monarch will |
Hidetoshi Seto | 0cced40 | 2009-08-06 14:51:58 -0700 | [diff] [blame] | 114 | * be frozen soon with no wait since the INIT rendezvous is skipped |
| 115 | * by kdump_in_progress. |
Hidetoshi Seto | 1726b08 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 116 | */ |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 117 | kdump_smp_send_stop(); |
Simon Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 118 | /* not all cpu response to IPI, send INIT to freeze them */ |
Hidetoshi Seto | 5959906 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 119 | if (kdump_wait_cpu_freeze()) { |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 120 | kdump_smp_send_init(); |
Hidetoshi Seto | 5959906 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 121 | /* wait again, don't go ahead if possible */ |
| 122 | kdump_wait_cpu_freeze(); |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 123 | } |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | machine_kdump_on_init(void) |
| 129 | { |
Takao Indoh | 072f042 | 2008-04-15 05:59:54 -0400 | [diff] [blame] | 130 | crash_save_vmcoreinfo(); |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 131 | local_irq_disable(); |
| 132 | kexec_disable_iosapic(); |
| 133 | machine_kexec(ia64_kimage); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | kdump_cpu_freeze(struct unw_frame_info *info, void *arg) |
| 138 | { |
| 139 | int cpuid; |
Hidetoshi Seto | 4295ab3 | 2009-08-06 14:51:56 -0700 | [diff] [blame] | 140 | |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 141 | local_irq_disable(); |
| 142 | cpuid = smp_processor_id(); |
| 143 | crash_save_this_cpu(); |
| 144 | current->thread.ksp = (__u64)info->sw - 16; |
Hidetoshi Seto | 4295ab3 | 2009-08-06 14:51:56 -0700 | [diff] [blame] | 145 | |
| 146 | ia64_set_psr_mc(); /* mask MCA/INIT and stop reentrance */ |
| 147 | |
Simon Horman | 0ac1fac | 2007-02-14 16:15:02 +0900 | [diff] [blame] | 148 | atomic_inc(&kdump_cpu_frozen); |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 149 | kdump_status[cpuid] = 1; |
| 150 | mb(); |
Magnus Damm | bcb9b99 | 2007-02-05 15:43:42 +0900 | [diff] [blame] | 151 | for (;;) |
| 152 | cpu_relax(); |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int |
| 156 | kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data) |
| 157 | { |
| 158 | struct ia64_mca_notify_die *nd; |
| 159 | struct die_args *args = data; |
| 160 | |
Hidetoshi Seto | 5959906 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 161 | if (atomic_read(&kdump_in_progress)) { |
| 162 | switch (val) { |
| 163 | case DIE_INIT_MONARCH_LEAVE: |
| 164 | if (!kdump_freeze_monarch) |
| 165 | break; |
| 166 | /* fall through */ |
| 167 | case DIE_INIT_SLAVE_LEAVE: |
Hidetoshi Seto | 0cced40 | 2009-08-06 14:51:58 -0700 | [diff] [blame] | 168 | case DIE_INIT_MONARCH_ENTER: |
Hidetoshi Seto | 5959906 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 169 | case DIE_MCA_RENDZVOUS_LEAVE: |
| 170 | unw_init_running(kdump_cpu_freeze, NULL); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
Hidetoshi Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 175 | if (!kdump_on_init && !kdump_on_fatal_mca) |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 176 | return NOTIFY_DONE; |
| 177 | |
Kenji Kaneshige | 2010d7f | 2007-09-01 16:37:48 +0900 | [diff] [blame] | 178 | if (!ia64_kimage) { |
| 179 | if (val == DIE_INIT_MONARCH_LEAVE) |
| 180 | ia64_mca_printk(KERN_NOTICE |
| 181 | "%s: kdump not configured\n", |
Harvey Harrison | d4ed808 | 2008-03-04 15:15:00 -0800 | [diff] [blame] | 182 | __func__); |
Kenji Kaneshige | 2010d7f | 2007-09-01 16:37:48 +0900 | [diff] [blame] | 183 | return NOTIFY_DONE; |
| 184 | } |
| 185 | |
Jay Lan | 311f594 | 2007-04-03 17:53:42 -0700 | [diff] [blame] | 186 | if (val != DIE_INIT_MONARCH_LEAVE && |
Jay Lan | 311f594 | 2007-04-03 17:53:42 -0700 | [diff] [blame] | 187 | val != DIE_INIT_MONARCH_PROCESS && |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 188 | val != DIE_MCA_MONARCH_LEAVE) |
| 189 | return NOTIFY_DONE; |
| 190 | |
| 191 | nd = (struct ia64_mca_notify_die *)args->err; |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 192 | |
| 193 | switch (val) { |
Hidetoshi Seto | 3975afff | 2008-04-08 13:33:08 +0900 | [diff] [blame] | 194 | case DIE_INIT_MONARCH_PROCESS: |
Hidetoshi Seto | 5959906 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 195 | /* Reason code 1 means machine check rendezvous*/ |
| 196 | if (kdump_on_init && (nd->sos->rv_rc != 1)) { |
Hidetoshi Seto | 1726b08 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 197 | if (atomic_inc_return(&kdump_in_progress) != 1) |
| 198 | kdump_freeze_monarch = 1; |
Hidetoshi Seto | 3975afff | 2008-04-08 13:33:08 +0900 | [diff] [blame] | 199 | } |
| 200 | break; |
| 201 | case DIE_INIT_MONARCH_LEAVE: |
Hidetoshi Seto | 5959906 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 202 | /* Reason code 1 means machine check rendezvous*/ |
| 203 | if (kdump_on_init && (nd->sos->rv_rc != 1)) |
| 204 | machine_kdump_on_init(); |
Hidetoshi Seto | 3975afff | 2008-04-08 13:33:08 +0900 | [diff] [blame] | 205 | break; |
| 206 | case DIE_MCA_MONARCH_LEAVE: |
Hidetoshi Seto | 4fa2f0e | 2008-04-17 17:00:37 +0900 | [diff] [blame] | 207 | /* *(nd->data) indicate if MCA is recoverable */ |
| 208 | if (kdump_on_fatal_mca && !(*(nd->data))) { |
Hidetoshi Seto | 0cced40 | 2009-08-06 14:51:58 -0700 | [diff] [blame] | 209 | if (atomic_inc_return(&kdump_in_progress) == 1) |
Hidetoshi Seto | 1726b08 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 210 | machine_kdump_on_init(); |
Hidetoshi Seto | 1726b08 | 2009-08-06 14:51:57 -0700 | [diff] [blame] | 211 | /* We got fatal MCA while kdump!? No way!! */ |
Hidetoshi Seto | 3975afff | 2008-04-08 13:33:08 +0900 | [diff] [blame] | 212 | } |
| 213 | break; |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 214 | } |
| 215 | return NOTIFY_DONE; |
| 216 | } |
| 217 | |
| 218 | #ifdef CONFIG_SYSCTL |
Joe Perches | 2841efa | 2014-06-06 14:37:55 -0700 | [diff] [blame] | 219 | static struct ctl_table kdump_ctl_table[] = { |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 220 | { |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 221 | .procname = "kdump_on_init", |
| 222 | .data = &kdump_on_init, |
| 223 | .maxlen = sizeof(int), |
| 224 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 225 | .proc_handler = proc_dointvec, |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 226 | }, |
Hidetoshi Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 227 | { |
Hidetoshi Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 228 | .procname = "kdump_on_fatal_mca", |
| 229 | .data = &kdump_on_fatal_mca, |
| 230 | .maxlen = sizeof(int), |
| 231 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 232 | .proc_handler = proc_dointvec, |
Hidetoshi Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 233 | }, |
Eric W. Biederman | d00faf8 | 2009-04-03 05:15:37 -0700 | [diff] [blame] | 234 | { } |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 235 | }; |
| 236 | |
Joe Perches | 2841efa | 2014-06-06 14:37:55 -0700 | [diff] [blame] | 237 | static struct ctl_table sys_table[] = { |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 238 | { |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 239 | .procname = "kernel", |
| 240 | .mode = 0555, |
Hidetoshi Seto | b0247a5 | 2008-04-08 13:31:47 +0900 | [diff] [blame] | 241 | .child = kdump_ctl_table, |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 242 | }, |
Eric W. Biederman | d00faf8 | 2009-04-03 05:15:37 -0700 | [diff] [blame] | 243 | { } |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 244 | }; |
| 245 | #endif |
| 246 | |
| 247 | static int |
| 248 | machine_crash_setup(void) |
| 249 | { |
Jay Lan | 311f594 | 2007-04-03 17:53:42 -0700 | [diff] [blame] | 250 | /* be notified before default_monarch_init_process */ |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 251 | static struct notifier_block kdump_init_notifier_nb = { |
| 252 | .notifier_call = kdump_init_notifier, |
Jay Lan | 311f594 | 2007-04-03 17:53:42 -0700 | [diff] [blame] | 253 | .priority = 1, |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 254 | }; |
| 255 | int ret; |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 256 | if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0) |
| 257 | return ret; |
| 258 | #ifdef CONFIG_SYSCTL |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 259 | register_sysctl_table(sys_table); |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 260 | #endif |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | __initcall(machine_crash_setup); |
| 265 | |