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