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 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// SPDX-License-Identifier: GPL-2.0
#define __FRAME_OFFSETS
#include <linux/errno.h>
#include <linux/string.h>
#include <sys/ucontext.h>
#include <asm/ptrace.h>
#include <asm/sigcontext.h>
#include <sysdep/ptrace.h>
#include <sysdep/mcontext.h>
#include <arch.h>
void get_regs_from_mc(struct uml_pt_regs *regs, mcontext_t *mc)
{
#ifdef __i386__
#define COPY2(X,Y) regs->gp[X] = mc->gregs[REG_##Y]
#define COPY(X) regs->gp[X] = mc->gregs[REG_##X]
#define COPY_SEG(X) regs->gp[X] = mc->gregs[REG_##X] & 0xffff;
#define COPY_SEG_CPL3(X) regs->gp[X] = (mc->gregs[REG_##X] & 0xffff) | 3;
COPY_SEG(GS); COPY_SEG(FS); COPY_SEG(ES); COPY_SEG(DS);
COPY(EDI); COPY(ESI); COPY(EBP);
COPY2(UESP, ESP); /* sic */
COPY(EBX); COPY(EDX); COPY(ECX); COPY(EAX);
COPY(EIP); COPY_SEG_CPL3(CS); COPY(EFL); COPY_SEG_CPL3(SS);
#undef COPY2
#undef COPY
#undef COPY_SEG
#undef COPY_SEG_CPL3
#else
#define COPY2(X,Y) regs->gp[X/sizeof(unsigned long)] = mc->gregs[REG_##Y]
#define COPY(X) regs->gp[X/sizeof(unsigned long)] = mc->gregs[REG_##X]
COPY(R8); COPY(R9); COPY(R10); COPY(R11);
COPY(R12); COPY(R13); COPY(R14); COPY(R15);
COPY(RDI); COPY(RSI); COPY(RBP); COPY(RBX);
COPY(RDX); COPY(RAX); COPY(RCX); COPY(RSP);
COPY(RIP);
COPY2(EFLAGS, EFL);
COPY2(CS, CSGSFS);
regs->gp[SS / sizeof(unsigned long)] = mc->gregs[REG_CSGSFS] >> 48;
#undef COPY2
#undef COPY
#endif
}
void mc_set_rip(void *_mc, void *target)
{
mcontext_t *mc = _mc;
#ifdef __i386__
mc->gregs[REG_EIP] = (unsigned long)target;
#else
mc->gregs[REG_RIP] = (unsigned long)target;
#endif
}
/* Same thing, but the copy macros are turned around. */
void get_mc_from_regs(struct uml_pt_regs *regs, mcontext_t *mc, int single_stepping)
{
#ifdef __i386__
#define COPY2(X,Y) mc->gregs[REG_##Y] = regs->gp[X]
#define COPY(X) mc->gregs[REG_##X] = regs->gp[X]
#define COPY_SEG(X) mc->gregs[REG_##X] = regs->gp[X] & 0xffff;
#define COPY_SEG_CPL3(X) mc->gregs[REG_##X] = (regs->gp[X] & 0xffff) | 3;
COPY_SEG(GS); COPY_SEG(FS); COPY_SEG(ES); COPY_SEG(DS);
COPY(EDI); COPY(ESI); COPY(EBP);
COPY2(UESP, ESP); /* sic */
COPY(EBX); COPY(EDX); COPY(ECX); COPY(EAX);
COPY(EIP); COPY_SEG_CPL3(CS); COPY(EFL); COPY_SEG_CPL3(SS);
#else
#define COPY2(X,Y) mc->gregs[REG_##Y] = regs->gp[X/sizeof(unsigned long)]
#define COPY(X) mc->gregs[REG_##X] = regs->gp[X/sizeof(unsigned long)]
COPY(R8); COPY(R9); COPY(R10); COPY(R11);
COPY(R12); COPY(R13); COPY(R14); COPY(R15);
COPY(RDI); COPY(RSI); COPY(RBP); COPY(RBX);
COPY(RDX); COPY(RAX); COPY(RCX); COPY(RSP);
COPY(RIP);
COPY2(EFLAGS, EFL);
mc->gregs[REG_CSGSFS] = mc->gregs[REG_CSGSFS] & 0xffffffffffffl;
mc->gregs[REG_CSGSFS] |= (regs->gp[SS / sizeof(unsigned long)] & 0xffff) << 48;
#endif
if (single_stepping)
mc->gregs[REG_EFL] |= X86_EFLAGS_TF;
else
mc->gregs[REG_EFL] &= ~X86_EFLAGS_TF;
}
#ifdef CONFIG_X86_32
struct _xstate_64 {
struct _fpstate_64 fpstate;
struct _header xstate_hdr;
struct _ymmh_state ymmh;
/* New processor state extensions go here: */
};
/* Not quite the right structures as these contain more information */
int um_i387_from_fxsr(struct _fpstate_32 *i387,
const struct _fpstate_64 *fxsave);
int um_fxsr_from_i387(struct _fpstate_64 *fxsave,
const struct _fpstate_32 *from);
#else
#define _xstate_64 _xstate
#endif
static struct _fpstate *get_fpstate(struct stub_data *data,
mcontext_t *mcontext,
int *fp_size)
{
struct _fpstate *res;
/* Assume floating point registers are on the same page */
res = (void *)(((unsigned long)mcontext->fpregs &
(UM_KERN_PAGE_SIZE - 1)) +
(unsigned long)&data->sigstack[0]);
if ((void *)res + sizeof(struct _fpstate) >
(void *)data->sigstack + sizeof(data->sigstack))
return NULL;
if (res->sw_reserved.magic1 != FP_XSTATE_MAGIC1) {
*fp_size = sizeof(struct _fpstate);
} else {
char *magic2_addr;
magic2_addr = (void *)res;
magic2_addr += res->sw_reserved.extended_size;
magic2_addr -= FP_XSTATE_MAGIC2_SIZE;
/* We still need to be within our stack */
if ((void *)magic2_addr >
(void *)data->sigstack + sizeof(data->sigstack))
return NULL;
/* If we do not read MAGIC2, then we did something wrong */
if (*(__u32 *)magic2_addr != FP_XSTATE_MAGIC2)
return NULL;
/* Remove MAGIC2 from the size, we do not save/restore it */
*fp_size = res->sw_reserved.extended_size -
FP_XSTATE_MAGIC2_SIZE;
}
return res;
}
int get_stub_state(struct uml_pt_regs *regs, struct stub_data *data,
unsigned long *fp_size_out)
{
mcontext_t *mcontext;
struct _fpstate *fpstate_stub;
struct _xstate_64 *xstate_stub;
int fp_size, xstate_size;
/* mctx_offset is verified by wait_stub_done_seccomp */
mcontext = (void *)&data->sigstack[data->mctx_offset];
get_regs_from_mc(regs, mcontext);
fpstate_stub = get_fpstate(data, mcontext, &fp_size);
if (!fpstate_stub)
return -EINVAL;
#ifdef CONFIG_X86_32
xstate_stub = (void *)&fpstate_stub->_fxsr_env;
xstate_size = fp_size - offsetof(struct _fpstate_32, _fxsr_env);
#else
xstate_stub = (void *)fpstate_stub;
xstate_size = fp_size;
#endif
if (fp_size_out)
*fp_size_out = xstate_size;
if (xstate_size > host_fp_size)
return -ENOSPC;
memcpy(®s->fp, xstate_stub, xstate_size);
/* We do not need to read the x86_64 FS_BASE/GS_BASE registers as
* we do not permit userspace to set them directly.
*/
#ifdef CONFIG_X86_32
/* Read the i387 legacy FP registers */
if (um_fxsr_from_i387((void *)®s->fp, fpstate_stub))
return -EINVAL;
#endif
return 0;
}
/* Copied because we cannot include regset.h here. */
struct task_struct;
struct user_regset;
struct membuf {
void *p;
size_t left;
};
int fpregs_legacy_get(struct task_struct *target,
const struct user_regset *regset,
struct membuf to);
int set_stub_state(struct uml_pt_regs *regs, struct stub_data *data,
int single_stepping)
{
mcontext_t *mcontext;
struct _fpstate *fpstate_stub;
struct _xstate_64 *xstate_stub;
int fp_size, xstate_size;
/* mctx_offset is verified by wait_stub_done_seccomp */
mcontext = (void *)&data->sigstack[data->mctx_offset];
if ((unsigned long)mcontext < (unsigned long)data->sigstack ||
(unsigned long)mcontext >
(unsigned long) data->sigstack +
sizeof(data->sigstack) - sizeof(*mcontext))
return -EINVAL;
get_mc_from_regs(regs, mcontext, single_stepping);
fpstate_stub = get_fpstate(data, mcontext, &fp_size);
if (!fpstate_stub)
return -EINVAL;
#ifdef CONFIG_X86_32
xstate_stub = (void *)&fpstate_stub->_fxsr_env;
xstate_size = fp_size - offsetof(struct _fpstate_32, _fxsr_env);
#else
xstate_stub = (void *)fpstate_stub;
xstate_size = fp_size;
#endif
memcpy(xstate_stub, ®s->fp, xstate_size);
#ifdef __i386__
/*
* On x86, the GDT entries are updated by arch_set_tls.
*/
/* Store the i387 legacy FP registers which the host will use */
if (um_i387_from_fxsr(fpstate_stub, (void *)®s->fp))
return -EINVAL;
#else
/*
* On x86_64, we need to sync the FS_BASE/GS_BASE registers using the
* arch specific data.
*/
if (data->arch_data.fs_base != regs->gp[FS_BASE / sizeof(unsigned long)]) {
data->arch_data.fs_base = regs->gp[FS_BASE / sizeof(unsigned long)];
data->arch_data.sync |= STUB_SYNC_FS_BASE;
}
if (data->arch_data.gs_base != regs->gp[GS_BASE / sizeof(unsigned long)]) {
data->arch_data.gs_base = regs->gp[GS_BASE / sizeof(unsigned long)];
data->arch_data.sync |= STUB_SYNC_GS_BASE;
}
#endif
return 0;
}
|