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