1*94483548Svisa /* $OpenBSD: sys_machdep.c,v 1.11 2019/12/20 13:34:41 visa Exp $ */
2f58c7388Spefo
3f58c7388Spefo /*
4f58c7388Spefo * Copyright (c) 1992, 1993
5f58c7388Spefo * The Regents of the University of California. All rights reserved.
6f58c7388Spefo *
7f58c7388Spefo * This code is derived from software contributed to Berkeley by
8f58c7388Spefo * Ralph Campbell.
9f58c7388Spefo *
10f58c7388Spefo * Redistribution and use in source and binary forms, with or without
11f58c7388Spefo * modification, are permitted provided that the following conditions
12f58c7388Spefo * are met:
13f58c7388Spefo * 1. Redistributions of source code must retain the above copyright
14f58c7388Spefo * notice, this list of conditions and the following disclaimer.
15f58c7388Spefo * 2. Redistributions in binary form must reproduce the above copyright
16f58c7388Spefo * notice, this list of conditions and the following disclaimer in the
17f58c7388Spefo * documentation and/or other materials provided with the distribution.
18f58c7388Spefo * 3. All advertising materials mentioning features or use of this software
19f58c7388Spefo * must display the following acknowledgement:
20f58c7388Spefo * This product includes software developed by the University of
21f58c7388Spefo * California, Berkeley and its contributors.
22f58c7388Spefo * 4. Neither the name of the University nor the names of its contributors
23f58c7388Spefo * may be used to endorse or promote products derived from this software
24f58c7388Spefo * without specific prior written permission.
25f58c7388Spefo *
26f58c7388Spefo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27f58c7388Spefo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28f58c7388Spefo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29f58c7388Spefo * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30f58c7388Spefo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31f58c7388Spefo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32f58c7388Spefo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33f58c7388Spefo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34f58c7388Spefo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35f58c7388Spefo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36f58c7388Spefo * SUCH DAMAGE.
37f58c7388Spefo *
38f58c7388Spefo * @(#)sys_machdep.c 8.1 (Berkeley) 6/10/93
39f58c7388Spefo */
40f58c7388Spefo
41f58c7388Spefo #include <sys/param.h>
42f58c7388Spefo #include <sys/systm.h>
43f58c7388Spefo #include <sys/ioctl.h>
44f58c7388Spefo #include <sys/time.h>
45f58c7388Spefo #include <sys/proc.h>
46f58c7388Spefo #include <sys/kernel.h>
47f58c7388Spefo #include <sys/buf.h>
48f58c7388Spefo
49f58c7388Spefo #include <sys/mount.h>
50f58c7388Spefo #include <sys/syscallargs.h>
51f58c7388Spefo
52a43a5a68Smpi #include <uvm/uvm_extern.h>
536029ab56Smiod
54737df64eSmiod #include <mips64/cache.h>
556029ab56Smiod #include <mips64/sysarch.h>
566029ab56Smiod
576029ab56Smiod #include <machine/autoconf.h>
586029ab56Smiod
596029ab56Smiod int mips64_cacheflush(struct proc *, struct mips64_cacheflush_args *);
606029ab56Smiod
61f58c7388Spefo int
sys_sysarch(struct proc * p,void * v,register_t * retval)620dfef174Svisa sys_sysarch(struct proc *p, void *v, register_t *retval)
63f58c7388Spefo {
64f58c7388Spefo struct sys_sysarch_args /* {
65f58c7388Spefo syscallarg(int) op;
66f58c7388Spefo syscallarg(char *) parms;
67f58c7388Spefo } */ *uap = v;
68f58c7388Spefo int error = 0;
69f58c7388Spefo
70f58c7388Spefo switch(SCARG(uap, op)) {
716029ab56Smiod case MIPS64_CACHEFLUSH:
726029ab56Smiod {
736029ab56Smiod struct mips64_cacheflush_args cfa;
746029ab56Smiod
756029ab56Smiod if ((error = copyin(SCARG(uap, parms), &cfa, sizeof cfa)) != 0)
766029ab56Smiod return error;
776029ab56Smiod error = mips64_cacheflush(p, &cfa);
786029ab56Smiod }
796029ab56Smiod break;
80f58c7388Spefo default:
81f58c7388Spefo error = EINVAL;
82f58c7388Spefo break;
83f58c7388Spefo }
846029ab56Smiod
856029ab56Smiod return error;
866029ab56Smiod }
876029ab56Smiod
886029ab56Smiod int
mips64_cacheflush(struct proc * p,struct mips64_cacheflush_args * cfa)896029ab56Smiod mips64_cacheflush(struct proc *p, struct mips64_cacheflush_args *cfa)
906029ab56Smiod {
916029ab56Smiod vaddr_t va;
926029ab56Smiod paddr_t pa;
936029ab56Smiod size_t sz, chunk;
946029ab56Smiod struct vm_map *map = &p->p_vmspace->vm_map;
956029ab56Smiod struct pmap *pm = map->pmap;
966029ab56Smiod struct vm_map_entry *entry;
976029ab56Smiod int rc = 0;
986029ab56Smiod
996029ab56Smiod /*
1006029ab56Smiod * Sanity checks.
1016029ab56Smiod */
1026029ab56Smiod if ((cfa->which & BCACHE) != cfa->which)
1036029ab56Smiod return EINVAL;
1046029ab56Smiod
1056029ab56Smiod if (cfa->which == 0)
1066029ab56Smiod return 0;
1076029ab56Smiod
1086029ab56Smiod va = cfa->va;
1096029ab56Smiod sz = cfa->sz;
1106029ab56Smiod chunk = PAGE_SIZE - (va & PAGE_MASK);
1116029ab56Smiod vm_map_lock_read(map);
1126029ab56Smiod if (va < vm_map_min(map) || va + sz > vm_map_max(map) || va + sz < va)
1136029ab56Smiod rc = EFAULT;
1146029ab56Smiod else while (sz != 0) {
1156029ab56Smiod if (chunk > sz)
1166029ab56Smiod chunk = sz;
1176029ab56Smiod
1186029ab56Smiod /*
1196029ab56Smiod * Check for a resident mapping first, this is faster than
1206029ab56Smiod * uvm_map_lookup_entry().
1216029ab56Smiod */
122*94483548Svisa if (pmap_extract(pm, va, &pa) != 0) {
1236029ab56Smiod if (cfa->which & ICACHE)
124c1805af1Smiod Mips_InvalidateICache(p->p_cpu, va, chunk);
1256029ab56Smiod if (cfa->which & DCACHE)
126b94f5bc5Smiod Mips_HitSyncDCache(p->p_cpu, va, chunk);
1276029ab56Smiod } else {
1286029ab56Smiod if (uvm_map_lookup_entry(map, va, &entry) == FALSE) {
1296029ab56Smiod rc = EFAULT;
1306029ab56Smiod break;
1316029ab56Smiod }
1326029ab56Smiod /* else simply not resident at the moment */
1336029ab56Smiod }
1346029ab56Smiod
1356029ab56Smiod va += chunk;
1366029ab56Smiod sz -= chunk;
1376029ab56Smiod chunk = PAGE_SIZE;
1386029ab56Smiod }
1396029ab56Smiod vm_map_unlock_read(map);
1406029ab56Smiod
1416029ab56Smiod return rc;
142f58c7388Spefo }
143