128029b68SRuslan Bukin /*-
2157654d0SRuslan Bukin * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
328029b68SRuslan Bukin * All rights reserved.
428029b68SRuslan Bukin *
528029b68SRuslan Bukin * Portions of this software were developed by SRI International and the
628029b68SRuslan Bukin * University of Cambridge Computer Laboratory under DARPA/AFRL contract
728029b68SRuslan Bukin * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
828029b68SRuslan Bukin *
928029b68SRuslan Bukin * Portions of this software were developed by the University of Cambridge
1028029b68SRuslan Bukin * Computer Laboratory as part of the CTSRD Project, with support from the
1128029b68SRuslan Bukin * UK Higher Education Innovation Fund (HEIF).
1228029b68SRuslan Bukin *
1328029b68SRuslan Bukin * Redistribution and use in source and binary forms, with or without
1428029b68SRuslan Bukin * modification, are permitted provided that the following conditions
1528029b68SRuslan Bukin * are met:
1628029b68SRuslan Bukin * 1. Redistributions of source code must retain the above copyright
1728029b68SRuslan Bukin * notice, this list of conditions and the following disclaimer.
1828029b68SRuslan Bukin * 2. Redistributions in binary form must reproduce the above copyright
1928029b68SRuslan Bukin * notice, this list of conditions and the following disclaimer in the
2028029b68SRuslan Bukin * documentation and/or other materials provided with the distribution.
2128029b68SRuslan Bukin *
2228029b68SRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2328029b68SRuslan Bukin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2428029b68SRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2528029b68SRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2628029b68SRuslan Bukin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2728029b68SRuslan Bukin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2828029b68SRuslan Bukin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2928029b68SRuslan Bukin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3028029b68SRuslan Bukin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3128029b68SRuslan Bukin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3228029b68SRuslan Bukin * SUCH DAMAGE.
3328029b68SRuslan Bukin */
3428029b68SRuslan Bukin
3528029b68SRuslan Bukin #include <sys/param.h>
3628029b68SRuslan Bukin #include <sys/systm.h>
3728029b68SRuslan Bukin #include <sys/limits.h>
3828029b68SRuslan Bukin #include <sys/proc.h>
3928029b68SRuslan Bukin #include <sys/sf_buf.h>
4028029b68SRuslan Bukin #include <sys/signal.h>
4128029b68SRuslan Bukin #include <sys/unistd.h>
4228029b68SRuslan Bukin
4328029b68SRuslan Bukin #include <vm/vm.h>
4428029b68SRuslan Bukin #include <vm/vm_page.h>
4528029b68SRuslan Bukin #include <vm/vm_map.h>
4628029b68SRuslan Bukin #include <vm/uma.h>
4728029b68SRuslan Bukin #include <vm/uma_int.h>
4828029b68SRuslan Bukin
4928029b68SRuslan Bukin #include <machine/riscvreg.h>
5028029b68SRuslan Bukin #include <machine/cpu.h>
5174ccb8ecSKonstantin Belousov #include <machine/cpufunc.h>
5228029b68SRuslan Bukin #include <machine/pcb.h>
5328029b68SRuslan Bukin #include <machine/frame.h>
545f8228b2SRuslan Bukin #include <machine/sbi.h>
5528029b68SRuslan Bukin
567bb4a84aSRuslan Bukin #if __riscv_xlen == 64
577bb4a84aSRuslan Bukin #define TP_OFFSET 16 /* sizeof(struct tcb) */
587bb4a84aSRuslan Bukin #endif
597bb4a84aSRuslan Bukin
6028029b68SRuslan Bukin /*
6128029b68SRuslan Bukin * Finish a fork operation, with process p2 nearly set up.
6228029b68SRuslan Bukin * Copy and update the pcb, set up the stack so that the child
6328029b68SRuslan Bukin * ready to run and return to user mode.
6428029b68SRuslan Bukin */
6528029b68SRuslan Bukin void
cpu_fork(struct thread * td1,struct proc * p2,struct thread * td2,int flags)6628029b68SRuslan Bukin cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
6728029b68SRuslan Bukin {
6828029b68SRuslan Bukin struct pcb *pcb2;
6928029b68SRuslan Bukin struct trapframe *tf;
7028029b68SRuslan Bukin
7128029b68SRuslan Bukin if ((flags & RFPROC) == 0)
7228029b68SRuslan Bukin return;
7328029b68SRuslan Bukin
747bb4a84aSRuslan Bukin /* RISCVTODO: save the FPU state here */
757bb4a84aSRuslan Bukin
7628029b68SRuslan Bukin pcb2 = (struct pcb *)(td2->td_kstack +
7728029b68SRuslan Bukin td2->td_kstack_pages * PAGE_SIZE) - 1;
7828029b68SRuslan Bukin
7928029b68SRuslan Bukin td2->td_pcb = pcb2;
8028029b68SRuslan Bukin bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
8128029b68SRuslan Bukin
8228029b68SRuslan Bukin tf = (struct trapframe *)STACKALIGN((struct trapframe *)pcb2 - 1);
8328029b68SRuslan Bukin bcopy(td1->td_frame, tf, sizeof(*tf));
8428029b68SRuslan Bukin
8528029b68SRuslan Bukin /* Clear syscall error flag */
8628029b68SRuslan Bukin tf->tf_t[0] = 0;
8728029b68SRuslan Bukin
8828029b68SRuslan Bukin /* Arguments for child */
8928029b68SRuslan Bukin tf->tf_a[0] = 0;
9028029b68SRuslan Bukin tf->tf_a[1] = 0;
917804dd52SRuslan Bukin tf->tf_sstatus |= (SSTATUS_SPIE); /* Enable interrupts. */
927804dd52SRuslan Bukin tf->tf_sstatus &= ~(SSTATUS_SPP); /* User mode. */
9328029b68SRuslan Bukin
9428029b68SRuslan Bukin td2->td_frame = tf;
9528029b68SRuslan Bukin
9628029b68SRuslan Bukin /* Set the return value registers for fork() */
97486ff498SRuslan Bukin td2->td_pcb->pcb_s[0] = (uintptr_t)fork_return;
98486ff498SRuslan Bukin td2->td_pcb->pcb_s[1] = (uintptr_t)td2;
9928029b68SRuslan Bukin td2->td_pcb->pcb_ra = (uintptr_t)fork_trampoline;
10028029b68SRuslan Bukin td2->td_pcb->pcb_sp = (uintptr_t)td2->td_frame;
10128029b68SRuslan Bukin
10228029b68SRuslan Bukin /* Setup to release spin count in fork_exit(). */
10328029b68SRuslan Bukin td2->td_md.md_spinlock_count = 1;
10498f50c44SRuslan Bukin td2->td_md.md_saved_sstatus_ie = (SSTATUS_SIE);
10528029b68SRuslan Bukin }
10628029b68SRuslan Bukin
10728029b68SRuslan Bukin void
cpu_reset(void)10828029b68SRuslan Bukin cpu_reset(void)
10928029b68SRuslan Bukin {
11028029b68SRuslan Bukin
1119bae4ce6SDanjel Qyteza sbi_system_reset(SBI_SRST_TYPE_COLD_REBOOT, SBI_SRST_REASON_NONE);
1125f8228b2SRuslan Bukin
1135f8228b2SRuslan Bukin while(1);
11428029b68SRuslan Bukin }
11528029b68SRuslan Bukin
11628029b68SRuslan Bukin void
cpu_thread_swapin(struct thread * td)11728029b68SRuslan Bukin cpu_thread_swapin(struct thread *td)
11828029b68SRuslan Bukin {
11928029b68SRuslan Bukin }
12028029b68SRuslan Bukin
12128029b68SRuslan Bukin void
cpu_thread_swapout(struct thread * td)12228029b68SRuslan Bukin cpu_thread_swapout(struct thread *td)
12328029b68SRuslan Bukin {
12428029b68SRuslan Bukin }
12528029b68SRuslan Bukin
12628029b68SRuslan Bukin void
cpu_set_syscall_retval(struct thread * td,int error)12728029b68SRuslan Bukin cpu_set_syscall_retval(struct thread *td, int error)
12828029b68SRuslan Bukin {
12928029b68SRuslan Bukin struct trapframe *frame;
13028029b68SRuslan Bukin
13128029b68SRuslan Bukin frame = td->td_frame;
13228029b68SRuslan Bukin
133b1497fb6SEdward Tomasz Napierala if (__predict_true(error == 0)) {
13428029b68SRuslan Bukin frame->tf_a[0] = td->td_retval[0];
13528029b68SRuslan Bukin frame->tf_a[1] = td->td_retval[1];
13628029b68SRuslan Bukin frame->tf_t[0] = 0; /* syscall succeeded */
137b1497fb6SEdward Tomasz Napierala return;
138b1497fb6SEdward Tomasz Napierala }
139b1497fb6SEdward Tomasz Napierala
140b1497fb6SEdward Tomasz Napierala switch (error) {
14128029b68SRuslan Bukin case ERESTART:
14228029b68SRuslan Bukin frame->tf_sepc -= 4; /* prev instruction */
14328029b68SRuslan Bukin break;
14428029b68SRuslan Bukin case EJUSTRETURN:
14528029b68SRuslan Bukin break;
14628029b68SRuslan Bukin default:
14728029b68SRuslan Bukin frame->tf_a[0] = error;
14828029b68SRuslan Bukin frame->tf_t[0] = 1; /* syscall error */
14928029b68SRuslan Bukin break;
15028029b68SRuslan Bukin }
15128029b68SRuslan Bukin }
15228029b68SRuslan Bukin
15328029b68SRuslan Bukin /*
1545c2cf818SKonstantin Belousov * Initialize machine state, mostly pcb and trap frame for a new
1555c2cf818SKonstantin Belousov * thread, about to return to userspace. Put enough state in the new
1565c2cf818SKonstantin Belousov * thread's PCB to get it to go back to the fork_return(), which
1575c2cf818SKonstantin Belousov * finalizes the thread state and handles peculiarities of the first
1585c2cf818SKonstantin Belousov * return to userspace for the new thread.
15928029b68SRuslan Bukin */
16028029b68SRuslan Bukin void
cpu_copy_thread(struct thread * td,struct thread * td0)1615c2cf818SKonstantin Belousov cpu_copy_thread(struct thread *td, struct thread *td0)
16228029b68SRuslan Bukin {
16328029b68SRuslan Bukin
16428029b68SRuslan Bukin bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
16528029b68SRuslan Bukin bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
16628029b68SRuslan Bukin
167486ff498SRuslan Bukin td->td_pcb->pcb_s[0] = (uintptr_t)fork_return;
168486ff498SRuslan Bukin td->td_pcb->pcb_s[1] = (uintptr_t)td;
16928029b68SRuslan Bukin td->td_pcb->pcb_ra = (uintptr_t)fork_trampoline;
17028029b68SRuslan Bukin td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
17128029b68SRuslan Bukin
17228029b68SRuslan Bukin /* Setup to release spin count in fork_exit(). */
17328029b68SRuslan Bukin td->td_md.md_spinlock_count = 1;
17498f50c44SRuslan Bukin td->td_md.md_saved_sstatus_ie = (SSTATUS_SIE);
17528029b68SRuslan Bukin }
17628029b68SRuslan Bukin
17728029b68SRuslan Bukin /*
1785c2cf818SKonstantin Belousov * Set that machine state for performing an upcall that starts
1795c2cf818SKonstantin Belousov * the entry function with the given argument.
18028029b68SRuslan Bukin */
1817b68fb5aSMark Johnston int
cpu_set_upcall(struct thread * td,void (* entry)(void *),void * arg,stack_t * stack)1825c2cf818SKonstantin Belousov cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
18328029b68SRuslan Bukin stack_t *stack)
18428029b68SRuslan Bukin {
185af19cc59SRuslan Bukin struct trapframe *tf;
186af19cc59SRuslan Bukin
187af19cc59SRuslan Bukin tf = td->td_frame;
18828029b68SRuslan Bukin
18928029b68SRuslan Bukin tf->tf_sp = STACKALIGN((uintptr_t)stack->ss_sp + stack->ss_size);
19028029b68SRuslan Bukin tf->tf_sepc = (register_t)entry;
19128029b68SRuslan Bukin tf->tf_a[0] = (register_t)arg;
1927b68fb5aSMark Johnston return (0);
19328029b68SRuslan Bukin }
19428029b68SRuslan Bukin
19528029b68SRuslan Bukin int
cpu_set_user_tls(struct thread * td,void * tls_base)19628029b68SRuslan Bukin cpu_set_user_tls(struct thread *td, void *tls_base)
19728029b68SRuslan Bukin {
19828029b68SRuslan Bukin
19928029b68SRuslan Bukin if ((uintptr_t)tls_base >= VM_MAXUSER_ADDRESS)
20028029b68SRuslan Bukin return (EINVAL);
20128029b68SRuslan Bukin
2026ae48dd8SMitchell Horne /*
2036ae48dd8SMitchell Horne * The user TLS is set by modifying the trapframe's tp value, which
2046ae48dd8SMitchell Horne * will be restored when returning to userspace.
2056ae48dd8SMitchell Horne */
2066ae48dd8SMitchell Horne td->td_frame->tf_tp = (register_t)tls_base + TP_OFFSET;
20728029b68SRuslan Bukin
20828029b68SRuslan Bukin return (0);
20928029b68SRuslan Bukin }
21028029b68SRuslan Bukin
21128029b68SRuslan Bukin void
cpu_thread_exit(struct thread * td)21228029b68SRuslan Bukin cpu_thread_exit(struct thread *td)
21328029b68SRuslan Bukin {
21428029b68SRuslan Bukin }
21528029b68SRuslan Bukin
21628029b68SRuslan Bukin void
cpu_thread_alloc(struct thread * td)21728029b68SRuslan Bukin cpu_thread_alloc(struct thread *td)
21828029b68SRuslan Bukin {
21928029b68SRuslan Bukin
22028029b68SRuslan Bukin td->td_pcb = (struct pcb *)(td->td_kstack +
22128029b68SRuslan Bukin td->td_kstack_pages * PAGE_SIZE) - 1;
22228029b68SRuslan Bukin td->td_frame = (struct trapframe *)STACKALIGN(
2234d50647dSRuslan Bukin (caddr_t)td->td_pcb - 8 - sizeof(struct trapframe));
22428029b68SRuslan Bukin }
22528029b68SRuslan Bukin
22628029b68SRuslan Bukin void
cpu_thread_free(struct thread * td)22728029b68SRuslan Bukin cpu_thread_free(struct thread *td)
22828029b68SRuslan Bukin {
22928029b68SRuslan Bukin }
23028029b68SRuslan Bukin
23128029b68SRuslan Bukin void
cpu_thread_clean(struct thread * td)23228029b68SRuslan Bukin cpu_thread_clean(struct thread *td)
23328029b68SRuslan Bukin {
23428029b68SRuslan Bukin }
23528029b68SRuslan Bukin
23628029b68SRuslan Bukin /*
23728029b68SRuslan Bukin * Intercept the return address from a freshly forked process that has NOT
23828029b68SRuslan Bukin * been scheduled yet.
23928029b68SRuslan Bukin *
24028029b68SRuslan Bukin * This is needed to make kernel threads stay in kernel mode.
24128029b68SRuslan Bukin */
24228029b68SRuslan Bukin void
cpu_fork_kthread_handler(struct thread * td,void (* func)(void *),void * arg)2435c2cf818SKonstantin Belousov cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
24428029b68SRuslan Bukin {
24528029b68SRuslan Bukin
246486ff498SRuslan Bukin td->td_pcb->pcb_s[0] = (uintptr_t)func;
247486ff498SRuslan Bukin td->td_pcb->pcb_s[1] = (uintptr_t)arg;
24828029b68SRuslan Bukin td->td_pcb->pcb_ra = (uintptr_t)fork_trampoline;
24928029b68SRuslan Bukin td->td_pcb->pcb_sp = (uintptr_t)td->td_frame;
25028029b68SRuslan Bukin }
25128029b68SRuslan Bukin
25228029b68SRuslan Bukin void
cpu_exit(struct thread * td)25328029b68SRuslan Bukin cpu_exit(struct thread *td)
25428029b68SRuslan Bukin {
25528029b68SRuslan Bukin }
25628029b68SRuslan Bukin
2576f1fe330SKonstantin Belousov bool
cpu_exec_vmspace_reuse(struct proc * p __unused,vm_map_t map __unused)2586f1fe330SKonstantin Belousov cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
2596f1fe330SKonstantin Belousov {
2606f1fe330SKonstantin Belousov
2616f1fe330SKonstantin Belousov return (true);
2626f1fe330SKonstantin Belousov }
2636f1fe330SKonstantin Belousov
264fd8d844fSKonstantin Belousov int
cpu_procctl(struct thread * td __unused,int idtype __unused,id_t id __unused,int com __unused,void * data __unused)265fd8d844fSKonstantin Belousov cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
266fd8d844fSKonstantin Belousov int com __unused, void *data __unused)
267fd8d844fSKonstantin Belousov {
268fd8d844fSKonstantin Belousov
269fd8d844fSKonstantin Belousov return (EINVAL);
270fd8d844fSKonstantin Belousov }
27174ccb8ecSKonstantin Belousov
27274ccb8ecSKonstantin Belousov void
cpu_sync_core(void)27374ccb8ecSKonstantin Belousov cpu_sync_core(void)
27474ccb8ecSKonstantin Belousov {
27574ccb8ecSKonstantin Belousov fence_i();
27674ccb8ecSKonstantin Belousov }
277