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