Changeset 0116f21 in mainline


Ignore:
Timestamp:
2019-07-18T15:30:31Z (5 years ago)
Author:
Matthieu Riolo <matthieu.riolo@…>
Children:
bb580548
Parents:
40043e8
git-author:
Matthieu Riolo <matthieu.riolo@…> (2019-07-18 15:26:49)
git-committer:
Matthieu Riolo <matthieu.riolo@…> (2019-07-18 15:30:31)
Message:

Implementing systemcall for shotdown

This commit implements a systemcall for shutdown the system.
It provides the necessary systemcall which creates a new thread
for the kernel task. The thread will then call halt(), reboot()
or cancel the pending shutdown process. The systemcall takes
three parameters: a modus (cancel, halt, reboot), a delay and
a boolean for changing into the kernel console

Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • abi/include/abi/syscall.h

    r40043e8 r0116f21  
    102102        SYS_DEBUG_CONSOLE,
    103103
     104        SYS_SHUTDOWN,
     105
    104106        SYS_KLOG,
    105107
  • kernel/generic/include/proc/thread.h

    r40043e8 r0116f21  
    4949#include <abi/proc/thread.h>
    5050#include <abi/sysinfo.h>
    51 #include <arch.h>
    5251
    5352#define THREAD              CURRENT->thread
  • kernel/generic/include/shutdown.h

    r40043e8 r0116f21  
    3737
    3838#include <atomic.h>
     39#include <typedefs.h>
     40
     41typedef struct thread thread_t;
    3942
    4043extern atomic_t haltstate;
     44extern thread_t *shutdown_thread;
    4145
    4246extern void halt(void) __attribute__((noreturn));
    4347extern void reboot(void);
    4448extern void arch_reboot(void);
     49extern sys_errno_t sys_shutdown(sysarg_t mode, sysarg_t delay, sysarg_t kconsole);
    4550
    4651#endif
  • kernel/generic/src/main/shutdown.c

    r40043e8 r0116f21  
    3737 */
    3838
     39#include <abi/shutdown.h>
    3940#include <shutdown.h>
    4041#include <log.h>
    4142#include <cpu.h>
    42 #include <arch/asm.h>
    43 #include <arch.h>
    4443#include <console/kconsole.h>
    45 #include <proc/task.h>
     44#include <console/console.h>
     45#include <proc/thread.h>
     46#include <stdlib.h>
     47
     48/* pointer to the thread for the shutdown process */
     49thread_t *shutdown_thread = NULL;
    4650
    4751/** Halt flag */
     
    8185}
    8286
     87/* Reboots the kernel */
    8388void reboot(void)
    8489{
     
    9398}
    9499
     100/* argument structure for the shutdown thread */
     101typedef struct {
     102        sysarg_t mode;
     103        sysarg_t delay;
     104} sys_shutdown_arg_t;
     105
     106/* function for the shutdown thread */
     107static void sys_shutdown_function(void *arguments)
     108{
     109        sys_shutdown_arg_t *arg = (sys_shutdown_arg_t *)arguments;
     110
     111        if (arg->delay != 0) {
     112                thread_sleep(arg->delay);
     113        }
     114
     115        if (thread_interrupted(THREAD)) {
     116                free(arguments);
     117                return;
     118        }
     119
     120        if (arg->mode == SHUTDOWN_REBOOT) {
     121                reboot();
     122        } else {
     123                halt();
     124        }
     125}
     126
     127/* system call handler for shutdown */
     128sys_errno_t sys_shutdown(sysarg_t mode, sysarg_t delay, sysarg_t kconsole)
     129{
     130
     131#if (defined(CONFIG_DEBUG)) && (defined(CONFIG_KCONSOLE))
     132        if (kconsole) {
     133                grab_console();
     134        }
     135#endif
     136
     137        irq_spinlock_lock(&threads_lock, true);
     138        thread_t *thread = atomic_load(&shutdown_thread);
     139        if (thread != NULL) {
     140                thread_interrupt(thread);
     141                atomic_store(&shutdown_thread, NULL);
     142        }
     143        irq_spinlock_unlock(&threads_lock, true);
     144
     145        /* `cancel` or default has been called */
     146        if (mode != SHUTDOWN_HALT && mode != SHUTDOWN_REBOOT) {
     147                return EOK;
     148        }
     149
     150        sys_shutdown_arg_t *arg = malloc(sizeof(sys_shutdown_arg_t));
     151        if (arg == NULL) {
     152                return ENOMEM;
     153        }
     154
     155        //TODO: find a better way for accessing the kernel task
     156        irq_spinlock_lock(&tasks_lock, true);
     157        task_t *kernel_task = task_find_by_id(1);
     158        irq_spinlock_unlock(&tasks_lock, true);
     159
     160        if (kernel_task == NULL) {
     161                goto error;
     162        }
     163
     164        arg->mode = mode;
     165        arg->delay = delay;
     166
     167        thread = thread_create(sys_shutdown_function, arg, kernel_task, THREAD_FLAG_NONE, "shutdown");
     168
     169        if (thread == NULL) {
     170                goto error;
     171        }
     172
     173        thread_ready(thread);
     174        atomic_store(&shutdown_thread, thread);
     175        return EOK;
     176
     177error:
     178        free(arg);
     179        return ENOENT;
     180}
     181
    95182/** @}
    96183 */
  • kernel/generic/src/syscall/syscall.c

    r40043e8 r0116f21  
    192192        [SYS_SYSINFO_GET_DATA] = (syshandler_t) sys_sysinfo_get_data,
    193193
     194        [SYS_SHUTDOWN] = (syshandler_t) sys_shutdown,
     195
    194196        /* Kernel console syscalls. */
    195197        [SYS_DEBUG_CONSOLE] = (syshandler_t) sys_debug_console,
Note: See TracChangeset for help on using the changeset viewer.