x86_reboot.c
2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
* 2014 René Kijewski <rene.kijewski@fu-berlin.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @ingroup x86
* @{
*
* @file
* @brief Configurable reboot handler for x86.
*
* @author René Kijewski <rene.kijewski@fu-berlin.de>
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include "x86_interrupts.h"
#include "x86_ports.h"
#include "x86_reboot.h"
#include "periph/pm.h"
#define KBC_DATA (0x60)
#define KBC_STATUS (0x64)
#define KBC_COMMAND (0x64)
#define KBC_OUTPUT_FULL (1u << 0)
#define KBC_INPUT_FULL (1u << 1)
#define KBC_RESET ((uint8_t) ~(1u << 0))
static const struct idtr_t EMPTY_IDT = {
.limit = 0,
.base = NULL,
};
void x86_load_empty_idt(void)
{
__asm__ volatile ("lidt %0" :: "m"(EMPTY_IDT));
}
static bool fail_violently;
void NORETURN x86_kbc_reboot(void)
{
/* First load an empty IDT. Any interrupt will cause a tripple fault. */
x86_load_empty_idt();
while (1) {
if (fail_violently) {
__asm__ volatile ("int3"); /* Cause a tripple fault. Won't return. */
}
fail_violently = true;
for (unsigned i = 0; i < 0x100; ++i) {
for (unsigned j = 0; j < 0x10000; ++j) {
uint8_t c = inb(KBC_STATUS);
if (c & KBC_OUTPUT_FULL) {
(void) inb(KBC_DATA);
}
else if (!(c & KBC_OUTPUT_FULL)) {
outb(KBC_COMMAND, KBC_RESET);
break;
}
}
}
__asm__ volatile ("int3"); /* Cause a tripple fault. Won't return. */
}
}
static x86_reboot_t reboot_fun;
static bool reboot_twice;
void pm_reboot(void)
{
__asm__ volatile ("cli");
if (!reboot_twice) {
reboot_twice = true;
if (reboot_fun) {
reboot_fun();
}
}
x86_kbc_reboot();
}
void pm_off(void)
{
x86_shutdown();
}
void x86_set_reboot_fun(x86_reboot_t fun)
{
reboot_fun = fun;
}
static x86_shutdown_t shutdown_fun;
bool x86_shutdown(void)
{
if (!shutdown_fun) {
return false;
}
return shutdown_fun();
}
void x86_set_shutdown_fun(x86_shutdown_t fun)
{
shutdown_fun = fun;
}