1 /*- 2 * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * Portions of this software were developed by SRI International and the 6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract 7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme. 8 * 9 * Portions of this software were developed by the University of Cambridge 10 * Computer Laboratory as part of the CTSRD Project, with support from the 11 * UK Higher Education Innovation Fund (HEIF). 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/limits.h> 41 #include <sys/proc.h> 42 #include <sys/sf_buf.h> 43 #include <sys/signal.h> 44 #include <sys/unistd.h> 45 46 #include <vm/vm.h> 47 #include <vm/vm_page.h> 48 #include <vm/vm_map.h> 49 #include <vm/uma.h> 50 #include <vm/uma_int.h> 51 52 #include <machine/riscvreg.h> 53 #include <machine/cpu.h> 54 #include <machine/pcb.h> 55 #include <machine/frame.h> 56 #include <machine/sbi.h> 57 58 /* 59 * Finish a fork operation, with process p2 nearly set up. 60 * Copy and update the pcb, set up the stack so that the child 61 * ready to run and return to user mode. 62 */ 63 void 64 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags) 65 { 66 struct pcb *pcb2; 67 struct trapframe *tf; 68 69 if ((flags & RFPROC) == 0) 70 return; 71 72 pcb2 = (struct pcb *)(td2->td_kstack + 73 td2->td_kstack_pages * PAGE_SIZE) - 1; 74 75 td2->td_pcb = pcb2; 76 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2)); 77 78 td2->td_pcb->pcb_l1addr = 79 vtophys(vmspace_pmap(td2->td_proc->p_vmspace)->pm_l1); 80 81 tf = (struct trapframe *)STACKALIGN((struct trapframe *)pcb2 - 1); 82 bcopy(td1->td_frame, tf, sizeof(*tf)); 83 84 /* Clear syscall error flag */ 85 tf->tf_t[0] = 0; 86 87 /* Arguments for child */ 88 tf->tf_a[0] = 0; 89 tf->tf_a[1] = 0; 90 tf->tf_sstatus |= (SSTATUS_SPIE); /* Enable interrupts. */ 91 tf->tf_sstatus |= (SSTATUS_SUM); /* Supervisor can access userspace. */ 92 tf->tf_sstatus &= ~(SSTATUS_SPP); /* User mode. */ 93 94 td2->td_frame = tf; 95 96 /* Set the return value registers for fork() */ 97 td2->td_pcb->pcb_s[0] = (uintptr_t)fork_return; 98 td2->td_pcb->pcb_s[1] = (uintptr_t)td2; 99 td2->td_pcb->pcb_ra = (uintptr_t)fork_trampoline; 100 td2->td_pcb->pcb_sp = (uintptr_t)td2->td_frame; 101 102 /* Setup to release spin count in fork_exit(). */ 103 td2->td_md.md_spinlock_count = 1; 104 td2->td_md.md_saved_sstatus_ie = (SSTATUS_SIE); 105 } 106 107 void 108 cpu_reset(void) 109 { 110 111 sbi_shutdown(); 112 113 while(1); 114 } 115 116 void 117 cpu_thread_swapin(struct thread *td) 118 { 119 } 120 121 void 122 cpu_thread_swapout(struct thread *td) 123 { 124 } 125 126 void 127 cpu_set_syscall_retval(struct thread *td, int error) 128 { 129 struct trapframe *frame; 130 131 frame = td->td_frame; 132 133 switch (error) { 134 case 0: 135 frame->tf_a[0] = td->td_retval[0]; 136 frame->tf_a[1] = td->td_retval[1]; 137 frame->tf_t[0] = 0; /* syscall succeeded */ 138 break; 139 case ERESTART: 140 frame->tf_sepc -= 4; /* prev instruction */ 141 break; 142 case EJUSTRETURN: 143 break; 144 default: 145 frame->tf_a[0] = error; 146 frame->tf_t[0] = 1; /* syscall error */ 147 break; 148 } 149 } 150 151 /* 152 * Initialize machine state, mostly pcb and trap frame for a new 153 * thread, about to return to userspace. Put enough state in the new 154 * thread's PCB to get it to go back to the fork_return(), which 155 * finalizes the thread state and handles peculiarities of the first 156 * return to userspace for the new thread. 157 */ 158 void 159 cpu_copy_thread(struct thread *td, struct thread *td0) 160 { 161 162 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe)); 163 bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb)); 164 165 td->td_pcb->pcb_s[0] = (uintptr_t)fork_return; 166 td->td_pcb->pcb_s[1] = (uintptr_t)td; 167 td->td_pcb->pcb_ra = (uintptr_t)fork_trampoline; 168 td->td_pcb->pcb_sp = (uintptr_t)td->td_frame; 169 170 /* Setup to release spin count in fork_exit(). */ 171 td->td_md.md_spinlock_count = 1; 172 td->td_md.md_saved_sstatus_ie = (SSTATUS_SIE); 173 } 174 175 /* 176 * Set that machine state for performing an upcall that starts 177 * the entry function with the given argument. 178 */ 179 void 180 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg, 181 stack_t *stack) 182 { 183 struct trapframe *tf; 184 185 tf = td->td_frame; 186 187 tf->tf_sp = STACKALIGN((uintptr_t)stack->ss_sp + stack->ss_size); 188 tf->tf_sepc = (register_t)entry; 189 tf->tf_a[0] = (register_t)arg; 190 } 191 192 int 193 cpu_set_user_tls(struct thread *td, void *tls_base) 194 { 195 struct pcb *pcb; 196 197 if ((uintptr_t)tls_base >= VM_MAXUSER_ADDRESS) 198 return (EINVAL); 199 200 pcb = td->td_pcb; 201 pcb->pcb_tp = (register_t)tls_base; 202 203 return (0); 204 } 205 206 void 207 cpu_thread_exit(struct thread *td) 208 { 209 } 210 211 void 212 cpu_thread_alloc(struct thread *td) 213 { 214 215 td->td_pcb = (struct pcb *)(td->td_kstack + 216 td->td_kstack_pages * PAGE_SIZE) - 1; 217 td->td_frame = (struct trapframe *)STACKALIGN( 218 (caddr_t)td->td_pcb - 8 - sizeof(struct trapframe)); 219 } 220 221 void 222 cpu_thread_free(struct thread *td) 223 { 224 } 225 226 void 227 cpu_thread_clean(struct thread *td) 228 { 229 } 230 231 /* 232 * Intercept the return address from a freshly forked process that has NOT 233 * been scheduled yet. 234 * 235 * This is needed to make kernel threads stay in kernel mode. 236 */ 237 void 238 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg) 239 { 240 241 td->td_pcb->pcb_s[0] = (uintptr_t)func; 242 td->td_pcb->pcb_s[1] = (uintptr_t)arg; 243 td->td_pcb->pcb_ra = (uintptr_t)fork_trampoline; 244 td->td_pcb->pcb_sp = (uintptr_t)td->td_frame; 245 } 246 247 void 248 cpu_exit(struct thread *td) 249 { 250 } 251 252 void 253 swi_vm(void *v) 254 { 255 256 /* Nothing to do here - busdma bounce buffers are not implemented. */ 257 } 258