2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2009 Free Software Foundation, Inc.
5
* GRUB is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* GRUB is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20
#include <grub/misc.h>
22
#include <grub/types.h>
24
#include <grub/term.h>
26
#include <grub/i386/relocator.h>
27
#include <grub/relocator_private.h>
29
extern grub_uint8_t grub_relocator_forward_start;
30
extern grub_uint8_t grub_relocator_forward_end;
31
extern grub_uint8_t grub_relocator_backward_start;
32
extern grub_uint8_t grub_relocator_backward_end;
34
extern void *grub_relocator_backward_dest;
35
extern void *grub_relocator_backward_src;
36
extern grub_size_t grub_relocator_backward_chunk_size;
38
extern void *grub_relocator_forward_dest;
39
extern void *grub_relocator_forward_src;
40
extern grub_size_t grub_relocator_forward_chunk_size;
42
extern grub_uint8_t grub_relocator16_start;
43
extern grub_uint8_t grub_relocator16_end;
44
extern grub_uint16_t grub_relocator16_cs;
45
extern grub_uint16_t grub_relocator16_ip;
46
extern grub_uint16_t grub_relocator16_ds;
47
extern grub_uint16_t grub_relocator16_es;
48
extern grub_uint16_t grub_relocator16_fs;
49
extern grub_uint16_t grub_relocator16_gs;
50
extern grub_uint16_t grub_relocator16_ss;
51
extern grub_uint16_t grub_relocator16_sp;
52
extern grub_uint32_t grub_relocator16_edx;
54
extern grub_uint8_t grub_relocator32_start;
55
extern grub_uint8_t grub_relocator32_end;
56
extern grub_uint32_t grub_relocator32_eax;
57
extern grub_uint32_t grub_relocator32_ebx;
58
extern grub_uint32_t grub_relocator32_ecx;
59
extern grub_uint32_t grub_relocator32_edx;
60
extern grub_uint32_t grub_relocator32_eip;
61
extern grub_uint32_t grub_relocator32_esp;
62
extern grub_uint32_t grub_relocator32_ebp;
63
extern grub_uint32_t grub_relocator32_esi;
64
extern grub_uint32_t grub_relocator32_edi;
66
extern grub_uint8_t grub_relocator64_start;
67
extern grub_uint8_t grub_relocator64_end;
68
extern grub_uint64_t grub_relocator64_rax;
69
extern grub_uint64_t grub_relocator64_rbx;
70
extern grub_uint64_t grub_relocator64_rcx;
71
extern grub_uint64_t grub_relocator64_rdx;
72
extern grub_uint64_t grub_relocator64_rip;
73
extern grub_uint64_t grub_relocator64_rip_addr;
74
extern grub_uint64_t grub_relocator64_rsp;
75
extern grub_uint64_t grub_relocator64_rsi;
76
extern grub_addr_t grub_relocator64_cr3;
78
#define RELOCATOR_SIZEOF(x) (&grub_relocator##x##_end - &grub_relocator##x##_start)
80
grub_size_t grub_relocator_align = 1;
81
grub_size_t grub_relocator_forward_size;
82
grub_size_t grub_relocator_backward_size;
84
grub_size_t grub_relocator_jumper_size = 12;
86
grub_size_t grub_relocator_jumper_size = 7;
90
grub_cpu_relocator_init (void)
92
grub_relocator_forward_size = RELOCATOR_SIZEOF(_forward);
93
grub_relocator_backward_size = RELOCATOR_SIZEOF(_backward);
97
grub_cpu_relocator_jumper (void *rels, grub_addr_t addr)
102
/* movq imm64, %rax (for relocator) */
103
*(grub_uint8_t *) ptr = 0x48;
105
*(grub_uint8_t *) ptr = 0xb8;
107
*(grub_uint64_t *) ptr = addr;
108
ptr += sizeof (grub_uint64_t);
110
/* movl imm32, %eax (for relocator) */
111
*(grub_uint8_t *) ptr = 0xb8;
113
*(grub_uint32_t *) ptr = addr;
114
ptr += sizeof (grub_uint32_t);
117
*(grub_uint8_t *) ptr = 0xff;
119
*(grub_uint8_t *) ptr = 0xe0;
124
grub_cpu_relocator_backward (void *ptr, void *src, void *dest,
127
grub_relocator_backward_dest = dest;
128
grub_relocator_backward_src = src;
129
grub_relocator_backward_chunk_size = size;
132
&grub_relocator_backward_start,
133
RELOCATOR_SIZEOF (_backward));
137
grub_cpu_relocator_forward (void *ptr, void *src, void *dest,
140
grub_relocator_forward_dest = dest;
141
grub_relocator_forward_src = src;
142
grub_relocator_forward_chunk_size = size;
145
&grub_relocator_forward_start,
146
RELOCATOR_SIZEOF (_forward));
150
grub_relocator32_boot (struct grub_relocator *rel,
151
struct grub_relocator32_state state)
155
grub_relocator_chunk_t ch;
157
err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
158
(0xffffffff - RELOCATOR_SIZEOF (32))
159
+ 1, RELOCATOR_SIZEOF (32), 16,
160
GRUB_RELOCATOR_PREFERENCE_NONE);
164
grub_relocator32_eax = state.eax;
165
grub_relocator32_ebx = state.ebx;
166
grub_relocator32_ecx = state.ecx;
167
grub_relocator32_edx = state.edx;
168
grub_relocator32_eip = state.eip;
169
grub_relocator32_esp = state.esp;
170
grub_relocator32_ebp = state.ebp;
171
grub_relocator32_esi = state.esi;
172
grub_relocator32_edi = state.edi;
174
grub_memmove (get_virtual_current_address (ch), &grub_relocator32_start,
175
RELOCATOR_SIZEOF (32));
177
err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
182
asm volatile ("cli");
183
((void (*) (void)) relst) ();
186
return GRUB_ERR_NONE;
190
grub_relocator16_boot (struct grub_relocator *rel,
191
struct grub_relocator16_state state)
195
grub_relocator_chunk_t ch;
197
err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
198
0xa0000 - RELOCATOR_SIZEOF (16),
199
RELOCATOR_SIZEOF (16), 16,
200
GRUB_RELOCATOR_PREFERENCE_NONE);
204
grub_relocator16_cs = state.cs;
205
grub_relocator16_ip = state.ip;
207
grub_relocator16_ds = state.ds;
208
grub_relocator16_es = state.es;
209
grub_relocator16_fs = state.fs;
210
grub_relocator16_gs = state.gs;
212
grub_relocator16_ss = state.ss;
213
grub_relocator16_sp = state.sp;
215
grub_relocator16_edx = state.edx;
217
grub_memmove (get_virtual_current_address (ch), &grub_relocator16_start,
218
RELOCATOR_SIZEOF (16));
220
err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
225
asm volatile ("cli");
226
((void (*) (void)) relst) ();
229
return GRUB_ERR_NONE;
233
grub_relocator64_boot (struct grub_relocator *rel,
234
struct grub_relocator64_state state,
235
grub_addr_t min_addr, grub_addr_t max_addr)
239
grub_relocator_chunk_t ch;
241
err = grub_relocator_alloc_chunk_align (rel, &ch, min_addr,
242
max_addr - RELOCATOR_SIZEOF (64),
243
RELOCATOR_SIZEOF (64), 16,
244
GRUB_RELOCATOR_PREFERENCE_NONE);
248
grub_relocator64_rax = state.rax;
249
grub_relocator64_rbx = state.rbx;
250
grub_relocator64_rcx = state.rcx;
251
grub_relocator64_rdx = state.rdx;
252
grub_relocator64_rip = state.rip;
253
grub_relocator64_rsp = state.rsp;
254
grub_relocator64_rsi = state.rsi;
255
grub_relocator64_cr3 = state.cr3;
257
grub_memmove (get_virtual_current_address (ch), &grub_relocator64_start,
258
RELOCATOR_SIZEOF (64));
260
err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
265
asm volatile ("cli");
266
((void (*) (void)) relst) ();
269
return GRUB_ERR_NONE;