1 /* $NetBSD: mem.c,v 1.25 2008/11/18 09:52:43 nonaka Exp $ */
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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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
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>
84 #include <sys/systm.h>
86 #include <sys/malloc.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 */
100 dev_type_ioctl(mmioctl
);
101 dev_type_mmap(mmmmap
);
103 const struct cdevsw mem_cdevsw
= {
105 .d_close
= nullclose
,
113 .d_kqfilter
= nokqfilter
,
119 mmrw(dev_t dev
, struct uio
*uio
, int flags
)
127 while (uio
->uio_resid
> 0 && error
== 0) {
129 if (iov
->iov_len
== 0) {
132 if (uio
->uio_iovcnt
< 0)
136 switch (minor(dev
)) {
140 prot
= uio
->uio_rw
== UIO_READ
? VM_PROT_READ
:
143 #ifdef PMAP_CACHE_VIPT
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
;
151 m
+= o
& arm_cache_prefer_mask
;
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
);
168 c
= min(iov
->iov_len
, MAXPHYS
);
169 if (!uvm_kernacc((void *)v
, c
,
170 uio
->uio_rw
== UIO_READ
? B_READ
: B_WRITE
))
172 error
= uiomove((void *)v
, c
, uio
);
176 if (uio
->uio_rw
== UIO_WRITE
)
184 if (uio
->uio_rw
== UIO_WRITE
) {
188 c
= min(iov
->iov_len
, PAGE_SIZE
);
189 error
= uiomove(zeropage
, c
, uio
);
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
212 if (minor(dev
) != DEV_MEM
)
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)
220 return arm_btop(off
);