forked from fail0verflow/ps4-kexec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkexec.c
168 lines (144 loc) · 5 KB
/
kexec.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
/*
* ps4-kexec - a kexec() implementation for Orbis OS / FreeBSD
*
* Copyright (C) 2015-2016 shuffle2 <[email protected]>
* Copyright (C) 2015-2016 Hector Martin "marcan" <[email protected]>
*
* This code is licensed to you under the 2-clause BSD license. See the LICENSE
* file for more information.
*/
#include "kernel.h"
#include "linux_boot.h"
#include "x86.h"
#include "kexec.h"
#include "firmware.h"
int sys_kexec(void *td, struct sys_kexec_args *uap)
{
int err = 0;
size_t initramfs_size = uap->initramfs_size;
void *image = NULL;
void *initramfs = NULL;
size_t firmware_size = 0;
struct boot_params *bp = NULL;
size_t cmd_line_maxlen = 0;
char *cmd_line = NULL;
kern.printf("sys_kexec invoked\n");
kern.printf("sys_kexec(%p, %zud, %p, %zud, \"%s\")\n", uap->image,
uap->image_size, uap->initramfs, uap->initramfs_size, uap->cmd_line);
// Look up our shutdown hook point
void *icc_query_nowait = kern.icc_query_nowait;
if (!icc_query_nowait) {
err = 2; // ENOENT
goto cleanup;
}
// Copy in kernel image
image = kernel_alloc_contig(uap->image_size);
if (!image) {
kern.printf("Failed to allocate image\n");
err = 12; // ENOMEM
goto cleanup;
}
err = kern.copyin(uap->image, image, uap->image_size);
if (err) {
kern.printf("Failed to copy in image\n");
goto cleanup;
}
// Copy in initramfs
initramfs = kernel_alloc_contig(initramfs_size + FW_CPIO_SIZE);
if (!initramfs) {
kern.printf("Failed to allocate initramfs\n");
err = 12; // ENOMEM
goto cleanup;
}
err = firmware_extract(((u8*)initramfs));
if (err < 0) {
kern.printf("Failed to extract GPU firmware - continuing anyway\n");
} else {
firmware_size = err;
}
if (initramfs_size) {
err = kern.copyin(uap->initramfs, initramfs + firmware_size, initramfs_size);
if (err) {
kern.printf("Failed to copy in initramfs\n");
goto cleanup;
}
}
initramfs_size += firmware_size;
// Copy in cmdline
cmd_line_maxlen = ((struct boot_params *)image)->hdr.cmdline_size + 1;
cmd_line = kernel_alloc_contig(cmd_line_maxlen);
if (!cmd_line) {
kern.printf("Failed to allocate cmdline\n");
err = 12;
goto cleanup;
}
err = kern.copyinstr(uap->cmd_line, cmd_line, cmd_line_maxlen, NULL);
if (err) {
kern.printf("Failed to copy in cmdline\n");
goto cleanup;
}
cmd_line[cmd_line_maxlen - 1] = 0;
kern.printf("\nkexec parameters:\n");
kern.printf(" Kernel image size: %zud bytes\n", uap->image_size);
kern.printf(" Initramfs size: %zud bytes (%zud from user)\n",
initramfs_size, uap->initramfs_size);
kern.printf(" Kernel command line: %s\n", cmd_line);
kern.printf(" Kernel image buffer: %p\n", image);
kern.printf(" Initramfs buffer: %p\n", initramfs);
// Allocate our boot params
bp = kernel_alloc_contig(sizeof(*bp));
if (!bp) {
kern.printf("Failed to allocate bp\n");
err = 12;
goto cleanup;
}
// Initialize bp
// TODO should probably do this from cpu_quiesce_gate, then bp doesn't
// need to be allocated here, just placed directly into low mem
prepare_boot_params(bp, image);
set_nix_info(image, bp, initramfs, initramfs_size, cmd_line);
// Hook the final ICC shutdown function
if (!kernel_hook_install(hook_icc_query_nowait, icc_query_nowait)) {
kern.printf("Failed to install shutdown hook\n");
err = 22;
goto cleanup;
}
kern.printf("******************************************************\n");
kern.printf("kexec successfully armed. Please shut down the system.\n");
kern.printf("******************************************************\n\n");
return 0;
cleanup:
kernel_free_contig(cmd_line, cmd_line_maxlen);
kernel_free_contig(bp, sizeof(*bp));
kernel_free_contig(image, uap->image_size);
kernel_free_contig(initramfs, uap->initramfs_size);
return err;
}
int kexec_init(void *_early_printf, sys_kexec_t *sys_kexec_ptr)
{
int rv = 0;
// potentially needed to write early_printf
u64 flags = intr_disable();
u64 wp = write_protect_disable();
if (_early_printf)
early_printf = _early_printf;
if (kernel_init() < 0) {
rv = -1;
goto cleanup;
}
kern.printf("Installing sys_kexec to system call #%d\n", SYS_KEXEC);
kernel_syscall_install(SYS_KEXEC, sys_kexec, SYS_KEXEC_NARGS);
kern.printf("kexec_init() successful\n\n");
if (sys_kexec_ptr)
*sys_kexec_ptr = sys_kexec;
cleanup:
write_protect_restore(wp);
if (kern.sched_unpin && wp & CR0_WP) {
// If we're returning to a state with WP enabled, assume the caller
// wants the thread unpinned. Else the caller is expected to
// call kern.sched_unpin() manually.
kern.sched_unpin();
}
intr_restore(flags);
return rv;
}