Shutdown driver (#119)

This commit is contained in:
Alex Kotov 2022-11-28 19:02:37 +04:00 committed by GitHub
parent b8991a2d3d
commit 0df3f9cbba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 0 deletions

View File

@ -120,6 +120,7 @@ if WITH_DRIVERS
libkernaux_la_SOURCES += \
src/drivers/console.c \
src/drivers/framebuffer.c \
src/drivers/shutdown.c \
src/drivers/qemu.c
# Intel 8253-compatible programmable interval timer

View File

@ -96,6 +96,7 @@ if WITH_DRIVERS
nobase_include_HEADERS += \
kernaux/drivers/console.h \
kernaux/drivers/framebuffer.h \
kernaux/drivers/shutdown.h \
kernaux/drivers/qemu.h
# Intel 8253-compatible programmable interval timer

View File

@ -0,0 +1,24 @@
#ifndef KERNAUX_INCLUDED_DRIVERS_SHUTDOWN
#define KERNAUX_INCLUDED_DRIVERS_SHUTDOWN
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
// TODO: make this cross-platform
#define KERNAUX_DRIVERS_SHUTDOWN_INT 3
#define KERNAUX_DRIVERS_SHUTDOWN_ASM "int $3"
bool kernaux_drivers_shutdown_is_doing();
__attribute__((noreturn))
void kernaux_drivers_shutdown_halt();
void kernaux_drivers_shutdown_poweroff();
#ifdef __cplusplus
}
#endif
#endif

37
src/drivers/shutdown.c Normal file
View File

@ -0,0 +1,37 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/drivers/shutdown.h>
#include <kernaux/drivers/qemu.h>
#include <stdbool.h>
static bool is_doing = false;
bool kernaux_drivers_shutdown_is_doing()
{
return is_doing;
}
void kernaux_drivers_shutdown_halt()
{
is_doing = true;
#ifdef ASM_X86
for (;;) __asm__ __volatile__(KERNAUX_DRIVERS_SHUTDOWN_ASM);
#endif
volatile int x = 0;
for (;;) ++x;
}
void kernaux_drivers_shutdown_poweroff()
{
is_doing = true;
kernaux_drivers_qemu_poweroff();
// If we can't poweroff then we halt
kernaux_drivers_shutdown_halt();
}