cpu_x86_64.c revision 1.2.48.2 1 /* $NetBSD: cpu_x86_64.c,v 1.2.48.2 2018/06/25 07:25:46 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2011 Reinoud Zandijk <reinoud (at) netbsd.org>
5 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: cpu_x86_64.c,v 1.2.48.2 2018/06/25 07:25:46 pgoyette Exp $");
33
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #include <sys/exec.h>
39 #include <sys/buf.h>
40 #include <sys/boot_flag.h>
41 #include <sys/ucontext.h>
42 #include <sys/utsname.h>
43 #include <machine/pcb.h>
44 #include <machine/psl.h>
45
46 #include <uvm/uvm_extern.h>
47 #include <uvm/uvm_page.h>
48
49 #include <dev/mm.h>
50 #include <machine/machdep.h>
51 #include <machine/thunk.h>
52 #include <machine/mcontext.h>
53
54 #if 0
55 static void dump_regs(register_t *reg);
56
57 static void
58 dump_regs(register_t *reg)
59 {
60 int i;
61
62 /* register dump before call */
63 const char *name[] = {"RDI", "RSI", "RDX", "RCX", "R8", "R9", "R10",
64 "R11", "R12", "R13", "R14", "R15", "RBP", "RAX",
65 "GS", "FS", "ES", "DS", "TRAPNO", "ERR", "RIP", "CS",
66 "RFLAGS", "RSP", "SS"};
67
68 for (i =0; i < 26; i++)
69 printf("reg[%02d] (%6s) = %"PRIx32"\n",
70 i, name[i], (uint32_t) reg[i]);
71 }
72 #endif
73
74
75 /* from sys/arch/amd64/include/frame.h : KEEP IN SYNC */
76
77 /*
78 * Signal frame
79 */
80 struct sigframe_siginfo {
81 uint64_t sf_ra; /* return address for handler */
82 siginfo_t sf_si; /* actual saved siginfo */
83 ucontext_t sf_uc; /* actual saved ucontext */
84 };
85
86
87 /*
88 * mcontext extensions to handle signal delivery.
89 */
90 void
91 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
92 {
93 struct lwp *l = curlwp;
94 struct proc *p = l->l_proc;
95 struct pcb *pcb = lwp_getpcb(l);
96 struct sigacts *ps = p->p_sigacts;
97 struct sigframe_siginfo *fp, frame;
98 int sig = ksi->ksi_signo;
99 sig_t catcher = SIGACTION(p, sig).sa_handler;
100 ucontext_t *ucp;
101 register_t *reg;
102 int onstack, error;
103 char *sp;
104
105 KASSERT(mutex_owned(p->p_lock));
106
107 ucp = &pcb->pcb_userret_ucp;
108 reg = (register_t *) &ucp->uc_mcontext.__gregs;
109 #if 0
110 thunk_printf("%s: ", __func__);
111 thunk_printf("flags %d, ", (int) ksi->ksi_flags);
112 thunk_printf("to lwp %d, signo %d, code %d, errno %d\n",
113 (int) ksi->ksi_lid,
114 ksi->ksi_signo,
115 ksi->ksi_code,
116 ksi->ksi_errno);
117 #endif
118
119 /* do we need to jump onto the signal stack? */
120 onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
121 && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
122
123 sp = ((char *) reg[24] - 128);/* why -128?*//* RSP */
124 if (onstack)
125 sp = (char *) l->l_sigstk.ss_sp + l->l_sigstk.ss_size;
126
127 sp -= sizeof(struct sigframe_siginfo);
128 /*
129 * Round down the stackpointer to a multiple of 16 for
130 * fxsave and the ABI
131 */
132 fp = (struct sigframe_siginfo *) (((unsigned long)sp & ~15) - 8);
133
134 /* set up stack frame */
135 frame.sf_ra = (uint64_t) ps->sa_sigdesc[sig].sd_tramp;
136 frame.sf_si._info = ksi->ksi_info;
137
138 /* copy our userret context into sf_uc */
139 memcpy(&frame.sf_uc, ucp, sizeof(ucontext_t));
140 frame.sf_uc.uc_sigmask = *mask;
141 frame.sf_uc.uc_link = l->l_ctxlink;
142 frame.sf_uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK)
143 ? _UC_SETSTACK : _UC_CLRSTACK;
144 memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
145 sendsig_reset(l, sig);
146
147 /* copyout our frame to the stackframe */
148 mutex_exit(p->p_lock);
149 error = copyout(&frame, fp, sizeof(frame));
150 mutex_enter(p->p_lock);
151
152 if (error != 0) {
153 /*
154 * Process has trashed its stack; give it an illegal
155 * instruction to halt it in its tracks.
156 */
157 sigexit(l, SIGILL);
158 /* NOTREACHED */
159 }
160
161 /* set catcher and the new stack pointer */
162 reg[24] = (register_t) fp; /* RSP */
163 reg[21] = (register_t) catcher; /* RIP */
164
165 reg[ 0] = sig; /* RDI */
166 reg[ 1] = (uint64_t) &fp->sf_si; /* RSI */
167 reg[ 2] = (uint64_t) &fp->sf_uc; /* RDX */
168 reg[11] = reg[ 2]; /* R15 = RDX */
169
170 /* Remember that we're now on the signal stack. */
171 if (onstack)
172 l->l_sigstk.ss_flags |= SS_ONSTACK;
173 }
174
175 void
176 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
177 {
178 struct pcb *pcb = lwp_getpcb(l);
179 ucontext_t *ucp;
180 register_t *reg;
181 int i;
182
183 /* set up the user context */
184 ucp = &pcb->pcb_userret_ucp;
185 reg = (register_t *) &ucp->uc_mcontext.__gregs;
186 for (i = 0; i < 15; i++)
187 reg[i] = 0;
188
189 reg[13] = l->l_proc->p_psstrp; /* RBX */
190 reg[21] = pack->ep_entry; /* RIP */
191 reg[24] = (register_t) stack; /* RSP */
192
193 /* use given stack */
194 ucp->uc_stack.ss_sp = (void *) stack;
195 ucp->uc_stack.ss_size = pack->ep_ssize;
196
197 //dump_regs(reg);
198 }
199
200 void
201 md_syscall_get_syscallnumber(ucontext_t *ucp, uint32_t *code)
202 {
203 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
204 *code = reg[14]; /* RAX */
205 }
206
207 int
208 md_syscall_getargs(lwp_t *l, ucontext_t *ucp, int nargs, int argsize,
209 register_t *args)
210 {
211 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
212 register_t *sp = (register_t *) reg[24];/* RSP */
213 int ret;
214
215 //dump_regs(reg);
216
217 /*
218 * 1st 6 syscall args are passed in
219 * rdi, rsi, rdx, r10, r8 and r9
220 */
221 args[0] = reg[ 0]; /* RDI */
222 args[1] = reg[ 1]; /* RSI */
223 args[2] = reg[ 2]; /* RDX */
224 args[3] = reg[ 6]; /* R10 (RCX got clobbered) */
225 args[4] = reg[ 4]; /* R8 */
226 args[5] = reg[ 5]; /* R9 */
227
228 ret = 0;
229 if (argsize > 6 * 8) {
230 ret = copyin(sp + 1,
231 args + 6, argsize - 6 * 8);
232 }
233
234 return ret;
235 }
236
237 void
238 md_syscall_set_returnargs(lwp_t *l, ucontext_t *ucp,
239 int error, register_t *rval)
240 {
241 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
242
243 reg[23] &= ~PSL_C; /* RFLAGS */
244 if (error > 0) {
245 rval[0] = error;
246 reg[23] |= PSL_C; /* RFLAGS */
247 }
248
249 /* set return parameters */
250 reg[14] = rval[0]; /* RAX */
251 if (error == 0)
252 reg[ 2] = rval[1]; /* RDX */
253
254 //dump_regs(reg);
255 }
256
257 register_t
258 md_get_pc(ucontext_t *ucp)
259 {
260 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
261
262 return reg[21]; /* RIP */
263 }
264
265 register_t
266 md_get_sp(ucontext_t *ucp)
267 {
268 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
269
270 return reg[24]; /* RSP */
271 }
272
273 int
274 md_syscall_check_opcode(ucontext_t *ucp)
275 {
276 uint32_t opcode;
277
278 md_syscall_get_opcode(ucp, &opcode);
279
280 switch (opcode) {
281 case 0xff0f: /* UD1 */
282 case 0xff0b: /* UD2 */
283 case 0x80cd: /* int $80 */
284 case 0x340f: /* sysenter */
285 case 0x050f: /* syscall */
286 return 1;
287 default:
288 return 0;
289 }
290 }
291
292
293 void
294 md_syscall_get_opcode(ucontext_t *ucp, uint32_t *opcode)
295 {
296 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
297 // uint8_t *p8 = (uint8_t *) (reg[21]);
298 uint16_t *p16 = (uint16_t*) (reg[21]); /* RIP */
299
300 switch (*p16) {
301 case 0xff0f: /* UD1 */
302 case 0xff0b: /* UD2 */
303 case 0x80cd: /* int $80 */
304 case 0x340f: /* sysenter */
305 case 0x050f: /* syscall */
306 *opcode = *p16;
307 break;
308 default:
309 *opcode = 0;
310 }
311 }
312
313 void
314 md_syscall_inc_pc(ucontext_t *ucp, uint32_t opcode)
315 {
316 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
317
318 /* advance program counter */
319 switch (opcode) {
320 case 0xff0f: /* UD1 */
321 case 0xff0b: /* UD2 */
322 case 0x80cd: /* int $80 */
323 case 0x340f: /* sysenter */
324 case 0x050f: /* syscall */
325 reg[21] += 2; /* RIP */
326 break;
327 default:
328 panic("%s, unknown illegal instruction: opcode = %x\n",
329 __func__, (uint32_t) opcode);
330 }
331 }
332
333 void
334 md_syscall_dec_pc(ucontext_t *ucp, uint32_t opcode)
335 {
336 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
337
338 switch (opcode) {
339 case 0xff0f: /* UD1 */
340 case 0xff0b: /* UD2 */
341 case 0x80cd: /* int $80 */
342 case 0x340f: /* sysenter */
343 case 0x050f: /* syscall */
344 reg[21] -= 2; /* RIP */
345 break;
346 default:
347 panic("%s, unknown illegal instruction: opcode = %x\n",
348 __func__, (uint32_t) opcode);
349 }
350 }
351
352