cpu_x86_64.c revision 1.2.28.1 1 /* $NetBSD: cpu_x86_64.c,v 1.2.28.1 2019/01/30 13:32:58 martin 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 * Note that this machdep.c uses the `dummy' mcontext_t defined for usermode.
32 * This is basicly a blob of PAGE_SIZE big. We might want to switch over to
33 * non-generic mcontext_t's one day, but will this break non-NetBSD hosts?
34 */
35
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: cpu_x86_64.c,v 1.2.28.1 2019/01/30 13:32:58 martin Exp $");
39
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/exec.h>
45 #include <sys/buf.h>
46 #include <sys/boot_flag.h>
47 #include <sys/ucontext.h>
48 #include <sys/utsname.h>
49 #include <machine/pcb.h>
50 #include <machine/psl.h>
51
52 #include <uvm/uvm_extern.h>
53 #include <uvm/uvm_page.h>
54
55 #include <dev/mm.h>
56 #include <machine/machdep.h>
57 #include <machine/thunk.h>
58
59
60 #if 0
61 static void dump_regs(register_t *reg);;
62
63 static void
64 dump_regs(register_t *reg)
65 {
66 int i;
67
68 /* register dump before call */
69 const char *name[] = {"RDI", "RSI", "RDX", "RCX", "R8", "R9", "R10",
70 "R11", "R12", "R13", "R14", "R15", "RBP", "RAX",
71 "GS", "FS", "ES", "DS", "TRAPNO", "ERR", "RIP", "CS",
72 "RFLAGS", "RSP", "SS"};
73
74 for (i =0; i < 26; i++)
75 printf("reg[%02d] (%6s) = %"PRIx32"\n",
76 i, name[i], (uint32_t) reg[i]);
77 }
78 #endif
79
80
81 /* from sys/arch/amd64/include/frame.h : KEEP IN SYNC */
82
83 /*
84 * Signal frame
85 */
86 struct sigframe_siginfo {
87 uint64_t sf_ra; /* return address for handler */
88 siginfo_t sf_si; /* actual saved siginfo */
89 ucontext_t sf_uc; /* actual saved ucontext */
90 };
91
92
93 /* should be the same as i386 */
94 /*
95 * mcontext extensions to handle signal delivery.
96 */
97 #define _UC_SETSTACK 0x00010000
98 #define _UC_CLRSTACK 0x00020000
99 #define _UC_VM 0x00040000
100 #define _UC_TLSBASE 0x00080000
101
102
103 void
104 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
105 {
106 struct lwp *l = curlwp;
107 struct proc *p = l->l_proc;
108 struct pcb *pcb = lwp_getpcb(l);
109 struct sigacts *ps = p->p_sigacts;
110 struct sigframe_siginfo *fp, frame;
111 int sig = ksi->ksi_signo;
112 sig_t catcher = SIGACTION(p, sig).sa_handler;
113 ucontext_t *ucp;
114 register_t *reg;
115 int onstack, error;
116 char *sp;
117
118 KASSERT(mutex_owned(p->p_lock));
119
120 ucp = &pcb->pcb_userret_ucp;
121 reg = (register_t *) &ucp->uc_mcontext;
122 #if 0
123 thunk_printf("%s: ", __func__);
124 thunk_printf("flags %d, ", (int) ksi->ksi_flags);
125 thunk_printf("to lwp %d, signo %d, code %d, errno %d\n",
126 (int) ksi->ksi_lid,
127 ksi->ksi_signo,
128 ksi->ksi_code,
129 ksi->ksi_errno);
130 #endif
131
132 /* do we need to jump onto the signal stack? */
133 onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
134 && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
135
136 sp = ((char *) reg[24] - 128);/* why -128?*//* RSP */
137 if (onstack)
138 sp = (char *) l->l_sigstk.ss_sp + l->l_sigstk.ss_size;
139
140 sp -= sizeof(struct sigframe_siginfo);
141 /*
142 * Round down the stackpointer to a multiple of 16 for
143 * fxsave and the ABI
144 */
145 fp = (struct sigframe_siginfo *) (((unsigned long)sp & ~15) - 8);
146
147 /* set up stack frame */
148 memset(&frame, 0, sizeof(frame));
149 frame.sf_ra = (uint64_t) ps->sa_sigdesc[sig].sd_tramp;
150 frame.sf_si._info = ksi->ksi_info;
151
152 /* copy our userret context into sf_uc */
153 memcpy(&frame.sf_uc, ucp, sizeof(ucontext_t));
154 frame.sf_uc.uc_sigmask = *mask;
155 frame.sf_uc.uc_link = l->l_ctxlink;
156 frame.sf_uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK)
157 ? _UC_SETSTACK : _UC_CLRSTACK;
158 memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
159 sendsig_reset(l, sig);
160
161 /* copyout our frame to the stackframe */
162 mutex_exit(p->p_lock);
163 error = copyout(&frame, fp, sizeof(frame));
164 mutex_enter(p->p_lock);
165
166 if (error != 0) {
167 /*
168 * Process has trashed its stack; give it an illegal
169 * instruction to halt it in its tracks.
170 */
171 sigexit(l, SIGILL);
172 /* NOTREACHED */
173 }
174
175 /* set catcher and the new stack pointer */
176 reg[24] = (register_t) fp; /* RSP */
177 reg[21] = (register_t) catcher; /* RIP */
178
179 reg[ 0] = sig; /* RDI */
180 reg[ 1] = (uint64_t) &fp->sf_si; /* RSI */
181 reg[ 2] = (uint64_t) &fp->sf_uc; /* RDX */
182 reg[11] = reg[ 2]; /* R15 = RDX */
183
184 /* Remember that we're now on the signal stack. */
185 if (onstack)
186 l->l_sigstk.ss_flags |= SS_ONSTACK;
187 }
188
189 void
190 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
191 {
192 struct pcb *pcb = lwp_getpcb(l);
193 ucontext_t *ucp;
194 register_t *reg;
195 int i;
196
197 /* set up the user context */
198 ucp = &pcb->pcb_userret_ucp;
199 reg = (register_t *) &ucp->uc_mcontext;
200 for (i = 0; i < 15; i++)
201 reg[i] = 0;
202
203 reg[13] = l->l_proc->p_psstrp; /* RBX */
204 reg[21] = pack->ep_entry; /* RIP */
205 reg[24] = (register_t) stack; /* RSP */
206
207 /* use given stack */
208 ucp->uc_stack.ss_sp = (void *) stack;
209 ucp->uc_stack.ss_size = pack->ep_ssize;
210
211 //dump_regs(reg);
212 }
213
214 void
215 md_syscall_get_syscallnumber(ucontext_t *ucp, uint32_t *code)
216 {
217 register_t *reg = (register_t *) &ucp->uc_mcontext;
218 *code = reg[14]; /* RAX */
219 }
220
221 int
222 md_syscall_getargs(lwp_t *l, ucontext_t *ucp, int nargs, int argsize,
223 register_t *args)
224 {
225 register_t *reg = (register_t *) &ucp->uc_mcontext;
226 register_t *sp = (register_t *) reg[24];/* RSP */
227 int ret;
228
229 //dump_regs(reg);
230
231 /*
232 * 1st 6 syscall args are passed in
233 * rdi, rsi, rdx, r10, r8 and r9
234 */
235 args[0] = reg[ 0]; /* RDI */
236 args[1] = reg[ 1]; /* RSI */
237 args[2] = reg[ 2]; /* RDX */
238 args[3] = reg[ 6]; /* R10 (RCX got clobbered) */
239 args[4] = reg[ 4]; /* R8 */
240 args[5] = reg[ 5]; /* R9 */
241
242 ret = 0;
243 if (argsize > 6 * 8) {
244 ret = copyin(sp + 1,
245 args + 6, argsize - 6 * 8);
246 }
247
248 return ret;
249 }
250
251 void
252 md_syscall_set_returnargs(lwp_t *l, ucontext_t *ucp,
253 int error, register_t *rval)
254 {
255 register_t *reg = (register_t *) &ucp->uc_mcontext;
256
257 reg[23] &= ~PSL_C; /* RFLAGS */
258 if (error > 0) {
259 rval[0] = error;
260 reg[23] |= PSL_C; /* RFLAGS */
261 }
262
263 /* set return parameters */
264 reg[14] = rval[0]; /* RAX */
265 if (error == 0)
266 reg[ 2] = rval[1]; /* RDX */
267
268 //dump_regs(reg);
269 }
270
271 register_t
272 md_get_pc(ucontext_t *ucp)
273 {
274 register_t *reg = (register_t *) &ucp->uc_mcontext;
275
276 return reg[21]; /* RIP */
277 }
278
279 register_t
280 md_get_sp(ucontext_t *ucp)
281 {
282 register_t *reg = (register_t *) &ucp->uc_mcontext;
283
284 return reg[24]; /* RSP */
285 }
286
287 int
288 md_syscall_check_opcode(ucontext_t *ucp)
289 {
290 uint32_t opcode;
291
292 md_syscall_get_opcode(ucp, &opcode);
293
294 switch (opcode) {
295 case 0xff0f: /* UD1 */
296 case 0xff0b: /* UD2 */
297 case 0x80cd: /* int $80 */
298 case 0x340f: /* sysenter */
299 case 0x050f: /* syscall */
300 return 1;
301 default:
302 return 0;
303 }
304 }
305
306
307 void
308 md_syscall_get_opcode(ucontext_t *ucp, uint32_t *opcode)
309 {
310 register_t *reg = (register_t *) &ucp->uc_mcontext;
311 // uint8_t *p8 = (uint8_t *) (reg[21]);
312 uint16_t *p16 = (uint16_t*) (reg[21]); /* RIP */
313
314 switch (*p16) {
315 case 0xff0f: /* UD1 */
316 case 0xff0b: /* UD2 */
317 case 0x80cd: /* int $80 */
318 case 0x340f: /* sysenter */
319 case 0x050f: /* syscall */
320 *opcode = *p16;
321 break;
322 default:
323 *opcode = 0;
324 }
325 }
326
327 void
328 md_syscall_inc_pc(ucontext_t *ucp, uint32_t opcode)
329 {
330 register_t *reg = (register_t *) &ucp->uc_mcontext;
331
332 /* advance program counter */
333 switch (opcode) {
334 case 0xff0f: /* UD1 */
335 case 0xff0b: /* UD2 */
336 case 0x80cd: /* int $80 */
337 case 0x340f: /* sysenter */
338 case 0x050f: /* syscall */
339 reg[21] += 2; /* RIP */
340 break;
341 default:
342 panic("%s, unknown illegal instruction: opcode = %x\n",
343 __func__, (uint32_t) opcode);
344 }
345 }
346
347 void
348 md_syscall_dec_pc(ucontext_t *ucp, uint32_t opcode)
349 {
350 register_t *reg = (register_t *) &ucp->uc_mcontext;
351
352 switch (opcode) {
353 case 0xff0f: /* UD1 */
354 case 0xff0b: /* UD2 */
355 case 0x80cd: /* int $80 */
356 case 0x340f: /* sysenter */
357 case 0x050f: /* syscall */
358 reg[21] -= 2; /* RIP */
359 break;
360 default:
361 panic("%s, unknown illegal instruction: opcode = %x\n",
362 __func__, (uint32_t) opcode);
363 }
364 }
365
366