blob: 42af83a5a1235eef6577ed0ba049a9f08dcb02a0 (
plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <LibC/sys/arch/i386/regs.h>
#include <Kernel/Arch/x86/ASM_wrapper.h>
#include <Kernel/Arch/x86/CPU.h>
#include <AK/Platform.h>
VALIDATE_IS_X86()
namespace Kernel {
struct [[gnu::packed]] RegisterState {
#if ARCH(I386)
FlatPtr ss;
FlatPtr gs;
FlatPtr fs;
FlatPtr es;
FlatPtr ds;
FlatPtr edi;
FlatPtr esi;
FlatPtr ebp;
FlatPtr esp;
FlatPtr ebx;
FlatPtr edx;
FlatPtr ecx;
FlatPtr eax;
#else
FlatPtr rdi;
FlatPtr rsi;
FlatPtr rbp;
FlatPtr rsp;
FlatPtr rbx;
FlatPtr rdx;
FlatPtr rcx;
FlatPtr rax;
FlatPtr r8;
FlatPtr r9;
FlatPtr r10;
FlatPtr r11;
FlatPtr r12;
FlatPtr r13;
FlatPtr r14;
FlatPtr r15;
#endif
u16 exception_code;
u16 isr_number;
#if ARCH(X86_64)
u32 padding;
#endif
#if ARCH(I386)
FlatPtr eip;
#else
FlatPtr rip;
#endif
FlatPtr cs;
#if ARCH(I386)
FlatPtr eflags;
FlatPtr userspace_esp;
FlatPtr userspace_ss;
#else
FlatPtr rflags;
FlatPtr userspace_rsp;
FlatPtr userspace_ss;
#endif
#if ARCH(I386)
FlatPtr userspace_sp() const
{
return userspace_esp;
}
void set_userspace_sp(FlatPtr value) { userspace_esp = value; }
FlatPtr ip() const { return eip; }
void set_ip(FlatPtr value) { eip = value; }
void set_dx(FlatPtr value) { edx = value; }
FlatPtr bp() const { return ebp; }
void set_bp(FlatPtr value) { ebp = value; }
FlatPtr flags() const { return eflags; }
void set_flags(FlatPtr value) { eflags = value; }
void set_return_reg(FlatPtr value) { eax = value; }
#elif ARCH(X86_64)
FlatPtr userspace_sp() const
{
return userspace_rsp;
}
void set_userspace_sp(FlatPtr value) { userspace_rsp = value; }
FlatPtr ip() const { return rip; }
void set_ip(FlatPtr value) { rip = value; }
void set_dx(FlatPtr value) { rdx = value; }
FlatPtr bp() const { return rbp; }
void set_bp(FlatPtr value) { rbp = value; }
FlatPtr flags() const { return rflags; }
void set_flags(FlatPtr value) { rflags = value; }
void set_return_reg(FlatPtr value) { rax = value; }
#endif
void capture_syscall_params(FlatPtr& function, FlatPtr& arg1, FlatPtr& arg2, FlatPtr& arg3, FlatPtr& arg4) const
{
#if ARCH(I386)
function = eax;
arg1 = edx;
arg2 = ecx;
arg3 = ebx;
arg4 = esi;
#else
// The syscall instruction clobbers rcx, so we must use a different calling convention to 32-bit.
function = rax;
arg1 = rdx;
arg2 = rdi;
arg3 = rbx;
arg4 = rsi;
#endif
}
};
#if ARCH(I386)
# define REGISTER_STATE_SIZE (19 * 4)
static_assert(AssertSize<RegisterState, REGISTER_STATE_SIZE>());
#elif ARCH(X86_64)
# define REGISTER_STATE_SIZE (22 * 8)
static_assert(AssertSize<RegisterState, REGISTER_STATE_SIZE>());
#endif
inline void copy_kernel_registers_into_ptrace_registers(PtraceRegisters& ptrace_regs, RegisterState const& kernel_regs)
{
#if ARCH(I386)
ptrace_regs.eax = kernel_regs.eax;
ptrace_regs.ecx = kernel_regs.ecx;
ptrace_regs.edx = kernel_regs.edx;
ptrace_regs.ebx = kernel_regs.ebx;
ptrace_regs.esp = kernel_regs.userspace_esp;
ptrace_regs.ebp = kernel_regs.ebp;
ptrace_regs.esi = kernel_regs.esi;
ptrace_regs.edi = kernel_regs.edi;
ptrace_regs.eip = kernel_regs.eip;
ptrace_regs.eflags = kernel_regs.eflags;
#else
ptrace_regs.rax = kernel_regs.rax;
ptrace_regs.rcx = kernel_regs.rcx;
ptrace_regs.rdx = kernel_regs.rdx;
ptrace_regs.rbx = kernel_regs.rbx;
ptrace_regs.rsp = kernel_regs.userspace_rsp;
ptrace_regs.rbp = kernel_regs.rbp;
ptrace_regs.rsi = kernel_regs.rsi;
ptrace_regs.rdi = kernel_regs.rdi;
ptrace_regs.rip = kernel_regs.rip;
ptrace_regs.r8 = kernel_regs.r8;
ptrace_regs.r9 = kernel_regs.r9;
ptrace_regs.r10 = kernel_regs.r10;
ptrace_regs.r11 = kernel_regs.r11;
ptrace_regs.r12 = kernel_regs.r12;
ptrace_regs.r13 = kernel_regs.r13;
ptrace_regs.r14 = kernel_regs.r14;
ptrace_regs.r15 = kernel_regs.r15;
ptrace_regs.rflags = kernel_regs.rflags,
#endif
ptrace_regs.cs = 0;
ptrace_regs.ss = 0;
ptrace_regs.ds = 0;
ptrace_regs.es = 0;
ptrace_regs.fs = 0;
ptrace_regs.gs = 0;
}
inline void copy_ptrace_registers_into_kernel_registers(RegisterState& kernel_regs, PtraceRegisters const& ptrace_regs)
{
#if ARCH(I386)
kernel_regs.eax = ptrace_regs.eax;
kernel_regs.ecx = ptrace_regs.ecx;
kernel_regs.edx = ptrace_regs.edx;
kernel_regs.ebx = ptrace_regs.ebx;
kernel_regs.esp = ptrace_regs.esp;
kernel_regs.ebp = ptrace_regs.ebp;
kernel_regs.esi = ptrace_regs.esi;
kernel_regs.edi = ptrace_regs.edi;
kernel_regs.eip = ptrace_regs.eip;
kernel_regs.eflags = (kernel_regs.eflags & ~safe_eflags_mask) | (ptrace_regs.eflags & safe_eflags_mask);
#else
kernel_regs.rax = ptrace_regs.rax;
kernel_regs.rcx = ptrace_regs.rcx;
kernel_regs.rdx = ptrace_regs.rdx;
kernel_regs.rbx = ptrace_regs.rbx;
kernel_regs.rsp = ptrace_regs.rsp;
kernel_regs.rbp = ptrace_regs.rbp;
kernel_regs.rsi = ptrace_regs.rsi;
kernel_regs.rdi = ptrace_regs.rdi;
kernel_regs.rip = ptrace_regs.rip;
kernel_regs.r8 = ptrace_regs.r8;
kernel_regs.r9 = ptrace_regs.r9;
kernel_regs.r10 = ptrace_regs.r10;
kernel_regs.r11 = ptrace_regs.r11;
kernel_regs.r12 = ptrace_regs.r12;
kernel_regs.r13 = ptrace_regs.r13;
kernel_regs.r14 = ptrace_regs.r14;
kernel_regs.r15 = ptrace_regs.r15;
// FIXME: do we need a separate safe_rflags_mask here?
kernel_regs.rflags = (kernel_regs.rflags & ~safe_eflags_mask) | (ptrace_regs.rflags & safe_eflags_mask);
#endif
}
struct [[gnu::packed]] DebugRegisterState {
FlatPtr dr0;
FlatPtr dr1;
FlatPtr dr2;
FlatPtr dr3;
FlatPtr dr6;
FlatPtr dr7;
};
inline void read_debug_registers_into(DebugRegisterState& state)
{
state.dr0 = read_dr0();
state.dr1 = read_dr1();
state.dr2 = read_dr2();
state.dr3 = read_dr3();
state.dr6 = read_dr6();
state.dr7 = read_dr7();
}
inline void write_debug_registers_from(DebugRegisterState const& state)
{
write_dr0(state.dr0);
write_dr1(state.dr1);
write_dr2(state.dr2);
write_dr3(state.dr3);
write_dr6(state.dr6);
write_dr7(state.dr7);
}
inline void clear_debug_registers()
{
write_dr0(0);
write_dr1(0);
write_dr2(0);
write_dr3(0);
write_dr7(1 << 10); // Bit 10 is reserved and must be set to 1.
}
}
|