[f74bbaf] | 1 | /*
|
---|
[f35749e] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[f74bbaf] | 3 | * Copyright (c) 2007 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[e88eb48] | 30 | /** @addtogroup kernel_generic
|
---|
[f74bbaf] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * @file
|
---|
[415d272] | 36 | * @brief Shutdown procedures.
|
---|
[f74bbaf] | 37 | */
|
---|
| 38 |
|
---|
| 39 | #include <arch.h>
|
---|
[f35749e] | 40 | #include <errno.h>
|
---|
[b2e121a] | 41 | #include <halt.h>
|
---|
[b2fa1204] | 42 | #include <log.h>
|
---|
[f35749e] | 43 | #include <main/main.h>
|
---|
| 44 | #include <main/shutdown.h>
|
---|
| 45 | #include <proc/task.h>
|
---|
| 46 | #include <proc/thread.h>
|
---|
| 47 |
|
---|
| 48 | static thread_t *reboot_thrd = NULL;
|
---|
| 49 | SPINLOCK_INITIALIZE(reboot_lock);
|
---|
[f74bbaf] | 50 |
|
---|
| 51 | void reboot(void)
|
---|
| 52 | {
|
---|
[f35749e] | 53 | task_done(kernel_task);
|
---|
[a35b458] | 54 |
|
---|
[f74bbaf] | 55 | #ifdef CONFIG_DEBUG
|
---|
[b2fa1204] | 56 | log(LF_OTHER, LVL_DEBUG, "Rebooting the system");
|
---|
[f74bbaf] | 57 | #endif
|
---|
[a35b458] | 58 |
|
---|
[f74bbaf] | 59 | arch_reboot();
|
---|
[415d272] | 60 | halt();
|
---|
[f74bbaf] | 61 | }
|
---|
| 62 |
|
---|
[f35749e] | 63 | /** Thread procedure for rebooting the system.
|
---|
| 64 | *
|
---|
| 65 | * @param arg Argument (unused)
|
---|
| 66 | */
|
---|
| 67 | static void reboot_thrd_proc(void *arg)
|
---|
| 68 | {
|
---|
| 69 | (void)arg;
|
---|
| 70 |
|
---|
| 71 | reboot();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /** Reboot the system.
|
---|
| 75 | *
|
---|
| 76 | * @return EOK if reboot started successfully. EBUSY if reboot already
|
---|
| 77 | * started, ENOMEM if out of memory.
|
---|
| 78 | */
|
---|
| 79 | sys_errno_t sys_reboot(void)
|
---|
| 80 | {
|
---|
| 81 | thread_t *thread;
|
---|
| 82 |
|
---|
| 83 | thread = thread_create(reboot_thrd_proc, NULL, kernel_task,
|
---|
| 84 | THREAD_FLAG_NONE, "reboot");
|
---|
| 85 | if (thread == NULL)
|
---|
| 86 | return ENOMEM;
|
---|
| 87 |
|
---|
| 88 | spinlock_lock(&reboot_lock);
|
---|
| 89 |
|
---|
| 90 | if (reboot_thrd != NULL) {
|
---|
| 91 | spinlock_unlock(&reboot_lock);
|
---|
| 92 | thread_put(thread);
|
---|
| 93 | return EBUSY;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | reboot_thrd = thread;
|
---|
| 97 |
|
---|
| 98 | spinlock_unlock(&reboot_lock);
|
---|
| 99 |
|
---|
| 100 | thread_start(thread);
|
---|
| 101 | thread_detach(thread);
|
---|
| 102 |
|
---|
| 103 | return EOK;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[f74bbaf] | 106 | /** @}
|
---|
| 107 | */
|
---|