Home | History | Annotate | Line # | Download | only in m68k
      1 /*	$NetBSD: vm_machdep.c,v 1.48 2026/08/30 00:46:50 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988 University of Utah.
      5  * Copyright (c) 1982, 1986, 1990, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * the Systems Programming Group of the University of Utah Computer
     10  * Science Department.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  * from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$
     37  *
     38  *	@(#)vm_machdep.c	8.6 (Berkeley) 1/12/94
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.48 2026/08/30 00:46:50 thorpej Exp $");
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/proc.h>
     47 #include <sys/cpu.h>
     48 #include <sys/buf.h>
     49 #include <sys/vnode.h>
     50 #include <sys/core.h>
     51 #include <sys/exec.h>
     52 
     53 #include <machine/frame.h>
     54 #include <machine/pcb.h>
     55 
     56 #include <uvm/uvm_extern.h>
     57 
     58 struct vm_map *phys_map;
     59 
     60 __CTASSERT(USPACE >= PAGE_SIZE);
     61 __CTASSERT((USPACE & PAGE_MASK) == 0);
     62 
     63 void
     64 cpu_proc_fork(struct proc *p1, struct proc *p2)
     65 {
     66 
     67 	p2->p_md.mdp_flags = p1->p_md.mdp_flags;
     68 }
     69 
     70 /*
     71  * Finish a fork operation, with process l2 nearly set up.
     72  * Copy and update the pcb and trap frame, making the child ready to run.
     73  *
     74  * Rig the child's kernel stack so that it will start out in
     75  * lwp_trampoline() and call child_return() with l2 as an
     76  * argument. This causes the newly-created child process to go
     77  * directly to user level with an apparent return value of 0 from
     78  * fork(), while the parent process returns normally.
     79  *
     80  * l1 is the process being forked; if l1 == &lwp0, we are creating
     81  * a kernel thread, and the return path and argument are specified with
     82  * `func' and `arg'.
     83  *
     84  * If an alternate user-level stack is requested (with non-zero values
     85  * in both the stack and stacksize args), set up the user stack pointer
     86  * accordingly.
     87  */
     88 void
     89 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
     90     void (*func)(void *), void *arg)
     91 {
     92 	struct pcb *pcb1, *pcb2;
     93 	struct trapframe *tf;
     94 	struct switchframe *sf;
     95 
     96 	pcb1 = lwp_getpcb(l1);
     97 	pcb2 = lwp_getpcb(l2);
     98 
     99 	l2->l_md.md_flags = l1->l_md.md_flags;
    100 
    101 	/* Copy pcb from lwp l1 to l2. */
    102 	if (l1 == curlwp) {
    103 		/* Sync the PCB before we copy it. */
    104 		savectx(curpcb);
    105 	} else {
    106 		KASSERT(l1 == &lwp0);
    107 	}
    108 
    109 	*pcb2 = *pcb1;
    110 
    111 	/*
    112 	 * Copy the trap frame.
    113 	 */
    114 	tf = (struct trapframe *)(uvm_lwp_getuarea(l2) + USPACE) - 1;
    115 	l2->l_md.md_regs = (int *)tf;
    116 	*tf = *(struct trapframe *)l1->l_md.md_regs;
    117 
    118 	/*
    119 	 * If specified, give the child a different stack.
    120 	 */
    121 	if (stack != NULL) {
    122 		tf->tf_regs[15] =
    123 		    ((uintptr_t)stack + stacksize) & ~STACK_ALIGNBYTES;
    124 	}
    125 
    126 	sf = (struct switchframe *)tf - 1;
    127 	sf->sf_pc = (u_int)lwp_trampoline;
    128 	pcb2->pcb_regs[6] = (int)func;		/* A2 */
    129 	pcb2->pcb_regs[7] = (int)arg;		/* A3 */
    130 	pcb2->pcb_regs[8] = (int)l2;		/* A4 */
    131 	pcb2->pcb_regs[11] = (int)sf;		/* SSP */
    132 	pcb2->pcb_ps = PSL_LOWIPL;		/* start kthreads at IPL 0 */
    133 }
    134 
    135 void
    136 cpu_lwp_free(struct lwp *l, int proc)
    137 {
    138 
    139 	/* Nothing to do */
    140 }
    141 
    142 void
    143 cpu_lwp_free2(struct lwp *l)
    144 {
    145 
    146 	/* Nothing to do */
    147 }
    148 
    149 /*
    150  * Ensure phys_map is large enough for at least 2 concurrent
    151  * MAXPHYS transfers.
    152  */
    153 __CTASSERT((MAXPHYS * 2) <= VM_PHYS_SIZE);
    154 
    155 /*
    156  * Map a user I/O request into kernel virtual address space.
    157  * Note: the pages are already locked by uvm_vslock(), so we
    158  * do not need to pass an access_type to pmap_enter().
    159  */
    160 int
    161 vmapbuf(struct buf *bp, vsize_t len)
    162 {
    163 	struct pmap *upmap, *kpmap __unused;
    164 	vaddr_t uva;		/* User VA (map from) */
    165 	vaddr_t kva;		/* Kernel VA (new to) */
    166 	paddr_t pa;		/* physical address */
    167 	vsize_t off;
    168 
    169 	if ((bp->b_flags & B_PHYS) == 0)
    170 		panic("vmapbuf");
    171 
    172 	uva = m68k_trunc_page(bp->b_saveaddr = bp->b_data);
    173 	off = (vaddr_t)bp->b_data - uva;
    174 	len = m68k_round_page(off + len);
    175 	kva = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
    176 	bp->b_data = (void *)(kva + off);
    177 
    178 	upmap = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);
    179 	kpmap = vm_map_pmap(phys_map);
    180 	do {
    181 		if (pmap_extract(upmap, uva, &pa) == false)
    182 			panic("vmapbuf: null page frame");
    183 #ifdef M68K_VAC
    184 		pmap_enter(kpmap, kva, pa, VM_PROT_READ | VM_PROT_WRITE,
    185 		    PMAP_WIRED);
    186 #else
    187 		pmap_kenter_pa(kva, pa, VM_PROT_READ | VM_PROT_WRITE, 0);
    188 #endif
    189 		uva += PAGE_SIZE;
    190 		kva += PAGE_SIZE;
    191 		len -= PAGE_SIZE;
    192 	} while (len);
    193 	pmap_update(kpmap);
    194 
    195 	return 0;
    196 }
    197 
    198 /*
    199  * Unmap a previously-mapped user I/O request.
    200  */
    201 void
    202 vunmapbuf(struct buf *bp, vsize_t len)
    203 {
    204 	vaddr_t kva;
    205 	vsize_t off;
    206 
    207 	if ((bp->b_flags & B_PHYS) == 0)
    208 		panic("vunmapbuf");
    209 
    210 	kva = m68k_trunc_page(bp->b_data);
    211 	off = (vaddr_t)bp->b_data - kva;
    212 	len = m68k_round_page(off + len);
    213 
    214 #ifdef M68K_VAC
    215 	pmap_remove(vm_map_pmap(phys_map), kva, kva + len);
    216 #else
    217 	pmap_kremove(kva, len);
    218 #endif
    219 	pmap_update(pmap_kernel());
    220 	uvm_km_free(phys_map, kva, len, UVM_KMF_VAONLY);
    221 	bp->b_data = bp->b_saveaddr;
    222 	bp->b_saveaddr = 0;
    223 }
    224