#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cpu_i386.c,v 1.8 2018/11/27 14:09:54 maxv Exp $");
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/exec.h>
#include <sys/buf.h>
#include <sys/boot_flag.h>
#include <sys/ucontext.h>
#include <sys/utsname.h>
#include <machine/pcb.h>
#include <machine/psl.h>
#include <uvm/uvm_extern.h>
#include <uvm/uvm_page.h>
#include <dev/mm.h>
#include <machine/machdep.h>
#include <machine/thunk.h>
#include <machine/mcontext.h>
#include "opt_exec.h"
#if 0
static void dump_regs(register_t *reg);
static void
dump_regs(register_t *reg)
{
int i;
const char *name[] = {"GS", "FS", "ES", "DS", "EDI", "ESI", "EBP", "ESP",
"EBX", "EDX", "ECX", "EAX", "TRAPNO", "ERR", "EIP", "CS", "EFL",
"UESP", "SS"};
for (i =0; i < 19; i++)
printf("reg[%02d] (%6s) = %"PRIx32"\n", i, name[i], (uint32_t) reg[i]);
}
#endif
struct sigframe_siginfo {
int sf_ra;
int sf_signum;
siginfo_t *sf_sip;
ucontext_t *sf_ucp;
siginfo_t sf_si;
ucontext_t sf_uc;
};
void
sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct pcb *pcb = lwp_getpcb(l);
struct sigacts *ps = p->p_sigacts;
struct sigframe_siginfo *fp, frame;
int sig = ksi->ksi_signo;
sig_t catcher = SIGACTION(p, sig).sa_handler;
ucontext_t *ucp;
register_t *reg;
int onstack, error;
KASSERT(mutex_owned(p->p_lock));
ucp = &pcb->pcb_userret_ucp;
reg = (register_t *) &ucp->uc_mcontext.__gregs;
#if 0
thunk_printf("%s: ", __func__);
thunk_printf("flags %d, ", (int) ksi->ksi_flags);
thunk_printf("to lwp %d, signo %d, code %d, errno %d\n",
(int) ksi->ksi_lid,
ksi->ksi_signo,
ksi->ksi_code,
ksi->ksi_errno);
#endif
onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
&& (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
fp = (void *) reg[17];
if (onstack)
fp = (void *)
((char *) l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
fp--;
memset(&frame, 0, sizeof(frame));
frame.sf_ra = (int)ps->sa_sigdesc[sig].sd_tramp;
frame.sf_signum = sig;
frame.sf_sip = &fp->sf_si;
frame.sf_ucp = &fp->sf_uc;
frame.sf_si._info = ksi->ksi_info;
memcpy(&frame.sf_uc, ucp, sizeof(ucontext_t));
frame.sf_uc.uc_sigmask = *mask;
frame.sf_uc.uc_link = l->l_ctxlink;
frame.sf_uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK)
? _UC_SETSTACK : _UC_CLRSTACK;
memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
sendsig_reset(l, sig);
mutex_exit(p->p_lock);
error = copyout(&frame, fp, sizeof(frame));
mutex_enter(p->p_lock);
if (error != 0) {
sigexit(l, SIGILL);
}
reg[17] = (register_t) fp;
reg[14] = (register_t) catcher;
if (onstack)
l->l_sigstk.ss_flags |= SS_ONSTACK;
}
void
setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
{
struct pcb *pcb = lwp_getpcb(l);
ucontext_t *ucp;
uint *reg, i;
#ifdef DEBUG_EXEC
printf("setregs called: lwp %p, exec package %p, stack %p\n",
l, pack, (void *) stack);
printf("current stat of pcb %p\n", pcb);
printf("\tpcb->pcb_ucp.uc_stack.ss_sp = %p\n",
pcb->pcb_ucp.uc_stack.ss_sp);
printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n",
(int) pcb->pcb_ucp.uc_stack.ss_size);
#endif
ucp = &pcb->pcb_userret_ucp;
reg = (int *) &ucp->uc_mcontext.__gregs;
for (i = 4; i < 11; i++)
reg[i] = 0;
ucp->uc_stack.ss_sp = (void *) (stack-4);
ucp->uc_stack.ss_size = 0;
thunk_makecontext(ucp, (void *) pack->ep_entry,
0, NULL, NULL, NULL, NULL);
reg[ 8] = l->l_proc->p_psstrp;
reg[17] = (stack);
#ifdef DEBUG_EXEC
printf("updated pcb %p\n", pcb);
printf("\tpcb->pcb_ucp.uc_stack.ss_sp = %p\n",
pcb->pcb_ucp.uc_stack.ss_sp);
printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n",
(int) pcb->pcb_ucp.uc_stack.ss_size);
printf("\tpack->ep_entry = %p\n",
(void *) pack->ep_entry);
#endif
}
void
md_syscall_get_syscallnumber(ucontext_t *ucp, uint32_t *code)
{
uint *reg = (int *) &ucp->uc_mcontext.__gregs;
*code = reg[11];
}
int
md_syscall_getargs(lwp_t *l, ucontext_t *ucp, int nargs, int argsize,
register_t *args)
{
uint *reg = (int *) &ucp->uc_mcontext.__gregs;
register_t *sp = (register_t *) reg[17];
int ret;
ret = copyin(sp + 1, args, argsize);
return ret;
}
void
md_syscall_set_returnargs(lwp_t *l, ucontext_t *ucp,
int error, register_t *rval)
{
register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
reg[16] &= ~PSL_C;
if (error > 0) {
rval[0] = error;
reg[16] |= PSL_C;
}
reg[11] = rval[0];
if (error == 0)
reg[ 9] = rval[1];
}
register_t
md_get_pc(ucontext_t *ucp)
{
KASSERT(ucp);
register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
return reg[14];
}
register_t
md_get_sp(ucontext_t *ucp)
{
KASSERT(ucp);
register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
return reg[17];
}
int
md_syscall_check_opcode(ucontext_t *ucp)
{
uint32_t opcode;
md_syscall_get_opcode(ucp, &opcode);
switch (opcode) {
case 0xff0f:
case 0xff0b:
case 0x80cd:
case 0x340f:
case 0x050f:
return 1;
default:
return 0;
}
}
void
md_syscall_get_opcode(ucontext_t *ucp, uint32_t *opcode)
{
KASSERT(ucp);
register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs;
uint16_t *p16 = (uint16_t*) (reg[14]);
switch (*p16) {
case 0xff0f:
case 0xff0b:
case 0x80cd:
case 0x340f:
case 0x050f:
*opcode = *p16;
break;
default:
*opcode = 0;
}
}
void
md_syscall_inc_pc(ucontext_t *ucp, uint32_t opcode)
{
KASSERT(ucp);
uint *reg = (int *) &ucp->uc_mcontext.__gregs;
switch (opcode) {
case 0xff0f:
case 0xff0b:
case 0x80cd:
case 0x340f:
case 0x050f:
reg[14] += 2;
break;
default:
panic("%s, unknown illegal instruction: opcode = %x\n",
__func__, (uint32_t) opcode);
}
}
void
md_syscall_dec_pc(ucontext_t *ucp, uint32_t opcode)
{
KASSERT(ucp);
uint *reg = (int *) &ucp->uc_mcontext.__gregs;
switch (opcode) {
case 0xff0f:
case 0xff0b:
case 0x80cd:
case 0x340f:
case 0x050f:
reg[14] -= 2;
break;
default:
panic("%s, unknown illegal instruction: opcode = %x\n",
__func__, (uint32_t) opcode);
}
}