cpu_i386.c revision 1.5 1 /* $NetBSD: cpu_i386.c,v 1.5 2018/05/18 20:21:14 reinoud 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_i386.c,v 1.5 2018/05/18 20:21:14 reinoud 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
53 #include "opt_exec.h"
54
55 #if 0
56 static void dump_regs(register_t *reg);;
57
58 static void
59 dump_regs(register_t *reg)
60 {
61 int i;
62
63 /* register dump before call */
64 const char *name[] = {"GS", "FS", "ES", "DS", "EDI", "ESI", "EBP", "ESP",
65 "EBX", "EDX", "ECX", "EAX", "TRAPNO", "ERR", "EIP", "CS", "EFL",
66 "UESP", "SS"};
67
68 for (i =0; i < 19; i++)
69 printf("reg[%02d] (%6s) = %"PRIx32"\n", i, name[i], (uint32_t) reg[i]);
70 }
71 #endif
72
73
74 /* from sys/arch/i386/include/frame.h : KEEP IN SYNC */
75
76 /*
77 * New-style signal frame
78 */
79 struct sigframe_siginfo {
80 int sf_ra; /* return address for handler */
81 int sf_signum; /* "signum" argument for handler */
82 siginfo_t *sf_sip; /* "sip" argument for handler */
83 ucontext_t *sf_ucp; /* "ucp" argument for handler */
84 siginfo_t sf_si; /* actual saved siginfo */
85 ucontext_t sf_uc; /* actual saved ucontext */
86 };
87
88
89 /*
90 * mcontext extensions to handle signal delivery.
91 */
92 #define _UC_SETSTACK 0x00010000
93 #define _UC_CLRSTACK 0x00020000
94 #define _UC_VM 0x00040000
95 #define _UC_TLSBASE 0x00080000
96
97
98 void
99 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
100 {
101 struct lwp *l = curlwp;
102 struct proc *p = l->l_proc;
103 struct pcb *pcb = lwp_getpcb(l);
104 struct sigacts *ps = p->p_sigacts;
105 struct sigframe_siginfo *fp, frame;
106 int sig = ksi->ksi_signo;
107 sig_t catcher = SIGACTION(p, sig).sa_handler;
108 ucontext_t *ucp;
109 register_t *reg;
110 int onstack, error;
111
112 KASSERT(mutex_owned(p->p_lock));
113
114 ucp = &pcb->pcb_userret_ucp;
115 reg = (register_t *) &ucp->uc_mcontext.__gregs;
116 #if 0
117 thunk_printf("%s: ", __func__);
118 thunk_printf("flags %d, ", (int) ksi->ksi_flags);
119 thunk_printf("to lwp %d, signo %d, code %d, errno %d\n",
120 (int) ksi->ksi_lid,
121 ksi->ksi_signo,
122 ksi->ksi_code,
123 ksi->ksi_errno);
124 #endif
125
126 /* do we need to jump onto the signal stack? */
127 onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
128 && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
129
130 fp = (void *) reg[17]; /* ESP */
131 if (onstack)
132 fp = (void *)
133 ((char *) l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
134
135 fp--;
136
137 /* set up stack frame */
138 frame.sf_ra = (int)ps->sa_sigdesc[sig].sd_tramp;
139 frame.sf_signum = sig;
140 frame.sf_sip = &fp->sf_si;
141 frame.sf_ucp = &fp->sf_uc;
142 frame.sf_si._info = ksi->ksi_info;
143
144 /* copy our userret context into sf_uc */
145 memcpy(&frame.sf_uc, ucp, sizeof(ucontext_t));
146 frame.sf_uc.uc_sigmask = *mask;
147 frame.sf_uc.uc_link = l->l_ctxlink; /* XXX ??? */
148 frame.sf_uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK)
149 ? _UC_SETSTACK : _UC_CLRSTACK;
150 memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
151
152 sendsig_reset(l, sig);
153
154 /* copyout our frame to the stackframe */
155 mutex_exit(p->p_lock);
156 error = copyout(&frame, fp, sizeof(frame));
157 mutex_enter(p->p_lock);
158
159 if (error != 0) {
160 /*
161 * Process has trashed its stack; give it an illegal
162 * instruction to halt it in its tracks.
163 */
164 sigexit(l, SIGILL);
165 /* NOTREACHED */
166 }
167
168 /* set catcher and the new stack pointer */
169 reg[17] = (register_t) fp; /* ESP */
170 reg[14] = (register_t) catcher; /* EIP */
171
172 /* Remember that we're now on the signal stack. */
173 if (onstack)
174 l->l_sigstk.ss_flags |= SS_ONSTACK;
175 }
176
177 void
178 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
179 {
180 struct pcb *pcb = lwp_getpcb(l);
181 ucontext_t *ucp;
182 uint *reg, i;
183
184 #ifdef DEBUG_EXEC
185 printf("setregs called: lwp %p, exec package %p, stack %p\n",
186 l, pack, (void *) stack);
187 printf("current stat of pcb %p\n", pcb);
188 printf("\tpcb->pcb_ucp.uc_stack.ss_sp = %p\n",
189 pcb->pcb_ucp.uc_stack.ss_sp);
190 printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n",
191 (int) pcb->pcb_ucp.uc_stack.ss_size);
192 #endif
193
194 /* set up the user context */
195 ucp = &pcb->pcb_userret_ucp;
196 reg = (int *) &ucp->uc_mcontext.__gregs;
197 for (i = 4; i < 11; i++)
198 reg[i] = 0;
199
200 /* use given stack */
201 ucp->uc_stack.ss_sp = (void *) (stack-4); /* to prevent clearing */
202 ucp->uc_stack.ss_size = 0; //pack->ep_ssize;
203 thunk_makecontext(ucp, (void *) pack->ep_entry,
204 0, NULL, NULL, NULL, NULL);
205
206 /* patch up */
207 reg[ 8] = l->l_proc->p_psstrp; /* _REG_EBX */
208 reg[17] = (stack); /* _REG_UESP */
209
210 //dump_regs(reg);
211
212 #ifdef DEBUG_EXEC
213 printf("updated pcb %p\n", pcb);
214 printf("\tpcb->pcb_ucp.uc_stack.ss_sp = %p\n",
215 pcb->pcb_ucp.uc_stack.ss_sp);
216 printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n",
217 (int) pcb->pcb_ucp.uc_stack.ss_size);
218 printf("\tpack->ep_entry = %p\n",
219 (void *) pack->ep_entry);
220 #endif
221 }
222
223 void
224 md_syscall_get_syscallnumber(ucontext_t *ucp, uint32_t *code)
225 {
226 uint *reg = (int *) &ucp->uc_mcontext.__gregs;
227 *code = reg[11]; /* EAX */
228 }
229
230 int
231 md_syscall_getargs(lwp_t *l, ucontext_t *ucp, int nargs, int argsize,
232 register_t *args)
233 {
234 uint *reg = (int *) &ucp->uc_mcontext.__gregs;
235 register_t *sp = (register_t *) reg[17];/* ESP */
236 int ret;
237
238 //dump_regs(reg);
239 ret = copyin(sp + 1, args, argsize);
240
241 return ret;
242 }
243
244 void
245 md_syscall_set_returnargs(lwp_t *l, ucontext_t *ucp,
246 int error, register_t *rval)
247 {
248 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
249
250 reg[16] &= ~PSL_C; /* EFL */
251 if (error > 0) {
252 rval[0] = error;
253 reg[16] |= PSL_C; /* EFL */
254 }
255
256 /* set return parameters */
257 reg[11] = rval[0]; /* EAX */
258 if (error == 0)
259 reg[ 9] = rval[1]; /* EDX */
260
261 //dump_regs(reg);
262 }
263
264 register_t
265 md_get_pc(ucontext_t *ucp)
266 {
267 KASSERT(ucp);
268 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
269
270 return reg[14]; /* EIP */
271 }
272
273 register_t
274 md_get_sp(ucontext_t *ucp)
275 {
276 KASSERT(ucp);
277 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
278
279 return reg[17]; /* ESP */
280 }
281
282 int
283 md_syscall_check_opcode(ucontext_t *ucp)
284 {
285 uint32_t opcode;
286
287 md_syscall_get_opcode(ucp, &opcode);
288
289 switch (opcode) {
290 case 0xff0f: /* UD1 */
291 case 0xff0b: /* UD2 */
292 case 0x80cd: /* int $80 */
293 case 0x340f: /* sysenter */
294 case 0x050f: /* syscall */
295 return 1;
296 default:
297 return 0;
298 }
299 }
300
301 void
302 md_syscall_get_opcode(ucontext_t *ucp, uint32_t *opcode)
303 {
304 KASSERT(ucp);
305 register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
306 // uint8_t *p8 = (uint8_t *) (reg[14]);
307 uint16_t *p16 = (uint16_t*) (reg[14]); /* EIP */
308
309 switch (*p16) {
310 case 0xff0f: /* UD1 */
311 case 0xff0b: /* UD2 */
312 case 0x80cd: /* int $80 */
313 case 0x340f: /* sysenter */
314 case 0x050f: /* syscall */
315 *opcode = *p16;
316 break;
317 default:
318 *opcode = 0;
319 }
320 }
321
322 void
323 md_syscall_inc_pc(ucontext_t *ucp, uint32_t opcode)
324 {
325 KASSERT(ucp);
326 uint *reg = (int *) &ucp->uc_mcontext.__gregs;
327
328 /* advance program counter */
329 switch (opcode) {
330 case 0xff0f: /* UD1 */
331 case 0xff0b: /* UD2 */
332 case 0x80cd: /* int $80 */
333 case 0x340f: /* sysenter */
334 case 0x050f: /* syscall */
335 reg[14] += 2; /* EIP */
336 break;
337 default:
338 panic("%s, unknown illegal instruction: opcode = %x\n",
339 __func__, (uint32_t) opcode);
340 }
341 }
342
343 void
344 md_syscall_dec_pc(ucontext_t *ucp, uint32_t opcode)
345 {
346 KASSERT(ucp);
347 uint *reg = (int *) &ucp->uc_mcontext.__gregs;
348
349 switch (opcode) {
350 case 0xff0f: /* UD1 */
351 case 0xff0b: /* UD2 */
352 case 0x80cd: /* int $80 */
353 case 0x340f: /* sysenter */
354 case 0x050f: /* syscall */
355 reg[14] -= 2; /* EIP */
356 break;
357 default:
358 panic("%s, unknown illegal instruction: opcode = %x\n",
359 __func__, (uint32_t) opcode);
360 }
361 }
362
363