forked from Ps3itaTeam/ps4-kexec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kexec.c
227 lines (197 loc) · 6.65 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* ps4-kexec - a kexec() implementation for Orbis OS / FreeBSD
*
* Copyright (C) 2015-2016 shuffle2 <godisgovernment@gmail.com>
* Copyright (C) 2015-2016 Hector Martin "marcan" <marcan@marcan.st>
*
* 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"
#include "string.h"
#include "acpi.h"
static int k_copyin(const void *uaddr, void *kaddr, size_t len)
{
if (!uaddr || !kaddr)
return EFAULT;
memcpy(kaddr, uaddr, len);
return 0;
}
static int k_copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
{
const char *ustr = (const char*)uaddr;
char *kstr = (char*)kaddr;
size_t ret;
if (!uaddr || !kaddr)
return EFAULT;
ret = strlcpy(kstr, ustr, len);
if (ret >= len) {
if (done)
*done = len;
return ENAMETOOLONG;
} else {
if (done)
*done = ret + 1;
}
return 0;
}
static int k_copyout(const void *kaddr, void *uaddr, size_t len)
{
if (!uaddr || !kaddr)
return EFAULT;
memcpy(uaddr, kaddr, len);
return 0;
}
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;
int (*copyin)(const void *uaddr, void *kaddr, size_t len) = td ? kern.copyin : k_copyin;
int (*copyinstr)(const void *uaddr, void *kaddr, size_t len, size_t *done) = td ? kern.copyinstr : k_copyinstr;
int (*copyout)(const void *kaddr, void *uaddr, size_t len) = td ? kern.copyout : k_copyout;
kern.printf("sys_kexec invoked\n");
kern.printf("sys_kexec(%p, %zu, %p, %zu, \"%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 = ENOENT;
goto cleanup;
}
// Set gpu frequencies and pstate
kern.set_pstate(3);
kern.set_gpu_freq(0, 800); //800
kern.set_gpu_freq(1, 800); //674
kern.set_gpu_freq(2, 800); //610
kern.set_gpu_freq(3, 800); //800
kern.set_gpu_freq(4, 800); //800
kern.set_gpu_freq(5, 800); //720
kern.set_gpu_freq(6, 800); //720
kern.set_gpu_freq(7, 800); //720
kern.update_vddnp(0x12);
kern.set_cu_power_gate(0x12);
// Copy in kernel image
image = kernel_alloc_contig(uap->image_size);
if (!image) {
kern.printf("Failed to allocate image\n");
err = ENOMEM;
goto cleanup;
}
err = 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 = 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 = 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 = ENOMEM;
goto cleanup;
}
err = 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: %zu bytes\n", uap->image_size);
kern.printf(" Initramfs size: %zu bytes (%zu 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 = ENOMEM;
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
set_nix_info(image, bp, initramfs, initramfs_size, cmd_line, uap->vram_gb);
prepare_boot_params(bp, image);
// 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 = EINVAL;
goto cleanup;
}
kern.printf("******************************************************\n");
kern.printf("kexec successfully armed. Please shut down the system.\n");
kern.printf("******************************************************\n\n");
/*
kern.printf("\nkern_reboot(0x%x)...\n", RB_POWEROFF);
if (kern.kern_reboot(RB_POWEROFF) == -1)
kern.printf("\nkern_reboot(0x%x) failed\n", RB_POWEROFF);
*/
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;
copyout(NULL, NULL, 0);
}
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 (kernel_init(_early_printf) < 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;
}