blob: a9b037b72f021d0f424eb6db97e4ba1aa6d981a1 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/kernel/reboot.c
4 *
5 * Copyright (C) 2013 Linus Torvalds
6 */
7
8#define pr_fmt(fmt) "reboot: " fmt
9
10#include <linux/ctype.h>
11#include <linux/export.h>
12#include <linux/kexec.h>
13#include <linux/kmod.h>
14#include <linux/kmsg_dump.h>
15#include <linux/reboot.h>
16#include <linux/suspend.h>
17#include <linux/syscalls.h>
18#include <linux/syscore_ops.h>
19#include <linux/uaccess.h>
20
21#ifdef CONFIG_PXA_RAMDUMP
22#include <soc/asr/ramdump.h>
23#endif
24
25#include <soc/asr/asr_mflag.h>
26
27/*
28 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
29 */
30
31int C_A_D = 1;
32struct pid *cad_pid;
33EXPORT_SYMBOL(cad_pid);
34
35#if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32)
36#define DEFAULT_REBOOT_MODE = REBOOT_HARD
37#else
38#define DEFAULT_REBOOT_MODE
39#endif
40enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
41EXPORT_SYMBOL_GPL(reboot_mode);
42enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
43EXPORT_SYMBOL_GPL(panic_reboot_mode);
44
45/*
46 * This variable is used privately to keep track of whether or not
47 * reboot_type is still set to its default value (i.e., reboot= hasn't
48 * been set on the command line). This is needed so that we can
49 * suppress DMI scanning for reboot quirks. Without it, it's
50 * impossible to override a faulty reboot quirk without recompiling.
51 */
52int reboot_default = 1;
53int reboot_cpu;
54enum reboot_type reboot_type = BOOT_ACPI;
55int reboot_force;
56
57/*
58 * If set, this is used for preparing the system to power off.
59 */
60
61void (*pm_power_off_prepare)(void);
62EXPORT_SYMBOL_GPL(pm_power_off_prepare);
63
64/**
65 * emergency_restart - reboot the system
66 *
67 * Without shutting down any hardware or taking any locks
68 * reboot the system. This is called when we know we are in
69 * trouble so this is our best effort to reboot. This is
70 * safe to call in interrupt context.
71 */
72void emergency_restart(void)
73{
74 kmsg_dump(KMSG_DUMP_EMERG);
75 system_state = SYSTEM_RESTART;
76 machine_emergency_restart();
77}
78EXPORT_SYMBOL_GPL(emergency_restart);
79
80void kernel_restart_prepare(char *cmd)
81{
82 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
83 system_state = SYSTEM_RESTART;
84 usermodehelper_disable();
85 device_shutdown();
86#ifdef CONFIG_PXA_RAMDUMP
87 ramdump_rdc_reset(); /* Clean RAMDUMP request on gracefull command */
88#endif
89}
90
91/**
92 * register_reboot_notifier - Register function to be called at reboot time
93 * @nb: Info about notifier function to be called
94 *
95 * Registers a function with the list of functions
96 * to be called at reboot time.
97 *
98 * Currently always returns zero, as blocking_notifier_chain_register()
99 * always returns zero.
100 */
101int register_reboot_notifier(struct notifier_block *nb)
102{
103 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
104}
105EXPORT_SYMBOL(register_reboot_notifier);
106
107/**
108 * unregister_reboot_notifier - Unregister previously registered reboot notifier
109 * @nb: Hook to be unregistered
110 *
111 * Unregisters a previously registered reboot
112 * notifier function.
113 *
114 * Returns zero on success, or %-ENOENT on failure.
115 */
116int unregister_reboot_notifier(struct notifier_block *nb)
117{
118 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
119}
120EXPORT_SYMBOL(unregister_reboot_notifier);
121
122static void devm_unregister_reboot_notifier(struct device *dev, void *res)
123{
124 WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
125}
126
127int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
128{
129 struct notifier_block **rcnb;
130 int ret;
131
132 rcnb = devres_alloc(devm_unregister_reboot_notifier,
133 sizeof(*rcnb), GFP_KERNEL);
134 if (!rcnb)
135 return -ENOMEM;
136
137 ret = register_reboot_notifier(nb);
138 if (!ret) {
139 *rcnb = nb;
140 devres_add(dev, rcnb);
141 } else {
142 devres_free(rcnb);
143 }
144
145 return ret;
146}
147EXPORT_SYMBOL(devm_register_reboot_notifier);
148
149/*
150 * Notifier list for kernel code which wants to be called
151 * to restart the system.
152 */
153static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
154
155/**
156 * register_restart_handler - Register function to be called to reset
157 * the system
158 * @nb: Info about handler function to be called
159 * @nb->priority: Handler priority. Handlers should follow the
160 * following guidelines for setting priorities.
161 * 0: Restart handler of last resort,
162 * with limited restart capabilities
163 * 128: Default restart handler; use if no other
164 * restart handler is expected to be available,
165 * and/or if restart functionality is
166 * sufficient to restart the entire system
167 * 255: Highest priority restart handler, will
168 * preempt all other restart handlers
169 *
170 * Registers a function with code to be called to restart the
171 * system.
172 *
173 * Registered functions will be called from machine_restart as last
174 * step of the restart sequence (if the architecture specific
175 * machine_restart function calls do_kernel_restart - see below
176 * for details).
177 * Registered functions are expected to restart the system immediately.
178 * If more than one function is registered, the restart handler priority
179 * selects which function will be called first.
180 *
181 * Restart handlers are expected to be registered from non-architecture
182 * code, typically from drivers. A typical use case would be a system
183 * where restart functionality is provided through a watchdog. Multiple
184 * restart handlers may exist; for example, one restart handler might
185 * restart the entire system, while another only restarts the CPU.
186 * In such cases, the restart handler which only restarts part of the
187 * hardware is expected to register with low priority to ensure that
188 * it only runs if no other means to restart the system is available.
189 *
190 * Currently always returns zero, as atomic_notifier_chain_register()
191 * always returns zero.
192 */
193int register_restart_handler(struct notifier_block *nb)
194{
195 return atomic_notifier_chain_register(&restart_handler_list, nb);
196}
197EXPORT_SYMBOL(register_restart_handler);
198
199/**
200 * unregister_restart_handler - Unregister previously registered
201 * restart handler
202 * @nb: Hook to be unregistered
203 *
204 * Unregisters a previously registered restart handler function.
205 *
206 * Returns zero on success, or %-ENOENT on failure.
207 */
208int unregister_restart_handler(struct notifier_block *nb)
209{
210 return atomic_notifier_chain_unregister(&restart_handler_list, nb);
211}
212EXPORT_SYMBOL(unregister_restart_handler);
213
214/**
215 * do_kernel_restart - Execute kernel restart handler call chain
216 *
217 * Calls functions registered with register_restart_handler.
218 *
219 * Expected to be called from machine_restart as last step of the restart
220 * sequence.
221 *
222 * Restarts the system immediately if a restart handler function has been
223 * registered. Otherwise does nothing.
224 */
225void do_kernel_restart(char *cmd)
226{
227 atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
228}
229
230void migrate_to_reboot_cpu(void)
231{
232 /* The boot cpu is always logical cpu 0 */
233 int cpu = reboot_cpu;
234
235 cpu_hotplug_disable();
236
237 /* Make certain the cpu I'm about to reboot on is online */
238 if (!cpu_online(cpu))
239 cpu = cpumask_first(cpu_online_mask);
240
241 /* Prevent races with other tasks migrating this task */
242 current->flags |= PF_NO_SETAFFINITY;
243
244 /* Make certain I only run on the appropriate processor */
245 set_cpus_allowed_ptr(current, cpumask_of(cpu));
246}
247
248/**
249 * kernel_restart - reboot the system
250 * @cmd: pointer to buffer containing command to execute for restart
251 * or %NULL
252 *
253 * Shutdown everything and perform a clean reboot.
254 * This is not safe to call in interrupt context.
255 */
256void kernel_restart(char *cmd)
257{
258 kernel_restart_prepare(cmd);
259 migrate_to_reboot_cpu();
260 syscore_shutdown();
261 if (!cmd)
262 pr_emerg("Restarting system by: %s\n", current->comm);
263 else
264 pr_emerg("Restarting system with command '%s': %s\n", cmd, current->comm);
265 kmsg_dump(KMSG_DUMP_SHUTDOWN);
266 machine_restart(cmd);
267}
268EXPORT_SYMBOL_GPL(kernel_restart);
269
270static void kernel_shutdown_prepare(enum system_states state)
271{
272 blocking_notifier_call_chain(&reboot_notifier_list,
273 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
274 system_state = state;
275 usermodehelper_disable();
276
277#ifdef CONFIG_PXA_RAMDUMP
278 ramdump_rdc_reset(); /* Clean RAMDUMP request on gracefull command */
279#endif
280 device_shutdown();
281}
282/**
283 * kernel_halt - halt the system
284 *
285 * Shutdown everything and perform a clean system halt.
286 */
287void kernel_halt(void)
288{
289 kernel_shutdown_prepare(SYSTEM_HALT);
290 migrate_to_reboot_cpu();
291 syscore_shutdown();
292 pr_emerg("System halted\n");
293 kmsg_dump(KMSG_DUMP_SHUTDOWN);
294 machine_halt();
295}
296EXPORT_SYMBOL_GPL(kernel_halt);
297
298/**
299 * kernel_power_off - power_off the system
300 *
301 * Shutdown everything and perform a clean system power_off.
302 */
303void kernel_power_off(void)
304{
305 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
306 if (pm_power_off_prepare)
307 pm_power_off_prepare();
308 migrate_to_reboot_cpu();
309 syscore_shutdown();
310 pr_emerg("Power down by: %s\n", current->comm);
311 kmsg_dump(KMSG_DUMP_SHUTDOWN);
312 machine_power_off();
313}
314EXPORT_SYMBOL_GPL(kernel_power_off);
315
316DEFINE_MUTEX(system_transition_mutex);
317
318/*
319 * Reboot system call: for obvious reasons only root may call it,
320 * and even root needs to set up some magic numbers in the registers
321 * so that some mistake won't make this reboot the whole machine.
322 * You can also set the meaning of the ctrl-alt-del-key here.
323 *
324 * reboot doesn't sync: do that yourself before calling this.
325 */
326SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
327 void __user *, arg)
328{
329 struct pid_namespace *pid_ns = task_active_pid_ns(current);
330 char buffer[256];
331 int ret = 0;
332 struct asr_mflag *asr_flag = get_asr_mflag();
333
334 /* We only trust the superuser with rebooting the system. */
335 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
336 return -EPERM;
337
338 /* For safety, we require "magic" arguments. */
339 if (magic1 != LINUX_REBOOT_MAGIC1 ||
340 (magic2 != LINUX_REBOOT_MAGIC2 &&
341 magic2 != LINUX_REBOOT_MAGIC2A &&
342 magic2 != LINUX_REBOOT_MAGIC2B &&
343 magic2 != LINUX_REBOOT_MAGIC2C))
344 return -EINVAL;
345
346 /*
347 * If pid namespaces are enabled and the current task is in a child
348 * pid_namespace, the command is handled by reboot_pid_ns() which will
349 * call do_exit().
350 */
351 ret = reboot_pid_ns(pid_ns, cmd);
352 if (ret)
353 return ret;
354
355 /* Instead of trying to make the power_off code look like
356 * halt when pm_power_off is not set do it the easy way.
357 */
358 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
359 cmd = LINUX_REBOOT_CMD_HALT;
360
361 mutex_lock(&system_transition_mutex);
362 switch (cmd) {
363 case LINUX_REBOOT_CMD_RESTART:
364 kernel_restart(NULL);
365 break;
366
367 case LINUX_REBOOT_CMD_CAD_ON:
368 C_A_D = 1;
369 break;
370
371 case LINUX_REBOOT_CMD_CAD_OFF:
372 C_A_D = 0;
373 break;
374
375 case LINUX_REBOOT_CMD_HALT:
376 kernel_halt();
377 do_exit(0);
378 panic("cannot halt");
379
380 case LINUX_REBOOT_CMD_POWER_OFF:
381 kernel_power_off();
382 do_exit(0);
383 break;
384
385 case LINUX_REBOOT_CMD_RESTART2:
386 ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
387 if (ret < 0) {
388 ret = -EFAULT;
389 break;
390 }
391 buffer[sizeof(buffer) - 1] = '\0';
392
393 if(strcmp(buffer,"fastboot") == 0)
394 asr_flag->fastboot_flag = 0x46415354;
395
396 kernel_restart(buffer);
397 break;
398
399#ifdef CONFIG_KEXEC_CORE
400 case LINUX_REBOOT_CMD_KEXEC:
401 ret = kernel_kexec();
402 break;
403#endif
404
405#ifdef CONFIG_HIBERNATION
406 case LINUX_REBOOT_CMD_SW_SUSPEND:
407 ret = hibernate();
408 break;
409#endif
410
411 default:
412 ret = -EINVAL;
413 break;
414 }
415 mutex_unlock(&system_transition_mutex);
416 return ret;
417}
418
419static void deferred_cad(struct work_struct *dummy)
420{
421 kernel_restart(NULL);
422}
423
424/*
425 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
426 * As it's called within an interrupt, it may NOT sync: the only choice
427 * is whether to reboot at once, or just ignore the ctrl-alt-del.
428 */
429void ctrl_alt_del(void)
430{
431 static DECLARE_WORK(cad_work, deferred_cad);
432
433 if (C_A_D)
434 schedule_work(&cad_work);
435 else
436 kill_cad_pid(SIGINT, 1);
437}
438
439char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
440static const char reboot_cmd[] = "/sbin/reboot";
441
442static int run_cmd(const char *cmd)
443{
444 char **argv;
445 static char *envp[] = {
446 "HOME=/",
447 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
448 NULL
449 };
450 int ret;
451 argv = argv_split(GFP_KERNEL, cmd, NULL);
452 if (argv) {
453 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
454 argv_free(argv);
455 } else {
456 ret = -ENOMEM;
457 }
458
459 return ret;
460}
461
462static int __orderly_reboot(void)
463{
464 int ret;
465
466 ret = run_cmd(reboot_cmd);
467
468 if (ret) {
469 pr_warn("Failed to start orderly reboot: forcing the issue\n");
470 emergency_sync();
471 kernel_restart(NULL);
472 }
473
474 return ret;
475}
476
477static int __orderly_poweroff(bool force)
478{
479 int ret;
480
481 ret = run_cmd(poweroff_cmd);
482
483 if (ret && force) {
484 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
485
486 /*
487 * I guess this should try to kick off some daemon to sync and
488 * poweroff asap. Or not even bother syncing if we're doing an
489 * emergency shutdown?
490 */
491 emergency_sync();
492 kernel_power_off();
493 }
494
495 return ret;
496}
497
498static bool poweroff_force;
499
500static void poweroff_work_func(struct work_struct *work)
501{
502 __orderly_poweroff(poweroff_force);
503}
504
505static DECLARE_WORK(poweroff_work, poweroff_work_func);
506
507/**
508 * orderly_poweroff - Trigger an orderly system poweroff
509 * @force: force poweroff if command execution fails
510 *
511 * This may be called from any context to trigger a system shutdown.
512 * If the orderly shutdown fails, it will force an immediate shutdown.
513 */
514void orderly_poweroff(bool force)
515{
516 if (force) /* do not override the pending "true" */
517 poweroff_force = true;
518 schedule_work(&poweroff_work);
519}
520EXPORT_SYMBOL_GPL(orderly_poweroff);
521
522static void reboot_work_func(struct work_struct *work)
523{
524 __orderly_reboot();
525}
526
527static DECLARE_WORK(reboot_work, reboot_work_func);
528
529/**
530 * orderly_reboot - Trigger an orderly system reboot
531 *
532 * This may be called from any context to trigger a system reboot.
533 * If the orderly reboot fails, it will force an immediate reboot.
534 */
535void orderly_reboot(void)
536{
537 schedule_work(&reboot_work);
538}
539EXPORT_SYMBOL_GPL(orderly_reboot);
540
541static int __init reboot_setup(char *str)
542{
543 for (;;) {
544 enum reboot_mode *mode;
545
546 /*
547 * Having anything passed on the command line via
548 * reboot= will cause us to disable DMI checking
549 * below.
550 */
551 reboot_default = 0;
552
553 if (!strncmp(str, "panic_", 6)) {
554 mode = &panic_reboot_mode;
555 str += 6;
556 } else {
557 mode = &reboot_mode;
558 }
559
560 switch (*str) {
561 case 'w':
562 *mode = REBOOT_WARM;
563 break;
564
565 case 'c':
566 *mode = REBOOT_COLD;
567 break;
568
569 case 'h':
570 *mode = REBOOT_HARD;
571 break;
572
573 case 's':
574 if (isdigit(*(str+1)))
575 reboot_cpu = simple_strtoul(str+1, NULL, 0);
576 else if (str[1] == 'm' && str[2] == 'p' &&
577 isdigit(*(str+3)))
578 reboot_cpu = simple_strtoul(str+3, NULL, 0);
579 else
580 *mode = REBOOT_SOFT;
581 if (reboot_cpu >= num_possible_cpus()) {
582 pr_err("Ignoring the CPU number in reboot= option. "
583 "CPU %d exceeds possible cpu number %d\n",
584 reboot_cpu, num_possible_cpus());
585 reboot_cpu = 0;
586 break;
587 }
588 break;
589
590 case 'g':
591 *mode = REBOOT_GPIO;
592 break;
593
594 case 'b':
595 case 'a':
596 case 'k':
597 case 't':
598 case 'e':
599 case 'p':
600 reboot_type = *str;
601 break;
602
603 case 'f':
604 reboot_force = 1;
605 break;
606 }
607
608 str = strchr(str, ',');
609 if (str)
610 str++;
611 else
612 break;
613 }
614 return 1;
615}
616__setup("reboot=", reboot_setup);