#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/socketvar.h>
#include <sys/syscall.h>
#include <sys/sysctl.h>
#include <sys/sysent.h>
#include <sys/unistd.h>
#include <machine/cpu.h>
#include <machine/frame.h>
#include <machine/pcb.h>
#include <machine/sysarch.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_extern.h>
#include <vm/vm_kern.h>
#include <vm/vm_page.h>
#include <vm/vm_map.h>
#include <vm/vm_param.h>
#include <vm/vm_pageout.h>
#include <vm/uma.h>
#include <vm/uma_int.h>
#include <machine/md_var.h>
#include <machine/vfp.h>
_Static_assert((sizeof(struct switchframe) % 8) == 0, "Bad alignment");
_Static_assert((sizeof(struct trapframe) % 8) == 0, "Bad alignment");
uint32_t initial_fpscr = VFPSCR_DN | VFPSCR_FZ;
void
cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
{
struct pcb *pcb2;
struct trapframe *tf;
struct mdproc *mdp2;
if ((flags & RFPROC) == 0)
return;
pcb2 = (struct pcb *)
(td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE) - 1;
#ifdef VFP
if (curthread == td1) {
if ((td1->td_pcb->pcb_fpflags & PCB_FP_STARTED) != 0)
vfp_save_state(td1, td1->td_pcb);
}
#endif
td2->td_pcb = pcb2;
bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
mdp2 = &p2->p_md;
bcopy(&td1->td_proc->p_md, mdp2, sizeof(*mdp2));
td2->td_frame = (struct trapframe *)pcb2 - 1;
*td2->td_frame = *td1->td_frame;
pmap_set_pcb_pagedir(vmspace_pmap(p2->p_vmspace), pcb2);
pcb2->pcb_regs.sf_r4 = (register_t)fork_return;
pcb2->pcb_regs.sf_r5 = (register_t)td2;
pcb2->pcb_regs.sf_lr = (register_t)fork_trampoline;
pcb2->pcb_regs.sf_sp = (register_t)STACKALIGN(td2->td_frame);
pcb2->pcb_regs.sf_tpidrurw = (register_t)get_tls();
#ifdef VFP
vfp_new_thread(td2, td1, true);
#endif
tf = td2->td_frame;
tf->tf_spsr &= ~PSR_C;
tf->tf_r0 = 0;
tf->tf_r1 = 0;
td2->td_md.md_spinlock_count = 1;
td2->td_md.md_saved_cspr = PSR_SVC32_MODE;
}
void
cpu_set_syscall_retval(struct thread *td, int error)
{
struct trapframe *frame;
frame = td->td_frame;
switch (error) {
case 0:
frame->tf_r0 = td->td_retval[0];
frame->tf_r1 = td->td_retval[1];
frame->tf_spsr &= ~PSR_C;
break;
case ERESTART:
if ((frame->tf_spsr & PSR_T) != 0)
frame->tf_pc -= THUMB_INSN_SIZE;
else
frame->tf_pc -= INSN_SIZE;
break;
case EJUSTRETURN:
break;
default:
frame->tf_r0 = error;
frame->tf_spsr |= PSR_C;
break;
}
}
void
cpu_copy_thread(struct thread *td, struct thread *td0)
{
bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
td->td_pcb->pcb_regs.sf_r4 = (register_t)fork_return;
td->td_pcb->pcb_regs.sf_r5 = (register_t)td;
td->td_pcb->pcb_regs.sf_lr = (register_t)fork_trampoline;
td->td_pcb->pcb_regs.sf_sp = (register_t)STACKALIGN(td->td_frame);
td->td_frame->tf_spsr &= ~PSR_C;
td->td_frame->tf_r0 = 0;
#ifdef VFP
vfp_new_thread(td, td0, false);
#endif
td->td_md.md_spinlock_count = 1;
td->td_md.md_saved_cspr = PSR_SVC32_MODE;
}
int
cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
stack_t *stack)
{
struct trapframe *tf = td->td_frame;
tf->tf_usr_sp = STACKALIGN((int)stack->ss_sp + stack->ss_size);
tf->tf_pc = (int)entry;
tf->tf_r0 = (int)arg;
tf->tf_spsr = PSR_USR32_MODE;
if ((register_t)entry & 1)
tf->tf_spsr |= PSR_T;
return (0);
}
int
cpu_set_user_tls(struct thread *td, void *tls_base, int thr_flags __unused)
{
td->td_pcb->pcb_regs.sf_tpidrurw = (register_t)tls_base;
if (td == curthread)
set_tls(tls_base);
return (0);
}
void
cpu_thread_exit(struct thread *td)
{
}
void
cpu_thread_alloc(struct thread *td)
{
td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_pages *
PAGE_SIZE) - 1;
td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb) - 1;
}
void
cpu_thread_free(struct thread *td)
{
}
void
cpu_thread_clean(struct thread *td)
{
}
void
cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
{
td->td_pcb->pcb_regs.sf_r4 = (register_t)func;
td->td_pcb->pcb_regs.sf_r5 = (register_t)arg;
}
void
cpu_update_pcb(struct thread *td)
{
MPASS(td == curthread);
td->td_pcb->pcb_regs.sf_tpidrurw = (register_t)get_tls();
}
void
cpu_exit(struct thread *td)
{
}
bool
cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
{
return (true);
}
int
cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
int com __unused, void *data __unused)
{
return (EINVAL);
}
void
cpu_sync_core(void)
{
}