No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / arm / arm32 / mem.c
blob11f199d6c7dbb119bfb3c1d8fc61a8714cf78fb6
1 /* $NetBSD: mem.c,v 1.25 2008/11/18 09:52:43 nonaka Exp $ */
3 /*
4 * Copyright (c) 1982, 1986, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 * Copyright (c) 1988 University of Utah.
38 * This code is derived from software contributed to Berkeley by
39 * the Systems Programming Group of the University of Utah Computer
40 * Science Department.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
72 * Memory special file
75 #include "opt_arm32_pmap.h"
76 #include "opt_compat_netbsd.h"
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: mem.c,v 1.25 2008/11/18 09:52:43 nonaka Exp $");
81 #include <sys/param.h>
82 #include <sys/conf.h>
83 #include <sys/buf.h>
84 #include <sys/systm.h>
85 #include <sys/uio.h>
86 #include <sys/malloc.h>
87 #include <sys/proc.h>
88 #include <sys/fcntl.h>
89 #include <sys/kauth.h>
91 #include <machine/cpu.h>
93 #include <uvm/uvm_extern.h>
95 extern vaddr_t memhook; /* in pmap.c (poor name!) */
96 extern kmutex_t memlock; /* in pmap.c */
97 extern void *zeropage; /* in pmap.c */
99 dev_type_read(mmrw);
100 dev_type_ioctl(mmioctl);
101 dev_type_mmap(mmmmap);
103 const struct cdevsw mem_cdevsw = {
104 .d_open = nullopen,
105 .d_close = nullclose,
106 .d_read = mmrw,
107 .d_write = mmrw,
108 .d_ioctl = mmioctl,
109 .d_stop = nostop,
110 .d_tty = notty,
111 .d_poll = nopoll,
112 .d_mmap = mmmmap,
113 .d_kqfilter = nokqfilter,
114 .d_flag = D_MPSAFE
117 /*ARGSUSED*/
119 mmrw(dev_t dev, struct uio *uio, int flags)
121 vaddr_t o, v, m;
122 int c;
123 struct iovec *iov;
124 int error = 0;
125 vm_prot_t prot;
127 while (uio->uio_resid > 0 && error == 0) {
128 iov = uio->uio_iov;
129 if (iov->iov_len == 0) {
130 uio->uio_iov++;
131 uio->uio_iovcnt--;
132 if (uio->uio_iovcnt < 0)
133 panic("mmrw");
134 continue;
136 switch (minor(dev)) {
138 case DEV_MEM:
139 v = uio->uio_offset;
140 prot = uio->uio_rw == UIO_READ ? VM_PROT_READ :
141 VM_PROT_WRITE;
142 m = memhook;
143 #ifdef PMAP_CACHE_VIPT
145 struct vm_page *pg;
146 pg = PHYS_TO_VM_PAGE(trunc_page(v));
147 if (pg != NULL && pmap_is_page_colored_p(pg))
148 o = pg->mdpage.pvh_attrs;
149 else
150 o = v;
151 m += o & arm_cache_prefer_mask;
153 #endif
154 mutex_enter(&memlock);
155 pmap_enter(pmap_kernel(), m,
156 trunc_page(v), prot, prot|PMAP_WIRED);
157 pmap_update(pmap_kernel());
158 o = uio->uio_offset & PGOFSET;
159 c = min(uio->uio_resid, (int)(PAGE_SIZE - o));
160 error = uiomove((char *)m + o, c, uio);
161 pmap_remove(pmap_kernel(), m, m + PAGE_SIZE);
162 pmap_update(pmap_kernel());
163 mutex_exit(&memlock);
164 break;
166 case DEV_KMEM:
167 v = uio->uio_offset;
168 c = min(iov->iov_len, MAXPHYS);
169 if (!uvm_kernacc((void *)v, c,
170 uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
171 return (EFAULT);
172 error = uiomove((void *)v, c, uio);
173 break;
175 case DEV_NULL:
176 if (uio->uio_rw == UIO_WRITE)
177 uio->uio_resid = 0;
178 return (0);
180 #ifdef COMPAT_16
181 case _DEV_ZERO_oARM:
182 #endif
183 case DEV_ZERO:
184 if (uio->uio_rw == UIO_WRITE) {
185 uio->uio_resid = 0;
186 return (0);
188 c = min(iov->iov_len, PAGE_SIZE);
189 error = uiomove(zeropage, c, uio);
190 break;
192 default:
193 return (ENXIO);
196 return (error);
199 paddr_t
200 mmmmap(dev_t dev, off_t off, int prot)
202 struct lwp *l = curlwp; /* XXX */
205 * /dev/mem is the only one that makes sense through this
206 * interface. For /dev/kmem any physaddr we return here
207 * could be transient and hence incorrect or invalid at
208 * a later time. /dev/null just doesn't make any sense
209 * and /dev/zero is a hack that is handled via the default
210 * pager in mmap().
212 if (minor(dev) != DEV_MEM)
213 return (-1);
215 /* minor device 0 is physical memory */
217 if (off >= ctob(physmem) && kauth_authorize_machdep(l->l_cred,
218 KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0)
219 return -1;
220 return arm_btop(off);