forked from sysml/mini-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown.c
179 lines (152 loc) · 4.85 KB
/
shutdown.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* MiniOS
*
* file: fromdevice.cc
*
* NEC Europe Ltd. PROPRIETARY INFORMATION
*
* This software is supplied under the terms of a license agreement
* or nondisclosure agreement with NEC Europe Ltd. and may not be
* copied or disclosed except in accordance with the terms of that
* agreement. The software and its source code contain valuable trade
* secrets and confidential information which have to be maintained in
* confidence.
* Any unauthorized publication, transfer to third parties or duplication
* of the object or source code - either totally or in part – is
* prohibited.
*
* Copyright (c) 2014 NEC Europe Ltd. All Rights Reserved.
*
* Authors: Filipe Manco <[email protected]>
*
* NEC Europe Ltd. DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE AND THE WARRANTY AGAINST LATENT
* DEFECTS, WITH RESPECT TO THE PROGRAM AND THE ACCOMPANYING
* DOCUMENTATION.
*
* No Liability For Consequential Damages IN NO EVENT SHALL NEC Europe
* Ltd., NEC Corporation OR ANY OF ITS SUBSIDIARIES BE LIABLE FOR ANY
* DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS
* OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF INFORMATION, OR
* OTHER PECUNIARY LOSS AND INDIRECT, CONSEQUENTIAL, INCIDENTAL,
* ECONOMIC OR PUNITIVE DAMAGES) ARISING OUT OF THE USE OF OR INABILITY
* TO USE THIS PROGRAM, EVEN IF NEC Europe Ltd. HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
*/
#include <mini-os/os.h>
#include <mini-os/events.h>
#include <mini-os/kernel.h>
#include <mini-os/sched.h>
#include <mini-os/shutdown.h>
#include <mini-os/xenbus.h>
#include <mini-os/xmalloc.h>
static start_info_t *start_info_ptr;
static const char *path = "control/shutdown";
static const char *token = "control/shutdown";
static xenbus_event_queue events = NULL;
static int end_shutdown_thread = 0;
/* This should be overridden by the application we are linked against. */
__attribute__((weak)) void app_shutdown(unsigned reason)
{
printk("Shutdown requested: %d\n", reason);
}
static void shutdown_thread(void *p)
{
char *shutdown, *err;
unsigned int shutdown_reason;
xenbus_watch_path_token(XBT_NIL, path, token, &events);
for ( ;; ) {
xenbus_wait_for_watch(&events);
if ((err = xenbus_read(XBT_NIL, path, &shutdown))) {
free(err);
do_exit();
}
if (end_shutdown_thread)
break;
if (!strcmp(shutdown, "")) {
/* Avoid spurious event on xenbus */
/* FIXME: investigate the reason of the spurious event */
free(shutdown);
continue;
} else if (!strcmp(shutdown, "poweroff")) {
shutdown_reason = SHUTDOWN_poweroff;
} else if (!strcmp(shutdown, "reboot")) {
shutdown_reason = SHUTDOWN_reboot;
} else if (!strcmp(shutdown, "suspend")) {
shutdown_reason = SHUTDOWN_suspend;
} else {
shutdown_reason = SHUTDOWN_crash;
}
free(shutdown);
/* Acknowledge shutdown request */
if ((err = xenbus_write(XBT_NIL, path, ""))) {
free(err);
do_exit();
}
app_shutdown(shutdown_reason);
}
}
static void fini_shutdown(void)
{
char *err;
end_shutdown_thread = 1;
xenbus_release_wait_for_watch(&events);
err = xenbus_unwatch_path_token(XBT_NIL, path, token);
if (err) {
free(err);
do_exit();
}
}
void init_shutdown(start_info_t *si)
{
start_info_ptr = si;
end_shutdown_thread = 0;
create_thread("shutdown", shutdown_thread, NULL);
}
void kernel_shutdown(int reason)
{
char* reason_str = NULL;
switch(reason) {
case SHUTDOWN_poweroff:
reason_str = "poweroff";
break;
case SHUTDOWN_reboot:
reason_str = "reboot";
break;
case SHUTDOWN_crash:
reason_str = "crash";
break;
default:
do_exit();
break;
}
printk("MiniOS will shutdown (reason = %s) ...\n", reason_str);
fini_shutdown();
stop_kernel();
for ( ;; ) {
struct sched_shutdown sched_shutdown = { .reason = reason };
HYPERVISOR_sched_op(SCHEDOP_shutdown, &sched_shutdown);
}
}
void kernel_suspend(void)
{
int rc;
printk("MiniOS will suspend ...\n");
pre_suspend();
arch_pre_suspend();
/*
* This hypercall returns 1 if the suspend
* was cancelled and 0 if resuming in a new domain
*/
rc = HYPERVISOR_suspend(virt_to_mfn(start_info_ptr));
arch_post_suspend(rc);
post_suspend(rc);
if (rc) {
printk("MiniOS suspend canceled!");
} else {
printk("MiniOS resumed from suspend!\n");
}
}