PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/freebsd/sys/powerpc/powerpc/mem.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 330 lines | 220 code | 43 blank | 67 comment | 52 complexity | 0cf09ae5ff5bfa7d95866432698d0b9e MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*-
  2. * Copyright (c) 1988 University of Utah.
  3. * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * the Systems Programming Group of the University of Utah Computer
  8. * Science Department, and code derived from software contributed to
  9. * Berkeley by William Jolitz.
  10. *
  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. * 4. 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.
  22. *
  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.
  34. *
  35. * from: Utah $Hdr: mem.c 1.13 89/10/08$
  36. * from: @(#)mem.c 7.2 (Berkeley) 5/9/91
  37. */
  38. #include <sys/cdefs.h>
  39. __FBSDID("$FreeBSD$");
  40. /*
  41. * Memory special file
  42. */
  43. #include <sys/param.h>
  44. #include <sys/conf.h>
  45. #include <sys/fcntl.h>
  46. #include <sys/kernel.h>
  47. #include <sys/lock.h>
  48. #include <sys/ioccom.h>
  49. #include <sys/malloc.h>
  50. #include <sys/memrange.h>
  51. #include <sys/module.h>
  52. #include <sys/mutex.h>
  53. #include <sys/proc.h>
  54. #include <sys/msgbuf.h>
  55. #include <sys/systm.h>
  56. #include <sys/signalvar.h>
  57. #include <sys/uio.h>
  58. #include <machine/md_var.h>
  59. #include <machine/vmparam.h>
  60. #include <vm/vm.h>
  61. #include <vm/pmap.h>
  62. #include <vm/vm_extern.h>
  63. #include <vm/vm_page.h>
  64. #include <machine/memdev.h>
  65. static void ppc_mrinit(struct mem_range_softc *);
  66. static int ppc_mrset(struct mem_range_softc *, struct mem_range_desc *, int *);
  67. MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
  68. static struct mem_range_ops ppc_mem_range_ops = {
  69. ppc_mrinit,
  70. ppc_mrset,
  71. NULL,
  72. NULL
  73. };
  74. struct mem_range_softc mem_range_softc = {
  75. &ppc_mem_range_ops,
  76. 0, 0, NULL
  77. };
  78. /* ARGSUSED */
  79. int
  80. memrw(struct cdev *dev, struct uio *uio, int flags)
  81. {
  82. struct iovec *iov;
  83. int error = 0;
  84. vm_offset_t va, eva, off, v;
  85. vm_prot_t prot;
  86. struct vm_page m;
  87. vm_page_t marr;
  88. vm_size_t cnt;
  89. cnt = 0;
  90. error = 0;
  91. GIANT_REQUIRED;
  92. while (uio->uio_resid > 0 && !error) {
  93. iov = uio->uio_iov;
  94. if (iov->iov_len == 0) {
  95. uio->uio_iov++;
  96. uio->uio_iovcnt--;
  97. if (uio->uio_iovcnt < 0)
  98. panic("memrw");
  99. continue;
  100. }
  101. if (dev2unit(dev) == CDEV_MINOR_MEM) {
  102. kmem_direct_mapped: v = uio->uio_offset;
  103. off = uio->uio_offset & PAGE_MASK;
  104. cnt = PAGE_SIZE - ((vm_offset_t)iov->iov_base &
  105. PAGE_MASK);
  106. cnt = min(cnt, PAGE_SIZE - off);
  107. cnt = min(cnt, iov->iov_len);
  108. if (mem_valid(v, cnt)) {
  109. error = EFAULT;
  110. break;
  111. }
  112. if (!pmap_dev_direct_mapped(v, cnt)) {
  113. error = uiomove((void *)v, cnt, uio);
  114. } else {
  115. m.phys_addr = trunc_page(v);
  116. marr = &m;
  117. error = uiomove_fromphys(&marr, off, cnt, uio);
  118. }
  119. }
  120. else if (dev2unit(dev) == CDEV_MINOR_KMEM) {
  121. va = uio->uio_offset;
  122. if ((va < VM_MIN_KERNEL_ADDRESS) || (va > virtual_end))
  123. goto kmem_direct_mapped;
  124. va = trunc_page(uio->uio_offset);
  125. eva = round_page(uio->uio_offset
  126. + iov->iov_len);
  127. /*
  128. * Make sure that all the pages are currently resident
  129. * so that we don't create any zero-fill pages.
  130. */
  131. for (; va < eva; va += PAGE_SIZE)
  132. if (pmap_extract(kernel_pmap, va) == 0)
  133. return (EFAULT);
  134. prot = (uio->uio_rw == UIO_READ)
  135. ? VM_PROT_READ : VM_PROT_WRITE;
  136. va = uio->uio_offset;
  137. if (kernacc((void *) va, iov->iov_len, prot)
  138. == FALSE)
  139. return (EFAULT);
  140. error = uiomove((void *)va, iov->iov_len, uio);
  141. continue;
  142. }
  143. }
  144. return (error);
  145. }
  146. /*
  147. * allow user processes to MMAP some memory sections
  148. * instead of going through read/write
  149. */
  150. int
  151. memmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
  152. int prot, vm_memattr_t *memattr)
  153. {
  154. int i;
  155. /*
  156. * /dev/mem is the only one that makes sense through this
  157. * interface. For /dev/kmem any physaddr we return here
  158. * could be transient and hence incorrect or invalid at
  159. * a later time.
  160. */
  161. if (dev2unit(dev) != CDEV_MINOR_MEM)
  162. return (-1);
  163. /* Only direct-mapped addresses. */
  164. if (mem_valid(offset, 0)
  165. && pmap_dev_direct_mapped(offset, 0))
  166. return (EFAULT);
  167. *paddr = offset;
  168. for (i = 0; i < mem_range_softc.mr_ndesc; i++) {
  169. if (!(mem_range_softc.mr_desc[i].mr_flags & MDF_ACTIVE))
  170. continue;
  171. if (offset >= mem_range_softc.mr_desc[i].mr_base &&
  172. offset < mem_range_softc.mr_desc[i].mr_base +
  173. mem_range_softc.mr_desc[i].mr_len) {
  174. switch (mem_range_softc.mr_desc[i].mr_flags &
  175. MDF_ATTRMASK) {
  176. case MDF_WRITEBACK:
  177. *memattr = VM_MEMATTR_WRITE_BACK;
  178. break;
  179. case MDF_WRITECOMBINE:
  180. *memattr = VM_MEMATTR_WRITE_COMBINING;
  181. break;
  182. case MDF_UNCACHEABLE:
  183. *memattr = VM_MEMATTR_UNCACHEABLE;
  184. break;
  185. case MDF_WRITETHROUGH:
  186. *memattr = VM_MEMATTR_WRITE_THROUGH;
  187. break;
  188. }
  189. break;
  190. }
  191. }
  192. return (0);
  193. }
  194. static void
  195. ppc_mrinit(struct mem_range_softc *sc)
  196. {
  197. sc->mr_cap = 0;
  198. sc->mr_ndesc = 8; /* XXX: Should be dynamically expandable */
  199. sc->mr_desc = malloc(sc->mr_ndesc * sizeof(struct mem_range_desc),
  200. M_MEMDESC, M_NOWAIT | M_ZERO);
  201. if (sc->mr_desc == NULL)
  202. panic("%s: malloc returns NULL", __func__);
  203. }
  204. static int
  205. ppc_mrset(struct mem_range_softc *sc, struct mem_range_desc *desc, int *arg)
  206. {
  207. int i;
  208. switch(*arg) {
  209. case MEMRANGE_SET_UPDATE:
  210. for (i = 0; i < sc->mr_ndesc; i++) {
  211. if (!sc->mr_desc[i].mr_len) {
  212. sc->mr_desc[i] = *desc;
  213. sc->mr_desc[i].mr_flags |= MDF_ACTIVE;
  214. return (0);
  215. }
  216. if (sc->mr_desc[i].mr_base == desc->mr_base &&
  217. sc->mr_desc[i].mr_len == desc->mr_len)
  218. return (EEXIST);
  219. }
  220. return (ENOSPC);
  221. case MEMRANGE_SET_REMOVE:
  222. for (i = 0; i < sc->mr_ndesc; i++)
  223. if (sc->mr_desc[i].mr_base == desc->mr_base &&
  224. sc->mr_desc[i].mr_len == desc->mr_len) {
  225. bzero(&sc->mr_desc[i], sizeof(sc->mr_desc[i]));
  226. return (0);
  227. }
  228. return (ENOENT);
  229. default:
  230. return (EOPNOTSUPP);
  231. }
  232. return (0);
  233. }
  234. /*
  235. * Operations for changing memory attributes.
  236. *
  237. * This is basically just an ioctl shim for mem_range_attr_get
  238. * and mem_range_attr_set.
  239. */
  240. /* ARGSUSED */
  241. int
  242. memioctl(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags,
  243. struct thread *td)
  244. {
  245. int nd, error = 0;
  246. struct mem_range_op *mo = (struct mem_range_op *)data;
  247. struct mem_range_desc *md;
  248. /* is this for us? */
  249. if ((cmd != MEMRANGE_GET) &&
  250. (cmd != MEMRANGE_SET))
  251. return (ENOTTY);
  252. /* any chance we can handle this? */
  253. if (mem_range_softc.mr_op == NULL)
  254. return (EOPNOTSUPP);
  255. /* do we have any descriptors? */
  256. if (mem_range_softc.mr_ndesc == 0)
  257. return (ENXIO);
  258. switch (cmd) {
  259. case MEMRANGE_GET:
  260. nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
  261. if (nd > 0) {
  262. md = (struct mem_range_desc *)
  263. malloc(nd * sizeof(struct mem_range_desc),
  264. M_MEMDESC, M_WAITOK);
  265. error = mem_range_attr_get(md, &nd);
  266. if (!error)
  267. error = copyout(md, mo->mo_desc,
  268. nd * sizeof(struct mem_range_desc));
  269. free(md, M_MEMDESC);
  270. }
  271. else
  272. nd = mem_range_softc.mr_ndesc;
  273. mo->mo_arg[0] = nd;
  274. break;
  275. case MEMRANGE_SET:
  276. md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
  277. M_MEMDESC, M_WAITOK);
  278. error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
  279. /* clamp description string */
  280. md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
  281. if (error == 0)
  282. error = mem_range_attr_set(md, &mo->mo_arg[0]);
  283. free(md, M_MEMDESC);
  284. break;
  285. }
  286. return (error);
  287. }