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